toolroll 0.9.117 → 0.9.119
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 +42 -0
- package/README.md +14 -14
- package/dist/agent-fence.js +54 -6
- package/dist/assignment.js +40 -29
- package/dist/browser/workspace.css +1 -1
- package/dist/browser/workspace.js +32 -31
- package/dist/builder.js +295 -237
- package/dist/central-recovery-stage.js +3 -3
- package/dist/check-failure.js +2 -2
- package/dist/check-levels.js +3 -0
- package/dist/check-report.js +58 -7
- package/dist/coding-context.js +16 -2
- package/dist/coding-provider.js +7 -3
- package/dist/coding-workspace.js +5 -5
- package/dist/contracts/console-api.js +4 -1
- package/dist/contracts/engine-api.js +1 -0
- package/dist/contracts/release-check.js +7 -4
- package/dist/contracts/release-terms.js +11 -2
- package/dist/contracts/verification-receipt.js +1 -1
- package/dist/deploy-gate.js +7 -1
- package/dist/direct-chat.js +3 -3
- package/dist/easy-start.js +8 -6
- package/dist/engine-reads.js +72 -0
- package/dist/flexible-conversation-schema.js +24 -0
- package/dist/flexible-conversation.js +162 -0
- package/dist/flexible-custody.js +3 -0
- package/dist/flexible-import.js +51 -0
- package/dist/flexible-session.js +84 -11
- package/dist/install-mode.js +1 -1
- package/dist/invoke.js +18 -6
- package/dist/local-landing.js +500 -0
- package/dist/local-owner.js +1 -0
- package/dist/operate.js +143 -22
- package/dist/passwordless.js +1 -1
- package/dist/planner.js +6 -1
- package/dist/project-delete.js +1 -0
- package/dist/project-skills.js +28 -13
- package/dist/rehearsal.js +27 -0
- package/dist/release-adapters.js +170 -2
- package/dist/release-approval.js +11 -8
- package/dist/release-card.js +7 -2
- package/dist/release-carry.js +1 -1
- package/dist/release-cli.js +115 -14
- package/dist/release-main-proof.js +49 -0
- package/dist/release-main.js +110 -0
- package/dist/release-recheck.js +10 -3
- package/dist/release.js +221 -33
- package/dist/reply-shape.js +6 -1
- package/dist/result-actions.js +7 -1
- package/dist/result-follow-ups.js +8 -4
- package/dist/scout.js +6 -2
- package/dist/serve.js +31 -14
- package/dist/server/direct-chat.js +88 -3
- package/dist/server/engine-api.js +43 -5
- package/dist/server/render-results.js +3 -1
- package/dist/server/render-tasks.js +10 -1
- package/dist/server/route-table.js +9 -0
- package/dist/server/settings.js +31 -7
- package/dist/server/task-views.js +15 -0
- package/dist/server/tasks.js +63 -3
- package/dist/settings-groups-ui.js +2 -2
- package/dist/skills-manifest.js +57 -0
- package/dist/store.js +29 -4
- package/dist/surface.js +9 -1
- 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/verification-evidence.js +17 -6
- package/dist/workspace-ui.js +1 -1
- package/package.json +3 -2
- package/scripts/release-proof.mjs +240 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, mkdtempSync, realpathSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { run as execRun } from "./exec.js";
|
|
6
|
+
import { hardenedGit } from "./teardown-git.js";
|
|
7
|
+
import { assignmentOf } from "./assignment.js";
|
|
8
|
+
import { projectCheckSet, quickVerifyKey } from "./check-levels.js";
|
|
9
|
+
import { recordActionWhole } from "./action-ledger.js";
|
|
10
|
+
import { reproveApprover, verifyApproverStanding } from "./principal.js";
|
|
11
|
+
import { localActNow, LOCAL_OWNER_LAPSED } from "./local-owner.js";
|
|
12
|
+
import { publishingOf } from "./pull-request-flow.js";
|
|
13
|
+
export const LAND_DESTINATION = "land destination";
|
|
14
|
+
export const LAND_INTENT = "land intent";
|
|
15
|
+
export const LAND_SETTLED = "land settled";
|
|
16
|
+
function checkBindingsOf(store, repo) {
|
|
17
|
+
return { full: store.liveVerifyCommand(repo)?.digest ?? null, quick: store.liveVerifyCommand(quickVerifyKey(repo))?.digest ?? null,
|
|
18
|
+
setup: store.liveWorktreeSetup(repo)?.digest ?? null, access: String(store.projectCheckAccess(repo)) };
|
|
19
|
+
}
|
|
20
|
+
const OID = /^[0-9a-f]{40}$/;
|
|
21
|
+
const refuse = (reason, message) => ({ ok: false, reason, message });
|
|
22
|
+
const short = (oid) => oid.slice(0, 7);
|
|
23
|
+
const keyOf = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex").slice(0, 16);
|
|
24
|
+
export function landableBranch(name) {
|
|
25
|
+
if (name === "" || name === "HEAD" || name.startsWith("-") || name.length > 100 || !/^[A-Za-z0-9._/-]+$/.test(name))
|
|
26
|
+
return false;
|
|
27
|
+
if (name.startsWith("toolroll/"))
|
|
28
|
+
return false;
|
|
29
|
+
return name.split("/").every(part => part !== "" && !part.startsWith(".") && !part.startsWith("-") && !part.endsWith(".lock") && !part.endsWith(".")) && !name.includes("..");
|
|
30
|
+
}
|
|
31
|
+
function commonDirOf(repo) {
|
|
32
|
+
try {
|
|
33
|
+
const dotgit = join(realpathSync(repo), ".git");
|
|
34
|
+
const held = lstatSync(dotgit);
|
|
35
|
+
return held.isDirectory() && !held.isSymbolicLink() ? realpathSync(dotgit) : null;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function gitFor(repo, given) {
|
|
42
|
+
const git = hardenedGit(given ?? execRun, "build", { repo });
|
|
43
|
+
return (args, cwd = repo, timeoutMs = 60_000) => git("git", args, { cwd, timeoutMs });
|
|
44
|
+
}
|
|
45
|
+
function rows(store, where, args) {
|
|
46
|
+
return store.handle.prepare(`SELECT id,at,actor,outcome,detail FROM action_ledger WHERE ${where} ORDER BY id DESC`).all(...args).flatMap(row => {
|
|
47
|
+
try {
|
|
48
|
+
const detail = JSON.parse(String(row["detail"] ?? "null"));
|
|
49
|
+
return detail !== null && typeof detail === "object" ? [{ id: Number(row["id"]), at: String(row["at"]), actor: String(row["actor"]), outcome: String(row["outcome"]), detail: detail }] : [];
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export function recordedDestination(store, repo) {
|
|
57
|
+
const row = rows(store, "repo = ? AND action = ? AND source = 'request'", [repo, LAND_DESTINATION])[0];
|
|
58
|
+
const ref = row?.detail["ref"], common = row?.detail["common"];
|
|
59
|
+
return typeof ref === "string" && typeof common === "string" ? { ref, common } : null;
|
|
60
|
+
}
|
|
61
|
+
function intentOf(row) {
|
|
62
|
+
const d = row.detail;
|
|
63
|
+
if (d["v"] !== 1 || typeof d["ref"] !== "string" || typeof d["old"] !== "string" || typeof d["commit"] !== "string" || !OID.test(d["old"]) || !OID.test(d["commit"])
|
|
64
|
+
|| (d["mode"] !== "files" && d["mode"] !== "ref") || typeof d["key"] !== "string" || (d["kind"] !== "land" && d["kind"] !== "undo"))
|
|
65
|
+
return null;
|
|
66
|
+
return { id: row.id, at: row.at, repo: row.repo, task: row.task, run: row.run, kind: d["kind"], ref: d["ref"], old: d["old"], commit: d["commit"], mode: d["mode"], key: d["key"],
|
|
67
|
+
reverts: typeof d["reverts"] === "number" ? d["reverts"] : null };
|
|
68
|
+
}
|
|
69
|
+
function intents(store, where, args) {
|
|
70
|
+
return store.handle.prepare(`SELECT id,at,actor,outcome,detail,repo,task_id,run_id FROM action_ledger WHERE ${where} AND action = ? ORDER BY id DESC`).all(...args, LAND_INTENT).flatMap(row => {
|
|
71
|
+
try {
|
|
72
|
+
const detail = JSON.parse(String(row["detail"] ?? "null"));
|
|
73
|
+
const one = intentOf({ id: Number(row["id"]), at: String(row["at"]), actor: String(row["actor"]), outcome: String(row["outcome"]), detail,
|
|
74
|
+
repo: String(row["repo"]), task: String(row["task_id"]), run: Number(row["run_id"]) });
|
|
75
|
+
return one === null ? [] : [one];
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function settledOf(store, intent) {
|
|
83
|
+
const row = rows(store, "action = ? AND outcome LIKE ?", [LAND_SETTLED, `${intent}:%`])[0];
|
|
84
|
+
return row === undefined ? null : { outcome: row.outcome.slice(String(intent).length + 1), at: row.at, detail: row.detail };
|
|
85
|
+
}
|
|
86
|
+
export function landingOf(store, rootId) {
|
|
87
|
+
const all = intents(store, "task_id = ?", [rootId]);
|
|
88
|
+
const landed = all.find(one => one.kind === "land" && settledOf(store, one.id)?.outcome.startsWith("landed") === true);
|
|
89
|
+
if (landed === undefined)
|
|
90
|
+
return null;
|
|
91
|
+
const undone = all.some(one => one.kind === "undo" && one.reverts === landed.id && settledOf(store, one.id)?.outcome.startsWith("landed") === true);
|
|
92
|
+
return { intent: landed, at: settledOf(store, landed.id).at, undone };
|
|
93
|
+
}
|
|
94
|
+
async function treeProblem(git, commit) {
|
|
95
|
+
const listed = await git(["ls-tree", "-r", "-z", "--full-tree", commit]);
|
|
96
|
+
if (listed.code !== 0)
|
|
97
|
+
return "its files couldn't be read";
|
|
98
|
+
for (const entry of listed.stdout.split("\0")) {
|
|
99
|
+
if (entry === "")
|
|
100
|
+
continue;
|
|
101
|
+
const match = /^(\d{6}) (\w+) ([0-9a-f]{40})\t(.*)$/s.exec(entry);
|
|
102
|
+
if (match === null)
|
|
103
|
+
return "its files couldn't be read";
|
|
104
|
+
const [, mode, , oid, path] = match;
|
|
105
|
+
if (mode === "160000")
|
|
106
|
+
return "it holds a submodule";
|
|
107
|
+
if (path === ".gitattributes" || path.endsWith("/.gitattributes")) {
|
|
108
|
+
const blob = await git(["cat-file", "blob", oid]);
|
|
109
|
+
if (blob.code !== 0)
|
|
110
|
+
return "its .gitattributes couldn't be read";
|
|
111
|
+
if (/\b(filter|working-tree-encoding)\s*=/.test(blob.stdout) || /(^|\s)filter(\s|$)/m.test(blob.stdout))
|
|
112
|
+
return "its files use a Git filter (like Git LFS) or an encoding";
|
|
113
|
+
}
|
|
114
|
+
if (path === ".gitmodules")
|
|
115
|
+
return "it holds a submodule";
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
async function checkoutsOn(git, ref) {
|
|
120
|
+
const listed = await git(["worktree", "list", "--porcelain", "-z"]);
|
|
121
|
+
if (listed.code !== 0)
|
|
122
|
+
return null;
|
|
123
|
+
const found = [];
|
|
124
|
+
let path = null;
|
|
125
|
+
for (const line of listed.stdout.split("\0")) {
|
|
126
|
+
if (line.startsWith("worktree "))
|
|
127
|
+
path = line.slice("worktree ".length);
|
|
128
|
+
else if (line === `branch ${ref}` && path !== null) {
|
|
129
|
+
try {
|
|
130
|
+
found.push(realpathSync(path));
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
found.push(path);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return found;
|
|
138
|
+
}
|
|
139
|
+
async function refProblem(git, repo, ref, old, own) {
|
|
140
|
+
const branch = ref.slice("refs/heads/".length);
|
|
141
|
+
const symbolic = await git(["symbolic-ref", "-q", ref]);
|
|
142
|
+
if (symbolic.code === 0)
|
|
143
|
+
return { reason: "destination", message: `${branch} points at another branch (${symbolic.stdout.trim().replace(/^refs\/heads\//, "")}), so Toolroll won't move it. Land it with git yourself.` };
|
|
144
|
+
if (symbolic.code !== 1)
|
|
145
|
+
return { reason: "git", message: `Toolroll couldn't read ${branch}.` };
|
|
146
|
+
if (old !== null) {
|
|
147
|
+
const read = await git(["rev-parse", "--verify", "-q", `${ref}^{commit}`]);
|
|
148
|
+
if (read.code !== 0 || read.stdout.trim() !== old)
|
|
149
|
+
return { reason: "moved", message: `${branch} moved since you looked. Look again, then land it.` };
|
|
150
|
+
}
|
|
151
|
+
const on = await checkoutsOn(git, ref);
|
|
152
|
+
if (on === null)
|
|
153
|
+
return { reason: "git", message: "Toolroll couldn't list this project's checkouts." };
|
|
154
|
+
if (on.length > 0) {
|
|
155
|
+
let repoReal = repo;
|
|
156
|
+
try {
|
|
157
|
+
repoReal = realpathSync(repo);
|
|
158
|
+
}
|
|
159
|
+
catch { }
|
|
160
|
+
const where = on.includes(repoReal) ? "your project folder" : `another folder (${on[0]})`;
|
|
161
|
+
return { reason: "occupied", message: `${branch} is checked out in ${where}, so Toolroll won't touch its files. To do it yourself there: ${own}` };
|
|
162
|
+
}
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
function projectOf(store, repo) {
|
|
166
|
+
if (repo === null || !store.knownRepos().includes(repo))
|
|
167
|
+
return null;
|
|
168
|
+
const common = commonDirOf(repo);
|
|
169
|
+
return common === null ? null : { repo, common };
|
|
170
|
+
}
|
|
171
|
+
export function publishesThroughPullRequests(store, repo) {
|
|
172
|
+
try {
|
|
173
|
+
return publishingOf(store, repo).on || store.publicationGrantFor(repo) !== null;
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export const PULL_REQUESTS_INSTEAD = "This project publishes through pull requests; open one from the task instead.";
|
|
180
|
+
const ALREADY_MSG = (ref, commit) => `Already on ${ref.replace(/^refs\/heads\//, "")} (${short(commit)}).`;
|
|
181
|
+
export async function readLanding(store, taskId, access, now, options) {
|
|
182
|
+
const assignment = assignmentOf(store, taskId, now, { principal: "operator", repos: access.repos, includeUnplaced: false }, options.root);
|
|
183
|
+
if (assignment === null)
|
|
184
|
+
return refuse("not-found", `No task \`${taskId}\` in your projects.`);
|
|
185
|
+
if (assignment.repo !== null && publishesThroughPullRequests(store, assignment.repo))
|
|
186
|
+
return refuse("pull-requests", PULL_REQUESTS_INSTEAD);
|
|
187
|
+
const project = projectOf(store, assignment.repo);
|
|
188
|
+
if (project === null)
|
|
189
|
+
return refuse("destination", "This task's project isn't a local Git folder Toolroll can land in.");
|
|
190
|
+
const git = gitFor(project.repo, options.git);
|
|
191
|
+
if (options.undo === true)
|
|
192
|
+
return readUndo(store, assignment.rootId, project, git, options);
|
|
193
|
+
const receipt = assignment.receipt;
|
|
194
|
+
if (assignment.state !== "complete" || assignment.completion === null || receipt === null)
|
|
195
|
+
return refuse("not-complete", "Accept it first: only accepted work lands.");
|
|
196
|
+
if (assignment.completion.digest !== receipt.digest)
|
|
197
|
+
return refuse("checks", "Its checks read differently since you accepted it. Run checks again, then land it.");
|
|
198
|
+
const candidate = receipt.head;
|
|
199
|
+
if (candidate === null || !OID.test(candidate) || receipt.completionKind === "research-report")
|
|
200
|
+
return refuse("nothing", "This result has no commit to land.");
|
|
201
|
+
const landed = landingOf(store, assignment.rootId);
|
|
202
|
+
if (landed !== null && !landed.undone && landed.intent.commit === candidate)
|
|
203
|
+
return { ok: true, landed: { at: landed.at, ref: landed.intent.ref, commit: landed.intent.commit, mode: landed.intent.mode } };
|
|
204
|
+
const checks = receipt.checks.status === "passed" ? "passed" : !projectCheckSet(store, project.repo) && receipt.checks.status === "not-run" ? "none" : null;
|
|
205
|
+
if (checks === null)
|
|
206
|
+
return refuse("checks", receipt.checks.status === "failed" ? "Its checks failed. Only work whose checks passed on its exact commit lands." : "Its checks haven't passed on its exact commit. Run checks, then land it.");
|
|
207
|
+
const destination = await destinationOf(store, project, git, options.into);
|
|
208
|
+
if (!destination.ok)
|
|
209
|
+
return destination;
|
|
210
|
+
return planFor(git, project, destination, { kind: "land", taskId: receipt.taskId, rootId: assignment.rootId, runId: receipt.runId, receipt: receipt.digest, candidate, checks,
|
|
211
|
+
bindings: checkBindingsOf(store, project.repo) });
|
|
212
|
+
}
|
|
213
|
+
async function destinationOf(store, project, git, into) {
|
|
214
|
+
const recorded = recordedDestination(store, project.repo);
|
|
215
|
+
if (into !== undefined) {
|
|
216
|
+
if (!landableBranch(into))
|
|
217
|
+
return refuse("destination", `\`${into}\` isn't a branch Toolroll can land on.`);
|
|
218
|
+
const ref = `refs/heads/${into}`;
|
|
219
|
+
return { ok: true, ref, recorded: recorded?.ref === ref && recorded.common === project.common };
|
|
220
|
+
}
|
|
221
|
+
if (recorded !== null && recorded.common === project.common)
|
|
222
|
+
return { ok: true, ref: recorded.ref, recorded: true };
|
|
223
|
+
const head = await git(["symbolic-ref", "-q", "HEAD"]);
|
|
224
|
+
const ref = head.stdout.trim();
|
|
225
|
+
if (head.code !== 0 || !ref.startsWith("refs/heads/") || !landableBranch(ref.slice("refs/heads/".length))) {
|
|
226
|
+
return refuse("destination", "Your project folder isn't on a branch Toolroll can land on. Name one: --into <branch>.");
|
|
227
|
+
}
|
|
228
|
+
return { ok: true, ref, recorded: false };
|
|
229
|
+
}
|
|
230
|
+
function ownCommand(repo, fixed) {
|
|
231
|
+
return fixed.candidate !== null ? `git -C ${repo} merge --ff-only ${fixed.candidate}` : `git -C ${repo} revert --no-edit ${fixed.reverts.old}..${fixed.reverts.landed}`;
|
|
232
|
+
}
|
|
233
|
+
async function planFor(git, project, destination, fixed) {
|
|
234
|
+
const { ref } = destination;
|
|
235
|
+
const branch = ref.slice("refs/heads/".length);
|
|
236
|
+
const read = await git(["rev-parse", "--verify", "-q", `${ref}^{commit}`]);
|
|
237
|
+
const old = read.stdout.trim();
|
|
238
|
+
if (read.code !== 0 || !OID.test(old))
|
|
239
|
+
return refuse("destination", `There's no branch \`${branch}\` in this project.`);
|
|
240
|
+
if (fixed.candidate !== null) {
|
|
241
|
+
if (old === fixed.candidate)
|
|
242
|
+
return refuse("nothing", ALREADY_MSG(ref, old));
|
|
243
|
+
const type = await git(["cat-file", "-t", fixed.candidate]);
|
|
244
|
+
if (type.code !== 0 || type.stdout.trim() !== "commit")
|
|
245
|
+
return refuse("git", `This result's commit ${short(fixed.candidate)} is no longer in the project.`);
|
|
246
|
+
const ancestor = await git(["merge-base", "--is-ancestor", old, fixed.candidate]);
|
|
247
|
+
if (ancestor.code === 1) {
|
|
248
|
+
return refuse("moved", `${branch} moved on since this work started (it's at ${short(old)} now), so it can't land as a fast-forward. `
|
|
249
|
+
+ `A merged commit would need its own check. To merge it yourself: git -C ${project.repo} merge ${fixed.candidate}`);
|
|
250
|
+
}
|
|
251
|
+
if (ancestor.code !== 0)
|
|
252
|
+
return refuse("git", "Toolroll couldn't compare the commits.");
|
|
253
|
+
const problem = await treeProblem(git, fixed.candidate) ?? await treeProblem(git, old);
|
|
254
|
+
if (problem !== null)
|
|
255
|
+
return refuse("unsupported", `This project can't be landed by Toolroll: ${problem}. Land it with git yourself.`);
|
|
256
|
+
}
|
|
257
|
+
const blocked = await refProblem(git, project.repo, ref, null, ownCommand(project.repo, fixed));
|
|
258
|
+
if (blocked !== null)
|
|
259
|
+
return refuse(blocked.reason, blocked.message);
|
|
260
|
+
const plan = { ...fixed, repo: project.repo, ref, branch, old, mode: "ref", recorded: destination.recorded };
|
|
261
|
+
return { ok: true, plan: { ...plan, key: keyOf({ v: 1, ...plan, common: project.common }) } };
|
|
262
|
+
}
|
|
263
|
+
async function readUndo(store, rootId, project, git, options) {
|
|
264
|
+
void options;
|
|
265
|
+
const landed = landingOf(store, rootId);
|
|
266
|
+
if (landed === null || landed.undone)
|
|
267
|
+
return refuse("nothing", landed === null ? "This task hasn't landed, so there's nothing to undo." : "This landing was already undone.");
|
|
268
|
+
const destination = { ref: landed.intent.ref, recorded: true };
|
|
269
|
+
return planFor(git, project, destination, { kind: "undo", taskId: landed.intent.task, rootId, runId: landed.intent.run, receipt: landed.intent.key, candidate: null, checks: "none",
|
|
270
|
+
reverts: { intent: landed.intent.id, old: landed.intent.old, landed: landed.intent.commit }, bindings: checkBindingsOf(store, project.repo) });
|
|
271
|
+
}
|
|
272
|
+
export function landingWords(plan) {
|
|
273
|
+
const where = `${plan.branch} in ${plan.repo} (no folder has it checked out; only the branch moves)`;
|
|
274
|
+
if (plan.kind === "undo") {
|
|
275
|
+
return [`Undo the landing of ${short(plan.reverts.old)}..${short(plan.reverts.landed)} on ${where}.`,
|
|
276
|
+
"A new commit reverts exactly those changes; nothing is rewritten. No check runs on the undo."];
|
|
277
|
+
}
|
|
278
|
+
return [`Land ${short(plan.candidate)} on ${where}, as a fast-forward from ${short(plan.old)}.`,
|
|
279
|
+
plan.checks === "passed" ? `Checks passed on ${short(plan.candidate)}.` : "No check ran: this project has no check command.",
|
|
280
|
+
...(plan.recorded ? [] : [`Toolroll remembers ${plan.branch} as where this project's work lands.`])];
|
|
281
|
+
}
|
|
282
|
+
async function settleUnfinished(store, repo, now, options) {
|
|
283
|
+
const project = projectOf(store, repo);
|
|
284
|
+
if (project === null)
|
|
285
|
+
return;
|
|
286
|
+
const git = gitFor(repo, options.git);
|
|
287
|
+
for (const intent of intents(store, "repo = ?", [repo]).filter(one => settledOf(store, one.id) === null)) {
|
|
288
|
+
const symbolic = await git(["symbolic-ref", "-q", intent.ref]);
|
|
289
|
+
const read = await git(["rev-parse", "--verify", "-q", `${intent.ref}^{commit}`]);
|
|
290
|
+
const at = read.stdout.trim();
|
|
291
|
+
let outcome, words;
|
|
292
|
+
if (symbolic.code !== 1) {
|
|
293
|
+
outcome = "needs-person";
|
|
294
|
+
words = `${intent.ref.replace(/^refs\/heads\//, "")} now points at another branch or can't be read. Look at ${repo} with git log.`;
|
|
295
|
+
}
|
|
296
|
+
else if (at === intent.old) {
|
|
297
|
+
outcome = "abandoned";
|
|
298
|
+
words = "It stopped before the branch moved; nothing changed.";
|
|
299
|
+
}
|
|
300
|
+
else if (at === intent.commit && intent.mode === "files") {
|
|
301
|
+
outcome = "needs-person";
|
|
302
|
+
words = `The branch moved to ${short(intent.commit)}; check your folder's files with git status in ${repo}.`;
|
|
303
|
+
}
|
|
304
|
+
else if (at === intent.commit) {
|
|
305
|
+
outcome = "landed:recovered";
|
|
306
|
+
words = "It had finished; recorded now.";
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
outcome = "needs-person";
|
|
310
|
+
words = `The branch is at ${OID.test(at) ? short(at) : "an unreadable commit"} now, neither before nor after the landing. Look at ${repo} with git log.`;
|
|
311
|
+
}
|
|
312
|
+
recordActionWhole(store.handle, { at: now.toISOString(), actor: "toolroll", repo, taskId: intent.task, runId: intent.run, action: LAND_SETTLED, outcome: `${intent.id}:${outcome}`, source: "work" }, { v: 1, words });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function standing(store, authority, repo) {
|
|
316
|
+
if (authority.kind === "local") {
|
|
317
|
+
const local = localActNow(store, "land-task");
|
|
318
|
+
if (local === null)
|
|
319
|
+
return refuse("authority", LOCAL_OWNER_LAPSED);
|
|
320
|
+
const proved = verifyApproverStanding(store, local.account, local.accountGeneration, store.knownRepos().filter(one => store.accountCanAccess(local.account, one)));
|
|
321
|
+
if (!proved.ok || !store.accountCanAccess(local.account, repo))
|
|
322
|
+
return refuse("authority", LOCAL_OWNER_LAPSED);
|
|
323
|
+
return { ok: true, actor: `operator:${local.account}` };
|
|
324
|
+
}
|
|
325
|
+
const proved = reproveApprover(store, authority.who);
|
|
326
|
+
if (!proved.ok || !authority.who.repos.includes(repo) || !store.accountCanAccess(authority.who.name, repo))
|
|
327
|
+
return refuse("authority", "Your access changed. Sign in again.");
|
|
328
|
+
return { ok: true, actor: `operator:${authority.who.name}` };
|
|
329
|
+
}
|
|
330
|
+
const projectLocks = new Map();
|
|
331
|
+
async function locked(repo, body) {
|
|
332
|
+
const before = projectLocks.get(repo) ?? Promise.resolve();
|
|
333
|
+
let release;
|
|
334
|
+
const mine = new Promise(resolve => { release = resolve; });
|
|
335
|
+
const chained = before.then(() => mine);
|
|
336
|
+
projectLocks.set(repo, chained);
|
|
337
|
+
await before.catch(() => undefined);
|
|
338
|
+
try {
|
|
339
|
+
return await body();
|
|
340
|
+
}
|
|
341
|
+
finally {
|
|
342
|
+
release();
|
|
343
|
+
if (projectLocks.get(repo) === chained)
|
|
344
|
+
projectLocks.delete(repo);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
export async function land(store, taskId, input, authority, access, now, options) {
|
|
348
|
+
if (!/^[0-9a-f]{16}$/.test(input.key))
|
|
349
|
+
return refuse("stale", "That landing isn't the one on screen. Look again, then land it.");
|
|
350
|
+
const ref = store.lookupRef(taskId);
|
|
351
|
+
const repo = ref?.repo ?? null;
|
|
352
|
+
if (repo === null || (access.repos !== null && !access.repos.includes(repo)))
|
|
353
|
+
return refuse("not-found", `No task \`${taskId}\` in your projects.`);
|
|
354
|
+
const first = standing(store, authority, repo);
|
|
355
|
+
if (!first.ok)
|
|
356
|
+
return first;
|
|
357
|
+
if (publishesThroughPullRequests(store, repo))
|
|
358
|
+
return refuse("pull-requests", PULL_REQUESTS_INSTEAD);
|
|
359
|
+
const named = input.into !== undefined && landableBranch(input.into) ? `refs/heads/${input.into}` : recordedDestination(store, repo)?.ref ?? null;
|
|
360
|
+
if (named !== null && projectOf(store, repo) !== null) {
|
|
361
|
+
const symbolic = await gitFor(repo, options.git)(["symbolic-ref", "-q", named]);
|
|
362
|
+
if (symbolic.code === 0)
|
|
363
|
+
return refuse("destination", `${named.replace(/^refs\/heads\//, "")} points at another branch, so Toolroll won't move it. Land it with git yourself.`);
|
|
364
|
+
}
|
|
365
|
+
return locked(repo, async () => {
|
|
366
|
+
const done = intents(store, "repo = ? AND task_id IS NOT NULL", [repo]).find(one => one.key === input.key);
|
|
367
|
+
if (done !== undefined) {
|
|
368
|
+
const settled = settledOf(store, done.id);
|
|
369
|
+
if (settled?.outcome.startsWith("landed") === true) {
|
|
370
|
+
const who = standing(store, authority, repo);
|
|
371
|
+
if (!who.ok)
|
|
372
|
+
return who;
|
|
373
|
+
return { ok: true, replayed: true, kind: done.kind, ref: done.ref, branch: done.ref.slice("refs/heads/".length), commit: done.commit, old: done.old, mode: done.mode, warning: null };
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
await settleUnfinished(store, repo, now, options);
|
|
377
|
+
const read = await readLanding(store, taskId, access, now, { ...options, ...(input.into === undefined ? {} : { into: input.into }), ...(input.undo === true ? { undo: true } : {}) });
|
|
378
|
+
if (!read.ok)
|
|
379
|
+
return read;
|
|
380
|
+
if (!("plan" in read))
|
|
381
|
+
return refuse("nothing", ALREADY_MSG(read.landed.ref, read.landed.commit));
|
|
382
|
+
const plan = read.plan;
|
|
383
|
+
if (plan.key !== input.key)
|
|
384
|
+
return refuse("stale", "Something changed since you looked (the branch, the checks or the result). Look again, then land it.");
|
|
385
|
+
const project = projectOf(store, plan.repo);
|
|
386
|
+
const git = gitFor(plan.repo, options.git);
|
|
387
|
+
const stillStands = () => {
|
|
388
|
+
const who = standing(store, authority, plan.repo);
|
|
389
|
+
if (!who.ok)
|
|
390
|
+
return who;
|
|
391
|
+
if (publishesThroughPullRequests(store, plan.repo))
|
|
392
|
+
return refuse("pull-requests", PULL_REQUESTS_INSTEAD);
|
|
393
|
+
if (plan.kind === "land") {
|
|
394
|
+
const again = assignmentOf(store, taskId, now, { principal: "operator", repos: access.repos }, options.root);
|
|
395
|
+
if (again?.state !== "complete" || again.receipt?.digest !== plan.receipt || again.completion?.digest !== plan.receipt)
|
|
396
|
+
return refuse("stale", "This result changed since you looked. Look again, then land it.");
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
const landed = landingOf(store, plan.rootId);
|
|
400
|
+
if (landed === null || landed.undone || landed.intent.id !== plan.reverts?.intent)
|
|
401
|
+
return refuse("stale", "That landing changed since you looked. Look again.");
|
|
402
|
+
}
|
|
403
|
+
if (JSON.stringify(checkBindingsOf(store, plan.repo)) !== JSON.stringify(plan.bindings))
|
|
404
|
+
return refuse("stale", "The project's checks changed since you looked. Look again, then land it.");
|
|
405
|
+
return who;
|
|
406
|
+
};
|
|
407
|
+
let commit = plan.candidate;
|
|
408
|
+
if (plan.kind === "undo") {
|
|
409
|
+
const pre = standing(store, authority, plan.repo);
|
|
410
|
+
if (!pre.ok)
|
|
411
|
+
return pre;
|
|
412
|
+
const made = await revertCommit(git, plan, options.scratch);
|
|
413
|
+
if (!made.ok)
|
|
414
|
+
return made;
|
|
415
|
+
commit = made.commit;
|
|
416
|
+
}
|
|
417
|
+
let intentId;
|
|
418
|
+
try {
|
|
419
|
+
intentId = store.transact(() => {
|
|
420
|
+
const who = stillStands();
|
|
421
|
+
if (!who.ok)
|
|
422
|
+
throw new Refused(who);
|
|
423
|
+
if (intents(store, "repo = ?", [plan.repo]).some(one => settledOf(store, one.id) === null))
|
|
424
|
+
throw new Refused(refuse("busy", "Another landing in this project hasn't finished. Try again in a moment."));
|
|
425
|
+
if (!plan.recorded)
|
|
426
|
+
recordActionWhole(store.handle, { at: now.toISOString(), actor: who.actor, repo: plan.repo, taskId: null, runId: null, action: LAND_DESTINATION, outcome: plan.ref, source: "request" }, { v: 1, ref: plan.ref, common: project.common });
|
|
427
|
+
return recordActionWhole(store.handle, { at: now.toISOString(), actor: who.actor, repo: plan.repo, taskId: plan.rootId, runId: plan.runId, action: LAND_INTENT, outcome: "pending", source: "request" }, { v: 1, kind: plan.kind, key: plan.key, ref: plan.ref, old: plan.old, commit, mode: "ref", ...(plan.reverts === undefined ? {} : { reverts: plan.reverts.intent }) });
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
if (error instanceof Refused)
|
|
432
|
+
return error.result;
|
|
433
|
+
throw error;
|
|
434
|
+
}
|
|
435
|
+
const settle = (outcome, words) => recordActionWhole(store.handle, { at: now.toISOString(), actor: "toolroll", repo: plan.repo, taskId: plan.rootId, runId: plan.runId,
|
|
436
|
+
action: LAND_SETTLED, outcome: `${intentId}:${outcome}`, source: "work" }, { v: 1, words });
|
|
437
|
+
const blocked = await refProblem(git, plan.repo, plan.ref, plan.old, ownCommand(plan.repo, plan));
|
|
438
|
+
if (blocked !== null) {
|
|
439
|
+
settle("refused", blocked.message);
|
|
440
|
+
return refuse(blocked.reason, blocked.message);
|
|
441
|
+
}
|
|
442
|
+
const last = store.transact(stillStands);
|
|
443
|
+
if (!last.ok) {
|
|
444
|
+
settle("refused", last.message);
|
|
445
|
+
return last;
|
|
446
|
+
}
|
|
447
|
+
const swapped = await git(["update-ref", "--no-deref", "-m", `toolroll: land ${plan.taskId}`, plan.ref, commit, plan.old]);
|
|
448
|
+
if (swapped.code !== 0) {
|
|
449
|
+
const words = `${plan.branch} moved while landing, so nothing changed. Look again, then land it.`;
|
|
450
|
+
settle("refused", words);
|
|
451
|
+
return refuse("moved", words);
|
|
452
|
+
}
|
|
453
|
+
settle("landed", plan.kind === "undo" ? "Undone." : "Landed.");
|
|
454
|
+
return { ok: true, replayed: false, kind: plan.kind, ref: plan.ref, branch: plan.branch, commit: commit, old: plan.old, mode: "ref", warning: null };
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
class Refused extends Error {
|
|
458
|
+
result;
|
|
459
|
+
constructor(result) {
|
|
460
|
+
super(result.message);
|
|
461
|
+
this.result = result;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async function revertCommit(git, plan, scratch) {
|
|
465
|
+
const reverts = plan.reverts;
|
|
466
|
+
const folder = mkdtempSync(join(scratch ?? tmpdir(), "toolroll-undo-"));
|
|
467
|
+
const checkout = join(folder, "checkout");
|
|
468
|
+
let added = false;
|
|
469
|
+
try {
|
|
470
|
+
const add = await git(["worktree", "add", "--detach", checkout, plan.old]);
|
|
471
|
+
added = add.code === 0;
|
|
472
|
+
if (!added)
|
|
473
|
+
return refuse("git", "Toolroll couldn't make a temporary checkout for the undo.");
|
|
474
|
+
const identity = ["-c", "user.name=Toolroll", "-c", "user.email=undo@toolroll.invalid", "-c", "commit.gpgsign=false", "-c", "core.hooksPath=/dev/null"];
|
|
475
|
+
const reverted = await git([...identity, "revert", "--no-edit", "--no-commit", `${reverts.old}..${reverts.landed}`], checkout);
|
|
476
|
+
if (reverted.code !== 0) {
|
|
477
|
+
await git(["revert", "--abort"], checkout);
|
|
478
|
+
return refuse("moved", `Undoing it conflicts with later changes on ${plan.branch}. To undo it yourself: git -C ${plan.repo} revert ${reverts.old}..${reverts.landed}`);
|
|
479
|
+
}
|
|
480
|
+
const committed = await git([...identity, "commit", "--no-verify", "--allow-empty", "-m", `Undo landing of ${plan.taskId} (${short(reverts.old)}..${short(reverts.landed)})`], checkout);
|
|
481
|
+
const head = await git(["rev-parse", "--verify", "-q", "HEAD^{commit}"], checkout);
|
|
482
|
+
const commit = head.stdout.trim();
|
|
483
|
+
if (committed.code !== 0 || head.code !== 0 || !OID.test(commit))
|
|
484
|
+
return refuse("git", "Toolroll couldn't make the undo commit.");
|
|
485
|
+
return { ok: true, commit };
|
|
486
|
+
}
|
|
487
|
+
finally {
|
|
488
|
+
if (added)
|
|
489
|
+
await git(["worktree", "remove", checkout]).catch(() => null);
|
|
490
|
+
rmSync(folder, { recursive: true, force: true });
|
|
491
|
+
await git(["worktree", "prune"]).catch(() => null);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
export function landedWords(store, rootId) {
|
|
495
|
+
const landed = landingOf(store, rootId);
|
|
496
|
+
if (landed === null)
|
|
497
|
+
return null;
|
|
498
|
+
return landed.undone ? `Landed on ${landed.intent.ref.replace(/^refs\/heads\//, "")} (${short(landed.intent.commit)}), then undone.`
|
|
499
|
+
: `Landed on ${landed.intent.ref.replace(/^refs\/heads\//, "")} (${short(landed.intent.commit)}).`;
|
|
500
|
+
}
|