@assistant-ui/react-statewire 0.34.1 → 0.35.1
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/dist/StatewireThread.d.ts +10 -8
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +4 -2
- package/dist/StatewireThread.js.map +1 -1
- package/dist/runCommands.d.ts +2 -1
- package/dist/runCommands.d.ts.map +1 -1
- package/dist/runCommands.js +23 -39
- package/dist/runCommands.js.map +1 -1
- package/dist/runsOptimistic.d.ts.map +1 -1
- package/dist/runsOptimistic.js +15 -6
- package/dist/runsOptimistic.js.map +1 -1
- package/package.json +2 -2
- package/src/StatewireThread.drafts.test.tsx +1 -2
- package/src/StatewireThread.held.test.tsx +6 -5
- package/src/StatewireThread.runs.test.tsx +74 -8
- package/src/StatewireThread.test.tsx +31 -31
- package/src/StatewireThread.ts +24 -17
- package/src/localStorage.test.ts +1 -1
- package/src/runCommands.test.ts +60 -4
- package/src/runCommands.ts +44 -37
- package/src/runsOptimistic.test.ts +269 -28
- package/src/runsOptimistic.ts +25 -15
package/src/runCommands.test.ts
CHANGED
|
@@ -38,7 +38,7 @@ describe("createRunQueueAdapter", () => {
|
|
|
38
38
|
const steered = { id: "s1", parts, metadata: { source: "panel" } };
|
|
39
39
|
const sent: StatewireThread.RunCommand[] = [];
|
|
40
40
|
const queue = createRunQueueAdapter(
|
|
41
|
-
() => ({ messages: [], runId: "run-1" }),
|
|
41
|
+
() => ({ messages: [], runId: "run-1", running: true }),
|
|
42
42
|
[queued],
|
|
43
43
|
[steered],
|
|
44
44
|
(command) => sent.push(command),
|
|
@@ -56,19 +56,75 @@ describe("createRunQueueAdapter", () => {
|
|
|
56
56
|
});
|
|
57
57
|
expect(sent).toEqual([
|
|
58
58
|
{
|
|
59
|
-
type: "run/
|
|
59
|
+
type: "run/send",
|
|
60
60
|
runId: "run-1",
|
|
61
|
+
behavior: "queue",
|
|
61
62
|
message: message("q1", { source: { tab: 1 } }),
|
|
62
63
|
},
|
|
63
64
|
{
|
|
64
|
-
type: "run/
|
|
65
|
+
type: "run/send",
|
|
65
66
|
runId: "run-1",
|
|
67
|
+
behavior: "steer",
|
|
66
68
|
message: message("s1", { source: "panel" }),
|
|
67
69
|
},
|
|
68
70
|
]);
|
|
69
|
-
const edited = sent[0] as StatewireThread.
|
|
71
|
+
const edited = sent[0] as StatewireThread.RunSendCommand & {
|
|
70
72
|
message: StatewireThread.UserMessage;
|
|
71
73
|
};
|
|
72
74
|
expect(edited.message.metadata!["source"]).not.toBe(queued.metadata.source);
|
|
73
75
|
});
|
|
76
|
+
|
|
77
|
+
const adapter = (running: boolean) => {
|
|
78
|
+
const sent: StatewireThread.RunCommand[] = [];
|
|
79
|
+
const record = (command: StatewireThread.RunCommand) => sent.push(command);
|
|
80
|
+
const parts = [{ type: "text" as const, text: "x" }];
|
|
81
|
+
const queue = createRunQueueAdapter(
|
|
82
|
+
() => ({ messages: [], runId: "run-1", running }),
|
|
83
|
+
[{ id: "q1", parts }],
|
|
84
|
+
[{ id: "s1", parts }],
|
|
85
|
+
record,
|
|
86
|
+
record,
|
|
87
|
+
);
|
|
88
|
+
return { queue, sent };
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
it("sends the composer's default lane as auto and the other lane explicitly", () => {
|
|
92
|
+
const running = adapter(true);
|
|
93
|
+
running.queue.steer(append());
|
|
94
|
+
running.queue.enqueue(append());
|
|
95
|
+
const idle = adapter(false);
|
|
96
|
+
idle.queue.enqueue(append());
|
|
97
|
+
idle.queue.steer(append());
|
|
98
|
+
expect(
|
|
99
|
+
[...running.sent, ...idle.sent].map(
|
|
100
|
+
(c) => (c as StatewireThread.RunSendCommand).behavior,
|
|
101
|
+
),
|
|
102
|
+
).toEqual(["auto", "queue", "auto", "steer"]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("moves an item with the target lane's behavior, defaulting to its own lane", () => {
|
|
106
|
+
const { queue, sent } = adapter(true);
|
|
107
|
+
queue.move("q1", { lane: "steer" });
|
|
108
|
+
queue.move("s1", { lane: "queue", insertAfter: null });
|
|
109
|
+
queue.move("q1", { insertBefore: "q2" });
|
|
110
|
+
queue.move("s1", {});
|
|
111
|
+
expect(sent).toEqual([
|
|
112
|
+
{ type: "run/send", runId: "run-1", messageId: "q1", behavior: "steer" },
|
|
113
|
+
{
|
|
114
|
+
type: "run/send",
|
|
115
|
+
runId: "run-1",
|
|
116
|
+
messageId: "s1",
|
|
117
|
+
behavior: "queue",
|
|
118
|
+
insertAfter: null,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
type: "run/send",
|
|
122
|
+
runId: "run-1",
|
|
123
|
+
messageId: "q1",
|
|
124
|
+
behavior: "queue",
|
|
125
|
+
insertBefore: "q2",
|
|
126
|
+
},
|
|
127
|
+
{ type: "run/send", runId: "run-1", messageId: "s1", behavior: "steer" },
|
|
128
|
+
]);
|
|
129
|
+
});
|
|
74
130
|
});
|
package/src/runCommands.ts
CHANGED
|
@@ -110,7 +110,7 @@ export const resolveEditCommand = (
|
|
|
110
110
|
};
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
/** The send fields for a run-creating or run-targeting
|
|
113
|
+
/** The send fields for a run-creating or run-targeting `run/send`: target the live run, or mint a fresh one anchored at the viewed leaf. */
|
|
114
114
|
export const sendFields = (
|
|
115
115
|
messages: readonly { id: string; status?: { type: string } }[],
|
|
116
116
|
runId: string | null,
|
|
@@ -129,7 +129,11 @@ export const sendFields = (
|
|
|
129
129
|
};
|
|
130
130
|
|
|
131
131
|
export const createRunQueueAdapter = (
|
|
132
|
-
live: () => {
|
|
132
|
+
live: () => {
|
|
133
|
+
messages: readonly { id: string }[];
|
|
134
|
+
runId: string | null;
|
|
135
|
+
running: boolean;
|
|
136
|
+
},
|
|
133
137
|
queueItems: readonly StatewireThread.RunQueueItem[],
|
|
134
138
|
steerQueueItems: readonly StatewireThread.RunQueueItem[],
|
|
135
139
|
dispatch: (command: StatewireThread.RunCommand) => void,
|
|
@@ -149,56 +153,59 @@ export const createRunQueueAdapter = (
|
|
|
149
153
|
throw new Error(`statewire: unknown queue item "${queueItemId}"`);
|
|
150
154
|
};
|
|
151
155
|
// edit-composer sends carry a sourceId; they bypass the lanes into run/edit
|
|
152
|
-
const
|
|
153
|
-
|
|
156
|
+
const send = (
|
|
157
|
+
behavior: StatewireThread.RunSendBehavior,
|
|
154
158
|
message: AppendMessage,
|
|
155
|
-
)
|
|
159
|
+
) => {
|
|
156
160
|
const { messages, runId } = live();
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
161
|
+
dispatchSend(
|
|
162
|
+
message.sourceId != null
|
|
163
|
+
? resolveEditCommand(messages, message, runId)
|
|
164
|
+
: {
|
|
165
|
+
type: "run/send",
|
|
166
|
+
behavior,
|
|
167
|
+
...sendFields(
|
|
168
|
+
messages,
|
|
169
|
+
runId,
|
|
170
|
+
toWireMessage(message, crypto.randomUUID()),
|
|
171
|
+
),
|
|
172
|
+
},
|
|
173
|
+
message,
|
|
163
174
|
);
|
|
164
|
-
return lane === "steer"
|
|
165
|
-
? { type: "run/steer", ...fields }
|
|
166
|
-
: { type: "run/enqueue", ...fields };
|
|
167
175
|
};
|
|
168
176
|
return {
|
|
169
177
|
items: queueItems.map(toQueueItemState),
|
|
170
178
|
steerItems: steerQueueItems.map(toQueueItemState),
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
179
|
+
// The composer steers while running and enqueues otherwise; its default lane sends "auto".
|
|
180
|
+
enqueue: (message) => send(live().running ? "queue" : "auto", message),
|
|
181
|
+
steer: (message) => send(live().running ? "auto" : "steer", message),
|
|
182
|
+
move: (queueItemId, placement) =>
|
|
183
|
+
dispatch({
|
|
184
|
+
type: "run/send",
|
|
176
185
|
runId: liveRunId(),
|
|
177
186
|
messageId: queueItemId,
|
|
187
|
+
behavior:
|
|
188
|
+
(placement.lane ?? laneOf(queueItemId)) === "steer"
|
|
189
|
+
? "steer"
|
|
190
|
+
: "queue",
|
|
178
191
|
...placementFields(placement),
|
|
179
|
-
}
|
|
180
|
-
dispatch(
|
|
181
|
-
lane === "steer"
|
|
182
|
-
? { type: "run/steer", ...fields }
|
|
183
|
-
: { type: "run/enqueue", ...fields },
|
|
184
|
-
);
|
|
185
|
-
},
|
|
192
|
+
}),
|
|
186
193
|
edit: (queueItemId, message) => {
|
|
187
194
|
const { metadata, ...edited } = toWireMessage(message, queueItemId);
|
|
188
195
|
const original = [...queueItems, ...steerQueueItems].find(
|
|
189
196
|
(item) => item.id === queueItemId,
|
|
190
197
|
);
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
);
|
|
198
|
+
dispatch({
|
|
199
|
+
type: "run/send",
|
|
200
|
+
runId: liveRunId(),
|
|
201
|
+
behavior: laneOf(queueItemId),
|
|
202
|
+
message: {
|
|
203
|
+
...edited,
|
|
204
|
+
...(original?.metadata !== undefined && {
|
|
205
|
+
metadata: JSON.parse(JSON.stringify(original.metadata)),
|
|
206
|
+
}),
|
|
207
|
+
},
|
|
208
|
+
});
|
|
202
209
|
},
|
|
203
210
|
remove: (queueItemId) =>
|
|
204
211
|
dispatch({
|
|
@@ -41,8 +41,9 @@ const apply = (draft: Draft, method: string, param?: unknown) =>
|
|
|
41
41
|
describe("runsOptimistic", () => {
|
|
42
42
|
it("parks a steer add in the lane while running", () => {
|
|
43
43
|
const draft = mount({ status: "running", runId: "run-1" });
|
|
44
|
-
apply(draft, "run/
|
|
45
|
-
type: "run/
|
|
44
|
+
apply(draft, "run/send", {
|
|
45
|
+
type: "run/send",
|
|
46
|
+
behavior: "steer",
|
|
46
47
|
runId: "run-1",
|
|
47
48
|
message: message("s1"),
|
|
48
49
|
});
|
|
@@ -59,8 +60,9 @@ describe("runsOptimistic", () => {
|
|
|
59
60
|
steerQueue: [message("s1")],
|
|
60
61
|
error: { message: "boom" },
|
|
61
62
|
});
|
|
62
|
-
apply(draft, "run/
|
|
63
|
-
type: "run/
|
|
63
|
+
apply(draft, "run/send", {
|
|
64
|
+
type: "run/send",
|
|
65
|
+
behavior: "steer",
|
|
64
66
|
runId: "run-1",
|
|
65
67
|
message: message("s2"),
|
|
66
68
|
});
|
|
@@ -79,8 +81,9 @@ describe("runsOptimistic", () => {
|
|
|
79
81
|
steerQueue: [message("s1")],
|
|
80
82
|
inputRequests: [{ type: "custom", id: "r1" }],
|
|
81
83
|
});
|
|
82
|
-
apply(draft, "run/
|
|
83
|
-
type: "run/
|
|
84
|
+
apply(draft, "run/send", {
|
|
85
|
+
type: "run/send",
|
|
86
|
+
behavior: "steer",
|
|
84
87
|
runId: "run-1",
|
|
85
88
|
message: message("s2"),
|
|
86
89
|
});
|
|
@@ -96,8 +99,9 @@ describe("runsOptimistic", () => {
|
|
|
96
99
|
|
|
97
100
|
it("dispatches an enqueue add only in ready or in error/stop with empty lanes", () => {
|
|
98
101
|
const ready = mount();
|
|
99
|
-
apply(ready, "run/
|
|
100
|
-
type: "run/
|
|
102
|
+
apply(ready, "run/send", {
|
|
103
|
+
type: "run/send",
|
|
104
|
+
behavior: "queue",
|
|
101
105
|
runId: "mint-1",
|
|
102
106
|
message: message("a"),
|
|
103
107
|
runAnchorMessageId: null,
|
|
@@ -111,8 +115,9 @@ describe("runsOptimistic", () => {
|
|
|
111
115
|
]);
|
|
112
116
|
|
|
113
117
|
const errored = mount({ status: "error", runId: "run-1" });
|
|
114
|
-
apply(errored, "run/
|
|
115
|
-
type: "run/
|
|
118
|
+
apply(errored, "run/send", {
|
|
119
|
+
type: "run/send",
|
|
120
|
+
behavior: "queue",
|
|
116
121
|
runId: "run-1",
|
|
117
122
|
message: message("a"),
|
|
118
123
|
});
|
|
@@ -127,8 +132,9 @@ describe("runsOptimistic", () => {
|
|
|
127
132
|
runId: "run-1",
|
|
128
133
|
queue: [message("q1")],
|
|
129
134
|
});
|
|
130
|
-
apply(stopped, "run/
|
|
131
|
-
type: "run/
|
|
135
|
+
apply(stopped, "run/send", {
|
|
136
|
+
type: "run/send",
|
|
137
|
+
behavior: "queue",
|
|
132
138
|
runId: "run-1",
|
|
133
139
|
message: message("a"),
|
|
134
140
|
});
|
|
@@ -142,8 +148,9 @@ describe("runsOptimistic", () => {
|
|
|
142
148
|
runId: "r",
|
|
143
149
|
queue: [message("a"), message("b")],
|
|
144
150
|
});
|
|
145
|
-
apply(draft, "run/
|
|
146
|
-
type: "run/
|
|
151
|
+
apply(draft, "run/send", {
|
|
152
|
+
type: "run/send",
|
|
153
|
+
behavior: "queue",
|
|
147
154
|
runId: "r",
|
|
148
155
|
message: message("x"),
|
|
149
156
|
insertAfter: "a",
|
|
@@ -157,13 +164,15 @@ describe("runsOptimistic", () => {
|
|
|
157
164
|
runId: "r",
|
|
158
165
|
dispatching: { trigger: "message-send", messages: [message("d1")] },
|
|
159
166
|
});
|
|
160
|
-
apply(draft, "run/
|
|
161
|
-
type: "run/
|
|
167
|
+
apply(draft, "run/send", {
|
|
168
|
+
type: "run/send",
|
|
169
|
+
behavior: "queue",
|
|
162
170
|
runId: "r",
|
|
163
171
|
message: message("d1"),
|
|
164
172
|
});
|
|
165
|
-
apply(draft, "run/
|
|
166
|
-
type: "run/
|
|
173
|
+
apply(draft, "run/send", {
|
|
174
|
+
type: "run/send",
|
|
175
|
+
behavior: "steer",
|
|
167
176
|
runId: "r",
|
|
168
177
|
messageId: "d1",
|
|
169
178
|
});
|
|
@@ -180,8 +189,9 @@ describe("runsOptimistic", () => {
|
|
|
180
189
|
runId: "r",
|
|
181
190
|
queue: [{ ...message("q1"), meta: { m: 1 } }, message("q2")],
|
|
182
191
|
});
|
|
183
|
-
apply(draft, "run/
|
|
184
|
-
type: "run/
|
|
192
|
+
apply(draft, "run/send", {
|
|
193
|
+
type: "run/send",
|
|
194
|
+
behavior: "steer",
|
|
185
195
|
runId: "r",
|
|
186
196
|
messageId: "q1",
|
|
187
197
|
});
|
|
@@ -189,8 +199,9 @@ describe("runsOptimistic", () => {
|
|
|
189
199
|
expect(run(draft).steerQueue.map((e: Draft) => e.id)).toEqual(["q1"]);
|
|
190
200
|
expect(run(draft).steerQueue[0].meta).toEqual({ m: 1 });
|
|
191
201
|
|
|
192
|
-
apply(draft, "run/
|
|
193
|
-
type: "run/
|
|
202
|
+
apply(draft, "run/send", {
|
|
203
|
+
type: "run/send",
|
|
204
|
+
behavior: "queue",
|
|
194
205
|
runId: "r",
|
|
195
206
|
message: { ...message("q2"), parts: [{ type: "text", text: "new" }] },
|
|
196
207
|
});
|
|
@@ -383,8 +394,9 @@ describe("runsOptimistic", () => {
|
|
|
383
394
|
|
|
384
395
|
it("dispatches from a bare ready mount by creating the run entry", () => {
|
|
385
396
|
const draft = mount();
|
|
386
|
-
apply(draft, "run/
|
|
387
|
-
type: "run/
|
|
397
|
+
apply(draft, "run/send", {
|
|
398
|
+
type: "run/send",
|
|
399
|
+
behavior: "steer",
|
|
388
400
|
runId: "mint-1",
|
|
389
401
|
message: message("s1"),
|
|
390
402
|
runAnchorMessageId: null,
|
|
@@ -451,8 +463,9 @@ describe("runsOptimistic", () => {
|
|
|
451
463
|
{ type: "custom", id: "r2" },
|
|
452
464
|
],
|
|
453
465
|
});
|
|
454
|
-
apply(draft, "run/
|
|
455
|
-
type: "run/
|
|
466
|
+
apply(draft, "run/send", {
|
|
467
|
+
type: "run/send",
|
|
468
|
+
behavior: "steer",
|
|
456
469
|
runId: "run-1",
|
|
457
470
|
message: message("s1"),
|
|
458
471
|
});
|
|
@@ -468,11 +481,239 @@ describe("runsOptimistic", () => {
|
|
|
468
481
|
it("leaves the draft untouched for unknown moves and idle run/stop", () => {
|
|
469
482
|
const draft = mount({ status: "stopped", runId: "run-1" });
|
|
470
483
|
apply(draft, "run/stop", { runId: "run-1" });
|
|
471
|
-
apply(draft, "run/
|
|
472
|
-
type: "run/
|
|
484
|
+
apply(draft, "run/send", {
|
|
485
|
+
type: "run/send",
|
|
486
|
+
behavior: "queue",
|
|
473
487
|
runId: "run-1",
|
|
474
488
|
messageId: "ghost",
|
|
475
489
|
});
|
|
476
490
|
expect(draft).toEqual(mount({ status: "stopped", runId: "run-1" }));
|
|
477
491
|
});
|
|
492
|
+
|
|
493
|
+
const send = (
|
|
494
|
+
draft: Draft,
|
|
495
|
+
behavior: string | undefined,
|
|
496
|
+
fields: Record<string, unknown>,
|
|
497
|
+
) =>
|
|
498
|
+
apply(draft, "run/send", {
|
|
499
|
+
type: "run/send",
|
|
500
|
+
...(behavior !== undefined && { behavior }),
|
|
501
|
+
...fields,
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
const lanes = (draft: Draft) => ({
|
|
505
|
+
queue: run(draft).queue.map((e: Draft) => e.id),
|
|
506
|
+
steerQueue: run(draft).steerQueue.map((e: Draft) => e.id),
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it("parks auto, steer and preempt in the steer lane and queue in the queue lane while running", () => {
|
|
510
|
+
for (const [behavior, lane] of [
|
|
511
|
+
[undefined, "steerQueue"],
|
|
512
|
+
["auto", "steerQueue"],
|
|
513
|
+
["steer", "steerQueue"],
|
|
514
|
+
["preempt", "steerQueue"],
|
|
515
|
+
["queue", "queue"],
|
|
516
|
+
] as const) {
|
|
517
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
518
|
+
send(draft, behavior, { runId: "run-1", message: message("m1") });
|
|
519
|
+
expect(lanes(draft)).toEqual({
|
|
520
|
+
queue: [],
|
|
521
|
+
steerQueue: [],
|
|
522
|
+
[lane]: ["m1"],
|
|
523
|
+
});
|
|
524
|
+
expect(run(draft).status).toBe("running");
|
|
525
|
+
expect(run(draft).dispatching).toBeUndefined();
|
|
526
|
+
expect(run(draft).stopping).toEqual(
|
|
527
|
+
behavior === "preempt" ? { reason: "stop" } : undefined,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it("keeps a preempt in the steer lane while the run stops", () => {
|
|
533
|
+
const draft = mount({
|
|
534
|
+
status: "running",
|
|
535
|
+
runId: "run-1",
|
|
536
|
+
stopping: { reason: "stop" },
|
|
537
|
+
});
|
|
538
|
+
send(draft, "preempt", { runId: "run-1", message: message("p1") });
|
|
539
|
+
expect(lanes(draft)).toEqual({ queue: [], steerQueue: ["p1"] });
|
|
540
|
+
expect(run(draft).stopping).toEqual({ reason: "stop" });
|
|
541
|
+
expect(run(draft).status).toBe("running");
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it("leaves a staged rewind's stopping window under a preempt", () => {
|
|
545
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
546
|
+
apply(draft, "run/edit", {
|
|
547
|
+
type: "run/edit",
|
|
548
|
+
runId: "run-1",
|
|
549
|
+
message: message("m1b"),
|
|
550
|
+
sourceId: "m1",
|
|
551
|
+
runAnchorMessageId: "p0",
|
|
552
|
+
});
|
|
553
|
+
send(draft, "preempt", { runId: "run-1", message: message("p1") });
|
|
554
|
+
expect(run(draft).stopping).toEqual({ reason: "message-edit" });
|
|
555
|
+
expect(lanes(draft)).toEqual({ queue: [], steerQueue: ["p1"] });
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it("follows the host when it places an auto send in the queue lane", () => {
|
|
559
|
+
const optimistic = mount({ status: "running", runId: "run-1" });
|
|
560
|
+
send(optimistic, "auto", { runId: "run-1", message: message("a1") });
|
|
561
|
+
expect(lanes(optimistic)).toEqual({ queue: [], steerQueue: ["a1"] });
|
|
562
|
+
|
|
563
|
+
// The prioritizer downgraded the send; the overlay still folds over it.
|
|
564
|
+
const canonical = mount({
|
|
565
|
+
status: "running",
|
|
566
|
+
runId: "run-1",
|
|
567
|
+
queue: [message("q0"), message("a1")],
|
|
568
|
+
});
|
|
569
|
+
send(canonical, "auto", { runId: "run-1", message: message("a1") });
|
|
570
|
+
expect(lanes(canonical)).toEqual({ queue: ["q0", "a1"], steerQueue: [] });
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("folds auto and queue as an enqueue and steer and preempt as a steer while idle", () => {
|
|
574
|
+
for (const behavior of ["auto", "queue"]) {
|
|
575
|
+
const ready = mount();
|
|
576
|
+
send(ready, behavior, {
|
|
577
|
+
runId: "mint-1",
|
|
578
|
+
message: message("a"),
|
|
579
|
+
runAnchorMessageId: null,
|
|
580
|
+
});
|
|
581
|
+
expect(run(ready).dispatching).toEqual({
|
|
582
|
+
trigger: "message-send",
|
|
583
|
+
messages: [message("a")],
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
const stopped = mount({
|
|
587
|
+
status: "stopped",
|
|
588
|
+
runId: "run-1",
|
|
589
|
+
steerQueue: [message("s1")],
|
|
590
|
+
});
|
|
591
|
+
send(stopped, behavior, { runId: "run-1", message: message("a") });
|
|
592
|
+
expect(stopped.status).toBe("stopped");
|
|
593
|
+
expect(lanes(stopped)).toEqual({ queue: ["a"], steerQueue: ["s1"] });
|
|
594
|
+
}
|
|
595
|
+
for (const behavior of ["steer", "preempt"]) {
|
|
596
|
+
const stopped = mount({
|
|
597
|
+
status: "stopped",
|
|
598
|
+
runId: "run-1",
|
|
599
|
+
steerQueue: [message("s1")],
|
|
600
|
+
});
|
|
601
|
+
send(stopped, behavior, { runId: "run-1", message: message("a") });
|
|
602
|
+
expect(stopped.status).toBe("running");
|
|
603
|
+
expect(lanes(stopped)).toEqual({ queue: [], steerQueue: ["s1", "a"] });
|
|
604
|
+
expect(run(stopped).stopping).toBeUndefined();
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
it("dispatches auto, steer and preempt directly in input-required and queues queue", () => {
|
|
609
|
+
for (const behavior of ["auto", "steer", "preempt", "queue"]) {
|
|
610
|
+
const draft = mount({
|
|
611
|
+
status: "input-required",
|
|
612
|
+
runId: "run-1",
|
|
613
|
+
inputRequests: [{ type: "custom", id: "r1" }],
|
|
614
|
+
});
|
|
615
|
+
send(draft, behavior, { runId: "run-1", message: message("m1") });
|
|
616
|
+
if (behavior === "queue") {
|
|
617
|
+
expect(draft.status).toBe("input-required");
|
|
618
|
+
expect(lanes(draft)).toEqual({ queue: ["m1"], steerQueue: [] });
|
|
619
|
+
} else {
|
|
620
|
+
expect(draft.status).toBe("running");
|
|
621
|
+
expect(run(draft).stopping).toBeUndefined();
|
|
622
|
+
expect(run(draft).dispatching.messages.map((e: Draft) => e.id)).toEqual(
|
|
623
|
+
["m1"],
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it("moves a queued message by behavior, auto as a steer while running", () => {
|
|
630
|
+
for (const [behavior, lane] of [
|
|
631
|
+
[undefined, "steerQueue"],
|
|
632
|
+
["auto", "steerQueue"],
|
|
633
|
+
["steer", "steerQueue"],
|
|
634
|
+
["preempt", "steerQueue"],
|
|
635
|
+
["queue", "queue"],
|
|
636
|
+
] as const) {
|
|
637
|
+
const draft = mount({
|
|
638
|
+
status: "running",
|
|
639
|
+
runId: "run-1",
|
|
640
|
+
queue: [message("q1")],
|
|
641
|
+
steerQueue: [message("s1")],
|
|
642
|
+
});
|
|
643
|
+
send(draft, behavior, { runId: "run-1", messageId: "q1" });
|
|
644
|
+
send(draft, behavior, {
|
|
645
|
+
runId: "run-1",
|
|
646
|
+
messageId: "s1",
|
|
647
|
+
insertAfter: null,
|
|
648
|
+
});
|
|
649
|
+
expect(lanes(draft)[lane]).toEqual(["s1", "q1"]);
|
|
650
|
+
expect(run(draft).stopping).toEqual(
|
|
651
|
+
behavior === "preempt" ? { reason: "stop" } : undefined,
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("steers an auto move during a stopping window", () => {
|
|
657
|
+
const draft = mount({
|
|
658
|
+
status: "stopping",
|
|
659
|
+
runId: "run-1",
|
|
660
|
+
stopping: { reason: "stop" },
|
|
661
|
+
queue: [message("q1")],
|
|
662
|
+
});
|
|
663
|
+
send(draft, "auto", { runId: "run-1", messageId: "q1" });
|
|
664
|
+
expect(lanes(draft)).toEqual({ queue: [], steerQueue: ["q1"] });
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it("keeps an auto move queued while the run is stopped", () => {
|
|
668
|
+
const draft = mount({
|
|
669
|
+
status: "stopped",
|
|
670
|
+
runId: "run-1",
|
|
671
|
+
queue: [message("q1")],
|
|
672
|
+
});
|
|
673
|
+
send(draft, "auto", { runId: "run-1", messageId: "q1" });
|
|
674
|
+
expect(lanes(draft)).toEqual({ queue: ["q1"], steerQueue: [] });
|
|
675
|
+
expect(draft.status).toBe("stopped");
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
it("skips a preempt move of a dispatched or unknown message", () => {
|
|
679
|
+
const draft = mount({
|
|
680
|
+
status: "running",
|
|
681
|
+
runId: "run-1",
|
|
682
|
+
dispatching: { trigger: "message-send", messages: [message("d1")] },
|
|
683
|
+
});
|
|
684
|
+
send(draft, "preempt", { runId: "run-1", messageId: "d1" });
|
|
685
|
+
send(draft, "preempt", { runId: "run-1", messageId: "ghost" });
|
|
686
|
+
expect(run(draft).stopping).toBeUndefined();
|
|
687
|
+
expect(lanes(draft)).toEqual({ queue: [], steerQueue: [] });
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
it("rejects an unknown behavior", () => {
|
|
691
|
+
expect(() =>
|
|
692
|
+
send(mount(), "later", { runId: "r", message: message("m1") }),
|
|
693
|
+
).toThrow('statewire: unknown run/send behavior "later"');
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
it("folds a held run/send into runsHeld only", () => {
|
|
697
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
698
|
+
runsOptimistic(
|
|
699
|
+
draft,
|
|
700
|
+
{
|
|
701
|
+
method: "run/send",
|
|
702
|
+
params: [
|
|
703
|
+
{
|
|
704
|
+
type: "run/send",
|
|
705
|
+
runId: "run-1",
|
|
706
|
+
behavior: "preempt",
|
|
707
|
+
message: message("h1"),
|
|
708
|
+
},
|
|
709
|
+
],
|
|
710
|
+
} as never,
|
|
711
|
+
{ ts: 0, state: "held", localMeta: { tag: 1 } },
|
|
712
|
+
);
|
|
713
|
+
expect(draft.runsHeld).toEqual([
|
|
714
|
+
{ ...message("h1"), localMeta: { tag: 1 } },
|
|
715
|
+
]);
|
|
716
|
+
expect(lanes(draft)).toEqual({ queue: [], steerQueue: [] });
|
|
717
|
+
expect(run(draft).stopping).toBeUndefined();
|
|
718
|
+
});
|
|
478
719
|
});
|
package/src/runsOptimistic.ts
CHANGED
|
@@ -52,14 +52,19 @@ const insert = (
|
|
|
52
52
|
lane.splice(index < 0 ? lane.length : index, 0, entry);
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
const
|
|
55
|
+
const BEHAVIORS = ["auto", "steer", "preempt", "queue"];
|
|
56
|
+
|
|
57
|
+
const send = (
|
|
56
58
|
draft: RunsDraft,
|
|
57
|
-
command: StatewireThread.
|
|
59
|
+
command: StatewireThread.RunSendCommand,
|
|
58
60
|
runId: string,
|
|
59
61
|
): void => {
|
|
60
62
|
const run = draft.runs?.[0];
|
|
61
63
|
// The entry status is the truth; hosts replicating the top-level projection agree.
|
|
62
64
|
const status = run?.status ?? "ready";
|
|
65
|
+
const behavior = command.behavior ?? "auto";
|
|
66
|
+
if (!BEHAVIORS.includes(behavior))
|
|
67
|
+
throw new Error(`statewire: unknown run/send behavior "${behavior}"`);
|
|
63
68
|
const id = "message" in command ? command.message.id : command.messageId;
|
|
64
69
|
// In dispatch limbo the canonical state already tells the story: no fold.
|
|
65
70
|
if (run?.dispatching?.messages.some((e) => e.id === id)) return;
|
|
@@ -67,6 +72,9 @@ const enqueueOrSteer = (
|
|
|
67
72
|
run === undefined
|
|
68
73
|
? undefined
|
|
69
74
|
: [run.queue, run.steerQueue].find((l) => l.some((e) => e.id === id));
|
|
75
|
+
// A laned "auto" send is host-placed: its lane is the host's decision.
|
|
76
|
+
if (behavior === "auto" && "message" in command && sourceLane !== undefined)
|
|
77
|
+
return;
|
|
70
78
|
|
|
71
79
|
let entry: LaneEntry;
|
|
72
80
|
let sourceIndex = -1;
|
|
@@ -83,7 +91,12 @@ const enqueueOrSteer = (
|
|
|
83
91
|
entry = laneEntry(command.message);
|
|
84
92
|
}
|
|
85
93
|
|
|
86
|
-
const steer =
|
|
94
|
+
const steer =
|
|
95
|
+
behavior === "auto"
|
|
96
|
+
? status === "running" ||
|
|
97
|
+
status === "input-required" ||
|
|
98
|
+
run?.stopping != null
|
|
99
|
+
: behavior !== "queue";
|
|
87
100
|
const direct = steer
|
|
88
101
|
? status === "ready" || status === "input-required"
|
|
89
102
|
: sourceLane === undefined &&
|
|
@@ -122,6 +135,12 @@ const enqueueOrSteer = (
|
|
|
122
135
|
if (sourceLane === lane && !hasPlacement) lane.splice(sourceIndex, 0, entry);
|
|
123
136
|
else insert(lane, entry, command);
|
|
124
137
|
if (continues) startRun(draft, runId);
|
|
138
|
+
if (
|
|
139
|
+
behavior === "preempt" &&
|
|
140
|
+
status === "running" &&
|
|
141
|
+
run.nextDispatch === undefined
|
|
142
|
+
)
|
|
143
|
+
run.stopping = { reason: "stop" };
|
|
125
144
|
};
|
|
126
145
|
|
|
127
146
|
const stageRewind = (run: RunEntry, dispatch: Dispatch): void => {
|
|
@@ -152,9 +171,7 @@ export const runsOptimistic = (
|
|
|
152
171
|
| { message?: StatewireThread.UserMessage }
|
|
153
172
|
| undefined;
|
|
154
173
|
if (
|
|
155
|
-
(command.method === "run/
|
|
156
|
-
command.method === "run/steer" ||
|
|
157
|
-
command.method === "run/edit") &&
|
|
174
|
+
(command.method === "run/send" || command.method === "run/edit") &&
|
|
158
175
|
params?.message !== undefined
|
|
159
176
|
)
|
|
160
177
|
(draft.runsHeld ??= []).push({
|
|
@@ -167,15 +184,8 @@ export const runsOptimistic = (
|
|
|
167
184
|
?.runId;
|
|
168
185
|
const runId = commandRunId ?? `optimistic-${ctx.seq ?? `q${ctx.ts}`}`;
|
|
169
186
|
switch (command.method) {
|
|
170
|
-
case "run/
|
|
171
|
-
|
|
172
|
-
enqueueOrSteer(
|
|
173
|
-
draft,
|
|
174
|
-
command.params[0] as
|
|
175
|
-
| StatewireThread.RunEnqueueCommand
|
|
176
|
-
| StatewireThread.RunSteerCommand,
|
|
177
|
-
runId,
|
|
178
|
-
);
|
|
187
|
+
case "run/send": {
|
|
188
|
+
send(draft, command.params[0] as StatewireThread.RunSendCommand, runId);
|
|
179
189
|
break;
|
|
180
190
|
}
|
|
181
191
|
case "run/edit": {
|