gsd-pi 2.41.0-dev.cac69f9 → 2.42.0-dev.97e9e30

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 (115) hide show
  1. package/dist/resources/extensions/gsd/auto/loop.js +80 -0
  2. package/dist/resources/extensions/gsd/auto/phases.js +2 -2
  3. package/dist/resources/extensions/gsd/auto/session.js +6 -0
  4. package/dist/resources/extensions/gsd/auto-dashboard.js +2 -0
  5. package/dist/resources/extensions/gsd/auto.js +28 -1
  6. package/dist/resources/extensions/gsd/bootstrap/tool-call-loop-guard.js +7 -2
  7. package/dist/resources/extensions/gsd/commands/catalog.js +32 -0
  8. package/dist/resources/extensions/gsd/commands/handlers/workflow.js +146 -0
  9. package/dist/resources/extensions/gsd/context-injector.js +74 -0
  10. package/dist/resources/extensions/gsd/custom-execution-policy.js +47 -0
  11. package/dist/resources/extensions/gsd/custom-verification.js +145 -0
  12. package/dist/resources/extensions/gsd/custom-workflow-engine.js +164 -0
  13. package/dist/resources/extensions/gsd/dashboard-overlay.js +1 -0
  14. package/dist/resources/extensions/gsd/definition-loader.js +352 -0
  15. package/dist/resources/extensions/gsd/dev-execution-policy.js +24 -0
  16. package/dist/resources/extensions/gsd/dev-workflow-engine.js +82 -0
  17. package/dist/resources/extensions/gsd/engine-resolver.js +40 -0
  18. package/dist/resources/extensions/gsd/engine-types.js +8 -0
  19. package/dist/resources/extensions/gsd/execution-policy.js +8 -0
  20. package/dist/resources/extensions/gsd/graph.js +225 -0
  21. package/dist/resources/extensions/gsd/run-manager.js +134 -0
  22. package/dist/resources/extensions/gsd/workflow-engine.js +7 -0
  23. package/dist/resources/skills/create-workflow/SKILL.md +103 -0
  24. package/dist/resources/skills/create-workflow/references/feature-patterns.md +128 -0
  25. package/dist/resources/skills/create-workflow/references/verification-policies.md +76 -0
  26. package/dist/resources/skills/create-workflow/references/yaml-schema-v1.md +46 -0
  27. package/dist/resources/skills/create-workflow/templates/blog-post-pipeline.yaml +60 -0
  28. package/dist/resources/skills/create-workflow/templates/code-audit.yaml +60 -0
  29. package/dist/resources/skills/create-workflow/templates/release-checklist.yaml +66 -0
  30. package/dist/resources/skills/create-workflow/templates/workflow-definition.yaml +32 -0
  31. package/dist/resources/skills/create-workflow/workflows/create-from-scratch.md +104 -0
  32. package/dist/resources/skills/create-workflow/workflows/create-from-template.md +72 -0
  33. package/dist/web/standalone/.next/BUILD_ID +1 -1
  34. package/dist/web/standalone/.next/app-path-routes-manifest.json +15 -15
  35. package/dist/web/standalone/.next/build-manifest.json +2 -2
  36. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  37. package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
  38. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  39. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  40. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  41. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  42. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  43. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  44. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  45. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  46. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  47. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  48. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  49. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  51. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/index.html +1 -1
  54. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  55. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  56. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  57. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  59. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app-paths-manifest.json +15 -15
  61. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  62. package/dist/web/standalone/.next/server/pages/500.html +2 -2
  63. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  64. package/package.json +1 -1
  65. package/packages/pi-coding-agent/package.json +1 -1
  66. package/pkg/package.json +1 -1
  67. package/src/resources/extensions/gsd/auto/loop.ts +91 -0
  68. package/src/resources/extensions/gsd/auto/phases.ts +2 -2
  69. package/src/resources/extensions/gsd/auto/session.ts +6 -0
  70. package/src/resources/extensions/gsd/auto-dashboard.ts +2 -0
  71. package/src/resources/extensions/gsd/auto.ts +31 -1
  72. package/src/resources/extensions/gsd/bootstrap/tool-call-loop-guard.ts +9 -2
  73. package/src/resources/extensions/gsd/commands/catalog.ts +32 -0
  74. package/src/resources/extensions/gsd/commands/handlers/workflow.ts +164 -0
  75. package/src/resources/extensions/gsd/context-injector.ts +100 -0
  76. package/src/resources/extensions/gsd/custom-execution-policy.ts +73 -0
  77. package/src/resources/extensions/gsd/custom-verification.ts +180 -0
  78. package/src/resources/extensions/gsd/custom-workflow-engine.ts +216 -0
  79. package/src/resources/extensions/gsd/dashboard-overlay.ts +1 -0
  80. package/src/resources/extensions/gsd/definition-loader.ts +462 -0
  81. package/src/resources/extensions/gsd/dev-execution-policy.ts +51 -0
  82. package/src/resources/extensions/gsd/dev-workflow-engine.ts +110 -0
  83. package/src/resources/extensions/gsd/engine-resolver.ts +57 -0
  84. package/src/resources/extensions/gsd/engine-types.ts +71 -0
  85. package/src/resources/extensions/gsd/execution-policy.ts +43 -0
  86. package/src/resources/extensions/gsd/graph.ts +312 -0
  87. package/src/resources/extensions/gsd/run-manager.ts +180 -0
  88. package/src/resources/extensions/gsd/tests/bundled-workflow-defs.test.ts +180 -0
  89. package/src/resources/extensions/gsd/tests/commands-workflow-custom.test.ts +283 -0
  90. package/src/resources/extensions/gsd/tests/context-injector.test.ts +313 -0
  91. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +540 -0
  92. package/src/resources/extensions/gsd/tests/custom-verification.test.ts +382 -0
  93. package/src/resources/extensions/gsd/tests/custom-workflow-engine.test.ts +339 -0
  94. package/src/resources/extensions/gsd/tests/dashboard-custom-engine.test.ts +87 -0
  95. package/src/resources/extensions/gsd/tests/definition-loader.test.ts +778 -0
  96. package/src/resources/extensions/gsd/tests/dev-engine-wrapper.test.ts +318 -0
  97. package/src/resources/extensions/gsd/tests/e2e-workflow-pipeline-integration.test.ts +476 -0
  98. package/src/resources/extensions/gsd/tests/engine-interfaces-contract.test.ts +271 -0
  99. package/src/resources/extensions/gsd/tests/graph-operations.test.ts +599 -0
  100. package/src/resources/extensions/gsd/tests/iterate-engine-integration.test.ts +429 -0
  101. package/src/resources/extensions/gsd/tests/run-manager.test.ts +229 -0
  102. package/src/resources/extensions/gsd/tests/tool-call-loop-guard.test.ts +45 -0
  103. package/src/resources/extensions/gsd/workflow-engine.ts +38 -0
  104. package/src/resources/skills/create-workflow/SKILL.md +103 -0
  105. package/src/resources/skills/create-workflow/references/feature-patterns.md +128 -0
  106. package/src/resources/skills/create-workflow/references/verification-policies.md +76 -0
  107. package/src/resources/skills/create-workflow/references/yaml-schema-v1.md +46 -0
  108. package/src/resources/skills/create-workflow/templates/blog-post-pipeline.yaml +60 -0
  109. package/src/resources/skills/create-workflow/templates/code-audit.yaml +60 -0
  110. package/src/resources/skills/create-workflow/templates/release-checklist.yaml +66 -0
  111. package/src/resources/skills/create-workflow/templates/workflow-definition.yaml +32 -0
  112. package/src/resources/skills/create-workflow/workflows/create-from-scratch.md +104 -0
  113. package/src/resources/skills/create-workflow/workflows/create-from-template.md +72 -0
  114. /package/dist/web/standalone/.next/static/{EnGUNqHeGbE0tuuUkTJVA → PXrI5DoWsm7rwAVnEU2rD}/_buildManifest.js +0 -0
  115. /package/dist/web/standalone/.next/static/{EnGUNqHeGbE0tuuUkTJVA → PXrI5DoWsm7rwAVnEU2rD}/_ssgManifest.js +0 -0
