@hmharness/kernel 0.23.50 → 0.23.52
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.js +23 -7
- package/dist/types.d.ts +8 -0
- package/package.json +1 -1
package/dist/loop.js
CHANGED
|
@@ -66,7 +66,14 @@ export async function runLoop(opts) {
|
|
|
66
66
|
// text conclusions). Ring of recent (tool+args) → result head; an EXACT
|
|
67
67
|
// repeat returns the cached result with a note, saving a network call and
|
|
68
68
|
// telling the model it already knows this.
|
|
69
|
+
//
|
|
70
|
+
// 2026-11-25: the ring used to cache EVERY successful call and nothing
|
|
71
|
+
// invalidated it - so "edit → re-run the same command" (the core agent
|
|
72
|
+
// loop) got the PRE-edit result back. Only readOnly tools are cached now,
|
|
73
|
+
// and only while `world` is unchanged: every state-changing execution and
|
|
74
|
+
// every runtime injection bumps it.
|
|
69
75
|
const recentCalls = new Map();
|
|
76
|
+
let world = 0;
|
|
70
77
|
const callKey = (name, args) => {
|
|
71
78
|
try {
|
|
72
79
|
return name + ':' + JSON.stringify(args);
|
|
@@ -101,6 +108,7 @@ export async function runLoop(opts) {
|
|
|
101
108
|
for (const inj of opts.injections?.poll() ?? []) {
|
|
102
109
|
working.push({ role: 'user', content: inj.text });
|
|
103
110
|
events?.onInjected?.(inj.text);
|
|
111
|
+
world++; // the user acted out of band: whatever was read before may be gone
|
|
104
112
|
}
|
|
105
113
|
const compacted = opts.summarizeContext
|
|
106
114
|
? await compactWithDigest(working, budget, opts.summarizeContext)
|
|
@@ -154,6 +162,7 @@ export async function runLoop(opts) {
|
|
|
154
162
|
if (pending) {
|
|
155
163
|
working.push({ role: 'user', content: pending });
|
|
156
164
|
events?.onInjected?.(pending);
|
|
165
|
+
world++;
|
|
157
166
|
continue;
|
|
158
167
|
}
|
|
159
168
|
events?.onFinal?.(text, turn);
|
|
@@ -238,15 +247,21 @@ export async function runLoop(opts) {
|
|
|
238
247
|
}
|
|
239
248
|
planned.push(p);
|
|
240
249
|
}
|
|
250
|
+
// A batch that contains any state-changing call leaves every cached read
|
|
251
|
+
// behind: bump the world BEFORE the cache lookups of this batch.
|
|
252
|
+
for (const p of planned)
|
|
253
|
+
if (!p.skip && !registry.get(p.name).readOnly)
|
|
254
|
+
world++;
|
|
241
255
|
await Promise.all(planned.map(async (p) => {
|
|
242
256
|
if (p.skip)
|
|
243
257
|
return;
|
|
244
258
|
// Re-verification breaker: an EXACT repeat (same tool, same args) of a
|
|
245
|
-
// recent successful call returns the cached head instead of
|
|
246
|
-
//
|
|
259
|
+
// recent successful readOnly call returns the cached head instead of
|
|
260
|
+
// re-running - but only if nothing has changed the world since. The note
|
|
261
|
+
// tells the model WHY, so it stops trying to re-verify.
|
|
247
262
|
const key = callKey(p.name, p.args);
|
|
248
263
|
const prev = recentCalls.get(key);
|
|
249
|
-
if (prev && !prev.isError && turn - prev.turn <= 20) {
|
|
264
|
+
if (registry.get(p.name).readOnly === true && prev && !prev.isError && prev.world === world && turn - prev.turn <= 20) {
|
|
250
265
|
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}`;
|
|
251
266
|
p.isError = false;
|
|
252
267
|
return;
|
|
@@ -255,10 +270,10 @@ export async function runLoop(opts) {
|
|
|
255
270
|
const r = await registry.get(p.name).execute(p.args, ctx);
|
|
256
271
|
p.output = r.output;
|
|
257
272
|
p.isError = r.isError === true;
|
|
258
|
-
// remember successful results for the dedup ring (errors go
|
|
259
|
-
// the failed-command short-circuit instead)
|
|
260
|
-
if (!p.isError) {
|
|
261
|
-
recentCalls.set(key, { head: p.output.slice(0, RESULT_HEAD_CHARS), isError: false, turn });
|
|
273
|
+
// remember successful READ-ONLY results for the dedup ring (errors go
|
|
274
|
+
// through the failed-command short-circuit instead)
|
|
275
|
+
if (!p.isError && registry.get(p.name).readOnly === true) {
|
|
276
|
+
recentCalls.set(key, { head: p.output.slice(0, RESULT_HEAD_CHARS), isError: false, turn, world });
|
|
262
277
|
if (recentCalls.size > 50) {
|
|
263
278
|
// drop the oldest entries when the ring fills
|
|
264
279
|
const firstKey = recentCalls.keys().next().value;
|
|
@@ -289,6 +304,7 @@ export async function runLoop(opts) {
|
|
|
289
304
|
if (injected) {
|
|
290
305
|
working.push({ role: 'user', content: injected });
|
|
291
306
|
events?.onInjected?.(injected);
|
|
307
|
+
world++; // the user acted out of band: whatever was read before may be gone
|
|
292
308
|
}
|
|
293
309
|
// Idle detection: count consecutive turns where NO tool succeeded. A
|
|
294
310
|
// productive agent always has at least one successful call; N consecutive
|
package/dist/types.d.ts
CHANGED
|
@@ -29,6 +29,14 @@ export interface Tool {
|
|
|
29
29
|
* approval unless their server is marked trusted.
|
|
30
30
|
*/
|
|
31
31
|
needsApproval?(args: Record<string, unknown>, ctx?: ToolContext): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Read-only: the result depends only on (args, world state) and executing
|
|
34
|
+
* it never changes world state (read_file, list_dir). The loop's repeat
|
|
35
|
+
* dedup serves a cached result ONLY for readOnly tools, and only while no
|
|
36
|
+
* state-changing call has run since. Absent = state-changing: an exact
|
|
37
|
+
* repeat always executes for real.
|
|
38
|
+
*/
|
|
39
|
+
readOnly?: boolean;
|
|
32
40
|
execute(args: Record<string, unknown>, ctx: ToolContext): Promise<ToolResult>;
|
|
33
41
|
}
|
|
34
42
|
/** OpenAI-style chat message, reused across provider adapters. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/kernel",
|
|
3
|
-
"version": "0.23.
|
|
3
|
+
"version": "0.23.52",
|
|
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",
|