Skip to content

Entering a session can rewind a running conversation's text and resurrect deleted messages #824

Closed

Description

What happens

loadMessages in frontend/workspace/src/context/sync.tsx guards against an in-flight snapshot overwriting newer streamed state:

// SSE keeps streaming while this request is in flight. Entities it
// changes meanwhile are newer than the response bytes and must win;
// otherwise entering a streaming session rolled its text backwards.
const startedAt = globalSync.transcript.revision(input.directory, input.sessionID)
...
const changes = globalSync.transcript.changesSince(input.directory, input.sessionID, startedAt)

input.directory is the route scope. Both call sites set it from sdk.scope:

const directory = sdk.scope
...
? loadMessages({ directory, ... })

and sdk.tsx:17 defines that as:

const scope = createMemo(() => props.scope ?? props.projectID ?? props.directory)

But the ledger is written under the event stream's physical directory — global-sync.tsx:1065 takes const directory = e.name off the SSE event, and markTranscriptMutation(directory, ...) keys the ledger with it. transcriptKey is a raw join with no folding:

const transcriptKey = (directory: string, sessionID: string) => `${directory}\0${sessionID}`

So the read key and the write key are in different namespaces whenever a projectID is present — which is the normal way a project session is opened. revision is permanently 0 and changesSince permanently returns empty sets.

Why it is wrong

The guard exists for exactly this case, and with empty preferCached / removed mergeHydratedMessages lets the older HTTP snapshot overwrite the newer streamed state and resurrects messages SSE deleted. Run against the real helper:

A) guard INERT (empty preferCached): [{"id":"msg_1","text":"older HTTP snapshot text"}]
B) guard WORKING:                   [{"id":"msg_1","text":"newer streamed text"}]
C) removal, empty removed:          [{"id":"msg_1"},{"id":"msg_2"}]   <- msg_2 deleted by SSE, back
D) removal, removed populated:      [{"id":"msg_1"}]

The user sees a running conversation's text rewind while the agent is still typing, and deleted messages flicker back.

The reconnect path in the same file (global-sync.tsx:994,997) uses the same physical key as markTranscriptMutation and is therefore correct — only the normal hydration call site is wrong. sync.tsx:23 also uses globalSync.child(sdk.directory, …), so events do reach the store correctly; it is purely the ledger lookup that is mis-keyed.

Reproduction

Proven end-to-end with a mounted provider stack, a held-open GET /session/{id}/message, and a message.part.updated frame arriving while the request is in flight:

(fail) a snapshot that predates a streamed part does not roll the transcript back
  Expected: ["the streamed, newer text"]
  Received: ["the older snapshot text"]

After the fix the same test keeps "the streamed, newer text".

Suggested fix

Thread the stream's directory alongside the store key and read the ledger through it:

const loadMessages = async (input: { directory: string; streamDirectory: string; ... }) => {
  const startedAt = globalSync.transcript.revision(input.streamDirectory, input.sessionID)
  ...
  const changes = globalSync.transcript.changesSince(input.streamDirectory, input.sessionID, startedAt)

with streamDirectory: sdk.directory at both call sites. input.directory still keys the inflight map and the store, which is correct — only the ledger needed the physical directory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions