@aidenhq/sdk 0.5.8 → 0.5.9
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/assets/page-kit.js +13 -33
- package/dist/src/attribution.d.ts +0 -4
- package/dist/src/attribution.js +1 -9
- package/dist/src/box-bundle.d.ts +1 -5
- package/dist/src/box-bundle.js +2 -9
- package/dist/src/caller.d.ts +0 -2
- package/dist/src/caller.js +2 -6
- package/dist/src/checks.d.ts +9 -52
- package/dist/src/checks.js +10 -53
- package/dist/src/churn.d.ts +2 -12
- package/dist/src/churn.js +3 -17
- package/dist/src/closed-event.d.ts +0 -1
- package/dist/src/closed-event.js +1 -5
- package/dist/src/executor.d.ts +4 -7
- package/dist/src/executor.js +2 -8
- package/dist/src/face-kit.js +1 -4
- package/dist/src/fence.d.ts +2 -10
- package/dist/src/fence.js +4 -15
- package/dist/src/for-others.d.ts +0 -1
- package/dist/src/for-others.js +1 -5
- package/dist/src/instance-details.d.ts +1 -7
- package/dist/src/instance-details.js +4 -16
- package/dist/src/instance-paths.d.ts +1 -6
- package/dist/src/instance-paths.js +2 -11
- package/dist/src/judge.d.ts +3 -21
- package/dist/src/judge.js +5 -25
- package/dist/src/kernel-client.d.ts +0 -1
- package/dist/src/kernel-client.js +3 -12
- package/dist/src/kit.d.ts +0 -3
- package/dist/src/kit.js +0 -3
- package/dist/src/maintained-repos.d.ts +5 -22
- package/dist/src/maintained-repos.js +7 -23
- package/dist/src/number-setting.d.ts +0 -2
- package/dist/src/number-setting.js +1 -6
- package/dist/src/owner-day.d.ts +0 -4
- package/dist/src/owner-day.js +1 -8
- package/dist/src/paperclip.d.ts +0 -3
- package/dist/src/paperclip.js +1 -7
- package/dist/src/pass-rule.d.ts +0 -2
- package/dist/src/pass-rule.js +2 -10
- package/dist/src/provider.d.ts +1 -7
- package/dist/src/provider.js +2 -8
- package/dist/src/quality.d.ts +0 -10
- package/dist/src/quality.js +2 -12
- package/dist/src/repeat.d.ts +1 -6
- package/dist/src/repeat.js +2 -10
- package/dist/src/responses.d.ts +1 -8
- package/dist/src/responses.js +3 -13
- package/dist/src/selector.d.ts +1 -11
- package/dist/src/selector.js +1 -6
- package/dist/src/source-store.d.ts +4 -26
- package/dist/src/source-store.js +5 -30
- package/dist/src/task-state-types.d.ts +3 -18
- package/dist/src/task-state-types.js +1 -3
- package/dist/src/teammate-name.d.ts +0 -1
- package/dist/src/teammate-name.js +2 -6
- package/dist/src/tiers.d.ts +1 -11
- package/dist/src/tiers.js +4 -14
- package/dist/src/tools-client.d.ts +3 -13
- package/dist/src/tools-client.js +7 -28
- package/dist/src/types.d.ts +0 -2
- package/dist/src/types.js +1 -2
- package/dist/src/url-setting.d.ts +0 -1
- package/dist/src/url-setting.js +1 -4
- package/dist/src/vendor/needs.d.ts +1 -7
- package/dist/src/vendor/needs.js +3 -12
- package/dist/src/vendor/oauth.d.ts +6 -35
- package/dist/src/vendor/oauth.js +12 -37
- package/dist/src/vendor/proxy.d.ts +5 -27
- package/dist/src/vendor/proxy.js +42 -114
- package/package.json +1 -1
package/dist/src/vendor/proxy.js
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
// A vendor's MCP server
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// /mcp the kernel's calls for agents (its service key), each tools/call carrying its signed caller assertion;
|
|
7
|
-
// /mcp/read the kernel's calls for runner jobs (the read key): only tools the module's manifest tags read (`reads`, the
|
|
8
|
-
// READ_TOOLS its module.yaml sets from those tags) that the upstream also marks read-only (or the connection's
|
|
9
|
-
// read rule says are reads), so the jobs' token never reaches a write;
|
|
10
|
-
// /auth/* the board's sign-in (state, the scopes the owner may pick, start, finish, or a device code), with the board's own key,
|
|
11
|
-
// and /auth/check: one live read upstream (a tools list), answering { ok: true } or { ok: false, message, fix? };
|
|
12
|
-
// /healthz whether it is connected.
|
|
13
|
-
// A connection adds only its real logic as hooks (a pinned argument, a site id, a deny rule) and its own routes (a write
|
|
14
|
-
// kind's executor). Not signed in: it still starts, lists the tools it saw last as stand-ins, and every call says how to
|
|
15
|
-
// connect (never a crash loop, never a silently empty tool list): a needs_owner answer (needs.ts), which the kernel routes to
|
|
16
|
-
// the owner or the digest.
|
|
17
|
-
//
|
|
18
|
-
// Every module imports it from npm (@aidenhq/sdk/vendor/proxy) at an exact pin (the MCP SDK, zod and pino are theirs: peer dependencies): a change here needs a version bump in package.json,
|
|
19
|
-
// which is what publishes it.
|
|
1
|
+
// A vendor's MCP server passed through whole; the kernel lists and forwards only the tools the module manifest tags and the caller's grant names, files writes as records, and refuses the rest.
|
|
2
|
+
// Routes: /mcp (kernel service key, signed caller assertion on each tools/call), /mcp/read (runner jobs' read key: only manifest-tagged reads the upstream also marks read-only),
|
|
3
|
+
// /auth/* (board sign-in with the board key; /auth/check does one live read upstream), /healthz.
|
|
4
|
+
// Not signed in: still starts, lists last-seen tools as stand-ins, and every call answers needs_owner (needs.ts).
|
|
5
|
+
// Modules import it from npm (@aidenhq/sdk/vendor/proxy) at an exact pin: a change here needs a package.json version bump, which publishes it.
|
|
20
6
|
import { timingSafeEqual } from "node:crypto";
|
|
21
7
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
22
8
|
import { createServer } from "node:http";
|
|
@@ -29,10 +15,8 @@ import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprot
|
|
|
29
15
|
import { z } from "zod";
|
|
30
16
|
import { needsOwner, signedIn } from "./needs.js";
|
|
31
17
|
import { AuthError, SignInError } from "./oauth.js";
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
// HubSpot answers an expired token with a bare 401 (no text), so the transport error's status counts too. An HTTP status
|
|
35
|
-
// decides alone: a 500 whose text mentions 401 may have done its write, which a retry would do again.
|
|
18
|
+
// A 401 refreshes once and retries; a failed refresh (AuthError) already marked its outcome and is never retried.
|
|
19
|
+
// HubSpot's bare 401 counts via the transport status; an HTTP status alone decides (a 500 mentioning 401 may have done its write).
|
|
36
20
|
const isAuthFailure = (err) => {
|
|
37
21
|
if (err instanceof AuthError)
|
|
38
22
|
return false;
|
|
@@ -42,34 +26,28 @@ const isAuthFailure = (err) => {
|
|
|
42
26
|
return /401|unauthori[sz]ed|unauthenticated|invalid_token|not_authenticated/i.test(String(err));
|
|
43
27
|
};
|
|
44
28
|
const refusal = (text) => ({ isError: true, content: [{ type: "text", text }] });
|
|
45
|
-
// Reads run unattended in Codex as read-only, closed-world tools
|
|
46
|
-
// its tier decides before anything runs; these hints only keep the harness from stopping to ask a person who isn't there.
|
|
29
|
+
// Reads run unattended in Codex as read-only, closed-world tools; the hints only stop the harness asking a person.
|
|
47
30
|
const READ_HINTS = { readOnlyHint: true, destructiveHint: false, openWorldHint: false };
|
|
48
31
|
const WRITE_HINTS = { readOnlyHint: false, destructiveHint: false, openWorldHint: false };
|
|
49
32
|
export function createVendorProxy(o) {
|
|
50
33
|
const { log, auth, hooks = {} } = o;
|
|
51
34
|
const isRead = hooks.isRead ?? ((t) => t.annotations?.readOnlyHint === true);
|
|
52
35
|
const reads = new Set(o.reads ?? []);
|
|
53
|
-
//
|
|
36
|
+
// Read endpoint: tagged read in the manifest and read by the upstream's word; either alone isn't enough.
|
|
54
37
|
const servesRead = (t) => reads.has(t.name) && isRead(t);
|
|
55
38
|
const denied = (name) => Boolean(hooks.deny?.test(name));
|
|
56
39
|
const cacheFile = o.stateDir ? join(o.stateDir, "tools.json") : undefined;
|
|
57
40
|
let upstream;
|
|
58
|
-
// A reset
|
|
59
|
-
// not become the cached session, or calls would run under the account the owner just replaced.
|
|
41
|
+
// A reset invalidates every in-flight connect: a stale connect must not become the session.
|
|
60
42
|
let generation = 0;
|
|
61
|
-
//
|
|
62
|
-
// connect can never hold up the calls after it. Which one becomes the session is decided at commit (see `commit`).
|
|
43
|
+
// Calls in one generation share its connect; a new generation starts its own so a stalled connect never blocks later calls.
|
|
63
44
|
let pending;
|
|
64
45
|
let resetting;
|
|
65
|
-
// A new sign-in
|
|
66
|
-
// never replayed on the new one: its arguments and its results belong to the owner who sent it, not the account that replaced it.
|
|
46
|
+
// A new sign-in, not a refresh: calls started under the old one are never replayed on it.
|
|
67
47
|
let signIn = 0;
|
|
68
48
|
const notConnected = () => (!o.upstream ? `${o.name} isn't connected yet: the owner presses Connect on the board (Settings, Connections).` : auth.status().state === "expired" ? `${o.name}'s sign-in stopped working: the owner reconnects it on the board.` : `${o.name} isn't connected: the owner signs in from the board.`);
|
|
69
|
-
/** The connect started before a reset, whose session belongs to the account or token that reset replaced. */
|
|
70
49
|
class StaleConnect extends Error {
|
|
71
50
|
}
|
|
72
|
-
/** A different sign-in replaced this call's while it waited: it is not sent (or replayed) under the new account. */
|
|
73
51
|
class StaleSignIn extends Error {
|
|
74
52
|
}
|
|
75
53
|
/** The token endpoint failed (a 5xx, a timeout): the vendor never refused the grant, so nothing is marked expired. */
|
|
@@ -85,8 +63,7 @@ export function createVendorProxy(o) {
|
|
|
85
63
|
throw new AuthError(notConnected());
|
|
86
64
|
const client = new Client({ name: `aidenhq-${o.name.toLowerCase()}-proxy`, version: "0.2.0" });
|
|
87
65
|
try {
|
|
88
|
-
// No redirect
|
|
89
|
-
// connection's key header) goes only to the upstream address it was issued for.
|
|
66
|
+
// No redirect: the token goes only to the address it was issued for.
|
|
90
67
|
await client.connect(new StreamableHTTPClientTransport(o.upstream, { requestInit: { headers: await auth.headers() }, fetch: (url, init) => fetch(url, { ...init, redirect: "error" }) }));
|
|
91
68
|
const tools = [];
|
|
92
69
|
let cursor;
|
|
@@ -95,32 +72,28 @@ export function createVendorProxy(o) {
|
|
|
95
72
|
tools.push(...page.tools);
|
|
96
73
|
cursor = page.nextCursor;
|
|
97
74
|
} while (cursor);
|
|
98
|
-
// A connect
|
|
75
|
+
// A superseded connect never runs the hook.
|
|
99
76
|
if (gen !== generation)
|
|
100
77
|
throw new StaleConnect();
|
|
101
78
|
const commitHook = await hooks.connected?.(client, tools);
|
|
102
|
-
//
|
|
79
|
+
// Superseded during the hook: its state must not land over the winner's.
|
|
103
80
|
if (gen !== generation)
|
|
104
81
|
throw new StaleConnect();
|
|
105
82
|
commitHook?.();
|
|
106
|
-
// The session this one replaces (a racing reconnect's) is retired, never left open.
|
|
107
83
|
if (upstream)
|
|
108
84
|
retire(upstream);
|
|
109
85
|
upstream = { client, tools, at: Date.now(), active: 0, retired: false };
|
|
110
86
|
}
|
|
111
87
|
catch (err) {
|
|
112
|
-
//
|
|
113
|
-
// the cached session: close it here, or its event stream stays open with nothing able to reach it.
|
|
88
|
+
// Close a failed connect's client or its event stream stays open.
|
|
114
89
|
void client.close().catch(() => undefined);
|
|
115
|
-
// A connect
|
|
116
|
-
// caller takes the current session instead of refreshing (or marking expired) against the grant that replaced it.
|
|
90
|
+
// A superseded connect's failure is stale: take the current session instead of refreshing.
|
|
117
91
|
if (gen !== generation)
|
|
118
92
|
throw new StaleConnect();
|
|
119
93
|
throw err;
|
|
120
94
|
}
|
|
121
95
|
auth.mark("ok");
|
|
122
96
|
const names = upstream.tools.map((t) => t.name).filter((n) => !denied(n));
|
|
123
|
-
// The kernel lists only what the module tags; a name here it doesn't tag reaches nobody until a module change does.
|
|
124
97
|
log.info({ upstream_tools: upstream.tools.length, tools: names }, `connected to ${o.name}`);
|
|
125
98
|
if (cacheFile)
|
|
126
99
|
try {
|
|
@@ -130,9 +103,7 @@ export function createVendorProxy(o) {
|
|
|
130
103
|
return upstream;
|
|
131
104
|
}
|
|
132
105
|
function connect() {
|
|
133
|
-
//
|
|
134
|
-
// other, and every caller waiting on them would lose its session in turn. A new generation opens its own at once (nothing
|
|
135
|
-
// queues): a stalled connect on a replaced sign-in must not hold up the calls after it, and `commit` decides the winner.
|
|
106
|
+
// Wait on the connect already running for this generation (two would replace each other); a new generation opens its own.
|
|
136
107
|
if (pending && pending.gen === generation)
|
|
137
108
|
return pending.p;
|
|
138
109
|
const gen = generation;
|
|
@@ -143,13 +114,11 @@ export function createVendorProxy(o) {
|
|
|
143
114
|
void entry.p.then(done, done);
|
|
144
115
|
return entry.p;
|
|
145
116
|
}
|
|
146
|
-
/** A replaced session closes once its in-flight calls end; until then they keep answering on it. */
|
|
147
117
|
function retire(old) {
|
|
148
118
|
old.retired = true;
|
|
149
119
|
if (old.active === 0)
|
|
150
120
|
void old.client.close().catch(() => undefined);
|
|
151
121
|
}
|
|
152
|
-
/** The session only: invalidated and retired, nothing connecting in its place (the sign-in is gone, or a check just failed). */
|
|
153
122
|
function drop() {
|
|
154
123
|
generation += 1;
|
|
155
124
|
const old = upstream;
|
|
@@ -157,11 +126,7 @@ export function createVendorProxy(o) {
|
|
|
157
126
|
if (old)
|
|
158
127
|
retire(old);
|
|
159
128
|
}
|
|
160
|
-
/**
|
|
161
|
-
* Replaces the session. Concurrent callers share one reset rather than each invalidating the other's connect, but only when the
|
|
162
|
-
* pending one already does everything they need: a refresh never joins a session-only reset (it would swallow the grant), and a
|
|
163
|
-
* new sign-in (`force`) never joins at all, since a connect started before it belongs to the account it replaced.
|
|
164
|
-
*/
|
|
129
|
+
/** Replaces the session. Callers share one reset only if it does all they need: a refresh never joins a session-only reset, a new sign-in (`force`) never joins. */
|
|
165
130
|
function reset(refresh, force = false) {
|
|
166
131
|
if (!force && resetting && (resetting.refresh || !refresh))
|
|
167
132
|
return resetting.p;
|
|
@@ -174,16 +139,14 @@ export function createVendorProxy(o) {
|
|
|
174
139
|
upstream = undefined;
|
|
175
140
|
if (old)
|
|
176
141
|
retire(old);
|
|
177
|
-
// A token endpoint failure is the vendor's, not a refusal
|
|
142
|
+
// A token endpoint failure is the vendor's, not a refusal: pass it on, nothing marked expired.
|
|
178
143
|
if (refresh)
|
|
179
144
|
await auth.refresh().catch((err) => { throw new RefreshFailed(err); });
|
|
180
|
-
// A
|
|
181
|
-
// session now: connecting here would replace the newer sign-in's session with one opened from this one's grant.
|
|
145
|
+
// A reset that replaced this refresh owns the session: connecting here would override the newer sign-in.
|
|
182
146
|
if (refresh && resetting !== entry)
|
|
183
147
|
return;
|
|
184
|
-
// A newer reset took over while this one ran: it owns the session now, so there is nothing left to connect here.
|
|
185
148
|
await connect().catch((err) => {
|
|
186
|
-
// A sign-in that
|
|
149
|
+
// A sign-in that could not connect is a real failure; a stale one is the newer reset's.
|
|
187
150
|
if (!(err instanceof StaleConnect))
|
|
188
151
|
throw err;
|
|
189
152
|
});
|
|
@@ -192,11 +155,7 @@ export function createVendorProxy(o) {
|
|
|
192
155
|
resetting = entry;
|
|
193
156
|
return entry.p;
|
|
194
157
|
}
|
|
195
|
-
/**
|
|
196
|
-
* Waits on a reset another call started, but never past `resetWaitMs`: a handshake that stalls there (a cold start, a periodic
|
|
197
|
-
* reconnect) must not hold up every call after it. A reset left waiting that long is abandoned, and this call opens a session of
|
|
198
|
-
* its own at once; the stalled connect is superseded, so it can never become the session.
|
|
199
|
-
*/
|
|
158
|
+
/** Waits on another call's reset for at most `resetWaitMs`, then opens its own session; the stalled connect is superseded. */
|
|
200
159
|
async function settled() {
|
|
201
160
|
const entry = resetting;
|
|
202
161
|
if (!entry)
|
|
@@ -210,19 +169,15 @@ export function createVendorProxy(o) {
|
|
|
210
169
|
clearTimeout(timer);
|
|
211
170
|
if (!stalled)
|
|
212
171
|
return;
|
|
213
|
-
// Only the caller that finds this reset
|
|
214
|
-
// bump the generation again, or it would invalidate the session the first one opened.
|
|
172
|
+
// Only the caller that finds this reset current invalidates it, or the generation bumps twice.
|
|
215
173
|
if (resetting !== entry)
|
|
216
174
|
return;
|
|
217
175
|
log.warn(`${o.name}: a ${entry.refresh ? "token refresh" : "reconnect"} stalled; opening a session of its own`);
|
|
218
176
|
resetting = undefined;
|
|
219
177
|
generation += 1;
|
|
220
178
|
}
|
|
221
|
-
/** The current session, or a fresh one; a connect a reset invalidated is retried on the session that reset opened. */
|
|
222
179
|
async function acquire() {
|
|
223
|
-
// A reset in flight
|
|
224
|
-
// the old one and come back 401 after the refresh succeeded, so wait for it and use the session it opened. A connect a reset
|
|
225
|
-
// superseded while it ran is the same case, so it waits for that reset too rather than opening another handshake at once.
|
|
180
|
+
// A reset in flight may be replacing the token: wait and use its session, or the handshake 401s.
|
|
226
181
|
await settled();
|
|
227
182
|
if (upstream)
|
|
228
183
|
return upstream;
|
|
@@ -232,13 +187,11 @@ export function createVendorProxy(o) {
|
|
|
232
187
|
catch (err) {
|
|
233
188
|
if (!(err instanceof StaleConnect))
|
|
234
189
|
throw err;
|
|
235
|
-
// A reset started
|
|
236
|
-
// wait for it again before deciding: opening a handshake here could send the token the refresh is replacing.
|
|
190
|
+
// A reset started during connect: wait again, it may still be replacing the token.
|
|
237
191
|
await settled();
|
|
238
192
|
return upstream ?? (await connect());
|
|
239
193
|
}
|
|
240
194
|
}
|
|
241
|
-
/** Runs one call on a session, counted so a retired session isn't closed under it. */
|
|
242
195
|
async function on(u, fn) {
|
|
243
196
|
u.active += 1;
|
|
244
197
|
try {
|
|
@@ -250,11 +203,7 @@ export function createVendorProxy(o) {
|
|
|
250
203
|
void u.client.close().catch(() => undefined);
|
|
251
204
|
}
|
|
252
205
|
}
|
|
253
|
-
/**
|
|
254
|
-
* One dispatch on the current session, bound to the sign-in the call started under: the check runs after every wait (a reset, a
|
|
255
|
-
* refresh, a connect), immediately before the tool call, so a sign-in that lands meanwhile fails the call instead of sending it —
|
|
256
|
-
* and its results — through the account that replaced the owner who asked.
|
|
257
|
-
*/
|
|
206
|
+
/** One dispatch bound to the sign-in the call started under, re-checked right before the tool call so a new sign-in fails it rather than sending it through the replacing account. */
|
|
258
207
|
async function dispatch(under, fn) {
|
|
259
208
|
const u = await acquire();
|
|
260
209
|
if (signIn !== under)
|
|
@@ -273,12 +222,10 @@ export function createVendorProxy(o) {
|
|
|
273
222
|
catch (err) {
|
|
274
223
|
if (!auth.signedIn() || !isAuthFailure(err) || u === upstream)
|
|
275
224
|
throw err;
|
|
276
|
-
// A different account signed in
|
|
277
|
-
// here rather than being replayed (and answered, private data included) under the account that replaced the sign-in.
|
|
225
|
+
// A different account signed in: fail rather than replay under it.
|
|
278
226
|
if (signIn !== under)
|
|
279
227
|
throw err;
|
|
280
|
-
// The session
|
|
281
|
-
// belongs to the token it was opened with, so retry on the current session and decide nothing about this one.
|
|
228
|
+
// The failing session is no longer current: its 401 belongs to the old token; retry on the current one.
|
|
282
229
|
return await dispatch(under, fn);
|
|
283
230
|
}
|
|
284
231
|
}
|
|
@@ -286,12 +233,10 @@ export function createVendorProxy(o) {
|
|
|
286
233
|
const under = signIn;
|
|
287
234
|
// Reconnect every 30 minutes or after an auth failure (inside the try: a token revoked early fails connect() too).
|
|
288
235
|
try {
|
|
289
|
-
//
|
|
290
|
-
// a call never joins a reset another call started and waits on it for as long as that connect hangs.
|
|
236
|
+
// Only an existing session can be too old; otherwise `acquire` opens it, bounded by `settled`.
|
|
291
237
|
if (upstream && Date.now() - upstream.at > 30 * 60_000) {
|
|
292
238
|
await reset(false);
|
|
293
|
-
// The reconnect may have waited on a refresh or
|
|
294
|
-
// other retry path does, so one that landed meanwhile never receives it.
|
|
239
|
+
// The reconnect may have waited on a refresh or sign-in: dispatch bound to this call's sign-in.
|
|
295
240
|
return await dispatch(under, fn);
|
|
296
241
|
}
|
|
297
242
|
return await run(fn);
|
|
@@ -299,11 +244,10 @@ export function createVendorProxy(o) {
|
|
|
299
244
|
catch (err) {
|
|
300
245
|
if (!auth.signedIn() || !isAuthFailure(err))
|
|
301
246
|
throw err;
|
|
302
|
-
// A different account signed in while this call ran: never refresh or replay it under the new sign-in (see `run`).
|
|
303
247
|
if (signIn !== under)
|
|
304
248
|
throw err;
|
|
305
249
|
// The vendor's REST side still answers this token: its MCP endpoint's 401 is that endpoint's own error, not a sign-in to refresh or reconnect.
|
|
306
|
-
//
|
|
250
|
+
// New session with the current token (cached headers are fixed at connect); no refresh grant used.
|
|
307
251
|
if (auth.refused && !(await auth.refused())) {
|
|
308
252
|
await reset(false);
|
|
309
253
|
return await dispatch(under, fn);
|
|
@@ -313,12 +257,10 @@ export function createVendorProxy(o) {
|
|
|
313
257
|
await reset(true);
|
|
314
258
|
}
|
|
315
259
|
catch (err) {
|
|
316
|
-
//
|
|
317
|
-
// expiry. Only a handshake that still 401s with the fresh token is a refused sign-in.
|
|
260
|
+
// Token endpoint failure (5xx, timeout) is transient, never an expiry; only a handshake still 401ing with a fresh token is a refusal.
|
|
318
261
|
if (err instanceof RefreshFailed)
|
|
319
262
|
throw err.reason;
|
|
320
|
-
//
|
|
321
|
-
// refusal probe, which may itself have taken long enough for a new sign-in to land.
|
|
263
|
+
// Only the current sign-in is marked, after the refusal probe (a new sign-in may land meanwhile).
|
|
322
264
|
const refused = isAuthFailure(err) && (await auth.refused?.() ?? true);
|
|
323
265
|
if (refused && signIn === under)
|
|
324
266
|
auth.mark("expired", `${o.name} rejected a freshly refreshed token`);
|
|
@@ -331,8 +273,7 @@ export function createVendorProxy(o) {
|
|
|
331
273
|
return await on(r, fn);
|
|
332
274
|
}
|
|
333
275
|
catch (again) {
|
|
334
|
-
// A late 401 from a
|
|
335
|
-
// rechecked after the refusal probe, which may itself have taken long enough for a new session or sign-in to land.
|
|
276
|
+
// A late 401 from a replaced session decides nothing: recheck after the probe.
|
|
336
277
|
if (r === upstream && isAuthFailure(again) && (await auth.refused?.() ?? true) && r === upstream && signIn === under) {
|
|
337
278
|
auth.mark("expired", `${o.name} rejected a freshly refreshed token`);
|
|
338
279
|
}
|
|
@@ -340,7 +281,6 @@ export function createVendorProxy(o) {
|
|
|
340
281
|
}
|
|
341
282
|
}
|
|
342
283
|
}
|
|
343
|
-
/** The tools as listed: deny applied, the connection's view, and the harness hints. */
|
|
344
284
|
function shown(tools, readOnly) {
|
|
345
285
|
const kept = tools.filter((t) => !denied(t.name) && (!readOnly || servesRead(t)));
|
|
346
286
|
return (hooks.tools ? hooks.tools(kept) : kept).map((t) => ({ ...t, annotations: { ...(t.annotations ?? {}), ...(isRead(t) ? READ_HINTS : WRITE_HINTS) } }));
|
|
@@ -360,8 +300,7 @@ export function createVendorProxy(o) {
|
|
|
360
300
|
// Only what the upstream lists: its schema is what the hooks pin arguments against.
|
|
361
301
|
if (!tool)
|
|
362
302
|
throw new Error(`${o.name} doesn't offer ${name}`);
|
|
363
|
-
// Checked on the session
|
|
364
|
-
// no longer marks read-only.
|
|
303
|
+
// Checked on the dispatching session: a reconnect may drop the tool's read-only mark.
|
|
365
304
|
if (readOnly && !servesRead(tool)) {
|
|
366
305
|
log.warn({ tool: name }, "refused a write on the read endpoint");
|
|
367
306
|
return refusal(`Tool ${name} is not a read: the read endpoint serves reads only.`);
|
|
@@ -390,14 +329,13 @@ export function createVendorProxy(o) {
|
|
|
390
329
|
log.warn({ tool: name }, "refused a denied tool");
|
|
391
330
|
return refusal(`Tool ${name} is not available through AidenHQ.`);
|
|
392
331
|
}
|
|
393
|
-
//
|
|
332
|
+
// Owner-only fixes: the kernel routes the need (needs.ts), never this text.
|
|
394
333
|
const need = () => needsOwner({ cause: auth.status().state === "expired" ? "expired" : "not_connected", detail: notConnected() });
|
|
395
334
|
if (!o.upstream || !auth.signedIn())
|
|
396
335
|
return need();
|
|
397
336
|
try {
|
|
398
|
-
//
|
|
337
|
+
// Success clears a down state in the kernel (needs.ts).
|
|
399
338
|
const result = await call(name, req.params.arguments ?? {}, readOnly);
|
|
400
|
-
// A connection that serves its calls itself (`hooks.call`) marks what its sign-ins served, and what they didn't.
|
|
401
339
|
return result.isError || hooks.call ? result : signedIn(result);
|
|
402
340
|
}
|
|
403
341
|
catch (err) {
|
|
@@ -429,7 +367,6 @@ export function createVendorProxy(o) {
|
|
|
429
367
|
return chunks.length ? JSON.parse(Buffer.concat(chunks).toString("utf8")) : undefined;
|
|
430
368
|
}
|
|
431
369
|
const sendJson = (res, status, body) => void res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body));
|
|
432
|
-
/** One live, read-only call upstream (its tool list): works, or what is wrong and the one thing to do about it. */
|
|
433
370
|
async function checkConnection() {
|
|
434
371
|
if (!o.upstream || !auth.signedIn())
|
|
435
372
|
return { ok: false, message: notConnected(), fix: "Connect" };
|
|
@@ -440,19 +377,16 @@ export function createVendorProxy(o) {
|
|
|
440
377
|
return problem ? { ok: false, message: `${o.name} is signed in but can't be used: ${problem}`, fix: "Reconnect" } : { ok: true };
|
|
441
378
|
}
|
|
442
379
|
catch (err) {
|
|
443
|
-
// A sign-in
|
|
444
|
-
// from the token endpoint (a 5xx, a timeout) leaves the grant untouched, so it is the vendor's outage, not the owner's sign-in.
|
|
380
|
+
// A refused sign-in is marked expired by refresh; a token-endpoint AuthError (5xx, timeout) is the vendor's outage.
|
|
445
381
|
const expired = auth.status().state === "expired";
|
|
446
382
|
const authFailure = !expired && !(err instanceof AuthError) && isAuthFailure(err);
|
|
447
383
|
const refused = authFailure && (await auth.refused?.() ?? true);
|
|
448
|
-
// A session
|
|
449
|
-
// the next check (when a bind changes the primary) opens one from the sign-in that is there now.
|
|
384
|
+
// A session the upstream no longer accepts is retired so the next check opens one from the current sign-in.
|
|
450
385
|
if (authFailure && upstream)
|
|
451
386
|
drop();
|
|
452
387
|
return expired || refused ? { ok: false, message: `${o.name} no longer accepts the sign-in.`, fix: "Reconnect" } : { ok: false, message: `${o.name} didn't answer: ${String(err.message).slice(0, 200)}` };
|
|
453
388
|
}
|
|
454
389
|
}
|
|
455
|
-
/** The board's sign-in endpoints: state, the scopes on offer, and starting or finishing a sign-in (never a token). */
|
|
456
390
|
async function handleAuth(req, res, path) {
|
|
457
391
|
if (!bearer(req, o.authKey))
|
|
458
392
|
return sendJson(res, 401, { error: "unauthorized" });
|
|
@@ -468,7 +402,6 @@ export function createVendorProxy(o) {
|
|
|
468
402
|
if (!o.upstream)
|
|
469
403
|
return sendJson(res, 400, { error: notConnected() });
|
|
470
404
|
try {
|
|
471
|
-
// A device sign-in: start (the code for the board to show), its state while this service polls, cancel.
|
|
472
405
|
if (path === "/auth/device" || path === "/auth/device/cancel") {
|
|
473
406
|
const d = auth.device;
|
|
474
407
|
if (!d)
|
|
@@ -496,20 +429,17 @@ export function createVendorProxy(o) {
|
|
|
496
429
|
const { redirect_uri, scopes, account, client_id } = z.object({ redirect_uri: z.string().max(200), scopes: z.array(z.string().max(200)).max(200).optional(), account: z.string().max(200).optional(), client_id: z.string().max(200).optional() }).parse(await readJson(req));
|
|
497
430
|
return sendJson(res, 200, await flow.start(redirect_uri, scopes, account || client_id ? { account, client_id } : undefined));
|
|
498
431
|
}
|
|
499
|
-
// One account signed out, on a connection with several (the board's per-account Disconnect).
|
|
500
432
|
if (path === "/auth/disconnect" && auth.disconnect) {
|
|
501
433
|
const { account } = z.object({ account: z.string().max(200).optional() }).parse((await readJson(req)) ?? {});
|
|
502
434
|
await auth.disconnect(account);
|
|
503
|
-
//
|
|
504
|
-
// a disconnect of one workspace must not outlast the board's 20 s on an unrelated workspace's handshake.
|
|
435
|
+
// Retired with the sign-in; never waits on the primary (must not outlast the board's 20 s on another workspace's handshake).
|
|
505
436
|
drop();
|
|
506
437
|
return sendJson(res, 200, { ok: true });
|
|
507
438
|
}
|
|
508
439
|
if (path === "/auth/finish") {
|
|
509
440
|
const { state, code } = z.object({ state: z.string().max(200), code: z.string().max(4096) }).parse(await readJson(req));
|
|
510
441
|
await flow.finish(state, code);
|
|
511
|
-
//
|
|
512
|
-
// vendor handshake never fails a sign-in whose token is already saved.
|
|
442
|
+
// Connects after the answer so a slow handshake never fails a saved sign-in.
|
|
513
443
|
void reset(false, true).catch((err) => log.warn({ err: String(err).slice(0, 300) }, "connecting after sign-in failed"));
|
|
514
444
|
log.info({ scope: auth.status().scope }, `signed in to ${o.name}`);
|
|
515
445
|
return sendJson(res, 200, { ok: true });
|
|
@@ -533,7 +463,6 @@ export function createVendorProxy(o) {
|
|
|
533
463
|
try {
|
|
534
464
|
if (url.pathname === "/healthz") {
|
|
535
465
|
const s = auth.status();
|
|
536
|
-
// Without a sign-in (a local server) the only question is whether it answers; a failed ping drops the client.
|
|
537
466
|
if (s.state === "none" && o.upstream) {
|
|
538
467
|
const down = await withUpstream((u) => u.client.ping({ timeout: 3000 })).then(() => undefined, (err) => { const old = upstream; upstream = undefined; if (old)
|
|
539
468
|
retire(old); return `${o.name} didn't answer: ${String(err.message).slice(0, 150)}`; });
|
|
@@ -581,7 +510,6 @@ export function createVendorProxy(o) {
|
|
|
581
510
|
res.writeHead(500).end();
|
|
582
511
|
}
|
|
583
512
|
});
|
|
584
|
-
// The current session's tools as agents see them, opened (or refreshed) like any call; undefined when it couldn't list them.
|
|
585
513
|
async function listTools() {
|
|
586
514
|
if (!o.upstream || !auth.signedIn())
|
|
587
515
|
return [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidenhq/sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.9",
|
|
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": {
|