@aidenhq/sdk 0.5.13 → 0.5.15
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/src/card-words.js +1 -0
- package/dist/src/reason-codes.d.ts +2 -2
- package/dist/src/reason-codes.js +2 -0
- package/dist/src/state-secrets.d.ts +15 -0
- package/dist/src/state-secrets.js +31 -0
- package/dist/src/vendor/oauth.d.ts +2 -1
- package/dist/src/vendor/oauth.js +29 -12
- package/package.json +3 -2
package/dist/src/card-words.js
CHANGED
|
@@ -16,6 +16,7 @@ export const REVIEW_WORDS = {
|
|
|
16
16
|
malicious: "The reviewer found it malicious.",
|
|
17
17
|
secret_exfiltration: "It would send a secret or private data out.",
|
|
18
18
|
prompt_injection: "It follows instructions from untrusted text.",
|
|
19
|
+
private_disclosure: "A reply would tell someone other than you what only you may see: you decide whether it goes.",
|
|
19
20
|
reviewer_failed: "The reviewer failed to answer.",
|
|
20
21
|
fail_open: "The review didn't finish twice, so it was approved (fail open).",
|
|
21
22
|
files_not_shown: "The diff didn't match the code host's own count of the change.",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** The AI reviewer's code on a card: what it judged (model-chosen, malice only) or what code decided in its place. */
|
|
2
|
-
export declare const REVIEW_CODES: readonly ["clean", "malicious", "secret_exfiltration", "prompt_injection", "reviewer_failed", "fail_open", "files_not_shown", "instance_details", "tool_call_unreadable", "owner_veto", "shadow", "refused_by_code", "stale_head"];
|
|
2
|
+
export declare const REVIEW_CODES: readonly ["clean", "malicious", "secret_exfiltration", "prompt_injection", "private_disclosure", "reviewer_failed", "fail_open", "files_not_shown", "instance_details", "tool_call_unreadable", "owner_veto", "shadow", "refused_by_code", "stale_head"];
|
|
3
3
|
export type ReviewCode = (typeof REVIEW_CODES)[number];
|
|
4
4
|
/** A grant refusal (kernel/src/identity.ts): the tool isn't granted, the agent lacks a role, or it has no grants at all. */
|
|
5
5
|
export declare const GRANT_CODES: readonly ["denied_not_granted", "denied_role", "denied_no_grants", "denied_unknown_caller"];
|
|
@@ -7,7 +7,7 @@ export declare const GRANT_CODES: readonly ["denied_not_granted", "denied_role",
|
|
|
7
7
|
export declare const GATEWAY_CODES: readonly ["cap_reached", "model_not_granted", "advisor_limit"];
|
|
8
8
|
/** A request already satisfied on its target: no card is filed (the duplicate-card guard). */
|
|
9
9
|
export declare const ALREADY_APPLIED_CODE = "already_applied";
|
|
10
|
-
export declare const REASON_CODES: readonly ["clean", "malicious", "secret_exfiltration", "prompt_injection", "reviewer_failed", "fail_open", "files_not_shown", "instance_details", "tool_call_unreadable", "owner_veto", "shadow", "refused_by_code", "stale_head", "denied_not_granted", "denied_role", "denied_no_grants", "denied_unknown_caller", "cap_reached", "model_not_granted", "advisor_limit", "already_applied"];
|
|
10
|
+
export declare const REASON_CODES: readonly ["clean", "malicious", "secret_exfiltration", "prompt_injection", "private_disclosure", "reviewer_failed", "fail_open", "files_not_shown", "instance_details", "tool_call_unreadable", "owner_veto", "shadow", "refused_by_code", "stale_head", "denied_not_granted", "denied_role", "denied_no_grants", "denied_unknown_caller", "cap_reached", "model_not_granted", "advisor_limit", "already_applied"];
|
|
11
11
|
export type ReasonCode = (typeof REASON_CODES)[number];
|
|
12
12
|
/** The shape of a tier rule (tiers.mjs `rule`; also the board's deploy_app, app_run and raise_cap rules). */
|
|
13
13
|
export declare const TIER_RULE: RegExp;
|
package/dist/src/reason-codes.js
CHANGED
|
@@ -14,6 +14,8 @@ export const REVIEW_CODES = [
|
|
|
14
14
|
"malicious",
|
|
15
15
|
"secret_exfiltration",
|
|
16
16
|
"prompt_injection",
|
|
17
|
+
// the model's escalate, only for a reply to someone other than the owner that would tell them what only the owner may see: the owner decides
|
|
18
|
+
"private_disclosure",
|
|
17
19
|
// the review didn't finish (a call failed, timed out, refused or hit a limit): tried once more, then approved
|
|
18
20
|
"reviewer_failed",
|
|
19
21
|
"fail_open",
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A state file's name: what the kernel accepts, one path segment. */
|
|
2
|
+
export declare const STATE_NAME: RegExp;
|
|
3
|
+
/** The most one state file holds (the kernel's cap). */
|
|
4
|
+
export declare const STATE_MAX: number;
|
|
5
|
+
export interface StateOptions {
|
|
6
|
+
/** The kernel's backplane (default AIDENHQ_KERNEL_URL, else the host bridge address every module reaches). */
|
|
7
|
+
url?: string;
|
|
8
|
+
/** The module's service key (default: the file named by MCP_SERVICE_TOKEN_FILE, else MCP_TOKEN_FILE). */
|
|
9
|
+
key?: string | (() => string);
|
|
10
|
+
fetchImpl?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
/** Whether this process can reach the kernel to save state (a key file is set); a dev or test run without one writes its own folder. */
|
|
13
|
+
export declare const canSaveState: (o?: StateOptions) => boolean;
|
|
14
|
+
/** Saves `text` as the module's state file `name`; resolves with the committed version once the kernel has persisted it, rejects otherwise. */
|
|
15
|
+
export declare function saveState(name: string, text: string, o?: StateOptions): Promise<string>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// A connector's runtime state (its sign-in's token pair, a registered client, the accounts it keeps): saved through the kernel,
|
|
2
|
+
// which writes the module's own secret store and answers only once the write is persisted (POST /v1/state/<name>,
|
|
3
|
+
// kernel/src/state-secrets.ts). The caller is known by its connection's service key, so it can write nothing but its own
|
|
4
|
+
// module's state; the target is never part of the request. Node built-ins only.
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
/** A state file's name: what the kernel accepts, one path segment. */
|
|
7
|
+
export const STATE_NAME = /^[\w.-]{1,100}$/;
|
|
8
|
+
/** The most one state file holds (the kernel's cap). */
|
|
9
|
+
export const STATE_MAX = 64 * 1024;
|
|
10
|
+
const keyFromEnv = () => {
|
|
11
|
+
const file = process.env.MCP_SERVICE_TOKEN_FILE ?? process.env.MCP_TOKEN_FILE;
|
|
12
|
+
return file ? readFileSync(file, "utf8").trim() : "";
|
|
13
|
+
};
|
|
14
|
+
/** Whether this process can reach the kernel to save state (a key file is set); a dev or test run without one writes its own folder. */
|
|
15
|
+
export const canSaveState = (o = {}) => Boolean(o.key ?? process.env.MCP_SERVICE_TOKEN_FILE ?? process.env.MCP_TOKEN_FILE);
|
|
16
|
+
/** Saves `text` as the module's state file `name`; resolves with the committed version once the kernel has persisted it, rejects otherwise. */
|
|
17
|
+
export async function saveState(name, text, o = {}) {
|
|
18
|
+
if (!STATE_NAME.test(name))
|
|
19
|
+
throw new Error(`state file name ${JSON.stringify(name)} is not one path segment (letters, digits, . _ -)`);
|
|
20
|
+
if (Buffer.byteLength(text) > STATE_MAX)
|
|
21
|
+
throw new Error(`state file ${name} is over ${STATE_MAX} bytes`);
|
|
22
|
+
const key = typeof o.key === "function" ? o.key() : (o.key ?? keyFromEnv());
|
|
23
|
+
if (!key)
|
|
24
|
+
throw new Error("no service key to save state with (MCP_SERVICE_TOKEN_FILE)");
|
|
25
|
+
const url = (o.url ?? process.env.AIDENHQ_KERNEL_URL ?? "http://10.231.0.1:3130").replace(/\/$/, "");
|
|
26
|
+
const r = await (o.fetchImpl ?? fetch)(`${url}/v1/state/${name}`, { method: "POST", headers: { authorization: `Bearer ${key}`, "content-type": "text/plain; charset=utf-8" }, body: text, signal: AbortSignal.timeout(30_000) });
|
|
27
|
+
const body = (await r.json().catch(() => ({})));
|
|
28
|
+
if (!r.ok || typeof body.version !== "string")
|
|
29
|
+
throw new Error(`the kernel did not save ${name} (${r.status}${body.error?.message ? `: ${String(body.error.message).slice(0, 200)}` : ""})`);
|
|
30
|
+
return body.version;
|
|
31
|
+
}
|
|
@@ -108,7 +108,8 @@ export interface OAuthOptions {
|
|
|
108
108
|
/** Where a sign-in's token is kept. `read` returns the same object until `write` replaces it, so a refresh can tell a new sign-in saved meanwhile; a throwing `write` saves nothing. */
|
|
109
109
|
export interface TokenStore {
|
|
110
110
|
read(): StoredToken | undefined;
|
|
111
|
-
|
|
111
|
+
/** Resolves once the token is persisted: nothing is kept in memory or used before that. */
|
|
112
|
+
write(t: StoredToken): void | Promise<void>;
|
|
112
113
|
}
|
|
113
114
|
/** token.json (0600), written atomically; a file removed from outside reads as signed out. */
|
|
114
115
|
export declare function fileStore(path: string): TokenStore;
|
package/dist/src/vendor/oauth.js
CHANGED
|
@@ -8,7 +8,8 @@ import { lookup } from "node:dns";
|
|
|
8
8
|
import { request } from "node:https";
|
|
9
9
|
import { isIP } from "node:net";
|
|
10
10
|
import { chmodSync, existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
11
|
-
import { dirname, join } from "node:path";
|
|
11
|
+
import { basename, dirname, join } from "node:path";
|
|
12
|
+
import { canSaveState, saveState } from "../state-secrets.js";
|
|
12
13
|
export class AuthError extends Error {
|
|
13
14
|
}
|
|
14
15
|
export class SignInError extends Error {
|
|
@@ -17,6 +18,15 @@ export function staticAuth(headers = {}) {
|
|
|
17
18
|
const since = new Date().toISOString();
|
|
18
19
|
return { headers: async () => headers, refresh: async () => { }, status: () => ({ state: "none", since }), mark: () => { }, signedIn: () => true, signInCount: () => 0 };
|
|
19
20
|
}
|
|
21
|
+
/** Writes `text` as the state file at `path`: through the kernel (awaited, into the module's own secret store) when this service can reach it, else beside the reads (a dev or test run). */
|
|
22
|
+
async function persist(path, text) {
|
|
23
|
+
if (canSaveState())
|
|
24
|
+
return void (await saveState(basename(path), text));
|
|
25
|
+
const tmp = join(dirname(path), `.${basename(path)}.${process.pid}.tmp`);
|
|
26
|
+
writeFileSync(tmp, text, { mode: 0o600 });
|
|
27
|
+
chmodSync(tmp, 0o600);
|
|
28
|
+
renameSync(tmp, path);
|
|
29
|
+
}
|
|
20
30
|
/** token.json (0600), written atomically; a file removed from outside reads as signed out. */
|
|
21
31
|
export function fileStore(path) {
|
|
22
32
|
let token;
|
|
@@ -26,11 +36,8 @@ export function fileStore(path) {
|
|
|
26
36
|
return (token = undefined);
|
|
27
37
|
return (token ??= JSON.parse(readFileSync(path, "utf8")));
|
|
28
38
|
},
|
|
29
|
-
write(t) {
|
|
30
|
-
|
|
31
|
-
writeFileSync(tmp, JSON.stringify(t, null, 2), { mode: 0o600 });
|
|
32
|
-
chmodSync(tmp, 0o600);
|
|
33
|
-
renameSync(tmp, path);
|
|
39
|
+
async write(t) {
|
|
40
|
+
await persist(path, JSON.stringify(t, null, 2));
|
|
34
41
|
token = t;
|
|
35
42
|
},
|
|
36
43
|
};
|
|
@@ -185,7 +192,15 @@ export function oauth(o) {
|
|
|
185
192
|
throw new AuthError(unbound ? changed() : `${o.name} isn't connected yet (Connect on the board)`);
|
|
186
193
|
return t;
|
|
187
194
|
}
|
|
188
|
-
|
|
195
|
+
// Commits one at a time: a refresh's save checks, inside its turn, that the token it refreshed is still the current one, so a
|
|
196
|
+
// reconnect that committed while it waited is never overwritten by the older grant.
|
|
197
|
+
let commits = Promise.resolve();
|
|
198
|
+
const save = (t, still) => {
|
|
199
|
+
const p = commits.then(async () => { if (still && !still())
|
|
200
|
+
return false; await store.write(t); return true; });
|
|
201
|
+
commits = p.catch(() => undefined);
|
|
202
|
+
return p;
|
|
203
|
+
};
|
|
189
204
|
const token = () => current();
|
|
190
205
|
async function doRefresh() {
|
|
191
206
|
const t = load();
|
|
@@ -233,7 +248,8 @@ export function oauth(o) {
|
|
|
233
248
|
}
|
|
234
249
|
// The answer updates the token, never what it is bound to or where the next refresh goes.
|
|
235
250
|
const fresh = Object.fromEntries(Object.entries(next).filter(([k]) => !BOUND.includes(k)));
|
|
236
|
-
save({ ...t, ...fresh, refresh_token: next.refresh_token ?? t.refresh_token, scope: next.scope ?? t.scope })
|
|
251
|
+
if (!(await save({ ...t, ...fresh, refresh_token: next.refresh_token ?? t.refresh_token, scope: next.scope ?? t.scope }, () => token() === t)))
|
|
252
|
+
return token();
|
|
237
253
|
return token();
|
|
238
254
|
}
|
|
239
255
|
const refresh = () => (refreshing ??= doRefresh().finally(() => (refreshing = undefined)));
|
|
@@ -316,8 +332,7 @@ export function oauth(o) {
|
|
|
316
332
|
if (!r.ok || typeof r.body.client_id !== "string")
|
|
317
333
|
throw new SignInError(`${o.name} refused to register AidenHQ (${String(r.body.error_description ?? r.body.error ?? r.status).slice(0, 200)})`);
|
|
318
334
|
const secret = typeof r.body.client_secret === "string" ? r.body.client_secret : undefined;
|
|
319
|
-
|
|
320
|
-
chmodSync(clientPath, 0o600);
|
|
335
|
+
await persist(clientPath, JSON.stringify({ client_id: r.body.client_id, ...(secret ? { client_secret: secret } : {}), redirect_uri: redirectUri, issuer: ep.issuer }, null, 2));
|
|
321
336
|
return { id: r.body.client_id, ...(secret ? { secret } : {}) };
|
|
322
337
|
}
|
|
323
338
|
const recommended = (supported) => {
|
|
@@ -382,7 +397,7 @@ export function oauth(o) {
|
|
|
382
397
|
catch (err) {
|
|
383
398
|
throw new SignInError(`${o.name} refused the sign-in: ${String(err.message).slice(0, 200)}`);
|
|
384
399
|
}
|
|
385
|
-
save({ ...strip(parsed), client_id: p.client.id, ...(p.client.secret ? { client_secret: p.client.secret } : {}), token_endpoint: p.ep.token, issuer: p.ep.issuer, ...binding() });
|
|
400
|
+
await save({ ...strip(parsed), client_id: p.client.id, ...(p.client.secret ? { client_secret: p.client.secret } : {}), token_endpoint: p.ep.token, issuer: p.ep.issuer, ...binding() });
|
|
386
401
|
signedInCount += 1;
|
|
387
402
|
mark("ok");
|
|
388
403
|
},
|
|
@@ -441,7 +456,9 @@ export function oauth(o) {
|
|
|
441
456
|
const parsed = parse(p.body);
|
|
442
457
|
if (run !== me || me.state.state !== "pending")
|
|
443
458
|
return; // cancelled while the answer was on its way: nothing saved
|
|
444
|
-
save
|
|
459
|
+
// A cancel that lands while the save waits its turn wins: the token never takes effect.
|
|
460
|
+
if (!(await save({ ...strip(parsed), client_id: d.client_id, token_endpoint: tokenUrl, issuer, ...binding() }, () => run === me && me.state.state === "pending")))
|
|
461
|
+
return;
|
|
445
462
|
signedInCount += 1;
|
|
446
463
|
mark("ok");
|
|
447
464
|
end({ state: "ok" });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidenhq/sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.15",
|
|
4
4
|
"description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the judges' models and call, the source writer and the vendor MCP pass-through.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -62,7 +62,8 @@
|
|
|
62
62
|
"./teammate-name": "./dist/src/teammate-name.js",
|
|
63
63
|
"./face-kit": "./dist/src/face-kit.js",
|
|
64
64
|
"./requested-parent": "./dist/src/requested-parent.js",
|
|
65
|
-
"./vendor/needs": "./dist/src/vendor/needs.js"
|
|
65
|
+
"./vendor/needs": "./dist/src/vendor/needs.js",
|
|
66
|
+
"./state-secrets": "./dist/src/state-secrets.js"
|
|
66
67
|
},
|
|
67
68
|
"files": ["dist", "LICENSE"],
|
|
68
69
|
"peerDependencies": {
|