@hmharness/kernel 0.23.51 → 0.23.53

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.
@@ -73,7 +73,7 @@ export class CapabilityRegistry {
73
73
  capabilities = new Map();
74
74
  rules;
75
75
  constructor(rules = DEFAULT_POLICY_RULES) {
76
- this.rules = [...rules].sort((a, b) => a.priority - b.priority);
76
+ this.rules = rules.toSorted((a, b) => a.priority - b.priority);
77
77
  }
78
78
  /** Register a capability. Duplicate ids are rejected (supply-chain safety). */
79
79
  register(manifest) {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @hmharness/kernel - deadline
3
+ *
4
+ * A deadline signal for a configured timeout value — the declarative form of
5
+ * `const ctrl = new AbortController(); setTimeout(() => ctrl.abort(), ms)`,
6
+ * with no timer to clear in a `finally`.
7
+ *
8
+ * Values a hand-edited config.json can hold are coerced the way `setTimeout`
9
+ * used to coerce them (NaN, negative and non-numeric values become an
10
+ * immediate deadline; values beyond the platform range are clamped): left
11
+ * raw they make `AbortSignal.timeout()` throw instead.
12
+ */
13
+ export declare function deadlineSignal(ms: number): AbortSignal;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @hmharness/kernel - deadline
3
+ *
4
+ * A deadline signal for a configured timeout value — the declarative form of
5
+ * `const ctrl = new AbortController(); setTimeout(() => ctrl.abort(), ms)`,
6
+ * with no timer to clear in a `finally`.
7
+ *
8
+ * Values a hand-edited config.json can hold are coerced the way `setTimeout`
9
+ * used to coerce them (NaN, negative and non-numeric values become an
10
+ * immediate deadline; values beyond the platform range are clamped): left
11
+ * raw they make `AbortSignal.timeout()` throw instead.
12
+ */
13
+ const MAX_TIMEOUT_MS = 0xFFFF_FFFF; // WebIDL unsigned long: the largest delay AbortSignal.timeout accepts
14
+ export function deadlineSignal(ms) {
15
+ const n = Number(ms);
16
+ return AbortSignal.timeout(Number.isFinite(n) && n > 0 ? Math.min(Math.floor(n), MAX_TIMEOUT_MS) : 0);
17
+ }
@@ -93,7 +93,7 @@ function nodeFetch(url, init) {
93
93
  target = new URL(url);
94
94
  }
95
95
  catch (err) {
96
- reject(new Error(`fetch failed invalid URL: ${String(err)}`));
96
+ reject(new Error(`fetch failed invalid URL: ${String(err)}`, { cause: err }));
97
97
  return;
98
98
  }
99
99
  const secure = target.protocol === 'https:';
package/dist/index.d.ts CHANGED
@@ -14,3 +14,4 @@ export * from './goal.js';
14
14
  export * from './mcp2026.js';
15
15
  export * from './adaptive-router.js';
16
16
  export * from './qrcode.js';
17
+ export * from './deadline.js';
package/dist/index.js CHANGED
@@ -14,3 +14,4 @@ export * from "./goal.js";
14
14
  export * from "./mcp2026.js";
15
15
  export * from "./adaptive-router.js";
16
16
  export * from "./qrcode.js";
17
+ export * from "./deadline.js";
package/dist/mcp.js CHANGED
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { spawn } from 'node:child_process';
10
10
  import { httpFetch, getReader } from "./http-transport.js";
11
+ import { deadlineSignal } from "./deadline.js";
11
12
  const PROTOCOL_VERSION = '2025-06-18';
12
13
  const CLIENT_INFO = { name: 'hmharness', version: '0.1.0' };
13
14
  /** Call-local incremental id; the wire only needs uniqueness per session. */
@@ -164,8 +165,8 @@ export class McpClient {
164
165
  };
165
166
  if (this.sessionId)
166
167
  headers['Mcp-Session-Id'] = this.sessionId;
167
- const ctrl = new AbortController();
168
- const timer = setTimeout(() => ctrl.abort(), timeoutMs);
168
+ // deadline covers the POST and the body read alike; nothing to clear
169
+ const signal = deadlineSignal(timeoutMs);
169
170
  try {
170
171
  // one retry for transient transport failures: a dead/restarting MCP
171
172
  // server used to surface a bare "TypeError: fetch failed" straight to
@@ -173,7 +174,7 @@ export class McpClient {
173
174
  let res;
174
175
  for (let attempt = 0; attempt < 2; attempt++) {
175
176
  try {
176
- res = await httpFetch(cfg.url, { method: 'POST', headers, body: JSON.stringify(payload), signal: ctrl.signal });
177
+ res = await httpFetch(cfg.url, { method: 'POST', headers, body: JSON.stringify(payload), signal });
177
178
  break;
178
179
  }
179
180
  catch (err) {
@@ -208,10 +209,9 @@ export class McpClient {
208
209
  const hint = isTransientTransport(msg)
209
210
  ? ` - the MCP server at ${cfg.url} is unreachable or cut the connection`
210
211
  : '';
211
- throw new Error(`mcp/${this.serverName}: request failed (${msg})${hint}`);
212
- }
213
- finally {
214
- clearTimeout(timer);
212
+ // message unchanged (the model sees String(err): annotation + hint only); the
213
+ // transport error is chained for logs and programmatic callers
214
+ throw new Error(`mcp/${this.serverName}: request failed (${msg})${hint}`, { cause: err });
215
215
  }
216
216
  }
217
217
  /** Read an SSE body up to the first `data:` JSON message, then stop. */
package/dist/provider.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { httpFetch, getReader } from "./http-transport.js";
2
+ import { deadlineSignal } from "./deadline.js";
2
3
  /** Transient failures worth retrying. `terminated` is undici's TypeError for
3
4
  * a socket cut mid-body (gateway dropped a long stream, proxy reset, server
4
5
  * restart) - the case that used to kill the whole task with a raw
@@ -304,8 +305,9 @@ export async function chatVision(cfg, prompt, imageDataUrl, opts = {}) {
304
305
  // same auth negotiation as chat(): explicit header, Bearer, then X-Api-Key
305
306
  let vAuth = cfg.authHeader ?? 'bearer';
306
307
  for (let attempt = 0; attempt < 2; attempt++) {
307
- const ctrl = new AbortController();
308
- const timer = setTimeout(() => ctrl.abort(), opts.timeoutMs ?? cfg.timeoutMs ?? 120_000);
308
+ // one deadline per attempt, nothing to clear in a finally; a timeout now
309
+ // surfaces as TimeoutError ("...aborted due to timeout") instead of AbortError
310
+ const signal = deadlineSignal(opts.timeoutMs ?? cfg.timeoutMs ?? 120_000);
309
311
  try {
310
312
  const vHeaders = vAuth === 'bearer'
311
313
  ? { 'Content-Type': 'application/json', Authorization: `Bearer ${cfg.apiKey}` }
@@ -314,7 +316,7 @@ export async function chatVision(cfg, prompt, imageDataUrl, opts = {}) {
314
316
  method: 'POST',
315
317
  headers: vHeaders,
316
318
  body: JSON.stringify(body),
317
- signal: ctrl.signal,
319
+ signal,
318
320
  });
319
321
  if (res.status === 401 && !cfg.authHeader && vAuth === 'bearer' && cfg.apiKey) {
320
322
  vAuth = 'X-Api-Key';
@@ -340,9 +342,6 @@ export async function chatVision(cfg, prompt, imageDataUrl, opts = {}) {
340
342
  throw err;
341
343
  await sleep(1500 * (attempt + 1));
342
344
  }
343
- finally {
344
- clearTimeout(timer);
345
- }
346
345
  }
347
346
  throw new Error(`provider: vision call failed after retry (${cfg.baseUrl}): ${lastError}`);
348
347
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/kernel",
3
- "version": "0.23.51",
3
+ "version": "0.23.53",
4
4
  "description": "hmharness kernel: tool registry, provider adapters, the agent loop, session log, config. Zero runtime dependencies (Node >=22 native fetch).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",