Skip to content

feat(pm-stack): quota gatherer writes the babysitter pool-state feed (#1338) - #12689

Merged
dezren39 merged 1 commit into
mainfrom
felix/1334-pool-state-feed
Nov 22, 2026
Merged

dezren39 merged 1 commit into
mainfrom
felix/1334-pool-state-feed

Conversation

@dezren39

@dezren39 dezren39 commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Lands #1338's remaining code slice — the pool-state.json quota feed — under epic #1334 (whose reconciliation is posted at #1334 (comment)).

The babysitter's verdict rounds (PR #12687) read pool wall state from pool-state.json in the claimant state dir, an input file nothing wrote: on a live install every round would record pool_state: "unread" and no pool could ever read walled, so a quota wall would still be misreadable as a stall. babysitter.py's own header named this wiring as the follow-up slice.

  • pm-stack/aggregator/src/pool-state.ts — pure projection of the gatherer's quota-current.json into the babysitter's shape, keyed by the canonical OpenCode provider id (the id a session names as model.providerID; the aggregator's service ids are a separate contract and never appear). Wall rule is derive.ts poolState's (any window ≥ 100%); a wall is asserted only from a fresh ok entry (quota-current.ts freshness bounds) — stale/errored entries surface as state with no wall and no percent, per quota-exhaustion.ts's stale_source honesty rule. Several credentials sharing a providerId: any fresh ok one at its limit walls the pool.
  • scripts/gather-quota.ts — writes the projection to the claimant state dir (POOL_STATE_PATH override, default ~/.local/state/opencode-claimant/pool-state.json) after quota-current.json, fail-soft like the Turso sink: a failed write warns and never fails the tick; a missing feed reads as "unread" on the claimant side, never as a false all-clear.
  • babysitter.py header now names the landed writer instead of the follow-up slice.

No schedule is created or changed (the writer rides the gatherer's existing 15-minute tick), so the schedule registry is untouched. No new dependencies.

Evidence (sandbox venue)

  • Fail-before: the new suite against pristine origin/main via git archive — load error, pool-state.ts absent.
  • Pass-after: pool-state.test.ts 11/11; neighbouring quota suites (quota-gather-map, quota-analytics) — 39/39 across the three files; claimant hermetic suite 203/203.
  • Cross-language contract check: a projection generated by the TS module was read back by babysitter.read_pool_state — status read; a fresh 100% pool lands walled: true, a 3-hour-stale 100% pool lands walled: false, state: "stale".
  • tsc --noEmit: no errors in the touched files (3 pre-existing errors about grist/seed/roadmap.json, absent from this sparse worktree, unrelated). Changed files formatted with the repo's pinned biome 2.3.2 / ruff per treefmt.

Remaining on #1338 after this

Deployment + live verification only: the laptop claimant and gatherer pick the code up on install; three live babysitter rounds with a live pool-state feed tick criteria 1–2's live arm. This PR does not close #1338.

Refs #1338, #1334.


Run Proof on this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…1338)

The claimant babysitter's verdict rounds read pool wall state from
pool-state.json in the claimant state dir, an input file nothing
wrote: on a live install every round recorded pool_state unread and
no pool could ever read walled. This lands the writer half of the
contract babysitter.py's header named as the follow-up slice.

- src/pool-state.ts: pure projection of quota-current.json into the
  babysitter shape, keyed by the canonical OpenCode provider id (the
  id a session names as model.providerID). A wall is asserted only
  from a fresh ok entry (quota-current freshness bounds) under
  derive.ts poolState's rule (any window >= 100%); stale/errored
  entries surface as state with no wall and no percent, per
  quota-exhaustion's stale_source honesty rule.
- gather-quota.ts: writes pool-state.json into the claimant state
  dir (POOL_STATE_PATH override) after quota-current.json,
  fail-soft like the Turso sink - a failed write warns and never
  fails the tick.
- babysitter.py header now names the landed writer.

Evidence (sandbox): fail-before on pristine origin/main (suite load
error, no pool-state.ts); pass-after pool-state 11/11, neighbouring
quota suites 39/39, claimant hermetic suite 203/203; cross-language
contract check - babysitter.read_pool_state reads the projection
back (fresh wall lands, stale wall does not). tsc clean for the
touched files (3 pre-existing grist/roadmap.json errors from the
sparse worktree, unrelated). No new dependencies.
@dezren39
dezren39 requested a review from a team as a code owner November 22, 2026 22:09
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T22:11:47.726453Z 690bbc7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@dezren39
dezren39 merged commit 2ce5c77 into main Nov 22, 2026
1 of 2 checks passed
@dezren39
dezren39 deleted the felix/1334-pool-state-feed branch November 22, 2026 22:10

@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: 690bbc7e99

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +63 to +65
if (entry.state !== "ok") return false;
const t = Date.parse(entry.measuredAt);
return Number.isFinite(t) && t - nowMs <= QUOTA_CURRENT_FUTURE_SKEW_MS && nowMs - t <= QUOTA_CURRENT_FRESH_MS;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude retained readings from fresh pool-wall decisions

When a provider fetch fails on the next 15-minute tick, markRetained() preserves the previous entry's state: "ok" and measuredAt while setting retainedFromFailedTick. That reading is still inside the 20-minute freshness bound, so this predicate accepts it and can publish its old 100% window as a current wall. By contrast, quota-current.ts explicitly classifies retained entries as stale immediately. If the quota has reset, the new feed incorrectly labels genuinely stalled sessions as walled, suppressing stall nudges. Reject entries with retainedFromFailedTick before applying the timestamp check.

Useful? React with 👍 / 👎.

Comment on lines +1019 to +1020
await writeAtomic(poolStatePath, JSON.stringify(projectPoolState(file), null, 2) + "\n").catch((err) =>
console.error(`quota-gatherer: pool-state write failed (${err}); babysitter pool state stays unread this tick`),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Expire published walls when the quota feed stops updating

After a successful tick publishes walled: true, a gatherer outage, early failTick() return, or subsequent write failure leaves that file in place indefinitely. babysitter.read_pool_state() ignores generatedAt and trusts the stored wall on every round, so the write failure does not make the state unread as this log claims. Once quota resets, genuinely stalled sessions can remain classified as walled indefinitely and never receive stall nudges. Add read-time expiry for the published snapshot, returning unknown/unread once its freshness bound is exceeded; checking freshness only during projection cannot protect against a stopped writer.

Useful? React with 👍 / 👎.

Comment on lines +73 to +75
const list = byProvider.get(entry.providerId) ?? [];
list.push(entry);
byProvider.set(entry.providerId, list);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the configured OpenAI instance IDs in pool-state keys

For folded OpenAI quota results, entryFromResult() in quota-gather-map.ts preserves providerId: "_openai" but recovers the credential's actual instance ID into serviceId (_openai.main or _openai.alt). The configured openai-plan uses those two instance IDs (.opencode/opencode.jsonc:2635), and the babysitter looks up each session's exact model.providerID. Grouping solely by entry.providerId therefore publishes the OpenAI wall under the retired bare _openai key, so neither configured instance can read as walled and quota-blocked sessions can incorrectly receive stall nudges. Use the recovered instance ID for these folded OpenAI entries rather than collapsing them under _openai.

Useful? React with 👍 / 👎.

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.

Babysitter live: interval rounds over all OpenCode sessions with verdicts, quota, and pool attribution

1 participant