Skip to content

fix(gateway): stop worker reconnect loops on archived thread events - #620

Merged
nixuuu merged 1 commit into
devfrom
fix/gateway-worker-event-recovery
Nov 13, 2026
Merged

nixuuu merged 1 commit into
devfrom
fix/gateway-worker-event-recovery

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

A background task can outlive its thread's archive. When Claude reports its completion by starting a native follow-up turn, the visibility trigger rejects it. The previous ingest drain disconnected the entire worker every 11 seconds and interrupted unrelated running turns.

Archived threads now retain late output for existing turns and background-task state, while completion notes are durably cancelled in the task-end transaction. Background processes keep running; neither the platform note nor a native Claude follow-up starts a new turn after archive. Unarchiving shows the completed task without replaying its continuation. An additive lifecycle snapshot/live message tells capable workers to suppress native wakes and stale StartTurn commands; snapshot ordering preserves newer lifecycle updates.

Gateway ingest resolves older journals automatically: unknown archived turns become durable quarantine notices; deleted threads receive only an ACK cursor, without retaining new payload/history/quarantine or resurrecting the thread. Other storage failures retry per thread with backoff, then quarantine the original event. An unavailable durable fallback retains the worker journal without draining the connection.

WorkerLink protocol remains 1. Old workers recover against the new gateway. New workers encountering an old gateway's ingest drain durably defer all then-unacknowledged journals (the old message identifies no offending thread) and resume them when the gateway is upgraded. Diagnostics recognize failed, dropped, quarantined and deferred ingest templates, redact thread/worker IDs and payloads, and expose aggregate counters. Polish execution, lifecycle, durability, protocol and diagnostics documentation describes the policy.

Validation:

  • All CI checks passed for aa005da: static checks, all four test shards (each through check:tmp), web/Linux builds and aggregate validation.
  • Local install, gen:check, typecheck, lint, format:check and web build passed with nice.
  • Real Claude SDK adapter + SQLite + gRPC: background app server → archive → offline completion/native wake → reconnect; no new turn/drain, unrelated turn survives and finishes, unarchive does not wake the task. Final-head check:tmp: 1 test / 13 assertions passed, temp directory empty.
  • Old-protocol completion/follow-up replay, archive/delete recovery, lost ACKs, gateway/journal restarts, transient retry exhaustion and successful-event ordering: 5 tests / 66 assertions passed locally. Legacy-gateway recovery, existing event-storage regressions and log-export security tests also passed.
  • Local full-suite limitation: the first sequential run failed two bot-files assertions and a bot-handoff timeout under heavy shared-host load; those complete files then passed separately (9/83 and 10/91) and in the second full run. The second full run reached 504 passing tests before computer-moves reported “Laptop C is unreachable” (run 3/20); --bail also bypassed global temp cleanup, so that local full check was not clean. No unrelated production code or test deadlines were changed. The complete CI suite and its temp-directory gates are green.

Copy audit (§8): adds sentence-case diagnostic labels (“Event storage failures”, “Events dropped after deletion”, “Events quarantined”, “Journals deferred until gateway update”) and the plain timeline notice “Automatic follow-up skipped because this thread is archived or deleted.” The notice explains why work did not start and contains no user content or identifiers. No controls/layout changed; no browser verification was performed.

Related: #51

@nixuuu
nixuuu force-pushed the fix/gateway-worker-event-recovery branch 2 times, most recently from 2a59f87 to 6eef63d Compare November 13, 2026 09:42
Isolate event persistence per thread, resolve archived and deleted backlogs durably, and retain legacy delivery until the gateway is upgraded. Add redacted ingest diagnostics and recovery coverage.

Related: #51
@nixuuu
nixuuu force-pushed the fix/gateway-worker-event-recovery branch from 6eef63d to aa005da Compare November 13, 2026 09:45
@nixuuu
nixuuu marked this pull request as ready for review November 13, 2026 10:08
@nixuuu
nixuuu merged commit 464ffff into dev Nov 13, 2026
11 checks passed
@nixuuu
nixuuu deleted the fix/gateway-worker-event-recovery branch November 13, 2026 10:09
nixuuu added a commit that referenced this pull request Nov 13, 2026
Adds owner-scoped manual sidebar positions with one thread write per
move. New threads receive a fractional key before existing threads; old
rows fall back to creation order without a backfill. Adds a separate
last-agent-reply timestamp and device-local sorting preference.

