@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,220 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { platform } from "node:os";
3
+ /**
4
+ * The Shells: pseudo-terminals the Session Host owns, opened beside an Agent Session and started in
5
+ * its Scope.
6
+ *
7
+ * A Shell is not an Agent Session and it is not served by one. It holds no Presentation Transcript,
8
+ * it is never Revived, and it does not survive a daemon restart — a process cannot. What it does
9
+ * hold is a Scrollback: a bounded ring of the bytes it most recently wrote, kept only so a client
10
+ * that closes the pane and reopens it does not arrive at a blank screen. That ring is lossy by
11
+ * design and is never written to disk, which is the whole distinction from a Presentation
12
+ * Transcript (ADR 0001).
13
+ *
14
+ * node-pty is imported lazily and its absence is reported rather than thrown. It is a native addon,
15
+ * so `npm run build:binary` cannot contain it — the same position `koffi` and the pi adapter are
16
+ * already in (scripts/build-binary.mjs). `available()` is what lets the web client hide the control
17
+ * instead of offering one that breaks, which is the rule Capabilities already encodes for backends.
18
+ */
19
+ /** Bytes of output kept per Shell for reattach. One screen of `ls -R` and change; not a history. */
20
+ const SCROLLBACK_LIMIT = 256 * 1024;
21
+ const DEFAULT_COLS = 80;
22
+ const DEFAULT_ROWS = 24;
23
+ export class ShellRegistry {
24
+ shells = new Map();
25
+ loadPty;
26
+ pty;
27
+ stopping = false;
28
+ ptyLoad;
29
+ constructor(options = {}) {
30
+ this.loadPty = options.loadPty ?? (() => import("node-pty"));
31
+ }
32
+ /**
33
+ * Whether this build can open a Shell at all.
34
+ *
35
+ * Resolved by actually loading node-pty rather than by guessing from `process.platform`, because
36
+ * the thing that fails is the addon load and nothing else predicts it.
37
+ */
38
+ async available() {
39
+ return (await this.load()) !== undefined;
40
+ }
41
+ async create(options) {
42
+ const pty = await this.load();
43
+ if (this.stopping)
44
+ throw new Error('Session Host is stopping');
45
+ if (!pty)
46
+ throw new Error("This build cannot open a Shell: node-pty is unavailable");
47
+ const cols = options.cols ?? DEFAULT_COLS;
48
+ const rows = options.rows ?? DEFAULT_ROWS;
49
+ const id = randomUUID();
50
+ const child = pty.spawn(shellCommand(), [], {
51
+ name: "xterm-256color",
52
+ cols,
53
+ rows,
54
+ cwd: options.cwd,
55
+ // TERM is set explicitly because the daemon may have been started by a launcher with no TERM
56
+ // at all, and a shell that inherits an empty one renders as a dumb terminal.
57
+ env: { ...stringEnv(), TERM: "xterm-256color" },
58
+ });
59
+ let resolveExit;
60
+ const exit = new Promise(resolve => { resolveExit = resolve; });
61
+ const record = {
62
+ summary: {
63
+ id,
64
+ sessionId: options.sessionId,
65
+ cwd: options.cwd,
66
+ createdAt: new Date().toISOString(),
67
+ },
68
+ pty: child,
69
+ scrollback: [],
70
+ scrollbackBytes: 0,
71
+ sinks: new Set(),
72
+ cols,
73
+ rows,
74
+ exited: false,
75
+ exit,
76
+ };
77
+ this.shells.set(id, record);
78
+ child.onData((data) => {
79
+ const chunk = Buffer.from(data, "utf8");
80
+ remember(record, chunk);
81
+ for (const sink of record.sinks)
82
+ sink.output(chunk);
83
+ });
84
+ child.onExit(({ exitCode, signal }) => {
85
+ record.exited = true;
86
+ resolveExit();
87
+ for (const sink of record.sinks)
88
+ sink.exit(exitCode, signal);
89
+ record.sinks.clear();
90
+ this.shells.delete(id);
91
+ });
92
+ return record.summary;
93
+ }
94
+ get(shellId) {
95
+ return this.shells.get(shellId)?.summary;
96
+ }
97
+ listFor(sessionId) {
98
+ return [...this.shells.values()]
99
+ .filter((record) => record.summary.sessionId === sessionId)
100
+ .map((record) => record.summary);
101
+ }
102
+ /**
103
+ * Watch a Shell, starting with its Scrollback.
104
+ *
105
+ * The replay is written to this sink alone rather than broadcast, so a second client attaching
106
+ * does not repaint the first one's screen. Returns the detach, which does not kill the Shell:
107
+ * closing the pane is hide, not kill.
108
+ */
109
+ attach(shellId, sink) {
110
+ const record = this.shells.get(shellId);
111
+ if (!record)
112
+ throw new Error(`No Shell ${shellId}`);
113
+ if (record.scrollbackBytes > 0)
114
+ sink.output(Buffer.concat(record.scrollback));
115
+ record.sinks.add(sink);
116
+ return () => record.sinks.delete(sink);
117
+ }
118
+ write(shellId, data) {
119
+ const record = this.shells.get(shellId);
120
+ if (!record || record.exited)
121
+ return;
122
+ record.pty.write(data.toString("utf8"));
123
+ }
124
+ /**
125
+ * Resize the pty.
126
+ *
127
+ * Last writer wins, deliberately. Two clients watching one Shell at different window sizes cannot
128
+ * both be satisfied — a pty has one size — and the alternative, taking the minimum, punishes the
129
+ * person actually typing for a tab someone left open.
130
+ */
131
+ resize(shellId, cols, rows) {
132
+ const record = this.shells.get(shellId);
133
+ if (!record || record.exited)
134
+ return;
135
+ if (cols <= 0 || rows <= 0)
136
+ return;
137
+ if (record.cols === cols && record.rows === rows)
138
+ return;
139
+ record.cols = cols;
140
+ record.rows = rows;
141
+ record.pty.resize(cols, rows);
142
+ }
143
+ kill(shellId) {
144
+ const record = this.shells.get(shellId);
145
+ if (!record)
146
+ return;
147
+ // Left in the map: onExit removes it, and does so after telling every sink why the screen
148
+ // stopped. Deleting here would strand watchers on a socket that never says anything again.
149
+ record.pty.kill();
150
+ }
151
+ /** Every Shell opened beside one Agent Session. The Settle, End and Reap path. */
152
+ killFor(sessionId) {
153
+ for (const record of this.shells.values()) {
154
+ if (record.summary.sessionId === sessionId)
155
+ record.pty.kill();
156
+ }
157
+ }
158
+ hasLiveShells() {
159
+ return [...this.shells.values()].some(shell => !shell.exited);
160
+ }
161
+ async killAll(timeoutMs = 1000) {
162
+ this.stopping = true;
163
+ const records = [...this.shells.values()];
164
+ const exited = Promise.all(records.map(record => record.exit));
165
+ for (const signal of ['SIGHUP', 'SIGKILL']) {
166
+ for (const record of records)
167
+ if (!record.exited)
168
+ record.pty.kill(signal);
169
+ let timer;
170
+ try {
171
+ await Promise.race([exited, new Promise(resolve => { timer = setTimeout(resolve, timeoutMs); })]);
172
+ }
173
+ finally {
174
+ clearTimeout(timer);
175
+ }
176
+ if (records.every(record => record.exited))
177
+ return;
178
+ }
179
+ throw new Error('Shell shutdown timed out; ownership retained');
180
+ }
181
+ async load() {
182
+ if (this.pty)
183
+ return this.pty;
184
+ // Memoised on the promise, not the result: two concurrent creates must not both import.
185
+ this.ptyLoad ??= this.loadPty()
186
+ .then((module) => {
187
+ this.pty = module;
188
+ return this.pty;
189
+ })
190
+ .catch(() => undefined);
191
+ return await this.ptyLoad;
192
+ }
193
+ }
194
+ function remember(record, chunk) {
195
+ record.scrollback.push(chunk);
196
+ record.scrollbackBytes += chunk.byteLength;
197
+ while (record.scrollbackBytes > SCROLLBACK_LIMIT && record.scrollback.length > 1) {
198
+ const dropped = record.scrollback.shift();
199
+ record.scrollbackBytes -= dropped?.byteLength ?? 0;
200
+ }
201
+ }
202
+ /**
203
+ * The reader's own shell. $SHELL is what they chose; the fallbacks are only for a daemon started
204
+ * without an environment.
205
+ */
206
+ function shellCommand() {
207
+ const configured = process.env["SHELL"];
208
+ if (configured)
209
+ return configured;
210
+ return platform() === "win32" ? "powershell.exe" : "/bin/sh";
211
+ }
212
+ /** process.env with the undefined values dropped, which is what node-pty's env type wants. */
213
+ function stringEnv() {
214
+ const env = {};
215
+ for (const [key, value] of Object.entries(process.env)) {
216
+ if (value !== undefined)
217
+ env[key] = value;
218
+ }
219
+ return env;
220
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Which Skills a Scope offers, asked without an Agent Session.
3
+ *
4
+ * The sibling of ./models.ts, and for the same reason: the New Agent Session view has to offer a
5
+ * Skill menu for a Scope nobody has created a session in yet, and `skills()` lives on
6
+ * `BackendSession` rather than on `AgentBackend` — there is no session-free path. So this opens a
7
+ * throwaway one, the same device the Summary Model uses (ADR 0020), tools and all off, and disposes
8
+ * of it.
9
+ *
10
+ * **What this costs: measured, and it is not what ./summariser.ts says.** That file's 24–44 seconds
11
+ * times `prompt → turn_ended`, a spawn *and* a full inference round-trip, and its remark about
12
+ * "thirty seconds before it can be asked anything" is true of the *model* path only. Reading a Skill
13
+ * list is a control request, which the CLI answers long before it is ready to be prompted:
14
+ * `ClaudeBackend.create` awaits nothing, and `supportedModels()` next door is documented as
15
+ * answering before the first prompt. **A cold Claude probe against a real CLI took 5.7 seconds**,
16
+ * comfortably under the nine that `PROBE_TIMEOUT_MS` in ./models.ts records for the model list. pi
17
+ * spawns no process at all, its `skills()` being a directory scan, and answers in milliseconds.
18
+ *
19
+ * **Nothing here is cached, which is the whole point.** A Skill directory changes between one
20
+ * keystroke and the next, which is why `BackendSession.skills` is "asked each time rather than
21
+ * cached" and why no transcript ever carries a copy. ADR 0020 already drew this line: `/api/models`
22
+ * is memoised for the daemon's life because entitlements change when an account does, while
23
+ * `/api/branches` beside it is asked fresh because its answer changes outside Flow. Skills are on the
24
+ * `/api/branches` side of that line. The nine seconds is hidden by asking early — the client probes
25
+ * when its Scope field settles, so the spawn overlaps with somebody choosing a backend and typing a
26
+ * first message — rather than by holding an answer that has stopped being true.
27
+ */
28
+ /**
29
+ * A backend that has not answered in this long is not going to.
30
+ *
31
+ * The same budget as ./models.ts and for the same reasons, which is why it is stated there rather
32
+ * than argued again here.
33
+ */
34
+ const PROBE_TIMEOUT_MS = 45_000;
35
+ /**
36
+ * Ask one Backend Adapter which Skills a Scope offers.
37
+ *
38
+ * Never throws, exactly as `probeModels` never throws and `SessionHost.listSkills` never refuses: a
39
+ * backend that cannot answer reports a `problem` and the menu says so. A refusal here must not be
40
+ * able to take down the view that asked.
41
+ */
42
+ export async function probeSkills(backend, scope) {
43
+ const failure = (problem) => ({
44
+ backend: backend.name,
45
+ scope,
46
+ skills: [],
47
+ problem,
48
+ });
49
+ /*
50
+ * `create` is raced, which is the one real departure from `probeModels`.
51
+ *
52
+ * That one leaves `create` unbounded because a stuck probe only makes a Settings page slow. Here
53
+ * the host holds this promise so concurrent askers share one spawn, so a `create` that never
54
+ * resolves would hold every later ask for that Scope behind it. The loser of the race still
55
+ * disposes whatever eventually arrives rather than orphaning a CLI — hence the `then` below and
56
+ * not a dropped reference.
57
+ */
58
+ const started = backend.create({ scope, tools: "none", emit: () => { } });
59
+ const session = await within(started, PROBE_TIMEOUT_MS);
60
+ if (session === undefined) {
61
+ void started.then((late) => void late.dispose().catch(() => { }), () => { });
62
+ return failure(`${backend.name} did not start in time`);
63
+ }
64
+ if (session instanceof Error)
65
+ return failure(message(session));
66
+ try {
67
+ /*
68
+ * An adapter whose session has no notion of Skills is a `problem` rather than an empty list, for
69
+ * the reason `ScopeSkills` exists: "this cannot tell you" and "there are none here" are different
70
+ * answers, and only one of them means the reader should stop looking.
71
+ */
72
+ if (!session.skills)
73
+ return failure(`${backend.name} has no notion of Skills`);
74
+ return { backend: backend.name, scope, skills: await session.skills() };
75
+ }
76
+ catch (error) {
77
+ return failure(message(error));
78
+ }
79
+ finally {
80
+ await session.dispose().catch(() => { });
81
+ }
82
+ }
83
+ function message(error) {
84
+ return error instanceof Error ? error.message : String(error);
85
+ }
86
+ /**
87
+ * The session, an `Error` it rejected with, or `undefined` if it took too long.
88
+ *
89
+ * Three outcomes rather than two because all three are different answers to a reader: a backend that
90
+ * refused can say why, and one that hung can only say that it hung. Clears the timer on the way out
91
+ * either way — see `within` in ./summariser.ts.
92
+ */
93
+ function within(work, ms) {
94
+ return new Promise((resolve) => {
95
+ const timer = setTimeout(() => resolve(undefined), ms);
96
+ void work.then((session) => {
97
+ clearTimeout(timer);
98
+ resolve(session);
99
+ }, (error) => {
100
+ clearTimeout(timer);
101
+ resolve(error instanceof Error ? error : new Error(String(error)));
102
+ });
103
+ });
104
+ }
@@ -0,0 +1,24 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { resolve } from "node:path";
3
+ import { run, GIT_READ_TIMEOUT_MS } from "./git.js";
4
+ export async function localStacks(scope) {
5
+ const path = await run(scope, ["rev-parse", "--git-path", "gh-stack"], GIT_READ_TIMEOUT_MS);
6
+ if (!path.ok)
7
+ throw new Error(path.failure.message);
8
+ let raw;
9
+ try {
10
+ raw = await readFile(resolve(scope, path.value.trim()), "utf8");
11
+ }
12
+ catch (error) {
13
+ if (error.code === "ENOENT")
14
+ return [];
15
+ throw error;
16
+ }
17
+ const data = JSON.parse(raw);
18
+ if (data.schemaVersion !== 1 || !Array.isArray(data.stacks) || data.stacks.some(s => !s.trunk || typeof s.trunk.branch !== "string" || !Array.isArray(s.branches) || s.branches.some(b => typeof b.branch !== "string")))
19
+ throw new Error("Invalid or unsupported gh stack metadata. Repair it with gh stack before publishing or syncing.");
20
+ return data.stacks;
21
+ }
22
+ export async function localStack(scope, branch) {
23
+ return (await localStacks(scope)).find(s => s.branches.some(b => b.branch === branch));
24
+ }