@abelxiaoxing/cadence 1.8.4 → 2.0.0

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 (202) hide show
  1. package/README.md +324 -117
  2. package/THIRD_PARTY_NOTICES.md +15 -0
  3. package/agents/design-explorer.md +14 -18
  4. package/agents/diagnosis-worker.md +14 -14
  5. package/agents/implementation-worker.md +16 -17
  6. package/config/workflow-task.example.json +7 -0
  7. package/docs/remote-access.md +60 -0
  8. package/docs/reuse-and-distribution.md +44 -0
  9. package/docs/service-management.md +108 -0
  10. package/licenses/bundled-web.txt +6245 -0
  11. package/licenses/pi-web-ui-MIT.txt +21 -0
  12. package/licenses/pi-web-ui-reuse.md +22 -0
  13. package/package.json +94 -21
  14. package/src/index.ts +21 -1802
  15. package/src/native-build.ts +6 -0
  16. package/src/native-client.ts +103 -0
  17. package/src/native-export.ts +58 -0
  18. package/src/native-history.ts +359 -0
  19. package/src/operator-cli.mjs +453 -1937
  20. package/src/plan-proposal.ts +87 -0
  21. package/src/sdk-worker.mjs +549 -0
  22. package/src/service/contracts/active.ts +82 -0
  23. package/src/service/contracts/budget.ts +148 -0
  24. package/src/service/contracts/build.ts +67 -0
  25. package/src/service/contracts/capture.ts +48 -0
  26. package/src/service/contracts/core.ts +33 -0
  27. package/src/service/contracts/current.ts +261 -0
  28. package/src/service/contracts/domain.ts +68 -0
  29. package/src/service/contracts/history.ts +2245 -0
  30. package/src/service/contracts/models.ts +139 -0
  31. package/src/service/coordination.ts +862 -0
  32. package/src/service/private-files.ts +204 -0
  33. package/src/service-cli.mjs +5592 -0
  34. package/src/service-client.ts +747 -0
  35. package/src/service-contract.ts +245 -0
  36. package/src/service-host.mjs +22382 -0
  37. package/src/service-http-client.ts +425 -0
  38. package/src/service-wire.ts +372 -0
  39. package/src/task-shell.ts +1045 -0
  40. package/src/task-store.ts +479 -0
  41. package/src/task-types.ts +184 -0
  42. package/src/verification-diagnostics.ts +29 -72
  43. package/src/worker-capture.mjs +1197 -0
  44. package/src/worker-public.mjs +1753 -0
  45. package/src/worker-redaction.mjs +608 -0
  46. package/src/worker-tools.mjs +202 -0
  47. package/web/dist/assets/KaTeX_AMS-Regular.ttf +0 -0
  48. package/web/dist/assets/KaTeX_AMS-Regular.woff +0 -0
  49. package/web/dist/assets/KaTeX_AMS-Regular.woff2 +0 -0
  50. package/web/dist/assets/KaTeX_Caligraphic-Bold.ttf +0 -0
  51. package/web/dist/assets/KaTeX_Caligraphic-Bold.woff +0 -0
  52. package/web/dist/assets/KaTeX_Caligraphic-Bold.woff2 +0 -0
  53. package/web/dist/assets/KaTeX_Caligraphic-Regular.ttf +0 -0
  54. package/web/dist/assets/KaTeX_Caligraphic-Regular.woff +0 -0
  55. package/web/dist/assets/KaTeX_Caligraphic-Regular.woff2 +0 -0
  56. package/web/dist/assets/KaTeX_Fraktur-Bold.ttf +0 -0
  57. package/web/dist/assets/KaTeX_Fraktur-Bold.woff +0 -0
  58. package/web/dist/assets/KaTeX_Fraktur-Bold.woff2 +0 -0
  59. package/web/dist/assets/KaTeX_Fraktur-Regular.ttf +0 -0
  60. package/web/dist/assets/KaTeX_Fraktur-Regular.woff +0 -0
  61. package/web/dist/assets/KaTeX_Fraktur-Regular.woff2 +0 -0
  62. package/web/dist/assets/KaTeX_Main-Bold.ttf +0 -0
  63. package/web/dist/assets/KaTeX_Main-Bold.woff +0 -0
  64. package/web/dist/assets/KaTeX_Main-Bold.woff2 +0 -0
  65. package/web/dist/assets/KaTeX_Main-BoldItalic.ttf +0 -0
  66. package/web/dist/assets/KaTeX_Main-BoldItalic.woff +0 -0
  67. package/web/dist/assets/KaTeX_Main-BoldItalic.woff2 +0 -0
  68. package/web/dist/assets/KaTeX_Main-Italic.ttf +0 -0
  69. package/web/dist/assets/KaTeX_Main-Italic.woff +0 -0
  70. package/web/dist/assets/KaTeX_Main-Italic.woff2 +0 -0
  71. package/web/dist/assets/KaTeX_Main-Regular.ttf +0 -0
  72. package/web/dist/assets/KaTeX_Main-Regular.woff +0 -0
  73. package/web/dist/assets/KaTeX_Main-Regular.woff2 +0 -0
  74. package/web/dist/assets/KaTeX_Math-BoldItalic.ttf +0 -0
  75. package/web/dist/assets/KaTeX_Math-BoldItalic.woff +0 -0
  76. package/web/dist/assets/KaTeX_Math-BoldItalic.woff2 +0 -0
  77. package/web/dist/assets/KaTeX_Math-Italic.ttf +0 -0
  78. package/web/dist/assets/KaTeX_Math-Italic.woff +0 -0
  79. package/web/dist/assets/KaTeX_Math-Italic.woff2 +0 -0
  80. package/web/dist/assets/KaTeX_SansSerif-Bold.ttf +0 -0
  81. package/web/dist/assets/KaTeX_SansSerif-Bold.woff +0 -0
  82. package/web/dist/assets/KaTeX_SansSerif-Bold.woff2 +0 -0
  83. package/web/dist/assets/KaTeX_SansSerif-Italic.ttf +0 -0
  84. package/web/dist/assets/KaTeX_SansSerif-Italic.woff +0 -0
  85. package/web/dist/assets/KaTeX_SansSerif-Italic.woff2 +0 -0
  86. package/web/dist/assets/KaTeX_SansSerif-Regular.ttf +0 -0
  87. package/web/dist/assets/KaTeX_SansSerif-Regular.woff +0 -0
  88. package/web/dist/assets/KaTeX_SansSerif-Regular.woff2 +0 -0
  89. package/web/dist/assets/KaTeX_Script-Regular.ttf +0 -0
  90. package/web/dist/assets/KaTeX_Script-Regular.woff +0 -0
  91. package/web/dist/assets/KaTeX_Script-Regular.woff2 +0 -0
  92. package/web/dist/assets/KaTeX_Size1-Regular.ttf +0 -0
  93. package/web/dist/assets/KaTeX_Size1-Regular.woff +0 -0
  94. package/web/dist/assets/KaTeX_Size1-Regular.woff2 +0 -0
  95. package/web/dist/assets/KaTeX_Size2-Regular.ttf +0 -0
  96. package/web/dist/assets/KaTeX_Size2-Regular.woff +0 -0
  97. package/web/dist/assets/KaTeX_Size2-Regular.woff2 +0 -0
  98. package/web/dist/assets/KaTeX_Size3-Regular.ttf +0 -0
  99. package/web/dist/assets/KaTeX_Size3-Regular.woff +0 -0
  100. package/web/dist/assets/KaTeX_Size3-Regular.woff2 +0 -0
  101. package/web/dist/assets/KaTeX_Size4-Regular.ttf +0 -0
  102. package/web/dist/assets/KaTeX_Size4-Regular.woff +0 -0
  103. package/web/dist/assets/KaTeX_Size4-Regular.woff2 +0 -0
  104. package/web/dist/assets/KaTeX_Typewriter-Regular.ttf +0 -0
  105. package/web/dist/assets/KaTeX_Typewriter-Regular.woff +0 -0
  106. package/web/dist/assets/KaTeX_Typewriter-Regular.woff2 +0 -0
  107. package/web/dist/assets/cadence.css +2 -0
  108. package/web/dist/assets/cadence.js +7 -0
  109. package/web/dist/assets/markdown.js +296 -0
  110. package/web/dist/assets/react.js +9 -0
  111. package/web/dist/assets/rolldown-runtime.js +1 -0
  112. package/web/dist/assets/timeline-client.js +41 -0
  113. package/web/dist/assets/vis-timeline.css +1274 -0
  114. package/web/dist/assets/vis-timeline.js +32 -0
  115. package/web/dist/index.html +18 -0
  116. package/web/dist/manifest.json +505 -0
  117. package/config/plan-draft.example.json +0 -125
  118. package/config/plan-draft.multiple-tasks.example.json +0 -375
  119. package/config/plan-draft.quick.example.json +0 -78
  120. package/prompts/abel-design.md +0 -382
  121. package/prompts/abel-diagnose.md +0 -71
  122. package/prompts/abel-implement.md +0 -284
  123. package/prompts/abel-init.md +0 -52
  124. package/src/activation.ts +0 -44
  125. package/src/activity-contracts.ts +0 -82
  126. package/src/agent-registry.ts +0 -53
  127. package/src/apply-transaction.ts +0 -1437
  128. package/src/artifact-store.ts +0 -213
  129. package/src/build-windows-job.mjs +0 -95
  130. package/src/candidate-artifact.ts +0 -143
  131. package/src/candidate-context.ts +0 -289
  132. package/src/candidate-patch.ts +0 -271
  133. package/src/candidate-workspace.ts +0 -80
  134. package/src/canonical.ts +0 -16
  135. package/src/change-contract-schema.ts +0 -191
  136. package/src/change-contract.ts +0 -324
  137. package/src/change-verification.ts +0 -1617
  138. package/src/contracts.ts +0 -2466
  139. package/src/control-contracts.ts +0 -382
  140. package/src/delivery-compiler.ts +0 -1805
  141. package/src/delivery-revision.ts +0 -42
  142. package/src/design-control.ts +0 -1299
  143. package/src/design-diagnostics.ts +0 -181
  144. package/src/design-journal.ts +0 -1773
  145. package/src/directory-sync.ts +0 -36
  146. package/src/durable-contracts.ts +0 -227
  147. package/src/durable-workflow.ts +0 -822
  148. package/src/evidence-draft.ts +0 -122
  149. package/src/execution-profile.ts +0 -64
  150. package/src/execution-retention.ts +0 -77
  151. package/src/file-snapshot.ts +0 -163
  152. package/src/implement-continuation.ts +0 -559
  153. package/src/implement-graph.ts +0 -690
  154. package/src/implement-plan.ts +0 -72
  155. package/src/install-config.d.mts +0 -6
  156. package/src/isolation-backend.ts +0 -353
  157. package/src/model-source.ts +0 -22
  158. package/src/openspec-cli.ts +0 -494
  159. package/src/operator-cli-entry.mjs +0 -24
  160. package/src/operator-tools.ts +0 -309
  161. package/src/package-candidate-process.ts +0 -206
  162. package/src/package-candidate.ts +0 -24
  163. package/src/package-delivery.ts +0 -486
  164. package/src/package-state.ts +0 -320
  165. package/src/package-verification.ts +0 -635
  166. package/src/package-workflow.ts +0 -305
  167. package/src/packet-runtime.ts +0 -459
  168. package/src/phase-execution.ts +0 -2163
  169. package/src/pi-adapter.ts +0 -19
  170. package/src/plan-draft-summary.ts +0 -527
  171. package/src/plan-draft.ts +0 -502
  172. package/src/remove-path.ts +0 -32
  173. package/src/run-state.ts +0 -420
  174. package/src/run-store.ts +0 -1179
  175. package/src/safe-path.ts +0 -77
  176. package/src/scoped-grep-worker.mjs +0 -23
  177. package/src/scoped-tools.ts +0 -775
  178. package/src/single-task-draft.ts +0 -142
  179. package/src/sqlite-schema.ts +0 -171
  180. package/src/state-root.ts +0 -215
  181. package/src/storage-schema.ts +0 -360
  182. package/src/subagent-activity.ts +0 -1417
  183. package/src/subagent-process.ts +0 -476
  184. package/src/task-ledger.ts +0 -1338
  185. package/src/transport-budget.ts +0 -50
  186. package/src/verification-capability.ts +0 -1191
  187. package/src/verification-environment-io.ts +0 -389
  188. package/src/verification-environment.ts +0 -162
  189. package/src/verification-identity.ts +0 -77
  190. package/src/verification-scan.ts +0 -64
  191. package/src/windows-job-backend.ts +0 -419
  192. package/src/windows-job.c +0 -149
  193. package/src/workflow-engine.ts +0 -28
  194. package/src/workflow-policy.ts +0 -1670
  195. package/src/workflow-recovery-policy.ts +0 -97
  196. package/src/workflow-scheduling.ts +0 -105
  197. package/src/workflow-state-machine.ts +0 -4341
  198. package/src/workflow-status.ts +0 -576
  199. package/src/workspace-io-entry.mjs +0 -76
  200. package/src/workspace-io-worker.mjs +0 -1312
  201. package/src/workspace-io.ts +0 -89
  202. package/src/workspace-store.ts +0 -634
