@hmharness/kernel 0.23.53 → 0.23.55

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/loop.d.ts CHANGED
@@ -65,15 +65,20 @@ export declare function sanitizeToolCalls<T extends {
65
65
  arguments: string;
66
66
  };
67
67
  }>(calls: T[]): T[];
68
+ /** Appended to a final answer the output cap severed (finish_reason 'length'):
69
+ * the one place the user learns the reply is not whole. */
70
+ export declare const TRUNCATED_TAIL = "\n\n[Reply truncated: the model hit its output length limit. Send \"continue\" to have it pick up where it stopped.]";
68
71
  export declare function runLoop(opts: {
69
72
  provider: ProviderConfig;
70
73
  registry: RegistryLike;
71
74
  messages: ChatMessage[];
72
75
  ctx: ToolContext;
73
76
  maxTurns?: number;
74
- /** AbortSignal: when fired, the loop stops at the next turn boundary and
75
- * returns with reason "interrupted". Use for user-initiated cancellation
76
- * (Esc key, /queue skip, web API interrupt). */
77
+ /** AbortSignal: when fired, the loop stops and returns with reason
78
+ * "interrupted" - at the next turn boundary, or right away if the model
79
+ * call is in flight (the call is cut and the stop is honoured at once).
80
+ * Use for user-initiated cancellation (Esc key, /queue skip, web API
81
+ * interrupt). */
77
82
  signal?: AbortSignal;
78
83
  /** Explicit transcript budget override (chars). Default: scales with the
79
84
  * model's context window (window.ts registry / provider.contextWindow). */
package/dist/loop.js CHANGED
@@ -41,6 +41,9 @@ export function sanitizeToolCalls(calls) {
41
41
  }
42
42
  });
43
43
  }
44
+ /** Appended to a final answer the output cap severed (finish_reason 'length'):
45
+ * the one place the user learns the reply is not whole. */
46
+ export const TRUNCATED_TAIL = '\n\n[Reply truncated: the model hit its output length limit. Send "continue" to have it pick up where it stopped.]';
44
47
  export async function runLoop(opts) {
45
48
  const { provider, registry, ctx, events } = opts;
46
49
  const modelCall = opts.chatImpl ?? chat;
@@ -130,12 +133,27 @@ export async function runLoop(opts) {
130
133
  content: `[turn checkpoint] You have been working for ${turn} turns. If the task is substantially complete, give your final answer. If not, continue — there is no turn limit; work until done.`,
131
134
  });
132
135
  }
133
- const chatRes = await modelCall(provider, compacted, tools, {
134
- onDelta: events?.onDelta,
135
- // the abort must pierce the in-flight call, not just turn boundaries -
136
- // a hung provider stream otherwise holds the session busy forever
137
- ...(opts.signal ? { signal: opts.signal } : {}),
138
- });
136
+ let chatRes;
137
+ try {
138
+ chatRes = await modelCall(provider, compacted, tools, {
139
+ onDelta: events?.onDelta,
140
+ // the abort must pierce the in-flight call, not just turn boundaries -
141
+ // a hung provider stream otherwise holds the session busy forever
142
+ ...(opts.signal ? { signal: opts.signal } : {}),
143
+ });
144
+ }
145
+ catch (err) {
146
+ // The abort pierces this call by design, so a stop that lands while the model
147
+ // is streaming arrives HERE as a rejected request. Until 0.23.55 it escaped the
148
+ // loop as a provider failure: the web UI showed "provider: request aborted …
149
+ // task child exit 1" for a stop the user had pressed on purpose. It is the same
150
+ // stop as one noticed at a turn boundary - keep what was built and say so.
151
+ if (opts.signal?.aborted) {
152
+ reason = 'interrupted';
153
+ break;
154
+ }
155
+ throw err;
156
+ }
139
157
  usage.promptTokens += chatRes.usage?.prompt_tokens ?? 0;
140
158
  usage.completionTokens += chatRes.usage?.completion_tokens ?? 0;
141
159
  // Usage fallback: many OpenAI-compatible gateways never report usage
