fix: honest activation + turn-scoped practice gates (the BKT stuck-slot incident)

The bkt-practice record showed ONE turn in which the model concluded
(clean ack) and then every pose died on 'a problem is already active'
until it gave up ('the problem slot is stuck — the conclude calls report
success'). Two flaws, both fixed:

1. GATES READ A FROZEN RECORD. problem/conclude gates re-read the stored
   record per execute — but the record freezes at the turn's write-ahead
   (persistTurn lands only at turn END), so a mid-turn conclude → pose
   pair could never see each other. practiceTools now builds ONE
   turn-scoped gate (createPracticeGate) seeded once from the record;
   the executes flip it. Fresh closure per turn (the agent is built per
   request) — no cross-turn leakage.

2. PHANTOM ACTIVATION. activeProblemOf ignored tool RESULTS, so the
   composer mounted from a pose the gate itself had refused (owner
   ruling: 'either it goes through or it doesn't'). Honest activation:
   a problem activates only on its clean ack; a conclude frees only on
   its clean ack; errored and dangling calls are skipped. Retroactively
   heals the BKT record (all its poses errored, all concludes clean →
   no active problem). pendingProblemDismissalOf follows the same law
   via the shared cleanToolCallIds helper.

Pinned: the mid-turn conclude→pose pair works in one toolset; the gate
re-arms after a successful pose; lone errored/dangling poses activate
nothing; a clean pose under a rejection stays active; an errored
conclude frees nothing.
This commit is contained in:
amosblomqvist
2026-10-05 01:22:25 +03:00
parent 9ccb33ea3b
commit e8fe5da6b1
4 changed files with 146 additions and 36 deletions
+31 -8
View File
@@ -111,6 +111,27 @@ export interface PracticeToolContext {
chatStore: { getChat(id: string): { messages: LhMessage[]; kind?: string } | null };
}
/** TURN-SCOPED GATE STATE (the BKT incident, 2026-10-05): the gates used
* to read the STORED record — but the record freezes at the turn's
* write-ahead (persistTurn lands only at turn END), so a mid-turn
* conclude → pose pair could never see each other: every conclude
* succeeded against the stale record and every following pose died on
* "a problem is already active" until the model gave up ("the problem
* slot is stuck"). The gate seeds ONCE from the record at construction
* (one practiceTools per turn — chat-handler builds the agent per
* request, so there is no cross-turn leakage), and the executes flip
* this closure — the intra-turn truth the record cannot hold yet. */
export interface PracticeGate {
problemActive: boolean;
}
/** Seed the turn's gate from the record (result-aware activeProblemOf —
* only a cleanly-posed problem counts). */
export function createPracticeGate(ctx: PracticeToolContext): PracticeGate {
const record = ctx.chatStore.getChat(ctx.chatId);
return { problemActive: record ? activeProblemOf(record.messages) !== null : false };
}
// ─── problem: the pose ───────────────────────────────────────────────────────
export const problemInputObject = Type.Object({
@@ -144,7 +165,7 @@ export const problemInputObject = Type.Object({
export type ProblemInput = Static<typeof problemInputObject>;
export function problemTool(ctx: PracticeToolContext): AgentTool<typeof problemInputObject> {
export function problemTool(gate: PracticeGate): AgentTool<typeof problemInputObject> {
return {
name: "problem",
label: "problem",
@@ -157,12 +178,12 @@ export function problemTool(ctx: PracticeToolContext): AgentTool<typeof problemI
executionMode: "sequential",
prepareArguments: (args: unknown) => healEncodedArguments(args, problemInputObject) as never,
execute: async (_toolCallId, _input) => {
const record = ctx.chatStore.getChat(ctx.chatId);
if (record && activeProblemOf(record.messages) !== null) {
if (gate.problemActive) {
return invalidInputResult("problem", [
"a problem is already active — call conclude on it before posing the next one",
]);
}
gate.problemActive = true;
return problemResult();
},
};
@@ -207,7 +228,7 @@ export const concludeInputObject = Type.Object({
export type ConcludeInput = Static<typeof concludeInputObject>;
export function concludeTool(ctx: PracticeToolContext): AgentTool<typeof concludeInputObject> {
export function concludeTool(gate: PracticeGate): AgentTool<typeof concludeInputObject> {
return {
name: "conclude",
label: "conclude",
@@ -219,10 +240,10 @@ export function concludeTool(ctx: PracticeToolContext): AgentTool<typeof conclud
prepareArguments: (args: unknown) => healEncodedArguments(args, concludeInputObject) as never,
executionMode: "sequential",
execute: async (_toolCallId, _input) => {
const record = ctx.chatStore.getChat(ctx.chatId);
if (!record || activeProblemOf(record.messages) === null) {
if (!gate.problemActive) {
return invalidInputResult("conclude", ["no active problem to conclude — pose one first"]);
}
gate.problemActive = false;
return concludeResult();
},
};
@@ -230,8 +251,10 @@ export function concludeTool(ctx: PracticeToolContext): AgentTool<typeof conclud
/** The practice toolset (the practice agent's whole registry). */
export function practiceTools(ctx: PracticeToolContext): Record<string, AgentTool<any>> {
// ONE gate per turn, shared by both tools — the intra-turn truth.
const gate = createPracticeGate(ctx);
return {
problem: problemTool(ctx),
conclude: concludeTool(ctx),
problem: problemTool(gate),
conclude: concludeTool(gate),
};
}
+32 -4
View File
@@ -316,9 +316,26 @@ export function concludedByDismissalPair(
* conclude call or a dismissal marker deactivates. The ack result at pose
* time does NOT (the problem stays on screen through nudges; the dock's
* subject is the call, not a dangling result). */
/** The ids of tool calls whose results are CLEAN (not errored) — the only
* calls that ever happened, per the honest-activation ruling. */
function cleanToolCallIds(messages: readonly LhMessage[]): Set<string> {
const ids = new Set<string>();
for (const m of messages) {
if ((m as { role?: string }).role !== "toolResult") continue;
const result = m as ToolResultMessage;
if (!result.isError) ids.add(result.toolCallId);
}
return ids;
}
export function activeProblemOf(
messages: readonly LhMessage[],
): { toolCallId: string; input: unknown } | null {
// HONEST ACTIVATION (owner ruling, 2026-10-05 — the BKT incident):
// calls count only through their RESULTS — a problem activates on its
// clean ack, a conclude frees on its clean ack; errored and dangling
// calls are skipped ("either it goes through or it doesn't").
const cleanCallIds = cleanToolCallIds(messages);
for (let mi = messages.length - 1; mi >= 0; mi--) {
const m = messages[mi]!;
const role = (m as { role?: string }).role;
@@ -332,9 +349,13 @@ export function activeProblemOf(
for (let bi = assistant.content.length - 1; bi >= 0; bi--) {
const block = assistant.content[bi]!;
if (block.type !== "toolCall") continue;
if (block.name === PRACTICE_CONCLUDE_TOOL) return null;
if (block.name === PRACTICE_CONCLUDE_TOOL) {
if (cleanCallIds.has(block.id)) return null; // a CLEAN conclude frees
continue; // errored/dangling conclude: the free never happened
}
if (block.name !== PRACTICE_PROBLEM_TOOL) continue;
return { toolCallId: block.id, input: block.arguments };
if (cleanCallIds.has(block.id)) return { toolCallId: block.id, input: block.arguments };
// errored/dangling pose — it did not go through; keep scanning
}
continue;
}
@@ -347,6 +368,7 @@ export function activeProblemOf(
* (input-events). Newest-marker-wins scan: the first dismissal marker or
* conclude call from the end decides. */
export function pendingProblemDismissalOf(messages: readonly LhMessage[]): string | null {
const cleanCallIds = cleanToolCallIds(messages);
for (let mi = messages.length - 1; mi >= 0; mi--) {
const m = messages[mi]!;
const role = (m as { role?: string }).role;
@@ -362,8 +384,14 @@ export function pendingProblemDismissalOf(messages: readonly LhMessage[]): strin
for (let bi = assistant.content.length - 1; bi >= 0; bi--) {
const block = assistant.content[bi]!;
if (block.type !== "toolCall" && block.type !== "text") continue;
if (block.type === "toolCall" && block.name === PRACTICE_CONCLUDE_TOOL) return null;
if (block.type === "toolCall" && block.name === PRACTICE_PROBLEM_TOOL) return null;
if (block.type === "toolCall" && block.name === PRACTICE_CONCLUDE_TOOL) {
if (cleanCallIds.has(block.id)) return null; // a CLEAN conclude settles it
continue; // errored/dangling: it never happened
}
if (block.type === "toolCall" && block.name === PRACTICE_PROBLEM_TOOL) {
if (cleanCallIds.has(block.id)) return null; // a CLEAN pose supersedes it
continue;
}
}
continue;
}