@agent-relay/core-bridge 17.2.1 → 18.1.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/dist/spawn.js ADDED
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Typed helpers for the core's native spawn controls (`spawn.*`), copied from
3
+ * `crates/relay-node/src/relay/contract/spawn_control.rs`. The contract carries
4
+ * no token, URL or header: the core uses the caller's own stored registration.
5
+ *
6
+ * Server rules a caller must know:
7
+ * - A service caller binds its connection first with `authenticate`
8
+ * (`auth.ts`) and then names no caller: the bound connection is the caller.
9
+ * Naming `caller_name` on a bound connection is refused.
10
+ * - A session caller names `caller_name`, which must be an active local
11
+ * registration of this core that has a stored token; the core never
12
+ * returns that token.
13
+ * - No credential is ever a param. A `caller_secret` key is refused
14
+ * (`legacy_credential_refused`) locally and by the core.
15
+ * - `spawn.invoke` targets only this computer's own spawn node, which must be
16
+ * live, have `handlers_live`, not be stale, and advertise `spawn:<harness>`.
17
+ * - Idempotency keys must be persisted by the caller and reused on retry.
18
+ * - `spawn.status` fills in `agent_id`, `name` and `node_id` only for a spawn
19
+ * made on this core.
20
+ *
21
+ * The helpers check params locally with the core's own rules and throw
22
+ * `CoreBridgeError` with code `invalid_params`, to save a round trip; the
23
+ * core stays authoritative.
24
+ */
25
+ import { randomUUID } from 'node:crypto';
26
+ import path from 'node:path';
27
+ import { CoreBridgeError, CoreRpcError } from './errors.js';
28
+ /** Every invocation status the core passes through (`spawn/control.rs`). */
29
+ export const SPAWN_STATUSES = ['pending', 'dispatched', 'running', 'completed', 'failed'];
30
+ /** The bounded machine codes the core lets through (`spawn/control.rs` `error_code`). */
31
+ export const SPAWN_ERROR_CODES = [
32
+ 'spawn_failed',
33
+ 'spawn_invalid_input',
34
+ 'spawn_harness_unavailable',
35
+ 'spawn_harness_unsupported',
36
+ 'spawn_capacity_exceeded',
37
+ 'capacity_changed',
38
+ 'spawn_target_unavailable',
39
+ 'spawn_target_offline',
40
+ 'spawn_agent_name_in_use',
41
+ 'spawn_disabled',
42
+ 'release_identity_mismatch',
43
+ 'release_failed',
44
+ 'handler_unavailable',
45
+ 'capability_mismatch',
46
+ ];
47
+ /**
48
+ * Whether a release is confirmed: only when the core reported
49
+ * `released === true`. A `completed` status, a missing field or any other
50
+ * value (the string `"true"` included) is not a confirmed release.
51
+ * @param invocation - A spawn control invocation, or a raw reply.
52
+ * @returns True only for an invocation whose `released` is exactly `true`.
53
+ */
54
+ export function isReleaseConfirmed(invocation) {
55
+ return invocation?.released === true;
56
+ }
57
+ /** A reply as the core sent it, with `released` read strictly. */
58
+ function invocationOf(reply) {
59
+ return { ...reply, released: isReleaseConfirmed(reply) };
60
+ }
61
+ /**
62
+ * The exact keys each params struct has, in the Rust field order. Optional
63
+ * keys left undefined are not sent.
64
+ */
65
+ export const SPAWN_INVOKE_KEYS = [
66
+ 'caller_name',
67
+ 'node_id',
68
+ 'node_name',
69
+ 'name',
70
+ 'harness',
71
+ 'task',
72
+ 'directory',
73
+ 'idempotency_key',
74
+ ];
75
+ export const SPAWN_STATUS_KEYS = [
76
+ 'caller_name',
77
+ 'action',
78
+ 'invocation_id',
79
+ ];
80
+ export const SPAWN_RELEASE_KEYS = [
81
+ 'caller_name',
82
+ 'node_id',
83
+ 'node_name',
84
+ 'name',
85
+ 'expected_agent_id',
86
+ 'idempotency_key',
87
+ ];
88
+ const SEGMENT = /^[A-Za-z0-9_-]{1,200}$/;
89
+ const MIN_KEY_LENGTH = 16;
90
+ const MAX_TASK_BYTES = 64 * 1024;
91
+ /**
92
+ * The default timeout of `spawn.invoke` and `spawn.release`: six minutes,
93
+ * because target lookup, verified agent startup and exact remote cleanup can
94
+ * outlast the bridge client's 15-second call deadline. Override it per call
95
+ * with {@link SpawnActionOptions.actionTimeoutMs}.
96
+ */
97
+ export const DEFAULT_SPAWN_ACTION_TIMEOUT_MS = 360_000;
98
+ function invalid(field, rule) {
99
+ // Names the field only: values (the task above all) may hold user text.
100
+ return new CoreBridgeError('invalid_params', `Native spawn input is invalid: ${field} ${rule}.`);
101
+ }
102
+ function segment(params, field) {
103
+ const value = params[field];
104
+ if (typeof value !== 'string' || !SEGMENT.test(value)) {
105
+ throw invalid(field, 'must be 1 to 200 letters, digits, "_" or "-"');
106
+ }
107
+ }
108
+ function key(params, field) {
109
+ segment(params, field);
110
+ if (params[field].length < MIN_KEY_LENGTH) {
111
+ throw invalid(field, `must be at least ${MIN_KEY_LENGTH} characters`);
112
+ }
113
+ }
114
+ function pick(params, keys) {
115
+ const out = {};
116
+ for (const k of keys) {
117
+ const value = params[k];
118
+ if (value !== undefined)
119
+ out[k] = value;
120
+ }
121
+ return out;
122
+ }
123
+ /**
124
+ * Refuses a `caller_secret` key before anything filters the params, so a
125
+ * secret is never silently dropped or sent. The message never holds the value.
126
+ */
127
+ function noLegacyCredential(params) {
128
+ if (params !== null && typeof params === 'object' && Object.hasOwn(params, 'caller_secret')) {
129
+ throw new CoreBridgeError('legacy_credential_refused', 'caller_secret is no longer accepted; bind the connection with authenticate() and omit the caller.');
130
+ }
131
+ }
132
+ function optionalSegment(params, field) {
133
+ if (params[field] !== undefined)
134
+ segment(params, field);
135
+ }
136
+ function requireMethod(client, method) {
137
+ if (!client.hello.methods.includes(method)) {
138
+ throw new CoreRpcError(`This Agent Relay core does not offer ${method}; update the core.`, {
139
+ rpcCode: -32601,
140
+ kind: 'method_not_found',
141
+ retryable: false,
142
+ });
143
+ }
144
+ }
145
+ /**
146
+ * A fresh idempotency key: a random UUID without dashes (32 hex characters).
147
+ * Persist it and reuse it when retrying the same spawn or release.
148
+ * @returns A fresh 32-character hexadecimal idempotency key.
149
+ */
150
+ export function newIdempotencyKey() {
151
+ return randomUUID().replace(/-/g, '');
152
+ }
153
+ /**
154
+ * Lists the workspace's spawn nodes (`spawn.targets`).
155
+ * @param client - A greeted connection to the Agent Relay core.
156
+ * @param options - Optional timeout and cancellation controls for this call.
157
+ * @returns The spawn targets visible to the core.
158
+ */
159
+ export async function spawnTargets(client, options) {
160
+ requireMethod(client, 'spawn.targets');
161
+ return client.call('spawn.targets', undefined, options);
162
+ }
163
+ /**
164
+ * Starts an agent on this computer's own spawn node (`spawn.invoke`). This
165
+ * makes Relaycast calls; poll on a second connection while it runs.
166
+ * @param client - A greeted connection to the Agent Relay core.
167
+ * @param params - The exact spawn request accepted by the core.
168
+ * @param options - Optional timeout and cancellation controls.
169
+ * `actionTimeoutMs` defaults to {@link DEFAULT_SPAWN_ACTION_TIMEOUT_MS} (six
170
+ * minutes), separate from the bridge client's 15-second call deadline.
171
+ * @returns The accepted spawn invocation receipt.
172
+ */
173
+ export async function spawnInvoke(client, params, options = {}) {
174
+ noLegacyCredential(params);
175
+ requireMethod(client, 'spawn.invoke');
176
+ optionalSegment(params, 'caller_name');
177
+ for (const field of ['node_id', 'node_name', 'name'])
178
+ segment(params, field);
179
+ key(params, 'idempotency_key');
180
+ if (typeof params.harness !== 'string' || params.harness.length === 0)
181
+ throw invalid('harness', 'must be named');
182
+ if (typeof params.task !== 'string' ||
183
+ params.task.trim().length === 0 ||
184
+ Buffer.byteLength(params.task, 'utf8') > MAX_TASK_BYTES) {
185
+ throw invalid('task', 'must be non-empty and at most 64 KiB');
186
+ }
187
+ if (typeof params.directory !== 'string' ||
188
+ !path.isAbsolute(params.directory) ||
189
+ params.directory.includes('\0')) {
190
+ throw invalid('directory', 'must be an absolute path');
191
+ }
192
+ return invocationOf(await client.call('spawn.invoke', pick(params, SPAWN_INVOKE_KEYS), spawnActionOptions(options)));
193
+ }
194
+ /**
195
+ * Reads a spawn or release invocation (`spawn.status`).
196
+ * @param client - A greeted connection to the Agent Relay core.
197
+ * @param params - The invocation identity and action family to read.
198
+ * @param options - Optional timeout and cancellation controls for this call.
199
+ * @returns The current invocation state.
200
+ */
201
+ export async function spawnStatus(client, params, options) {
202
+ noLegacyCredential(params);
203
+ requireMethod(client, 'spawn.status');
204
+ optionalSegment(params, 'caller_name');
205
+ segment(params, 'invocation_id');
206
+ if (params.action !== 'spawn' && params.action !== 'release')
207
+ throw invalid('action', 'must be "spawn" or "release"');
208
+ return invocationOf(await client.call('spawn.status', pick(params, SPAWN_STATUS_KEYS), options));
209
+ }
210
+ /**
211
+ * Releases an agent this core spawned, by its exact identity (`spawn.release`).
212
+ * @param client - A greeted connection to the Agent Relay core.
213
+ * @param params - The exact worker identity and stable release key.
214
+ * @param options - Optional timeout and cancellation controls.
215
+ * `actionTimeoutMs` defaults to {@link DEFAULT_SPAWN_ACTION_TIMEOUT_MS} (six
216
+ * minutes), separate from the bridge client's 15-second call deadline.
217
+ * @returns The accepted release invocation receipt.
218
+ */
219
+ export async function spawnRelease(client, params, options = {}) {
220
+ noLegacyCredential(params);
221
+ requireMethod(client, 'spawn.release');
222
+ optionalSegment(params, 'caller_name');
223
+ for (const field of ['node_id', 'node_name', 'name', 'expected_agent_id'])
224
+ segment(params, field);
225
+ key(params, 'idempotency_key');
226
+ return invocationOf(await client.call('spawn.release', pick(params, SPAWN_RELEASE_KEYS), spawnActionOptions(options)));
227
+ }
228
+ function spawnActionOptions(options) {
229
+ const { actionTimeoutMs, ...call } = options;
230
+ return { ...call, timeoutMs: actionTimeoutMs ?? call.timeoutMs ?? DEFAULT_SPAWN_ACTION_TIMEOUT_MS };
231
+ }
232
+ //# sourceMappingURL=spawn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spawn.js","sourceRoot":"","sources":["../src/spawn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAiE5D,4EAA4E;AAC5E,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,SAAS,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,CAAU,CAAC;AAGnG,yFAAyF;AACzF,MAAM,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AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAkB,EAClB,MAA0B,EAC1B,UAA8B,EAAE;IAEhC,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC3B,aAAa,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACvC,eAAe,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACvC,KAAK,MAAM,KAAK,IAAI,CAAC,SAAS,EAAE,WAAW,EAAE,MAAM,EAAE,mBAAmB,CAAC;QAAE,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAClG,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;IAC/B,OAAO,YAAY,CACjB,MAAM,MAAM,CAAC,IAAI,CACf,eAAe,EACf,IAAI,CAAC,MAAM,EAAE,kBAAkB,CAAC,EAChC,kBAAkB,CAAC,OAAO,CAAC,CAC5B,CACF,CAAC;AACJ,CAAC;AAED,SAAS,kBAAkB,CAAC,OAA2B;IACrD,MAAM,EAAE,eAAe,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IAC7C,OAAO,EAAE,GAAG,IAAI,EAAE,SAAS,EAAE,eAAe,IAAI,IAAI,CAAC,SAAS,IAAI,+BAA+B,EAAE,CAAC;AACtG,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Refuse a socket whose directory another user could have planted it in: it
3
+ * must be this user's, and writable by no one else (the core creates it 0700).
4
+ * A symlinked directory passes only when both the link and its target are
5
+ * this user's. Does nothing on Windows or where `process.getuid` is absent.
6
+ * @param socket - The core's bridge socket path
7
+ * @throws CoreUntrustedError with reason `directory`
8
+ */
9
+ export declare function trustedSocketDirectory(socket: string): void;
10
+ /**
11
+ * Refuse a socket the core did not make for this user: the core binds it
12
+ * 0600 and owned by the user it runs as. Node cannot read a Unix socket's
13
+ * peer credentials, so the socket file's owner and mode stand in for them
14
+ * (only its owner, or root, can make or chmod it in a directory only this
15
+ * user can write to). Does nothing on Windows or where `process.getuid` is absent.
16
+ * @param socket - The core's bridge socket path
17
+ * @throws CoreUntrustedError with reason `socket`
18
+ */
19
+ export declare function trustedSocketFile(socket: string): void;
20
+ //# sourceMappingURL=trust.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trust.d.ts","sourceRoot":"","sources":["../src/trust.ts"],"names":[],"mappings":"AAKA;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAY3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAStD"}
package/dist/trust.js ADDED
@@ -0,0 +1,40 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { CoreUntrustedError } from './errors.js';
4
+ /**
5
+ * Refuse a socket whose directory another user could have planted it in: it
6
+ * must be this user's, and writable by no one else (the core creates it 0700).
7
+ * A symlinked directory passes only when both the link and its target are
8
+ * this user's. Does nothing on Windows or where `process.getuid` is absent.
9
+ * @param socket - The core's bridge socket path
10
+ * @throws CoreUntrustedError with reason `directory`
11
+ */
12
+ export function trustedSocketDirectory(socket) {
13
+ if (process.platform === 'win32' || typeof process.getuid !== 'function')
14
+ return;
15
+ const directory = path.dirname(socket);
16
+ const entry = fs.lstatSync(directory);
17
+ const target = fs.statSync(directory);
18
+ const uid = process.getuid();
19
+ if (entry.uid !== uid || target.uid !== uid || !target.isDirectory() || (target.mode & 0o022) !== 0) {
20
+ throw new CoreUntrustedError('directory', `${directory} must be a directory only you can write to; refusing the core socket in it.`);
21
+ }
22
+ }
23
+ /**
24
+ * Refuse a socket the core did not make for this user: the core binds it
25
+ * 0600 and owned by the user it runs as. Node cannot read a Unix socket's
26
+ * peer credentials, so the socket file's owner and mode stand in for them
27
+ * (only its owner, or root, can make or chmod it in a directory only this
28
+ * user can write to). Does nothing on Windows or where `process.getuid` is absent.
29
+ * @param socket - The core's bridge socket path
30
+ * @throws CoreUntrustedError with reason `socket`
31
+ */
32
+ export function trustedSocketFile(socket) {
33
+ if (process.platform === 'win32' || typeof process.getuid !== 'function')
34
+ return;
35
+ const entry = fs.lstatSync(socket);
36
+ if (!entry.isSocket() || entry.uid !== process.getuid() || (entry.mode & 0o077) !== 0) {
37
+ throw new CoreUntrustedError('socket', `${socket} must be a socket only you can use (owned by you, mode 0600); refusing to speak to it.`);
38
+ }
39
+ }
40
+ //# sourceMappingURL=trust.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trust.js","sourceRoot":"","sources":["../src/trust.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAc;IACnD,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU;QAAE,OAAO;IACjF,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACvC,MAAM,KAAK,GAAG,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IACtC,MAAM,MAAM,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;IACtC,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAC7B,IAAI,KAAK,CAAC,GAAG,KAAK,GAAG,IAAI,MAAM,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QACpG,MAAM,IAAI,kBAAkB,CAC1B,WAAW,EACX,GAAG,SAAS,6EAA6E,CAC1F,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAc;IAC9C,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU;QAAE,OAAO;IACjF,MAAM,KAAK,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IACnC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,KAAK,CAAC,GAAG,KAAK,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QACtF,MAAM,IAAI,kBAAkB,CAC1B,QAAQ,EACR,GAAG,MAAM,wFAAwF,CAClG,CAAC;IACJ,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-relay/core-bridge",
3
- "version": "17.2.1",
3
+ "version": "18.1.0",
4
4
  "engines": {
5
5
  "node": ">=22.0.0"
6
6
  },
@@ -20,7 +20,7 @@
20
20
  "README.md"
21
21
  ],
22
22
  "scripts": {
23
- "build": "tsc",
23
+ "build": "tsc && tsc -p tsconfig.type-tests.json",
24
24
  "clean": "rm -rf dist",
25
25
  "test": "vitest run --root ../.. packages/core-bridge/",
26
26
  "test:watch": "vitest --root ../.. packages/core-bridge/"