@wichayutdew/pi-workflows 0.3.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +55 -39
  2. package/dist/index.js +5771 -4794
  3. package/examples/mr-comments.workflow.yaml +2 -0
  4. package/package.json +1 -1
  5. package/schemas/workflow.schema.json +10 -1
  6. package/src/command-names.ts +7 -1
  7. package/src/commands.ts +126 -52
  8. package/src/config/catalog.ts +162 -0
  9. package/src/config/ceiling.ts +120 -116
  10. package/src/config/command-conflicts.ts +4 -4
  11. package/src/config/diagnostics.ts +28 -0
  12. package/src/config/load-settings.ts +37 -0
  13. package/src/config/load-types.ts +53 -0
  14. package/src/config/load-workflows.ts +135 -0
  15. package/src/config/load.ts +43 -311
  16. package/src/config/types.ts +150 -137
  17. package/src/config/validate.ts +12 -1261
  18. package/src/config/validation/permissions.ts +312 -0
  19. package/src/config/validation/prompt.ts +21 -0
  20. package/src/config/validation/settings.ts +122 -0
  21. package/src/config/validation/shared.ts +129 -0
  22. package/src/config/validation/shortcut.ts +118 -0
  23. package/src/config/validation/step.ts +228 -0
  24. package/src/config/validation/subagent.ts +288 -0
  25. package/src/config/validation/workflow.ts +156 -0
  26. package/src/config/yaml.ts +29 -0
  27. package/src/digest.ts +38 -6
  28. package/src/engine/checkpoint.ts +13 -9
  29. package/src/engine/create-run.ts +43 -0
  30. package/src/engine/gate-transitions.ts +187 -0
  31. package/src/engine/reconciliation-history.ts +88 -0
  32. package/src/engine/resume.ts +19 -7
  33. package/src/engine/run-advance.ts +122 -0
  34. package/src/engine/run-lifecycle.ts +124 -0
  35. package/src/engine/run-reconciliation.ts +116 -0
  36. package/src/engine/run-validation.ts +129 -0
  37. package/src/engine/state-types.ts +66 -0
  38. package/src/engine/state.ts +10 -193
  39. package/src/engine/transition-helpers.ts +28 -0
  40. package/src/engine/transition-types.ts +8 -0
  41. package/src/engine/transitions.ts +17 -471
  42. package/src/harness/action-context.ts +181 -0
  43. package/src/harness/catalog.ts +45 -0
  44. package/src/harness/context-idle.ts +21 -0
  45. package/src/harness/core-actions.ts +299 -0
  46. package/src/harness/delegation-control-actions.ts +250 -0
  47. package/src/harness/delegation-failure.ts +215 -0
  48. package/src/harness/delegation-plan.ts +219 -0
  49. package/src/harness/delegation-recovery-validation.ts +159 -0
  50. package/src/harness/delegation-response-actions.ts +295 -0
  51. package/src/harness/delegation-retry-policy.ts +118 -0
  52. package/src/harness/dependencies.ts +165 -0
  53. package/src/harness/gate-submission-action.ts +154 -0
  54. package/src/harness/lifecycle-actions.ts +154 -0
  55. package/src/harness/pause-actions.ts +120 -0
  56. package/src/harness/plannotator-result-actions.ts +123 -0
  57. package/src/harness/prompt-gate-actions.ts +270 -0
  58. package/src/harness/resume-action.ts +261 -0
  59. package/src/harness/start-actions.ts +183 -0
  60. package/src/harness/status-actions.ts +191 -0
  61. package/src/harness/step-execution-actions.ts +283 -0
  62. package/src/harness/types.ts +76 -0
  63. package/src/harness.ts +236 -2303
  64. package/src/index.ts +73 -11
  65. package/src/integrations/plannotator-requests.ts +127 -0
  66. package/src/integrations/plannotator-responses.ts +141 -0
  67. package/src/integrations/plannotator-types.ts +44 -0
  68. package/src/integrations/plannotator.ts +16 -235
  69. package/src/integrations/prompt-gate.ts +17 -10
  70. package/src/integrations/subagents/child-policy-envelope.ts +119 -0
  71. package/src/integrations/subagents/child-policy-paths.ts +66 -0
  72. package/src/integrations/subagents/child-policy-sections.ts +186 -0
  73. package/src/integrations/subagents/child-policy-types.ts +32 -0
  74. package/src/integrations/subagents/child-policy-validation.ts +163 -0
  75. package/src/integrations/subagents/child-runtime-completion.ts +56 -0
  76. package/src/integrations/subagents/child-runtime-dependencies.ts +47 -0
  77. package/src/integrations/subagents/child-runtime-files.ts +156 -0
  78. package/src/integrations/subagents/child-runtime-policy.ts +83 -0
  79. package/src/integrations/subagents/child-runtime-types.ts +30 -0
  80. package/src/integrations/subagents/child-runtime.ts +140 -301
  81. package/src/integrations/subagents/client-delegation.ts +181 -0
  82. package/src/integrations/subagents/client-messages.ts +66 -0
  83. package/src/integrations/subagents/client-types.ts +36 -0
  84. package/src/integrations/subagents/client.ts +127 -219
  85. package/src/integrations/subagents/delegated-result.ts +31 -0
  86. package/src/integrations/subagents/diagnostic-format.ts +40 -0
  87. package/src/integrations/subagents/diagnostic-text.ts +114 -0
  88. package/src/integrations/subagents/diagnostic-types.ts +77 -0
  89. package/src/integrations/subagents/diagnostics.ts +24 -977
  90. package/src/integrations/subagents/failure-correlation.ts +235 -0
  91. package/src/integrations/subagents/failure-transcript.ts +192 -0
  92. package/src/integrations/subagents/hidden-bash-failure.ts +98 -0
  93. package/src/integrations/subagents/protocol-events.ts +27 -0
  94. package/src/integrations/subagents/protocol.ts +30 -375
  95. package/src/integrations/subagents/replay-audit.ts +147 -0
  96. package/src/integrations/subagents/replay-safety.ts +82 -0
  97. package/src/integrations/subagents/session-diagnostics.ts +258 -0
  98. package/src/policy/approved-command-extraction.ts +71 -0
  99. package/src/policy/approved-commands.ts +15 -432
  100. package/src/policy/bash-authorization.ts +86 -0
  101. package/src/policy/bash-read-only.ts +161 -0
  102. package/src/policy/bash-types.ts +20 -0
  103. package/src/policy/bash.ts +4 -346
  104. package/src/policy/completion-batch.ts +21 -16
  105. package/src/policy/immutable-input.ts +5 -2
  106. package/src/policy/mcp-authorization.ts +71 -0
  107. package/src/policy/restricted-command.ts +141 -0
  108. package/src/policy/restricted-git.ts +30 -0
  109. package/src/policy/reviewed-artifact.ts +97 -0
  110. package/src/policy/reviewed-command-safety.ts +150 -0
  111. package/src/policy/reviewed-command-shape.ts +49 -0
  112. package/src/policy/reviewed-repository-contract.ts +138 -0
  113. package/src/policy/reviewed-repository-cwd.ts +102 -0
  114. package/src/policy/tool-call-authorization.ts +58 -0
  115. package/src/policy/tool-selection.ts +72 -0
  116. package/src/policy/tool-types.ts +14 -0
  117. package/src/policy/tools.ts +11 -150
  118. package/src/preflight.ts +96 -61
  119. package/src/prompt/main-workflow-notice.ts +41 -0
  120. package/src/prompt/retry-task.ts +83 -0
  121. package/src/prompt/step-contract.ts +101 -0
  122. package/src/prompt/step-sections.ts +75 -0
  123. package/src/prompt/step-task.ts +156 -0
  124. package/src/prompt/template.ts +83 -0
  125. package/src/prompt.ts +10 -254
  126. package/src/runtime/completion-tool.ts +3 -0
  127. package/src/runtime/main-step-completion.ts +78 -0
  128. package/src/runtime/main-step-lifecycle.ts +37 -0
  129. package/src/runtime/main-step-policy.ts +84 -0
  130. package/src/runtime/main-step-runtime-types.ts +64 -0
  131. package/src/runtime/main-step-runtime.ts +129 -200
  132. package/src/runtime/main-step-state.ts +85 -0
  133. package/src/runtime/serial-task-queue.ts +48 -13
  134. package/src/runtime/step-result.ts +48 -27
  135. package/src/workflow-list.ts +15 -6
  136. package/src/workflow-status/format-status.ts +52 -0
  137. package/src/workflow-status/formatting.ts +178 -0
  138. package/src/workflow-status/layout.ts +109 -0
  139. package/src/workflow-status/render-board.ts +101 -0
  140. package/src/workflow-status/render-path.ts +123 -0
  141. package/src/workflow-status/render-summary.ts +144 -0
  142. package/src/workflow-status/types.ts +40 -0
  143. package/src/workflow-status/view.ts +273 -0
  144. package/src/workflow-status.ts +20 -820
