Skip to content

fix(desktop): restore WPP thread continuation - #73

Closed
trmosala wants to merge 1 commit into
devfrom
bridge-turn-fixes
Closed

trmosala wants to merge 1 commit into
devfrom
bridge-turn-fixes

Conversation

@trmosala

Copy link
Copy Markdown
Owner

Related issue (optional)

None. Follows the bridge review findings for the duplicate preamble, the strict thread echo check and the repeated capability probe.

Companion PR: https://github.com/trmosala/CM-WPP-Bits/pull/2 ports the same fix to the extracted bridge package. That keeps the fix when the CookieMonster split removes packages/desktop/src/main/wpp-bridge/ from this repo.

Overlaps #72 in openaiCompat.mjs and AGENTS.md, on different lines. #72 changes the parser call sites and the normalizer entry. This PR changes finalizeStreamedCompletion and the sessionThreads.mjs entry.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Restores WPP thread continuation and removes an extra WPP request from fresh turns:

  • Duplicate preamble. On a streamed tool turn, sentence quarantine sends the prose before the first tool-call marker live. finalizeStreamedCompletion then wrote the whole preamble again, and OpenCode stored the doubled text. Finalize now writes only the part of the content that wasn't streamed, then the tool-call chunks. Recovery replays and turns that streamed nothing still get the full content.

  • Thread echo check. sessionThreads.mjs compared the committed response with the client's replay of it byte for byte. That replay differs in harmless ways:

    • it keeps the whitespace before a tool call that the normalizer trims
    • it sends "" (V1 SDK) or null (V2) for a tool-only turn
    • it re-serializes tool arguments in its own key order

    The doubled preamble never matched either, so every turn fell back to a fresh replay of the full transcript. The echo hash now ignores surrounding whitespace in assistant text, treats null and "" alike, and compares arguments as JSON values. Different text, arguments or ids still force a fresh replay.

  • Capability probe. ensureProtocolCapability skipped the probe only on continued turns, so every fresh job sent an extra WPP model request for the handshake first. A worker that passed for an agent now skips the probe on fresh jobs too. claim() still clears the result when the worker switches agents, and content.js still opens a fresh chat for every job that doesn't continue a thread.

Trade-off: if the WPP instruction is removed from an agent while a worker is alive, that worker no longer notices until it is retired. New workers still probe.

The sessionThreads.mjs entry in AGENTS.md now describes the looser echo check. This PR doesn't change the protocol markers, the stream gate, authentication, browser grants, logging, dependencies or release workflows.

How did you verify your code works?

  • bun test src/main/wpp-bridge/ from packages/desktop: 504 pass, 0 fail, including 4 new tests:
    • a preamble streamed live before its tool call reaches the client once
    • a tool turn replayed with extra whitespace and reordered arguments still continues
    • a tool-only turn committed with null content continues from ""
    • two fresh jobs on one worker and agent probe once
  • bun typecheck from packages/desktop: pass.
  • oxlint on the six changed files: 0 errors. The 8 warnings are on lines this PR doesn't touch.
  • Prettier: the sessionThreads and worker-pool files are clean. The two openaiCompat files keep their existing semicolon style.
  • Not run: a live WPP or Electron session. Run logs don't record the thread mode, so the real continuation rate isn't measured.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

A streamed tool turn sent its preamble twice. Sentence quarantine releases
the prose before the first tool-call marker live, and finalize then wrote
the whole preamble again. OpenCode stored the doubled text, so the next
turn's assistant echo never matched the committed response and every turn
replayed the full transcript. Finalize now writes only the unsent part of
the content whenever prose already streamed, then the tool-call chunks.

The echo check now ignores differences the client introduces when it
replays a turn: surrounding whitespace in assistant text, an empty string
or null for a tool-only turn, and the key order of re-serialized tool
arguments. Different text, arguments or ids still force a fresh replay.

A worker that passed the capability handshake for an agent no longer
probes again on every fresh job. The handshake checks the agent's
installed WPP instruction, which a fresh chat does not change, and
claim() still clears the result when the worker switches agents.

The sessionThreads entry in AGENTS.md describes the looser echo check.
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

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.

1 participant