@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.
Files changed (141) hide show
  1. package/README.md +641 -0
  2. package/build/workflow-runtime.cjs +213964 -0
  3. package/dist/backend/claude/async-queue.js +44 -0
  4. package/dist/backend/claude/auto-compaction.js +10 -0
  5. package/dist/backend/claude/background-calls.js +116 -0
  6. package/dist/backend/claude/enquiries.js +162 -0
  7. package/dist/backend/claude/index.js +1138 -0
  8. package/dist/backend/claude/mcp.js +55 -0
  9. package/dist/backend/claude/permissions.js +106 -0
  10. package/dist/backend/claude/streamed-message.js +120 -0
  11. package/dist/backend/claude/subagents.js +128 -0
  12. package/dist/backend/claude/workflow-parent.js +12 -0
  13. package/dist/backend/claude/workflow-processes.js +95 -0
  14. package/dist/backend/claude/workflow-subagent.js +234 -0
  15. package/dist/backend/effort.js +30 -0
  16. package/dist/backend/fake/index.js +570 -0
  17. package/dist/backend/mcp.js +190 -0
  18. package/dist/backend/pi/auto-compaction.js +16 -0
  19. package/dist/backend/pi/background-calls.js +56 -0
  20. package/dist/backend/pi/enquiries.js +63 -0
  21. package/dist/backend/pi/index.js +597 -0
  22. package/dist/backend/pi/mcp.js +35 -0
  23. package/dist/backend/pi/subagents.js +23 -0
  24. package/dist/backend/pi/work.js +104 -0
  25. package/dist/backend/pi/workflow-parent.js +21 -0
  26. package/dist/backend/pi/workflow-subagent.js +199 -0
  27. package/dist/backend/registry.js +34 -0
  28. package/dist/backend/types.js +1 -0
  29. package/dist/backend/workflow-tools.js +25 -0
  30. package/dist/build-id +1 -0
  31. package/dist/cli/application.js +397 -0
  32. package/dist/cli/bootstrap.js +535 -0
  33. package/dist/cli/host-control.js +100 -0
  34. package/dist/cli/install-guard.js +270 -0
  35. package/dist/cli/install-process.js +52 -0
  36. package/dist/cli/main.js +12 -0
  37. package/dist/cli/update.js +128 -0
  38. package/dist/client/connection.js +183 -0
  39. package/dist/client/context-usage.js +66 -0
  40. package/dist/client/diff.js +36 -0
  41. package/dist/client/enquiry.js +105 -0
  42. package/dist/client/markdown.js +192 -0
  43. package/dist/client/model-choices.js +32 -0
  44. package/dist/client/permission.js +60 -0
  45. package/dist/client/project-name.js +19 -0
  46. package/dist/client/reduce.js +463 -0
  47. package/dist/client/relative-time.js +30 -0
  48. package/dist/client/scope-kind.js +28 -0
  49. package/dist/client/search.js +136 -0
  50. package/dist/client/session-label.js +9 -0
  51. package/dist/client/status.js +84 -0
  52. package/dist/client/tool-chains.js +52 -0
  53. package/dist/client/tool-summary.js +98 -0
  54. package/dist/daemon/auth.js +590 -0
  55. package/dist/daemon/config-store.js +183 -0
  56. package/dist/daemon/config.js +687 -0
  57. package/dist/daemon/credential-redaction.js +9 -0
  58. package/dist/daemon/git.js +312 -0
  59. package/dist/daemon/host-control-routes.js +29 -0
  60. package/dist/daemon/host.js +2343 -0
  61. package/dist/daemon/http.js +19 -0
  62. package/dist/daemon/log.js +44 -0
  63. package/dist/daemon/mcp-auth.js +164 -0
  64. package/dist/daemon/models.js +85 -0
  65. package/dist/daemon/ownership.js +104 -0
  66. package/dist/daemon/projects.js +317 -0
  67. package/dist/daemon/publish.js +289 -0
  68. package/dist/daemon/pull-request.js +198 -0
  69. package/dist/daemon/runtime.js +100 -0
  70. package/dist/daemon/secret-store.js +65 -0
  71. package/dist/daemon/server.js +827 -0
  72. package/dist/daemon/shell.js +220 -0
  73. package/dist/daemon/skills.js +104 -0
  74. package/dist/daemon/stack-metadata.js +24 -0
  75. package/dist/daemon/stack.js +743 -0
  76. package/dist/daemon/store.js +130 -0
  77. package/dist/daemon/summariser.js +476 -0
  78. package/dist/daemon/workflow-execution-routes.js +105 -0
  79. package/dist/daemon/workflow-executions.js +1018 -0
  80. package/dist/daemon/workflow-mcp.js +20 -0
  81. package/dist/daemon/workflow-routes.js +102 -0
  82. package/dist/protocol/attachments.js +59 -0
  83. package/dist/protocol/commands.js +1 -0
  84. package/dist/protocol/events.js +20 -0
  85. package/dist/protocol/fonts.js +26 -0
  86. package/dist/protocol/git.js +12 -0
  87. package/dist/protocol/mcp.js +51 -0
  88. package/dist/protocol/projects.js +21 -0
  89. package/dist/protocol/publish.js +1 -0
  90. package/dist/protocol/pull-request.js +1 -0
  91. package/dist/protocol/secrets.js +4 -0
  92. package/dist/protocol/settings.js +63 -0
  93. package/dist/protocol/shells.js +8 -0
  94. package/dist/protocol/skills.js +16 -0
  95. package/dist/protocol/stack.js +41 -0
  96. package/dist/protocol/workflow-executions.js +1 -0
  97. package/dist/protocol/workflows.js +1 -0
  98. package/dist/tui/app.js +518 -0
  99. package/dist/tui/keys.js +54 -0
  100. package/dist/tui/render.js +366 -0
  101. package/dist/web/assets.js +10 -0
  102. package/dist/web/embedded.js +12 -0
  103. package/dist/web/manifest.js +70 -0
  104. package/dist/workflows/decisions.js +65 -0
  105. package/dist/workflows/executors.js +132 -0
  106. package/dist/workflows/graph.js +276 -0
  107. package/dist/workflows/json-schema.js +49 -0
  108. package/dist/workflows/loops.js +102 -0
  109. package/dist/workflows/mcp.js +18 -0
  110. package/dist/workflows/records.js +111 -0
  111. package/dist/workflows/recovery.js +25 -0
  112. package/dist/workflows/runtime-asset.js +17 -0
  113. package/dist/workflows/runtime-libs.js +1 -0
  114. package/dist/workflows/runtime-settings.js +23 -0
  115. package/dist/workflows/runtime.js +343 -0
  116. package/dist/workflows/scheduler.js +573 -0
  117. package/dist/workflows/schema.js +161 -0
  118. package/dist/workflows/store.js +93 -0
  119. package/package.json +87 -0
  120. package/web/dist/assets/__vite-browser-external-2447137e-BvRk9kiK.js +0 -0
  121. package/web/dist/assets/ghostty-vt-DOMeXDrv.wasm +0 -0
  122. package/web/dist/assets/index-BC0gLS53.js +106 -0
  123. package/web/dist/assets/index-CCeZYKES.css +2 -0
  124. package/web/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
  125. package/web/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
  126. package/web/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
  127. package/web/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
  128. package/web/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
  129. package/web/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
  130. package/web/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
  131. package/web/dist/assets/rolldown-runtime-B0Z9INg1.js +1 -0
  132. package/web/dist/assets/select-6i3wxovH.js +1 -0
  133. package/web/dist/assets/settings-mcp-CcT0ADJR.js +1 -0
  134. package/web/dist/assets/settings-secrets-CK4uF7fd.js +1 -0
  135. package/web/dist/assets/settings-workflows-CrbZF-nM.js +23 -0
  136. package/web/dist/assets/switch-ftZleVB8.js +6 -0
  137. package/web/dist/assets/workflow-editors-naPqAmYO.js +65 -0
  138. package/web/dist/assets/workflows-D4T4Gy_7.css +1 -0
  139. package/web/dist/assets/workflows-hh6ioeEA.js +7 -0
  140. package/web/dist/assets/workflows-pane-Db-MHf7Z.js +1 -0
  141. package/web/dist/index.html +31 -0