@@ -154,7 +172,7 @@ export async function runLoop(opts) {
154
172
  events?.onAssistant?.(message);
155
173
  const calls = message.tool_calls ?? [];
156
174
  if (calls.length === 0) {
157
- const text = message.content ?? '';
175
+ let text = message.content ?? '';
158
176
  // A runtime-steering injection may have arrived while this turn was in
159
177
  // flight; honor it by continuing instead of ending (codex Enter while
160
178
  // the last answer streams). Only end when the queue is empty.
@@ -165,6 +183,17 @@ export async function runLoop(opts) {
165
183
  world++;
166
184
  continue;
167
185
  }
186
+ // The output cap cut the answer mid-sentence (finish_reason 'length').
187
+ // Nothing else on the wire says so, and until 0.23.55 the severed text
188
+ // reached every surface dressed as a complete reply. The marker travels
189
+ // as a text delta: the web UI, the TUI and the one-shot task render the
190
+ // STREAM and never the returned text, so a tail appended to the return
191
+ // value alone was seen nowhere. This also keeps the return equal to what
192
+ // was streamed. The stored transcript keeps the raw message.
193
+ if (chatRes.finishReason === 'length') {
194
+ events?.onDelta?.('text', TRUNCATED_TAIL);
195
+ text += TRUNCATED_TAIL;
196
+ }
168
197
  events?.onFinal?.(text, turn);
169
198
  return { text, turns: turn, toolUses, messages: working, usage, reason: 'final' };
170
199
  }
@@ -209,9 +238,11 @@ export async function runLoop(opts) {
209
238
  p.skip = true;
210
239
  }
211
240
  else if (badArgs) {
212
- p.output = rawArgs
213
- ? `unparseable tool arguments for ${name} (arguments must be valid JSON — repeat the call with complete JSON): ${rawArgs.slice(0, 200)}`
214
- : `empty tool arguments for ${name} (arguments must be valid JSON — repeat the call with complete JSON)`;
241
+ p.output = chatRes.finishReason === 'length'
242
+ ? `the ${name} call was cut off by the model's output length limit before its JSON arguments were complete. Repeat it with a much smaller payload (split a long file into several smaller writes).`
243
+ : rawArgs
244
+ ? `unparseable tool arguments for ${name} (arguments must be valid JSON — repeat the call with complete JSON): ${rawArgs.slice(0, 200)}`
245
+ : `empty tool arguments for ${name} (arguments must be valid JSON — repeat the call with complete JSON)`;
215
246
  p.isError = true;
216
247
  p.skip = true;
217
248
  }
@@ -13,6 +13,12 @@ export interface ChatResponse {
13
13
  prompt_tokens?: number;
14
14
  completion_tokens?: number;
15
15
  };
16
+ /** Why the model stopped, as the provider reports it: 'stop' (natural end),
17
+ * 'length' (the output cap CUT the reply or a tool call's JSON mid-way),
18
+ * 'content_filter', 'tool_calls', ... Absent when the provider reports none.
19
+ * Until 0.23.55 both parse paths dropped it, so a truncated reply reached
20
+ * every caller dressed as a complete one. */
21
+ finishReason?: string;
16
22
  }
17
23
  export type DeltaKind = 'text' | 'reasoning' | 'reset';
18
24
  /** Permanent provider-side auth/payment failures: 401 (bad key), 402
package/dist/provider.js CHANGED
@@ -148,6 +148,7 @@ export async function chat(cfg, messages, tools, opts = {}) {
148
148
  ...(choice.message?.tool_calls ? { tool_calls: choice.message.tool_calls } : {}),
149
149
  },
150
150
  usage: data.usage,
151
+ ...(choice.finish_reason ? { finishReason: String(choice.finish_reason) } : {}),
151
152
  };
152
153
  }
153
154
  catch (err) {
@@ -186,6 +187,7 @@ async function consumeStream(res, onDelta) {
186
187
  let text = '';
187
188
  let reasoning = '';
188
189
  let usage;
190
+ let finishReason;
189
191
  // tool_calls accumulate across deltas, keyed by their index in the stream.
190
192
  const calls = new Map();
191
193
  // Idle guard rather than a total cap: generation length is unbounded.
@@ -223,6 +225,11 @@ async function consumeStream(res, onDelta) {
223
225
  }
224
226
  if (chunk.usage)
225
227
  usage = chunk.usage;
228
+ // the reason rides the LAST choice - usually one whose delta is empty,
229
+ // so it has to be read before the empty-delta skip below
230
+ const reason = chunk.choices?.[0]?.finish_reason;
231
+ if (reason)
232
+ finishReason = String(reason);
226
233
  const delta = chunk.choices?.[0]?.delta;
227
234
  if (!delta)
228
235
  continue;
@@ -274,6 +281,7 @@ async function consumeStream(res, onDelta) {
274
281
  ...(ordered.length > 0 ? { tool_calls: ordered } : {}),
275
282
  },
276
283
  usage,
284
+ ...(finishReason ? { finishReason } : {}),
277
285
  };
278
286
  }
279
287
  /** Accept bases with any /vN suffix (v1 OpenAI convention, v4 zhipu coding
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/kernel",
3
- "version": "0.23.53",
3
+ "version": "0.23.55",
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",