Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions loopx/control_plane/quota/heartbeat_receipt_identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ export function heartbeatWorkRequalification(
throw receiptIdentityConflict("work requalification cannot replace the original semantic replan guard");
}
const qualified = {...oldDetails};
// The guarded route becomes durable when this binding first admits work.
const spendSource = currentDetails.quota_spend_source;
if (spendSource != null) {
if (spendSource !== "heartbeat" && spendSource !== "visible-goal") {
throw receiptIdentityConflict("work requalification has an unsupported quota spend source");
}
if (oldDetails.quota_spend_source != null && oldDetails.quota_spend_source !== spendSource) {
throw receiptIdentityConflict("work requalification cannot replace the original quota spend source");
}
qualified.quota_spend_source = spendSource;
}
// The first positive work admission owns these facts. A repair-only
// receipt's earlier selection cannot describe the newly admitted work.
for (const field of ["ok", "should_run", "must_attempt_work", "delivery_allowed", "quiet_noop_allowed",
Expand Down
21 changes: 17 additions & 4 deletions tests/control_plane/test_refresh_settlement_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,33 @@ def test_unadmitted_explicit_turn_does_not_report_settlement_debt(tmp_path, monk
assert _spend_run_count(runtime) == 0


@pytest.mark.parametrize("handoff_mode", ["soft_claim", "hard_lease"])
@pytest.mark.parametrize("provider", ["file", "sqlite"])
@pytest.mark.parametrize("profile,spend_source", [("codex_app_heartbeat", "heartbeat"), ("codex_app_ssh_goal", "visible-goal")])
def test_rejected_write_and_upgrade_state_recover_original_turn_once(tmp_path, monkeypatch, provider, profile, spend_source):
def test_rejected_write_and_upgrade_state_recover_original_turn_once(
tmp_path, monkeypatch, provider, profile, spend_source, handoff_mode,
):
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = _write_fixture(tmp_path)
_initialize_fixture_authority(project, runtime, provider=provider)
_initialize_fixture_authority(project, runtime, provider=provider, handoff_mode=handoff_mode, leases=[])
binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID]
refresh = ["refresh-state", *binding, "--no-global-sync", "--suppress-external-sinks"]
maintenance = [*refresh, "--classification", "local_upgrade_status_updated", "--delivery-outcome", "surface_only"]
rc, setup = _run_cli(registry, runtime, *maintenance, cwd=project)
assert rc == 0 and setup["ok"] and "settlement_recovery" not in setup, setup
rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--runtime-profile", profile, *binding,
"--todo-id", TODO_ID, "--turn-instance-id", TURN_ID, cwd=project)
guard_args = ["quota", "should-run", "--runtime-profile", profile, *binding,
"--todo-id", TODO_ID, "--turn-instance-id", TURN_ID]
rc, guard = _run_cli(registry, runtime, *guard_args, cwd=project)
if handoff_mode == "hard_lease":
assert rc == 0 and guard["execution_preparation"]["state"] == "claim_required", guard
prepared_identity = guard["heartbeat_receipt"]["settlement_identity"]
rc, claim = _run_cli(registry, runtime, "todo", "claim", *binding,
"--claimed-by", AGENT_ID, "--todo-id", TODO_ID, cwd=project)
assert rc == 0 and claim["execution_managed"], claim
rc, guard = _run_cli(registry, runtime, *guard_args, cwd=project)
assert guard["heartbeat_receipt"]["settlement_identity"] == prepared_identity
assert rc == 0 and guard["normal_delivery_allowed"], guard
assert guard["interaction_contract"]["cli_channel"]["quota_spend_source"] == spend_source
identity = guard["heartbeat_receipt"]["settlement_identity"]
receipts = _heartbeat_receipt_count(runtime, TURN_ID)

Expand Down
9 changes: 5 additions & 4 deletions tests/control_plane_ts/native_child_admission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ test("same-binding reentry appends only explicit work qualification and preserve
pending_action_selection: {todo_id: todoId}, closeout_required: false,
}};
await writeFile(log, JSON.stringify(original) + "\n");
const current = {...event.details, semantic_replan_obligation_id: "",
const current = {...event.details, semantic_replan_obligation_id: "", quota_spend_source: "visible-goal",
delivery_workspace_causality: {requirement: "not_required"}, ignored_caller_fact: true};
const readback = await readQuotaSettlement({...request, heartbeat_reentry_guard: current});
assert.equal(readback.native_child_admission.report_permission, "not_admitted");
const qualification = readback.heartbeat_reentry_qualification;
assert.equal(qualification.append, true);
assert.deepEqual(qualification.details, {...original.details, ...proof,
closeout_required: true, settlement_receipt_revision: "work_admission"});
closeout_required: true, settlement_receipt_revision: "work_admission", quota_spend_source: "visible-goal"});
// A readback cannot commit or grant work by itself.
assert.equal((await readQuotaSettlement(request)).native_child_admission.report_permission, "not_admitted");
const corrected = {...original, event_id: "event-qualified", status: "normal_run",
Expand Down Expand Up @@ -263,14 +263,15 @@ test("work requalification fails closed on binding and semantic guard conflicts"
const {root, identity, event} = await fixture();
try {
const original = {...event, details: {...event.details, delivery_allowed: false,
semantic_replan_obligation_id: "replan-0000000000000001"}};
semantic_replan_obligation_id: "replan-0000000000000001", quota_spend_source: "visible-goal"}};
for (const changed of [
{todo_id: "todo_other_validation"}, {settlement_effect_id: "different-effect"},
{replan_obligation_id: "replan-0000000000000002"},
{semantic_replan_obligation_id: "replan-0000000000000002"},
{quota_spend_source: "heartbeat"}, {quota_spend_source: "unexpected"},
]) {
assert.throws(() => heartbeatWorkRequalification(original, {...event.details, ...changed},
identity, "open", false), /binding|guard|conflicting/);
identity, "open", false), /binding|guard|conflicting|spend source/);
}
} finally {await rm(root, {recursive: true, force: true});}
});
Expand Down
Loading