@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,198 @@
|
|
|
1
|
+
import { gh } from "./publish.js";
|
|
2
|
+
import { cleanStack } from "./stack.js";
|
|
3
|
+
import { head, isRepository, run, GIT_READ_TIMEOUT_MS, GIT_WRITE_TIMEOUT_MS } from "./git.js";
|
|
4
|
+
const page = "pageInfo { hasNextPage endCursor }";
|
|
5
|
+
const commentFields = "id author { login } body url createdAt";
|
|
6
|
+
const comment = (value) => ({ ...value, author: value.author?.login ?? "[deleted]" });
|
|
7
|
+
async function api(scope, github, query, variables) {
|
|
8
|
+
const args = ["api", "graphql", "-f", `query=${query}`];
|
|
9
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
10
|
+
if (value !== null)
|
|
11
|
+
args.push(typeof value === "string" ? "-f" : "-F", `${key}=${value}`);
|
|
12
|
+
}
|
|
13
|
+
const result = JSON.parse(await github(scope, args));
|
|
14
|
+
if (result.errors?.length || !result.data)
|
|
15
|
+
throw new Error(result.errors?.map(error => error.message).join("; ") || "GitHub returned no data.");
|
|
16
|
+
return result.data;
|
|
17
|
+
}
|
|
18
|
+
async function pages(load) {
|
|
19
|
+
const result = [];
|
|
20
|
+
let cursor = null;
|
|
21
|
+
const seen = new Set();
|
|
22
|
+
for (let count = 0; count < 1000; count++) {
|
|
23
|
+
const connection = await load(cursor);
|
|
24
|
+
result.push(...connection.nodes);
|
|
25
|
+
if (!connection.pageInfo.hasNextPage)
|
|
26
|
+
return result;
|
|
27
|
+
cursor = connection.pageInfo.endCursor;
|
|
28
|
+
if (!cursor || seen.has(cursor))
|
|
29
|
+
throw new Error("GitHub returned an invalid page cursor.");
|
|
30
|
+
seen.add(cursor);
|
|
31
|
+
}
|
|
32
|
+
throw new Error("GitHub pagination limit exceeded.");
|
|
33
|
+
}
|
|
34
|
+
async function connection(scope, github, id, type, field, fields) {
|
|
35
|
+
return pages(async (cursor) => {
|
|
36
|
+
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 });
|
|
37
|
+
return data.node[field];
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async function select(scope, github) {
|
|
41
|
+
if (!isRepository(scope))
|
|
42
|
+
return null;
|
|
43
|
+
const branch = await head(scope);
|
|
44
|
+
if (!branch.ok)
|
|
45
|
+
throw new Error(branch.failure.message);
|
|
46
|
+
if (branch.value.detached)
|
|
47
|
+
return null;
|
|
48
|
+
const base = JSON.parse(await github(scope, ["repo", "view", "--json", "nameWithOwner"]));
|
|
49
|
+
const remoteNames = await run(scope, ["remote"], GIT_READ_TIMEOUT_MS);
|
|
50
|
+
if (!remoteNames.ok)
|
|
51
|
+
throw new Error(remoteNames.failure.message);
|
|
52
|
+
const names = remoteNames.value.trim().split("\n").filter(Boolean);
|
|
53
|
+
const configured = await run(scope, ["config", "--get", `branch.${branch.value.name}.pushRemote`], GIT_READ_TIMEOUT_MS);
|
|
54
|
+
const pushDefault = await run(scope, ["config", "--get", "remote.pushDefault"], GIT_READ_TIMEOUT_MS);
|
|
55
|
+
const upstream = await run(scope, ["config", "--get", `branch.${branch.value.name}.remote`], GIT_READ_TIMEOUT_MS);
|
|
56
|
+
const preferred = [configured, pushDefault, upstream].find(value => value.ok && value.value.trim() !== ".");
|
|
57
|
+
const remote = preferred?.ok ? preferred.value.trim() : names.includes("origin") ? "origin" : names.length === 1 ? names[0] : undefined;
|
|
58
|
+
if (!remote)
|
|
59
|
+
throw new Error("Cannot identify this branch's source remote.");
|
|
60
|
+
const url = await run(scope, ["remote", "get-url", "--push", remote], GIT_READ_TIMEOUT_MS);
|
|
61
|
+
if (!url.ok)
|
|
62
|
+
throw new Error(url.failure.message);
|
|
63
|
+
const source = /^(?:https:\/\/github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([^/]+\/[^/]+?)(?:\.git)?$/.exec(url.value.trim())?.[1];
|
|
64
|
+
if (!source)
|
|
65
|
+
throw new Error("Cannot identify the GitHub source repository.");
|
|
66
|
+
const sourceRepo = JSON.parse(await github(scope, ["repo", "view", source, "--json", "id"]));
|
|
67
|
+
if (!sourceRepo.id)
|
|
68
|
+
throw new Error("Cannot verify the source repository: GitHub did not return its ID.");
|
|
69
|
+
const [owner, name] = base.nameWithOwner.split("/");
|
|
70
|
+
const candidates = await pages(async (cursor) => {
|
|
71
|
+
const data = await api(scope, github, `query($owner:String!,$name:String!,$branch:String!,$cursor:String){repository(owner:$owner,name:$name){pullRequests(headRefName:$branch,first:100,after:$cursor,orderBy:{field:UPDATED_AT,direction:DESC}){nodes{id number state updatedAt closedAt headRepository{id}} ${page}}}}`, { owner: owner, name: name, branch: branch.value.name, cursor });
|
|
72
|
+
return data.repository.pullRequests;
|
|
73
|
+
});
|
|
74
|
+
const matches = candidates.filter(pr => pr.headRepository?.id === sourceRepo.id);
|
|
75
|
+
matches.sort((a, b) => Number(b.state === "OPEN") - Number(a.state === "OPEN") || (b.closedAt ?? b.updatedAt).localeCompare(a.closedAt ?? a.updatedAt));
|
|
76
|
+
const chosen = matches[0];
|
|
77
|
+
return chosen ? { repo: base.nameWithOwner, number: chosen.number, id: chosen.id } : null;
|
|
78
|
+
}
|
|
79
|
+
export async function pullRequest(scope, github = gh) {
|
|
80
|
+
const ref = await select(scope, github);
|
|
81
|
+
if (!ref)
|
|
82
|
+
return null;
|
|
83
|
+
const data = await api(scope, github, `query($id:ID!){node(id:$id){... on PullRequest{id number url title body state isDraft reviewDecision author{login} headRefName headRefOid headRepository{nameWithOwner} baseRefName createdAt updatedAt mergeable mergeStateStatus locked repository{isArchived viewerPermission mergeCommitAllowed squashMergeAllowed rebaseMergeAllowed} commits(last:1){nodes{commit{statusCheckRollup{id}}}}}}}`, ref);
|
|
84
|
+
const { commits, author, headRepository, locked, repository, ...details } = data.node;
|
|
85
|
+
const viewerCanComment = !repository.isArchived && (!locked || ["ADMIN", "MAINTAIN", "WRITE"].includes(repository.viewerPermission ?? ""));
|
|
86
|
+
const comments = (await connection(scope, github, ref.id, "PullRequest", "comments", commentFields)).map(comment);
|
|
87
|
+
const reviews = (await connection(scope, github, ref.id, "PullRequest", "reviews", `${commentFields} state`)).map(value => ({ ...comment(value), state: value.state }));
|
|
88
|
+
const rawThreads = await connection(scope, github, ref.id, "PullRequest", "reviewThreads", "id path line isOutdated isResolved viewerCanReply viewerCanResolve viewerCanUnresolve");
|
|
89
|
+
const threads = [];
|
|
90
|
+
for (const thread of rawThreads) {
|
|
91
|
+
const entries = await connection(scope, github, thread.id, "PullRequestReviewThread", "comments", `${commentFields} diffHunk`);
|
|
92
|
+
threads.push({ ...thread, diffHunk: entries[0]?.diffHunk ?? "", comments: entries.map(comment) });
|
|
93
|
+
}
|
|
94
|
+
const rollup = commits.nodes[0]?.commit.statusCheckRollup;
|
|
95
|
+
const statusCheckRollup = rollup ? await connection(scope, github, rollup.id, "StatusCheckRollup", "contexts", "... on CheckRun { name status conclusion detailsUrl } ... on StatusContext { context state targetUrl }") : [];
|
|
96
|
+
const mergeMethods = [];
|
|
97
|
+
if (!repository.isArchived && ["ADMIN", "MAINTAIN", "WRITE"].includes(repository.viewerPermission ?? "")) {
|
|
98
|
+
if (repository.mergeCommitAllowed)
|
|
99
|
+
mergeMethods.push("MERGE");
|
|
100
|
+
if (repository.squashMergeAllowed)
|
|
101
|
+
mergeMethods.push("SQUASH");
|
|
102
|
+
if (repository.rebaseMergeAllowed)
|
|
103
|
+
mergeMethods.push("REBASE");
|
|
104
|
+
}
|
|
105
|
+
return { ...details, ...ref, mergeMethods, ...(headRepository ? { headRepository: headRepository.nameWithOwner } : {}), author: author?.login ?? "[deleted]", viewerCanComment, comments, reviews, threads, statusCheckRollup };
|
|
106
|
+
}
|
|
107
|
+
async function git(scope, args) {
|
|
108
|
+
const result = await run(scope, args, GIT_WRITE_TIMEOUT_MS);
|
|
109
|
+
if (!result.ok)
|
|
110
|
+
throw new Error(result.failure.message);
|
|
111
|
+
return result.value.trim();
|
|
112
|
+
}
|
|
113
|
+
export async function pullBranch(scope, branch) {
|
|
114
|
+
await cleanStack(scope);
|
|
115
|
+
const selected = await head(scope);
|
|
116
|
+
if (!selected.ok || selected.value.detached || selected.value.name !== branch)
|
|
117
|
+
throw new Error("The branch has changed. Refresh Git status before pulling.");
|
|
118
|
+
await git(scope, ["pull", "--ff-only", "--no-rebase", "--no-autostash", "--no-squash"]);
|
|
119
|
+
}
|
|
120
|
+
export async function changePullRequest(scope, action, input, github = gh) {
|
|
121
|
+
validate(input?.pr);
|
|
122
|
+
if (action !== "rebase" && action !== "merge")
|
|
123
|
+
throw new Error("Invalid pull request action.");
|
|
124
|
+
if (!/^[0-9a-f]{40,64}$/.test(input.headOid) || typeof input.baseBranch !== "string")
|
|
125
|
+
throw new Error("Invalid pull request action.");
|
|
126
|
+
const pr = await current(scope, input.pr, github);
|
|
127
|
+
if (pr.state !== "OPEN" || pr.headRefOid !== input.headOid || pr.baseRefName !== input.baseBranch)
|
|
128
|
+
throw new Error("The pull request has changed. Reload it before continuing.");
|
|
129
|
+
if (action === "merge") {
|
|
130
|
+
if (pr.isDraft || !input.method || !pr.mergeMethods?.includes(input.method))
|
|
131
|
+
throw new Error("This merge method is not permitted for this pull request.");
|
|
132
|
+
await api(scope, github, "mutation($id:ID!,$head:GitObjectID!,$method:PullRequestMergeMethod!){mergePullRequest(input:{pullRequestId:$id,expectedHeadOid:$head,mergeMethod:$method}){pullRequest{id state}}}", { id: pr.id, head: input.headOid, method: input.method });
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
await cleanStack(scope);
|
|
136
|
+
await git(scope, ["check-ref-format", `refs/heads/${pr.baseRefName}`]);
|
|
137
|
+
const before = await git(scope, ["rev-parse", "HEAD"]);
|
|
138
|
+
let baseUrl = `https://github.com/${pr.repo}.git`;
|
|
139
|
+
for (const remote of (await git(scope, ["remote"])).split("\n").filter(Boolean)) {
|
|
140
|
+
const url = await git(scope, ["remote", "get-url", remote]);
|
|
141
|
+
const repo = /^(?:https:\/\/github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([^/]+\/[^/]+?)(?:\.git)?$/.exec(url)?.[1];
|
|
142
|
+
if (repo?.toLowerCase() === pr.repo.toLowerCase()) {
|
|
143
|
+
baseUrl = url;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
await git(scope, ["fetch", "--no-tags", baseUrl, `refs/heads/${pr.baseRefName}`]);
|
|
148
|
+
const base = await git(scope, ["rev-parse", "FETCH_HEAD"]);
|
|
149
|
+
const latest = await current(scope, input.pr, github);
|
|
150
|
+
const branch = await head(scope);
|
|
151
|
+
if (latest.state !== "OPEN" || latest.headRefOid !== input.headOid || latest.baseRefName !== input.baseBranch || !branch.ok || branch.value.detached || branch.value.name !== pr.headRefName || await git(scope, ["rev-parse", "HEAD"]) !== before)
|
|
152
|
+
throw new Error("The branch or pull request has changed. Reload it before rebasing.");
|
|
153
|
+
await cleanStack(scope);
|
|
154
|
+
const rebased = await run(scope, ["-c", "rebase.autoStash=false", "-c", "rebase.updateRefs=false", "rebase", base], GIT_WRITE_TIMEOUT_MS);
|
|
155
|
+
if (!rebased.ok) {
|
|
156
|
+
const aborted = await run(scope, ["rebase", "--abort"], GIT_WRITE_TIMEOUT_MS);
|
|
157
|
+
throw new Error(`${rebased.failure.message}\n${aborted.ok ? "Rebase aborted. Local changes were not pushed." : `Rebase did not complete. Check Git status in the Shell; if a rebase remains, run git rebase --abort. ${aborted.failure.message}`}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function validId(value) { return typeof value === "string" && /^[A-Za-z0-9_+/=-]+$/.test(value) && value.length <= 256; }
|
|
161
|
+
function validate(ref) {
|
|
162
|
+
if (!ref || typeof ref.repo !== "string" || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(ref.repo) || !Number.isSafeInteger(ref.number) || ref.number < 1 || !validId(ref.id))
|
|
163
|
+
throw new Error("Invalid pull request reference.");
|
|
164
|
+
}
|
|
165
|
+
async function current(scope, ref, github) {
|
|
166
|
+
const pr = await pullRequest(scope, github);
|
|
167
|
+
if (!pr || pr.id !== ref.id || pr.number !== ref.number || pr.repo !== ref.repo)
|
|
168
|
+
throw new Error("The branch's pull request has changed. Reload it before continuing.");
|
|
169
|
+
return pr;
|
|
170
|
+
}
|
|
171
|
+
export async function commentPullRequest(scope, input, github = gh) {
|
|
172
|
+
validate(input?.pr);
|
|
173
|
+
if (typeof input.body !== "string" || !input.body.trim() || (input.threadId !== undefined && !validId(input.threadId)))
|
|
174
|
+
throw new Error("Invalid comment input.");
|
|
175
|
+
const pr = await current(scope, input.pr, github);
|
|
176
|
+
if (input.threadId !== undefined) {
|
|
177
|
+
const thread = pr.threads.find(value => value.id === input.threadId);
|
|
178
|
+
if (!thread?.viewerCanReply)
|
|
179
|
+
throw new Error("Cannot reply to this thread.");
|
|
180
|
+
await api(scope, github, "mutation($id:ID!,$body:String!){addPullRequestReviewThreadReply(input:{pullRequestReviewThreadId:$id,body:$body}){comment{id}}}", { id: thread.id, body: input.body });
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
if (!pr.viewerCanComment)
|
|
184
|
+
throw new Error("Cannot comment on this pull request.");
|
|
185
|
+
await api(scope, github, "mutation($id:ID!,$body:String!){addComment(input:{subjectId:$id,body:$body}){commentEdge{node{id}}}}", { id: pr.id, body: input.body });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
export async function resolvePullRequestThread(scope, input, github = gh) {
|
|
189
|
+
validate(input?.pr);
|
|
190
|
+
if (!validId(input.threadId) || typeof input.resolved !== "boolean")
|
|
191
|
+
throw new Error("Invalid thread input.");
|
|
192
|
+
const pr = await current(scope, input.pr, github);
|
|
193
|
+
const thread = pr.threads.find(value => value.id === input.threadId);
|
|
194
|
+
if (!thread || !(input.resolved ? thread.viewerCanResolve : thread.viewerCanUnresolve))
|
|
195
|
+
throw new Error("Cannot change this thread's resolved state.");
|
|
196
|
+
const operation = input.resolved ? "resolveReviewThread" : "unresolveReviewThread";
|
|
197
|
+
await api(scope, github, `mutation($id:ID!){${operation}(input:{threadId:$id}){thread{id}}}`, { id: thread.id });
|
|
198
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { registerBackends } from "../backend/registry.js";
|
|
2
|
+
import { createOidcGateFromEnv, readOrCreateToken } from "./auth.js";
|
|
3
|
+
import { ConfigStore } from "./config-store.js";
|
|
4
|
+
import { SecretStore } from "./secret-store.js";
|
|
5
|
+
import { WorkflowStore } from "../workflows/store.js";
|
|
6
|
+
import { WorkflowExecutionService } from "./workflow-executions.js";
|
|
7
|
+
import { SessionHost } from "./host.js";
|
|
8
|
+
import { McpAuth } from "./mcp-auth.js";
|
|
9
|
+
import { serve } from "./server.js";
|
|
10
|
+
import { ShellRegistry } from "./shell.js";
|
|
11
|
+
import { TranscriptStore } from "./store.js";
|
|
12
|
+
import { acquireHost, readHost, oidcFingerprint } from "./ownership.js";
|
|
13
|
+
export async function startRuntime(options) {
|
|
14
|
+
const ownership = acquireHost(options.root);
|
|
15
|
+
const root = ownership.root;
|
|
16
|
+
let cleanup = async () => ownership.release();
|
|
17
|
+
try {
|
|
18
|
+
const previous = readHost(root);
|
|
19
|
+
if (previous) {
|
|
20
|
+
const reachable = await fetch(`${previous.url}/api/sessions`, {
|
|
21
|
+
headers: { authorization: `Bearer ${previous.token}` }, signal: AbortSignal.timeout(500),
|
|
22
|
+
}).then(response => response.ok, () => false);
|
|
23
|
+
if (reachable)
|
|
24
|
+
throw new Error('A Session Host is already reachable for this state root');
|
|
25
|
+
}
|
|
26
|
+
const config = new ConfigStore(root);
|
|
27
|
+
if (config.warning)
|
|
28
|
+
console.error(` WARNING: ${config.warning}`);
|
|
29
|
+
const mcpAuth = new McpAuth(root);
|
|
30
|
+
const store = new TranscriptStore(root);
|
|
31
|
+
const secrets = new SecretStore(root);
|
|
32
|
+
const host = new SessionHost({
|
|
33
|
+
store, resolveSecret: name => secrets.resolve(name), retention: config.retention,
|
|
34
|
+
mcpConnections: config.mcpConnections, mcpAuth,
|
|
35
|
+
standingAuthorisations: config.standingAuthorisations, allowTool: config.allowTool,
|
|
36
|
+
defaultBackend: config.defaultBackend, defaultModel: config.defaultModel,
|
|
37
|
+
defaultEffort: config.defaultEffort, autoCompaction: config.autoCompaction, summaryModel: config.summaryModel,
|
|
38
|
+
});
|
|
39
|
+
const shells = new ShellRegistry();
|
|
40
|
+
let running;
|
|
41
|
+
let oidc;
|
|
42
|
+
let sweep;
|
|
43
|
+
let reaping = Promise.resolve();
|
|
44
|
+
let stopping;
|
|
45
|
+
const stop = () => stopping ??= (async () => {
|
|
46
|
+
clearInterval(sweep);
|
|
47
|
+
const interrupt = async () => { await Promise.all([host.shutdown(), shells.killAll(), reaping]); };
|
|
48
|
+
if (running)
|
|
49
|
+
await running.stopAdmission(interrupt);
|
|
50
|
+
else
|
|
51
|
+
await interrupt();
|
|
52
|
+
if (running)
|
|
53
|
+
await running.stopAdmission(() => host.shutdown());
|
|
54
|
+
else
|
|
55
|
+
await host.shutdown();
|
|
56
|
+
await shells.killAll();
|
|
57
|
+
if (running)
|
|
58
|
+
await running.close();
|
|
59
|
+
else {
|
|
60
|
+
mcpAuth.dispose();
|
|
61
|
+
oidc?.dispose();
|
|
62
|
+
}
|
|
63
|
+
ownership.release();
|
|
64
|
+
})();
|
|
65
|
+
cleanup = stop;
|
|
66
|
+
registerBackends(host);
|
|
67
|
+
const workflows = new WorkflowStore(root);
|
|
68
|
+
const workflowExecutions = new WorkflowExecutionService(host, workflows, secrets, config, options.workflowRuntime());
|
|
69
|
+
await host.load();
|
|
70
|
+
workflowExecutions.reconcile();
|
|
71
|
+
sweep = setInterval(() => { reaping = reaping.then(async () => { await host.reap(); }); }, 60 * 60 * 1000);
|
|
72
|
+
sweep.unref();
|
|
73
|
+
host.onSessionClosed(sessionId => shells.killFor(sessionId));
|
|
74
|
+
const token = readOrCreateToken(root);
|
|
75
|
+
oidc = await createOidcGateFromEnv(root);
|
|
76
|
+
const identity = {
|
|
77
|
+
instanceId: ownership.instanceId, pid: process.pid, version: options.version, url: '', token, mode: options.mode,
|
|
78
|
+
settings: { port: options.port ?? 0, address: options.address ?? '127.0.0.1', cwd: process.cwd(), oidc: oidcFingerprint() },
|
|
79
|
+
};
|
|
80
|
+
running = await serve({
|
|
81
|
+
control: {
|
|
82
|
+
identity, stop,
|
|
83
|
+
hasActiveWork: () => host.hasActiveWork() || shells.hasLiveShells() || host.list().some(session => workflowExecutions.list(session.id).occupied),
|
|
84
|
+
},
|
|
85
|
+
host, token, shells, config, mcpAuth, store, workflows, secrets, workflowExecutions,
|
|
86
|
+
assets: options.assets(), scope: process.cwd(),
|
|
87
|
+
...(oidc === undefined ? {} : { oidc }),
|
|
88
|
+
...(options.port === undefined ? {} : { port: options.port }),
|
|
89
|
+
...(options.address === undefined ? {} : { address: options.address }),
|
|
90
|
+
});
|
|
91
|
+
const url = running.url.replace(`//${options.address ?? '127.0.0.1'}:`, '//127.0.0.1:');
|
|
92
|
+
identity.url = url;
|
|
93
|
+
ownership.publish(identity);
|
|
94
|
+
return { running, daemon: { url, token }, stop };
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
await cleanup();
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { validSecretName } from "../protocol/secrets.js";
|
|
5
|
+
export class SecretStore {
|
|
6
|
+
directory;
|
|
7
|
+
constructor(stateRoot) {
|
|
8
|
+
this.directory = join(stateRoot, 'secrets');
|
|
9
|
+
mkdirSync(this.directory, { recursive: true, mode: 0o700 });
|
|
10
|
+
chmodSync(this.directory, 0o700);
|
|
11
|
+
}
|
|
12
|
+
list() {
|
|
13
|
+
return readdirSync(this.directory).filter(name => name.endsWith('.secret'))
|
|
14
|
+
.map(name => name.slice(0, -7)).filter(validSecretName).sort();
|
|
15
|
+
}
|
|
16
|
+
has(name) { return existsSync(this.path(name)); }
|
|
17
|
+
set(name, value) {
|
|
18
|
+
const target = this.path(name);
|
|
19
|
+
if (typeof value !== 'string' || Buffer.byteLength(value) > 64_000 || value.length === 0)
|
|
20
|
+
throw new Error('Invalid secret value');
|
|
21
|
+
const temporary = join(this.directory, `.${randomUUID()}.tmp`);
|
|
22
|
+
try {
|
|
23
|
+
const fd = openSync(temporary, 'wx', 0o600);
|
|
24
|
+
try {
|
|
25
|
+
writeFileSync(fd, value);
|
|
26
|
+
fsyncSync(fd);
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
closeSync(fd);
|
|
30
|
+
}
|
|
31
|
+
renameSync(temporary, target);
|
|
32
|
+
this.sync();
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
rmSync(temporary, { force: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
delete(name) {
|
|
39
|
+
rmSync(this.path(name), { force: true });
|
|
40
|
+
this.sync();
|
|
41
|
+
}
|
|
42
|
+
resolve(name, signal) {
|
|
43
|
+
signal?.throwIfAborted();
|
|
44
|
+
try {
|
|
45
|
+
return readFileSync(this.path(name), 'utf8');
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new Error('Secret unavailable');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
path(name) {
|
|
52
|
+
if (!validSecretName(name))
|
|
53
|
+
throw new Error('Invalid secret name');
|
|
54
|
+
return join(this.directory, `${name}.secret`);
|
|
55
|
+
}
|
|
56
|
+
sync() {
|
|
57
|
+
const fd = openSync(this.directory, 'r');
|
|
58
|
+
try {
|
|
59
|
+
fsyncSync(fd);
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
closeSync(fd);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|