Skip to content

opencode-claimant: migration-0022 — clear bound session_id on requeue + bind-time claim guard (#5001) - #5349

Merged
dezren39 merged 3 commits into
mainfrom
queue/9043-5001-session-binding-clearing
Nov 4, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/9043-5001-session-binding-clearing

Conversation

@dezren39

@dezren39 dezren39 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

What

Adds pm-stack/opencode-claimant/migration-0022.sql — the durable repo copy of the migration already applied live to the muse-dev queue DB by task 9043 (issue #5001) on 2026-11-04. This PR is the record copy, mirroring where migration-0021 lives; merging it changes no runtime behavior.

The fix (live since ~08:08Z)

Verification

  • Dry-run in a rolled-back transaction against the live DB: 9/9 checks pass (bind guard on queued + expired lease, bind on live claim, fail/expire/park clearing, rebind after requeue, claim clearing a residual stale binding, bind after claim-clear).
  • Post-apply readback: all six function definitions carry the new logic.
  • Reconciliation: 51 bound non-claimed rows cleared (queued/parked/blocked across the pilot queues); fresh probe afterwards shows 0 queued/parked/blocked rows carrying a progress or ledger binding. Terminal (done/cancelled) rows keep their binding as the execution record, by design.

Refs #5001 (task 9043 will close it on completion).


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@dezren39
dezren39 requested a review from a team as a code owner November 4, 2026 08:10
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 42 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f598d465-322a-464c-8427-98f0c8663cb4
Commits

Reviewing files that changed from the base of the PR and between 0818c0b and 8bdc8b1.

Files selected for processing: 1
  • pm-stack/opencode-claimant/migration-0023.sql
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T10:34:34.936906Z 8bdc8b1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 884f122652

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

t agent.tasks;
row_out agent.repo_task_lane_ledger;
begin
select * into t from agent.tasks where id = p_task_id;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Lock the task row before binding its session

When a same-session bind retry overlaps park_task or another binding-clearing transition, the new clearing code locks the task row before updating the ledger, while this function updates the ledger before locking the task. This introduces a lock-order cycle that can deadlock and abort either the bind or the task transition. Acquire the task row with FOR UPDATE here before accessing the ledger; this also prevents a transition from invalidating the new claim/lease check before the binding is written.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Agent note · disposition (queue worker, task 9043, lane code-b)

Acknowledged — this is a real defect in the live-applied function body. This PR is the record copy of a migration already applied live (2026-11-04T08:08Z); the copy must stay faithful to the applied SQL, so the fix does not land by editing this file. It is tracked as issue #5393 with queue task 9348: a new claimant migration that re-extracts the live function bodies, takes the task row FOR UPDATE before touching the ledger in bind_repo_task_lane_session, and keeps lock order consistent across every binding-clearing transition.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed live by migration-0024 (record copy: PR #5399, for #5393): bind_repo_task_lane_session now takes the task row FOR UPDATE in its first statement, before any ledger access — matching the task-first lock order of every binding-clearing transition in migration-0023. Verified by pg_get_functiondef before/after diff (only this statement changed) plus lock-order inspection across all six migration-0023 transitions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed live by migration-0024 (record copy: PR #5399, for #5393): bind_repo_task_lane_session now takes the task row FOR UPDATE in its first statement, before any ledger access — matching the task-first lock order of every binding-clearing transition in migration-0023. Verified by pg_get_functiondef before/after diff (only this statement changed) plus lock-order inspection across all six migration-0023 transitions.

if r.progress ->> 'session_id' is not null then
update agent.repo_task_lane_ledger
set session_id = null, updated_at = now()
where task_id = r.id and session_id is not null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Qualify the ledger task identifier in the release branch

When revalidation releases a parked task that still has progress.session_id, this statement fails under PostgreSQL's default PL/pgSQL variable-conflict handling: RETURNS TABLE(task_id bigint, outcome text) declares a task_id output variable, making the unqualified task_id here ambiguous with the ledger column. The error rolls back the entire sweep, including other releases processed in that call. Alias the ledger table and qualify its task_id column.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Agent note · disposition (queue worker, task 9043, lane code-b)

Acknowledged — same disposition as the lock-order finding: real defect in the live-applied body, tracked as issue #5393 with queue task 9348. The follow-up migration aliases the ledger table and qualifies task_id in the revalidate_parked_tasks release branch, and its acceptance includes exercising that branch with a parked task that still carries progress.session_id. This PR stays a faithful record copy of the applied SQL (renumbered to migration-0023.sql only because main landed a different migration-0022.sql for issue #4758).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed live by migration-0024 (record copy: PR #5399, for #5393): the release branch now aliases agent.repo_task_lane_ledger as l and qualifies l.task_id. Reproduced before the fix in a rolled-back transaction (synthetic parked task with progress.session_id): ERROR 42702 column reference "task_id" is ambiguous, aborting the sweep. After the fix the same exercise completes — task released to queued, ledger session cleared — and the pg_get_functiondef diff shows only this change.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8bdc8b1114

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +434 to +436
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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the existing session-recovery mode

When the claimant recovers a deleted zero-message session or an absent session, prompt.md:92-112 still calls this RPC with opencode-claimant-recovery:* after lease expiry. Migration 0022 explicitly handles those calls by compare-and-clearing both bindings, recording launch_failed, and incrementing attempts. This replacement removes that branch and rejects the expired lease instead; even with a live lease, it merely rebinds the old session ID. Applying migrations in order therefore disables the documented recovery operation and leaves the unavailable session bound until another transition clears it. Preserve the recovery branch and apply the live-lease guard only to normal binding calls.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant