toolroll 0.9.109 → 0.9.111
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 +73 -0
- package/README.md +5 -1
- package/THIRD_PARTY_NOTICES.md +30 -0
- package/dist/agent-detect.js +244 -0
- package/dist/agent-fence.js +12 -1
- package/dist/agent-said-schema.js +4 -0
- package/dist/agent-said.js +23 -0
- package/dist/agentconfig.js +2 -2
- package/dist/approval-summary.js +375 -0
- package/dist/attest.js +2 -2
- package/dist/browser/THIRD_PARTY_NOTICES.txt +30 -0
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +27 -27
- package/dist/builder.js +42 -3
- package/dist/chat-actions.js +24 -17
- package/dist/chat-core.js +3 -1
- package/dist/chat-decide.js +101 -46
- package/dist/chat-delivery-state.js +2 -0
- package/dist/chat-delivery.js +3 -2
- package/dist/chat-live.js +2 -1
- package/dist/chat-task-actions.js +6 -0
- package/dist/check-report.js +1 -1
- package/dist/claim.js +28 -13
- package/dist/cli-help.js +1 -1
- package/dist/cli.js +2 -0
- package/dist/codex-limits.js +21 -8
- package/dist/console-instance.js +11 -1
- package/dist/contracts/console-api.js +13 -7
- package/dist/contracts/engine-api.js +283 -0
- package/dist/contracts/route.js +1 -0
- package/dist/coordinator.js +2 -2
- package/dist/discord-api.js +4 -3
- package/dist/discord-chat.js +3 -1
- package/dist/dispatch.js +1 -1
- package/dist/dispose.js +15 -9
- package/dist/easy-start-args.js +7 -3
- package/dist/easy-start.js +40 -1
- package/dist/egress.js +72 -10
- package/dist/engine-handoff.js +34 -0
- package/dist/engine-reads.js +338 -0
- package/dist/eval-world.js +3 -2
- package/dist/evidence.js +2 -0
- package/dist/fast-start.js +68 -18
- package/dist/fast-terms.js +2 -2
- package/dist/first-run.js +10 -2
- package/dist/flexible-agents.js +234 -0
- package/dist/flexible-answers-schema.js +61 -0
- package/dist/flexible-answers.js +889 -0
- package/dist/flexible-custody.js +494 -0
- package/dist/flexible-import.js +782 -0
- package/dist/flexible-launch-schema.js +72 -0
- package/dist/flexible-launch.js +331 -0
- package/dist/flexible-launcher.js +88 -0
- package/dist/flexible-levels.js +34 -0
- package/dist/flexible-merge-guard.js +47 -0
- package/dist/flexible-refs.js +47 -0
- package/dist/flexible-relay-cancel.js +19 -0
- package/dist/flexible-relay-schema.js +55 -0
- package/dist/flexible-relay-upstreams.js +36 -0
- package/dist/flexible-relay.js +1032 -0
- package/dist/flexible-session.js +732 -0
- package/dist/flexible-ship-schema.js +81 -0
- package/dist/flexible-ship.js +694 -0
- package/dist/flexible-start.js +80 -0
- package/dist/flexible-thread.js +175 -25
- package/dist/flexible-turns-schema.js +69 -0
- package/dist/flexible-turns.js +490 -0
- package/dist/flexible.js +10 -1
- package/dist/flow-actions.js +6 -4
- package/dist/flow-steps.js +1 -1
- package/dist/flows-cli.js +2 -1
- package/dist/git-views.js +6 -3
- package/dist/install-mode.js +225 -0
- package/dist/integrations.js +6 -1
- package/dist/lead-doors.js +7 -1
- package/dist/limits-ui.js +7 -0
- package/dist/live-reply.js +6 -0
- package/dist/live-terminal.js +425 -29
- package/dist/local-owner.js +7 -0
- package/dist/model-choices.js +12 -6
- package/dist/needs-you.js +2 -0
- package/dist/notification-delivery.js +11 -0
- package/dist/operate.js +128 -37
- package/dist/outbound-privacy.js +204 -0
- package/dist/passwordless.js +2 -1
- package/dist/phase-routing.js +3 -1
- package/dist/plane-review.js +1 -1
- package/dist/project-delete.js +14 -0
- package/dist/proposal.js +2 -2
- package/dist/provider-connection.js +19 -5
- package/dist/publish.js +5 -0
- package/dist/pull-request-flow.js +26 -7
- package/dist/push.js +5 -1
- package/dist/release-card.js +11 -3
- package/dist/release.js +10 -1
- package/dist/route-store.js +3 -3
- package/dist/sandbox-launch.js +32 -8
- package/dist/sandbox-runtime.js +16 -7
- package/dist/sandbox-tools.js +176 -0
- package/dist/scope.js +12 -36
- package/dist/scout-net.js +7 -2
- package/dist/serve.js +62 -13
- package/dist/server/chrome.js +3 -1
- package/dist/server/console.css +33 -6
- package/dist/server/engine-api.js +378 -0
- package/dist/server/flows.js +2 -2
- package/dist/server/http.js +7 -0
- package/dist/server/link-redemption.js +52 -0
- package/dist/server/mutation-key.js +21 -7
- package/dist/server/pages.js +72 -40
- package/dist/server/people-tokens.js +17 -41
- package/dist/server/render-chat.js +1 -0
- package/dist/server/render-new-task.js +3 -3
- package/dist/server/render-results.js +4 -4
- package/dist/server/render-tasks.js +133 -139
- package/dist/server/route-table.js +32 -1
- package/dist/server/settings-section-table.js +6 -0
- package/dist/server/settings-sections.js +69 -0
- package/dist/server/settings.js +120 -10
- package/dist/server/task-views.js +8 -2
- package/dist/server/tasks.js +56 -12
- package/dist/server/terminals.js +181 -28
- package/dist/server/welcome.js +35 -10
- package/dist/settings-groups-ui.js +2 -2
- package/dist/settings-proofs.js +14 -9
- package/dist/slack-api.js +4 -3
- package/dist/slack-chat.js +5 -3
- package/dist/spend.js +46 -12
- package/dist/status-probe.js +343 -0
- package/dist/store.js +181 -12
- package/dist/task-sizing-schema.js +14 -0
- package/dist/task-sizing.js +76 -10
- package/dist/teams-api.js +4 -3
- package/dist/teams-chat.js +4 -2
- package/dist/telegram-lead.js +2 -1
- package/dist/telegram-live.js +2 -1
- package/dist/telegram-team.js +2 -1
- package/dist/telegram.js +17 -5
- package/dist/toolroll-update.js +3 -1
- package/dist/trusted-path.js +162 -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-launch.js +282 -0
- package/dist/tui-source.js +5 -3
- package/dist/tui.js +1 -1
- package/dist/webhooks.js +14 -4
- package/dist/welcome-facts.js +22 -15
- package/dist/work-index.js +2 -2
- package/dist/workspace-ui.js +5 -0
- package/package.json +1 -1
- package/scripts/crash-canary.mjs +1 -1
- package/scripts/pilot.mjs +1 -1
- package/scripts/provider-canary.mjs +5 -3
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, constants as fsConstants, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, opendirSync, openSync, readFileSync, readSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
4
|
+
import { FLEXIBLE_AVAILABLE } from "./flexible-available.js";
|
|
5
|
+
import { FLEXIBLE_UNAVAILABLE } from "./flexible.js";
|
|
6
|
+
import { flexibleThread, flexibleThreadBranch, flexibleThreadPaths, withFlexibleThreadLock } from "./flexible-thread.js";
|
|
7
|
+
import { readProjectRefs, refsChangedOutside, refSnapshotOf, saveRefSnapshot } from "./flexible-refs.js";
|
|
8
|
+
import { PROTECTED_FILES, protectedChanges, protectedLinks } from "./agent-fence.js";
|
|
9
|
+
import { scanPatchForSecrets } from "./evidence.js";
|
|
10
|
+
import { PROOF_LIMITS } from "./proof.js";
|
|
11
|
+
import { run } from "./exec.js";
|
|
12
|
+
import { hardenedGit, recordedCommonDir } from "./teardown-git.js";
|
|
13
|
+
import { projectRowKnown } from "./store.js";
|
|
14
|
+
const NOTHING = "Nothing was imported.";
|
|
15
|
+
const PRE_WORDS = {
|
|
16
|
+
"not-available": FLEXIBLE_UNAVAILABLE,
|
|
17
|
+
"thread-unknown": "There's no such thread.",
|
|
18
|
+
"thread-not-ready": "This thread's repository isn't ready, so nothing was imported.",
|
|
19
|
+
"not-a-project": "That thread's project isn't in Toolroll any more, so nothing was imported.",
|
|
20
|
+
"repository-unusable": "This project's repository can't take an import safely, so nothing was imported.",
|
|
21
|
+
"thread-folder-changed": "This thread's folder isn't the one Toolroll made, so nothing was imported.",
|
|
22
|
+
};
|
|
23
|
+
const CHECK_WORDS = Object.freeze({
|
|
24
|
+
"candidate-unreadable": `The thread's branch couldn't be read safely. ${NOTHING}`,
|
|
25
|
+
"candidate-changed": `The thread's branch moved since these terms were shown. ${NOTHING} Review the new terms.`,
|
|
26
|
+
"nothing-to-send": `The thread has no committed changes to send. ${NOTHING}`,
|
|
27
|
+
"not-descendant": `The thread's branch doesn't build on the commit it started from. ${NOTHING}`,
|
|
28
|
+
"merge": `The thread's commits include a merge. Only the thread's own commits on top of its starting commit can be imported. ${NOTHING}`,
|
|
29
|
+
"too-large": `The change is bigger than Toolroll imports at once. ${NOTHING}`,
|
|
30
|
+
"bad-path": `A changed file has a name Toolroll can't import safely. ${NOTHING}`,
|
|
31
|
+
"submodule": `The change adds a submodule. ${NOTHING}`,
|
|
32
|
+
"placeholder": `The change commits one of the placeholder files Toolroll put in the thread. ${NOTHING}`,
|
|
33
|
+
"protected": `The change touches a file Toolroll protects: settings, hooks or startup files that run for whoever opens the project next. ${NOTHING}`,
|
|
34
|
+
"symlink": `The change adds a link that leads outside the project or into its Git folder. ${NOTHING}`,
|
|
35
|
+
"secret": `An added line looks like a real key. ${NOTHING} If it's a real key, rotate it and take it out of the thread's commits.`,
|
|
36
|
+
"unreadable": `The thread's commits couldn't be read safely. ${NOTHING}`,
|
|
37
|
+
"import-exists": `An import with this number is already in your repository. ${NOTHING}`,
|
|
38
|
+
"branch-moved": `The thread's branch in your repository was changed by someone else. ${NOTHING}`,
|
|
39
|
+
"default-branch": `The thread's branch is your repository's checked-out branch. ${NOTHING}`,
|
|
40
|
+
"fetch-failed": `Git refused the thread's commits: they failed its object checks or took too long. ${NOTHING}`,
|
|
41
|
+
"ambiguous": `Two names in the change are the same name on a computer that ignores case or accents. ${NOTHING}`,
|
|
42
|
+
"timed-out": `Checking the change took longer than Toolroll allows for one import. ${NOTHING}`,
|
|
43
|
+
});
|
|
44
|
+
const pre = (reason, detail) => ({ ok: false, reason, message: detail === undefined ? PRE_WORDS[reason] : `${PRE_WORDS[reason]} (${detail})` });
|
|
45
|
+
const refused = (check, detail) => ({ ok: false, reason: "import-refused", check, message: detail === undefined ? CHECK_WORDS[check] : `${CHECK_WORDS[check]} (${detail})` });
|
|
46
|
+
export const FLEXIBLE_IMPORT_LIMITS = Object.freeze({
|
|
47
|
+
commits: 1_000,
|
|
48
|
+
objects: 50_000,
|
|
49
|
+
blobBytes: 16 * 1024 * 1024,
|
|
50
|
+
diskBytes: 256 * 1024 * 1024,
|
|
51
|
+
patchBytes: 64 * 1024 * 1024,
|
|
52
|
+
fetchMs: 120_000,
|
|
53
|
+
objectEntries: 500_000,
|
|
54
|
+
inflatedBytes: 512 * 1024 * 1024,
|
|
55
|
+
treeEntries: 200_000,
|
|
56
|
+
importMs: 300_000,
|
|
57
|
+
});
|
|
58
|
+
const limitsOf = (over) => ({ ...FLEXIBLE_IMPORT_LIMITS, ...(over ?? {}) });
|
|
59
|
+
export const FLEXIBLE_IMPORTS_DIR = ".imports";
|
|
60
|
+
const SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
61
|
+
export const flexibleImportRef = (thread, seq) => `refs/toolroll/import/${thread}/${seq}`;
|
|
62
|
+
function ownFolder(path) {
|
|
63
|
+
try {
|
|
64
|
+
const held = lstatSync(path);
|
|
65
|
+
const uid = process.getuid?.();
|
|
66
|
+
return held.isDirectory() && !held.isSymbolicLink() && (uid === undefined || held.uid === uid) && (held.mode & 0o077) === 0 && realpathSync(path) === join(realpathSync(dirname(path)), basename(path));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const plainDir = (path) => { try {
|
|
73
|
+
const held = lstatSync(path);
|
|
74
|
+
return held.isDirectory() && !held.isSymbolicLink();
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return false;
|
|
78
|
+
} };
|
|
79
|
+
function stateFolderOf(store) {
|
|
80
|
+
const file = store.databaseFile();
|
|
81
|
+
if (file === null || file === "" || file === ":memory:")
|
|
82
|
+
return null;
|
|
83
|
+
try {
|
|
84
|
+
return dirname(realpathSync(file));
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
class DeadlinePassed extends Error {
|
|
91
|
+
}
|
|
92
|
+
const IMPORT_FLAGS = ["--no-replace-objects", "-c", "core.commitGraph=false", "-c", "core.multiPackIndex=false"];
|
|
93
|
+
function gitIn(inner, mode, deadline = Number.POSITIVE_INFINITY) {
|
|
94
|
+
return async (cwd, args, options = {}) => {
|
|
95
|
+
const left = deadline - Date.now();
|
|
96
|
+
if (left <= 0)
|
|
97
|
+
throw new DeadlinePassed("the import's time ran out");
|
|
98
|
+
const ran = await hardenedGit(inner, mode, { repo: cwd })("git", [...IMPORT_FLAGS, ...args], { cwd, timeoutMs: Math.max(1, Math.min(options.timeoutMs ?? 60_000, left)), ...(options.maxBuffer === undefined ? {} : { maxBuffer: options.maxBuffer }),
|
|
99
|
+
...(options.stdin === undefined ? {} : { stdin: options.stdin, processGroup: true }) });
|
|
100
|
+
if (ran.timedOut && Date.now() >= deadline)
|
|
101
|
+
throw new DeadlinePassed("the import's time ran out");
|
|
102
|
+
return ran;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const HFS_IGNORABLE = /[\u200c-\u200f\u202a-\u202e\u206a-\u206f\ufeff]/g;
|
|
106
|
+
export function foldName(part) {
|
|
107
|
+
return part.normalize("NFC").replace(HFS_IGNORABLE, "").toLowerCase().replace(/[. ]+$/, "");
|
|
108
|
+
}
|
|
109
|
+
const foldPath = (path) => path.split("/").map(foldName).join("/");
|
|
110
|
+
const ALTERNATES_MAX = 4096;
|
|
111
|
+
const WALK_TIMED_OUT = "the import's time ran out while its objects were walked";
|
|
112
|
+
function threadObjectsProblem(paths, recorded, objects, limits, deadline) {
|
|
113
|
+
if (recorded !== paths.folder)
|
|
114
|
+
return "its record names another folder";
|
|
115
|
+
if (!ownFolder(paths.root) || !ownFolder(paths.folder))
|
|
116
|
+
return "its folder isn't a private folder of yours";
|
|
117
|
+
const gitdir = join(paths.repo, ".git");
|
|
118
|
+
const threadObjects = join(gitdir, "objects");
|
|
119
|
+
if (!plainDir(paths.repo) || !plainDir(gitdir) || !plainDir(threadObjects))
|
|
120
|
+
return "its repository isn't a plain one";
|
|
121
|
+
if (existsSync(join(gitdir, "commondir")) || existsSync(join(gitdir, "reftable")))
|
|
122
|
+
return "its repository isn't a plain one";
|
|
123
|
+
try {
|
|
124
|
+
if (realpathSync(threadObjects) !== join(realpathSync(paths.folder), "repo", ".git", "objects"))
|
|
125
|
+
return "its objects folder isn't its own";
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return "its objects folder can't be read";
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const alternates = join(threadObjects, "info", "alternates");
|
|
132
|
+
const held = lstatSync(alternates);
|
|
133
|
+
if (!held.isFile() || held.isSymbolicLink())
|
|
134
|
+
return "its alternates changed";
|
|
135
|
+
if (held.size > ALTERNATES_MAX)
|
|
136
|
+
return "its alternates file is too large to be the one Toolroll wrote";
|
|
137
|
+
const fd = openSync(alternates, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
|
|
138
|
+
try {
|
|
139
|
+
const opened = fstatSync(fd);
|
|
140
|
+
if (!opened.isFile() || opened.size > ALTERNATES_MAX)
|
|
141
|
+
return "its alternates file is too large to be the one Toolroll wrote";
|
|
142
|
+
const bytes = Buffer.alloc(ALTERNATES_MAX + 1);
|
|
143
|
+
const read = readSync(fd, bytes, 0, bytes.length, 0);
|
|
144
|
+
if (read > ALTERNATES_MAX || bytes.subarray(0, read).toString("utf8") !== `${objects}\n`)
|
|
145
|
+
return "its alternates changed";
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
closeSync(fd);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return "its alternates can't be read";
|
|
153
|
+
}
|
|
154
|
+
if (existsSync(join(threadObjects, "info", "http-alternates")))
|
|
155
|
+
return "it borrows objects over the network";
|
|
156
|
+
let seen = 0;
|
|
157
|
+
const folders = [threadObjects];
|
|
158
|
+
while (folders.length > 0) {
|
|
159
|
+
const folder = folders.pop();
|
|
160
|
+
let handle;
|
|
161
|
+
try {
|
|
162
|
+
handle = opendirSync(folder);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return "its objects can't be listed";
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
for (let one = handle.readSync(); one !== null; one = handle.readSync()) {
|
|
169
|
+
if (++seen > limits.objectEntries)
|
|
170
|
+
return "it holds too many object files";
|
|
171
|
+
if (Date.now() >= deadline)
|
|
172
|
+
return WALK_TIMED_OUT;
|
|
173
|
+
if (one.isDirectory())
|
|
174
|
+
folders.push(join(folder, one.name));
|
|
175
|
+
else if (!one.isFile())
|
|
176
|
+
return `its objects folder has a link or special file (${join(folder, one.name).slice(threadObjects.length + 1)})`;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return "its objects can't be listed";
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
handle.closeSync();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
function threadBranchTip(paths, id) {
|
|
189
|
+
const gitdir = join(paths.repo, ".git");
|
|
190
|
+
const name = `refs/heads/${flexibleThreadBranch(id)}`;
|
|
191
|
+
let at = gitdir;
|
|
192
|
+
let loose = true;
|
|
193
|
+
for (const part of ["refs", "heads", "toolroll"]) {
|
|
194
|
+
at = join(at, part);
|
|
195
|
+
let held;
|
|
196
|
+
try {
|
|
197
|
+
held = lstatSync(at);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
loose = false;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
if (!held.isDirectory() || held.isSymbolicLink())
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
if (loose) {
|
|
207
|
+
const file = join(gitdir, name);
|
|
208
|
+
let held = null;
|
|
209
|
+
try {
|
|
210
|
+
held = lstatSync(file);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
held = null;
|
|
214
|
+
}
|
|
215
|
+
if (held !== null) {
|
|
216
|
+
if (!held.isFile() || held.isSymbolicLink() || held.size > 200)
|
|
217
|
+
return null;
|
|
218
|
+
const read = /^([0-9a-f]{40}|[0-9a-f]{64})\n?$/.exec(readFileSync(file, "utf8"));
|
|
219
|
+
return read === null ? null : read[1];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const packed = join(gitdir, "packed-refs");
|
|
223
|
+
let held;
|
|
224
|
+
try {
|
|
225
|
+
held = lstatSync(packed);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
if (!held.isFile() || held.isSymbolicLink() || held.size > 32 * 1024 * 1024)
|
|
231
|
+
return null;
|
|
232
|
+
for (const line of readFileSync(packed, "utf8").split("\n")) {
|
|
233
|
+
if (line.startsWith("#") || line.startsWith("^"))
|
|
234
|
+
continue;
|
|
235
|
+
const space = line.indexOf(" ");
|
|
236
|
+
if (space < 0 || line.slice(space + 1) !== name)
|
|
237
|
+
continue;
|
|
238
|
+
const sha = line.slice(0, space);
|
|
239
|
+
return SHA.test(sha) ? sha : null;
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
function sourceOf(store, id, limits, deadline) {
|
|
244
|
+
const thread = flexibleThread(store, id);
|
|
245
|
+
if (thread === null)
|
|
246
|
+
return pre("thread-unknown");
|
|
247
|
+
if (thread.state !== "ready")
|
|
248
|
+
return pre("thread-not-ready");
|
|
249
|
+
if (!projectRowKnown(store, thread.repo))
|
|
250
|
+
return pre("not-a-project");
|
|
251
|
+
const state = stateFolderOf(store);
|
|
252
|
+
if (state === null)
|
|
253
|
+
return pre("repository-unusable", "no state folder");
|
|
254
|
+
let project, objects;
|
|
255
|
+
try {
|
|
256
|
+
project = realpathSync(thread.repo);
|
|
257
|
+
const common = recordedCommonDir(project);
|
|
258
|
+
if (!plainDir(join(common, "objects")))
|
|
259
|
+
throw new Error("objects");
|
|
260
|
+
if (existsSync(join(common, "objects", "info", "alternates")) || existsSync(join(common, "objects", "info", "http-alternates")))
|
|
261
|
+
throw new Error("alternates");
|
|
262
|
+
objects = realpathSync(join(common, "objects"));
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return pre("repository-unusable");
|
|
266
|
+
}
|
|
267
|
+
const paths = flexibleThreadPaths(state, thread.id);
|
|
268
|
+
const problem = threadObjectsProblem(paths, thread.folder, objects, limits, deadline);
|
|
269
|
+
if (problem === WALK_TIMED_OUT)
|
|
270
|
+
return refused("timed-out");
|
|
271
|
+
if (problem !== null)
|
|
272
|
+
return pre("thread-folder-changed", problem);
|
|
273
|
+
return { thread, project, objects, paths, threadObjects: realpathSync(join(paths.repo, ".git", "objects")), format: thread.baseSha.length === 64 ? "sha256" : "sha1" };
|
|
274
|
+
}
|
|
275
|
+
async function withQuarantine(source, work) {
|
|
276
|
+
const imports = join(source.paths.root, FLEXIBLE_IMPORTS_DIR);
|
|
277
|
+
try {
|
|
278
|
+
mkdirSync(imports, { mode: 0o700 });
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
if (error.code !== "EEXIST")
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
if (!ownFolder(imports))
|
|
285
|
+
throw new Error(`${imports} isn't a private folder of yours`);
|
|
286
|
+
const folder = mkdtempSync(join(imports, `${source.thread.id}-`));
|
|
287
|
+
try {
|
|
288
|
+
const repo = join(folder, "repo");
|
|
289
|
+
const gitdir = join(repo, ".git");
|
|
290
|
+
for (const dir of [repo, gitdir, join(gitdir, "objects"), join(gitdir, "objects", "info"), join(gitdir, "objects", "pack"), join(gitdir, "refs"), join(gitdir, "refs", "heads"), join(gitdir, "refs", "tags"), join(gitdir, "info")])
|
|
291
|
+
mkdirSync(dir, { mode: 0o700 });
|
|
292
|
+
writeFileSync(join(gitdir, "HEAD"), "ref: refs/heads/quarantine\n", { mode: 0o600, flag: "wx" });
|
|
293
|
+
const sha256 = source.format === "sha256";
|
|
294
|
+
writeFileSync(join(gitdir, "config"), [
|
|
295
|
+
"[core]", `\trepositoryformatversion = ${sha256 ? 1 : 0}`, "\tfilemode = true", "\tbare = false", "\tlogallrefupdates = false",
|
|
296
|
+
"\tcommitGraph = false", "\tmultiPackIndex = false",
|
|
297
|
+
...(sha256 ? ["[extensions]", "\tobjectFormat = sha256"] : []), "",
|
|
298
|
+
].join("\n"), { mode: 0o600, flag: "wx" });
|
|
299
|
+
writeFileSync(join(gitdir, "objects", "info", "alternates"), `${source.objects}\n${source.threadObjects}\n`, { mode: 0o600, flag: "wx" });
|
|
300
|
+
return await work(repo);
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
rmSync(folder, { recursive: true, force: true });
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function parseRawDiff(stdout) {
|
|
307
|
+
const fields = stdout.split("\0");
|
|
308
|
+
if (fields.at(-1) === "")
|
|
309
|
+
fields.pop();
|
|
310
|
+
if (fields.length % 2 !== 0)
|
|
311
|
+
return null;
|
|
312
|
+
const entries = [];
|
|
313
|
+
for (let at = 0; at < fields.length; at += 2) {
|
|
314
|
+
const meta = /^:(\d{6}) (\d{6}) ([0-9a-f]{40,64}) ([0-9a-f]{40,64}) ([ADMT])$/.exec(fields[at]);
|
|
315
|
+
if (meta === null)
|
|
316
|
+
return null;
|
|
317
|
+
entries.push({ oldMode: meta[1], newMode: meta[2], oldSha: meta[3], newSha: meta[4], status: meta[5], path: fields[at + 1] });
|
|
318
|
+
}
|
|
319
|
+
return entries;
|
|
320
|
+
}
|
|
321
|
+
async function changedEntries(read, repo, base, candidate) {
|
|
322
|
+
const listed = await read(repo, ["diff-tree", "-r", "-z", "--raw", "--no-renames", "--no-commit-id", base, candidate], { maxBuffer: 32 * 1024 * 1024 });
|
|
323
|
+
if (listed.code !== 0)
|
|
324
|
+
return refused("unreadable", "its changes couldn't be listed");
|
|
325
|
+
const entries = parseRawDiff(listed.stdout);
|
|
326
|
+
if (entries === null)
|
|
327
|
+
return refused("unreadable", "its changes couldn't be listed");
|
|
328
|
+
if (entries.length > PROOF_LIMITS.changed)
|
|
329
|
+
return refused("too-large", `${entries.length} changed files`);
|
|
330
|
+
for (const one of entries)
|
|
331
|
+
if (one.path === "" || Buffer.byteLength(one.path) > PROOF_LIMITS.changedPath || /[\x00-\x1f\x7f]/.test(one.path) || one.path.split("/").some(part => part === "" || part === "." || part === ".."))
|
|
332
|
+
return refused("bad-path");
|
|
333
|
+
return entries;
|
|
334
|
+
}
|
|
335
|
+
export async function readFlexibleCandidate(store, input) {
|
|
336
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
337
|
+
return pre("not-available");
|
|
338
|
+
const limits = limitsOf(input.limits);
|
|
339
|
+
const deadline = Date.now() + limits.importMs;
|
|
340
|
+
const source = sourceOf(store, input.thread, limits, deadline);
|
|
341
|
+
if ("ok" in source)
|
|
342
|
+
return source;
|
|
343
|
+
const candidate = threadBranchTip(source.paths, source.thread.id);
|
|
344
|
+
if (candidate === null || candidate.length !== source.thread.baseSha.length)
|
|
345
|
+
return refused("candidate-unreadable");
|
|
346
|
+
if (candidate === source.thread.baseSha)
|
|
347
|
+
return refused("nothing-to-send");
|
|
348
|
+
const read = gitIn(input.runner ?? run, "read", deadline);
|
|
349
|
+
return guarded(() => withQuarantine(source, async (repo) => {
|
|
350
|
+
const lineage = await descends(read, repo, source.thread.baseSha, candidate);
|
|
351
|
+
if (lineage !== null)
|
|
352
|
+
return lineage;
|
|
353
|
+
const entries = await changedEntries(read, repo, source.thread.baseSha, candidate);
|
|
354
|
+
if (!Array.isArray(entries))
|
|
355
|
+
return entries;
|
|
356
|
+
if (entries.length === 0)
|
|
357
|
+
return refused("nothing-to-send");
|
|
358
|
+
return { ok: true, candidate, base: source.thread.baseSha, changedPaths: entries.map(one => one.path).sort() };
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
async function guarded(work) {
|
|
362
|
+
try {
|
|
363
|
+
return await work();
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
if (error instanceof DeadlinePassed)
|
|
367
|
+
return refused("timed-out");
|
|
368
|
+
return refused("unreadable", error instanceof Error ? error.message.slice(0, 200) : undefined);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
async function descends(read, repo, base, candidate) {
|
|
372
|
+
const type = await read(repo, ["cat-file", "-t", candidate]);
|
|
373
|
+
if (type.code !== 0 || type.stdout.trim() !== "commit")
|
|
374
|
+
return refused("candidate-unreadable", "it isn't a commit Toolroll can read");
|
|
375
|
+
const lineage = await read(repo, ["merge-base", "--is-ancestor", base, candidate]);
|
|
376
|
+
if (lineage.code === 1)
|
|
377
|
+
return refused("not-descendant");
|
|
378
|
+
if (lineage.code !== 0)
|
|
379
|
+
return refused("unreadable");
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
function placeholderBlobs(format) {
|
|
383
|
+
return new Set(["", "{}\n"].map(text => createHash(format === "sha256" ? "sha256" : "sha1").update(`blob ${Buffer.byteLength(text)}\0${text}`, "utf8").digest("hex")));
|
|
384
|
+
}
|
|
385
|
+
const PLACEHOLDER_NAMES = new Set(PROTECTED_FILES.filter(name => !name.startsWith(".git/")));
|
|
386
|
+
export function linkLeadsOut(path, target) {
|
|
387
|
+
if (target === "" || /[\0\\]/.test(target) || isAbsolute(target) || /^[A-Za-z]:/.test(target))
|
|
388
|
+
return true;
|
|
389
|
+
const parts = path.split("/").slice(0, -1);
|
|
390
|
+
for (const part of target.split("/")) {
|
|
391
|
+
if (part === "" || part === ".")
|
|
392
|
+
continue;
|
|
393
|
+
if (part === "..") {
|
|
394
|
+
if (parts.length === 0)
|
|
395
|
+
return true;
|
|
396
|
+
parts.pop();
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (foldName(part) === ".git")
|
|
400
|
+
return true;
|
|
401
|
+
parts.push(part);
|
|
402
|
+
}
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
function parseRawLog(stdout) {
|
|
406
|
+
const fields = stdout.split("\0").map(field => field.replace(/^\n/, ""));
|
|
407
|
+
if (fields.at(-1) === "")
|
|
408
|
+
fields.pop();
|
|
409
|
+
const commits = [];
|
|
410
|
+
for (let at = 0; at < fields.length; at++) {
|
|
411
|
+
const field = fields[at];
|
|
412
|
+
const header = /^commit:([0-9a-f]{40}|[0-9a-f]{64})$/.exec(field);
|
|
413
|
+
if (header !== null) {
|
|
414
|
+
commits.push({ commit: header[1], entries: [] });
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const meta = /^:(\d{6}) (\d{6}) ([0-9a-f]{40,64}) ([0-9a-f]{40,64}) ([ADMT])$/.exec(field);
|
|
418
|
+
const path = fields[at + 1];
|
|
419
|
+
if (meta === null || path === undefined || commits.length === 0)
|
|
420
|
+
return null;
|
|
421
|
+
commits.at(-1).entries.push({ oldMode: meta[1], newMode: meta[2], oldSha: meta[3], newSha: meta[4], status: meta[5], path });
|
|
422
|
+
at++;
|
|
423
|
+
}
|
|
424
|
+
return commits;
|
|
425
|
+
}
|
|
426
|
+
async function treeOf(read, repo, commit, limits) {
|
|
427
|
+
const listed = await read(repo, ["ls-tree", "-r", "-t", "-z", "--full-tree", commit], { maxBuffer: limits.treeEntries * 160 });
|
|
428
|
+
if (listed.code !== 0)
|
|
429
|
+
return listed.stdout.length >= limits.treeEntries * 150 ? refused("too-large", "its tree has too many entries") : refused("unreadable", "its tree couldn't be listed");
|
|
430
|
+
const tree = new Map();
|
|
431
|
+
const entries = listed.stdout.split("\0").filter(one => one !== "");
|
|
432
|
+
if (entries.length > limits.treeEntries)
|
|
433
|
+
return refused("too-large", "its tree has too many entries");
|
|
434
|
+
for (const entry of entries) {
|
|
435
|
+
const found = /^(\d{6}) (blob|tree|commit) ([0-9a-f]{40,64})\t(.+)$/s.exec(entry);
|
|
436
|
+
if (found === null)
|
|
437
|
+
return refused("unreadable", "its tree couldn't be read");
|
|
438
|
+
const path = found[4], folded = foldPath(path);
|
|
439
|
+
const other = tree.get(folded);
|
|
440
|
+
if (other !== undefined && other.path !== path)
|
|
441
|
+
return refused("ambiguous", `${other.path} and ${path}, in commit ${commit.slice(0, 12)}`);
|
|
442
|
+
tree.set(folded, { path, mode: found[1], sha: found[3] });
|
|
443
|
+
}
|
|
444
|
+
return tree;
|
|
445
|
+
}
|
|
446
|
+
async function linkEscaping(read, repo, tree, links) {
|
|
447
|
+
const targets = new Map();
|
|
448
|
+
const targetOf = async (entry) => {
|
|
449
|
+
if (!targets.has(entry.sha)) {
|
|
450
|
+
const blob = await read(repo, ["cat-file", "blob", entry.sha], { maxBuffer: 8192 });
|
|
451
|
+
targets.set(entry.sha, blob.code !== 0 || blob.stdout.length > 4096 ? null : blob.stdout);
|
|
452
|
+
}
|
|
453
|
+
return targets.get(entry.sha);
|
|
454
|
+
};
|
|
455
|
+
for (const link of links) {
|
|
456
|
+
const entry = tree.get(foldPath(link));
|
|
457
|
+
if (entry === undefined || entry.path !== link || entry.mode !== "120000")
|
|
458
|
+
return link;
|
|
459
|
+
const start = await targetOf(entry);
|
|
460
|
+
if (start === null || linkLeadsOut(link, start))
|
|
461
|
+
return link;
|
|
462
|
+
const stack = link.split("/").slice(0, -1);
|
|
463
|
+
let queue = start.split("/");
|
|
464
|
+
let steps = 0;
|
|
465
|
+
let escaped = false;
|
|
466
|
+
while (queue.length > 0 && !escaped) {
|
|
467
|
+
const part = queue.shift();
|
|
468
|
+
if (part === "" || part === ".")
|
|
469
|
+
continue;
|
|
470
|
+
if (part === "..") {
|
|
471
|
+
if (stack.length === 0)
|
|
472
|
+
escaped = true;
|
|
473
|
+
else
|
|
474
|
+
stack.pop();
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
if (foldName(part) === ".git") {
|
|
478
|
+
escaped = true;
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
stack.push(part);
|
|
482
|
+
const here = stack.join("/");
|
|
483
|
+
const found = tree.get(foldPath(here));
|
|
484
|
+
if (found === undefined)
|
|
485
|
+
continue;
|
|
486
|
+
if (found.path !== here) {
|
|
487
|
+
escaped = true;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (found.mode !== "120000")
|
|
491
|
+
continue;
|
|
492
|
+
if (++steps > 40) {
|
|
493
|
+
escaped = true;
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const next = await targetOf(found);
|
|
497
|
+
if (next === null || next === "" || /[\0\\]/.test(next) || isAbsolute(next) || /^[A-Za-z]:/.test(next)) {
|
|
498
|
+
escaped = true;
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
stack.pop();
|
|
502
|
+
queue = [...next.split("/"), ...queue];
|
|
503
|
+
}
|
|
504
|
+
if (escaped)
|
|
505
|
+
return link;
|
|
506
|
+
}
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
async function quarantineChecks(read, repo, source, candidate, limits) {
|
|
510
|
+
const base = source.thread.baseSha;
|
|
511
|
+
if (candidate === base)
|
|
512
|
+
return refused("nothing-to-send");
|
|
513
|
+
const lineage = await descends(read, repo, base, candidate);
|
|
514
|
+
if (lineage !== null)
|
|
515
|
+
return lineage;
|
|
516
|
+
const merges = await read(repo, ["rev-list", "--min-parents=2", "--max-count=1", `${base}..${candidate}`]);
|
|
517
|
+
if (merges.code !== 0)
|
|
518
|
+
return refused("unreadable");
|
|
519
|
+
if (merges.stdout.trim() !== "")
|
|
520
|
+
return refused("merge", `commit ${merges.stdout.trim().slice(0, 12)}`);
|
|
521
|
+
const counted = await read(repo, ["rev-list", "--count", `${base}..${candidate}`]);
|
|
522
|
+
const commits = Number(counted.stdout.trim());
|
|
523
|
+
if (counted.code !== 0 || !Number.isSafeInteger(commits))
|
|
524
|
+
return refused("unreadable");
|
|
525
|
+
if (commits > limits.commits)
|
|
526
|
+
return refused("too-large", `${commits} commits`);
|
|
527
|
+
const objects = await read(repo, ["rev-list", "--objects", "--no-object-names", `--filter=blob:limit=${limits.blobBytes}`, "--filter-print-omitted", `${base}..${candidate}`], { maxBuffer: (limits.objects + 1) * 70 });
|
|
528
|
+
if (objects.code !== 0)
|
|
529
|
+
return refused(objects.stdout.length >= limits.objects * 41 ? "too-large" : "unreadable", "too many objects, or they couldn't be listed");
|
|
530
|
+
const listed = objects.stdout.split("\n").filter(line => line !== "");
|
|
531
|
+
if (listed.some(line => line.startsWith("~")))
|
|
532
|
+
return refused("too-large", `a file is over ${Math.round(limits.blobBytes / 1024 / 1024)} MB`);
|
|
533
|
+
if (listed.length > limits.objects)
|
|
534
|
+
return refused("too-large", `${listed.length} objects`);
|
|
535
|
+
if (listed.some(line => !SHA.test(line)))
|
|
536
|
+
return refused("unreadable");
|
|
537
|
+
if (listed.length > 0) {
|
|
538
|
+
const sized = await read(repo, ["cat-file", "--batch-check=%(objectsize)"], { stdin: `${listed.join("\n")}\n`, maxBuffer: (listed.length + 1) * 24 });
|
|
539
|
+
const sizes = sized.code === 0 ? sized.stdout.split("\n").filter(line => line !== "") : null;
|
|
540
|
+
if (sizes === null || sizes.length !== listed.length || sizes.some(one => !/^\d{1,15}$/.test(one)))
|
|
541
|
+
return refused("unreadable", "its objects' sizes couldn't be read");
|
|
542
|
+
const inflated = sizes.reduce((sum, one) => sum + Number(one), 0);
|
|
543
|
+
if (inflated > limits.inflatedBytes)
|
|
544
|
+
return refused("too-large", `${Math.round(inflated / 1024 / 1024)} MB uncompressed`);
|
|
545
|
+
}
|
|
546
|
+
const disk = await read(repo, ["rev-list", "--objects", "--disk-usage", `${base}..${candidate}`]);
|
|
547
|
+
const bytes = Number(disk.stdout.trim());
|
|
548
|
+
if (disk.code !== 0 || !Number.isSafeInteger(bytes))
|
|
549
|
+
return refused("unreadable");
|
|
550
|
+
if (bytes > limits.diskBytes)
|
|
551
|
+
return refused("too-large", `${Math.round(bytes / 1024 / 1024)} MB`);
|
|
552
|
+
const net = await changedEntries(read, repo, base, candidate);
|
|
553
|
+
if (!Array.isArray(net))
|
|
554
|
+
return net;
|
|
555
|
+
const netSubmodule = net.find(one => one.status !== "D" && one.newMode === "160000");
|
|
556
|
+
if (netSubmodule !== undefined)
|
|
557
|
+
return refused("submodule", netSubmodule.path);
|
|
558
|
+
const range = await read(repo, ["rev-list", "--reverse", `${base}..${candidate}`], { maxBuffer: (limits.commits + 1) * 70 });
|
|
559
|
+
const inRange = range.code === 0 ? range.stdout.split("\n").filter(line => line !== "") : null;
|
|
560
|
+
if (inRange === null || inRange.length !== commits || inRange.some(one => !SHA.test(one)))
|
|
561
|
+
return refused("unreadable", "its commits couldn't be listed");
|
|
562
|
+
const logged = await read(repo, ["log", "--reverse", "--no-renames", "--diff-merges=first-parent", "--ignore-submodules=none", "--raw", "-z", "--no-abbrev", "--format=commit:%H", `${base}..${candidate}`], { maxBuffer: 64 * 1024 * 1024 });
|
|
563
|
+
const perCommit = logged.code === 0 ? parseRawLog(logged.stdout) : null;
|
|
564
|
+
if (perCommit === null || perCommit.length !== inRange.length || perCommit.some((one, at) => one.commit !== inRange[at]))
|
|
565
|
+
return refused("unreadable", "its commits couldn't be listed one by one");
|
|
566
|
+
const placeholders = placeholderBlobs(source.format);
|
|
567
|
+
for (const { commit, entries } of perCommit) {
|
|
568
|
+
const at = `in commit ${commit.slice(0, 12)}`;
|
|
569
|
+
for (const one of entries)
|
|
570
|
+
if (one.path === "" || Buffer.byteLength(one.path) > PROOF_LIMITS.changedPath || /[\x00-\x1f\x7f]/.test(one.path) || one.path.split("/").some(part => part === "" || part === "." || part === ".."))
|
|
571
|
+
return refused("bad-path", at);
|
|
572
|
+
const added = entries.filter(one => one.status !== "D");
|
|
573
|
+
const submodule = added.find(one => one.newMode === "160000");
|
|
574
|
+
if (submodule !== undefined)
|
|
575
|
+
return refused("submodule", `${submodule.path}, ${at}`);
|
|
576
|
+
const placeholder = added.find(one => PLACEHOLDER_NAMES.has(one.path) && placeholders.has(one.newSha));
|
|
577
|
+
if (placeholder !== undefined)
|
|
578
|
+
return refused("placeholder", `${placeholder.path}, ${at}`);
|
|
579
|
+
const gitNamed = entries.find(one => one.path.split("/").some(part => foldName(part) === ".git"));
|
|
580
|
+
if (gitNamed !== undefined)
|
|
581
|
+
return refused("protected", `${gitNamed.path}, ${at}`);
|
|
582
|
+
const named = protectedChanges(entries.map(one => foldPath(one.path)));
|
|
583
|
+
if (named.length > 0)
|
|
584
|
+
return refused("protected", `${named.slice(0, 3).join(", ")}, ${at}`);
|
|
585
|
+
const links = added.filter(one => one.newMode === "120000");
|
|
586
|
+
const linkedProtected = protectedLinks(links.map(one => foldPath(one.path)));
|
|
587
|
+
if (linkedProtected.length > 0)
|
|
588
|
+
return refused("protected", `${linkedProtected.slice(0, 3).join(", ")}, ${at}`);
|
|
589
|
+
if (links.length > 0) {
|
|
590
|
+
const tree = await treeOf(read, repo, commit, limits);
|
|
591
|
+
if (!(tree instanceof Map))
|
|
592
|
+
return tree;
|
|
593
|
+
const escaping = await linkEscaping(read, repo, tree, links.map(one => one.path));
|
|
594
|
+
if (escaping !== null)
|
|
595
|
+
return refused("symlink", `${escaping}, ${at}`);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const patch = await read(repo, ["log", "-p", "--reverse", "--no-ext-diff", "--no-textconv", "--no-color", "--no-renames", "--diff-merges=first-parent", "--ignore-submodules=none", "--format=commit %H", `${base}..${candidate}`], { maxBuffer: limits.patchBytes });
|
|
599
|
+
if (patch.code !== 0)
|
|
600
|
+
return refused(patch.stdout.length >= limits.patchBytes ? "too-large" : "unreadable", "its commits' diffs couldn't be read whole");
|
|
601
|
+
const patched = [...patch.stdout.matchAll(/^commit ([0-9a-f]{40}|[0-9a-f]{64})$/gm)].map(match => match[1]);
|
|
602
|
+
if (patched.length !== inRange.length || patched.some((one, at) => one !== inRange[at]))
|
|
603
|
+
return refused("unreadable", "its commits' diffs couldn't be read one by one");
|
|
604
|
+
const hits = scanPatchForSecrets(patch.stdout);
|
|
605
|
+
if (hits.length > 0)
|
|
606
|
+
return refused("secret", hits.slice(0, 3).map(hit => `${hit.file}:${hit.fileLine} (${hit.name})`).join(", "));
|
|
607
|
+
const whole = await treeOf(read, repo, candidate, limits);
|
|
608
|
+
if (!(whole instanceof Map))
|
|
609
|
+
return whole;
|
|
610
|
+
if (net.length === 0)
|
|
611
|
+
return refused("nothing-to-send");
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
export async function importFlexibleCandidate(store, input) {
|
|
615
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
616
|
+
return pre("not-available");
|
|
617
|
+
const locked = await withFlexibleThreadLock(input.thread, () => guarded(() => importLocked(store, input)));
|
|
618
|
+
if (locked.ok === false && (locked.reason === "not-available" || locked.reason === "thread-unknown"))
|
|
619
|
+
return pre(locked.reason);
|
|
620
|
+
return locked;
|
|
621
|
+
}
|
|
622
|
+
async function importLocked(store, input) {
|
|
623
|
+
const limits = limitsOf(input.limits);
|
|
624
|
+
const deadline = Date.now() + limits.importMs;
|
|
625
|
+
if (!Number.isSafeInteger(input.seq) || input.seq < 1 || !SHA.test(input.expected) || (input.branchBefore !== null && !SHA.test(input.branchBefore)))
|
|
626
|
+
return refused("unreadable", "the import's own terms aren't valid");
|
|
627
|
+
const source = sourceOf(store, input.thread, limits, deadline);
|
|
628
|
+
if ("ok" in source)
|
|
629
|
+
return source;
|
|
630
|
+
const inner = input.runner ?? run;
|
|
631
|
+
const read = gitIn(inner, "read", deadline), build = gitIn(inner, "build", deadline);
|
|
632
|
+
const candidate = threadBranchTip(source.paths, source.thread.id);
|
|
633
|
+
if (candidate === null || candidate.length !== source.thread.baseSha.length)
|
|
634
|
+
return refused("candidate-unreadable");
|
|
635
|
+
if (candidate !== input.expected)
|
|
636
|
+
return refused("candidate-changed");
|
|
637
|
+
const importRef = flexibleImportRef(source.thread.id, input.seq);
|
|
638
|
+
const branch = flexibleThreadBranch(source.thread.id);
|
|
639
|
+
const zero = "0".repeat(candidate.length);
|
|
640
|
+
return withQuarantine(source, async (repo) => {
|
|
641
|
+
const problem = await quarantineChecks(read, repo, source, candidate, limits);
|
|
642
|
+
if (problem !== null)
|
|
643
|
+
return problem;
|
|
644
|
+
if ((await read(source.project, ["rev-parse", "--verify", "--quiet", "--end-of-options", importRef])).code === 0)
|
|
645
|
+
return refused("import-exists");
|
|
646
|
+
const current = await read(source.project, ["rev-parse", "--verify", "--quiet", "--end-of-options", `refs/heads/${branch}`]);
|
|
647
|
+
const held = current.code === 0 ? current.stdout.trim() : null;
|
|
648
|
+
if (held !== input.branchBefore)
|
|
649
|
+
return refused("branch-moved");
|
|
650
|
+
const head = await read(source.project, ["symbolic-ref", "-q", "HEAD"]);
|
|
651
|
+
if (head.code === 0 && head.stdout.trim() === `refs/heads/${branch}`)
|
|
652
|
+
return refused("default-branch");
|
|
653
|
+
const before = await readProjectRefs(read, source.project);
|
|
654
|
+
const snapshot = refSnapshotOf(store, source.thread.id);
|
|
655
|
+
const refsChanged = before === null || snapshot === null ? null : refsChangedOutside(snapshot, before);
|
|
656
|
+
const named = await build(repo, ["update-ref", "--no-deref", "refs/heads/candidate", candidate, zero]);
|
|
657
|
+
if (named.code !== 0)
|
|
658
|
+
return refused("unreadable", "the quarantine couldn't name the candidate");
|
|
659
|
+
const settle = gitIn(inner, "read"), finish = gitIn(inner, "build");
|
|
660
|
+
let fetched = null;
|
|
661
|
+
try {
|
|
662
|
+
fetched = await build(source.project, ["-c", "protocol.file.allow=always", "-c", "transfer.fsckObjects=true", "-c", "fetch.fsckObjects=true", "-c", "fetch.writeCommitGraph=false",
|
|
663
|
+
"fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "--no-recurse-submodules", "--no-auto-gc", `--negotiation-tip=${source.thread.baseSha}`, "--", repo, `refs/heads/candidate:${importRef}`], { timeoutMs: limits.fetchMs });
|
|
664
|
+
}
|
|
665
|
+
catch (error) {
|
|
666
|
+
if (!(error instanceof DeadlinePassed))
|
|
667
|
+
throw error;
|
|
668
|
+
}
|
|
669
|
+
const landed = await settle(source.project, ["rev-parse", "--verify", "--quiet", "--end-of-options", importRef]);
|
|
670
|
+
if (fetched === null || fetched.code !== 0 || landed.code !== 0 || landed.stdout.trim() !== candidate) {
|
|
671
|
+
if (landed.code === 0)
|
|
672
|
+
await finish(source.project, ["update-ref", "--no-deref", "-d", importRef, landed.stdout.trim()]);
|
|
673
|
+
return refused(fetched === null ? "timed-out" : "fetch-failed");
|
|
674
|
+
}
|
|
675
|
+
const moved = await finish(source.project, ["update-ref", "--no-deref", "-m", `toolroll: Flexible import ${input.seq}`, `refs/heads/${branch}`, candidate, input.branchBefore ?? zero]);
|
|
676
|
+
if (moved.code !== 0) {
|
|
677
|
+
await finish(source.project, ["update-ref", "--no-deref", "-d", importRef, candidate]);
|
|
678
|
+
return refused("branch-moved");
|
|
679
|
+
}
|
|
680
|
+
const after = await readProjectRefs(settle, source.project);
|
|
681
|
+
store.transact(() => {
|
|
682
|
+
if (after !== null)
|
|
683
|
+
saveRefSnapshot(store, source.thread.id, after, input.now);
|
|
684
|
+
if (refsChanged !== null && refsChanged.length > 0) {
|
|
685
|
+
store.recordAction({ at: input.now.toISOString(), actor: "toolroll", repo: source.thread.repo, taskId: null, runId: null, source: "policy", action: "flexible refs changed", outcome: "changed-by-someone-else",
|
|
686
|
+
detail: `thread ${source.thread.id} · while it ran: ${refsChanged.slice(0, 20).join(", ")}${refsChanged.length > 20 ? ` and ${refsChanged.length - 20} more` : ""}`.slice(0, 2000) });
|
|
687
|
+
}
|
|
688
|
+
store.recordAction({ at: input.now.toISOString(), actor: input.by, repo: source.thread.repo, taskId: null, runId: null, source: "policy", action: "flexible import", outcome: "imported",
|
|
689
|
+
detail: `thread ${source.thread.id} · send ${input.seq} · ${candidate} · ${importRef} · ${branch}` });
|
|
690
|
+
});
|
|
691
|
+
return { ok: true, candidate, importRef, branch, branchBefore: input.branchBefore, refsChanged };
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
export async function removeFlexibleImportRefs(store, input) {
|
|
695
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
696
|
+
return pre("not-available");
|
|
697
|
+
const locked = await withFlexibleThreadLock(input.thread, async () => {
|
|
698
|
+
const thread = flexibleThread(store, input.thread);
|
|
699
|
+
if (thread === null)
|
|
700
|
+
return pre("thread-unknown");
|
|
701
|
+
if (!SHA.test(input.candidate) || (input.branchBefore !== null && !SHA.test(input.branchBefore)))
|
|
702
|
+
return refused("unreadable");
|
|
703
|
+
let project;
|
|
704
|
+
try {
|
|
705
|
+
project = realpathSync(thread.repo);
|
|
706
|
+
recordedCommonDir(project);
|
|
707
|
+
}
|
|
708
|
+
catch {
|
|
709
|
+
return pre("repository-unusable");
|
|
710
|
+
}
|
|
711
|
+
const inner = input.runner ?? run;
|
|
712
|
+
const read = gitIn(inner, "read"), build = gitIn(inner, "build");
|
|
713
|
+
const branch = `refs/heads/${flexibleThreadBranch(thread.id)}`;
|
|
714
|
+
const current = await read(project, ["rev-parse", "--verify", "--quiet", "--end-of-options", branch]);
|
|
715
|
+
if (current.code !== 0 || current.stdout.trim() !== input.candidate)
|
|
716
|
+
return refused("branch-moved");
|
|
717
|
+
const back = input.branchBefore === null ? await build(project, ["update-ref", "--no-deref", "-d", branch, input.candidate])
|
|
718
|
+
: await build(project, ["update-ref", "--no-deref", "-m", "toolroll: Flexible import removed", branch, input.branchBefore, input.candidate]);
|
|
719
|
+
if (back.code !== 0)
|
|
720
|
+
return refused("branch-moved");
|
|
721
|
+
await build(project, ["update-ref", "--no-deref", "-d", flexibleImportRef(thread.id, input.seq), input.candidate]);
|
|
722
|
+
return { ok: true };
|
|
723
|
+
});
|
|
724
|
+
if (locked.ok === false && (locked.reason === "not-available" || locked.reason === "thread-unknown"))
|
|
725
|
+
return pre(locked.reason);
|
|
726
|
+
return locked;
|
|
727
|
+
}
|
|
728
|
+
export async function flexibleTaskBranch(store, input) {
|
|
729
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
730
|
+
return false;
|
|
731
|
+
const thread = flexibleThread(store, input.thread);
|
|
732
|
+
if (thread === null || !SHA.test(input.at) || !/^toolroll\/flexible-[0-9a-f]{32}$/.test(input.branch))
|
|
733
|
+
return false;
|
|
734
|
+
let project;
|
|
735
|
+
try {
|
|
736
|
+
project = realpathSync(thread.repo);
|
|
737
|
+
recordedCommonDir(project);
|
|
738
|
+
}
|
|
739
|
+
catch {
|
|
740
|
+
return false;
|
|
741
|
+
}
|
|
742
|
+
const build = gitIn(input.runner ?? run, "build");
|
|
743
|
+
const ref = `refs/heads/${input.branch}`;
|
|
744
|
+
const done = input.make ? await build(project, ["update-ref", "--no-deref", "--create-reflog", "-m", "toolroll: Flexible check task", ref, input.at, "0".repeat(input.at.length)])
|
|
745
|
+
: await build(project, ["update-ref", "--no-deref", "-d", ref, input.at]);
|
|
746
|
+
return done.code === 0;
|
|
747
|
+
}
|
|
748
|
+
export async function settleInterruptedImport(store, input) {
|
|
749
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
750
|
+
return pre("not-available");
|
|
751
|
+
const thread = flexibleThread(store, input.thread);
|
|
752
|
+
if (thread === null)
|
|
753
|
+
return pre("thread-unknown");
|
|
754
|
+
if (!SHA.test(input.candidate) || (input.branchBefore !== null && !SHA.test(input.branchBefore)))
|
|
755
|
+
return refused("unreadable");
|
|
756
|
+
let project;
|
|
757
|
+
try {
|
|
758
|
+
project = realpathSync(thread.repo);
|
|
759
|
+
recordedCommonDir(project);
|
|
760
|
+
}
|
|
761
|
+
catch {
|
|
762
|
+
return pre("repository-unusable");
|
|
763
|
+
}
|
|
764
|
+
const inner = input.runner ?? run;
|
|
765
|
+
const read = gitIn(inner, "read"), build = gitIn(inner, "build");
|
|
766
|
+
const value = async (ref) => { const out = await read(project, ["rev-parse", "--verify", "--quiet", "--end-of-options", ref]); return out.code === 0 ? out.stdout.trim() : null; };
|
|
767
|
+
const importRef = flexibleImportRef(thread.id, input.seq);
|
|
768
|
+
const branch = `refs/heads/${flexibleThreadBranch(thread.id)}`;
|
|
769
|
+
const heldImport = await value(importRef);
|
|
770
|
+
let importState = heldImport === null ? "absent" : "moved";
|
|
771
|
+
if (heldImport === input.candidate && (await build(project, ["update-ref", "--no-deref", "-d", importRef, input.candidate])).code === 0)
|
|
772
|
+
importState = "removed";
|
|
773
|
+
const heldBranch = await value(branch);
|
|
774
|
+
let branchState = heldBranch === input.branchBefore ? "untouched" : "moved";
|
|
775
|
+
if (heldBranch === input.candidate && input.candidate !== input.branchBefore) {
|
|
776
|
+
const back = input.branchBefore === null ? await build(project, ["update-ref", "--no-deref", "-d", branch, input.candidate])
|
|
777
|
+
: await build(project, ["update-ref", "--no-deref", "-m", "toolroll: interrupted Flexible import put back", branch, input.branchBefore, input.candidate]);
|
|
778
|
+
if (back.code === 0)
|
|
779
|
+
branchState = "restored";
|
|
780
|
+
}
|
|
781
|
+
return { ok: true, importRef: importState, branch: branchState };
|
|
782
|
+
}
|