Skip to content

fix: serialize task claims and completions under one lock - #93

Merged
youdie006 merged 2 commits into
mainfrom
fix/task-update-lock
Nov 9, 2026
Merged

youdie006 merged 2 commits into
mainfrom
fix/task-update-lock

Conversation

@youdie006

Copy link
Copy Markdown
Owner

Problem

Found 2026-11-09 by running real processes against one ledger.

A claim can overwrite a completion. Only claimTask took a lock, an O_EXCL .claim.lock. A completion (prodex tasks complete, or MCP bridge_complete_task) could run between a claim's read and its write, and the claim then wrote "claimed" over the completion.

  • With tasks claim and tasks complete running at the same moment, 5 of 30 tasks ended claimed with no result_path. Each sat next to a done result and its trusted receipt.
  • Both commands reported success. The claimer could not complete the task afterwards ("already has a different result"), and it stayed in tasks list --status claimed.

A stale claim lock lets two claimers win. When a crashed claimer left its lock behind, each new claimer checked the lock's age, deleted it and created its own. Two claimers that both found it stale both won: 6 of 15 rounds of four concurrent claims.

Change

claimTask and completeTask now run under withTaskUpdateLock. It is the existing withCrossProcessFileLock applied per task, on .<task>.update.lock, with a 10 s wait.

  • The lock's holder is identified by pid, so a crashed holder's lock is taken over atomically instead of judged by its age.
  • A concurrent claimer now waits briefly and then finds the task claimed, instead of failing at once with "already being claimed".
  • A .claim.lock left by an older version is ignored.

Verification

  • tests/task-update-lock.test.ts pauses a claim mid-write while a completion runs. It fails against main (claimed instead of done) and passes here.
  • tests/store.test.ts passes, including the existing cases for a stale lock and concurrent claims.
  • The real-process race scripts: 5/30 inconsistent and 6/15 double-claimed against main; 0/30 and 0/15 with this branch.
  • npx tsc --noEmit passes. Full suite: 1870 passed, 3 skipped.

Only claimTask took a lock (an O_EXCL `.claim.lock`), so a completion could
run while a claim was between its read and its write; the claim then wrote
"claimed" over the completion. Found 2026-11-09 with real processes: 5 of
30 tasks claimed and completed at once ended claimed with no result_path,
next to a done result and its trusted receipt, and the claimer could not
complete them. The stale-lock reap (stat, rm, recreate) also let two
claimers both win when a crashed claimer had left its lock: 6 of 15
rounds of four concurrent claims.

claimTask and completeTask now run under withTaskUpdateLock, a
withCrossProcessFileLock per task (`.<task>.update.lock`, 10 s wait). Its
holder is identified by pid, so a crashed holder's lock is taken over
atomically instead of by age. A `.claim.lock` left by an older version is
ignored.

Verified: tests/task-update-lock.test.ts (claim paused mid-write while a
completion runs) fails against main and passes here; tests/store.test.ts
passes, including the old stale-lock case; the hunt's real-process
scripts went from 5/30 inconsistent and 6/15 double-claimed against main
to 0/30 and 0/15; npx tsc --noEmit; full suite 1870 passed, 3 skipped.

Claude-Session: https://claude.ai/code/session_01EMqNwnnhHXznd2P2PDsaDV
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

What this touches

  • bridge store
  • tests

3 file(s), 137 line(s) changed. Tests changed.

Worth a look

  • Careful read: touches the receipt/integrity path.

This is an automated first pass, not a review. CI is the gate; a person decides.

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T01:35:22.836796Z 4fc888f PR opened
ℹ️ 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: 4fc888fb33

ℹ️ 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 thread src/store.ts
Comment on lines +274 to +276
private async withTaskUpdateLock<T>(taskId: string, fn: () => Promise<T>): Promise<T> {
return withCrossProcessFileLock(
path.join(this.dir("tasks"), `.${taskId}.update.lock`),

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 Validate task IDs before constructing and acquiring the lock

When a caller supplies a traversal-containing task ID, this acquires the lock before getTask() reaches the existing assertBridgeRecordId() validation. Unlike the previous completion path, withCrossProcessFileLock() recursively creates the lock's parent directory first, so an MCP bridge_complete_task request with task_id: "x/../../../../escaped/nested/task" creates directories outside the repository even though the request ultimately fails validation. The MCP input checks permit this string. Validate the task ID before invoking the lock helper so rejected IDs cannot cause filesystem changes outside task storage.

Useful? React with 👍 / 👎.

@youdie006
youdie006 merged commit 555d9c5 into main Nov 9, 2026
7 checks passed
youdie006 added a commit that referenced this pull request Nov 10, 2026
…e lock) to the container image

Same change as main PR #93 (merge 555d9c5), released in 0.40.51, applied to
src, tests and docs without changes. The CHANGELOG lines are main's with
the PR noted.

Claude-Session: https://claude.ai/code/session_01EMqNwnnhHXznd2P2PDsaDV
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