@swirls/looms 0.25.0 → 0.27.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 (109) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/dist/actor/cell.d.ts +3 -1
  3. package/dist/actor/cell.js +1 -0
  4. package/dist/actor/host.d.ts +3 -1
  5. package/dist/actor/host.js +2 -1
  6. package/dist/actor/routing.d.ts +6 -3
  7. package/dist/actor/routing.js +6 -4
  8. package/dist/agent/agent-session.js +2 -2
  9. package/dist/agent/debugger/agent-chat.js +4 -3
  10. package/dist/agent/definitions.d.ts +16 -4
  11. package/dist/agent/effects.js +20 -9
  12. package/dist/agent/llm.d.ts +3 -1
  13. package/dist/agent/threads.js +4 -1
  14. package/dist/ai-vercel/messages.d.ts +3 -3
  15. package/dist/ai-vercel/messages.js +23 -6
  16. package/dist/ai-vercel/vercel-llm.js +1 -1
  17. package/dist/approval/steps.d.ts +3 -1
  18. package/dist/approval/steps.js +2 -2
  19. package/dist/client/client.d.ts +7 -1
  20. package/dist/client/client.js +5 -2
  21. package/dist/cloudflare/durable-object.d.ts +3 -1
  22. package/dist/cloudflare/durable-object.js +1 -0
  23. package/dist/cloudflare/queue-consumer.d.ts +3 -1
  24. package/dist/cloudflare/queue-consumer.js +2 -1
  25. package/dist/cloudflare/router.d.ts +3 -1
  26. package/dist/cloudflare/router.js +1 -1
  27. package/dist/core/compose.d.ts +8 -2
  28. package/dist/core/compose.js +6 -3
  29. package/dist/core/effects.d.ts +3 -0
  30. package/dist/core/envelope.d.ts +2 -0
  31. package/dist/core/envelope.js +2 -2
  32. package/dist/core/event-codec.d.ts +4 -3
  33. package/dist/core/event-codec.js +7 -7
  34. package/dist/core/fold.d.ts +3 -0
  35. package/dist/core/fold.js +3 -1
  36. package/dist/core/fork.js +1 -1
  37. package/dist/core/ids.d.ts +70 -5
  38. package/dist/core/ids.js +164 -41
  39. package/dist/core/snapshots.d.ts +2 -0
  40. package/dist/core/snapshots.js +2 -1
  41. package/dist/core/thread.d.ts +8 -0
  42. package/dist/debugger/app/assets/{index-GLtrulmj.js → index-D_FKxPrN.js} +2 -2
  43. package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
  44. package/dist/debugger/app/index.html +1 -1
  45. package/dist/debugger/context.d.ts +4 -1
  46. package/dist/debugger/context.js +4 -4
  47. package/dist/debugger/shell/debugger-shell.d.ts +4 -1
  48. package/dist/debugger/shell/debugger-shell.js +2 -2
  49. package/dist/debugger/shell/use-start-run.d.ts +1 -1
  50. package/dist/debugger/shell/use-start-run.js +1 -2
  51. package/dist/evaluator/steps.d.ts +2 -2
  52. package/dist/evaluator/steps.js +3 -3
  53. package/dist/react/react.d.ts +8 -3
  54. package/dist/react/react.js +15 -2
  55. package/dist/react/store.d.ts +5 -1
  56. package/dist/react/store.js +5 -4
  57. package/dist/runtime/cancellation.js +5 -5
  58. package/dist/runtime/deadline-transition-planner.d.ts +1 -0
  59. package/dist/runtime/deadline-transition-planner.js +8 -7
  60. package/dist/runtime/effect-admission.js +1 -0
  61. package/dist/runtime/helpers.d.ts +6 -5
  62. package/dist/runtime/helpers.js +8 -8
  63. package/dist/runtime/local-effect-executor.js +15 -10
  64. package/dist/runtime/looms.d.ts +6 -1
  65. package/dist/runtime/looms.js +4 -1
  66. package/dist/runtime/retry-policy.d.ts +2 -1
  67. package/dist/runtime/retry-policy.js +2 -1
  68. package/dist/runtime/runtime.js +10 -10
  69. package/dist/runtime/server/routes/runs.js +5 -5
  70. package/dist/runtime/types.d.ts +7 -1
  71. package/dist/runtime/wake-engine.js +10 -9
  72. package/dist/runtime/worker-callbacks.js +9 -8
  73. package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
  74. package/dist/runtime/worker-effect-dispatch.js +7 -5
  75. package/dist/swarm/board.d.ts +51 -3
  76. package/dist/swarm/board.js +8 -0
  77. package/dist/swarm/conformance.js +54 -1
  78. package/dist/swarm/definitions.d.ts +15 -0
  79. package/dist/swarm/definitions.js +3 -0
  80. package/dist/swarm/effects.d.ts +1009 -31
  81. package/dist/swarm/effects.js +73 -18
  82. package/dist/swarm/ensure.d.ts +9 -2
  83. package/dist/swarm/ensure.js +2 -1
  84. package/dist/swarm/events.d.ts +51 -0
  85. package/dist/swarm/events.js +10 -0
  86. package/dist/swarm/index.d.ts +1 -0
  87. package/dist/swarm/index.js +1 -0
  88. package/dist/swarm/memory-board.js +18 -2
  89. package/dist/swarm/module.d.ts +2055 -115
  90. package/dist/swarm/module.js +3 -1
  91. package/dist/swarm/prompt.d.ts +5 -0
  92. package/dist/swarm/prompt.js +27 -2
  93. package/dist/swarm/scope.d.ts +80 -0
  94. package/dist/swarm/services.d.ts +2 -2
  95. package/dist/swarm/services.js +5 -2
  96. package/dist/swarm/steps.d.ts +3 -3
  97. package/dist/swarm/steps.js +5 -5
  98. package/dist/swarm/threads.d.ts +4 -0
  99. package/dist/swarm/threads.js +24 -2
  100. package/dist/swarm/tools.d.ts +3 -2
  101. package/dist/swarm/tools.js +25 -18
  102. package/dist/workflow/effects.js +6 -5
  103. package/dist/workflow/graph.d.ts +3 -3
  104. package/dist/workflow/graph.js +5 -5
  105. package/dist/workflow/module.d.ts +8 -2
  106. package/dist/workflow/threads.d.ts +14 -2
  107. package/dist/workflow/threads.js +54 -44
  108. package/package.json +1 -1
  109. package/dist/debugger/app/assets/routes-CECZJJob.js +0 -47
