Skip to content

feat(chat): thread message queue — queued bubbles, cancel/run-now, stop always frees the thread - #4365

Merged
tlgimenes merged 28 commits into
mainfrom
feat/thread-message-queue
Jul 8, 2026
Merged

tlgimenes merged 28 commits into
mainfrom
feat/thread-message-queue

Conversation

@tlgimenes

@tlgimenes tlgimenes commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Surfaces the thread's message queue in the chat UI and makes a thread never brickable from the user's seat. Queued messages stack in a tray above the composer (Cursor-style): each row shows the message text with ✏️ edit / 🗑️ remove / ↑ send-now actions, and the message enters the chat body at the exact moment its DBOS gate workflow starts executing it. The composer never disables, and the stop button now guaranteedly frees the thread — including from a zombie PENDING gate (the in-product version of the manual DBOSClient.cancelWorkflow unbrick).

The DBOS thread-gate queue (partition=threadId, concurrency=1) already is the message queue — an ENQUEUED thread-run:<threadId>:<messageId> workflow is a queued message, with its parts durably persisted at POST. This PR exposes that queue rather than building a parallel one. The tray is server-durable: close the tab and the queue still drains; reload rebuilds the tray (hydrated from persisted parts); other clients see the same queue.

Correctness foundation (first commits)

Fence-at-dispatch: POSTing turn B while turn A ran used to mint + persist a fresh fence at POST time, superseding A's live fence → A's projector skipped projection → A's reply was silently lost. The gate's claimRunFenceForDispatch (already the automations path) is now the sole fence authority, minting only while holding the partition slot. Net −126 lines; regression e2e proven to fail against the pre-fix code.

How the tray works

  • Send while a run is active → the message POSTs as today (parts persisted + gate workflow ENQUEUED) and appears in the tray; the full original message is stashed client-side. It does NOT render in the body (a pre-pairing filter also hides DB-fetched queued rows on reload).
  • Dispatch-time transition: the RUN_STARTED reactor's decopilot.thread.status {in_progress} event — the only dispatch-time push signal covering BOTH topologies (hosted decopilot and user-desktop CLI) — now carries message_id. The client flips exactly that tray item into the body with zero fetches (stash applied via the deduped merge path), then the turn streams normally.
  • Tray actions: ↑ send-now (first row only; cancels the current turn, FIFO promotes), 🗑️ remove (cancels the ENQUEUED workflow + deletes the persisted parts + drops any preloaded local row), ✏️ edit (inline; cancel + re-POST → queue tail; draft stays visible until the flow resolves and survives failures; hidden for attachment-bearing messages).
  • Unbrick guarantee: stop = full teardown + cancelThreadGateHead — frees the concurrency=1 partition even when the gate is a zombie whose in-memory continuation is gone.
  • GET /:org/decopilot/queue/:threadId returns {workflowId, messageId, status, enqueuedAt, text, hasAttachments}; POST …/queue/:threadId/cancel/:workflowId removes one item.

Review process

Built task-by-task with per-task adversarial reviews + two whole-branch reviews (one per iteration; the tray iteration reworked the earlier in-body-bubble UX in place). Every Critical/Important finding fixed pre-push, including: flip idempotency vs SSE-reconnect refetch (mergeAndSort dedupe), edit draft preservation across cancel/POST failures, send-latch draft loss, ghost-bubble resurrect on remove/edit of preloaded rows.

