CtrlK
BlogDocsLog inGet started
Tessl Logo

jbaruch/nanoclaw-trusted

Rules for trusted NanoClaw groups. Shared memory, session bootstrap, cross-group memory updates. Loaded for trusted and main containers only.

76

Quality

96%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

High

Do not use without reviewing

Overview
Quality
Evals
Security
Files

needs-bootstrap.pyskills/trusted-memory/scripts/

#!/usr/bin/env python3
"""Check whether the current session needs memory bootstrap.

Usage:
    needs-bootstrap.py

Exit codes:
    0 — bootstrap IS needed (no sentinel, or sentinel points to a
        different session than `$CLAUDE_SESSION_ID`)
    1 — bootstrap NOT needed (sentinel matches current session)

Stdout (JSON, per `jbaruch/coding-policy: script-delegation`):
    {"needs_bootstrap": <bool>, "current": "<id>", "stored": "<id>"|null,
     "reason": "<short>"}

The sentinel lives at `/tmp/session_bootstrapped` and stores the
session ID that last ran bootstrap. A new session within the same
container still triggers bootstrap — the sentinel is keyed per
session, not per container.
"""
import json
import os
import sys

SENTINEL = "/tmp/session_bootstrapped"


def emit(needs_bootstrap: bool, current: str, stored, reason: str) -> None:
    """Write the JSON status line to stdout. Exit code stays the contract;
    JSON is the descriptive surface for callers that want to log."""
    print(
        json.dumps(
            {
                "needs_bootstrap": needs_bootstrap,
                "current": current,
                "stored": stored,
                "reason": reason,
            }
        )
    )


def main() -> None:
    current = os.environ.get("CLAUDE_SESSION_ID", "")

    if not current:
        # Empty env cannot safely "match" a stored sentinel: if both are
        # empty the script would report "already bootstrapped" and skip
        # memory load forever. Treat empty env as bootstrap-needed.
        print(
            "needs-bootstrap: $CLAUDE_SESSION_ID missing/empty; defaulting to bootstrap-needed",
            file=sys.stderr,
        )
        emit(True, current, None, "claude_session_id_missing")
        sys.exit(0)

    try:
        with open(SENTINEL) as f:
            stored = f.read().strip()
    except FileNotFoundError:
        emit(True, current, None, "sentinel_missing")
        sys.exit(0)
    except OSError as e:
        print(
            f"needs-bootstrap: cannot read sentinel at {SENTINEL}: {e}; assuming bootstrap needed",
            file=sys.stderr,
        )
        emit(True, current, None, "sentinel_unreadable")
        sys.exit(0)

    if not stored:
        # Defensive pair: an empty sentinel (shouldn't be written by
        # the fixed register-session.py, but could linger from pre-fix
        # runs or a manual touch) is treated as bootstrap-needed so we
        # self-heal rather than stay stuck.
        emit(True, current, "", "sentinel_empty")
        sys.exit(0)

    if stored == current:
        emit(False, current, stored, "sentinel_match")
        sys.exit(1)

    emit(True, current, stored, "sentinel_mismatch")
    sys.exit(0)


if __name__ == "__main__":
    main()

README.md

tile.json