Session-file ingester: parse recorded sessionFilePath jsonl into per-request spend and per-tool-call events; retention policy for .sandcastle/logs #98

Closed
opened 2026-08-26 03:02:33 +00:00 by matou-build-agent · 3 comments

From the 2026-08-25 observability review — the requirements are "each single action taken and how long it took" and "tokens per agent action".

Symptom. The true per-action record — every tool call, every API request with its usage — already exists in the claude session jsonl files whose paths record-run-result.sh stores as iteration events, but nothing ever parses them: per-action granularity is archaeology. Separately, nothing prunes .sandcastle/logs/ (unbounded growth) and nothing guarantees the files survive long enough to be harvested.

Direction. A post-run ingest step (host-side, best-effort like every swarm.db writer): walk the run's session files, emit per-request spend rows (cache classes split — pairs with the spend-fidelity ticket) and per-tool-call events (tool name, duration from message timestamps). Then a retention rule: keep raw jsonl N days after ingest, prune on the sweep path. Ingest-then-prune turns the logs from an unbounded liability into a bounded pipeline.

Depends on the spend-fidelity ticket's schema additions.

From the 2026-08-25 observability review — the requirements are "each single action taken and how long it took" and "tokens per agent action". **Symptom.** The true per-action record — every tool call, every API request with its usage — already exists in the claude session jsonl files whose paths `record-run-result.sh` stores as `iteration` events, but nothing ever parses them: per-action granularity is archaeology. Separately, nothing prunes `.sandcastle/logs/` (unbounded growth) and nothing guarantees the files survive long enough to be harvested. **Direction.** A post-run ingest step (host-side, best-effort like every swarm.db writer): walk the run's session files, emit per-request `spend` rows (cache classes split — pairs with the spend-fidelity ticket) and per-tool-call `events` (tool name, duration from message timestamps). Then a retention rule: keep raw jsonl N days after ingest, prune on the sweep path. Ingest-then-prune turns the logs from an unbounded liability into a bounded pipeline. Depends on the spend-fidelity ticket's schema additions.
Author
Member

This was generated by AI during triage.

Ruled by agent under ADR 0174 — veto anytime.
Ruling: ready-for-agent, with a formal tracker dependency on #96 (this ticket consumes the cache_creation_tokens/cache_read_tokens/model columns #96 adds). Two-way door — a best-effort, additive ingest step plus a bounded retention window; revertible (the window is a config value changeable in a later commit), and provable by tests on the ingest and prune logic. The retention/prune half touches log deletion, which could look like the one-way-door "data destruction" item at a glance — but the ticket's own design makes it safe: ingest happens BEFORE prune, so nothing is destroyed before its durable record (spend/events rows) exists, and only bounded, already-durably-captured debug/session logs are pruned, never tracker or product data. That keeps it a two-way door: the retention window is just a number, adjustable by a later commit.

Agent Brief

Category: enhancement
Summary: Parse recorded session jsonl files into per-request spend and per-tool-call events, and add a retention rule for .sandcastle/logs.

Current behavior:
The true per-action record — every tool call, every API request with its usage — already exists in the claude session jsonl files whose paths record-run-result.sh stores as iteration events, but nothing ever parses them: per-action granularity is unavailable today. Separately, nothing prunes .sandcastle/logs/, so it grows unbounded, and nothing guarantees the files survive long enough to be harvested.

Desired behavior:
A post-run, best-effort ingest step walks a run's session jsonl files and emits per-request spend rows (cache classes split, using the columns #96 adds) and per-tool-call events (tool name, duration derived from message timestamps). A retention rule then keeps raw jsonl for N days after ingest and prunes it on the existing sweep path — ingest-then-prune turns the logs from an unbounded liability into a bounded pipeline.

Key interfaces:

  • The spend table's new cache/model columns from #96 (this ticket depends on that schema landing).
  • The session jsonl format (message timestamps, tool-use blocks).
  • The existing sweep/prune path.
  • The iteration events record-run-result.sh already writes (source of each run's session file paths).

Acceptance criteria:

  • A post-run step parses a run's session jsonl file(s) into per-request spend rows with cache classes split
  • Per-tool-call events are emitted with tool name and duration
  • Raw jsonl files are retained at least N days after ingest, then pruned by the sweep path
  • Ingest is best-effort: a parse failure never fails the run
  • Tests cover ingest and the retention/prune rule

Out of scope:

  • Changing the spend schema itself (that's #96)
  • Real-time, as-it-happens ingestion — this is strictly post-run
> *This was generated by AI during triage.* **Ruled by agent under ADR 0174 — veto anytime.** Ruling: `ready-for-agent`, with a formal tracker dependency on #96 (this ticket consumes the `cache_creation_tokens`/`cache_read_tokens`/`model` columns #96 adds). Two-way door — a best-effort, additive ingest step plus a bounded retention window; revertible (the window is a config value changeable in a later commit), and provable by tests on the ingest and prune logic. The retention/prune half touches log deletion, which could look like the one-way-door "data destruction" item at a glance — but the ticket's own design makes it safe: ingest happens BEFORE prune, so nothing is destroyed before its durable record (spend/events rows) exists, and only bounded, already-durably-captured debug/session logs are pruned, never tracker or product data. That keeps it a two-way door: the retention window is just a number, adjustable by a later commit. ## Agent Brief **Category:** enhancement **Summary:** Parse recorded session jsonl files into per-request spend and per-tool-call events, and add a retention rule for `.sandcastle/logs`. **Current behavior:** The true per-action record — every tool call, every API request with its usage — already exists in the claude session jsonl files whose paths `record-run-result.sh` stores as iteration events, but nothing ever parses them: per-action granularity is unavailable today. Separately, nothing prunes `.sandcastle/logs/`, so it grows unbounded, and nothing guarantees the files survive long enough to be harvested. **Desired behavior:** A post-run, best-effort ingest step walks a run's session jsonl files and emits per-request `spend` rows (cache classes split, using the columns #96 adds) and per-tool-call `events` (tool name, duration derived from message timestamps). A retention rule then keeps raw jsonl for N days after ingest and prunes it on the existing sweep path — ingest-then-prune turns the logs from an unbounded liability into a bounded pipeline. **Key interfaces:** - The `spend` table's new cache/model columns from #96 (this ticket depends on that schema landing). - The session jsonl format (message timestamps, tool-use blocks). - The existing sweep/prune path. - The iteration events `record-run-result.sh` already writes (source of each run's session file paths). **Acceptance criteria:** - [ ] A post-run step parses a run's session jsonl file(s) into per-request `spend` rows with cache classes split - [ ] Per-tool-call `events` are emitted with tool name and duration - [ ] Raw jsonl files are retained at least N days after ingest, then pruned by the sweep path - [ ] Ingest is best-effort: a parse failure never fails the run - [ ] Tests cover ingest and the retention/prune rule **Out of scope:** - Changing the `spend` schema itself (that's #96) - Real-time, as-it-happens ingestion — this is strictly post-run
Author
Member

swarm-claim host=matou-dev-server-01 run=1160
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=1160 (automated multi-host claim — lowest live claim id works this ticket)
Author
Member

close-report for #98 — status success

✅ close-report gates passed — every claim verified against main; issue closed.

envelope
{
  "issue": 98,
  "status": "success",
  "commits": [
    "73c1fa4"
  ],
  "changed_files": [
    "STATUS.md",
    "record-run-result.sh",
    "report-lib.sh",
    "swarm-db-lib.sh",
    "swarm-db.py",
    "sweep-lib.sh",
    "tests/record-run-result-test.sh",
    "tests/swarm-db-test.sh",
    "tests/sweep-lib-test.sh"
  ],
  "tests": [
    {
      "command": "bash tests/swarm-db-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/record-run-result-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/sweep-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "for t in tests/*.sh onboarding/tests/*.sh; do case \"$t\" in *fakebin*|*fixtures*) continue;; esac; bash \"$t\" || echo FAIL: $t; done",
      "exit_code": 0
    }
  ],
  "criteria": {
    "A post-run step parses a run's session jsonl file(s) into per-request spend rows with cache classes split": "swarm-db.py ingest_session/cmd_ingest emits one spend row per assistant message.usage block (requests=1, input_tokens fresh, cache_creation_tokens/cache_read_tokens split, model + account); record-run-result.sh runs it host-side per iteration on the recorded sessionFilePath. Proven by tests/swarm-db-test.sh and tests/record-run-result-test.sh (RUN98 yields 2 per-request rows replacing the aggregate).",
    "Per-tool-call events are emitted with tool name and duration": "ingest_session emits kind='tool-call' events, tool name in detail and duration_ms=<n> in evidence computed from the assistant->tool_result timestamp gap (unmatched call records name, no duration). Proven by swarm-db-test.sh (Bash=2000ms, Read=1500ms, truncated=no duration) and record-run-result-test.sh (RUN98 Bash duration_ms=3000).",
    "Raw jsonl files are retained at least N days after ingest, then pruned by the sweep path": "sweep-lib.sh prune_session_logs removes *.jsonl under .sandcastle/logs older than SESSION_LOG_RETENTION_DAYS (default 14) and is wired into report-lib.sh report_sweep; ingest is synchronous post-run so an aged file was already harvested. Proven by tests/sweep-lib-test.sh (30-day jsonl pruned, fresh kept, non-jsonl and missing-dir spared).",
    "Ingest is best-effort: a parse failure never fails the run": "cmd_ingest swallows every exception and always exits 0; swarmdb_ingest echoes '0 0' on missing engine/failure; record-run-result.sh keeps its always-exit-0 posture and falls back to the aggregate spend row. Proven by swarm-db-test.sh (missing/unparseable session => '0 0', python-absent no-op).",
    "Tests cover ingest and the retention/prune rule": "tests/swarm-db-test.sh (ingest command + swarmdb_ingest wrapper), tests/record-run-result-test.sh (real session file, aggregate-replacement, no double count), tests/sweep-lib-test.sh (prune_session_logs window/spare/no-op, run before the in-sandbox git-fence skip). Full offline suite green."
  },
  "summary": "Added a best-effort session-jsonl ingester (per-request spend rows + per-tool-call events) and a bounded retention rule that prunes ingested raw jsonl on the sweep path.",
  "blockers": []
}
**close-report** for #98 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 98, "status": "success", "commits": [ "73c1fa4" ], "changed_files": [ "STATUS.md", "record-run-result.sh", "report-lib.sh", "swarm-db-lib.sh", "swarm-db.py", "sweep-lib.sh", "tests/record-run-result-test.sh", "tests/swarm-db-test.sh", "tests/sweep-lib-test.sh" ], "tests": [ { "command": "bash tests/swarm-db-test.sh", "exit_code": 0 }, { "command": "bash tests/record-run-result-test.sh", "exit_code": 0 }, { "command": "bash tests/sweep-lib-test.sh", "exit_code": 0 }, { "command": "for t in tests/*.sh onboarding/tests/*.sh; do case \"$t\" in *fakebin*|*fixtures*) continue;; esac; bash \"$t\" || echo FAIL: $t; done", "exit_code": 0 } ], "criteria": { "A post-run step parses a run's session jsonl file(s) into per-request spend rows with cache classes split": "swarm-db.py ingest_session/cmd_ingest emits one spend row per assistant message.usage block (requests=1, input_tokens fresh, cache_creation_tokens/cache_read_tokens split, model + account); record-run-result.sh runs it host-side per iteration on the recorded sessionFilePath. Proven by tests/swarm-db-test.sh and tests/record-run-result-test.sh (RUN98 yields 2 per-request rows replacing the aggregate).", "Per-tool-call events are emitted with tool name and duration": "ingest_session emits kind='tool-call' events, tool name in detail and duration_ms=<n> in evidence computed from the assistant->tool_result timestamp gap (unmatched call records name, no duration). Proven by swarm-db-test.sh (Bash=2000ms, Read=1500ms, truncated=no duration) and record-run-result-test.sh (RUN98 Bash duration_ms=3000).", "Raw jsonl files are retained at least N days after ingest, then pruned by the sweep path": "sweep-lib.sh prune_session_logs removes *.jsonl under .sandcastle/logs older than SESSION_LOG_RETENTION_DAYS (default 14) and is wired into report-lib.sh report_sweep; ingest is synchronous post-run so an aged file was already harvested. Proven by tests/sweep-lib-test.sh (30-day jsonl pruned, fresh kept, non-jsonl and missing-dir spared).", "Ingest is best-effort: a parse failure never fails the run": "cmd_ingest swallows every exception and always exits 0; swarmdb_ingest echoes '0 0' on missing engine/failure; record-run-result.sh keeps its always-exit-0 posture and falls back to the aggregate spend row. Proven by swarm-db-test.sh (missing/unparseable session => '0 0', python-absent no-op).", "Tests cover ingest and the retention/prune rule": "tests/swarm-db-test.sh (ingest command + swarmdb_ingest wrapper), tests/record-run-result-test.sh (real session file, aggregate-replacement, no double count), tests/sweep-lib-test.sh (prune_session_logs window/spare/no-op, run before the in-sandbox git-fence skip). Full offline suite green." }, "summary": "Added a best-effort session-jsonl ingester (per-request spend rows + per-tool-call events) and a bounded retention rule that prunes ingested raw jsonl on the sweep path.", "blockers": [] } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Matou/dev-factory#98
No description provided.