pi-crew 0.8.14 → 0.9.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 (86) hide show
  1. package/CHANGELOG.md +366 -0
  2. package/README.md +112 -2
  3. package/docs/FEATURE_INTAKE.md +1 -1
  4. package/docs/HARNESS.md +20 -19
  5. package/docs/PROJECT_REVIEW.md +132 -133
  6. package/docs/PROJECT_REVIEW_FIXES.md +130 -131
  7. package/docs/actions-reference.md +127 -121
  8. package/docs/architecture.md +1 -1
  9. package/docs/code-review-2026-05-11.md +134 -134
  10. package/docs/commands-reference.md +108 -106
  11. package/docs/comparison-pi-subagents-vs-pi-crew.md +105 -105
  12. package/docs/deep-review-report.md +1 -1
  13. package/docs/dynamic-workflows.md +90 -0
  14. package/docs/fixes/BATCH_A_H1_H2.md +17 -17
  15. package/docs/fixes/bug-007-async-notifier-stale-ctx.md +23 -23
  16. package/docs/followup-plan-2026-05-12.md +135 -135
  17. package/docs/followup-review-2026-05-12.md +86 -86
  18. package/docs/followup-review-round3-2026-05-12.md +123 -123
  19. package/docs/goals.md +59 -0
  20. package/docs/implementation-plan-top3.md +4 -4
  21. package/docs/issue-29-analysis.md +2 -2
  22. package/docs/oh-my-pi-research.md +154 -154
  23. package/docs/optimization-plan.md +2 -0
  24. package/docs/perf/baseline-2026-05.md +9 -9
  25. package/docs/perf/final-report-2026-05.md +2 -2
  26. package/docs/perf/sprint-1-report.md +2 -2
  27. package/docs/perf/sprint-2-report.md +1 -1
  28. package/docs/perf/upgrade-plan-2026-05.md +72 -72
  29. package/docs/pi-crew-bugs.md +230 -230
  30. package/docs/pi-crew-investigation-report.md +102 -102
  31. package/docs/pi-crew-test-round5.md +4 -4
  32. package/docs/runtime-analysis-child-vs-live.md +57 -57
  33. package/docs/runtime-migration-in-process-analysis.md +97 -97
  34. package/package.json +2 -4
  35. package/skills/orchestration/SKILL.md +11 -11
  36. package/src/agents/agent-config.ts +4 -0
  37. package/src/config/config.ts +39 -0
  38. package/src/config/types.ts +11 -0
  39. package/src/extension/action-suggestions.ts +2 -1
  40. package/src/extension/async-notifier.ts +10 -0
  41. package/src/extension/help.ts +14 -0
  42. package/src/extension/registration/commands.ts +27 -0
  43. package/src/extension/team-tool/destructive-gate.ts +1 -1
  44. package/src/extension/team-tool/goal-wrap.ts +288 -0
  45. package/src/extension/team-tool/goal.ts +405 -0
  46. package/src/extension/team-tool/run.ts +103 -4
  47. package/src/extension/team-tool/workflow-manage.ts +194 -0
  48. package/src/extension/team-tool.ts +20 -0
  49. package/src/hooks/types.ts +3 -1
  50. package/src/runtime/async-runner.ts +27 -2
  51. package/src/runtime/background-runner.ts +68 -19
  52. package/src/runtime/child-pi.ts +9 -1
  53. package/src/runtime/completion-guard.ts +1 -1
  54. package/src/runtime/dynamic-workflow-context.ts +450 -0
  55. package/src/runtime/dynamic-workflow-runner.ts +180 -0
  56. package/src/runtime/global-worker-cap.ts +96 -0
  57. package/src/runtime/goal-evaluator.ts +294 -0
  58. package/src/runtime/goal-loop-runner.ts +612 -0
  59. package/src/runtime/goal-state-store.ts +209 -0
  60. package/src/runtime/iteration-hooks.ts +2 -1
  61. package/src/runtime/pi-args.ts +10 -2
  62. package/src/runtime/post-checks.ts +2 -1
  63. package/src/runtime/result-extractor.ts +32 -0
  64. package/src/runtime/team-runner.ts +11 -1
  65. package/src/runtime/verification-gates.ts +88 -5
  66. package/src/runtime/verification-integrity.ts +110 -0
  67. package/src/runtime/verification-worktree.ts +136 -0
  68. package/src/runtime/workspace-lock.ts +448 -0
  69. package/src/schema/config-schema.ts +26 -0
  70. package/src/schema/team-tool-schema.ts +39 -4
  71. package/src/state/atomic-write.ts +9 -0
  72. package/src/state/contracts.ts +14 -0
  73. package/src/state/crew-init.ts +18 -5
  74. package/src/state/event-log.ts +7 -1
  75. package/src/state/state-store.ts +2 -0
  76. package/src/state/types.ts +82 -0
  77. package/src/state/worker-atomic-writer.ts +190 -0
  78. package/src/utils/env-allowlist.ts +30 -0
  79. package/src/utils/redaction.ts +104 -24
  80. package/src/utils/safe-paths.ts +55 -14
  81. package/src/workflows/discover-workflows.ts +25 -1
  82. package/src/workflows/workflow-config.ts +13 -0
  83. package/src/worktree/cleanup.ts +2 -1
  84. package/src/worktree/worktree-manager.ts +4 -3
  85. package/teams/parallel-research.team.md +1 -1
  86. package/workflows/examples/hello.dwf.ts +24 -0
