conductor-remote 1.117.0 → 1.118.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 (200) hide show
  1. package/README.md +10 -8
  2. package/dist/assets/{PierrePatch-D3_YBIAr.js → PierrePatch-Dh-08CLI.js} +1 -1
  3. package/dist/assets/index-BRzsSW8g.css +1 -0
  4. package/dist/assets/index-BpY3Jhxk.js +65 -0
  5. package/dist/index.html +2 -2
  6. package/dist/push-sw.js +2 -2
  7. package/dist/sw.js +1 -1
  8. package/dist-node/scripts/nosleep-setup.js +2 -2
  9. package/dist-node/scripts/nosleep.js +3 -3
  10. package/dist-node/scripts/service/commands.js +32 -0
  11. package/dist-node/scripts/service/configuration.js +205 -0
  12. package/dist-node/scripts/service/environment.js +59 -0
  13. package/dist-node/scripts/service/flags.js +45 -0
  14. package/dist-node/scripts/service/installation.js +91 -0
  15. package/dist-node/scripts/service/launch-agent.js +134 -0
  16. package/dist-node/scripts/service/network.js +244 -0
  17. package/dist-node/scripts/service/presentation.js +101 -0
  18. package/dist-node/scripts/service/voice.js +113 -0
  19. package/dist-node/scripts/service.js +5 -980
  20. package/dist-node/src/{agent-config.js → agents/agent-config.js} +1 -1
  21. package/dist-node/src/{model-cache.js → agents/model-cache.js} +1 -1
  22. package/dist-node/src/{roles.js → agents/roles.js} +3 -3
  23. package/dist-node/src/db.js +1 -1
  24. package/dist-node/src/{parked.js → delivery/parked.js} +1 -1
  25. package/dist-node/src/{dev-server.js → dev-server/controller.js} +8 -291
  26. package/dist-node/src/dev-server/ports.js +63 -0
  27. package/dist-node/src/dev-server/processes.js +73 -0
  28. package/dist-node/src/dev-server/proxy.js +158 -0
  29. package/dist-node/src/{run-activity.js → dev-server/run-activity.js} +2 -2
  30. package/dist-node/src/dev-server/types.js +1 -0
  31. package/dist-node/src/{file-preview.js → files/file-preview.js} +1 -1
  32. package/dist-node/src/{change-stats.js → git/change-stats.js} +1 -1
  33. package/dist-node/src/{git.js → git/diff.js} +43 -18
  34. package/dist-node/src/{autoupdate.js → host/autoupdate.js} +1 -1
  35. package/dist-node/src/{funnel-watchdog.js → host/funnel-watchdog.js} +4 -4
  36. package/dist-node/src/{logbuf.js → host/logbuf.js} +1 -1
  37. package/dist-node/src/{nosleep-helper.js → host/nosleep-helper.js} +1 -1
  38. package/dist-node/src/{tailscale.js → host/tailscale.js} +1 -1
  39. package/dist-node/src/http/router-types.js +2 -0
  40. package/dist-node/src/http/router.js +78 -0
  41. package/dist-node/src/http/routes/create-workspace.js +90 -0
  42. package/dist-node/src/http/routes/files.js +53 -0
  43. package/dist-node/src/http/routes/prompts.js +363 -0
  44. package/dist-node/src/http/routes/sessions.js +279 -0
  45. package/dist-node/src/http/routes/state.js +186 -0
  46. package/dist-node/src/http/routes/system.js +220 -0
  47. package/dist-node/src/http/routes/voice.js +117 -0
  48. package/dist-node/src/http/routes/workflows.js +248 -0
  49. package/dist-node/src/http/routes/workspaces.js +267 -0
  50. package/dist-node/src/http/services/base.js +252 -0
  51. package/dist-node/src/http/services/delegations.js +301 -0
  52. package/dist-node/src/http/services/delivery.js +433 -0
  53. package/dist-node/src/http/services/files.js +199 -0
  54. package/dist-node/src/http/services/mcp.js +94 -0
  55. package/dist-node/src/http/services/responses.js +120 -0
  56. package/dist-node/src/http/services/voice.js +147 -0
  57. package/dist-node/src/http/services/workflow-probes.js +215 -0
  58. package/dist-node/src/http/services/workflow-state.js +195 -0
  59. package/dist-node/src/http/services/workflow.js +366 -0
  60. package/dist-node/src/http/services.js +35 -0
  61. package/dist-node/src/mcp/arguments.js +14 -0
  62. package/dist-node/src/mcp/dispatcher.js +56 -0
  63. package/dist-node/src/mcp/formatters.js +110 -0
  64. package/dist-node/src/mcp/protocol.js +7 -0
  65. package/dist-node/src/mcp/registry.js +66 -0
  66. package/dist-node/src/mcp/tools/agent-options.js +122 -0
  67. package/dist-node/src/mcp/tools/chat-actions.js +194 -0
  68. package/dist-node/src/mcp/tools/chats.js +194 -0
  69. package/dist-node/src/mcp/tools/delegation.js +135 -0
  70. package/dist-node/src/mcp/tools/system.js +135 -0
  71. package/dist-node/src/mcp/tools/voice.js +136 -0
  72. package/dist-node/src/mcp/tools/workspaces.js +259 -0
  73. package/dist-node/src/mcp/types.js +1 -0
  74. package/dist-node/src/mcp.js +6 -4
  75. package/dist-node/src/{notify.js → notifications/notify.js} +5 -5
  76. package/dist-node/src/{webpush.js → notifications/webpush.js} +1 -1
  77. package/dist-node/src/orchestration/delegation/codec.js +201 -0
  78. package/dist-node/src/{delegation-intake.js → orchestration/delegation/intake.js} +2 -2
  79. package/dist-node/src/{delegation-prompt.js → orchestration/delegation/prompt.js} +1 -1
  80. package/dist-node/src/orchestration/delegation/queue.js +255 -0
  81. package/dist-node/src/orchestration/delegation/store.js +111 -0
  82. package/dist-node/src/orchestration/delegation/types.js +2 -0
  83. package/dist-node/src/orchestration/persistence/capabilities.js +109 -0
  84. package/dist-node/src/orchestration/persistence/codecs.js +148 -0
  85. package/dist-node/src/orchestration/persistence/connection.js +205 -0
  86. package/dist-node/src/orchestration/persistence/db.js +101 -0
  87. package/dist-node/src/orchestration/persistence/effect-recovery.js +227 -0
  88. package/dist-node/src/orchestration/persistence/effect-transitions.js +157 -0
  89. package/dist-node/src/orchestration/persistence/effects.js +109 -0
  90. package/dist-node/src/orchestration/persistence/errors.js +39 -0
  91. package/dist-node/src/orchestration/persistence/events.js +29 -0
  92. package/dist-node/src/orchestration/persistence/idempotency.js +82 -0
  93. package/dist-node/src/orchestration/persistence/job-attempts.js +114 -0
  94. package/dist-node/src/orchestration/persistence/jobs.js +159 -0
  95. package/dist-node/src/orchestration/persistence/projections.js +190 -0
  96. package/dist-node/src/orchestration/persistence/quarantine.js +46 -0
  97. package/dist-node/src/orchestration/persistence/records.js +115 -0
  98. package/dist-node/src/orchestration/persistence/relays.js +78 -0
  99. package/dist-node/src/orchestration/persistence/runs.js +156 -0
  100. package/dist-node/src/{orchestration-schema.js → orchestration/persistence/schema.js} +1 -1
  101. package/dist-node/src/orchestration/persistence/types.js +1 -0
  102. package/dist-node/src/orchestration/persistence/ui-lease.js +197 -0
  103. package/dist-node/src/orchestration/persistence/values.js +114 -0
  104. package/dist-node/src/orchestration/workflow/commands.js +309 -0
  105. package/dist-node/src/orchestration/workflow/coordinator.js +28 -0
  106. package/dist-node/src/orchestration/workflow/effect-recovery.js +125 -0
  107. package/dist-node/src/orchestration/workflow/effects.js +307 -0
  108. package/dist-node/src/orchestration/workflow/errors.js +13 -0
  109. package/dist-node/src/orchestration/workflow/helpers.js +154 -0
  110. package/dist-node/src/orchestration/workflow/job-dispatch.js +312 -0
  111. package/dist-node/src/orchestration/workflow/job-results.js +280 -0
  112. package/dist-node/src/{workflow.js → orchestration/workflow/prompts.js} +2 -2
  113. package/dist-node/src/orchestration/workflow/root.js +293 -0
  114. package/dist-node/src/orchestration/workflow/start.js +99 -0
  115. package/dist-node/src/orchestration/workflow/state.js +105 -0
  116. package/dist-node/src/orchestration/workflow/types.js +1 -0
  117. package/dist-node/src/orchestration/workflow/wake.js +164 -0
  118. package/dist-node/src/prefs.js +1 -1
  119. package/dist-node/src/{background-tasks.js → reads/background-tasks.js} +1 -1
  120. package/dist-node/src/reads/messages.js +362 -0
  121. package/dist-node/src/reads/repository.js +87 -0
  122. package/dist-node/src/reads/sessions.js +165 -0
  123. package/dist-node/src/reads/types.js +1 -0
  124. package/dist-node/src/reads/workspaces.js +230 -0
  125. package/dist-node/src/reads/worktrees.js +45 -0
  126. package/dist-node/src/routes.js +5 -5
  127. package/dist-node/src/{search.js → search/coordinator.js} +4 -4
  128. package/dist-node/src/{search-worker.js → search/worker.js} +3 -3
  129. package/dist-node/src/server.js +14 -3930
  130. package/dist-node/src/settings.js +1 -1
  131. package/dist-node/src/shared.js +10 -10
  132. package/dist-node/src/{transcript.js → transcript/parser.js} +4 -4
  133. package/dist-node/src/{tool-usage-worker.js → usage/tool-usage-worker.js} +1 -1
  134. package/dist-node/src/{tool-usage.js → usage/tool-usage.js} +1 -1
  135. package/dist-node/src/voice/brief.js +22 -50
  136. package/dist-node/src/voice/context.js +1 -1
  137. package/dist-node/src/voice/dates.js +38 -0
  138. package/dist-node/src/voice/history.js +14 -5
  139. package/dist-node/src/voice/prompt.js +10 -9
  140. package/dist-node/src/voice/recall.js +115 -0
  141. package/dist-node/src/{speech.js → voice/speech.js} +1 -1
  142. package/dist-node/src/voice/tools.js +92 -3
  143. package/dist-node/src/voice/webrtc.js +1 -1
  144. package/dist-node/src/wire.js +2 -2
  145. package/dist-node/src/writes/actuator.js +124 -0
  146. package/dist-node/src/writes/agent-options.js +151 -0
  147. package/dist-node/src/writes/applescript/agent-options.applescript +334 -0
  148. package/dist-node/src/writes/applescript/chat-targeting.applescript +149 -0
  149. package/dist-node/src/writes/applescript/composer.applescript +195 -0
  150. package/dist-node/src/writes/applescript/hotspot.applescript +155 -0
  151. package/dist-node/src/writes/applescript/lifecycle.applescript +266 -0
  152. package/dist-node/src/writes/applescript/run-tasks.applescript +208 -0
  153. package/dist-node/src/writes/applescript/source.js +52 -0
  154. package/dist-node/src/writes/applescript/window.applescript +436 -0
  155. package/dist-node/src/writes/applescript/workspace-status.applescript +177 -0
  156. package/dist-node/src/writes/applescript/workspace-targeting.applescript +359 -0
  157. package/dist-node/src/writes/chats.js +160 -0
  158. package/dist-node/src/writes/guards.js +77 -0
  159. package/dist-node/src/writes/runner.js +67 -0
  160. package/dist-node/src/{sidecar.js → writes/sidecar.js} +1 -1
  161. package/dist-node/src/writes/system.js +81 -0
  162. package/dist-node/src/writes/targeting.js +121 -0
  163. package/dist-node/src/writes/types.js +1 -0
  164. package/dist-node/src/writes/ui-lock.js +229 -0
  165. package/dist-node/src/writes/workspaces.js +233 -0
  166. package/docs/voice-setup.md +34 -12
  167. package/package.json +2 -2
  168. package/dist/assets/index-ByTwVInL.css +0 -1
  169. package/dist/assets/index-LAm9X7wv.js +0 -65
  170. package/dist-node/src/conductor.applescript +0 -2279
  171. package/dist-node/src/delegations.js +0 -557
  172. package/dist-node/src/mcp-tools.js +0 -1319
  173. package/dist-node/src/orchestration-db.js +0 -2186
  174. package/dist-node/src/reads.js +0 -787
  175. package/dist-node/src/workflow-coordinator.js +0 -2119
  176. package/dist-node/src/writes.js +0 -1223
  177. /package/dist-node/src/{conductor-settings.js → agents/conductor-settings.js} +0 -0
  178. /package/dist-node/src/{firstprompt.js → delivery/firstprompt.js} +0 -0
  179. /package/dist-node/src/{sendonce.js → delivery/sendonce.js} +0 -0
  180. /package/dist-node/src/{preview-urls.js → dev-server/preview-urls.js} +0 -0
  181. /package/dist-node/src/{run-configs.js → dev-server/run-configs.js} +0 -0
  182. /package/dist-node/src/{attachments.js → files/attachments.js} +0 -0
  183. /package/dist-node/src/{icons.js → files/icons.js} +0 -0
  184. /package/dist-node/src/{staged-attachments.js → files/staged-attachments.js} +0 -0
  185. /package/dist-node/src/{fork-workspace.js → git/fork-workspace.js} +0 -0
  186. /package/dist-node/src/{merge.js → git/merge.js} +0 -0
  187. /package/dist-node/src/{pr.js → git/pr.js} +0 -0
  188. /package/dist-node/src/{nosleep.js → host/nosleep.js} +0 -0
  189. /package/dist-node/src/{relay-processes.js → host/relay-processes.js} +0 -0
  190. /package/dist-node/src/{ui-lease-watchdog.js → host/ui-lease-watchdog.js} +0 -0
  191. /package/dist-node/src/{wifi.js → host/wifi.js} +0 -0
  192. /package/dist-node/src/{orchestration-schema.generated.js → orchestration/persistence/schema.generated.js} +0 -0
  193. /package/dist-node/src/{workflow-effect-runner.js → orchestration/workflow/effect-runner.js} +0 -0
  194. /package/dist-node/src/{workflow-http.js → orchestration/workflow/http.js} +0 -0
  195. /package/dist-node/src/{workflow-machine.js → orchestration/workflow/machine.js} +0 -0
  196. /package/dist-node/src/{session-poller.js → reads/session-poller.js} +0 -0
  197. /package/dist-node/src/{context-breakdown.js → transcript/context-breakdown.js} +0 -0
  198. /package/dist-node/src/{chat-cursor.js → transcript/cursor.js} +0 -0
  199. /package/dist-node/src/{plan-usage.js → usage/plan-usage.js} +0 -0
  200. /package/dist-node/src/{tool-usage-service.js → usage/tool-usage-service.js} +0 -0
@@ -0,0 +1,312 @@
1
+ import { scrubWorkflowSecrets } from "../../shared.js";
2
+ import { WorkflowTransitionError } from "../persistence/db.js";
3
+ import { markReceiptLost } from "./effect-recovery.js";
4
+ import { runDurableEffect } from "./effects.js";
5
+ import { assertDeliveryReceipt, assertSessionReceipt, cleanUnknown, isDeliveryReceipt, messageReceipt, privateCorrelationBlock, sameRelay, workflowEffectCorrelationMarker } from "./helpers.js";
6
+ import { driveJobBaton, driveJobOutcome } from "./job-results.js";
7
+ import { isTerminalWorkflowJobState } from "./machine.js";
8
+ import { effectCall, effectReadCall, requireEffect } from "./state.js";
9
+ export async function driveNextJob(context, run) {
10
+ let jobs = context.db
11
+ .listWorkflowJobs(run.id)
12
+ .filter(job => job.cycle === run.cycle && job.role === (run.phase === 'exploring' ? 'exploration' : 'implementation'));
13
+ const queued = jobs.find(job => job.state === 'queued');
14
+ if (queued)
15
+ return Boolean(context.db.claimNextWorkflowJob(context.relay, run.id));
16
+ const owned = jobs.find(job => job.state === 'owned');
17
+ if (owned) {
18
+ if (!sameRelay(owned.owner, context.relay)) {
19
+ const recovered = context.db.reconcileAbandonedWorkflowJobClaim({
20
+ jobId: owned.id,
21
+ eventKey: `recover-job-claim:${owned.id}:${owned.attemptCount}`
22
+ });
23
+ return recovered.status === 'requeued';
24
+ }
25
+ await beginJobAttempt(context, run, owned);
26
+ return true;
27
+ }
28
+ jobs = context.db.listWorkflowJobs(run.id).filter(job => job.cycle === run.cycle);
29
+ for (const active of jobs.filter(job => !isTerminalWorkflowJobState(job.state) && job.state !== 'dormant')) {
30
+ if (await driveJob(context, run, active))
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+ export async function beginJobAttempt(context, run, job) {
36
+ if (!run.workspaceId)
37
+ throw new WorkflowTransitionError(`Workflow ${run.id} has no workspace for child creation`);
38
+ const sessionBaseline = cleanUnknown(await context.deps.captureSessionBaseline(run.workspaceId));
39
+ const latest = context.db.getWorkflowJob(job.id);
40
+ if (latest?.state !== 'owned' || !sameRelay(latest.owner, context.relay))
41
+ return;
42
+ const attemptNumber = job.attemptCount + 1;
43
+ const openAction = `${job.id}:open:${attemptNumber}`;
44
+ const configureAction = `${job.id}:configure:${attemptNumber}`;
45
+ const taskAction = `${job.id}:task:${attemptNumber}`;
46
+ const batonAction = `${job.id}:baton:${attemptNumber}`;
47
+ context.db.idempotentMutation('begin_workflow_job_attempt', `${run.id}:${job.id}:${attemptNumber}`, { jobId: job.id, attemptNumber, openAction, sessionBaseline }, () => {
48
+ context.db.createWorkflowJobAttempt({
49
+ jobId: job.id,
50
+ owner: context.relay,
51
+ state: 'opening',
52
+ effectIds: {
53
+ open: `${run.id}:${openAction}`,
54
+ configure: `${run.id}:${configureAction}`,
55
+ task: `${run.id}:${taskAction}`,
56
+ baton: `${run.id}:${batonAction}`
57
+ }
58
+ });
59
+ context.db.prepareWorkflowEffect({
60
+ id: `${run.id}:${openAction}`,
61
+ runId: run.id,
62
+ actionId: openAction,
63
+ kind: 'open_child',
64
+ jobId: job.id,
65
+ target: { workspaceId: run.workspaceId, rootSessionId: run.rootSessionId },
66
+ inputs: { correlationMarker: workflowEffectCorrelationMarker(run.id, openAction) },
67
+ baseline: sessionBaseline,
68
+ expectedCancellationGeneration: run.cancellationGeneration,
69
+ eventKey: `prepare:${openAction}`
70
+ });
71
+ context.db.updateWorkflowJob({
72
+ jobId: job.id,
73
+ expectedStates: ['owned'],
74
+ expectedCancellationGeneration: run.cancellationGeneration,
75
+ state: 'opening',
76
+ eventKey: `job-opening:${job.id}:${attemptNumber}`,
77
+ eventType: 'workflow_job_opening'
78
+ });
79
+ return { runId: run.id, jobId: job.id };
80
+ }, { runId: run.id, actionId: openAction });
81
+ }
82
+ export async function driveJob(context, run, job) {
83
+ if (job.state === 'opening')
84
+ return driveJobOpen(context, run, job);
85
+ if (job.state === 'configuring')
86
+ return driveJobConfigure(context, run, job);
87
+ if (job.state === 'sending')
88
+ return driveJobSend(context, run, job);
89
+ if (job.state === 'running')
90
+ return driveJobOutcome(context, run, job);
91
+ if (job.state === 'returning')
92
+ return driveJobBaton(context, run, job);
93
+ return false;
94
+ }
95
+ export async function driveJobOpen(context, run, job) {
96
+ const actionId = `${job.id}:open:${job.attemptCount}`;
97
+ const effect = requireEffect(context, run.id, actionId);
98
+ const result = await runDurableEffect(context, {
99
+ run,
100
+ effect,
101
+ job,
102
+ execute: (_token, dispatch) => context.deps.openChild({ ...effectCall(run, effect, dispatch, job), job }),
103
+ validate: assertSessionReceipt
104
+ });
105
+ const current = result.effect.state === 'committed' ? result.effect : requireEffect(context, run.id, actionId);
106
+ if (current.state !== 'committed')
107
+ return result.changed;
108
+ const receipt = assertSessionReceipt(current.receipt);
109
+ const freshJob = context.db.getWorkflowJob(job.id);
110
+ if (freshJob?.state !== 'opening')
111
+ return true;
112
+ context.db.idempotentMutation('workflow_job_opened', `${job.id}:${job.attemptCount}:${receipt.sessionId}`, receipt, () => {
113
+ context.db.updateWorkflowJobAttempt({
114
+ jobId: job.id,
115
+ attemptNumber: job.attemptCount,
116
+ expectedState: 'opening',
117
+ state: 'configuring',
118
+ childSessionId: receipt.sessionId,
119
+ eventKey: `attempt-configuring:${job.id}:${job.attemptCount}`,
120
+ eventType: 'workflow_job_attempt_configuring'
121
+ });
122
+ context.db.updateWorkflowJob({
123
+ jobId: job.id,
124
+ expectedStates: ['opening'],
125
+ expectedCancellationGeneration: run.cancellationGeneration,
126
+ state: 'configuring',
127
+ childSessionId: receipt.sessionId,
128
+ eventKey: `job-configuring:${job.id}:${job.attemptCount}`,
129
+ eventType: 'workflow_job_configuring'
130
+ });
131
+ return { runId: run.id, jobId: job.id };
132
+ }, { runId: run.id, actionId });
133
+ return true;
134
+ }
135
+ export async function driveJobConfigure(context, run, job) {
136
+ if (!job.childSessionId)
137
+ throw new WorkflowTransitionError(`job ${job.id} is configuring without a child`);
138
+ const actionId = `${job.id}:configure:${job.attemptCount}`;
139
+ let effect = context.db.getWorkflowEffect(run.id, actionId);
140
+ if (!effect) {
141
+ effect = context.db.prepareWorkflowEffect({
142
+ id: `${run.id}:${actionId}`,
143
+ runId: run.id,
144
+ actionId,
145
+ kind: 'configure_child',
146
+ jobId: job.id,
147
+ target: { sessionId: job.childSessionId },
148
+ inputs: {
149
+ role: job.resolvedRole,
150
+ correlationMarker: workflowEffectCorrelationMarker(run.id, actionId)
151
+ },
152
+ expectedCancellationGeneration: run.cancellationGeneration,
153
+ eventKey: `prepare:${actionId}`
154
+ }).effect;
155
+ }
156
+ const result = await runDurableEffect(context, {
157
+ run,
158
+ effect,
159
+ job,
160
+ execute: (_token, dispatch) => context.deps.configureSession({
161
+ ...effectCall(run, effect, dispatch, job),
162
+ sessionId: job.childSessionId,
163
+ role: job.resolvedRole
164
+ }),
165
+ validate: value => cleanUnknown(value ?? { matched: true })
166
+ });
167
+ const current = result.effect.state === 'committed' ? result.effect : requireEffect(context, run.id, actionId);
168
+ if (current.state !== 'committed')
169
+ return result.changed;
170
+ const freshJob = context.db.getWorkflowJob(job.id);
171
+ if (freshJob?.state !== 'configuring')
172
+ return true;
173
+ const taskAction = `${job.id}:task:${job.attemptCount}`;
174
+ const existingTask = context.db.getWorkflowEffect(run.id, taskAction);
175
+ if (existingTask) {
176
+ if (existingTask.kind !== 'send_task' || existingTask.jobId !== job.id) {
177
+ throw new WorkflowTransitionError(`task effect ${taskAction} does not match job ${job.id}`);
178
+ }
179
+ const prompt = existingTask.inputs &&
180
+ typeof existingTask.inputs === 'object' &&
181
+ typeof existingTask.inputs.prompt === 'string'
182
+ ? existingTask.inputs.prompt
183
+ : undefined;
184
+ if (!prompt)
185
+ throw new WorkflowTransitionError(`task effect ${taskAction} has no frozen prompt`);
186
+ finishJobConfiguration(context, run, job, taskAction, existingTask.cursor, prompt, false);
187
+ return true;
188
+ }
189
+ const cursor = await context.deps.captureDeliveryCursor(job.childSessionId);
190
+ const handoff = scrubWorkflowSecrets((await context.deps.materializeHandoff?.({ run, job })) ?? '');
191
+ const basePrompt = handoff ? `${job.prompt}\n\nSanitized root handoff: ${handoff}` : job.prompt;
192
+ const prompt = `${basePrompt}\n\n${privateCorrelationBlock(run.id, taskAction)}`;
193
+ finishJobConfiguration(context, run, job, taskAction, cursor, prompt, true);
194
+ return true;
195
+ }
196
+ export function finishJobConfiguration(context, run, job, taskAction, cursor, prompt, prepareTask) {
197
+ context.db.idempotentMutation('workflow_job_configured', `${job.id}:${job.attemptCount}`, { cursor, prompt }, () => {
198
+ if (prepareTask) {
199
+ context.db.prepareWorkflowEffect({
200
+ id: `${run.id}:${taskAction}`,
201
+ runId: run.id,
202
+ actionId: taskAction,
203
+ kind: 'send_task',
204
+ jobId: job.id,
205
+ target: { sessionId: job.childSessionId },
206
+ inputs: {
207
+ prompt,
208
+ correlationMarker: workflowEffectCorrelationMarker(run.id, taskAction)
209
+ },
210
+ cursor,
211
+ expectedCancellationGeneration: run.cancellationGeneration,
212
+ eventKey: `prepare:${taskAction}`
213
+ });
214
+ }
215
+ context.db.updateWorkflowJobAttempt({
216
+ jobId: job.id,
217
+ attemptNumber: job.attemptCount,
218
+ expectedState: 'configuring',
219
+ state: 'sending',
220
+ eventKey: `attempt-sending:${job.id}:${job.attemptCount}`,
221
+ eventType: 'workflow_job_attempt_sending'
222
+ });
223
+ context.db.updateWorkflowJob({
224
+ jobId: job.id,
225
+ expectedStates: ['configuring'],
226
+ expectedCancellationGeneration: run.cancellationGeneration,
227
+ state: 'sending',
228
+ eventKey: `job-sending:${job.id}:${job.attemptCount}`,
229
+ eventType: 'workflow_job_sending'
230
+ });
231
+ return { runId: run.id, jobId: job.id };
232
+ }, { runId: run.id, actionId: taskAction });
233
+ }
234
+ export async function driveJobSend(context, run, job) {
235
+ if (!job.childSessionId)
236
+ throw new WorkflowTransitionError(`job ${job.id} is sending without a child`);
237
+ const actionId = `${job.id}:task:${job.attemptCount}`;
238
+ const effect = requireEffect(context, run.id, actionId);
239
+ let current = effect;
240
+ let changed = false;
241
+ if (effect.state !== 'committed') {
242
+ const prompt = effect.inputs &&
243
+ typeof effect.inputs === 'object' &&
244
+ typeof effect.inputs.prompt === 'string'
245
+ ? effect.inputs.prompt
246
+ : job.prompt;
247
+ const result = await runDurableEffect(context, {
248
+ run,
249
+ effect,
250
+ job,
251
+ execute: (_token, dispatch) => context.deps.sendPrompt({
252
+ ...effectCall(run, effect, dispatch, job),
253
+ sessionId: job.childSessionId,
254
+ text: prompt
255
+ }),
256
+ validate: assertDeliveryReceipt
257
+ });
258
+ current = result.effect;
259
+ changed = result.changed;
260
+ }
261
+ if (current.state !== 'committed' || !isDeliveryReceipt(current.receipt))
262
+ return changed;
263
+ const latestJob = context.db.getWorkflowJob(job.id);
264
+ if (latestJob?.state !== 'sending')
265
+ return true;
266
+ const resolution = await context.deps.resolveDeliveryReceipt({
267
+ ...effectReadCall(run, current, job),
268
+ sessionId: job.childSessionId,
269
+ receipt: current.receipt
270
+ });
271
+ if (resolution.status === 'lost') {
272
+ markReceiptLost(context, run, current, resolution.evidence);
273
+ return true;
274
+ }
275
+ const resolved = messageReceipt(resolution, current.receipt.id);
276
+ if (!resolved) {
277
+ if (!isDeliveryReceipt(latestJob.taskReceipt)) {
278
+ context.db.updateWorkflowJob({
279
+ jobId: job.id,
280
+ expectedStates: ['sending'],
281
+ expectedCancellationGeneration: run.cancellationGeneration,
282
+ state: 'sending',
283
+ taskReceipt: current.receipt,
284
+ eventKey: `task-accepted:${job.id}:${current.receipt.id}`,
285
+ eventType: 'workflow_task_accepted'
286
+ });
287
+ return true;
288
+ }
289
+ return changed;
290
+ }
291
+ context.db.idempotentMutation('workflow_task_delivered', `${job.id}:${resolved.id}`, resolved, () => {
292
+ context.db.updateWorkflowJobAttempt({
293
+ jobId: job.id,
294
+ attemptNumber: job.attemptCount,
295
+ expectedState: 'sending',
296
+ state: 'running',
297
+ eventKey: `attempt-running:${job.id}:${job.attemptCount}`,
298
+ eventType: 'workflow_job_attempt_running'
299
+ });
300
+ context.db.updateWorkflowJob({
301
+ jobId: job.id,
302
+ expectedStates: ['sending'],
303
+ expectedCancellationGeneration: run.cancellationGeneration,
304
+ state: 'running',
305
+ taskReceipt: resolved,
306
+ eventKey: `task-delivered:${job.id}:${resolved.id}`,
307
+ eventType: 'workflow_task_delivered'
308
+ });
309
+ return { runId: run.id, jobId: job.id };
310
+ }, { runId: run.id, actionId });
311
+ return true;
312
+ }
@@ -0,0 +1,280 @@
1
+ import { workflowPrivateEnvelope } from "../../shared.js";
2
+ import { WorkflowTransitionError } from "../persistence/db.js";
3
+ import { markReceiptLost } from "./effect-recovery.js";
4
+ import { runDurableEffect } from "./effects.js";
5
+ import { assertDeliveryReceipt, effectGrant, guardJobs, isDeliveryReceipt, messageReceipt, privateCorrelationBlock, sanitizeChildOutcome, workflowEffectCorrelationMarker } from "./helpers.js";
6
+ import { phaseAfterDeliveredBaton } from "./machine.js";
7
+ import { blockRun, effectCall, effectReadCall, requireEffect, requireRun } from "./state.js";
8
+ export async function driveJobOutcome(context, run, job) {
9
+ const outcome = await context.deps.readChildOutcome({ run, job });
10
+ if (!outcome)
11
+ return false;
12
+ const sanitized = sanitizeChildOutcome(outcome);
13
+ const freshRun = requireRun(context, run.id);
14
+ if (freshRun.phase === 'cancelled') {
15
+ context.db.recordLateWorkflowChildResult({
16
+ runId: freshRun.id,
17
+ jobId: job.id,
18
+ attemptNumber: job.attemptCount,
19
+ outcome: sanitized,
20
+ eventKey: `late-child:${job.id}:${job.attemptCount}`
21
+ });
22
+ return true;
23
+ }
24
+ if (sanitized.kind === 'failure') {
25
+ context.db.idempotentMutation('workflow_job_failed', `${job.id}:${job.attemptCount}`, sanitized, () => {
26
+ context.db.updateWorkflowJobAttempt({
27
+ jobId: job.id,
28
+ attemptNumber: job.attemptCount,
29
+ expectedState: 'running',
30
+ state: 'failed',
31
+ outcome: sanitized,
32
+ failureEvidence: sanitized.evidence,
33
+ eventKey: `attempt-failed:${job.id}:${job.attemptCount}`,
34
+ eventType: 'workflow_job_attempt_failed'
35
+ });
36
+ context.db.updateWorkflowJob({
37
+ jobId: job.id,
38
+ expectedStates: ['running'],
39
+ expectedCancellationGeneration: run.cancellationGeneration,
40
+ state: 'failed',
41
+ outcome: sanitized,
42
+ clearOwner: true,
43
+ eventKey: `job-failed:${job.id}:${job.attemptCount}`,
44
+ eventType: 'workflow_job_failed'
45
+ });
46
+ blockRun(context, requireRun(context, run.id), {
47
+ actionId: `job:${job.id}`,
48
+ errorCode: sanitized.code,
49
+ message: sanitized.message,
50
+ retryClass: sanitized.retryClass ?? 'deterministic'
51
+ });
52
+ return { runId: run.id, jobId: job.id };
53
+ }, { runId: run.id, actionId: `job:${job.id}` });
54
+ return true;
55
+ }
56
+ context.db.idempotentMutation('workflow_job_outcome', `${job.id}:${job.attemptCount}`, sanitized, () => {
57
+ context.db.updateWorkflowJobAttempt({
58
+ jobId: job.id,
59
+ attemptNumber: job.attemptCount,
60
+ expectedState: 'running',
61
+ state: 'returning',
62
+ outcome: sanitized,
63
+ eventKey: `attempt-returning:${job.id}:${job.attemptCount}`,
64
+ eventType: 'workflow_job_attempt_returning'
65
+ });
66
+ context.db.updateWorkflowJob({
67
+ jobId: job.id,
68
+ expectedStates: ['running'],
69
+ expectedCancellationGeneration: run.cancellationGeneration,
70
+ state: 'returning',
71
+ outcome: sanitized,
72
+ eventKey: `job-returning:${job.id}:${job.attemptCount}`,
73
+ eventType: 'workflow_job_returning'
74
+ });
75
+ return { runId: run.id, jobId: job.id };
76
+ }, { runId: run.id, actionId: `job:${job.id}:outcome` });
77
+ return true;
78
+ }
79
+ export function potentialBatonGrant(context, run, job) {
80
+ const peers = context.db
81
+ .listWorkflowJobs(run.id)
82
+ .filter(peer => peer.cycle === job.cycle && peer.role === job.role && peer.id !== job.id);
83
+ const peersDelivered = peers.every(peer => peer.state === 'returned' && isDeliveryReceipt(peer.batonReceipt) && peer.batonReceipt.kind === 'message');
84
+ if (!peersDelivered)
85
+ return undefined;
86
+ return {
87
+ phase: job.role === 'exploration' ? 'planning' : 'reviewing',
88
+ cycle: run.cycle,
89
+ revision: run.revision + 1,
90
+ allowedRoles: ['exploration', 'implementation']
91
+ };
92
+ }
93
+ export async function driveJobBaton(context, run, job) {
94
+ if (!run.rootSessionId)
95
+ throw new WorkflowTransitionError(`Workflow ${run.id} has no root for Baton return`);
96
+ const outcome = job.outcome;
97
+ if (outcome?.kind !== 'success')
98
+ throw new WorkflowTransitionError(`job ${job.id} has no successful outcome`);
99
+ const actionId = `${job.id}:baton:${job.attemptCount}`;
100
+ let effect = context.db.getWorkflowEffect(run.id, actionId);
101
+ if (!effect) {
102
+ const roleJobs = context.db
103
+ .listWorkflowJobs(run.id)
104
+ .filter(peer => peer.cycle === job.cycle && peer.role === job.role);
105
+ const designatedFinal = roleJobs.at(-1)?.id === job.id;
106
+ const grant = designatedFinal ? potentialBatonGrant(context, run, job) : undefined;
107
+ // Exactly one current-cycle Baton carries the next phase capability. Hold
108
+ // that deterministic final job until every earlier sibling Baton is a
109
+ // durable root message; accepted outbox rows do not satisfy the barrier.
110
+ if (designatedFinal && !grant)
111
+ return false;
112
+ effect = context.db.prepareWorkflowEffect({
113
+ id: `${run.id}:${actionId}`,
114
+ runId: run.id,
115
+ actionId,
116
+ kind: 'return_baton',
117
+ jobId: job.id,
118
+ target: { sessionId: run.rootSessionId },
119
+ inputs: {
120
+ ...(grant ? { grant } : {}),
121
+ correlationMarker: workflowEffectCorrelationMarker(run.id, actionId)
122
+ },
123
+ cursor: await context.deps.captureDeliveryCursor(run.rootSessionId),
124
+ expectedCancellationGeneration: run.cancellationGeneration,
125
+ eventKey: `prepare:${actionId}`
126
+ }).effect;
127
+ }
128
+ const grant = effectGrant(effect) ?? undefined;
129
+ let current = effect;
130
+ let changed = false;
131
+ if (effect.state !== 'committed') {
132
+ const result = await runDurableEffect(context, {
133
+ run,
134
+ effect,
135
+ job,
136
+ ...(grant ? { capabilityGrant: grant } : {}),
137
+ execute: (token, dispatch) => {
138
+ const envelope = grant && token
139
+ ? workflowPrivateEnvelope({
140
+ workflowId: run.id,
141
+ phaseCapability: token,
142
+ cycle: grant.cycle,
143
+ revision: grant.revision,
144
+ allowedRoles: grant.allowedRoles
145
+ })
146
+ : '';
147
+ return context.deps.returnBaton({
148
+ ...effectCall(run, effect, dispatch, job),
149
+ job,
150
+ sessionId: run.rootSessionId,
151
+ text: [outcome.baton, envelope, privateCorrelationBlock(run.id, effect.actionId)].filter(Boolean).join('\n\n')
152
+ });
153
+ },
154
+ validate: assertDeliveryReceipt
155
+ });
156
+ current = result.effect;
157
+ changed = result.changed;
158
+ }
159
+ if (current.state !== 'committed' || !isDeliveryReceipt(current.receipt))
160
+ return changed;
161
+ const latestJob = context.db.getWorkflowJob(job.id);
162
+ if (latestJob?.state !== 'returning')
163
+ return true;
164
+ const resolution = await context.deps.resolveDeliveryReceipt({
165
+ ...effectReadCall(run, current, job),
166
+ sessionId: run.rootSessionId,
167
+ receipt: current.receipt
168
+ });
169
+ if (resolution.status === 'lost') {
170
+ markReceiptLost(context, run, current, resolution.evidence);
171
+ return true;
172
+ }
173
+ const resolved = messageReceipt(resolution, current.receipt.id);
174
+ if (!resolved) {
175
+ if (!isDeliveryReceipt(latestJob.batonReceipt)) {
176
+ context.db.updateWorkflowJob({
177
+ jobId: job.id,
178
+ expectedStates: ['returning'],
179
+ expectedCancellationGeneration: run.cancellationGeneration,
180
+ state: 'returning',
181
+ batonReceipt: current.receipt,
182
+ eventKey: `baton-accepted:${job.id}:${current.receipt.id}`,
183
+ eventType: 'workflow_baton_accepted'
184
+ });
185
+ return true;
186
+ }
187
+ return changed;
188
+ }
189
+ context.db.idempotentMutation('workflow_baton_delivered', `${job.id}:${resolved.id}`, resolved, () => {
190
+ context.db.updateWorkflowJobAttempt({
191
+ jobId: job.id,
192
+ attemptNumber: job.attemptCount,
193
+ expectedState: 'returning',
194
+ state: 'returned',
195
+ outcome: outcome,
196
+ eventKey: `attempt-returned:${job.id}:${job.attemptCount}`,
197
+ eventType: 'workflow_job_attempt_returned'
198
+ });
199
+ context.db.updateWorkflowJob({
200
+ jobId: job.id,
201
+ expectedStates: ['returning'],
202
+ expectedCancellationGeneration: run.cancellationGeneration,
203
+ state: 'returned',
204
+ batonReceipt: resolved,
205
+ clearOwner: true,
206
+ eventKey: `job-returned:${job.id}:${resolved.id}`,
207
+ eventType: 'workflow_job_returned'
208
+ });
209
+ return { runId: run.id, jobId: job.id };
210
+ }, { runId: run.id, actionId });
211
+ advanceDeliveredBarrier(context, requireRun(context, run.id));
212
+ return true;
213
+ }
214
+ export function advanceDeliveredBarrier(context, run) {
215
+ if (run.phase !== 'exploring' && run.phase !== 'implementing')
216
+ return false;
217
+ const role = run.phase === 'exploring' ? 'exploration' : 'implementation';
218
+ const jobs = context.db.listWorkflowJobs(run.id);
219
+ const next = phaseAfterDeliveredBaton(role, guardJobs(jobs), run.cycle);
220
+ if (!next)
221
+ return false;
222
+ const grantJob = [...jobs].reverse().find(job => {
223
+ if (job.role !== role || job.cycle !== run.cycle || job.state !== 'returned')
224
+ return false;
225
+ const effect = context.db.getWorkflowEffect(run.id, `${job.id}:baton:${job.attemptCount}`);
226
+ return effect ? effectGrant(effect)?.phase === next : false;
227
+ });
228
+ if (!grantJob) {
229
+ blockRun(context, run, {
230
+ actionId: `barrier:${role}:${run.cycle}`,
231
+ errorCode: 'workflow_barrier_capability_missing',
232
+ message: 'The final delivered Baton had no matching phase authorization.',
233
+ retryClass: 'terminal'
234
+ });
235
+ return true;
236
+ }
237
+ const effect = requireEffect(context, run.id, `${grantJob.id}:baton:${grantJob.attemptCount}`);
238
+ const receipt = isDeliveryReceipt(grantJob.batonReceipt) && grantJob.batonReceipt.kind === 'message' ? grantJob.batonReceipt : null;
239
+ const grant = effectGrant(effect);
240
+ if (!receipt || !grant || !effect.launchNonce)
241
+ return false;
242
+ context.db.idempotentMutation('advance_workflow_baton_barrier', `${run.id}:${role}:${run.cycle}:${receipt.id}`, { role, receipt, grant, tokenHash: effect.launchNonce }, () => {
243
+ const current = requireRun(context, run.id);
244
+ if (current.phase !== run.phase || current.cycle !== run.cycle || current.revision !== run.revision) {
245
+ return { runId: run.id };
246
+ }
247
+ const implementationDelivered = context.db
248
+ .listWorkflowJobs(run.id)
249
+ .filter(job => job.role === 'implementation' &&
250
+ job.state === 'returned' &&
251
+ isDeliveryReceipt(job.batonReceipt) &&
252
+ job.batonReceipt.kind === 'message').length;
253
+ const advanced = context.db.transitionWorkflowRun({
254
+ runId: run.id,
255
+ expectedPhase: run.phase,
256
+ expectedCancellationGeneration: current.cancellationGeneration,
257
+ phase: next,
258
+ cycle: grant.cycle,
259
+ revision: grant.revision,
260
+ implementationBatonsDelivered: implementationDelivered,
261
+ eventKey: `barrier-advanced:${role}:${receipt.id}`,
262
+ eventType: 'workflow_baton_barrier_satisfied',
263
+ eventData: { role, receipt }
264
+ });
265
+ context.db.revokeWorkflowCapabilities(run.id, `revoke-after-barrier:${receipt.id}`, run.phase);
266
+ context.db.issueWorkflowCapability({
267
+ tokenHash: effect.launchNonce,
268
+ runId: run.id,
269
+ rootSessionId: advanced.rootSessionId,
270
+ cycle: advanced.cycle,
271
+ phase: next,
272
+ revision: advanced.revision,
273
+ allowedRoles: grant.allowedRoles,
274
+ issuedWith: { rowid: receipt.rowid, ...(receipt.turnId ? { turnId: receipt.turnId } : {}) },
275
+ eventKey: `capability-after-baton:${receipt.id}`
276
+ });
277
+ return { runId: run.id };
278
+ }, { runId: run.id, actionId: effect.actionId });
279
+ return true;
280
+ }
@@ -1,5 +1,5 @@
1
- import { resolveRole } from "./roles.js";
2
- import { scrubWorkflowSecrets, WORKFLOW_OBJECTIVE_HEADING, workflowPrivateEnvelope } from "./shared.js";
1
+ import { resolveRole } from "../../agents/roles.js";
2
+ import { scrubWorkflowSecrets, WORKFLOW_OBJECTIVE_HEADING, workflowPrivateEnvelope } from "../../shared.js";
3
3
  export const WORKFLOW_ROLE_NAMES = [
4
4
  'planning',
5
5
  'exploration',