@@ -0,0 +1,44 @@
1
+ /**
2
+ * An async iterable that stays open until explicitly closed.
3
+ *
4
+ * The Claude Agent SDK's `query()` is a run, not a session: it consumes an iterable of user
5
+ * messages and finishes when that iterable does. Feeding it a queue that never ends on its own is
6
+ * what turns a run into a long-lived Backend Session we can keep prompting.
7
+ */
8
+ export class AsyncQueue {
9
+ items = [];
10
+ waiting = [];
11
+ closed = false;
12
+ push(item) {
13
+ if (this.closed)
14
+ return;
15
+ const waiter = this.waiting.shift();
16
+ if (waiter)
17
+ waiter({ value: item, done: false });
18
+ else
19
+ this.items.push(item);
20
+ }
21
+ close() {
22
+ if (this.closed)
23
+ return;
24
+ this.closed = true;
25
+ while (this.waiting.length > 0) {
26
+ this.waiting.shift()?.({ value: undefined, done: true });
27
+ }
28
+ }
29
+ async *[Symbol.asyncIterator]() {
30
+ while (true) {
31
+ const item = this.items.shift();
32
+ if (item !== undefined) {
33
+ yield item;
34
+ continue;
35
+ }
36
+ if (this.closed)
37
+ return;
38
+ const next = await new Promise((resolve) => this.waiting.push(resolve));
39
+ if (next.done)
40
+ return;
41
+ yield next.value;
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,10 @@
1
+ export function claudeAutoCompactionEnv(setting, inherited = process.env) {
2
+ if (!setting)
3
+ return undefined;
4
+ const env = { ...inherited };
5
+ delete env.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE;
6
+ env.DISABLE_AUTO_COMPACT = setting.mode === "disabled" ? "1" : "0";
7
+ if (setting.mode === "enabled")
8
+ env.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE = String(setting.targetPercent);
9
+ return env;
10
+ }
@@ -0,0 +1,116 @@
1
+ /**
2
+ * The tool calls this turn has made, and the ones the CLI is running past it.
3
+ *
4
+ * Its own module rather than fields on ClaudeSession for the reason `Subagents` gives: that session
5
+ * cannot be constructed without spawning a Claude process, so none of this would be reachable from
6
+ * a test there. Apart from `Subagents` rather than inside it because `Subagents.noteTask` refusing a
7
+ * `callId` it does not know is what keeps a backgrounded `Bash` out of Subagent bookkeeping without
8
+ * a list of task types to maintain (ADR 0016) — and that refusal is only a boundary while there are
9
+ * two objects for it to be between. `Subagents` is unedited by this feature, which is the evidence.
10
+ *
11
+ * **It holds nothing, and there is nowhere to put a hold.** A Background Call is not occupancy
12
+ * (ADR 0016): the model that made it is Idle and the Steering Queue must stay live. `Subagents.hold`
13
+ * has no counterpart here by construction rather than by discipline, so there is no field for a
14
+ * later edit to start holding a turn with.
15
+ */
16
+ export class BackgroundCalls {
17
+ /**
18
+ * Every tool call this turn announced, by id, so `task_started` can be told what it started.
19
+ *
20
+ * Every call rather than the ones we recognise, which is what makes detection structural: the name
21
+ * is only knowable from the `tool_use` block, and recording only names we expect to background is
22
+ * the tool-name allowlist ADR 0021 refuses.
23
+ *
24
+ * Nothing prunes a foreground call from here as its result arrives, because nothing could promote
25
+ * one afterwards — a `task_started` names the callId it started, so a call that never gets one is
26
+ * never reachable. `clear()` at the turn end is the whole of the bound, and one turn's calls is
27
+ * the whole of the cost.
28
+ */
29
+ pending = new Map();
30
+ /** Launched and unsettled. Outlives its turn; dies with its Backend Session. */
31
+ open = new Map();
32
+ /**
33
+ * `task_id` to `callId`.
34
+ *
35
+ * Not a convenience: `task_updated` is the message that reports a Call killed rather than
36
+ * finished, and it carries no `tool_use_id` at all, so this map is the only way back to the id
37
+ * space (ADR 0015).
38
+ */
39
+ tasks = new Map();
40
+ /** Remember a tool call, in case the CLI turns out to be running it in the background. */
41
+ called(callId, brief) {
42
+ this.pending.set(callId, brief);
43
+ }
44
+ /**
45
+ * Promote an announced call to an open Background Call, and answer with what it is.
46
+ *
47
+ * Undefined for a call this never saw announced — which gets no card, rather than one named after
48
+ * a guess — and for one already open, so the CLI repeating `task_started` cannot emit a second
49
+ * launch snapshot or double-count the Call.
50
+ */
51
+ launch(callId) {
52
+ const brief = this.pending.get(callId);
53
+ if (brief === undefined)
54
+ return undefined;
55
+ this.pending.delete(callId);
56
+ this.open.set(callId, brief);
57
+ return brief;
58
+ }
59
+ /** What an open Background Call is, or undefined for a call that is not one. */
60
+ describe(callId) {
61
+ return this.open.get(callId);
62
+ }
63
+ /**
64
+ * Remember which call a task id belongs to. Refuses a call this has never heard of.
65
+ *
66
+ * Accepts one still only announced as well as one already open, because `task_started` arrives
67
+ * *before* the launching `tool_result` — captured at 9.2s against 9.3s in
68
+ * `spikes/background-call-messages.ts` — so at the moment this is called the call is usually
69
+ * `pending` and not yet `open`.
70
+ */
71
+ noteTask(taskId, callId) {
72
+ if (!this.pending.has(callId) && !this.open.has(callId))
73
+ return;
74
+ this.tasks.set(taskId, callId);
75
+ }
76
+ /** The Background Call a settled task is about, or undefined when the task is not one's. */
77
+ callIdOf(taskId) {
78
+ return this.tasks.get(taskId);
79
+ }
80
+ /**
81
+ * Record that a Background Call settled, answering with what it was so the snapshot can name it.
82
+ *
83
+ * Undefined the second time, which is load-bearing rather than defensive: a Call settles through
84
+ * `task_updated` *and* `task_notification`, both arriving in the same tick with the same outcome
85
+ * (captured in the spike), and a caller that emitted a terminal snapshot for each would write two.
86
+ */
87
+ settled(callId) {
88
+ const brief = this.open.get(callId);
89
+ if (brief === undefined)
90
+ return undefined;
91
+ this.forgetTasks(callId);
92
+ this.open.delete(callId);
93
+ return brief;
94
+ }
95
+ /**
96
+ * Forget this turn's announced calls.
97
+ *
98
+ * Open Background Calls and their task ids deliberately survive: one outlives the turn that made
99
+ * it, and the snapshot closing it is emitted turns later from the brief kept here.
100
+ */
101
+ clear() {
102
+ this.pending.clear();
103
+ }
104
+ /** Forget open Calls too, for a Backend Session being taken away. They are children of it. */
105
+ abandon() {
106
+ this.clear();
107
+ this.open.clear();
108
+ this.tasks.clear();
109
+ }
110
+ forgetTasks(callId) {
111
+ for (const [taskId, owner] of this.tasks) {
112
+ if (owner === callId)
113
+ this.tasks.delete(taskId);
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,162 @@
1
+ /** The tool whose call asks a human something. Named once, because two spellings is one bug. */
2
+ export const ASK_TOOL = "AskUserQuestion";
3
+ /**
4
+ * The Enquiries open in one turn, and the permission callbacks held for them.
5
+ *
6
+ * The Claude SDK asks whether a tool may run through `canUseTool`, and does not continue until that
7
+ * promise settles. For every other tool the adapter answers immediately; for this one the answer is
8
+ * a human's, which may be minutes away or may never come. So a promise is parked here, and the whole
9
+ * of this module's job is that **every path settles it**.
10
+ *
11
+ * That is the one rule, and it is why nothing here rejects. A rejected callback leaves the CLI with
12
+ * a `tool_use` it never resolved: the turn cannot end, the Session Host never clears `turnInFlight`,
13
+ * and the Agent Session is pinned in `running` with no key that unpins it. A *denial* is a real
14
+ * `tool_result` — `spikes/ask-user-question.ts` confirmed it comes back well-formed, carrying
15
+ * `is_error` against the right id — so the conversation stays complete and a later Revive resumes
16
+ * onto a turn with nothing dangling.
17
+ *
18
+ * Its own module rather than fields on ClaudeSession because that session cannot be constructed
19
+ * without spawning a Claude process, so none of this would be reachable from a test there — the same
20
+ * reason `Subagents` and `StreamedMessage` live apart.
21
+ */
22
+ export class PendingEnquiries {
23
+ open = new Map();
24
+ /** Park a callback against the call that asked. `askId` is the SDK's `toolUseID`. */
25
+ hold(askId, questions, input, settle) {
26
+ this.open.set(askId, { questions, input, settle });
27
+ }
28
+ /** What an open Enquiry asked, or undefined for one already settled. */
29
+ describe(askId) {
30
+ return this.open.get(askId)?.questions;
31
+ }
32
+ /**
33
+ * Settle an open Enquiry with a human's answers. False when there was nothing open under this id,
34
+ * which is an ordinary race — a second click, or an answer typed against a transcript older than
35
+ * the Backend Session serving it.
36
+ */
37
+ answer(askId, answers) {
38
+ const pending = this.open.get(askId);
39
+ if (!pending)
40
+ return false;
41
+ this.open.delete(askId);
42
+ pending.settle({ behavior: "allow", updatedInput: answersFor(pending.input, answers) });
43
+ return true;
44
+ }
45
+ /**
46
+ * Settle an open Enquiry as unanswered, denying the tool with something the model can act on.
47
+ * Answers with what it was asking, so the caller can emit the terminal snapshot for it.
48
+ */
49
+ abandon(askId, why) {
50
+ const pending = this.open.get(askId);
51
+ if (!pending)
52
+ return undefined;
53
+ this.open.delete(askId);
54
+ pending.settle({ behavior: "deny", message: denial(why) });
55
+ return pending.questions;
56
+ }
57
+ /** Abandon everything open, and say what was abandoned so each gets its terminal snapshot. */
58
+ abandonAll(why) {
59
+ return [...this.open.keys()].flatMap((askId) => {
60
+ const questions = this.abandon(askId, why);
61
+ return questions ? [{ askId, questions }] : [];
62
+ });
63
+ }
64
+ /**
65
+ * Forget everything without settling anything.
66
+ *
67
+ * Beside `Subagents.clear()` and for the same reason — nothing may reach the turn after this one —
68
+ * but unlike it, this is **not** how a turn ends. Anything still open at that point must be
69
+ * abandoned first, or its callback is dropped and the CLI waits forever. `clear()` is for when the
70
+ * callbacks are already gone: the process behind them is down.
71
+ */
72
+ clear() {
73
+ this.open.clear();
74
+ }
75
+ }
76
+ /**
77
+ * The tool input with the human's answers folded into it, which is what an allowed call hands back.
78
+ *
79
+ * `answers` is keyed by the question's own text, and takes a bare string or an array of them — both
80
+ * confirmed against the CLI in `spikes/ask-user-question.ts`, where an array came back comma-joined
81
+ * in the tool result. A single-select therefore sends a string rather than a one-element array, so
82
+ * what the model reads is what a human would write.
83
+ *
84
+ * The rest of `input` is carried through untouched. The SDK replaces the call's arguments with this,
85
+ * so anything dropped here is dropped from the call.
86
+ */
87
+ export function answersFor(input, answers) {
88
+ const questions = questionsOf(input);
89
+ const keyed = {};
90
+ questions.forEach((question, index) => {
91
+ const chosen = answers[index] ?? [];
92
+ // Last-wins on two questions with identical text, which is all a text-keyed map can express.
93
+ // The alternative is dropping one, and an answer the human gave is worse to lose than to move.
94
+ keyed[question.question] = question.multiSelect ? chosen : (chosen[0] ?? "");
95
+ });
96
+ return { ...input, answers: keyed };
97
+ }
98
+ /**
99
+ * The Questions a tool call is posing, read leniently.
100
+ *
101
+ * Lenient on purpose, and in one specific direction: it does not enforce the schema's one-to-four
102
+ * Questions or two-to-four Options. Refusing a shape the CLI itself produced would stall a turn over
103
+ * a disagreement about a document, so this carries whatever arrived and lets a front-end render it.
104
+ *
105
+ * Everything is narrowed rather than trusted — `canUseTool` hands over `Record<string, unknown>`, and
106
+ * there is no `AskUserQuestionInput` type in the SDK to lean on. A Question with no text at all is
107
+ * dropped, because a picker cannot show one and the human would be answering a blank.
108
+ */
109
+ export function questionsOf(input) {
110
+ if (!input || typeof input !== "object")
111
+ return [];
112
+ const raw = input.questions;
113
+ if (!Array.isArray(raw))
114
+ return [];
115
+ return raw.flatMap((entry) => {
116
+ if (!entry || typeof entry !== "object")
117
+ return [];
118
+ const record = entry;
119
+ const question = typeof record.question === "string" ? record.question : "";
120
+ if (question === "")
121
+ return [];
122
+ return [
123
+ {
124
+ // The header is a label, and falling back to the question keeps a progress row printable.
125
+ header: typeof record.header === "string" && record.header !== "" ? record.header : question,
126
+ question,
127
+ multiSelect: record.multiSelect === true,
128
+ options: optionsOf(record.options),
129
+ },
130
+ ];
131
+ });
132
+ }
133
+ function optionsOf(raw) {
134
+ if (!Array.isArray(raw))
135
+ return [];
136
+ return raw.flatMap((entry) => {
137
+ if (!entry || typeof entry !== "object")
138
+ return [];
139
+ const record = entry;
140
+ // The label is the option's identity — it is what is sent back as the answer — so one without a
141
+ // label is not a choice anybody could make, and is dropped rather than rendered blank.
142
+ if (typeof record.label !== "string" || record.label === "")
143
+ return [];
144
+ return [
145
+ {
146
+ label: record.label,
147
+ ...(typeof record.description === "string" ? { description: record.description } : {}),
148
+ ...(typeof record.preview === "string" ? { preview: record.preview } : {}),
149
+ },
150
+ ];
151
+ });
152
+ }
153
+ /**
154
+ * What the model is told when nobody answered.
155
+ *
156
+ * Ends the way the adapter's existing refusal does — "Continue without it" — because the model is
157
+ * not being corrected, it is being unblocked, and a turn that stops here has failed for the human
158
+ * twice over.
159
+ */
160
+ function denial(why) {
161
+ return `The human did not answer: ${why}. Continue without it.`;
162
+ }