Tracked follow-ups (none blocking): queue-refresh ordering epoch guard; queue-cancel TOCTOU on a stale status read; pendingBodies stash leak on legacy no-message_id runs; owner-only GET /queue (thread viewers don't see queue state); second-client tray liveness at refresh boundaries; phantom edit-row lingering (cosmetic).

Notes for reviewers

  • DBOS discipline: one snapshot re-baseline in the whole branch (iteration 1's fence fix — comment + dead-helper removal, recovery-compatible); the tray iteration touches zero workflow-source files. No DBOS_WORKFLOW_VERSION bump.
  • Non-queue regression surface: with zero queued messages, the tray renders null, the body filter short-circuits to the same array reference, and pair rendering is byte-equivalent to main's structure.
  • ⚠️ Watch the e2e job: two new Playwright scenarios (queue text hydration, edit-requeues-at-tail) execute for the first time in this CI run. The rest of the suite (fence regression, visibility, remove-deletes-parts, run-now, stop-frees-slot, authz) already passed on real infra in the previous run.

Testing

  • Unit: 365+ chat-suite tests incl. flip idempotency, quiet-enqueue invariants, hydration fold, composer action, reconcile (8), message_id plumb.
  • bun run check, fmt:check, lint (0 errors), knip clean.
  • Storage-integration + Playwright suites in CI.

🤖 Generated with Claude Code

tlgimenes and others added 28 commits July 8, 2026 15:49
A message queued behind a running turn minted+persisted a fresh fence at
POST time, superseding the running turn's fence and making the projector
skip its projection (reply stranded, terminal status unwritten). The gate's
claimRunFenceForDispatch — already the automations path — is now the sole
fence authority.

Co-Authored-By: Claude Fable 5 <[email protected]>
GET /queue/:threadId lists the thread's pending gate workflows; POST
/queue/:threadId/cancel/:workflowId removes one (deleting a queued user
turn's persisted parts). The stop endpoint's shared teardown now also
cancels a PENDING gate head, so stop always frees the partition — the
in-product version of the manual DBOS unbrick.

Co-Authored-By: Claude Fable 5 <[email protected]>
A second draft typed while the previous send's POST was in flight was
silently dropped by the per-thread send latch while the composer cleared
it anyway — silent input loss. The composer now probes the latch
synchronously and keeps the draft (with a toast) instead. Also refresh a
stale fence comment from the fence-at-dispatch change.

Co-Authored-By: Claude Fable 5 <[email protected]>
knip: the salvaged export had no external consumer — refreshMessageQueue
is its only caller, same module.

Co-Authored-By: Claude Fable 5 <[email protected]>
… tier

The salvaged test posted harnessId decopilot, which resolves a model tier
at POST time — 400 TierUnavailableError in the providerless e2e org.
Externalization is harness-agnostic; use the suite's claude-code +
tunnel-daemon pattern like every other live test in this spec.

Co-Authored-By: Claude Fable 5 <[email protected]>
The durable gate input no longer carries message content at all (the gate
reloads history at dispatch), so 'externalized into the input' is an
obsolete contract. Pin the real ones: content-free workflow input +
mesh-storage refs (no raw base64) in the persisted request parts.

Co-Authored-By: Claude Fable 5 <[email protected]>
The RUN_STARTED reactor emit is the only dispatch-time push signal that
fires for both topologies; carrying the dispatched user message's id lets
the queue tray flip exactly that item into the chat body with no fetch.

Co-Authored-By: Claude Fable 5 <[email protected]>
The tray displays queued messages (the body no longer does), so GET /queue
joins the persisted parts for display text and edit-eligibility. Also stop
publishing waiting-runner for sends queued behind a live run (it re-lit the
running turn's status indicator mid-stream).

Co-Authored-By: Claude Fable 5 <[email protected]>
conn.enqueue no longer renders optimistically; the full message is stashed
and applied to the body when the in_progress event carries its message_id
(both topologies). Server list refreshes reconcile the tray.

Co-Authored-By: Claude Fable 5 <[email protected]>
Queued turns no longer render as in-body bubbles; the pre-pairing filter
also hides DB-fetched queued parts on reload. Removes the queuedInfo
threading, spinner affordances, and liveIndex routing — plain last-pair
rendering returns.

Co-Authored-By: Claude Fable 5 <[email protected]>
…dited

A reload/refetch can preload a queued message's persisted row into the
local store (render-hidden only while queued). Cancelling or editing it
then unhid a stale forever-unanswered bubble — remove the local row once
the server confirms the cancel.

Co-Authored-By: Claude Fable 5 <[email protected]>
…t base

Sibling of maxCreatedAtMsForRun, scoped to one message_id. Lets the
projector anchor a queued turn's assistant reply to its OWN request
message instead of the run-wide max, which a later-queued turn can
already have pushed forward by projection time.

Co-Authored-By: Claude Fable 5 <[email protected]>
The durable projector seeded the assistant PartEmitter's created_at
base from the run-wide max part timestamp. Since run_id === threadId
for every part in a thread, a queued turn that dispatches late (gate
concurrency=1) would find a LATER turn's user message already
persisted at projection time and stamp its own reply after it -
scrambling render order and dropping the reply from the next turn's
history context.

Thread the request messageId down to createAssistantEmitter (the sole
writer of the assistant base) through consumeRunProjection ->
projector-workflow -> run-persistence, and prefer that message's own
max created_at when present, falling back to the run-wide max for
legacy callers or a message with no parts yet.

This touches thread-gate-workflow.ts, a registered DBOS workflow, so
the source-guard snapshot is re-baselined. DBOS_WORKFLOW_VERSION is
untouched: this only adds an argument to an existing step's closure,
no step is added/removed/reordered, and the step's recorded output
(void) is unchanged - recovery-compatible.

Co-Authored-By: Claude Fable 5 <[email protected]>
Extends the existing running-head/queued-tail scenario to assert the
full interleaved message order (u1, a1, u2, a2) via the same folded
read path (COLLECTION_THREAD_MESSAGES_LIST) the chat UI renders from.
Regression coverage for the queue-ordering fix in run-persistence.ts /
projector-workflow.ts - runs in CI only (needs Postgres + NATS + the
fake tunnel-link daemon).

Co-Authored-By: Claude Fable 5 <[email protected]>
Address review: replace the untyped `(ctx.request as { messageId?: string })`
cast at the projection call with a `"messages" in ctx.request` discriminant
(the same signal isDurableDispatchRunInput uses). The messageId read now stays
compile-time-linked to DurableDispatchRunInput, so a future rename breaks the
build here instead of silently reverting queued replies to run-wide-max
ordering (which only CI e2e would catch). Also correct the plumb test's comment
to reflect what it actually asserts. Snapshot re-baselined; no version bump.

Co-Authored-By: Claude Fable 5 <[email protected]>
Tray container now uses the composer's exact surface (rounded-2xl,
bg-card / dark:bg-muted) instead of rounded-lg + bg-background, with
overflow-hidden so the header clips to the radius.

Remove the per-row Edit affordance: DBOS workflow ids are once-ever, so
edit could only cancel + re-POST — landing at the queue TAIL, not in
place, which reads as a bug to users. Rows keep Remove and the head-only
Send now. The editQueuedMessage composition stays in chat-context for a
future in-place design; the API-level edit e2e still guards the
cancel + re-POST server contract.

Co-Authored-By: Claude Fable 5 <[email protected]>
Make the tray's FIFO semantics legible (design settled via visual
companion): rows are uniform `number chip · text · ✕`, and the promote
action moves off the head row into the header — "N queued" left, a ghost
↑ button right with a "Send now" tooltip, wired to the same stop() the
composer uses (cancel current run → gate FIFO promotes the head). The
number chip takes dark:bg-background so it stays visible against the
tray's dark bg-muted surface.

Co-Authored-By: Claude Fable 5 <[email protected]>
@tlgimenes
tlgimenes force-pushed the feat/thread-message-queue branch from 2ccda9b to c3925b9 Compare July 8, 2026 18:50
@tlgimenes
tlgimenes enabled auto-merge (squash) July 8, 2026 18:50
@tlgimenes
tlgimenes merged commit dffa9ed into main Jul 8, 2026
14 checks passed
@tlgimenes
tlgimenes deleted the feat/thread-message-queue branch July 8, 2026 18:56
decocms Bot pushed a commit that referenced this pull request Jul 8, 2026
PR: #4365 feat(chat): thread message queue — queued bubbles, cancel/run-now, stop always frees the thread
Bump type: minor

- decocms (apps/mesh/package.json): 3.105.1 -> 3.106.0
- @decocms/e2e (packages/e2e/package.json): 0.8.0 -> 0.9.0
- @decocms/mesh-sdk (packages/mesh-sdk/package.json): 1.16.0 -> 1.17.0

Deploy-Scope: both
tlgimenes pushed a commit that referenced this pull request Jul 16, 2026
…TS.md (#4475)

Mined ~300 merged PRs for the delta between the first PR proposal and the
work added before merge. The dominant shape: a first draft ships the happy
path, then a hardening pass (often a different engineer who takes over the
branch and merges — the vibecoder→engineer handoff) adds the same categories
every time. Encoded those 9 categories as an actionable first-pass checklist,
each citing a real PR (#4008, #4230, #4365, #4409, #4426, #4445, #4357, #4355,
#4449, #4350, #4373, #4416, #4446, #4461, #4134, #4469).

Covers: variant/edge-case handling, tenant/permission scoping, concurrency &
silent-data-loss, test-per-fix + invert-bug-tests + real-PG, dead-code/knip,
lifecycle symmetry + state reset, kill-switch/GC/git-exclude for risky infra,
input validation vs over-engineering, compile-time type-safety.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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.

1 participant