Skip to content

feat: intern region entry content at the runtime boundary - #3

Open
a-programmers-programmer wants to merge 7 commits into
mainfrom
feature/intern-region-content-v3
Open

feat: intern region entry content at the runtime boundary#3
a-programmers-programmer wants to merge 7 commits into
mainfrom
feature/intern-region-content-v3

Conversation

@a-programmers-programmer

@a-programmers-programmer a-programmers-programmer commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Summary

Fleets of agents with identical pinned / system material currently pay N heap copies of the same text because RegionEntry.content is an owned String. This PR makes entry text an InternedString (Arc<str>) and interns only at the runtime boundary, so identical strings share one allocation within a World.

Commit: de5d2eb on top of current GEMISIS/leviath main.

Architecture

PipelineWorld::new
  └─ ContentInternerRes                 ← Bevy Resource (World-scoped)
         │
         │ handle() clone at spawn
         ▼
ContextWindow { interner: private }
  └─ RegionEntry::interned → Region::insert_entry
         │
         ▼
Region (domain)                         ← never takes &ContentInterner
  └─ add_entry / upsert → unique Arc
  └─ insert_entry(entry) ← runtime edge only

Separation of concerns

Layer Responsibility
ContentInterner Shareable table handle — no process-global state
ContentInternerRes Bevy Resource; source of truth per World
ContextWindow Holds cloned handle; only place that interns on write
Region Pure domain — budgets, schemas, eviction. No interner params
RegionEntry Private InternedString; public API is plain &str

Design details

  • Domain stays pure: Region::add_entry / upsert_by_key take impl AsRef<str> and allocate uniquely. Domain tests never mention an interner.
  • Runtime edge: ContextWindow write paths call RegionEntry::interned then Region::insert_entry. Convenience writers delegate to insert_entry / insert_tainted_entry (DRY).
  • Restore: snapshots still store plain JSON strings; restore re-interns through the window handle.
  • Isolation: distinct interners never share; Arc payload is immutable.

Out of scope

  • Online cold-spill of agent state
  • Selective interning by region kind
  • Smarter intern table (ahash / sharding)

Test plan

  • cargo test -p leviath-core --lib (656 tests)
  • cargo test -p leviath-runtime --lib (769 tests)
  • cargo test -p leviath-runtime --test context_management (18 tests)
  • cargo clippy -p leviath-core -p leviath-runtime --all-targets -- -D warnings
  • cargo fmt --all -- --check
  • Unit: same interner + same text → shares_content_with; distinct interners do not share; domain add_entry needs no interner

Files (18, clean vs GEMISIS main)

Path Change
crates/leviath-core/src/intern.rs ContentInterner + InternedString
crates/leviath-core/src/region.rs Encapsulated entry; pure domain API; insert_entry
crates/leviath-runtime/src/content_interner.rs ContentInternerRes Resource
crates/leviath-runtime/src/components.rs Window holds handle; write paths intern
crates/leviath-runtime/src/{world,pipeline/transition,restore}.rs Resource insert; spawn clone; restore re-intern
other runtime call sites content() / content_owned() accessors

Note: this fork PR may show extra files if the fork's main is stale. Against GEMISIS/leviath main the diff is the 18 files above only.

Upstream PR not opened yet — waiting for go.

GEMISIS and others added 2 commits July 31, 2026 15:40
The `lev run` error hint suggested `lev run agents/coder "task"`, wrong
twice: the agents/ path form no longer resolves from a checkout, and the
task is a -t flag, not positional. It now suggests `lev run coder -t
"task"`.