@@ -15,7 +15,7 @@ Use this skill when orchestrating multi-phase tasks across pi-crew teams and wor
15
15
 
16
16
  ## Role definition
17
17
 
18
- You are the orchestrator — bạn là người điều phối, không phải người thực thi.
18
+ You are the orchestrator — the coordinator, not the executor.
19
19
 
20
20
  You decompose, dispatch, verify, and iterate. You do NOT edit code directly. If you find yourself opening a file to fix a typo "real quick," stop — spawn a worker instead.
21
21
 
@@ -25,35 +25,35 @@ Adapted from oh-my-pi's orchestrate command pattern for pi-crew context.
25
25
 
26
26
  ### 1. Do not yield until everything is closed
27
27
 
28
- Không trả lại control khi vẫn còn việc chưa xong. Run every phase to completion. The orchestrator owns the full lifecycle — from first dispatch to final green gate.
28
+ Do not yield control while work remains unfinished. Run every phase to completion. The orchestrator owns the full lifecycle — from first dispatch to final green gate.
29
29
 
30
30
  ### 2. Enumerate the full surface before dispatching
31
31
 
32
- Before writing any task packet, read every referenced file and understand the complete work surface. Liệt toàn bộ surface trước khi giao việc không giao việc khi chưa hiểu hết scope.
32
+ Before writing any task packet, read every referenced file and understand the complete work surface. Enumerate the entire surface before assigning workdo not assign work before you fully understand the scope.
33
33
 
34
34
  ### 3. Parallelize maximally
35
35
 
36
- Every set of edits with disjoint file scope MUST ship as one batch. Nếu 5 tasks chỉnh 5 file khác nhau không phụ thuộc nhau, dispatch tất cả cùng lúc. Never serialize what can be parallelized.
36
+ Every set of edits with disjoint file scope MUST ship as one batch. If 5 tasks edit 5 different files and are independent of one another, dispatch all of them at once. Never serialize what can be parallelized.
37
37
 
38
38
  ### 4. Each task assignment is self-contained
39
39
 
40
- Subagents have no shared context. Mỗi worker chỉ biết những bạn ghi trong task packet. Include all necessary context, file paths, constraints, and acceptance criteria in every task.
40
+ Subagents have no shared context. Each worker only knows what you write in the task packet. Include all necessary context, file paths, constraints, and acceptance criteria in every task.
41
41
 
42
42
  ### 5. Verify after every phase before launching the next
43
43
 
44
- Run appropriate gates between phases: typecheck, tests, lint. Không bỏ qua verification — một phase đỏ không được phép chuyển sang phase tiếp theo.
44
+ Run appropriate gates between phases: typecheck, tests, lint. Do not skip verification — a red phase must not advance to the next phase.
45
45
 
46
46
  ### 6. Commit policy — green only
47
47
 
48
- Commit after each green phase. Never commit a red tree. Chỉ commit khi tất cả gates pass. If the phase fails, fix it first.
48
+ Commit after each green phase. Never commit a red tree. Only commit when all gates pass. If the phase fails, fix it first.
49
49
 
50
50
  ### 7. Respawn, do not absorb
51
51
 
52
- If a subagent returns incomplete or broken work, spawn a corrective subagent with a focused fix-up task packet. Không tự sửa lỗi của worker — respawn worker mới để sửa.
52
+ If a subagent returns incomplete or broken work, spawn a corrective subagent with a focused fix-up task packet. Do not fix a worker's mistakes yourself — respawn a new worker to fix them.
53
53
 
54
54
  ### 8. No scope creep, no scope shrink
55
55
 
56
- Maintain the original scope exactly. Không mở rộng scope "thấy thêm việc," cũng không thu hẹp "tạm đủ." If scope needs to change, escalate to the requester.
56
+ Maintain the original scope exactly. Do not expand scope because you "spot more work," and do not shrink it because "it's good enough for now." If scope needs to change, escalate to the requester.
57
57
 
58
58
  ## Workflow (7 steps)
59
59
 
@@ -69,7 +69,7 @@ Maintain the original scope exactly. Không mở rộng scope vì "thấy thêm
69
69
  - Materialize the full work surface as ordered phases.
