@botiverse/hands-cli 0.5.15 → 0.5.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/api.js +17 -15
- package/dist/commands/api.js.map +1 -1
- package/dist/commands/login.js +59 -12
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/whoami.js +2 -2
- package/dist/commands/whoami.js.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/agent_auth.d.ts +70 -0
- package/dist/lib/agent_auth.js +318 -0
- package/dist/lib/agent_auth.js.map +1 -0
- package/dist/lib/agent_env.d.ts +33 -0
- package/dist/lib/agent_env.js +112 -0
- package/dist/lib/agent_env.js.map +1 -0
- package/dist/lib/agent_refresh.d.ts +32 -0
- package/dist/lib/agent_refresh.js +323 -0
- package/dist/lib/agent_refresh.js.map +1 -0
- package/dist/lib/api.d.ts +8 -0
- package/dist/lib/api.js +62 -23
- package/dist/lib/api.js.map +1 -1
- package/dist/lib/config.d.ts +11 -4
- package/dist/lib/config.js +61 -13
- package/dist/lib/config.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent CLI login flow (RFC 057, CP3): the non-interactive `hands login` used inside
|
|
3
|
+
* a managed Raft agent. No browser, no paste.
|
|
4
|
+
*
|
|
5
|
+
* 1. Generate a PKCE code_verifier locally; send only its S256 challenge to Raft.
|
|
6
|
+
* 2. Run the EXACT wrapper `$SLOCK_CLI_TRANSPORT_DIR/raft integration invoke
|
|
7
|
+
* --action agent-login --json` (never PATH `raft`); require exit 0; STRICTLY
|
|
8
|
+
* validate the result (outer success + exact service/action/status + closed grant
|
|
9
|
+
* result schema + RFC3339/future expiry within a bounded client sanity window). The
|
|
10
|
+
* server issues a 300s TTL (RFC 057) and enforces the real expiry; the client accepts
|
|
11
|
+
* up to 300s + clock-skew headroom so a boundary grant is not rejected under skew.
|
|
12
|
+
* Errors carry only stable reasons
|
|
13
|
+
* — never the raw stdout/stderr/body (which could contain grant/action payload).
|
|
14
|
+
* 3. Exchange { grant, code_verifier } at the Hands PUBLIC endpoint for a
|
|
15
|
+
* raft-cli-agent-session.v1; strictly validate it (closed keys + RFC3339 expiries).
|
|
16
|
+
* 4. Atomically persist under $SLOCK_HOME (O_EXCL temp + fsync + rename; 0600 file /
|
|
17
|
+
* verified-0700 dirs), recording the api base so the resolver never needs config.
|
|
18
|
+
*
|
|
19
|
+
* The verifier never reaches Raft, logs, or the store. Only the Hands token is stored.
|
|
20
|
+
*/
|
|
21
|
+
import { spawnSync } from "node:child_process";
|
|
22
|
+
import { openSync, writeSync, fsyncSync, closeSync, renameSync, rmSync, mkdirSync, statSync, chmodSync, } from "node:fs";
|
|
23
|
+
import { join } from "node:path";
|
|
24
|
+
import { randomBytes, createHash } from "node:crypto";
|
|
25
|
+
import { getApiBase } from "./api.js";
|
|
26
|
+
import { agentAuthPath, HANDS_SERVICE } from "./agent_env.js";
|
|
27
|
+
function base64url(buf) {
|
|
28
|
+
return buf.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
29
|
+
}
|
|
30
|
+
/** PKCE S256: 64-byte verifier → 86 unreserved base64url chars (within RFC 7636's
|
|
31
|
+
* 43–128), challenge = unpadded base64url SHA-256. */
|
|
32
|
+
export function generatePkce() {
|
|
33
|
+
const verifier = base64url(randomBytes(64));
|
|
34
|
+
const challenge = base64url(createHash("sha256").update(verifier).digest());
|
|
35
|
+
return { verifier, challenge };
|
|
36
|
+
}
|
|
37
|
+
function hasExactKeys(o, keys) {
|
|
38
|
+
const k = Object.keys(o);
|
|
39
|
+
return k.length === keys.length && keys.every((key) => Object.prototype.hasOwnProperty.call(o, key));
|
|
40
|
+
}
|
|
41
|
+
/** Parse an RFC3339 date-time to epoch ms, or null. Rejects bare dates / bad offsets. */
|
|
42
|
+
function parseRfc3339(s) {
|
|
43
|
+
if (typeof s !== "string")
|
|
44
|
+
return null;
|
|
45
|
+
if (!/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/.test(s))
|
|
46
|
+
return null;
|
|
47
|
+
const t = Date.parse(s);
|
|
48
|
+
return Number.isFinite(t) ? t : null;
|
|
49
|
+
}
|
|
50
|
+
// Client-side sanity ceiling on the grant lifetime: the server issues expiry =
|
|
51
|
+
// server_now + 300s (RFC 057). We allow generous clock-skew headroom above that so a
|
|
52
|
+
// legitimate grant is never rejected at the exact boundary; the server enforces the
|
|
53
|
+
// real, shorter expiry, so this bound only rejects an absurdly long-lived grant.
|
|
54
|
+
const AGENT_GRANT_TTL_CEILING_MS = 300_000 + 120_000; // 300s + 120s skew
|
|
55
|
+
/**
|
|
56
|
+
* Strictly validate a `raft integration invoke --action agent-login --json` result.
|
|
57
|
+
* Real envelope (verified against the live daemon):
|
|
58
|
+
* { ok:true, data:{ service, action, status, result:{schema,service,grant,expires_at} } }
|
|
59
|
+
* The Hands-owned `result` is closed-key validated; the Raft envelope (outer/data) is
|
|
60
|
+
* validated by exact required values. NO part of stdout is ever echoed in an error.
|
|
61
|
+
*/
|
|
62
|
+
export function parseAgentLoginInvoke(stdout, service, now) {
|
|
63
|
+
let outer;
|
|
64
|
+
try {
|
|
65
|
+
outer = JSON.parse(stdout);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new Error("agent-login: `raft integration invoke` did not return JSON");
|
|
69
|
+
}
|
|
70
|
+
if (!outer || typeof outer !== "object")
|
|
71
|
+
throw new Error("agent-login: invoke output is not an object");
|
|
72
|
+
// Check success first: a failed invoke may legitimately carry a different shape
|
|
73
|
+
// (e.g. {ok:false, error}); only the success wrapper is the closed {ok, data}.
|
|
74
|
+
if (outer.ok !== true)
|
|
75
|
+
throw new Error("agent-login: invoke did not succeed");
|
|
76
|
+
// Closed envelope: an unexpected extra field means the wire drifted from the
|
|
77
|
+
// checkpoint contract; fail rather than ignore.
|
|
78
|
+
if (!hasExactKeys(outer, ["ok", "data"]))
|
|
79
|
+
throw new Error("agent-login: invoke envelope has unexpected fields");
|
|
80
|
+
const data = outer.data;
|
|
81
|
+
if (!data || typeof data !== "object")
|
|
82
|
+
throw new Error("agent-login: invoke result is missing data");
|
|
83
|
+
if (!hasExactKeys(data, ["service", "action", "status", "result"])) {
|
|
84
|
+
throw new Error("agent-login: invoke data has unexpected fields");
|
|
85
|
+
}
|
|
86
|
+
if (data.service !== service)
|
|
87
|
+
throw new Error("agent-login: invoke service does not match the requested service");
|
|
88
|
+
if (data.action !== "agent-login")
|
|
89
|
+
throw new Error("agent-login: invoke action is not agent-login");
|
|
90
|
+
if (data.status !== 200)
|
|
91
|
+
throw new Error("agent-login: agent-login action did not return HTTP 200");
|
|
92
|
+
const result = data.result;
|
|
93
|
+
if (!result || typeof result !== "object")
|
|
94
|
+
throw new Error("agent-login: invoke result is missing the grant body");
|
|
95
|
+
if (!hasExactKeys(result, ["schema", "service", "grant", "expires_at"])) {
|
|
96
|
+
throw new Error("agent-login: grant result has unexpected fields");
|
|
97
|
+
}
|
|
98
|
+
if (result.schema !== "raft-cli-agent-login-grant.v1")
|
|
99
|
+
throw new Error("agent-login: unexpected grant result schema");
|
|
100
|
+
if (result.service !== service)
|
|
101
|
+
throw new Error("agent-login: grant result service mismatch");
|
|
102
|
+
if (typeof result.grant !== "string" || result.grant.length === 0)
|
|
103
|
+
throw new Error("agent-login: grant is missing");
|
|
104
|
+
const exp = parseRfc3339(result.expires_at);
|
|
105
|
+
if (exp === null)
|
|
106
|
+
throw new Error("agent-login: grant expires_at is not an RFC3339 timestamp");
|
|
107
|
+
if (exp <= now)
|
|
108
|
+
throw new Error("agent-login: grant is already expired");
|
|
109
|
+
// The server issues expiry = server_now + 300s (RFC 057). The client parses it later and
|
|
110
|
+
// its clock may differ, so a zero-tolerance ceiling flakes at the exact boundary. This is
|
|
111
|
+
// a sanity bound (the server enforces the real expiry), so allow clock-skew headroom.
|
|
112
|
+
if (exp > now + AGENT_GRANT_TTL_CEILING_MS)
|
|
113
|
+
throw new Error("agent-login: grant expiry exceeds the ceiling");
|
|
114
|
+
return { grant: result.grant, expires_at: result.expires_at };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Strictly validate the exchange/refresh success body (closed keys + RFC3339).
|
|
118
|
+
* `now` is injected so validation is deterministic: a freshly issued session MUST
|
|
119
|
+
* have a strictly-future access expiry (and refresh expiry, when present) — otherwise
|
|
120
|
+
* we would persist an already-dead session that the very next request must refresh.
|
|
121
|
+
*/
|
|
122
|
+
export function parseAgentSession(body, now) {
|
|
123
|
+
if (!body || typeof body !== "object")
|
|
124
|
+
throw new Error("agent-login: exchange response is not an object");
|
|
125
|
+
if (!hasExactKeys(body, ["schema", "token_type", "access_token", "access_expires_at", "refresh_token", "refresh_expires_at"])) {
|
|
126
|
+
throw new Error("agent-login: session has unexpected fields");
|
|
127
|
+
}
|
|
128
|
+
if (body.schema !== "raft-cli-agent-session.v1")
|
|
129
|
+
throw new Error("agent-login: unexpected session schema");
|
|
130
|
+
if (body.token_type !== "Bearer")
|
|
131
|
+
throw new Error("agent-login: unexpected token_type");
|
|
132
|
+
for (const k of ["access_token", "refresh_token"]) {
|
|
133
|
+
if (typeof body[k] !== "string" || body[k].length === 0)
|
|
134
|
+
throw new Error(`agent-login: session missing ${k}`);
|
|
135
|
+
}
|
|
136
|
+
const accessExp = parseRfc3339(body.access_expires_at);
|
|
137
|
+
if (accessExp === null)
|
|
138
|
+
throw new Error("agent-login: session access_expires_at is not RFC3339");
|
|
139
|
+
if (accessExp <= now)
|
|
140
|
+
throw new Error("agent-login: session access token is already expired");
|
|
141
|
+
if (body.refresh_expires_at !== null) {
|
|
142
|
+
const refreshExp = parseRfc3339(body.refresh_expires_at);
|
|
143
|
+
if (refreshExp === null)
|
|
144
|
+
throw new Error("agent-login: session refresh_expires_at must be RFC3339 or null");
|
|
145
|
+
if (refreshExp <= now)
|
|
146
|
+
throw new Error("agent-login: session refresh token is already expired");
|
|
147
|
+
}
|
|
148
|
+
return body;
|
|
149
|
+
}
|
|
150
|
+
/** mkdir (if needed) + verify/repair 0700 (no group/other) on one path component. */
|
|
151
|
+
function ensureSecureDir(dir) {
|
|
152
|
+
try {
|
|
153
|
+
mkdirSync(dir, { mode: 0o700 });
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
if (e?.code !== "EEXIST")
|
|
157
|
+
throw e;
|
|
158
|
+
}
|
|
159
|
+
const st = statSync(dir);
|
|
160
|
+
if (!st.isDirectory())
|
|
161
|
+
throw new Error("agent-login: store path component is not a directory");
|
|
162
|
+
if ((st.mode & 0o077) !== 0)
|
|
163
|
+
chmodSync(dir, 0o700); // repair a pre-existing wide dir
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Atomic, hardened store write: O_EXCL unique temp in the same dir, 0600, write +
|
|
167
|
+
* fsync + close, atomic rename. Each dir component under $SLOCK_HOME is ensured AND
|
|
168
|
+
* verified 0700 (repairing a pre-existing wide dir). $SLOCK_HOME itself is not chmod'd.
|
|
169
|
+
* On any failure the temp is removed and the previous file is left intact.
|
|
170
|
+
*/
|
|
171
|
+
export function writeAgentSession(a, service, session, apiBase, now = () => new Date().toISOString()) {
|
|
172
|
+
const path = agentAuthPath(a, service); // validates slug + agent id + containment
|
|
173
|
+
let dir = a.slockHome;
|
|
174
|
+
for (const seg of ["agents", a.agentId, "integrations", service]) {
|
|
175
|
+
dir = join(dir, seg);
|
|
176
|
+
ensureSecureDir(dir);
|
|
177
|
+
}
|
|
178
|
+
const record = { ...session, service, api_base: apiBase, updated_at: now() };
|
|
179
|
+
const payload = JSON.stringify(record, null, 2) + "\n";
|
|
180
|
+
const tmp = join(dir, `.auth.${randomBytes(8).toString("hex")}.tmp`);
|
|
181
|
+
let fd = null;
|
|
182
|
+
try {
|
|
183
|
+
fd = openSync(tmp, "wx", 0o600); // 'wx' = O_CREAT|O_EXCL|O_WRONLY
|
|
184
|
+
writeSync(fd, payload);
|
|
185
|
+
fsyncSync(fd);
|
|
186
|
+
closeSync(fd);
|
|
187
|
+
fd = null;
|
|
188
|
+
renameSync(tmp, path);
|
|
189
|
+
}
|
|
190
|
+
catch (e) {
|
|
191
|
+
if (fd !== null) {
|
|
192
|
+
try {
|
|
193
|
+
closeSync(fd);
|
|
194
|
+
}
|
|
195
|
+
catch { /* ignore */ }
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
rmSync(tmp, { force: true });
|
|
199
|
+
}
|
|
200
|
+
catch { /* ignore */ }
|
|
201
|
+
throw e; // previous auth.json (if any) is left intact
|
|
202
|
+
}
|
|
203
|
+
return path;
|
|
204
|
+
}
|
|
205
|
+
// Token responses are small JSON; cap the read so a hostile/broken endpoint cannot
|
|
206
|
+
// stream an unbounded body. Shared by the exchange and refresh token requests.
|
|
207
|
+
export const MAX_TOKEN_RESPONSE_BYTES = 64 * 1024;
|
|
208
|
+
/**
|
|
209
|
+
* Read a response body with a hard byte cap. If an AbortController is supplied it is
|
|
210
|
+
* aborted when the cap is exceeded (so a still-open connection is torn down, and — in
|
|
211
|
+
* `rotate` — the same controller's deadline keeps covering this read). Falls back to
|
|
212
|
+
* `.text()` for response doubles that expose no stream (still cap-checked).
|
|
213
|
+
*/
|
|
214
|
+
export async function readBoundedText(res, controller) {
|
|
215
|
+
const declared = Number(res.headers?.get?.("content-length"));
|
|
216
|
+
if (Number.isFinite(declared) && declared > MAX_TOKEN_RESPONSE_BYTES) {
|
|
217
|
+
controller?.abort();
|
|
218
|
+
throw new Error("agent-login: token response exceeds the size limit");
|
|
219
|
+
}
|
|
220
|
+
const reader = res.body?.getReader?.();
|
|
221
|
+
if (!reader) {
|
|
222
|
+
const t = await res.text();
|
|
223
|
+
if (t.length > MAX_TOKEN_RESPONSE_BYTES)
|
|
224
|
+
throw new Error("agent-login: token response exceeds the size limit");
|
|
225
|
+
return t;
|
|
226
|
+
}
|
|
227
|
+
const chunks = [];
|
|
228
|
+
let total = 0;
|
|
229
|
+
for (;;) {
|
|
230
|
+
const { done, value } = await reader.read();
|
|
231
|
+
if (done)
|
|
232
|
+
break;
|
|
233
|
+
if (value) {
|
|
234
|
+
total += value.byteLength;
|
|
235
|
+
if (total > MAX_TOKEN_RESPONSE_BYTES) {
|
|
236
|
+
controller?.abort();
|
|
237
|
+
try {
|
|
238
|
+
await reader.cancel();
|
|
239
|
+
}
|
|
240
|
+
catch { /* ignore */ }
|
|
241
|
+
throw new Error("agent-login: token response exceeds the size limit");
|
|
242
|
+
}
|
|
243
|
+
chunks.push(value);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const out = new Uint8Array(total);
|
|
247
|
+
let off = 0;
|
|
248
|
+
for (const c of chunks) {
|
|
249
|
+
out.set(c, off);
|
|
250
|
+
off += c.byteLength;
|
|
251
|
+
}
|
|
252
|
+
return new TextDecoder().decode(out);
|
|
253
|
+
}
|
|
254
|
+
function defaultInvoke(raftBin, args) {
|
|
255
|
+
const res = spawnSync(raftBin, args, { encoding: "utf8" });
|
|
256
|
+
return {
|
|
257
|
+
status: res.error ? null : res.status,
|
|
258
|
+
stdout: res.stdout ?? "",
|
|
259
|
+
stderr: res.stderr ?? "",
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Full agent-login flow: invoke the pinned wrapper → strict-validate grant → exchange
|
|
264
|
+
* → strict-validate session → atomic store. Returns the stored session on success.
|
|
265
|
+
*/
|
|
266
|
+
export async function runAgentLogin(a, opts = {}) {
|
|
267
|
+
const service = opts.service ?? HANDS_SERVICE;
|
|
268
|
+
const now = opts.now ?? Date.now();
|
|
269
|
+
const { verifier, challenge } = generatePkce();
|
|
270
|
+
const args = [
|
|
271
|
+
"integration", "invoke",
|
|
272
|
+
"--service", service,
|
|
273
|
+
"--action", "agent-login",
|
|
274
|
+
"--json",
|
|
275
|
+
"--data-json", JSON.stringify({
|
|
276
|
+
schema: "raft-cli-agent-login-request.v1",
|
|
277
|
+
code_challenge: challenge,
|
|
278
|
+
code_challenge_method: "S256",
|
|
279
|
+
}),
|
|
280
|
+
];
|
|
281
|
+
const runner = opts.invoke ?? ((a2) => defaultInvoke(a.raftBin, a2));
|
|
282
|
+
const res = runner(args);
|
|
283
|
+
if (res.status !== 0) {
|
|
284
|
+
// Require a clean exit; never echo stdout/stderr (may carry grant/action payload).
|
|
285
|
+
throw new Error(`agent-login: raft invoke exited with a non-zero status (${res.status ?? "spawn error"})`);
|
|
286
|
+
}
|
|
287
|
+
const { grant } = parseAgentLoginInvoke(res.stdout, service, now);
|
|
288
|
+
// Independent token request: NO stored Bearer, NO auto-refresh. `hands login` is the
|
|
289
|
+
// recovery path when the stored refresh has reached a terminal state
|
|
290
|
+
// (expired/reused/revoked); routing this exchange through the api client would first
|
|
291
|
+
// try to refresh that dead token and throw before the fresh grant is ever spent.
|
|
292
|
+
const apiBase = getApiBase();
|
|
293
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
294
|
+
const exchangeRes = await fetchImpl(new URL("/api/auth/agent/exchange", apiBase).toString(), {
|
|
295
|
+
method: "POST",
|
|
296
|
+
headers: { "content-type": "application/json", accept: "application/json" },
|
|
297
|
+
body: JSON.stringify({ schema: "raft-cli-agent-login-exchange.v1", grant, code_verifier: verifier }),
|
|
298
|
+
// Never follow a redirect: a 307/308 would forward the grant + code_verifier body
|
|
299
|
+
// to the redirect target. `manual` leaves a 3xx as a non-ok status we reject below.
|
|
300
|
+
redirect: "manual",
|
|
301
|
+
});
|
|
302
|
+
if (!exchangeRes.ok) {
|
|
303
|
+
// Stable reason only; never echo the response body (may carry token material).
|
|
304
|
+
throw new Error(`agent-login: grant exchange failed (HTTP ${exchangeRes.status})`);
|
|
305
|
+
}
|
|
306
|
+
const exchangeText = await readBoundedText(exchangeRes);
|
|
307
|
+
let exchangeBody;
|
|
308
|
+
try {
|
|
309
|
+
exchangeBody = JSON.parse(exchangeText);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
throw new Error("agent-login: exchange response was not JSON");
|
|
313
|
+
}
|
|
314
|
+
const session = parseAgentSession(exchangeBody, now);
|
|
315
|
+
writeAgentSession(a, service, session, apiBase);
|
|
316
|
+
return session;
|
|
317
|
+
}
|
|
318
|
+
//# sourceMappingURL=agent_auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent_auth.js","sourceRoot":"","sources":["../../src/lib/agent_auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EACL,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAC7D,SAAS,EAAE,QAAQ,EAAE,SAAS,GAC/B,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAiB,MAAM,gBAAgB,CAAC;AAE7E,SAAS,SAAS,CAAC,GAAW;IAC5B,OAAO,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AAC3F,CAAC;AAED;uDACuD;AACvD,MAAM,UAAU,YAAY;IAC1B,MAAM,QAAQ,GAAG,SAAS,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IAC5C,MAAM,SAAS,GAAG,SAAS,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5E,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,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
|
|
1
|
+
export declare const HANDS_SERVICE = "hands-4cc7a2";
|
|
2
|
+
export interface AgentEnv {
|
|
3
|
+
transportDir: string;
|
|
4
|
+
slockHome: string;
|
|
5
|
+
agentId: string;
|
|
6
|
+
raftBin: string;
|
|
7
|
+
}
|
|
8
|
+
export type Admission = {
|
|
9
|
+
kind: "human";
|
|
10
|
+
} | {
|
|
11
|
+
kind: "agent";
|
|
12
|
+
env: AgentEnv;
|
|
13
|
+
} | {
|
|
14
|
+
kind: "fail_closed";
|
|
15
|
+
reason: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Decide human vs agent vs fail-closed. Once ANY agent marker is present we never
|
|
19
|
+
* return `human` — a partial/invalid environment fails closed rather than silently
|
|
20
|
+
* using ambient human credentials.
|
|
21
|
+
*/
|
|
22
|
+
export declare function admitAgent(env?: NodeJS.ProcessEnv): Admission;
|
|
23
|
+
/** Canonical per-agent store path, with slug validation + root containment. */
|
|
24
|
+
export declare function agentAuthPath(a: AgentEnv, service?: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Read the stored Hands access token for this agent, or null if absent/unreadable.
|
|
27
|
+
* Dependency-free (fs + path only) so `config.ts` can call it without an import cycle
|
|
28
|
+
* through the api client. Auto-refresh-on-expiry lands in CP3 checkpoint-2.
|
|
29
|
+
*/
|
|
30
|
+
export declare function readAgentAccessToken(a: AgentEnv, service?: string): string | null;
|
|
31
|
+
/** Read the api base recorded in the agent store (so the resolver never reads the
|
|
32
|
+
* human config in agent mode), or null. */
|
|
33
|
+
export declare function readAgentApiBase(a: AgentEnv, service?: string): string | null;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent runtime admission + credential-store path (RFC 057 agent login, CP3).
|
|
3
|
+
*
|
|
4
|
+
* Admission is TRI-STATE (Volta): no markers → normal human/CI; any agent marker but
|
|
5
|
+
* incomplete/invalid/no-executable-wrapper → FAIL CLOSED (never fall back to human
|
|
6
|
+
* credentials); complete + valid → agent mode, pinned to the exact `raft` wrapper
|
|
7
|
+
* inside $SLOCK_CLI_TRANSPORT_DIR (never the PATH `raft`).
|
|
8
|
+
*/
|
|
9
|
+
import { accessSync, constants, existsSync, readFileSync, statSync } from "node:fs";
|
|
10
|
+
import { join, resolve, sep } from "node:path";
|
|
11
|
+
// Compiled-fixed exact installed Raft client key. NOT environment-overridable: it is
|
|
12
|
+
// both the invoke target AND part of the on-disk store path, so an override would be a
|
|
13
|
+
// cross-service / path-injection vector. Tests inject a service via function params.
|
|
14
|
+
export const HANDS_SERVICE = "hands-4cc7a2";
|
|
15
|
+
// RFC 057 service slug.
|
|
16
|
+
const SERVICE_SLUG_RE = /^[a-z0-9][a-z0-9._-]{0,79}$/;
|
|
17
|
+
// Daemon-issued agent id: a conservative safe token (no separators/traversal).
|
|
18
|
+
const AGENT_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
19
|
+
function isExecutableFile(p) {
|
|
20
|
+
try {
|
|
21
|
+
if (!statSync(p).isFile())
|
|
22
|
+
return false;
|
|
23
|
+
accessSync(p, constants.X_OK);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Decide human vs agent vs fail-closed. Once ANY agent marker is present we never
|
|
32
|
+
* return `human` — a partial/invalid environment fails closed rather than silently
|
|
33
|
+
* using ambient human credentials.
|
|
34
|
+
*/
|
|
35
|
+
export function admitAgent(env = process.env) {
|
|
36
|
+
const transportDir = env.SLOCK_CLI_TRANSPORT_DIR;
|
|
37
|
+
const slockHome = env.SLOCK_HOME;
|
|
38
|
+
const agentId = env.SLOCK_AGENT_ID;
|
|
39
|
+
if (!transportDir && !slockHome && !agentId)
|
|
40
|
+
return { kind: "human" };
|
|
41
|
+
if (!transportDir || !slockHome || !agentId) {
|
|
42
|
+
return { kind: "fail_closed", reason: "incomplete agent markers" };
|
|
43
|
+
}
|
|
44
|
+
if (!AGENT_ID_RE.test(agentId)) {
|
|
45
|
+
return { kind: "fail_closed", reason: "invalid SLOCK_AGENT_ID" };
|
|
46
|
+
}
|
|
47
|
+
const raftBin = join(transportDir, "raft");
|
|
48
|
+
if (!isExecutableFile(raftBin)) {
|
|
49
|
+
return { kind: "fail_closed", reason: "raft wrapper missing or not executable in transport dir" };
|
|
50
|
+
}
|
|
51
|
+
return { kind: "agent", env: { transportDir, slockHome, agentId, raftBin } };
|
|
52
|
+
}
|
|
53
|
+
/** Canonical per-agent store path, with slug validation + root containment. */
|
|
54
|
+
export function agentAuthPath(a, service = HANDS_SERVICE) {
|
|
55
|
+
if (!SERVICE_SLUG_RE.test(service))
|
|
56
|
+
throw new Error("invalid service slug");
|
|
57
|
+
if (!AGENT_ID_RE.test(a.agentId))
|
|
58
|
+
throw new Error("invalid agent id");
|
|
59
|
+
const root = resolve(a.slockHome, "agents", a.agentId, "integrations");
|
|
60
|
+
const path = resolve(root, service, "auth.json");
|
|
61
|
+
// Belt-and-suspenders containment (agentId/service are already regex-validated).
|
|
62
|
+
if (path !== join(root, service, "auth.json") || !path.startsWith(root + sep)) {
|
|
63
|
+
throw new Error("resolved store path escapes the integrations root");
|
|
64
|
+
}
|
|
65
|
+
return path;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Read the stored Hands access token for this agent, or null if absent/unreadable.
|
|
69
|
+
* Dependency-free (fs + path only) so `config.ts` can call it without an import cycle
|
|
70
|
+
* through the api client. Auto-refresh-on-expiry lands in CP3 checkpoint-2.
|
|
71
|
+
*/
|
|
72
|
+
export function readAgentAccessToken(a, service = HANDS_SERVICE) {
|
|
73
|
+
let path;
|
|
74
|
+
try {
|
|
75
|
+
path = agentAuthPath(a, service);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (!existsSync(path))
|
|
81
|
+
return null;
|
|
82
|
+
try {
|
|
83
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
84
|
+
return typeof parsed?.access_token === "string" && parsed.access_token.length > 0
|
|
85
|
+
? parsed.access_token
|
|
86
|
+
: null;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Read the api base recorded in the agent store (so the resolver never reads the
|
|
93
|
+
* human config in agent mode), or null. */
|
|
94
|
+
export function readAgentApiBase(a, service = HANDS_SERVICE) {
|
|
95
|
+
let path;
|
|
96
|
+
try {
|
|
97
|
+
path = agentAuthPath(a, service);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (!existsSync(path))
|
|
103
|
+
return null;
|
|
104
|
+
try {
|
|
105
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
106
|
+
return typeof parsed?.api_base === "string" && parsed.api_base.length > 0 ? parsed.api_base : null;
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=agent_env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent_env.js","sourceRoot":"","sources":["../../src/lib/agent_env.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACpF,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAE/C,qFAAqF;AACrF,uFAAuF;AACvF,qFAAqF;AACrF,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC;AAE5C,wBAAwB;AACxB,MAAM,eAAe,GAAG,6BAA6B,CAAC;AACtD,+EAA+E;AAC/E,MAAM,WAAW,GAAG,oCAAoC,CAAC;AAczD,SAAS,gBAAgB,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AI,MAAM,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC;YAC/E,CAAC,CAAC,MAAM,CAAC,YAAY;YACrB,CAAC,CAAC,IAAI,CAAC;IACX,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED;4CAC4C;AAC5C,MAAM,UAAU,gBAAgB,CAAC,CAAW,EAAE,UAAkB,aAAa;IAC3E,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACnC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACnC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAA2B,CAAC;QAChF,OAAO,OAAO,MAAM,EAAE,QAAQ,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC;IACrG,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type AgentEnv } from "./agent_env.js";
|
|
2
|
+
export declare const REFRESH_SKEW_MS = 60000;
|
|
3
|
+
export interface RefreshOptions {
|
|
4
|
+
service?: string;
|
|
5
|
+
now?: number;
|
|
6
|
+
fetchImpl?: typeof fetch;
|
|
7
|
+
/** Injectable sleep for tests (defaults to real setTimeout). */
|
|
8
|
+
sleepImpl?: (ms: number) => Promise<void>;
|
|
9
|
+
/** Injectable refresh deadline for tests (defaults to REFRESH_DEADLINE_MS). */
|
|
10
|
+
deadlineMs?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Return a valid access token, refreshing (single-flight) if it is within the skew
|
|
14
|
+
* window. Returns null if there is no stored session (caller must `hands login`).
|
|
15
|
+
*/
|
|
16
|
+
export declare function getFreshAgentAccessToken(a: AgentEnv, opts?: RefreshOptions): Promise<string | null>;
|
|
17
|
+
/**
|
|
18
|
+
* Force one refresh (used on a 401 even if the token looked unexpired), single-flight.
|
|
19
|
+
* Returns null if there is no stored session.
|
|
20
|
+
*/
|
|
21
|
+
export declare function forceRefreshAgentToken(a: AgentEnv, opts?: RefreshOptions): Promise<string | null>;
|
|
22
|
+
/** Delete the lock only if it still carries OUR owner id (never a foreign holder's). */
|
|
23
|
+
export declare function releaseOwnLock(lock: string, ownerId: string): void;
|
|
24
|
+
/**
|
|
25
|
+
* Recover a lock whose owner process is provably dead and acquire it, serialized by an
|
|
26
|
+
* exclusive reaper fence. Judge-dead → unlink → re-acquire all happen WHILE the fence is
|
|
27
|
+
* held, so at most one recoverer reaps-and-rebuilds — two concurrent recoverers can never
|
|
28
|
+
* both re-acquire (→ never double-rotate). A LIVE owner (incl. suspended/stalled — pid
|
|
29
|
+
* still exists) is never touched. Any uncertainty fails closed. Returns true iff WE now
|
|
30
|
+
* hold the main lock carrying `ownerId`; false → the caller is a loser.
|
|
31
|
+
*/
|
|
32
|
+
export declare function acquireIfDeadOwner(lock: string, ownerId: string): boolean;
|