@@ -2,6 +2,7 @@ import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent
2
2
 
3
3
  import { existsSync, readFileSync, unlinkSync } from "node:fs";
4
4
  import { join } from "node:path";
5
+ import { parse as parseYaml } from "yaml";
5
6
 
6
7
  import { handleQuick } from "../../quick.js";
7
8
  import { showDiscuss, showHeadlessMilestoneCreation, showQueue } from "../../guided-flow.js";
@@ -13,8 +14,171 @@ import { loadEffectiveGSDPreferences } from "../../preferences.js";
13
14
  import { nextMilestoneId } from "../../milestone-ids.js";
14
15
  import { findMilestoneIds } from "../../guided-flow.js";
15
16
  import { projectRoot } from "../context.js";
17
+ import { createRun, listRuns } from "../../run-manager.js";
18
+ import {
19
+ setActiveEngineId,
20
+ setActiveRunDir,
21
+ startAuto,
22
+ pauseAuto,
23
+ isAutoActive,
24
+ getActiveEngineId,
25
+ } from "../../auto.js";
26
+ import { validateDefinition } from "../../definition-loader.js";
27
+
28
+ // ─── Custom Workflow Subcommands ─────────────────────────────────────────
29
+
30
+ const WORKFLOW_USAGE = [
31
+ "Usage: /gsd workflow <subcommand>",
32
+ "",
33
+ " new — Create a new workflow definition (via skill)",
34
+ " run <name> [k=v] — Create a run and start auto-mode",
35
+ " list [name] — List workflow runs (optionally filtered by name)",
36
+ " validate <name> — Validate a workflow definition YAML",
37
+ " pause — Pause custom workflow auto-mode",
38
+ " resume — Resume paused custom workflow auto-mode",
39
+ ].join("\n");
40
+
41
+ async function handleCustomWorkflow(
42
+ sub: string,
43
+ ctx: ExtensionCommandContext,
44
+ pi: ExtensionAPI,
45
+ ): Promise<boolean> {
46
+ // Bare `/gsd workflow` — show usage
47
+ if (!sub) {
48
+ ctx.ui.notify(WORKFLOW_USAGE, "info");
49
+ return true;
50
+ }
51
+
52
+ // ── new ──
53
+ if (sub === "new") {
54
+ ctx.ui.notify("Use the create-workflow skill: /skill create-workflow", "info");
55
+ return true;
56
+ }
57
+
58
+ // ── run <name> [param=value ...] ──
59
+ if (sub === "run" || sub.startsWith("run ")) {
60
+ const args = sub.slice("run".length).trim();
61
+ if (!args) {
62
+ ctx.ui.notify("Usage: /gsd workflow run <name> [param=value ...]", "warning");
63
+ return true;
64
+ }
65
+ const parts = args.split(/\s+/);
66
+ const defName = parts[0];
67
+ const overrides: Record<string, string> = {};
68
+ for (let i = 1; i < parts.length; i++) {
69
+ const eqIdx = parts[i].indexOf("=");
70
+ if (eqIdx > 0) {
71
+ overrides[parts[i].slice(0, eqIdx)] = parts[i].slice(eqIdx + 1);
72
+ }
73
+ }
74
+ try {
75
+ const base = projectRoot();
76
+ const runDir = createRun(base, defName, Object.keys(overrides).length > 0 ? overrides : undefined);
77
+ setActiveEngineId("custom");
78
+ setActiveRunDir(runDir);
79
+ ctx.ui.notify(`Created workflow run: ${defName}\nRun dir: ${runDir}`, "info");
80
+ await startAuto(ctx, pi, base, false);
81
+ } catch (err) {
82
+ // Clean up engine state so a failed workflow run doesn't pollute the next /gsd auto
83
+ setActiveEngineId(null);
84
+ setActiveRunDir(null);
85
+ const msg = err instanceof Error ? err.message : String(err);
86
+ ctx.ui.notify(`Failed to run workflow "${defName}": ${msg}`, "error");
87
+ }
88
+ return true;
89
+ }
90
+
91
+ // ── list [name] ──
92
+ if (sub === "list" || sub.startsWith("list ")) {
93
+ const filterName = sub.slice("list".length).trim() || undefined;
94
+ const base = projectRoot();
95
+ const runs = listRuns(base, filterName);
96
+ if (runs.length === 0) {
97
+ ctx.ui.notify("No workflow runs found.", "info");
98
+ return true;
99
+ }
100
+ const lines = runs.map((r) => {
101
+ const stepInfo = `${r.steps.completed}/${r.steps.total} steps`;
102
+ return `• ${r.name} [${r.timestamp}] — ${r.status} (${stepInfo})`;
103
+ });
104
+ ctx.ui.notify(lines.join("\n"), "info");
105
+ return true;
106
+ }
107
+
108
+ // ── validate <name> ──
109
+ if (sub === "validate" || sub.startsWith("validate ")) {
110
+ const defName = sub.slice("validate".length).trim();
111
+ if (!defName) {
112
+ ctx.ui.notify("Usage: /gsd workflow validate <name>", "warning");
113
+ return true;
114
+ }
115
+ const base = projectRoot();
116
+ const defPath = join(base, ".gsd", "workflow-defs", `${defName}.yaml`);
117
+ if (!existsSync(defPath)) {
118
+ ctx.ui.notify(`Definition not found: ${defPath}`, "error");
119
+ return true;
120
+ }
121
+ try {
122
+ const raw = readFileSync(defPath, "utf-8");
123
+ const parsed = parseYaml(raw);
124
+ const result = validateDefinition(parsed);
125
+ if (result.valid) {
126
+ ctx.ui.notify(`✓ "${defName}" is a valid workflow definition.`, "info");
127
+ } else {
128
+ ctx.ui.notify(`✗ "${defName}" has errors:\n - ${result.errors.join("\n - ")}`, "error");
129
+ }
130
+ } catch (err) {
131
+ const msg = err instanceof Error ? err.message : String(err);
132
+ ctx.ui.notify(`Failed to validate "${defName}": ${msg}`, "error");
133
+ }
134
+ return true;
135
+ }
136
+
137
+ // ── pause ──
138
+ if (sub === "pause") {
139
+ const engineId = getActiveEngineId();
140
+ if (engineId === "dev" || engineId === null) {
141
+ ctx.ui.notify("No custom workflow is running. Use /gsd pause for dev workflow.", "warning");
142
+ return true;
143
+ }
144
+ if (!isAutoActive()) {
145
+ ctx.ui.notify("Auto-mode is not active.", "warning");
146
+ return true;
147
+ }
148
+ await pauseAuto(ctx, pi);
149
+ ctx.ui.notify("Custom workflow paused.", "info");
150
+ return true;
151
+ }
152
+
153
+ // ── resume ──
154
+ if (sub === "resume") {
155
+ const engineId = getActiveEngineId();
156
+ if (engineId === "dev" || engineId === null) {
157
+ ctx.ui.notify("No custom workflow to resume. Use /gsd auto for dev workflow.", "warning");
158
+ return true;
159
+ }
160
+ try {
161
+ await startAuto(ctx, pi, projectRoot(), false);
162
+ ctx.ui.notify("Custom workflow resumed.", "info");
163
+ } catch (err) {
164
+ const msg = err instanceof Error ? err.message : String(err);
165
+ ctx.ui.notify(`Failed to resume workflow: ${msg}`, "error");
166
+ }
167
+ return true;
168
+ }
169
+
170
+ // Unknown subcommand — show usage
171
+ ctx.ui.notify(`Unknown workflow subcommand: "${sub}"\n\n${WORKFLOW_USAGE}`, "warning");
172
+ return true;
173
+ }
16
174
 
17
175
  export async function handleWorkflowCommand(trimmed: string, ctx: ExtensionCommandContext, pi: ExtensionAPI): Promise<boolean> {
176
+ // ── Custom workflow commands (`/gsd workflow ...`) ──
177
+ if (trimmed === "workflow" || trimmed.startsWith("workflow ")) {
178
+ const sub = trimmed.slice("workflow".length).trim();
179
+ return handleCustomWorkflow(sub, ctx, pi);
180
+ }
181
+
18
182
  if (trimmed === "queue") {
19
183
  await showQueue(ctx, pi, projectRoot());
20
184
  return true;
@@ -0,0 +1,100 @@
1
+ /**
2
+ * context-injector.ts — Inject prior step artifacts as context into step prompts.
3
+ *
4
+ * Reads the frozen DEFINITION.yaml from a run directory, finds the current step's
5
+ * `contextFrom` references, locates each referenced step's `produces` artifacts
6
+ * on disk, reads their content (truncated to 10k chars), and prepends formatted
7
+ * context blocks to the step prompt.
8
+ *
9
+ * Observability:
10
+ * - Truncation is logged via console.warn when it occurs, preventing silent overflow.
11
+ * - Missing artifact files are skipped silently (the step may not have produced them yet).
12
+ * - Unknown step IDs in contextFrom produce a console.warn for diagnosis.
13
+ * - The frozen DEFINITION.yaml on disk is the single source of truth for contextFrom config.
14
+ */
15
+
16
+ import { readFileSync, existsSync } from "node:fs";
17
+ import { join, resolve, sep } from "node:path";
18
+ import type { StepDefinition } from "./definition-loader.js";
19
+ import { readFrozenDefinition } from "./custom-workflow-engine.js";
20
+
21
+ /** Maximum characters per artifact to prevent context window blowout. */
22
+ const MAX_CONTEXT_CHARS = 10_000;
23
+
24
+ /**
25
+ * Inject context from prior step artifacts into a step's prompt.
26
+ *
27
+ * Reads the frozen DEFINITION.yaml from `runDir`, finds the step matching
28
+ * `stepId`, and for each step ID in its `contextFrom` array, looks up that
29
+ * step's `produces` paths, reads them from disk (relative to `runDir`),
30
+ * truncates to MAX_CONTEXT_CHARS, and prepends as labeled context blocks.
31
+ *
32
+ * @param runDir — absolute path to the workflow run directory
33
+ * @param stepId — the step ID whose prompt to enrich
34
+ * @param prompt — the original step prompt
35
+ * @returns The prompt with context blocks prepended, or unchanged if no context applies
36
+ * @throws Error if DEFINITION.yaml is missing or unreadable
37
+ */
38
+ export function injectContext(
39
+ runDir: string,
40
+ stepId: string,
41
+ prompt: string,
42
+ ): string {
43
+ const def = readFrozenDefinition(runDir);
44
+
45
+ const step = def.steps.find((s: StepDefinition) => s.id === stepId);
46
+ if (!step || !step.contextFrom || step.contextFrom.length === 0) {
47
+ return prompt;
48
+ }
49
+
50
+ const contextBlocks: string[] = [];
51
+
52
+ for (const refStepId of step.contextFrom) {
53
+ const refStep = def.steps.find((s: StepDefinition) => s.id === refStepId);
54
+ if (!refStep) {
55
+ console.warn(
56
+ `context-injector: step "${stepId}" references unknown step "${refStepId}" in contextFrom — skipping`,
57
+ );
58
+ continue;
59
+ }
60
+
61
+ if (!refStep.produces || refStep.produces.length === 0) {
62
+ continue;
63
+ }
64
+
65
+ for (const relPath of refStep.produces) {
66
+ const absPath = resolve(runDir, relPath);
67
+ // Path traversal guard: ensure resolved path stays within runDir
68
+ if (!absPath.startsWith(resolve(runDir) + sep) && absPath !== resolve(runDir)) {
69
+ console.warn(
70
+ `context-injector: artifact path "${relPath}" resolves outside runDir — skipping`,
71
+ );
72
+ continue;
73
+ }
74
+ if (!existsSync(absPath)) {
75
+ // Artifact not yet produced or optional — skip silently
76
+ continue;
77
+ }
78
+
79
+ let content = readFileSync(absPath, "utf-8");
80
+
81
+ if (content.length > MAX_CONTEXT_CHARS) {
82
+ console.warn(
83
+ `context-injector: truncating artifact "${relPath}" from step "${refStepId}" ` +
84
+ `(${content.length} chars → ${MAX_CONTEXT_CHARS} chars)`,
85
+ );
86
+ content = content.slice(0, MAX_CONTEXT_CHARS) + "\n...[truncated]";
87
+ }
88
+
89
+ contextBlocks.push(
90
+ `--- Context from step "${refStepId}" (file: ${relPath}) ---\n${content}\n---`,
91
+ );
92
+ }
93
+ }
94
+
95
+ if (contextBlocks.length === 0) {
96
+ return prompt;
97
+ }
98
+
99
+ return contextBlocks.join("\n\n") + "\n\n" + prompt;
100
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * custom-execution-policy.ts — ExecutionPolicy for custom workflows.
3
+ *
4
+ * Delegates verification to the step-level verification module which reads
5
+ * the frozen DEFINITION.yaml and dispatches to the appropriate policy handler.
6
+ *
7
+ * Observability:
8
+ * - verify() returns the outcome from runCustomVerification() — four policies
9
+ * are supported: content-heuristic, shell-command, prompt-verify, human-review.
10
+ * - selectModel() returns null — defers to loop defaults.
11
+ * - recover() returns retry — simple default recovery strategy.
12
+ */
13
+
14
+ import type { ExecutionPolicy } from "./execution-policy.js";
15
+ import type { RecoveryAction, CloseoutResult } from "./engine-types.js";
16
+ import { runCustomVerification } from "./custom-verification.js";
17
+
18
+ export class CustomExecutionPolicy implements ExecutionPolicy {
19
+ private readonly runDir: string;
20
+
21
+ constructor(runDir: string) {
22
+ this.runDir = runDir;
23
+ }
24
+
25
+ /** No workspace preparation needed for custom workflows. */
26
+ async prepareWorkspace(_basePath: string, _milestoneId: string): Promise<void> {
27
+ // No-op — custom workflows don't need worktree setup
28
+ }
29
+
30
+ /** Defer model selection to loop defaults. */
31
+ async selectModel(
32
+ _unitType: string,
33
+ _unitId: string,
34
+ _context: { basePath: string },
35
+ ): Promise<{ tier: string; modelDowngraded: boolean } | null> {
36
+ return null;
37
+ }
38
+
39
+ /**
40
+ * Verify step output by dispatching to the step's configured verification policy.
41
+ *
42
+ * Extracts the step ID from unitId (format: "<workflowName>/<stepId>")
43
+ * and calls runCustomVerification() which reads the frozen DEFINITION.yaml
44
+ * to determine which policy to apply.
45
+ */
46
+ async verify(
47
+ _unitType: string,
48
+ unitId: string,
49
+ _context: { basePath: string },
50
+ ): Promise<"continue" | "retry" | "pause"> {
51
+ const parts = unitId.split("/");
52
+ const stepId = parts[parts.length - 1];
53
+ return runCustomVerification(this.runDir, stepId);
54
+ }
55
+
56
+ /** Default recovery: retry the step. */
57
+ async recover(
58
+ _unitType: string,
59
+ _unitId: string,
60
+ _context: { basePath: string },
61
+ ): Promise<RecoveryAction> {
62
+ return { outcome: "retry", reason: "Default retry" };
63
+ }
64
+
65
+ /** No-op closeout — no commits or artifact capture. */
66
+ async closeout(
67
+ _unitType: string,
68
+ _unitId: string,
69
+ _context: { basePath: string; startedAt: number },
70
+ ): Promise<CloseoutResult> {
71
+ return { committed: false, artifacts: [] };
72
+ }
73
+ }
@@ -0,0 +1,180 @@
1
+ /**
2
+ * custom-verification.ts — Step verification for custom workflows.
3
+ *
4
+ * Reads the frozen DEFINITION.yaml from a run directory, finds the step's
5
+ * `verify` policy, and dispatches to the appropriate handler. Four policies:
6
+ *
7
+ * - content-heuristic: file existence + optional minSize + optional pattern match
8
+ * - shell-command: spawnSync with 30s timeout, exit 0 → continue, else retry
9
+ * - prompt-verify: always "pause" (defers to agent)
10
+ * - human-review: always "pause" (waits for manual inspection)
11
+ * - (no policy): returns "continue" (passthrough)
12
+ *
13
+ * Observability:
14
+ * - Return value is the typed verification outcome ("continue" | "retry" | "pause").
15
+ * - shell-command captures stderr from spawnSync — callers can inspect on retry.
16
+ * - content-heuristic logs the specific failure (missing file, below minSize, pattern mismatch).
17
+ * - The frozen DEFINITION.yaml on disk is the single source of truth for step policies.
18
+ */
19
+
20
+ import { readFileSync, existsSync, statSync } from "node:fs";
21
+ import { join, resolve, sep } from "node:path";
22
+ import { spawnSync } from "node:child_process";
23
+ import type { StepDefinition, VerifyPolicy } from "./definition-loader.js";
24
+ import { readFrozenDefinition } from "./custom-workflow-engine.js";
25
+
26
+ /** Verification outcome type — matches ExecutionPolicy.verify() return type. */
27
+ export type VerificationOutcome = "continue" | "retry" | "pause";
28
+
29
+ /**
30
+ * Run custom verification for a specific step in a workflow run.
31
+ *
32
+ * Reads the frozen DEFINITION.yaml from `runDir`, finds the step with the
33
+ * given `stepId`, and dispatches to the appropriate verification handler
34
+ * based on the step's `verify.policy` field.
35
+ *
36
+ * @param runDir — absolute path to the workflow run directory
37
+ * @param stepId — the step ID to verify (e.g. "step-1")
38
+ * @returns "continue" if verification passes, "retry" if it should retry, "pause" if it needs review
39
+ * @throws Error if DEFINITION.yaml is missing or unreadable
40
+ */
41
+ export function runCustomVerification(
42
+ runDir: string,
43
+ stepId: string,
44
+ ): VerificationOutcome {
45
+ const def = readFrozenDefinition(runDir);
46
+
47
+ const step = def.steps.find((s: StepDefinition) => s.id === stepId);
48
+ if (!step) {
49
+ // Step not found in definition — nothing to verify, continue
50
+ return "continue";
51
+ }
52
+
53
+ if (!step.verify) {
54
+ // No verification policy configured — passthrough
55
+ return "continue";
56
+ }
57
+
58
+ return dispatchPolicy(runDir, step, step.verify);
59
+ }
60
+
61
+ /**
62
+ * Dispatch to the correct policy handler.
63
+ */
64
+ function dispatchPolicy(
65
+ runDir: string,
66
+ step: StepDefinition,
67
+ verify: VerifyPolicy,
68
+ ): VerificationOutcome {
69
+ switch (verify.policy) {
70
+ case "content-heuristic":
71
+ return handleContentHeuristic(runDir, step, verify);
72
+ case "shell-command":
73
+ return handleShellCommand(runDir, verify);
74
+ case "prompt-verify":
75
+ return "pause";
76
+ case "human-review":
77
+ return "pause";
78
+ default:
79
+ // Unknown policy — safe default is pause
80
+ return "pause";
81
+ }
82
+ }
83
+
84
+ /**
85
+ * content-heuristic handler.
86
+ *
87
+ * For each path in the step's `produces` array:
88
+ * 1. Check that the file exists (resolved relative to runDir)
89
+ * 2. If `minSize` is set, check that file size >= minSize bytes
90
+ * 3. If `pattern` is set, check that file content matches the regex
91
+ *
92
+ * Returns "continue" if all checks pass, "pause" if any fail.
93
+ * If `produces` is empty or undefined, returns "continue" (nothing to check).
94
+ */
95
+ function handleContentHeuristic(
96
+ runDir: string,
97
+ step: StepDefinition,
98
+ verify: { policy: "content-heuristic"; minSize?: number; pattern?: string },
99
+ ): VerificationOutcome {
100
+ const produces = step.produces;
101
+ if (!produces || produces.length === 0) {
102
+ return "continue";
103
+ }
104
+
105
+ for (const relPath of produces) {
106
+ const absPath = resolve(runDir, relPath);
107
+ // Path traversal guard
108
+ if (!absPath.startsWith(resolve(runDir) + sep) && absPath !== resolve(runDir)) {
109
+ return "pause";
110
+ }
111
+
112
+ // 1. File existence
113
+ if (!existsSync(absPath)) {
114
+ return "pause";
115
+ }
116
+
117
+ // 2. Minimum size check
118
+ if (verify.minSize !== undefined) {
119
+ const stat = statSync(absPath);
120
+ if (stat.size < verify.minSize) {
121
+ return "pause";
122
+ }
123
+ }
124
+
125
+ // 3. Pattern match check (with timeout guard against ReDoS)
126
+ if (verify.pattern !== undefined) {
127
+ const content = readFileSync(absPath, "utf-8");
128
+ try {
129
+ if (!new RegExp(verify.pattern).test(content)) {
130
+ return "pause";
131
+ }
132
+ } catch {
133
+ // Invalid regex at runtime — treat as verification failure
134
+ return "pause";
135
+ }
136
+ }
137
+ }
138
+
139
+ return "continue";
140
+ }
141
+
142
+ /**
143
+ * shell-command handler.
144
+ *
145
+ * Runs the command via `sh -c` with cwd set to the run directory
146
+ * and a 30-second timeout. Returns "continue" if exit code 0,
147
+ * "retry" otherwise (including timeout/signal kills).
148
+ *
149
+ * SECURITY: The command string comes from a frozen DEFINITION.yaml written
150
+ * at run-creation time. The trust boundary is the workflow definition author.
151
+ * Commands run with the same privileges as the GSD process. Only use
152
+ * shell-command verification with definitions you trust.
153
+ */
154
+ function handleShellCommand(
155
+ runDir: string,
156
+ verify: { policy: "shell-command"; command: string },
157
+ ): VerificationOutcome {
158
+ // Guard: reject commands containing shell expansion patterns that suggest injection
159
+ const dangerousPatterns = /\$\(|`|;\s*(rm|curl|wget|nc|bash|sh|eval)\b/;
160
+ if (dangerousPatterns.test(verify.command)) {
161
+ console.warn(
162
+ `custom-verification: shell-command contains suspicious pattern, skipping: ${verify.command}`,
163
+ );
164
+ return "pause";
165
+ }
166
+
167
+ const result = spawnSync("sh", ["-c", verify.command], {
168
+ cwd: runDir,
169
+ timeout: 30_000,
170
+ encoding: "utf-8",
171
+ stdio: "pipe",
172
+ env: { ...process.env, PATH: process.env.PATH },
173
+ });
174
+
175
+ if (result.status === 0) {
176
+ return "continue";
177
+ }
178
+
179
+ return "retry";
180
+ }