Skip to content

feat(pr-watch): persist host polling and session wake receipts - #9693

Open
Dante-dan wants to merge 28 commits into
code-yeongyu:devfrom
Dante-dan:feat/9493-persistent-pr-watch
Open

Dante-dan wants to merge 28 commits into
code-yeongyu:devfrom
Dante-dan:feat/9493-persistent-pr-watch

Conversation

@Dante-dan

@Dante-dan Dante-dan commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Persist OpenCode PR watches so a session can register once and receive actionable observations across host restarts. This implements the OpenCode runtime slice of #9493 and includes #9684's batched fingerprint prerequisite while that PR remains open.

Changes

  • Shared polling and transport: deduplicate each PR's fingerprint/detail/activity reads across sessions, use one REST/GraphQL transport per GitHub host, paginate complete observations, cache credentials, honor ETags and rate limits, and pin GHES requests to the project remote or explicit GH_HOST. Host-qualified PR URLs are rejected before authentication if they do not match the project host.
  • Cross-process coordination: reuse the existing team-core file-lock protocol for registry transactions and one host-owner lock spanning polling and receipt/dispatch. A persisted polling deadline prevents consecutive processes from repeating a pass; a persisted retry deadline prevents immediate ambiguous redispatch.
  • Private, bounded persistence: create the registry directory as 0700 and its exclusive temporary/state file as 0600; fsync before replacing the snapshot. Remove delivered outbox entries, stopped registrations and orphan snapshots, including residue left by earlier versions. Retain only current-observation dedupe keys; generation tokens prevent stale in-flight reads from reviving a removed watch.
  • Final wakes and durable delivery: preserve earlier pending wakes when a PR closes or merges, queue the final pass's events and terminal notice, then stop polling. Treat failed receipt reads as errors, persist ordered OpenCode message IDs before dispatch, and acknowledge only a durable first-party receipt. Keep the original receipt identity for older pending wakes. A corrupt/future registry remains intact and no longer takes the whole monitor down; failed scheduling reads retry.
  • Transition and tool behavior: give late subscribers their own initial green/conflict observation, preserve known conflict state through UNKNOWN, escape untrusted prompt lines, and expose native watch/list/unwatch tools. The watcher reports observations; the agent retains all merge decisions.
  • Remove all 17 new non-null assertions identified by CI, and resolve the four unused-export findings exposed by the subsequent knip step without disabling either check.

QA & Evidence

  • Focused existing/regression suites: Bun 1.4.4, 52 pass; 0 fail; 177 expect() calls, across watcher, transport, monitor and fingerprint suites. Covers both terminal states, late subscribers, UNKNOWN conflict suppression, multiline check names, failed receipts, accepted dispatch followed by restart before acknowledgment, legacy receipts, message ordering, idle legacy cleanup, private file modes and 500 acknowledged events remaining below 1500 bytes. Two actual child runtimes preserve both registrations while producing one poll and one dispatch. Artifact: .omo/evidence/20261105-9693-review/focused-knip-fix.txt.
  • Real OpenCode 1.18.34 source-plugin QA: a local model invokes the native watch tool in an isolated XDG project. A controlled pending terminal wake survives two host restarts: first receipt count 1, second receipt count 1, outbox entry and stopped registration removed. This is a controlled persisted event, not a claimed live GitHub transition. Real session DB remains absent -> absent; every QA child exits. Sandbox rules deny writes to the real OMO config and OpenCode DB. Artifacts: .omo/evidence/20261105-9693-review/live07/{live-result,isolation}.json.
  • Type checks/build: complete bun run typecheck passes after the export cleanup. Full bun run build passes at the runtime revision with Bun 1.4.4 / Node 26 and the repository's existing OMO_SKIP_MATERIALIZE=1 switch; unrelated generated output changes are excluded. The OpenCode source bundle is rebuilt successfully after the final declaration/internal-export-only cleanup. Current upstream ratchet rules report zero diagnostics for watcher/tool sources. The latest head's complete typecheck job passes TypeScript, lint, lint ratchet and the knip dead-code check. The separate Lint ratchet job also passes. Other current-head CI jobs remain in progress.
  • Full repository tests: bun test --timeout 20000 --bail remains failed/incomplete: 21,700 passed, 65 skipped and 1 failed, stopping after 21,766 tests across 2,603 files (677.79 s). The watcher boot-cleanup regression that failed in the earlier intermediate run passes here. The remaining failure is packages/omo-senpi/src/components/memory/worker/run-reconciliation-liveness.test.ts:58; that test is byte-identical to target dev and passes when run alone (1 pass; 0 fail). The full-run failure cause and a matching base-suite failure are not established. Artifacts: .omo/evidence/20261105-9693-review/{root-frozen-result.json,root-failure-isolated.txt}.

Decisive real-harness capture:

{"sourcePlugin":true,"officialOpenCode":"1.18.34","controlledSeedNotRealGitHubTransition":true,"firstReceiptCount":1,"secondReceiptCount":1,"outboxPruned":true,"registrationPruned":true,"realSessionCountBefore":"absent","realSessionCountAfter":"absent"}

Local ignored evidence hashes:

9e9e2e51f36aece818683d05e101174be4953cb77ca5aa2b35ff724f1d975876  .omo/evidence/20261105-9693-review/focused-knip-fix.txt
757d5fe9882878ba981f952986e93c00bb3d90ca76652b8b3e35617e0b2ee610  .omo/evidence/20261105-9693-review/typecheck-knip-fix.txt
2542fcc4d239d1201fb6afdcda60dc779774bcccc198e2206905adf5d026092e  .omo/evidence/20261105-9693-review/ratchet-knip-fix.json
035266365bccb6763f0204125d174fd939a5ed55dc221e98a4531b3411f7e37a  .omo/evidence/20261105-9693-review/final-opencode-bundle.txt
841fd6bb9fb28dc4ba25b0d32d616b020195872fd0515900dc753e29ea0b690e  .omo/evidence/20261105-9693-review/live07/live-result.json
58ea8526edeee1583e6671d2540536d53e094f9ee93c201117311a21ae8e6ee7  .omo/evidence/20261105-9693-review/live07/isolation.json
dc244793cafa2e5c79bb7e024a3cb6dcb59fd682a1c0a5bee1ad41b91d438331  .omo/evidence/20261105-9693-review/root-frozen-result.json
7ff44a231dd023b2404f18dbf9f1d9bbd4d7d5769c5ee67dfa93206dad23fbc7  .omo/evidence/20261105-9693-review/root-failure-isolated.txt

Risks & Residuals

Related Issues

Related to #9493. Depends on #9684. Does not close the entire issue.


Summary by cubic

Makes OpenCode PR watches durable across host restarts: a session registers once, polls each distinct PR once per minute, and is woken on check, conflict, or comment changes instead of relying on per-invocation fingerprint reads.

Behavior

  • Shares one host-level transport across project plugin instances: caches credentials for five minutes, invalidates on 401, respects primary/secondary rate limits, and uses ETag-driven conditional REST reads.
  • Atomically persists registrations, snapshots, and a pending outbox; a stable first-party receipt acknowledges delivery, and generation guards plus file locking coordinate polling and durable dispatch across processes.
  • Prunes private session state while preserving terminal wakes, pins each watch to its project host, and rejects incomplete reads (paging, head moves) rather than acknowledging a partial baseline.
  • Stops on terminal PRs, 15 minutes of unreadable host data, or ten consecutive comment-only wakes; idle cleanup removes stale watches.
  • Adds watch_pull_request, list_pull_request_watches, and unwatch_pull_request to the enabled monitor tools.
  • Ships a batched fingerprint helper (up to 25 PRs per GraphQL pass) that advises when detail reads are needed, plus a release fragment.

Depends on #9684 for the batched fingerprint primitive; does not close the full #9493 scope.

Written for commit 4d9ea8d. Summary will update on new commits.

View guided diff

@github-actions github-actions Bot added the opencode OpenCode edition: packages/omo-opencode label Oct 6, 2026
@code-yeongyu code-yeongyu added the review-queued Queued for batched Opus review label Oct 10, 2026
@code-yeongyu code-yeongyu removed the review-queued Queued for batched Opus review label Oct 26, 2026
@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks @Dante-dan for the thorough PR watch design, including rate-limit handling and GHES endpoints. Sorry this sat for a while. Because this changes shipped code, the changelog gate needs one fragment at changes/unreleased/9693.md (format in docs/reference/changelog-fragments.md); could you add it? Then it will be queued for review.