@@ -0,0 +1,40 @@
1
+ import type { ToolFailureDiagnostic } from './diagnostic-types.ts';
2
+
3
+ /**
4
+ * Extracts a failed tool name from a terminal error string.
5
+ */
6
+ export const failedToolName = (error: string | undefined): string | undefined =>
7
+ error?.match(/\b([a-z][\w-]*) failed(?:\s*\(|:)/i)?.[1];
8
+
9
+ /**
10
+ * Formats a correlated tool failure as concise human-readable lines.
11
+ */
12
+ export const formatToolFailureDiagnostic = (
13
+ diagnostic: ToolFailureDiagnostic,
14
+ ): Array<string> => {
15
+ const hasSuccessfulOutputCorrelation =
16
+ diagnostic.correlation === 'successful-output-before-completion';
17
+ return [
18
+ `${hasSuccessfulOutputCorrelation ? 'Terminal-reported tool' : 'Failed tool'}: ${diagnostic.tool}`,
19
+ ...(diagnostic.call
20
+ ? [
21
+ `${diagnostic.tool === 'bash' ? 'Command' : 'Arguments'}: ${diagnostic.call}`,
22
+ ]
23
+ : []),
24
+ ...(diagnostic.output
25
+ ? [
26
+ `${hasSuccessfulOutputCorrelation ? 'Successful tool output' : 'Tool error'}: ${diagnostic.output}`,
27
+ ]
28
+ : []),
29
+ ...(diagnostic.correlation === 'latest-before-completion'
30
+ ? [
31
+ 'Correlation: latest failed tool call before successful structured_output; terminal text did not identify the call',
32
+ ]
33
+ : []),
34
+ ...(hasSuccessfulOutputCorrelation
35
+ ? [
36
+ 'Correlation: terminal error text came from a successful tool result before the final structured_output',
37
+ ]
38
+ : []),
39
+ ];
40
+ };
@@ -0,0 +1,114 @@
1
+ const MAX_DIAGNOSTIC_FIELD_CHARS = 1_600;
2
+ const TRUNCATION_MARKER = '… [truncated] …';
3
+
4
+ /**
5
+ * Narrows an unknown transcript value to a plain record.
6
+ */
7
+ export const isDiagnosticRecord = (
8
+ value: unknown,
9
+ ): value is Record<string, unknown> =>
10
+ value !== null && typeof value === 'object' && !Array.isArray(value);
11
+
12
+ /**
13
+ * Bounds diagnostic text while preserving evidence from both ends.
14
+ */
15
+ export const boundedDiagnosticText = (value: string): string => {
16
+ if (value.length <= MAX_DIAGNOSTIC_FIELD_CHARS) return value;
17
+ const available = MAX_DIAGNOSTIC_FIELD_CHARS - TRUNCATION_MARKER.length - 2;
18
+ const startLength = Math.ceil(available / 2);
19
+ const endLength = Math.floor(available / 2);
20
+ return `${value.slice(0, startLength)}\n${TRUNCATION_MARKER}\n${value.slice(-endLength)}`;
21
+ };
22
+
23
+ /**
24
+ * Joins and bounds every text item in a transcript content array.
25
+ */
26
+ export const diagnosticTextContent = (value: unknown): string | undefined => {
27
+ if (!Array.isArray(value)) return undefined;
28
+ const text = value
29
+ .flatMap((item) =>
30
+ isDiagnosticRecord(item) &&
31
+ item.type === 'text' &&
32
+ typeof item.text === 'string'
33
+ ? [item.text]
34
+ : [],
35
+ )
36
+ .join('\n')
37
+ .trim();
38
+ return text ? boundedDiagnosticText(text) : undefined;
39
+ };
40
+
41
+ /**
42
+ * Returns the first unmodified text item in transcript content.
43
+ */
44
+ export const firstDiagnosticTextContent = (
45
+ value: unknown,
46
+ ): string | undefined => {
47
+ if (!Array.isArray(value)) return undefined;
48
+ const content: ReadonlyArray<unknown> = value;
49
+ const text = content.find(
50
+ (item) =>
51
+ isDiagnosticRecord(item) &&
52
+ item.type === 'text' &&
53
+ typeof item.text === 'string',
54
+ );
55
+ return isDiagnosticRecord(text) && typeof text.text === 'string'
56
+ ? text.text
57
+ : undefined;
58
+ };
59
+
60
+ /**
61
+ * Formats a tool call for bounded diagnostic output.
62
+ */
63
+ export const diagnosticToolCallText = ({
64
+ tool,
65
+ argumentsValue,
66
+ }: {
67
+ readonly tool: string;
68
+ readonly argumentsValue: unknown;
69
+ }): string | undefined => {
70
+ if (!isDiagnosticRecord(argumentsValue)) return undefined;
71
+ if (tool === 'bash') {
72
+ const command = argumentsValue.command ?? argumentsValue.cmd;
73
+ if (typeof command === 'string' && command.trim()) {
74
+ return boundedDiagnosticText(command.trim());
75
+ }
76
+ }
77
+ return boundedDiagnosticText(JSON.stringify(argumentsValue));
78
+ };
79
+
80
+ /**
81
+ * Extracts the value from an exclusive structured-output argument shape.
82
+ */
83
+ export const structuredCompletionValue = (
84
+ argumentsValue: unknown,
85
+ ): Readonly<Record<string, unknown>> | undefined => {
86
+ if (!isDiagnosticRecord(argumentsValue)) return undefined;
87
+ if (
88
+ Object.keys(argumentsValue).length !== 1 ||
89
+ !Object.hasOwn(argumentsValue, 'value') ||
90
+ !isDiagnosticRecord(argumentsValue.value)
91
+ ) {
92
+ return undefined;
93
+ }
94
+ return argumentsValue.value;
95
+ };
96
+
97
+ const normalizeDiagnosticText = (value: string): string =>
98
+ value.replace(/\s+/g, ' ').trim().toLowerCase();
99
+
100
+ /**
101
+ * Produces normalized, non-trivial fragments for error correlation.
102
+ */
103
+ export const comparableDiagnosticFragments = (
104
+ value: string,
105
+ ): ReadonlyArray<string> => {
106
+ const normalizedValue = normalizeDiagnosticText(value);
107
+ const lines = value
108
+ .split(/\r?\n/)
109
+ .map(normalizeDiagnosticText)
110
+ .filter((line) => line.length >= 8);
111
+ return [...new Set([normalizedValue, ...lines])].filter(
112
+ (fragment) => fragment.length >= 8,
113
+ );
114
+ };
@@ -0,0 +1,77 @@
1
+ import type { BashPermission } from '../../config/types.ts';
2
+
3
+ export type ToolFailureDiagnostic = {
4
+ readonly tool: string;
5
+ readonly call?: string;
6
+ readonly output?: string;
7
+ readonly replaySafe?: true;
8
+ readonly completionAfterFailure?: true;
9
+ readonly completionValue?: Readonly<Record<string, unknown>>;
10
+ readonly transcriptToolCount?: number;
11
+ readonly transcriptTurnCount?: number;
12
+ readonly correlation?:
13
+ 'latest-before-completion' | 'successful-output-before-completion';
14
+ };
15
+
16
+ export type DelegationReplayAudit = {
17
+ readonly replaySafe: boolean;
18
+ readonly toolCount: number;
19
+ };
20
+
21
+ export type DelegationReplayExpectation = {
22
+ readonly task: string;
23
+ readonly bashPermission: BashPermission;
24
+ readonly approvedBashCommands: ReadonlyArray<string>;
25
+ };
26
+
27
+ export type SubagentSessionIdentity = {
28
+ readonly runId: string;
29
+ readonly childIndex: number;
30
+ };
31
+
32
+ export type RecordedToolCall = {
33
+ readonly id: string;
34
+ readonly order: number;
35
+ readonly tool: string;
36
+ readonly call?: string;
37
+ readonly completionValue?: Readonly<Record<string, unknown>>;
38
+ };
39
+
40
+ export type RecordedToolFailure = ToolFailureDiagnostic & {
41
+ readonly callId?: string;
42
+ readonly order: number;
43
+ };
44
+
45
+ export type RecordedToolSuccess = {
46
+ readonly order: number;
47
+ readonly tool: string;
48
+ readonly call?: string;
49
+ readonly output?: string;
50
+ readonly detectorOutput?: string;
51
+ };
52
+
53
+ export type RecordedCompletion = {
54
+ readonly order: number;
55
+ readonly value: Readonly<Record<string, unknown>>;
56
+ };
57
+
58
+ export type RecordedMessage = {
59
+ readonly order: number;
60
+ readonly value: Readonly<Record<string, unknown>>;
61
+ };
62
+
63
+ export type ParsedFailureTranscript = {
64
+ readonly recordedCalls: ReadonlyArray<RecordedToolCall>;
65
+ readonly diagnostics: ReadonlyArray<RecordedToolFailure>;
66
+ readonly successfulResults: ReadonlyArray<RecordedToolSuccess>;
67
+ readonly successfulCompletions: ReadonlyArray<RecordedCompletion>;
68
+ readonly recordedMessages: ReadonlyArray<RecordedMessage>;
69
+ readonly resultCallIds: ReadonlySet<string>;
70
+ readonly hasValidFalsePositiveProof: boolean;
71
+ readonly lastInteractionOrder: number;
72
+ };
73
+
74
+ export type SessionTail = {
75
+ readonly content: string;
76
+ readonly truncated: boolean;
77
+ };