probe-research 0.41.0 → 0.42.0
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/lib/sign-in.js +182 -11
- package/package.json +1 -1
package/lib/sign-in.js
CHANGED
|
@@ -43,17 +43,22 @@ const AGENT_CLIENT_CONTEXT = Object.freeze({ install: true, surface: "agent" });
|
|
|
43
43
|
const AGENT_GRANTS = Object.freeze(["api", "mcp"]);
|
|
44
44
|
const TOKEN_NAME_PREFIX = "Probe CLI · ";
|
|
45
45
|
/**
|
|
46
|
-
* Each coding agent
|
|
47
|
-
*
|
|
48
|
-
*
|
|
46
|
+
* Each coding agent, in the harness registry's order (`harnesses.json`: id, detect_env, session_env,
|
|
47
|
+
* the names its process goes by (binary, process_title), pid_env): the agent this sign-in runs in,
|
|
48
|
+
* named on the approval page (`client_context.agent`, display only, D79).
|
|
49
|
+
* agent/tests/test_launcher_matches_cli.py holds this table to the registry.
|
|
49
50
|
*/
|
|
50
51
|
const AGENT_MARKERS = Object.freeze([
|
|
51
|
-
["claude_code", ["CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT"], "CLAUDE_CODE_SESSION_ID"],
|
|
52
|
-
["codex", ["CODEX_SANDBOX", "CODEX_THREAD_ID"], "CODEX_THREAD_ID"],
|
|
53
|
-
["pi", ["PI_CODING_AGENT"], "PI_SESSION_ID"],
|
|
54
|
-
["kimi_code", ["PROBE_KIMI_SESSION_ID"], "PROBE_KIMI_SESSION_ID"],
|
|
55
|
-
["cursor_agent", ["CURSOR_AGENT"], "CURSOR_CONVERSATION_ID"],
|
|
52
|
+
["claude_code", ["CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT"], "CLAUDE_CODE_SESSION_ID", ["claude"], "CLAUDE_PID"],
|
|
53
|
+
["codex", ["CODEX_SANDBOX", "CODEX_THREAD_ID"], "CODEX_THREAD_ID", ["codex"], null],
|
|
54
|
+
["pi", ["PI_CODING_AGENT"], "PI_SESSION_ID", ["pi"], null],
|
|
55
|
+
["kimi_code", ["PROBE_KIMI_SESSION_ID"], "PROBE_KIMI_SESSION_ID", ["kimi", "kimi-code"], null],
|
|
56
|
+
["cursor_agent", ["CURSOR_AGENT"], "CURSOR_CONVERSATION_ID", ["cursor-agent"], null],
|
|
56
57
|
]);
|
|
58
|
+
/** Script runtimes: a process started as `node /usr/local/bin/pi` is pi (the CLI's `_title_of`). */
|
|
59
|
+
const SCRIPT_RUNTIMES = Object.freeze(["node", "nodejs", "bun"]);
|
|
60
|
+
/** How far up the walk goes: agent -> shell -> npx -> this launcher. */
|
|
61
|
+
const ANCESTOR_LIMIT = 16;
|
|
57
62
|
/**
|
|
58
63
|
* Hosted agents, whose chats run on the agent maker's computers, never the person's (plan D12): a
|
|
59
64
|
* marker each sets itself, never a guess from the machine (a Docker container on the person's own
|
|
@@ -88,11 +93,175 @@ function getHostname() {
|
|
|
88
93
|
}
|
|
89
94
|
}
|
|
90
95
|
|
|
96
|
+
/** A process's title from its argv: the script a runtime runs, else argv[0]. */
|
|
97
|
+
function getTitle(argv) {
|
|
98
|
+
if (!argv.length) return "";
|
|
99
|
+
if (SCRIPT_RUNTIMES.includes(path.basename(argv[0])) && argv.length > 1 && !argv[1].startsWith("-")) return argv[1];
|
|
100
|
+
return argv[0];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function readLinuxProcess(pid) {
|
|
104
|
+
try {
|
|
105
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
106
|
+
const argv = fs.readFileSync(`/proc/${pid}/cmdline`).toString("utf8").split("\0").filter(Boolean);
|
|
107
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 1).trim().split(/\s+/);
|
|
108
|
+
return { parent: Number(fields[1]), title: getTitle(argv) };
|
|
109
|
+
} catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function runPs(columns) {
|
|
115
|
+
return require("node:child_process").execFileSync("ps", ["-A", "-ww", "-o", columns], {
|
|
116
|
+
encoding: "utf8", timeout: 3000, stdio: ["ignore", "pipe", "ignore"], env: { ...process.env, LC_ALL: "C" },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Every process as {pid: {parent, title}}, from `ps` (macOS). `command` cannot say where a path with
|
|
122
|
+
* a space ends (the Claude app keeps Claude Code under `~/Library/Application Support/`); `comm`, the
|
|
123
|
+
* last column, is the whole path: used when it starts with the command's first word (the CLI's
|
|
124
|
+
* `_ancestors` reads the kernel's path the same way).
|
|
125
|
+
*/
|
|
126
|
+
function readPsTable(run = runPs) {
|
|
127
|
+
const table = new Map();
|
|
128
|
+
try {
|
|
129
|
+
const paths = new Map();
|
|
130
|
+
for (const line of run("pid=,comm=").split("\n")) {
|
|
131
|
+
const match = line.trim().match(/^(\d+)\s+(.*)$/);
|
|
132
|
+
if (match) paths.set(Number(match[1]), match[2]);
|
|
133
|
+
}
|
|
134
|
+
for (const line of run("pid=,ppid=,command=").split("\n")) {
|
|
135
|
+
const match = line.trim().match(/^(\d+)\s+(\d+)\s+(.*)$/);
|
|
136
|
+
if (!match) continue;
|
|
137
|
+
const argv = match[3].split(/\s+/);
|
|
138
|
+
let title = getTitle(argv);
|
|
139
|
+
const whole = paths.get(Number(match[1]));
|
|
140
|
+
if (title === argv[0] && whole && whole.startsWith(`${argv[0]} `)) title = whole;
|
|
141
|
+
table.set(Number(match[1]), { parent: Number(match[2]), title });
|
|
142
|
+
}
|
|
143
|
+
} catch {
|
|
144
|
+
// no `ps`: nothing above can be read
|
|
145
|
+
}
|
|
146
|
+
return table;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The machine's own pid namespace: the kernel gives it this fixed inode (PROC_PID_INIT_INO). */
|
|
150
|
+
const ROOT_PID_NAMESPACE = "pid:[4026531836]";
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Whether this process runs in a child pid namespace (a sandbox, a container): the CLI's
|
|
154
|
+
* `_is_in_child_pid_namespace`. `NSpid` cannot say so where the sandbox mounted its own /proc.
|
|
155
|
+
*/
|
|
156
|
+
function isInChildPidNamespace() {
|
|
157
|
+
try {
|
|
158
|
+
return fs.readlinkSync("/proc/self/ns/pid") !== ROOT_PID_NAMESPACE;
|
|
159
|
+
} catch {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let ancestorsRead = null;
|
|
165
|
+
|
|
166
|
+
/** [pid, title] for this process's parent and each process above it, nearest first, up to pid 1. */
|
|
167
|
+
function getAncestors() {
|
|
168
|
+
if (ancestorsRead) return ancestorsRead;
|
|
169
|
+
const linux = fs.existsSync("/proc/self");
|
|
170
|
+
const table = linux ? null : readPsTable();
|
|
171
|
+
const chain = [];
|
|
172
|
+
let current = process.ppid;
|
|
173
|
+
for (let i = 0; i < ANCESTOR_LIMIT && current >= 1; i += 1) {
|
|
174
|
+
const info = linux ? readLinuxProcess(current) : table.get(current);
|
|
175
|
+
if (!info) break;
|
|
176
|
+
chain.push([current, info.title]);
|
|
177
|
+
current = info.parent;
|
|
178
|
+
}
|
|
179
|
+
ancestorsRead = chain;
|
|
180
|
+
return chain;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A process's own environment (Linux /proc only), or null. */
|
|
184
|
+
function readEnviron(pid) {
|
|
185
|
+
try {
|
|
186
|
+
const out = {};
|
|
187
|
+
for (const item of fs.readFileSync(`/proc/${pid}/environ`).toString("utf8").split("\0")) {
|
|
188
|
+
const at = item.indexOf("=");
|
|
189
|
+
if (at > 0) out[item.slice(0, at)] = item.slice(at + 1);
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
} catch {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function getAgentKeys([, markers, sessionEnv]) {
|
|
198
|
+
return [...markers, sessionEnv].filter(Boolean);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* `env` without the variables of the agents that do not run this process, when two or more agents'
|
|
203
|
+
* are set (a Codex started from a Claude Code shell inherits Claude Code's): the CLI's rule,
|
|
204
|
+
* `agent_session._find_nearest_agent_env`. The nearest agent above wins when sure (its own
|
|
205
|
+
* environment holds the others' values, or, where that cannot be read, the others are above it, or
|
|
206
|
+
* it is a sandbox's pid 1). An agent whose own pid (`pid_env`) is not above a chain read up to the
|
|
207
|
+
* machine's pid 1 that shows another agent has stale variables; in a child pid namespace no pid
|
|
208
|
+
* counts.
|
|
209
|
+
*/
|
|
210
|
+
function getNearestAgentEnv(env, readAncestors = getAncestors, environOf = readEnviron,
|
|
211
|
+
nested = isInChildPidNamespace) {
|
|
212
|
+
const present = AGENT_MARKERS.filter((row) => getAgentKeys(row).some((key) => env[key]));
|
|
213
|
+
if (present.length < 2) return env;
|
|
214
|
+
const ownPid = new Map();
|
|
215
|
+
for (const [id, , , , pidEnv] of present) {
|
|
216
|
+
const raw = pidEnv ? env[pidEnv] : null;
|
|
217
|
+
if (raw && /^\d+$/.test(raw) && Number(raw) > 1) ownPid.set(id, Number(raw));
|
|
218
|
+
}
|
|
219
|
+
const inChild = nested();
|
|
220
|
+
const byPid = inChild ? new Map() : new Map([...ownPid].map(([id, pid]) => [pid, id]));
|
|
221
|
+
const chain = readAncestors();
|
|
222
|
+
const complete = !inChild && chain.length > 0 && chain[chain.length - 1][0] === 1;
|
|
223
|
+
let found = [];
|
|
224
|
+
for (const [pid, title] of chain) {
|
|
225
|
+
const name = path.basename(title);
|
|
226
|
+
const helper = pid === 1 ? name : "";
|
|
227
|
+
const named = present.find(([, , , names]) => names.some((wanted) => wanted === name || wanted === title));
|
|
228
|
+
const helperOf = present.find(([, , , names]) => names.some((wanted) => helper.startsWith(`${wanted}-`)));
|
|
229
|
+
if (named) found.push([named[0], pid, false]);
|
|
230
|
+
else if (byPid.has(pid)) found.push([byPid.get(pid), pid, false]);
|
|
231
|
+
else if (helperOf) found.push([helperOf[0], pid, true]);
|
|
232
|
+
}
|
|
233
|
+
const above = new Set(found.map(([, pid]) => pid));
|
|
234
|
+
const stale = new Set([...ownPid]
|
|
235
|
+
.filter(([id, pid]) => complete && !above.has(pid) && !found.some(([agent]) => agent === id)
|
|
236
|
+
&& found.some(([agent]) => agent !== id))
|
|
237
|
+
.map(([id]) => id));
|
|
238
|
+
const keepOnly = (keep) => {
|
|
239
|
+
const out = { ...env };
|
|
240
|
+
for (const row of AGENT_MARKERS) if (!keep(row[0])) for (const key of getAgentKeys(row)) delete out[key];
|
|
241
|
+
return out;
|
|
242
|
+
};
|
|
243
|
+
const fallback = stale.size ? keepOnly((id) => !stale.has(id)) : env;
|
|
244
|
+
const live = present.filter(([id]) => !stale.has(id));
|
|
245
|
+
found = found.filter(([id]) => !stale.has(id));
|
|
246
|
+
if (live.length < 2 || !found.length) return fallback;
|
|
247
|
+
const [nearest, at, sandbox] = found[0];
|
|
248
|
+
const others = live.filter(([id]) => id !== nearest);
|
|
249
|
+
const launched = environOf(at);
|
|
250
|
+
if (launched) {
|
|
251
|
+
if (others.flatMap(getAgentKeys).some((key) => env[key] && env[key] !== launched[key])) return fallback;
|
|
252
|
+
} else if (!sandbox && !others.every(([id]) => found.slice(1).some(([agent]) => agent === id))) {
|
|
253
|
+
return fallback;
|
|
254
|
+
}
|
|
255
|
+
return keepOnly((id) => id === nearest);
|
|
256
|
+
}
|
|
257
|
+
|
|
91
258
|
/**
|
|
92
|
-
* The coding agent this runs in, or null:
|
|
93
|
-
*
|
|
259
|
+
* The coding agent this runs in, or null: the nearest of several (`getNearestAgentEnv`), then a
|
|
260
|
+
* marker that is only a marker first, then any (the CLI's `agent_session.session_agent_from_env`: a
|
|
261
|
+
* stale Codex id in a pi shell is no Codex).
|
|
94
262
|
*/
|
|
95
|
-
function detectAgent(env) {
|
|
263
|
+
function detectAgent(env, readAncestors = getAncestors, environOf = readEnviron, nested = isInChildPidNamespace) {
|
|
264
|
+
env = getNearestAgentEnv(env, readAncestors, environOf, nested);
|
|
96
265
|
for (const [id, markers, sessionEnv] of AGENT_MARKERS) {
|
|
97
266
|
if (markers.some((name) => name !== sessionEnv && env[name])) return id;
|
|
98
267
|
}
|
|
@@ -374,6 +543,8 @@ module.exports = {
|
|
|
374
543
|
buildRequestBody,
|
|
375
544
|
clearPending,
|
|
376
545
|
detectAgent,
|
|
546
|
+
getNearestAgentEnv,
|
|
547
|
+
readPsTable,
|
|
377
548
|
fetchApproval,
|
|
378
549
|
findHostedAgent,
|
|
379
550
|
getSayLine,
|
package/package.json
CHANGED