@hmharness/kernel 0.23.30 → 0.23.32

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/context.d.ts CHANGED
@@ -15,6 +15,15 @@
15
15
  import type { ChatMessage } from './types.js';
16
16
  export declare const DEFAULT_CONTEXT_CHARS = 160000;
17
17
  export declare function transcriptChars(messages: ChatMessage[]): number;
18
+ /** A pruned tool result keeps its HEAD - the first line(s) of a command
19
+ * output usually contain the finding ("Inno @ 722596", "z-ai provider
20
+ * found"). A zero-information "[context pruned]" tombstone forces the model
21
+ * to re-derive facts it already had, which is the #1 driver of the
22
+ * re-verification loop observed in long sessions (2026-10-07 export:
23
+ * 108 tool calls, 20+ re-verification turns on facts the model already
24
+ * knew). The headstone carries enough signal to say "I already know this"
25
+ * without carrying the full dump. */
26
+ export declare const HEADSTONE_CHARS = 200;
18
27
  export declare const DIGEST_MARK = "[rolling digest of earlier context]";
19
28
  export declare function compactMessages(messages: ChatMessage[], budget?: number): ChatMessage[];
20
29
  export interface CompactResult {
package/dist/context.js CHANGED
@@ -18,6 +18,19 @@ function protectedRange(messages) {
18
18
  keep.add(i);
19
19
  return keep;
20
20
  }
21
+ /** A pruned tool result keeps its HEAD - the first line(s) of a command
22
+ * output usually contain the finding ("Inno @ 722596", "z-ai provider
23
+ * found"). A zero-information "[context pruned]" tombstone forces the model
24
+ * to re-derive facts it already had, which is the #1 driver of the
25
+ * re-verification loop observed in long sessions (2026-10-07 export:
26
+ * 108 tool calls, 20+ re-verification turns on facts the model already
27
+ * knew). The headstone carries enough signal to say "I already know this"
28
+ * without carrying the full dump. */
29
+ export const HEADSTONE_CHARS = 200;
30
+ function headstone(original) {
31
+ const head = original.slice(0, HEADSTONE_CHARS).replace(/\s+$/, '');
32
+ return `[earlier tool output, pruned to fit budget — head preserved]\n${head}${original.length > HEADSTONE_CHARS ? '\n…' : ''}`;
33
+ }
21
34
  export const DIGEST_MARK = '[rolling digest of earlier context]';
22
35
  function findDigest(messages) {
23
36
  return messages.findIndex((m) => m.role === 'system' && typeof m.content === 'string' && m.content.startsWith(DIGEST_MARK));
@@ -30,7 +43,7 @@ export function compactMessages(messages, budget = DEFAULT_CONTEXT_CHARS) {
30
43
  for (let i = 0; i < out.length && transcriptChars(out) > budget; i++) {
31
44
  if (keep.has(i) || out[i].role !== 'tool')
32
45
  continue;
33
- out[i] = { ...out[i], content: '[context pruned: earlier tool output removed to fit budget]' };
46
+ out[i] = { ...out[i], content: headstone(String(out[i].content ?? '')) };
34
47
  }
35
48
  return out;
36
49
  }
@@ -46,7 +59,7 @@ export function compactWithEvictions(messages, budget = DEFAULT_CONTEXT_CHARS) {
46
59
  if (keep.has(i) || out[i].role !== 'tool')
47
60
  continue;
48
61
  evicted += out[i].content?.length ?? 0;
49
- out[i] = { ...out[i], content: '[context pruned: earlier tool output removed to fit budget]' };
62
+ out[i] = { ...out[i], content: headstone(String(out[i].content ?? '')) };
50
63
  }
51
64
  return { messages: out, evictedChars: evicted };
52
65
  }
@@ -66,7 +79,8 @@ export async function compactWithDigest(messages, budget, summarize) {
66
79
  const evicted = [];
67
80
  // what was evicted = tombstoned positions vs the input (compare by index)
68
81
  for (let i = 0; i < pruned.length; i++) {
69
- if (pruned[i].role === 'tool' && pruned[i].content === '[context pruned: earlier tool output removed to fit budget]'
82
+ if (pruned[i].role === 'tool'
83
+ && String(pruned[i].content ?? '').includes('pruned to fit budget')
70
84
  && messages[i]?.role === 'tool' && messages[i].content !== pruned[i].content) {
71
85
  evicted.push(String(messages[i].content).slice(0, 4000));
72
86
  }
package/dist/loop.js CHANGED
@@ -29,6 +29,22 @@ export async function runLoop(opts) {
29
29
  let turn = 0;
30
30
  let idleTurns = 0; // consecutive turns with no successful tool calls
31
31
  let reason = 'final';
32
+ // Re-verification loop breaker (2026-10-07 export: 108 tool calls in 5
33
+ // user turns, 20+ turns re-deriving the SAME fact because the earlier tool
34
+ // result got tombstoned by compaction and the model doesn't trust its own
35
+ // text conclusions). Ring of recent (tool+args) → result head; an EXACT
36
+ // repeat returns the cached result with a note, saving a network call and
37
+ // telling the model it already knows this.
38
+ const recentCalls = new Map();
39
+ const callKey = (name, args) => {
40
+ try {
41
+ return name + ':' + JSON.stringify(args);
42
+ }
43
+ catch {
44
+ return name + ':unserializable';
45
+ }
46
+ };
47
+ const RESULT_HEAD_CHARS = 500;
32
48
  // The loop runs until the model gives a final answer (no tool calls), goes
33
49
  // idle (stuck), or hits a safety valve. Soft checkpoints at the adaptive
34
50
  // turn limit nudge the model but don't stop it — days-long tasks run
@@ -149,10 +165,31 @@ export async function runLoop(opts) {
149
165
  await Promise.all(planned.map(async (p) => {
150
166
  if (p.skip)
151
167
  return;
168
+ // Re-verification breaker: an EXACT repeat (same tool, same args) of a
169
+ // recent successful call returns the cached head instead of re-running.
170
+ // The note tells the model WHY, so it stops trying to re-verify.
171
+ const key = callKey(p.name, p.args);
172
+ const prev = recentCalls.get(key);
173
+ if (prev && !prev.isError && turn - prev.turn <= 20) {
174
+ p.output = `[you already ran ${p.name} with these exact arguments at turn ${prev.turn}; re-running identical calls wastes time — here is what it returned]\n${prev.head}`;
175
+ p.isError = false;
176
+ return;
177
+ }
152
178
  try {
153
179
  const r = await registry.get(p.name).execute(p.args, ctx);
154
180
  p.output = r.output;
155
181
  p.isError = r.isError === true;
182
+ // remember successful results for the dedup ring (errors go through
183
+ // the failed-command short-circuit instead)
184
+ if (!p.isError) {
185
+ recentCalls.set(key, { head: p.output.slice(0, RESULT_HEAD_CHARS), isError: false, turn });
186
+ if (recentCalls.size > 50) {
187
+ // drop the oldest entries when the ring fills
188
+ const firstKey = recentCalls.keys().next().value;
189
+ if (firstKey !== undefined)
190
+ recentCalls.delete(firstKey);
191
+ }
192
+ }
156
193
  }
157
194
  catch (err) {
158
195
  p.output = String(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/kernel",
3
- "version": "0.23.30",
3
+ "version": "0.23.32",
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",