talentos 1.4.10 → 1.4.11
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/README.md +3 -3
- package/dist/agent/adapters.js +75 -11
- package/dist/agent/codex-policy.js +11 -0
- package/dist/agent/codex-probe.js +9 -5
- package/dist/agent/mcp-entry.js +22 -9
- package/dist/agent/native/approvals.js +15 -11
- package/dist/agent/native/codex-launch.js +1 -0
- package/dist/agent/native/gate.js +35 -1
- package/dist/agent/native/hook-entry.js +31 -8
- package/dist/agent/native/launch.js +1 -0
- package/dist/agent/native/policy.js +62 -14
- package/dist/agent/native/probe.js +13 -1
- package/dist/agent/native/touch.js +15 -1
- package/dist/agent/node-floor.js +140 -0
- package/dist/agent/serve.js +53 -7
- package/dist/agent-http.js +48 -0
- package/dist/agent-session.js +137 -0
- package/dist/args.js +1 -0
- package/dist/client.js +11 -2
- package/dist/commands/agent-session.js +160 -0
- package/dist/commands/agent.js +29 -3
- package/dist/commands/init.js +2 -2
- package/dist/commands/login.js +2 -2
- package/dist/gateway-install.js +50 -11
- package/dist/index.js +13 -4
- package/dist/install.js +39 -14
- package/dist/mcp-proxy.js +30 -2
- package/package.json +2 -2
|
@@ -443,7 +443,7 @@ export async function probeOwnerNative(input) {
|
|
|
443
443
|
}
|
|
444
444
|
}
|
|
445
445
|
}
|
|
446
|
-
function cacheKey(binary, version, hookEntry) {
|
|
446
|
+
export function cacheKey(binary, version, hookEntry) {
|
|
447
447
|
const real = realpathSync.native(binary);
|
|
448
448
|
return createHash("sha256")
|
|
449
449
|
.update(JSON.stringify([
|
|
@@ -455,6 +455,18 @@ function cacheKey(binary, version, hookEntry) {
|
|
|
455
455
|
]))
|
|
456
456
|
.digest("hex");
|
|
457
457
|
}
|
|
458
|
+
/** Whether the cached pass still names this exact binary, version, probe and hook code. */
|
|
459
|
+
export async function ownerNativeProbeCached(input) {
|
|
460
|
+
try {
|
|
461
|
+
const cached = JSON.parse(await readFile(input.cachePath, "utf8"));
|
|
462
|
+
return (cached.ok === true &&
|
|
463
|
+
cached.key ===
|
|
464
|
+
cacheKey(input.binary, input.version, input.hookEntry ?? HOOK_ENTRY));
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
458
470
|
/** Cached probe: re-proves only when the binary, its version, the probe or the hook code changed. */
|
|
459
471
|
export async function ensureOwnerNativeProbe(input) {
|
|
460
472
|
const hookEntry = input.hookEntry ?? HOOK_ENTRY;
|
|
@@ -12,6 +12,9 @@ import { parseShell } from "./shell.js";
|
|
|
12
12
|
// workspace, a command the policy had to ask about, a tool we do not know)
|
|
13
13
|
// counts as touched.
|
|
14
14
|
const ROOM_SERVER_PREFIX = "mcp__talentos_room__";
|
|
15
|
+
// The room tools that reach beyond a room's own id: `list_rooms` has no roomId
|
|
16
|
+
// and names every room the agent can reach.
|
|
17
|
+
const ALL_ROOMS_TOOL = `${ROOM_SERVER_PREFIX}list_rooms`;
|
|
15
18
|
// Tools that read files by path; the path decides.
|
|
16
19
|
const PATH_TOOLS = new Set(["Glob", "Grep", "LS", "NotebookRead", "Read"]);
|
|
17
20
|
const EDIT_TOOLS = new Set(["Edit", "MultiEdit", "NotebookEdit", "Write"]);
|
|
@@ -37,6 +40,16 @@ const PATH_START = /[/~]/u;
|
|
|
37
40
|
function text(value) {
|
|
38
41
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
39
42
|
}
|
|
43
|
+
// A room tool aimed at another room reads words the server's "alone" check does
|
|
44
|
+
// not cover: it judges the job's own room only (#13436). A job whose room is
|
|
45
|
+
// unknown fails closed, so every room read taints.
|
|
46
|
+
function readsOtherRoom(call, jobRoomId) {
|
|
47
|
+
if (call.tool === ALL_ROOMS_TOOL) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
const aimed = call.toolInput.roomId;
|
|
51
|
+
return aimed !== undefined && aimed !== jobRoomId;
|
|
52
|
+
}
|
|
40
53
|
export function touchesPrivate(call, context, allowedByPolicy) {
|
|
41
54
|
// The policy had to ask the owner: it is outward, irreversible or unknown, and
|
|
42
55
|
// once approved it may read anything.
|
|
@@ -52,7 +65,8 @@ export function touchesPrivate(call, context, allowedByPolicy) {
|
|
|
52
65
|
return context.writeRoots.some((root) => isInside(real, root));
|
|
53
66
|
};
|
|
54
67
|
if (tool.startsWith("mcp__")) {
|
|
55
|
-
return !tool.startsWith(ROOM_SERVER_PREFIX)
|
|
68
|
+
return (!tool.startsWith(ROOM_SERVER_PREFIX) ||
|
|
69
|
+
readsOtherRoom(call, context.roomId));
|
|
56
70
|
}
|
|
57
71
|
if (NEUTRAL_TOOLS.has(tool)) {
|
|
58
72
|
return false;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import { launcherNodeCandidates, npmSpawnEnv } from "../gateway-install.js";
|
|
4
|
+
import { compareGatewayVersions } from "../gateway-version.js";
|
|
5
|
+
const exec = promisify(execFile);
|
|
6
|
+
const NPM_VIEW_TIMEOUT_MS = 15_000;
|
|
7
|
+
// Only the grammar `engines.node` uses here: `||` sets of space-separated
|
|
8
|
+
// comparators. Anything else (^, ~, x, hyphen ranges) is "can't tell".
|
|
9
|
+
const COMPARATOR = /^(>=|<=|>|<|=)?(\d+)(?:\.(\d+))?(?:\.(\d+))?$/u;
|
|
10
|
+
const WHITESPACE = /\s+/u;
|
|
11
|
+
const NODE_RANGE = /^[\w<>=.|\s-]{1,200}$/u;
|
|
12
|
+
function compareTriples(a, b) {
|
|
13
|
+
return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
|
|
14
|
+
}
|
|
15
|
+
function satisfiesComparator(node, token) {
|
|
16
|
+
const match = COMPARATOR.exec(token);
|
|
17
|
+
if (!match) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const [, operator = "=", major, minor, patch] = match;
|
|
21
|
+
const partial = minor === undefined || patch === undefined;
|
|
22
|
+
const bound = [Number(major), Number(minor ?? 0), Number(patch ?? 0)];
|
|
23
|
+
const difference = compareTriples(node, bound);
|
|
24
|
+
if (operator === ">=") {
|
|
25
|
+
return difference >= 0;
|
|
26
|
+
}
|
|
27
|
+
if (operator === "<") {
|
|
28
|
+
return difference < 0;
|
|
29
|
+
}
|
|
30
|
+
// A partial bound on the others means "the whole 22.x", which this does not
|
|
31
|
+
// model.
|
|
32
|
+
if (partial) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (operator === ">") {
|
|
36
|
+
return difference > 0;
|
|
37
|
+
}
|
|
38
|
+
return operator === "<=" ? difference <= 0 : difference === 0;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether `nodeVersion` satisfies an `engines.node` range: true, false, or
|
|
42
|
+
* undefined when the range is outside the small grammar read here, so the
|
|
43
|
+
* caller never blocks on a range it did not understand.
|
|
44
|
+
*/
|
|
45
|
+
export function nodeSatisfies(range, nodeVersion) {
|
|
46
|
+
const node = nodeVersion.split(".").map(Number);
|
|
47
|
+
if (!NODE_RANGE.test(range) ||
|
|
48
|
+
node.length !== 3 ||
|
|
49
|
+
node.some((part) => !Number.isInteger(part))) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
let unknown = false;
|
|
53
|
+
for (const set of range.split("||")) {
|
|
54
|
+
const results = set
|
|
55
|
+
.trim()
|
|
56
|
+
.split(WHITESPACE)
|
|
57
|
+
.map((token) => satisfiesComparator(node, token));
|
|
58
|
+
if (results.length === 0 || results.includes(undefined)) {
|
|
59
|
+
unknown = true;
|
|
60
|
+
}
|
|
61
|
+
else if (results.every(Boolean)) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return unknown ? undefined : false;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The `engines.node` range the registry publishes for a CLI version, or
|
|
69
|
+
* undefined when it cannot be read (offline, npm missing, no range): the
|
|
70
|
+
* caller then updates as before and never goes offline over a metadata miss.
|
|
71
|
+
*/
|
|
72
|
+
export async function targetNodeRange(version, signal) {
|
|
73
|
+
// The version reaches a shell on Windows, so only a plain version passes.
|
|
74
|
+
if (compareGatewayVersions(version, version) === undefined) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const windows = process.platform === "win32";
|
|
78
|
+
try {
|
|
79
|
+
const { stdout } = await exec(windows ? "npm.cmd" : "npm", ["view", `talentos@${version}`, "engines.node", "--json"], {
|
|
80
|
+
env: npmSpawnEnv(),
|
|
81
|
+
shell: windows,
|
|
82
|
+
signal,
|
|
83
|
+
timeout: NPM_VIEW_TIMEOUT_MS,
|
|
84
|
+
});
|
|
85
|
+
const range = JSON.parse(stdout.trim() || "null");
|
|
86
|
+
return typeof range === "string" && range.length <= 200 ? range : undefined;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const PROBE_TIMEOUT_MS = 3000;
|
|
93
|
+
// The whole candidate walk is bounded: a hung Node must not hold up claims.
|
|
94
|
+
const PROBE_BUDGET_MS = 10_000;
|
|
95
|
+
const NODE_VERSION = /^\d{1,3}\.\d{1,3}\.\d{1,3}$/u;
|
|
96
|
+
/** The version a Node executable runs, as the launcher asks it; undefined when it is missing, not executable or silent. */
|
|
97
|
+
export async function probeNodeVersion(path, signal) {
|
|
98
|
+
try {
|
|
99
|
+
const { stdout } = await exec(path, ["-p", "process.versions.node"], {
|
|
100
|
+
signal,
|
|
101
|
+
timeout: PROBE_TIMEOUT_MS,
|
|
102
|
+
});
|
|
103
|
+
const version = stdout.trim();
|
|
104
|
+
return NODE_VERSION.test(version) ? version : undefined;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Whether installing `version` would leave this computer unable to start it:
|
|
112
|
+
* `engines.node` excludes the running Node AND every other Node the launcher
|
|
113
|
+
* would start (its own candidate list, probed). Anything unreadable, or a range
|
|
114
|
+
* outside the grammar read here, is not a block.
|
|
115
|
+
*/
|
|
116
|
+
export async function nodeBlocksUpdate(version, options = {}) {
|
|
117
|
+
const nodeVersion = options.nodeVersion ?? process.versions.node;
|
|
118
|
+
const range = await (options.range ?? targetNodeRange)(version, options.signal);
|
|
119
|
+
if (!range || nodeSatisfies(range, nodeVersion) !== false) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const probe = options.probe ?? probeNodeVersion;
|
|
123
|
+
const deadline = Date.now() + PROBE_BUDGET_MS;
|
|
124
|
+
for (const candidate of await (options.candidates?.() ??
|
|
125
|
+
launcherNodeCandidates())) {
|
|
126
|
+
if (Date.now() >= deadline || options.signal?.aborted) {
|
|
127
|
+
// Out of time to look is not proof that none qualifies.
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const found = await probe(candidate, options.signal);
|
|
131
|
+
if (found && nodeSatisfies(range, found) !== false) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
forVersion: version,
|
|
137
|
+
range,
|
|
138
|
+
message: `TalentOS CLI ${version} needs Node ${range}; this computer runs ${nodeVersion}. Update Node, then run npx talentos@${version} agent repair.`,
|
|
139
|
+
};
|
|
140
|
+
}
|
package/dist/agent/serve.js
CHANGED
|
@@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { mkdir, open, readFile, rm } from "node:fs/promises";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
-
import { installSharedGateway, newerInstalledVersion, } from "../gateway-install.js";
|
|
5
|
+
import { installSharedGateway, launcherNodeCandidates, newerInstalledVersion, } from "../gateway-install.js";
|
|
6
6
|
import { compareGatewayVersions } from "../gateway-version.js";
|
|
7
7
|
import { homeDir } from "../install.js";
|
|
8
8
|
import { RoomToolsUnavailableError, runProvider, } from "./adapters.js";
|
|
@@ -13,6 +13,7 @@ import { NativeGate } from "./native/gate.js";
|
|
|
13
13
|
import { ROOM_ONLY_STATUS, roomOnlyStatus } from "./native/native-issue.js";
|
|
14
14
|
import { ProjectRequests } from "./native/project-requests.js";
|
|
15
15
|
import { mirrorOf } from "./native/projects.js";
|
|
16
|
+
import { nodeBlocksUpdate } from "./node-floor.js";
|
|
16
17
|
import { failureReasonOf } from "./provider-failure.js";
|
|
17
18
|
import { sweepRoomJobs } from "./room-profile.js";
|
|
18
19
|
const HEARTBEAT_MS = 20_000;
|
|
@@ -21,6 +22,9 @@ const NATIVE_RENEW_MS = 10_000;
|
|
|
21
22
|
// A launch waits this long for the server before it starts the provider anyway.
|
|
22
23
|
const START_WAIT_MS = 120_000;
|
|
23
24
|
const IDLE_MS = 30 * 60_000;
|
|
25
|
+
// A declined update (no Node here can run it) is not looked at again sooner,
|
|
26
|
+
// unless the floor it was declined for changes.
|
|
27
|
+
const NODE_BLOCK_RETRY_MS = 60 * 60_000;
|
|
24
28
|
// The room-only reason stays up this long before progress lines resume.
|
|
25
29
|
const FALLBACK_HOLD_MS = 5000;
|
|
26
30
|
export function needsUpdate(current, minimum) {
|
|
@@ -87,7 +91,10 @@ export async function serveAgent(config, directory, version, stop, verify, optio
|
|
|
87
91
|
wakeResolve?.();
|
|
88
92
|
};
|
|
89
93
|
let active;
|
|
90
|
-
let minimum = version,
|
|
94
|
+
let minimum = version,
|
|
95
|
+
// The Node range of an update this computer declined, reported until the
|
|
96
|
+
// update is no longer blocked.
|
|
97
|
+
blocked, lastJobAt = Date.now(), push = false, dispose;
|
|
91
98
|
let terminalError;
|
|
92
99
|
const failService = (error) => {
|
|
93
100
|
if (error instanceof GatewayError &&
|
|
@@ -116,6 +123,11 @@ export async function serveAgent(config, directory, version, stop, verify, optio
|
|
|
116
123
|
const issue = gate.issue?.();
|
|
117
124
|
const response = await gateway.request("/agent-gateway/heartbeat", {
|
|
118
125
|
cliVersion: version,
|
|
126
|
+
nodeVersion: options.node?.nodeVersion ?? process.versions.node,
|
|
127
|
+
// Only while the update it was declined for is still the one due.
|
|
128
|
+
...(blocked?.forVersion === minimum && needsUpdate(version, minimum)
|
|
129
|
+
? { updateBlockedNodeRange: blocked.range }
|
|
130
|
+
: {}),
|
|
119
131
|
nativeOptIn: gate.reportable(),
|
|
120
132
|
...(mirror ? { projects: mirror } : {}),
|
|
121
133
|
// Why an opted-in computer can't use full setup (an untested Codex),
|
|
@@ -272,16 +284,45 @@ export async function serveAgent(config, directory, version, stop, verify, optio
|
|
|
272
284
|
return newer;
|
|
273
285
|
}
|
|
274
286
|
}
|
|
287
|
+
// A block is for one floor: a new floor is looked at at once, and when
|
|
288
|
+
// no update is due the range stops being sent.
|
|
289
|
+
if (blocked &&
|
|
290
|
+
(blocked.forVersion !== minimum || !needsUpdate(version, minimum))) {
|
|
291
|
+
blocked = undefined;
|
|
292
|
+
updateAfter = 0;
|
|
293
|
+
}
|
|
275
294
|
if (needsUpdate(version, minimum) && Date.now() >= updateAfter) {
|
|
276
295
|
if (process.platform === "win32") {
|
|
277
296
|
throw new Error(`Gateway update required; re-run npx talentos@${minimum} agent serve --agent ${config.agentId}. Windows gateways update in the foreground.`);
|
|
278
297
|
}
|
|
279
|
-
|
|
280
|
-
|
|
298
|
+
// Never update into a CLI no Node here can start: it would go offline
|
|
299
|
+
// with nothing but the owner's terminal saying why. An unreadable or
|
|
300
|
+
// unfamiliar range is not a block, and the update goes ahead.
|
|
301
|
+
const block = await nodeBlocksUpdate(minimum, {
|
|
302
|
+
candidates: () => launcherNodeCandidates(dirname(dirname(dirname(directory)))),
|
|
303
|
+
...options.node,
|
|
304
|
+
signal,
|
|
305
|
+
});
|
|
306
|
+
if (block) {
|
|
307
|
+
updateAfter = Date.now() + NODE_BLOCK_RETRY_MS;
|
|
308
|
+
if (block.range !== blocked?.range ||
|
|
309
|
+
block.forVersion !== blocked.forVersion) {
|
|
310
|
+
blocked = block;
|
|
311
|
+
console.error(block.message);
|
|
312
|
+
// Tell the server now, not at the next beat, so a waiting job's
|
|
313
|
+
// room line names Node.
|
|
314
|
+
await heartbeat().catch(failService);
|
|
315
|
+
}
|
|
281
316
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
317
|
+
else {
|
|
318
|
+
blocked = undefined;
|
|
319
|
+
try {
|
|
320
|
+
return await installGatewayUpdate(directory, minimum, signal);
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
updateAfter = Date.now() + 60_000;
|
|
324
|
+
console.error("Gateway update unavailable. Serving the current version; retrying in one minute.");
|
|
325
|
+
}
|
|
285
326
|
}
|
|
286
327
|
}
|
|
287
328
|
if (config.provider === "codex") {
|
|
@@ -401,6 +442,11 @@ export async function serveAgent(config, directory, version, stop, verify, optio
|
|
|
401
442
|
job,
|
|
402
443
|
workDir,
|
|
403
444
|
owner: owner && { ...owner, watch: bridge },
|
|
445
|
+
// Tells the server the job read the web, before it can write
|
|
446
|
+
// what a page steered (#13163).
|
|
447
|
+
webRead: async () => {
|
|
448
|
+
await gateway.request(`/agent-gateway/jobs/${job.jobId}/web-read`, { leaseToken: job.leaseToken }, signal);
|
|
449
|
+
},
|
|
404
450
|
// Room tools and the current packet carry continuity. Replaying
|
|
405
451
|
// provider history grows forever and carries stale file copies.
|
|
406
452
|
signal: AbortSignal.any([signal, cancel.signal]),
|
package/dist/agent-http.js
CHANGED
|
@@ -152,6 +152,41 @@ export async function unlinkRemoteAgent(agent) {
|
|
|
152
152
|
}
|
|
153
153
|
return true;
|
|
154
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Asks, with the agent's gateway key, for a terminal key bound to the same
|
|
157
|
+
* agent (#10527). Only the new key's hash is sent; a repeat replaces the
|
|
158
|
+
* agent's previous terminal key, so `replaced` says one was cut.
|
|
159
|
+
*/
|
|
160
|
+
export async function registerSessionKey(agent, keyHash) {
|
|
161
|
+
const response = await safeFetch(`${apiOrigin(agent.apiUrl)}/agent-link/session-key`, {
|
|
162
|
+
method: "POST",
|
|
163
|
+
headers: {
|
|
164
|
+
Authorization: `Bearer ${agent.apiKey}`,
|
|
165
|
+
"Content-Type": "application/json",
|
|
166
|
+
},
|
|
167
|
+
body: JSON.stringify({ keyHash }),
|
|
168
|
+
});
|
|
169
|
+
if (response.status === 409) {
|
|
170
|
+
// The server's own sentence: a busy agent says to try again later.
|
|
171
|
+
throw new Error((await refusalSentence(response)) ?? "Could not connect this terminal.");
|
|
172
|
+
}
|
|
173
|
+
if (!response.ok) {
|
|
174
|
+
throw new AgentHttpError("Could not connect this terminal", response);
|
|
175
|
+
}
|
|
176
|
+
const value = (await readJson(response));
|
|
177
|
+
return { replaced: value.replaced === true };
|
|
178
|
+
}
|
|
179
|
+
/** Stops every terminal key of the agent. False when the agent itself is gone. */
|
|
180
|
+
export async function revokeSessionKeys(agent) {
|
|
181
|
+
const response = await safeFetch(`${apiOrigin(agent.apiUrl)}/agent-link/session-key`, { method: "DELETE", headers: { Authorization: `Bearer ${agent.apiKey}` } });
|
|
182
|
+
if (response.status === 401) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
if (!response.ok) {
|
|
186
|
+
throw new AgentHttpError("Could not disconnect this terminal", response);
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
155
190
|
export async function waitForHeartbeat(agent, options = {}) {
|
|
156
191
|
const deadline = Date.now() + (options.timeoutMs ?? 10 * 60_000);
|
|
157
192
|
let serverStartedAt;
|
|
@@ -218,6 +253,19 @@ async function linkRefusal(response, token) {
|
|
|
218
253
|
}
|
|
219
254
|
: undefined;
|
|
220
255
|
}
|
|
256
|
+
async function refusalSentence(response) {
|
|
257
|
+
try {
|
|
258
|
+
const value = (await response.json());
|
|
259
|
+
return typeof value.error === "string" &&
|
|
260
|
+
value.error.length <= REFUSAL_MAX_LENGTH &&
|
|
261
|
+
ONE_LINE.test(value.error)
|
|
262
|
+
? value.error
|
|
263
|
+
: undefined;
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
221
269
|
async function readJson(response) {
|
|
222
270
|
try {
|
|
223
271
|
return await response.json();
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { chmod, mkdir, readFile, realpath, rm, writeFile, } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, parse, sep } from "node:path";
|
|
4
|
+
import { agentsDir, isMissing, writePrivateFile } from "./agent-config.js";
|
|
5
|
+
import { gatewayLauncherPath } from "./gateway-install.js";
|
|
6
|
+
import { CONFIG_MODE, claudeRun, codexConfigPath, onPath, readIfPresent, removeCodexSection, upsertCodexSection, } from "./install.js";
|
|
7
|
+
/**
|
|
8
|
+
* The member's everyday terminal acts in rooms as their linked agent (#10527).
|
|
9
|
+
* It registers as its own MCP server beside the personal `talentos` one, and
|
|
10
|
+
* as a stdio command, so the key stays in this private file: never in the
|
|
11
|
+
* client's config, its argv, or a log.
|
|
12
|
+
*/
|
|
13
|
+
export const SESSION_SERVER = "talentos-room";
|
|
14
|
+
export const SESSION_TARGETS = ["claude", "codex"];
|
|
15
|
+
const CLAUDE_LIST_TIMEOUT_MS = 10_000;
|
|
16
|
+
const NOT_ON_PATH = "the `claude` command is not on PATH";
|
|
17
|
+
export function sessionKeyPath(agentId, home) {
|
|
18
|
+
return join(agentsDir(home), agentId, "session.json");
|
|
19
|
+
}
|
|
20
|
+
export async function readSessionKey(agentId, home) {
|
|
21
|
+
try {
|
|
22
|
+
const value = JSON.parse(await readFile(sessionKeyPath(agentId, home), "utf8"));
|
|
23
|
+
return typeof value.apiKey === "string" &&
|
|
24
|
+
value.apiKey.startsWith("tos_") &&
|
|
25
|
+
typeof value.apiUrl === "string"
|
|
26
|
+
? { apiKey: value.apiKey, apiUrl: value.apiUrl }
|
|
27
|
+
: undefined;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (isMissing(error) || error instanceof SyntaxError) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export async function writeSessionKey(agentId, key, home) {
|
|
37
|
+
await writePrivateFile(sessionKeyPath(agentId, home), `${JSON.stringify(key, null, 2)}\n`);
|
|
38
|
+
}
|
|
39
|
+
export async function removeSessionKey(agentId, home) {
|
|
40
|
+
await rm(sessionKeyPath(agentId, home), { force: true });
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The folder a terminal's `upload_file` may read: where the session runs, but
|
|
44
|
+
* never the home folder, a folder that contains it, or the filesystem root.
|
|
45
|
+
*/
|
|
46
|
+
export async function sessionUploadRoot(cwd, home) {
|
|
47
|
+
try {
|
|
48
|
+
const [real, realHome] = await Promise.all([realpath(cwd), realpath(home)]);
|
|
49
|
+
// Home itself, anything that contains it (/home, /Users, /), or the root
|
|
50
|
+
// would reach everything under the home folder.
|
|
51
|
+
return real === parse(real).root ||
|
|
52
|
+
real === realHome ||
|
|
53
|
+
realHome.startsWith(real + sep)
|
|
54
|
+
? undefined
|
|
55
|
+
: real;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** The command a client runs: the stable launcher when installed, else npx. */
|
|
62
|
+
export function sessionEntry(agentId, home, platform = process.platform) {
|
|
63
|
+
const tail = ["agent", "mcp", "--agent", agentId, "--surface", "session"];
|
|
64
|
+
const launcher = gatewayLauncherPath(home);
|
|
65
|
+
return platform !== "win32" && existsSync(launcher)
|
|
66
|
+
? { command: launcher, args: tail }
|
|
67
|
+
: { command: "npx", args: ["-y", "talentos@1", ...tail] };
|
|
68
|
+
}
|
|
69
|
+
/** The command to run by hand, for a client we could not write to. */
|
|
70
|
+
export function sessionSnippet(target, entry) {
|
|
71
|
+
return target === "claude"
|
|
72
|
+
? `claude mcp add --scope user ${SESSION_SERVER} -- ${[entry.command, ...entry.args].join(" ")}`
|
|
73
|
+
: codexBlock(entry);
|
|
74
|
+
}
|
|
75
|
+
export async function registerSession(target, entry, home, run = claudeRun, hasClaude = () => onPath("claude")) {
|
|
76
|
+
if (target === "claude") {
|
|
77
|
+
return registerClaude(entry, run, hasClaude);
|
|
78
|
+
}
|
|
79
|
+
const path = codexConfigPath(home);
|
|
80
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
81
|
+
await writeFile(path, upsertCodexSection(await readIfPresent(path), SESSION_SERVER, codexBlock(entry)), { mode: CONFIG_MODE });
|
|
82
|
+
await chmod(path, CONFIG_MODE);
|
|
83
|
+
return { done: true, detail: path };
|
|
84
|
+
}
|
|
85
|
+
export async function unregisterSession(target, home, run = claudeRun, hasClaude = () => onPath("claude")) {
|
|
86
|
+
if (target === "claude") {
|
|
87
|
+
if (!hasClaude()) {
|
|
88
|
+
return { done: false, detail: NOT_ON_PATH };
|
|
89
|
+
}
|
|
90
|
+
run(["mcp", "remove", "--scope", "user", SESSION_SERVER]);
|
|
91
|
+
return { done: true, detail: "claude mcp remove --scope user" };
|
|
92
|
+
}
|
|
93
|
+
const path = codexConfigPath(home);
|
|
94
|
+
const existing = await readIfPresent(path);
|
|
95
|
+
if (existing !== undefined) {
|
|
96
|
+
const next = removeCodexSection(existing, SESSION_SERVER);
|
|
97
|
+
if (next !== existing) {
|
|
98
|
+
await writeFile(path, next, { mode: CONFIG_MODE });
|
|
99
|
+
await chmod(path, CONFIG_MODE);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { done: true, detail: path };
|
|
103
|
+
}
|
|
104
|
+
function registerClaude(entry, run, hasClaude) {
|
|
105
|
+
if (!hasClaude()) {
|
|
106
|
+
return { done: false, detail: NOT_ON_PATH };
|
|
107
|
+
}
|
|
108
|
+
const listed = run(["mcp", "list"], { timeout: CLAUDE_LIST_TIMEOUT_MS });
|
|
109
|
+
// Re-adding on top of an existing entry keeps the old command, so drop it.
|
|
110
|
+
if (listed.error === undefined && listed.stdout?.includes(SESSION_SERVER)) {
|
|
111
|
+
run(["mcp", "remove", "--scope", "user", SESSION_SERVER]);
|
|
112
|
+
}
|
|
113
|
+
const added = run([
|
|
114
|
+
"mcp",
|
|
115
|
+
"add",
|
|
116
|
+
"--scope",
|
|
117
|
+
"user",
|
|
118
|
+
SESSION_SERVER,
|
|
119
|
+
"--",
|
|
120
|
+
entry.command,
|
|
121
|
+
...entry.args,
|
|
122
|
+
]);
|
|
123
|
+
if (added.status !== 0) {
|
|
124
|
+
return {
|
|
125
|
+
done: false,
|
|
126
|
+
detail: added.stderr?.trim().split("\n")[0] || "`claude mcp add` failed",
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return { done: true, detail: "claude mcp add --scope user" };
|
|
130
|
+
}
|
|
131
|
+
function codexBlock(entry) {
|
|
132
|
+
return [
|
|
133
|
+
`[mcp_servers.${SESSION_SERVER}]`,
|
|
134
|
+
`command = ${JSON.stringify(entry.command)}`,
|
|
135
|
+
`args = [${entry.args.map((arg) => JSON.stringify(arg)).join(", ")}]`,
|
|
136
|
+
].join("\n");
|
|
137
|
+
}
|
package/dist/args.js
CHANGED
|
@@ -24,6 +24,7 @@ const CLI_OPTIONS = {
|
|
|
24
24
|
agent: { type: "string" },
|
|
25
25
|
"upload-root": { type: "string" },
|
|
26
26
|
"owner-job-dir": { type: "string" },
|
|
27
|
+
surface: { type: "string" },
|
|
27
28
|
};
|
|
28
29
|
/** Groups whose first positional is a sub-command rather than an argument. */
|
|
29
30
|
const ACTION_GROUPS = new Set(["agent", "mcp", "skills", "memory", "routines"]);
|
package/dist/client.js
CHANGED
|
@@ -21,6 +21,7 @@ export const UNAUTHORIZED = "Not logged in or key revoked. Run `talentos login`.
|
|
|
21
21
|
export class AgentClient {
|
|
22
22
|
credentials;
|
|
23
23
|
client;
|
|
24
|
+
connecting;
|
|
24
25
|
retryDelaysMs;
|
|
25
26
|
constructor(credentials, retryDelaysMs = LIST_RETRY_DELAYS_MS) {
|
|
26
27
|
this.credentials = credentials;
|
|
@@ -65,10 +66,18 @@ export class AgentClient {
|
|
|
65
66
|
}
|
|
66
67
|
return await run();
|
|
67
68
|
}
|
|
68
|
-
|
|
69
|
+
// One connection at a time: the proxy lists at boot while a call may already
|
|
70
|
+
// be waiting, and two connects would leave the first session open and unused.
|
|
71
|
+
connect() {
|
|
69
72
|
if (this.client) {
|
|
70
|
-
return this.client;
|
|
73
|
+
return Promise.resolve(this.client);
|
|
71
74
|
}
|
|
75
|
+
this.connecting ??= this.open().finally(() => {
|
|
76
|
+
this.connecting = undefined;
|
|
77
|
+
});
|
|
78
|
+
return this.connecting;
|
|
79
|
+
}
|
|
80
|
+
async open() {
|
|
72
81
|
const client = new Client(CLIENT_INFO, { capabilities: {} });
|
|
73
82
|
const transport = new StreamableHTTPClientTransport(new URL(`${this.credentials.apiUrl}/mcp`), {
|
|
74
83
|
requestInit: {
|