@swirls/looms 0.24.0 → 0.26.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.
- package/CHANGELOG.md +34 -0
- package/dist/actor/cell.d.ts +3 -1
- package/dist/actor/cell.js +1 -0
- package/dist/actor/host.d.ts +3 -1
- package/dist/actor/host.js +2 -1
- package/dist/actor/routing.d.ts +6 -3
- package/dist/actor/routing.js +6 -4
- package/dist/agent/agent-session.js +2 -2
- package/dist/agent/debugger/agent-chat.js +4 -3
- package/dist/agent/definitions.d.ts +3 -1
- package/dist/agent/effects.js +5 -3
- package/dist/agent/module.d.ts +730 -730
- package/dist/agent/threads.js +4 -1
- package/dist/approval/effects.d.ts +26 -26
- package/dist/approval/module.d.ts +52 -52
- package/dist/approval/steps.d.ts +3 -1
- package/dist/approval/steps.js +2 -2
- package/dist/client/client.d.ts +7 -1
- package/dist/client/client.js +5 -2
- package/dist/cloudflare/durable-object.d.ts +3 -1
- package/dist/cloudflare/durable-object.js +1 -0
- package/dist/cloudflare/queue-consumer.d.ts +3 -1
- package/dist/cloudflare/queue-consumer.js +2 -1
- package/dist/cloudflare/router.d.ts +3 -1
- package/dist/cloudflare/router.js +1 -1
- package/dist/core/compose.d.ts +8 -2
- package/dist/core/compose.js +6 -3
- package/dist/core/effects.d.ts +3 -0
- package/dist/core/envelope.d.ts +2 -0
- package/dist/core/envelope.js +2 -2
- package/dist/core/event-codec.d.ts +4 -3
- package/dist/core/event-codec.js +7 -7
- package/dist/core/fold.d.ts +3 -0
- package/dist/core/fold.js +3 -1
- package/dist/core/fork.js +1 -1
- package/dist/core/ids.d.ts +70 -5
- package/dist/core/ids.js +164 -41
- package/dist/core/snapshots.d.ts +2 -0
- package/dist/core/snapshots.js +2 -1
- package/dist/core/thread.d.ts +8 -0
- package/dist/debugger/app/assets/index-D_FKxPrN.js +23 -0
- package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/context.d.ts +4 -1
- package/dist/debugger/context.js +4 -4
- package/dist/debugger/shell/debugger-shell.d.ts +4 -1
- package/dist/debugger/shell/debugger-shell.js +2 -2
- package/dist/debugger/shell/use-start-run.d.ts +1 -1
- package/dist/debugger/shell/use-start-run.js +1 -2
- package/dist/evaluator/steps.d.ts +2 -2
- package/dist/evaluator/steps.js +3 -3
- package/dist/react/react.d.ts +8 -3
- package/dist/react/react.js +15 -2
- package/dist/react/store.d.ts +5 -1
- package/dist/react/store.js +5 -4
- package/dist/runtime/cancellation.js +5 -5
- package/dist/runtime/deadline-transition-planner.d.ts +1 -0
- package/dist/runtime/deadline-transition-planner.js +8 -7
- package/dist/runtime/effect-admission.js +1 -0
- package/dist/runtime/helpers.d.ts +6 -5
- package/dist/runtime/helpers.js +8 -8
- package/dist/runtime/local-effect-executor.js +15 -10
- package/dist/runtime/looms.d.ts +6 -1
- package/dist/runtime/looms.js +4 -1
- package/dist/runtime/retry-policy.d.ts +2 -1
- package/dist/runtime/retry-policy.js +2 -1
- package/dist/runtime/runtime.js +10 -10
- package/dist/runtime/server/routes/runs.js +5 -5
- package/dist/runtime/types.d.ts +7 -1
- package/dist/runtime/wake-engine.js +10 -9
- package/dist/runtime/worker-callbacks.js +9 -8
- package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
- package/dist/runtime/worker-effect-dispatch.js +7 -5
- package/dist/swarm/board.d.ts +292 -0
- package/dist/swarm/board.js +123 -0
- package/dist/swarm/conformance.d.ts +15 -0
- package/dist/swarm/conformance.js +225 -0
- package/dist/swarm/definitions.d.ts +94 -0
- package/dist/swarm/definitions.js +51 -0
- package/dist/swarm/effects.d.ts +4720 -0
- package/dist/swarm/effects.js +296 -0
- package/dist/swarm/ensure.d.ts +16 -0
- package/dist/swarm/ensure.js +24 -0
- package/dist/swarm/events.d.ts +240 -0
- package/dist/swarm/events.js +49 -0
- package/dist/swarm/index.d.ts +15 -0
- package/dist/swarm/index.js +15 -0
- package/dist/swarm/memory-board.d.ts +10 -0
- package/dist/swarm/memory-board.js +344 -0
- package/dist/swarm/module.d.ts +9593 -0
- package/dist/swarm/module.js +94 -0
- package/dist/swarm/projections.d.ts +60 -0
- package/dist/swarm/projections.js +102 -0
- package/dist/swarm/prompt.d.ts +4 -0
- package/dist/swarm/prompt.js +35 -0
- package/dist/swarm/scope.d.ts +357 -0
- package/dist/swarm/scope.js +8 -0
- package/dist/swarm/services.d.ts +29 -0
- package/dist/swarm/services.js +40 -0
- package/dist/swarm/signals.d.ts +14 -0
- package/dist/swarm/signals.js +15 -0
- package/dist/swarm/steps.d.ts +15 -0
- package/dist/swarm/steps.js +27 -0
- package/dist/swarm/threads.d.ts +41 -0
- package/dist/swarm/threads.js +244 -0
- package/dist/swarm/tools.d.ts +20 -0
- package/dist/swarm/tools.js +100 -0
- package/dist/workflow/effects.js +6 -5
- package/dist/workflow/graph.d.ts +3 -3
- package/dist/workflow/graph.js +5 -5
- package/dist/workflow/module.d.ts +8 -2
- package/dist/workflow/threads.d.ts +14 -2
- package/dist/workflow/threads.js +54 -44
- package/package.json +7 -2
- package/dist/debugger/app/assets/index-CxbIGkpY.js +0 -22
- package/dist/debugger/app/assets/routes-Ch30UGik.js +0 -47
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* A message board is the only thing swarm members share. It holds channels of threaded messages,
|
|
4
|
+
* tasks that members claim under a lease, and one ordered event log that a swarm follows.
|
|
5
|
+
*
|
|
6
|
+
* Implement {@link MessageBoard} over any store and check it with `boardConformance`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const BoardLabelsSchema: Schema.$Record<Schema.String, Schema.String>;
|
|
9
|
+
export type BoardLabels = Schema.Schema.Type<typeof BoardLabelsSchema>;
|
|
10
|
+
export declare const BoardMessageSchema: Schema.Struct<{
|
|
11
|
+
readonly id: Schema.String;
|
|
12
|
+
/** A path such as `support` or `support/billing`. The first post creates it. */
|
|
13
|
+
readonly channel: Schema.String;
|
|
14
|
+
readonly text: Schema.String;
|
|
15
|
+
/** The member or person who wrote it. */
|
|
16
|
+
readonly by: Schema.String;
|
|
17
|
+
/** The root message of the thread, or null when this message is a root. */
|
|
18
|
+
readonly re: Schema.NullOr<Schema.String>;
|
|
19
|
+
/** Names written as `@name` in `text`. */
|
|
20
|
+
readonly mentions: Schema.$Array<Schema.String>;
|
|
21
|
+
/** `key=value` state. Keys under `board.` belong to the board. */
|
|
22
|
+
readonly labels: Schema.$Record<Schema.String, Schema.String>;
|
|
23
|
+
readonly createdAt: Schema.Finite;
|
|
24
|
+
}>;
|
|
25
|
+
export type BoardMessage = Schema.Schema.Type<typeof BoardMessageSchema>;
|
|
26
|
+
export declare const TaskStateSchema: Schema.Union<readonly [Schema.Literal<"waiting">, Schema.Literal<"ready">, Schema.Literal<"claimed">, Schema.Literal<"blocked">, Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"canceled">]>;
|
|
27
|
+
export type TaskState = Schema.Schema.Type<typeof TaskStateSchema>;
|
|
28
|
+
/** A task is a thread with state the board keeps. Its id is the id of its root message. */
|
|
29
|
+
export declare const BoardTaskSchema: Schema.Struct<{
|
|
30
|
+
readonly id: Schema.String;
|
|
31
|
+
readonly channel: Schema.String;
|
|
32
|
+
readonly text: Schema.String;
|
|
33
|
+
readonly by: Schema.String;
|
|
34
|
+
/** The only member who may claim it, or null when anyone may. */
|
|
35
|
+
readonly for: Schema.NullOr<Schema.String>;
|
|
36
|
+
/** Tasks that must complete before this one is ready. */
|
|
37
|
+
readonly needs: Schema.$Array<Schema.String>;
|
|
38
|
+
readonly state: Schema.Union<readonly [Schema.Literal<"waiting">, Schema.Literal<"ready">, Schema.Literal<"claimed">, Schema.Literal<"blocked">, Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"canceled">]>;
|
|
39
|
+
/** Who holds the current claim. */
|
|
40
|
+
readonly holder: Schema.NullOr<Schema.String>;
|
|
41
|
+
/** Counts claims. A claim from an earlier epoch can no longer change the task. */
|
|
42
|
+
readonly epoch: Schema.Finite;
|
|
43
|
+
readonly leaseUntil: Schema.NullOr<Schema.Finite>;
|
|
44
|
+
/** The result, failure reason, or blocker written when the task last changed state. */
|
|
45
|
+
readonly note: Schema.NullOr<Schema.String>;
|
|
46
|
+
readonly createdAt: Schema.Finite;
|
|
47
|
+
readonly updatedAt: Schema.Finite;
|
|
48
|
+
}>;
|
|
49
|
+
export type BoardTask = Schema.Schema.Type<typeof BoardTaskSchema>;
|
|
50
|
+
export declare const TaskClaimSchema: Schema.Struct<{
|
|
51
|
+
readonly task: Schema.Struct<{
|
|
52
|
+
readonly id: Schema.String;
|
|
53
|
+
readonly channel: Schema.String;
|
|
54
|
+
readonly text: Schema.String;
|
|
55
|
+
readonly by: Schema.String;
|
|
56
|
+
/** The only member who may claim it, or null when anyone may. */
|
|
57
|
+
readonly for: Schema.NullOr<Schema.String>;
|
|
58
|
+
/** Tasks that must complete before this one is ready. */
|
|
59
|
+
readonly needs: Schema.$Array<Schema.String>;
|
|
60
|
+
readonly state: Schema.Union<readonly [Schema.Literal<"waiting">, Schema.Literal<"ready">, Schema.Literal<"claimed">, Schema.Literal<"blocked">, Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"canceled">]>;
|
|
61
|
+
/** Who holds the current claim. */
|
|
62
|
+
readonly holder: Schema.NullOr<Schema.String>;
|
|
63
|
+
/** Counts claims. A claim from an earlier epoch can no longer change the task. */
|
|
64
|
+
readonly epoch: Schema.Finite;
|
|
65
|
+
readonly leaseUntil: Schema.NullOr<Schema.Finite>;
|
|
66
|
+
/** The result, failure reason, or blocker written when the task last changed state. */
|
|
67
|
+
readonly note: Schema.NullOr<Schema.String>;
|
|
68
|
+
readonly createdAt: Schema.Finite;
|
|
69
|
+
readonly updatedAt: Schema.Finite;
|
|
70
|
+
}>;
|
|
71
|
+
/** Proves the holder to `renew` and `settle`. It stops working when the claim is lost. */
|
|
72
|
+
readonly token: Schema.String;
|
|
73
|
+
readonly until: Schema.Finite;
|
|
74
|
+
}>;
|
|
75
|
+
export type TaskClaim = Schema.Schema.Type<typeof TaskClaimSchema>;
|
|
76
|
+
export declare const BoardEventTypeSchema: Schema.Union<readonly [Schema.Literal<"message.posted">, Schema.Literal<"message.labeled">, Schema.Literal<"task.created">, Schema.Literal<"task.ready">, Schema.Literal<"task.claimed">, Schema.Literal<"task.blocked">, Schema.Literal<"task.completed">, Schema.Literal<"task.failed">, Schema.Literal<"task.canceled">, Schema.Literal<"task.reopened">]>;
|
|
77
|
+
export type BoardEventType = Schema.Schema.Type<typeof BoardEventTypeSchema>;
|
|
78
|
+
export declare const BoardEventSchema: Schema.Struct<{
|
|
79
|
+
/** Opaque position in the board's log. Pass it back unchanged; do not compare or parse it. */
|
|
80
|
+
readonly cursor: Schema.String;
|
|
81
|
+
readonly type: Schema.Union<readonly [Schema.Literal<"message.posted">, Schema.Literal<"message.labeled">, Schema.Literal<"task.created">, Schema.Literal<"task.ready">, Schema.Literal<"task.claimed">, Schema.Literal<"task.blocked">, Schema.Literal<"task.completed">, Schema.Literal<"task.failed">, Schema.Literal<"task.canceled">, Schema.Literal<"task.reopened">]>;
|
|
82
|
+
readonly at: Schema.Finite;
|
|
83
|
+
/** Who caused the event. `BOARD_ACTOR` when the board did, such as an expired lease. */
|
|
84
|
+
readonly by: Schema.String;
|
|
85
|
+
/** The message as it was after the event. Present on message events and on `task.created`. */
|
|
86
|
+
readonly message: Schema.optional<Schema.Struct<{
|
|
87
|
+
readonly id: Schema.String;
|
|
88
|
+
/** A path such as `support` or `support/billing`. The first post creates it. */
|
|
89
|
+
readonly channel: Schema.String;
|
|
90
|
+
readonly text: Schema.String;
|
|
91
|
+
/** The member or person who wrote it. */
|
|
92
|
+
readonly by: Schema.String;
|
|
93
|
+
/** The root message of the thread, or null when this message is a root. */
|
|
94
|
+
readonly re: Schema.NullOr<Schema.String>;
|
|
95
|
+
/** Names written as `@name` in `text`. */
|
|
96
|
+
readonly mentions: Schema.$Array<Schema.String>;
|
|
97
|
+
/** `key=value` state. Keys under `board.` belong to the board. */
|
|
98
|
+
readonly labels: Schema.$Record<Schema.String, Schema.String>;
|
|
99
|
+
readonly createdAt: Schema.Finite;
|
|
100
|
+
}>>;
|
|
101
|
+
/** The task as it was after the event. Present on every task event. */
|
|
102
|
+
readonly task: Schema.optional<Schema.Struct<{
|
|
103
|
+
readonly id: Schema.String;
|
|
104
|
+
readonly channel: Schema.String;
|
|
105
|
+
readonly text: Schema.String;
|
|
106
|
+
readonly by: Schema.String;
|
|
107
|
+
/** The only member who may claim it, or null when anyone may. */
|
|
108
|
+
readonly for: Schema.NullOr<Schema.String>;
|
|
109
|
+
/** Tasks that must complete before this one is ready. */
|
|
110
|
+
readonly needs: Schema.$Array<Schema.String>;
|
|
111
|
+
readonly state: Schema.Union<readonly [Schema.Literal<"waiting">, Schema.Literal<"ready">, Schema.Literal<"claimed">, Schema.Literal<"blocked">, Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"canceled">]>;
|
|
112
|
+
/** Who holds the current claim. */
|
|
113
|
+
readonly holder: Schema.NullOr<Schema.String>;
|
|
114
|
+
/** Counts claims. A claim from an earlier epoch can no longer change the task. */
|
|
115
|
+
readonly epoch: Schema.Finite;
|
|
116
|
+
readonly leaseUntil: Schema.NullOr<Schema.Finite>;
|
|
117
|
+
/** The result, failure reason, or blocker written when the task last changed state. */
|
|
118
|
+
readonly note: Schema.NullOr<Schema.String>;
|
|
119
|
+
readonly createdAt: Schema.Finite;
|
|
120
|
+
readonly updatedAt: Schema.Finite;
|
|
121
|
+
}>>;
|
|
122
|
+
}>;
|
|
123
|
+
export type BoardEvent = Schema.Schema.Type<typeof BoardEventSchema>;
|
|
124
|
+
/** The `by` of events the board causes itself. */
|
|
125
|
+
export declare const BOARD_ACTOR = "board";
|
|
126
|
+
/** The label a task's root message carries. Its value is the task state. */
|
|
127
|
+
export declare const TASK_LABEL = "board.task";
|
|
128
|
+
/** Label keys with this prefix are written only by the board. */
|
|
129
|
+
export declare const RESERVED_LABEL_PREFIX = "board.";
|
|
130
|
+
export declare const DEFAULT_LEASE_MS: number;
|
|
131
|
+
export declare const MAX_LEASE_MS: number;
|
|
132
|
+
export type BoardErrorCode = 'invalid_input' | 'message_not_found' | 'task_not_found' | 'duplicate_key' | 'reserved_label' | 'label_conflict' | 'invalid_needs' | 'task_claimed' | 'task_assigned' | 'task_not_ready' | 'task_finished' | 'claim_lost';
|
|
133
|
+
declare const BoardError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
134
|
+
readonly _tag: "BoardError";
|
|
135
|
+
} & Readonly<A>;
|
|
136
|
+
/**
|
|
137
|
+
* A request the board refused. Retrying the same request fails the same way, so the swarm module
|
|
138
|
+
* reports it to the calling agent and does not retry. Throw any other error for a fault that a
|
|
139
|
+
* retry might clear.
|
|
140
|
+
*/
|
|
141
|
+
export declare class BoardError extends BoardError_base<{
|
|
142
|
+
readonly code: BoardErrorCode;
|
|
143
|
+
readonly message: string;
|
|
144
|
+
}> {
|
|
145
|
+
}
|
|
146
|
+
export interface PostInput {
|
|
147
|
+
readonly by: string;
|
|
148
|
+
readonly channel: string;
|
|
149
|
+
readonly text: string;
|
|
150
|
+
readonly labels?: BoardLabels;
|
|
151
|
+
/**
|
|
152
|
+
* Idempotency key. Posting the same key again returns the first message and writes nothing.
|
|
153
|
+
* The same key in a different channel or thread fails with `duplicate_key`.
|
|
154
|
+
*/
|
|
155
|
+
readonly key?: string;
|
|
156
|
+
}
|
|
157
|
+
export interface ReplyInput {
|
|
158
|
+
readonly by: string;
|
|
159
|
+
/** Any message of the thread. The reply attaches to that thread's root. */
|
|
160
|
+
readonly to: string;
|
|
161
|
+
readonly text: string;
|
|
162
|
+
readonly labels?: BoardLabels;
|
|
163
|
+
readonly key?: string;
|
|
164
|
+
}
|
|
165
|
+
export interface LabelInput {
|
|
166
|
+
readonly by: string;
|
|
167
|
+
readonly message: string;
|
|
168
|
+
readonly set?: BoardLabels;
|
|
169
|
+
readonly unset?: readonly string[];
|
|
170
|
+
/**
|
|
171
|
+
* Compare-and-set. Each key must hold the given value, or be absent for `null`, or the call
|
|
172
|
+
* fails with `label_conflict` and changes nothing.
|
|
173
|
+
*/
|
|
174
|
+
readonly if?: {
|
|
175
|
+
readonly [key: string]: string | null;
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
export interface MessageQuery {
|
|
179
|
+
/** `support` matches that channel. `support/` matches it and every channel below it. */
|
|
180
|
+
readonly channel?: string;
|
|
181
|
+
/** Only the thread with this root: the root itself and its replies. */
|
|
182
|
+
readonly thread?: string;
|
|
183
|
+
readonly by?: string;
|
|
184
|
+
readonly mentions?: string;
|
|
185
|
+
/** Every pair must be on the message. */
|
|
186
|
+
readonly labels?: BoardLabels;
|
|
187
|
+
/** Only messages after the one with this id. */
|
|
188
|
+
readonly after?: string;
|
|
189
|
+
/** At most this many. Without `after` these are the latest matches. Default 50. */
|
|
190
|
+
readonly limit?: number;
|
|
191
|
+
}
|
|
192
|
+
export interface CreateTaskInput {
|
|
193
|
+
readonly by: string;
|
|
194
|
+
readonly channel: string;
|
|
195
|
+
readonly text: string;
|
|
196
|
+
readonly for?: string;
|
|
197
|
+
readonly needs?: readonly string[];
|
|
198
|
+
readonly labels?: BoardLabels;
|
|
199
|
+
readonly key?: string;
|
|
200
|
+
}
|
|
201
|
+
export interface TaskQuery {
|
|
202
|
+
readonly channel?: string;
|
|
203
|
+
readonly state?: TaskState;
|
|
204
|
+
readonly for?: string;
|
|
205
|
+
readonly holder?: string;
|
|
206
|
+
readonly limit?: number;
|
|
207
|
+
}
|
|
208
|
+
export interface ClaimInput {
|
|
209
|
+
readonly by: string;
|
|
210
|
+
/** Claim this task. Fails when it is held, assigned to someone else, or not ready. */
|
|
211
|
+
readonly task?: string;
|
|
212
|
+
/**
|
|
213
|
+
* Without `task`: claim the oldest ready task in this channel filter that is assigned to the
|
|
214
|
+
* caller, then the oldest unassigned one. Resolves to null when there is none.
|
|
215
|
+
*/
|
|
216
|
+
readonly channel?: string;
|
|
217
|
+
/** Default {@link DEFAULT_LEASE_MS}, at most {@link MAX_LEASE_MS}. */
|
|
218
|
+
readonly leaseMs?: number;
|
|
219
|
+
}
|
|
220
|
+
export interface RenewInput {
|
|
221
|
+
readonly task: string;
|
|
222
|
+
readonly token: string;
|
|
223
|
+
readonly leaseMs?: number;
|
|
224
|
+
}
|
|
225
|
+
/** `released` returns the task to ready without finishing it. */
|
|
226
|
+
export type SettleOutcome = 'completed' | 'failed' | 'blocked' | 'released';
|
|
227
|
+
export interface SettleInput {
|
|
228
|
+
readonly task: string;
|
|
229
|
+
readonly token: string;
|
|
230
|
+
readonly outcome: SettleOutcome;
|
|
231
|
+
readonly note?: string;
|
|
232
|
+
}
|
|
233
|
+
export interface TaskChangeInput {
|
|
234
|
+
readonly by: string;
|
|
235
|
+
readonly task: string;
|
|
236
|
+
readonly note?: string;
|
|
237
|
+
}
|
|
238
|
+
export interface EventQuery {
|
|
239
|
+
/** Only events after this cursor. Omit to read from the start of the log. */
|
|
240
|
+
readonly after?: string;
|
|
241
|
+
/** Default 100. */
|
|
242
|
+
readonly limit?: number;
|
|
243
|
+
}
|
|
244
|
+
export interface EventPage {
|
|
245
|
+
readonly events: readonly BoardEvent[];
|
|
246
|
+
/** The cursor of the last event returned, or the `after` that was passed when there were none. */
|
|
247
|
+
readonly cursor: string | null;
|
|
248
|
+
/** Whether more events were already in the log past this page. */
|
|
249
|
+
readonly more: boolean;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The storage contract behind a swarm. One instance is one board.
|
|
253
|
+
*
|
|
254
|
+
* Rules every implementation keeps:
|
|
255
|
+
*
|
|
256
|
+
* - Every write appends to one totally ordered event log, in the order the writes took effect.
|
|
257
|
+
* - `post`, `reply` and `createTask` are idempotent on `key`.
|
|
258
|
+
* - At most one claim holds a task. `claim` on a held task fails with `task_claimed`.
|
|
259
|
+
* - A claim ends when it is settled, when its lease passes, or when the task is canceled. After
|
|
260
|
+
* that its token fails with `claim_lost` and changes nothing, so a member that stalled cannot
|
|
261
|
+
* settle a task another member has since claimed.
|
|
262
|
+
* - A task whose lease has passed is ready again, and the log gains a `task.ready` event no later
|
|
263
|
+
* than the next call that reads tasks or events.
|
|
264
|
+
* - Completing a task makes ready every waiting task whose `needs` are now all completed.
|
|
265
|
+
* - Refusals reject with {@link BoardError}. Any other rejection is treated as a fault and retried.
|
|
266
|
+
*/
|
|
267
|
+
export interface MessageBoard {
|
|
268
|
+
post(input: PostInput): Promise<BoardMessage>;
|
|
269
|
+
reply(input: ReplyInput): Promise<BoardMessage>;
|
|
270
|
+
label(input: LabelInput): Promise<BoardMessage>;
|
|
271
|
+
/** Oldest first. */
|
|
272
|
+
messages(query?: MessageQuery): Promise<readonly BoardMessage[]>;
|
|
273
|
+
createTask(input: CreateTaskInput): Promise<BoardTask>;
|
|
274
|
+
/** Oldest first. */
|
|
275
|
+
tasks(query?: TaskQuery): Promise<readonly BoardTask[]>;
|
|
276
|
+
claim(input: ClaimInput): Promise<TaskClaim | null>;
|
|
277
|
+
renew(input: RenewInput): Promise<TaskClaim>;
|
|
278
|
+
settle(input: SettleInput): Promise<BoardTask>;
|
|
279
|
+
/** Return a blocked, failed or canceled task to ready, or to waiting if it has unmet needs. */
|
|
280
|
+
reopen(input: TaskChangeInput): Promise<BoardTask>;
|
|
281
|
+
cancel(input: TaskChangeInput): Promise<BoardTask>;
|
|
282
|
+
events(query?: EventQuery): Promise<EventPage>;
|
|
283
|
+
}
|
|
284
|
+
/** Looks a board up by id, so one host can serve a board per tenant. */
|
|
285
|
+
export type BoardResolver = (boardId: string) => MessageBoard | Promise<MessageBoard>;
|
|
286
|
+
/** The distinct `@name` handles in `text`, lowercased, in the order they first appear. */
|
|
287
|
+
export declare function parseMentions(text: string): string[];
|
|
288
|
+
/** Whether `channel` is selected by `filter`. A filter ending in `/` selects a whole subtree. */
|
|
289
|
+
export declare function channelMatches(filter: string, channel: string): boolean;
|
|
290
|
+
/** Whether a task is finished and will not change state again without `reopen`. */
|
|
291
|
+
export declare function isFinishedTask(state: TaskState): boolean;
|
|
292
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Data, Schema } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* A message board is the only thing swarm members share. It holds channels of threaded messages,
|
|
4
|
+
* tasks that members claim under a lease, and one ordered event log that a swarm follows.
|
|
5
|
+
*
|
|
6
|
+
* Implement {@link MessageBoard} over any store and check it with `boardConformance`.
|
|
7
|
+
*/
|
|
8
|
+
export const BoardLabelsSchema = Schema.Record(Schema.String, Schema.String);
|
|
9
|
+
export const BoardMessageSchema = Schema.Struct({
|
|
10
|
+
id: Schema.String,
|
|
11
|
+
/** A path such as `support` or `support/billing`. The first post creates it. */
|
|
12
|
+
channel: Schema.String,
|
|
13
|
+
text: Schema.String,
|
|
14
|
+
/** The member or person who wrote it. */
|
|
15
|
+
by: Schema.String,
|
|
16
|
+
/** The root message of the thread, or null when this message is a root. */
|
|
17
|
+
re: Schema.NullOr(Schema.String),
|
|
18
|
+
/** Names written as `@name` in `text`. */
|
|
19
|
+
mentions: Schema.Array(Schema.String),
|
|
20
|
+
/** `key=value` state. Keys under `board.` belong to the board. */
|
|
21
|
+
labels: BoardLabelsSchema,
|
|
22
|
+
createdAt: Schema.Finite,
|
|
23
|
+
});
|
|
24
|
+
export const TaskStateSchema = Schema.Union([
|
|
25
|
+
/** Has prerequisites that are not completed. */
|
|
26
|
+
Schema.Literal('waiting'),
|
|
27
|
+
Schema.Literal('ready'),
|
|
28
|
+
Schema.Literal('claimed'),
|
|
29
|
+
Schema.Literal('blocked'),
|
|
30
|
+
Schema.Literal('completed'),
|
|
31
|
+
Schema.Literal('failed'),
|
|
32
|
+
Schema.Literal('canceled'),
|
|
33
|
+
]);
|
|
34
|
+
/** A task is a thread with state the board keeps. Its id is the id of its root message. */
|
|
35
|
+
export const BoardTaskSchema = Schema.Struct({
|
|
36
|
+
id: Schema.String,
|
|
37
|
+
channel: Schema.String,
|
|
38
|
+
text: Schema.String,
|
|
39
|
+
by: Schema.String,
|
|
40
|
+
/** The only member who may claim it, or null when anyone may. */
|
|
41
|
+
for: Schema.NullOr(Schema.String),
|
|
42
|
+
/** Tasks that must complete before this one is ready. */
|
|
43
|
+
needs: Schema.Array(Schema.String),
|
|
44
|
+
state: TaskStateSchema,
|
|
45
|
+
/** Who holds the current claim. */
|
|
46
|
+
holder: Schema.NullOr(Schema.String),
|
|
47
|
+
/** Counts claims. A claim from an earlier epoch can no longer change the task. */
|
|
48
|
+
epoch: Schema.Finite,
|
|
49
|
+
leaseUntil: Schema.NullOr(Schema.Finite),
|
|
50
|
+
/** The result, failure reason, or blocker written when the task last changed state. */
|
|
51
|
+
note: Schema.NullOr(Schema.String),
|
|
52
|
+
createdAt: Schema.Finite,
|
|
53
|
+
updatedAt: Schema.Finite,
|
|
54
|
+
});
|
|
55
|
+
export const TaskClaimSchema = Schema.Struct({
|
|
56
|
+
task: BoardTaskSchema,
|
|
57
|
+
/** Proves the holder to `renew` and `settle`. It stops working when the claim is lost. */
|
|
58
|
+
token: Schema.String,
|
|
59
|
+
until: Schema.Finite,
|
|
60
|
+
});
|
|
61
|
+
export const BoardEventTypeSchema = Schema.Union([
|
|
62
|
+
Schema.Literal('message.posted'),
|
|
63
|
+
Schema.Literal('message.labeled'),
|
|
64
|
+
Schema.Literal('task.created'),
|
|
65
|
+
/** A waiting task's prerequisites completed, or a claim was released or expired. */
|
|
66
|
+
Schema.Literal('task.ready'),
|
|
67
|
+
Schema.Literal('task.claimed'),
|
|
68
|
+
Schema.Literal('task.blocked'),
|
|
69
|
+
Schema.Literal('task.completed'),
|
|
70
|
+
Schema.Literal('task.failed'),
|
|
71
|
+
Schema.Literal('task.canceled'),
|
|
72
|
+
Schema.Literal('task.reopened'),
|
|
73
|
+
]);
|
|
74
|
+
export const BoardEventSchema = Schema.Struct({
|
|
75
|
+
/** Opaque position in the board's log. Pass it back unchanged; do not compare or parse it. */
|
|
76
|
+
cursor: Schema.String,
|
|
77
|
+
type: BoardEventTypeSchema,
|
|
78
|
+
at: Schema.Finite,
|
|
79
|
+
/** Who caused the event. `BOARD_ACTOR` when the board did, such as an expired lease. */
|
|
80
|
+
by: Schema.String,
|
|
81
|
+
/** The message as it was after the event. Present on message events and on `task.created`. */
|
|
82
|
+
message: Schema.optional(BoardMessageSchema),
|
|
83
|
+
/** The task as it was after the event. Present on every task event. */
|
|
84
|
+
task: Schema.optional(BoardTaskSchema),
|
|
85
|
+
});
|
|
86
|
+
/** The `by` of events the board causes itself. */
|
|
87
|
+
export const BOARD_ACTOR = 'board';
|
|
88
|
+
/** The label a task's root message carries. Its value is the task state. */
|
|
89
|
+
export const TASK_LABEL = 'board.task';
|
|
90
|
+
/** Label keys with this prefix are written only by the board. */
|
|
91
|
+
export const RESERVED_LABEL_PREFIX = 'board.';
|
|
92
|
+
export const DEFAULT_LEASE_MS = 15 * 60_000;
|
|
93
|
+
export const MAX_LEASE_MS = 60 * 60_000;
|
|
94
|
+
/**
|
|
95
|
+
* A request the board refused. Retrying the same request fails the same way, so the swarm module
|
|
96
|
+
* reports it to the calling agent and does not retry. Throw any other error for a fault that a
|
|
97
|
+
* retry might clear.
|
|
98
|
+
*/
|
|
99
|
+
export class BoardError extends Data.TaggedError('BoardError') {
|
|
100
|
+
}
|
|
101
|
+
const MENTION = /(?:^|[^\w@])@([a-z][a-z0-9-]*)/gi;
|
|
102
|
+
/** The distinct `@name` handles in `text`, lowercased, in the order they first appear. */
|
|
103
|
+
export function parseMentions(text) {
|
|
104
|
+
const names = new Set();
|
|
105
|
+
for (const match of text.matchAll(MENTION)) {
|
|
106
|
+
const name = match[1];
|
|
107
|
+
if (name !== undefined) {
|
|
108
|
+
names.add(name.toLowerCase());
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return [...names];
|
|
112
|
+
}
|
|
113
|
+
/** Whether `channel` is selected by `filter`. A filter ending in `/` selects a whole subtree. */
|
|
114
|
+
export function channelMatches(filter, channel) {
|
|
115
|
+
if (!filter.endsWith('/')) {
|
|
116
|
+
return filter === channel;
|
|
117
|
+
}
|
|
118
|
+
return channel === filter.slice(0, -1) || channel.startsWith(filter);
|
|
119
|
+
}
|
|
120
|
+
/** Whether a task is finished and will not change state again without `reopen`. */
|
|
121
|
+
export function isFinishedTask(state) {
|
|
122
|
+
return state === 'completed' || state === 'failed' || state === 'canceled';
|
|
123
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type MessageBoard } from './board';
|
|
2
|
+
/** A clock the suite advances to expire leases. A board under test must read time from it. */
|
|
3
|
+
export interface ConformanceClock {
|
|
4
|
+
readonly now: () => number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Runs the {@link MessageBoard} contract against a board implementation and returns one line per
|
|
8
|
+
* failure. `make` is called for a fresh, empty board before each check and must read time from
|
|
9
|
+
* the clock it is given.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* expect(await boardConformance((clock) => createMemoryBoard(clock))).toEqual([])
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function boardConformance(make: (clock: ConformanceClock) => MessageBoard | Promise<MessageBoard>): Promise<string[]>;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { BOARD_ACTOR, BoardError, TASK_LABEL, } from './board.js';
|
|
2
|
+
class Mismatch extends Error {
|
|
3
|
+
}
|
|
4
|
+
function expect(condition, message) {
|
|
5
|
+
if (!condition) {
|
|
6
|
+
throw new Mismatch(message);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
async function refused(attempt, code) {
|
|
10
|
+
try {
|
|
11
|
+
await attempt;
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (!(error instanceof BoardError)) {
|
|
15
|
+
throw new Mismatch(`expected a BoardError with code "${code}", got ${String(error)}`);
|
|
16
|
+
}
|
|
17
|
+
expect(error.code === code, `expected code "${code}", got "${error.code}"`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
throw new Mismatch(`expected a BoardError with code "${code}", but the call succeeded`);
|
|
21
|
+
}
|
|
22
|
+
const checks = {
|
|
23
|
+
'a post is read back with its mentions': async (board) => {
|
|
24
|
+
const posted = await board.post({ by: 'ada', channel: 'ops', text: 'ping @Bo and @cy, @bo' });
|
|
25
|
+
const read = await board.messages({ channel: 'ops' });
|
|
26
|
+
expect(read.length === 1 && read[0]?.id === posted.id, 'the post was not read back');
|
|
27
|
+
expect(posted.re === null, 'a post must be a thread root');
|
|
28
|
+
expect(posted.mentions.join(',') === 'bo,cy', `mentions were "${posted.mentions.join(',')}"`);
|
|
29
|
+
},
|
|
30
|
+
'a post is idempotent on its key': async (board) => {
|
|
31
|
+
const first = await board.post({ by: 'ada', channel: 'ops', text: 'once', key: 'key-1' });
|
|
32
|
+
const again = await board.post({ by: 'ada', channel: 'ops', text: 'twice', key: 'key-1' });
|
|
33
|
+
const page = await board.events();
|
|
34
|
+
expect(again.id === first.id && again.text === 'once', 'the retry did not return the original');
|
|
35
|
+
expect(page.events.length === 1, 'the retry wrote a second event');
|
|
36
|
+
await refused(board.post({ by: 'ada', channel: 'dev', text: 'x', key: 'key-1' }), 'duplicate_key');
|
|
37
|
+
},
|
|
38
|
+
'a reply attaches to the root of its thread': async (board) => {
|
|
39
|
+
const root = await board.post({ by: 'ada', channel: 'ops', text: 'root' });
|
|
40
|
+
const first = await board.reply({ by: 'bo', to: root.id, text: 'one' });
|
|
41
|
+
const second = await board.reply({ by: 'cy', to: first.id, text: 'two' });
|
|
42
|
+
const thread = await board.messages({ thread: root.id });
|
|
43
|
+
expect(second.re === root.id, 'a reply to a reply must attach to the root');
|
|
44
|
+
expect(second.channel === 'ops', 'a reply must take the channel of its thread');
|
|
45
|
+
expect(thread.map((item) => item.text).join(',') === 'root,one,two', 'thread order is wrong');
|
|
46
|
+
await refused(board.reply({ by: 'bo', to: 'missing', text: 'x' }), 'message_not_found');
|
|
47
|
+
},
|
|
48
|
+
'a channel filter ending in a slash selects the subtree': async (board) => {
|
|
49
|
+
await board.post({ by: 'ada', channel: 'ops', text: 'a' });
|
|
50
|
+
await board.post({ by: 'ada', channel: 'ops/db', text: 'b' });
|
|
51
|
+
await board.post({ by: 'ada', channel: 'opsx', text: 'c' });
|
|
52
|
+
const exact = await board.messages({ channel: 'ops' });
|
|
53
|
+
const subtree = await board.messages({ channel: 'ops/' });
|
|
54
|
+
expect(exact.length === 1, 'an exact filter matched other channels');
|
|
55
|
+
expect(subtree.map((item) => item.text).join(',') === 'a,b', 'the subtree filter is wrong');
|
|
56
|
+
},
|
|
57
|
+
'messages page forward from a message id': async (board) => {
|
|
58
|
+
const first = await board.post({ by: 'ada', channel: 'ops', text: '1' });
|
|
59
|
+
await board.post({ by: 'ada', channel: 'ops', text: '2' });
|
|
60
|
+
await board.post({ by: 'ada', channel: 'ops', text: '3' });
|
|
61
|
+
const latest = await board.messages({ channel: 'ops', limit: 2 });
|
|
62
|
+
const after = await board.messages({ channel: 'ops', after: first.id, limit: 1 });
|
|
63
|
+
expect(latest.map((item) => item.text).join(',') === '2,3', 'limit must keep the latest');
|
|
64
|
+
expect(after.map((item) => item.text).join(',') === '2', 'after must page forward');
|
|
65
|
+
},
|
|
66
|
+
'labels compare and set': async (board) => {
|
|
67
|
+
const message = await board.post({ by: 'ada', channel: 'ops', text: 'x' });
|
|
68
|
+
await board.label({ by: 'ada', message: message.id, set: { state: 'open' } });
|
|
69
|
+
await refused(board.label({ by: 'bo', message: message.id, set: { state: 'mine' }, if: { state: null } }), 'label_conflict');
|
|
70
|
+
const moved = await board.label({
|
|
71
|
+
by: 'bo',
|
|
72
|
+
message: message.id,
|
|
73
|
+
set: { state: 'done' },
|
|
74
|
+
if: { state: 'open' },
|
|
75
|
+
});
|
|
76
|
+
const cleared = await board.label({ by: 'bo', message: message.id, unset: ['state'] });
|
|
77
|
+
expect(moved.labels.state === 'done', 'a matching precondition must apply the change');
|
|
78
|
+
expect(cleared.labels.state === undefined, 'unset must remove the label');
|
|
79
|
+
await refused(board.label({ by: 'bo', message: message.id, set: { [TASK_LABEL]: 'ready' } }), 'reserved_label');
|
|
80
|
+
},
|
|
81
|
+
'the event log is ordered and resumes from a cursor': async (board) => {
|
|
82
|
+
await board.post({ by: 'ada', channel: 'ops', text: '1' });
|
|
83
|
+
await board.post({ by: 'ada', channel: 'ops', text: '2' });
|
|
84
|
+
await board.post({ by: 'ada', channel: 'ops', text: '3' });
|
|
85
|
+
const first = await board.events({ limit: 2 });
|
|
86
|
+
const rest = await board.events({ after: first.cursor ?? undefined });
|
|
87
|
+
const idle = await board.events({ after: rest.cursor ?? undefined });
|
|
88
|
+
expect(first.events.length === 2 && first.more, 'a short page must report more');
|
|
89
|
+
expect(rest.events.length === 1 && !rest.more, 'the second page must finish the log');
|
|
90
|
+
expect(rest.events[0]?.message?.text === '3', 'the cursor did not resume after the page');
|
|
91
|
+
expect(idle.events.length === 0, 'an idle read must return no events');
|
|
92
|
+
expect(idle.cursor === rest.cursor, 'an idle read must return the cursor it was given');
|
|
93
|
+
},
|
|
94
|
+
'a task is a thread that carries its state as a label': async (board) => {
|
|
95
|
+
const task = await board.createTask({ by: 'ada', channel: 'ops', text: 'rotate keys' });
|
|
96
|
+
const root = await board.messages({ thread: task.id });
|
|
97
|
+
const page = await board.events();
|
|
98
|
+
expect(task.state === 'ready', 'a task with no needs must start ready');
|
|
99
|
+
expect(root[0]?.labels[TASK_LABEL] === 'ready', 'the root message must carry the task label');
|
|
100
|
+
expect(page.events[0]?.type === 'task.created', 'creating a task must log task.created');
|
|
101
|
+
expect(page.events[0]?.message?.id === task.id, 'task.created must carry the root message');
|
|
102
|
+
},
|
|
103
|
+
'only one claim holds a task': async (board) => {
|
|
104
|
+
const task = await board.createTask({ by: 'ada', channel: 'ops', text: 'x' });
|
|
105
|
+
const claim = await board.claim({ by: 'bo', task: task.id });
|
|
106
|
+
expect(claim?.task.holder === 'bo', 'the claim must name its holder');
|
|
107
|
+
expect(claim?.task.state === 'claimed', 'a claimed task must be in state claimed');
|
|
108
|
+
await refused(board.claim({ by: 'cy', task: task.id }), 'task_claimed');
|
|
109
|
+
const queued = await board.claim({ by: 'cy', channel: 'ops' });
|
|
110
|
+
expect(queued === null, 'a held task must not be handed out from the queue');
|
|
111
|
+
},
|
|
112
|
+
'the queue prefers tasks assigned to the caller': async (board) => {
|
|
113
|
+
const open = await board.createTask({ by: 'ada', channel: 'ops', text: 'open' });
|
|
114
|
+
const theirs = await board.createTask({ by: 'ada', channel: 'ops', text: 'cy', for: 'cy' });
|
|
115
|
+
const mine = await board.createTask({ by: 'ada', channel: 'ops', text: 'bo', for: 'bo' });
|
|
116
|
+
const first = await board.claim({ by: 'bo', channel: 'ops' });
|
|
117
|
+
const second = await board.claim({ by: 'bo', channel: 'ops' });
|
|
118
|
+
const third = await board.claim({ by: 'bo', channel: 'ops' });
|
|
119
|
+
expect(first?.task.id === mine.id, 'an assigned task must be claimed first');
|
|
120
|
+
expect(second?.task.id === open.id, 'then the oldest unassigned task');
|
|
121
|
+
expect(third === null, 'a task assigned to someone else must not be handed out');
|
|
122
|
+
await refused(board.claim({ by: 'bo', task: theirs.id }), 'task_assigned');
|
|
123
|
+
},
|
|
124
|
+
'settling records the outcome and ends the claim': async (board) => {
|
|
125
|
+
const task = await board.createTask({ by: 'ada', channel: 'ops', text: 'x' });
|
|
126
|
+
const claim = await board.claim({ by: 'bo', task: task.id });
|
|
127
|
+
const token = claim?.token ?? '';
|
|
128
|
+
const done = await board.settle({ task: task.id, token, outcome: 'completed', note: 'ok' });
|
|
129
|
+
const root = await board.messages({ thread: task.id });
|
|
130
|
+
expect(done.state === 'completed' && done.note === 'ok', 'the outcome was not recorded');
|
|
131
|
+
expect(done.holder === null, 'a settled task must have no holder');
|
|
132
|
+
expect(root[0]?.labels[TASK_LABEL] === 'completed', 'the task label must follow the state');
|
|
133
|
+
await refused(board.settle({ task: task.id, token, outcome: 'failed' }), 'claim_lost');
|
|
134
|
+
await refused(board.claim({ by: 'cy', task: task.id }), 'task_finished');
|
|
135
|
+
},
|
|
136
|
+
'a released task is ready again and the old token is dead': async (board) => {
|
|
137
|
+
const task = await board.createTask({ by: 'ada', channel: 'ops', text: 'x' });
|
|
138
|
+
const claim = await board.claim({ by: 'bo', task: task.id });
|
|
139
|
+
const token = claim?.token ?? '';
|
|
140
|
+
await board.settle({ task: task.id, token, outcome: 'released' });
|
|
141
|
+
const again = await board.claim({ by: 'cy', task: task.id });
|
|
142
|
+
expect(again?.task.epoch === 2, 'each claim must raise the epoch');
|
|
143
|
+
await refused(board.settle({ task: task.id, token, outcome: 'completed' }), 'claim_lost');
|
|
144
|
+
},
|
|
145
|
+
'an expired lease returns the task to ready': async (board, advance) => {
|
|
146
|
+
const task = await board.createTask({ by: 'ada', channel: 'ops', text: 'x' });
|
|
147
|
+
const claim = await board.claim({ by: 'bo', task: task.id, leaseMs: 1_000 });
|
|
148
|
+
const token = claim?.token ?? '';
|
|
149
|
+
advance(500);
|
|
150
|
+
await board.renew({ task: task.id, token, leaseMs: 1_000 });
|
|
151
|
+
advance(800);
|
|
152
|
+
const held = await board.tasks({ state: 'claimed' });
|
|
153
|
+
expect(held.length === 1, 'a renewed lease must still hold the task');
|
|
154
|
+
advance(500);
|
|
155
|
+
const page = await board.events();
|
|
156
|
+
const expiry = page.events.at(-1);
|
|
157
|
+
expect(expiry?.type === 'task.ready', 'an expired lease must log task.ready');
|
|
158
|
+
expect(expiry?.by === BOARD_ACTOR, 'the board must be the actor of an expiry');
|
|
159
|
+
await refused(board.settle({ task: task.id, token, outcome: 'completed' }), 'claim_lost');
|
|
160
|
+
await refused(board.renew({ task: task.id, token }), 'claim_lost');
|
|
161
|
+
const next = await board.claim({ by: 'cy', channel: 'ops' });
|
|
162
|
+
expect(next?.task.id === task.id, 'an expired task must be claimable again');
|
|
163
|
+
},
|
|
164
|
+
'a task waits for its needs': async (board) => {
|
|
165
|
+
const first = await board.createTask({ by: 'ada', channel: 'ops', text: 'first' });
|
|
166
|
+
const second = await board.createTask({
|
|
167
|
+
by: 'ada',
|
|
168
|
+
channel: 'ops',
|
|
169
|
+
text: 'second',
|
|
170
|
+
needs: [first.id],
|
|
171
|
+
});
|
|
172
|
+
expect(second.state === 'waiting', 'a task with unmet needs must wait');
|
|
173
|
+
await refused(board.claim({ by: 'bo', task: second.id }), 'task_not_ready');
|
|
174
|
+
await refused(board.createTask({ by: 'ada', channel: 'ops', text: 'x', needs: ['missing'] }), 'invalid_needs');
|
|
175
|
+
const claim = await board.claim({ by: 'bo', task: first.id });
|
|
176
|
+
await board.settle({ task: first.id, token: claim?.token ?? '', outcome: 'completed' });
|
|
177
|
+
const ready = await board.tasks({ state: 'ready' });
|
|
178
|
+
const page = await board.events();
|
|
179
|
+
expect(ready.length === 1 && ready[0]?.id === second.id, 'the dependent did not become ready');
|
|
180
|
+
expect(page.events.at(-1)?.type === 'task.ready', 'the dependent must log task.ready');
|
|
181
|
+
},
|
|
182
|
+
'a failed need readies nothing, and reopen restores the task': async (board) => {
|
|
183
|
+
const first = await board.createTask({ by: 'ada', channel: 'ops', text: 'first' });
|
|
184
|
+
await board.createTask({ by: 'ada', channel: 'ops', text: 'second', needs: [first.id] });
|
|
185
|
+
const claim = await board.claim({ by: 'bo', task: first.id });
|
|
186
|
+
await board.settle({ task: first.id, token: claim?.token ?? '', outcome: 'failed', note: 'no' });
|
|
187
|
+
const waiting = await board.tasks({ state: 'waiting' });
|
|
188
|
+
const reopened = await board.reopen({ by: 'ada', task: first.id });
|
|
189
|
+
expect(waiting.length === 1, 'a failed need must leave its dependent waiting');
|
|
190
|
+
expect(reopened.state === 'ready', 'reopen must return a failed task to ready');
|
|
191
|
+
},
|
|
192
|
+
'cancel ends a claim': async (board) => {
|
|
193
|
+
const task = await board.createTask({ by: 'ada', channel: 'ops', text: 'x' });
|
|
194
|
+
const claim = await board.claim({ by: 'bo', task: task.id });
|
|
195
|
+
const canceled = await board.cancel({ by: 'ada', task: task.id, note: 'not needed' });
|
|
196
|
+
expect(canceled.state === 'canceled' && canceled.holder === null, 'cancel did not take effect');
|
|
197
|
+
await refused(board.settle({ task: task.id, token: claim?.token ?? '', outcome: 'completed' }), 'claim_lost');
|
|
198
|
+
await refused(board.cancel({ by: 'ada', task: task.id }), 'task_finished');
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Runs the {@link MessageBoard} contract against a board implementation and returns one line per
|
|
203
|
+
* failure. `make` is called for a fresh, empty board before each check and must read time from
|
|
204
|
+
* the clock it is given.
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* expect(await boardConformance((clock) => createMemoryBoard(clock))).toEqual([])
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export async function boardConformance(make) {
|
|
211
|
+
const failures = [];
|
|
212
|
+
for (const [name, check] of Object.entries(checks)) {
|
|
213
|
+
let time = 1_000_000;
|
|
214
|
+
try {
|
|
215
|
+
const board = await make({ now: () => time });
|
|
216
|
+
await check(board, (ms) => {
|
|
217
|
+
time += ms;
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
failures.push(`${name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return failures;
|
|
225
|
+
}
|