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
|
@@ -3,16 +3,24 @@ import { existsSync } from "node:fs";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { promisify } from "node:util";
|
|
5
5
|
import { providerPathEnv, resolveProviderBinary, } from "../../provider-binary.js";
|
|
6
|
+
import { codexFenceEligible, codexFenceVersion, codexUnsupportedMessage, } from "./codex-fence.js";
|
|
7
|
+
import { codexFenceProbeCached, ensureCodexFenceProbe, } from "./codex-fence-probe.js";
|
|
8
|
+
import { codexDisqualifier, codexOwnerContextPresent } from "./codex-owner.js";
|
|
6
9
|
import { NATIVE_FILE, readNativeConfig } from "./config.js";
|
|
7
10
|
import { ownerContextPresent } from "./context.js";
|
|
8
11
|
import { claudeDisqualifier } from "./disqualify.js";
|
|
9
12
|
import { loginShellEnv } from "./env.js";
|
|
10
13
|
import { ensureOwnerNativeProbe } from "./probe.js";
|
|
14
|
+
import { readProjects } from "./projects.js";
|
|
11
15
|
import { prepareWorkspace } from "./workspace.js";
|
|
12
16
|
const WHITESPACE = /\s+/u;
|
|
13
17
|
const exec = promisify(execFile);
|
|
14
18
|
const ENV_TTL_MS = 10 * 60_000;
|
|
15
19
|
const RETRY_MS = 10 * 60_000;
|
|
20
|
+
export const UNKNOWN_PROJECT = "That project isn't set up on this computer, so I'm working room-only.";
|
|
21
|
+
export const NO_PROJECT = "No project is set up on this computer, so I'm working room-only.";
|
|
22
|
+
export const MISSING_PROJECT = "That project's folder isn't available on this computer, so I'm working room-only.";
|
|
23
|
+
export const LOCAL_BASE = "Couldn't get the latest code, so I started from your local copy.";
|
|
16
24
|
/**
|
|
17
25
|
* Everything the owner-native profile needs on this computer, decided here and
|
|
18
26
|
* nowhere else: the owner's local yes, a passing cached probe, settings that
|
|
@@ -22,6 +30,10 @@ const RETRY_MS = 10 * 60_000;
|
|
|
22
30
|
*/
|
|
23
31
|
export class NativeGate {
|
|
24
32
|
probe = "none";
|
|
33
|
+
codexIssue;
|
|
34
|
+
// The server's exact list of Codex versions allowed full setup, from the
|
|
35
|
+
// last heartbeat. Until it arrives, a Codex is never probed or reported ready.
|
|
36
|
+
codexTested;
|
|
25
37
|
retryAt = 0;
|
|
26
38
|
env;
|
|
27
39
|
agent;
|
|
@@ -36,15 +48,31 @@ export class NativeGate {
|
|
|
36
48
|
optedIn() {
|
|
37
49
|
return existsSync(join(this.directory, NATIVE_FILE));
|
|
38
50
|
}
|
|
51
|
+
/** The server's tested Codex list, from each heartbeat (absent = unknown). */
|
|
52
|
+
setCodexTested(tested) {
|
|
53
|
+
const changed = tested?.join("\n") !== this.codexTested?.join("\n");
|
|
54
|
+
this.codexTested = tested;
|
|
55
|
+
if (changed && this.probe !== "pending") {
|
|
56
|
+
// A version may have just been verified or withdrawn: stop reporting
|
|
57
|
+
// ready on the old list and re-check now (a cached fence pass makes the
|
|
58
|
+
// re-check one `codex --version`).
|
|
59
|
+
this.probe = "none";
|
|
60
|
+
this.retryAt = 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Why an opted-in computer can't use full setup, for the heartbeat. */
|
|
64
|
+
issue() {
|
|
65
|
+
return this.optedIn() ? this.codexIssue : undefined;
|
|
66
|
+
}
|
|
39
67
|
/** Whether to tell the plane this computer is ready (heartbeat). */
|
|
40
68
|
reportable() {
|
|
41
69
|
return this.probe === "ok" && this.optedIn();
|
|
42
70
|
}
|
|
43
71
|
/** Starts the cached probe when the owner has opted in. Safe to call every beat. */
|
|
44
72
|
refresh() {
|
|
45
|
-
if (
|
|
46
|
-
process.platform === "win32" ||
|
|
73
|
+
if (process.platform === "win32" ||
|
|
47
74
|
!this.optedIn() ||
|
|
75
|
+
(this.agent.provider === "codex" && this.codexTested === undefined) ||
|
|
48
76
|
this.probe === "pending" ||
|
|
49
77
|
this.probe === "ok" ||
|
|
50
78
|
(this.probe === "failed" && Date.now() < this.retryAt)) {
|
|
@@ -54,20 +82,56 @@ export class NativeGate {
|
|
|
54
82
|
return;
|
|
55
83
|
}
|
|
56
84
|
this.probe = "pending";
|
|
85
|
+
// The list this probe judged against: if the server's list changes before
|
|
86
|
+
// it finishes, the result is for an old list and must not become "ready".
|
|
87
|
+
const listAtStart = this.codexTested?.join("\n");
|
|
88
|
+
const stale = () => this.codexTested?.join("\n") !== listAtStart;
|
|
57
89
|
this.runProbe().then(() => {
|
|
58
|
-
this.probe = "ok";
|
|
90
|
+
this.probe = stale() ? "none" : "ok";
|
|
59
91
|
}, () => {
|
|
92
|
+
if (stale()) {
|
|
93
|
+
this.probe = "none";
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
60
96
|
this.probe = "failed";
|
|
61
97
|
// A failing provider is not re-probed on every heartbeat.
|
|
62
98
|
this.retryAt = Date.now() + RETRY_MS;
|
|
63
99
|
});
|
|
64
100
|
}
|
|
65
101
|
async runProbe() {
|
|
66
|
-
const binary = resolveProviderBinary(
|
|
102
|
+
const binary = resolveProviderBinary(this.agent.provider, this.agent.providerPath);
|
|
103
|
+
const env = { ...process.env, PATH: providerPathEnv(binary, this.home) };
|
|
67
104
|
const { stdout } = await exec(binary, ["--version"], {
|
|
68
|
-
env
|
|
105
|
+
env,
|
|
69
106
|
timeout: 5000,
|
|
70
107
|
});
|
|
108
|
+
if (this.agent.provider === "codex") {
|
|
109
|
+
// The exact version (with prerelease) must be on the server's list, so a
|
|
110
|
+
// newer or stable-but-unverified Codex stays room-only. Without the list
|
|
111
|
+
// (`native enable` runs offline) the probe still proves this binary; the
|
|
112
|
+
// list is checked again at every claim.
|
|
113
|
+
const version = codexFenceVersion(stdout);
|
|
114
|
+
if (this.codexTested && !codexFenceEligible(stdout, this.codexTested)) {
|
|
115
|
+
this.codexIssue = untestedCodex(version, this.codexTested);
|
|
116
|
+
throw new Error(codexUnsupportedMessage(stdout, this.codexTested));
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
await ensureCodexFenceProbe({
|
|
120
|
+
binary,
|
|
121
|
+
cachePath: join(this.directory, "codex-fence-probe.json"),
|
|
122
|
+
version: version ?? "unknown",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
this.codexIssue = {
|
|
127
|
+
code: "codex_checks",
|
|
128
|
+
...(version ? { version } : {}),
|
|
129
|
+
};
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
this.codexIssue = undefined;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
71
135
|
await ensureOwnerNativeProbe({
|
|
72
136
|
binary,
|
|
73
137
|
cachePath: join(this.directory, "native-probe.json"),
|
|
@@ -81,32 +145,152 @@ export class NativeGate {
|
|
|
81
145
|
}
|
|
82
146
|
return this.env.value;
|
|
83
147
|
}
|
|
84
|
-
/**
|
|
85
|
-
async
|
|
148
|
+
/** The registry, for the heartbeat's id + name mirror; never a path. */
|
|
149
|
+
async projects() {
|
|
150
|
+
return this.optedIn() ? await readProjects(this.directory) : [];
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Per owner job (R4): returns the launch inputs, or undefined to run
|
|
154
|
+
* room-only. `projectId` comes from the claim (the owner's seat choice); an
|
|
155
|
+
* id this computer does not hold runs room-only, never another project. A
|
|
156
|
+
* claim without one (an older server) uses project #1.
|
|
157
|
+
*/
|
|
158
|
+
async prepare(threadId, job = {}) {
|
|
86
159
|
this.refresh();
|
|
87
160
|
const config = await readNativeConfig(this.directory);
|
|
88
161
|
if (!config || this.probe !== "ok") {
|
|
89
162
|
return;
|
|
90
163
|
}
|
|
91
164
|
const env = await this.loginEnv();
|
|
165
|
+
// Codex: the per-job fence checks come before any workspace is created.
|
|
166
|
+
const codex = this.agent.provider === "codex"
|
|
167
|
+
? await this.codexReady(env, job.codexTested)
|
|
168
|
+
: undefined;
|
|
169
|
+
if (this.agent.provider === "codex" && !codex) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
92
172
|
const configDir = env.CLAUDE_CONFIG_DIR || join(this.home, ".claude");
|
|
93
|
-
|
|
173
|
+
const projects = await readProjects(this.directory);
|
|
174
|
+
const project = job.projectId === undefined
|
|
175
|
+
? projects[0]
|
|
176
|
+
: projects.find((entry) => entry.id === job.projectId);
|
|
177
|
+
if (!project) {
|
|
178
|
+
job.status?.(projects.length > 0 ? UNKNOWN_PROJECT : NO_PROJECT);
|
|
94
179
|
return;
|
|
95
180
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
181
|
+
if (!codex &&
|
|
182
|
+
claudeDisqualifier({ configDir, projectRoots: [project.root] })) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
let workspace;
|
|
186
|
+
try {
|
|
187
|
+
workspace = await prepareWorkspace({
|
|
188
|
+
config: project,
|
|
189
|
+
env,
|
|
105
190
|
home: this.home,
|
|
106
|
-
|
|
191
|
+
onSlow: () => job.status?.("Getting the latest code…"),
|
|
192
|
+
threadId,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
// A moved, replaced or missing folder (device + inode) is never guessed at.
|
|
197
|
+
job.status?.(MISSING_PROJECT);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (workspace.base?.tried && !workspace.base.fetched) {
|
|
201
|
+
job.status?.(LOCAL_BASE);
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
...(codex
|
|
205
|
+
? {
|
|
206
|
+
codex: {
|
|
207
|
+
binary: codex.binary,
|
|
208
|
+
bundledCatalog: codex.bundledCatalog,
|
|
209
|
+
},
|
|
210
|
+
}
|
|
211
|
+
: {}),
|
|
212
|
+
// Fail closed for Codex: the owner's ~/.codex loads (AGENTS.md, MCP
|
|
213
|
+
// servers, skills, memories, profiles), so a shared-room post waits for
|
|
214
|
+
// review unless this computer affirms none of it was present.
|
|
215
|
+
context: codex
|
|
216
|
+
? codexOwnerContextPresent(codex.home)
|
|
217
|
+
: ownerContextPresent({
|
|
218
|
+
configDir,
|
|
219
|
+
cwd: workspace.cwd,
|
|
220
|
+
home: this.home,
|
|
221
|
+
}),
|
|
107
222
|
env,
|
|
108
223
|
home: this.home,
|
|
224
|
+
project: { id: project.id, ...(project.legacy ? { legacy: true } : {}) },
|
|
109
225
|
workspace,
|
|
110
226
|
};
|
|
111
227
|
}
|
|
228
|
+
/** Per Codex owner job: the binary, its catalog and ~/.codex, or undefined. */
|
|
229
|
+
async codexReady(env, tested) {
|
|
230
|
+
// Fail closed: a claim without the server's list (an older server) never
|
|
231
|
+
// runs Codex with the owner's setup.
|
|
232
|
+
if (!tested) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const binary = resolveProviderBinary("codex", this.agent.providerPath);
|
|
236
|
+
const probeEnv = { ...env, PATH: providerPathEnv(binary, this.home) };
|
|
237
|
+
// Per job, never trusting the background probe's last word: the binary
|
|
238
|
+
// answering now must be a tested version AND the one the cached fence pass
|
|
239
|
+
// proved (an `npm update` replaces it in place). Otherwise room-only, and
|
|
240
|
+
// the next heartbeat re-probes.
|
|
241
|
+
const fenceCache = join(this.directory, "codex-fence-probe.json");
|
|
242
|
+
const versionOutput = await exec(binary, ["--version"], {
|
|
243
|
+
env: probeEnv,
|
|
244
|
+
timeout: 5000,
|
|
245
|
+
}).then(({ stdout }) => stdout, () => "");
|
|
246
|
+
const version = codexFenceVersion(versionOutput);
|
|
247
|
+
if (!codexFenceEligible(versionOutput, tested)) {
|
|
248
|
+
// Updated in place onto an unlisted version since the probe passed, or
|
|
249
|
+
// the server just removed this one.
|
|
250
|
+
this.codexIssue = untestedCodex(version, tested);
|
|
251
|
+
}
|
|
252
|
+
if (!(version &&
|
|
253
|
+
codexFenceEligible(versionOutput, tested) &&
|
|
254
|
+
(await codexFenceProbeCached({
|
|
255
|
+
binary,
|
|
256
|
+
cachePath: fenceCache,
|
|
257
|
+
version,
|
|
258
|
+
})))) {
|
|
259
|
+
this.probe = "none";
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
// Hooks off in the effective config disqualifies (checked every job).
|
|
263
|
+
if (await codexDisqualifier(binary, probeEnv)) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const bundledCatalog = await this.codexCatalog(binary, probeEnv);
|
|
267
|
+
if (!bundledCatalog) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
binary,
|
|
272
|
+
bundledCatalog,
|
|
273
|
+
home: env.CODEX_HOME || join(this.home, ".codex"),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
async codexCatalog(binary, env) {
|
|
277
|
+
try {
|
|
278
|
+
const { stdout } = await exec(binary, ["debug", "models", "--bundled"], {
|
|
279
|
+
env,
|
|
280
|
+
maxBuffer: 8_000_000,
|
|
281
|
+
timeout: 10_000,
|
|
282
|
+
});
|
|
283
|
+
return stdout;
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function untestedCodex(version, tested) {
|
|
291
|
+
return {
|
|
292
|
+
code: "codex_version",
|
|
293
|
+
supported: [...tested],
|
|
294
|
+
...(version ? { version } : {}),
|
|
295
|
+
};
|
|
112
296
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ROOM_ONLY_STATUS = "Codex is answering room-only: full setup isn't available on that computer right now.";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ROOM_TOOLS } from "../codex-policy.js";
|
|
1
|
+
import { ROOM_LOCAL_TOOLS, ROOM_TOOLS } from "../codex-policy.js";
|
|
2
2
|
import { canonical, isInside, protectedPaths, protectedReason, reachesProtected, } from "./paths.js";
|
|
3
3
|
import { parseShell } from "./shell.js";
|
|
4
4
|
const NON_ALNUM = /[^A-Za-z\d]+/u;
|
|
@@ -156,7 +156,12 @@ function nameWords(name) {
|
|
|
156
156
|
}
|
|
157
157
|
export function classifyMcpTool(server, tool) {
|
|
158
158
|
if (server === "talentos_room") {
|
|
159
|
-
|
|
159
|
+
// run_command reaches our local proxy, which classifies the command itself
|
|
160
|
+
// (the parity of Claude's Bash). Letting the MCP call through here, rather
|
|
161
|
+
// than classifying the command twice, keeps one gate and one ask.
|
|
162
|
+
return ROOM_TOOLS.includes(tool) || ROOM_LOCAL_TOOLS.includes(tool)
|
|
163
|
+
? allow()
|
|
164
|
+
: deny(0);
|
|
160
165
|
}
|
|
161
166
|
// The owner's personal-key TalentOS server carries owner authority; a room
|
|
162
167
|
// job never gets it (it is also removed from the tool list at launch).
|
|
@@ -837,6 +842,35 @@ export function classifyShell(source, ctx) {
|
|
|
837
842
|
function text(value) {
|
|
838
843
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
839
844
|
}
|
|
845
|
+
const APPLY_PATCH_PATH = /^\*\*\*\s+(?:Add File|Update File|Delete File|Move to):\s*(.+?)\s*$/gmu;
|
|
846
|
+
/** Every file path an apply_patch touches: add, update, delete and move-to. */
|
|
847
|
+
export function applyPatchPaths(command) {
|
|
848
|
+
if (!command) {
|
|
849
|
+
return [];
|
|
850
|
+
}
|
|
851
|
+
const paths = [];
|
|
852
|
+
for (const match of command.matchAll(APPLY_PATCH_PATH)) {
|
|
853
|
+
if (match[1]) {
|
|
854
|
+
paths.push(match[1]);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return paths;
|
|
858
|
+
}
|
|
859
|
+
/** The write rule shared by Edit tools and apply_patch: inside a write root, or ask. */
|
|
860
|
+
function classifyWrite(tool, real, ctx) {
|
|
861
|
+
if (protectedReason(real, "write", ctx.home, ctx.guard) ||
|
|
862
|
+
gitControlFile(real, ctx)) {
|
|
863
|
+
return deny(0);
|
|
864
|
+
}
|
|
865
|
+
return ctx.writeRoots.some((root) => isInside(real, root))
|
|
866
|
+
? allow(true)
|
|
867
|
+
: askDeny("That file is outside this job's workspace. Needs the owner's approval.", {
|
|
868
|
+
detail: `${tool} ${real}`,
|
|
869
|
+
kind: "write-outside-workspace",
|
|
870
|
+
summary: redactSummary(`${tool} ${real}`),
|
|
871
|
+
tier: 1,
|
|
872
|
+
});
|
|
873
|
+
}
|
|
840
874
|
export function classifyCall(call, context) {
|
|
841
875
|
const guard = context.guard ?? protectedPaths(context.home);
|
|
842
876
|
const ctx = { ...context, guard, cwd: call.cwd };
|
|
@@ -856,19 +890,34 @@ export function classifyCall(call, context) {
|
|
|
856
890
|
if (!target) {
|
|
857
891
|
return deny(1);
|
|
858
892
|
}
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
893
|
+
return classifyWrite(tool, canonical(target, call.cwd, context.home), ctx);
|
|
894
|
+
}
|
|
895
|
+
// Codex's freeform file-edit tool: the whole patch is in `command`, so every
|
|
896
|
+
// path it adds, updates, moves to or deletes is checked like an Edit, and one
|
|
897
|
+
// path outside the workspace or into a protected file refuses the patch.
|
|
898
|
+
if (tool === "apply_patch") {
|
|
899
|
+
const paths = applyPatchPaths(text(input.command));
|
|
900
|
+
if (paths.length === 0) {
|
|
901
|
+
return deny(1);
|
|
863
902
|
}
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
903
|
+
let verdict = allow(true);
|
|
904
|
+
for (const path of paths) {
|
|
905
|
+
const refused = classifyWrite("apply_patch", canonical(path, call.cwd, context.home), ctx);
|
|
906
|
+
if (!refused.allow) {
|
|
907
|
+
if (refused.tier === 0) {
|
|
908
|
+
return refused;
|
|
909
|
+
}
|
|
910
|
+
verdict = refused;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return verdict;
|
|
914
|
+
}
|
|
915
|
+
// Codex reads a local image to look at it; judged like any other read.
|
|
916
|
+
if (tool === "view_image") {
|
|
917
|
+
const path = text(input.path) ?? text(input.file_path);
|
|
918
|
+
return path
|
|
919
|
+
? (pathCheck({ glob: false, text: path }, ctx, false) ?? allow())
|
|
920
|
+
: deny(1);
|
|
872
921
|
}
|
|
873
922
|
if (PLAIN_TOOLS.has(tool)) {
|
|
874
923
|
const raw = text(input.file_path) ?? text(input.notebook_path) ?? text(input.path);
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { discoverProjects } from "./discover.js";
|
|
2
|
+
import { checkRepo, readProjects, registerRoot, removeProject, } from "./projects.js";
|
|
3
|
+
const DISCOVERY_TTL_MS = 5 * 60_000;
|
|
4
|
+
export class ProjectRequests {
|
|
5
|
+
discoveries = new Map();
|
|
6
|
+
running;
|
|
7
|
+
again = false;
|
|
8
|
+
input;
|
|
9
|
+
constructor(input) {
|
|
10
|
+
this.input = input;
|
|
11
|
+
}
|
|
12
|
+
/** Coalesced: a burst of wakes runs at most one pass plus one follow-up. */
|
|
13
|
+
poke() {
|
|
14
|
+
if (this.running) {
|
|
15
|
+
this.again = true;
|
|
16
|
+
return this.running;
|
|
17
|
+
}
|
|
18
|
+
this.running = (async () => {
|
|
19
|
+
try {
|
|
20
|
+
do {
|
|
21
|
+
this.again = false;
|
|
22
|
+
await this.pass();
|
|
23
|
+
} while (this.again);
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
this.running = undefined;
|
|
27
|
+
}
|
|
28
|
+
})();
|
|
29
|
+
return this.running;
|
|
30
|
+
}
|
|
31
|
+
async pass() {
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
for (const [id, entry] of this.discoveries) {
|
|
34
|
+
if (now - entry.at > DISCOVERY_TTL_MS) {
|
|
35
|
+
this.discoveries.delete(id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const requests = (await this.input.post("/agent-gateway/projects/requests", {})) ?? [];
|
|
39
|
+
for (const request of requests) {
|
|
40
|
+
await this.handle(request).catch(() => undefined);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async handle(request) {
|
|
44
|
+
const { directory, home } = this.input;
|
|
45
|
+
if (request.kind === "remove") {
|
|
46
|
+
if (request.projectId) {
|
|
47
|
+
await removeProject(directory, request.projectId);
|
|
48
|
+
}
|
|
49
|
+
await this.close(request.requestId);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (request.status === "requested") {
|
|
53
|
+
const env = await this.input.env();
|
|
54
|
+
const discovery = await discoverProjects({
|
|
55
|
+
claudeConfigDir: env.CLAUDE_CONFIG_DIR,
|
|
56
|
+
codexHome: env.CODEX_HOME,
|
|
57
|
+
home,
|
|
58
|
+
registered: (await readProjects(directory)).map((project) => project.root),
|
|
59
|
+
});
|
|
60
|
+
this.discoveries.set(request.requestId, {
|
|
61
|
+
at: Date.now(),
|
|
62
|
+
names: new Map(discovery.candidates.map((candidate) => [
|
|
63
|
+
candidate.candidateId,
|
|
64
|
+
candidate.hint
|
|
65
|
+
? `${candidate.name} (${candidate.hint})`
|
|
66
|
+
: candidate.name,
|
|
67
|
+
])),
|
|
68
|
+
roots: discovery.roots,
|
|
69
|
+
});
|
|
70
|
+
await this.input.post("/agent-gateway/projects/offer", {
|
|
71
|
+
candidates: discovery.candidates,
|
|
72
|
+
requestId: request.requestId,
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (request.status !== "chosen") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const offered = this.discoveries.get(request.requestId);
|
|
80
|
+
for (const candidateId of request.picks ?? []) {
|
|
81
|
+
const root = offered?.roots.get(candidateId);
|
|
82
|
+
if (!root) {
|
|
83
|
+
// Not an id this process offered for this request: never a path.
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
// Re-checked now: the folder must still be the one that was offered.
|
|
88
|
+
const now = await checkRepo(root.root, home);
|
|
89
|
+
if (now.root === root.root &&
|
|
90
|
+
now.dev === root.dev &&
|
|
91
|
+
now.ino === root.ino) {
|
|
92
|
+
await registerRoot(directory, now, offered?.names.get(candidateId));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// A folder that moved or failed its checks is skipped.
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
this.discoveries.delete(request.requestId);
|
|
100
|
+
await this.close(request.requestId);
|
|
101
|
+
}
|
|
102
|
+
async close(requestId) {
|
|
103
|
+
await this.input.mirror().catch(() => undefined);
|
|
104
|
+
await this.input.post("/agent-gateway/projects/done", { requestId });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { basename, join } from "node:path";
|
|
5
|
+
import { isMissing, writePrivateFile } from "../../agent-config.js";
|
|
6
|
+
import { readNativeConfig, repoRoot, rootProblem, } from "./config.js";
|
|
7
|
+
export const PROJECTS_FILE = "projects.json";
|
|
8
|
+
export const PROJECTS_MAX = 20;
|
|
9
|
+
const PROJECT_ID = /^[a-f\d]{32}$/u;
|
|
10
|
+
/** 128 random bits; never derived from the path. */
|
|
11
|
+
export const newProjectId = () => randomBytes(16).toString("hex");
|
|
12
|
+
export const isProjectId = (value) => typeof value === "string" && PROJECT_ID.test(value);
|
|
13
|
+
function valid(value) {
|
|
14
|
+
return (isProjectId(value.id) &&
|
|
15
|
+
typeof value.name === "string" &&
|
|
16
|
+
typeof value.root === "string" &&
|
|
17
|
+
(value.kind === "repo" || value.kind === "managed") &&
|
|
18
|
+
typeof value.dev === "number" &&
|
|
19
|
+
typeof value.ino === "number");
|
|
20
|
+
}
|
|
21
|
+
async function writeProjects(directory, projects) {
|
|
22
|
+
await writePrivateFile(join(directory, PROJECTS_FILE), `${JSON.stringify({ projects })}\n`);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The registry. With none yet, the root `native enable` chose becomes project
|
|
26
|
+
* #1, so a setup from before projects keeps working unchanged.
|
|
27
|
+
*/
|
|
28
|
+
export async function readProjects(directory) {
|
|
29
|
+
try {
|
|
30
|
+
const value = JSON.parse(await readFile(join(directory, PROJECTS_FILE), "utf8"));
|
|
31
|
+
return (Array.isArray(value.projects) ? value.projects : [])
|
|
32
|
+
.filter(valid)
|
|
33
|
+
.slice(0, PROJECTS_MAX);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (!(isMissing(error) || error instanceof SyntaxError)) {
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const config = await readNativeConfig(directory);
|
|
41
|
+
if (!config) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
const projects = [
|
|
45
|
+
{ ...rootOf(config), id: newProjectId(), legacy: true },
|
|
46
|
+
];
|
|
47
|
+
await writeProjects(directory, projects);
|
|
48
|
+
return projects;
|
|
49
|
+
}
|
|
50
|
+
function rootOf(config) {
|
|
51
|
+
return {
|
|
52
|
+
dev: config.dev,
|
|
53
|
+
ino: config.ino,
|
|
54
|
+
kind: config.kind,
|
|
55
|
+
name: config.kind === "repo" ? basename(config.root) : "TalentOS folder",
|
|
56
|
+
root: config.root,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Adds a checked root once; the same folder again returns the project it has. */
|
|
60
|
+
export async function registerRoot(directory, config, name) {
|
|
61
|
+
const projects = await readProjects(directory);
|
|
62
|
+
const known = projects.find((project) => project.root === config.root);
|
|
63
|
+
if (known) {
|
|
64
|
+
return known;
|
|
65
|
+
}
|
|
66
|
+
if (projects.length >= PROJECTS_MAX) {
|
|
67
|
+
throw new Error(`An agent can have ${PROJECTS_MAX} projects. Remove one first.`);
|
|
68
|
+
}
|
|
69
|
+
const project = {
|
|
70
|
+
...rootOf(config),
|
|
71
|
+
...(name ? { name } : {}),
|
|
72
|
+
id: newProjectId(),
|
|
73
|
+
};
|
|
74
|
+
await writeProjects(directory, [...projects, project]);
|
|
75
|
+
return project;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The repo that contains `folder`, checked like `native enable` checks its
|
|
79
|
+
* root: a git top level, the same folder by device and inode, yours, and
|
|
80
|
+
* neither home, `/` nor anything holding credentials. Throws a plain reason.
|
|
81
|
+
*/
|
|
82
|
+
export async function checkRepo(folder, home) {
|
|
83
|
+
const top = await repoRoot(folder);
|
|
84
|
+
if (!top) {
|
|
85
|
+
throw new Error("That folder isn't in a git repository.");
|
|
86
|
+
}
|
|
87
|
+
const root = realpathSync.native(top);
|
|
88
|
+
const stats = statSync(root);
|
|
89
|
+
const config = {
|
|
90
|
+
dev: stats.dev,
|
|
91
|
+
ino: stats.ino,
|
|
92
|
+
kind: "repo",
|
|
93
|
+
root,
|
|
94
|
+
};
|
|
95
|
+
const problem = rootProblem(config, home);
|
|
96
|
+
if (problem) {
|
|
97
|
+
throw new Error(problem);
|
|
98
|
+
}
|
|
99
|
+
return config;
|
|
100
|
+
}
|
|
101
|
+
export async function removeProject(directory, id) {
|
|
102
|
+
const projects = await readProjects(directory);
|
|
103
|
+
const kept = projects.filter((project) => project.id !== id);
|
|
104
|
+
if (kept.length === projects.length) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
await writeProjects(directory, kept);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
/** What the server may know: ids and names, never a path. */
|
|
111
|
+
export const mirrorOf = (projects) => projects.map(({ id, name }) => ({ id, name }));
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { canonical, isInside } from "./paths.js";
|
|
2
|
+
import { applyPatchPaths, } from "./policy.js";
|
|
2
3
|
import { parseShell } from "./shell.js";
|
|
3
4
|
// "Private-touched" (#10987 PR 2b, R12 R1): did this call reach into the owner's
|
|
4
5
|
// own setup, so that what it returns may be private? The server holds back what
|
|
@@ -56,6 +57,15 @@ export function touchesPrivate(call, context, allowedByPolicy) {
|
|
|
56
57
|
if (NEUTRAL_TOOLS.has(tool)) {
|
|
57
58
|
return false;
|
|
58
59
|
}
|
|
60
|
+
// Codex's apply_patch touches private setup only if a path leaves the
|
|
61
|
+
// workspace; view_image is a read, touched when the image is outside it.
|
|
62
|
+
if (tool === "apply_patch") {
|
|
63
|
+
return applyPatchPaths(text(input.command)).some((path) => !inside(path));
|
|
64
|
+
}
|
|
65
|
+
if (tool === "view_image") {
|
|
66
|
+
const path = text(input.path) ?? text(input.file_path);
|
|
67
|
+
return path === undefined ? true : !inside(path);
|
|
68
|
+
}
|
|
59
69
|
if (PATH_TOOLS.has(tool) || EDIT_TOOLS.has(tool)) {
|
|
60
70
|
const paths = [
|
|
61
71
|
text(input.file_path),
|