@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks for adding the changelog fragment, @Dante-dan. CI now fails only the lint ratchet (Lint ratchet and the same step in typecheck), which blocks new non-null assertions in non-test code. The one it found is packages/omo-opencode/src/features/pr-watch/transport.ts:135:

if (this.#cache.size >= 256 && !this.#cache.has(url)) this.#cache.delete(this.#cache.keys().next().value!)

Something like this avoids it:

const oldest = this.#cache.keys().next()
if (this.#cache.size >= 256 && !this.#cache.has(url) && !oldest.done) this.#cache.delete(oldest.value)

I've queued the full review in the meantime, so findings will come here either way.

@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks, @Dante-dan. The full review at 482324dc2c is in. First, a correction to my last comment: the lint ratchet counts 17 new non-null assertions, not one. Besides transport.ts:135 there's github.ts:67 (details!), 14 in reactor.test.ts and 1 in transport.test.ts, so fixing only the one I named would leave typecheck and Lint ratchet red. Sorry about that.

Before merge:

  • Persisted state grows without bound. acknowledgePrWatchWake (state.ts:133) marks a wake delivered but never removes it, the told list grows on every event (state.ts:101), and snapshots and stopped registrations are never cleaned up. Since the whole file is rewritten on each write (registry.ts:43), 500 wake-and-acknowledge cycles left a 183 KB file. Please prune delivered wakes and told keys, and drop state for stopped watches.
  • File permissions. registry.ts:42 (mkdirSync) and writeFileAtomically set no mode, so under a normal umask the directory is 755 and the file 644. The file holds session ids, project paths, private-repo PR and comment URLs, and logins, so please create them 0700 / 0600.
  • Wakes are dropped when a PR is merged or closed. reactor.ts:49 calls stopPrWatch, which deletes every pending undelivered wake (state.ts:84), e.g. a check-failure wake waiting on a busy session. Events from that final pass are never recorded and no merged/closed notice goes out; only list_pull_request_watches shows pr_merged. The final pass's events plus a merged/closed wake should be delivered before the watch stops.
  • No test for delivery and restart dedupe. manager.ts:107-135 (message-receipt lookup, acknowledgment, retry after an accepted dispatch, the stopped check) has no unit test; reactor.test.ts acknowledges wakes by hand. "Never double-wake or drop a wake across a restart" needs a test that fails without the dedupe.
  • No cross-process coordination. The state file is shared host-wide (keyed only by GH_HOST, manager.ts:15-16), but writes are serialized only in memory (registry.ts:25). Two OpenCode processes on one machine each poll (doubling GitHub reads), can lose each other's updates, and can both deliver the same pending wake. The PR body mentions it, but the default layout hits it, so it needs a file lock (or a single owner per host) before this can merge.

Smaller:

  • A failed message-receipt read counts as "no receipt" (manager.ts:116 falls back to receipt.data ?? []), so after a restart it can dispatch again.
  • A corrupt or future-version state file throws inside the MonitorManager constructor (pr-watch/manager.ts:39 via monitor/manager.ts:45) and takes the monitor feature down; a throw in rescheduling (manager.ts:95) silently stops polling until restart.
  • Watch references carry no host, so a GHES project without GH_HOST polls a same-named repo on github.com (manager.ts:15, transport.ts:40).
  • Check names (author-controlled) go into the wake prompt unescaped (transitions.ts:16, manager.ts:130); newlines can forge lines inside the [OMO PR WATCH] block.
  • Conflict wakes can repeat on CONFLICTING → UNKNOWN → CONFLICTING (transitions.ts:22-23), and a second session watching the same PR gets no initial green/conflict wake because the snapshot already exists.
  • Wake message ids are hash-shaped (manager.ts:112), while OpenCode's MessageV2.latest() orders user messages by id; it worked on 1.18.18 but isn't guaranteed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

opencode OpenCode edition: packages/omo-opencode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants