Skip to content

perf(agent,event-log): fix Scenario M O(1) scaling, cell/catalog leaks, history indexes, and RequestComposition dedup - #7115

Merged
M4n5ter merged 7 commits into
rust-previewfrom
stress/forever-running-o1-soak
Nov 21, 2026
Merged

M4n5ter merged 7 commits into
rust-previewfrom
stress/forever-running-o1-soak

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds opt-in Scenario M (forever-running O(1) scaling & steady-state soak test) and fixes the real root-cause leaks, O(N) scheduler query plan, and SQLite storage/payload amplification bottlenecks exposed by its baseline run — with zero backwards-compatibility branches (pre-release direct schema updates + standalone Python migration script below).

Root-Cause Fixes

  1. Session-removal reclamation (agent_cells, agent_catalog_sessions, agent_catalog_controls) & dead table removal (802fee0a1):
    • Added DELETE FROM agent_cells WHERE run=?1 to RUN_HISTORY_RELEASES (covers both full and profile-computer tails while preserving standalone run IS NULL cells and keeping agent_cell_notifications content nomination intact).
    • Added DELETE FROM agent_catalog_sessions and DELETE FROM agent_catalog_controls to SESSION_RELEASES (retaining agent_catalog_events for control-command idempotency).
    • Removed unused agent_cell_operation_pins table and redundant projection writes (CellView::apply(ChildBound) already enforces immutable RecoveryRef in canonical fold).
  2. O(1) model_reconciliation_runs query plan (802fee0a1):
    • Changed outer join to FROM candidates p CROSS JOIN agent_runs r ON r.run=p.run so SQLite probes agent_runs by primary key from the small candidates CTE instead of scanning historical completed runs via agent_runs_due(owner=?) (129×–187× fewer SQLite VM steps at 3,000 runs).
  3. Bounded runtime WAL size (802fee0a1):
    • Set PRAGMA journal_size_limit = 16777216; (16 MiB) on runtime writer initialization (leaving read-only inspection's EXCLUSIVE / NO_CKPT_ON_CLOSE / query_only sequence untouched).
  4. ROWID + partial sparse indexes on agent_session_history & 3-column ROWID agent_history_content_refs (5c57476c9):
    • Converted agent_session_history from WITHOUT ROWID to sequence INTEGER PRIMARY KEY, removed redundant trailing sequence key columns from secondary indexes, and made agent_session_history_tool (WHERE tool_use_id IS NOT NULL), agent_session_history_result (WHERE result_source=1), and agent_session_history_message (WHERE opening_message=1) partial indexes.
    • Converted agent_history_content_refs to a 3-column ROWID table (run, content_id, first_sequence, UNIQUE(run,content_id)), dropped redundant agent_history_content_refs_run, and resolved (owner, session) via JOIN agent_run_specs in agent_history_content.
  5. Out-of-line content-addressed CompositionSurface in RequestSpec (2d45d25b0):
    • Moved static system_prompt, tools, and sources out of RequestComposition into CompositionSurface referenced by composition.surface: ContentRef and wired into RequestSpec::content_refs(), so identical tool catalogs/system prompts/source revisions deduplicate across turns and sessions in contents (reducing RequestSpec raw JSON by 9.5× and eliminating SQLite overflow pages).
  6. Scenario M telemetry & strict zero-false-green assertions (0a0d5f57b, e4202a4a8, d300b0939):
    • Added real_host_forever_running_o1_soak (#[ignore]) covering 4 phases: 14-wave zero-survivor churn (476 deleted sessions), 12× dormant fleet scale-out (250→1,000→3,000 sessions), 300-turn immortal session with rolling compactions + 200-post room, and clean + SIGKILL cold reopen.
    • Kept application_operations intact (no blind N=256 pruning that would break replay idempotency / receipt recovery).
    • Phase 1 verifies SQLite count query exit status + every expected key (no silent missing-key-as-0 coercion) and asserts 0 residual rows across all reclaimed domain tables (agent_facts, agent_session_history, agent_history_content_refs, agent_request_specs, agent_cells, agent_catalog_sessions, agent_catalog_controls, orphan_contents) and flat non_receipt_contents.
    • Phase 3 asserts >= 20 durable committed context_compacted facts on immortal_sid directly in agent_session_history.
    • Phase 4 anchors SIGKILL on a deterministic in-flight sentinel run (SOAK_P4_CRASH_SENTINEL, confirmed live in both mock and agent/observe) alongside 12/12 concurrent hot sends, and verifies recovery of that exact crashed run ID after reopen.

Scenario M --release A/B Benchmark (3,525 sessions, ~4,913 model calls)

Metric Before (0a0d5f57b) After (d300b0939) Change
Violations 3 0 PASS
Total wall time 325.7s 315.5s -3%
Phase 1 agent_cells residual (0 survivors) leaked (0.81 MiB) 0 rows -100%
Phase 1 agent_catalog_sessions / controls residual leaked (956 rows) 0 rows -100%
Phase 1 orphan_contents / non_receipt_contents leaked 0 orphan; 48→48 (1.00×) Flat
Phase 1 steady-state db + rwal (W14) ~150+ MiB (wal=102.7 MiB) 61.94 MiB (rwal=17.28 MiB) -60%
Runtime WAL steady-state (T1 / T2 / T3) 102.3 MiB 16.0 MiB -84.4% (6.4×)
Final DB + WAL (dir) 635.3 MiB (+102 MiB WAL) 470.8 MiB (464.2 db + 6.7 wal) -171 MiB DB (-27%)
contents B-tree (T3_3000 / final) 202.3 / 241.7 MiB 139.1 / 192.9 MiB -63 / -49 MiB
agent_request_specs raw JSON (T3_3000 / final) 54.16 MiB (at T3) 5.72 / 7.12 MiB -89% (9.5×)
agent_session_history + indexes (T3_3000) 101.70 MiB ~51.5 MiB -49%
agent_history_content_refs + indexes (T3_3000) 59.06 MiB ~6 MiB (out of top 12) -90%
Phase 3 durable context_compacted / +db / 50t ~21.5 MiB 32 committed / 16.4→16.5 MiB -24%, flat
Phase 3 Immortal RSS / turn_p50 (300 turns) 437.9→437.7 MiB / 329→336ms 420.2→417.6 MiB / 321→330ms Flat
Phase 2 RSS growth (+2,750 dormant sessions) +29.4 MiB (~11 KiB/sess) +21.6 MiB (~7.9 KiB/sess) -27%
Phase 2 Idle CPU (250 / 1k / 3k) 0.00% 0.00% Flat
Phase 4 Clean / SIGKILL Reopen 544ms / 579ms 551ms / 600ms (sentinel recovered, integrity=Ok(2), orphans=0) <1s

Local Database Migration (Existing Pre-PR runtime-rust.sqlite)

Because Maka is pre-release and this PR modifies migrations 6, 8, 33, 49 and RequestSpec.composition directly without runtime compatibility branches, migrate any existing local runtime-rust.sqlite in place with this standalone, idempotent Python script (creates a timestamped backup automatically, realigns agent_recall_heads.digest and canonical agent_facts / RequestSpec / CompositionSurface run content pins, and verifies all business invariants + PRAGMA foreign_key_check + PRAGMA integrity_check before committing):

python3 migrate_runtime_sqlite_pr7115.py /path/to/runtime-rust.sqlite --vacuum

Verification

  • cargo test -p maka-event-log -p maka-agent-facts -p maka-agent-state -p maka-agent-engine — 100% pass (incl. new query-plan regression test for model_reconciliation_runs and cell/catalog reclamation tests).
  • cargo test -p maka-agent-native --lib -- history snapshot recovery removal compaction summary — 106/106 pass.
  • cargo clippy --no-deps -p maka-cli --test mainline_reopen -- -D warnings — 0 warnings.
  • cargo test --release -p maka-cli --test mainline_reopen real_host_forever_running_o1_soak -- --ignored --nocapture --test-threads=1 — 0 violations, 315.47s, durable_compactions=32, P4 CRASH SENTINEL run=... state=idle pre_kill_mock_hits=1 hot_accepted=12/12, integrity=Ok(2), orphans=0.
  • Standalone Python migration script verified end-to-end against a full 49-migration pre-PR (0a0d5f57b) SQLite DB across two consecutive runs (migration + idempotent no-op), verifying agent_recall_heads.digest alignment, canonical fact/request/surface agent_content_pins, old blob reclamation, checksums, triggers, FK integrity, and PRAGMA integrity_check = ok.

… soak

Adds an opt-in (#[ignore]) release soak harness in
products/maka/cli/tests/mainline_reopen/host_forever_soak.rs
(real_host_forever_running_o1_soak) to verify that CPU, RSS, FD, latency,
and storage do not grow linearly with historical session load:

- Phase 1 (Zero-Survivor Pure Churn): 14 waves x 28 heavy sessions (V8 cells,
  subagents, tools, streams), 100% deleted every wave, sampling RSS, FDs,
  CPU/session, per-table counts, and SQLite dbstat bytes.
- Phase 2 (Scale-Out Dormant Fleet): 250 -> 1,000 -> 3,000 historical sessions
  (12x jump across idle, archived, removed tombstones, rooms, memories) while
  12 slow/churning watch clients hammer the Host, running an identical fixed
  hot probe (16 active sessions, list, sessionList, observe, recall, usage,
  usageSummary, remove) and a 3s quiet idle CPU probe at each tier.
- Phase 3 (Immortal Session Scale-Deep): 300 turns with rolling compaction on
  a tight 14k window + 200-post immortal Room.
- Phase 4 (Cold Reopen & SIGKILL Recovery): clean reopen and mid-flight
  SIGKILL reopen under the full ~3,500+ historical session fleet.
@M4n5ter M4n5ter changed the title test(cli): add Scenario M forever-running O(1) scaling soak and fix steady-state leaks perf(agent,event-log): fix Scenario M O(1) scaling, cell/catalog leaks, history indexes, and RequestComposition dedup Nov 21, 2026
@M4n5ter
M4n5ter merged commit bcd808b into rust-preview Nov 21, 2026
6 of 8 checks passed
@M4n5ter
M4n5ter deleted the stress/forever-running-o1-soak branch November 21, 2026 08:58
YongGoose added a commit to YongGoose/maka that referenced this pull request Nov 22, 2026
The event log used SQLite's default auto-checkpoint of 1,000 pages. Every
commit rewrites the same hot pages, so each checkpoint cycle copied them
back into the database again. Checkpointing every 4,000 pages copies them
back a quarter as often, and 4,000 pages of 4 KiB still fit within the
16 MiB journal_size_limit set in apache#7115.

With 16 concurrent sessions running scripted repair work, storage writes
fell from about 27.0 MiB to 24.0 MiB per tool call. Wall time did not
regress.

Generated-by: Claude Code
YongGoose added a commit to YongGoose/maka that referenced this pull request Nov 23, 2026
The event log used SQLite's default auto-checkpoint of 1,000 pages. Every
commit rewrites the same hot pages, so each checkpoint cycle copied them
back into the database again. Checkpointing every 4,000 pages copies them
back a quarter as often, and 4,000 pages of 4 KiB still fit within the
16 MiB journal_size_limit set in apache#7115.

With 16 concurrent sessions running scripted repair work, storage writes
fell from about 27.0 MiB to 24.0 MiB per tool call. Wall time did not
regress.

Generated-by: Claude Code
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