feat(kb): wire kb-block fade into before_prompt_build
The previous commit shipped renderFaded + tick + seed on Block but left them dead code because openclaw's `before_prompt_build` hook ctx doesn't carry a currentTurn signal — the hook called plain render() and turnFor() returned 0, so add() always stamped last_refresh_at_turn=0 and fade distance never moved. This adds a small per-session turn tracker (tools/turn-tracker.ts) that the hook bumps each fire. On the first bump after plugin start the counter is seeded from max(entry.last_refresh_at_turn) on the loaded block, so a restart doesn't regress accumulated fade for surviving entries. dynamic-kb-cache now reads currentTurnForSession via the same counter so a fact cached on turn N reads back at d=0 on turn N+1. The hook now: bumps turn, ticks (drop entries past the m% threshold, log + save when anything drops), and renderFaded() into appendSystemContext. Same algorithm as the Plexum-side RenderDynamicSubblock path. KBDeps.turnFor signature changed from (agentId) → (sessionId) since turn is session-scoped — fixed the one cache-tool call site. Verified end-to-end with a standalone smoke replaying the full hook+tool flow against a temp profile: stale entry (d=201) drops on turn 1, fresh entry (d=1) survives untouched, restart re-seeds the counter from the surviving entry's last_refresh.
This commit is contained in:
@@ -39,8 +39,13 @@ export interface KBDeps {
|
||||
tokenFor: ((agentId: string) => Promise<string | null>) | null;
|
||||
/** Build new client given a token (so each call gets fresh per-agent client). */
|
||||
makeClient: ((token: string) => KBClient) | null;
|
||||
/** Best-effort current turn number; 0 is acceptable. */
|
||||
turnFor: (agentId: string) => number;
|
||||
/**
|
||||
* Best-effort current turn number for a session; 0 is acceptable.
|
||||
* Wired against the same per-session counter that the
|
||||
* before_prompt_build hook bumps, so a fact added during turn N
|
||||
* reads back at d=0 on turn N+1.
|
||||
*/
|
||||
turnFor: (sessionId: string) => number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,7 +216,7 @@ export function createCacheTool(deps: KBDeps) {
|
||||
return err('dynamic-kb-cache: ' + (e?.message || String(e)));
|
||||
}
|
||||
}
|
||||
const turn = deps.turnFor(ctx.agentId);
|
||||
const turn = deps.turnFor(ctx.sessionId);
|
||||
const fetchedByID = new Map(fetched.map((f) => [f.id, f]));
|
||||
const added: number[] = [];
|
||||
for (const id of toFetch) {
|
||||
|
||||
Reference in New Issue
Block a user