Skip to content

feat(gateway): chat.send expectedLeafEntryId precondition keeps stale panes off switched branches - #113073

Merged
steipete merged 1 commit into
mainfrom
claude/chat-send-expected-leaf
Jul 23, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/chat-send-expected-leaf

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

The last known cross-client race in the rewind/fork feature set (deliberate follow-up from that work): tab A displays branch X of a session; tab B switches the session's active branch to Y; tab A sends — the message lands on Y, a branch the sender never saw. Confirmed live twice on an isolated dev gateway, including the subtle failure mode where a first-cut guard based on the client's latest branch fetch was defeated because background branch refreshes race ahead of what the pane renders.

Why This Change Was Made

Adds an additive chat.send precondition anchored to what the user actually sees:

  • Protocol: optional expectedLeafEntryId on ChatSendParamsSchema; chat.history's session info now carries activeLeafEntryId (both additive, no version bump).
  • Gateway: enforcement inside the locked lifecycle-admission revalidation — the same fence that serializes branch switches — comparing against the canonical transcript's active leaf (shared resolver with branch listing). Mismatch rejects with a typed terminal error (details.reason: "active-leaf-changed"); param absent → unchanged behavior; fresh session with a claimed leaf → rejected as stale.
  • Web UI: panes remember the leaf id of the transcript they last rendered (set only on history commits/cache restores, never by background branch refreshes) and attach it to foreground sends. On rejection: the message is parked with "The thread switched branches — review and resend.", the composer draft is restored, and history+branches refresh so a resend goes through. Outbox/reconnect replays intentionally omit the precondition (their reconcile path already re-reads current history).

User Impact

A message typed against one branch can no longer silently land on another. The sender gets an explicit review-and-resend prompt with their draft intact; other clients (iOS/Android) can adopt the same additive param independently.

