@swirls/looms 0.24.0 → 0.25.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 +16 -0
- 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/debugger/app/assets/index-GLtrulmj.js +23 -0
- package/dist/debugger/app/assets/{routes-Ch30UGik.js → routes-CECZJJob.js} +1 -1
- package/dist/debugger/app/index.html +1 -1
- package/dist/evaluator/effects.d.ts +33 -33
- package/dist/evaluator/module.d.ts +66 -66
- package/dist/schedule/module.d.ts +36 -36
- 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 +19 -0
- package/dist/swarm/tools.js +100 -0
- package/package.json +7 -2
- package/dist/debugger/app/assets/index-CxbIGkpY.js +0 -22
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
import { type DefinitionRef, type JsonValue } from '@swirls/looms/core';
|
|
3
|
+
export declare const DEFAULT_POLL_INTERVAL_MS = 60000;
|
|
4
|
+
export declare const SwarmStartSchema: Schema.Union<readonly [Schema.Struct<{
|
|
5
|
+
/** The board this swarm follows. Defaults to the swarm's run id. */
|
|
6
|
+
readonly board: Schema.optional<Schema.String>;
|
|
7
|
+
/** Start after this board cursor. Omit to read the board's log from the start. */
|
|
8
|
+
readonly after: Schema.optional<Schema.String>;
|
|
9
|
+
}>, Schema.Null]>;
|
|
10
|
+
export type SwarmStart = Schema.Schema.Type<typeof SwarmStartSchema>;
|
|
11
|
+
/** One agent in a swarm. Its name is how it signs board messages and how others `@mention` it. */
|
|
12
|
+
export interface SwarmMember {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** What the swarm starts when the board has something for this member. */
|
|
15
|
+
readonly agent: DefinitionRef;
|
|
16
|
+
/** Told to the other members so they know what to hand over. */
|
|
17
|
+
readonly description?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Channel filters the member listens to: `support` for one channel, `support/` for a subtree.
|
|
20
|
+
* The member is woken for every message there and for unassigned tasks that become ready
|
|
21
|
+
* there. Without channels it is woken only when mentioned or assigned a task.
|
|
22
|
+
*/
|
|
23
|
+
readonly channels?: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
export interface SwarmDefinition<TName extends string = string> extends DefinitionRef {
|
|
26
|
+
readonly kind: 'swarm';
|
|
27
|
+
readonly name: TName;
|
|
28
|
+
readonly version: string;
|
|
29
|
+
readonly description?: string;
|
|
30
|
+
readonly input: typeof SwarmStartSchema;
|
|
31
|
+
readonly members: readonly SwarmMember[];
|
|
32
|
+
/** Added to every member's activation prompt. */
|
|
33
|
+
readonly instructions?: string;
|
|
34
|
+
/**
|
|
35
|
+
* How often the swarm reads the board when nothing nudges it. `null` reads only on a nudge and
|
|
36
|
+
* after a member finishes, for hosts that signal `nudge()` on every outside write.
|
|
37
|
+
*/
|
|
38
|
+
readonly pollIntervalMs: number | null;
|
|
39
|
+
}
|
|
40
|
+
export type AnySwarmDefinition = SwarmDefinition;
|
|
41
|
+
export declare function defineSwarm<TName extends string>(def: {
|
|
42
|
+
readonly name: TName;
|
|
43
|
+
readonly version?: string;
|
|
44
|
+
readonly description?: string;
|
|
45
|
+
readonly members: readonly SwarmMember[];
|
|
46
|
+
readonly instructions?: string;
|
|
47
|
+
readonly pollIntervalMs?: number | null;
|
|
48
|
+
}): SwarmDefinition<TName>;
|
|
49
|
+
/** The input of a member's run: who it is, which board it is on, and why it was woken. */
|
|
50
|
+
export declare const ActivationSchema: Schema.Struct<{
|
|
51
|
+
/** Becomes the agent's first user message. */
|
|
52
|
+
readonly prompt: Schema.String;
|
|
53
|
+
readonly swarm: Schema.Struct<{
|
|
54
|
+
readonly name: Schema.String;
|
|
55
|
+
readonly runId: Schema.String;
|
|
56
|
+
readonly board: Schema.String;
|
|
57
|
+
readonly member: Schema.String;
|
|
58
|
+
readonly activation: Schema.Finite;
|
|
59
|
+
}>;
|
|
60
|
+
readonly events: Schema.$Array<Schema.Struct<{
|
|
61
|
+
readonly cursor: Schema.String;
|
|
62
|
+
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">]>;
|
|
63
|
+
readonly at: Schema.Finite;
|
|
64
|
+
readonly by: Schema.String;
|
|
65
|
+
readonly message: Schema.optional<Schema.Struct<{
|
|
66
|
+
readonly id: Schema.String;
|
|
67
|
+
readonly channel: Schema.String;
|
|
68
|
+
readonly text: Schema.String;
|
|
69
|
+
readonly by: Schema.String;
|
|
70
|
+
readonly re: Schema.NullOr<Schema.String>;
|
|
71
|
+
readonly mentions: Schema.$Array<Schema.String>;
|
|
72
|
+
readonly labels: Schema.$Record<Schema.String, Schema.String>;
|
|
73
|
+
readonly createdAt: Schema.Finite;
|
|
74
|
+
}>>;
|
|
75
|
+
readonly task: Schema.optional<Schema.Struct<{
|
|
76
|
+
readonly id: Schema.String;
|
|
77
|
+
readonly channel: Schema.String;
|
|
78
|
+
readonly text: Schema.String;
|
|
79
|
+
readonly by: Schema.String;
|
|
80
|
+
readonly for: Schema.NullOr<Schema.String>;
|
|
81
|
+
readonly needs: Schema.$Array<Schema.String>;
|
|
82
|
+
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">]>;
|
|
83
|
+
readonly holder: Schema.NullOr<Schema.String>;
|
|
84
|
+
readonly epoch: Schema.Finite;
|
|
85
|
+
readonly leaseUntil: Schema.NullOr<Schema.Finite>;
|
|
86
|
+
readonly note: Schema.NullOr<Schema.String>;
|
|
87
|
+
readonly createdAt: Schema.Finite;
|
|
88
|
+
readonly updatedAt: Schema.Finite;
|
|
89
|
+
}>>;
|
|
90
|
+
}>>;
|
|
91
|
+
}>;
|
|
92
|
+
export type Activation = Schema.Schema.Type<typeof ActivationSchema>;
|
|
93
|
+
/** The activation in a member run's input, or null when the run was not started by a swarm. */
|
|
94
|
+
export declare function readActivation(input: JsonValue | undefined): Activation | null;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Option, Schema } from 'effect';
|
|
2
|
+
import { DEFAULT_DEFINITION_VERSION, createKind, } from '@swirls/looms/core';
|
|
3
|
+
import { BoardEventSchema } from './board.js';
|
|
4
|
+
const swarmKind = createKind('swarm');
|
|
5
|
+
const MEMBER_NAME = /^[a-z][a-z0-9-]*$/;
|
|
6
|
+
export const DEFAULT_POLL_INTERVAL_MS = 60_000;
|
|
7
|
+
export const SwarmStartSchema = Schema.Union([
|
|
8
|
+
Schema.Struct({
|
|
9
|
+
/** The board this swarm follows. Defaults to the swarm's run id. */
|
|
10
|
+
board: Schema.optional(Schema.String),
|
|
11
|
+
/** Start after this board cursor. Omit to read the board's log from the start. */
|
|
12
|
+
after: Schema.optional(Schema.String),
|
|
13
|
+
}),
|
|
14
|
+
Schema.Null,
|
|
15
|
+
]);
|
|
16
|
+
export function defineSwarm(def) {
|
|
17
|
+
const names = new Set();
|
|
18
|
+
for (const member of def.members) {
|
|
19
|
+
if (!MEMBER_NAME.test(member.name)) {
|
|
20
|
+
throw new Error(`Swarm member name "${member.name}" must be lowercase letters, digits and hyphens`);
|
|
21
|
+
}
|
|
22
|
+
if (names.has(member.name)) {
|
|
23
|
+
throw new Error(`Swarm "${def.name}" lists member "${member.name}" twice`);
|
|
24
|
+
}
|
|
25
|
+
names.add(member.name);
|
|
26
|
+
}
|
|
27
|
+
return swarmKind.define({
|
|
28
|
+
...def,
|
|
29
|
+
version: def.version ?? DEFAULT_DEFINITION_VERSION,
|
|
30
|
+
input: SwarmStartSchema,
|
|
31
|
+
pollIntervalMs: def.pollIntervalMs === undefined ? DEFAULT_POLL_INTERVAL_MS : def.pollIntervalMs,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** The input of a member's run: who it is, which board it is on, and why it was woken. */
|
|
35
|
+
export const ActivationSchema = Schema.Struct({
|
|
36
|
+
/** Becomes the agent's first user message. */
|
|
37
|
+
prompt: Schema.String,
|
|
38
|
+
swarm: Schema.Struct({
|
|
39
|
+
name: Schema.String,
|
|
40
|
+
runId: Schema.String,
|
|
41
|
+
board: Schema.String,
|
|
42
|
+
member: Schema.String,
|
|
43
|
+
activation: Schema.Finite,
|
|
44
|
+
}),
|
|
45
|
+
events: Schema.Array(BoardEventSchema),
|
|
46
|
+
});
|
|
47
|
+
const decodeActivation = Schema.decodeUnknownOption(ActivationSchema);
|
|
48
|
+
/** The activation in a member run's input, or null when the run was not started by a swarm. */
|
|
49
|
+
export function readActivation(input) {
|
|
50
|
+
return input === undefined ? null : Option.getOrNull(decodeActivation(input));
|
|
51
|
+
}
|