Skip to content

Report ledger freshness in agent-observer health - #150

Merged
lukemaj merged 26 commits into
mainfrom
fix/149-health-ledger-freshness
Nov 25, 2026
Merged

lukemaj merged 26 commits into
mainfrom
fix/149-health-ledger-freshness

Conversation

@lukemaj

@lukemaj lukemaj commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What agent-observer health reports each harness's newest native record, newest ledger import, and count of newer native sources.
Why A stopped sync could leave the ledger behind while the live-session checks still passed.
So what Health exits 4 when the ledger falls behind. CI and independent review passed. The PR is ready for merge approval.

Closes #149.

Behavior

  • The default interval is 3600 seconds, configurable with health --sync-interval-seconds N. Text and JSON show the same measurements. Freshness uses timestamps and counts without emitting transcript content or native source paths.
  • The comparison uses each harness's latest import from the read-only ledger snapshot. A later import for one harness cannot cover another harness's gap. Individual older source gaps can fall before a harness's latest import.
  • Unreadable ledgers and observed scan errors surface as unavailable. The existing visibility, Cue, and bare-root results remain available.
  • Version 1.5.3 → 1.6.0. Website impact is none because the change affects the local CLI and its README.

Initial proof

  • Base 75ed333b0c9928fc306bcfc188f84829594a0d83, head 92423fad76b81b4c5881610f77ad602a734a68db. versionctl release-check --json passed.
  • python3 -m unittest discover -s tests -p test_health.py passed 24 tests. The baseline returned 0 on the stale and never-synced fixtures. Coverage includes stale, current, exactly one interval, a never-synced ledger, an unreadable ledger, and OpenCode part updates in WAL. The 21 bare-root tests also passed.
  • Ran the CLI on a local SQLite backup made through a read-only connection to the bestia ledger. At the default interval the copy was within tolerance and exited 0. At 1800 seconds both text and JSON exited 4 with 0 invisible sessions. The first text run reported:
Ledger freshness (sync interval: 1800 seconds)
- t3: stale, newest native 2026-11-25T18:44:39+01:00, newest import 2026-11-25T17:51:27+01:00, 1 newer sources, lag 3192 seconds
- codex: stale, newest native 2026-11-25T18:44:39+01:00, newest import 2026-11-25T17:50:37+01:00, 12 newer sources, lag 3243 seconds
- claude: stale, newest native 2026-11-25T18:44:38+01:00, newest import 2026-11-25T17:50:37+01:00, 10 newer sources, lag 3241 seconds

First review fixes

The new head 41e952807dfb430dd5ff264079b9f84b2a8e1d08 includes Router-owned Codex rollouts, reports Grok listing errors, and keeps freshness output available when the identity conflict query fails. The three new regressions failed on the initial head and passed on the revised head.

The revised head passed 85 targeted tests, compile and version checks, and versionctl release-check --json. A fresh read-only bestia ledger backup produced exit 4 in both text and JSON at a 120-second interval. Exact output and current proof.

Second review fixes

Head 2249476d9302073fd13990b287a5b32ae1d7b52e surfaces denied listings and file lookups through a shared health discovery mode for native adapter paths and bare receipts. Sync uses the existing lenient discovery behavior. Permission fixtures cover Claude, Codex, Router-owned rollouts, Muse, Grok and Codex bare receipts. They failed on the previous head and passed here.

151 targeted tests passed, plus compile, CLI version and versionctl release-check --json. The CLI exited 4 in text and JSON on a read-only SQLite backup of bestia's ledger. Exact stale ledger output and current proof.

Third review fixes

Head 4aec14d95dd25df8b67128f00e9ec8c0948c3bb3 propagates denied Chromeria V2 state access into freshness and receipt discovery, tracks Grok summary and chat metadata imports, and counts hard-linked Codex rollouts once. The added fixtures reproduced all three issues on the last head. The Grok test also proves a successful fixture sync clears the metadata-only gap.

149 targeted tests passed, plus compile, CLI version and versionctl release-check --json. The CLI exited 4 in text and JSON on a read-only SQLite backup of bestia's ledger. Exact output and current proof.

Fourth review fixes

Head 6bfb5ffa6297054bc0ffc896c036695ed446797c keeps Grok freshness stale after a failed metadata read, clears it after a successful revalidation, and looks for each SQLite WAL beside the resolved native database. The added regressions exercise refused reads, identical rewrites, and a committed WAL behind a symlink. 97 targeted tests and versionctl release-check --json passed. Both output formats exited 4 on a read-only copy of bestia's ledger at a 120-second interval. Exact output.

Fifth review fixes

Head fe00d365645064040ee82a6254c25542a82d8de7 continues importing valid Grok transcripts when optional metadata fails and reports that failure without advancing its metadata timestamp. Router records a successful revalidation of newer SQLite files when the logical fingerprint matches. 145 targeted tests and versionctl release-check --json passed. Text and JSON both exited 4 on a read-only copy of bestia's ledger at a 120-second interval. Exact output.

Sixth review fixes

Head 4493cf40907c2aa5a5ff915ea0b28d7753bbe566 preserves Grok freshness when an unchanged transcript is paired with malformed metadata. Malformed or incomplete chat history is reported as a sync failure. Valid transcript changes are still imported. 159 targeted tests and versionctl release-check --json passed. Both output formats exited 4 on a read-only copy of bestia's ledger at a 120-second interval. Exact output.

Seventh review fixes

Head 03e098b3996b89c6808e0e37bd21be4cc0c36b2a preserves the last successful import timestamp for unchanged Grok transcripts when metadata fails, including empty files and forced replays. Both regression cases exited 0 on the previous head and now exit 4. The revised head passed 156 targeted tests and versionctl release-check --json. Text and JSON exited 4 on a read-only bestia ledger copy. Exact output.

Eighth review fixes

Head fe4ced666725362b2d1e6e479c2579a20be04fdd commits valid Grok transcript rows during a partial sync and advances freshness only after a complete session import. The regressions cover an incomplete append, a timestamp-only touch, and a first partial import. All 156 targeted tests and versionctl release-check --json passed. Text and JSON exited 4 on a read-only copy of bestia's ledger. Exact output.

Ninth review fixes

Head e4e8060127e4a4186927dbada2007482adc75833 preserves regular transcript import timestamps for existing reports and tracks complete Grok session freshness separately. An ordinary sync retries affected sessions and clears the gap after successful reconciliation. The targeted suite and release check passed. Text and JSON exited 4 on a read-only copy of bestia's ledger. Exact output.

Tenth review fixes

Head b78555530cbb30cac4b80c3c6cdb40f4be4777aa preserves the recorded Grok identity when summary reads fail, preserves transcript import timestamps over repeated unchanged failures, and counts symlink aliases once. A regression fixture exercises all three and verifies recovery restores the two responses' model and effort. 147 targeted tests and versionctl release-check --json passed. Exact output on a read-only ledger copy.

Eleventh review fixes

Head d25a9873b915f82ff2c9e207e7ac492ebdc5dfb9 reuses recognized Grok source identities across retries and reads the validated native notification ID when the summary is unavailable on the first transcript import. The fixture covers recovery from a prior metadata-only import, one response with restored model, and health returning to 0. 148 targeted tests and versionctl release-check --json passed. On a read-only ledger copy, text and JSON exited 4 at the default one-hour threshold. Exact output.

Twelfth review fixes

Head 4584bd9e6a844e7a34dee16c5d077953a9eba529 defers Grok transcript import when a summary read fails and the source and transcript provide no known native identity. The regression reproduces the duplicate session on the prior head and confirms both records import under one identity after recovery. 149 targeted tests and the release check passed. Text and JSON exited 4 at the default one-hour interval on a read-only copy of bestia's ledger. Exact output.

Thirteenth review fixes

Head af16e49b6f1c6a6eb61218e007957021cd3f3892 waits for a Grok native ID when the first transcript has no ID and the summary is missing, empty, or unreadable. The fixture exercises all three conditions and confirms recovery under one identity. 149 targeted tests and the release check passed. Text and JSON exited 4 on a read-only ledger copy at a 120-second interval. Exact output.

Fourteenth review fixes

Head b120a9da5baac4a3bee6339f081e0531ecd1bf8f accepts a valid native identity from a Grok event when summary and update identity are unavailable. An initial stream with no validated ID waits for identity evidence. 163 targeted tests and the release check passed. Text and JSON exited 4 on a read-only ledger copy at a 120-second interval. Exact output.

Fifteenth review fixes

Head 6954eb0ae9d4ca28d81563c610ada0366ef266ea restores directory fallback for readable Grok metadata without an ID, gives a validated summary ID precedence, and rejects identity from malformed update envelopes. As on base, a later explicit ID applies to newly imported records and earlier records keep their historical keys. 164 targeted tests and the release check passed. Text and JSON exited 4 on a read-only ledger copy at a 120-second interval. Exact output.

Sixteenth review fixes

Head 0e79f00b1452d2a4ccf4c8e8fcd590e0120d2c28 uses turn-start event IDs and validated completed updates as Grok identity evidence. Its regression covers malformed tool updates, unknown kinds and missing update payloads. 164 targeted tests and the release check passed. Text and JSON exited 4 on a read-only ledger copy at a 120-second interval. Exact output.

Seventeenth review fixes

Head a660c3b13f7ef68c34a3c15c09c36be7521aa45e validates Grok tool notification IDs using the same check as transcript ingestion. A valid tool call supplies native identity during a summary failure, while malformed tool calls cannot supply it. 165 targeted tests and the release check passed. Text and JSON exited 4 on a read-only ledger copy at a 120-second interval. Exact output.

Eighteenth review fixes

Head 8336bc2e67fec498a547280e38b184a2f733d527 imports valid Grok user and assistant messages when their notification is the only source of native identity. The fixture verifies both rows are attached to the native session and the summary error is reported. 166 targeted tests and the release check passed. Exact output on a read-only ledger copy.

Nineteenth review fixes

Head cd6b4935ac403146078f9bcf9d4f21d44a1ad0e2 uses importer-recognized Grok compaction and extension updates as native identity evidence. The fixtures verify both events land and the summary error remains visible. 167 targeted tests and the release check passed. Exact output on a read-only ledger copy.

Twentieth review fixes

Head 843de0efa32d45f59c394ecc8312ac1099922bb2 lets Router sync continue through an optional timestamp probe failure. SQLite still controls import success, while unchanged rows keep their previous import timestamp if the probe failed. All 83 targeted tests and the release check passed. Exact output on a read-only ledger copy.

Twenty-first review fixes

Head d6f41d60a73a022fffd31229a499b2a98d001e2c keeps native paths out of freshness scan errors and retains the unavailable status and exit code. 106 targeted tests and the release check passed. Exact output on a read-only ledger copy.

Twenty-second review fixes

Head 0f6252fe84400bb4cb69b30a24874812a23466fe keeps ledger permission errors path-free and validates native timestamps before either output format renders them. 108 targeted tests and the release check passed. A read-only ledger copy produced exit 4 in text and JSON, with four stale harnesses and zero invisible sessions. Exact output.

Twenty-third review fixes

Head 71bdd488cdece0f9b925d6ad290c92b3a195ea11 validates Grok's transcript and metadata timestamps before comparing them. It detects malformed OpenCode timestamps during freshness discovery and preserves the process visibility report. The two reported regressions failed on the prior head and passed here. All 50 health tests and the release check passed. Exact output on a read-only ledger copy.

Twenty-fourth review fixes

Head 4a185b06d1f41e859b5203e6af1cfef8bed08d45 includes OpenCode message and part creation timestamps when checking freshness. A malformed timestamp cannot obscure a valid session in the separate process visibility check. Both regressions failed on the prior head and passed here. All 52 health tests and release validation passed. Exact output on a read-only ledger copy.

Current head and proof

Head a71c37599888d8da2aa9170d69bed87b2fbaa084 keeps both output formats working when OpenCode's visibility query fails. The two reproduced cases use a creation-only schema and a corrupt native database. All 53 health tests and release validation passed locally. Independent review passed at this head with no blocking findings, and all 1,004 tests and GitHub proof passed. Exact stale-ledger output.

Elon record

Requirements and who asked The user asked for health to show when Observer is behind actual sessions. #149 specifies per-harness times, source counts, configurable failure, JSON, and visibility preservation.
Deleted A daemon, scheduling changes, cross-machine transport, and a new ledger table. Health can compare existing native metadata with the ledger's import timestamps.
Bottleneck Health only checked running-session visibility, so a failed sync could go undetected while native records accumulated.
Checked myself Reproduced the stale and never-synced false successes with fixtures, ran the health and bare-root tests, and confirmed the CLI behavior against a real SQLite backup.

@lukemaj

lukemaj commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Agent work on this PR

Estimated cost at least $2.27 · 72 responses · 64 sessions · 4.7 h wall time

Model Responses Tokens Estimated cost
gpt-6.2-sol (codex, xhigh) 67 17,881,053 $2.25
grok-4.7 (grok, medium) 4 6,272,683 unknown
gpt-6.2-sol (codex, high) 1 131,363 $0.02

Flags: 3 human corrections · 65 large tool outputs · 1 permission request · 12 repeated commands · 4 repeated failures · 11 repeated reads · 8 repeated skill loads · 10 tests edited after a failure · 4 unpriced responses

Details: snapshot, prices, coverage, counters
  • Task toolboxmd/agent-observer#150: outcome unknown (recorded acceptance only; a finished process never implies it).
  • Proof: Report ledger freshness in agent-observer health #150
  • Snapshot 7ed1ade66cdfb250a8f4d8a39d38db0966a8fbc246b86d527d917e3003f0287d, records up to 2026-11-25 19:51 UTC.
  • 64 sessions on claude, codex, grok; AgentsMD 17.5.0, 17.6.3.
  • The total includes work shared with 11 other tasks, counted whole in each, so sums across PRs overlap.
  • Not counted: 3,517 responses ($163.51) in sessions shared with other PRs that worked in no single PR's checkout.
  • 100 responses in these sessions worked on other PRs and are counted there.
  • Totals reconcile with the measured sessions: yes. Evidence complete: yes.
  • Prices: list-price estimate from T3 local rate table (path withheld) as of 2026-11-25, schedule cd3585d6e9b2f16c6ff763f7fa4e829835e876178a944480fa73c91422fed7fa. Unknown prices stay unknown, never zero.
    • T3 LiteLLM rate table when present; bundled schedule covers the rest.
    • Model price sources: T3 local rate table (path withheld).
    • Unpriced: unknown model (4).
  • Harness-reported cost: none reported.
  • Usage totals are not billing. Subscription spending is separate and is never posted as spend.
  • Crashed runs are counted separately: 0.

Token counters by model (native counter semantics; never added across semantics):

  • gpt-6.2-sol (codex, xhigh), codex counters: input includes cached, output includes reasoning: input 17,850,775; cache-read 17,765,376; cache-write 0; output 30,278; reasoning 4,806; harness total 17,881,053.
  • grok-4.7 (grok, medium), grok counters: input includes cached, output includes reasoning: input 6,211,904; cache-read 6,018,560; cache-write 0; output 60,779; reasoning 36,593; harness total 6,272,683.
  • gpt-6.2-sol (codex, high), codex counters: input includes cached, output includes reasoning: input 131,080; cache-read 130,048; cache-write 0; output 283; reasoning 108; harness total 131,363.

Generated tokens (reasoning and other output are disjoint; other output includes code, tool calls and replies):

Model Reasoning Other output Reasoning share Split known for
gpt-6.2-sol (codex, xhigh) 4,806 25,472 15.9% 67 of 67
grok-4.7 (grok, medium) unknown unknown unknown 0 of 4
gpt-6.2-sol (codex, high) 108 175 38.2% 1 of 1

Usage by work phase (this task's own responses):

Phase Model Responses Input Cache read Cache write Output Reasoning Total Priced part
unclassified gpt-6.2-sol (codex, xhigh) 67 17,850,775 17,765,376 0 30,278 4,806 17,881,053 $2.250116
unclassified gpt-6.2-sol (codex, high) 1 131,080 130,048 0 283 108 131,363 $0.017899

Recorded activity by phase (observations, not separate token bills):

Phase Tool calls MCP results Reads Skill invocations File changes Failed tool results
unclassified 494 126 38 unavailable 99 27

Selected rates (USD per million tokens). These rates value the report at the selected schedule date; they do not establish historical prices or subscription spending.

Model Input tier Input Cache read Cache write Other output Reasoning
gpt-6.2-sol all 2 0.1 2.5 10 10
grok-4.7 all unknown unknown unknown unknown unknown

Other output and reasoning are priced without double counting inclusive native output. Missing rates remain unknown.

Local measurement from native records; usage totals are not billing. Updated in place by agent-observer publish.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Independent review verdict on 92423fad76b81b4c5881610f77ad602a734a68db: FAIL: changes requested.

  1. Router-owned Codex rollouts are missing from freshness discovery. Default sync imports rollouts under codex-sessions and kits/*/sessions into the Codex ledger. Health only checks Codex’s normal, archived, and Chromeria bare roots, plus the Router database under router. If one of those rollouts grows while jobs.db stays unchanged, health can exit 0 with missing Codex work. Reuse the rollout discovery in health and add a fixture where only a Router-owned rollout advances.
  2. An unreadable native directory can look empty. Grok discovery discards OSError from listing both the root and its project directories. If the only newer Grok sessions are under a directory the health process cannot list, health can report no_native_records or current and exit 0. Preserve discovery errors for health while keeping absent optional roots valid.
  3. A damaged ledger can abort health before JSON is emitted. A SQLite ledger with the current schema marker but missing import_errors opens successfully, then raises in identity_conflicts() before either output format is emitted. The corrupt-file fixture covers failure at open only. Report the failed query as unavailable, preserve the other results, and test exit 4 with valid JSON.

The text and JSON paths share the same exit decision. The fixtures cover per-harness isolation, the exact interval boundary, absent and empty ledgers, Grok directory counting, and OpenCode part updates in WAL. Older schema markers take the unavailable path, and the version mirrors agree at 1.6.0. git diff --check passed. The test and real-copy runs are reported in the PR. This review was static and did not rerun them or access private session data.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 2 independent review of 41e952807dfb430dd5ff264079b9f84b2a8e1d08: FAIL, changes requested.

  • Unreadable Claude and Codex sources can still look healthy. A stale Claude transcript or Router-owned Codex rollout returned exit 4 while readable. Removing directory read permission changed both to exit 0 with no_native_records because glob discovery suppresses the scan error. An unreadable Codex bare root also returned exit 0 even though the bare-root report explicitly said unreadable. Propagate discovery errors and known unreadable bare roots to freshness, while preserving the existing behavior for missing optional roots.
  • Grok strict discovery still hides permission errors during file checks. In a fixture where the session directory could be listed but lacked search permission, stat of updates.jsonl raised PermissionError. _looks_like_session() swallowed that error through os.path.isfile(), and health returned exit 0 with no_native_records. Make the file checks propagate permission errors in strict mode.

Fourteen focused tests passed, including Router fallback and alternate roots, interval and never-synced behavior, visibility, and valid JSON after the identity query fails. Two further fixtures confirmed that missing Grok bare roots remain optional and unreadable Grok bare roots fail health. GitHub proof passed for this head. Independent runtime verification used synthetic fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 3 independent review of 2249476d9302073fd13990b287a5b32ae1d7b52e: FAIL, changes requested.

  1. A denied Chromeria V2 database can make health exit 0. With a stale V2 database behind a symlink, health exited 4 while the target was accessible. Denying search permission on the target directory made stat raise PermissionError, but state_path() fell back to the absent V1 database and health exited 0 with no_native_records. Make strict state selection propagate access errors into both T3 freshness and bare receipt discovery while preserving fallback for an absent V2 database.
  2. Grok health misses sessions that sync imports from metadata. A new session containing only summary.json left health at exit 0 with current and zero newer sources. The Grok importer created a ledger session from that same fixture. Include metadata-only activity in freshness and account for its successful import so the gap clears after sync.
  3. Codex counts hard-linked rollouts twice. Two rollout names linked to the same file produced newer_sources: 2, while normal sync resolved them to one source. Deduplicate by device and inode, as Codex sync does.

All 140 targeted tests passed, including the prior regression cases and normal sync fixtures. Accessible fixture layouts agreed across strict and ordinary discovery, and GitHub proof passed. Independent runtime checks used synthetic data.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 4 independent review of 4aec14d95dd25df8b67128f00e9ec8c0948c3bb3: FAIL, changes requested.

  1. Grok can report current after failing to read newer metadata. A fixture replaced summary.json and denied read access. Sync reported no failures, retained the old branch, and advanced imported_at, changing health from stale to current. The reverse also occurs: an identical rewrite moves the file’s timestamp forward, but successful sync leaves health stale because the fingerprint matches. Advance the timestamp only after successfully reading or revalidating the metadata, including identical rewrites.
  2. Chromeria health misses WAL activity behind a database symlink. SQLite read a committed row through the V2 symlink, but health exited 0 because it checked for -wal beside the link. Resolve the database path before checking its WAL and add a regression with a real WAL commit.

All 981 deterministic tests passed. Synthetic checks confirmed V1 fallback when V2 is absent, migration from the old Grok metadata schema, and timestamp stability on an unchanged repeat sync. I did not repeat the stale-ledger-copy proof on this head.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 5 independent review of 6bfb5ffa6297054bc0ffc896c036695ed446797c: Changes requested. Both blockers reproduced.

  1. Malformed Grok metadata blocks valid transcript imports. A fixture with a truncated summary.json and a complete turn_completed record imported one response on 4aec14d; this head reported one failure and imported zero responses. Preserve transcript ingestion when optional metadata fails, surface the metadata failure, and leave its freshness timestamp unchanged.
  2. Router can stay stale after a successful sync. A VACUUM advanced the fixture database mtime without changing its rows. Sync revalidated the fingerprint and reported unchanged, but health still exited 4 with 9,000 seconds of lag. Record successful revalidation when the database or WAL is newer than the stored import time, or measure Router activity from imported records.

All 15 freshness tests passed, including the denied Grok metadata, identical rewrite, and symlinked WAL regressions. git diff --check passed. Independent runtime proof used synthetic fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 6 independent review of fe00d365645064040ee82a6254c25542a82d8de7: changes requested.

  1. A failed Grok summary read still clears the freshness gap when the transcript is unchanged. In a fixture where only summary.json changed, sync reported invalid Grok summary JSON, imported zero responses, and left the metadata timestamp at 11000. The unconditional transcript finish() advanced the timestamp health uses to 21000, flipping health from exit 4 (stale) to exit 0 (current). Preserve the transcript timestamp when its contents are unchanged and the metadata read failed.
  2. Malformed Grok chat metadata is stamped as a successful import. A metadata-only fixture with invalid JSON in chat_history.jsonl set chat_reliable=0, reported zero sync failures, and moved health from exit 4 to exit 0. Distinguish an absent optional file from invalid chat content so valid transcript ingestion can continue while the metadata gap remains visible.

All 18 focused tests passed, including valid transcript ingestion alongside a broken summary. Router revalidation also passed with both a newer database mtime and a WAL-only commit that left the imported rows unchanged. git diff --check passed. Runtime evidence used synthetic fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 7 independent review of 4493cf40907c2aa5a5ff915ea0b28d7753bbe566: Changes requested.

Blocking: An unchanged, empty events.jsonl still lets a failed Grok summary import clear the freshness gap. In a synthetic fixture where only summary.json changed, sync reported invalid Grok summary JSON and imported zero responses, but advanced the empty log’s import timestamp from 11000 to 21000. health --json flipped from exit 4 (stale, 9,000 seconds behind) to exit 0 (current). A full sync reproduced the same failure with an unchanged, complete updates.jsonl. Both paths bypass the guard because JsonlSource.unchanged is false for a zero-offset source or a forced replay.

All 151 focused tests and git diff --check passed. The malformed-chat fixture failed against the previous Grok adapter and passed at this head. The new summary fixture passed on both revisions because its original {} matches the failed-read fallback. With a populated original summary and a complete transcript, I verified that the previous revision returned current and this head returned stale. Runtime verification used synthetic fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 8 independent review: changes requested on 03e098b3996b89c6808e0e37bd21be4cc0c36b2a.

Blocking: Grok import bookkeeping treats a file size or mtime change as import progress even when metadata fails and no complete transcript record was added. In a synthetic fixture, appending an unterminated line and corrupting the newer summary.json left the transcript read offset and metadata import time unchanged. Sync reported invalid Grok summary JSON but advanced the transcript import timestamp from 11000 to 21000. health --json flipped from exit 4 (stale, 9,000 seconds behind) to exit 0 (current). A timestamp-only touch reproduced the same result. Preserve the freshness timestamp in these cases while continuing to ingest valid transcript changes.

Both round 7 regressions failed on 4493cf4 and passed on this head. All 986 local tests and git diff --check passed. Independent runtime evidence used synthetic fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 9 independent review: changes requested on fe4ced666725362b2d1e6e479c2579a20be04fdd.

  1. An ordinary Grok retry leaves health stale and response metadata missing. I appended a valid completion while reads of summary.json were denied. The partial sync imported the response and correctly left health at exit 4. After access was restored, ordinary sync reported zero failures, but health still showed 9,000 seconds of lag, and the new response still lacked its model and effort. A full replay corrected both. The unchanged fast path needs to reconcile rows from the partial import before recording successful completion, even when the metadata fingerprint matches.
  2. The zero timestamp leaks into existing reports. A first partial import correctly produced never_synced in health, but search reported ledger_synced_at: 1970-01-01T00:00:00Z, and the task report's source cutoff was 0.0. Keep the transcript import time accurate for existing consumers and track complete-session freshness separately.

All 986 tests, compilation, the CLI version check, and git diff --check passed. The touch, incomplete-append, and first-partial regressions failed on the prior head and passed on this one. Independent runtime proof used synthetic fixtures. The PR's stale-ledger-copy proof covers an earlier head.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 10 independent review: changes requested on e4e8060127e4a4186927dbada2007482adc75833.

  1. Grok recovery can leave duplicate usage and stale health. When summary.info.id differs from the directory name, a denied summary read imports transcript rows under the directory name and stores the pending flag under that key. After access returns, ordinary sync looks up the summary ID and misses the pending replay. In the fixture, sync reported zero failures, health still exited 4 with 9,000 seconds of lag, and the ledger held three response rows for two completions.
  2. Repeated failed retries advance existing report timestamps. With summary access still denied and the transcript unchanged, a second ordinary sync moved the task report's source cutoff from 21000 to 21500 and advanced search's ledger_synced_at. The pending flag forces a replay whose finish() records a new import time.
  3. Grok health counts symlink aliases twice. A session directory and its symlink produced newer_sources: 2. Ordinary sync imported one source and one session.

Both Round 9 cases passed when the summary ID matched the directory name. Ordinary retry restored model and effort and moved health from exit 4 to 0. A first partial import kept health at never_synced while search and task reports showed the actual transcript import time.

All 986 tests, compilation, CLI version, and git diff --check passed. Independent runtime evidence used synthetic fixtures. The exact-head stale-ledger-copy run is documented in #149.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 11 independent review: changes requested on b78555530cbb30cac4b80c3c6cdb40f4be4777aa.

Blocking: If summary.info.id differs from the directory name and the summary is unreadable during the first transcript import, Grok saves the response and pending retry under the directory name. Once the summary becomes readable, ordinary sync looks for the pending retry under the summary ID and misses it. I reproduced two outcomes with one completion: an empty ledger ended up with two response rows while health exited 0, and a ledger with a prior metadata-only import stayed 9,000 seconds behind with health exiting 4, even though sync reported zero failures. Recovery needs to reconcile the pending import by source across the identity change.

All 37 health tests passed, including the new fixture for an already-recorded transcript. git diff --check and the exact-head GitHub proof check passed. Independent runtime evidence used synthetic fixtures. The stale-ledger-copy proof was outside this review.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 12 independent review: changes requested on d25a9873b915f82ff2c9e207e7ac492ebdc5dfb9.

Blocking: When events.jsonl is the first log imported while summary.json is unreadable, Grok records the directory name as the session ID. On retry, the recorded ID overrides both the recovered summary and a later update carrying the correct ID. I reproduced health --json switching from exit 4 to exit 0 while the ledger still held two session rows, with the event and response attached to grok:directory-id instead of the existing grok:native-id row. The same split occurs when the first completion has no sessionId. Reconcile the provisional ID with the recovered native ID before marking the retry complete.

The new ID-bearing fixture failed on the prior head and passed on this one. All 988 tests, compilation, the CLI version check, and git diff --check passed. Runtime verification used synthetic fixtures. The stale-ledger-copy evidence reviewed covers an earlier head.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 13 independent review: changes requested on 4584bd9e6a844e7a34dee16c5d077953a9eba529.

The denied-summary case now recovers correctly. When the first sync sees events.jsonl alongside a missing or empty summary, it still records the directory name as the session ID. I reproduced a later sync that received native-id in both the summary and the update but attached the response to grok:directory-id and reported health current with exit 0. Treat directory-derived IDs as provisional and reconcile them when the native ID arrives.

GitHub's proof check passed, along with 153 focused tests and 19 placement tests locally. I stopped the local full-suite retry when a placement test reached the host Cue database and reran placement with an isolated Cue path. Independent behavioral evidence used synthetic fixtures. The reported stale-ledger copy run covers the previous head.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 14 independent review: changes requested on af16e49b6f1c6a6eb61218e007957021cd3f3892.

  • Valid events-only sessions can stay unimported. With a missing or empty summary, a supported turn_started event carrying session_id: "native-id" imported zero events and returned Grok session identity unavailable. The previous head imported the event. Use the validated event ID before deferring.
  • A readable summary without an ID still locks in the directory name. With {"head_branch":"main"} as the initial summary, sync stored the event under grok:directory-id. After both the summary and an update supplied native-id, sync still attached the response to grok:directory-id and health --json exited 0 with current. Resolve identity from a validated ID before treating a directory-derived key as final.

The denied, missing, and empty summary recovery cases passed, along with 219 focused tests covering freshness, JSON, and existing visibility checks. GitHub proof passed. Independent runtime verification used synthetic fixtures. The stale-ledger copy run is documented in #149.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 15 independent review: changes requested on b120a9da5baac4a3bee6339f081e0531ecd1bf8f.

  1. A complete Grok notification with no explicit ID and a readable summary could no longer use its directory as the session ID.
  2. A recorded legacy directory ID overrode a later explicit summary ID, unlike base behavior.
  3. A rejected update missing params.update could supply an ID that displaced the valid events stream ID.

The events-only regression passed. Isolated full-suite testing passed 989 of 990 tests, with the lone environment-dependent test passing in a 151-test rerun. Runtime evidence used fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 16 independent review: changes requested on 6954eb0ae9d4ca28d81563c610ada0366ef266ea.

Blocking: With no summary ID, Grok's identity lookup accepts an update before the importer validates it. An unknown update kind and a tool_call missing toolCallId each reproduced the regression: the importer rejected the update but stored the valid turn_started event under grok:wrong-id rather than the native identity. Health returned exit 0 with current status. Require update validation before using its session ID.

The 221 focused tests and GitHub proof passed. Runtime verification used isolated fixtures. The documented stale-ledger copy run covers an earlier revision.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 17 independent review: changes requested on 0e79f00b1452d2a4ccf4c8e8fcd590e0120d2c28.

Blocking regression: When a Grok summary is malformed and the only native ID comes from a valid tool_call notification, the completion-only identity check skips the entire transcript. In an isolated fixture, base and the previous head each imported one tool event, while this head imported zero. Accept a tool notification as identity evidence after applying the importer's existing toolCallId validation, and keep reporting the summary error.

Round 16 cases passed independently, including rejection of malformed usage before a valid completion. All 223 focused tests and git diff --check passed. Runtime verification used isolated fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 18 independent review: changes requested on a660c3b13f7ef68c34a3c15c09c36be7521aa45e.

Blocking regression against 75ed333: On first sync with a malformed Grok summary, identity lookup ignores valid user and assistant message updates carrying a native session ID. With those updates as the only identity evidence, base imported one user message and one assistant event. This head imported neither until the summary was repaired. Both tool notification cases now import while reporting the summary error.

GitHub proof, 211 focused tests, and versionctl release-check passed. The local archive run lacked Git history, and all 55 tests in the affected modules passed when rerun from the checkout. Runtime review used isolated fixtures.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 19 independent review: changes requested on 8336bc2e67fec498a547280e38b184a2f733d527.

Blocking regression. On first sync with a malformed summary.json, Grok still defers the entire import when its only native session ID comes from a valid auto_compact_started or retry_state update. Base imported one event in each case, while this head imported zero and reported invalid Grok summary JSON. Both kinds are already accepted by the importer. Use that validation for identity recovery while preserving the summary error.

The user-only and assistant-only fixtures both passed. All 993 local tests, GitHub proof, and versionctl release-check --json passed. Runtime verification used isolated fixtures. The stale-ledger-copy run was outside this review.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 20 independent review: changes requested on cd6b4935ac403146078f9bcf9d4f21d44a1ad0e2.

Blocking regression: Router sync probes the WAL before opening jobs.db. A readable jobs.db and WAL symlink whose target directory denied traversal yielded exit 0 and JSON with one imported source on base. The PR head raised PermissionError: [Errno 13] and emitted no JSON. Let SQLite determine whether sync can proceed when the timestamp probe fails, and leave the freshness timestamp unchanged if the probe could not verify it.

Round 19's Grok regression is fixed. The 104 focused tests, release validation and GitHub proof passed. Nine local archive test errors came from missing Git history, and all 49 tests in the affected modules passed in a scratch clone. Runtime review used fixtures only.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 21 independent review: changes requested on 843de0efa32d45f59c394ecc8312ac1099922bb2.

A denied Claude scan correctly exits 4, but the raw exception reaches both text and JSON and exposes the absolute native source directory. This reproduces on the candidate and is absent on base. Preserve the unavailable result with a path-free reason.

The Router permission regression is fixed. All 995 tests passed in a scratch clone and GitHub proof passed. Runtime verification used isolated fixtures.

@lukemaj lukemaj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round 22 independent review: changes requested on d6f41d60a73a022fffd31229a499b2a98d001e2c.

  1. Ledger permission errors expose the absolute ledger path. Denying traversal of a fixture ledger's parent directory made both text and JSON exit 4 and print the path. Use a path-free diagnostic and preserve the unavailable status.
  2. An out-of-range OpenCode timestamp crashes text health. A fixture with time_updated=1760000000000000 raised ValueError: year must be in 1..9999, not 57742 in text mode. JSON exited 4 and reported never synced. Validate timestamps before rendering so both formats report the failure and preserve other harness results.

The prior Claude path disclosure is fixed. All 996 tests, the release check and git diff --check passed. Runtime checks used isolated fixtures.

@lukemaj

lukemaj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Round 23 independent review: changes requested on 0f6252fe84400bb4cb69b30a24874812a23466fe.

  • A malformed Grok import timestamp aborts health. A fixture with a numeric transcript timestamp and a text metadata timestamp raised TypeError before either format emitted output. Base still emitted its visibility report. Validate both timestamps before comparing them so Grok reports unavailable, health exits 4, and other harness results survive.
  • A malformed OpenCode timestamp can hide a newer session. A text value in message.time_updated won SQLite’s MAX over a valid numeric session timestamp, then Python discarded it. Both formats reported no_native_records and exited 0. Detect invalid timestamp types before aggregation and report the scan as unavailable.

Both Round 22 regressions passed on this head and failed on the prior revision. All 998 tests, release validation, and git diff --check passed. Runtime review used isolated fixtures. The stale-ledger-copy run was outside this review.

@lukemaj

lukemaj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Round 24 independent review: changes requested on 71bdd488cdece0f9b925d6ad290c92b3a195ea11.

  • Blocking. OpenCode freshness reads only time_updated for messages and parts. A fixture with a message created 9,000 seconds after the last import and a NULL update time reported current, zero newer sources, and exit 0 in both formats. The importer accepted the fixture on base and head. Setting only the message’s update time to its creation time made health report stale, one newer source, and exit 4. Include message and part creation timestamps in the scan.
  • Nonblocking. A malformed OpenCode session timestamp can still win the visibility query’s MAX and cause a false invisible-session finding even when a valid session exists for the same directory. The freshness scan correctly fails health in this case.

The three timestamp regressions failed on the previous commit and passed on this head. python3 -m unittest discover -s tests passed all 1,001 tests, and git diff --check, compilation, versionctl release-check --json, and GitHub proof passed. A synthetic stale-ledger copy exited 4 in both formats at 3,600 seconds and 0 at the exact 9,000-second boundary. Runtime checks used isolated fixtures, and the scratch clones were removed.

@lukemaj

lukemaj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Round 25 independent review: changes requested on 4a185b06d1f41e859b5203e6af1cfef8bed08d45.

  • Blocker: With a running OpenCode process, the visibility query can abort health before any harness reports freshness. In isolated CLI fixtures, a creation-only session schema raised no such column: time_updated, and a corrupt native database raised file is not a database. Both text and JSON produced zero stdout. Handle SQLite query failures as an unavailable visibility check and continue to freshness.
  • Proof: Both round-24 regressions failed on 71bdd48 and passed on the reviewed head. A separate fixture confirmed that committed WAL activity contributes both timestamp columns, counts each session once, and preserves valid visibility alongside a malformed timestamp.
  • Checks: All 52 health tests and all 1,003 deterministic tests passed, along with compilation, git diff --check, and release validation for 1.6.0. Proof used isolated fixtures, with no live ledgers or GitHub writes.

@lukemaj

lukemaj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Independent review passed for PR #150 at a71c37599888d8da2aa9170d69bed87b2fbaa084 against 75ed333b0c9928fc306bcfc188f84829594a0d83, with no blocking findings.

In isolated CLI fixtures, a creation-only OpenCode schema and a corrupt database both crashed the prior head before it emitted output. The reviewed head exits 4 in text and JSON, reports OpenCode visibility as unavailable, and preserves Claude’s stale-ledger result. A missing OpenCode database still produces the existing invisible-process finding, and lag exactly equal to the configured interval exits 0.

All 1,004 local tests passed, including 53 health tests, along with compilation, git diff --check, release validation for 1.6.0, and GitHub proof. Runtime verification used synthetic fixtures. GitHub proof

@lukemaj
lukemaj merged commit c2a035e into main Nov 25, 2026
2 checks passed
@lukemaj
lukemaj deleted the fix/149-health-ledger-freshness branch November 25, 2026 20:29
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.

health: report when the ledger is behind the machine's native session records

1 participant