@abelxiaoxing/cadence 2.0.21 → 2.0.23

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.
@@ -3,37 +3,20 @@
3
3
  import path from "node:path";
4
4
 
5
5
  // src/task-store.ts
6
- import { spawnSync } from "node:child_process";
7
- import { createHash, randomUUID } from "node:crypto";
6
+ import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
8
7
  import {
9
- closeSync,
10
- constants,
11
- fstatSync,
12
- fsyncSync,
13
- lstatSync,
14
- mkdirSync,
15
- openSync,
8
+ closeSync as closeSync3,
9
+ fsyncSync as fsyncSync2,
10
+ openSync as openSync3,
16
11
  readdirSync,
17
- readSync,
18
- realpathSync,
19
12
  renameSync,
20
- unlinkSync,
21
- writeFileSync
13
+ unlinkSync as unlinkSync2,
14
+ writeFileSync as writeFileSync2
22
15
  } from "node:fs";
23
- import { homedir, hostname } from "node:os";
24
- import {
25
- dirname,
26
- isAbsolute,
27
- join,
28
- parse,
29
- relative,
30
- resolve,
31
- sep
32
- } from "node:path";
16
+ import { hostname as hostname2 } from "node:os";
17
+ import { join as join3 } from "node:path";
33
18
 
34
- // src/task-types.ts
35
- var TASK_SCHEMA = "cadence-workflow-assistant/1";
36
- var MAX_RECORD_BYTES = 128 * 1024;
19
+ // src/data-shapes.ts
37
20
  function object(value) {
38
21
  return value !== null && typeof value === "object" && !Array.isArray(value);
39
22
  }
@@ -43,6 +26,9 @@ function boundedString(value, max) {
43
26
  function validId(value) {
44
27
  return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value);
45
28
  }
