Skip to content

feat(chat): faithful replay of durable runs — parts accumulator, catch-up watermark, card fidelity - #10910

Closed
CelesteHeartsong wants to merge 1 commit into
unslothai:mainfrom
CelesteHeartsong:feat/durable-replay-fidelity
Closed

CelesteHeartsong wants to merge 1 commit into
unslothai:mainfrom
CelesteHeartsong:feat/durable-replay-fidelity

Conversation

@CelesteHeartsong

@CelesteHeartsong CelesteHeartsong commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

Faithful replay of durable runs: a reopened tab renders tool cards at their offsets, thought cards with their tags and seconds, a still-parked call with its Approve/Deny buttons, and a still-running call's streamed stdout — instead of flattening everything to one string and re-parsing per publish. A catch-up watermark means you open on where the generation is, not on a re-type from character zero.

What

  • Parts accumulator (createRecoveryReplay): folds persisted frames into a part array one at a time, stamping each tool call with the offset it ran at. Text/reasoning runs extend in place; tool frames land on the card their id names. Publish is gated on a changed flag instead of firing per event.
  • Catch-up watermark: frames written before this tab attached fold silently into the accumulator; ONE write lands at the live edge. From there every frame is genuinely live and shows as it arrives.
  • Thought fidelity: replayed thoughts keep their own tags (the replay closes only blocks IT opened), their durations are timed against the frames' own createdAt, and a mid-flight thought joined by a second tab is timed by both.
  • Tool card fidelity: results land as the same object a watched card rendered (both readers call one shaper); artifact envelopes split off before output-preference so files and images ride along; a still-running call's stdout is visible to the reopening tab.
  • Parked approval re-raise: a call still parked when the tab closed sits in storage with its tool_start at or below the follower's cursor — the seed itself re-raises it, keyed ${scopeId}:${approvalId} exactly as live keyed it, and Approve/Deny renders in THIS tab.

Depends on