@@ -1,5 +1,5 @@
1
1
  import { defineModule, makeDefinitionStore } from '@swirls/looms/core';
2
- import { activateEffect, claimTaskEffect, createTaskEffect, labelEffect, listTasksEffect, postEffect, pullEffect, readEffect, replyEffect, settleTaskEffect, } from './effects.js';
2
+ import { activateEffect, claimTaskEffect, createTaskEffect, labelEffect, listTasksEffect, postEffect, pullEffect, readEffect, replyEffect, settleTaskEffect, viewEffect, } from './effects.js';
3
3
  import { swarmStatus } from './projections.js';
4
4
  import { swarmModule } from './scope.js';
5
5
  import { SwarmServicesLive, memoryBoards } from './services.js';
@@ -20,6 +20,7 @@ export function swarm(options = {}) {
20
20
  post: postEffect,
21
21
  reply: replyEffect,
22
22
  read: readEffect,
23
+ view: viewEffect,
23
24
  label: labelEffect,
24
25
  createTask: createTaskEffect,
25
26
  listTasks: listTasksEffect,
@@ -51,6 +52,7 @@ export function swarm(options = {}) {
51
52
  re: null,
52
53
  mentions: ['triage'],
53
54
  labels: {},
55
+ attachments: [],
54
56
  createdAt: 1_000,
55
57
  },
56
58
  },
@@ -1,4 +1,9 @@
1
+ import type { Attachment } from '@swirls/looms/agent';
1
2
  import type { BoardEvent } from './board';
2
3
  import type { SwarmDefinition, SwarmMember } from './definitions';
4
+ /** Files shown to a member when it is woken. It reads the rest with `board_view`. */
5
+ export declare const ACTIVATION_ATTACHMENT_LIMIT = 10;
6
+ /** The distinct files on the messages of `events`, oldest first, up to the activation limit. */
7
+ export declare function activationAttachments(events: readonly BoardEvent[]): Attachment[];
3
8
  /** The first user message of a member's run: who it is, who else is here, and what happened. */
4
9
  export declare function activationPrompt(definition: SwarmDefinition, member: SwarmMember, events: readonly BoardEvent[]): string;
@@ -1,18 +1,42 @@
1
1
  const TEXT_LIMIT = 600;
2
+ /** Files shown to a member when it is woken. It reads the rest with `board_view`. */
3
+ export const ACTIVATION_ATTACHMENT_LIMIT = 10;
4
+ /** The distinct files on the messages of `events`, oldest first, up to the activation limit. */
5
+ export function activationAttachments(events) {
6
+ const files = new Map();
7
+ for (const event of events) {
8
+ for (const file of event.message?.attachments ?? []) {
9
+ if (!files.has(file.id)) {
10
+ files.set(file.id, file);
11
+ }
12
+ }
13
+ }
14
+ return [...files.values()].slice(0, ACTIVATION_ATTACHMENT_LIMIT);
15
+ }
2
16
  function clip(text) {
3
17
  return text.length > TEXT_LIMIT ? `${text.slice(0, TEXT_LIMIT)}…` : text;
4
18
  }
19
+ function describeFiles(event) {
20
+ const files = event.message?.attachments ?? [];
21
+ if (files.length === 0) {
22
+ return '';
23
+ }
24
+ const listed = files.map((file) => file.name
25
+ ? `${file.name} (${file.mediaType}, id "${file.id}")`
26
+ : `"${file.id}" (${file.mediaType})`);
27
+ return ` [files: ${listed.join(', ')}]`;
28
+ }
5
29
  function describeEvent(event, member) {
6
30
  const { message, task } = event;
7
31
  if (task) {
8
32
  const assigned = task.for === member ? ' (assigned to you)' : '';
9
33
  const what = event.type === 'task.created' ? `created by ${task.by}` : event.type.slice(5);
10
- return `- task [${task.id}] in ${task.channel}, ${what}, now ${task.state}${assigned}: ${clip(task.text)}`;
34
+ return `- task [${task.id}] in ${task.channel}, ${what}, now ${task.state}${assigned}: ${clip(task.text)}${describeFiles(event)}`;
11
35
  }
12
36
  if (message) {
13
37
  const where = message.re === null ? message.channel : `${message.channel}, thread [${message.re}]`;
14
38
  const mentioned = message.mentions.includes(member) ? ' (mentions you)' : '';
15
- return `- message [${message.id}] by ${message.by} in ${where}${mentioned}: ${clip(message.text)}`;
39
+ return `- message [${message.id}] by ${message.by} in ${where}${mentioned}: ${clip(message.text)}${describeFiles(event)}`;
16
40
  }
17
41
  return `- ${event.type} by ${event.by}`;
18
42
  }
