feat(memory): capture Bash failures, redacted and bounded, per project and session
This commit is contained in:
1 parent
cc1da66fd4
commit
6072522558
2 files changed
+565
-30
No files matched your search
@@ -73,6 +73,225 @@ export function normalize(line: string): string {
|
||||
.trim()
|
||||
}
|
||||
|
||||
// The store: one key per project root, at most MAX_SESSIONS sessions each.
|
||||
type Failure = {
|
||||
key: string // normalize(line): what makes two failures the same
|
||||
line: string
|
||||
excerpt: string
|
||||
command: string
|
||||
agent: boolean
|
||||
at: number
|
||||
count: number
|
||||
}
|
||||
type Milestone = {
|
||||
kind: 'commit' | 'finish' | 'green'
|
||||
label: string
|
||||
at: number
|
||||
}
|
||||
type Session = {
|
||||
id: string
|
||||
at: number // last activity
|
||||
failures: Failure[]
|
||||
milestones: Milestone[]
|
||||
red: string[]
|
||||
}
|
||||
type Project = { sessions: Session[] }
|
||||
|
||||
const STORE_PREFIX = 'p:'
|
||||
const MAX_ROOT = 400
|
||||
const MAX_SESSIONS = 3
|
||||
const MAX_FAILURES = 50
|
||||
const MAX_MILESTONES = 20
|
||||
const MAX_RED = 10
|
||||
const MAX_RED_LEN = 300
|
||||
const MAX_LINE = 300
|
||||
const MAX_COMMAND = 300
|
||||
const EXCERPT_LEN = 600
|
||||
const MAX_ENTRY = 2048
|
||||
const STORE_CAP = 1048576
|
||||
const LINE_WINDOW = 1000 // bounds the trailer regex on one long line
|
||||
|
||||
const EXIT_CODE = /^Exit code \d+/
|
||||
const RTK_TRAILER =
|
||||
/\s*\[(?:\+\d+ hidden|full output): rtk recall [0-9a-f]+\]$/
|
||||
|
||||
/**
|
||||
* The line that tells what failed, from an already redacted answer: the
|
||||
* last line after the first of an `Exit code N` text. `undefined` when the
|
||||
* text is another kind, has no such line, or the line holds no letter.
|
||||
*/
|
||||
export function failureLine(text: string): string | undefined {
|
||||
if (!EXIT_CODE.test(text)) return undefined
|
||||
const lines = text.split('\n')
|
||||
.map(l => l.slice(-LINE_WINDOW).replace(RTK_TRAILER, '').trim())
|
||||
.filter(l => l !== '')
|
||||
if (lines.length < 2) return undefined
|
||||
const line = (lines[lines.length - 1] ?? '').slice(0, MAX_LINE)
|
||||
const letters = normalize(line).replace(/<path>|<hex>|#/g, '')
|
||||
return /\p{L}/u.test(letters) ? line : undefined
|
||||
}
|
||||
|
||||
function isSession(v: unknown): v is Session {
|
||||
const s = v as Partial<Session> | null
|
||||
return typeof s === 'object' && s !== null && typeof s.id === 'string' &&
|
||||
typeof s.at === 'number' && Array.isArray(s.failures) &&
|
||||
Array.isArray(s.milestones) && Array.isArray(s.red)
|
||||
}
|
||||
|
||||
/** A stored value read as a project; malformed parts are dropped. */
|
||||
function asProject(value: unknown): Project {
|
||||
const sessions = (value as Partial<Project> | null)?.sessions
|
||||
if (!Array.isArray(sessions)) return { sessions: [] }
|
||||
return { sessions: sessions.filter(isSession) }
|
||||
}
|
||||
|
||||
const storeKey = (root: string) => STORE_PREFIX + root.slice(0, MAX_ROOT)
|
||||
|
||||
export async function loadProject($: Api, root: string): Promise<Project> {
|
||||
return asProject(await $.store.get(storeKey(root)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the session or creates it, stamps its activity and keeps the
|
||||
* MAX_SESSIONS latest of the project; the one returned is never trimmed.
|
||||
*/
|
||||
export function sessionOf(p: Project, id: string, now: number): Session {
|
||||
const found = p.sessions.find(s => s.id === id)
|
||||
const s = found ?? { id, at: now, failures: [], milestones: [], red: [] }
|
||||
s.at = now
|
||||
const others = p.sessions.filter(o => o !== s).sort((a, b) => b.at - a.at)
|
||||
p.sessions = [s, ...others].slice(0, MAX_SESSIONS)
|
||||
return s
|
||||
}
|
||||
|
||||
export function addFailure(s: Session, f: Failure): void {
|
||||
const same = s.failures.find(x => x.key === f.key)
|
||||
if (same === undefined) {
|
||||
s.failures = [...s.failures, f].slice(-MAX_FAILURES)
|
||||
return
|
||||
}
|
||||
same.count++
|
||||
same.at = f.at
|
||||
}
|
||||
|
||||
/** An entry over MAX_ENTRY loses its excerpt first, then its line. */
|
||||
function fitEntry(f: Failure): Failure {
|
||||
const out = { ...f }
|
||||
for (const field of ['excerpt', 'line', 'command', 'key'] as const) {
|
||||
const over = JSON.stringify(out).length - MAX_ENTRY
|
||||
if (over <= 0) break
|
||||
out[field] = field === 'excerpt'
|
||||
? '' : out[field].slice(0, Math.max(0, out[field].length - over))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* The failure entry of a tool answer, or `undefined` when the answer is
|
||||
* not a capturable failure. Everything stored comes from the redacted text.
|
||||
*/
|
||||
function buildFailure(
|
||||
text: string,
|
||||
command: string,
|
||||
agent: boolean,
|
||||
at: number,
|
||||
): Failure | undefined {
|
||||
const red = redact(tail(text, MAX_REGEX_INPUT))
|
||||
const line = failureLine(red)
|
||||
if (line === undefined) return undefined
|
||||
return fitEntry({
|
||||
key: normalize(line),
|
||||
line,
|
||||
excerpt: tail(red, EXCERPT_LEN),
|
||||
command: redact(cap(command, MAX_REGEX_INPUT)).slice(0, MAX_COMMAND),
|
||||
agent,
|
||||
at,
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
|
||||
function bounded(s: Session): Session {
|
||||
return {
|
||||
...s,
|
||||
milestones: s.milestones.slice(-MAX_MILESTONES),
|
||||
red: s.red.slice(-MAX_RED).map(r => r.slice(0, MAX_RED_LEN)),
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveProject(
|
||||
$: Api,
|
||||
root: string,
|
||||
p: Project,
|
||||
): Promise<void> {
|
||||
await $.store.set(storeKey(root), { sessions: p.sessions.map(bounded) })
|
||||
}
|
||||
|
||||
type Held = { key: string; size: number; project: Project; changed: boolean }
|
||||
|
||||
async function readHeld($: Api): Promise<Held[]> {
|
||||
const keys = (await $.store.keys()).filter(k => k.startsWith(STORE_PREFIX))
|
||||
const held: Held[] = []
|
||||
for (const key of keys) {
|
||||
const raw = await $.store.get(key)
|
||||
const size = JSON.stringify(raw ?? null).length
|
||||
held.push({ key, size, project: asProject(raw), changed: false })
|
||||
}
|
||||
return held
|
||||
}
|
||||
|
||||
/** The oldest session that is not the current one, with its project. */
|
||||
function oldestOther(held: Held[], cur: string, id: string) {
|
||||
let best: { h: Held; s: Session } | undefined
|
||||
for (const h of held) {
|
||||
for (const s of h.project.sessions) {
|
||||
if (h.key === cur && s.id === id) continue
|
||||
if (best === undefined || s.at < best.s.at) best = { h, s }
|
||||
}
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the oldest non-current sessions, across projects, until the
|
||||
* `p:*` keys fit STORE_CAP or none is droppable. Runs when a session
|
||||
* record is created, not on every write.
|
||||
*/
|
||||
async function sweepStore($: Api, curKey: string, id: string): Promise<void> {
|
||||
const held = await readHeld($)
|
||||
let total = held.reduce((n, h) => n + h.size, 0)
|
||||
while (total > STORE_CAP) {
|
||||
const victim = oldestOther(held, curKey, id)
|
||||
if (victim === undefined) break
|
||||
const { h, s } = victim
|
||||
h.project.sessions = h.project.sessions.filter(x => x !== s)
|
||||
h.changed = true
|
||||
const size = JSON.stringify(h.project).length
|
||||
total += size - h.size
|
||||
h.size = size
|
||||
}
|
||||
for (const h of held.filter(x => x.changed)) {
|
||||
if (h.project.sessions.length === 0) await $.store.delete(h.key)
|
||||
else await $.store.set(h.key, h.project)
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs `job` after every earlier write; a failed link breaks nothing. */
|
||||
function queueWrite(st: State, job: () => Promise<void>): Promise<void> {
|
||||
const run = st.writes.then(job)
|
||||
st.writes = run.catch(() => undefined)
|
||||
return run
|
||||
}
|
||||
|
||||
type Where = { root: string; id: string }
|
||||
|
||||
async function recordFailure($: Api, at: Where, f: Failure): Promise<void> {
|
||||
const p = await loadProject($, at.root)
|
||||
const created = !p.sessions.some(s => s.id === at.id)
|
||||
addFailure(sessionOf(p, at.id, f.at), f)
|
||||
await saveProject($, at.root, p)
|
||||
if (created) await sweepStore($, storeKey(at.root), at.id)
|
||||
}
|
||||
|
||||
function newState(): State {
|
||||
return {
|
||||
enabled: null,
|
||||
@@ -154,13 +373,36 @@ function registerSession(on: On, st: State): void {
|
||||
})
|
||||
}
|
||||
|
||||
/** Stores the failure a Bash answer carries; never throws, never re-runs. */
|
||||
async function captureFailure(
|
||||
$: Api,
|
||||
st: State,
|
||||
at: Where,
|
||||
e: { command?: unknown; agentId?: unknown },
|
||||
out: { isError?: boolean; text?: string },
|
||||
): Promise<void> {
|
||||
try {
|
||||
if (out.isError !== true) return
|
||||
const agent = e.agentId !== undefined
|
||||
const f = buildFailure(
|
||||
String(out.text ?? ''), String(e.command ?? ''), agent,
|
||||
await $.clock.now(),
|
||||
)
|
||||
if (f !== undefined) await queueWrite(st, () => recordFailure($, at, f))
|
||||
} catch {
|
||||
warnOnce(st, $, 'Bash capture', 'store')
|
||||
}
|
||||
}
|
||||
|
||||
function registerBash(on: On, st: State): void {
|
||||
on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
|
||||
if (!(await ensureEnabled($, st))) return next(e)
|
||||
// read before next: a failure here must not run the tool twice
|
||||
const root = await $.session.root()
|
||||
if (root === '') return next(e)
|
||||
return next(e) // capture comes with the store
|
||||
const at = { root: await $.session.root(), id: await $.session.id() }
|
||||
if (at.root === '') return next(e)
|
||||
const out = await next(e)
|
||||
await captureFailure($, st, at, e, out)
|
||||
return out
|
||||
}).catch(($, e, next) => {
|
||||
warnOnce(st, $, 'Bash', next.error.kind)
|
||||
return next(e)
|
||||
|
||||
Reference in new issue
Block a user