Skip to content

Two seconds off every agent turn: the steering drain no longer takes a lock when nothing is queued - #617

Merged
FMXExpress merged 4 commits into
mainfrom
claude/turn-overhead
Oct 26, 2026
Merged

FMXExpress merged 4 commits into
mainfrom
claude/turn-overhead

Conversation

@FMXExpress

@FMXExpress FMXExpress commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Third improve-mode loop against PasClaw's own building ability, this time for the runtime's cost per turn. Targets main; not stacked. main (#608 through #616) is merged in; the only conflicts were Makefile target lists, and both sides are kept. docs/agent-loop-plan.md is the plan this was picked from; bench/ragbench/OVERHEAD.md has the numbers.

The loop

Benchmark. Six turns of the RAG-pipeline task with a relay model that answers instantly: 12.2 s wall. The gap between the model's answer and its next request was 2,012 ms on every turn, whatever the tool.

Profile. /v1/relay/status showed each request enqueued and dequeued within 10 ms, so the time was inside the agent loop before enqueue. strace -e nanosleep on the gateway showed one thread sleeping 25 ms exactly 80 times per gap. That is LOCK_SPIN_MS × LOCK_MAX_WAIT_MS in PasClaw.Agent.Steering.

  • The tool loop drains the steering queue at the top of every iteration.
  • DrainSteering took its mkdir lock before checking whether a queue file existed.
  • In a home nobody had steered, the steering directory did not exist. So CreateDir on the lock path failed with ENOENT on every spin, the stale-lock check could not run, and the wait ran out at the full 2,000 ms.

That happened on every iteration of every session, on the gateway, the CLI and the TUI, until the first pasclaw steer created the directory. With a real provider it hid inside inference time, which is why nobody saw it.

Improve. DrainSteering returns before locking when nothing can be queued. AcquireLock creates the steering directory first, so a real push/drain from a fresh home contends instead of failing.

Verify. Same task, same decisions, three runs:

wall for 6 turns gap per turn
before 12.2 s 2,012 / 2,014 / 2,012 / 2,021 / 2,113 ms
after 0.1 s 0 / 0 / 1 / 1 / 1 / 102 ms

The last turn's 102 ms is the test run inside shell_exec, the task's own work. Project files byte-identical. Not covered: homes that had been steered once never paid this; tool work was never in the gap.

Research. With the gap at the millisecond level the remaining per-turn time is the work itself, so this loop stops here. The plan's next candidate is independent verification.

Review fix

P2, the fast path must not outrun a push mid-write. PushSteering takes the lock before it creates the queue file, so "no file" can also mean "a writer is between its lock and its write". The first cut exited then. The turn ran on without a steer the user had already sent, and if that iteration's response carried no tool calls, the message waited for the next turn.

  • 719b47e: the fast path now requires both no queue file and no lock held, so it falls back to waiting on the lock exactly when someone is mid-push. The hot case, a key nobody has steered, has neither, so the whole win is kept.
  • 6c539a6: the lock is checked first. With the file checked first, a push holding the lock at that moment could write and release before the lock check, and both checks came back empty. Lock first, any push in progress when the drain looked is waited for. This window cannot be hit deterministically without a hook between the two checks, so it is argued in the comment rather than tested.

Tests

steering_fastpath_tests (make test-steering-fastpath):

  • An empty drain on a fresh home returns in under 200 ms (was 2,000; 0 ms here).
  • Push then drain returns the message, and the following empty drain is fast again.
  • A drain racing a push that already holds the lock waits for it and returns the message. The fixture uses a real PushSteering file moved aside, with the lock held by a thread that completes the push 400 ms later; the drain waited 403 ms. Against the unfixed check it returned "0 message(s) after 0 ms".

Full make test green after the merge (54 passes).

Files

src/pkg/agent/PasClaw.Agent.Steering.pas, src/tests/steering_fastpath_tests.pas, Makefile, docs/agent-loop-plan.md, bench/ragbench/OVERHEAD.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U

…a lock when nothing is queued

Third improve-mode loop against PasClaw's building ability, this time
for the runtime's own cost per turn (docs/agent-loop-plan.md picks the
candidates; bench/ragbench/OVERHEAD.md has the numbers).

Benchmark: six turns of the RAG task with a model that answers
instantly took 12.2 s. The gap between the model's answer and its next
request was 2,012 ms on every turn, whatever the tool.

Profile: /v1/relay/status showed the request enqueued and dequeued
within 10 ms, so the time was inside the agent loop before enqueue.
strace on the gateway showed one thread sleeping 25 ms exactly 80 times
per gap: LOCK_SPIN_MS x LOCK_MAX_WAIT_MS in PasClaw.Agent.Steering. The
loop drains the steering queue at every iteration top, and
DrainSteering took its mkdir lock before checking whether a queue file
existed. In a home nobody had steered the steering directory did not
exist, CreateDir on the lock path failed with ENOENT on every spin, the
stale check never ran, and the wait ran out at 2,000 ms. Every
iteration of every session on the gateway, the CLI and the TUI, until
the first `pasclaw steer` created the directory. With a real provider
it hid inside inference time.

