Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(loops): list every withheld-noise sender in JSON, cap only the human render (#7285)
garrytan44023ec91492
diff --git a/docs/architecture/key-files/open-loops-engine.md b/docs/architecture/key-files/open-loops-engine.mdindex 205d800448..abe04b76c7 100644--- a/docs/architecture/key-files/open-loops-engine.md+++ b/docs/architecture/key-files/open-loops-engine.md@@ -13,5 +13,5 @@ close semantics). The Gmail sweep that feeds both lanes is in- `src/core/google/loops-reciprocity.ts` — the opt-in Updates reciprocity rule (#5012): key `loops.updates_require_reciprocity` (default false; `validateLoopsReciprocityConfigValue` at `config set`). `ownerWroteToFor(engine, source, myAddresses)` returns null when off; on, the addresses in `participants` of the source's email pages whose `senders` include an owner address, cached per engine and source for 10 minutes. Tests: `test/loops-noise-5012.test.ts`, `test/loops-noise-5012-store.test.ts` (+ Postgres twin).- `src/core/google/loops-extract.ts` — the LLM half of the open-loop engine: ONE extractor per recent thread page projects each commitment into THREE substrates in the same pass — the `open_loops` row (dedup `commit:<sha8>`), a facts row via writeSingleFact (kind=commitment, fence-first, deduped; its id lands on `open_loops.fact_id` so entity cards / recall / context_pack see the commitment through existing read paths with zero new read code; a failed facts write is logged with slug, source and a bounded error, never the commitment text, and the loop row still lands), and a typed edge thread-page → person-page (`owes_to` / `awaiting_reply_from`) for relational search. `extract_facts` never runs separately on google-source email pages. Guardrails: injection-hardened input, ALL-or-nothing parse barrier (a malformed model response writes NOTHING), a structural eligibility gate (`loopExtractionEligibility` — Gmail labels, `List-Unsubscribe`, `Auto-Submitted`, calendar part, who wrote the message; no vendor lists) plus the per-source waiting-depth enqueue budget (`LOOPS_EXTRACT_ENQUEUE_CEILING`, a spend backstop — see `google-source.ts`), only the last `LOOPS_EXTRACT_WINDOW_DAYS` (30) of mail (the deep backfill is never extracted), kill switch `loops.extraction_enabled` (default ON for google sources). Suppressions gate the lane before any model/facts/edge write: a muted thread id, or a muted SENDER — `fm.from` ∪ `fm.senders`, every address that AUTHORED a message, so a muted counterparty who wrote earlier in the thread still suppresses — returns `reason: 'suppressed'`; recipients/CC never count (muting one person must not hide everyone else's commitments in a group thread, and an outside sender must not be able to dodge extraction by CC'ing a muted address). Pages without a `senders:` field fall back to `fm.from` alone until their next re-render. Eligibility computes the substantive (non-noise, non-calendar) message set FIRST and `owner_participated` requires the owner to have written a SUBSTANTIVE message — a pure-calendar thread the owner RSVP'd to stays `no_substantive_messages`. When the chat provider is unavailable, `runLoopsExtract` throws `LoopsExtractRetryableError` (reason `llm_unavailable`) rather than returning a skipped result: a completed no-work row would hold the revision-keyed idempotency slot forever, while a retried-then-dead row frees it so the same revision re-enqueues on the next sweep. On a managed brain `managedFactWritePreflight` checks the managed fact authority and canonical owner before chat (an unaccepted writer refuses with `writer_coordinator_required`), and the commitment fact publishes through writeSingleFact's coordinator path; disabled, missing-page and suppressed cases retain their existing no-work outcomes. A judge response cut off at the 2048-token cap (`stopReason: length`) is retried once at 8192; still truncated, the job fails retryable (`truncated`) with the ceiling named. Non-length errors and refusals are never escalated.- `src/core/loops/loops-store.ts` — SQL accessors for the `open_loops` + `loop_suppressions` tables over `engine.executeRaw` with IDENTICAL SQL text on both engines (parity by construction, the sources-ops.ts pattern — no per-engine method twins). JSONB discipline: evidence binds through `$N::text::jsonb`, never a bare `::jsonb` cast over JSON.stringify. Loops close by state transition, never delete: reply-driven auto-close flips status to `done` and stamps `closed_by`, keeping the audit trail. `openedAt` sets `opened_at` (default now()); an open row keeps the earlier of its stored and incoming value, a reopened row takes the incoming one. The upsert's DO UPDATE carries a manual-close guard in its WHERE: a closed row (done/dropped/stale) only reopens on GENUINELY newer activity, so a routine sweep re-seeing the same thread never resurrects a hand-closed loop, and the staleness auto-close (`closed_by: 'staleness'`) is likewise guarded against the upsert's reopen. `listOpenLoops` takes an optional `id` that composes with the status/type/counterparty filters and never replaces the source scope; `counterpartySourceId` narrows a `counterparty` slug match to the source the page lives in (`COALESCE(counterparty_source_id, source_id)`), leaving an email match untouched (#5504). `removeSuppression(engine, sourceId, kind, value)` is the exact inverse of the mute write — deletes the one `(source_id, kind, value)` row with the same lower-casing and returns `{removed: boolean}` (false = was not muted, callers treat it as a no-op success). #5012: `closeMutedLoops` closes (or, without apply, lists) the open reply-owed loops a mute covers as `dropped` / `closed_by: muted`, and the upsert never updates a `muted` row for the same counterparty; `requeueMutedLoops` hands an unmuted loop no other suppression covers to `closed_by: mute_recheck` (no thread: `unmuted`, reported `unverifiable`), which the upsert reopens and `closeThreadLoops` closes on the re-check's verdict, and `settleMuteRecheck` marks `unmuted` when neither happens; `muteRecheckThreads` feeds `loadSuppressions().recheckThreads` and the sweep seed; `listSenderInboundLoops` backs `loops drop sender`.-- `src/core/ops/loops.ts` — the op surface: `open_loops` (read), `loops_close` (write), `loops_mute` (write; also closes the covered reply-owed loops and returns `closed`, dry run `would_close`, plus `undo_command`), `loops_unmute` (returns `requeued`, `unverifiable`, `stale_source` + `recheck_command`, `undo_command`; dry run `would_requeue`). The grouped `open_loops` view adds `withheld_noise` (null when none): open reply-owed loops from senders in each source's `loop_withheld_senders` record, top 10 with a `preview_command`. `loops_close` retires a commitment loop's fact through `persistence/loop-fact-retirement.ts` and returns `{ closed, id, status, fact_expired, retryable, reason? }` from the real publication outcome; a loop already closed whose fact is still active re-attempts the retirement. `open_loops` is deliberately NOT localOnly (hosted serves it over HTTP to the authenticated owner); instead it applies fail-closed evidence redaction for `ctx.remote !== false` — counts, counterparty, summary, due date only; verbatim quotes, Gmail deep links, and the injectable `text` digest are trusted-local only. Ranking and rendered ages use one reference time, `as_of` (ISO, default now, echoed in the grouped result; `gbrain waiting --as-of`), so a published order is reproducible (N7-5). An optional `id` (positive safe integer, else `invalid_params` through `invalidParam` without echoing the value) finds one loop at any status unless `status` is given; explicit filters still intersect, scope and redaction are unchanged, so an out-of-scope id returns exactly what a missing id returns, and an id lookup carries no `text` digest. The result carries per-source last-successful-sync ages + a `stale` flag (>24h): all stale sources remain a refusal condition for `gbrain waiting`, while a mixed fresh/stale set continues with a warning naming stale sources (`stale_sources` in JSON). Per-call scope params `source_id`/`all_sources` resolve through `resolveRequestedScope` (an MCP client bound to another source can reach the google source's loops; remote callers stay in-grant, out-of-grant `source_id` is denied); a scope with NO google source returns `no_google_sources: true` and the digest says the engine has nothing to read instead of a false "You are clean". Scope is fail-closed too: an UNSCOPED remote `open_loops` read is refused outright (the op must not rely on transports to enforce it — an unscoped remote read would span every source), and the write ops (`loops_close`, `loops_mute`, `loops_unmute`) require a single-source remote scope that matches the caller's grants — trusting a caller-supplied `source_id` for a remote write would let any remote client plant (or lift) suppression rows cross-source; `loops_unmute` reverses a suppression via `removeSuppression` and reports `{removed}` honestly. #5871: grouped output ranks people only; loops with neither a counterparty slug nor an email return in `no_counterparty` (`loop_count`, `by_type`, shown loops, `loops_omitted`; null when none), never ranked or counted by `limit`, and the digest lists them last and never says "You are clean" while they exist; each group shows at most `LOOPS_PER_GROUP` (5) loops, due-soonest then most recently active, with `loops_omitted` (`test/ops-loops.test.ts`, `test/loops-cli.test.ts`).+- `src/core/ops/loops.ts` — the op surface: `open_loops` (read), `loops_close` (write), `loops_mute` (write; also closes the covered reply-owed loops and returns `closed`, dry run `would_close`, plus `undo_command`), `loops_unmute` (returns `requeued`, `unverifiable`, `stale_source` + `recheck_command`, `undo_command`; dry run `would_requeue`). The grouped `open_loops` view adds `withheld_noise` (null when none): open reply-owed loops from senders in each source's `loop_withheld_senders` record, every sender ordered by open loop count, each with a `preview_command` (#7285; `gbrain waiting` prints the first 10 and counts the rest). `loops_close` retires a commitment loop's fact through `persistence/loop-fact-retirement.ts` and returns `{ closed, id, status, fact_expired, retryable, reason? }` from the real publication outcome; a loop already closed whose fact is still active re-attempts the retirement. `open_loops` is deliberately NOT localOnly (hosted serves it over HTTP to the authenticated owner); instead it applies fail-closed evidence redaction for `ctx.remote !== false` — counts, counterparty, summary, due date only; verbatim quotes, Gmail deep links, and the injectable `text` digest are trusted-local only. Ranking and rendered ages use one reference time, `as_of` (ISO, default now, echoed in the grouped result; `gbrain waiting --as-of`), so a published order is reproducible (N7-5). An optional `id` (positive safe integer, else `invalid_params` through `invalidParam` without echoing the value) finds one loop at any status unless `status` is given; explicit filters still intersect, scope and redaction are unchanged, so an out-of-scope id returns exactly what a missing id returns, and an id lookup carries no `text` digest. The result carries per-source last-successful-sync ages + a `stale` flag (>24h): all stale sources remain a refusal condition for `gbrain waiting`, while a mixed fresh/stale set continues with a warning naming stale sources (`stale_sources` in JSON). Per-call scope params `source_id`/`all_sources` resolve through `resolveRequestedScope` (an MCP client bound to another source can reach the google source's loops; remote callers stay in-grant, out-of-grant `source_id` is denied); a scope with NO google source returns `no_google_sources: true` and the digest says the engine has nothing to read instead of a false "You are clean". Scope is fail-closed too: an UNSCOPED remote `open_loops` read is refused outright (the op must not rely on transports to enforce it — an unscoped remote read would span every source), and the write ops (`loops_close`, `loops_mute`, `loops_unmute`) require a single-source remote scope that matches the caller's grants — trusting a caller-supplied `source_id` for a remote write would let any remote client plant (or lift) suppression rows cross-source; `loops_unmute` reverses a suppression via `removeSuppression` and reports `{removed}` honestly. #5871: grouped output ranks people only; loops with neither a counterparty slug nor an email return in `no_counterparty` (`loop_count`, `by_type`, shown loops, `loops_omitted`; null when none), never ranked or counted by `limit`, and the digest lists them last and never says "You are clean" while they exist; each group shows at most `LOOPS_PER_GROUP` (5) loops, due-soonest then most recently active, with `loops_omitted` (`test/ops-loops.test.ts`, `test/loops-cli.test.ts`).- `src/commands/loops.ts` — `gbrain waiting [--top N] [--json] [--stale-ok]` + `gbrain loops list|show|done|drop|mute`: all paths dispatch through the trusted-local op layer (`handleToolCall`, remote:false) so CLI and MCP share one behavior. `waiting` REFUSES when EVERY google source has gone >24h without a successful sync and prints the exact fix (`--stale-ok` bypasses; per-source sync ages are always reported); when only some sources are stale, it continues with the warning and source ids from `open_loops`. Output per counterparty: what's owed, evidence quotes, Gmail deep links, entity-card context, a paste-ready digest. Reads default to the `__all__` brain span (loops live in google sources, not `default` — a default-scoped read would say "all clean" while people wait); `--source <id>` narrows explicitly. `loops show <id>` passes the id to `open_loops` (any status and rank); a missing positive id is a usage error (exit 2) and an unknown id renders `not_found` with the `gbrain loops list` fix (exit 1). An unqualified `mute`/`unmute` resolves the brain's google source (never `default`) through the SAME resolution — an unmute can never aim at a different source than the mute it reverses — and refuses with the exact fix when none or multiple exist. `unmute sender <email> | thread <id>` is exact and queues the loops the mute closed for re-check; both take `--dry-run` and print the count and the undo command. A repeated unmute is a no-op success (`removed: false`, exit 0) so scripts can call it unconditionally. `loops drop sender <email> [--source] [--note] [--dry-run]` closes one sender's open reply-owed loops through `loops_close` (the dry run is the consent step; a drop cannot be undone), and `waiting` prints the `withheld_noise` lines.diff --git a/docs/guides/open-loops.md b/docs/guides/open-loops.mdindex c4934e3450..c26bdcd262 100644--- a/docs/guides/open-loops.md+++ b/docs/guides/open-loops.md@@ -313,8 +313,9 @@ rule withheld (`loop_withheld_senders`: reason, count, last time; at mostthread of up to 500 senders with open reply-owed loops, so the record coversolder loops. `gbrain waiting` then prints how many open reply-owed loops comefrom those senders and the exact `gbrain loops drop sender <email> --source-<id> --dry-run` to preview for each (JSON and the `open_loops` grouped view:-`withheld_noise`). Nothing is closed automatically. The sync log prints+<id> --dry-run` to preview for each: the 10 senders with the most loops,+then a count of the rest. `gbrain waiting --json` and the `open_loops` grouped+view list every sender in `withheld_noise`. Nothing is closed automatically. The sync log prints`[google] reply-owed loops withheld: automated_mail=N`.**Opt-in: Updates mail needs reciprocity.** Withdiff --git a/src/commands/loops.ts b/src/commands/loops.tsindex 35bac0217e..fb6916d464 100644--- a/src/commands/loops.ts+++ b/src/commands/loops.ts@@ -165,8 +165,8 @@ export async function runWaiting(engine: BrainEngine, args: string[]): Promise<vif (noise) {process.stdout.write(`\n${noise.loop_count} open reply-owed loop(s) come from ${noise.sender_count} sender(s) whose mail is automated; `+ `the detector would not open them now. Preview closing them, sender by sender:\n`- + noise.senders.map((x) => ` ${x.preview_command} (${x.open_loops})`).join('\n') + '\n'- + (noise.sender_count > noise.senders.length ? ` … and ${noise.sender_count - noise.senders.length} more sender(s)\n` : ''));+ + noise.senders.slice(0, 10).map((x) => ` ${x.preview_command} (${x.open_loops})`).join('\n') + '\n'+ + (noise.sender_count > 10 ? ` … and ${noise.sender_count - 10} more sender(s); every sender: gbrain waiting --json\n` : ''));}if (result.groups.length > 0 || result.no_counterparty) {process.stdout.write(diff --git a/src/core/ops/loops.ts b/src/core/ops/loops.tsindex 44e776d04d..2a988739eb 100644--- a/src/core/ops/loops.ts+++ b/src/core/ops/loops.ts@@ -173,7 +173,7 @@ async function withheldNoise(ctx: OperationContext, sources: GoogleSourceFreshne}if (!senders.length) return null;senders.sort((a, b) => b.open_loops - a.open_loops || a.email.localeCompare(b.email));- return { loop_count: senders.reduce((n, x) => n + x.open_loops, 0), sender_count: senders.length, senders: senders.slice(0, 10) };+ return { loop_count: senders.reduce((n, x) => n + x.open_loops, 0), sender_count: senders.length, senders };} catch {return null;}diff --git a/test/loops-noise-5012.test.ts b/test/loops-noise-5012.test.tsindex 36e8a7925c..502233a13c 100644--- a/test/loops-noise-5012.test.ts+++ b/test/loops-noise-5012.test.ts@@ -315,6 +315,27 @@ describe('gbrain waiting reports the loops the rule would not have opened', () =senders: [{ email: '[email protected]', source_id: 'g1', open_loops: 2 }] });});+ test('#7285: JSON lists every recorded sender by open loop count; the human render caps at 10', async () => {+ const record: Record<string, { reason: string; count: number; last_ms: number }> = {};+ const senders = [...Array.from({ length: 11 }, (_, i) => `alerts-${String(i).padStart(2, '0')}@bank-example.test`), '[email protected]'];+ let n = 0;+ for (const email of senders) {+ record[email] = { reason: 'automated_mail', count: 1, last_ms: 1 };+ for (let k = 0; k < (email.startsWith('zz-') ? 3 : 1); k++) {+ await upsertOpenLoop(engine, loop({ threadId: `18c2f4a9b3d2${(0x2000 + n++).toString(16)}`, counterpartyEmail: email }));+ }+ }+ writeFileSync(join(stateDir, '.google-source.json'), JSON.stringify({ loop_withheld_senders: record }));+ const j = await captured(() => runWaiting(engine, ['--json']));+ const noise = JSON.parse(j.out).withheld_noise;+ expect(noise).toMatchObject({ loop_count: 14, sender_count: 12 });+ expect(noise.senders.map((s: { email: string }) => s.email)).toEqual(['[email protected]', ...senders.slice(0, 11)]);+ expect(noise.senders[0]).toMatchObject({ open_loops: 3, preview_command: 'gbrain loops drop sender [email protected] --source g1 --dry-run' });+ const r = await captured(() => runWaiting(engine, []));+ expect(r.out.split('\n').filter((line) => line.includes('gbrain loops drop sender')).length).toBe(10);+ expect(r.out).toContain(' … and 2 more sender(s); every sender: gbrain waiting --json');+ });+test('the one-shot sender backfill queues the newest thread of each sender with open reply-owed loops', async () => {await upsertOpenLoop(engine, loop({ lastActivityAt: '2026-08-01T00:00:00Z' }));await upsertOpenLoop(engine, loop({ threadId: '18c2f4a9b3d21e14', lastActivityAt: '2026-08-02T00:00:00Z' }));