fix(session): preserve plugin turn invariants
This commit is contained in:
@@ -17,6 +17,11 @@ interface HookTransition {
|
||||
delta: 1 | -1
|
||||
}
|
||||
|
||||
interface HookTrace {
|
||||
openTurn: number | null
|
||||
pending: Map<string, number>
|
||||
}
|
||||
|
||||
/** Correlation key shared by an invoked/result pair. */
|
||||
function hookKey(data: { turn: number; point: string; handlerId: string }): string {
|
||||
return `${data.turn}\0${data.point}\0${data.handlerId}`
|
||||
@@ -24,10 +29,15 @@ function hookKey(data: { turn: number; point: string; handlerId: string }): stri
|
||||
|
||||
/** Validate one hook event against committed pending invocations. */
|
||||
function validateHookEvent(
|
||||
pending: ReadonlyMap<string, number>,
|
||||
trace: HookTrace,
|
||||
event: SessionEvent,
|
||||
fail: InvariantFailure,
|
||||
): HookTransition | undefined {
|
||||
if (event.type !== 'hook/invoked' && event.type !== 'hook/result') return undefined
|
||||
if (trace.openTurn === null) fail(`${event.type} appended outside any open turn`)
|
||||
if (event.data.turn !== trace.openTurn) {
|
||||
fail(`${event.type} names turn ${event.data.turn} but open turn is ${trace.openTurn}`)
|
||||
}
|
||||
if (event.type === 'hook/invoked') {
|
||||
if (event.data.point.length === 0 || event.data.handlerId.length === 0) {
|
||||
fail('hook/invoked point and handlerId must be non-empty')
|
||||
@@ -38,9 +48,8 @@ function validateHookEvent(
|
||||
}
|
||||
return { key: hookKey(event.data), delta: 1 }
|
||||
}
|
||||
if (event.type !== 'hook/result') return undefined
|
||||
const key = hookKey(event.data)
|
||||
if ((pending.get(key) ?? 0) === 0) {
|
||||
if ((trace.pending.get(key) ?? 0) === 0) {
|
||||
fail(`hook/result has no matching hook/invoked for ${JSON.stringify(event.data.handlerId)}`)
|
||||
}
|
||||
if (!Number.isFinite(event.data.durationMs) || event.data.durationMs < 0) {
|
||||
@@ -60,28 +69,39 @@ function applyHookTransition(pending: Map<string, number>, transition: HookTrans
|
||||
// Event owners keep precommit staging local so their vocabularies never move into a central helper.
|
||||
/* jscpd:ignore-start */
|
||||
const install: InvariantInstaller = Object.assign((ctx: Context, fail: InvariantFailure) => {
|
||||
const traces = new WeakMap<Session, Map<string, number>>()
|
||||
const traces = new WeakMap<Session, HookTrace>()
|
||||
const staged = new WeakMap<SessionEvent, { session: Session; transition: HookTransition }>()
|
||||
const seed = (session: Session): Map<string, number> => {
|
||||
const pending = new Map<string, number>()
|
||||
traces.set(session, pending)
|
||||
const seed = (session: Session): HookTrace => {
|
||||
const trace: HookTrace = { openTurn: null, pending: new Map() }
|
||||
traces.set(session, trace)
|
||||
for (const event of session.events) {
|
||||
const transition = validateHookEvent(pending, event, fail)
|
||||
if (transition !== undefined) applyHookTransition(pending, transition)
|
||||
if (event.type === 'turn/start') trace.openTurn = event.data.turn
|
||||
else if (event.type === 'turn/end') trace.openTurn = null
|
||||
const transition = validateHookEvent(trace, event, fail)
|
||||
if (transition !== undefined) applyHookTransition(trace.pending, transition)
|
||||
}
|
||||
return pending
|
||||
return trace
|
||||
}
|
||||
const traceFor = (session: Session): Map<string, number> => traces.get(session) ?? seed(session)
|
||||
const traceFor = (session: Session): HookTrace => traces.get(session) ?? seed(session)
|
||||
|
||||
for (const session of ctx.sessions.list()) seed(session)
|
||||
ctx.on('session/created', (session) => { seed(session) }, { global: true })
|
||||
ctx.on('session/event', (session, event) => {
|
||||
const trace = traceFor(session)
|
||||
if (event.type === 'turn/start') {
|
||||
trace.openTurn = event.data.turn
|
||||
return
|
||||
}
|
||||
if (event.type === 'turn/end') {
|
||||
trace.openTurn = null
|
||||
return
|
||||
}
|
||||
if (event.type !== 'hook/invoked' && event.type !== 'hook/result') return
|
||||
const candidate = staged.get(event)
|
||||
/* v8 ignore next -- internal/dispatch stages every hook provenance event */
|
||||
if (candidate === undefined || candidate.session !== session) return fail('hook event published without pre-commit validation')
|
||||
staged.delete(event)
|
||||
applyHookTransition(traceFor(session), candidate.transition)
|
||||
applyHookTransition(trace.pending, candidate.transition)
|
||||
}, { global: true })
|
||||
ctx.on('internal/dispatch', (_mode, eventName, args) => {
|
||||
if (eventName !== 'session/event') return
|
||||
|
||||
Reference in New Issue
Block a user