@@ -25,6 +49,7 @@ export function activationPrompt(definition, member, events) {
25
49
  `You are "${member.name}", a member of the "${definition.name}" swarm.`,
26
50
  'The swarm shares one message board, and it is the only way members coordinate. Nothing you write outside the board reaches anyone. You keep no memory between activations, so put anything worth keeping on the board.',
27
51
  'Use the board tools to read threads, reply, hand work over by mentioning a member with @name or by creating a task, and claim a task before you work on it. Settle every task you claim.',
52
+ `Messages and tasks can carry files such as screenshots and recordings. The files on the activity below are attached to this message, up to ${ACTIVATION_ATTACHMENT_LIMIT} of them. Call board_view to look at the files on any other message. To share a file, pass its reference in \`attachments\` when you post, reply, or create a task.`,
28
53
  definition.instructions,
29
54
  others.length > 0 ? `Other members:\n${others.join('\n')}` : undefined,
30
55
  `New on the board for you, oldest first:\n${events.map((event) => describeEvent(event, member.name)).join('\n')}`,
@@ -17,6 +17,13 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
17
17
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
18
18
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
19
19
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
20
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
21
+ readonly id: import("effect/Schema").String;
22
+ readonly mediaType: import("effect/Schema").String;
23
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
24
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
25
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
26
+ }>>;
20
27
  readonly createdAt: import("effect/Schema").Finite;
21
28
  }>>;
22
29
  readonly task: import("effect/Schema").optional<import("effect/Schema").Struct<{
@@ -59,6 +66,13 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
59
66
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
60
67
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
61
68
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
69
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
70
+ readonly id: import("effect/Schema").String;
71
+ readonly mediaType: import("effect/Schema").String;
72
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
73
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
74
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
75
+ }>>;
62
76
  readonly createdAt: import("effect/Schema").Finite;
63
77
  }>;
64
78
  }>;
@@ -73,6 +87,13 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
73
87
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
74
88
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
75
89
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
90
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
91
+ readonly id: import("effect/Schema").String;
92
+ readonly mediaType: import("effect/Schema").String;
93
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
94
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
95
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
96
+ }>>;
76
97
  readonly createdAt: import("effect/Schema").Finite;
77
98
  }>;
78
99
  }>;
@@ -87,9 +108,28 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
87
108
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
88
109
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
89
110
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
111
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
112
+ readonly id: import("effect/Schema").String;
113
+ readonly mediaType: import("effect/Schema").String;
114
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
115
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
116
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
117
+ }>>;
90
118
  readonly createdAt: import("effect/Schema").Finite;
91
119
  }>>;
92
120
  }>;
121
+ 'attachments.viewed': import("effect/Schema").Struct<{
122
+ readonly callId: import("effect/Schema").String;
123
+ readonly board: import("effect/Schema").String;
124
+ readonly message: import("effect/Schema").String;
125
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
126
+ readonly id: import("effect/Schema").String;
127
+ readonly mediaType: import("effect/Schema").String;
128
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
129
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
130
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
131
+ }>>;
132
+ }>;
93
133
  'task.created': import("effect/Schema").Struct<{
94
134
  readonly callId: import("effect/Schema").String;
95
135
  readonly board: import("effect/Schema").String;
@@ -194,6 +234,13 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
194
234
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
195
235
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
196
236
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
237
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
238
+ readonly id: import("effect/Schema").String;
239
+ readonly mediaType: import("effect/Schema").String;
240
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
241
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
242
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
243
+ }>>;
197
244
  readonly createdAt: import("effect/Schema").Finite;
198
245
  }>>;
199
246
  readonly task: import("effect/Schema").optional<import("effect/Schema").Struct<{
@@ -236,6 +283,13 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
236
283
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
237
284
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
238
285
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
286
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
287
+ readonly id: import("effect/Schema").String;
288
+ readonly mediaType: import("effect/Schema").String;
289
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
290
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
291
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
292
+ }>>;
239
293
  readonly createdAt: import("effect/Schema").Finite;
240
294
  }>;
241
295
  }>;
@@ -250,6 +304,13 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
250
304
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
251
305
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
252
306
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
307
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
308
+ readonly id: import("effect/Schema").String;
309
+ readonly mediaType: import("effect/Schema").String;
310
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
311
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
312
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
313
+ }>>;
253
314
  readonly createdAt: import("effect/Schema").Finite;
254
315
  }>;
255
316
  }>;
@@ -264,9 +325,28 @@ export declare const swarmModule: import("@swirls/looms/core").ModuleScope<"swar
264
325
  readonly re: import("effect/Schema").NullOr<import("effect/Schema").String>;
265
326
  readonly mentions: import("effect/Schema").$Array<import("effect/Schema").String>;
266
327
  readonly labels: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").String>;