70
70
  - For each phase, enumerate: files to touch, workers needed, dependencies on other phases.
71
71
  - Phases must be ordered by dependency; tasks within a phase must be independent (disjoint file scope).
72
- - Write the plan down — không giữ plan trong head.
72
+ - Write the plan down — do not keep the plan in your head.
73
73
 
74
74
  ### Step 3 — Dispatch phase
75
75
 
@@ -117,7 +117,7 @@ If ANY answer is NO → Stop. Complete planning before dispatching.
117
117
 
118
118
  ## Anti-patterns
119
119
 
120
- These are the behaviours that kill orchestration quality — tránh xa:
120
+ These are the behaviours that kill orchestration quality — avoid them:
121
121
 
122
122
  | Anti-pattern | Why it's wrong |
123
123
  |---|---|
@@ -92,6 +92,10 @@ export interface AgentConfig {
92
92
  effort?: "low" | "medium" | "high";
93
93
  /** Tools to explicitly forbid for this agent. Takes precedence over allowedTools. */
94
94
  disallowedTools?: string[];
95
+ /** Disable ALL tools (Pi `--no-tools`). Used by capability-locked agents like the goal-judge (P1)
96
+ * that must have NO agency — only emit a verdict. §0c C6: an empty `tools:[]` is INSUFFICIENT
97
+ * because pi-args.ts skips empty arrays, leaving default tools enabled. */
98
+ disableTools?: boolean;
95
99
  disabled?: boolean;
96
100
  override?: { source: "config"; path: string };
97
101
  }
