Skip to content

Commit 3333ca6

Browse files
fix(scheduler): keep identity-less quota audits from expiring a receipt
heartbeatReceiptStatus() re-decided what counts as a successor receipt: after a run_id-carrying receipt it also treated an identity-less `normal_run` row with details.ok/should_run as a superseding receipt. The host protocol (host-integration-plugin-plan-v0) is explicit that only a quota event with a nonempty string Turn identity is a heartbeat receipt, and that identity-less invocations stay diagnostic audits: they neither grant host writeback nor expire a valid receipt. The discovery predicate already encoded that rule, so the successor scan now reuses it (isHeartbeatReceiptEvent) instead of keeping a second, looser definition. Without this, the Python compatibility route reports heartbeat_receipt_stale and refuses to write scheduler state whenever such an audit row follows the admitted Turn's own receipt. Tests: replace the TypeScript case that locked the old behaviour with one proving the ACK still commits and that a real newer Turn still expires the older receipt, and add the same diagnostic-row scenario to the Python compatibility-route gate. Signed-off-by: GZY-SUPER-HACKER <162807803+GZY-SUPER-HACKER@users.noreply.github.com>
1 parent 8300e64 commit 3333ca6

3 files changed

Lines changed: 97 additions & 22 deletions

File tree

‎loopx/control_plane/rollout_receipt_log.ts‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,32 @@ export function goalHeartbeatReceiptsFromSnapshot(
118118
return heartbeatReceiptsFromEvents(snapshot.events, goalId, agentId);
119119
}
120120

121+
/**
122+
* One receipt predicate, shared by discovery and by successor scans.
123+
*
124+
* A failed invocation without a real Turn identity is an audit: the host
125+
* protocol keeps it visible in the log, but lets it neither grant host
126+
* writeback nor expire the receipt of the Turn it follows.
127+
*/
128+
export function isHeartbeatReceiptEvent(
129+
event: JsonObject,
130+
goalId: string,
131+
agentId?: string | null,
132+
): boolean {
133+
return event.event_kind === HEARTBEAT_RECEIPT_EVENT_KIND &&
134+
event.goal_id === goalId &&
135+
typeof event.run_id === "string" && event.run_id.trim().length > 0 &&
136+
(agentId === undefined || event.agent_id === agentId);
137+
}
138+
121139
/** The heartbeat receipt filter over events a caller already read. */
122140
export function heartbeatReceiptsFromEvents(
123141
events: readonly JsonObject[],
124142
goalId: string,
125143
agentId?: string | null,
126144
): JsonObject[] {
127145
return events.filter((event) =>
128-
event.event_kind === HEARTBEAT_RECEIPT_EVENT_KIND &&
129-
event.goal_id === goalId &&
130-
// A failed invocation without a real Turn identity is an audit, not a
131-
// receipt that can grant host writeback or supersede an admitted Turn.
132-
typeof event.run_id === "string" && event.run_id.trim().length > 0 &&
133-
(agentId === undefined || event.agent_id === agentId)
146+
isHeartbeatReceiptEvent(event, goalId, agentId)
134147
);
135148
}
136149

@@ -177,21 +190,14 @@ export async function heartbeatReceiptStatus(params: {
177190
? -1
178191
: snapshot.events.indexOf(firstReceipt);
179192
if (firstMatch < 0) return "missing";
180-
return snapshot.events.slice(firstMatch + 1).some((event) => {
181-
if (
182-
event.event_kind !== HEARTBEAT_RECEIPT_EVENT_KIND ||
183-
event.goal_id !== params.goalId ||
184-
event.agent_id !== params.agentId
185-
) return false;
186-
if (typeof event.run_id === "string" && event.run_id.trim().length > 0) {
187-
return event.run_id !== params.turnInstanceId;
188-
}
189-
const details = event.details;
190-
return event.status === "normal_run" &&
191-
typeof details === "object" && details !== null && !Array.isArray(details) &&
192-
(details as JsonObject).ok === true &&
193-
(details as JsonObject).should_run === true;
194-
})
193+
// Only a real Turn receipt expires an admitted one. Identity-less quota rows
194+
// are diagnostics: the protocol neither grants them host writeback nor lets
195+
// them supersede the receipt they follow, so the successor scan reuses the
196+
// discovery predicate rather than re-deciding what a receipt is.
197+
return snapshot.events.slice(firstMatch + 1).some((event) =>
198+
isHeartbeatReceiptEvent(event, params.goalId, params.agentId) &&
199+
event.run_id !== params.turnInstanceId
200+
)
195201
? "stale"
196202
: "fresh";
197203
}

‎tests/control_plane/test_scheduler_stale_receipt_gate.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,64 @@ def test_stale_receipt_rejection_does_not_migrate_legacy_scheduler_state(
195195
assert result["scheduler_state_mutated"] is False
196196
assert legacy_path.read_bytes() == legacy_bytes
197197
assert not canonical_path.exists()
198+
199+
200+
def _diagnostic_row(runtime: Path) -> None:
201+
"""Append one identity-less successful quota row: a host-protocol audit."""
202+
203+
append_rollout_event(
204+
rollout_event_log_path(runtime, GOAL),
205+
build_rollout_event(
206+
goal_id=GOAL,
207+
event_kind="quota_should_run",
208+
agent_id=SCOPED_AGENT_ID,
209+
status="normal_run",
210+
details={"ok": True, "should_run": True},
211+
),
212+
)
213+
214+
215+
def test_identity_less_diagnostic_row_cannot_expire_the_receipt(
216+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
217+
) -> None:
218+
"""A diagnostic row after the receipt is audit, not a successor.
219+
220+
The host protocol requires a nonempty Turn identity for a heartbeat
221+
receipt; an identity-less quota row must neither grant host writeback nor
222+
expire the admitted Turn's receipt, on either backend.
223+
"""
224+
225+
registry, runtime, project = _fixture(tmp_path, monkeypatch)
226+
_should_run(registry, runtime, project, TURN_1)
227+
_diagnostic_row(runtime)
228+
229+
code, result = _ack(registry, runtime, project, TURN_1)
230+
231+
assert code == 0, result
232+
assert result["ok"] is True
233+
assert result["scheduler_state_mutated"] is True
234+
state = load_scheduler_state(
235+
runtime, goal_id=GOAL, agent_id=SCOPED_AGENT_ID, state_key=APP_KEY
236+
)
237+
assert state is not None
238+
assert state["last_applied_rrule"]
239+
240+
241+
def test_a_real_successor_receipt_still_expires_the_receipt(
242+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
243+
) -> None:
244+
"""Strategy: the relaxation above must not weaken real successors."""
245+
246+
registry, runtime, project = _fixture(tmp_path, monkeypatch)
247+
_should_run(registry, runtime, project, TURN_1)
248+
_diagnostic_row(runtime)
249+
_supercede(runtime, TURN_2)
250+
251+
code, result = _ack(registry, runtime, project, TURN_1)
252+
253+
assert code == 1, result
254+
assert result["status"] == "heartbeat_receipt_stale"
255+
assert result["scheduler_state_mutated"] is False
256+
assert not scheduler_state_path(
257+
runtime, goal_id=GOAL, agent_id=SCOPED_AGENT_ID, state_key=APP_KEY
258+
).exists()

‎tests/control_plane_ts/scheduler_heartbeat_followup.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ test("another Agent receipt cannot supersede ACK or host-failure writeback", asy
325325
assert.equal(failed.failure_count, 1);
326326
});
327327

328-
test("a successful identity-less quota guard supersedes an older host receipt", async (t) => {
328+
test("a successful identity-less quota guard cannot supersede an older host receipt", async (t) => {
329329
const runtimeRoot = await tempRuntime(t);
330330
await appendReceipt(runtimeRoot, "turn-followup-1");
331331
await writeFile(receiptPath(runtimeRoot), `${JSON.stringify({
@@ -337,6 +337,14 @@ test("a successful identity-less quota guard supersedes an older host receipt",
337337
details: { ok: true, should_run: true },
338338
})}\n`, { flag: "a" });
339339

340+
// The host protocol keeps identity-less quota rows as diagnostic audits: they
341+
// never expire the admitted Turn's receipt, so this ACK still commits.
342+
const acknowledged = await evaluateSchedulerHeartbeatFollowup(request(runtimeRoot));
343+
assert.equal(acknowledged.ok, true);
344+
assert.equal((acknowledged.scheduler_commit as Record<string, unknown>).status, "written");
345+
346+
// A real newer Turn still supersedes the older receipt, diagnostics or not.
347+
await appendReceipt(runtimeRoot, "turn-followup-2");
340348
const stale = await evaluateSchedulerHeartbeatFollowup(request(runtimeRoot));
341349
assert.equal(stale.error_code, "SCHEDULER_FOLLOWUP_HEARTBEAT_RECEIPT_STALE");
342350
assert.equal(stale.scheduler_state_mutated, false);

0 commit comments

Comments
 (0)