The stack is rebased on dev `464fffffa` (#620). Migration
`0133_thread_order` follows `0132_worker_ingest_outcomes`; the
registration, expected schema list and Polish documentation use 0133.
Number 0134 is left free for the thread-cache warming stack. The
schema/order checks passed 28/28 after this rebase.

Validation (commands run with `nice`): installation, generated-code
check, full typecheck, lint, formatting and web build passed. Focused
gateway order and schema checks passed 28/28; they cover ownership,
invalid/stale bounds, concurrent moves, key growth, old rows, durable
reads, new-thread placement and reply timestamps.

CI passed on all three rebased heads, including every test shard, static
checks and builds. The PRs remain drafts at the maintainer's request.
Native gesture checks are deferred to nightly testing and do not block
this handoff.

Local failure investigation: the earlier full run had 2690 pass, 166
skip and one transient checkout-alias busy-directory rejection. Later
runs under heavy host load hit 5-second deadlines and linked-computer
heartbeat/disconnection failures. None of the ten failing test files is
changed by this stack. All ten were then rerun **one file at a time,
with the default timeouts**, through `nice -n 10 bun run check:tmp`:
**85 pass, 0 fail**. The move file includes all 20 repeated history/file
transfers plus its restart, owner and move-all cases. No production
code, tests or timeout values were changed to obtain these results.
Earlier linked-computer failures also reproduced on original base
`38214a54c` without this feature.

| Isolated file | Passed |
|---|---:|
| `scripts/channel-identity.test.ts` | 3 |
| `scripts/release.test.ts` | 18 |
| `tests/integration/ask-user.test.ts` | 13 |
| `tests/integration/bench-plans.test.ts` | 1 |
| `tests/integration/bot-outgoing-files.test.ts` | 6 |
| `tests/integration/bot-workspace.test.ts` | 4 |
| `tests/integration/bot-view-deltas.test.ts` | 5 |
| `tests/integration/composer-shell-linked.test.ts` | 1 |
| `tests/integration/thread-branches.test.ts` | 8 |
| `tests/integration/computer-moves.test.ts` | 26 |

Every isolated `check:tmp` run reported nothing left in its private temp
directory. The complete local suite was not rerun after this
investigation; the full-suite evidence is the green CI on each current
head.

Remote thread positions remain editable at their home and travel through
incremental metadata replication. Polish API, data-model and
linked-computer docs define grouping and Working behavior.

Copy audit (§8): no UI surface is added in this layer. New gateway
errors state the problem and an action (for example, absent → “The order
changed elsewhere. Refresh the list and try again.”). Sort choice labels
use sentence case and existing terminology. Width/theme checks belong to
the web layer.
nixuuu added a commit that referenced this pull request Nov 13, 2026
…rn (#630)

Two background-task completions could race a dispatched continuation or
a provider wake-up and show a failed Work turn with `THREAD_ERROR: a
turn of this thread is already running`. Notes now follow the existing
steer/queue policy: a running turn receives a platform steer; notes
arriving while its StartTurn is dispatched wait durably for turn_started
instead of creating another continuation or speculative steers.
Otherwise notes queue a continuation. Notes in a batch keep `(ended_at,
item_id)` order.

Worker admission serializes StartTurn per thread without blocking
control commands. Durable admission receipts prevent the same turn from
executing again after restart, even with a fresh command ID. The gateway
atomically requeues busy refusals with the same turn, input, note
bindings, ordinal and expiry, logs the retry, and increments diagnostics
metric 44. Recovery also dispatches note turns/steers committed before a
gateway crash.

Related: #620, merged into `dev`; this branch is rebased onto
`464fffffa`. This builds on its task-end cancellation, ingest queue,
ThreadLifecycle snapshots/deltas and Claude wake suppression, without
changing `core-workers.ts` or those mechanisms. The stop query in
`thread-lifecycle.ts` additionally interrupts dispatched queued starts
before the running turn. Worker admission retains the lifecycle guard
after waiting and rechecks it before provider admission; durable
interrupt receipts also fence a cancelled turn after unarchive/restart.
Diagnostics 41–43 remain unchanged; this adds 44. No PostgreSQL
migration or protocol change is added.

Validation:

- All CI checks are green for
`4865aa66fbd88dbd10abe40369c6eb1be787d369`: static checks, all four test
shards through `check:tmp`, web/Linux builds, aggregate validation and
commitlint. [CI
run](https://github.com/itsoltech/canopy/actions/runs/43409611147).
Tests (2/4) passed after one rerun of a missed SQL lock-wait observation
in `thread-range-send-locks.test.ts`; that test also passed in the local
full run without changes.
- Local handover checks ran with `nice -n 10`: install, gen:check,
typecheck, lint, format:check and web build passed. The worktree is
clean.
- Focused gRPC runs passed: 16 collision/background-note tests; 13
collision/admission tests covering ordered execution, controls,
gateway/worker restart deduplication, archive, unarchive and new work.
The real Claude archived-background regression from #620 and journal
receipt tests also passed. Temp directories were clean.
- Local full sequential `check:tmp --timeout 30000`: 2722 passed, 166
skipped, one failed across 509 files; temp directory clean. The
unrelated `config-sync-projects.test.ts` origin-attachment assertion
returned `attached=false`. Its entire file then passed separately (12
tests, 92 assertions), with temp cleanup clean. The complete CI suite is
green; no unrelated production code or test deadlines were changed.

Copy audit (§8): no metric → “Turn starts requeued”, a sentence-case
diagnostics counter explaining internal retries. Existing “Could not
finish this turn” remains for actual failures; busy collisions no longer
produce it. No controls or layout changed.
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