@@ -60,6 +60,7 @@ import type {
60
60
  CrewToolsConfig,
61
61
  CrewUiConfig,
62
62
  CrewWorktreeConfig,
63
+ GoalWrapWorkflowConfig,
63
64
  LoadedPiTeamsConfig,
64
65
  PiTeamsAutonomousConfig,
65
66
  PiTeamsAutonomyProfile,
@@ -912,6 +913,43 @@ function parseWorktreeConfig(value: unknown): CrewWorktreeConfig | undefined {
912
913
  : undefined;
913
914
  }
914
915
 
916
+ /** Parse goalWrap config (RFC v0.5 vision: apply goal completion-guarantee to builtins). */
917
+ function parseGoalWrapConfig(
918
+ value: unknown,
919
+ ): Record<string, GoalWrapWorkflowConfig> | undefined {
920
+ const obj = asRecord(value);
921
+ if (!obj) return undefined;
922
+ const result: Record<string, GoalWrapWorkflowConfig> = {};
923
+ let hasAny = false;
924
+ for (const [workflowName, entry] of Object.entries(obj)) {
925
+ const entryObj = asRecord(entry);
926
+ if (!entryObj) continue;
927
+ const parsed: GoalWrapWorkflowConfig = {
928
+ enabled: parseWithSchema(Type.Boolean(), entryObj.enabled),
929
+ maxTurns: parseWithSchema(Type.Integer({ minimum: 1, maximum: 50 }), entryObj.maxTurns),
930
+ evaluatorModel: parseWithSchema(Type.String({ minLength: 1 }), entryObj.evaluatorModel),
931
+ budgetTotal: parseWithSchema(Type.Integer({ minimum: 1000 }), entryObj.budgetTotal),
932
+ budgetUnlimited: parseWithSchema(Type.Boolean(), entryObj.budgetUnlimited),
933
+ };
934
+ // Parse verification sub-object.
935
+ const verObj = asRecord(entryObj.verification);
936
+ if (verObj) {
937
+ const commands = Array.isArray(verObj.commands)
938
+ ? verObj.commands.filter((c): c is string => typeof c === "string" && c.length > 0)
939
+ : undefined;
940
+ const mode = verObj.mode === "text-only" ? "text-only" as const : undefined;
941
+ if (commands || mode) {
942
+ parsed.verification = { ...(commands ? { commands } : { commands: [] }), ...(mode ? { mode } : {}) };
943
+ }
944
+ }
945
+ if (Object.values(parsed).some((v) => v !== undefined)) {
946
+ result[workflowName] = parsed;
947
+ hasAny = true;
948
+ }
949
+ }
950
+ return hasAny ? result : undefined;
951
+ }
952
+
915
953
  function parseAgentOverride(value: unknown): AgentOverrideConfig | undefined {
916
954
  const obj = asRecord(value);
917
955
  if (!obj) return undefined;
@@ -1232,6 +1270,7 @@ export function parseConfig(raw: unknown): PiTeamsConfig {
1232
1270
  runtime: parseRuntimeConfig(obj.runtime),
1233
1271
  control: parseControlConfig(obj.control),
1234
1272
  worktree: parseWorktreeConfig(obj.worktree),
1273
+ goalWrap: parseGoalWrapConfig(obj.goalWrap),
1235
1274
  agents: parseAgentsConfig(obj.agents),
1236
1275
  tools: parseToolsConfig(obj.tools),
1237
1276
  telemetry: parseTelemetryConfig(obj.telemetry),
@@ -84,6 +84,16 @@ export interface CrewWorktreeConfig {
84
84
  seedPaths?: string[];
85
85
  }
86
86
 
87
+ /** Goal-wrap config (RFC v0.5 vision: apply `goal` completion-guarantee to builtin workflows). */
88
+ export interface GoalWrapWorkflowConfig {
89
+ enabled?: boolean;
90
+ maxTurns?: number;
91
+ evaluatorModel?: string;
92
+ verification?: { commands: string[]; mode?: "text-only" };
93
+ budgetTotal?: number;
94
+ budgetUnlimited?: boolean;
95
+ }
96
+
87
97
  export interface CrewUiConfig {
88
98
  widgetPlacement?: "aboveEditor" | "belowEditor";
89
99
  widgetMaxLines?: number;
@@ -217,6 +227,7 @@ export interface PiTeamsConfig {
217
227
  runtime?: CrewRuntimeConfig;
218
228
  control?: CrewControlConfig;
219
229
  worktree?: CrewWorktreeConfig;
230
+ goalWrap?: Record<string, GoalWrapWorkflowConfig>;
220
231
  agents?: CrewAgentsConfig;
221
232
  tools?: CrewToolsConfig;
222
233
  telemetry?: CrewTelemetryConfig;
@@ -28,7 +28,8 @@ export const KNOWN_TEAM_ACTIONS = [
28
28
  "config", "init", "recommend", "autonomy", "api", "settings", "steer",
29
29
  "invalidate", "health", "graph", "onboard", "explain", "cache",
30
30
  "checkpoint", "search", "orchestrate", "schedule", "scheduled", "anchor",
31
- "auto-summarize", "auto_boomerang",
31
+ "auto-summarize", "auto_boomerang", "goal",
32
+ "workflow-create", "workflow-get", "workflow-list", "workflow-save", "workflow-delete",
32
33
  ] as const;
33
34
 
34
35
  /**
@@ -110,6 +110,16 @@ export function startAsyncRunNotifier(ctx: ExtensionContext, state: AsyncNotifie
110
110
  const current = markDeadAsyncRunIfNeeded(run) ?? run;
111
111
  if (!isFinished(current.status) || state.seenFinishedRunIds.has(current.runId)) continue;
112
112
  state.seenFinishedRunIds.add(current.runId);
113
+ // Suppress notifications for INTERNAL goal-loop sub-runs.
114
+ // The outer goal-loop creates a synthetic 'goal-turn' workflow per turn
115
+ // (see buildTurnWorkflow in goal-loop-runner.ts). These runs are
116
+ // implementation details of the autonomous loop — the user only cares
117
+ // about the OUTER goal-loop's status (runKind:'goal-loop'), which has
118
+ // its own event stream + status command. Without this filter, every
119
+ // turn that hits e.g. a transient model rate limit triggers an
120
+ // alarming 'Error: pi-crew run failed' toast for an internal sub-run
121
+ // the user never started directly.
122
+ if (current.workflow === "goal-turn" && current.team.startsWith("goal-")) continue;
113
123
  const level = current.status === "completed" ? "info" : current.status === "cancelled" ? "warning" : "error";
114
124
  ctx.ui.notify(`pi-crew run ${current.status}: ${current.runId} (${current.team}/${current.workflow ?? "none"})`, level);
115
125
  }
@@ -43,6 +43,20 @@ export function piTeamsHelp(): string {
43
43
  "- /team-validate",
44
44
  "- /team-help",
45
45
  "",
46
+ "Goal loops (P0/P1 — autonomous goal loop):",
47
+ "- team action='goal' config.subAction='start' config.objective='...' config.evaluatorModel='...' [config.maxTurns=20] [budgetTotal=N]",
48
+ "- team action='goal' config.subAction='status' [config.goalId=<id>]",
49
+ "- team action='goal' config.subAction='pause|resume|stop|step|clear' config.goalId=<id>",
50
+ "",
51
+ "Dynamic workflows (P2/P3 — JS-script orchestration):",
52
+ "- Place a .dwf.ts under .crew/workflows/ then: team action='run' workflow='<name>' goal='...'",
53
+ "- team action='workflow-list'",
54
+ "- team action='workflow-get' config.name='<name>'",
55
+ "- team action='workflow-create' confirm=true config.name='<name>' config.script='<.dwf.ts source>' (USER-initiated only; ACE-gated)",
56
+ "- team action='workflow-save' config.name='<name>' config.script='<source>'",
57
+ "- team action='workflow-delete' confirm=true config.name='<name>'",
58
+ "- /workflows — list dynamic workflows",
59
+ "",
46
60
  "Real child workers are enabled by default. Use runtime.mode=scaffold or executeWorkers=false only for dry runs.",
47
61
  ].join("\n");
48
62
  }
@@ -381,6 +381,33 @@ export function registerTeamCommands(pi: ExtensionAPI, deps: RegisterTeamCommand
381
381
  },
382
382
  });
383
383
 
384
+ pi.registerCommand("team-goal", {
385
+ description: "Autonomous goal loop control: [start|status|pause|resume|stop|step|clear] [goalId] [--objective=...] [--evaluatorModel=...] [--maxTurns=N]",
386
+ handler: async (args: string, ctx: ExtensionCommandContext) => {
387
+ const tokens = args.trim().split(/\s+/).filter(Boolean);
388
+ const knownSubs = new Set(["start", "status", "pause", "resume", "stop", "step", "clear", "cancel", "reset"]);
389
+ const subAction = tokens[0] && knownSubs.has(tokens[0]) ? tokens[0] : "status";
390
+ const positional = tokens.filter((token) => !token.includes("=") && !token.startsWith("--") && token !== subAction);
391
+ const goalId = positional[0];
392
+ const config: Record<string, unknown> = { subAction };
393
+ if (goalId) config.goalId = goalId;
394
+ for (const token of tokens.filter((item) => item.includes("="))) {
395
+ const [key, ...rest] = token.split("=");
396
+ if (key) config[key.replace(/^--/, "")] = parseScalar(rest.join("="));
397
+ }
398
+ const result = await handleTeamTool({ action: "goal", config }, teamCommandContext(ctx));
399
+ await notifyCommandResult(ctx, commandText(result));
400
+ },
401
+ });
402
+
403
+ pi.registerCommand("workflows", {
404
+ description: "List all workflows (static + dynamic .dwf.ts)",
405
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
406
+ const result = await handleTeamTool({ action: "workflow-list" }, teamCommandContext(ctx));
407
+ await notifyCommandResult(ctx, commandText(result));
408
+ },
409
+ });
410
+
384
411
  pi.registerCommand("team-metrics", { description: "Show pi-crew metrics snapshot: [filter]", handler: async (args: string, ctx: ExtensionCommandContext) => {
385
412
  const filter = args.trim() || undefined;
386
413
  const result = await handleTeamTool({ action: "api", config: { operation: "metrics-snapshot", filter } }, { ...teamCommandContext(ctx), metricRegistry: deps.getMetricRegistry?.() });
@@ -20,7 +20,7 @@
20
20
  * 5. Otherwise → blocked with a reason telling the user what to pass.
21
21
  */
22
22
 
23
- export const DESTRUCTIVE_TEAM_ACTIONS = new Set(["delete", "forget", "prune", "cleanup"]);
23
+ export const DESTRUCTIVE_TEAM_ACTIONS = new Set(["delete", "forget", "prune", "cleanup", "workflow-create", "workflow-save", "workflow-delete"]);
24
24
 
25
25
  export interface TeamToolInputLike {
26
26
  action?: unknown;
@@ -0,0 +1,288 @@
1
+ /**
2
+ * goal-wrap.ts — Apply the `goal` completion-guarantee to builtin workflows.
3
+ *
4
+ * RFC v0.5 vision: when `goalWrap[workflowName].enabled` is set in .crew/config.json,
5
+ * the builtin workflow runs as the WORKER TURN inside a goal loop (worker → judge →
6
+ * feedback → redo until achieved / maxTurns / budget / stuck).
7
+ *
8
+ * Design (A + D confirmed with user):
9
+ * - OUTER wrap: the builtin workflow IS the worker turn (judge evaluates the whole thing)
10
+ * - KEEP .workflow.md: no convert; the existing adaptive planner (e.g. `implementation`)
11
+ * keeps its flexibility; we just re-run it per turn with the judge's feedback.
12
+ * - Per-workflow toggle via team-setting config.
13
+ *
14
+ * Reuses the Phase 1 goal infrastructure: GoalStore, GoalLoopState, runGoalLoop. The
15
+ * builtin workflow's per-turn execution goes through executeTeamRun (same as a normal
16
+ * goal worker turn), so Phase 1's protections (integrity snapshot, budget guard,
17
+ * nonce-token feedback, worker cap, workspace lock) all apply.
18
+ */
19
+
20
+ import { createRunPaths, saveRunManifest } from "../../state/state-store.ts";
21
+ import { atomicWriteJson } from "../../state/atomic-write.ts";
22
+ import { appendEvent } from "../../state/event-log.ts";
23
+ import { spawnBackgroundTeamRun } from "../../subagents/async-entry.ts";
24
+ import { GoalStore } from "../../runtime/goal-state-store.ts";
25
+ import { snapshotManifests } from "../../runtime/verification-integrity.ts";
26
+ import { logInternalError } from "../../utils/internal-error.ts";
27
+ import { loadConfig } from "../../config/config.ts";
28
+ import type { GoalLoopState, TeamRunManifest } from "../../state/types.ts";
29
+ import type { GoalWrapWorkflowConfig } from "../../config/types.ts";
30
+ import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
31
+ import type { WorkflowConfig } from "../../workflows/workflow-config.ts";
32
+ import { result, type TeamContext } from "./context.ts";
33
+
34
+ /** Builtin workflows eligible for goal-wrap (have a clear "done" condition). */
35
+ export const GOAL_WRAP_ELIGIBLE_BUILTINS = new Set([
36
+ "implementation",
37
+ "fast-fix",
38
+ "default",
39
+ ]);
40
+
41
+ /**
42
+ * Maximum number of workflow steps allowed for goal-wrap.
43
+ *
44
+ * Multi-step workflows (>=2 steps in their .workflow.md definition) crash
45
+ * non-deterministically when run as goal-wrap worker turns in the background
46
+ * goal-loop process. Root cause: V8/libuv-level race during event-loop yields
47
+ * in the team-runner batch transition (see investigation commit a9f6e09 +
48
+ * Phase 1.5 RFC 15). Sync fs workarounds regress; worker-thread isolation
49
+ * doesn't help. The crash is NOT in fs writes.
50
+ *
51
+ * Decision: REFUSE goal-wrap for multi-step workflows rather than ship a
52
+ * feature with hidden non-deterministic crashes. Single-step workflows
53
+ * (e.g. implementation, which has only the adaptive `assess` step and
54
+ * injects more tasks via the adaptive planner) work reliably end-to-end.
55
+ *
56
+ * Users who want goal completion-guarantee on multi-step work should use
57
+ * `team action='run' workflow=<multi-step>` for the one-shot execution, or
58
+ * break the work into single-step goals.
59
+ */
60
+ export const GOAL_WRAP_MAX_STEPS = 1;
61
+
62
+ // GoalWrapWorkflowConfig is re-exported from config/types.ts (single source of truth).
63
+ export type { GoalWrapWorkflowConfig };
64
+
65
+ /** Read the goal-wrap config for a given workflow name (merged user + project config). */
66
+ export function readGoalWrapConfig(
67
+ cwd: string,
68
+ workflowName: string,
69
+ ): GoalWrapWorkflowConfig | undefined {
70
+ const loaded = loadConfig(cwd);
71
+ const cfg = loaded.config?.goalWrap as Record<string, GoalWrapWorkflowConfig> | undefined;
72
+ if (!cfg) return undefined;
73
+ return cfg[workflowName];
74
+ }
75
+
76
+ /** Is goal-wrap enabled for this workflow (per config)? */
77
+ export function isGoalWrapEnabled(cwd: string, workflowName: string): boolean {
78
+ if (!GOAL_WRAP_ELIGIBLE_BUILTINS.has(workflowName)) return false;
79
+ const wc = readGoalWrapConfig(cwd, workflowName);
80
+ return wc?.enabled === true;
81
+ }
82
+
83
+ /**
84
+ * Validate a goal-wrap config entry. Returns an error string if invalid, undefined if OK.
85
+ * Mirrors the Phase 1 validation: budget required (or unlimited), evaluatorModel required.
86
+ */
87
+ export function validateGoalWrapConfig(
88
+ wc: GoalWrapWorkflowConfig,
89
+ ): string | undefined {
90
+ if (!wc.evaluatorModel) {
91
+ return "goalWrap config requires evaluatorModel (the goal-judge model). No silent default.";
92
+ }
93
+ const hasBudget = typeof wc.budgetTotal === "number" && wc.budgetTotal >= 1000;
94
+ if (!wc.budgetUnlimited && !hasBudget) {
95
+ return "goalWrap config requires either budgetTotal (>=1000) OR budgetUnlimited:true. No silent unbounded-spend default.";
96
+ }
97
+ if (wc.budgetUnlimited && hasBudget) {
98
+ return "goalWrap config: budgetTotal and budgetUnlimited are mutually exclusive.";
99
+ }
100
+ return undefined;
101
+ }
102
+
103
+ /**
104
+ * Start a goal-wrapped run. Creates a GoalLoopState + goal-loop manifest + spawns the
105
+ * background goal-loop process. The worker turn's workflow is the resolved builtin.
106
+ *
107
+ * The goal-loop-runner's buildTurnWorkflow() generates a 1-step "goal-turn" workflow; for
108
+ * goal-wrap we OVERRIDE that by storing the target workflow name on the GoalLoopState
109
+ * and having the runner use it per turn. (See the `team` field carry-through.)
110
+ */
111
+ /**
112
+ * Persist `async: { pid, logPath, spawnedAt }` on a goal-loop manifest and write
113
+ * it atomically to disk. This is the missing piece that makes goal-loop runs
114
+ * detectable by async-notifier.markDeadAsyncRunIfNeeded — without it, the
115
+ * notifier returns early on `!run.async` and the goal appears to hang at "1/3"
116
+ * forever even after the background runner has died.
117
+ *
118
+ * Mirrors the normal-run path in run.ts:371-372 which writes the async field on
119
+ * the team-run manifest via `atomicWriteJson(paths.manifestPath, asyncManifest)`.
120
+ */
121
+ export function persistAsyncOnGoalLoopManifest(
122
+ manifestPath: string,
123
+ manifest: TeamRunManifest,
124
+ spawned: { pid: number; logPath: string },
125
+ ): void {
126
+ const asyncGoalManifest = {
127
+ ...manifest,
128
+ async: { pid: spawned.pid, logPath: spawned.logPath, spawnedAt: new Date().toISOString() },
129
+ };
130
+ atomicWriteJson(manifestPath, asyncGoalManifest);
131
+ }
132
+ /**
133
+ * Decide whether a workflow should be goal-wrapped.
134
+ *
135
+ * Returns one of:
136
+ * { enabled: true } — goal-wrap is enabled AND safe
137
+ * { enabled: false, reason: "config-off" } — goal-wrap not enabled in config
138
+ * { enabled: false, reason: "multi-step" } — enabled but refused (multi-step
139
+ * workflows crash non-deterministically
140
+ * in the background goal-loop process
141
+ * due to V8/libuv race; see GOAL_WRAP_MAX_STEPS)
142
+ * { enabled: false, reason: "invalid-config" } — config present but invalid
143
+ *
144
+ * Use this in run.ts to decide whether to route to startGoalWrappedRun or fall
145
+ * through to the normal team-run path. NEVER refuse with an error — when
146
+ * goal-wrap is unsafe for a given workflow, silently fall back so the user
147
+ * still gets the workflow run they asked for.
148
+ */
149
+ export function shouldGoalWrap(cwd: string, workflow: WorkflowConfig): { enabled: true } | { enabled: false; reason: "config-off" | "multi-step" | "invalid-config"; message?: string } {
150
+ const wc = readGoalWrapConfig(cwd, workflow.name);
151
+ if (!wc || wc.enabled !== true) {
152
+ return { enabled: false, reason: "config-off" };
153
+ }
154
+ const validationError = validateGoalWrapConfig(wc);
155
+ if (validationError) {
156
+ return { enabled: false, reason: "invalid-config", message: validationError };
157
+ }
158
+ if (workflow.steps.length > GOAL_WRAP_MAX_STEPS) {
159
+ return {
160
+ enabled: false,
161
+ reason: "multi-step",
162
+ message: `goal-wrap disabled for '${workflow.name}' (${workflow.steps.length} steps): multi-step workflows crash non-deterministically in the background goal-loop (V8/libuv race). Running as a normal one-shot team run instead.`,
163
+ };
164
+ }
165
+ return { enabled: true };
166
+ }
167
+
168
+ export async function startGoalWrappedRun(
169
+ params: TeamToolParamsValue,
170
+ ctx: TeamContext,
171
+ workflow: WorkflowConfig,
172
+ goal: string,
173
+ ): Promise<ReturnType<typeof result>> {
174
+ const cwd = ctx.cwd;
175
+ const wc = readGoalWrapConfig(cwd, workflow.name);
176
+ if (!wc || wc.enabled !== true) {
177
+ return result(`goal-wrap is not enabled for workflow '${workflow.name}' in .crew/config.json.`, { action: "run", status: "error" }, true);
178
+ }
179
+ const validationError = validateGoalWrapConfig(wc);
180
+ if (validationError) {
181
+ return result(`Invalid goalWrap config for '${workflow.name}': ${validationError}`, { action: "run", status: "error" }, true);
182
+ }
183
+
184
+ try {
185
+ const store = new GoalStore(cwd);
186
+ const goalId = store.createGoalId();
187
+ const ownerSessionId = ctx.sessionId ?? "unknown";
188
+ const now = new Date().toISOString();
189
+ const maxTurns = typeof wc.maxTurns === "number" && wc.maxTurns > 0 ? wc.maxTurns : 5; // goal-wrap default: tighter than standalone goal's 20
190
+
191
+ // P1a integrity snapshot (only when verification.commands declared).
192
+ const verification = wc.verification;
193
+ const isTextOnly = verification?.mode === "text-only" || !verification?.commands?.length;
194
+ let verificationIntegrity: GoalLoopState["verificationIntegrity"];
195
+ if (isTextOnly) {
196
+ verificationIntegrity = "none-text-only";
197
+ } else {
198
+ try {
199
+ verificationIntegrity = { snapshot: snapshotManifests(cwd), takenAt: now };
200
+ } catch (error) {
201
+ logInternalError("goal-wrap.integritySnapshot", error, `goalId=${goalId}`);
202
+ verificationIntegrity = "none-text-only";
203
+ }
204
+ }
205
+
206
+ const goalState: GoalLoopState = {
207
+ goalId,
208
+ ownerSessionId,
209
+ objective: goal,
210
+ state: "running",
211
+ maxTurns,
212
+ turnsUsed: 0,
213
+ budgetTotal: typeof wc.budgetTotal === "number" ? wc.budgetTotal : undefined,
214
+ budgetUnlimited: wc.budgetUnlimited || undefined,
215
+ budgetWarning: 0.8,
216
+ budgetAbort: 0.95,
217
+ budgetUsed: 0,
218
+ verificationIntegrity,
219
+ verification: verification as { commands: string[]; allowManualEvidence?: boolean } | undefined,
220
+ evaluatorModel: wc.evaluatorModel!,
221
+ workerAgent: params.agent ?? "executor",
222
+ workerModel: typeof params.model === "string" ? params.model : undefined,
223
+ team: typeof params.team === "string" ? params.team : undefined,
224
+ cwd,
225
+ verdicts: [],
226
+ history: [],
227
+ createdAt: now,
228
+ updatedAt: now,
229
+ };
230
+ // Carry the target workflow name so the runner uses it (not the default goal-turn).
231
+ // Stored on the state via a documented extension field.
232
+ (goalState as GoalLoopState & { goalWrapWorkflow?: string }).goalWrapWorkflow = workflow.name;
233
+ store.save(goalState);
234
+
235
+ const paths = createRunPaths(cwd, goalId);
236
+ const goalLoopManifest: TeamRunManifest = {
237
+ schemaVersion: 1,
238
+ runId: goalId,
239
+ sessionId: ownerSessionId,
240
+ team: `goal-wrap-${goalId}`,
241
+ workflow: "goal-loop",
242
+ goal,
243
+ status: "queued",
244
+ workspaceMode: "single",
245
+ createdAt: now,
246
+ updatedAt: now,
247
+ cwd,
248
+ stateRoot: paths.stateRoot,
249
+ artifactsRoot: paths.artifactsRoot,
250
+ tasksPath: paths.tasksPath,
251
+ eventsPath: paths.eventsPath,
252
+ artifacts: [],
253
+ ownerSessionId,
254
+ runKind: "goal-loop",
255
+ };
256
+ saveRunManifest(goalLoopManifest);
257
+ appendEvent(paths.eventsPath, { type: "goal.loop_start", runId: goalId, data: { goalId, objective: goal, maxTurns, goalWrapWorkflow: workflow.name } });
258
+
259
+ const spawned = await spawnBackgroundTeamRun(goalLoopManifest);
260
+ const pid = spawned.pid ?? 0;
261
+ // FIX: persist async.pid on the OUTER goal-loop manifest (not just goal state).
262
+ // Without this, async-notifier.markDeadAsyncRunIfNeeded returns early on
263
+ // `!run.async` and the user sees the goal hang at "1/3" forever even after the
264
+ // background runner dies (it currently dies silently due to a multi-step
265
+ // atomic-write bug — see investigation report). Mirrors run.ts:371-372 which
266
+ // writes asyncManifest = { ...effectiveManifest, async: {...} } to manifestPath.
267
+ persistAsyncOnGoalLoopManifest(paths.manifestPath, goalLoopManifest, { pid, logPath: spawned.logPath });
268
+ const withAsync = { ...goalState, async: { pid, logPath: spawned.logPath, spawnedAt: new Date().toISOString() } };
269
+ store.save(withAsync);
270
+
271
+ return result(
272
+ [
273
+ `Goal-wrapped '${workflow.name}' started (background pid=${pid}).`,
274
+ `Goal ${goalId} [running] — worker = '${workflow.name}' workflow, judged each turn by ${wc.evaluatorModel}.`,
275
+ ` turn: 0/${maxTurns} budget: ${wc.budgetUnlimited ? "∞ (unlimited)" : `${wc.budgetTotal}`}`,
276
+ verification?.commands?.length ? ` verification: ${verification.commands.join(", ")}` : " verification: text-only (no objective oracle)",
277
+ ``,
278
+ `Next: \`team action='goal' config.subAction='status' config.goalId='${goalId}'\`.`,
279
+ `Log: ${spawned.logPath}`,
280
+ ].join("\n"),
281
+ { action: "run", status: "ok", runId: goalId, artifactsRoot: paths.artifactsRoot, data: { goalId, goalWrap: true, workflow: workflow.name, pid } },
282
+ false,
283
+ );
284
+ } catch (error) {
285
+ const message = error instanceof Error ? error.message : String(error);
286
+ return result(`goal-wrap start failed: ${message}`, { action: "run", status: "error" }, true);
287
+ }
288
+ }