328
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
329
+ readonly id: import("effect/Schema").String;
330
+ readonly mediaType: import("effect/Schema").String;
331
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
332
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
333
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
334
+ }>>;
267
335
  readonly createdAt: import("effect/Schema").Finite;
268
336
  }>>;
269
337
  }>;
338
+ 'attachments.viewed': import("effect/Schema").Struct<{
339
+ readonly callId: import("effect/Schema").String;
340
+ readonly board: import("effect/Schema").String;
341
+ readonly message: import("effect/Schema").String;
342
+ readonly attachments: import("effect/Schema").$Array<import("effect/Schema").Struct<{
343
+ readonly id: import("effect/Schema").String;
344
+ readonly mediaType: import("effect/Schema").String;
345
+ readonly name: import("effect/Schema").optional<import("effect/Schema").String>;
346
+ readonly size: import("effect/Schema").optional<import("effect/Schema").Finite>;
347
+ readonly url: import("effect/Schema").optional<import("effect/Schema").String>;
348
+ }>>;
349
+ }>;
270
350
  'task.created': import("effect/Schema").Struct<{
271
351
  readonly callId: import("effect/Schema").String;
272
352
  readonly board: import("effect/Schema").String;
@@ -1,6 +1,6 @@
1
1
  import { Context, Effect, Layer } from 'effect';
2
2
  import type { DefinitionStore } from '@swirls/looms/core';
3
- import { BoardError, type BoardResolver, type MessageBoard } from './board';
3
+ import { BoardError, type BoardCaller, type BoardResolver, type MessageBoard } from './board';
4
4
  import type { SwarmDefinition } from './definitions';
5
5
  export type SwarmDefinitionStore = DefinitionStore<SwarmDefinition>;
6
6
  declare const SwarmDefinitionsTag_base: Context.ServiceClass<SwarmDefinitionsTag, "looms/SwarmDefinitions", SwarmDefinitionStore>;
@@ -17,7 +17,7 @@ export declare class BoardFaultError extends BoardFaultError_base<{
17
17
  }
18
18
  export interface Boards {
19
19
  /** Run one call against a board. Refusals fail with `BoardError`, faults with `BoardFaultError`. */
20
- readonly call: <A>(boardId: string, run: (board: MessageBoard) => Promise<A>) => Effect.Effect<A, BoardError | BoardFaultError>;
20
+ readonly call: <A>(boardId: string, caller: BoardCaller, run: (board: MessageBoard) => Promise<A>) => Effect.Effect<A, BoardError | BoardFaultError>;
21
21
  }
22
22
  declare const BoardsTag_base: Context.ServiceClass<BoardsTag, "looms/SwarmBoards", Boards>;
23
23
  export declare class BoardsTag extends BoardsTag_base {
@@ -18,8 +18,11 @@ function fault(cause) {
18
18
  }
19
19
  export function makeBoards(resolve) {
20
20
  return {
21
- call: (boardId, run) => Effect.tryPromise({
22
- try: () => Promise.resolve(resolve(boardId)).then(run),
21
+ call: (boardId, caller, run) => Effect.tryPromise({
22
+ // Inside the promise so a resolver that throws is a refusal or a fault, not a defect.
23
+ try: () => Promise.resolve()
24
+ .then(() => resolve(boardId, caller))
25
+ .then(run),
23
26
  catch: fault,
24
27
  }),
25
28
  };
@@ -1,5 +1,5 @@
1
1
  import type { Schema } from 'effect';
2
- import { type RuntimeEffect } from '@swirls/looms/core';
2
+ import { type LoomsIds, type RuntimeEffect } from '@swirls/looms/core';
3
3
  import { type CreateTaskArgsSchema, type PostArgsSchema } from './effects';
4
4
  /** Which board a step writes to and the name it writes under. */
5
5
  export interface BoardBinding {
@@ -10,6 +10,6 @@ export interface BoardBinding {
10
10
  * Post to a board from a thread or workflow node, and park until the board has answered.
11
11
  * The node resumes with the posted message, or fails if the board refused the post.
12
12
  */
13
- export declare function post(binding: BoardBinding, args: Schema.Schema.Type<typeof PostArgsSchema>): RuntimeEffect[];
13
+ export declare function post(binding: BoardBinding, args: Schema.Schema.Type<typeof PostArgsSchema>, ids?: LoomsIds): RuntimeEffect[];
14
14
  /** Create a board task from a thread or workflow node, and park until the board has answered. */
15
- export declare function createTask(binding: BoardBinding, args: Schema.Schema.Type<typeof CreateTaskArgsSchema>): RuntimeEffect[];
15
+ export declare function createTask(binding: BoardBinding, args: Schema.Schema.Type<typeof CreateTaskArgsSchema>, ids?: LoomsIds): RuntimeEffect[];
@@ -1,11 +1,11 @@
1
- import { createWaitId, invoke, wait } from '@swirls/looms/core';
1
+ import { invoke, typeIds, wait } from '@swirls/looms/core';
2
2
  import { createTaskEffect, postEffect, } from './effects.js';
3
3
  /**
4
4
  * Post to a board from a thread or workflow node, and park until the board has answered.
5
5
  * The node resumes with the posted message, or fails if the board refused the post.
6
6
  */
7
- export function post(binding, args) {
8
- const callId = createWaitId('swarm');
7
+ export function post(binding, args, ids = typeIds) {
8
+ const callId = ids.create('wait');
9
9
  return [
10
10
  invoke(postEffect, { callId, ...binding, ...args }, `post_${callId}`),
11
11
  wait({
@@ -15,8 +15,8 @@ export function post(binding, args) {
15
15
  ];
16
16
  }
17
17
  /** Create a board task from a thread or workflow node, and park until the board has answered. */
18
- export function createTask(binding, args) {
19
- const callId = createWaitId('swarm');
18
+ export function createTask(binding, args, ids = typeIds) {
19
+ const callId = ids.create('wait');
20
20
  return [
21
21
  invoke(createTaskEffect, { callId, ...binding, ...args }, `task_${callId}`),
22
22
  wait({
@@ -24,6 +24,8 @@ export interface SwarmState {
24
24
  readonly cursor: string | null;
25
25
  /** Completed pulls. Names the next pull, so each pull effect has its own tag. */
26
26
  readonly pulls: number;
27
+ /** Pulls that have failed in a row since the last one that worked. */
28
+ readonly pullFailures: number;
27
29
  /** Read the board as soon as possible. */
28
30
  readonly pullNow: boolean;
29
31
  /** Otherwise read it at this time, or not until nudged when null. */
@@ -38,4 +40,6 @@ export interface SwarmState {
38
40
  }
39
41
  /** Whether a board event should wake `member`. A member is never woken by its own writes. */
40
42
  export declare function concerns(member: SwarmMember, event: BoardEvent): boolean;
43
+ /** The longest a swarm waits before it tries an unreachable board again. */
44
+ export declare const MAX_PULL_BACKOFF_MS: number;
41
45
  export declare function createSwarmThread(definitions: DefinitionStore<SwarmDefinition>): ThreadDefinition<SwarmState>;
@@ -86,6 +86,12 @@ function cutBatches(state) {
86
86
  function requestPull(state) {
87
87
  return state.pullNow ? { ...state, dirty: true } : { ...state, pullNow: true };
88
88
  }
89
+ /** The longest a swarm waits before it tries an unreachable board again. */
90
+ export const MAX_PULL_BACKOFF_MS = 15 * 60_000;
91
+ const PULL_BACKOFF_MS = 30_000;
92
+ function pullTag(state, when) {
93
+ return `pull:${state.pulls}:${state.pullFailures}:${when}`;
94
+ }
89
95
  function activationTag(member, activation) {
90
96
  return `activate:${member}:${activation}`;
91
97
  }
@@ -104,6 +110,7 @@ export function createSwarmThread(definitions) {
104
110
  board: config.board ?? ctx.runId,
105
111
  cursor: config.after ?? null,
106
112
  pulls: 0,
113
+ pullFailures: 0,
107
114
  pullNow: true,
108
115
  nextPullAt: null,
109
116
  dirty: false,
@@ -125,6 +132,7 @@ export function createSwarmThread(definitions) {
125
132
  ...state,
126
133
  cursor: event.payload.cursor,
127
134
  pulls: state.pulls + 1,
135
+ pullFailures: 0,
128
136
  pullNow: state.dirty || event.payload.more,
129
137
  nextPullAt: event.payload.nextPullAt,
130
138
  dirty: false,
@@ -166,6 +174,20 @@ export function createSwarmThread(definitions) {
166
174
  }
167
175
  case 'runtime.effect.failed': {
168
176
  const tag = event.effectId ? parseEffectId(event.effectId)?.tag : undefined;
177
+ if (event.threadId === ctx.threadId && tag?.startsWith(pullTag(state, ''))) {
178
+ // A board that refuses the swarm will refuse it again, so the swarm fails where
179
+ // someone will see it. An unreachable board is tried again, less and less often.
180
+ if (event.payload.nonRetryable) {
181
+ return { ...state, invalid: event.payload.error || 'the board refused the swarm' };
182
+ }
183
+ const backoff = Math.min(PULL_BACKOFF_MS * 2 ** state.pullFailures, MAX_PULL_BACKOFF_MS);
184
+ return {
185
+ ...state,
186
+ pullFailures: state.pullFailures + 1,
187
+ pullNow: false,
188
+ nextPullAt: event.ts + backoff,
189
+ };
190
+ }
169
191
  const entry = Object.entries(state.members).find(([name, member]) => member.batch !== null && tag === activationTag(name, member.activations));
170
192
  if (!entry || event.threadId !== ctx.threadId) {
171
193
  return state;
@@ -220,10 +242,10 @@ export function createSwarmThread(definitions) {
220
242
  const effects = [];
221
243
  const pull = { ...definition, board: state.board, after: state.cursor, pull: state.pulls };
222
244
  if (state.pullNow) {
223
- effects.push(invoke(pullEffect, pull, `pull:${state.pulls}:now`));
245
+ effects.push(invoke(pullEffect, pull, pullTag(state, 'now')));
224
246
  }
225
247
  else if (state.nextPullAt !== null) {
226
- effects.push(invoke(pullEffect, pull, `pull:${state.pulls}:${state.nextPullAt}`, {
248
+ effects.push(invoke(pullEffect, pull, pullTag(state, String(state.nextPullAt)), {
227
249
  at: state.nextPullAt,
228
250
  }));
229
251
  }
@@ -1,11 +1,11 @@
1
1
  import { type EffectsTool } from '@swirls/looms/agent';
2
- import { type JsonValue } from '@swirls/looms/core';
2
+ import { type JsonValue, type LoomsIds } from '@swirls/looms/core';
3
3
  import type { BoardBinding } from './steps';
4
4
  /**
5
5
  * The board as agent tools, writing as `binding.as` on `binding.board`. Build a fresh set for
6
6
  * each turn (`resolveTools` does), because every tool carries the id of the one call it serves.
7
7
  */
8
- export declare function boardTools(binding: BoardBinding): EffectsTool[];
8
+ export declare function boardTools(binding: BoardBinding, ids?: LoomsIds): EffectsTool[];
9
9
  /**
10
10
  * The board tools of the swarm member a run was started for, or none when the run was not
11
11
  * started by a swarm. Pass it as an agent's `resolveTools`:
@@ -16,4 +16,5 @@ export declare function boardTools(binding: BoardBinding): EffectsTool[];
16
16
  */
17
17
  export declare function memberTools(ctx: {
18
18
  readonly input: JsonValue;
19
+ readonly ids?: LoomsIds;
19
20
  }): EffectsTool[];
@@ -1,14 +1,14 @@
1
1
  import { Predicate } from 'effect';
2
2
  import { asEffectsTool } from '@swirls/looms/agent';
3
- import { createWaitId, invoke } from '@swirls/looms/core';
3
+ import { invoke, typeIds } from '@swirls/looms/core';
4
4
  import { readActivation } from './definitions.js';
5
- import { ClaimTaskArgsSchema, CreateTaskArgsSchema, LabelArgsSchema, ListTasksArgsSchema, PostArgsSchema, ReadArgsSchema, ReplyArgsSchema, SettleTaskArgsSchema, claimTaskEffect, createTaskEffect, labelEffect, listTasksEffect, postEffect, readEffect, replyEffect, settleTaskEffect, } from './effects.js';
5
+ import { ClaimTaskArgsSchema, CreateTaskArgsSchema, LabelArgsSchema, ListTasksArgsSchema, PostArgsSchema, ReadArgsSchema, ReplyArgsSchema, SettleTaskArgsSchema, ViewArgsSchema, claimTaskEffect, createTaskEffect, labelEffect, listTasksEffect, postEffect, readEffect, replyEffect, settleTaskEffect, viewEffect, } from './effects.js';
6
6
  /**
7
7
  * A tool whose call becomes one board effect. The agent waits for the event that carries the
8
8
  * same `callId`: the board's answer, or `swarm.call.rejected` when the board refused.
9
9
  */
10
- function boardTool(binding, def) {
11
- const callId = createWaitId('swarm');
10
+ function boardTool(binding, def, ids) {
11
+ const callId = ids.create('wait');
12
12
  return asEffectsTool({
13
13
  name: def.name,
14
14
  description: def.description,
@@ -24,64 +24,71 @@ function boardTool(binding, def) {
24
24
  * The board as agent tools, writing as `binding.as` on `binding.board`. Build a fresh set for
25
25
  * each turn (`resolveTools` does), because every tool carries the id of the one call it serves.
26
26
  */
27
- export function boardTools(binding) {
27
+ export function boardTools(binding, ids = typeIds) {
28
28
  return [
29
29
  boardTool(binding, {
30
30
  name: 'board_read',
31
- description: 'Read messages from the board, oldest first. Pass `thread` with a message id to read a whole thread, `channel` to read a channel ("support") or a subtree ("support/"), or `mentionsMe` for messages that mention you. Without `after` you get the latest `limit` matches.',
31
+ description: 'Read messages from the board, oldest first. Pass `thread` with a message id to read a whole thread, `channel` to read a channel ("support") or a subtree ("support/"), or `mentionsMe` for messages that mention you. Without `after` you get the latest `limit` matches. A message lists its files under `attachments`. To look at them, call board_view.',
32
32
  input: ReadArgsSchema,
33
33
  effect: readEffect,
34
34
  answer: 'swarm.messages.read',
35
- }),
35
+ }, ids),
36
+ boardTool(binding, {
37
+ name: 'board_view',
38
+ description: 'Look at the files attached to one message or task, such as a screenshot or a recording. The files are shown to you with the result.',
39
+ input: ViewArgsSchema,
40
+ effect: viewEffect,
41
+ answer: 'swarm.attachments.viewed',
42
+ }, ids),
36
43
  boardTool(binding, {
37
44
  name: 'board_post',
38
- description: 'Start a new thread in a channel. Mention a member with @name to wake them. To continue an existing thread, use board_reply.',
45
+ description: 'Start a new thread in a channel. Mention a member with @name to wake them. To continue an existing thread, use board_reply. Pass `attachments` to share files: each is a reference `{ id, mediaType, name }` copied from a message or from the tool that produced the file.',
39
46
  input: PostArgsSchema,
40
47
  effect: postEffect,
41
48
  answer: 'swarm.message.posted',
42
- }),
49
+ }, ids),
43
50
  boardTool(binding, {
44
51
  name: 'board_reply',
45
- description: 'Reply in the thread of message `to`. Use it to answer, report progress on a task, or mention a member with @name.',
52
+ description: 'Reply in the thread of message `to`. Use it to answer, report progress on a task, or mention a member with @name. Pass `attachments` to share files.',
46
53
  input: ReplyArgsSchema,
47
54
  effect: replyEffect,
48
55
  answer: 'swarm.message.posted',
49
- }),
56
+ }, ids),
50
57
  boardTool(binding, {
51
58
  name: 'board_label',
52
59
  description: 'Set or unset key=value labels on a message to track state. Pass `if` to change them only when the labels still have the values you expect (null means unset).',
53
60
  input: LabelArgsSchema,
54
61
  effect: labelEffect,
55
62
  answer: 'swarm.message.labeled',
56
- }),
63
+ }, ids),
57
64
  boardTool(binding, {
58
65
  name: 'board_create_task',
59
- description: 'Put a unit of work on the board for a member to claim. Pass `for` to assign it to one member, and `needs` with task ids that must complete first. The text is the instructions for whoever claims it.',
66
+ description: 'Put a unit of work on the board for a member to claim. Pass `for` to assign it to one member, and `needs` with task ids that must complete first. The text is the instructions for whoever claims it. Pass `attachments` for files the work needs.',
60
67
  input: CreateTaskArgsSchema,
61
68
  effect: createTaskEffect,
62
69
  answer: 'swarm.task.created',
63
- }),
70
+ }, ids),
64
71
  boardTool(binding, {
65
72
  name: 'board_list_tasks',
66
73
  description: 'List tasks, oldest first, filtered by channel, state, or assignee.',
67
74
  input: ListTasksArgsSchema,
68
75
  effect: listTasksEffect,
69
76
  answer: 'swarm.tasks.listed',
70
- }),
77
+ }, ids),
71
78
  boardTool(binding, {
72
79
  name: 'board_claim_task',
73
80
  description: 'Claim a task before working on it, so no other member does the same work. Pass `task` to claim one task, or `channel` to take the oldest ready task you may work on. The result has a `token`: keep it for board_settle_task. `claim` is null when nothing is ready.',
74
81
  input: ClaimTaskArgsSchema,
75
82
  effect: claimTaskEffect,
76
83
  answer: 'swarm.task.claimed',
77
- }),
84
+ }, ids),
78
85
  boardTool(binding, {
79
86
  name: 'board_settle_task',
80
87
  description: 'Finish a task you claimed, using the token from board_claim_task. Outcome "completed" with the result in `note`, "failed" with the reason, "blocked" with what it is waiting on, or "released" to return it to the queue untouched.',
81
88
  input: SettleTaskArgsSchema,
82
89
  effect: settleTaskEffect,
83
90
  answer: 'swarm.task.settled',
84
- }),
91
+ }, ids),
85
92
  ];
86
93
  }
87
94
  /**
@@ -95,6 +102,6 @@ export function boardTools(binding) {
95
102
  export function memberTools(ctx) {
96
103
  const activation = readActivation(ctx.input);
97
104
  return activation
98
- ? boardTools({ board: activation.swarm.board, as: activation.swarm.member })
105
+ ? boardTools({ board: activation.swarm.board, as: activation.swarm.member }, ctx.ids)
99
106
  : [];
100
107
  }
@@ -1,5 +1,5 @@
1
1
  import { Clock, Data, Effect, Predicate, Schema } from 'effect';
2
- import { createThreadId, createWaitId, DEFAULT_DEFINITION_VERSION, } from '@swirls/looms/core';
2
+ import { createWaitId, DEFAULT_DEFINITION_VERSION, } from '@swirls/looms/core';
3
3
  import { NodeStateSchema, nodeResultBuilders, } from './definitions.js';
4
4
  import { WorkflowDefinitionsTag } from './definitions-store.js';
5
5
  import { assertJsonWithinLimit, DEFAULT_CHECKPOINT_SIZE_LIMIT_BYTES, DEFAULT_GRAPH_CONCURRENCY, getReadyNodeIds, getSkippableNodeIds, getUnusedFallbackIds, iterationChildWorkflowId, nodeChildWorkflowId, nodeResults, nodeRunKey, } from './graph.js';
@@ -158,6 +158,7 @@ export const runNodeEffect = workflowModule.effect({
158
158
  : undefined;
159
159
  const run = {
160
160
  threadId: ctx.threadId,
161
+ ids: ctx.ids,
161
162
  nodeId: input.nodeId,
162
163
  attempt: input.attempt,
163
164
  iteration: input.iteration,
@@ -207,7 +208,7 @@ function runNode(definition, run) {
207
208
  }
208
209
  function nodeResultEvents(definition, nodeType, run, result) {
209
210
  return Effect.gen(function* () {
210
- const { threadId, nodeId, attempt } = run;
211
+ const { threadId, ids, nodeId, attempt } = run;
211
212
  const outputLabel = `Node ${nodeId} output`;
212
213
  switch (result.type) {
213
214
  case 'value':
@@ -227,8 +228,8 @@ function nodeResultEvents(definition, nodeType, run, result) {
227
228
  case 'spawn': {
228
229
  const childThreadId = result.childThreadId ??
229
230
  (nodeType === 'workflow'
230
- ? nodeChildWorkflowId(threadId, nodeId, attempt)
231
- : createThreadId());
231
+ ? nodeChildWorkflowId(threadId, nodeId, attempt, ids)
232
+ : ids.create('thread'));
232
233
  return spawnRequested(definition, run, childThreadId, result, result.input);
233
234
  }
234
235
  case 'sleep': {
@@ -283,7 +284,7 @@ function nodeResultEvents(definition, nodeType, run, result) {
283
284
  if (index >= result.maxIterations) {
284
285
  return finished(run, run.iteration?.previous ?? null, null);
285
286
  }
286
- const childThreadId = iterationChildWorkflowId(threadId, nodeId, 'loop', attempt, index);
287
+ const childThreadId = iterationChildWorkflowId(threadId, nodeId, 'loop', attempt, index, ids);
287
288
  return spawnRequested(definition, run, childThreadId, result, result.input, index);
288
289
  }
289
290
  default: {
@@ -1,5 +1,5 @@
1
1
  import { Effect } from 'effect';
2
- import { type JsonValue } from '@swirls/looms/core';
2
+ import { type JsonValue, type LoomsIds } from '@swirls/looms/core';
3
3
  import type { NodeState, WorkflowDefinition, WorkflowNodeDefinition } from './definitions';
4
4
  export declare const MIN_GRAPH_CONCURRENCY = 1;
5
5
  export declare const MAX_GRAPH_CONCURRENCY = 64;
@@ -34,8 +34,8 @@ export interface WorkflowEdge {
34
34
  export type GraphNodeStatuses = Record<string, NodeState['status']>;
35
35
  /** Distinguishes each attempt, and each loop iteration within it, of one node. */
36
36
  export declare function nodeRunKey(nodeId: string, attempt: number, iteration?: number): string;
37
- export declare function iterationChildWorkflowId(parentThreadId: string, nodeId: string, kind: 'map' | 'loop', attempt: number, index: number): string;
38
- export declare function nodeChildWorkflowId(parentThreadId: string, nodeId: string, attempt: number): string;
37
+ export declare function iterationChildWorkflowId(parentThreadId: string, nodeId: string, kind: 'map' | 'loop', attempt: number, index: number, ids?: LoomsIds): string;
38
+ export declare function nodeChildWorkflowId(parentThreadId: string, nodeId: string, attempt: number, ids?: LoomsIds): string;
39
39
  export declare function assertJsonWithinLimit(value: JsonValue | Readonly<object>, label: string, limitBytes?: number): Effect.Effect<void, WorkflowValueTooLargeError>;
40
40
  export declare function resolveGraphConcurrency(value?: number): number;
41
41
  export declare function workflowEdges<TInput>(definition: WorkflowDefinition<string, TInput>): WorkflowEdge[];
@@ -1,5 +1,5 @@
1
1
  import { Data, Effect } from 'effect';
2
- import { deterministicThreadId, isJsonString, utf8JsonBytes } from '@swirls/looms/core';
2
+ import { isJsonString, typeIds, utf8JsonBytes } from '@swirls/looms/core';
3
3
  export const MIN_GRAPH_CONCURRENCY = 1;
4
4
  export const MAX_GRAPH_CONCURRENCY = 64;
5
5
  export const DEFAULT_GRAPH_CONCURRENCY = 8;
@@ -24,11 +24,11 @@ export class WorkflowValueTooLargeError extends Data.TaggedError('WorkflowValueT
24
24
  export function nodeRunKey(nodeId, attempt, iteration) {
25
25
  return iteration === undefined ? `${nodeId}_${attempt}` : `${nodeId}_${attempt}_${iteration}`;
26
26
  }
27
- export function iterationChildWorkflowId(parentThreadId, nodeId, kind, attempt, index) {
28
- return deterministicThreadId(parentThreadId, nodeId, kind, attempt, index);
27
+ export function iterationChildWorkflowId(parentThreadId, nodeId, kind, attempt, index, ids = typeIds) {
28
+ return ids.derive('thread', [parentThreadId, nodeId, kind, attempt, index]);
29
29
  }
30
- export function nodeChildWorkflowId(parentThreadId, nodeId, attempt) {
31
- return deterministicThreadId(parentThreadId, nodeId, 'workflow', attempt);
30
+ export function nodeChildWorkflowId(parentThreadId, nodeId, attempt, ids = typeIds) {
31
+ return ids.derive('thread', [parentThreadId, nodeId, 'workflow', attempt]);
32
32
  }
33
33
  export function assertJsonWithinLimit(value, label, limitBytes = DEFAULT_VALUE_SIZE_LIMIT_BYTES) {
34
34
  const actualBytes = utf8JsonBytes(value);
@@ -1089,7 +1089,10 @@ export declare function workflow(options?: WorkflowModuleOptions): import("@swir
1089
1089
  readonly idempotencyKey?: string | null | undefined;
1090
1090
  };
1091
1091
  }[];
1092
- readonly pendingCancels: string[];
1092
+ readonly pendingCancels: {
1093
+ readonly target: string;
1094
+ readonly execution: "run" | "thread";
1095
+ }[];
1093
1096
  readonly status: "completed" | "failed" | "running";
1094
1097
  readonly error: string | null;
1095
1098
  }>;
@@ -2078,7 +2081,10 @@ export declare function workflow(options?: WorkflowModuleOptions): import("@swir
2078
2081
  readonly idempotencyKey?: string | null | undefined;
2079
2082
  };
2080
2083
  }[];
2081
- readonly pendingCancels: string[];
2084
+ readonly pendingCancels: {
2085
+ readonly target: string;
2086
+ readonly execution: "run" | "thread";
2087
+ }[];
2082
2088
  readonly status: "completed" | "failed" | "running";
2083
2089
  readonly error: string | null;
2084
2090
  }>;