feat: persist the claim ledger with an inverted SM-2 interval (#27)
feat: persist the claim ledger with an inverted SM-2 interval (#27) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #27.
This commit is contained in:
@@ -144,6 +144,44 @@ function check(turn: Turn): Promise<Finding[]> {
|
||||
// Guard against overlapping sweeps when prompts arrive quickly.
|
||||
let refreshing = false
|
||||
|
||||
// The context hook fires for every model call, including tool continuations, so
|
||||
// the same prose would be recorded again and again and inflate reuse_count.
|
||||
// Remember what was last recorded per session and only record on change.
|
||||
const recordedProse = new Map<string, string>()
|
||||
|
||||
/** Persist verdicts into the ledger (fire-and-forget). */
|
||||
function record(sessionID: string, prose: string, findings: Finding[]): void {
|
||||
if (findings.length === 0) return
|
||||
const seen = recordedProse.get(sessionID)
|
||||
if (seen === prose) return
|
||||
recordedProse.set(sessionID, prose)
|
||||
|
||||
const child = execFile(BIN, ["claims", "record"], { timeout: INGEST_TIMEOUT_MS }, () => {})
|
||||
child.stdin?.end(JSON.stringify({ findings }))
|
||||
}
|
||||
|
||||
type DueClaim = { Assertion: string; ReuseCount: number; VerifyCount: number }
|
||||
|
||||
/** Claims the ledger says are overdue for a re-check. */
|
||||
function dueClaims(): Promise<DueClaim[]> {
|
||||
return new Promise((resolve) => {
|
||||
execFile(
|
||||
BIN,
|
||||
["claims", "due", "--json", "--limit", "3"],
|
||||
{ timeout: TIMEOUT_MS, maxBuffer: 1 << 20 },
|
||||
(err, stdout) => {
|
||||
if (err) return resolve([])
|
||||
try {
|
||||
const parsed = JSON.parse(stdout)
|
||||
resolve(Array.isArray(parsed) ? parsed : [])
|
||||
} catch {
|
||||
resolve([])
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-index opencode sessions in the background.
|
||||
*
|
||||
@@ -169,12 +207,38 @@ export default {
|
||||
refreshSessions()
|
||||
})
|
||||
|
||||
// Turn-start digest from the ledger: claims this machine keeps restating
|
||||
// without ever re-checking. Cross-session, which the per-message warning
|
||||
// cannot be.
|
||||
await ctx.session.hook("context", async (event: any) => {
|
||||
try {
|
||||
const messages = (event.messages ?? []) as any[]
|
||||
const last = messages[messages.length - 1]
|
||||
if (roleOf(last) !== "user") return
|
||||
const due = await dueClaims()
|
||||
if (due.length === 0) return
|
||||
const list = due
|
||||
.map((d) => `"${d.Assertion}" (restated ${d.ReuseCount}x since last check)`)
|
||||
.join("; ")
|
||||
event.system.push({
|
||||
type: "text",
|
||||
text:
|
||||
`Claims in the ledger that need re-checking before reuse: ${list}. ` +
|
||||
`Re-measure or state where the figure came from.`,
|
||||
})
|
||||
} catch {
|
||||
/* a digest must never break the turn */
|
||||
}
|
||||
})
|
||||
|
||||
await ctx.session.hook("context", async (event: any) => {
|
||||
try {
|
||||
const turn = splitTurns(event.messages as any[])
|
||||
if (!turn.prose) return
|
||||
|
||||
const findings = await check(turn)
|
||||
record(String(event.sessionID ?? "unknown"), turn.prose, findings)
|
||||
|
||||
// Only stale claims: "unsupported" is usually a hypothetical, and
|
||||
// warning on those trains the reader to ignore the warning.
|
||||
const stale = findings.filter((f) => f.status === "stale")
|
||||
|
||||
Reference in New Issue
Block a user