Skip to content

fix(cleanup-sessions): hard-fail when sqlite3 is missing or the query errors - #1889

Closed
Sanjays2402 wants to merge 1 commit into
nanocoai:mainfrom
Sanjays2402:fix/cleanup-sessions-env-fallback
Closed

Sanjays2402 wants to merge 1 commit into
nanocoai:mainfrom
Sanjays2402:fix/cleanup-sessions-env-fallback

Conversation

@Sanjays2402

Copy link
Copy Markdown

Type of Change

  • Fix - bug fix or security fix to source code

Description

Addresses the silent-data-loss half of #1825.

The cleanup script treats sqlite3 failures as "no active sessions":

ACTIVE_IDS=$(sqlite3 "$STORE_DB" "SELECT session_id FROM sessions;" 2>/dev/null || true)

This swallows every sqlite3 error — missing binary, malformed DB, locked DB, permissions. ACTIVE_IDS ends up empty, is_active() returns false for everything, and the rest of the script goes on to delete:

  • Session JSONLs + tool-results dirs older than 7 days
  • Debug logs older than 3 days
  • Todo files older than 3 days
  • Telemetry files older than 7 days

…including the currently-active session if any of its files happen to be past the retention window.

The fix

Refuse to run when we can't reliably enumerate active sessions:

  1. command -v sqlite3 up front — if absent, print an install hint and exit 1.
  2. Capture sqlite3 output with ACTIVE_IDS=$(sqlite3 …) directly instead of piping through || true, and exit 1 if the query itself fails.

What this PR does NOT do

The issue also requested env-var fallbacks (NANOCLAW_STORE_DIR / NANOCLAW_DATA_DIR / NANOCLAW_GROUPS_DIR). After auditing src/config.ts, those env vars are not currently supported by NanoClaw itself — STORE_DIR / DATA_DIR / GROUPS_DIR are hardcoded via path.resolve(PROJECT_ROOT, ...). So adding fallbacks to the script alone would just hide a mismatch rather than fix one. If the project wants to adopt those env vars, the right sequence is (1) add them to src/config.ts, (2) mirror them here — happy to file that as a follow-up PR if maintainers want it.

Diff

-if [ ! -f "$STORE_DB" ]; then
-  log "ERROR: database not found at $STORE_DB"
-  exit 1
-fi
-
-ACTIVE_IDS=$(sqlite3 "$STORE_DB" "SELECT session_id FROM sessions;" 2>/dev/null || true)
+if ! command -v sqlite3 >/dev/null 2>&1; then
+  log "ERROR: sqlite3 CLI not found — refusing to run so we don't wipe the active session."
+  log "       Install sqlite3 (e.g. 'apt install sqlite3' or 'brew install sqlite') and re-run."
+  exit 1
+fi
+
+if [ ! -f "$STORE_DB" ]; then
+  log "ERROR: database not found at $STORE_DB"
+  exit 1
+fi
+
+if ! ACTIVE_IDS=$(sqlite3 "$STORE_DB" "SELECT session_id FROM sessions;" 2>&1); then
+  log "ERROR: sqlite3 query failed: $ACTIVE_IDS"
+  log "       Refusing to run so we don't wipe the active session."
+  exit 1
+fi

1 file changed, +11, −1.

Verification

  • bash -n scripts/cleanup-sessions.sh → clean.
  • Manually tested:
    • Run with sqlite3 missing from PATH → logs the install hint and exits 1 before any remove call.
    • Run with a bogus path to STORE_DB → existing "database not found" branch still wins, exits 1.
    • Run against a valid DB → same behavior as before, no regression.
  • Behavior on healthy machines is unchanged — sqlite3 is a practical hard dep of NanoClaw already (it is the message store).

For Skills

N/A — no skills changed.

… errors

The old flow:

    ACTIVE_IDS=$(sqlite3 "$STORE_DB" "SELECT session_id FROM sessions;" \
        2>/dev/null || true)

swallows every sqlite3 error (missing binary, malformed DB, locked DB,
permissions). ACTIVE_IDS ends up empty, is_active() returns false for
every id, and the rest of the script happily deletes session JSONLs,
tool-results dirs, debug logs, todos, and telemetry whose mtime is
past the retention window \u2014 including the currently-active session if
it happens to be older than 7 days.

Refuse to run when we can't reliably enumerate active sessions:

1. Check command -v sqlite3 up front. If absent, print an install hint
   and exit 1.
2. Capture the sqlite3 output with ACTIVE_IDS=$(sqlite3 ...) instead of
   piping through '|| true', and exit 1 if the query itself fails.

No behavior change on healthy machines \u2014 sqlite3 is a hard dep in
practice (the project literally uses sqlite for the store), so making
the script say so explicitly costs nothing and avoids a silent data-
loss foot-gun.

Addresses the second half of #1825.
@Sanjays2402
Sanjays2402 force-pushed the fix/cleanup-sessions-env-fallback branch from 41610a9 to b54f937 Compare April 23, 2026 02:47
gavrielc pushed a commit that referenced this pull request May 6, 2026
Setup deliberately avoids the sqlite3 CLI (`setup/verify.ts:5` calls
this out: "Uses better-sqlite3 directly (no sqlite3 CLI)") and never
installs or probes for the binary. Despite that, 13 skills shelled out
to `sqlite3 ...` directly, breaking on hosts where the CLI isn't
preinstalled — the same root cause as #2191 but spread across the
skill surface.

Add `scripts/q.ts`, a ~30-LOC wrapper over the `better-sqlite3` dep
that setup already installs and verifies. Default output matches
`sqlite3 -list` (pipe-separated, no header) so existing skill text
reads identically — only the binary changes. SELECT/WITH queries go
through `db.prepare().all()`; everything else (INSERT/UPDATE/DELETE,
including compound statements) goes through `db.exec()`.

Migrate every in-tree caller:

- 17 hardcoded invocations across 8 SKILL.md files (init-first-agent,
  add-deltachat, add-signal, add-emacs, add-whatsapp, add-ollama-provider,
  debug, add-parallel) plus add-deltachat/VERIFY.md.
- `manage-channels/SKILL.md` shows canonical SQL but never prescribed
  a tool, so the assistant defaulted to `sqlite3` and silently failed.
  Add a one-line wrapper hint above the SQL block.
- `migrate-v2.sh` schema/count probes (was the original #2191 case).
  Replace `.tables` with `SELECT name FROM sqlite_master`.
- Document the wrapper convention in root `CLAUDE.md` under "Central DB".

Add `scripts/q.test.ts` with 6 vitest cases covering both modes,
NULL rendering, empty-result, compound mutations, and arg validation.
Wire `scripts/**/*.test.ts` into `vitest.config.ts`.

Out of scope (flagged for follow-up):
- `debug` and `add-parallel` still reference the v1-only path
  `store/messages.db`. Routing through the wrapper now produces a
  cleaner "no such file" error, but the surrounding sections are
  v1-era throughout — a v1-content cleanup is its own PR.
- `cleanup-sessions.sh` is being addressed in #1889 (different style,
  hard-fail rather than wrap); left untouched here to avoid stepping
  on that author's work.

Closes #2191.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
cianwhalley pushed a commit to cianwhalley/nanoclaw that referenced this pull request May 26, 2026
Setup deliberately avoids the sqlite3 CLI (`setup/verify.ts:5` calls
this out: "Uses better-sqlite3 directly (no sqlite3 CLI)") and never
installs or probes for the binary. Despite that, 13 skills shelled out
to `sqlite3 ...` directly, breaking on hosts where the CLI isn't
preinstalled — the same root cause as nanocoai#2191 but spread across the
skill surface.

Add `scripts/q.ts`, a ~30-LOC wrapper over the `better-sqlite3` dep
that setup already installs and verifies. Default output matches
`sqlite3 -list` (pipe-separated, no header) so existing skill text
reads identically — only the binary changes. SELECT/WITH queries go
through `db.prepare().all()`; everything else (INSERT/UPDATE/DELETE,
including compound statements) goes through `db.exec()`.

Migrate every in-tree caller:

- 17 hardcoded invocations across 8 SKILL.md files (init-first-agent,
  add-deltachat, add-signal, add-emacs, add-whatsapp, add-ollama-provider,
  debug, add-parallel) plus add-deltachat/VERIFY.md.
- `manage-channels/SKILL.md` shows canonical SQL but never prescribed
  a tool, so the assistant defaulted to `sqlite3` and silently failed.
  Add a one-line wrapper hint above the SQL block.
- `migrate-v2.sh` schema/count probes (was the original nanocoai#2191 case).
  Replace `.tables` with `SELECT name FROM sqlite_master`.
- Document the wrapper convention in root `CLAUDE.md` under "Central DB".

Add `scripts/q.test.ts` with 6 vitest cases covering both modes,
NULL rendering, empty-result, compound mutations, and arg validation.
Wire `scripts/**/*.test.ts` into `vitest.config.ts`.

Out of scope (flagged for follow-up):
- `debug` and `add-parallel` still reference the v1-only path
  `store/messages.db`. Routing through the wrapper now produces a
  cleaner "no such file" error, but the surrounding sections are
  v1-era throughout — a v1-content cleanup is its own PR.
- `cleanup-sessions.sh` is being addressed in nanocoai#1889 (different style,
  hard-fail rather than wrap); left untouched here to avoid stepping
  on that author's work.

Closes nanocoai#2191.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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