@nathanstephenson/flow 0.1.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/README.md +641 -0
- package/build/workflow-runtime.cjs +213964 -0
- package/dist/backend/claude/async-queue.js +44 -0
- package/dist/backend/claude/auto-compaction.js +10 -0
- package/dist/backend/claude/background-calls.js +116 -0
- package/dist/backend/claude/enquiries.js +162 -0
- package/dist/backend/claude/index.js +1138 -0
- package/dist/backend/claude/mcp.js +55 -0
- package/dist/backend/claude/permissions.js +106 -0
- package/dist/backend/claude/streamed-message.js +120 -0
- package/dist/backend/claude/subagents.js +128 -0
- package/dist/backend/claude/workflow-parent.js +12 -0
- package/dist/backend/claude/workflow-processes.js +95 -0
- package/dist/backend/claude/workflow-subagent.js +234 -0
- package/dist/backend/effort.js +30 -0
- package/dist/backend/fake/index.js +570 -0
- package/dist/backend/mcp.js +190 -0
- package/dist/backend/pi/auto-compaction.js +16 -0
- package/dist/backend/pi/background-calls.js +56 -0
- package/dist/backend/pi/enquiries.js +63 -0
- package/dist/backend/pi/index.js +597 -0
- package/dist/backend/pi/mcp.js +35 -0
- package/dist/backend/pi/subagents.js +23 -0
- package/dist/backend/pi/work.js +104 -0
- package/dist/backend/pi/workflow-parent.js +21 -0
- package/dist/backend/pi/workflow-subagent.js +199 -0
- package/dist/backend/registry.js +34 -0
- package/dist/backend/types.js +1 -0
- package/dist/backend/workflow-tools.js +25 -0
- package/dist/build-id +1 -0
- package/dist/cli/application.js +397 -0
- package/dist/cli/bootstrap.js +535 -0
- package/dist/cli/host-control.js +100 -0
- package/dist/cli/install-guard.js +270 -0
- package/dist/cli/install-process.js +52 -0
- package/dist/cli/main.js +12 -0
- package/dist/cli/update.js +128 -0
- package/dist/client/connection.js +183 -0
- package/dist/client/context-usage.js +66 -0
- package/dist/client/diff.js +36 -0
- package/dist/client/enquiry.js +105 -0
- package/dist/client/markdown.js +192 -0
- package/dist/client/model-choices.js +32 -0
- package/dist/client/permission.js +60 -0
- package/dist/client/project-name.js +19 -0
- package/dist/client/reduce.js +463 -0
- package/dist/client/relative-time.js +30 -0
- package/dist/client/scope-kind.js +28 -0
- package/dist/client/search.js +136 -0
- package/dist/client/session-label.js +9 -0
- package/dist/client/status.js +84 -0
- package/dist/client/tool-chains.js +52 -0
- package/dist/client/tool-summary.js +98 -0
- package/dist/daemon/auth.js +590 -0
- package/dist/daemon/config-store.js +183 -0
- package/dist/daemon/config.js +687 -0
- package/dist/daemon/credential-redaction.js +9 -0
- package/dist/daemon/git.js +312 -0
- package/dist/daemon/host-control-routes.js +29 -0
- package/dist/daemon/host.js +2343 -0
- package/dist/daemon/http.js +19 -0
- package/dist/daemon/log.js +44 -0
- package/dist/daemon/mcp-auth.js +164 -0
- package/dist/daemon/models.js +85 -0
- package/dist/daemon/ownership.js +104 -0
- package/dist/daemon/projects.js +317 -0
- package/dist/daemon/publish.js +289 -0
- package/dist/daemon/pull-request.js +198 -0
- package/dist/daemon/runtime.js +100 -0
- package/dist/daemon/secret-store.js +65 -0
- package/dist/daemon/server.js +827 -0
- package/dist/daemon/shell.js +220 -0
- package/dist/daemon/skills.js +104 -0
- package/dist/daemon/stack-metadata.js +24 -0
- package/dist/daemon/stack.js +743 -0
- package/dist/daemon/store.js +130 -0
- package/dist/daemon/summariser.js +476 -0
- package/dist/daemon/workflow-execution-routes.js +105 -0
- package/dist/daemon/workflow-executions.js +1018 -0
- package/dist/daemon/workflow-mcp.js +20 -0
- package/dist/daemon/workflow-routes.js +102 -0
- package/dist/protocol/attachments.js +59 -0
- package/dist/protocol/commands.js +1 -0
- package/dist/protocol/events.js +20 -0
- package/dist/protocol/fonts.js +26 -0
- package/dist/protocol/git.js +12 -0
- package/dist/protocol/mcp.js +51 -0
- package/dist/protocol/projects.js +21 -0
- package/dist/protocol/publish.js +1 -0
- package/dist/protocol/pull-request.js +1 -0
- package/dist/protocol/secrets.js +4 -0
- package/dist/protocol/settings.js +63 -0
- package/dist/protocol/shells.js +8 -0
- package/dist/protocol/skills.js +16 -0
- package/dist/protocol/stack.js +41 -0
- package/dist/protocol/workflow-executions.js +1 -0
- package/dist/protocol/workflows.js +1 -0
- package/dist/tui/app.js +518 -0
- package/dist/tui/keys.js +54 -0
- package/dist/tui/render.js +366 -0
- package/dist/web/assets.js +10 -0
- package/dist/web/embedded.js +12 -0
- package/dist/web/manifest.js +70 -0
- package/dist/workflows/decisions.js +65 -0
- package/dist/workflows/executors.js +132 -0
- package/dist/workflows/graph.js +276 -0
- package/dist/workflows/json-schema.js +49 -0
- package/dist/workflows/loops.js +102 -0
- package/dist/workflows/mcp.js +18 -0
- package/dist/workflows/records.js +111 -0
- package/dist/workflows/recovery.js +25 -0
- package/dist/workflows/runtime-asset.js +17 -0
- package/dist/workflows/runtime-libs.js +1 -0
- package/dist/workflows/runtime-settings.js +23 -0
- package/dist/workflows/runtime.js +343 -0
- package/dist/workflows/scheduler.js +573 -0
- package/dist/workflows/schema.js +161 -0
- package/dist/workflows/store.js +93 -0
- package/package.json +87 -0
- package/web/dist/assets/__vite-browser-external-2447137e-BvRk9kiK.js +0 -0
- package/web/dist/assets/ghostty-vt-DOMeXDrv.wasm +0 -0
- package/web/dist/assets/index-BC0gLS53.js +106 -0
- package/web/dist/assets/index-CCeZYKES.css +2 -0
- package/web/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/web/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/web/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/web/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/web/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/web/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/web/dist/assets/rolldown-runtime-B0Z9INg1.js +1 -0
- package/web/dist/assets/select-6i3wxovH.js +1 -0
- package/web/dist/assets/settings-mcp-CcT0ADJR.js +1 -0
- package/web/dist/assets/settings-secrets-CK4uF7fd.js +1 -0
- package/web/dist/assets/settings-workflows-CrbZF-nM.js +23 -0
- package/web/dist/assets/switch-ftZleVB8.js +6 -0
- package/web/dist/assets/workflow-editors-naPqAmYO.js +65 -0
- package/web/dist/assets/workflows-D4T4Gy_7.css +1 -0
- package/web/dist/assets/workflows-hh6ioeEA.js +7 -0
- package/web/dist/assets/workflows-pane-Db-MHf7Z.js +1 -0
- package/web/dist/index.html +31 -0
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { clampEffort } from "../effort.js";
|
|
3
|
+
// Two models on purpose: one with an effort control and one without, which is the split every
|
|
4
|
+
// real backend has (Claude's haiku offers no effort) and the one clients must cope with. The same
|
|
5
|
+
// two carry the Attachment split for the same reason — pi's registry has models that cannot be
|
|
6
|
+
// shown an image, so a client that assumes every model can is a client with a bug nothing here
|
|
7
|
+
// would have caught.
|
|
8
|
+
const FAKE_CAPABILITIES = {
|
|
9
|
+
providers: ["fake"],
|
|
10
|
+
models: [
|
|
11
|
+
{ id: "fake-1", provider: "fake", label: "Fake 1", effortLevels: ["low", "medium", "high"], acceptsImages: true },
|
|
12
|
+
{ id: "fake-2", provider: "fake", label: "Fake 2" },
|
|
13
|
+
],
|
|
14
|
+
compaction: false,
|
|
15
|
+
fork: false,
|
|
16
|
+
subagents: true,
|
|
17
|
+
// True by default, like `subagents` and unlike `compaction`: an Enquiry is something almost every
|
|
18
|
+
// host and reducer test needs to be able to drive, and the cannot-ask case stays reachable through
|
|
19
|
+
// the overrides for the handful that assert on a backend which has no channel to ask through.
|
|
20
|
+
enquiries: true,
|
|
21
|
+
// True for the reason `enquiries` is: a Permission Prompt is something the host, reducer and
|
|
22
|
+
// durability tests all need to drive, and the cannot-ask case stays reachable through the
|
|
23
|
+
// overrides for the few that assert on a backend which never asks.
|
|
24
|
+
permissions: true,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* A Backend Adapter with no model behind it, driven entirely by the test.
|
|
28
|
+
*
|
|
29
|
+
* Turns do not end on their own — the test calls `completeTurn()`. That makes queue and lifecycle
|
|
30
|
+
* assertions deterministic instead of timing-dependent.
|
|
31
|
+
*/
|
|
32
|
+
export class FakeSession {
|
|
33
|
+
capabilities;
|
|
34
|
+
prompts = [];
|
|
35
|
+
/** Every compaction asked of this session, with whatever instructions came with it. */
|
|
36
|
+
compactions = [];
|
|
37
|
+
/** Parallel to `prompts`, so a test can assert what reached the backend beside each text. */
|
|
38
|
+
promptedAttachments = [];
|
|
39
|
+
resumedFrom;
|
|
40
|
+
/** What this session was told the Agent Session had already spent, for asserting a Revive. */
|
|
41
|
+
priorSpend;
|
|
42
|
+
/** What this session was told the machine already authorises, for asserting the read-at-create. */
|
|
43
|
+
standingAuthorisations;
|
|
44
|
+
/** Parent Workflow tools, exposed so relay orchestration can be driven without a model in tests. */
|
|
45
|
+
workflow;
|
|
46
|
+
/** Whether this session was created as a one-shot text call, for asserting the Summary Model's. */
|
|
47
|
+
toolless;
|
|
48
|
+
/** Every Subagent begun in this session, in the style of `prompts`. */
|
|
49
|
+
subagents = [];
|
|
50
|
+
workflowSubagents = [];
|
|
51
|
+
startWorkflowSubagent(options) {
|
|
52
|
+
if (this.disposed)
|
|
53
|
+
throw new Error('Backend Session is disposed');
|
|
54
|
+
const handle = new FakeWorkflowSubagent(options);
|
|
55
|
+
this.workflowSubagents.push(handle);
|
|
56
|
+
return handle;
|
|
57
|
+
}
|
|
58
|
+
/** Every Enquiry asked in this session, open or not, in the style of `prompts`. */
|
|
59
|
+
enquiries = [];
|
|
60
|
+
/** What was answered, so a test can assert what reached the backend rather than what it emitted. */
|
|
61
|
+
answered = [];
|
|
62
|
+
/** Every Permission Prompt raised in this session, open or not, in the style of `enquiries`. */
|
|
63
|
+
permissionPrompts = [];
|
|
64
|
+
/** What was decided, so a test can assert what reached the backend rather than what it emitted. */
|
|
65
|
+
decided = [];
|
|
66
|
+
modelId;
|
|
67
|
+
effort;
|
|
68
|
+
disposed = false;
|
|
69
|
+
emit;
|
|
70
|
+
turnId;
|
|
71
|
+
wantedEffort;
|
|
72
|
+
/**
|
|
73
|
+
* Present only when this session declares it can compact, which is what the conformance contract
|
|
74
|
+
* asserts of every adapter: the flag and the method have to say the same thing, or a client that
|
|
75
|
+
* hides its control on the flag will call a method that is not there.
|
|
76
|
+
*/
|
|
77
|
+
compact;
|
|
78
|
+
/**
|
|
79
|
+
* Present only when this session declares it can ask, for the reason `compact` is: the conformance
|
|
80
|
+
* contract asserts the flag and the method say the same thing, or a client that hides its control
|
|
81
|
+
* on the flag will call a method that is not there.
|
|
82
|
+
*/
|
|
83
|
+
answerEnquiry;
|
|
84
|
+
/** Present only when this session declares it can be asked before it acts. See `answerEnquiry`. */
|
|
85
|
+
answerPermission;
|
|
86
|
+
/**
|
|
87
|
+
* Leaves a requested compaction running — the turn opens and never closes.
|
|
88
|
+
*
|
|
89
|
+
* Real compactions take minutes, and everything that goes wrong around one goes wrong *during*
|
|
90
|
+
* it: a message racing ahead of it, a second request arriving on top of it. A fake that finishes
|
|
91
|
+
* before it returns cannot reproduce any of that.
|
|
92
|
+
*/
|
|
93
|
+
holdCompaction = false;
|
|
94
|
+
/**
|
|
95
|
+
* Answer every prompt with this text and end the turn, instead of waiting to be driven.
|
|
96
|
+
*
|
|
97
|
+
* The whole fake is built the other way round — turns end when the test says so, which is what
|
|
98
|
+
* makes queue and lifecycle assertions deterministic. But a Summary Model session (ADR 0020) is
|
|
99
|
+
* prompted by the Session Host rather than by the test, so one that had to be driven would hang
|
|
100
|
+
* until the summariser's timeout. This is the one case that needs a fake answering by itself.
|
|
101
|
+
*
|
|
102
|
+
* A test *can* get a handle on the session first, now that naming is pre-warmed — the spare is
|
|
103
|
+
* `backend.sessions[0]` long before it is claimed — but it still cannot get between the host's
|
|
104
|
+
* `prompt` and the answer, which is what this is for.
|
|
105
|
+
*
|
|
106
|
+
* Set here it governs this session; left unset it falls through to the backend's, **read at
|
|
107
|
+
* prompt time rather than copied at create**. That matters now that sessions are warmed long
|
|
108
|
+
* before they are used: a test that sets `backend.autoReply` and then triggers a naming expects
|
|
109
|
+
* the new text, and a spare booted minutes earlier would otherwise answer with the old one. A
|
|
110
|
+
* real backend decides what it says when it is asked, not when it is started.
|
|
111
|
+
*/
|
|
112
|
+
autoReply;
|
|
113
|
+
backendAutoReply;
|
|
114
|
+
constructor(options, overrides = {}, backendAutoReply) {
|
|
115
|
+
this.capabilities = { ...FAKE_CAPABILITIES, ...overrides };
|
|
116
|
+
this.standingAuthorisations = options.standingAuthorisations ?? [];
|
|
117
|
+
this.workflow = options.workflow;
|
|
118
|
+
this.toolless = options.tools === "none";
|
|
119
|
+
this.backendAutoReply = backendAutoReply;
|
|
120
|
+
if (this.capabilities.enquiries) {
|
|
121
|
+
this.answerEnquiry = async (askId, answers) => {
|
|
122
|
+
const open = this.enquiries.find((enquiry) => enquiry.askId === askId);
|
|
123
|
+
// Answered twice is an ordinary race, not a fault — the host turns the false into a refusal.
|
|
124
|
+
if (!open || this.answered.some((seen) => seen.askId === askId))
|
|
125
|
+
return false;
|
|
126
|
+
this.answered.push({ askId, answers });
|
|
127
|
+
this.emit({ type: "enquiry", askId, questions: open.questions, state: "answered", answers });
|
|
128
|
+
this.emit({ type: "tool_ended", callId: askId, result: "The user answered.", isError: false });
|
|
129
|
+
return true;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (this.capabilities.permissions) {
|
|
133
|
+
this.answerPermission = async (callId, decision) => {
|
|
134
|
+
const open = this.permissionPrompts.find((prompt) => prompt.callId === callId);
|
|
135
|
+
// Decided twice is an ordinary race, not a fault — the host turns the false into a refusal,
|
|
136
|
+
// and a false is also what tells it to persist nothing.
|
|
137
|
+
if (!open || this.decided.some((seen) => seen.callId === callId))
|
|
138
|
+
return false;
|
|
139
|
+
this.decided.push({ callId, decision });
|
|
140
|
+
this.emit({ type: "permission", callId, tool: open.tool, state: "decided", decision });
|
|
141
|
+
// A denial still ends the tool call, which is the whole reason a Deny is not an Abort.
|
|
142
|
+
this.emit({
|
|
143
|
+
type: "tool_ended",
|
|
144
|
+
callId,
|
|
145
|
+
result: decision === "deny" ? `${open.tool} is not enabled for this session.` : "ok",
|
|
146
|
+
isError: decision === "deny",
|
|
147
|
+
});
|
|
148
|
+
return true;
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (this.capabilities.compaction) {
|
|
152
|
+
// Opens and closes a turn, because a compaction is one: it spends money and holds the backend
|
|
153
|
+
// while it runs, and the Steering Queue only orders messages correctly if it is told so.
|
|
154
|
+
this.compact = async (instructions) => {
|
|
155
|
+
this.compactions.push(instructions);
|
|
156
|
+
this.turnId = randomUUID();
|
|
157
|
+
this.emit({ type: "turn_started", turnId: this.turnId });
|
|
158
|
+
if (this.holdCompaction)
|
|
159
|
+
return;
|
|
160
|
+
this.emit({ type: "compacted", trigger: "manual", before: 1_000, after: 100 });
|
|
161
|
+
this.completeTurn("complete");
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
this.emit = options.emit;
|
|
165
|
+
this.modelId = options.modelId ?? "fake-1";
|
|
166
|
+
this.resumedFrom = options.resume;
|
|
167
|
+
this.priorSpend = options.priorSpend;
|
|
168
|
+
this.emit({ type: "model_changed", model: { id: this.modelId, provider: "fake" } });
|
|
169
|
+
if (options.effort)
|
|
170
|
+
void this.setEffort(options.effort);
|
|
171
|
+
}
|
|
172
|
+
resumeToken() {
|
|
173
|
+
return "fake-resume";
|
|
174
|
+
}
|
|
175
|
+
async prompt(text, attachments) {
|
|
176
|
+
this.prompts.push(text);
|
|
177
|
+
this.promptedAttachments.push(attachments ?? []);
|
|
178
|
+
this.turnId = randomUUID();
|
|
179
|
+
this.emit({ type: "turn_started", turnId: this.turnId });
|
|
180
|
+
const reply = this.autoReply ?? this.backendAutoReply?.();
|
|
181
|
+
if (reply === undefined)
|
|
182
|
+
return;
|
|
183
|
+
this.say(reply);
|
|
184
|
+
this.completeTurn("complete");
|
|
185
|
+
}
|
|
186
|
+
async abort() {
|
|
187
|
+
this.completeTurn("aborted");
|
|
188
|
+
}
|
|
189
|
+
async setModel(modelId) {
|
|
190
|
+
this.modelId = modelId;
|
|
191
|
+
this.emit({ type: "model_changed", model: { id: modelId, provider: "fake" } });
|
|
192
|
+
if (this.wantedEffort)
|
|
193
|
+
await this.setEffort(this.wantedEffort);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Two, and one of them takes arguments — the split a menu has to render, the same way the two
|
|
197
|
+
* models here carry the Effort and Attachment splits. Reassigned by a test that wants a different
|
|
198
|
+
* catalogue, or emptied by one that wants none.
|
|
199
|
+
*/
|
|
200
|
+
skillList = [
|
|
201
|
+
{ name: "tdd", description: "Red, green, refactor" },
|
|
202
|
+
{ name: "review", description: "Review the diff", argumentHint: "[<pr#>|<branch>]" },
|
|
203
|
+
];
|
|
204
|
+
async skills() {
|
|
205
|
+
return this.skillList;
|
|
206
|
+
}
|
|
207
|
+
async setEffort(effort) {
|
|
208
|
+
this.wantedEffort = effort;
|
|
209
|
+
const levels = FAKE_CAPABILITIES.models.find((model) => model.id === this.modelId)?.effortLevels;
|
|
210
|
+
const level = clampEffort(effort, levels);
|
|
211
|
+
if (!level)
|
|
212
|
+
return;
|
|
213
|
+
this.effort = level;
|
|
214
|
+
this.emit({ type: "effort_changed", effort: level });
|
|
215
|
+
}
|
|
216
|
+
async dispose() {
|
|
217
|
+
this.disposed = true;
|
|
218
|
+
await Promise.all(this.workflowSubagents.map(handle => handle.cancel()));
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Test affordance: report that this session has broken, as an adapter does when its process dies.
|
|
222
|
+
*
|
|
223
|
+
* What a pre-warmed Summary Model session looks like when its CLI crashes or its machine sleeps
|
|
224
|
+
* while it sits idle (ADR 0020). Nobody is reading its events, so this `notice` is the only
|
|
225
|
+
* evidence the spare is a corpse — and handing a corpse to a naming is worse than never having
|
|
226
|
+
* warmed one, because it burns the whole timeout.
|
|
227
|
+
*/
|
|
228
|
+
fail(text = "the backend went away") {
|
|
229
|
+
this.emit({ type: "notice", level: "error", text });
|
|
230
|
+
}
|
|
231
|
+
/** Test affordance: emit assistant text for the turn in flight. */
|
|
232
|
+
say(text, final = true) {
|
|
233
|
+
this.emit({ type: "message", id: `msg-${this.prompts.length}`, text, final });
|
|
234
|
+
}
|
|
235
|
+
/** Test affordance: report Spend, which is how a Revive gets something to carry forward. */
|
|
236
|
+
reportSpend(spend) {
|
|
237
|
+
this.emit({ type: "context_usage", used: 10, window: 100, spend });
|
|
238
|
+
}
|
|
239
|
+
/** Test affordance: emit a complete tool call. */
|
|
240
|
+
useTool(name, input, result, isError = false) {
|
|
241
|
+
const callId = randomUUID();
|
|
242
|
+
this.emit({ type: "tool_started", callId, name, input });
|
|
243
|
+
this.emit({ type: "tool_ended", callId, result, isError });
|
|
244
|
+
return callId;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Test affordance: open a turn nobody prompted for.
|
|
248
|
+
*
|
|
249
|
+
* What a real adapter mints when a backgrounded Subagent settles and the CLI wakes the model on
|
|
250
|
+
* its own (ADR 0016). No `user_message` precedes it, which is the whole point: the Session Host
|
|
251
|
+
* has to take occupancy from the event rather than from having dispatched.
|
|
252
|
+
*/
|
|
253
|
+
startTurn() {
|
|
254
|
+
if (this.turnId)
|
|
255
|
+
return;
|
|
256
|
+
this.turnId = randomUUID();
|
|
257
|
+
this.emit({ type: "turn_started", turnId: this.turnId });
|
|
258
|
+
}
|
|
259
|
+
/** Test affordance: end the turn in flight. */
|
|
260
|
+
completeTurn(reason = "complete") {
|
|
261
|
+
if (!this.turnId)
|
|
262
|
+
return;
|
|
263
|
+
const turnId = this.turnId;
|
|
264
|
+
this.turnId = undefined;
|
|
265
|
+
this.emit({ type: "turn_ended", turnId, reason });
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Ask the human something, as a real backend would: the tool call that asks, and the Enquiry
|
|
269
|
+
* snapshot sharing its id.
|
|
270
|
+
*
|
|
271
|
+
* Both, because both is what a real adapter produces — an Enquiry whose tool call never appeared
|
|
272
|
+
* is a shape nothing downstream should have to cope with, and the front-ends suppress the tool row
|
|
273
|
+
* by pairing it with this one. Returns the `askId` so a test can answer it.
|
|
274
|
+
*
|
|
275
|
+
* Deliberately, `completeTurn()` does **not** close an open Enquiry. Leaving one open is the torn
|
|
276
|
+
* fixture the Session Host has to cope with, and it is the contract `beginSubagent` already sets.
|
|
277
|
+
*/
|
|
278
|
+
ask(questions) {
|
|
279
|
+
const askId = randomUUID();
|
|
280
|
+
this.enquiries.push({ askId, questions });
|
|
281
|
+
this.emit({ type: "tool_started", callId: askId, name: "AskUserQuestion", input: { questions } });
|
|
282
|
+
this.emit({ type: "enquiry", askId, questions, state: "asked" });
|
|
283
|
+
return askId;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Raise a Permission Prompt, as a real backend would: the tool call awaiting authorisation, and
|
|
287
|
+
* the prompt snapshot sharing its id.
|
|
288
|
+
*
|
|
289
|
+
* Both, and here the tool call matters more than it does for an Enquiry — the front-ends fold the
|
|
290
|
+
* decision *onto* that row rather than adding one of their own, so a prompt whose call never
|
|
291
|
+
* appeared is a prompt with nowhere to render. Returns the `callId` so a test can decide it.
|
|
292
|
+
*
|
|
293
|
+
* `completeTurn()` deliberately does not close an open one: that is the torn fixture the Session
|
|
294
|
+
* Host has to cope with, the contract `ask` and `beginSubagent` already set.
|
|
295
|
+
*/
|
|
296
|
+
askPermission(tool, input = {}) {
|
|
297
|
+
const callId = randomUUID();
|
|
298
|
+
this.permissionPrompts.push({ callId, tool });
|
|
299
|
+
this.emit({ type: "tool_started", callId, name: tool, input });
|
|
300
|
+
this.emit({ type: "permission", callId, tool, state: "asked" });
|
|
301
|
+
return callId;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Test affordance: begin a Subagent, and get a handle whose emissions are attributed to it.
|
|
305
|
+
*
|
|
306
|
+
* The only way a test can produce an interleaved parent-and-child stream, which is the case that
|
|
307
|
+
* breaks anything assuming one producer per turn. Emits the spawning `tool_started` as well as the
|
|
308
|
+
* first snapshot, because ADR 0015 has the two share an id and a Subagent whose tool call never
|
|
309
|
+
* appeared would be a shape no real backend can produce.
|
|
310
|
+
*
|
|
311
|
+
* `completeTurn` deliberately does not close an open Subagent: leaving one running is the
|
|
312
|
+
* torn-Subagent fixture, and the Session Host is what has to cope with it.
|
|
313
|
+
*/
|
|
314
|
+
beginSubagent(name, description) {
|
|
315
|
+
const subagentId = randomUUID();
|
|
316
|
+
const subagent = new FakeSubagent(subagentId, name, description, this.emit);
|
|
317
|
+
this.subagents.push(subagent);
|
|
318
|
+
this.emit({ type: "tool_started", callId: subagentId, name: "Agent", input: { name, description } });
|
|
319
|
+
subagent.snapshot({ state: "running" });
|
|
320
|
+
return subagent;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Test affordance: background a tool call, as a real adapter reports one (ADR 0021).
|
|
324
|
+
*
|
|
325
|
+
* All three emissions, because the shape everything downstream has to cope with is a `complete`
|
|
326
|
+
* tool row over a `running` card under one id — a fixture that skipped the receipt would test a
|
|
327
|
+
* transcript no backend produces. The input carries a `command` so `toolSummary` really produces
|
|
328
|
+
* a précis, which is what makes "the tool row is the only place the command lives" testable rather
|
|
329
|
+
* than vacuous.
|
|
330
|
+
*
|
|
331
|
+
* It emits **no** `subagent` event, which is what makes the counts-do-not-cross assertions real.
|
|
332
|
+
*
|
|
333
|
+
* `completeTurn` deliberately does not settle it: an open Background Call outliving its turn is
|
|
334
|
+
* the whole of ADR 0021, and one left open at shutdown is the torn fixture.
|
|
335
|
+
*/
|
|
336
|
+
backgroundCall(tool = "Bash", input = { command: "npm test -- --watch" }) {
|
|
337
|
+
const callId = randomUUID();
|
|
338
|
+
const call = new FakeBackgroundCall(callId, tool, this.emit);
|
|
339
|
+
this.emit({ type: "tool_started", callId, name: tool, input });
|
|
340
|
+
// The receipt returns at once, which is why the row above the card reads `complete`.
|
|
341
|
+
this.emit({ type: "tool_ended", callId, result: { backgroundTaskId: callId }, isError: false });
|
|
342
|
+
call.snapshot({ state: "running" });
|
|
343
|
+
return call;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/** One Background Call under test. */
|
|
347
|
+
export class FakeBackgroundCall {
|
|
348
|
+
finished = false;
|
|
349
|
+
// Plain fields rather than constructor parameter properties: `node --experimental-strip-types`
|
|
350
|
+
// refuses those, and this module is loaded by tests that run under it.
|
|
351
|
+
callId;
|
|
352
|
+
tool;
|
|
353
|
+
emit;
|
|
354
|
+
constructor(callId, tool, emit) {
|
|
355
|
+
this.callId = callId;
|
|
356
|
+
this.tool = tool;
|
|
357
|
+
this.emit = emit;
|
|
358
|
+
}
|
|
359
|
+
settle(reason = "complete") {
|
|
360
|
+
if (this.finished)
|
|
361
|
+
return;
|
|
362
|
+
this.finished = true;
|
|
363
|
+
// Only the snapshot. The receipt already returned at launch, and a second `tool_ended` is a
|
|
364
|
+
// shape no adapter produces — the same rule `FakeSubagent.finish` states.
|
|
365
|
+
this.snapshot({ state: reason });
|
|
366
|
+
}
|
|
367
|
+
/** @internal — used by FakeSession to emit the opening snapshot. */
|
|
368
|
+
snapshot(state) {
|
|
369
|
+
this.emit({ type: "background_call", callId: this.callId, tool: this.tool, ...state });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* One Subagent under test. Every emission carries `producer`, so a test can interleave a parent's
|
|
374
|
+
* stream with a child's and assert neither takes the other's Entry.
|
|
375
|
+
*/
|
|
376
|
+
export class FakeSubagent {
|
|
377
|
+
subagentId;
|
|
378
|
+
name;
|
|
379
|
+
description;
|
|
380
|
+
finished = false;
|
|
381
|
+
launched = false;
|
|
382
|
+
emit;
|
|
383
|
+
/** Bumped when a message finalises, so a partial and its finished half share one id. */
|
|
384
|
+
messageIndex = 0;
|
|
385
|
+
constructor(subagentId, name, description, emit) {
|
|
386
|
+
this.subagentId = subagentId;
|
|
387
|
+
this.name = name;
|
|
388
|
+
this.description = description;
|
|
389
|
+
this.emit = emit;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Attributed assistant text. The id is stable until the message finalises, because a partial and
|
|
393
|
+
* its finished half landing on different ids is the stranded-caret bug — a shape no real adapter
|
|
394
|
+
* may produce, so the double must not either.
|
|
395
|
+
*/
|
|
396
|
+
say(text, final = true) {
|
|
397
|
+
this.emit({
|
|
398
|
+
type: "message",
|
|
399
|
+
id: `${this.subagentId}-msg-${this.messageIndex}`,
|
|
400
|
+
text,
|
|
401
|
+
final,
|
|
402
|
+
producer: { subagentId: this.subagentId },
|
|
403
|
+
});
|
|
404
|
+
if (final)
|
|
405
|
+
this.messageIndex += 1;
|
|
406
|
+
}
|
|
407
|
+
/** An attributed tool call — the subagent's Read, not the parent's. */
|
|
408
|
+
useTool(name, input, result, isError = false) {
|
|
409
|
+
const callId = randomUUID();
|
|
410
|
+
const producer = { subagentId: this.subagentId };
|
|
411
|
+
this.emit({ type: "tool_started", callId, name, input, producer });
|
|
412
|
+
this.emit({ type: "tool_ended", callId, result, isError, producer });
|
|
413
|
+
return callId;
|
|
414
|
+
}
|
|
415
|
+
/** Move to waiting, naming what is being waited on. Snapshot semantics: callable repeatedly. */
|
|
416
|
+
wait(on) {
|
|
417
|
+
this.snapshot({ state: "waiting", on });
|
|
418
|
+
}
|
|
419
|
+
/** Back to running from waiting, without ending. */
|
|
420
|
+
resume() {
|
|
421
|
+
this.snapshot({ state: "running" });
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Background it: return the spawning tool call now, and go on running (ADR 0016).
|
|
425
|
+
*
|
|
426
|
+
* The launch receipt, which is what a real adapter reads to tell a backgrounded Subagent from a
|
|
427
|
+
* finished one. Emits no snapshot — the card stays running — so the turn may end with this
|
|
428
|
+
* Subagent still open, and `finish` closes it however many turns later.
|
|
429
|
+
*/
|
|
430
|
+
launch() {
|
|
431
|
+
if (this.launched || this.finished)
|
|
432
|
+
return;
|
|
433
|
+
this.launched = true;
|
|
434
|
+
this.emit({
|
|
435
|
+
type: "tool_ended",
|
|
436
|
+
callId: this.subagentId,
|
|
437
|
+
result: `${this.name} launched in the background`,
|
|
438
|
+
isError: false,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
finish(reason = "complete") {
|
|
442
|
+
if (this.finished)
|
|
443
|
+
return;
|
|
444
|
+
this.finished = true;
|
|
445
|
+
this.snapshot({ state: reason });
|
|
446
|
+
// The tool result is what returns a foreground Subagent, the same way a real backend closes one.
|
|
447
|
+
// A backgrounded one already returned it at launch, and returning it twice is a shape no adapter
|
|
448
|
+
// produces.
|
|
449
|
+
if (this.launched)
|
|
450
|
+
return;
|
|
451
|
+
this.emit({ type: "tool_ended", callId: this.subagentId, result: `${this.name} finished`, isError: reason === "error" });
|
|
452
|
+
}
|
|
453
|
+
/** @internal — used by FakeSession to emit the opening snapshot. */
|
|
454
|
+
snapshot(state) {
|
|
455
|
+
this.emit({
|
|
456
|
+
type: "subagent",
|
|
457
|
+
subagentId: this.subagentId,
|
|
458
|
+
name: this.name,
|
|
459
|
+
...(this.description === undefined ? {} : { description: this.description }),
|
|
460
|
+
...state,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
export class FakeBackend {
|
|
465
|
+
name = "fake";
|
|
466
|
+
sessions = [];
|
|
467
|
+
// A plain field, not a constructor parameter property: `node --experimental-strip-types` erases
|
|
468
|
+
// types and cannot emit the assignment one implies.
|
|
469
|
+
overrides;
|
|
470
|
+
/** Given to every session this backend creates — see `FakeSession.autoReply`. Settable mid-test. */
|
|
471
|
+
autoReply;
|
|
472
|
+
/**
|
|
473
|
+
* Leave `create` unresolved until `releaseCreate()`, in the style of `holdCompaction`.
|
|
474
|
+
*
|
|
475
|
+
* A real adapter takes the better part of a minute to boot, and the failure that hides in that
|
|
476
|
+
* window is a Session Host that disposed a pre-warmed Summary Model session (ADR 0020) *while it
|
|
477
|
+
* was still starting* — which orphans a child process, since nothing holds a reference to hand
|
|
478
|
+
* back. A fake whose `create` resolves immediately cannot reproduce it.
|
|
479
|
+
*/
|
|
480
|
+
holdCreate = false;
|
|
481
|
+
waiting = [];
|
|
482
|
+
constructor(overrides = {}) {
|
|
483
|
+
this.overrides = overrides;
|
|
484
|
+
}
|
|
485
|
+
async create(options) {
|
|
486
|
+
if (this.holdCreate) {
|
|
487
|
+
await new Promise((resolve) => this.waiting.push(resolve));
|
|
488
|
+
}
|
|
489
|
+
const session = new FakeSession(options, this.overrides, () => this.autoReply);
|
|
490
|
+
this.sessions.push(session);
|
|
491
|
+
return session;
|
|
492
|
+
}
|
|
493
|
+
/** Let every held `create` finish. The sessions exist only after this. */
|
|
494
|
+
releaseCreate() {
|
|
495
|
+
this.holdCreate = false;
|
|
496
|
+
for (const resume of this.waiting.splice(0))
|
|
497
|
+
resume();
|
|
498
|
+
}
|
|
499
|
+
get latest() {
|
|
500
|
+
const session = this.sessions.at(-1);
|
|
501
|
+
if (!session)
|
|
502
|
+
throw new Error("No fake session created yet");
|
|
503
|
+
return session;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
export class FakeWorkflowSubagent {
|
|
507
|
+
options;
|
|
508
|
+
done;
|
|
509
|
+
answers = [];
|
|
510
|
+
decisions = [];
|
|
511
|
+
resolve;
|
|
512
|
+
reject;
|
|
513
|
+
stopped = false;
|
|
514
|
+
enquiries = new Map();
|
|
515
|
+
permissions = new Map();
|
|
516
|
+
constructor(options) {
|
|
517
|
+
this.options = options;
|
|
518
|
+
this.done = new Promise((resolve, reject) => { this.resolve = resolve; this.reject = reject; });
|
|
519
|
+
}
|
|
520
|
+
emit(event) {
|
|
521
|
+
if (!this.stopped)
|
|
522
|
+
this.options.emit({ subagentId: this.options.id, event });
|
|
523
|
+
}
|
|
524
|
+
complete(output) { if (!this.stopped) {
|
|
525
|
+
this.stopped = true;
|
|
526
|
+
this.resolve(JSON.stringify(output));
|
|
527
|
+
} }
|
|
528
|
+
completeText(text) { if (!this.stopped) {
|
|
529
|
+
this.stopped = true;
|
|
530
|
+
this.resolve(text);
|
|
531
|
+
} }
|
|
532
|
+
fail() { if (!this.stopped) {
|
|
533
|
+
this.stopped = true;
|
|
534
|
+
this.reject(new Error('Fake workflow failed'));
|
|
535
|
+
} }
|
|
536
|
+
async cancel() { if (!this.stopped) {
|
|
537
|
+
this.stopped = true;
|
|
538
|
+
this.reject(new Error('Fake workflow cancelled'));
|
|
539
|
+
} }
|
|
540
|
+
ask(questions, askId = randomUUID()) {
|
|
541
|
+
this.enquiries.set(askId, questions);
|
|
542
|
+
this.emit({ type: 'enquiry', state: 'asked', askId, questions });
|
|
543
|
+
return askId;
|
|
544
|
+
}
|
|
545
|
+
requestPermission(tool, callId = randomUUID()) {
|
|
546
|
+
if (this.options.permissionMode === 'auto-accept')
|
|
547
|
+
return callId;
|
|
548
|
+
this.permissions.set(callId, tool);
|
|
549
|
+
this.emit({ type: 'permission', state: 'asked', callId, tool });
|
|
550
|
+
return callId;
|
|
551
|
+
}
|
|
552
|
+
async answerEnquiry(askId, answers) {
|
|
553
|
+
const questions = this.enquiries.get(askId);
|
|
554
|
+
if (this.stopped || !questions || answers.length !== questions.length)
|
|
555
|
+
return false;
|
|
556
|
+
this.enquiries.delete(askId);
|
|
557
|
+
this.answers.push({ askId, answers });
|
|
558
|
+
this.emit({ type: 'enquiry', state: 'answered', askId, questions, answers });
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
async answerPermission(callId, decision) {
|
|
562
|
+
const tool = this.permissions.get(callId);
|
|
563
|
+
if (this.stopped || !tool)
|
|
564
|
+
return false;
|
|
565
|
+
this.permissions.delete(callId);
|
|
566
|
+
this.decisions.push({ callId, decision });
|
|
567
|
+
this.emit({ type: 'permission', state: 'decided', callId, tool, decision });
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
}
|