@discovery-tree/runner 0.1.16 → 0.1.17
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/dist/cli.js +307 -64
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -48,6 +48,58 @@ var AGENT_RUN_MAX_WALL_MS = 6 * 60 * 60 * 1e3;
|
|
|
48
48
|
var AGENT_RUN_ABANDONED_GRACE_MS = 30 * 60 * 1e3;
|
|
49
49
|
var AGENT_RUN_AUTO_RECOVER_MAX_MS = 5 * 6e4;
|
|
50
50
|
|
|
51
|
+
// ../contracts/src/discovered-work.ts
|
|
52
|
+
var MAX_DISCOVERED_TASKS_PER_TURN = 5;
|
|
53
|
+
var EXAMPLE_TITLE = "Return 404 instead of 500 when an invoice id is unknown";
|
|
54
|
+
var DISCOVERED_WORK_INSTRUCTION = `## Work you discover
|
|
55
|
+
|
|
56
|
+
While doing this task you may find work that needs doing but is not this task: a bug next to
|
|
57
|
+
the code you changed, a follow-up the change makes necessary, a missing piece the outcome
|
|
58
|
+
needs. Do not widen this task to do it, and do not leave it only in prose. Add it to the plan:
|
|
59
|
+
end your response with one block per task. Each is added to the tree, or proposed for a person
|
|
60
|
+
to accept, as the tree is set up; a person starts it.
|
|
61
|
+
|
|
62
|
+
<!-- DT-DISCOVERED
|
|
63
|
+
title: ${EXAMPLE_TITLE}
|
|
64
|
+
waits_on: nothing
|
|
65
|
+
channel: ai
|
|
66
|
+
complexity: low
|
|
67
|
+
impact: medium
|
|
68
|
+
model_reason: one handler and its test; callers show the raw 500 today
|
|
69
|
+
---
|
|
70
|
+
Goal: GET /api/invoices/:id answers 500 for an unknown id; it should answer 404.
|
|
71
|
+
Context: found while adding invoice export. \`InvoicesService.findOne\` throws a plain Error.
|
|
72
|
+
Files: backend/src/invoices/invoices.service.ts, invoices.service.spec.ts
|
|
73
|
+
Approach: throw NotFoundException when the row is missing, as OrdersService does.
|
|
74
|
+
Out of scope: other endpoints' error codes.
|
|
75
|
+
Verify: a spec for the unknown id; pnpm --filter backend test invoices.
|
|
76
|
+
-->
|
|
77
|
+
|
|
78
|
+
Rules:
|
|
79
|
+
- Only work you found by doing this task, that you are not doing, and that someone should do.
|
|
80
|
+
Not what you did, not a wish list, not this task restated. Most runs discover nothing; then
|
|
81
|
+
write no block.
|
|
82
|
+
- At most ${MAX_DISCOVERED_TASKS_PER_TURN} blocks. One block is one pull request's worth of work.
|
|
83
|
+
- \`title\` is the deliverable, one line. Below \`---\`, a brief another agent can carry out cold:
|
|
84
|
+
Goal, Context (what you found and where), Files, Approach, Out of scope, Verify.
|
|
85
|
+
- \`waits_on: this\` when it builds on this task's changes, so it starts after they land;
|
|
86
|
+
\`waits_on: nothing\` when it does not.
|
|
87
|
+
- Do not write \`-->\` inside a block: it ends the block.
|
|
88
|
+
- \`channel: human\` for a decision or approval only a person can give; otherwise \`ai\`.
|
|
89
|
+
- For an \`ai\` task, rate \`complexity\` and \`impact\` (low, medium, high) with a one-line
|
|
90
|
+
\`model_reason\`; they choose the model it runs on.`;
|
|
91
|
+
|
|
92
|
+
// ../contracts/src/environments.ts
|
|
93
|
+
var ENV_SETUP_SCRIPT_MAX_LENGTH = 16 * 1024;
|
|
94
|
+
|
|
95
|
+
// ../contracts/src/shared-artifacts.ts
|
|
96
|
+
var SHARED_ARTIFACT_TEXT_MAX_BYTES = 1024 * 1024;
|
|
97
|
+
var SHARED_ARTIFACT_IMAGE_MAX_BYTES = 5 * 1024 * 1024;
|
|
98
|
+
var SHARED_ARTIFACT_TREE_MAX_BYTES = 200 * 1024 * 1024;
|
|
99
|
+
|
|
100
|
+
// ../contracts/src/artifact-block.ts
|
|
101
|
+
var MAX_ARTIFACT_BLOCKS_BYTES = 1024 * 1024;
|
|
102
|
+
|
|
51
103
|
// ../contracts/src/design-review-images.ts
|
|
52
104
|
var MAX_REVIEW_IMAGES_PER_RUN = 12;
|
|
53
105
|
var MAX_REVIEW_IMAGE_CAPTION_LENGTH = 300;
|
|
@@ -65,8 +117,10 @@ var RUNNER_SESSION_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-
|
|
|
65
117
|
function isRunnerSessionId(value) {
|
|
66
118
|
return typeof value === "string" && RUNNER_SESSION_ID_RE.test(value);
|
|
67
119
|
}
|
|
120
|
+
var RUNNER_TASK_IMAGES_DIR = ".dt-task-images";
|
|
68
121
|
var RUNNER_REVIEW_IMAGE_MAX_BYTES = 5e6;
|
|
69
122
|
var RUNNER_DELIVERABLE_FILE = "DELIVERABLE.md";
|
|
123
|
+
var RUNNER_REFERENCE_REPOSITORY_DIR = "repository";
|
|
70
124
|
var RUNNER_COMPLETION_MAX_TEXT = 2e5;
|
|
71
125
|
|
|
72
126
|
// ../contracts/src/agent-permissions.ts
|
|
@@ -115,7 +169,7 @@ function toolGrantRepositoryKey(url) {
|
|
|
115
169
|
}
|
|
116
170
|
|
|
117
171
|
// src/version.ts
|
|
118
|
-
var RUNNER_VERSION = true ? "0.1.
|
|
172
|
+
var RUNNER_VERSION = true ? "0.1.17" : "0.0.0-dev";
|
|
119
173
|
var RUNNER_PLATFORM = `${process.platform}-${process.arch}`;
|
|
120
174
|
|
|
121
175
|
// src/api.ts
|
|
@@ -198,6 +252,15 @@ var RunnerApi = class {
|
|
|
198
252
|
{ timeoutMs: Math.max(this.timeoutMs, 12e4) }
|
|
199
253
|
);
|
|
200
254
|
}
|
|
255
|
+
/** An image a session's prompt names (`start_session.promptImages`), to save for its agent. */
|
|
256
|
+
getPromptImage(sessionId, attachmentId) {
|
|
257
|
+
return this.request(
|
|
258
|
+
"GET",
|
|
259
|
+
`/runner/sessions/${encodeURIComponent(sessionId)}/prompt-images/${encodeURIComponent(attachmentId)}`,
|
|
260
|
+
void 0,
|
|
261
|
+
{ timeoutMs: Math.max(this.timeoutMs, 12e4) }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
201
264
|
/** Unauthenticated reachability check (`dt-runner status`). */
|
|
202
265
|
async health() {
|
|
203
266
|
try {
|
|
@@ -925,6 +988,55 @@ function isLocked(err) {
|
|
|
925
988
|
function removeDir(path) {
|
|
926
989
|
return rm(path, { recursive: true, force: true, maxRetries: 8, retryDelay: 250 });
|
|
927
990
|
}
|
|
991
|
+
async function ensureRepoCache(repoDir, ref, protocol) {
|
|
992
|
+
if (!await exists(join2(repoDir, ".git"))) {
|
|
993
|
+
await mkdir(dirname(repoDir), { recursive: true });
|
|
994
|
+
await rm(repoDir, { recursive: true, force: true });
|
|
995
|
+
await git(dirname(repoDir), ["clone", "--no-checkout", cloneUrl(ref, protocol), repoDir]);
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
const wanted = cloneUrl(ref, protocol);
|
|
999
|
+
const current2 = await exec("git", ["config", "--get", "remote.origin.url"], {
|
|
1000
|
+
cwd: repoDir,
|
|
1001
|
+
env: gitEnv()
|
|
1002
|
+
});
|
|
1003
|
+
if (current2.stdout.trim() !== wanted) {
|
|
1004
|
+
await git(repoDir, ["remote", "set-url", "origin", wanted]);
|
|
1005
|
+
}
|
|
1006
|
+
await git(repoDir, ["fetch", "--prune", "origin"]);
|
|
1007
|
+
}
|
|
1008
|
+
async function resolveBase(repoDir, wanted) {
|
|
1009
|
+
const baseRef = wanted ?? await defaultBranch(repoDir);
|
|
1010
|
+
let start2 = `origin/${baseRef}`;
|
|
1011
|
+
if (!await gitOk(repoDir, ["rev-parse", "--verify", "--quiet", `${start2}^{commit}`])) {
|
|
1012
|
+
if (!await gitOk(repoDir, ["rev-parse", "--verify", "--quiet", `${baseRef}^{commit}`])) {
|
|
1013
|
+
throw new Error(`Base branch ${baseRef} was not found on origin.`);
|
|
1014
|
+
}
|
|
1015
|
+
start2 = baseRef;
|
|
1016
|
+
}
|
|
1017
|
+
return { baseRef, start: start2 };
|
|
1018
|
+
}
|
|
1019
|
+
async function prepareReferenceCheckout(input) {
|
|
1020
|
+
const ref = parseRepoUrl(input.url);
|
|
1021
|
+
if (!ref) throw new Error(repositoryUrlError(input.url));
|
|
1022
|
+
if (input.baseRef) assertSafeRef(input.baseRef, "Base branch");
|
|
1023
|
+
const repoDir = join2(input.reposDir, ref.owner, ref.repo);
|
|
1024
|
+
return withRepoLock(repoDir, async () => {
|
|
1025
|
+
await ensureRepoCache(repoDir, ref, input.protocol);
|
|
1026
|
+
const { baseRef, start: start2 } = await resolveBase(repoDir, input.baseRef);
|
|
1027
|
+
const sha = await git(repoDir, ["rev-parse", `${start2}^{commit}`]);
|
|
1028
|
+
await exec("git", ["worktree", "remove", "--force", input.dir], {
|
|
1029
|
+
cwd: repoDir,
|
|
1030
|
+
env: gitEnv(),
|
|
1031
|
+
timeoutMs: 6e4
|
|
1032
|
+
});
|
|
1033
|
+
await removeDir(input.dir);
|
|
1034
|
+
await git(repoDir, ["worktree", "prune"]);
|
|
1035
|
+
await mkdir(dirname(input.dir), { recursive: true });
|
|
1036
|
+
await git(repoDir, ["worktree", "add", "--detach", input.dir, sha]);
|
|
1037
|
+
return { repoDir, dir: input.dir, baseRef, sha };
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
928
1040
|
async function prepareWorktree(input) {
|
|
929
1041
|
const ref = parseRepoUrl(input.url);
|
|
930
1042
|
if (!ref) throw new Error(repositoryUrlError(input.url));
|
|
@@ -935,34 +1047,8 @@ async function prepareWorktree(input) {
|
|
|
935
1047
|
let worktree = join2(input.worktreesDir, input.worktreeKey || input.sessionId);
|
|
936
1048
|
const remove = input.removeDir ?? removeDir;
|
|
937
1049
|
return withRepoLock(repoDir, async () => {
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
await rm(repoDir, { recursive: true, force: true });
|
|
941
|
-
await git(dirname(repoDir), [
|
|
942
|
-
"clone",
|
|
943
|
-
"--no-checkout",
|
|
944
|
-
cloneUrl(ref, input.protocol),
|
|
945
|
-
repoDir
|
|
946
|
-
]);
|
|
947
|
-
} else {
|
|
948
|
-
const wanted = cloneUrl(ref, input.protocol);
|
|
949
|
-
const current2 = await exec("git", ["config", "--get", "remote.origin.url"], {
|
|
950
|
-
cwd: repoDir,
|
|
951
|
-
env: gitEnv()
|
|
952
|
-
});
|
|
953
|
-
if (current2.stdout.trim() !== wanted) {
|
|
954
|
-
await git(repoDir, ["remote", "set-url", "origin", wanted]);
|
|
955
|
-
}
|
|
956
|
-
await git(repoDir, ["fetch", "--prune", "origin"]);
|
|
957
|
-
}
|
|
958
|
-
const baseRef = input.baseRef ?? await defaultBranch(repoDir);
|
|
959
|
-
let start2 = `origin/${baseRef}`;
|
|
960
|
-
if (!await gitOk(repoDir, ["rev-parse", "--verify", "--quiet", `${start2}^{commit}`])) {
|
|
961
|
-
if (!await gitOk(repoDir, ["rev-parse", "--verify", "--quiet", `${baseRef}^{commit}`])) {
|
|
962
|
-
throw new Error(`Base branch ${baseRef} was not found on origin.`);
|
|
963
|
-
}
|
|
964
|
-
start2 = baseRef;
|
|
965
|
-
}
|
|
1050
|
+
await ensureRepoCache(repoDir, ref, input.protocol);
|
|
1051
|
+
const { baseRef, start: start2 } = await resolveBase(repoDir, input.baseRef);
|
|
966
1052
|
let baseSha = await git(repoDir, ["rev-parse", `${start2}^{commit}`]);
|
|
967
1053
|
let checkout = start2;
|
|
968
1054
|
let continuedFrom;
|
|
@@ -1169,9 +1255,16 @@ function evidenceBranch(workBranch) {
|
|
|
1169
1255
|
}
|
|
1170
1256
|
async function ensureEvidenceDir(worktree) {
|
|
1171
1257
|
await mkdir2(join3(worktree, EVIDENCE_DIR), { recursive: true });
|
|
1258
|
+
await excludeFromGit(
|
|
1259
|
+
worktree,
|
|
1260
|
+
EVIDENCE_DIR,
|
|
1261
|
+
"Screenshots dt-runner publishes on their own branch"
|
|
1262
|
+
);
|
|
1263
|
+
}
|
|
1264
|
+
async function excludeFromGit(worktree, dir, why) {
|
|
1172
1265
|
const rel = await git(worktree, ["rev-parse", "--git-path", "info/exclude"]);
|
|
1173
1266
|
const file = isAbsolute2(rel) ? rel : join3(worktree, rel);
|
|
1174
|
-
const line = `/${
|
|
1267
|
+
const line = `/${dir}/`;
|
|
1175
1268
|
let current2 = "";
|
|
1176
1269
|
try {
|
|
1177
1270
|
current2 = await readFile(file, "utf8");
|
|
@@ -1180,12 +1273,9 @@ async function ensureEvidenceDir(worktree) {
|
|
|
1180
1273
|
if (current2.split("\n").some((l) => l.trim() === line)) return;
|
|
1181
1274
|
await mkdir2(join3(file, ".."), { recursive: true });
|
|
1182
1275
|
const sep2 = current2 && !current2.endsWith("\n") ? "\n" : "";
|
|
1183
|
-
await writeFile(
|
|
1184
|
-
file,
|
|
1185
|
-
`${current2}${sep2}# Screenshots dt-runner publishes on their own branch
|
|
1276
|
+
await writeFile(file, `${current2}${sep2}# ${why}
|
|
1186
1277
|
${line}
|
|
1187
|
-
`
|
|
1188
|
-
);
|
|
1278
|
+
`);
|
|
1189
1279
|
}
|
|
1190
1280
|
async function publishEvidence(input) {
|
|
1191
1281
|
const dir = join3(input.worktree, EVIDENCE_DIR);
|
|
@@ -1523,6 +1613,15 @@ function runnerSystemPrompt(input) {
|
|
|
1523
1613
|
"You are running a Discovery Tree task through the person\u2019s local Discovery Tree runner.",
|
|
1524
1614
|
"Your working directory is a scratch directory created for this task; it is not a git repository."
|
|
1525
1615
|
];
|
|
1616
|
+
if (input.reference) {
|
|
1617
|
+
scratch.push(
|
|
1618
|
+
`The repository this task concerns, ${input.reference.url}, is checked out at`,
|
|
1619
|
+
`\`${input.reference.dir}\` (\`${input.reference.baseRef}\` as it is now), for you to read.`,
|
|
1620
|
+
"Read it before you propose anything, so your work matches the product as it is: its",
|
|
1621
|
+
"screens, components, styles, words and data. It is for reading only: do not change it,",
|
|
1622
|
+
"commit or push. Nothing in it is kept, and this task\u2019s output is your response."
|
|
1623
|
+
);
|
|
1624
|
+
}
|
|
1526
1625
|
if (input.reviewImages) {
|
|
1527
1626
|
const folder = join4(input.cwd, EVIDENCE_DIR);
|
|
1528
1627
|
scratch.push(
|
|
@@ -3456,7 +3555,7 @@ function resolveConfig(file, overrides = {}, env = process.env, paths = runnerPa
|
|
|
3456
3555
|
}
|
|
3457
3556
|
|
|
3458
3557
|
// src/daemon.ts
|
|
3459
|
-
import { join as
|
|
3558
|
+
import { join as join13 } from "node:path";
|
|
3460
3559
|
|
|
3461
3560
|
// src/backoff.ts
|
|
3462
3561
|
function backoffDelay(attempt, baseMs = 1e3, maxMs = 6e4) {
|
|
@@ -3579,16 +3678,74 @@ function reportableSessionIds(ids, onDropped = () => void 0) {
|
|
|
3579
3678
|
}
|
|
3580
3679
|
|
|
3581
3680
|
// src/session-manager.ts
|
|
3582
|
-
import { mkdir as
|
|
3583
|
-
import { join as
|
|
3681
|
+
import { mkdir as mkdir7, rm as rm6, writeFile as writeFile5 } from "node:fs/promises";
|
|
3682
|
+
import { join as join12 } from "node:path";
|
|
3683
|
+
|
|
3684
|
+
// src/prompt-images.ts
|
|
3685
|
+
import { mkdir as mkdir4, writeFile as writeFile3 } from "node:fs/promises";
|
|
3686
|
+
import { join as join9 } from "node:path";
|
|
3687
|
+
var EXTENSIONS = {
|
|
3688
|
+
"image/png": ".png",
|
|
3689
|
+
"image/jpeg": ".jpg",
|
|
3690
|
+
"image/gif": ".gif",
|
|
3691
|
+
"image/webp": ".webp"
|
|
3692
|
+
};
|
|
3693
|
+
function promptImageFileName(index, filename, mimeType) {
|
|
3694
|
+
const base = (filename.split(/[\\/]/).pop() ?? "").replace(/[\u0000-\u001f\u007f<>:"|?*]+/g, "_").replace(/^\.+/, "").trim().slice(0, 120);
|
|
3695
|
+
const ext = EXTENSIONS[mimeType] ?? "";
|
|
3696
|
+
const named = base || `image${ext}`;
|
|
3697
|
+
const withExt = ext && !named.toLowerCase().endsWith(ext) ? `${named}${ext}` : named;
|
|
3698
|
+
return `${String(index + 1).padStart(2, "0")}-${withExt}`;
|
|
3699
|
+
}
|
|
3700
|
+
async function savePromptImages(input) {
|
|
3701
|
+
const out = { saved: [], failed: [] };
|
|
3702
|
+
if (input.images.length === 0) return out;
|
|
3703
|
+
const dir = join9(input.cwd, RUNNER_TASK_IMAGES_DIR);
|
|
3704
|
+
await mkdir4(dir, { recursive: true });
|
|
3705
|
+
if (input.inRepository) {
|
|
3706
|
+
await excludeFromGit(
|
|
3707
|
+
input.cwd,
|
|
3708
|
+
RUNNER_TASK_IMAGES_DIR,
|
|
3709
|
+
"Task images dt-runner saves for its agent"
|
|
3710
|
+
);
|
|
3711
|
+
}
|
|
3712
|
+
for (const [index, image] of input.images.entries()) {
|
|
3713
|
+
try {
|
|
3714
|
+
const res = await input.fetcher.getPromptImage(input.sessionId, image.attachmentId);
|
|
3715
|
+
const path = join9(
|
|
3716
|
+
dir,
|
|
3717
|
+
promptImageFileName(index, image.filename, res.mimeType || image.mimeType)
|
|
3718
|
+
);
|
|
3719
|
+
await writeFile3(path, Buffer.from(res.dataBase64, "base64"));
|
|
3720
|
+
out.saved.push({ filename: image.filename, path });
|
|
3721
|
+
} catch {
|
|
3722
|
+
out.failed.push(image.filename);
|
|
3723
|
+
}
|
|
3724
|
+
}
|
|
3725
|
+
return out;
|
|
3726
|
+
}
|
|
3727
|
+
function promptWithImagePaths(prompt, images) {
|
|
3728
|
+
if (images.saved.length === 0 && images.failed.length === 0) return prompt;
|
|
3729
|
+
const lines = [
|
|
3730
|
+
"",
|
|
3731
|
+
"IMAGE FILES FOR THIS TASK \u2014 the images the attachment lists above refer to, saved as",
|
|
3732
|
+
"files. Open each one and look at it before you start (read it as an image).",
|
|
3733
|
+
...images.saved.map((i) => `- ${i.filename}: ${i.path}`),
|
|
3734
|
+
...images.failed.map(
|
|
3735
|
+
(name) => `- ${name}: could not be downloaded; say so rather than describing it`
|
|
3736
|
+
)
|
|
3737
|
+
];
|
|
3738
|
+
return `${prompt}
|
|
3739
|
+
${lines.join("\n")}`;
|
|
3740
|
+
}
|
|
3584
3741
|
|
|
3585
3742
|
// src/fsutil.ts
|
|
3586
|
-
import { mkdir as
|
|
3743
|
+
import { mkdir as mkdir5, readFile as readFile4, rename as rename2, writeFile as writeFile4 } from "node:fs/promises";
|
|
3587
3744
|
import { dirname as dirname4 } from "node:path";
|
|
3588
3745
|
async function writeJsonAtomic(file, value) {
|
|
3589
|
-
await
|
|
3746
|
+
await mkdir5(dirname4(file), { recursive: true });
|
|
3590
3747
|
const tmp = `${file}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
3591
|
-
await
|
|
3748
|
+
await writeFile4(tmp, JSON.stringify(value), { mode: 384 });
|
|
3592
3749
|
await rename2(tmp, file);
|
|
3593
3750
|
}
|
|
3594
3751
|
async function readJson(file) {
|
|
@@ -3838,7 +3995,7 @@ import { basename, dirname as dirname6 } from "node:path";
|
|
|
3838
3995
|
import { createInterface as createInterface2 } from "node:readline";
|
|
3839
3996
|
|
|
3840
3997
|
// src/tool-grants.ts
|
|
3841
|
-
import { dirname as dirname5, join as
|
|
3998
|
+
import { dirname as dirname5, join as join10, posix } from "node:path";
|
|
3842
3999
|
var isGrantableDenial = isGrantableToolDenial;
|
|
3843
4000
|
var FILE_TOOLS2 = /* @__PURE__ */ new Set(["Read", "Edit", "MultiEdit", "Write", "NotebookEdit"]);
|
|
3844
4001
|
var EDIT_TOOLS = /* @__PURE__ */ new Set(["Edit", "MultiEdit", "Write", "NotebookEdit"]);
|
|
@@ -4036,7 +4193,7 @@ function filePatternMatches(spec, file) {
|
|
|
4036
4193
|
let pattern = spec.trim();
|
|
4037
4194
|
if (pattern.startsWith("~/")) {
|
|
4038
4195
|
if (!home) return false;
|
|
4039
|
-
pattern = rulePath(
|
|
4196
|
+
pattern = rulePath(join10(home, pattern.slice(2)).replace(/\\/g, "/"));
|
|
4040
4197
|
}
|
|
4041
4198
|
if (!pattern.startsWith("//")) return false;
|
|
4042
4199
|
const path = rulePath(file);
|
|
@@ -4057,7 +4214,7 @@ function shellPatternMatches(spec, command) {
|
|
|
4057
4214
|
return new RegExp(`^${re}${optionalTail ? "(?: .*)?" : ""}$`, "s").test(command);
|
|
4058
4215
|
}
|
|
4059
4216
|
function toolGrantsFile(configFile) {
|
|
4060
|
-
return
|
|
4217
|
+
return join10(dirname5(configFile), "tool-grants.json");
|
|
4061
4218
|
}
|
|
4062
4219
|
var ToolGrantStore = class {
|
|
4063
4220
|
constructor(file) {
|
|
@@ -4223,40 +4380,57 @@ function ghError(command, res) {
|
|
|
4223
4380
|
}
|
|
4224
4381
|
|
|
4225
4382
|
// src/session-store.ts
|
|
4226
|
-
import { mkdir as
|
|
4227
|
-
import { join as
|
|
4383
|
+
import { mkdir as mkdir6, readdir as readdir2, rm as rm5 } from "node:fs/promises";
|
|
4384
|
+
import { join as join11 } from "node:path";
|
|
4228
4385
|
var HANDLED_COMMAND_IDS_KEPT = 100;
|
|
4386
|
+
var pendingWrites = /* @__PURE__ */ new Map();
|
|
4229
4387
|
var SessionStore = class {
|
|
4230
4388
|
constructor(stateDir) {
|
|
4231
4389
|
this.stateDir = stateDir;
|
|
4232
4390
|
}
|
|
4233
4391
|
dir(sessionId) {
|
|
4234
|
-
return
|
|
4392
|
+
return join11(this.stateDir, "sessions", sessionId);
|
|
4235
4393
|
}
|
|
4236
4394
|
outboxFile(sessionId) {
|
|
4237
|
-
return
|
|
4395
|
+
return join11(this.dir(sessionId), "outbox.json");
|
|
4238
4396
|
}
|
|
4397
|
+
/**
|
|
4398
|
+
* Writes the record as it is now. A session's saves land in the order they were made:
|
|
4399
|
+
* two in flight at once (a turn's start and its quick result) could otherwise rename in
|
|
4400
|
+
* either order and leave the older state on disk, such as a turn still active, which a
|
|
4401
|
+
* restarted runner would then report as cut short.
|
|
4402
|
+
*/
|
|
4239
4403
|
async save(record) {
|
|
4240
4404
|
record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
4241
|
-
|
|
4405
|
+
const file = join11(this.dir(record.sessionId), "session.json");
|
|
4406
|
+
const snapshot = JSON.parse(JSON.stringify(record));
|
|
4407
|
+
const previous = pendingWrites.get(file) ?? Promise.resolve();
|
|
4408
|
+
const write = previous.then(() => writeJsonAtomic(file, snapshot));
|
|
4409
|
+
const settled = write.catch(() => void 0);
|
|
4410
|
+
pendingWrites.set(file, settled);
|
|
4411
|
+
void settled.then(() => {
|
|
4412
|
+
if (pendingWrites.get(file) === settled) pendingWrites.delete(file);
|
|
4413
|
+
});
|
|
4414
|
+
await write;
|
|
4242
4415
|
}
|
|
4243
4416
|
async list() {
|
|
4244
|
-
const root =
|
|
4245
|
-
await
|
|
4417
|
+
const root = join11(this.stateDir, "sessions");
|
|
4418
|
+
await mkdir6(root, { recursive: true });
|
|
4246
4419
|
const out = [];
|
|
4247
4420
|
for (const entry of await readdir2(root, { withFileTypes: true })) {
|
|
4248
4421
|
if (!entry.isDirectory()) continue;
|
|
4249
|
-
const rec = await readJson(
|
|
4422
|
+
const rec = await readJson(join11(root, entry.name, "session.json"));
|
|
4250
4423
|
if (rec?.sessionId === entry.name) out.push(upgradeRecord(rec));
|
|
4251
4424
|
}
|
|
4252
4425
|
return out;
|
|
4253
4426
|
}
|
|
4254
4427
|
async remove(sessionId) {
|
|
4428
|
+
await pendingWrites.get(join11(this.dir(sessionId), "session.json"));
|
|
4255
4429
|
await rm5(this.dir(sessionId), { recursive: true, force: true });
|
|
4256
4430
|
}
|
|
4257
4431
|
/** Scratch working directory for a session without a repository. */
|
|
4258
4432
|
scratchDir(sessionId) {
|
|
4259
|
-
return
|
|
4433
|
+
return join11(this.stateDir, "scratch", sessionId);
|
|
4260
4434
|
}
|
|
4261
4435
|
};
|
|
4262
4436
|
function upgradeRecord(stored) {
|
|
@@ -4815,7 +4989,8 @@ ${text}` : text;
|
|
|
4815
4989
|
workBranch: this.record.workBranch,
|
|
4816
4990
|
baseRef: this.record.baseRef,
|
|
4817
4991
|
cwd: this.record.cwd,
|
|
4818
|
-
reviewImages: !!this.record.reviewImages
|
|
4992
|
+
reviewImages: !!this.record.reviewImages,
|
|
4993
|
+
reference: this.record.referenceRepository ?? null
|
|
4819
4994
|
}),
|
|
4820
4995
|
...prompt !== void 0 ? { prompt } : {},
|
|
4821
4996
|
...this.deps.agentConfig ? { agentConfig: this.deps.agentConfig } : {}
|
|
@@ -5534,14 +5709,37 @@ var SessionManager = class {
|
|
|
5534
5709
|
record.cwd = this.store.scratchDir(
|
|
5535
5710
|
resume ? safeWorktreeKey(resume.worktreeKey, cmd.sessionId) : cmd.sessionId
|
|
5536
5711
|
);
|
|
5537
|
-
await
|
|
5712
|
+
await mkdir7(record.cwd, { recursive: true });
|
|
5538
5713
|
if (cmd.reviewImages === true && this.opts.reviewImageUploader) {
|
|
5539
5714
|
record.reviewImages = true;
|
|
5540
|
-
await
|
|
5715
|
+
await mkdir7(join12(record.cwd, EVIDENCE_DIR), { recursive: true });
|
|
5716
|
+
}
|
|
5717
|
+
const reference = cmd.referenceRepository;
|
|
5718
|
+
if (reference?.url) {
|
|
5719
|
+
try {
|
|
5720
|
+
const checkout = await prepareReferenceCheckout({
|
|
5721
|
+
url: reference.url,
|
|
5722
|
+
baseRef: reference.baseRef ?? null,
|
|
5723
|
+
reposDir: this.opts.config.reposDir,
|
|
5724
|
+
dir: join12(record.cwd, RUNNER_REFERENCE_REPOSITORY_DIR),
|
|
5725
|
+
protocol: this.opts.config.gitProtocol
|
|
5726
|
+
});
|
|
5727
|
+
record.referenceRepository = {
|
|
5728
|
+
url: reference.url,
|
|
5729
|
+
baseRef: checkout.baseRef,
|
|
5730
|
+
dir: checkout.dir,
|
|
5731
|
+
repoDir: checkout.repoDir
|
|
5732
|
+
};
|
|
5733
|
+
} catch (err) {
|
|
5734
|
+
this.opts.logger.warn(
|
|
5735
|
+
`session ${cmd.sessionId}: could not check out ${reference.url} for the agent to read; it works without it`,
|
|
5736
|
+
{ error: safeError(err) }
|
|
5737
|
+
);
|
|
5738
|
+
}
|
|
5541
5739
|
}
|
|
5542
5740
|
const deliverable = resume?.deliverable;
|
|
5543
5741
|
if (typeof deliverable === "string" && deliverable.trim()) {
|
|
5544
|
-
await
|
|
5742
|
+
await writeFile5(join12(record.cwd, RUNNER_DELIVERABLE_FILE), deliverable, "utf8");
|
|
5545
5743
|
}
|
|
5546
5744
|
}
|
|
5547
5745
|
if (resume) {
|
|
@@ -5560,7 +5758,7 @@ var SessionManager = class {
|
|
|
5560
5758
|
this.sessions.set(cmd.sessionId, session);
|
|
5561
5759
|
if (resume) return { ok: true };
|
|
5562
5760
|
try {
|
|
5563
|
-
await session.startFirstTurn(cmd
|
|
5761
|
+
await session.startFirstTurn(await this.promptWithImages(cmd, record));
|
|
5564
5762
|
} catch (err) {
|
|
5565
5763
|
const message = safeError(err);
|
|
5566
5764
|
this.opts.logger.error(`session ${cmd.sessionId}: ${message}`);
|
|
@@ -5699,6 +5897,36 @@ var SessionManager = class {
|
|
|
5699
5897
|
* session's folder), then the session is removed as a close would remove it. Called from
|
|
5700
5898
|
* inside the start, so it does not wait for the start as `close` does.
|
|
5701
5899
|
*/
|
|
5900
|
+
/**
|
|
5901
|
+
* The first turn's prompt, with the paths of the images it names (`promptImages`) saved in
|
|
5902
|
+
* the session's working directory. Never fails the start: an image that cannot be fetched
|
|
5903
|
+
* is listed as such, and so are all of them when there is nothing to fetch them with.
|
|
5904
|
+
*/
|
|
5905
|
+
async promptWithImages(cmd, record) {
|
|
5906
|
+
const images = Array.isArray(cmd.promptImages) ? cmd.promptImages : [];
|
|
5907
|
+
if (images.length === 0) return cmd.prompt;
|
|
5908
|
+
const fetcher = this.opts.promptImageFetcher;
|
|
5909
|
+
let saved = {
|
|
5910
|
+
saved: [],
|
|
5911
|
+
failed: images.map((i) => i.filename)
|
|
5912
|
+
};
|
|
5913
|
+
if (fetcher) {
|
|
5914
|
+
try {
|
|
5915
|
+
saved = await savePromptImages({
|
|
5916
|
+
sessionId: cmd.sessionId,
|
|
5917
|
+
cwd: record.cwd,
|
|
5918
|
+
inRepository: !!record.repoDir,
|
|
5919
|
+
images,
|
|
5920
|
+
fetcher
|
|
5921
|
+
});
|
|
5922
|
+
} catch (err) {
|
|
5923
|
+
this.opts.logger.warn(`session ${cmd.sessionId}: could not save the task's images`, {
|
|
5924
|
+
error: safeError(err)
|
|
5925
|
+
});
|
|
5926
|
+
}
|
|
5927
|
+
}
|
|
5928
|
+
return promptWithImagePaths(cmd.prompt, saved);
|
|
5929
|
+
}
|
|
5702
5930
|
async forgetFailedStart(session) {
|
|
5703
5931
|
const { sessionId } = session;
|
|
5704
5932
|
await session.outbox.flush(this.opts.failedStartFlushMs ?? 5e3);
|
|
@@ -5727,6 +5955,16 @@ var SessionManager = class {
|
|
|
5727
5955
|
})
|
|
5728
5956
|
);
|
|
5729
5957
|
} else if (record.cwd) {
|
|
5958
|
+
if (record.referenceRepository) {
|
|
5959
|
+
await removeWorktree(
|
|
5960
|
+
record.referenceRepository.repoDir,
|
|
5961
|
+
record.referenceRepository.dir
|
|
5962
|
+
).catch(
|
|
5963
|
+
(err) => this.opts.logger.warn(`session ${sessionId}: could not remove the checkout it read`, {
|
|
5964
|
+
error: safeError(err)
|
|
5965
|
+
})
|
|
5966
|
+
);
|
|
5967
|
+
}
|
|
5730
5968
|
await rm6(record.cwd, { recursive: true, force: true }).catch(() => void 0);
|
|
5731
5969
|
}
|
|
5732
5970
|
await session.outbox.settled();
|
|
@@ -5884,6 +6122,7 @@ var RunnerDaemon = class {
|
|
|
5884
6122
|
agentProblem: (driver) => this.agentProblem(driver),
|
|
5885
6123
|
grants: this.grants,
|
|
5886
6124
|
reviewImageUploader: this.api,
|
|
6125
|
+
promptImageFetcher: this.api,
|
|
5887
6126
|
// What the person already allows in their own Claude Code for a repository.
|
|
5888
6127
|
inherited: opts.config.inheritClaudeCodePermissions ? new ClaudeCodeRules({
|
|
5889
6128
|
exclude: [opts.config.reposDir, opts.config.worktreesDir]
|
|
@@ -6092,7 +6331,11 @@ var RunnerDaemon = class {
|
|
|
6092
6331
|
// And attaches a design proposal's rendered screens to its task (`reviewImages`).
|
|
6093
6332
|
uploadsReviewImages: true,
|
|
6094
6333
|
// And keeps a continued non-code run's deliverable whole (`resume.deliverable`).
|
|
6095
|
-
receivesDeliverable: true
|
|
6334
|
+
receivesDeliverable: true,
|
|
6335
|
+
// And saves the images a prompt names as files for its agent (`promptImages`).
|
|
6336
|
+
receivesPromptImages: true,
|
|
6337
|
+
// And checks out a non-code task's repository for it to read (`referenceRepository`).
|
|
6338
|
+
checksOutReferenceRepository: true
|
|
6096
6339
|
},
|
|
6097
6340
|
activeSessionIds: this.heartbeatSessionIds(),
|
|
6098
6341
|
maxConcurrentSessions: this.opts.config.maxSessions,
|
|
@@ -6201,7 +6444,7 @@ var RunnerDaemon = class {
|
|
|
6201
6444
|
if (this.droppedSessionIds.has(id)) return;
|
|
6202
6445
|
this.droppedSessionIds.add(id);
|
|
6203
6446
|
this.opts.logger.warn(
|
|
6204
|
-
`session ${id}: not a Discovery Tree session id, so it is left out of the heartbeat (its state is in ${
|
|
6447
|
+
`session ${id}: not a Discovery Tree session id, so it is left out of the heartbeat (its state is in ${join13(this.opts.config.stateDir, "sessions", id)})`
|
|
6205
6448
|
);
|
|
6206
6449
|
});
|
|
6207
6450
|
}
|
|
@@ -6428,10 +6671,10 @@ function formatChecks(checks) {
|
|
|
6428
6671
|
}
|
|
6429
6672
|
|
|
6430
6673
|
// src/lock.ts
|
|
6431
|
-
import { mkdir as
|
|
6432
|
-
import { join as
|
|
6674
|
+
import { mkdir as mkdir8, open, readFile as readFile5, rm as rm7 } from "node:fs/promises";
|
|
6675
|
+
import { join as join14 } from "node:path";
|
|
6433
6676
|
function lockFile(stateDir) {
|
|
6434
|
-
return
|
|
6677
|
+
return join14(stateDir, "daemon.pid");
|
|
6435
6678
|
}
|
|
6436
6679
|
function pidAlive(pid) {
|
|
6437
6680
|
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
@@ -6453,7 +6696,7 @@ async function lockHolder(stateDir) {
|
|
|
6453
6696
|
return pidAlive(pid) && pid !== process.pid ? pid : null;
|
|
6454
6697
|
}
|
|
6455
6698
|
async function acquireDaemonLock(stateDir, pid = process.pid) {
|
|
6456
|
-
await
|
|
6699
|
+
await mkdir8(stateDir, { recursive: true, mode: 448 });
|
|
6457
6700
|
const file = lockFile(stateDir);
|
|
6458
6701
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
6459
6702
|
try {
|
package/package.json
CHANGED