Skip to content

fix(ai): a run ended before it starts never launches its adapter; compact test waits for the run events - #670

Merged
omridevk merged 2 commits into
mainfrom
flake-core-compact-running
Oct 31, 2026
Merged

omridevk merged 2 commits into
mainfrom
flake-core-compact-running

Conversation

@omridevk

Copy link
Copy Markdown
Contributor

Why

core-api.it.test.ts > rpc wire > chat > compaction > compact runs as its own turn, never overlaps another, marks the boundary and keeps the history failed on CI (PR #664, shard 4) and locally with expected false to be true on the running read. The RCA found a test defect and, behind it, a product bug.

Test defect

chatKit().events(sessionId) only opens the session stream and resolves on headers; it waits for nothing. The test fired two compacts and read sessions.meta before either run had written its row (15 of 15 instrumented runs read before any run announced itself; a 150ms delay before drive reproduces the exact CI assertion). The test now opens the stream first and waits for two conciv.session-run events with distinct run ids, which the server emits only after the running row exists.

Product bug

Once the test waited on the real signal the two compacts ran concurrently and 9 of 30 runs failed the overlap assertion. guardedAdapter in packages/ai/src/server/run-guard.ts always called adapter.chatStream and raced the first chunk against the run's ending, even when the ending had already fired. A run ended by a takeover before reaching the model still launched its CLI and was killed afterwards, so two CLI runs overlapped.

Fix: the run's ending is an AbortController (the platform's settled-state primitive) instead of a PromiseWithResolvers; guarded checks ended.aborted before starting the adapter and yields the ending event without launching anything. endRuns and the silence timeout call abort(event) where they called resolve(event). Changeset for @conciv/ai.

Regression case in the existing packages/ai/test/ai.test.ts through createChatServer: a newer run ends an older one held in its setup, and only the newer adapter runs. It fails on the old code with expected ['second', 'first'] to deeply equal ['second'].

Verification

  • Compact test: 30 of 30 with fix and new wait (21 of 30 with the wait alone, 0 natural failures before, which is why it hid). rpc wire > chat group 5 of 5.
  • Gates serially: ai 28 passed, core 311 passed, typecheck 22/22, lint, format, fallow pass, changeset coverage exits 0.

🤖 Generated with Claude Code

omridevk and others added 2 commits November 1, 2026 00:38
When two turns or compacts start on one thread at once, the newer run ends the
older one during its setup. The run guard still called the older run's adapter,
so its CLI launched alongside the newer run and the two overlapped. The guard
now holds the ending on an AbortSignal and returns the ending event without
calling the adapter when the run was already ended.

The compaction test waited on opening the session event stream, which says
nothing about the compact run, so its running check raced the server. It now
opens the stream first and waits for both compact runs to announce themselves.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 22:39
@coderabbitai

coderabbitai Bot commented Oct 31, 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 28 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: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1cbd7106-727f-464b-8221-016245620c22
Commits

Reviewing files that changed from the base of the PR and between 14fec2c and f2b8c36.

Files selected for processing: 5
  • .changeset/ai-run-guard-ended-before-start.md
  • packages/ai/src/server/chat-server.ts
  • packages/ai/src/server/run-guard.ts
  • packages/ai/test/ai.test.ts
  • packages/core/test/core-api.it.test.ts
  • 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified in the changes guarding canceled runs against adapter startup and correcting the compaction test’s timing.

No open issues 🎉

What changed in this PR

Prevents superseded AI runs from launching their adapters after cancellation and fixes the compaction test’s premature running-state check.

Changes:

  • Uses an abort signal to track run endings and skip adapter startup for already-ended runs.
  • Adds a regression test for takeover during setup.
  • Makes the compaction test wait for two distinct run events before checking session state.
File Description
packages/​core/​test/​core-api.it.test.ts Waits for distinct run events before asserting compaction is running.
packages/​ai/​test/​ai.test.ts Tests cancellation before adapter startup and extracts shared server setup.
packages/​ai/​src/​server/​run-guard.ts Checks cancellation before launching the adapter.
packages/​ai/​src/​server/​chat-server.ts Uses abort controllers for run-ending events.
.changeset/​ai-run-guard-ended-before-start.md Records the AI package patch release.

Review effort: Balanced

@omridevk
omridevk merged commit 187cbda into main Oct 31, 2026
46 of 48 checks passed
@omridevk
omridevk deleted the flake-core-compact-running branch October 31, 2026 22:58
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