Improve: DrainSteering returns before locking when no queue file
exists (a file that appears afterwards is drained next iteration, as a
contended lock already behaved), and AcquireLock creates the steering
directory first so a real push/drain from a fresh home contends
instead of failing.

Verify: same task, same decisions, three runs: 12.2 s -> 0.1 s, gaps
0-1 ms with the last turn's 102 ms being the test run itself. Project
files byte-identical. Not covered: tool work (none was in the gap), and
homes that had been steered once never paid this.

Research: the loop's remaining per-turn cost is now the work itself, so
this loop stops here; the plan's next candidate is independent
verification.

Tested in steering_fastpath_tests: an empty drain on a fresh home
returns in under 200 ms (was 2,000), push then drain returns the
message, the following empty drain is fast again.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-10-22T00:30:03.469715Z 4adf797 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: 4adf797bec

ℹ️ 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".

answer and its next request, on every turn. A queue file that
appears between this check and the next iteration is picked up then,
exactly as a contended lock already behaves. *)
if not FileExists(Path) then Exit;

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 synchronization with an in-progress push

When PushSteering has acquired the per-session lock but has not created the queue file yet, this unlocked check returns immediately instead of waiting for that push to finish. RunToolLoop then proceeds to its provider call, so if the response has no tool calls and ends the loop, the user's steering message remains queued and never affects the active turn; previously AcquireLock waited for the push and the same iteration drained it. Keep the empty-queue fast path only when no lock directory exists, or otherwise synchronize with an in-progress writer.

Useful? React with 👍 / 👎.

The empty-queue check read "no queue file" as "nothing queued". It is
not: PushSteering takes the lock BEFORE it creates the file, so there
is a window where the lock is held and the file does not exist yet.
A drain landing in that window returned instantly, the turn carried on
without a steer the user had already sent, and if that iteration's
response carried no tool calls the loop ended and the message waited
for the NEXT turn -- the one thing steering exists to prevent. Before
the fast path, AcquireLock blocked on that push and the same iteration
drained it.

The fast path now asks for both: no file AND nobody holding the lock.
That keeps the entire win -- the hot case is a key nobody has ever
steered, which has neither -- and falls back to contending for the
lock exactly when a writer is mid-push. A file appearing after both
checks belongs to a push that started after we looked, and is picked
up next iteration, which is how a contended lock already behaves.

Pinned by a test that reproduces the window with real artifacts rather
than fabricated ones: a message written by PushSteering itself, moved
aside so the file is absent, with the lock held by a background thread
that completes the push 400 ms later. The drain has to wait and come
back with the message. Verified against the unfixed check, which
returns "0 message(s) after 0 ms".

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
main moved on by #608 through #616 since this branch was cut. One
conflicted file, the Makefile, in three places: the .PHONY list and the
`test` prerequisites take main's lines plus this branch's one target,
test-steering-fastpath; its recipe sits beside #616's
test-workspace-listing, where both had been added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
719b47e made the empty-queue fast path ask for both "no queue file" and
"nobody holding the lock", which is the fix the review asked for. The
two checks ran file first, lock second, and that order left a window:
a push that already held the lock when the drain looked for the file
could write the file and release the lock before the drain looked for
the lock. Both checks then came back empty and the message missed the
iteration it was sent during -- the same outcome the review was about,
in a narrower window.

Lock first closes it. A push holding the lock at that check sends the
drain to wait for it; a push that takes the lock afterwards started
after the drain looked, and its file is either there for the second
check (drained now) or picked up next iteration, as a contended lock
already behaves. The unit has no {$B+}, so "and" short-circuits left to
right and this is the order the checks run in.

Not covered by a test: hitting the window deterministically needs a
hook between the two checks. steering_fastpath_tests still pins the
fast path (0 ms on a fresh home) and the wait on a push mid-write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@FMXExpress
FMXExpress merged commit abfe88b into main Oct 26, 2026
1 check passed
FMXExpress pushed a commit that referenced this pull request Oct 26, 2026
main moved on by #608 through #617 since this branch was cut. Five
conflicted files, all two independent additions side by side, all kept:

  - Makefile: main's .PHONY and `test` lists plus test-verify; its
    recipe beside #617's test-steering-fastpath.
  - Cmd.Agent BuildLoopConfig: the --scenarios / verification block
    beside the permissions OnAsk wiring.
  - Config: the Verification field, its defaults and its loading beside
    shell_timeout_sec, mcp_codemode_default, background_jobs_max,
    permissions, worktree, cli and instructions.
  - ToolLoop: ScenariosDir / VerifyMaxRounds beside OnAsk and
    Workspace; PasClaw.Tools.Sandbox listed once, for both uses.
  - docs/configuration.md: the two verification rows after main's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@FMXExpress FMXExpress mentioned this pull request Nov 13, 2026
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.

2 participants