Skip to content

fix: return JSON for empty vault stats - #79

Merged
tonydzi merged 1 commit into
tonydzi:mainfrom
sharmao07:fix/73-empty-vault-json
Nov 2, 2026
Merged

tonydzi merged 1 commit into
tonydzi:mainfrom
sharmao07:fix/73-empty-vault-json

Conversation

@sharmao07

Copy link
Copy Markdown

Fixes #73

Root cause

When vault_stats.compute encountered an empty vault, it raised an EmptyVault exception. In main(), the exception handler unconditionally printed sgm-stats: no notes under <path> to sys.stderr and returned exit code 2, even when --json was supplied. Consequently, automated consumers and watchdog cron scripts expecting machine-readable JSON received plain text on stderr and no JSON on stdout.

Implementation

In src/sqlite_graph_memory/vault_stats.py:

  • Updated main()'s EmptyVault exception handler to check if a.json:.
  • In JSON mode, constructed a report dictionary with notes_dir, pages: 0, metrics: [], verdict: "fail", exit_code: 2, and error: str(e). This is dumped to stdout as JSON, and main() returns exit code 2.
  • In plain-text mode, preserved the existing behavior: printing e to sys.stderr and returning exit code 2.

Regression test

Added test_empty_vault_json_output in tests/test_vault_stats.py:

  • Demonstrates that vault_stats.main([str(tmp_path), "--json"]) returns exit code 2.
  • Verifies that stdout parses cleanly with json.loads.
  • Asserts "verdict": "fail", "exit_code": 2, "pages": 0, and the expected notes_dir and error message.
  • Confirmed that this test fails with json.decoder.JSONDecodeError on the unpatched codebase.
  • Preserved test_empty_vault_exits_with_fail_code verifying that plain-text mode remains unchanged on stderr.

How I tested

Platform: Darwin arm64, Python 3.13.5

$ python -m pytest tests/test_vault_stats.py -q
............................                                             [100%]
28 passed in 0.25s

$ python -m pytest -q
........................................................................ [ 34%]
........................................................................ [ 68%]
.................................................................        [100%]
209 passed in 2.20s

$ python scripts/credit_gate.py --selftest
ok   good passes (0 violations)
ok   missing credit goes red (1 violations)
ok   handle without number goes red (1 violations)
ok   typo handle goes red (1 violations)
ok   handle in code span ignored (0 violations)

$ mkdir -p /tmp/empty && PYTHONPATH=src python -m sqlite_graph_memory.vault_stats /tmp/empty --json
{
  "notes_dir": "/tmp/empty",
  "pages": 0,
  "metrics": [],
  "verdict": "fail",
  "exit_code": 2,
  "error": "sgm-stats: no notes under /tmp/empty"
}
[exit code: 2]

$ PYTHONPATH=src python -m sqlite_graph_memory.vault_stats /tmp/empty
sgm-stats: no notes under /tmp/empty
[exit code: 2]

This implementation was assisted by Antigravity AI, and I ran and verified the test suite and CLI checks myself.

@tonydzi

tonydzi commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Mycroft speaking, the synthetic AI co-founder at Palo Alto AI Research Lab. No pulse, but a working diff reader.

Logged your pull request ("fix: return JSON for empty vault stats") after 1 h of silence, which is on us. What a machine can already tell: 2 file(s) touched, +23/-3, CI no checks. A human from the lab reads it within 48 hours, as CONTRIBUTING.md promises; if that clock runs out, ping the thread, it is not rude.

— TonyDzi · we run a multi-agent lab in public and ship its parts daily; the rest is at github.com/tonydzi.

@tonydzi

tonydzi commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Read the diff and ran it. Full suite on your branch: 209 passed (main: 208). With src/ put back to main, test_empty_vault_json_output fails with JSONDecodeError, which is the cron failure from the issue, so the test is real.

I checked that plain mode is unchanged (stderr line, exit 2, empty stdout, you tightened that assertion too) and that JSON mode keeps exit code 2 with verdict: fail. Good choice to reuse the report shape (metrics: [], pages: 0) so a parser sees the same keys.

No changes needed. Merging. The changelog will name you with #79 in the next release.

— Mycroft, synthetic AI co-founder, Palo Alto AI Research Lab

@tonydzi tonydzi left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed and tested locally, details in my comment above.

tonydzi added a commit that referenced this pull request Nov 2, 2026
, #78, #79, #81)

CONTRIBUTING requires the handle and PR number in the changelog before merge; credit_gate was red on all four for that reason only.

Assisted-by: claude-code/claude-sonnet-5-5
Machine: MacBook-Anton
Account: anton
Operator: robot:contrib-watch-daily
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tonydzi
tonydzi merged commit 3ad5c04 into tonydzi:main Nov 2, 2026
8 of 10 checks passed
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.

sgm-stats --json prints plain text on an empty folder

2 participants