talentos 1.3.0 → 1.4.2
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/agent/adapters.js +23 -0
- package/dist/agent/codex-policy.js +3 -0
- package/dist/agent/gateway.js +11 -1
- package/dist/agent/mcp-entry.js +4 -1
- package/dist/agent/native/codex-fence-probe.js +629 -0
- package/dist/agent/native/codex-fence.js +162 -0
- package/dist/agent/native/codex-launch.js +86 -0
- package/dist/agent/native/codex-owner-probe.js +145 -4
- package/dist/agent/native/codex-run-command.js +302 -0
- package/dist/agent/native/config.js +8 -2
- package/dist/agent/native/discover.js +137 -0
- package/dist/agent/native/fresh.js +104 -0
- package/dist/agent/native/gate.js +202 -18
- package/dist/agent/native/native-issue.js +1 -0
- package/dist/agent/native/policy.js +63 -14
- package/dist/agent/native/project-requests.js +106 -0
- package/dist/agent/native/projects.js +111 -0
- package/dist/agent/native/touch.js +10 -0
- package/dist/agent/native/workspace.js +20 -5
- package/dist/agent/serve.js +83 -6
- package/dist/args.js +1 -0
- package/dist/commands/agent-native.js +42 -6
- package/dist/commands/agent.js +5 -1
- package/dist/mcp-proxy.js +25 -2
- package/package.json +1 -1
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { loginShellEnv } from "./env.js";
|
|
5
|
+
import { runHook as defaultRunHook } from "./hook-entry.js";
|
|
6
|
+
// run_command: the owner-native Codex fence (#10987 slice 1). Codex's own shell
|
|
7
|
+
// is removed at launch (the model catalog sets shell_type="disabled", which
|
|
8
|
+
// drops exec_command AND write_stdin together); this one-shot tool, served by
|
|
9
|
+
// our LOCAL stdio MCP proxy, is the only way the model runs a command. It is the
|
|
10
|
+
// parity of Claude Code's Bash: every call goes through the SAME classifier,
|
|
11
|
+
// approval hold, writeRoots, taint/touch report and receipts as Claude's, by
|
|
12
|
+
// building a Claude-shaped Bash call and running the EXISTING hook.
|
|
13
|
+
//
|
|
14
|
+
// The proxy, not the model-side PreToolUse hook, is the shell gate. A same-user
|
|
15
|
+
// process that forges a tools/call into the proxy's stdin (the /proc or
|
|
16
|
+
// pidfd-getfd reopen the evidence probe demonstrates) is classified here exactly
|
|
17
|
+
// like a model call, because classification happens at execution, not at the
|
|
18
|
+
// model boundary. So there is one classifier and no way to reach a command
|
|
19
|
+
// without it.
|
|
20
|
+
/** The owner approves what RUNS, so the model may not choose cwd, env or the cap. */
|
|
21
|
+
export const RUN_COMMAND_TOOL = {
|
|
22
|
+
name: "run_command",
|
|
23
|
+
description: "Run one shell command in your working folder and return its output. " +
|
|
24
|
+
"The command runs once (no interactive session), with no stdin, in the " +
|
|
25
|
+
"job's working folder. Long output is truncated and long runs time out; " +
|
|
26
|
+
"the result says when that happened. The owner may be asked to approve the " +
|
|
27
|
+
"command before it runs.",
|
|
28
|
+
inputSchema: {
|
|
29
|
+
type: "object",
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
properties: {
|
|
32
|
+
command: {
|
|
33
|
+
type: "string",
|
|
34
|
+
description: "The shell command to run.",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
required: ["command"],
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
// v1 ceilings, stated to the model in the result when they bite (R6: no hidden
|
|
41
|
+
// limits). Ten minutes matches the longest a Claude Bash call may ask for, so a
|
|
42
|
+
// real test suite finishes; it is fixed by us, never chosen by the model. The
|
|
43
|
+
// MCP tool-call timeout set for the room server sits above it. Background and
|
|
44
|
+
// long-running (beyond the cap) commands are a known v1 gap.
|
|
45
|
+
export const RUN_COMMAND_TIMEOUT_MS = 600_000;
|
|
46
|
+
export const RUN_COMMAND_OUTPUT_MAX = 32_000;
|
|
47
|
+
const KILL_GRACE_MS = 2000;
|
|
48
|
+
// The owner-native environment for run_command children: the owner's real
|
|
49
|
+
// login-shell env (their PATH, nvm/pyenv shims, the tokens their own tools
|
|
50
|
+
// expect), captured and scrubbed exactly as Claude owner-native does (env.ts),
|
|
51
|
+
// so `pnpm test` works the same under both. It is NOT the proxy's own process
|
|
52
|
+
// env: Codex launches the proxy as an stdio MCP server and passes it a short
|
|
53
|
+
// allowlist plus whatever Codex injects (its own OpenAI/Codex auth), so a
|
|
54
|
+
// bootstrap base of only safe shell variables seeds the login shell, and the
|
|
55
|
+
// shell supplies the owner's real env. No credential Codex gave the proxy can
|
|
56
|
+
// reach a child this way.
|
|
57
|
+
const BOOTSTRAP_VARS = [
|
|
58
|
+
"HOME",
|
|
59
|
+
"PATH",
|
|
60
|
+
"USER",
|
|
61
|
+
"LOGNAME",
|
|
62
|
+
"SHELL",
|
|
63
|
+
"TERM",
|
|
64
|
+
"LANG",
|
|
65
|
+
"LC_ALL",
|
|
66
|
+
"TMPDIR",
|
|
67
|
+
"TZ",
|
|
68
|
+
];
|
|
69
|
+
export function bootstrapEnv(source = process.env) {
|
|
70
|
+
return Object.fromEntries(BOOTSTRAP_VARS.filter((key) => source[key] !== undefined).map((key) => [
|
|
71
|
+
key,
|
|
72
|
+
source[key],
|
|
73
|
+
]));
|
|
74
|
+
}
|
|
75
|
+
/** The owner login env seeded from only safe bootstrap vars, then scrubbed. */
|
|
76
|
+
export function resolveOwnerEnv(source = process.env, shell) {
|
|
77
|
+
const base = bootstrapEnv(source);
|
|
78
|
+
return loginShellEnv(base, shell ?? base.SHELL ?? "/bin/zsh");
|
|
79
|
+
}
|
|
80
|
+
let ownerEnv;
|
|
81
|
+
function ownerCommandEnv() {
|
|
82
|
+
ownerEnv ??= resolveOwnerEnv();
|
|
83
|
+
return ownerEnv;
|
|
84
|
+
}
|
|
85
|
+
// Every live one-shot child's process-group id, so job Stop, a timeout or the
|
|
86
|
+
// proxy exiting kills the whole group and nothing outlives the job.
|
|
87
|
+
const liveGroups = new Set();
|
|
88
|
+
function killGroup(pgid, signal) {
|
|
89
|
+
try {
|
|
90
|
+
process.kill(-pgid, signal);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// The group may already be gone.
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Kills every in-flight one-shot command. Call on proxy shutdown. */
|
|
97
|
+
export function stopAllRunCommands() {
|
|
98
|
+
for (const pgid of liveGroups) {
|
|
99
|
+
killGroup(pgid, "SIGKILL");
|
|
100
|
+
}
|
|
101
|
+
liveGroups.clear();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Wires proxy shutdown so nothing outlives the job: an in-flight run_command is
|
|
105
|
+
* killed when the owner-native MCP proxy gets a termination signal OR its stdin
|
|
106
|
+
* from Codex closes (Codex crashing or being SIGKILLed never sends us a signal,
|
|
107
|
+
* so the stdin close is the backstop). Returns the abort signal in-flight
|
|
108
|
+
* commands watch. `exit` defaults to killing the process; tests pass their own.
|
|
109
|
+
*/
|
|
110
|
+
export function wireShutdown(input) {
|
|
111
|
+
const controller = new AbortController();
|
|
112
|
+
let done = false;
|
|
113
|
+
const stop = (exit) => {
|
|
114
|
+
if (!done) {
|
|
115
|
+
done = true;
|
|
116
|
+
controller.abort();
|
|
117
|
+
stopAllRunCommands();
|
|
118
|
+
}
|
|
119
|
+
if (exit) {
|
|
120
|
+
(input.exit ?? (() => process.exit(0)))();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
for (const signal of input.signals ?? []) {
|
|
124
|
+
process.once(signal, () => stop(true));
|
|
125
|
+
}
|
|
126
|
+
input.stdin.once("end", () => stop(true));
|
|
127
|
+
input.stdin.once("close", () => stop(true));
|
|
128
|
+
return controller.signal;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Runs `command` once in `cwd` with no stdin, in its own process group, with the
|
|
132
|
+
* captured owner login environment (see ownerCommandEnv; not the proxy's env),
|
|
133
|
+
* capturing stdout+stderr up to the cap, timing out, and killing
|
|
134
|
+
* the whole group on timeout or abort. Never throws for a non-zero exit.
|
|
135
|
+
*/
|
|
136
|
+
export async function runOneShot(input) {
|
|
137
|
+
const env = input.env ?? (await ownerCommandEnv());
|
|
138
|
+
const child = spawn("/bin/sh", ["-c", input.command], {
|
|
139
|
+
cwd: input.cwd,
|
|
140
|
+
env,
|
|
141
|
+
// stdin is /dev/null: no interactive session, no write_stdin equivalent.
|
|
142
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
143
|
+
detached: true,
|
|
144
|
+
});
|
|
145
|
+
const pgid = child.pid;
|
|
146
|
+
if (pgid !== undefined) {
|
|
147
|
+
liveGroups.add(pgid);
|
|
148
|
+
}
|
|
149
|
+
let output = "";
|
|
150
|
+
let truncated = false;
|
|
151
|
+
const collect = (chunk) => {
|
|
152
|
+
if (truncated) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
output += chunk.toString("utf8");
|
|
156
|
+
if (output.length > RUN_COMMAND_OUTPUT_MAX) {
|
|
157
|
+
output = output.slice(0, RUN_COMMAND_OUTPUT_MAX);
|
|
158
|
+
truncated = true;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
child.stdout?.on("data", collect);
|
|
162
|
+
child.stderr?.on("data", collect);
|
|
163
|
+
let timedOut = false;
|
|
164
|
+
let grace;
|
|
165
|
+
// Always targets the whole group, even after the shell leader has exited: a
|
|
166
|
+
// backgrounded child keeps the group (and the stdout pipe) alive, and nothing
|
|
167
|
+
// may outlive the command. There is no background or long-running mode in v1.
|
|
168
|
+
const stop = (markTimeout) => {
|
|
169
|
+
if (pgid === undefined) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
timedOut ||= markTimeout;
|
|
173
|
+
killGroup(pgid, "SIGTERM");
|
|
174
|
+
grace ??= setTimeout(() => killGroup(pgid, "SIGKILL"), KILL_GRACE_MS);
|
|
175
|
+
};
|
|
176
|
+
const timer = setTimeout(() => stop(true), RUN_COMMAND_TIMEOUT_MS);
|
|
177
|
+
const onAbort = () => stop(false);
|
|
178
|
+
input.signal?.addEventListener("abort", onAbort, { once: true });
|
|
179
|
+
if (input.signal?.aborted) {
|
|
180
|
+
stop(false);
|
|
181
|
+
}
|
|
182
|
+
// Resolve when the shell leader exits (not on "close", which waits for every
|
|
183
|
+
// inherited stdout pipe and so hangs on a backgrounded child); then reap the
|
|
184
|
+
// group so no orphan survives.
|
|
185
|
+
const [code, signal] = await new Promise((resolve) => {
|
|
186
|
+
child.once("error", () => resolve([null, null]));
|
|
187
|
+
child.once("exit", (c, s) => resolve([c, s]));
|
|
188
|
+
});
|
|
189
|
+
clearTimeout(timer);
|
|
190
|
+
input.signal?.removeEventListener("abort", onAbort);
|
|
191
|
+
if (pgid !== undefined) {
|
|
192
|
+
killGroup(pgid, "SIGKILL");
|
|
193
|
+
liveGroups.delete(pgid);
|
|
194
|
+
}
|
|
195
|
+
if (grace) {
|
|
196
|
+
clearTimeout(grace);
|
|
197
|
+
}
|
|
198
|
+
// A short drain so output buffered right before exit is not lost.
|
|
199
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
200
|
+
return { code, output, signal, timedOut, truncated };
|
|
201
|
+
}
|
|
202
|
+
function formatResult(result) {
|
|
203
|
+
const notes = [];
|
|
204
|
+
if (result.timedOut) {
|
|
205
|
+
notes.push(`[timed out after ${Math.round(RUN_COMMAND_TIMEOUT_MS / 60_000)} minutes and was stopped. ` +
|
|
206
|
+
"run_command has no background or longer-running mode in this version; " +
|
|
207
|
+
"run a smaller scope, or a faster subset of the work.]");
|
|
208
|
+
}
|
|
209
|
+
if (result.truncated) {
|
|
210
|
+
notes.push(`[output truncated at ${RUN_COMMAND_OUTPUT_MAX} characters. ` +
|
|
211
|
+
"Re-run narrowing the output, e.g. with head, grep or a smaller scope.]");
|
|
212
|
+
}
|
|
213
|
+
if (!result.timedOut) {
|
|
214
|
+
notes.push(result.signal
|
|
215
|
+
? `[killed by signal ${result.signal}]`
|
|
216
|
+
: `[exit code ${result.code ?? "unknown"}]`);
|
|
217
|
+
}
|
|
218
|
+
const body = result.output.length > 0 ? result.output : "(no output)";
|
|
219
|
+
return `${body}\n${notes.join("\n")}`;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Classifies, holds for the owner, reports touch/taint and records a receipt by
|
|
223
|
+
* running the EXISTING owner-native hook with a Claude-shaped Bash call, then —
|
|
224
|
+
* only if the hook allowed it — runs the command once. A deny (including the
|
|
225
|
+
* owner declining or not answering) comes back as the hook's plain message, as
|
|
226
|
+
* a tool result the model can read, never a proxy error.
|
|
227
|
+
*/
|
|
228
|
+
export async function handleRunCommand(input) {
|
|
229
|
+
const command = input.command;
|
|
230
|
+
if (typeof command !== "string" || command.trim() === "") {
|
|
231
|
+
return {
|
|
232
|
+
content: [
|
|
233
|
+
{ text: "run_command needs a non-empty command string.", type: "text" },
|
|
234
|
+
],
|
|
235
|
+
isError: true,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
let cwd;
|
|
239
|
+
try {
|
|
240
|
+
const context = JSON.parse(readFileSync(join(input.jobDir, "context.json"), "utf8"));
|
|
241
|
+
const root = Array.isArray(context.writeRoots)
|
|
242
|
+
? context.writeRoots[0]
|
|
243
|
+
: undefined;
|
|
244
|
+
if (typeof root !== "string") {
|
|
245
|
+
throw new Error("No workspace root");
|
|
246
|
+
}
|
|
247
|
+
cwd = root;
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return {
|
|
251
|
+
content: [
|
|
252
|
+
{
|
|
253
|
+
text: "The room safety check could not run; this command is blocked.",
|
|
254
|
+
type: "text",
|
|
255
|
+
},
|
|
256
|
+
],
|
|
257
|
+
isError: true,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
const runHook = input.runHook ?? defaultRunHook;
|
|
261
|
+
// The cwd is fixed to the job worktree, never taken from the model, so the
|
|
262
|
+
// classifier judges relative paths against the same folder the command runs in.
|
|
263
|
+
const raw = JSON.stringify({
|
|
264
|
+
cwd,
|
|
265
|
+
tool_input: { command },
|
|
266
|
+
tool_name: "Bash",
|
|
267
|
+
});
|
|
268
|
+
let decision;
|
|
269
|
+
try {
|
|
270
|
+
decision = JSON.parse(await runHook(input.jobDir, raw)).hookSpecificOutput;
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return {
|
|
274
|
+
content: [
|
|
275
|
+
{
|
|
276
|
+
text: "The room safety check could not run; this command is blocked.",
|
|
277
|
+
type: "text",
|
|
278
|
+
},
|
|
279
|
+
],
|
|
280
|
+
isError: true,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (decision.permissionDecision !== "allow") {
|
|
284
|
+
return {
|
|
285
|
+
content: [
|
|
286
|
+
{
|
|
287
|
+
text: decision.permissionDecisionReason ??
|
|
288
|
+
"This command was not allowed. Do not retry; tell the owner what you wanted to do.",
|
|
289
|
+
type: "text",
|
|
290
|
+
},
|
|
291
|
+
],
|
|
292
|
+
isError: true,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
const result = await runOneShot({
|
|
296
|
+
command,
|
|
297
|
+
cwd,
|
|
298
|
+
env: input.env,
|
|
299
|
+
signal: input.signal,
|
|
300
|
+
});
|
|
301
|
+
return { content: [{ text: formatResult(result), type: "text" }] };
|
|
302
|
+
}
|
|
@@ -4,7 +4,7 @@ import { readFile, rm } from "node:fs/promises";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import { isMissing, writePrivateFile } from "../../agent-config.js";
|
|
7
|
-
import { isInside, reachesProtected } from "./paths.js";
|
|
7
|
+
import { isInside, protectedPaths, reachesProtected } from "./paths.js";
|
|
8
8
|
const exec = promisify(execFile);
|
|
9
9
|
export const NATIVE_FILE = "native.json";
|
|
10
10
|
export async function readNativeConfig(directory) {
|
|
@@ -41,7 +41,8 @@ export function folderSlug(name, agentId) {
|
|
|
41
41
|
.slice(0, 8)
|
|
42
42
|
.toLowerCase()}`;
|
|
43
43
|
}
|
|
44
|
-
|
|
44
|
+
/** The git top level of `cwd`, or undefined outside a repo. */
|
|
45
|
+
export async function repoRoot(cwd) {
|
|
45
46
|
try {
|
|
46
47
|
const { stdout } = await exec("git", ["rev-parse", "--show-toplevel"], {
|
|
47
48
|
cwd,
|
|
@@ -79,6 +80,11 @@ export function rootProblem(config, home) {
|
|
|
79
80
|
reachesProtected(real, home)) {
|
|
80
81
|
return "The workspace folder is too broad.";
|
|
81
82
|
}
|
|
83
|
+
// Inside ~/.ssh, ~/.claude and the like: never a workspace. (Only the
|
|
84
|
+
// folders: a repo merely named like a secrets file is fine.)
|
|
85
|
+
if (protectedPaths(home).readWrite.some((entry) => isInside(real, entry))) {
|
|
86
|
+
return "The workspace folder holds credentials or provider settings.";
|
|
87
|
+
}
|
|
82
88
|
return;
|
|
83
89
|
}
|
|
84
90
|
catch {
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
4
|
+
import { checkRepo } from "./projects.js";
|
|
5
|
+
export const CANDIDATES_MAX = 60;
|
|
6
|
+
const KEYS_MAX = 300;
|
|
7
|
+
const NAME_MAX = 80;
|
|
8
|
+
const CONTROL = /\p{Cc}/gu;
|
|
9
|
+
const CODEX_PROJECT = /^\s*\[\s*projects\s*\.\s*"((?:[^"\\]|\\.)*)"\s*\]\s*$/u;
|
|
10
|
+
const TABLE = /^\s*\[/u;
|
|
11
|
+
const TRUSTED = /^\s*trust_level\s*=\s*"trusted"\s*(#.*)?$/u;
|
|
12
|
+
async function readText(path) {
|
|
13
|
+
try {
|
|
14
|
+
return await readFile(path, "utf8");
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Claude Code keeps every folder it has opened as a key of `projects`. */
|
|
21
|
+
export async function claudeProjectFolders(paths) {
|
|
22
|
+
const folders = [];
|
|
23
|
+
for (const path of paths) {
|
|
24
|
+
const raw = await readText(path);
|
|
25
|
+
try {
|
|
26
|
+
const projects = raw
|
|
27
|
+
? JSON.parse(raw).projects
|
|
28
|
+
: undefined;
|
|
29
|
+
if (projects && typeof projects === "object") {
|
|
30
|
+
folders.push(...Object.keys(projects));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// An unreadable file offers nothing.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return folders;
|
|
38
|
+
}
|
|
39
|
+
/** Codex's trusted folders: `[projects."<path>"]` with `trust_level = "trusted"`. */
|
|
40
|
+
export function codexTrustedFolders(toml) {
|
|
41
|
+
const folders = [];
|
|
42
|
+
let current;
|
|
43
|
+
for (const line of toml.split("\n")) {
|
|
44
|
+
const header = CODEX_PROJECT.exec(line);
|
|
45
|
+
if (header?.[1] !== undefined) {
|
|
46
|
+
current = JSON.parse(`"${header[1]}"`);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (TABLE.test(line)) {
|
|
50
|
+
current = undefined;
|
|
51
|
+
}
|
|
52
|
+
else if (current !== undefined && TRUSTED.test(line)) {
|
|
53
|
+
folders.push(current);
|
|
54
|
+
current = undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return folders;
|
|
58
|
+
}
|
|
59
|
+
const clean = (name) => name.replace(CONTROL, "").trim().slice(0, NAME_MAX) || "repo";
|
|
60
|
+
export async function discoverProjects(input) {
|
|
61
|
+
const { home } = input;
|
|
62
|
+
const claudeFiles = [join(home, ".claude.json")];
|
|
63
|
+
if (input.claudeConfigDir) {
|
|
64
|
+
claudeFiles.push(join(input.claudeConfigDir, ".claude.json"));
|
|
65
|
+
}
|
|
66
|
+
const codexToml = await readText(join(input.codexHome || join(home, ".codex"), "config.toml"));
|
|
67
|
+
const folders = new Map();
|
|
68
|
+
const add = (folder, source) => {
|
|
69
|
+
if (isAbsolute(folder) && folders.size < KEYS_MAX) {
|
|
70
|
+
folders.set(folder, (folders.get(folder) ?? new Set()).add(source));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
for (const folder of await claudeProjectFolders(claudeFiles)) {
|
|
74
|
+
add(folder, "claude_code");
|
|
75
|
+
}
|
|
76
|
+
for (const folder of codexToml ? codexTrustedFolders(codexToml) : []) {
|
|
77
|
+
add(folder, "codex");
|
|
78
|
+
}
|
|
79
|
+
// A folder inside a repo offers that repo; the same repo is offered once.
|
|
80
|
+
// Eight git processes at a time: a long Claude Code history stays quick
|
|
81
|
+
// without a burst of hundreds of them.
|
|
82
|
+
const entries = [...folders];
|
|
83
|
+
const checked = [];
|
|
84
|
+
for (let at = 0; at < entries.length; at += 8) {
|
|
85
|
+
checked.push(...(await Promise.all(entries.slice(at, at + 8).map(async ([folder, sources]) => {
|
|
86
|
+
try {
|
|
87
|
+
return { config: await checkRepo(folder, home), sources };
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}))));
|
|
93
|
+
}
|
|
94
|
+
const byRoot = new Map();
|
|
95
|
+
for (const entry of checked) {
|
|
96
|
+
if (!entry) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const known = byRoot.get(entry.config.root);
|
|
100
|
+
if (known) {
|
|
101
|
+
for (const source of entry.sources) {
|
|
102
|
+
known.sources.add(source);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (byRoot.size < CANDIDATES_MAX) {
|
|
106
|
+
byRoot.set(entry.config.root, {
|
|
107
|
+
config: entry.config,
|
|
108
|
+
sources: new Set(entry.sources),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const names = new Map();
|
|
113
|
+
for (const root of byRoot.keys()) {
|
|
114
|
+
const name = clean(basename(root));
|
|
115
|
+
names.set(name, (names.get(name) ?? 0) + 1);
|
|
116
|
+
}
|
|
117
|
+
const registered = new Set(input.registered);
|
|
118
|
+
const roots = new Map();
|
|
119
|
+
const candidates = [...byRoot.values()].map(({ config, sources }) => {
|
|
120
|
+
const candidateId = randomBytes(16).toString("hex");
|
|
121
|
+
const name = clean(basename(config.root));
|
|
122
|
+
roots.set(candidateId, config);
|
|
123
|
+
return {
|
|
124
|
+
...(registered.has(config.root) ? { added: true } : {}),
|
|
125
|
+
candidateId,
|
|
126
|
+
...((names.get(name) ?? 0) > 1
|
|
127
|
+
? { hint: clean(basename(dirname(config.root))) }
|
|
128
|
+
: {}),
|
|
129
|
+
name,
|
|
130
|
+
sources: [...sources].sort(),
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
candidates.sort((a, b) => Number(a.added ?? false) - Number(b.added ?? false) ||
|
|
134
|
+
a.name.localeCompare(b.name) ||
|
|
135
|
+
(a.hint ?? "").localeCompare(b.hint ?? ""));
|
|
136
|
+
return { candidates, roots };
|
|
137
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
const exec = promisify(execFile);
|
|
4
|
+
// A new thread starts from the remote's default branch, fetched just now
|
|
5
|
+
// (#10987, Projects). The CLI runs this, never the model, and it never touches
|
|
6
|
+
// the owner's checkout: the fetch writes remote-tracking refs only and the
|
|
7
|
+
// thread gets its own worktree. Every git call here runs no repo hooks (a
|
|
8
|
+
// tracked hooks folder would run code from the remote) and no `ext::`
|
|
9
|
+
// transport.
|
|
10
|
+
export const SAFE_GIT = [
|
|
11
|
+
"-c",
|
|
12
|
+
"core.hooksPath=/dev/null",
|
|
13
|
+
"-c",
|
|
14
|
+
"protocol.ext.allow=never",
|
|
15
|
+
];
|
|
16
|
+
const FETCH_TIMEOUT_MS = 20_000;
|
|
17
|
+
const SLOW_MS = 1000;
|
|
18
|
+
const BRANCH = /^(?!-)(?!.*\.\.)[\w./-]{1,200}$/u;
|
|
19
|
+
const local = (tried) => ({
|
|
20
|
+
fetched: false,
|
|
21
|
+
label: "your local copy",
|
|
22
|
+
receipt: tried
|
|
23
|
+
? "Started from your local copy: couldn't get the latest code"
|
|
24
|
+
: "Started from your local copy: no remote to fetch",
|
|
25
|
+
start: "HEAD",
|
|
26
|
+
tried,
|
|
27
|
+
});
|
|
28
|
+
async function git(root, args, options = {}) {
|
|
29
|
+
const { stdout } = await exec("git", [...SAFE_GIT, ...args], {
|
|
30
|
+
cwd: root,
|
|
31
|
+
env: { ...(options.env ?? process.env), GIT_TERMINAL_PROMPT: "0" },
|
|
32
|
+
timeout: options.timeout ?? 5000,
|
|
33
|
+
});
|
|
34
|
+
return stdout.trim();
|
|
35
|
+
}
|
|
36
|
+
async function defaultBranch(root) {
|
|
37
|
+
const head = await git(root, [
|
|
38
|
+
"symbolic-ref",
|
|
39
|
+
"--quiet",
|
|
40
|
+
"--short",
|
|
41
|
+
"refs/remotes/origin/HEAD",
|
|
42
|
+
]).catch(() => "");
|
|
43
|
+
if (head.startsWith("origin/")) {
|
|
44
|
+
return head.slice("origin/".length);
|
|
45
|
+
}
|
|
46
|
+
for (const name of ["main", "master"]) {
|
|
47
|
+
const found = await git(root, [
|
|
48
|
+
"show-ref",
|
|
49
|
+
"--verify",
|
|
50
|
+
"--quiet",
|
|
51
|
+
`refs/remotes/origin/${name}`,
|
|
52
|
+
]).then(() => true, () => false);
|
|
53
|
+
if (found) {
|
|
54
|
+
return name;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Fetches origin's default branch into `refs/remotes/origin/<branch>` and
|
|
61
|
+
* returns it as the start point; on any miss (no origin, no default branch, a
|
|
62
|
+
* failed or slow fetch) returns the local HEAD. `--refmap=` (empty) stops git's
|
|
63
|
+
* opportunistic update of the configured refspecs, so even a mirror-style
|
|
64
|
+
* `fetch = +refs/heads/*:refs/heads/*` cannot move a local branch.
|
|
65
|
+
*/
|
|
66
|
+
export async function freshBase(root, options = {}) {
|
|
67
|
+
const hasOrigin = await git(root, ["remote", "get-url", "origin"]).then(Boolean, () => false);
|
|
68
|
+
const branch = hasOrigin ? await defaultBranch(root) : undefined;
|
|
69
|
+
if (!(branch && BRANCH.test(branch))) {
|
|
70
|
+
return local(false);
|
|
71
|
+
}
|
|
72
|
+
const slow = setTimeout(() => options.onSlow?.(), SLOW_MS);
|
|
73
|
+
try {
|
|
74
|
+
await git(root, [
|
|
75
|
+
"fetch",
|
|
76
|
+
"--quiet",
|
|
77
|
+
"--no-tags",
|
|
78
|
+
"--no-recurse-submodules",
|
|
79
|
+
"--no-write-fetch-head",
|
|
80
|
+
"--refmap=",
|
|
81
|
+
"origin",
|
|
82
|
+
`+refs/heads/${branch}:refs/remotes/origin/${branch}`,
|
|
83
|
+
], { env: options.env, timeout: FETCH_TIMEOUT_MS });
|
|
84
|
+
await git(root, [
|
|
85
|
+
"rev-parse",
|
|
86
|
+
"--verify",
|
|
87
|
+
"--quiet",
|
|
88
|
+
`refs/remotes/origin/${branch}^{commit}`,
|
|
89
|
+
]);
|
|
90
|
+
return {
|
|
91
|
+
fetched: true,
|
|
92
|
+
label: `origin/${branch}`,
|
|
93
|
+
receipt: `Started from origin/${branch}, fetched just now`,
|
|
94
|
+
start: `refs/remotes/origin/${branch}`,
|
|
95
|
+
tried: true,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return local(true);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
clearTimeout(slow);
|
|
103
|
+
}
|
|
104
|
+
}
|