@@ -0,0 +1,747 @@
1
+ import {
2
+ type Action,
3
+ type ActiveRuns,
4
+ type CancelCommand,
5
+ type CheckObligations,
6
+ type ChooseModelRequest,
7
+ type Command,
8
+ type ContextVersion,
9
+ ContractError,
10
+ type CurrentChanges,
11
+ type CurrentRequest,
12
+ type CurrentView,
13
+ canonicalJson,
14
+ compareSequence,
15
+ controlTask,
16
+ controlWorkspace,
17
+ type DetailReadResult,
18
+ decodeActiveRuns,
19
+ decodeCancel,
20
+ decodeCheckObligations,
21
+ decodeChooseModelRequest,
22
+ decodeCommand,
23
+ decodeCurrentChanges,
24
+ decodeCurrentChangesRequest,
25
+ decodeCurrentRequest,
26
+ decodeCurrentView,
27
+ decodeCursor,
28
+ decodeDetailReadResult,
29
+ decodeEntityReadRequest,
30
+ decodeEntityReadResult,
31
+ decodeExportRequest,
32
+ decodeGraphReadRequest,
33
+ decodeGraphReadResult,
34
+ decodeHistoryReadResult,
35
+ decodeId,
36
+ decodeListRequest,
37
+ decodeLiveEvent,
38
+ decodeModelCatalog,
39
+ decodeModelChoiceState,
40
+ decodePreflight,
41
+ decodePreflightRequest,
42
+ decodeReceipt,
43
+ decodeReceiptLookup,
44
+ decodeReply,
45
+ decodeScope,
46
+ decodeSearchPage,
47
+ decodeSessionBootstrap,
48
+ decodeSnapshot,
49
+ decodeStoredCommand,
50
+ decodeWorkspaceBootstrap,
51
+ type EntityReadRequest,
52
+ type EntityReadResult,
53
+ type ExportFrame,
54
+ type ExportRequest,
55
+ ExportTracker,
56
+ type GraphReadRequest,
57
+ type GraphReadResult,
58
+ type HistoryReadResult,
59
+ historyReplyMatches,
60
+ LIMITS,
61
+ type ListRequest,
62
+ type LiveEvent,
63
+ type ModelCatalog,
64
+ type ModelChoiceState,
65
+ type Preflight,
66
+ type PreflightRequest,
67
+ publicError,
68
+ type ReadCursor,
69
+ type ReaderKind,
70
+ type Receipt,
71
+ type ReceiptLookup,
72
+ type Reply,
73
+ type Scope,
74
+ type SearchPage,
75
+ type SessionBootstrap,
76
+ type Snapshot,
77
+ type WorkspaceBootstrap,
78
+ } from "./service-contract.ts";
79
+
80
+ /** Transport supplies authentication separately. No URL, auth value, generic send,
81
+ * SDK instance or execution ownership is exposed by the client API.
82
+ * Text transports use parseWireJson BEFORE handing unknown values to this client.
83
+ */
84
+ export interface ServiceTransport {
85
+ current?(request: CurrentRequest, signal?: AbortSignal): Promise<unknown>;
86
+ currentChanges?(
87
+ request: CurrentRequest,
88
+ signal?: AbortSignal,
89
+ ): Promise<unknown>;
90
+ activeRuns?(signal?: AbortSignal): Promise<unknown>;
91
+ models?(signal?: AbortSignal): Promise<unknown>;
92
+ chooseModel?(request: ChooseModelRequest): Promise<unknown>;
93
+ checkObligations(taskId: string, signal?: AbortSignal): Promise<unknown>;
94
+ entity(request: EntityReadRequest, signal?: AbortSignal): Promise<unknown>;
95
+ session(signal?: AbortSignal): Promise<unknown>;
96
+ workspace(signal?: AbortSignal): Promise<unknown>;
97
+ tasks(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
98
+ runs(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
99
+ messages(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
100
+ tools(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
101
+ contents(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
102
+ events(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
103
+ checks(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
104
+ changes(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
105
+ reviews(request: ListRequest, signal?: AbortSignal): Promise<unknown>;
106
+ graph(request: GraphReadRequest, signal?: AbortSignal): Promise<unknown>;
107
+ export(request: ExportRequest, signal: AbortSignal): AsyncIterable<unknown>;
108
+ preflight(request: PreflightRequest): Promise<unknown>;
109
+ command(command: Command): Promise<unknown>;
110
+ cancel(command: CancelCommand): Promise<unknown>;
111
+ receipt(commandId: string, signal?: AbortSignal): Promise<unknown>;
112
+ snapshot(scope: Scope, signal?: AbortSignal): Promise<unknown>;
113
+ detail(cursor: ReadCursor, signal?: AbortSignal): Promise<unknown>;
114
+ search(cursor: ReadCursor, signal?: AbortSignal): Promise<unknown>;
115
+ }
116
+ type NamedCommand<K extends Action["kind"]> = Omit<Command, "action"> & {
117
+ action: Extract<Action, { kind: K }>;
118
+ };
119
+ const unavailable = <T>(mutation: boolean): Reply<T> => ({
120
+ kind: "error",
121
+ error: publicError("unavailable", mutation ? "uncertain" : "none"),
122
+ });
123
+
124
+ export class ServiceClient {
125
+ constructor(private readonly transport: ServiceTransport) {}
126
+ async current(
127
+ input: CurrentRequest,
128
+ signal?: AbortSignal,
129
+ ): Promise<Reply<CurrentView>> {
130
+ const request = decodeCurrentRequest(input);
131
+ try {
132
+ if (!this.transport.current) return unavailable(false);
133
+ const reply = decodeReply(
134
+ await this.transport.current(request, signal),
135
+ decodeCurrentView,
136
+ );
137
+ return reply.kind === "ok" &&
138
+ canonicalJson(reply.value.request) !== canonicalJson(request)
139
+ ? unavailable(false)
140
+ : reply;
141
+ } catch {
142
+ return unavailable(false);
143
+ }
144
+ }
145
+ async currentChanges(
146
+ input: CurrentRequest,
147
+ signal?: AbortSignal,
148
+ ): Promise<Reply<CurrentChanges>> {
149
+ const request = decodeCurrentChangesRequest(input);
150
+ try {
151
+ if (!this.transport.currentChanges) return unavailable(false);
152
+ const reply = decodeReply(
153
+ await this.transport.currentChanges(request, signal),
154
+ decodeCurrentChanges,
155
+ );
156
+ return reply.kind === "ok" &&
157
+ canonicalJson(reply.value.request) !== canonicalJson(request)
158
+ ? unavailable(false)
159
+ : reply;
160
+ } catch {
161
+ return unavailable(false);
162
+ }
163
+ }
164
+ async activeRuns(signal?: AbortSignal): Promise<Reply<ActiveRuns>> {
165
+ try {
166
+ if (!this.transport.activeRuns) return unavailable(false);
167
+ return decodeReply(
168
+ await this.transport.activeRuns(signal),
169
+ decodeActiveRuns,
170
+ );
171
+ } catch {
172
+ return unavailable(false);
173
+ }
174
+ }
175
+ async models(signal?: AbortSignal): Promise<Reply<ModelCatalog>> {
176
+ try {
177
+ if (!this.transport.models) return unavailable(false);
178
+ return decodeReply(
179
+ await this.transport.models(signal),
180
+ decodeModelCatalog,
181
+ );
182
+ } catch {
183
+ return unavailable(false);
184
+ }
185
+ }
186
+ async chooseModel(
187
+ input: ChooseModelRequest,
188
+ ): Promise<Reply<ModelChoiceState>> {
189
+ const request = decodeChooseModelRequest(input);
190
+ try {
191
+ if (!this.transport.chooseModel) return unavailable(false);
192
+ const reply = decodeReply(
193
+ await this.transport.chooseModel(request),
194
+ decodeModelChoiceState,
195
+ );
196
+ if (reply.kind === "ok") {
197
+ const { sourceSelectionId: _id, ...selector } =
198
+ reply.value.selected ?? {};
199
+ if (
200
+ canonicalJson(selector) !== canonicalJson(request.choice) ||
201
+ reply.value.version.epoch !== request.expected.epoch ||
202
+ reply.value.version.accessGeneration !==
203
+ request.expected.accessGeneration ||
204
+ ![
205
+ request.expected.configRev,
206
+ request.expected.configRev + 1,
207
+ ].includes(reply.value.version.configRev) ||
208
+ (reply.value.version.configRev === request.expected.configRev) !==
209
+ (reply.value.version.catalogId === request.expected.catalogId)
210
+ )
211
+ return unavailable(true);
212
+ }
213
+ return reply;
214
+ } catch {
215
+ return unavailable(true);
216
+ }
217
+ }
218
+ async checkObligations(
219
+ taskId: string,
220
+ signal?: AbortSignal,
221
+ ): Promise<Reply<CheckObligations>> {
222
+ decodeId(taskId);
223
+ try {
224
+ const reply = decodeReply(
225
+ await this.transport.checkObligations(taskId, signal),
226
+ decodeCheckObligations,
227
+ );
228
+ return reply.kind === "ok" && reply.value.taskId !== taskId
229
+ ? unavailable(false)
230
+ : reply;
231
+ } catch {
232
+ return unavailable(false);
233
+ }
234
+ }
235
+ task(
236
+ input: Omit<EntityReadRequest, "kind">,
237
+ signal?: AbortSignal,
238
+ ): Promise<Reply<EntityReadResult>> {
239
+ return this.entity("task", input, signal);
240
+ }
241
+ message(
242
+ input: Omit<EntityReadRequest, "kind">,
243
+ signal?: AbortSignal,
244
+ ): Promise<Reply<EntityReadResult>> {
245
+ return this.entity("message", input, signal);
246
+ }
247
+ tool(
248
+ input: Omit<EntityReadRequest, "kind">,
249
+ signal?: AbortSignal,
250
+ ): Promise<Reply<EntityReadResult>> {
251
+ return this.entity("tool", input, signal);
252
+ }
253
+ private async entity(
254
+ kind: EntityReadRequest["kind"],
255
+ input: Omit<EntityReadRequest, "kind">,
256
+ signal?: AbortSignal,
257
+ ): Promise<Reply<EntityReadResult>> {
258
+ const request = decodeEntityReadRequest({ ...input, kind });
259
+ try {
260
+ const reply = decodeReply(
261
+ await this.transport.entity(request, signal),
262
+ decodeEntityReadResult,
263
+ );
264
+ return reply.kind === "ok" &&
265
+ canonicalJson(reply.value.request) !== canonicalJson(request)
266
+ ? unavailable(false)
267
+ : reply;
268
+ } catch {
269
+ return unavailable(false);
270
+ }
271
+ }
272
+ async session(signal?: AbortSignal): Promise<Reply<SessionBootstrap>> {
273
+ try {
274
+ return decodeReply(
275
+ await this.transport.session(signal),
276
+ decodeSessionBootstrap,
277
+ );
278
+ } catch {
279
+ return unavailable(false);
280
+ }
281
+ }
282
+ async workspace(signal?: AbortSignal): Promise<Reply<WorkspaceBootstrap>> {
283
+ try {
284
+ return decodeReply(
285
+ await this.transport.workspace(signal),
286
+ decodeWorkspaceBootstrap,
287
+ );
288
+ } catch {
289
+ return unavailable(false);
290
+ }
291
+ }
292
+ tasks(
293
+ request: Omit<ListRequest, "reader">,
294
+ signal?: AbortSignal,
295
+ ): Promise<Reply<HistoryReadResult>> {
296
+ return this.page("tasks", request, signal);
297
+ }
298
+ runs(
299
+ request: Omit<ListRequest, "reader">,
300
+ signal?: AbortSignal,
301
+ ): Promise<Reply<HistoryReadResult>> {
302
+ return this.page("runs", request, signal);
303
+ }
304
+ messages(
305
+ request: Omit<ListRequest, "reader">,
306
+ signal?: AbortSignal,
307
+ ): Promise<Reply<HistoryReadResult>> {
308
+ return this.page("messages", request, signal);
309
+ }
310
+ tools(
311
+ request: Omit<ListRequest, "reader">,
312
+ signal?: AbortSignal,
313
+ ): Promise<Reply<HistoryReadResult>> {
314
+ return this.page("tools", request, signal);
315
+ }
316
+ contents(
317
+ request: Omit<ListRequest, "reader">,
318
+ signal?: AbortSignal,
319
+ ): Promise<Reply<HistoryReadResult>> {
320
+ return this.page("contents", request, signal);
321
+ }
322
+ events(
323
+ request: Omit<ListRequest, "reader">,
324
+ signal?: AbortSignal,
325
+ ): Promise<Reply<HistoryReadResult>> {
326
+ return this.page("events", request, signal);
327
+ }
328
+ checks(
329
+ request: Omit<ListRequest, "reader">,
330
+ signal?: AbortSignal,
331
+ ): Promise<Reply<HistoryReadResult>> {
332
+ return this.page("checks", request, signal);
333
+ }
334
+ changes(
335
+ request: Omit<ListRequest, "reader">,
336
+ signal?: AbortSignal,
337
+ ): Promise<Reply<HistoryReadResult>> {
338
+ return this.page("changes", request, signal);
339
+ }
340
+ reviews(
341
+ request: Omit<ListRequest, "reader">,
342
+ signal?: AbortSignal,
343
+ ): Promise<Reply<HistoryReadResult>> {
344
+ return this.page("reviews", request, signal);
345
+ }
346
+ private async page(
347
+ reader: ReaderKind,
348
+ input: Omit<ListRequest, "reader">,
349
+ signal?: AbortSignal,
350
+ ): Promise<Reply<HistoryReadResult>> {
351
+ const request = decodeListRequest({ ...input, reader });
352
+ try {
353
+ const reply = decodeReply(
354
+ await this.transport[reader](request, signal),
355
+ decodeHistoryReadResult,
356
+ );
357
+ if (reply.kind === "ok" && !historyReplyMatches(request, reply.value))
358
+ return unavailable(false);
359
+ return reply;
360
+ } catch {
361
+ return unavailable(false);
362
+ }
363
+ }
364
+ async graph(
365
+ input: GraphReadRequest,
366
+ signal?: AbortSignal,
367
+ ): Promise<Reply<GraphReadResult>> {
368
+ const request = decodeGraphReadRequest(input);
369
+ try {
370
+ const reply = decodeReply(
371
+ await this.transport.graph(request, signal),
372
+ decodeGraphReadResult,
373
+ );
374
+ return reply.kind === "ok" &&
375
+ canonicalJson(reply.value.request) !== canonicalJson(request)
376
+ ? unavailable(false)
377
+ : reply;
378
+ } catch {
379
+ return unavailable(false);
380
+ }
381
+ }
382
+ async *export(
383
+ input: ExportRequest,
384
+ signal: AbortSignal,
385
+ ): AsyncGenerator<ExportFrame> {
386
+ const request = decodeExportRequest(input),
387
+ tracker = new ExportTracker(request);
388
+ let trailer: ExportFrame | undefined;
389
+ try {
390
+ if (signal.aborted) throw new ContractError("unavailable");
391
+ for await (const value of this.transport.export(request, signal)) {
392
+ if (signal.aborted) throw new ContractError("unavailable");
393
+ const frame = tracker.receive(value);
394
+ if (frame.event.kind === "trailer") trailer = frame;
395
+ else yield frame;
396
+ }
397
+ if (signal.aborted || !trailer) throw new ContractError("unavailable");
398
+ yield trailer; // only after original EOF, never synthesize a trailer
399
+ } catch {
400
+ throw new ContractError("unavailable");
401
+ }
402
+ }
403
+ async preflight(
404
+ expected: ContextVersion,
405
+ action: Action,
406
+ ): Promise<Reply<Preflight>> {
407
+ const request = decodePreflightRequest({ expected, action });
408
+ try {
409
+ const reply = decodeReply(
410
+ await this.transport.preflight(request),
411
+ decodePreflight,
412
+ );
413
+ if (
414
+ reply.kind === "ok" &&
415
+ (canonicalJson(reply.value.preview.version) !==
416
+ canonicalJson(request.expected) ||
417
+ canonicalJson(reply.value.preview.action) !==
418
+ canonicalJson(request.action))
419
+ )
420
+ return unavailable(false);
421
+ return reply;
422
+ } catch {
423
+ return unavailable(false);
424
+ }
425
+ }
426
+ // Explicitly named methods. IDs and confirmations come from the caller; uncertainty
427
+ // NEVER mints a new ID, retries a command, refreshes auth or repeats preflight.
428
+ create(command: NamedCommand<"create">): Promise<Reply<Receipt>> {
429
+ return this.execute("create", command);
430
+ }
431
+ savePlan(command: NamedCommand<"save-plan">): Promise<Reply<Receipt>> {
432
+ return this.execute("save-plan", command);
433
+ }
434
+ generatePlan(
435
+ command: NamedCommand<"generate-plan">,
436
+ ): Promise<Reply<Receipt>> {
437
+ return this.execute("generate-plan", command);
438
+ }
439
+ start(command: NamedCommand<"start">): Promise<Reply<Receipt>> {
440
+ return this.execute("start", command);
441
+ }
442
+ check(command: NamedCommand<"check">): Promise<Reply<Receipt>> {
443
+ return this.execute("check", command);
444
+ }
445
+ review(command: NamedCommand<"review">): Promise<Reply<Receipt>> {
446
+ return this.execute("review", command);
447
+ }
448
+ private async execute(
449
+ kind: Action["kind"],
450
+ input: Command,
451
+ ): Promise<Reply<Receipt>> {
452
+ const command = decodeCommand(input);
453
+ if (command.action.kind !== kind)
454
+ return { kind: "error", error: publicError("invalid") };
455
+ try {
456
+ const reply = decodeReply(
457
+ await this.transport.command(command),
458
+ decodeReceipt,
459
+ );
460
+ if (reply.kind === "ok") {
461
+ if (reply.value.commandId !== command.commandId)
462
+ return unavailable(true);
463
+ // Reuse the admission/history correlation rules: matching commandId alone
464
+ // does not make a missing created task or wrong operation settlement valid.
465
+ decodeStoredCommand({
466
+ identity: {
467
+ kind: "action",
468
+ expected: command.expected,
469
+ action: command.action,
470
+ },
471
+ receipt: reply.value,
472
+ });
473
+ }
474
+ return reply;
475
+ } catch {
476
+ return unavailable(true);
477
+ }
478
+ }
479
+ async cancel(input: CancelCommand): Promise<Reply<Receipt>> {
480
+ const command = decodeCancel(input);
481
+ try {
482
+ const reply = decodeReply(
483
+ await this.transport.cancel(command),
484
+ decodeReceipt,
485
+ );
486
+ if (reply.kind === "ok") {
487
+ if (reply.value.commandId !== command.commandId)
488
+ return unavailable(true);
489
+ decodeStoredCommand({
490
+ identity: {
491
+ kind: "cancel",
492
+ epoch: command.epoch,
493
+ runId: command.runId,
494
+ },
495
+ receipt: reply.value,
496
+ });
497
+ }
498
+ return reply;
499
+ } catch {
500
+ return unavailable(true);
501
+ }
502
+ }
503
+ async receipt(
504
+ commandId: string,
505
+ signal?: AbortSignal,
506
+ ): Promise<ReceiptLookup> {
507
+ decodeId(commandId);
508
+ try {
509
+ const result = decodeReceiptLookup(
510
+ await this.transport.receipt(commandId, signal),
511
+ );
512
+ if (result.status === "found" && result.receipt.commandId !== commandId)
513
+ throw new Error("invalid");
514
+ return result;
515
+ } catch {
516
+ return {
517
+ status: "inconclusive",
518
+ error: publicError("receipt-inconclusive", "uncertain"),
519
+ };
520
+ }
521
+ }
522
+ async snapshot(input: Scope, signal?: AbortSignal): Promise<Reply<Snapshot>> {
523
+ const scope = decodeScope(input);
524
+ try {
525
+ const reply = decodeReply(
526
+ await this.transport.snapshot(scope, signal),
527
+ decodeSnapshot,
528
+ );
529
+ if (
530
+ reply.kind === "ok" &&
531
+ canonicalJson(reply.value.scope) !== canonicalJson(scope)
532
+ )
533
+ return unavailable(false);
534
+ return reply;
535
+ } catch {
536
+ return unavailable(false);
537
+ }
538
+ }
539
+ async detail(
540
+ input: ReadCursor,
541
+ signal?: AbortSignal,
542
+ ): Promise<Reply<DetailReadResult>> {
543
+ const cursor = decodeCursor(input);
544
+ try {
545
+ const reply = decodeReply(
546
+ await this.transport.detail(cursor, signal),
547
+ decodeDetailReadResult,
548
+ );
549
+ if (
550
+ reply.kind === "ok" &&
551
+ canonicalJson(reply.value.cursor) !== canonicalJson(cursor)
552
+ )
553
+ return unavailable(false);
554
+ return reply;
555
+ } catch {
556
+ return unavailable(false);
557
+ }
558
+ }
559
+ async search(
560
+ input: ReadCursor,
561
+ signal?: AbortSignal,
562
+ ): Promise<Reply<SearchPage>> {
563
+ const cursor = decodeCursor(input);
564
+ try {
565
+ const reply = decodeReply(
566
+ await this.transport.search(cursor, signal),
567
+ decodeSearchPage,
568
+ );
569
+ if (
570
+ reply.kind === "ok" &&
571
+ canonicalJson(reply.value.cursor) !== canonicalJson(cursor)
572
+ )
573
+ return unavailable(false);
574
+ return reply;
575
+ } catch {
576
+ return unavailable(false);
577
+ }
578
+ }
579
+ }
580
+
581
+ export interface ObservationState {
582
+ snapshot: Snapshot;
583
+ connection: "current" | "stale" | "resync";
584
+ lastObservedMs: number;
585
+ }
586
+ /** Read-only correlation/revision reducer; never a scheduler. A new snapshot begins
587
+ * a new subscription generation; old socket callbacks cannot mutate the new view.
588
+ * Polling adapters call needsPoll every 30s even after the socket's last update.
589
+ */
590
+ export class ProjectionTracker {
591
+ private generation = 0;
592
+ private snapshotRequest = 0;
593
+ private observation: ObservationState | undefined;
594
+ private lastSnapshotMs = 0;
595
+ /** Capture before the read await; pass the ticket to install. */
596
+ beginSnapshot(): number {
597
+ return ++this.snapshotRequest;
598
+ }
599
+ install(input: unknown, now: number, request: number): number | undefined {
600
+ if (request !== this.snapshotRequest) return undefined;
601
+ const snapshot = decodeSnapshot(input);
602
+ const previous = this.observation?.snapshot;
603
+ if (
604
+ previous &&
605
+ previous.storeId === snapshot.storeId &&
606
+ canonicalJson(previous.scope) === canonicalJson(snapshot.scope) &&
607
+ compareSequence(snapshot.committedSeq, previous.committedSeq) < 0
608
+ ) {
609
+ this.resync();
610
+ return undefined;
611
+ }
612
+ this.observation = {
613
+ snapshot,
614
+ connection: "current",
615
+ lastObservedMs: now,
616
+ };
617
+ this.lastSnapshotMs = now;
618
+ return ++this.generation;
619
+ }
620
+ private resync(): "resync" {
621
+ if (this.observation) this.observation.connection = "resync";
622
+ // A response started before the gap/epoch fault cannot repair that fault,
623
+ // even if it happens to carry the same committed watermark.
624
+ this.snapshotRequest++;
625
+ return "resync";
626
+ }
627
+ get state(): ObservationState | undefined {
628
+ return this.observation && structuredClone(this.observation);
629
+ }
630
+ needsPoll(now: number): boolean {
631
+ return (
632
+ !this.observation ||
633
+ this.observation.connection === "resync" ||
634
+ now < this.lastSnapshotMs ||
635
+ now - this.lastSnapshotMs >= LIMITS.reconcileMs
636
+ );
637
+ }
638
+ tick(now: number): void {
639
+ const state = this.observation;
640
+ if (
641
+ state &&
642
+ state.connection !== "resync" &&
643
+ (now < state.lastObservedMs ||
644
+ now - state.lastObservedMs >= LIMITS.staleMs)
645
+ )
646
+ state.connection = "stale";
647
+ }
648
+ receive(
649
+ input: unknown,
650
+ generation: number,
651
+ now: number,
652
+ ): "applied" | "duplicate" | "ignored" | "resync" {
653
+ const state = this.observation;
654
+ if (!state || generation !== this.generation) return "ignored";
655
+ if (state.connection === "resync") return "resync";
656
+ let event: LiveEvent;
657
+ try {
658
+ event = decodeLiveEvent(input);
659
+ } catch {
660
+ return this.resync();
661
+ }
662
+ const snapshot = state.snapshot;
663
+ if (
664
+ event.storeId !== snapshot.storeId ||
665
+ event.epoch !== snapshot.epoch ||
666
+ canonicalJson(event.scope) !== canonicalJson(snapshot.scope)
667
+ ) {
668
+ return this.resync();
669
+ }
670
+ const comparison = compareSequence(
671
+ event.committedSeq,
672
+ snapshot.committedSeq,
673
+ );
674
+ if (comparison < 0) return "duplicate";
675
+ if (event.kind === "heartbeat") {
676
+ if (comparison > 0) {
677
+ return this.resync();
678
+ }
679
+ state.lastObservedMs = now;
680
+ state.connection = "current";
681
+ return "applied";
682
+ }
683
+ if (comparison === 0) return "duplicate";
684
+ if (event.previousSeq !== snapshot.committedSeq) {
685
+ return this.resync();
686
+ }
687
+ if (
688
+ controlWorkspace(event.context) !== controlWorkspace(snapshot.context) ||
689
+ controlTask(event.context) !== controlTask(snapshot.context) ||
690
+ (event.context.status === "available" &&
691
+ snapshot.context.status === "available" &&
692
+ event.context.version.controlRev < snapshot.context.version.controlRev)
693
+ )
694
+ return this.resync();
695
+ // One update contains ALL row changes at its committed watermark. Validate
696
+ // on a copy: a bad sibling cannot leave a partly applied batch behind.
697
+ const rows = [...snapshot.runs];
698
+ for (const key of event.removed) {
699
+ const index = rows.findIndex((r) => (r.childRunId ?? r.runId) === key);
700
+ if (index === -1 || rows[index].lifecycle !== "settled")
701
+ return this.resync();
702
+ rows.splice(index, 1);
703
+ }
704
+ for (const run of event.runs) {
705
+ const key = run.childRunId ?? run.runId;
706
+ const index = rows.findIndex((r) => (r.childRunId ?? r.runId) === key);
707
+ if (index === -1) rows.push(run);
708
+ else {
709
+ const previous = rows[index];
710
+ if (
711
+ previous.runId !== run.runId ||
712
+ previous.childRunId !== run.childRunId ||
713
+ previous.assignmentId !== run.assignmentId ||
714
+ canonicalJson(previous.admission) !== canonicalJson(run.admission)
715
+ )
716
+ return this.resync();
717
+ rows[index] = run;
718
+ }
719
+ }
720
+ if (rows.length > LIMITS.maxPageEntries) return this.resync();
721
+ const candidate = {
722
+ ...snapshot,
723
+ runs: rows,
724
+ context: event.context,
725
+ committedSeq: event.committedSeq,
726
+ horizon: {
727
+ ...snapshot.horizon,
728
+ historicalRuns: {
729
+ ...snapshot.horizon.historicalRuns,
730
+ snapshotSeq: event.committedSeq,
731
+ },
732
+ },
733
+ };
734
+ try {
735
+ decodeSnapshot(candidate);
736
+ } catch {
737
+ return this.resync();
738
+ }
739
+ snapshot.runs = rows;
740
+ snapshot.horizon = candidate.horizon;
741
+ snapshot.context = event.context;
742
+ snapshot.committedSeq = event.committedSeq;
743
+ state.lastObservedMs = now;
744
+ state.connection = "current";
745
+ return "applied";
746
+ }
747
+ }