Evidence

  • Live falsification loop on an isolated gateway (OpenAI inference, two browser tabs): v1 fetch-based guard demonstrably defeated (stale send landed cross-branch, parent-chain proof in transcript_events); display-derived v2 verified — typed rejection shown, composer restored, view resynced, zero server-side writes; immediate resend after resync landed with its reply on the current branch.
  • Tests: gateway enforcement (mismatch/match/absent/fresh-session), protocol schema (param + history field, closedObject), UI submit-time capture including a regression proving a background chatBranches refresh cannot change what a send attaches; focused runs green (233 gateway/core, 4 protocol, 420 UI).
  • Autoreview clean; docs updated (docs/gateway/protocol.md, docs/web/webchat.md).

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: web-ui App: web-ui gateway Gateway runtime size: L labels Jul 23, 2026
@steipete
steipete marked this pull request as ready for review July 23, 2026 15:57
@openclaw-barnacle openclaw-barnacle Bot added the maintainer Maintainer-authored PR label Jul 23, 2026
@steipete steipete closed this Jul 23, 2026
@steipete steipete reopened this Jul 23, 2026
@openclaw-barnacle openclaw-barnacle Bot reopened this Jul 23, 2026
@steipete
steipete force-pushed the claude/chat-send-expected-leaf branch from 85a20f4 to f492663 Compare July 23, 2026 19:04
@steipete
steipete force-pushed the claude/chat-send-expected-leaf branch from f492663 to 459272c Compare July 23, 2026 19:21
@steipete
steipete merged commit c519e01 into main Jul 23, 2026
126 of 127 checks passed
@steipete
steipete deleted the claude/chat-send-expected-leaf branch July 23, 2026 19:33
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 24, 2026
obviyus added a commit that referenced this pull request Nov 16, 2026
… and stop refusing on captured ids (#200191)

## What Problem This Solves

Work was stopped by inference: a check captured an id that changes on its own (a transcript id, lifecycle revision, run id, generation or displayed leaf), and later treated "it changed" as "it was revoked". Users saw:

- A message queued in Control UI during a turn was refused as "switched branches" once the turn ended. The sender's leaf was one entry behind the new reply, and a send without a session id needed an exact leaf match.
- `sessions_send` to a task session was refused with "Task resume differs from the captured communication owner." after the task moved to a new run (exec wake, yield, follow-up) or was compacted.
- A requester-wake failure notice for a lost subagent batch was pinned to the requester's old transcript and refused as `session-rebound` after a reset or compaction successor.
- Deleting a chat left its queued outbound replies in the durable queue, so a chat recreated with the same key could receive them.
- Every delivery, queue, wake and session-event API took a `SessionDeliveryGeneration` with a transcript id and revision, so any new caller could pin a chat to an id again.

## Why This Change Was Made

Make the bad state hard to express and guard the rest:

- **`ChatRef`** (`src/sessions/chat-ref.ts`) is the only durable chat address: agent id, store path and session key, branded. `chatRef()` copies only the address, so ids in rows queued by older builds never travel with it. Delivery, queue, wake, cron delivery, memory audience and session-event APIs take a `ChatRef`. `SessionDeliveryGeneration` and `SessionGenerationFacts` are deleted.
- **`TranscriptLease`** (`src/config/sessions/transcript-lease.ts`) is the only way to hold one transcript. Live writers (cron root, fork, subagent control, restart resume, a scheduled occurrence) acquire it with a private pin. It refuses to serialise.
- **One prefix rule for sends:** a send is admitted while the sender's leaf is on the active path; an empty view is a prefix of every path. A rewind, branch switch or reset away from the leaf still refuses it (#113073 holds). Deleted: the exact-match branch, `activePathNavigationMoved` and its reader, the send's own session-id fence, and the test-only relation reader. `activePathRelation` becomes `onActivePath`.
- **Task sends follow the chat:** `sessions_send` continues the task's current run. The `previousRunId` / `generation` / `childSessionId` compares, the retained-task pins and the target-incarnation compare are deleted. It refuses only when the task ended or was cancelled, the chat was deleted, or another session controls the task.
- **Lost-run notice by key**, and **delete purges queued sends** (a send that already started stays with its recovery owner).
- **Guard:** `check:session-identity-ratchet` counts, per file outside `src/config/sessions/`, any `===`/`!==` on a session id, run id, generation, lifecycle revision or leaf id, the `expectedSessionId` / `expectedLifecycleRevision` pin names, and refusal strings ("differs from the captured", "no longer active", "changed before", "was replaced"). Existing hits are listed in `config/session-identity-allowlist.txt`, which may only shrink.

Kept on purpose:
- **Typed Stop** keeps its incarnation fence (an empty view must still be empty, and the session id must match), so a Stop never cancels work that started after it. It moves to a recorded stop cause in the next change.
- **Memory grants** hold the incarnation they were minted in through a transcript lease: a reset revokes the grant, a compaction successor keeps it. This is the narrower authority and matches the memory audience's own tests.
- Knowledge partitions, the deep-research job store and GitHub publication rows key on transcript ids in stored rows; changing them needs a data migration and is left for a separate decision.

Production code (`src`, `extensions`, without tests): +524 / −602.

## User Impact

- A message queued during a turn is sent when the turn ends.
- A pane that still shows a branch another client switched away from is still refused, and the message is parked as not sent.
- A send to a task session reaches its current run after wakes, yields and compaction; a send to a cancelled task is refused with a reason.
- Deleting a chat drops its unsent queued replies.
- Rows queued by older builds still deliver. A row queued by this build and replayed by an older build has no transcript id; the older build treats it as unavailable and retries it until its retry budget ends.

## Evidence

Base = merge base `edbc5bcea9b6`, head = this PR.

| Journey | Tier | Base | Head |
|---|---|---|---|
| Control UI: queue a message during a held turn, release the turn (send carries only the displayed leaf) | Control UI E2E, real Gateway, scripted provider (`ui/src/e2e/chat-send-leaf-ancestor.real-gateway.e2e.test.ts`) | `chat.send` ok=false, message parked "Not sent" | `chat.send` ok=true, reply "Flag mentioned." |
| Control UI: pane still shows Plan C, another client switches to the Plan B tip, pane sends | same | refused, no provider request | refused, no provider request |
| Compaction successor while a background exec runs, then a next turn | `ocx src` scratch Gateway, real Claude model | exec wake lands in the successor transcript; next turn runs | same |
| Queue an outbound reply, delete the chat, recreate the key | `ocx src` scratch Gateway | 1 queued reply still pending | 0 pending |
| Reaction, then edit a later message | `ocx src` scratch Gateway | reaction still listed | reaction still listed |
| Plant `entry.sessionId === saved` in a new file | `check:session-identity-ratchet` | — | fails: `src/sessions/chat-ref.ts: 1 > 0` |

The task-send fix is proven by unit tests only: a send to an owned task after its run id and generation advanced, with a stale resume binding, is delivered (fails on base), and a send after the task is killed is refused. Real-Gateway journeys with a real Claude model (spawn a task, then send after an exec wake, after compaction, after cancel, or while the task is busy so the send is held and released) did not reproduce the refusal on base: base delivered in each. The live refusal needs the in-process resume binding of a retained self-hold replay, which these journeys do not reach.

Gates: oxfmt, oxlint, `pnpm tsgo:core`, `tsgo:ui`, `tsgo:scripts`, `pnpm check:test-types`, and `ocx test` on the lease, session store, outbound queue, subagent registry, cron, memory audience, session events, chat send and Stop, `sessions_send`, restart resume and UI E2E inventory suites.

`test/plugins/deep-research-routing.gateway.test.ts` fails the same way on `main` (CI run 43939332007, job 133553190527); this PR does not touch it.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 17, 2026
… and stop refusing on captured ids (openclaw#200191)

## What Problem This Solves

Work was stopped by inference: a check captured an id that changes on its own (a transcript id, lifecycle revision, run id, generation or displayed leaf), and later treated "it changed" as "it was revoked". Users saw:

- A message queued in Control UI during a turn was refused as "switched branches" once the turn ended. The sender's leaf was one entry behind the new reply, and a send without a session id needed an exact leaf match.
- `sessions_send` to a task session was refused with "Task resume differs from the captured communication owner." after the task moved to a new run (exec wake, yield, follow-up) or was compacted.
- A requester-wake failure notice for a lost subagent batch was pinned to the requester's old transcript and refused as `session-rebound` after a reset or compaction successor.
- Deleting a chat left its queued outbound replies in the durable queue, so a chat recreated with the same key could receive them.
- Every delivery, queue, wake and session-event API took a `SessionDeliveryGeneration` with a transcript id and revision, so any new caller could pin a chat to an id again.

## Why This Change Was Made

Make the bad state hard to express and guard the rest:

- **`ChatRef`** (`src/sessions/chat-ref.ts`) is the only durable chat address: agent id, store path and session key, branded. `chatRef()` copies only the address, so ids in rows queued by older builds never travel with it. Delivery, queue, wake, cron delivery, memory audience and session-event APIs take a `ChatRef`. `SessionDeliveryGeneration` and `SessionGenerationFacts` are deleted.
- **`TranscriptLease`** (`src/config/sessions/transcript-lease.ts`) is the only way to hold one transcript. Live writers (cron root, fork, subagent control, restart resume, a scheduled occurrence) acquire it with a private pin. It refuses to serialise.
- **One prefix rule for sends:** a send is admitted while the sender's leaf is on the active path; an empty view is a prefix of every path. A rewind, branch switch or reset away from the leaf still refuses it (openclaw#113073 holds). Deleted: the exact-match branch, `activePathNavigationMoved` and its reader, the send's own session-id fence, and the test-only relation reader. `activePathRelation` becomes `onActivePath`.
- **Task sends follow the chat:** `sessions_send` continues the task's current run. The `previousRunId` / `generation` / `childSessionId` compares, the retained-task pins and the target-incarnation compare are deleted. It refuses only when the task ended or was cancelled, the chat was deleted, or another session controls the task.
- **Lost-run notice by key**, and **delete purges queued sends** (a send that already started stays with its recovery owner).
- **Guard:** `check:session-identity-ratchet` counts, per file outside `src/config/sessions/`, any `===`/`!==` on a session id, run id, generation, lifecycle revision or leaf id, the `expectedSessionId` / `expectedLifecycleRevision` pin names, and refusal strings ("differs from the captured", "no longer active", "changed before", "was replaced"). Existing hits are listed in `config/session-identity-allowlist.txt`, which may only shrink.

Kept on purpose:
- **Typed Stop** keeps its incarnation fence (an empty view must still be empty, and the session id must match), so a Stop never cancels work that started after it. It moves to a recorded stop cause in the next change.
- **Memory grants** hold the incarnation they were minted in through a transcript lease: a reset revokes the grant, a compaction successor keeps it. This is the narrower authority and matches the memory audience's own tests.
- Knowledge partitions, the deep-research job store and GitHub publication rows key on transcript ids in stored rows; changing them needs a data migration and is left for a separate decision.

Production code (`src`, `extensions`, without tests): +524 / −602.

## User Impact

- A message queued during a turn is sent when the turn ends.
- A pane that still shows a branch another client switched away from is still refused, and the message is parked as not sent.
- A send to a task session reaches its current run after wakes, yields and compaction; a send to a cancelled task is refused with a reason.
- Deleting a chat drops its unsent queued replies.
- Rows queued by older builds still deliver. A row queued by this build and replayed by an older build has no transcript id; the older build treats it as unavailable and retries it until its retry budget ends.

## Evidence

Base = merge base `2c94bae9c3ea`, head = this PR.

| Journey | Tier | Base | Head |
|---|---|---|---|
| Control UI: queue a message during a held turn, release the turn (send carries only the displayed leaf) | Control UI E2E, real Gateway, scripted provider (`ui/src/e2e/chat-send-leaf-ancestor.real-gateway.e2e.test.ts`) | `chat.send` ok=false, message parked "Not sent" | `chat.send` ok=true, reply "Flag mentioned." |
| Control UI: pane still shows Plan C, another client switches to the Plan B tip, pane sends | same | refused, no provider request | refused, no provider request |
| Compaction successor while a background exec runs, then a next turn | `ocx src` scratch Gateway, real Claude model | exec wake lands in the successor transcript; next turn runs | same |
| Queue an outbound reply, delete the chat, recreate the key | `ocx src` scratch Gateway | 1 queued reply still pending | 0 pending |
| Reaction, then edit a later message | `ocx src` scratch Gateway | reaction still listed | reaction still listed |
| Plant `entry.sessionId === saved` in a new file | `check:session-identity-ratchet` | — | fails: `src/sessions/chat-ref.ts: 1 > 0` |

The task-send fix is proven by unit tests only: a send to an owned task after its run id and generation advanced, with a stale resume binding, is delivered (fails on base), and a send after the task is killed is refused. Real-Gateway journeys with a real Claude model (spawn a task, then send after an exec wake, after compaction, after cancel, or while the task is busy so the send is held and released) did not reproduce the refusal on base: base delivered in each. The live refusal needs the in-process resume binding of a retained self-hold replay, which these journeys do not reach.

Gates: oxfmt, oxlint, `pnpm tsgo:core`, `tsgo:ui`, `tsgo:scripts`, `pnpm check:test-types`, and `ocx test` on the lease, session store, outbound queue, subagent registry, cron, memory audience, session events, chat send and Stop, `sessions_send`, restart resume and UI E2E inventory suites.

`test/plugins/deep-research-routing.gateway.test.ts` fails the same way on `main` (CI run 43939332007, job 133553190527); this PR does not touch it.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 19, 2026
… and stop refusing on captured ids (#200191)

## What Problem This Solves

Work was stopped by inference: a check captured an id that changes on its own (a transcript id, lifecycle revision, run id, generation or displayed leaf), and later treated "it changed" as "it was revoked". Users saw:

- A message queued in Control UI during a turn was refused as "switched branches" once the turn ended. The sender's leaf was one entry behind the new reply, and a send without a session id needed an exact leaf match.
- `sessions_send` to a task session was refused with "Task resume differs from the captured communication owner." after the task moved to a new run (exec wake, yield, follow-up) or was compacted.
- A requester-wake failure notice for a lost subagent batch was pinned to the requester's old transcript and refused as `session-rebound` after a reset or compaction successor.
- Deleting a chat left its queued outbound replies in the durable queue, so a chat recreated with the same key could receive them.
- Every delivery, queue, wake and session-event API took a `SessionDeliveryGeneration` with a transcript id and revision, so any new caller could pin a chat to an id again.

## Why This Change Was Made

Make the bad state hard to express and guard the rest:

- **`ChatRef`** (`src/sessions/chat-ref.ts`) is the only durable chat address: agent id, store path and session key, branded. `chatRef()` copies only the address, so ids in rows queued by older builds never travel with it. Delivery, queue, wake, cron delivery, memory audience and session-event APIs take a `ChatRef`. `SessionDeliveryGeneration` and `SessionGenerationFacts` are deleted.
- **`TranscriptLease`** (`src/config/sessions/transcript-lease.ts`) is the only way to hold one transcript. Live writers (cron root, fork, subagent control, restart resume, a scheduled occurrence) acquire it with a private pin. It refuses to serialise.
- **One prefix rule for sends:** a send is admitted while the sender's leaf is on the active path; an empty view is a prefix of every path. A rewind, branch switch or reset away from the leaf still refuses it (#113073 holds). Deleted: the exact-match branch, `activePathNavigationMoved` and its reader, the send's own session-id fence, and the test-only relation reader. `activePathRelation` becomes `onActivePath`.
- **Task sends follow the chat:** `sessions_send` continues the task's current run. The `previousRunId` / `generation` / `childSessionId` compares, the retained-task pins and the target-incarnation compare are deleted. It refuses only when the task ended or was cancelled, the chat was deleted, or another session controls the task.
- **Lost-run notice by key**, and **delete purges queued sends** (a send that already started stays with its recovery owner).
- **Guard:** `check:session-identity-ratchet` counts, per file outside `src/config/sessions/`, any `===`/`!==` on a session id, run id, generation, lifecycle revision or leaf id, the `expectedSessionId` / `expectedLifecycleRevision` pin names, and refusal strings ("differs from the captured", "no longer active", "changed before", "was replaced"). Existing hits are listed in `config/session-identity-allowlist.txt`, which may only shrink.

Kept on purpose:
- **Typed Stop** keeps its incarnation fence (an empty view must still be empty, and the session id must match), so a Stop never cancels work that started after it. It moves to a recorded stop cause in the next change.
- **Memory grants** hold the incarnation they were minted in through a transcript lease: a reset revokes the grant, a compaction successor keeps it. This is the narrower authority and matches the memory audience's own tests.
- Knowledge partitions, the deep-research job store and GitHub publication rows key on transcript ids in stored rows; changing them needs a data migration and is left for a separate decision.

Production code (`src`, `extensions`, without tests): +524 / −602.

## User Impact

- A message queued during a turn is sent when the turn ends.
- A pane that still shows a branch another client switched away from is still refused, and the message is parked as not sent.
- A send to a task session reaches its current run after wakes, yields and compaction; a send to a cancelled task is refused with a reason.
- Deleting a chat drops its unsent queued replies.
- Rows queued by older builds still deliver. A row queued by this build and replayed by an older build has no transcript id; the older build treats it as unavailable and retries it until its retry budget ends.

## Evidence

Base = merge base `edbc5bcea9b6`, head = this PR.

| Journey | Tier | Base | Head |
|---|---|---|---|
| Control UI: queue a message during a held turn, release the turn (send carries only the displayed leaf) | Control UI E2E, real Gateway, scripted provider (`ui/src/e2e/chat-send-leaf-ancestor.real-gateway.e2e.test.ts`) | `chat.send` ok=false, message parked "Not sent" | `chat.send` ok=true, reply "Flag mentioned." |
| Control UI: pane still shows Plan C, another client switches to the Plan B tip, pane sends | same | refused, no provider request | refused, no provider request |
| Compaction successor while a background exec runs, then a next turn | `ocx src` scratch Gateway, real Claude model | exec wake lands in the successor transcript; next turn runs | same |
| Queue an outbound reply, delete the chat, recreate the key | `ocx src` scratch Gateway | 1 queued reply still pending | 0 pending |
| Reaction, then edit a later message | `ocx src` scratch Gateway | reaction still listed | reaction still listed |
| Plant `entry.sessionId === saved` in a new file | `check:session-identity-ratchet` | — | fails: `src/sessions/chat-ref.ts: 1 > 0` |

The task-send fix is proven by unit tests only: a send to an owned task after its run id and generation advanced, with a stale resume binding, is delivered (fails on base), and a send after the task is killed is refused. Real-Gateway journeys with a real Claude model (spawn a task, then send after an exec wake, after compaction, after cancel, or while the task is busy so the send is held and released) did not reproduce the refusal on base: base delivered in each. The live refusal needs the in-process resume binding of a retained self-hold replay, which these journeys do not reach.

Gates: oxfmt, oxlint, `pnpm tsgo:core`, `tsgo:ui`, `tsgo:scripts`, `pnpm check:test-types`, and `ocx test` on the lease, session store, outbound queue, subagent registry, cron, memory audience, session events, chat send and Stop, `sessions_send`, restart resume and UI E2E inventory suites.

`test/plugins/deep-research-routing.gateway.test.ts` fails the same way on `main` (CI run 43939332007, job 133553190527); this PR does not touch it.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit 8d581e9)

Release adaptation: Direct backport; retain baseline event-handoff fixes and adopt ChatRef plus transcript leases consistently.
Follow-up disposition: Active-path admission and requester-event changes retained; focused regression proof recorded in lane report.

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 6191c103b746aa121f5d88f570f760064c6da731)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant