Skip to content

Phase 4: incremental hallu sync via latest_version on listing - #415

Merged
neuromechanist merged 2 commits into
feature/issue-406-epic-rate-limit-hardeningfrom
feature/issue-409-phase4-hallu-incremental
May 10, 2026
Merged

neuromechanist merged 2 commits into
feature/issue-406-epic-rate-limit-hardeningfrom
feature/issue-409-phase4-hallu-incremental

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Eliminate the ~100 per-dataset `/datasets//manifest` API calls the hallu cron makes every hour by surfacing `latest_version` on the listing endpoint and consuming it in the script.

Closes #409
Part of epic #406

Changes

  • `backend/src/routes/datasets.ts`: `GET /datasets` listing SELECTs add a correlated subquery returning the most-recent `dataset_versions.version` per dataset (null when none). Applied to both the `mine` branch and the public-catalog managed branch; catalog-only rows get `NULL AS latest_version` so the UNION column count stays balanced.
  • `src/lib/api.ts`: `Dataset` interface gains `latest_version?: string | null` (optional + nullable so older backend deployments don't break callers).
  • `scripts/hallu-sync.sh`:
    • `discover_datasets` emits `dataset_idlatest_version` pairs (TSV) instead of plain IDs.
    • Main loop uses the listing-provided version directly; falls back to the per-dataset `get_latest_version` only when the listing didn't carry one (older backend / no minted version yet). The fallback count is logged in the sync summary so operators can spot regressions.
    • Per-dataset log lines now use consistent tags: `[FRESH]` first download, `[UPDATE]` version changed, `[SKIP]` steady-state no-op, `[ADOPT]` existing checkout adopted into manifest, `[CLEAN]` partial-download cleanup, `[ZIP]` zip sync, `[WAIT]` zip not yet generated, `[FAIL]` error.

Acceptance checklist (from #409)

  • Cron run on a fully-synced state issues 0 `git clone` commands. (Already true before this PR.)
  • Bandwidth on a steady-state cron run is negligible. With this PR, zero per-dataset API calls in the steady state — only the paginated listing.
  • Manifest is updated atomically; failure on one dataset does not block others. (Already true; preserved.)
  • Log lines clearly differentiate 'skipped (unchanged)' from 'fetched (new version)'. Tagged `[SKIP]` vs `[UPDATE]` / `[FRESH]`.

Out of scope (with reason)

  • Shallow first-time clone. Current first-download path uses `nemar dataset download` (a CLI op); rewriting to `git clone --depth 1` would either touch the CLI (broader change) or duplicate logic in the script. Subsequent runs are already O(1) so the one-time cost is acceptable.
  • Exponential backoff on fetch failures. Current behavior is "log and continue" per dataset which satisfies acceptance criterion 3.
  • Server-side `?prefix=nm` filter on the listing. Today the script filters by regex post-fetch; the listing returns at most a few hundred rows so this is fine.

Test plan

  • `bash -n scripts/hallu-sync.sh` clean
  • `bun run build` clean
  • `bun test test/{workflow-sync,rate-limit-retry,tree-batched-commit}.test.ts` -> 51/51 pass (no regressions from prior phases)
  • Live api.test.ts assertion (needs test API keys configured against a deployed dev backend): `bun test test/api.test.ts` should show the new "latest_version" test passing. Post-merge.
  • Manual on hallu post-merge: `bash scripts/hallu-sync.sh` verbose; `grep '[SKIP]' .nm-sync.log` shows skipped rows, `grep '/manifest' .nm-sync.log` shows zero calls when listing carries `latest_version`.

…PI call

Backend: `GET /datasets` listing now includes `latest_version` from
the most-recent `dataset_versions` row per dataset (null when none).
Adds one correlated subquery per row; for the typical 50-100-row
listing the added cost is negligible.

CLI: `Dataset` type gains `latest_version?: string | null`.

Script: `discover_datasets()` emits `dataset_id<TAB>latest_version`
pairs. The main loop uses the listing-provided version directly,
falling back to the per-dataset `/manifest` call only when the listing
didn't carry one (older backend / no minted version yet). For ~100 nm
datasets this drops ~100 extra API calls per cron cycle to zero in the
steady state.

Logging: per-dataset lines now carry consistent tags `[FRESH]`,
`[UPDATE]`, `[SKIP]`, `[ADOPT]`, `[CLEAN]`, `[ZIP]`, `[WAIT]`,
`[FAIL]` so grep-based dashboards can differentiate steady-state
skips from real fetches (acceptance criterion 4 in #409).
@github-actions

github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR415

Version: 0.8.9-PR415.1efb1ba
Commit: 1efb1ba


🤖 Updated on each push.

…test

scripts/hallu-sync.sh:
- discover_datasets() now captures jq exit code and stderr; a parse
  failure (malformed API JSON, schema regression) returns 1 and
  log_errors with the captured message instead of silently truncating
  the page set. The /datasets length sanity check also validates the
  shape before continuing.
- main() propagates discover_datasets failure as a sync abort.
- Added a closing alarm: when the listing carried no latest_version for
  any dataset (manifest_fallbacks == total > 0), log_error explicitly
  flags 'the listing optimisation may have regressed' so cron monitoring
  fires.

backend/src/routes/datasets.ts:
- SQL comments at both managed SELECTs document that latest_version is
  the hallu-sync contract; future maintainers shouldn't change the
  ordering without updating the script.

test/api.test.ts:
- New value-correctness test: cross-checks listing's latest_version
  against /versions (same DESC-by-created_at order as the SQL subquery).
  Tolerates dev DBs that have no minted version via early return.

Intentionally not addressed:
- Renamed log-string tags break grep-based runbooks. The new tags
  ([SKIP]/[UPDATE]/[FRESH]/[FAIL]) are the AC #4 improvement; the
  rename is called out in the PR description and the operator runbook
  in MEMORY (infra_hallu_sync.md) will be updated separately.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review summary

Three parallel reviewers ran (code-reviewer, silent-failure-hunter, pr-test-analyzer).

Critical — addressed

  • silent-failure-hunter: `jq` stderr suppression + `[[ -z "$rows" ]]; break` conflated three different outcomes (end of pagination, malformed JSON, schema regression), silently truncating the sync. Fix: capture jq exit code + stderr to a tempfile; on non-zero, `log_error` with the captured message and `return 1`. The `/datasets | length` sanity check also validates shape before continuing. `discover_datasets` failure now aborts the cycle instead of silently producing a no-op.

High — addressed

  • silent-failure-hunter: If the listing regressed to all-null `latest_version`, the script silently fell back to the per-dataset API call with no alarm. Fix: closing log_error when `manifest_fallbacks == total > 0` so cron monitoring fires.
  • pr-test-analyzer: Test was shape-only; an SQL regression returning null for everything would still pass. Fix: added a cross-check test that compares the listing's `latest_version` against `GET /datasets/:id/versions` (which reads the same `dataset_versions` table with the same ordering). Tolerates dev DBs with no minted version via early return.

Important — addressed

  • code-reviewer: SQL contract not documented; future maintainers might change the ordering without updating the script. Fix: SQL comments at both managed SELECTs.

Intentionally not addressed

  • code-reviewer: renamed log strings break grep-based runbooks. The new tags (`[SKIP]` / `[UPDATE]` / `[FRESH]` / `[FAIL]` / `[ZIP]` / `[WAIT]` / `[ADOPT]` / `[CLEAN]`) are the explicit AC Remove automatic collaborator addition on user approval #4 improvement; reverting to legacy phrases defeats the point. The PR description calls out the rename; the operator runbook (`infra_hallu_sync.md` per MEMORY) will be updated separately.
  • silent-failure-hunter: TSV row missing tab → `listing_version=""` → falls back to /manifest call. Functionally safe via the existing fallback path; not worth a separate `[WARN]`.

Verification

  • `bash -n scripts/hallu-sync.sh` clean
  • `bun run build` clean
  • `bun test test/{workflow-sync,rate-limit-retry,tree-batched-commit}.test.ts` -> 51/51 pass (no regressions from prior phases)
  • New `test/api.test.ts` value-correctness assertion runs against live dev backend post-deploy.

@neuromechanist
neuromechanist merged commit d1e24da into feature/issue-406-epic-rate-limit-hardening May 10, 2026
1 check passed
@neuromechanist
neuromechanist deleted the feature/issue-409-phase4-hallu-incremental branch May 11, 2026 05:15

This branch was previously deployed

1 inactive deployment
npm — 1efb1ba9 Deployed May 10, 2026 by neuromechanist via publish #727
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.

1 participant