toolroll 0.9.115 → 0.9.117
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/CHANGELOG.md +55 -0
- package/README.md +43 -26
- package/dist/acp-agents.js +14 -1
- package/dist/acp-client.js +19 -1
- package/dist/agent-change-check.js +60 -0
- package/dist/agent-change-schema.js +19 -0
- package/dist/agent-detect.js +11 -1
- package/dist/agent-fence.js +39 -10
- package/dist/agent-network-request.js +108 -0
- package/dist/agent-requests.js +105 -0
- package/dist/approval-summary.js +3 -3
- package/dist/backup.js +17 -1
- package/dist/batch-checks.js +81 -18
- package/dist/browser/terminal.js +41 -41
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +36 -36
- package/dist/builder.js +3 -1
- package/dist/candidate-materialize.js +119 -0
- package/dist/central-enrollment-contract.js +156 -0
- package/dist/central-recovery-stage.js +329 -0
- package/dist/chat-actions.js +101 -2
- package/dist/chat-channel.js +40 -0
- package/dist/check-only-standing.js +13 -0
- package/dist/check-only.js +25 -13
- package/dist/check-recovery-hold.js +199 -0
- package/dist/check-recovery-index-schema.js +3 -0
- package/dist/check-screenshots.js +83 -0
- package/dist/cli.js +125 -6
- package/dist/codex-limits.js +4 -0
- package/dist/connect-cli.js +257 -11
- package/dist/contracts/chat-actions.js +4 -0
- package/dist/contracts/console-api.js +15 -4
- package/dist/contracts/engine-api.js +344 -2
- package/dist/contracts/lead-tools.js +56 -2
- package/dist/contracts/provider.js +1 -1
- package/dist/contracts/route.js +1 -1
- package/dist/contracts/terminal-connection.js +50 -0
- package/dist/converse.js +80 -4
- package/dist/device-pair.js +280 -0
- package/dist/direct-chat-artifacts.js +347 -0
- package/dist/direct-chat-catalog.js +311 -0
- package/dist/direct-chat-fence.js +96 -0
- package/dist/direct-chat-git.js +343 -0
- package/dist/direct-chat-laptop.js +19 -0
- package/dist/direct-chat-schema.js +181 -0
- package/dist/direct-chat-ship.js +861 -0
- package/dist/direct-chat-workspace.js +389 -0
- package/dist/direct-chat.js +2107 -0
- package/dist/doctor.js +1 -1
- package/dist/easy-start.js +18 -0
- package/dist/egress.js +25 -4
- package/dist/engine-chat.js +454 -0
- package/dist/engine-reads.js +141 -13
- package/dist/exec.js +4 -3
- package/dist/flexible-agents.js +8 -16
- package/dist/flexible-authority-end.js +2 -0
- package/dist/flexible-interactive-schema.js +133 -0
- package/dist/flexible-interactive.js +94 -0
- package/dist/flexible-session.js +117 -49
- package/dist/flexible-ship.js +87 -7
- package/dist/flexible-sign-in.js +10 -0
- package/dist/flexible-start.js +21 -3
- package/dist/flexible-thread.js +2 -3
- package/dist/flexible-turns.js +26 -5
- package/dist/flexible.js +13 -0
- package/dist/flow-triggers.js +13 -4
- package/dist/flows-ui.js +54 -20
- package/dist/handoff-cli.js +630 -0
- package/dist/invoke.js +55 -13
- package/dist/lead-contract.js +5 -4
- package/dist/lead-doors.js +80 -1
- package/dist/lead-progress.js +9 -0
- package/dist/lead-tools.js +682 -10
- package/dist/lead.js +37 -9
- package/dist/live-bus.js +11 -0
- package/dist/live-terminal.js +132 -12
- package/dist/local-handoff.js +363 -0
- package/dist/local-owner-acts.js +264 -2
- package/dist/local-owner.js +16 -1
- package/dist/multiplayer-handoff.js +528 -0
- package/dist/multiplayer-presence.js +471 -0
- package/dist/names.js +17 -4
- package/dist/needs-you.js +10 -0
- package/dist/operate.js +280 -19
- package/dist/policy.js +2 -2
- package/dist/project-delete-ui.js +18 -11
- package/dist/project-delete.js +65 -4
- package/dist/project-tools.js +1 -1
- package/dist/prompt.js +1 -1
- package/dist/provider-connection.js +2 -0
- package/dist/provider-home.js +251 -8
- package/dist/provider.js +3 -2
- package/dist/publish.js +119 -64
- package/dist/pull-request-flow.js +35 -14
- package/dist/read-executor-worker.js +1 -1
- package/dist/read-executor.js +95 -1
- package/dist/restore-retirement.js +152 -0
- package/dist/result-follow-ups.js +116 -37
- package/dist/route-change.js +44 -4
- package/dist/scope.js +62 -5
- package/dist/serve.js +49 -5
- package/dist/server/chat.js +94 -13
- package/dist/server/console.css +9 -9
- package/dist/server/direct-chat.js +583 -0
- package/dist/server/engine-api.js +219 -21
- package/dist/server/flows.js +47 -13
- package/dist/server/found-repos.js +25 -0
- package/dist/server/guards.js +20 -6
- package/dist/server/live.js +81 -23
- package/dist/server/pages.js +67 -13
- package/dist/server/people-tokens.js +47 -6
- package/dist/server/remote.js +29 -0
- package/dist/server/render-results.js +6 -2
- package/dist/server/render-settings.js +4 -2
- package/dist/server/render-tasks.js +18 -17
- package/dist/server/route-table.js +69 -2
- package/dist/server/settings.js +22 -7
- package/dist/server/task-views.js +15 -3
- package/dist/server/tasks.js +39 -2
- package/dist/server/terminals.js +185 -11
- package/dist/settings-proofs.js +1 -1
- package/dist/spend-report.js +2 -2
- package/dist/spend.js +3 -3
- package/dist/store.js +600 -59
- package/dist/subagent-admin.js +23 -10
- package/dist/subagent-backup-schema.js +36 -0
- package/dist/subagent-backup.js +92 -0
- package/dist/subagent-week.js +2 -2
- package/dist/subagent-work.js +70 -47
- package/dist/subagents-ui.js +6 -2
- package/dist/subagents.js +5 -2
- package/dist/subscription-chat.js +36 -5
- package/dist/subscription-usage.js +233 -0
- package/dist/task-live.js +2 -1
- package/dist/teardown-git.js +34 -4
- package/dist/terminal-connection.js +159 -0
- package/dist/terminal-safety.js +114 -0
- package/dist/tui/darwin-arm64/toolroll-tui +0 -0
- package/dist/tui/darwin-x64/toolroll-tui +0 -0
- package/dist/tui/linux-arm64/toolroll-tui +0 -0
- package/dist/tui/linux-x64/toolroll-tui +0 -0
- package/dist/tui-contract.js +1 -1
- package/dist/tui-launch.js +32 -1
- package/dist/workspace-identity-schema.js +21 -0
- package/package.json +4 -2
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { withActor } from "./actor.js";
|
|
5
|
+
import { materializePromotedCandidate } from "./candidate-materialize.js";
|
|
6
|
+
import { checkCommandFor } from "./check-levels.js";
|
|
7
|
+
import { resultRunsOf } from "./engine-reads.js";
|
|
8
|
+
import { owePullRequest, pullRequestBlocker } from "./pull-request-flow.js";
|
|
9
|
+
import { followUpChecksOf } from "./result-follow-ups.js";
|
|
10
|
+
import { remoteAllows, reproveRemote } from "./operate-remote.js";
|
|
11
|
+
import { verifyApproverStanding } from "./principal.js";
|
|
12
|
+
import { rowVisible } from "./project.js";
|
|
13
|
+
import { teardownGit } from "./teardown-git.js";
|
|
14
|
+
import { admitRemoteTerminalRequest, isRemoteTerminalRequest } from "./terminal-connection.js";
|
|
15
|
+
const ASSIGN_PATH = /^\/api\/handoff\/task\/([^/]+)\/assign-person$/;
|
|
16
|
+
const CANDIDATES_PATH = /^\/api\/handoff\/task\/([^/]+)\/candidates$/;
|
|
17
|
+
const PROMOTE_PATH = /^\/api\/handoff\/task\/([^/]+)\/candidate\/(cand-[a-f0-9]{16})\/promote$/;
|
|
18
|
+
const OPEN_PR_PATH = /^\/api\/handoff\/task\/([^/]+)\/candidate\/(cand-[a-f0-9]{16})\/open-pr$/;
|
|
19
|
+
const CANDIDATE_ID = /^cand-[a-f0-9]{16}$/;
|
|
20
|
+
const SHA_PATTERN = /^[0-9a-f]{40,64}$/;
|
|
21
|
+
const MAX_SMALL_BODY = 8 * 1024;
|
|
22
|
+
function sendJson(response, status, body, extraHeaders = {}) {
|
|
23
|
+
const payload = JSON.stringify(body);
|
|
24
|
+
response.writeHead(status, {
|
|
25
|
+
"content-type": "application/json; charset=utf-8",
|
|
26
|
+
"cache-control": "no-store",
|
|
27
|
+
"content-length": String(Buffer.byteLength(payload)),
|
|
28
|
+
...extraHeaders,
|
|
29
|
+
});
|
|
30
|
+
response.end(payload);
|
|
31
|
+
}
|
|
32
|
+
function readJsonBody(request, maxBytes = MAX_SMALL_BODY) {
|
|
33
|
+
return new Promise(resolve => {
|
|
34
|
+
const chunks = [];
|
|
35
|
+
let size = 0;
|
|
36
|
+
let over = false;
|
|
37
|
+
request.on("data", (chunk) => {
|
|
38
|
+
size += chunk.length;
|
|
39
|
+
if (size > maxBytes) {
|
|
40
|
+
over = true;
|
|
41
|
+
chunks.length = 0;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
chunks.push(chunk);
|
|
45
|
+
});
|
|
46
|
+
request.on("end", () => {
|
|
47
|
+
if (over)
|
|
48
|
+
return resolve(null);
|
|
49
|
+
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
|
50
|
+
if (raw === "")
|
|
51
|
+
return resolve({});
|
|
52
|
+
try {
|
|
53
|
+
const parsed = JSON.parse(raw);
|
|
54
|
+
resolve(parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
resolve(null);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
request.on("error", () => resolve(null));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function authenticateCaller(request, response, options, needMutation) {
|
|
64
|
+
const { store, clock, allowedHost, browserKeys, identify, authenticateBearer, admitBearer } = options;
|
|
65
|
+
if (isRemoteTerminalRequest(request)) {
|
|
66
|
+
const principal = authenticateBearer(request);
|
|
67
|
+
const admitted = admitRemoteTerminalRequest(request, store, principal ?? undefined, clock);
|
|
68
|
+
if (!admitted.ok) {
|
|
69
|
+
sendJson(response, admitted.status, { ok: false, error: admitted.error, message: `Remote terminal rejected (${admitted.error}).` });
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const budget = admitBearer(principal);
|
|
73
|
+
if (!budget.ok) {
|
|
74
|
+
const headers = {};
|
|
75
|
+
if (budget.status !== 503)
|
|
76
|
+
headers["retry-after"] = String(budget.retryAfter);
|
|
77
|
+
sendJson(response, budget.status === 503 ? 503 : 429, { ok: false, error: "rate-limited", message: "Too many requests." }, headers);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const acct = store.accountOf(principal.account);
|
|
81
|
+
if (acct === null || acct.revokedAt !== null) {
|
|
82
|
+
sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your account is unavailable." });
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (needMutation && (admitted.scope !== "act" || acct.role !== "approver")) {
|
|
86
|
+
sendJson(response, 403, { ok: false, error: "forbidden", message: "This action requires an approver act API token." });
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return { account: principal.account, generation: principal.generation, role: acct.role, principal: principal, localOwner: false };
|
|
90
|
+
}
|
|
91
|
+
const who = identify(request, needMutation);
|
|
92
|
+
if (who === null || who.via !== "cookie") {
|
|
93
|
+
sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Sign in to continue." });
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const acct = store.accountOf(who.name);
|
|
97
|
+
if (acct === null || acct.revokedAt !== null || acct.generation !== who.session.generation) {
|
|
98
|
+
sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your session has ended." });
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
if (needMutation) {
|
|
102
|
+
if (acct.role !== "approver") {
|
|
103
|
+
sendJson(response, 403, { ok: false, error: "approver_required", message: "Only an approver can perform this action." });
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const origin = request.headers.origin;
|
|
107
|
+
const referer = request.headers.referer;
|
|
108
|
+
const named = typeof origin === "string" && origin !== "null" ? origin : typeof referer === "string" ? referer : null;
|
|
109
|
+
if (named === null || !allowedHost(named.replace(/^https?:\/\//, "").split("/")[0])) {
|
|
110
|
+
sendJson(response, 403, { ok: false, error: "origin", message: "Same-origin request required." });
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (request.headers["x-csrf-token"] !== who.session.csrf) {
|
|
114
|
+
sendJson(response, 403, { ok: false, error: "csrf", message: "Invalid form token." });
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const keyErr = browserKeys.check(who, request, null);
|
|
118
|
+
if (keyErr !== null) {
|
|
119
|
+
sendJson(response, keyErr.status, { ok: false, error: "key", message: keyErr.message });
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { account: who.name, generation: acct.generation, role: acct.role, principal: null, localOwner: who.authority === "local-owner" };
|
|
124
|
+
}
|
|
125
|
+
function visibleTaskFamily(store, ceiling, caller, taskId) {
|
|
126
|
+
const allowedRepos = caller.principal === null || caller.principal.projects === null
|
|
127
|
+
? null
|
|
128
|
+
: caller.principal.projects.filter(repo => store.accountCanAccess(caller.account, repo));
|
|
129
|
+
const family = store.taskFamilyOf(taskId, allowedRepos, false);
|
|
130
|
+
const repo = family?.root.repo ?? family?.current.repo ?? null;
|
|
131
|
+
if (family === null || family.problem !== null || repo === null)
|
|
132
|
+
return null;
|
|
133
|
+
if (!rowVisible(ceiling, repo) || !store.accountCanAccess(caller.account, repo))
|
|
134
|
+
return null;
|
|
135
|
+
if (caller.principal !== null && !remoteAllows(store, caller.principal, repo))
|
|
136
|
+
return null;
|
|
137
|
+
return { family, repo };
|
|
138
|
+
}
|
|
139
|
+
function currentTaskBaseCommitReadOnly(store, family) {
|
|
140
|
+
const latest = resultRunsOf(store, family)[0] ?? null;
|
|
141
|
+
if (latest?.headRevision && SHA_PATTERN.test(latest.headRevision))
|
|
142
|
+
return latest.headRevision;
|
|
143
|
+
if (latest?.baseRevision && SHA_PATTERN.test(latest.baseRevision))
|
|
144
|
+
return latest.baseRevision;
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
async function currentTaskBaseCommit(store, family, repo) {
|
|
148
|
+
const fromStore = currentTaskBaseCommitReadOnly(store, family);
|
|
149
|
+
if (fromStore !== null)
|
|
150
|
+
return fromStore;
|
|
151
|
+
if (existsSync(join(repo, ".git"))) {
|
|
152
|
+
const git = teardownGit();
|
|
153
|
+
const head = await git(repo, ["rev-parse", "HEAD"]).catch(() => null);
|
|
154
|
+
if (head !== null && head.code === 0 && SHA_PATTERN.test(head.stdout.trim()))
|
|
155
|
+
return head.stdout.trim();
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
function taskNeedsApproval(store, family) {
|
|
160
|
+
const scope = store.getScope(family.current.id);
|
|
161
|
+
if (scope === null)
|
|
162
|
+
return family.current.state === "queued";
|
|
163
|
+
return scope.approvedAt === null || scope.approvedDigest === null || scope.approvedDigest !== scope.digest;
|
|
164
|
+
}
|
|
165
|
+
export async function handleAssignTaskPersonHttp(request, response, url, options) {
|
|
166
|
+
if (request.method !== "POST")
|
|
167
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "POST" });
|
|
168
|
+
const match = ASSIGN_PATH.exec(url.pathname);
|
|
169
|
+
if (match === null)
|
|
170
|
+
return sendJson(response, 404, { ok: false, error: "not_found" });
|
|
171
|
+
let taskId;
|
|
172
|
+
try {
|
|
173
|
+
taskId = decodeURIComponent(match[1]);
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return sendJson(response, 400, { ok: false, error: "bad_task" });
|
|
177
|
+
}
|
|
178
|
+
const caller = authenticateCaller(request, response, options, true);
|
|
179
|
+
if (caller === null)
|
|
180
|
+
return;
|
|
181
|
+
const body = await readJsonBody(request);
|
|
182
|
+
if (body === null || typeof body["person"] !== "string" || body["person"].trim() === "") {
|
|
183
|
+
return sendJson(response, 400, { ok: false, error: "bad_request", message: "Choose a teammate to hand this task to." });
|
|
184
|
+
}
|
|
185
|
+
const person = body["person"].trim();
|
|
186
|
+
const whyRaw = typeof body["why"] === "string" ? body["why"].trim() : "";
|
|
187
|
+
const why = whyRaw === "" ? `Handed to you by ${caller.account}.` : whyRaw.slice(0, 400);
|
|
188
|
+
const found = visibleTaskFamily(options.store, options.liveCeiling(), caller, taskId);
|
|
189
|
+
if (found === null)
|
|
190
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Task not found or outside your projects." });
|
|
191
|
+
const now = options.clock();
|
|
192
|
+
if (caller.principal !== null && !reproveRemote(options.store, caller.principal, now).ok) {
|
|
193
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
194
|
+
}
|
|
195
|
+
const handed = withActor({ account: caller.account, lead: false }, () => options.store.askPerson(found.family.current.id, person, why, now));
|
|
196
|
+
if (!handed.ok) {
|
|
197
|
+
const status = handed.reason === "unknown-person" ? 400 : 404;
|
|
198
|
+
const message = handed.reason === "unknown-person"
|
|
199
|
+
? `${person} does not have access to this project.`
|
|
200
|
+
: "Task could not be handed off.";
|
|
201
|
+
return sendJson(response, status, { ok: false, error: handed.reason, message });
|
|
202
|
+
}
|
|
203
|
+
options.store.recordAction({
|
|
204
|
+
at: now.toISOString(),
|
|
205
|
+
actor: caller.account,
|
|
206
|
+
repo: found.repo,
|
|
207
|
+
taskId: found.family.current.id,
|
|
208
|
+
runId: null,
|
|
209
|
+
action: "task.handoff",
|
|
210
|
+
outcome: "recorded",
|
|
211
|
+
source: caller.principal !== null ? "api" : "work",
|
|
212
|
+
detail: `handed to ${person}: ${why}`,
|
|
213
|
+
});
|
|
214
|
+
options.store.bumpWake();
|
|
215
|
+
const recipient = options.store.accountOf(person);
|
|
216
|
+
const warning = recipient?.role === "viewer" && taskNeedsApproval(options.store, found.family)
|
|
217
|
+
? `${person} is a Viewer and cannot approve this Task — an Approver will still need to approve it.`
|
|
218
|
+
: null;
|
|
219
|
+
return sendJson(response, 200, {
|
|
220
|
+
ok: true,
|
|
221
|
+
taskId: found.family.current.id,
|
|
222
|
+
person,
|
|
223
|
+
why,
|
|
224
|
+
...(warning === null ? {} : { warning }),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
function candidateLiveCheckState(store, evidenceRoot, runId, now) {
|
|
228
|
+
const followUps = followUpChecksOf(store, runId, now, evidenceRoot);
|
|
229
|
+
const latestCheck = followUps.at(-1) ?? null;
|
|
230
|
+
if (latestCheck !== null)
|
|
231
|
+
return latestCheck.state;
|
|
232
|
+
const rc = store.runCheckFor(runId);
|
|
233
|
+
if (rc !== null && (rc.status === "passed" || rc.status === "failed" || rc.status === "not-run")) {
|
|
234
|
+
return rc.status;
|
|
235
|
+
}
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
export async function handleListCandidatesHttp(request, response, url, options) {
|
|
239
|
+
if (request.method !== "GET")
|
|
240
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "GET" });
|
|
241
|
+
const match = CANDIDATES_PATH.exec(url.pathname);
|
|
242
|
+
if (match === null)
|
|
243
|
+
return sendJson(response, 404, { ok: false, error: "not_found" });
|
|
244
|
+
let taskId;
|
|
245
|
+
try {
|
|
246
|
+
taskId = decodeURIComponent(match[1]);
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
return sendJson(response, 400, { ok: false, error: "bad_task" });
|
|
250
|
+
}
|
|
251
|
+
const caller = authenticateCaller(request, response, options, false);
|
|
252
|
+
if (caller === null)
|
|
253
|
+
return;
|
|
254
|
+
const found = visibleTaskFamily(options.store, options.liveCeiling(), caller, taskId);
|
|
255
|
+
if (found === null)
|
|
256
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Task not found or outside your projects." });
|
|
257
|
+
const now = options.clock();
|
|
258
|
+
const currentBaseCommit = currentTaskBaseCommitReadOnly(options.store, found.family);
|
|
259
|
+
const hasFullCheck = checkCommandFor(options.store, found.repo, "full").command !== null;
|
|
260
|
+
const dir = join(options.evidenceRoot, "handoff-candidates", found.family.root.id);
|
|
261
|
+
const candidates = [];
|
|
262
|
+
if (existsSync(dir)) {
|
|
263
|
+
for (const file of readdirSync(dir)) {
|
|
264
|
+
if (!file.endsWith(".json"))
|
|
265
|
+
continue;
|
|
266
|
+
const id = file.slice(0, -5);
|
|
267
|
+
if (!CANDIDATE_ID.test(id))
|
|
268
|
+
continue;
|
|
269
|
+
if (!existsSync(join(dir, `${id}.patch`)))
|
|
270
|
+
continue;
|
|
271
|
+
try {
|
|
272
|
+
const parsed = JSON.parse(readFileSync(join(dir, file), "utf8"));
|
|
273
|
+
if (parsed && parsed.id === id && typeof parsed.patchSha256 === "string") {
|
|
274
|
+
const stale = parsed.promotedAt === null && parsed.baseCommit !== null && currentBaseCommit !== null && parsed.baseCommit !== currentBaseCommit;
|
|
275
|
+
const liveCheckState = typeof parsed.runId === "number"
|
|
276
|
+
? candidateLiveCheckState(options.store, options.evidenceRoot, parsed.runId, now)
|
|
277
|
+
: null;
|
|
278
|
+
let publication = null;
|
|
279
|
+
let canOpenPr = false;
|
|
280
|
+
if (typeof parsed.runId === "number") {
|
|
281
|
+
const pub = options.store.publicationForRun(parsed.runId);
|
|
282
|
+
if (pub !== null) {
|
|
283
|
+
publication = { id: pub.id, state: pub.state, prNumber: pub.prNumber, prUrl: pub.prUrl };
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
const checksReady = !hasFullCheck || liveCheckState === "passed";
|
|
287
|
+
canOpenPr = checksReady && pullRequestBlocker(options.store, parsed.runId) === null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
candidates.push({ ...parsed, stale, liveCheckState, publication, canOpenPr });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch { }
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
candidates.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
297
|
+
if (caller.principal !== null && !reproveRemote(options.store, caller.principal, options.clock()).ok) {
|
|
298
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
299
|
+
}
|
|
300
|
+
return sendJson(response, 200, { ok: true, taskId: found.family.current.id, currentBaseCommit, candidates });
|
|
301
|
+
}
|
|
302
|
+
export async function handlePromoteCandidateHttp(request, response, url, options) {
|
|
303
|
+
if (request.method !== "POST")
|
|
304
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "POST" });
|
|
305
|
+
const match = PROMOTE_PATH.exec(url.pathname);
|
|
306
|
+
if (match === null)
|
|
307
|
+
return sendJson(response, 404, { ok: false, error: "not_found" });
|
|
308
|
+
let taskId;
|
|
309
|
+
try {
|
|
310
|
+
taskId = decodeURIComponent(match[1]);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
return sendJson(response, 400, { ok: false, error: "bad_task" });
|
|
314
|
+
}
|
|
315
|
+
const candidateId = match[2];
|
|
316
|
+
const caller = authenticateCaller(request, response, options, true);
|
|
317
|
+
if (caller === null)
|
|
318
|
+
return;
|
|
319
|
+
const found = visibleTaskFamily(options.store, options.liveCeiling(), caller, taskId);
|
|
320
|
+
if (found === null)
|
|
321
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Task not found or outside your projects." });
|
|
322
|
+
const dir = join(options.evidenceRoot, "handoff-candidates", found.family.root.id);
|
|
323
|
+
const metaPath = join(dir, `${candidateId}.json`);
|
|
324
|
+
const patchPath = join(dir, `${candidateId}.patch`);
|
|
325
|
+
if (!existsSync(metaPath) || !existsSync(patchPath)) {
|
|
326
|
+
return sendJson(response, 404, { ok: false, error: "candidate_not_found", message: "Candidate not found for this task." });
|
|
327
|
+
}
|
|
328
|
+
let meta;
|
|
329
|
+
let patch;
|
|
330
|
+
try {
|
|
331
|
+
meta = JSON.parse(readFileSync(metaPath, "utf8"));
|
|
332
|
+
patch = readFileSync(patchPath, "utf8");
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
return sendJson(response, 500, { ok: false, error: "candidate_unreadable", message: "Could not read saved candidate." });
|
|
336
|
+
}
|
|
337
|
+
const actualSha = createHash("sha256").update(patch, "utf8").digest("hex");
|
|
338
|
+
if (actualSha !== meta.patchSha256) {
|
|
339
|
+
return sendJson(response, 409, { ok: false, error: "digest_mismatch", message: "Saved candidate patch digest did not match its metadata." });
|
|
340
|
+
}
|
|
341
|
+
const currentBaseCommit = await currentTaskBaseCommit(options.store, found.family, found.repo);
|
|
342
|
+
if (meta.baseCommit !== null && currentBaseCommit !== null && meta.baseCommit !== currentBaseCommit) {
|
|
343
|
+
return sendJson(response, 409, {
|
|
344
|
+
ok: false,
|
|
345
|
+
error: "stale_candidate",
|
|
346
|
+
baseCommit: meta.baseCommit,
|
|
347
|
+
currentBaseCommit,
|
|
348
|
+
message: `Task head moved from ${meta.baseCommit.slice(0, 8)} to ${currentBaseCommit.slice(0, 8)}. Run \`toolroll handoff rebase ${found.family.current.id}\` before promoting.`,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
const now = options.clock();
|
|
352
|
+
if (caller.principal !== null && !reproveRemote(options.store, caller.principal, now).ok) {
|
|
353
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
354
|
+
}
|
|
355
|
+
const effectiveBase = meta.baseCommit ?? currentBaseCommit;
|
|
356
|
+
let runId = meta.runId ?? null;
|
|
357
|
+
let branch = meta.branch ?? null;
|
|
358
|
+
let headCommit = meta.headCommit ?? null;
|
|
359
|
+
let checks = null;
|
|
360
|
+
if (runId === null && effectiveBase !== null && existsSync(join(found.repo, ".git"))) {
|
|
361
|
+
const materialized = await materializePromotedCandidate({
|
|
362
|
+
store: options.store,
|
|
363
|
+
evidenceRoot: options.evidenceRoot,
|
|
364
|
+
repo: found.repo,
|
|
365
|
+
taskRef: found.family.current.refId,
|
|
366
|
+
taskId: found.family.current.id,
|
|
367
|
+
candidateId,
|
|
368
|
+
author: meta.author,
|
|
369
|
+
promoter: caller.account,
|
|
370
|
+
baseCommit: effectiveBase,
|
|
371
|
+
patch,
|
|
372
|
+
patchSha256: actualSha,
|
|
373
|
+
note: meta.note,
|
|
374
|
+
now,
|
|
375
|
+
});
|
|
376
|
+
if (!materialized.ok) {
|
|
377
|
+
return sendJson(response, 409, { ok: false, error: materialized.error, message: materialized.message });
|
|
378
|
+
}
|
|
379
|
+
runId = materialized.runId;
|
|
380
|
+
branch = materialized.branch;
|
|
381
|
+
headCommit = materialized.headCommit;
|
|
382
|
+
checks = materialized.checks;
|
|
383
|
+
}
|
|
384
|
+
const updated = {
|
|
385
|
+
...meta,
|
|
386
|
+
promotedAt: meta.promotedAt ?? now.toISOString(),
|
|
387
|
+
promotedBy: meta.promotedBy ?? caller.account,
|
|
388
|
+
runId,
|
|
389
|
+
branch,
|
|
390
|
+
headCommit,
|
|
391
|
+
checkStatus: checks?.status ?? meta.checkStatus ?? null,
|
|
392
|
+
checkRequestId: checks?.status === "queued" ? checks.requestId : (meta.checkRequestId ?? null),
|
|
393
|
+
};
|
|
394
|
+
const tmp = `${metaPath}.tmp`;
|
|
395
|
+
writeFileSync(tmp, JSON.stringify(updated, null, 2), { mode: 0o600 });
|
|
396
|
+
renameSync(tmp, metaPath);
|
|
397
|
+
const runWords = runId === null ? "" : ` into central Run #${runId} (${branch ?? "candidate"}, ${headCommit?.slice(0, 8) ?? ""})`;
|
|
398
|
+
const checkWords = checks?.status === "queued"
|
|
399
|
+
? ` and queued ${checks.level} central checks (request #${checks.requestId})`
|
|
400
|
+
: checks?.status === "not-configured"
|
|
401
|
+
? " (no project check command configured yet)"
|
|
402
|
+
: "";
|
|
403
|
+
const detail = `Promoted Candidate ${candidateId} (sha256:${actualSha.slice(0, 12)}, ${meta.files.length} file${meta.files.length === 1 ? "" : "s"}, by ${meta.author})${runWords}${checkWords}.`;
|
|
404
|
+
options.store.recordAction({
|
|
405
|
+
at: now.toISOString(),
|
|
406
|
+
actor: caller.account,
|
|
407
|
+
repo: found.repo,
|
|
408
|
+
taskId: found.family.current.id,
|
|
409
|
+
runId,
|
|
410
|
+
action: "handoff.promote",
|
|
411
|
+
outcome: "recorded",
|
|
412
|
+
source: caller.principal !== null ? "api" : "work",
|
|
413
|
+
detail,
|
|
414
|
+
});
|
|
415
|
+
const standing = verifyApproverStanding(options.store, caller.account, caller.generation, [found.repo]);
|
|
416
|
+
if (standing.ok) {
|
|
417
|
+
options.store.fileSteerNote(found.family.current.id, standing.who.name, detail, now);
|
|
418
|
+
}
|
|
419
|
+
options.store.bumpWake();
|
|
420
|
+
return sendJson(response, 200, {
|
|
421
|
+
ok: true,
|
|
422
|
+
taskId: found.family.current.id,
|
|
423
|
+
candidateId,
|
|
424
|
+
sha256: actualSha,
|
|
425
|
+
promotedAt: updated.promotedAt,
|
|
426
|
+
promotedBy: updated.promotedBy,
|
|
427
|
+
runId,
|
|
428
|
+
branch,
|
|
429
|
+
headCommit,
|
|
430
|
+
checks,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
export async function handleOpenCandidatePrHttp(request, response, url, options) {
|
|
434
|
+
if (request.method !== "POST")
|
|
435
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "POST" });
|
|
436
|
+
const match = OPEN_PR_PATH.exec(url.pathname);
|
|
437
|
+
if (match === null)
|
|
438
|
+
return sendJson(response, 404, { ok: false, error: "not_found" });
|
|
439
|
+
let taskId;
|
|
440
|
+
try {
|
|
441
|
+
taskId = decodeURIComponent(match[1]);
|
|
442
|
+
}
|
|
443
|
+
catch {
|
|
444
|
+
return sendJson(response, 400, { ok: false, error: "bad_task" });
|
|
445
|
+
}
|
|
446
|
+
const candidateId = match[2];
|
|
447
|
+
const caller = authenticateCaller(request, response, options, true);
|
|
448
|
+
if (caller === null)
|
|
449
|
+
return;
|
|
450
|
+
if (caller.localOwner) {
|
|
451
|
+
return sendJson(response, 403, { ok: false, error: "password_required", message: "Opening a pull request requires a password-authenticated session or act API token." });
|
|
452
|
+
}
|
|
453
|
+
const found = visibleTaskFamily(options.store, options.liveCeiling(), caller, taskId);
|
|
454
|
+
if (found === null)
|
|
455
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Task not found or outside your projects." });
|
|
456
|
+
const dir = join(options.evidenceRoot, "handoff-candidates", found.family.root.id);
|
|
457
|
+
const metaPath = join(dir, `${candidateId}.json`);
|
|
458
|
+
if (!existsSync(metaPath)) {
|
|
459
|
+
return sendJson(response, 404, { ok: false, error: "candidate_not_found", message: "Candidate not found for this task." });
|
|
460
|
+
}
|
|
461
|
+
let meta;
|
|
462
|
+
try {
|
|
463
|
+
meta = JSON.parse(readFileSync(metaPath, "utf8"));
|
|
464
|
+
}
|
|
465
|
+
catch {
|
|
466
|
+
return sendJson(response, 500, { ok: false, error: "candidate_unreadable", message: "Could not read saved candidate." });
|
|
467
|
+
}
|
|
468
|
+
if (typeof meta.runId !== "number" || meta.promotedAt === null) {
|
|
469
|
+
return sendJson(response, 409, { ok: false, error: "not_promoted", message: "Promote this Candidate into a central Run before opening a pull request." });
|
|
470
|
+
}
|
|
471
|
+
const now = options.clock();
|
|
472
|
+
if (caller.principal !== null && !reproveRemote(options.store, caller.principal, now).ok) {
|
|
473
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
474
|
+
}
|
|
475
|
+
const hasFullCheck = checkCommandFor(options.store, found.repo, "full").command !== null;
|
|
476
|
+
const liveCheckState = candidateLiveCheckState(options.store, options.evidenceRoot, meta.runId, now);
|
|
477
|
+
if (hasFullCheck && liveCheckState !== "passed") {
|
|
478
|
+
return sendJson(response, 409, {
|
|
479
|
+
ok: false,
|
|
480
|
+
error: "checks_required",
|
|
481
|
+
liveCheckState,
|
|
482
|
+
message: `Central checks must pass before opening a pull request (current state: ${liveCheckState ?? "not-run"}).`,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
const blocker = pullRequestBlocker(options.store, meta.runId);
|
|
486
|
+
if (blocker !== null) {
|
|
487
|
+
return sendJson(response, 409, { ok: false, error: "not_publishable", message: blocker });
|
|
488
|
+
}
|
|
489
|
+
let publication;
|
|
490
|
+
try {
|
|
491
|
+
publication = owePullRequest(options.store, meta.runId, caller.account, null, now);
|
|
492
|
+
}
|
|
493
|
+
catch (error) {
|
|
494
|
+
return sendJson(response, 409, {
|
|
495
|
+
ok: false,
|
|
496
|
+
error: "publish_refused",
|
|
497
|
+
message: error instanceof Error ? error.message : "Could not queue pull request.",
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
options.store.recordAction({
|
|
501
|
+
at: now.toISOString(),
|
|
502
|
+
actor: caller.account,
|
|
503
|
+
repo: found.repo,
|
|
504
|
+
taskId: found.family.current.id,
|
|
505
|
+
runId: meta.runId,
|
|
506
|
+
action: "handoff.open-pr",
|
|
507
|
+
outcome: "recorded",
|
|
508
|
+
source: caller.principal !== null ? "api" : "work",
|
|
509
|
+
detail: `Queued pull request for promoted Candidate ${candidateId} (Run #${meta.runId}, ${publication.head} -> ${publication.base}).`,
|
|
510
|
+
});
|
|
511
|
+
options.store.bumpWake();
|
|
512
|
+
return sendJson(response, 200, {
|
|
513
|
+
ok: true,
|
|
514
|
+
taskId: found.family.current.id,
|
|
515
|
+
candidateId,
|
|
516
|
+
runId: meta.runId,
|
|
517
|
+
publication: {
|
|
518
|
+
id: publication.id,
|
|
519
|
+
state: publication.state,
|
|
520
|
+
githubRepo: publication.githubRepo,
|
|
521
|
+
base: publication.base,
|
|
522
|
+
head: publication.head,
|
|
523
|
+
headSha: publication.headSha,
|
|
524
|
+
prNumber: publication.prNumber,
|
|
525
|
+
prUrl: publication.prUrl,
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
}
|