29
+ // src/task-types.ts
30
+ var TASK_SCHEMA = "cadence-workflow-assistant/1";
31
+ var MAX_RECORD_BYTES = 128 * 1024;
46
32
  function decodeCheck(value) {
47
33
  if (!object(value) || !boundedString(value.name, 128) || !value.name.trim() || !boundedString(value.command, 4096) || !value.command.trim() || !Number.isInteger(value.timeoutMs) || Number(value.timeoutMs) < 100 || Number(value.timeoutMs) > 1200000)
48
34
  throw new Error("Invalid check definition");
@@ -111,7 +97,29 @@ function diagnosticText(value, maximum = 4096, secrets = []) {
111
97
  return bytes.toString("utf8").replace(/\ufffd$/, "") + marker;
112
98
  }
113
99
 
114
- // src/task-store.ts
100
+ // src/workspace.ts
101
+ import { spawnSync } from "node:child_process";
102
+ import { createHash } from "node:crypto";
103
+ import {
104
+ closeSync,
105
+ constants,
106
+ fstatSync,
107
+ lstatSync,
108
+ mkdirSync,
109
+ openSync,
110
+ readSync,
111
+ realpathSync
112
+ } from "node:fs";
113
+ import { homedir } from "node:os";
114
+ import {
115
+ dirname,
116
+ isAbsolute,
117
+ join,
118
+ parse,
119
+ relative,
120
+ resolve,
121
+ sep
122
+ } from "node:path";
115
123
  function parseRecord(data) {
116
124
  try {
117
125
  return JSON.parse(data.toString("utf8"));
@@ -247,13 +255,6 @@ function defaultStateHome() {
247
255
  throw new Error("State home must be absolute");
248
256
  return candidate ?? join(homedir(), process.platform === "win32" ? "AppData/Local" : ".local/state");
249
257
  }
250
- function recordText(record) {
251
- return `${JSON.stringify(record, null, 2)}
252
- `;
253
- }
254
- function contentIdentity(text) {
255
- return createHash("sha256").update(text).digest("hex");
256
- }
257
258
  function canonicalAmbientRoot(path) {
258
259
  const full = resolve(path);
259
260
  try {
@@ -293,29 +294,39 @@ function effectiveWorkspace(requested, stateHome, _home) {
293
294
  throw new Error("Choose a project outside Cadence private state; no default workspace was created");
294
295
  return { workspace: requested, redirected: false };
295
296
  }
296
-
297
- class TaskStore {
297
+ // src/service/checkout-lock.ts
298
+ import { randomUUID } from "node:crypto";
299
+ import {
300
+ closeSync as closeSync2,
301
+ fsyncSync,
302
+ lstatSync as lstatSync2,
303
+ openSync as openSync2,
304
+ realpathSync as realpathSync2,
305
+ unlinkSync,
306
+ writeFileSync
307
+ } from "node:fs";
308
+ import { hostname } from "node:os";
309
+ import { dirname as dirname2, join as join2 } from "node:path";
310
+ class CheckoutStore {
298
311
  root;
299
- tasks;
300
312
  privateRoot;
301
313
  constructor(root, stateHome = defaultStateHome()) {
302
- this.root = realpathSync(root);
314
+ this.root = realpathSync2(root);
303
315
  if (privateWorkspace(this.root, stateHome))
304
316
  throw new Error("Private state cannot be a workspace");
305
- this.tasks = join(this.root, ".cadence", "assistant", "tasks");
306
317
  this.privateRoot = privateRootFor(this.root, stateHome);
307
318
  if (contains(this.root, this.privateRoot))
308
319
  throw new Error("Private state home must be outside the working copy");
309
320
  }
310
321
  get lockPath() {
311
- return join(this.privateRoot, "checkout.lock");
322
+ return join2(this.privateRoot, "checkout.lock");
312
323
  }
313
324
  inspectPrivate() {
314
- const namespace = dirname(this.privateRoot);
325
+ const namespace = dirname2(this.privateRoot);
315
326
  for (const path of [namespace, this.privateRoot]) {
316
327
  if (!safeDirectory(path))
317
328
  continue;
318
- const st = lstatSync(path);
329
+ const st = lstatSync2(path);
319
330
  if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
320
331
  throw new Error("Private state permissions refused");
321
332
  }
@@ -325,7 +336,7 @@ class TaskStore {
325
336
  if (!safeDirectory(this.privateRoot))
326
337
  return;
327
338
  try {
328
- const st = lstatSync(path);
339
+ const st = lstatSync2(path);
329
340
  if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1 || st.size > max || process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
330
341
  throw new Error("Unsafe private state file");
331
342
  } catch (error) {
@@ -356,7 +367,7 @@ class TaskStore {
356
367
  };
357
368
  let fd;
358
369
  try {
359
- fd = retryWindowsFileOperation(() => openSync(this.lockPath, "wx", 384));
370
+ fd = retryWindowsFileOperation(() => openSync2(this.lockPath, "wx", 384));
360
371
  } catch {
361
372
  throw new Error("Checkout busy or unresolved lock; inspect and explicitly recover only a dead owner");
362
373
  }
@@ -364,7 +375,7 @@ class TaskStore {
364
375
  writeFileSync(fd, JSON.stringify(lock));
365
376
  fsyncSync(fd);
366
377
  } finally {
367
- closeSync(fd);
378
+ closeSync2(fd);
368
379
  }
369
380
  return lock;
370
381
  }
@@ -378,8 +389,24 @@ class TaskStore {
378
389
  this.assertOwner(lock);
379
390
  retryWindowsFileOperation(() => unlinkSync(this.lockPath), () => this.assertOwner(lock));
380
391
  }
392
+ }
393
+
394
+ // src/task-store.ts
395
+ function recordText(record) {
396
+ return `${JSON.stringify(record, null, 2)}
397
+ `;
398
+ }
399
+ function contentIdentity(text) {
400
+ return createHash2("sha256").update(text).digest("hex");
401
+ }
402
+ class TaskStore extends CheckoutStore {
403
+ tasks;
404
+ constructor(root, stateHome = defaultStateHome()) {
405
+ super(root, stateHome);
406
+ this.tasks = join3(this.root, ".cadence", "assistant", "tasks");
407
+ }
381
408
  recover(expected, acknowledgeEscapedProcesses) {
382
- if (!acknowledgeEscapedProcesses || expected.host !== hostname())
409
+ if (!acknowledgeEscapedProcesses || expected.host !== hostname2())
383
410
  throw new Error("Recovery requires informed local-owner confirmation");
384
411
  this.assertOwner(expected);
385
412
  try {
@@ -388,7 +415,7 @@ class TaskStore {
388
415
  if (!object(error) || error.code !== "ESRCH")
389
416
  throw new Error("Owner status unknown");
390
417
  this.assertOwner(expected);
391
- unlinkSync(this.lockPath);
418
+ unlinkSync2(this.lockPath);
392
419
  return;
393
420
  }
394
421
  throw new Error("Owner is live; stop it first");
@@ -399,10 +426,10 @@ class TaskStore {
399
426
  loadSnapshot(id) {
400
427
  if (!validId(id))
401
428
  throw new Error("Invalid task id");
402
- const directory = join(this.tasks, id);
429
+ const directory = join3(this.tasks, id);
403
430
  if (safeDirectory(directory) && readdirSync(directory).some((name) => name !== "task.json"))
404
431
  throw new Error("Unknown task directory content; preserved without reset");
405
- const data = readBounded(join(this.tasks, id, "task.json"), MAX_RECORD_BYTES);
432
+ const data = readBounded(join3(this.tasks, id, "task.json"), MAX_RECORD_BYTES);
406
433
  if (!data)
407
434
  return;
408
435
  const record = decodeTask(parseRecord(data));
@@ -431,7 +458,7 @@ class TaskStore {
431
458
  if (Buffer.byteLength(text) > MAX_RECORD_BYTES)
432
459
  throw new Error("Task record limit");
433
460
  const lock = held ?? this.acquire();
434
- if (lock.pid !== process.pid || lock.host !== hostname())
461
+ if (lock.pid !== process.pid || lock.host !== hostname2())
435
462
  throw new Error("Not the lock owner");
436
463
  try {
437
464
  this.assertOwner(lock);
@@ -440,25 +467,25 @@ class TaskStore {
440
467
  throw new Error("Revision conflict");
441
468
  if (current?.identity !== expectedIdentity)
442
469
  throw new Error("Task content changed; reopen it");
443
- const dir = join(this.tasks, next.id);
470
+ const dir = join3(this.tasks, next.id);
444
471
  safeDirectory(dir, true);
445
472
  if (!current && readdirSync(dir).length)
446
473
  throw new Error("Unknown task directory");
447
- const temporary = join(dir, `task.${randomUUID()}.tmp`);
448
- const fd = openSync(temporary, "wx", 384);
474
+ const temporary = join3(dir, `task.${randomUUID2()}.tmp`);
475
+ const fd = openSync3(temporary, "wx", 384);
449
476
  try {
450
- writeFileSync(fd, text);
451
- fsyncSync(fd);
477
+ writeFileSync2(fd, text);
478
+ fsyncSync2(fd);
452
479
  } finally {
453
- closeSync(fd);
480
+ closeSync3(fd);
454
481
  }
455
482
  this.assertOwner(lock);
456
- const latest = readBounded(join(dir, "task.json"), MAX_RECORD_BYTES);
483
+ const latest = readBounded(join3(dir, "task.json"), MAX_RECORD_BYTES);
457
484
  if ((latest ? contentIdentity(latest) : undefined) !== expectedIdentity)
458
485
  throw new Error("Task content changed; replacement preserved without overwrite");
459
- retryWindowsFileOperation(() => renameSync(temporary, join(dir, "task.json")), () => {
486
+ retryWindowsFileOperation(() => renameSync(temporary, join3(dir, "task.json")), () => {
460
487
  this.assertOwner(lock);
461
- const now = readBounded(join(dir, "task.json"), MAX_RECORD_BYTES);
488
+ const now = readBounded(join3(dir, "task.json"), MAX_RECORD_BYTES);
462
489
  if ((now ? contentIdentity(now) : undefined) !== expectedIdentity)
463
490
  throw new Error("Task content changed; replacement preserved without overwrite");
464
491
  });
@@ -469,7 +496,7 @@ class TaskStore {
469
496
  }
470
497
  }
471
498
  checks() {
472
- const data = readBounded(join(this.root, ".cadence", "assistant", "config.json"), 32768);
499
+ const data = readBounded(join3(this.root, ".cadence", "assistant", "config.json"), 32768);
473
500
  if (!data)
474
501
  return [];
475
502
  const value = parseRecord(data);
@@ -483,16 +510,16 @@ class TaskStore {
483
510
  readLog(name) {
484
511
  if (!/^[a-f0-9-]{36}\.log$/.test(name))
485
512
  throw new Error("Invalid diagnostic name");
486
- this.inspectPrivateFile(join(this.privateRoot, name), 65536);
487
- const data = readBounded(join(this.privateRoot, name), 65536);
513
+ this.inspectPrivateFile(join3(this.privateRoot, name), 65536);
514
+ const data = readBounded(join3(this.privateRoot, name), 65536);
488
515
  return data ? diagnosticText(data.toString("utf8"), 65536) : "Private diagnostic unavailable";
489
516
  }
490
517
  writeLog(lock, output) {
491
- if (lock.pid !== process.pid || lock.host !== hostname())
518
+ if (lock.pid !== process.pid || lock.host !== hostname2())
492
519
  throw new Error("Not the lock owner");
493
520
  this.assertOwner(lock);
494
- const name = `${randomUUID()}.log`;
495
- writeFileSync(join(this.privateRoot, name), diagnosticText(output, 65536), {
521
+ const name = `${randomUUID2()}.log`;
522
+ writeFileSync2(join3(this.privateRoot, name), diagnosticText(output, 65536), {
496
523
  flag: "wx",
497
524
  mode: 384
498
525
  });
@@ -500,6 +527,7 @@ class TaskStore {
500
527
  }
501
528
  }
502
529
  var TASK_CONFIG_SCHEMA = "cadence-assistant-checks/1";
530
+ var stores = new WeakMap;
503
531
 
504
532
  // src/operator-tools.ts
505
533
  var observed = (cwd) => resolveWorkspace(effectiveWorkspace(cwd).workspace);
@@ -0,0 +1,142 @@
1
+ /** Existing checkout lock identity and original filesystem semantics, without task CRUD. */
2
+ import { randomUUID } from "node:crypto";
3
+ import {
4
+ closeSync,
5
+ fsyncSync,
6
+ lstatSync,
7
+ openSync,
8
+ realpathSync,
9
+ unlinkSync,
10
+ writeFileSync,
11
+ } from "node:fs";
12
+ import { hostname } from "node:os";
13
+ import { dirname, join } from "node:path";
14
+ import { object, validId } from "../data-shapes.ts";
15
+ import {
16
+ absent,
17
+ contains,
18
+ defaultStateHome,
19
+ parseRecord,
20
+ privateRootFor,
21
+ privateWorkspace,
22
+ readBounded,
23
+ retryWindowsFileOperation,
24
+ safeDirectory,
25
+ } from "../workspace.ts";
26
+ export interface CheckoutLock {
27
+ schema: "cadence-assistant-lock/1";
28
+ token: string;
29
+ pid: number;
30
+ host: string;
31
+ runId: string;
32
+ root: string;
33
+ }
34
+ export class CheckoutStore {
35
+ readonly root: string;
36
+ readonly privateRoot: string;
37
+ constructor(root: string, stateHome = defaultStateHome()) {
38
+ this.root = realpathSync(root);
39
+ if (privateWorkspace(this.root, stateHome))
40
+ throw new Error("Private state cannot be a workspace");
41
+ this.privateRoot = privateRootFor(this.root, stateHome);
42
+ if (contains(this.root, this.privateRoot))
43
+ throw new Error("Private state home must be outside the working copy");
44
+ }
45
+ get lockPath(): string {
46
+ return join(this.privateRoot, "checkout.lock");
47
+ }
48
+ private inspectPrivate(): void {
49
+ const namespace = dirname(this.privateRoot);
50
+ for (const path of [namespace, this.privateRoot]) {
51
+ if (!safeDirectory(path)) continue;
52
+ const st = lstatSync(path);
53
+ if (
54
+ process.platform !== "win32" &&
55
+ ((st.mode & 0o077) !== 0 ||
56
+ (process.getuid && st.uid !== process.getuid()))
57
+ )
58
+ throw new Error("Private state permissions refused");
59
+ }
60
+ }
61
+ protected inspectPrivateFile(path: string, max: number): void {
62
+ this.inspectPrivate();
63
+ if (!safeDirectory(this.privateRoot)) return;
64
+ try {
65
+ const st = lstatSync(path);
66
+ if (
67
+ !st.isFile() ||
68
+ st.isSymbolicLink() ||
69
+ st.nlink !== 1 ||
70
+ st.size > max ||
71
+ (process.platform !== "win32" &&
72
+ ((st.mode & 0o077) !== 0 ||
73
+ (process.getuid && st.uid !== process.getuid())))
74
+ )
75
+ throw new Error("Unsafe private state file");
76
+ } catch (error) {
77
+ if (!absent(error)) throw error;
78
+ }
79
+ }
80
+ inspectLock(): CheckoutLock | undefined {
81
+ this.inspectPrivateFile(this.lockPath, 8192);
82
+ const data = readBounded(this.lockPath, 8192);
83
+ if (!data) return undefined;
84
+ const lock: unknown = parseRecord(data);
85
+ if (
86
+ !object(lock) ||
87
+ lock.schema !== "cadence-assistant-lock/1" ||
88
+ lock.root !== this.root ||
89
+ typeof lock.token !== "string" ||
90
+ !validId(lock.token) ||
91
+ typeof lock.runId !== "string" ||
92
+ !validId(lock.runId) ||
93
+ typeof lock.host !== "string" ||
94
+ !Number.isInteger(lock.pid) ||
95
+ Number(lock.pid) < 1
96
+ )
97
+ throw new Error("Unknown or corrupt coordination lock");
98
+ return lock as unknown as CheckoutLock;
99
+ }
100
+ acquire(): CheckoutLock {
101
+ this.inspectPrivate();
102
+ safeDirectory(this.privateRoot, true);
103
+ const lock: CheckoutLock = {
104
+ schema: "cadence-assistant-lock/1",
105
+ token: randomUUID(),
106
+ runId: randomUUID(),
107
+ pid: process.pid,
108
+ host: hostname(),
109
+ root: this.root,
110
+ };
111
+ let fd: number;
112
+ try {
113
+ fd = retryWindowsFileOperation(() =>
114
+ openSync(this.lockPath, "wx", 0o600),
115
+ );
116
+ } catch {
117
+ throw new Error(
118
+ "Checkout busy or unresolved lock; inspect and explicitly recover only a dead owner",
119
+ );
120
+ }
121
+ try {
122
+ writeFileSync(fd, JSON.stringify(lock));
123
+ fsyncSync(fd);
124
+ } finally {
125
+ closeSync(fd);
126
+ }
127
+ return lock;
128
+ }
129
+ assertOwner(lock: CheckoutLock): void {
130
+ if (JSON.stringify(this.inspectLock()) !== JSON.stringify(lock))
131
+ throw new Error("Coordination lock changed");
132
+ }
133
+ release(lock: CheckoutLock): void {
134
+ if (lock.pid !== process.pid || lock.host !== hostname())
135
+ throw new Error("Only the owning process may release a lock");
136
+ this.assertOwner(lock);
137
+ retryWindowsFileOperation(
138
+ () => unlinkSync(this.lockPath),
139
+ () => this.assertOwner(lock),
140
+ );
141
+ }
142
+ }
@@ -1,6 +1,7 @@
1
1
  /** Finite, authenticated workbench API. Ordinary chat is not task admission. */
2
2
  import {
3
3
  bool,
4
+ ContractError,
4
5
  canonicalJson,
5
6
  closed,
6
7
  decodeHash,
@@ -18,6 +19,7 @@ import {
18
19
  decodeWorkflowRequest,
19
20
  decodeWorkflowResponse,
20
21
  decodeWorkflowRun,
22
+ WORKFLOW_LIMITS,
21
23
  type WorkflowProposal,
22
24
  type WorkflowRequest,
23
25
  type WorkflowResponse,
@@ -31,8 +33,14 @@ export interface CadenceConversation {
31
33
  head: string | null;
32
34
  proposal: WorkflowProposal | null;
33
35
  runs: string[];
36
+ /** Present only in a read projection of an unusable retained record: every
37
+ * command for that conversation refuses; nothing is inferred or repaired.
38
+ * Never persisted. */
39
+ notice?: string;
34
40
  }
35
41
  export function decodeCadenceConversation(v: unknown): CadenceConversation {
42
+ const degraded =
43
+ v !== null && typeof v === "object" && Object.hasOwn(v, "notice");
36
44
  const o = closed(v, [
37
45
  "id",
38
46
  "revision",
@@ -40,6 +48,7 @@ export function decodeCadenceConversation(v: unknown): CadenceConversation {
40
48
  "head",
41
49
  "proposal",
42
50
  "runs",
51
+ ...(degraded ? ["notice"] : []),
43
52
  ]);
44
53
  return {
45
54
  id: decodeId(o.id),
@@ -48,11 +57,18 @@ export function decodeCadenceConversation(v: unknown): CadenceConversation {
48
57
  head: nullable(o.head, decodeId),
49
58
  proposal: nullable(o.proposal, decodeWorkflowProposal),
50
59
  runs: list(o.runs, 200, decodeId),
60
+ ...(degraded ? { notice: text(o.notice, 1024, true) } : {}),
51
61
  };
52
62
  }
63
+ /** Browser conversation that owns an active workflow; native runs have none. */
64
+ export interface WorkflowLink {
65
+ runId: string;
66
+ conversationId: string;
67
+ }
53
68
  export type WorkbenchRead =
54
69
  | { kind: "cadence-conversation"; id: string }
55
70
  | { kind: "workflow-active" }
71
+ | { kind: "workflow-history"; after: string | null; scan?: string }
56
72
  | { kind: "workflow-detail"; id: string; stepId: string }
57
73
  | { kind: "workflow-transcript"; id: string; stepId: string }
58
74
  | { kind: "workflow"; id: string }
@@ -106,13 +122,6 @@ export type WorkbenchCommand =
106
122
  epoch: string;
107
123
  request: WorkflowRequest;
108
124
  }
109
- | {
110
- kind: "link-task";
111
- commandId: string;
112
- epoch: string;
113
- taskId: string;
114
- conversationId: string;
115
- }
116
125
  | {
117
126
  kind: "send";
118
127
  commandId: string;
@@ -169,6 +178,12 @@ export interface FileEntry {
169
178
  directory: boolean;
170
179
  }
171
180
  export type WorkbenchResult =
181
+ | { kind: "workflow-history-progress"; after: string | null; scan: string }
182
+ | {
183
+ kind: "workflow-history";
184
+ entries: { id: string; run: WorkflowRun | null }[];
185
+ next: string | null;
186
+ }
172
187
  | {
173
188
  kind: "workflow-transcript";
174
189
  runId: string;
@@ -176,7 +191,12 @@ export type WorkbenchResult =
176
191
  turn: ChatTurn | null;
177
192
  }
178
193
  | { kind: "cadence-conversation"; state: CadenceConversation }
179
- | { kind: "workflow-active"; runs: WorkflowRun[] }
194
+ | {
195
+ kind: "workflow-active";
196
+ runs: WorkflowRun[];
197
+ /** At most one link per listed run; set by the workbench composition. */
198
+ links?: WorkflowLink[];
199
+ }
180
200
  | {
181
201
  kind: "cadence-grant";
182
202
  commandId: string;
@@ -232,6 +252,15 @@ export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
232
252
  const o = closed(v, ["kind", "id"]);
233
253
  return { kind, id: decodeId(o.id) };
234
254
  }
255
+ case "workflow-history": {
256
+ const continued = Object.hasOwn(v as object, "scan");
257
+ const o = closed(v, ["kind", "after", ...(continued ? ["scan"] : [])]);
258
+ return {
259
+ kind,
260
+ after: nullable(o.after, decodeId),
261
+ ...(continued ? { scan: decodeId(o.scan) } : {}),
262
+ };
263
+ }
235
264
  case "workflow-active":
236
265
  closed(v, ["kind"]);
237
266
  return { kind };
@@ -347,22 +376,7 @@ export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
347
376
  request: decodeWorkflowRequest(o.request),
348
377
  };
349
378
  }
350
- if (kind === "link-task") {
351
- const o = closed(v, [
352
- "kind",
353
- "commandId",
354
- "epoch",
355
- "taskId",
356
- "conversationId",
357
- ]);
358
- return {
359
- kind,
360
- commandId: decodeId(o.commandId),
361
- epoch: decodeId(o.epoch),
362
- taskId: decodeId(o.taskId),
363
- conversationId: decodeId(o.conversationId),
364
- };
365
- }
379
+
366
380
  if (kind === "send") {
367
381
  const o = closed(v, [
368
382
  "kind",
@@ -473,9 +487,51 @@ export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
473
487
  const o = closed(v, ["kind", "state"]);
474
488
  return { kind: k, state: decodeCadenceConversation(o.state) };
475
489
  }
490
+ if (k === "workflow-history-progress") {
491
+ const o = closed(v, ["kind", "after", "scan"]);
492
+ return {
493
+ kind: k,
494
+ after: nullable(o.after, decodeId),
495
+ scan: decodeId(o.scan),
496
+ };
497
+ }
498
+ if (k === "workflow-history") {
499
+ const o = closed(v, ["kind", "entries", "next"]);
500
+ const entries = list(o.entries, WORKFLOW_LIMITS.historyPage, (v) => {
501
+ const e = closed(v, ["id", "run"]);
502
+ const id = decodeId(e.id),
503
+ run = nullable(e.run, decodeWorkflowRun);
504
+ if (run && run.id !== id) throw new ContractError("invalid");
505
+ return { id, run };
506
+ });
507
+ const next = nullable(o.next, decodeId);
508
+ if (
509
+ new Set(entries.map((e) => e.id)).size !== entries.length ||
510
+ (next !== null && next !== entries.at(-1)?.id)
511
+ )
512
+ throw new ContractError("invalid");
513
+ return { kind: k, entries, next };
514
+ }
476
515
  if (k === "workflow-active") {
477
- const o = closed(v, ["kind", "runs"]);
478
- return { kind: k, runs: list(o.runs, 4, decodeWorkflowRun) };
516
+ const linked =
517
+ v !== null && typeof v === "object" && Object.hasOwn(v, "links");
518
+ const o = closed(v, ["kind", "runs", ...(linked ? ["links"] : [])]);
519
+ const runs = list(o.runs, 4, decodeWorkflowRun);
520
+ const links = linked
521
+ ? list(o.links, 4, (value) => {
522
+ const l = closed(value, ["runId", "conversationId"]);
523
+ return {
524
+ runId: decodeId(l.runId),
525
+ conversationId: decodeId(l.conversationId),
526
+ };
527
+ })
528
+ : [];
529
+ if (
530
+ new Set(links.map((l) => l.runId)).size !== links.length ||
531
+ links.some((l) => !runs.some((r) => r.id === l.runId))
532
+ )
533
+ throw new ContractError("invalid");
534
+ return { kind: k, runs, links };
479
535
  }
480
536
  if (k === "cadence-grant") {
481
537
  const o = closed(v, [