The launchd service label was ai.sunforge.leviath, the last user-visible
string from the organization the project moved off of. It is now
dev.leviath.daemon, and because a label rename would otherwise strand an
already-supervised daemon under the old name, install and uninstall also
boot out and delete any registration listed in LEGACY_SERVICE_LABELS.
The cleanup actions are pure data in the tested daemon_service core;
running them stays in the binary with the rest of the subprocess I/O.
Linux is unaffected (its unit was always leviath.service).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Km9mfcU1ezK2HgB9SJa2R2
First version to ride the automated pipeline end to end: alpha builds
it, beta and prod promote it, and prod's publish-crates job pushes all
twelve crates to crates.io from the promoted commit. Carries the launch
cleanup (service label migration, corrected run hint, refreshed crate
pages) to the registry.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Km9mfcU1ezK2HgB9SJa2R2
jek584 and others added 4 commits July 31, 2026 20:10
C-suite agents (CTO, TL, bottleneck-investigator) need to read files
beyond their workdir — design docs, run archives, other agent blueprints.
Without this, they fall back to shell (defeating the sandbox), or require
workdir = filesystem root.

Adds:
- ReadPathsConfig ([read_paths].allow) in agent.leviath blueprints
- ReadPathEntry enum: Exact, Glob (glob: prefix), Regex (regex: prefix)
- resolve_within fallback to read_paths when workdir check fails
- Only applies to read_file/read_files; writes stay sandboxed
- Symlink validation against matched entry roots

Fixes: # (to be filed)
Reworks the [read_paths] allowlist so it fits the repo's tighten-only
invariant and cannot be used to widen an agent's reach past what the user
granted.

- Reads and writes no longer share one resolver. read_file/read_files/
  list_dir get a new resolve_read() with the allowlist fallback; write_file
  and edit_file stay confined to the workdir.
- glob/regex entries match the symlink-resolved canonical path (the previous
  resolves_within(p, p) check was a self-comparison, always true). Regexes are
  auto-anchored; Windows matching is case-insensitive with the \\?\ prefix
  stripped. Entry matching moves to a new leviath-core::read_paths module.
- A blueprint's declarations are inert unless the user's config grants them:
  [security] read_paths, [agent_read_paths.<name>], or the
  allow_blueprint_read_paths override. Ungranted declarations warn at spawn
  with the exact stanza to add.
- [read_paths] is now parsed by manifest.rs (it was never read before, so the
  feature was inert through `lev run`); invalid entries are hard errors.
- ~/ expands via LEVIATH_HOME-aware home_dir(); bare relative entries resolve
  against the workdir; a new `lev run --workdir` sets that workdir.
- Read tools are classified Private when a grant is active. Capabilities are
  surfaced in `lev add` and `lev validate`; docs updated in SECURITY.md and
  the security/agents/cli pages.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
`/..` is not absolute on Windows, so the workdir join reshaped it and the
"cannot be resolved" bail in resolve_outside never fired there, leaving
exec.rs:142 uncovered on windows-latest. Drive it with a leading `..`
against an empty base, which yields a leading ParentDir on every platform.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…base

The prior fix (`../x` against an empty base) still missed exec.rs:142 on
windows-latest: an empty base is not absolute there and the join reshapes it
so pop() never fails. Mirror resolve_rejects_excessive_parent_dir_traversal
instead - a relative base "wd" gives exactly one leading Normal component with
no platform-specific prefix, so the first `..` pops it and the second pops an
empty accumulator, firing the bail on every OS.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@a-programmers-programmer
a-programmers-programmer force-pushed the feature/intern-region-content-v3 branch from 0166474 to 7537583 Compare August 1, 2026 03:26
@a-programmers-programmer a-programmers-programmer changed the title feat: intern region text at the runtime boundary feat: intern region entry content at the runtime boundary Aug 1, 2026
Domain Region APIs stay pure (no ContentInterner parameter). A
World-scoped ContentInternerRes is cloned onto each ContextWindow at
spawn; write and restore paths intern, then insert_entry.

- RegionEntry stores private InternedString; callers see &str only
- add_entry / upsert allocate uniquely; insert_entry is the runtime edge
- No process-global table — distinct interners never share
- Snapshots remain plain strings; restore re-interns through the window
@a-programmers-programmer
a-programmers-programmer force-pushed the feature/intern-region-content-v3 branch from 7537583 to de5d2eb Compare August 1, 2026 03:28
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.

3 participants