Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pulse-memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ skills/pulse-memory/ # filter SoT
SKILL.md
references/admissions.md
references/store.md # SQLite archive vs memory
scripts/ # admit, retrieve, backfill
.grok/workflows/pulse-memory.rhai
test/
```

Plugins do not auto-register Rhai. Copy/symlink the workflow.

## CLI

```bash
PULSE_MEMORY_DB=./pulse.sqlite scripts/pulse_admit.py apply/example/constraints "Keep the cap." test tool-verified fact locked
PULSE_MEMORY_DB=./pulse.sqlite scripts/pulse_retrieve.py --key apply/example/constraints
PULSE_MEMORY_DB=./pulse.sqlite scripts/pulse_backfill.py
```

Retrieve returns JSON: one pulse (or a few, max 8), or `{ "ok": false, "status": "unknown" }`. It never prints the whole store.

## Tests

```bash
Expand Down
99 changes: 99 additions & 0 deletions pulse-memory/scripts/pulse_admit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3
"""Admit one memory trace, with a content hash and a prefetch key.

pulse_admit.py NAT_KEY SNIPPET [source] [status] [kind] [lock]
pulse_admit.py --supersede OLD_KEY NAT_KEY SNIPPET [source] [status] [kind] [lock]
echo '{"nat_key":"...","snippet":"..."}' | pulse_admit.py --json
"""

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from pulse_common import (
AdmitRejected,
admit,
connect,
reject_and_maybe_archive,
)


def run_admit(obj, source_explicit=True, supersede=None):
conn = connect()
try:
return admit(
conn,
obj["nat_key"],
obj["snippet"],
obj.get("source", "steward"),
obj.get("status", "user-stated"),
obj.get("kind", "memory"),
obj.get("lock", "open"),
obj.get("time"),
supersede=supersede or obj.get("supersede") or obj.get("supersedes"),
source_explicit=source_explicit,
topic=obj.get("topic"),
)
except AdmitRejected as exc:
reject_and_maybe_archive(
conn,
exc.reason,
obj.get("source", "unknown"),
obj.get("nat_key"),
obj.get("snippet"),
exc.archive,
)
print(f"rejected:{exc.reason}")
sys.exit(3)
finally:
conn.close()


def from_json(raw, supersede=None):
if not raw.strip():
raise SystemExit("empty stdin for --json")
obj = json.loads(raw)
source_explicit = "source" in obj
print(run_admit(obj, source_explicit=source_explicit, supersede=supersede))


def main():
args = sys.argv[1:]
supersede = None
if args and args[0] == "--supersede":
if len(args) < 2:
print("usage: pulse_admit.py --supersede OLD_KEY ...", file=sys.stderr)
sys.exit(2)
supersede = args[1]
args = args[2:]
if args and args[0] in ("--json", "-j"):
from_json(sys.stdin.read(), supersede=supersede)
return
if not args and not sys.stdin.isatty():
raw = sys.stdin.read()
if raw.strip().startswith("{"):
from_json(raw, supersede=supersede)
return
if len(args) < 2:
print(
"usage: pulse_admit.py NAT_KEY SNIPPET [source] [status] [kind] [lock]\n"
" pulse_admit.py --supersede OLD_KEY NAT_KEY SNIPPET [...]\n"
" pulse_admit.py --json < trace.json",
file=sys.stderr,
)
sys.exit(2)
obj = {
"nat_key": args[0],
"snippet": args[1],
"source": args[2] if len(args) > 2 else "steward",
"status": args[3] if len(args) > 3 else "user-stated",
"kind": args[4] if len(args) > 4 else "memory",
"lock": args[5] if len(args) > 5 else "open",
}
source_explicit = len(args) > 2
print(run_admit(obj, source_explicit=source_explicit, supersede=supersede))


if __name__ == "__main__":
main()
27 changes: 27 additions & 0 deletions pulse-memory/scripts/pulse_backfill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""Fill empty verify_hash and prefetch_key values. Idempotent. No deletes."""

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from pulse_common import backfill, connect


def main():
if "-h" in sys.argv or "--help" in sys.argv:
print("usage: pulse_backfill.py")
return
conn = connect()
try:
result = backfill(conn)
print(json.dumps(result, ensure_ascii=False))
if result["before"] != result["after"]:
sys.exit(1)
finally:
conn.close()


if __name__ == "__main__":
main()
Loading
Loading