@nathanstephenson/flow 0.1.10-beta.4 → 0.1.10-beta.6

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 (101) hide show
  1. package/README.md +2 -2
  2. package/build/backend-worker-chunks/{chunk-JOI2POLP.mjs → chunk-U73G6KK4.mjs} +18 -0
  3. package/build/backend-worker-chunks/{claude-GY27HBAY.mjs → claude-THGSOTC2.mjs} +7 -1
  4. package/build/backend-worker-chunks/{pi-JV6MWPW7.mjs → pi-5LAWIJNE.mjs} +82 -3
  5. package/build/backend-worker.mjs +7 -3
  6. package/build/workflow-runtime.cjs +12 -12
  7. package/dist/backend/claude/index.js +6 -0
  8. package/dist/backend/claude/workflow-parent.js +1 -1
  9. package/dist/backend/fake/index.js +6 -0
  10. package/dist/backend/pi/index.js +34 -2
  11. package/dist/backend/pi/model-auth.js +2 -2
  12. package/dist/backend/pi/plan-limits.js +41 -0
  13. package/dist/backend/pi/workflow-parent.js +1 -1
  14. package/dist/backend/plan-limits.js +17 -0
  15. package/dist/backend/worker/entry.js +4 -1
  16. package/dist/backend/worker/index.js +9 -3
  17. package/dist/backend/worker/launcher.js +11 -4
  18. package/dist/backend/worker/protocol.js +2 -1
  19. package/dist/build-id +1 -1
  20. package/dist/cli/application.js +5 -5
  21. package/dist/cli/bootstrap.js +1 -1
  22. package/dist/cli/host-control.js +1 -1
  23. package/dist/cli/install-guard.js +1 -1
  24. package/dist/cli/main.js +4 -4
  25. package/dist/cli/systemd-update.js +3 -3
  26. package/dist/cli/update.js +6 -6
  27. package/dist/cli/web-update.js +6 -6
  28. package/dist/client/context-usage.js +1 -1
  29. package/dist/client/markdown.js +3 -2
  30. package/dist/daemon/changes.js +116 -0
  31. package/dist/daemon/commit.js +84 -0
  32. package/dist/daemon/config-store.js +4 -3
  33. package/dist/daemon/config.js +31 -2
  34. package/dist/daemon/git-state.js +197 -0
  35. package/dist/daemon/github-image.js +18 -0
  36. package/dist/daemon/github.js +54 -0
  37. package/dist/daemon/host-control-routes.js +1 -1
  38. package/dist/daemon/host.js +280 -81
  39. package/dist/daemon/log.js +3 -0
  40. package/dist/daemon/mcp-auth.js +1 -1
  41. package/dist/daemon/pi-model-cache.js +1 -1
  42. package/dist/daemon/pull-request.js +156 -129
  43. package/dist/daemon/push.js +43 -0
  44. package/dist/daemon/repository.js +106 -0
  45. package/dist/daemon/runtime.js +28 -21
  46. package/dist/daemon/secret-store.js +1 -1
  47. package/dist/daemon/server.js +37 -10
  48. package/dist/daemon/skills.js +24 -0
  49. package/dist/daemon/spend-ledger.js +118 -0
  50. package/dist/daemon/stack-metadata.js +12 -5
  51. package/dist/daemon/stack.js +127 -656
  52. package/dist/daemon/summariser.js +30 -12
  53. package/dist/daemon/tour.js +42 -0
  54. package/dist/daemon/workflow-authoring-scope-checks.js +1 -1
  55. package/dist/daemon/workflow-authoring-scope.js +1 -1
  56. package/dist/daemon/workflow-builder-files.js +2 -2
  57. package/dist/daemon/workflow-builder-routes.js +2 -2
  58. package/dist/daemon/workflow-builder.js +8 -6
  59. package/dist/daemon/workflow-environment.js +5 -5
  60. package/dist/daemon/workflow-execution-launch.js +5 -5
  61. package/dist/daemon/workflow-execution-resources.js +4 -4
  62. package/dist/daemon/workflow-execution-routes.js +3 -3
  63. package/dist/daemon/workflow-execution-values.js +5 -5
  64. package/dist/daemon/workflow-executions.js +19 -19
  65. package/dist/daemon/workflow-mcp-authoring-routes.js +2 -2
  66. package/dist/daemon/workflow-mcp-authoring.js +4 -4
  67. package/dist/daemon/workflow-mcp.js +1 -1
  68. package/dist/daemon/workflow-routes.js +22 -3
  69. package/dist/daemon/workflow-schedule-driver.js +8 -8
  70. package/dist/daemon/workflow-schedule-routes.js +5 -5
  71. package/dist/daemon/workflow-schedules.js +9 -8
  72. package/dist/isolation/toolchain-assets.js +1 -1
  73. package/dist/protocol/spend.js +2 -0
  74. package/dist/protocol/stack.js +1 -41
  75. package/dist/workflows/calls.js +2 -2
  76. package/dist/workflows/executors.js +3 -3
  77. package/dist/workflows/graph.js +6 -6
  78. package/dist/workflows/invocations.js +1 -1
  79. package/dist/workflows/mcp.js +1 -1
  80. package/dist/workflows/records.js +6 -6
  81. package/dist/workflows/recovery.js +2 -2
  82. package/dist/workflows/runtime-settings.js +1 -1
  83. package/dist/workflows/runtime.js +2 -2
  84. package/dist/workflows/schedule-store.js +2 -2
  85. package/dist/workflows/scheduler.js +35 -23
  86. package/dist/workflows/schema.js +1 -1
  87. package/dist/workflows/store.js +19 -5
  88. package/package.json +3 -2
  89. package/web/dist/assets/index-BhfJhrVJ.js +51 -0
  90. package/web/dist/assets/index-BvkM2cWL.css +2 -0
  91. package/web/dist/assets/{settings-mcp-bcNKV147.js → settings-mcp-3cgqQB28.js} +1 -1
  92. package/web/dist/assets/settings-workflows-Cutm8isB.js +23 -0
  93. package/web/dist/assets/{switch-DIogWclb.js → switch-B00FEUA7.js} +1 -1
  94. package/web/dist/assets/{workflows-DHG-CIo3.js → workflows-D6oEUUhj.js} +31 -31
  95. package/web/dist/assets/{workflows-pane-9u_hJvgQ.js → workflows-pane-KIdSDKJB.js} +1 -1
  96. package/web/dist/index.html +4 -4
  97. package/dist/daemon/publish.js +0 -289
  98. package/web/dist/assets/index-BWKZK5HD.css +0 -2
  99. package/web/dist/assets/index-nIc09tKZ.js +0 -40
  100. package/web/dist/assets/settings-workflows-C_TKOhVa.js +0 -23
  101. /package/dist/protocol/{publish.js → plan-limits.js} +0 -0
@@ -0,0 +1,197 @@
1
+ import { createHash } from "node:crypto";
2
+ import { lstat } from "node:fs/promises";
3
+ import { resolve } from "node:path";
4
+ import { head, isRepository, run, GIT_READ_TIMEOUT_MS } from "./git.js";
5
+ import { gh } from "./github.js";
6
+ import { localRepository, releaseRepositories, resolvePullRequest, resolveRepository } from "./repository.js";
7
+ const READ_INTERVAL_MS = 3_000;
8
+ const WATCHED_POLL_MS = 30_000;
9
+ const UNWATCHED_POLL_MS = 5 * 60_000;
10
+ const POLL_JITTER_MS = 5_000;
11
+ const pathOffsets = { "1": 8, u: 10, "?": 1 };
12
+ export async function readStatus(scope) {
13
+ const status = await run(scope, ["status", "--porcelain=v2", "--branch", "-z", "--untracked-files=all", "--no-renames"], GIT_READ_TIMEOUT_MS);
14
+ if (!status.ok)
15
+ throw new Error(status.failure.message.includes("ENOENT") ? "Git is missing. Install git on the Session Host." : status.failure.message);
16
+ const records = status.value.split("\0").filter(Boolean);
17
+ const revision = createHash("sha256");
18
+ const fingerprint = createHash("sha256");
19
+ const state = { changed: 0, ahead: 0, behind: 0, upstream: false };
20
+ const files = [];
21
+ for (const record of records) {
22
+ revision.update(`${record}\0`);
23
+ if (!/^# branch\.(head|upstream|ab) /.test(record))
24
+ fingerprint.update(`${record}\0`);
25
+ const counts = /^# branch\.ab \+(\d+) -(\d+)$/.exec(record);
26
+ if (counts)
27
+ Object.assign(state, { upstream: true, ahead: Number(counts[1]), behind: Number(counts[2]) });
28
+ const branch = /^# branch\.head (.+)$/.exec(record)?.[1];
29
+ if (branch && branch !== "(detached)")
30
+ state.branch = branch;
31
+ const oid = /^# branch\.oid ([0-9a-f]+)$/.exec(record)?.[1];
32
+ if (oid)
33
+ state.head = oid;
34
+ const offset = pathOffsets[record[0] ?? ""];
35
+ if (offset === undefined || record[1] !== " ")
36
+ continue;
37
+ const path = record.split(" ").slice(offset).join(" ");
38
+ state.changed += 1;
39
+ files.push({ status: record[0] === "?" ? "??" : record.slice(2, 4).replaceAll(".", " "), path });
40
+ const stat = await lstat(resolve(scope, path)).catch(() => undefined);
41
+ revision.update(`\0${stat?.mtimeMs}\0${stat?.size}`);
42
+ fingerprint.update(`\0${stat?.mtimeMs}\0${stat?.size}`);
43
+ }
44
+ return { local: { revision: revision.digest("hex").slice(0, 16), ...state }, fingerprint: fingerprint.digest("hex"), files };
45
+ }
46
+ export async function readLocal(scope) {
47
+ const read = await readStatus(scope).catch(() => undefined);
48
+ if (!read)
49
+ return undefined;
50
+ const repository = await localRepository(scope, read.local.branch).catch(() => undefined);
51
+ const defaultBranch = repository?.defaultBranches[0];
52
+ return { ...read.local, ...(repository?.github ? { github: true } : {}), ...(defaultBranch ? { defaultBranch } : {}) };
53
+ }
54
+ function due(elapsed, watched) {
55
+ return elapsed >= (watched ? WATCHED_POLL_MS : UNWATCHED_POLL_MS) - POLL_JITTER_MS;
56
+ }
57
+ export class GitStates {
58
+ entries = new Map();
59
+ polledAt = new Map();
60
+ results = new Map();
61
+ bases = new Map();
62
+ refs = new Map();
63
+ targets = new Map();
64
+ github;
65
+ read;
66
+ constructor(github = gh, read = readLocal) {
67
+ this.github = github;
68
+ this.read = read;
69
+ }
70
+ entry(scope) {
71
+ let entry = this.entries.get(scope);
72
+ if (!entry)
73
+ this.entries.set(scope, entry = { local: undefined, pr: undefined, prRef: undefined, prBase: undefined, prBranch: undefined, lastRead: -Infinity, generation: 0, trailing: undefined });
74
+ return entry;
75
+ }
76
+ async refresh(scope) {
77
+ const entry = this.entry(scope);
78
+ entry.lastRead = Date.now();
79
+ const generation = (entry.generation += 1);
80
+ const local = await this.read(scope);
81
+ if (generation !== entry.generation)
82
+ return;
83
+ entry.local = local;
84
+ if (local?.branch !== entry.prBranch)
85
+ entry.pr = undefined;
86
+ }
87
+ touch(scope) {
88
+ const entry = this.entry(scope);
89
+ if (entry.trailing)
90
+ return;
91
+ const wait = entry.lastRead + READ_INTERVAL_MS - Date.now();
92
+ if (wait <= 0) {
93
+ void this.refresh(scope);
94
+ return;
95
+ }
96
+ entry.trailing = setTimeout(() => {
97
+ entry.trailing = undefined;
98
+ void this.refresh(scope);
99
+ }, wait);
100
+ entry.trailing.unref();
101
+ }
102
+ summary(scope) {
103
+ const entry = this.entries.get(scope);
104
+ if (!entry?.local)
105
+ return undefined;
106
+ return { ...entry.local, ...(entry.pr ? { pr: entry.pr } : {}) };
107
+ }
108
+ hasPullRequest(scope, branch) {
109
+ const entry = this.entries.get(scope);
110
+ return Boolean(entry?.pr && entry.prBranch === branch);
111
+ }
112
+ pullRequestRef(scope) {
113
+ return this.entries.get(scope)?.prRef;
114
+ }
115
+ pullRequestBase(scope, branch) {
116
+ const entry = this.entries.get(scope);
117
+ return entry?.pr && entry.prBranch === branch ? entry.prBase : undefined;
118
+ }
119
+ async poll(sessions, forced) {
120
+ const now = Date.now();
121
+ const scopes = new Map();
122
+ for (const session of sessions) {
123
+ if (session.lifecycle === "live" || session.lifecycle === "dormant")
124
+ scopes.set(session.scope, scopes.get(session.scope) || session.watched);
125
+ }
126
+ const groups = new Map();
127
+ for (const [scope, watched] of scopes) {
128
+ const cached = this.targets.get(scope);
129
+ const target = cached && !watched && scope !== forced && !due(now - cached.readAt, false) && this.entries.get(scope)?.local?.branch === cached.localBranch ? cached : await this.target(scope, watched, now);
130
+ const entry = this.entry(scope);
131
+ const { branch: name, repository } = target;
132
+ if (!name || !repository) {
133
+ entry.pr = undefined;
134
+ continue;
135
+ }
136
+ const key = `${repository.base.repo}\0${repository.head.repo}\0${name}`;
137
+ const group = groups.get(key) ?? { scopes: [], repository, branch: name, watched: false, forced: false };
138
+ group.scopes.push(scope);
139
+ group.watched ||= watched;
140
+ group.forced ||= scope === forced;
141
+ groups.set(key, group);
142
+ }
143
+ for (const key of this.polledAt.keys())
144
+ if (!groups.has(key))
145
+ this.polledAt.delete(key);
146
+ for (const key of this.results.keys())
147
+ if (!groups.has(key))
148
+ this.results.delete(key);
149
+ for (const key of this.bases.keys())
150
+ if (!groups.has(key))
151
+ this.bases.delete(key);
152
+ for (const key of this.refs.keys())
153
+ if (!groups.has(key))
154
+ this.refs.delete(key);
155
+ for (const [key, group] of groups) {
156
+ if (group.forced || due(now - (this.polledAt.get(key) ?? -Infinity), group.watched)) {
157
+ this.polledAt.set(key, now);
158
+ const found = await resolvePullRequest(group.scopes[0], group.repository, group.branch, this.github).then(pr => ({ pr }), () => undefined);
159
+ if (found) {
160
+ this.bases.set(key, found.pr?.state === "OPEN" ? found.pr.baseBranch : undefined);
161
+ this.refs.set(key, found.pr && { repo: found.pr.repo, number: found.pr.number, id: found.pr.id });
162
+ this.results.set(key, found.pr && { number: found.pr.number, url: found.pr.url, state: found.pr.state, isDraft: found.pr.isDraft, checks: found.pr.checks, checksCompleted: found.pr.checksCompleted, reviewDecision: found.pr.reviewDecision, unresolvedThreads: found.pr.unresolvedThreads, updatedAt: found.pr.updatedAt });
163
+ }
164
+ }
165
+ if (!this.results.has(key))
166
+ continue;
167
+ for (const scope of group.scopes)
168
+ Object.assign(this.entry(scope), { pr: this.results.get(key), prRef: this.refs.get(key), prBase: this.bases.get(key), prBranch: group.branch });
169
+ }
170
+ this.release(new Set(sessions.map(session => session.scope)));
171
+ }
172
+ release(kept) {
173
+ for (const [scope, entry] of this.entries) {
174
+ if (kept.has(scope))
175
+ continue;
176
+ clearTimeout(entry.trailing);
177
+ this.entries.delete(scope);
178
+ }
179
+ for (const scope of this.targets.keys())
180
+ if (!kept.has(scope))
181
+ this.targets.delete(scope);
182
+ releaseRepositories(kept);
183
+ }
184
+ async target(scope, watched, now) {
185
+ const target = { readAt: now, localBranch: undefined, branch: undefined, repository: undefined };
186
+ this.targets.set(scope, target);
187
+ if (!isRepository(scope))
188
+ return target;
189
+ if (watched || !this.entries.get(scope)?.local)
190
+ await this.refresh(scope);
191
+ target.localBranch = this.entries.get(scope)?.local?.branch;
192
+ const branch = await head(scope);
193
+ target.branch = branch.ok && !branch.value.detached ? branch.value.name : undefined;
194
+ target.repository = target.branch === undefined ? undefined : await resolveRepository(scope, target.branch, this.github).catch(() => undefined);
195
+ return target;
196
+ }
197
+ }
@@ -0,0 +1,18 @@
1
+ const TYPES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
2
+ function isGithubImage(url) {
3
+ if (url.protocol !== "https:")
4
+ return false;
5
+ if (url.hostname === "raw.githubusercontent.com")
6
+ return /^\/[^/]+\/[^/]+\/.+/.test(url.pathname);
7
+ return url.hostname === "github.com" && /^\/user-attachments\/assets\/[0-9a-f-]+$/i.test(url.pathname);
8
+ }
9
+ export async function githubImage(href, token, fetcher = fetch) {
10
+ if (!URL.canParse(href) || !isGithubImage(new URL(href)))
11
+ return null;
12
+ const response = await fetcher(href, { headers: { authorization: `token ${(await token()).trim()}` } });
13
+ const type = response.headers.get("content-type")?.split(";")[0]?.trim() ?? "";
14
+ if (response.ok && TYPES.has(type))
15
+ return response;
16
+ await response.body?.cancel();
17
+ return null;
18
+ }
@@ -0,0 +1,54 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ import { GIT_WRITE_TIMEOUT_MS } from "./git.js";
4
+ const exec = promisify(execFile);
5
+ export const gh = async (scope, args) => {
6
+ try {
7
+ return (await exec("gh", args, {
8
+ cwd: scope,
9
+ timeout: GIT_WRITE_TIMEOUT_MS,
10
+ maxBuffer: 2 << 20,
11
+ env: { ...process.env, GH_PROMPT_DISABLED: "1", GIT_TERMINAL_PROMPT: "0", ...(args[0] === "stack" ? { GIT_EDITOR: "true", GIT_SEQUENCE_EDITOR: "true" } : {}) },
12
+ })).stdout;
13
+ }
14
+ catch (error) {
15
+ const failure = error;
16
+ if (failure.code === "ENOENT")
17
+ throw new Error("GitHub CLI is missing. Install gh on the Session Host.");
18
+ const detail = (args[0] === "stack" ? [failure.stdout, failure.stderr].filter(Boolean).join("\n").trim() : failure.stderr?.trim()) || failure.message || "Unknown error";
19
+ const requestFailed = /HTTP (?:429|5\d\d)|connection|timed? out|network|TLS|certificate|could not resolve/i.test(detail);
20
+ if ((failure.code === 4 && args[0] !== "stack") || (args[0] === "auth" && !requestFailed)) {
21
+ throw new Error(`GitHub authentication failed. Run gh auth login on the Session Host. ${detail}`);
22
+ }
23
+ throw new Error(`GitHub request failed: ${detail}`);
24
+ }
25
+ };
26
+ export const page = "pageInfo { hasNextPage endCursor }";
27
+ export async function api(scope, github, query, variables) {
28
+ const args = ["api", "graphql", "-f", `query=${query}`];
29
+ for (const [key, value] of Object.entries(variables)) {
30
+ if (value !== null)
31
+ args.push(typeof value === "string" ? "-f" : "-F", `${key}=${value}`);
32
+ }
33
+ const result = JSON.parse(await github(scope, args));
34
+ if (result.errors?.length || !result.data)
35
+ throw new Error(result.errors?.map(error => error.message).join("; ") || "GitHub returned no data.");
36
+ return result.data;
37
+ }
38
+ export async function connection(scope, github, first, id, type, field, fields) {
39
+ const result = [...first.nodes];
40
+ const seen = new Set();
41
+ let current = first;
42
+ for (let count = 0; count < 1000; count++) {
43
+ if (!current.pageInfo.hasNextPage)
44
+ return result;
45
+ const cursor = current.pageInfo.endCursor;
46
+ if (!cursor || seen.has(cursor))
47
+ throw new Error("GitHub returned an invalid page cursor.");
48
+ seen.add(cursor);
49
+ const data = await api(scope, github, `query($id:ID!,$cursor:String){node(id:$id){... on ${type}{${field}(first:100,after:$cursor){nodes{${fields}} ${page}}}}}`, { id, cursor });
50
+ current = data.node[field];
51
+ result.push(...current.nodes);
52
+ }
53
+ throw new Error("GitHub pagination limit exceeded.");
54
+ }
@@ -1,4 +1,4 @@
1
- import { readBody, send } from "./http.js";
1
+ import { readBody, send } from './http.js';
2
2
  export async function hostControlRoute(request, response, path, control, admission) {
3
3
  if (path === '/api/host' && request.method === 'GET') {
4
4
  const { token: _token, ...status } = control.identity;