Tests

  • chat-generation-replay-parts.test.ts — cards keyed by minted ids, offset placement, fuller-output preference
  • chat-generation-replay-durations.test.ts — seeded groups, frame-clock timing
  • generation-recovery-catch-up.test.ts — single catch-up write, watermark never drags backwards
  • generation-tool-recovery.test.ts — parked approval re-raise, stdout visibility
  • durable-gate.test.ts — gate truth table (carried from feat(chat): make agentic turns survive tab-close #10365)

@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: e48e4f4b84

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

This carries the whole of #10365 as well as the replay work, and it also flips UNSLOTH_STUDIO_DURABLE_TOOL_TURNS on by default in studio/backend/routes/chat_generation_runs.py so every tool-enabled turn leaves the legacy stream. Could you land #10365 first and rebase so this is the replay-only diff, and confirm the approval park timeout drop from 3600s to 300s in state/tool_approvals.py is intended?

@danielhanchen

Copy link
Copy Markdown
Member

Frontend build + bundle sanity and Frontend unit tests (Windows) fail on the same single subtest, so it is one issue on both:

not ok 727 - a turn that fails before any text is still saved as interrupted
  location: studio/frontend/tests/chat-continuation.test.ts:1825:1
  The input was expected to not match the regular expression /if \(partialContent\.length > 0\)/

That test reads the generator source and asserts the partial-save path is not wrapped in a partialContent.length > 0 guard, which is what makes a turn that fails before emitting any text still get saved as interrupted. This branch reintroduces that guard around the yield, so a zero-length partial is dropped and nothing is saved.

Worth deciding which side is right before changing either: if the accumulator genuinely needs the guard, the test is the thing to revisit, but the behaviour it is protecting (an immediate failure still shows as interrupted rather than vanishing) looks deliberate.

The Core failures on this PR were unrelated, from unsloth-zoo main, and are fixed by unslothai/unsloth-zoo#1219.

@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

This carries the whole of #10365 as well as the replay work, and it also flips UNSLOTH_STUDIO_DURABLE_TOOL_TURNS on by default in studio/backend/routes/chat_generation_runs.py so every tool-enabled turn leaves the legacy stream. Could you land #10365 first and rebase so this is the replay-only diff, and confirm the approval park timeout drop from 3600s to 300s in state/tool_approvals.py is intended?

Yep yep, the plan was to land #10365 first (this PR was a split from that PR for just the replay section)

I believe both of your issues are inherited form #10365 as well.

state/tool_approvals.py and routes/chat_generation_runs.py are byte-identical between this branch and #10365, so once #10365 lands and I rebase, the DURABLE_TOOL_TURNS default flip and the park-timeout change both drop out of this diff — they'll have landed with #10365.

On the 300s:

yes, intended. It only applies to durable/parked runs (cancel_event.durable); the legacy path still uses the 3600s ceiling. A parked approval denying after ~300s (env-tunable via UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S) is the deliberate tradeoff — it feeds the rejection back to the model so an unattended agent adapts and keeps going instead of holding its slot for the full lease window. If you think 300s is too aggressive for "grab your phone," I'd rather set that number in #10365 where it's introduced.

@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Frontend build + bundle sanity and Frontend unit tests (Windows) fail on the same single subtest, so it is one issue on both:

not ok 727 - a turn that fails before any text is still saved as interrupted
  location: studio/frontend/tests/chat-continuation.test.ts:1825:1
  The input was expected to not match the regular expression /if \(partialContent\.length > 0\)/

That test reads the generator source and asserts the partial-save path is not wrapped in a partialContent.length > 0 guard, which is what makes a turn that fails before emitting any text still get saved as interrupted. This branch reintroduces that guard around the yield, so a zero-length partial is dropped and nothing is saved.

Worth deciding which side is right before changing either: if the accumulator genuinely needs the guard, the test is the thing to revisit, but the behaviour it is protecting (an immediate failure still shows as interrupted rather than vanishing) looks deliberate.

The Core failures on this PR were unrelated, from unsloth-zoo main, and are fixed by unslothai/unsloth-zoo#1219.

Agreed the behavior is the deliberate one — an immediate failure should read as interrupted rather than vanish. The guard was the bug, so I removed it in #10365 (6946ed1) and kept the test. This branch still carries the pre-fix guard (it branched before that commit), which is why this subtest fails here; once #10365 lands and I rebase onto main it picks up the fix and goes green with no change on this side. Happy to cherry-pick 6946ed1 onto this branch now if you'd rather see it red-free sooner than waiting on the merge.

And noted on Core — thanks, that's the unsloth-zoo-main issue (unslothai/unsloth-zoo#1219), unrelated to this work.

@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Cherry-picked 6946ed1 onto this branch (c82ba28) so the frontend reds clear without waiting on #10365's merge — a turn that fails before any text is still saved as interrupted passes now, and both source-pin suites are green (chat-continuation 94/94, stuck-generation-run 34/34). That should take Frontend build + bundle sanity and Frontend unit tests (Windows) to green; the only reds left will be the three Core checks from unsloth-zoo main (unslothai/unsloth-zoo#1219). The later rebase onto main will absorb this same change, so expect a trivial already-applied dedupe there.

CelesteHeartsong pushed a commit to CelesteHeartsong/unsloth that referenced this pull request Sep 22, 2026
…ai#10365 conflicts)

unslothai#10365 landed on main after this branch forked from the shared dev base, so its
reviewed durable-turns code collided with the copy carried here. Resolved per file:

- backend state/tool_approvals.py, tests/test_tool_approvals.py and
  tests/studio/test_desktop_reliability_frontend_contract.py: take main's reviewed
  version (this PR has no unique work in them).
- core/inference/chat_generation_runs.py: take main's reviewed park/lease logic.
- routes/chat_generation_runs.py, durable-gate.ts, durable-gate.test.ts and
  test_multi_chat_prompt_queue_contract.py: keep THIS PR's media-turns work -
  UNSLOTH_STUDIO_DURABLE_MEDIA_TURNS toggle (default on), media removed from the
  frontend gate, and the matching "media turns are durable" assertions.
- chat-adapter.ts: keep shapeToolResult shaper + media-durable gate args; take main
  for import formatting and the partial-yield block (unslothai#10811 removed that guard); drop
  the duplicate local isMcpImageToolResult (imported from utils/tool-result-shape).
- runtime-provider.tsx: keep this PR's self-consistent recovery impl.
- generation-tool-recovery.test.ts: union the mock store stubs.

REVIEW NOTE: main's media-refusal branch also detects input-audio / embedded-image /
video inside messages[] (added after this branch forked); this branch's toggle-off
refusal only checks top-level _MEDIA_FIELDS. Decide whether to fold those in.

Follow-ups before landing: tsc -b + npm test; backend test_chat_generation_runs +
test_tool_approvals; reconcile the shared replay files with unslothai#10910 (keep unslothai#10910's).
CelesteHeartsong pushed a commit to CelesteHeartsong/unsloth that referenced this pull request Sep 22, 2026
…ngine

unslothai#10910 replaced createGenerationToolRecovery's arm/disarm lifecycle and the
client-side parseThinkTags flag with createRecoveryReplay (frame-type reasoning
split + toolConfirmations register/resolve). Update literal-think-when-thinking-off
and seeded-approval-terminal-run to pin that new wiring instead of the removed call sites.
CelesteHeartsong pushed a commit to CelesteHeartsong/unsloth that referenced this pull request Sep 22, 2026
…ngine

unslothai#10910 replaced createGenerationToolRecovery's arm/disarm lifecycle and the
client-side parseThinkTags flag with createRecoveryReplay (frame-type reasoning
split + toolConfirmations register/resolve). Update literal-think-when-thinking-off
and seeded-approval-terminal-run to pin that new wiring instead of the removed call sites.
@danielhanchen

Copy link
Copy Markdown
Member

This still carries #10365 alongside the replay work in chat-generation-replay.ts, so could you rebase it down to the replay-only changes once #10365 lands? The chat-continuation partial-save test also needs the zero-length guard around the yield removed again.

…h-up watermark, card fidelity

Rebased onto main after unslothai#10365 landed, so this is the replay-only delta: no durable-gate, approval-timeout, or DURABLE_TOOL_TURNS changes. A reopened tab renders tool cards at their offsets, thought cards with their tags and seconds, a still-parked call with Approve/Deny, and a still-running call's streamed stdout; the catch-up watermark opens on where the generation is rather than re-typing from character zero.
@CelesteHeartsong
CelesteHeartsong force-pushed the feat/durable-replay-fidelity branch from 18cb65a to 0fd68b0 Compare September 23, 2026 02:08
@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Rebased onto current main, which now has #10365, so this PR is a single commit containing only the replay work. None of #10365's changes (state/tool_approvals.py, routes/chat_generation_runs.py, durable-gate.ts) are in this diff anymore.

On the partial-save guard: it's removed from chat-adapter.ts, there's no if (partialContent.length > 0) wrapping the yield { content: partialContent, … incomplete: { … } }. Locally chat-continuation is green 110/110 including "a turn that fails before any text is still saved as interrupted", and the full frontend suite passes with 0 failures. Waiting on CI to confirm.

@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Updated the description to remove stated dependency on #10406. This PR now contains aspects of the replay solution that are also being used in the other PR, but that functionality is more fitting to be worked on and completed in this PR.

Once this PR is finished then we can wrap up this whole project with the durable media turns in #10406.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this work. Since this branch was cut, main has absorbed most of it: the catch-up publish gate landed in #12464, and seeded approval re-raise, tool-card offsets and result shaping live in createGenerationToolRecovery (#10365, #10584, #12442, #9301). Reasoning durations now arrive from the server via reasoning_summary.

Rebased as-is, this diff would remove main's terminal-run guard and pending check on seeded approvals, the parseThinkTags detection, and the replayFrom rewind from #10584, and its tool-result shaper predates MCP Apps ui.

The remaining piece, showing a still-running tool's stdout to a reopening tab, would be very welcome as a small focused PR on top of current main's recovery. Closing this one in favor of that.

@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Thank you for getting the functionality merged in! I've noticed on main already that the main issue I was trying to resolve is fixed. (Phone going to sleep used to cancel running generations)

@CelesteHeartsong
CelesteHeartsong deleted the feat/durable-replay-fidelity branch October 7, 2026 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants