Repository navigation
fix(opencode-claimant): migration-0026 restores guarded lane-session recovery (#5400) #8384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| -- migration-0026: restore the guarded lane-session recovery modes in | ||
| -- bind_repo_task_lane_session (#5400). | ||
| -- | ||
| -- migration-0022 introduced two narrowly reserved recovery modes for | ||
| -- the opencode claimant: when a bound session is verified deleted | ||
| -- while still zero-message, or verified absent from the server, the | ||
| -- claimant calls bind with p_launched_by set to | ||
| -- 'opencode-claimant-recovery:zero-message-deleted' or | ||
| -- 'opencode-claimant-recovery:gone-session' and p_session_id set to | ||
| -- the PRIOR session id. The function compare-and-clears that id | ||
| -- against BOTH agent.tasks.progress and agent.repo_task_lane_ledger | ||
| -- before clearing either binding, records stage 'launch_failed' on | ||
| -- the task, and consumes one attempt. | ||
| -- | ||
| -- The migration-0023 record copy rewrote the function without the | ||
| -- recovery branches, and migration-0024 (the current definition) | ||
| -- kept that shape: its claimed + live-lease guard now rejects every | ||
| -- recovery call, because prompt.md only invokes recovery AFTER the | ||
| -- lease has expired. The claimant's documented recovery operation | ||
| -- (#4758) therefore fails against the recorded function contract. | ||
| -- | ||
| -- This migration is migration-0024's bind_repo_task_lane_session | ||
| -- body with the migration-0022 recovery branch restored ahead of | ||
| -- the normal-bind guard. Nothing else changes: | ||
| -- | ||
| -- * Task-row-first lock order (0024, Codex finding 4465191796) is | ||
| -- preserved in both branches: the task row is locked FOR UPDATE | ||
| -- before the ledger row is touched; the recovery branch then | ||
| -- locks the ledger row FOR UPDATE, as 0022 did. | ||
| -- * The normal branch is migration-0024 unchanged: a normal bind | ||
| -- still requires a claimed task with a live lease. | ||
| -- * Recovery-mode decisions (the point triage task 10034 left open): | ||
| -- recovery requires status 'claimed' AND an expired lease | ||
| -- (lease_until null or <= now()). prompt.md invokes recovery only | ||
| -- after lease expiry, and a live lease means the bound session's | ||
| -- launch window is still owned — a recovery call against a live | ||
| -- lease is refused, so a stale claimant can never clear a binding | ||
| -- whose replacement may still legitimately launch. Recovery also | ||
| -- still requires p_worker = 'opencode-claimant', the prior | ||
| -- session id present in BOTH stores (any mismatch raises and | ||
| -- leaves both stores unchanged), and attempts remaining. | ||
| -- | ||
| -- Launch eligibility after recovery: recovery deliberately does | ||
| -- NOT renew the lease or change the task status. The recovered row | ||
| -- is claimed with an expired lease and stage 'launch_failed', which | ||
| -- is the broker's handoff shape: lease expiry returns such a row to | ||
| -- the handoff sink, and the claimant collects it there with a fresh | ||
| -- claim (a new live lease) before launching the replacement session | ||
| -- and binding it through the normal branch above. Renewing the | ||
| -- lease inside recovery would diverge from the live bind contract, | ||
| -- which renews nothing at bind or recovery time. The test file | ||
| -- covers this full sequence (scenario S9). | ||
| -- | ||
| -- revalidate_parked_tasks is not redefined here: migration-0024's | ||
| -- definition stands. | ||
|
|
||
| CREATE OR REPLACE FUNCTION agent.bind_repo_task_lane_session(p_task_id bigint, p_worker text, p_session_id text, p_launched_by text DEFAULT 'opencode-claimant'::text) | ||
| RETURNS agent.repo_task_lane_ledger | ||
| LANGUAGE plpgsql | ||
| AS $function$ | ||
| declare | ||
| t agent.tasks; | ||
| row_out agent.repo_task_lane_ledger; | ||
| recovery_reason text; | ||
| begin | ||
| select * into t from agent.tasks where id = p_task_id for update; | ||
| if not found then | ||
| raise exception 'bind_repo_task_lane_session: task % does not exist', p_task_id; | ||
| end if; | ||
|
|
||
| -- Recovery branch (restored from migration-0022): the claimant | ||
| -- uses these reserved launched_by values only after the old | ||
| -- session is verified deleted-while-zero-message or verified | ||
| -- absent, and only after the task lease has expired. Matching | ||
| -- both stores before changing either makes recovery an atomic | ||
| -- compare-and-clear rather than a progress-only checkpoint. | ||
| if p_launched_by in ( | ||
| 'opencode-claimant-recovery:zero-message-deleted', | ||
| 'opencode-claimant-recovery:gone-session' | ||
| ) then | ||
| if p_worker is distinct from 'opencode-claimant' then | ||
| raise exception 'bind_repo_task_lane_session: recovery must be performed by opencode-claimant'; | ||
| end if; | ||
| if p_session_id is null or btrim(p_session_id) = '' then | ||
| raise exception 'bind_repo_task_lane_session: recovery requires the prior session id'; | ||
| end if; | ||
| select * into row_out from agent.repo_task_lane_ledger | ||
| where task_id = p_task_id for update; | ||
| if not found then | ||
| raise exception 'bind_repo_task_lane_session: no lane ledger row for task %', p_task_id; | ||
| end if; | ||
| if t.lease_until is not null and t.lease_until > now() then | ||
| raise exception 'bind_repo_task_lane_session: recovery for task % requires an expired lease (lease_until %)', p_task_id, t.lease_until; | ||
| end if; | ||
| if t.status is distinct from 'claimed' | ||
| or coalesce(t.progress ->> 'lane', '') <> 'opencode' | ||
| or t.progress ->> 'session_id' is distinct from p_session_id | ||
| or row_out.assigned_lane is distinct from 'opencode' | ||
| or row_out.session_id is distinct from p_session_id then | ||
| raise exception 'bind_repo_task_lane_session: recovery binding mismatch for task %', p_task_id; | ||
| end if; | ||
| if t.max_attempts is not null | ||
| and coalesce(t.attempts, 0) >= t.max_attempts then | ||
| raise exception 'bind_repo_task_lane_session: recovery attempts exhausted for task %', p_task_id; | ||
| end if; | ||
|
|
||
| recovery_reason := case p_launched_by | ||
| when 'opencode-claimant-recovery:zero-message-deleted' | ||
| then 'verified zero-message session deleted before recovery' | ||
| else 'bound session verified absent before recovery' | ||
| end; | ||
|
|
||
| update agent.repo_task_lane_ledger | ||
| set session_id = null, updated_at = now() | ||
| where task_id = p_task_id | ||
| returning * into row_out; | ||
| update agent.tasks | ||
| set attempts = coalesce(attempts, 0) + 1, | ||
| progress = coalesce(progress, '{}'::jsonb) | ||
| || jsonb_build_object( | ||
| 'session_id', null::text, | ||
| 'launched_by', null::text, | ||
| 'stage', 'launch_failed', | ||
| 'launch_failed_session_id', p_session_id, | ||
| 'launch_failed_at', now(), | ||
| 'launch_failed_reason', recovery_reason), | ||
| updated_at = now() | ||
| where id = p_task_id; | ||
| insert into agent.events (worker_id, task_id, kind, detail) | ||
| values (p_worker, p_task_id, 'lane_session_recovered', | ||
| jsonb_build_object('session_id', p_session_id, | ||
| 'reason', recovery_reason, | ||
| 'stage', 'launch_failed', | ||
| 'via', 'bind_repo_task_lane_session')); | ||
| return row_out; | ||
| end if; | ||
|
|
||
| if t.status <> 'claimed' or t.lease_until is null or t.lease_until <= now() then | ||
| raise exception 'bind_repo_task_lane_session: task % is not claimed with a live lease (status %, lease_until %)', | ||
| p_task_id, t.status, t.lease_until; | ||
| end if; | ||
| select * into row_out from agent.repo_task_lane_ledger | ||
| where task_id = p_task_id; | ||
| if not found then | ||
| raise exception 'bind_repo_task_lane_session: no lane ledger row for task %', p_task_id; | ||
| end if; | ||
| if row_out.session_id is not null and row_out.session_id <> p_session_id then | ||
| raise exception 'bind_repo_task_lane_session: task % already bound to session %', p_task_id, row_out.session_id; | ||
| end if; | ||
| update agent.repo_task_lane_ledger | ||
| set session_id = p_session_id, updated_at = now() | ||
| where task_id = p_task_id | ||
| returning * into row_out; | ||
| update agent.tasks | ||
| set progress = coalesce(progress, '{}'::jsonb) | ||
| || jsonb_build_object('session_id', p_session_id, | ||
| 'launched_by', p_launched_by), | ||
| updated_at = now() | ||
| where id = p_task_id; | ||
| insert into agent.events (worker_id, task_id, kind, detail) | ||
| values (p_worker, p_task_id, 'lane_session_bound', | ||
| jsonb_build_object('session_id', p_session_id, | ||
| 'launched_by', p_launched_by, | ||
| 'via', 'bind_repo_task_lane_session')); | ||
| return row_out; | ||
| end $function$; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| -- Focused tests for migration-0026 (#5400): the restored guarded | ||
| -- recovery modes in agent.bind_repo_task_lane_session. | ||
| -- | ||
| -- Run via test_migration_0026.sh, which starts a scratch Postgres, | ||
| -- applies THIS schema file first, then ../migration-0026.sql, then | ||
| -- migration-0026.test.sql (the scenarios) with ON_ERROR_STOP. | ||
| -- | ||
| -- The schema here is the minimal subset of columns the function | ||
| -- touches (the base agent schema predates the record-copy | ||
| -- migrations in this directory and lives with the live DB). | ||
|
|
||
| create schema agent; | ||
|
|
||
| create table agent.tasks ( | ||
| id bigint primary key, | ||
| queue text, | ||
| status text, | ||
| lease_until timestamptz, | ||
| progress jsonb not null default '{}'::jsonb, | ||
| attempts integer not null default 0, | ||
| max_attempts integer, | ||
| worker_id text, | ||
| updated_at timestamptz not null default now() | ||
| ); | ||
|
|
||
| create table agent.repo_task_lane_ledger ( | ||
| task_id bigint primary key, | ||
| queue text, | ||
| assigned_lane text, | ||
| executed_lane text, | ||
| exclusion_reason text, | ||
| assigned_by text, | ||
| session_id text, | ||
| updated_at timestamptz not null default now() | ||
| ); | ||
|
|
||
| create table agent.events ( | ||
| id bigserial primary key, | ||
| worker_id text, | ||
| task_id bigint, | ||
| kind text, | ||
| detail jsonb, | ||
| created_at timestamptz not null default now() | ||
| ); | ||
|
|
||
| -- Seed one claimed opencode-lane task (id 1) bound to ses_old in | ||
| -- both stores; p_lease is relative to now() ('-10 minutes' expired, | ||
| -- '+10 minutes' live). | ||
| create or replace function agent.test_seed(p_lease interval, p_attempts integer, p_max_attempts integer) | ||
| returns void language plpgsql as $$ | ||
| begin | ||
| truncate agent.events, agent.repo_task_lane_ledger, agent.tasks; | ||
| insert into agent.tasks (id, queue, status, lease_until, progress, attempts, max_attempts, worker_id) | ||
| values (1, 'repo-health-code-pilot', 'claimed', now() + p_lease, | ||
| '{"lane": "opencode", "session_id": "ses_old"}'::jsonb, | ||
| p_attempts, p_max_attempts, 'repo-worker-code'); | ||
| insert into agent.repo_task_lane_ledger (task_id, queue, assigned_lane, assigned_by, session_id) | ||
| values (1, 'repo-health-code-pilot', 'opencode', 'repo-worker-code', 'ses_old'); | ||
| end $$; | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For tasks recovered through this PostgreSQL function, recovery requires an expired lease but this update leaves that lease unchanged. The recovered task therefore becomes an unbound
launch_failedcandidate that cannot pass the normal-bind live-lease guard at lines 127–129. Inclaimant.py,launch_stepdoes not renew or reclaim these claimed candidates before binding; it creates a replacement session, encounters the bind failure, and deletes the replacement without prompting it. Restore a valid claim/lease transition as part of recovery or before relaunch, and cover recovery followed by a normal bind in the tests; the current isolated recovery assertions miss this failure.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disposition: not a defect in this migration — launch eligibility is restored by the broker's expiry/collection transition, not by the bind call, and the sequence is now covered by a test (scenario S9, head 892b99a).
Evidence from the live system this record mirrors:
What changed in response to the finding: the migration-0026 header now documents this eligibility path explicitly, and test/migration-0026.test.sql gains scenario S9 — recovery, then the expiry/collection transition under a fresh claim lease, then a normal bind of the replacement session succeeds against both stores. All 9 scenarios pass (test/test_migration_0026.sh, scratch Postgres).