Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

perf(dev): a self-contained build and a two-tier test loop on every platform

gb3efb5b0b8939
diff --git a/.cargo/config.toml b/.cargo/config.toml
new file mode 100644
index 00000000..faf06178
--- /dev/null
+++ b/.cargo/config.toml
@@ -0,0 +1,12 @@
+# Cargo aliases for the two test tiers in .config/nextest.toml. Aliases only:
+# an env table here would apply to every cargo invocation on every platform.
+#
+# `cargo t` has no `--workspace`: it builds the workspace's default-members
+# (everything but the evals harness) and lets `cargo t -p <crate>` build just
+# that crate. `cargo tf` is the gate, so it covers the whole workspace. Neither
+# passes `--all-targets`: there are no examples or benches, and it would only
+# add harnesses for the two `test = false` targets. nextest filters still
+# apply, e.g. `cargo t -E 'test(/purge/)'`.
+[alias]
+t = "nextest run"
+tf = "nextest run --workspace -P full"
diff --git a/.config/nextest.toml b/.config/nextest.toml
new file mode 100644
index 00000000..a1a5b2cf
--- /dev/null
+++ b/.config/nextest.toml
@@ -0,0 +1,45 @@
+# cargo-nextest configuration. Two profiles for people, one for CI:
+#
+# cargo t everyday: everything except modules named slow* / stress*
+# cargo tf the gate: everything
+#
+# The skip is by name, so the convention is the whole mechanism: a test that
+# needs more than ~1s alone either gets fixed or moves into a `slow`/`stress`
+# module, and the 5s slow-timeout below surfaces new offenders in every run's
+# summary. Two independent things run the skipped tier anyway: the pre-push
+# hook (`scripts/install-git-hooks.sh`) and CI, which uses `cargo test` and
+# never reads this file.
+
+[profile.default]
+default-filter = "not test(/(^|::)(slow|stress)[a-z0-9_]*::/)"
+# One run shows every failure; fail-fast costs a whole extra loop to learn
+# about the second one.
+fail-fast = false
+failure-output = "immediate-final"
+status-level = "fail"
+final-status-level = "slow"
+# Nothing in the everyday tier should take 5s alone, so a marker means "new
+# slow test" or "wedged". Kill at 2 minutes so a hung test cannot stall a loop.
+slow-timeout = { period = "5s", terminate-after = 24 }
+
+[profile.full]
+# `all()` is load-bearing: profiles inherit `default-filter` from
+# profile.default, so without it the gate would skip the very tier it covers.
+default-filter = "all()"
+fail-fast = false
+failure-output = "immediate-final"
+status-level = "fail"
+# The slow tier legitimately sits in the 10-20s range.
+slow-timeout = { period = "30s", terminate-after = 8 }
+# No retries: no genuinely flaky test has been found in this suite, and a
+# local gate should tell the truth. Retries belong in profile.ci.
+
+[profile.ci]
+default-filter = "all()"
+# Runner contention is real and separate from flakiness.
+retries = 2
+failure-output = "immediate-final"
+fail-fast = false
+
+[profile.ci.junit]
+path = "junit.xml"
diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index ed353807..f944d28c 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -9,8 +9,9 @@
## Test plan
- [ ] `cargo fmt --all -- --check` passes
+- [ ] `git diff --check` passes
- [ ] `cargo clippy --workspace --all-targets -- -D warnings` passes
-- [ ] `cargo test --workspace` passes
+- [ ] `cargo tf` (or `cargo test --workspace --all-targets`) passes
- [ ] Manual test: <!-- describe what you ran and what you observed -->
## Commit attribution
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 14bac896..21b7e67b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -58,7 +58,22 @@ jobs:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: dtolnay/rust-toolchain@4cda84d5c5c54efe2404f9d843567869ab1699d4 # stable
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
+ # On macOS, rustls-tls-native-roots otherwise enumerates Keychain every
+ # time a test process builds a reqwest client. Scope the PEM-bundle
+ # shortcut to macOS CI instead of setting a repo-wide Cargo env var that
+ # breaks other local platforms.
+ - name: Use macOS PEM bundle
+ if: matrix.os == 'macos-latest'
+ run: echo "SSL_CERT_FILE=/etc/ssl/cert.pem" >> "$GITHUB_ENV"
+ # Linux regenerates the web stylesheet from source, so a stale vendored
+ # static/tailwind.css fails here instead of shipping.
+ - name: Regenerate the Tailwind bundle
+ if: matrix.os == 'ubuntu-latest'
+ run: echo "TAILWIND_BUILD=1" >> "$GITHUB_ENV"
- run: cargo test --workspace --all-targets
+ - name: Vendored tailwind.css is current
+ if: matrix.os == 'ubuntu-latest'
+ run: git diff --exit-code -- crates/ai-memory-web/static/tailwind.css
# Native Windows coverage lives in `.github/workflows/windows.yml`: it
# runs on every push to main, nightly, on demand, and on any pull request
@@ -113,9 +128,7 @@ jobs:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: dtolnay/rust-toolchain@4cda84d5c5c54efe2404f9d843567869ab1699d4 # stable
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
- - env:
- TAILWIND_SKIP: "1"
- run: cargo build --release --bin ai-memory
+ - run: cargo build --release --bin ai-memory
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: ci-ai-memory-${{ matrix.artifact }}
@@ -134,8 +147,6 @@ jobs:
toolchain: "1.95"
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
- name: Install with a fresh dependency resolution
- env:
- TAILWIND_SKIP: "1"
run: cargo install --path crates/ai-memory-cli --debug --root target/source-install
- name: Smoke test installed binary
run: target/source-install/bin/ai-memory --version
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 99e1837b..33b13882 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -83,8 +83,6 @@ jobs:
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
- name: Build release binary
- env:
- TAILWIND_SKIP: "1"
run: cargo build --locked --release -p ai-memory-cli
- name: Create release tarball
@@ -140,8 +138,6 @@ jobs:
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
- name: Build release binary
- env:
- TAILWIND_SKIP: "1"
run: cargo build --locked --release -p ai-memory-cli
# Mirrors the Linux tarball, swapping the Linux-only service assets
@@ -214,8 +210,6 @@ jobs:
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
- name: Build release binary
- env:
- TAILWIND_SKIP: "1"
run: cargo build --locked --release -p ai-memory-cli
# Mirrors the Linux tarball, minus the Linux-only service assets
diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml
index d8cd1eef..efb9fbc4 100644
--- a/.github/workflows/windows.yml
+++ b/.github/workflows/windows.yml
@@ -69,5 +69,3 @@ jobs:
- uses: dtolnay/rust-toolchain@4cda84d5c5c54efe2404f9d843567869ab1699d4 # stable
- uses: Swatinem/rust-cache@f0d9c3887740aee45f6153b24b3a6b815192ec16 # v2
- run: cargo test --workspace --all-targets
- env:
- TAILWIND_SKIP: "1"
diff --git a/AGENTS.md b/AGENTS.md
index 79dc78e8..29bfe31a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -196,27 +196,96 @@ below.
## Build and test commands
-Rust 1.95 is required (the pinned toolchain installs automatically via
-rustup). Before claiming any Rust change is ready, run the full local
-gate — the same gates CI (`.github/workflows/ci.yml`) and `bin/release`
-enforce:
+Rust 1.95, pinned in `rust-toolchain.toml`; rustup selects it automatically.
+The build is self-contained (bundled SQLite, vendored libgit2, vendored
+Tailwind CSS), so no command below needs an environment variable.
+
+Two loops, and the split matters: iterate with the everyday tier, run the
+full gate once before handing work off.
```bash
-cargo fmt --all -- --check # formatting
-git diff --check # whitespace
-TAILWIND_SKIP=1 cargo test --workspace # tests
-TAILWIND_SKIP=1 cargo clippy --workspace --all-targets -- -D warnings
-cargo deny check # dependency policy (if installed)
+# Everyday loop (nextest: `cargo install cargo-nextest --locked`).
+cargo t # every shipped crate: 11 test binaries, ~20s warm
+cargo t -p ai-memory-store # one crate: builds only its binary, ~5s
+cargo t -E 'test(/purge/)' # one topic (still builds everything)
+
+# Before claiming a change is ready: the gates CI and bin/release enforce.
+cargo fmt --all -- --check
+git diff --check
+cargo clippy --workspace --all-targets -- -D warnings
+cargo tf # whole workspace, every test, slow tier included
+cargo deny check # dependency policy (if installed)
```
-- `TAILWIND_SKIP=1` skips the Tailwind asset build in `ai-memory-web`'s
- build script; use it for local test/clippy runs. CI's Linux/macOS test
- job runs the full build without it.
+`cargo t` and `cargo tf` are aliases in `.cargo/config.toml` for
+`cargo nextest run` under the `default` and `full` profiles of
+`.config/nextest.toml`. Run them from the repo root. `cargo t` builds the
+workspace's default members, which is every shipped crate; the evals harness
+is two more test binaries that only `cargo tf`, the pre-push hook, and CI
+build (`--workspace`). Without nextest,
+`cargo test --workspace --all-targets` is what CI runs: everything, slower,
+no tiers.
+
+- **Slow tier.** A test whose module path has a segment starting with `slow`
+ or `stress` (`packaging::slow::*`, `stress_autoscope::*`) runs only under
+ `cargo tf`, the pre-push hook, and CI. Budget for everything else: about 1s
+ per test alone; the everyday profile lists anything over 5s in its summary.
+ Fix a slow test before tiering it: an injectable timeout, a smaller fixture,
+ `journal_mode=MEMORY` for a throwaway SQLite, an accept-and-close endpoint
+ instead of a closed port.
+- **Adding an integration test.** Put the file in the crate's `tests/suite/`
+ and declare it with `mod name;` in the entry file there. For every crate
+ but the CLI the entry is `mod.rs`, included from `src/lib.rs` under
+ `#[cfg(test)]`, so the tests compile into the lib's own harness and cost no
+ extra binary; the CLI keeps a separate `main.rs` target because its tests
+ run the built executable. Every test binary is a link and, on macOS and
+ Windows, a first-run malware scan, so each crate gets at most one. A
+ repo-layout test in the CLI suite fails on an undeclared file, a stray
+ top-level `tests/*.rs`, or a `mod.rs` that `lib.rs` never includes.
+- **Shared test helpers** live in `crates/ai-memory-test-support`
+ (dev-dependency only, no workspace dependencies, no test binary of its own).
+- **Pre-push hook.** `scripts/install-git-hooks.sh` (from Git Bash on Windows)
+ installs a hook that runs the full tier before every push and only touches
+ its own marked block in `.git/hooks/pre-push`. Bypass a work-in-progress
+ push with `git push --no-verify`.
+- **Regenerating the web stylesheet.** `TAILWIND_BUILD=1 cargo build -p
+ ai-memory-web` downloads the pinned Tailwind CLI and rewrites
+ `static/tailwind.css`; commit the result. CI regenerates it on Linux and
+ fails if the committed file is stale, so nothing else needs the download.
- Run the companion importer separately:
`cargo test --manifest-path companions/ai-memory-importer/Cargo.toml`
(plus fmt/clippy on the same manifest). Root `--workspace` commands do
not cover it.
-- Useful focused runs: `cargo test -p ai-memory-store`, etc.
+
+### Platform notes
+
+- **All.** `target/` grows without bound: every edit to a shared crate leaves
+ the previous copy of each 100-180 MB test binary behind (seen at 157 GiB).
+ `cargo install cargo-sweep --locked` once, then `cargo sweep --time 7`
+ weekly. Give rust-analyzer its own target dir
+ (`rust-analyzer.cargo.targetDir = true`) so a save-triggered check never
+ holds the lock a `cargo t` is waiting on.
+- **macOS.** `SSL_CERT_FILE=/etc/ssl/cert.pem cargo t` stops reqwest
+ re-reading the Keychain in every test process (workspace test time 75s to
+ 41s); leave it unset if you rely on a private CA in your login Keychain.
+ After a `cargo clean`, `touch target/.metadata_never_index` keeps Spotlight
+ off the build artifacts.
+- **Windows, GNU toolchain.** Two per-machine fixes, each worth about 2x on
+ the loop. mingw's `ld` is ~3x slower than the lld the toolchain ships; in
+ `~/.cargo/config.toml`:
+
+ ```toml
+ [target.x86_64-pc-windows-gnu]
+ rustflags = ["-C", "link-arg=-fuse-ld=lld",
+ "-C", "link-arg=-B<sysroot>/lib/rustlib/x86_64-pc-windows-gnu/bin/gcc-ld"]
+ ```
+
+ with `<sysroot>` from `rustc --print sysroot`, forward slashes. And Defender
+ scans every freshly linked binary on first run (~1.7s each, which nextest
+ pays serially for all 11 before the first test starts); from an elevated
+ PowerShell: `Add-MpPreference -ExclusionPath "$PWD\target", "$HOME\.cargo",
+ "$HOME\.rustup"`.
+
- Shell-level checks: `tests/hooks/test_lib.sh`,
`tests/e2e/handoff_smoke.sh`, `scripts/check-native-packaging.sh`.
- CI additionally runs `cargo build --release --bin ai-memory` on
@@ -363,7 +432,8 @@ Additional boundary rules:
- New disk+SQL mutations need recovery/rollback tests.
- The recall-eval framework lives at
`crates/ai-memory-consolidate/tests/recall_eval.rs`.
-- Tests run with `cargo test --workspace` (use `TAILWIND_SKIP=1` locally).
+- Tests run with `cargo t` locally and `cargo test --workspace --all-targets`
+ in CI.
## Security considerations
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a6f92c69..5ae746e0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Changed
+- The build is self-contained on every platform: the vendored web stylesheet
+ is now the default and `TAILWIND_BUILD=1 cargo build -p ai-memory-web`
+ regenerates it, so no command needs `TAILWIND_SKIP=1` any more. CI
+ regenerates the bundle on Linux and fails if the committed
+ `static/tailwind.css` is stale, a check that did not exist before.
+- Developer loop: `cargo t` (everyday, skips `slow`/`stress` modules) and
+ `cargo tf` (everything) aliases over cargo-nextest, integration tests that
+ compile into each crate's own test harness from `tests/suite/` (78 test
+ binaries down to 11 in the everyday loop; only the CLI keeps a separate one,
+ and the evals harness builds only under `--workspace`), a dev profile that
+ keeps only line tables, and an opt-in pre-push hook that runs the full tier.
+ `bin/release` and the documented gate also run `git diff --check`. Measured:
+ workspace edit-to-result ~380s to ~150s on macOS; the warm everyday test run
+ 28s to 19s on a 32-thread Windows box.
+
+### Fixed
+- Consolidation prompt assembly no longer re-renders the whole observation
+ projection and re-scores every observation after each pruned one. With a few
+ hundred long observations the quadratic loop cost ~14s per prompt; pruning
+ now works from per-observation scores and block sizes computed once, with
+ byte-identical output.
+
## [2.0.3] - 2026-09-04
### Changed
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index a39e6701..8150bded 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -6,7 +6,7 @@
git clone https://github.com/akitaonrails/ai-memory
cd ai-memory
cargo build --workspace
-cargo test --workspace
+cargo test --workspace --all-targets
```
Rust 1.95 is required (pinned in `rust-toolchain.toml`). The build is
@@ -39,24 +39,54 @@ solely to change attribution because doing so invalidates commit hashes and
breaks existing clones and forks. Maintainers use [`.mailmap`](.mailmap) to
canonicalize accidental aliases without changing published commits.
-## Required gates before every PR
+## Required gates before push/merge
-All four must pass — the CI workflow enforces them and so does the `bin/release`
-script:
+All of these must pass; CI enforces them and so does `bin/release`. The build
+is self-contained, so none of them needs an environment variable.
```bash
-cargo fmt --all -- --check # formatting
-cargo clippy --workspace --all-targets -- -D warnings # lints
-cargo test --workspace # tests
+cargo fmt --all -- --check
+git diff --check
+cargo clippy --workspace --all-targets -- -D warnings
+cargo tf # every test (alias: cargo nextest run -P full)
cargo deny check # dependency policy
```
+`cargo tf` needs nextest (`cargo install cargo-nextest --locked`); without it,
+`cargo test --workspace --all-targets` is the equivalent and is what CI runs.
If `cargo-deny` or `cargo-audit` are not installed:
```bash
cargo install cargo-deny cargo-audit
```
+### The everyday loop
+
+```bash
+cargo t # all but the slow tier, ~20s warm
+cargo t -p ai-memory-store # one crate: builds only its test binaries
+cargo t -E 'test(/purge/)' # one topic (builds everything, runs a subset)
+```
+
+The everyday profile skips tests by name: any module segment starting with
+`slow` or `stress` (`packaging::slow::*` drives the real wrapper scripts and
+fake container engines at 10-20s each; `stress_*` modules hammer concurrency).
+The budget for everything else is about 1s per test alone, and the profile
+lists anything over 5s in its summary. Fix a slow test before tiering it.
+Skipped tests still count as "skipped" in the summary, never hidden, and two
+independent things run them anyway: the pre-push hook and CI.
+
+Install the hook once per clone with `scripts/install-git-hooks.sh` (from Git
+Bash on Windows). It appends or updates only ai-memory's managed block in
+`.git/hooks/pre-push`, preserving any existing hook body. Bypass it on a
+work-in-progress branch with `git push --no-verify`.
+
+Integration tests live in `tests/suite/` per crate and compile into the
+crate's own test harness (declare a new file with `mod name;` in
+`tests/suite/mod.rs`); only the CLI keeps a separate test binary, because its
+tests run the built executable. Helpers shared across crates go in
+`crates/ai-memory-test-support`. Platform-specific speedups
+(macOS Keychain, Windows linker and Defender) are in AGENTS.md.
## CHANGELOG is a merge gate
Every **user-facing** change must add a `CHANGELOG.md` entry under
diff --git a/Cargo.lock b/Cargo.lock
index 6a976b71..388a9f24 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -41,6 +41,7 @@ dependencies = [
"ai-memory-llm",
"ai-memory-mcp",
"ai-memory-store",
+ "ai-memory-test-support",
"ai-memory-web",
"ai-memory-wiki",
"ai-memory-workstream",
@@ -85,6 +86,7 @@ dependencies = [
"ai-memory-core",
"ai-memory-llm",
"ai-memory-store",
+ "ai-memory-test-support",
"ai-memory-wiki",
"anyhow",
"async-trait",
@@ -147,6 +149,7 @@ dependencies = [
"ai-memory-core",
"ai-memory-llm",
"ai-memory-store",
+ "ai-memory-test-support",
"ai-memory-wiki",
"anyhow",
"async-trait",
@@ -185,7 +188,7 @@ dependencies = [
"sha2",
"tempfile",
"thiserror 2.0.18",
- "tokenizers 0.21.4",
+ "tokenizers",
"tokio",
"tracing",
"uuid",
@@ -250,6 +253,10 @@ dependencies = [
"uuid",
]
+[[package]]
+name = "ai-memory-test-support"
+version = "2.0.3"
+
[[package]]
name = "ai-memory-web"
version = "2.0.3"
@@ -666,7 +673,7 @@ dependencies = [
"rayon",
"safetensors",
"thiserror 2.0.18",
- "tokenizers 0.22.2",
+ "tokenizers",
"yoke",
"zerocopy",
"zip",
@@ -3996,39 +4003,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
-[[package]]
-name = "tokenizers"
-version = "0.21.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a620b996116a59e184c2fa2dfd8251ea34a36d0a514758c6f966386bd2e03476"
-dependencies = [
- "ahash",
- "aho-corasick",
- "compact_str",
- "dary_heap",
- "derive_builder",
- "esaxx-rs",
- "fancy-regex 0.14.0",
- "getrandom 0.3.4",
- "itertools",
- "log",
- "macro_rules_attribute",
- "monostate",
- "paste",
- "rand 0.9.4",
- "rayon",
- "rayon-cond",
- "regex",
- "regex-syntax",
- "serde",
- "serde_json",
- "spm_precompiled",
- "thiserror 2.0.18",
- "unicode-normalization-alignments",
- "unicode-segmentation",
- "unicode_categories",
-]
-
[[package]]
name = "tokenizers"
version = "0.22.2"
@@ -4041,6 +4015,7 @@ dependencies = [
"dary_heap",
"derive_builder",
"esaxx-rs",
+ "fancy-regex 0.14.0",
"getrandom 0.3.4",
"itertools",
"log",
diff --git a/Cargo.toml b/Cargo.toml
index bb17dcdf..550b3a0b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,10 +11,28 @@ members = [
"crates/ai-memory-web",
"crates/ai-memory-cli",
"crates/ai-memory-workstream",
+ # Test-only helpers shared across crates (dev-dependency, never shipped).
+ "crates/ai-memory-test-support",
# Live A/B harness — not part of the shipped binary, but in
# the workspace so it shares deps + builds with the rest.
"evals",
]
+# What a bare `cargo t` / `cargo build` at the root means: everything that
+# ships, plus test-support. The evals harness is two more test binaries nobody
+# iterates on; `--workspace` (CI, the pre-push hook, `cargo tf`) still covers it.
+default-members = [
+ "crates/ai-memory-core",
+ "crates/ai-memory-store",
+ "crates/ai-memory-wiki",
+ "crates/ai-memory-mcp",
+ "crates/ai-memory-hooks",
+ "crates/ai-memory-llm",
+ "crates/ai-memory-consolidate",
+ "crates/ai-memory-web",
+ "crates/ai-memory-cli",
+ "crates/ai-memory-workstream",
+ "crates/ai-memory-test-support",
+]
[workspace.package]
version = "2.0.3"
@@ -35,6 +53,7 @@ ai-memory-llm = { path = "crates/ai-memory-llm", version = "2.0.3" }
ai-memory-consolidate = { path = "crates/ai-memory-consolidate", version = "2.0.3" }
ai-memory-web = { path = "crates/ai-memory-web", version = "2.0.3" }
ai-memory-workstream = { path = "crates/ai-memory-workstream", version = "2.0.3" }
+ai-memory-test-support = { path = "crates/ai-memory-test-support" }
# (Workspace shared deps follow below)
@@ -151,7 +170,7 @@ reqwest = { version = "0.12", default-features = false, features = ["json", "rus
candle-core = "0.11"
candle-nn = "0.11"
candle-transformers = "0.11"
-tokenizers = { version = "0.21", default-features = false, features = ["fancy-regex"] }
+tokenizers = { version = "0.22", default-features = false, features = ["fancy-regex"] }
futures-util = "0.3"
async-trait = "0.1"
@@ -174,4 +193,19 @@ strip = "symbols"
[profile.dev]
opt-level = 0
-debug = true
+# Full debuginfo put ~190 MB in each of the 71 test binaries and made the
+# build linker-bound. Line tables keep file:line in panics; use
+# `RUSTFLAGS="-C debuginfo=2"` for a real debugger session.
+debug = "line-tables-only"
+
+# Deps are not what you step through, and they dominate the graph.
+# opt-level 1 costs one slow rebuild and buys faster tests: the store crate's
+# suite went 8.6s to 6.3s. Deps recompile rarely, so it amortises.
+[profile.dev.package."*"]
+debug = false
+opt-level = 1
+
+# Proc macros and build scripts are *run* by every crate that depends on them,
+# so optimising them speeds compilation rather than slowing it.
+[profile.dev.build-override]
+opt-level = 3
diff --git a/bin/release b/bin/release
index 2e3791ad..83db50dd 100755
--- a/bin/release
+++ b/bin/release
@@ -56,11 +56,14 @@ cd "$REPO_ROOT"
echo "==> cargo fmt --check"
cargo fmt --all -- --check
+echo "==> git diff --check"
+git diff --check
+
echo "==> cargo clippy"
cargo clippy --workspace --all-targets -- -D warnings
-echo "==> cargo test"
-cargo test --workspace
+echo "==> cargo test --workspace --all-targets"
+cargo test --workspace --all-targets
echo "==> cargo deny check (warn-only)"
if command -v cargo-deny &>/dev/null; then
diff --git a/crates/ai-memory-cli/Cargo.toml b/crates/ai-memory-cli/Cargo.toml
index 2b4ceafc..9ace14fc 100644
--- a/crates/ai-memory-cli/Cargo.toml
+++ b/crates/ai-memory-cli/Cargo.toml
@@ -7,10 +7,20 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "`ai-memory` binary entry point."
+# One integration-test binary instead of one per file. Each binary
+# statically links the whole dep graph and gets scanned by macOS on
+# first run.
+
+[lib]
+name = "ai_memory_cli"
+path = "src/lib.rs"
[[bin]]
name = "ai-memory"
path = "src/main.rs"
+# main.rs is a shim with no tests of its own. Without this, `cargo test` builds
+# a second ~120 MB harness binary for it that contains no tests.
+test = false
[dependencies]
ai-memory-consolidate.workspace = true
@@ -77,6 +87,7 @@ uuid.workspace = true
tempfile.workspace = true
[dev-dependencies]
+ai-memory-test-support.workspace = true
tempfile.workspace = true
rstest.workspace = true
tower.workspace = true
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index 2bc01eb8..72aec5ba 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -948,11 +948,19 @@ mod tests {
assert_eq!(persisted_capture_mode(tmp.path()), CaptureMode::Denylist);
}
+ /// "The server is down": a loopback endpoint that accepts and immediately
+ /// closes every connection. A closed port would do, but Windows takes ~2s
+ /// to report a refused loopback connect, which made every test that posts
+ /// to a dead server cost 2s per request.
+ fn dead_server_url() -> String {
+ ai_memory_test_support::dead_http_endpoint()
+ }
+
fn devin_hook_args(event: &str) -> HookArgs {
HookArgs {
event: event.into(),
agent: "devin".into(),
- server_url: "http://127.0.0.1:1".into(),
+ server_url: dead_server_url(),
auth_token: None,
project_strategy: None,
check_capture: false,
@@ -1196,7 +1204,7 @@ mod tests {
let mut stdout = Vec::new();
run_with_payload(
Some(data_dir.clone()),
- antigravity_hook_args("pre-tool-use", "http://127.0.0.1:1"),
+ antigravity_hook_args("pre-tool-use", &dead_server_url()),
serde_json::json!({
"conversationId": "agy-session",
"workspacePaths": [tmp.path()],
@@ -1220,7 +1228,7 @@ mod tests {
let mut stdout = Vec::new();
run_with_payload(
Some(data_dir.clone()),
- antigravity_hook_args("pre-tool-use", "http://127.0.0.1:1"),
+ antigravity_hook_args("pre-tool-use", &dead_server_url()),
"not-json".into(),
&mut stdout,
|_, _| Ok(()),
@@ -1677,7 +1685,7 @@ mod tests {
let args = HookArgs {
event: "session-end".into(),
agent: "claude-code".into(),
- server_url: "http://127.0.0.1:1".into(),
+ server_url: dead_server_url(),
auth_token: None,
project_strategy: None,
check_capture: false,
@@ -1720,7 +1728,7 @@ mod tests {
let args = HookArgs {
event: event.into(),
agent: "claude-code".into(),
- server_url: "http://127.0.0.1:1".into(),
+ server_url: dead_server_url(),
auth_token: None,
project_strategy: None,
check_capture: false,
@@ -1762,7 +1770,7 @@ mod tests {
let args = HookArgs {
event: "session-end".into(),
agent: "claude-code".into(),
- server_url: "http://127.0.0.1:1".into(),
+ server_url: dead_server_url(),
auth_token: None,
project_strategy: None,
check_capture: false,
@@ -1794,7 +1802,7 @@ mod tests {
let args = HookArgs {
event: "session-end".into(),
agent: "devin".into(),
- server_url: "http://127.0.0.1:1".into(),
+ server_url: dead_server_url(),
auth_token: None,
project_strategy: None,
check_capture: false,
@@ -1893,7 +1901,7 @@ mod tests {
let mut stdout = Vec::new();
let called = std::cell::Cell::new(false);
let mut args = devin_hook_args("post-tool-use");
- args.server_url = "http://127.0.0.1:1".into();
+ args.server_url = dead_server_url();
run_with_payload(Some(data_dir.clone()), args, serde_json::json!({"cwd":tmp.path(),"tool_name":"Edit","tool_input":{"path":"secret/SENTINEL"}}).to_string(), &mut stdout, |_, _| { called.set(true); Ok(()) }).await.unwrap();
assert_eq!(stdout, b"{}\n");
assert!(!called.get());
diff --git a/crates/ai-memory-cli/src/commands/render_shared.rs b/crates/ai-memory-cli/src/commands/render_shared.rs
index 1c0e246c..4c3d7b55 100644
--- a/crates/ai-memory-cli/src/commands/render_shared.rs
+++ b/crates/ai-memory-cli/src/commands/render_shared.rs
@@ -1972,6 +1972,18 @@ mod tests {
String::from_utf16(&utf16).expect("invalid UTF-16 PowerShell program")
}
+ #[cfg(windows)]
+ fn command_for_available_powershell(command: &str, exe: &str) -> String {
+ if exe.eq_ignore_ascii_case("powershell.exe") {
+ command.to_owned()
+ } else {
+ format!(
+ "function powershell.exe {{ & {} @args }}; {command}",
+ powershell_quote(exe)
+ )
+ }
+ }
+
fn build_posix_hook_payload(
events: &[(&str, &str)],
root: &Path,
@@ -2995,13 +3007,15 @@ $payload = [Console]::In.ReadToEnd()
HookCommandContext::new(HookCommandPlatform::Windows, "antigravity-cli", None, None),
);
- let mut child = Command::new("powershell.exe")
+ let powershell = ai_memory_test_support::powershell_exe();
+ let outer_command = command_for_available_powershell(&command, powershell);
+ let mut child = Command::new(powershell)
.args([
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-Command",
- &command,
+ &outer_command,
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
diff --git a/crates/ai-memory-cli/src/lib.rs b/crates/ai-memory-cli/src/lib.rs
new file mode 100644
index 00000000..7c92b179
--- /dev/null
+++ b/crates/ai-memory-cli/src/lib.rs
@@ -0,0 +1,170 @@
+//! `ai-memory` CLI library.
+//!
+//! Holds everything the `ai-memory` binary does. `main.rs` is a shim over
+//! [`run`], so this logic lives in a lib target rather than a bin target:
+//! unit tests compile into a reusable rlib, integration tests can link it
+//! directly instead of shelling out to the executable, and `--lib` runs skip
+//! building the binary entirely.
+//!
+//! Loads configuration once at startup, initialises tracing, then dispatches
+//! to the requested subcommand. Domain crates take `&Config` by reference;
+//! there is no global state, no `lazy_static`, no second config-read path
+//! (lesson from agentmemory #456 / #469).
+
+#![doc(html_no_source)]
+
+use std::sync::Arc;
+
+use anyhow::Result;
+use clap::Parser;
+use tracing::info;
+
+mod auth_bearer;
+mod cli;
+mod commands;
+mod config;
+mod http_client;
+mod logging;
+mod marker;
+mod process_guard;
+
+use cli::{Cli, Command};
+use config::Config;
+
+/// Parses argv, loads config, and dispatches to the requested subcommand.
+///
+/// Returns the subcommand's result. Commands that carry a non-zero process
+/// exit code call [`std::process::exit`] directly rather than encoding it in
+/// the return type.
+pub async fn run() -> Result<()> {
+ let Cli {
+ data_dir,
+ config: config_path,
+ command,
+ } = Cli::parse();
+
+ // Hooks fire on every tool call: they must be cheap and must emit ONLY
+ // their JSON object to stdout. Short-circuit before config load and
+ // tracing init (added latency + possible stdout noise). The hook reads
+ // its server URL + token from flags; it only needs the data-dir to locate
+ // a stored OIDC token when no explicit `--auth-token` is given, so we pass
+ // the bare path rather than loading the full config.
+ let command = match command {
+ Command::Hook(args) => return commands::hook::run(data_dir, args).await,
+ Command::HookDrain(_args) => return commands::hook::run_drain(data_dir).await,
+ // Completions are pure text derived from the command tree. Emitting
+ // them must not require a loadable config or an initialised data dir
+ // (they are typically generated before `init`, or in a packaging
+ // step), and tracing must not get the chance to interleave anything
+ // into the script on stdout.
+ Command::Completions(args) => return commands::completions::run(args),
+ other => other,
+ };
+
+ let config = Arc::new(Config::load(config_path.as_deref(), data_dir)?);
+ // Only the long-running server warns when file logging degrades (an
+ // operator wants to know persistent logs moved); one-shot client
+ // commands degrade silently: their file logs are irrelevant and the
+ // warning read like the command itself had a problem.
+ let degrade_warnings = if matches!(command, Command::Serve(_)) {
+ logging::DegradeWarnings::Loud
+ } else {
+ logging::DegradeWarnings::Quiet
+ };
+ let _logging_guard = logging::init(&config, degrade_warnings)?;
+
+ info!(
+ version = env!("CARGO_PKG_VERSION"),
+ server_url = %config.server_url,
+ data_dir = %config.data_dir.display(),
+ bind = %config.bind,
+ "ai-memory starting",
+ );
+
+ match command {
+ Command::Init(args) => commands::init::run(&config, args, config_path.as_deref()),
+ Command::Status(args) => commands::status::run(&config, args).await,
+ Command::Run(args) => {
+ let exit_code = commands::run::run(&config, args).await?;
+ if exit_code != 0 {
+ std::process::exit(exit_code);
+ }
+ Ok(())
+ }
+ Command::Show(args) => {
+ let exit_code = commands::show::run(&config, args).await?;
+ if exit_code != 0 {
+ std::process::exit(exit_code);
+ }
+ Ok(())
+ }
+ Command::Continue(args) => {
+ let exit_code = commands::continue_session::run(&config, args).await?;
+ if exit_code != 0 {
+ std::process::exit(exit_code);
+ }
+ Ok(())
+ }
+ Command::Resume(args) => {
+ let exit_code = commands::resume::run(&config, args).await?;
+ if exit_code != 0 {
+ std::process::exit(exit_code);
+ }
+ Ok(())
+ }
+ Command::Handoffs(args) => commands::handoffs::run(&config, args).await,
+ Command::Workstreams(args) => commands::workstreams::run(&config, args).await,
+ Command::RenameWorkstream(args) => commands::rename_workstream::run(&config, args).await,
+ Command::WorkstreamSearch(args) => commands::workstream_search::run(&config, args).await,
+ Command::AuditContamination(args) => {
+ commands::audit_contamination::run(&config, args).await
+ }
+ Command::Search(args) => commands::search::run(&config, args).await,
+ Command::ReadPage(args) => commands::read_page::run(&config, args).await,
+ Command::WritePage(args) => commands::write_page::run(&config, args).await,
+ Command::DeletePage(args) => commands::delete_page::run(&config, args).await,
+ Command::Serve(args) => commands::serve::run(&config, args).await,
+ Command::Reset(args) => commands::reset::run(&config, args),
+ Command::Compact(args) => commands::compact::run(&config, args).await,
+ Command::Backup(args) => commands::backup::run(&config, args).await,
+ Command::ExportOkf(args) => commands::export_okf::run(&config, args).await,
+ Command::Restore(args) => commands::restore::run(&config, args),
+ Command::Reindex(args) => commands::reindex::run(&config, args).await,
+ Command::InstallHooks(args) => commands::install_hooks::run(&config, args),
+ // `Hook` is handled in the fast-path above (before config/tracing).
+ Command::Hook(args) => commands::hook::run(Some(config.data_dir.clone()), args).await,
+ // `HookDrain` is handled in the fast-path above (before config/tracing).
+ Command::HookDrain(_args) => commands::hook::run_drain(Some(config.data_dir.clone())).await,
+ Command::InstallMcp(args) => commands::install_mcp::run(&config, args),
+ Command::McpBridge(args) => commands::mcp_bridge::run(&config, args).await,
+ Command::Commit(args) => commands::commit::run(&config, args).await,
+ Command::Checkpoints(args) => commands::checkpoints::run(&config, args).await,
+ Command::RestorePage(args) => commands::restore_page::run(&config, args).await,
+ Command::LlmTest(args) => commands::llm_test::run(&config, args).await,
+ Command::ForgetSweep(args) => commands::forget_sweep::run(&config, args).await,
+ Command::Lint(args) => commands::lint::run(&config, args).await,
+ Command::Curator(args) => commands::curator::run(&config, args).await,
+ Command::AutoImproveReport(args) => commands::auto_improve_report::run(&config, args).await,
+ Command::AutoImprove(args) => commands::auto_improve::run(&config, args).await,
+ Command::FinalizeSession(args) => commands::finalize_session::run(&config, args).await,
+ Command::PendingWrites(args) => commands::pending_writes::run(&config, args).await,
+ Command::Embed(args) => commands::embed::run(&config, args).await,
+ Command::GenerateAuthToken(args) => commands::generate_auth_token::run(&config, args),
+ Command::SetupAgent(args) => commands::setup_agent::run(&config, args),
+ Command::Bootstrap(args) => commands::bootstrap::run(&config, args).await,
+ Command::InstallInstructions(args) => commands::install_instructions::run(&config, args),
+ Command::InstallSkills(args) => commands::install_skills::run(&config, args),
+ Command::Reorg(args) => commands::reorg::run(&config, args).await,
+ Command::PurgeProject(args) => commands::purge_project::run(&config, args).await,
+ Command::PurgeSession(args) => commands::purge_session::run(&config, args).await,
+ Command::RenameProject(args) => commands::rename_project::run(&config, args).await,
+ Command::MoveProject(args) => commands::move_project::run(&config, args).await,
+ Command::MoveSession(args) => commands::move_session::run(&config, args).await,
+ Command::Uninstall(args) => commands::uninstall::run(&config, args),
+ Command::Auth(args) => commands::auth::run(&config, args).await,
+ Command::User(args) => commands::user::run(&config, args).await,
+ Command::ApiKey(args) => commands::api_key::run(&config, args).await,
+ // `Completions` is handled in the fast-path above (before config/tracing).
+ Command::Completions(args) => commands::completions::run(args),
+ }
+}
diff --git a/crates/ai-memory-cli/src/main.rs b/crates/ai-memory-cli/src/main.rs
index 04087421..c3eab39d 100644
--- a/crates/ai-memory-cli/src/main.rs
+++ b/crates/ai-memory-cli/src/main.rs
@@ -1,160 +1,13 @@
//! `ai-memory` binary entry point.
//!
-//! Loads configuration once at startup, initialises tracing, then dispatches
-//! to the requested subcommand. Domain crates take `&Config` by reference;
-//! there is no global state, no `lazy_static`, no second config-read path
-//! (lesson from agentmemory #456 / #469).
+//! Deliberately thin: all logic lives in the `ai_memory_cli` lib target so it
+//! is unit-testable and linkable. See that crate's docs for the dispatch flow.
#![doc(html_no_source)]
-use std::sync::Arc;
-
use anyhow::Result;
-use clap::Parser;
-use tracing::info;
-
-mod auth_bearer;
-mod cli;
-mod commands;
-mod config;
-mod http_client;
-mod logging;
-mod marker;
-mod process_guard;
-
-use cli::{Cli, Command};
-use config::Config;
#[tokio::main]
async fn main() -> Result<()> {
- let Cli {
- data_dir,
- config: config_path,
- command,
- } = Cli::parse();
-
- // Hooks fire on every tool call: they must be cheap and must emit ONLY
- // their JSON object to stdout. Short-circuit before config load and
- // tracing init (added latency + possible stdout noise). The hook reads
- // its server URL + token from flags; it only needs the data-dir to locate
- // a stored OIDC token when no explicit `--auth-token` is given, so we pass
- // the bare path rather than loading the full config.
- let command = match command {
- Command::Hook(args) => return commands::hook::run(data_dir, args).await,
- Command::HookDrain(_args) => return commands::hook::run_drain(data_dir).await,
- // Completions are pure text derived from the command tree. Emitting
- // them must not require a loadable config or an initialised data dir
- // (they are typically generated before `init`, or in a packaging
- // step), and tracing must not get the chance to interleave anything
- // into the script on stdout.
- Command::Completions(args) => return commands::completions::run(args),
- other => other,
- };
-
- let config = Arc::new(Config::load(config_path.as_deref(), data_dir)?);
- // Only the long-running server warns when file logging degrades (an
- // operator wants to know persistent logs moved); one-shot client
- // commands degrade silently — their file logs are irrelevant and the
- // warning read like the command itself had a problem.
- let degrade_warnings = if matches!(command, Command::Serve(_)) {
- logging::DegradeWarnings::Loud
- } else {
- logging::DegradeWarnings::Quiet
- };
- let _logging_guard = logging::init(&config, degrade_warnings)?;
-
- info!(
- version = env!("CARGO_PKG_VERSION"),
- server_url = %config.server_url,
- data_dir = %config.data_dir.display(),
- bind = %config.bind,
- "ai-memory starting",
- );
-
- match command {
- Command::Init(args) => commands::init::run(&config, args, config_path.as_deref()),
- Command::Status(args) => commands::status::run(&config, args).await,
- Command::Run(args) => {
- let exit_code = commands::run::run(&config, args).await?;
- if exit_code != 0 {
- std::process::exit(exit_code);
- }
- Ok(())
- }
- Command::Show(args) => {
- let exit_code = commands::show::run(&config, args).await?;
- if exit_code != 0 {
- std::process::exit(exit_code);
- }
- Ok(())
- }
- Command::Continue(args) => {
- let exit_code = commands::continue_session::run(&config, args).await?;
- if exit_code != 0 {
- std::process::exit(exit_code);
- }
- Ok(())
- }
- Command::Resume(args) => {
- let exit_code = commands::resume::run(&config, args).await?;
- if exit_code != 0 {
- std::process::exit(exit_code);
- }
- Ok(())
- }
- Command::Handoffs(args) => commands::handoffs::run(&config, args).await,
- Command::Workstreams(args) => commands::workstreams::run(&config, args).await,
- Command::RenameWorkstream(args) => commands::rename_workstream::run(&config, args).await,
- Command::WorkstreamSearch(args) => commands::workstream_search::run(&config, args).await,
- Command::AuditContamination(args) => {
- commands::audit_contamination::run(&config, args).await
- }
- Command::Search(args) => commands::search::run(&config, args).await,
- Command::ReadPage(args) => commands::read_page::run(&config, args).await,
- Command::WritePage(args) => commands::write_page::run(&config, args).await,
- Command::DeletePage(args) => commands::delete_page::run(&config, args).await,
- Command::Serve(args) => commands::serve::run(&config, args).await,
- Command::Reset(args) => commands::reset::run(&config, args),
- Command::Compact(args) => commands::compact::run(&config, args).await,
- Command::Backup(args) => commands::backup::run(&config, args).await,
- Command::ExportOkf(args) => commands::export_okf::run(&config, args).await,
- Command::Restore(args) => commands::restore::run(&config, args),
- Command::Reindex(args) => commands::reindex::run(&config, args).await,
- Command::InstallHooks(args) => commands::install_hooks::run(&config, args),
- // `Hook` is handled in the fast-path above (before config/tracing).
- Command::Hook(args) => commands::hook::run(Some(config.data_dir.clone()), args).await,
- // `HookDrain` is handled in the fast-path above (before config/tracing).
- Command::HookDrain(_args) => commands::hook::run_drain(Some(config.data_dir.clone())).await,
- Command::InstallMcp(args) => commands::install_mcp::run(&config, args),
- Command::McpBridge(args) => commands::mcp_bridge::run(&config, args).await,
- Command::Commit(args) => commands::commit::run(&config, args).await,
- Command::Checkpoints(args) => commands::checkpoints::run(&config, args).await,
- Command::RestorePage(args) => commands::restore_page::run(&config, args).await,
- Command::LlmTest(args) => commands::llm_test::run(&config, args).await,
- Command::ForgetSweep(args) => commands::forget_sweep::run(&config, args).await,
- Command::Lint(args) => commands::lint::run(&config, args).await,
- Command::Curator(args) => commands::curator::run(&config, args).await,
- Command::AutoImproveReport(args) => commands::auto_improve_report::run(&config, args).await,
- Command::AutoImprove(args) => commands::auto_improve::run(&config, args).await,
- Command::FinalizeSession(args) => commands::finalize_session::run(&config, args).await,
- Command::PendingWrites(args) => commands::pending_writes::run(&config, args).await,
- Command::Embed(args) => commands::embed::run(&config, args).await,
- Command::GenerateAuthToken(args) => commands::generate_auth_token::run(&config, args),
- Command::SetupAgent(args) => commands::setup_agent::run(&config, args),
- Command::Bootstrap(args) => commands::bootstrap::run(&config, args).await,
- Command::InstallInstructions(args) => commands::install_instructions::run(&config, args),
- Command::InstallSkills(args) => commands::install_skills::run(&config, args),
- Command::Reorg(args) => commands::reorg::run(&config, args).await,
- Command::PurgeProject(args) => commands::purge_project::run(&config, args).await,
- Command::PurgeSession(args) => commands::purge_session::run(&config, args).await,
- Command::RenameProject(args) => commands::rename_project::run(&config, args).await,
- Command::MoveProject(args) => commands::move_project::run(&config, args).await,
- Command::MoveSession(args) => commands::move_session::run(&config, args).await,
- Command::Uninstall(args) => commands::uninstall::run(&config, args),
- Command::Auth(args) => commands::auth::run(&config, args).await,
- Command::User(args) => commands::user::run(&config, args).await,
- Command::ApiKey(args) => commands::api_key::run(&config, args).await,
- // `Completions` is handled in the fast-path above (before config/tracing).
- Command::Completions(args) => commands::completions::run(args),
- }
+ ai_memory_cli::run().await
}
diff --git a/crates/ai-memory-cli/tests/autoscope_env.rs b/crates/ai-memory-cli/tests/suite/autoscope_env.rs
similarity index 100%
rename from crates/ai-memory-cli/tests/autoscope_env.rs
rename to crates/ai-memory-cli/tests/suite/autoscope_env.rs
diff --git a/crates/ai-memory-cli/tests/completions.rs b/crates/ai-memory-cli/tests/suite/completions.rs
similarity index 100%
rename from crates/ai-memory-cli/tests/completions.rs
rename to crates/ai-memory-cli/tests/suite/completions.rs
diff --git a/crates/ai-memory-cli/tests/hook_drain.rs b/crates/ai-memory-cli/tests/suite/hook_drain.rs
similarity index 100%
rename from crates/ai-memory-cli/tests/hook_drain.rs
rename to crates/ai-memory-cli/tests/suite/hook_drain.rs
diff --git a/crates/ai-memory-cli/tests/hook_payload.rs b/crates/ai-memory-cli/tests/suite/hook_payload.rs
similarity index 99%
rename from crates/ai-memory-cli/tests/hook_payload.rs
rename to crates/ai-memory-cli/tests/suite/hook_payload.rs
index 6acccc85..5e8186c2 100644
--- a/crates/ai-memory-cli/tests/hook_payload.rs
+++ b/crates/ai-memory-cli/tests/suite/hook_payload.rs
@@ -24,7 +24,7 @@ fn run_hook_full(data_dir: &Path, event: &str, payload: &[u8], capture_assistant
"--agent".to_string(),
"claude-code".to_string(),
"--server-url".to_string(),
- "http://127.0.0.1:1".to_string(),
+ ai_memory_test_support::dead_http_endpoint(),
];
if capture_assistant {
args.push("--capture-assistant".to_string());
diff --git a/crates/ai-memory-cli/tests/suite/main.rs b/crates/ai-memory-cli/tests/suite/main.rs
new file mode 100644
index 00000000..4a5cbdaa
--- /dev/null
+++ b/crates/ai-memory-cli/tests/suite/main.rs
@@ -0,0 +1,17 @@
+//! Single binary for this crate's integration tests.
+//!
+//! Every file in this directory is a module of this one test binary: one
+//! link per rebuild instead of one per file. Cargo treats `tests/suite/main.rs`
+//! as the single `suite` target and never builds the sibling files on their own,
+//! so a new file must be declared here (`scripts/check-test-suites.*` enforces it).
+
+mod autoscope_env;
+mod completions;
+mod hook_drain;
+mod hook_payload;
+mod marker_scope;
+mod packaging;
+mod removal;
+mod repo_layout;
+mod routing_instructions;
+mod routing_skills;
diff --git a/crates/ai-memory-cli/tests/marker_scope.rs b/crates/ai-memory-cli/tests/suite/marker_scope.rs
similarity index 95%
rename from crates/ai-memory-cli/tests/marker_scope.rs
rename to crates/ai-memory-cli/tests/suite/marker_scope.rs
index abc1d2ce..49dba212 100644
--- a/crates/ai-memory-cli/tests/marker_scope.rs
+++ b/crates/ai-memory-cli/tests/suite/marker_scope.rs
@@ -38,9 +38,12 @@ fn search_stderr(cwd: &Path, data_dir: &Path, extra_env: &[(&str, &str)], args:
.current_dir(cwd)
.env("HOME", cwd)
.env("AI_MEMORY_DATA_DIR", data_dir)
- // Port 1 is reserved and never listening: the command resolves its
- // scope, prints the notice, then fails on connect.
- .env("AI_MEMORY_SERVER_URL", "http://127.0.0.1:1")
+ // A dead endpoint: the command resolves its scope, prints the notice,
+ // then fails on connect.
+ .env(
+ "AI_MEMORY_SERVER_URL",
+ ai_memory_test_support::dead_http_endpoint(),
+ )
// `AI_MEMORY_HOME` outranks `$HOME` in `path_util::home_dir`, so an
// exported one on the developer's machine would unpin the walk.
.env_remove("AI_MEMORY_HOME")
diff --git a/crates/ai-memory-cli/tests/packaging.rs b/crates/ai-memory-cli/tests/suite/packaging.rs
similarity index 52%
rename from crates/ai-memory-cli/tests/packaging.rs
rename to crates/ai-memory-cli/tests/suite/packaging.rs
index b1dd4ac3..59566695 100644
--- a/crates/ai-memory-cli/tests/packaging.rs
+++ b/crates/ai-memory-cli/tests/suite/packaging.rs
@@ -235,7 +235,11 @@ fn aur_packages_install_all_native_assets() {
#[test]
fn docker_source_build_uses_vendored_tailwind() {
let dockerfile = read_repo("docker/Dockerfile");
- assert!(dockerfile.contains("TAILWIND_SKIP=1 cargo build --locked --release -p ai-memory-cli"));
+ assert!(dockerfile.contains("cargo build --locked --release -p ai-memory-cli"));
+ assert!(
+ !dockerfile.contains("TAILWIND"),
+ "the vendored stylesheet is the default; the image build must not opt into a regeneration"
+ );
}
#[test]
@@ -289,36 +293,6 @@ fn docker_publish_jobs_use_prebuilt_binaries() {
assert!(ci.contains("--target runtime-prebuilt-amd64"));
}
-#[cfg(unix)]
-#[test]
-fn macos_wrapper_routes_urls_by_real_subcommand() {
- for subcommand in ["install-mcp", "install-hooks", "setup-agent"] {
- let args = run_wrapper_on_fake_macos(&[subcommand]);
- assert!(
- !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "{subcommand} renders host-side config and must keep loopback defaults; got {args}"
- );
- }
-
- let args = run_wrapper_on_fake_macos(&["status"]);
- assert!(
- args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "thin-client commands must reach the host server through Docker Desktop; got {args}"
- );
-
- let args = run_wrapper_on_fake_macos(&["search", "install-hooks"]);
- assert!(
- args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "only the actual subcommand should control URL routing; got {args}"
- );
-
- let args = run_wrapper_on_fake_macos(&["--config", "/tmp/config.toml", "install-hooks"]);
- assert!(
- !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "global options before install-hooks must not hide the real subcommand; got {args}"
- );
-}
-
// Like run_wrapper_on_fake_macos's fake docker, but the wrapper is spawned with
// stdin on a pipe: the shape every `cat page.md | ai-memory write-page --body -`
// (and every CI/cron) invocation actually has.
@@ -371,40 +345,6 @@ fn run_wrapper_with_piped_stdin(args: &[&str], stdin_payload: &str) -> String {
std::fs::read_to_string(docker_args).unwrap()
}
-#[cfg(unix)]
-#[test]
-fn wrapper_keeps_stdin_attached_when_it_is_a_pipe() {
- let args = run_wrapper_with_piped_stdin(
- &["write-page", "--path", "notes/x.md", "--body", "-"],
- "# body that must survive the container boundary\n",
- );
- let flags: Vec<&str> = args.lines().collect();
-
- // Without `-i` docker gives the container a closed stdin, so `--body -`
- // reads an empty string and the page is persisted with frontmatter only —
- // silently, because the CLI still reports a successful write.
- assert!(
- flags.contains(&"-i"),
- "piped stdin must stay attached for `--body -`; got {args}"
- );
- // A pipe is not a terminal: asking for a TTY here makes docker fail with
- // "the input device is not a TTY".
- assert!(
- !flags.contains(&"-t") && !flags.contains(&"-it"),
- "no TTY may be requested when stdin is a pipe; got {args}"
- );
- assert!(
- flags
- .iter()
- .any(|arg| arg.starts_with("AI_MEMORY_SCOPE_CWD=/scope")),
- "an outside-home checkout must expose its bounded marker path; got {args}"
- );
- assert!(
- flags.iter().any(|arg| arg.ends_with(":/scope:ro")),
- "the marker scope mount must be read-only; got {args}"
- );
-}
-
#[test]
fn docker_wrappers_keep_stdin_attached_independently_of_tty_allocation() {
let posix = read_repo("bin/ai-memory");
@@ -548,202 +488,66 @@ fn workflows_keep_fixed_rust_jobs_on_the_fixed_toolchain() {
}
}
-#[cfg(unix)]
#[test]
-fn wrapper_self_upgrade_rejects_a_checksum_mismatch() {
- let tmp = tempfile::tempdir().unwrap();
- let bin_dir = tmp.path().join("bin");
- std::fs::create_dir_all(&bin_dir).unwrap();
- let wrapper = bin_dir.join("ai-memory");
- let original = read_repo("bin/ai-memory");
- std::fs::write(&wrapper, &original).unwrap();
-
- let payload = tmp.path().join("hostile-wrapper");
- std::fs::write(
- &payload,
- "#!/usr/bin/env bash\nprintf 'hostile payload executed\\n' >&2\nexit 91\n",
- )
- .unwrap();
- let curl = bin_dir.join("curl");
- std::fs::write(
- &curl,
- "#!/usr/bin/env bash\n\
- set -euo pipefail\n\
- url=''\n\
- out=''\n\
- while [ \"$#\" -gt 0 ]; do\n\
- case \"$1\" in\n\
- -o) out=\"$2\"; shift 2 ;;\n\
- -*) shift ;;\n\
- *) url=\"$1\"; shift ;;\n\
- esac\n\
- done\n\
- case \"$url\" in\n\
- *.sha256) printf '%064d ai-memory-wrapper\\n' 0 > \"$out\" ;;\n\
- *) cp \"$FAKE_WRAPPER_PAYLOAD\" \"$out\" ;;\n\
- esac\n",
- )
- .unwrap();
- let docker = bin_dir.join("docker");
- std::fs::write(&docker, "#!/usr/bin/env bash\nexit 0\n").unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- for path in [&wrapper, &payload, &curl, &docker] {
- std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
- }
-
- let path = format!(
- "{}:{}",
- shell_path(&bin_dir),
- std::env::var("PATH").unwrap_or_default()
- );
- let output = freshly_written_shell_script_command(&wrapper)
- .arg("upgrade")
- .env("PATH", path)
- .env("HOME", tmp.path())
- .env("AI_MEMORY_DOCKER", &docker)
- .env(
- "AI_MEMORY_WRAPPER_URL",
- "https://example.invalid/ai-memory-wrapper",
- )
- .env("FAKE_WRAPPER_PAYLOAD", &payload)
- .output()
- .unwrap();
+fn dev_loop_is_wired_consistently() {
+ // One gate, written down in four places; keep them from drifting apart.
+ let ci = read_repo(".github/workflows/ci.yml");
+ assert!(ci.contains("cargo test --workspace --all-targets"));
assert!(
- output.status.success(),
- "upgrade failed: stdout={} stderr={}",
- String::from_utf8_lossy(&output.stdout),
- String::from_utf8_lossy(&output.stderr)
+ ci.contains("TAILWIND_BUILD=1"),
+ "CI must regenerate the stylesheet somewhere"
);
- let stdout = String::from_utf8_lossy(&output.stdout);
- assert!(stdout.contains("wrapper checksum mismatch; refusing update"));
- assert!(!String::from_utf8_lossy(&output.stderr).contains("hostile payload executed"));
- assert_eq!(std::fs::read_to_string(&wrapper).unwrap(), original);
-}
+ assert!(ci.contains("git diff --exit-code -- crates/ai-memory-web/static/tailwind.css"));
-#[cfg(unix)]
-#[test]
-fn wrapper_self_upgrade_installs_and_runs_a_verified_payload() {
- let tmp = tempfile::tempdir().unwrap();
- let bin_dir = tmp.path().join("bin");
- std::fs::create_dir_all(&bin_dir).unwrap();
- let wrapper = bin_dir.join("ai-memory");
- std::fs::write(&wrapper, read_repo("bin/ai-memory")).unwrap();
+ let release = read_repo("bin/release");
+ assert!(release.contains("git diff --check"));
+ assert!(release.contains("cargo test --workspace --all-targets"));
- let payload = tmp.path().join("verified-wrapper");
- let payload_body = "#!/usr/bin/env bash\nprintf 'verified wrapper executed\\n'\n";
- std::fs::write(&payload, payload_body).unwrap();
- let curl = bin_dir.join("curl");
- std::fs::write(
- &curl,
- "#!/usr/bin/env bash\n\
- set -euo pipefail\n\
- url=''\n\
- out=''\n\
- while [ \"$#\" -gt 0 ]; do\n\
- case \"$1\" in\n\
- -o) out=\"$2\"; shift 2 ;;\n\
- -*) shift ;;\n\
- *) url=\"$1\"; shift ;;\n\
- esac\n\
- done\n\
- case \"$url\" in\n\
- *.sha256) printf '%s ai-memory-wrapper\\n' \"$FAKE_WRAPPER_CHECKSUM\" > \"$out\" ;;\n\
- *) cp \"$FAKE_WRAPPER_PAYLOAD\" \"$out\" ;;\n\
- esac\n",
- )
- .unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- for path in [&wrapper, &payload, &curl] {
- std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
- }
+ let pr_template = read_repo(".github/pull_request_template.md");
+ assert!(pr_template.contains("git diff --check"));
+ assert!(pr_template.contains("cargo tf"));
- let path = format!(
- "{}:{}",
- shell_path(&bin_dir),
- std::env::var("PATH").unwrap_or_default()
- );
- let output = freshly_written_shell_script_command(&wrapper)
- .arg("upgrade")
- .env("PATH", path)
- .env("HOME", tmp.path())
- .env(
- "AI_MEMORY_WRAPPER_URL",
- "https://example.invalid/ai-memory-wrapper",
- )
- .env("FAKE_WRAPPER_PAYLOAD", &payload)
- .env("FAKE_WRAPPER_CHECKSUM", sha256_file(&payload))
- .output()
- .unwrap();
- assert!(
- output.status.success(),
- "upgrade failed: stdout={} stderr={}",
- String::from_utf8_lossy(&output.stdout),
- String::from_utf8_lossy(&output.stderr)
- );
- assert!(String::from_utf8_lossy(&output.stdout).contains("verified wrapper executed"));
- assert_eq!(std::fs::read_to_string(&wrapper).unwrap(), payload_body);
-}
+ let contributing = read_repo("CONTRIBUTING.md");
+ assert!(contributing.contains("cargo tf"));
+ assert!(contributing.contains("install-git-hooks.sh"));
-#[cfg(unix)]
-#[test]
-fn hook_installer_rejects_a_checksum_mismatch_before_writing_scripts() {
- let tmp = tempfile::tempdir().unwrap();
- let bin_dir = tmp.path().join("bin");
- std::fs::create_dir_all(&bin_dir).unwrap();
- let curl = bin_dir.join("curl");
- std::fs::write(
- &curl,
- "#!/usr/bin/env bash\n\
- set -euo pipefail\n\
- url=''\n\
- out=''\n\
- while [ \"$#\" -gt 0 ]; do\n\
- case \"$1\" in\n\
- -o) out=\"$2\"; shift 2 ;;\n\
- -*) shift ;;\n\
- *) url=\"$1\"; shift ;;\n\
- esac\n\
- done\n\
- case \"$url\" in\n\
- *.sha256) printf '%064d ai-memory-hooks.tar.gz\\n' 0 > \"$out\" ;;\n\
- *) printf 'not the expected archive' > \"$out\" ;;\n\
- esac\n",
- )
- .unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- std::fs::set_permissions(&curl, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
- let path = format!(
- "{}:{}",
- shell_path(&bin_dir),
- std::env::var("PATH").unwrap_or_default()
- );
- let destination = tmp.path().join("hooks");
- let output = shell_script_command(&repo_root().join("scripts/install-hooks.sh"))
- .args(["--agent", "claude-code", "--to"])
- .arg(&destination)
- .env("PATH", path)
- .env("HOME", tmp.path())
- .output()
- .unwrap();
+ let installer = read_repo("scripts/install-git-hooks.sh");
+ assert!(installer.contains("# >>> ai-memory pre-push >>>"));
assert!(
- !output.status.success(),
- "checksum mismatch must fail closed"
+ !installer.contains("cat > \"$hook\""),
+ "the hook installer must not overwrite an existing user hook"
);
+
+ // `.cargo/config.toml` carries the tier aliases and nothing else. An env
+ // table there applies to every cargo invocation on every platform.
+ let cargo_config = read_repo(".cargo/config.toml");
assert!(
- String::from_utf8_lossy(&output.stderr)
- .contains("hook bundle checksum mismatch; refusing installation")
- );
- let agent_dir = destination.join("claude-code");
- assert!(
- !agent_dir.exists() || std::fs::read_dir(agent_dir).unwrap().next().is_none(),
- "no hook script may be written before archive verification"
+ !cargo_config.contains("[env]"),
+ "keep .cargo/config.toml to aliases"
);
+ for alias in ["t = ", "tf = "] {
+ assert!(
+ cargo_config.contains(alias),
+ "missing cargo alias {alias:?}"
+ );
+ }
+
+ // The build is self-contained: nothing should still ask for the old skip flag.
+ for path in [
+ ".github/workflows/ci.yml",
+ ".github/workflows/release.yml",
+ ".github/workflows/windows.yml",
+ "docker/Dockerfile",
+ "bin/release",
+ "AGENTS.md",
+ "CONTRIBUTING.md",
+ ".github/pull_request_template.md",
+ ] {
+ assert!(
+ !read_repo(path).contains("TAILWIND_SKIP"),
+ "{path} still sets TAILWIND_SKIP"
+ );
+ }
}
#[cfg(unix)]
@@ -836,308 +640,59 @@ fn installed_hook_names(agent_arg: &str, canonical_agent: &str, hooks: &[&str])
names
}
+// Unlike run_wrapper_on_fake_macos's docker fake (which only ever sees one
+// meaningful call — the final `docker run`), the rootless-Docker UID check
+// calls `docker info` *before* `docker run`, so this fake must dispatch on
+// $1: real stdout for `info` (read by the wrapper's `grep -q rootless`) vs.
+// logging argv to a file for `run` (read back by the test).
+// How the fake `docker` binary answers `docker info --format …`. The two
+// engines expose the same two facts under incompatible keys, and the wrapper
+// has to read both, so the fake has to be able to impersonate either one.
#[cfg(unix)]
-#[test]
-fn hook_installer_writes_only_expected_files_from_a_verified_archive() {
- const HOOKS: &[&str] = &[
- "post-tool-use",
- "pre-compact",
- "pre-tool-use",
- "session-end",
- "session-start",
- "stop",
- "user-prompt-submit",
- ];
-
- let names = installed_hook_names("claude-code", "claude-code", HOOKS);
- let expected = HOOKS
- .iter()
- .map(|hook| format!("{hook}.sh"))
- .collect::<Vec<_>>();
- assert_eq!(names, expected);
+enum FakeInfo<'a> {
+ // Docker: `{{.SecurityOptions}}` prints the security-options list.
+ Docker(&'a str),
+ // Podman, including the podman-docker `docker` shim: `.SecurityOptions`
+ // is not a field of its info report, so the template fails and the
+ // command exits non-zero with nothing on stdout. The equivalent facts
+ // live under `.Host.Security.*`.
+ Podman { rootless: bool, selinux: bool },
}
#[cfg(unix)]
-#[test]
-fn hook_installer_writes_only_command_code_stable_events() {
- const HOOKS: &[&str] = &["post-tool-use", "pre-tool-use", "session-start", "stop"];
-
- let names = installed_hook_names("cmdc", "command-code", HOOKS);
- let expected = HOOKS
- .iter()
- .map(|hook| format!("{hook}.sh"))
- .collect::<Vec<_>>();
- assert_eq!(names, expected);
+fn run_wrapper_with_fake_docker(args: &[&str], docker_info_stdout: &str) -> String {
+ run_wrapper_with_fake_docker_and_uname(args, docker_info_stdout, None)
}
#[cfg(unix)]
-#[test]
-fn managed_host_commands_use_native_path_and_remote_server_without_docker() {
- let tmp = tempfile::tempdir().unwrap();
- let native = tmp.path().join("native-ai-memory");
- let docker = tmp.path().join("docker");
- let record = tmp.path().join("native-record.txt");
- let docker_record = tmp.path().join("docker-record.txt");
- std::fs::write(
- &native,
- format!(
- "#!/usr/bin/env bash\n\
- printf 'server=%s\\nauth=%s\\npath=%s\\n' \"$AI_MEMORY_SERVER_URL\" \"$AI_MEMORY_AUTH_TOKEN\" \"$PATH\" > {}\n\
- printf 'arg=%s\\n' \"$@\" >> {}\n",
- shell_path(&record),
- shell_path(&record)
- ),
+fn run_wrapper_with_fake_docker_and_claude_config(
+ args: &[&str],
+ docker_info_stdout: &str,
+ claude_config_dir: &str,
+) -> String {
+ run_wrapper_with_fake_docker_env(
+ args,
+ FakeInfo::Docker(docker_info_stdout),
+ None,
+ Some(claude_config_dir),
+ None,
+ &[],
)
- .unwrap();
- std::fs::write(
- &docker,
- format!(
- "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\nexit 99\n",
- shell_path(&docker_record)
- ),
- )
- .unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o755)).unwrap();
- std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
- let host_path = format!(
- "{}:{}",
- shell_path(tmp.path()),
- std::env::var("PATH").unwrap_or_default()
- );
- let commands: &[&[&str]] = &[
- &["run", "codex", "--yolo", "resume"],
- &["show", "--json", "--no-scan"],
- &["continue", "--workspace", "work", "--yolo"],
- &["resume", "--workspace", "work", "--limit", "5"],
- &["workstreams", "--limit", "5", "--json"],
- &["rename-workstream", "--from", "old", "--to", "new"],
- ];
- for args in commands {
- let output = shell_script_command(&repo_root().join("bin/ai-memory"))
- .args(args.iter().copied())
- .env("AI_MEMORY_NATIVE_BIN", &native)
- .env("AI_MEMORY_DOCKER", &docker)
- .env("AI_MEMORY_SERVER_URL", "http://192.168.0.90:49374")
- .env("AI_MEMORY_AUTH_TOKEN", "remote-test-token")
- .env("PATH", &host_path)
- .output()
- .unwrap();
- assert!(
- output.status.success(),
- "wrapper failed for {args:?}: {}",
- String::from_utf8_lossy(&output.stderr)
- );
- let mut expected = format!(
- "server=http://192.168.0.90:49374\n\
- auth=remote-test-token\n\
- path={host_path}\n"
- );
- for arg in *args {
- expected.push_str(&format!("arg={arg}\n"));
- }
- assert_eq!(std::fs::read_to_string(&record).unwrap(), expected);
- }
- assert!(
- !docker_record.exists(),
- "managed host command entered Docker"
- );
-}
-
-#[cfg(unix)]
-#[test]
-fn wrapper_upgrade_does_not_claim_an_updated_remote_server_is_stale() {
- let tmp = tempfile::tempdir().unwrap();
- let docker = tmp.path().join("docker");
- std::fs::write(
- &docker,
- "#!/usr/bin/env bash\n\
- case \"$1\" in\n\
- pull | ps) exit 0 ;;\n\
- *) exit 1 ;;\n\
- esac\n",
- )
- .unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
-
- let output = shell_script_command(&repo_root().join("bin/ai-memory"))
- .arg("upgrade")
- .env("AI_MEMORY_DOCKER", &docker)
- .env("AI_MEMORY_SKIP_SELF_UPGRADE", "1")
- .env("AI_MEMORY_SERVER_URL", "http://192.168.0.90:49374")
- .env("HOME", tmp.path())
- .output()
- .unwrap();
- assert!(
- output.status.success(),
- "wrapper failed: {}",
- String::from_utf8_lossy(&output.stderr)
- );
- let stdout = String::from_utf8_lossy(&output.stdout);
- assert!(stdout.contains("does not\n inspect or redeploy the remote server"));
- assert!(stdout.contains("If that host is not already current"));
- assert!(!stdout.contains("remote server still\n runs the previous version"));
-}
-
-#[cfg(unix)]
-#[test]
-fn docker_wrapper_completions_tolerate_an_early_reader_close() {
- let tmp = tempfile::tempdir().unwrap();
- let docker = tmp.path().join("docker");
- std::fs::write(
- &docker,
- "#!/usr/bin/env bash\n\
- if [ \"$1\" = info ]; then\n\
- printf '[name=seccomp,profile=default]\\n'\n\
- exit 0\n\
- fi\n\
- if [ \"$1\" = run ]; then\n\
- i=0\n\
- while [ \"$i\" -lt 20000 ]; do\n\
- printf 'complete -c ai-memory -n condition-%s\\n' \"$i\"\n\
- i=$((i + 1))\n\
- done\n\
- exit 0\n\
- fi\n\
- exit 1\n",
- )
- .unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
-
- let mut child = shell_script_command(&repo_root().join("bin/ai-memory"))
- .args(["completions", "fish"])
- .env("AI_MEMORY_DOCKER", &docker)
- .env("AI_MEMORY_NO_TTY", "1")
- .env("AI_MEMORY_NO_VERSION_CHECK", "1")
- .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
- .env("HOME", tmp.path())
- .stdout(Stdio::piped())
- .stderr(Stdio::piped())
- .spawn()
- .unwrap();
- let mut stdout = std::io::BufReader::new(child.stdout.take().unwrap());
- let mut first_line = String::new();
- stdout.read_line(&mut first_line).unwrap();
- drop(stdout);
-
- let output = child.wait_with_output().unwrap();
- assert_eq!(first_line, "complete -c ai-memory -n condition-0\n");
- assert!(
- output.status.success(),
- "early close should stay quiet and successful: {}",
- String::from_utf8_lossy(&output.stderr)
- );
- assert!(
- !String::from_utf8_lossy(&output.stderr).contains("broken pipe"),
- "wrapper leaked Docker's broken-pipe diagnostic: {}",
- String::from_utf8_lossy(&output.stderr)
- );
-}
-
-#[cfg(unix)]
-#[test]
-fn docker_wrapper_completions_preserve_helper_failure_without_partial_output() {
- let tmp = tempfile::tempdir().unwrap();
- let docker = tmp.path().join("docker");
- std::fs::write(
- &docker,
- "#!/usr/bin/env bash\n\
- if [ \"$1\" = info ]; then\n\
- printf '[name=seccomp,profile=default]\\n'\n\
- exit 0\n\
- fi\n\
- if [ \"$1\" = run ]; then\n\
- printf 'partial completion output\\n'\n\
- printf 'helper failed\\n' >&2\n\
- exit 42\n\
- fi\n\
- exit 1\n",
- )
- .unwrap();
- {
- use std::os::unix::fs::PermissionsExt as _;
- std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
-
- let output = shell_script_command(&repo_root().join("bin/ai-memory"))
- .args(["completions", "fish"])
- .env("AI_MEMORY_DOCKER", &docker)
- .env("AI_MEMORY_NO_TTY", "1")
- .env("AI_MEMORY_NO_VERSION_CHECK", "1")
- .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
- .env("HOME", tmp.path())
- .output()
- .unwrap();
- assert_eq!(output.status.code(), Some(42));
- assert!(
- output.stdout.is_empty(),
- "failed helper leaked partial completions: {}",
- String::from_utf8_lossy(&output.stdout)
- );
- assert_eq!(String::from_utf8_lossy(&output.stderr), "helper failed\n");
-}
-
-// Unlike run_wrapper_on_fake_macos's docker fake (which only ever sees one
-// meaningful call — the final `docker run`), the rootless-Docker UID check
-// calls `docker info` *before* `docker run`, so this fake must dispatch on
-// $1: real stdout for `info` (read by the wrapper's `grep -q rootless`) vs.
-// logging argv to a file for `run` (read back by the test).
-// How the fake `docker` binary answers `docker info --format …`. The two
-// engines expose the same two facts under incompatible keys, and the wrapper
-// has to read both, so the fake has to be able to impersonate either one.
-#[cfg(unix)]
-enum FakeInfo<'a> {
- // Docker: `{{.SecurityOptions}}` prints the security-options list.
- Docker(&'a str),
- // Podman, including the podman-docker `docker` shim: `.SecurityOptions`
- // is not a field of its info report, so the template fails and the
- // command exits non-zero with nothing on stdout. The equivalent facts
- // live under `.Host.Security.*`.
- Podman { rootless: bool, selinux: bool },
-}
-
-#[cfg(unix)]
-fn run_wrapper_with_fake_docker(args: &[&str], docker_info_stdout: &str) -> String {
- run_wrapper_with_fake_docker_and_uname(args, docker_info_stdout, None)
-}
-
-#[cfg(unix)]
-fn run_wrapper_with_fake_docker_and_claude_config(
- args: &[&str],
- docker_info_stdout: &str,
- claude_config_dir: &str,
-) -> String {
- run_wrapper_with_fake_docker_env(
- args,
- FakeInfo::Docker(docker_info_stdout),
- None,
- Some(claude_config_dir),
- None,
- &[],
- )
-}
-
-#[cfg(unix)]
-fn run_wrapper_with_fake_docker_and_forwarded_env(
- args: &[&str],
- docker_info_stdout: &str,
- forwarded_env: &[(&str, &str)],
-) -> String {
- run_wrapper_with_fake_docker_env(
- args,
- FakeInfo::Docker(docker_info_stdout),
- None,
- None,
- None,
- forwarded_env,
+}
+
+#[cfg(unix)]
+fn run_wrapper_with_fake_docker_and_forwarded_env(
+ args: &[&str],
+ docker_info_stdout: &str,
+ forwarded_env: &[(&str, &str)],
+) -> String {
+ run_wrapper_with_fake_docker_env(
+ args,
+ FakeInfo::Docker(docker_info_stdout),
+ None,
+ None,
+ None,
+ forwarded_env,
)
}
@@ -1310,20 +865,6 @@ fn run_wrapper_with_fake_docker_env(
std::fs::read_to_string(docker_args).unwrap()
}
-#[cfg(unix)]
-#[test]
-fn wrapper_forwards_claude_config_dir_to_helper_container() {
- let args = run_wrapper_with_fake_docker_and_claude_config(
- &["install-hooks", "--agent", "claude-code", "--apply"],
- "[name=seccomp,profile=default]",
- "/home/alice/.config/claude",
- );
- assert!(
- args.contains("-e\nCLAUDE_CONFIG_DIR"),
- "wrapper must forward Claude's config root; got {args}"
- );
-}
-
#[cfg(unix)]
fn run_wrapper_with_fake_rootless_docker_on_fake_macos(args: &[&str]) -> String {
run_wrapper_with_fake_docker_and_uname(
@@ -1333,202 +874,46 @@ fn run_wrapper_with_fake_rootless_docker_on_fake_macos(args: &[&str]) -> String
)
}
-#[cfg(unix)]
#[test]
-fn rootless_docker_uses_root_uid_only_for_host_config_commands() {
- let rootless_info = "[name=apparmor name=seccomp,profile=default name=rootless]";
-
- for subcommand in [
- "install-mcp",
- "install-hooks",
- "setup-agent",
- "install-instructions",
- "install-skills",
- // uninstall edits the same host agent-config files; backup writes
- // its tarball to a host path, and restore reads one — same bind
- // mounts, same UID rule.
- "uninstall",
- "backup",
- "restore",
- ] {
- let args = run_wrapper_with_fake_docker(&[subcommand], rootless_info);
+fn powershell_wrapper_lists_subscription_oauth_tokens_in_its_env_allowlist() {
+ let wrapper = read_repo("bin/ai-memory.ps1");
+ let allowlist = wrapper
+ .split_once("foreach ($Name in @(")
+ .and_then(|(_, rest)| rest.split_once(")) {"))
+ .map(|(allowlist, _)| allowlist)
+ .expect("PowerShell wrapper env allowlist");
+
+ for name in ["ANTHROPIC_OAUTH_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN"] {
assert!(
- args.contains("-u\n0:0"),
- "{subcommand} writes host bind-mounted files and must run as root \
- under rootless Docker so the write lands as the real host user \
- (rootlesskit only maps container UID 0 back to it); got {args}"
+ allowlist
+ .lines()
+ .any(|line| line.trim() == format!("\"{name}\",")),
+ "PowerShell wrapper must list {name} in the helper env allowlist"
);
}
-
- let args = run_wrapper_with_fake_docker(&["status"], rootless_info);
- assert!(
- !args.contains("-u\n0:0"),
- "thin-client commands only touch the /data named volume, which isn't \
- host-visible, so they must keep the host-UID mapping; got {args}"
- );
}
-#[cfg(unix)]
#[test]
-fn fake_macos_rootless_docker_keeps_root_uid_for_host_config_commands() {
- let args = run_wrapper_with_fake_rootless_docker_on_fake_macos(&["install-mcp"]);
- assert!(
- args.contains("-u\n0:0"),
- "macOS rootless Docker still needs uid 0 for host config writes; got {args}"
- );
-
- let args = run_wrapper_with_fake_rootless_docker_on_fake_macos(&["status"]);
- assert!(
- !args.contains("-u\n0:0"),
- "macOS thin-client commands should keep Docker Desktop's default uid; got {args}"
- );
-}
+fn powershell_wrapper_trims_paths_with_single_character_separators() {
+ let wrapper = read_repo("bin/ai-memory.ps1");
-#[cfg(unix)]
-#[test]
-fn fake_macos_rootful_docker_keeps_default_uid_for_host_config_commands() {
- let args = run_wrapper_with_fake_docker_and_uname(
- &["install-mcp"],
- "[name=seccomp,profile=default]",
- Some("Darwin"),
- );
- assert!(
- !args.contains("-u\n0:0") && !args.contains("-u\n"),
- "macOS rootful Docker should keep Docker Desktop's default uid; got {args}"
+ assert_eq!(
+ wrapper.matches(r"TrimEnd([char[]]@('/', '\'))").count(),
+ 2,
+ "PowerShell TrimEnd arguments must contain individual characters"
);
-}
-
-#[cfg(unix)]
-#[test]
-fn rootful_docker_keeps_host_uid_for_host_config_commands() {
- let rootful_info = "[name=seccomp,profile=default]";
-
- let args = run_wrapper_with_fake_docker(&["install-hooks"], rootful_info);
assert!(
- !args.contains("-u\n0:0"),
- "rootful Docker must not switch to root UID — that would write \
- ~/.local/share/ai-memory/hooks owned by root instead of the invoking \
- user; got {args}"
+ !wrapper.contains(r"TrimEnd([char[]]@('/', '\\'))"),
+ "a multi-character backslash string cannot be converted to System.Char"
);
}
-#[cfg(unix)]
-#[test]
-fn selinux_enforcing_disables_labels_only_for_host_file_commands() {
- let selinux_info = "[name=seccomp,profile=default name=selinux name=cgroupns]";
-
- for subcommand in [
- "install-mcp",
- "install-hooks",
- "setup-agent",
- "install-instructions",
- "install-skills",
- "uninstall",
- "backup",
- "restore",
- ] {
- let args = run_wrapper_with_fake_selinux(&[subcommand], selinux_info, "Enforcing");
- assert!(
- args.contains("--security-opt\nlabel=disable"),
- "{subcommand} writes bind-mounted host files and needs the scoped \
- SELinux exception; got {args}"
- );
- }
-
- let args = run_wrapper_with_fake_selinux(&["status"], selinux_info, "Enforcing");
- assert!(
- !args.contains("label=disable"),
- "thin-client commands must retain SELinux label confinement; got {args}"
- );
-}
-
-#[cfg(unix)]
-#[test]
-fn selinux_label_exception_requires_enforcement_and_daemon_support() {
- let selinux_info = "[name=seccomp,profile=default name=selinux name=cgroupns]";
- let args = run_wrapper_with_fake_selinux(&["install-mcp"], selinux_info, "Permissive");
- assert!(
- !args.contains("label=disable"),
- "permissive hosts do not need a label exception; got {args}"
- );
-
- let args = run_wrapper_with_fake_selinux(
- &["install-mcp"],
- "[name=seccomp,profile=default name=cgroupns]",
- "Enforcing",
- );
- assert!(
- !args.contains("label=disable"),
- "a daemon without SELinux support must not receive SELinux options; got {args}"
- );
-}
-
-#[cfg(unix)]
-#[test]
-fn posix_wrapper_forwards_subscription_oauth_tokens_without_putting_values_in_argv() {
- let tokens = [
- ("ANTHROPIC_OAUTH_TOKEN", "oauth-canary-primary"),
- ("CLAUDE_CODE_OAUTH_TOKEN", "oauth-canary-fallback"),
- ];
- let args = run_wrapper_with_fake_docker_and_forwarded_env(
- &["llm-test", "--provider", "anthropic-oauth"],
- "[name=seccomp,profile=default]",
- &tokens,
- );
- let args: Vec<&str> = args.lines().collect();
-
- for (name, value) in tokens {
- assert!(
- args.windows(2).any(|pair| pair == ["-e", name]),
- "wrapper must forward {name} by name; got {args:?}"
- );
- assert!(
- !args.iter().any(|arg| arg.contains(value)),
- "wrapper must not put the value of {name} in Docker argv"
- );
- }
-}
-
-#[test]
-fn powershell_wrapper_lists_subscription_oauth_tokens_in_its_env_allowlist() {
- let wrapper = read_repo("bin/ai-memory.ps1");
- let allowlist = wrapper
- .split_once("foreach ($Name in @(")
- .and_then(|(_, rest)| rest.split_once(")) {"))
- .map(|(allowlist, _)| allowlist)
- .expect("PowerShell wrapper env allowlist");
-
- for name in ["ANTHROPIC_OAUTH_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN"] {
- assert!(
- allowlist
- .lines()
- .any(|line| line.trim() == format!("\"{name}\",")),
- "PowerShell wrapper must list {name} in the helper env allowlist"
- );
- }
-}
-
-#[test]
-fn powershell_wrapper_trims_paths_with_single_character_separators() {
- let wrapper = read_repo("bin/ai-memory.ps1");
-
- assert_eq!(
- wrapper.matches(r"TrimEnd([char[]]@('/', '\'))").count(),
- 2,
- "PowerShell TrimEnd arguments must contain individual characters"
- );
- assert!(
- !wrapper.contains(r"TrimEnd([char[]]@('/', '\\'))"),
- "a multi-character backslash string cannot be converted to System.Char"
- );
-}
-
-// The PowerShell counterpart of run_wrapper_on_fake_macos: a fake `docker.cmd`
-// records the argv the wrapper built. No `uname` shadowing is needed because
-// the Windows arm under test is selected by running on Windows at all.
-#[cfg(windows)]
-fn run_powershell_wrapper(args: &[&str]) -> String {
- run_powershell_wrapper_with_server_url(args, None)
+// The PowerShell counterpart of run_wrapper_on_fake_macos: a fake `docker.cmd`
+// records the argv the wrapper built. No `uname` shadowing is needed because
+// the Windows arm under test is selected by running on Windows at all.
+#[cfg(windows)]
+fn run_powershell_wrapper(args: &[&str]) -> String {
+ run_powershell_wrapper_with_server_url(args, None)
}
#[cfg(windows)]
@@ -1542,7 +927,7 @@ fn run_powershell_wrapper_with_server_url(args: &[&str], server_url: Option<&str
)
.unwrap();
- let mut command = Command::new("powershell.exe");
+ let mut command = Command::new(ai_memory_test_support::powershell_exe());
// See the execution-policy note on the OAuth-token test below.
command
.args([
@@ -1573,263 +958,953 @@ fn run_powershell_wrapper_with_server_url(args: &[&str], server_url: Option<&str
std::fs::read_to_string(docker_args).unwrap()
}
-// The Windows mirror of macos_wrapper_routes_urls_by_real_subcommand: Docker
-// Desktop gives Linux containers no host networking on Windows either, so the
-// helper container cannot reach the host-published server over loopback.
-#[cfg(windows)]
#[test]
-fn windows_wrapper_routes_urls_by_real_subcommand() {
- for subcommand in ["install-mcp", "install-hooks", "setup-agent"] {
- let args = run_powershell_wrapper(&[subcommand]);
- assert!(
- !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "{subcommand} renders host-side config and must keep loopback defaults; got {args}"
- );
- }
-
- let args = run_powershell_wrapper(&["status"]);
- assert!(
- args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "thin-client commands must reach the host server through Docker Desktop; got {args}"
- );
-
- let args = run_powershell_wrapper(&["search", "install-hooks"]);
+fn macos_docs_use_valid_install_commands_and_release_body_points_to_them() {
+ let docs = read_repo("docs/macos.md");
+ assert!(docs.contains("install-hooks --agent claude-code --apply"));
+ assert!(docs.contains("install-mcp --client claude-code --apply"));
assert!(
- args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "only the actual subcommand should control URL routing; got {args}"
+ !docs.contains("setup-agent --agent claude-code --source ./hooks"),
+ "setup-agent has no --apply path; use install-hooks for native macOS docs"
);
-
- let args = run_powershell_wrapper(&["--config", "C:\\tmp\\config.toml", "install-hooks"]);
assert!(
- !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "global options before install-hooks must not hide the real subcommand; got {args}"
+ !docs.contains("init` configures the bearer token"),
+ "init writes token_pepper, not a bearer token"
);
+ assert!(docs.contains("Host-side agent config should use"));
+ assert!(docs.contains("Tagged releases publish a multi-arch manifest"));
- // A homelab/remote server is configured through the environment, and the
- // helper container must not be redirected back at the local Docker host.
- let args =
- run_powershell_wrapper_with_server_url(&["status"], Some("http://192.168.1.50:49374"));
- assert!(
- !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
- "an explicit AI_MEMORY_SERVER_URL must win over the Docker Desktop alias; got {args}"
- );
- assert!(
- args.contains("-e AI_MEMORY_SERVER_URL"),
- "an explicit AI_MEMORY_SERVER_URL must still be forwarded by name; got {args}"
- );
+ let release = read_repo(".github/workflows/release.yml");
+ assert!(release.contains("follow the bundled docs/macos.md"));
}
-#[cfg(windows)]
-#[test]
-fn powershell_wrapper_forwards_subscription_oauth_tokens_without_putting_values_in_argv() {
- let tmp = tempfile::tempdir().unwrap();
- let docker_args = tmp.path().join("docker-args.txt");
- let docker = tmp.path().join("docker.cmd");
- std::fs::write(
- &docker,
- "@echo off\r\n>\"%AI_MEMORY_TEST_DOCKER_ARGS%\" echo %*\r\nexit /b 0\r\n",
- )
- .unwrap();
+/// The slow tier. These drive the real wrapper scripts, a fake `uname`, and
+/// fake container engines, 10-20s each, and cannot break from an ordinary Rust
+/// edit. `.config/nextest.toml` skips every `slow::` module in the everyday
+/// profile; `-P full`, the pre-push hook, and CI run them.
+mod slow {
+ #![allow(unused_imports)]
+ use super::*;
- let tokens = [
- ("ANTHROPIC_OAUTH_TOKEN", "oauth-canary-primary"),
- ("CLAUDE_CODE_OAUTH_TOKEN", "oauth-canary-fallback"),
- ];
- let mut command = Command::new("powershell.exe");
- command
- // -ExecutionPolicy Bypass: `-File` loads a script from disk, and execution policy
- // governs script *files*, so on a machine left at the Windows client default of
- // `Restricted` the unsigned bin/ai-memory.ps1 is refused (`UnauthorizedAccess`) and
- // the wrapper never runs — failing this test for a reason unrelated to what it
- // checks. The other in-repo invocation (render_shared.rs) uses `-Command`, which is
- // not policy-gated and therefore needs no override; the generated hook commands pass
- // Bypass defensively. GitHub's runner is permissive enough that this passes there
- // today, so the fix is for running the suite on a stock Windows install.
- .args([
- "-NoLogo",
- "-NoProfile",
- "-NonInteractive",
- "-ExecutionPolicy",
- "Bypass",
- "-File",
- ])
- .arg(repo_root().join("bin/ai-memory.ps1"))
- .args(["llm-test", "--provider", "anthropic-oauth"])
- .env("AI_MEMORY_DOCKER", &docker)
- .env("AI_MEMORY_TEST_DOCKER_ARGS", &docker_args);
- for (name, value) in tokens {
- command.env(name, value);
- }
- let output = command.output().unwrap();
- assert!(
- output.status.success(),
- "PowerShell wrapper failed: stdout={} stderr={}",
- String::from_utf8_lossy(&output.stdout),
- String::from_utf8_lossy(&output.stderr)
- );
+ #[cfg(unix)]
+ #[test]
+ fn macos_wrapper_routes_urls_by_real_subcommand() {
+ for subcommand in ["install-mcp", "install-hooks", "setup-agent"] {
+ let args = run_wrapper_on_fake_macos(&[subcommand]);
+ assert!(
+ !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "{subcommand} renders host-side config and must keep loopback defaults; got {args}"
+ );
+ }
+
+ let args = run_wrapper_on_fake_macos(&["status"]);
+ assert!(
+ args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "thin-client commands must reach the host server through Docker Desktop; got {args}"
+ );
- let args = std::fs::read_to_string(docker_args).unwrap();
- for (name, value) in tokens {
+ let args = run_wrapper_on_fake_macos(&["search", "install-hooks"]);
assert!(
- args.contains(&format!("-e {name}")),
- "PowerShell wrapper must forward {name} by name; got {args}"
+ args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "only the actual subcommand should control URL routing; got {args}"
);
+
+ let args = run_wrapper_on_fake_macos(&["--config", "/tmp/config.toml", "install-hooks"]);
assert!(
- !args.contains(value),
- "PowerShell wrapper must not put the value of {name} in Docker argv"
+ !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "global options before install-hooks must not hide the real subcommand; got {args}"
);
}
-}
-#[cfg(unix)]
-#[test]
-fn podman_rootless_and_selinux_are_detected_without_the_docker_only_field() {
- // Podman answers nothing for `{{.SecurityOptions}}`, so both gates used to
- // read as "rootful, no SELinux" and every host-file write died with
- // Permission denied. Both adjustments are required: neither alone makes
- // the write land.
- for subcommand in [
- "install-mcp",
- "install-hooks",
- "setup-agent",
- "install-instructions",
- "install-skills",
- "uninstall",
- "backup",
- "restore",
- ] {
- let args = run_wrapper_with_fake_podman(&[subcommand], true, true, "Enforcing");
+ #[cfg(unix)]
+ #[test]
+ fn wrapper_keeps_stdin_attached_when_it_is_a_pipe() {
+ let args = run_wrapper_with_piped_stdin(
+ &["write-page", "--path", "notes/x.md", "--body", "-"],
+ "# body that must survive the container boundary\n",
+ );
+ let flags: Vec<&str> = args.lines().collect();
+
+ // Without `-i` docker gives the container a closed stdin, so `--body -`
+ // reads an empty string and the page is persisted with frontmatter only —
+ // silently, because the CLI still reports a successful write.
assert!(
- args.contains("-u\n0:0"),
- "{subcommand} needs the rootless UID remap under podman too; got {args}"
+ flags.contains(&"-i"),
+ "piped stdin must stay attached for `--body -`; got {args}"
);
+ // A pipe is not a terminal: asking for a TTY here makes docker fail with
+ // "the input device is not a TTY".
assert!(
- args.contains("--security-opt\nlabel=disable"),
- "{subcommand} needs the scoped SELinux exception under podman too; got {args}"
+ !flags.contains(&"-t") && !flags.contains(&"-it"),
+ "no TTY may be requested when stdin is a pipe; got {args}"
+ );
+ assert!(
+ flags
+ .iter()
+ .any(|arg| arg.starts_with("AI_MEMORY_SCOPE_CWD=/scope")),
+ "an outside-home checkout must expose its bounded marker path; got {args}"
+ );
+ assert!(
+ flags.iter().any(|arg| arg.ends_with(":/scope:ro")),
+ "the marker scope mount must be read-only; got {args}"
);
}
- let args = run_wrapper_with_fake_podman(&["status"], true, true, "Enforcing");
- assert!(
- !args.contains("-u\n0:0") && !args.contains("label=disable"),
- "thin-client commands touch only the named volume and must stay \
- confined under podman as well; got {args}"
- );
-}
+ #[cfg(unix)]
+ #[test]
+ fn wrapper_self_upgrade_rejects_a_checksum_mismatch() {
+ let tmp = tempfile::tempdir().unwrap();
+ let bin_dir = tmp.path().join("bin");
+ std::fs::create_dir_all(&bin_dir).unwrap();
+ let wrapper = bin_dir.join("ai-memory");
+ let original = read_repo("bin/ai-memory");
+ std::fs::write(&wrapper, &original).unwrap();
+
+ let payload = tmp.path().join("hostile-wrapper");
+ std::fs::write(
+ &payload,
+ "#!/usr/bin/env bash\nprintf 'hostile payload executed\\n' >&2\nexit 91\n",
+ )
+ .unwrap();
+ let curl = bin_dir.join("curl");
+ std::fs::write(
+ &curl,
+ "#!/usr/bin/env bash\n\
+ set -euo pipefail\n\
+ url=''\n\
+ out=''\n\
+ while [ \"$#\" -gt 0 ]; do\n\
+ case \"$1\" in\n\
+ -o) out=\"$2\"; shift 2 ;;\n\
+ -*) shift ;;\n\
+ *) url=\"$1\"; shift ;;\n\
+ esac\n\
+ done\n\
+ case \"$url\" in\n\
+ *.sha256) printf '%064d ai-memory-wrapper\\n' 0 > \"$out\" ;;\n\
+ *) cp \"$FAKE_WRAPPER_PAYLOAD\" \"$out\" ;;\n\
+ esac\n",
+ )
+ .unwrap();
+ let docker = bin_dir.join("docker");
+ std::fs::write(&docker, "#!/usr/bin/env bash\nexit 0\n").unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ for path in [&wrapper, &payload, &curl, &docker] {
+ std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+ }
-#[cfg(unix)]
-#[test]
-fn podman_gates_still_respect_engine_and_host_state() {
- // The fallback reports what podman reports; it must not hard-code "yes".
- let args = run_wrapper_with_fake_podman(&["install-mcp"], false, true, "Enforcing");
- assert!(
- !args.contains("-u\n0:0"),
- "rootful podman maps the host UID directly and must keep it; got {args}"
- );
+ let path = format!(
+ "{}:{}",
+ shell_path(&bin_dir),
+ std::env::var("PATH").unwrap_or_default()
+ );
+ let output = freshly_written_shell_script_command(&wrapper)
+ .arg("upgrade")
+ .env("PATH", path)
+ .env("HOME", tmp.path())
+ .env("AI_MEMORY_DOCKER", &docker)
+ .env(
+ "AI_MEMORY_WRAPPER_URL",
+ "https://example.invalid/ai-memory-wrapper",
+ )
+ .env("FAKE_WRAPPER_PAYLOAD", &payload)
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "upgrade failed: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(stdout.contains("wrapper checksum mismatch; refusing update"));
+ assert!(!String::from_utf8_lossy(&output.stderr).contains("hostile payload executed"));
+ assert_eq!(std::fs::read_to_string(&wrapper).unwrap(), original);
+ }
- let args = run_wrapper_with_fake_podman(&["install-mcp"], true, false, "Enforcing");
- assert!(
- !args.contains("label=disable"),
- "an engine without SELinux support must not receive SELinux options; got {args}"
- );
+ #[cfg(unix)]
+ #[test]
+ fn wrapper_self_upgrade_installs_and_runs_a_verified_payload() {
+ let tmp = tempfile::tempdir().unwrap();
+ let bin_dir = tmp.path().join("bin");
+ std::fs::create_dir_all(&bin_dir).unwrap();
+ let wrapper = bin_dir.join("ai-memory");
+ std::fs::write(&wrapper, read_repo("bin/ai-memory")).unwrap();
+
+ let payload = tmp.path().join("verified-wrapper");
+ let payload_body = "#!/usr/bin/env bash\nprintf 'verified wrapper executed\\n'\n";
+ std::fs::write(&payload, payload_body).unwrap();
+ let curl = bin_dir.join("curl");
+ std::fs::write(
+ &curl,
+ "#!/usr/bin/env bash\n\
+ set -euo pipefail\n\
+ url=''\n\
+ out=''\n\
+ while [ \"$#\" -gt 0 ]; do\n\
+ case \"$1\" in\n\
+ -o) out=\"$2\"; shift 2 ;;\n\
+ -*) shift ;;\n\
+ *) url=\"$1\"; shift ;;\n\
+ esac\n\
+ done\n\
+ case \"$url\" in\n\
+ *.sha256) printf '%s ai-memory-wrapper\\n' \"$FAKE_WRAPPER_CHECKSUM\" > \"$out\" ;;\n\
+ *) cp \"$FAKE_WRAPPER_PAYLOAD\" \"$out\" ;;\n\
+ esac\n",
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ for path in [&wrapper, &payload, &curl] {
+ std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+ }
- let args = run_wrapper_with_fake_podman(&["install-mcp"], true, true, "Permissive");
- assert!(
- !args.contains("label=disable"),
- "permissive hosts do not need a label exception; got {args}"
- );
-}
+ let path = format!(
+ "{}:{}",
+ shell_path(&bin_dir),
+ std::env::var("PATH").unwrap_or_default()
+ );
+ let output = freshly_written_shell_script_command(&wrapper)
+ .arg("upgrade")
+ .env("PATH", path)
+ .env("HOME", tmp.path())
+ .env(
+ "AI_MEMORY_WRAPPER_URL",
+ "https://example.invalid/ai-memory-wrapper",
+ )
+ .env("FAKE_WRAPPER_PAYLOAD", &payload)
+ .env("FAKE_WRAPPER_CHECKSUM", sha256_file(&payload))
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "upgrade failed: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert!(String::from_utf8_lossy(&output.stdout).contains("verified wrapper executed"));
+ assert_eq!(std::fs::read_to_string(&wrapper).unwrap(), payload_body);
+ }
-#[cfg(unix)]
-#[test]
-fn bootstrap_gets_host_file_treatment_because_it_reads_the_repo() {
- // bootstrap only reads host files, but an unmapped UID blocks reads just
- // as hard: it degrades silently to "no .git found at /work" and then dies
- // with Permission denied. Same gates as the writers, on both engines.
- let args = run_wrapper_with_fake_podman(&["bootstrap"], true, true, "Enforcing");
- assert!(
- args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
- "bootstrap reads the repo bind-mounted at /work and needs both \
- adjustments; got {args}"
- );
+ #[cfg(unix)]
+ #[test]
+ fn hook_installer_rejects_a_checksum_mismatch_before_writing_scripts() {
+ let tmp = tempfile::tempdir().unwrap();
+ let bin_dir = tmp.path().join("bin");
+ std::fs::create_dir_all(&bin_dir).unwrap();
+ let curl = bin_dir.join("curl");
+ std::fs::write(
+ &curl,
+ "#!/usr/bin/env bash\n\
+ set -euo pipefail\n\
+ url=''\n\
+ out=''\n\
+ while [ \"$#\" -gt 0 ]; do\n\
+ case \"$1\" in\n\
+ -o) out=\"$2\"; shift 2 ;;\n\
+ -*) shift ;;\n\
+ *) url=\"$1\"; shift ;;\n\
+ esac\n\
+ done\n\
+ case \"$url\" in\n\
+ *.sha256) printf '%064d ai-memory-hooks.tar.gz\\n' 0 > \"$out\" ;;\n\
+ *) printf 'not the expected archive' > \"$out\" ;;\n\
+ esac\n",
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ std::fs::set_permissions(&curl, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+ let path = format!(
+ "{}:{}",
+ shell_path(&bin_dir),
+ std::env::var("PATH").unwrap_or_default()
+ );
+ let destination = tmp.path().join("hooks");
+ let output = shell_script_command(&repo_root().join("scripts/install-hooks.sh"))
+ .args(["--agent", "claude-code", "--to"])
+ .arg(&destination)
+ .env("PATH", path)
+ .env("HOME", tmp.path())
+ .output()
+ .unwrap();
+ assert!(
+ !output.status.success(),
+ "checksum mismatch must fail closed"
+ );
+ assert!(
+ String::from_utf8_lossy(&output.stderr)
+ .contains("hook bundle checksum mismatch; refusing installation")
+ );
+ let agent_dir = destination.join("claude-code");
+ assert!(
+ !agent_dir.exists() || std::fs::read_dir(agent_dir).unwrap().next().is_none(),
+ "no hook script may be written before archive verification"
+ );
+ }
- let args = run_wrapper_with_fake_selinux(
- &["bootstrap"],
- "[name=seccomp,profile=default name=selinux name=cgroupns]",
- "Enforcing",
- );
- assert!(
- args.contains("--security-opt\nlabel=disable"),
- "the same read applies under Docker on an SELinux host; got {args}"
- );
-}
+ #[cfg(unix)]
+ #[test]
+ fn hook_installer_writes_only_expected_files_from_a_verified_archive() {
+ const HOOKS: &[&str] = &[
+ "post-tool-use",
+ "pre-compact",
+ "pre-tool-use",
+ "session-end",
+ "session-start",
+ "stop",
+ "user-prompt-submit",
+ ];
+
+ let names = installed_hook_names("claude-code", "claude-code", HOOKS);
+ let expected = HOOKS
+ .iter()
+ .map(|hook| format!("{hook}.sh"))
+ .collect::<Vec<_>>();
+ assert_eq!(names, expected);
+ }
-#[cfg(unix)]
-#[test]
-fn explicit_config_gets_host_file_treatment() {
- let args = run_wrapper_with_fake_podman(
- &["--config", "/tmp/config.toml", "status"],
- true,
- true,
- "Enforcing",
- );
- assert!(
- args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
- "an explicit config is read through a host bind and needs both adjustments; got {args}"
- );
+ #[cfg(unix)]
+ #[test]
+ fn hook_installer_writes_only_command_code_stable_events() {
+ const HOOKS: &[&str] = &["post-tool-use", "pre-tool-use", "session-start", "stop"];
- let args = run_wrapper_with_fake_podman(
- &["--config=/tmp/config.toml", "status"],
- true,
- true,
- "Enforcing",
- );
- assert!(
- args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
- "the equals form of --config must receive the same treatment; got {args}"
- );
-}
+ let names = installed_hook_names("cmdc", "command-code", HOOKS);
+ let expected = HOOKS
+ .iter()
+ .map(|hook| format!("{hook}.sh"))
+ .collect::<Vec<_>>();
+ assert_eq!(names, expected);
+ }
-#[cfg(unix)]
-#[test]
-fn custom_data_dir_makes_thin_commands_touch_host_files() {
- let args = run_wrapper_with_fake_docker_env(
- &["status"],
- FakeInfo::Podman {
- rootless: true,
- selinux: true,
- },
- Some("Linux"),
- None,
- Some("Enforcing"),
- &[("AI_MEMORY_DATA_DIR", "/tmp")],
- );
- assert!(
- args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
- "a thin command backed by a host data directory needs both adjustments; got {args}"
- );
- assert!(
- args.contains("/tmp:/data"),
- "the custom data directory must remain the /data bind; got {args}"
- );
-}
+ #[cfg(unix)]
+ #[test]
+ fn managed_host_commands_use_native_path_and_remote_server_without_docker() {
+ let tmp = tempfile::tempdir().unwrap();
+ let native = tmp.path().join("native-ai-memory");
+ let docker = tmp.path().join("docker");
+ let record = tmp.path().join("native-record.txt");
+ let docker_record = tmp.path().join("docker-record.txt");
+ std::fs::write(
+ &native,
+ format!(
+ "#!/usr/bin/env bash\n\
+ printf 'server=%s\\nauth=%s\\npath=%s\\n' \"$AI_MEMORY_SERVER_URL\" \"$AI_MEMORY_AUTH_TOKEN\" \"$PATH\" > {}\n\
+ printf 'arg=%s\\n' \"$@\" >> {}\n",
+ shell_path(&record),
+ shell_path(&record)
+ ),
+ )
+ .unwrap();
+ std::fs::write(
+ &docker,
+ format!(
+ "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\nexit 99\n",
+ shell_path(&docker_record)
+ ),
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o755)).unwrap();
+ std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+ let host_path = format!(
+ "{}:{}",
+ shell_path(tmp.path()),
+ std::env::var("PATH").unwrap_or_default()
+ );
+ let commands: &[&[&str]] = &[
+ &["run", "codex", "--yolo", "resume"],
+ &["show", "--json", "--no-scan"],
+ &["continue", "--workspace", "work", "--yolo"],
+ &["resume", "--workspace", "work", "--limit", "5"],
+ &["workstreams", "--limit", "5", "--json"],
+ &["rename-workstream", "--from", "old", "--to", "new"],
+ ];
+ for args in commands {
+ let output = shell_script_command(&repo_root().join("bin/ai-memory"))
+ .args(args.iter().copied())
+ .env("AI_MEMORY_NATIVE_BIN", &native)
+ .env("AI_MEMORY_DOCKER", &docker)
+ .env("AI_MEMORY_SERVER_URL", "http://192.168.0.90:49374")
+ .env("AI_MEMORY_AUTH_TOKEN", "remote-test-token")
+ .env("PATH", &host_path)
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "wrapper failed for {args:?}: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let mut expected = format!(
+ "server=http://192.168.0.90:49374\n\
+ auth=remote-test-token\n\
+ path={host_path}\n"
+ );
+ for arg in *args {
+ expected.push_str(&format!("arg={arg}\n"));
+ }
+ assert_eq!(std::fs::read_to_string(&record).unwrap(), expected);
+ }
+ assert!(
+ !docker_record.exists(),
+ "managed host command entered Docker"
+ );
+ }
-#[test]
-fn macos_docs_use_valid_install_commands_and_release_body_points_to_them() {
- let docs = read_repo("docs/macos.md");
- assert!(docs.contains("install-hooks --agent claude-code --apply"));
- assert!(docs.contains("install-mcp --client claude-code --apply"));
- assert!(
- !docs.contains("setup-agent --agent claude-code --source ./hooks"),
- "setup-agent has no --apply path; use install-hooks for native macOS docs"
- );
- assert!(
- !docs.contains("init` configures the bearer token"),
- "init writes token_pepper, not a bearer token"
- );
- assert!(docs.contains("Host-side agent config should use"));
- assert!(docs.contains("Tagged releases publish a multi-arch manifest"));
+ #[cfg(unix)]
+ #[test]
+ fn wrapper_upgrade_does_not_claim_an_updated_remote_server_is_stale() {
+ let tmp = tempfile::tempdir().unwrap();
+ let docker = tmp.path().join("docker");
+ std::fs::write(
+ &docker,
+ "#!/usr/bin/env bash\n\
+ case \"$1\" in\n\
+ pull | ps) exit 0 ;;\n\
+ *) exit 1 ;;\n\
+ esac\n",
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
- let release = read_repo(".github/workflows/release.yml");
- assert!(release.contains("follow the bundled docs/macos.md"));
+ let output = shell_script_command(&repo_root().join("bin/ai-memory"))
+ .arg("upgrade")
+ .env("AI_MEMORY_DOCKER", &docker)
+ .env("AI_MEMORY_SKIP_SELF_UPGRADE", "1")
+ .env("AI_MEMORY_SERVER_URL", "http://192.168.0.90:49374")
+ .env("HOME", tmp.path())
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "wrapper failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(stdout.contains("does not\n inspect or redeploy the remote server"));
+ assert!(stdout.contains("If that host is not already current"));
+ assert!(!stdout.contains("remote server still\n runs the previous version"));
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn docker_wrapper_completions_tolerate_an_early_reader_close() {
+ let tmp = tempfile::tempdir().unwrap();
+ let docker = tmp.path().join("docker");
+ std::fs::write(
+ &docker,
+ "#!/usr/bin/env bash\n\
+ if [ \"$1\" = info ]; then\n\
+ printf '[name=seccomp,profile=default]\\n'\n\
+ exit 0\n\
+ fi\n\
+ if [ \"$1\" = run ]; then\n\
+ i=0\n\
+ while [ \"$i\" -lt 20000 ]; do\n\
+ printf 'complete -c ai-memory -n condition-%s\\n' \"$i\"\n\
+ i=$((i + 1))\n\
+ done\n\
+ exit 0\n\
+ fi\n\
+ exit 1\n",
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+
+ let mut child = shell_script_command(&repo_root().join("bin/ai-memory"))
+ .args(["completions", "fish"])
+ .env("AI_MEMORY_DOCKER", &docker)
+ .env("AI_MEMORY_NO_TTY", "1")
+ .env("AI_MEMORY_NO_VERSION_CHECK", "1")
+ .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
+ .env("HOME", tmp.path())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()
+ .unwrap();
+ let mut stdout = std::io::BufReader::new(child.stdout.take().unwrap());
+ let mut first_line = String::new();
+ stdout.read_line(&mut first_line).unwrap();
+ drop(stdout);
+
+ let output = child.wait_with_output().unwrap();
+ assert_eq!(first_line, "complete -c ai-memory -n condition-0\n");
+ assert!(
+ output.status.success(),
+ "early close should stay quiet and successful: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert!(
+ !String::from_utf8_lossy(&output.stderr).contains("broken pipe"),
+ "wrapper leaked Docker's broken-pipe diagnostic: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn docker_wrapper_completions_preserve_helper_failure_without_partial_output() {
+ let tmp = tempfile::tempdir().unwrap();
+ let docker = tmp.path().join("docker");
+ std::fs::write(
+ &docker,
+ "#!/usr/bin/env bash\n\
+ if [ \"$1\" = info ]; then\n\
+ printf '[name=seccomp,profile=default]\\n'\n\
+ exit 0\n\
+ fi\n\
+ if [ \"$1\" = run ]; then\n\
+ printf 'partial completion output\\n'\n\
+ printf 'helper failed\\n' >&2\n\
+ exit 42\n\
+ fi\n\
+ exit 1\n",
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+
+ let output = shell_script_command(&repo_root().join("bin/ai-memory"))
+ .args(["completions", "fish"])
+ .env("AI_MEMORY_DOCKER", &docker)
+ .env("AI_MEMORY_NO_TTY", "1")
+ .env("AI_MEMORY_NO_VERSION_CHECK", "1")
+ .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
+ .env("HOME", tmp.path())
+ .output()
+ .unwrap();
+ assert_eq!(output.status.code(), Some(42));
+ assert!(
+ output.stdout.is_empty(),
+ "failed helper leaked partial completions: {}",
+ String::from_utf8_lossy(&output.stdout)
+ );
+ assert_eq!(String::from_utf8_lossy(&output.stderr), "helper failed\n");
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn wrapper_forwards_claude_config_dir_to_helper_container() {
+ let args = run_wrapper_with_fake_docker_and_claude_config(
+ &["install-hooks", "--agent", "claude-code", "--apply"],
+ "[name=seccomp,profile=default]",
+ "/home/alice/.config/claude",
+ );
+ assert!(
+ args.contains("-e\nCLAUDE_CONFIG_DIR"),
+ "wrapper must forward Claude's config root; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn rootless_docker_uses_root_uid_only_for_host_config_commands() {
+ let rootless_info = "[name=apparmor name=seccomp,profile=default name=rootless]";
+
+ for subcommand in [
+ "install-mcp",
+ "install-hooks",
+ "setup-agent",
+ "install-instructions",
+ "install-skills",
+ // uninstall edits the same host agent-config files; backup writes
+ // its tarball to a host path, and restore reads one — same bind
+ // mounts, same UID rule.
+ "uninstall",
+ "backup",
+ "restore",
+ ] {
+ let args = run_wrapper_with_fake_docker(&[subcommand], rootless_info);
+ assert!(
+ args.contains("-u\n0:0"),
+ "{subcommand} writes host bind-mounted files and must run as root \
+ under rootless Docker so the write lands as the real host user \
+ (rootlesskit only maps container UID 0 back to it); got {args}"
+ );
+ }
+
+ let args = run_wrapper_with_fake_docker(&["status"], rootless_info);
+ assert!(
+ !args.contains("-u\n0:0"),
+ "thin-client commands only touch the /data named volume, which isn't \
+ host-visible, so they must keep the host-UID mapping; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn fake_macos_rootless_docker_keeps_root_uid_for_host_config_commands() {
+ let args = run_wrapper_with_fake_rootless_docker_on_fake_macos(&["install-mcp"]);
+ assert!(
+ args.contains("-u\n0:0"),
+ "macOS rootless Docker still needs uid 0 for host config writes; got {args}"
+ );
+
+ let args = run_wrapper_with_fake_rootless_docker_on_fake_macos(&["status"]);
+ assert!(
+ !args.contains("-u\n0:0"),
+ "macOS thin-client commands should keep Docker Desktop's default uid; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn fake_macos_rootful_docker_keeps_default_uid_for_host_config_commands() {
+ let args = run_wrapper_with_fake_docker_and_uname(
+ &["install-mcp"],
+ "[name=seccomp,profile=default]",
+ Some("Darwin"),
+ );
+ assert!(
+ !args.contains("-u\n0:0") && !args.contains("-u\n"),
+ "macOS rootful Docker should keep Docker Desktop's default uid; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn rootful_docker_keeps_host_uid_for_host_config_commands() {
+ let rootful_info = "[name=seccomp,profile=default]";
+
+ let args = run_wrapper_with_fake_docker(&["install-hooks"], rootful_info);
+ assert!(
+ !args.contains("-u\n0:0"),
+ "rootful Docker must not switch to root UID — that would write \
+ ~/.local/share/ai-memory/hooks owned by root instead of the invoking \
+ user; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn selinux_enforcing_disables_labels_only_for_host_file_commands() {
+ let selinux_info = "[name=seccomp,profile=default name=selinux name=cgroupns]";
+
+ for subcommand in [
+ "install-mcp",
+ "install-hooks",
+ "setup-agent",
+ "install-instructions",
+ "install-skills",
+ "uninstall",
+ "backup",
+ "restore",
+ ] {
+ let args = run_wrapper_with_fake_selinux(&[subcommand], selinux_info, "Enforcing");
+ assert!(
+ args.contains("--security-opt\nlabel=disable"),
+ "{subcommand} writes bind-mounted host files and needs the scoped \
+ SELinux exception; got {args}"
+ );
+ }
+
+ let args = run_wrapper_with_fake_selinux(&["status"], selinux_info, "Enforcing");
+ assert!(
+ !args.contains("label=disable"),
+ "thin-client commands must retain SELinux label confinement; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn selinux_label_exception_requires_enforcement_and_daemon_support() {
+ let selinux_info = "[name=seccomp,profile=default name=selinux name=cgroupns]";
+ let args = run_wrapper_with_fake_selinux(&["install-mcp"], selinux_info, "Permissive");
+ assert!(
+ !args.contains("label=disable"),
+ "permissive hosts do not need a label exception; got {args}"
+ );
+
+ let args = run_wrapper_with_fake_selinux(
+ &["install-mcp"],
+ "[name=seccomp,profile=default name=cgroupns]",
+ "Enforcing",
+ );
+ assert!(
+ !args.contains("label=disable"),
+ "a daemon without SELinux support must not receive SELinux options; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn posix_wrapper_forwards_subscription_oauth_tokens_without_putting_values_in_argv() {
+ let tokens = [
+ ("ANTHROPIC_OAUTH_TOKEN", "oauth-canary-primary"),
+ ("CLAUDE_CODE_OAUTH_TOKEN", "oauth-canary-fallback"),
+ ];
+ let args = run_wrapper_with_fake_docker_and_forwarded_env(
+ &["llm-test", "--provider", "anthropic-oauth"],
+ "[name=seccomp,profile=default]",
+ &tokens,
+ );
+ let args: Vec<&str> = args.lines().collect();
+
+ for (name, value) in tokens {
+ assert!(
+ args.windows(2).any(|pair| pair == ["-e", name]),
+ "wrapper must forward {name} by name; got {args:?}"
+ );
+ assert!(
+ !args.iter().any(|arg| arg.contains(value)),
+ "wrapper must not put the value of {name} in Docker argv"
+ );
+ }
+ }
+
+ // The Windows mirror of macos_wrapper_routes_urls_by_real_subcommand: Docker
+ // Desktop gives Linux containers no host networking on Windows either, so the
+ // helper container cannot reach the host-published server over loopback.
+ #[cfg(windows)]
+ #[test]
+ fn windows_wrapper_routes_urls_by_real_subcommand() {
+ for subcommand in ["install-mcp", "install-hooks", "setup-agent"] {
+ let args = run_powershell_wrapper(&[subcommand]);
+ assert!(
+ !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "{subcommand} renders host-side config and must keep loopback defaults; got {args}"
+ );
+ }
+
+ let args = run_powershell_wrapper(&["status"]);
+ assert!(
+ args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "thin-client commands must reach the host server through Docker Desktop; got {args}"
+ );
+
+ let args = run_powershell_wrapper(&["search", "install-hooks"]);
+ assert!(
+ args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "only the actual subcommand should control URL routing; got {args}"
+ );
+
+ let args = run_powershell_wrapper(&["--config", "C:\\tmp\\config.toml", "install-hooks"]);
+ assert!(
+ !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "global options before install-hooks must not hide the real subcommand; got {args}"
+ );
+
+ // A homelab/remote server is configured through the environment, and the
+ // helper container must not be redirected back at the local Docker host.
+ let args =
+ run_powershell_wrapper_with_server_url(&["status"], Some("http://192.168.1.50:49374"));
+ assert!(
+ !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
+ "an explicit AI_MEMORY_SERVER_URL must win over the Docker Desktop alias; got {args}"
+ );
+ assert!(
+ args.contains("-e AI_MEMORY_SERVER_URL"),
+ "an explicit AI_MEMORY_SERVER_URL must still be forwarded by name; got {args}"
+ );
+ }
+
+ #[cfg(windows)]
+ #[test]
+ fn powershell_wrapper_forwards_subscription_oauth_tokens_without_putting_values_in_argv() {
+ let tmp = tempfile::tempdir().unwrap();
+ let docker_args = tmp.path().join("docker-args.txt");
+ let docker = tmp.path().join("docker.cmd");
+ std::fs::write(
+ &docker,
+ "@echo off\r\n>\"%AI_MEMORY_TEST_DOCKER_ARGS%\" echo %*\r\nexit /b 0\r\n",
+ )
+ .unwrap();
+
+ let tokens = [
+ ("ANTHROPIC_OAUTH_TOKEN", "oauth-canary-primary"),
+ ("CLAUDE_CODE_OAUTH_TOKEN", "oauth-canary-fallback"),
+ ];
+ let mut command = Command::new(ai_memory_test_support::powershell_exe());
+ command
+ // -ExecutionPolicy Bypass: `-File` loads a script from disk, and execution policy
+ // governs script *files*, so on a machine left at the Windows client default of
+ // `Restricted` the unsigned bin/ai-memory.ps1 is refused (`UnauthorizedAccess`) and
+ // the wrapper never runs — failing this test for a reason unrelated to what it
+ // checks. The other in-repo invocation (render_shared.rs) uses `-Command`, which is
+ // not policy-gated and therefore needs no override; the generated hook commands pass
+ // Bypass defensively. GitHub's runner is permissive enough that this passes there
+ // today, so the fix is for running the suite on a stock Windows install.
+ .args([
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-ExecutionPolicy",
+ "Bypass",
+ "-File",
+ ])
+ .arg(repo_root().join("bin/ai-memory.ps1"))
+ .args(["llm-test", "--provider", "anthropic-oauth"])
+ .env("AI_MEMORY_DOCKER", &docker)
+ .env("AI_MEMORY_TEST_DOCKER_ARGS", &docker_args);
+ for (name, value) in tokens {
+ command.env(name, value);
+ }
+ let output = command.output().unwrap();
+ assert!(
+ output.status.success(),
+ "PowerShell wrapper failed: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ let args = std::fs::read_to_string(docker_args).unwrap();
+ for (name, value) in tokens {
+ assert!(
+ args.contains(&format!("-e {name}")),
+ "PowerShell wrapper must forward {name} by name; got {args}"
+ );
+ assert!(
+ !args.contains(value),
+ "PowerShell wrapper must not put the value of {name} in Docker argv"
+ );
+ }
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn podman_rootless_and_selinux_are_detected_without_the_docker_only_field() {
+ // Podman answers nothing for `{{.SecurityOptions}}`, so both gates used to
+ // read as "rootful, no SELinux" and every host-file write died with
+ // Permission denied. Both adjustments are required: neither alone makes
+ // the write land.
+ for subcommand in [
+ "install-mcp",
+ "install-hooks",
+ "setup-agent",
+ "install-instructions",
+ "install-skills",
+ "uninstall",
+ "backup",
+ "restore",
+ ] {
+ let args = run_wrapper_with_fake_podman(&[subcommand], true, true, "Enforcing");
+ assert!(
+ args.contains("-u\n0:0"),
+ "{subcommand} needs the rootless UID remap under podman too; got {args}"
+ );
+ assert!(
+ args.contains("--security-opt\nlabel=disable"),
+ "{subcommand} needs the scoped SELinux exception under podman too; got {args}"
+ );
+ }
+
+ let args = run_wrapper_with_fake_podman(&["status"], true, true, "Enforcing");
+ assert!(
+ !args.contains("-u\n0:0") && !args.contains("label=disable"),
+ "thin-client commands touch only the named volume and must stay \
+ confined under podman as well; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn podman_gates_still_respect_engine_and_host_state() {
+ // The fallback reports what podman reports; it must not hard-code "yes".
+ let args = run_wrapper_with_fake_podman(&["install-mcp"], false, true, "Enforcing");
+ assert!(
+ !args.contains("-u\n0:0"),
+ "rootful podman maps the host UID directly and must keep it; got {args}"
+ );
+
+ let args = run_wrapper_with_fake_podman(&["install-mcp"], true, false, "Enforcing");
+ assert!(
+ !args.contains("label=disable"),
+ "an engine without SELinux support must not receive SELinux options; got {args}"
+ );
+
+ let args = run_wrapper_with_fake_podman(&["install-mcp"], true, true, "Permissive");
+ assert!(
+ !args.contains("label=disable"),
+ "permissive hosts do not need a label exception; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn bootstrap_gets_host_file_treatment_because_it_reads_the_repo() {
+ // bootstrap only reads host files, but an unmapped UID blocks reads just
+ // as hard: it degrades silently to "no .git found at /work" and then dies
+ // with Permission denied. Same gates as the writers, on both engines.
+ let args = run_wrapper_with_fake_podman(&["bootstrap"], true, true, "Enforcing");
+ assert!(
+ args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
+ "bootstrap reads the repo bind-mounted at /work and needs both \
+ adjustments; got {args}"
+ );
+
+ let args = run_wrapper_with_fake_selinux(
+ &["bootstrap"],
+ "[name=seccomp,profile=default name=selinux name=cgroupns]",
+ "Enforcing",
+ );
+ assert!(
+ args.contains("--security-opt\nlabel=disable"),
+ "the same read applies under Docker on an SELinux host; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn explicit_config_gets_host_file_treatment() {
+ let args = run_wrapper_with_fake_podman(
+ &["--config", "/tmp/config.toml", "status"],
+ true,
+ true,
+ "Enforcing",
+ );
+ assert!(
+ args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
+ "an explicit config is read through a host bind and needs both adjustments; got {args}"
+ );
+
+ let args = run_wrapper_with_fake_podman(
+ &["--config=/tmp/config.toml", "status"],
+ true,
+ true,
+ "Enforcing",
+ );
+ assert!(
+ args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
+ "the equals form of --config must receive the same treatment; got {args}"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn custom_data_dir_makes_thin_commands_touch_host_files() {
+ let args = run_wrapper_with_fake_docker_env(
+ &["status"],
+ FakeInfo::Podman {
+ rootless: true,
+ selinux: true,
+ },
+ Some("Linux"),
+ None,
+ Some("Enforcing"),
+ &[("AI_MEMORY_DATA_DIR", "/tmp")],
+ );
+ assert!(
+ args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
+ "a thin command backed by a host data directory needs both adjustments; got {args}"
+ );
+ assert!(
+ args.contains("/tmp:/data"),
+ "the custom data directory must remain the /data bind; got {args}"
+ );
+ }
}
diff --git a/crates/ai-memory-cli/tests/removal.rs b/crates/ai-memory-cli/tests/suite/removal.rs
similarity index 100%
rename from crates/ai-memory-cli/tests/removal.rs
rename to crates/ai-memory-cli/tests/suite/removal.rs
diff --git a/crates/ai-memory-cli/tests/suite/repo_layout.rs b/crates/ai-memory-cli/tests/suite/repo_layout.rs
new file mode 100644
index 00000000..04d397fa
--- /dev/null
+++ b/crates/ai-memory-cli/tests/suite/repo_layout.rs
@@ -0,0 +1,100 @@
+//! Repository layout rules that cargo does not enforce on its own.
+
+use std::fs;
+use std::path::{Path, PathBuf};
+
+fn crates_dir() -> PathBuf {
+ Path::new(env!("CARGO_MANIFEST_DIR"))
+ .parent()
+ .expect("crate lives under crates/")
+ .to_path_buf()
+}
+
+/// Every test binary is a link and, on macOS and Windows, a first-run malware
+/// scan, so each crate gets at most one. Integration tests live in
+/// `tests/suite/` and are compiled either into the lib's own harness (entry
+/// `mod.rs`, included from `src/lib.rs`) or, only where they must drive the
+/// built executable, into a single `suite` target (entry `main.rs`). Cargo
+/// compiles a sibling file only if the entry declares it, so an undeclared
+/// file's tests silently never run, and a top-level `tests/*.rs` quietly
+/// becomes a binary of its own.
+#[test]
+fn integration_tests_cost_at_most_one_binary_per_crate() {
+ let mut problems = Vec::new();
+ let crate_dirs = fs::read_dir(crates_dir())
+ .expect("read crates/")
+ .flatten()
+ .map(|entry| entry.path())
+ .filter(|path| path.is_dir());
+ for crate_dir in crate_dirs {
+ let tests_dir = crate_dir.join("tests");
+ if !tests_dir.is_dir() {
+ continue;
+ }
+ for entry in fs::read_dir(&tests_dir).expect("read tests dir").flatten() {
+ let path = entry.path();
+ if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") {
+ problems.push(format!(
+ "{} would build as its own test binary; move it into tests/suite/ and declare it there",
+ path.display()
+ ));
+ }
+ }
+
+ let suite_dir = tests_dir.join("suite");
+ if !suite_dir.is_dir() {
+ continue;
+ }
+ let main = suite_dir.join("main.rs");
+ let module = suite_dir.join("mod.rs");
+ let entry = match (main.is_file(), module.is_file()) {
+ (true, false) => main,
+ (false, true) => {
+ let lib = fs::read_to_string(crate_dir.join("src/lib.rs")).unwrap_or_default();
+ if !lib.contains("#[path = \"../tests/suite/mod.rs\"]") {
+ problems.push(format!(
+ "{} exists but src/lib.rs never includes it, so none of its tests run",
+ module.display()
+ ));
+ }
+ module
+ }
+ (true, true) => {
+ problems.push(format!(
+ "{} has both main.rs and mod.rs; pick one entry",
+ suite_dir.display()
+ ));
+ continue;
+ }
+ (false, false) => {
+ problems.push(format!(
+ "{} has no main.rs or mod.rs entry",
+ suite_dir.display()
+ ));
+ continue;
+ }
+ };
+ let declared = fs::read_to_string(&entry).expect("read suite entry");
+ for sibling in fs::read_dir(&suite_dir).expect("read suite dir").flatten() {
+ let path = sibling.path();
+ if path.extension().is_none_or(|ext| ext != "rs") {
+ continue;
+ }
+ let stem = path.file_stem().unwrap_or_default().to_string_lossy();
+ if stem == "main" || stem == "mod" {
+ continue;
+ }
+ let is_declared = declared
+ .lines()
+ .any(|line| line == format!("mod {stem};") || line == format!("pub mod {stem};"));
+ if !is_declared {
+ problems.push(format!(
+ "{} is not declared in {} (add `mod {stem};`)",
+ path.display(),
+ entry.display()
+ ));
+ }
+ }
+ }
+ assert!(problems.is_empty(), "{}", problems.join("\n"));
+}
diff --git a/crates/ai-memory-cli/tests/routing_instructions.rs b/crates/ai-memory-cli/tests/suite/routing_instructions.rs
similarity index 100%
rename from crates/ai-memory-cli/tests/routing_instructions.rs
rename to crates/ai-memory-cli/tests/suite/routing_instructions.rs
diff --git a/crates/ai-memory-cli/tests/routing_skills.rs b/crates/ai-memory-cli/tests/suite/routing_skills.rs
similarity index 100%
rename from crates/ai-memory-cli/tests/routing_skills.rs
rename to crates/ai-memory-cli/tests/suite/routing_skills.rs
diff --git a/crates/ai-memory-consolidate/Cargo.toml b/crates/ai-memory-consolidate/Cargo.toml
index 190413f3..b1879557 100644
--- a/crates/ai-memory-consolidate/Cargo.toml
+++ b/crates/ai-memory-consolidate/Cargo.toml
@@ -7,6 +7,9 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "Karpathy-style ingest / query / lint consolidation pipeline."
+# One integration-test binary instead of one per file. Each binary
+# statically links the whole dep graph and gets scanned by macOS on
+# first run.
[dependencies]
ai-memory-core.workspace = true
@@ -25,6 +28,7 @@ tracing.workspace = true
jiff.workspace = true
[dev-dependencies]
+ai-memory-test-support.workspace = true
async-trait.workspace = true
tempfile.workspace = true
rusqlite.workspace = true
diff --git a/crates/ai-memory-consolidate/src/auto_improve.rs b/crates/ai-memory-consolidate/src/auto_improve.rs
index 9895258d..48590319 100644
--- a/crates/ai-memory-consolidate/src/auto_improve.rs
+++ b/crates/ai-memory-consolidate/src/auto_improve.rs
@@ -1993,35 +1993,39 @@ mod tests {
#[cfg(windows)]
fn write_eval_script(body: &str) -> String {
let dir = tempfile::TempDir::new().unwrap().keep();
- let path = dir.join("eval.cmd");
+ let path = dir.join("eval.ps1");
let body = match body {
"#!/bin/sh\ncat >/dev/null\nprintf '%s' '{\"score_before\":0.72,\"score_after\":0.76,\"passed\":true}'\n" => {
- "more >NUL\r\necho {\"score_before\":0.72,\"score_after\":0.76,\"passed\":true}\r\n"
+ "$null = [Console]::In.ReadToEnd()\n[Console]::Out.Write('{\"score_before\":0.72,\"score_after\":0.76,\"passed\":true}')\n"
.into()
}
"#!/bin/sh\ncat >/dev/null\nprintf '%s' '{\"score_before\":0.72,\"score_after\":0.70,\"passed\":true}'\n" => {
- "more >NUL\r\necho {\"score_before\":0.72,\"score_after\":0.70,\"passed\":true}\r\n"
+ "$null = [Console]::In.ReadToEnd()\n[Console]::Out.Write('{\"score_before\":0.72,\"score_after\":0.70,\"passed\":true}')\n"
.into()
}
- "#!/bin/sh\ncat >/dev/null\nexit 7\n" => "more >NUL\r\nexit /B 7\r\n".into(),
- "#!/bin/sh\nexit 7\n" => "exit /B 7\r\n".into(),
+ "#!/bin/sh\ncat >/dev/null\nexit 7\n" => {
+ "$null = [Console]::In.ReadToEnd()\nexit 7\n".into()
+ }
+ "#!/bin/sh\nexit 7\n" => "exit 7\n".into(),
"#!/bin/sh\ncat >/dev/null\nprintf 'not-json'\n" => {
- "more >NUL\r\necho not-json\r\n".into()
+ "$null = [Console]::In.ReadToEnd()\n[Console]::Out.Write('not-json')\n".into()
}
"#!/bin/sh\ncat >/dev/null\nsleep 3\n" => {
- "more >NUL\r\nping -n 4 127.0.0.1 >NUL\r\n".into()
+ "$null = [Console]::In.ReadToEnd()\nStart-Sleep -Seconds 3\n".into()
}
- "#!/bin/sh\nsleep 5\n" => "ping -n 6 127.0.0.1 >NUL\r\n".into(),
+ "#!/bin/sh\nsleep 5\n" => "Start-Sleep -Seconds 20\n".into(),
"#!/bin/sh\ni=0\nwhile [ $i -lt 70000 ]; do printf x; i=$((i + 1)); done\n" => {
let chunk = "x".repeat(100);
- format!(
- "set \"chunk={chunk}\"\r\nfor /L %%i in (1,1,700) do <NUL set /p _=%chunk%\r\n"
- )
+ format!("for ($i = 0; $i -lt 700; $i++) {{ [Console]::Out.Write('{chunk}') }}\n")
}
other => panic!("unmapped eval script fixture for Windows: {other:?}"),
};
- std::fs::write(&path, format!("@echo off\r\n{body}")).unwrap();
- format!("cmd.exe /C {}", path.display())
+ std::fs::write(&path, body).unwrap();
+ format!(
+ "{} -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File {}",
+ ai_memory_test_support::powershell_exe(),
+ path.display()
+ )
}
#[tokio::test]
@@ -2151,7 +2155,16 @@ mod tests {
assert!(proposals.is_empty());
assert_eq!(rejected[0].reason, "eval_gate_timeout");
- assert!(started.elapsed() < Duration::from_secs(3));
+ let elapsed = started.elapsed();
+ let max_elapsed = if cfg!(windows) {
+ Duration::from_secs(10)
+ } else {
+ Duration::from_secs(3)
+ };
+ assert!(
+ elapsed < max_elapsed,
+ "blocked stdin timeout returned after {elapsed:?}, expected below {max_elapsed:?}"
+ );
}
#[tokio::test]
diff --git a/crates/ai-memory-consolidate/src/lib.rs b/crates/ai-memory-consolidate/src/lib.rs
index 9bfc2e41..2c6f8689 100644
--- a/crates/ai-memory-consolidate/src/lib.rs
+++ b/crates/ai-memory-consolidate/src/lib.rs
@@ -74,3 +74,13 @@ pub use types::{
ConsolidatedBatch, ConsolidatedPage, ConsolidatedPageUpdate, ConsolidationOutcome, PageKind,
SlotKind,
};
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_consolidate;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-consolidate/src/projection.rs b/crates/ai-memory-consolidate/src/projection.rs
index 74ab2b42..7bd224a9 100644
--- a/crates/ai-memory-consolidate/src/projection.rs
+++ b/crates/ai-memory-consolidate/src/projection.rs
@@ -117,16 +117,40 @@ pub fn project_observations(
};
}
- let mut selected = select_observation_indices(observations, cfg.max_selected_observations);
- let mut rendered = render_projection(observations, &selected, cfg.per_body_excerpt_chars);
+ // Scores and rendered blocks depend only on an observation and its position
+ // in the full list, never on which other observations were selected, so
+ // both are computed once here. The prune loop below used to recompute
+ // every remaining score (each one scans the body) and re-render the whole
+ // text on every removal: quadratic, and 14s for 256 observations of 4k
+ // chars, in production consolidation as much as in the test.
+ let scores: Vec<i32> = observations
+ .iter()
+ .enumerate()
+ .map(|(idx, obs)| observation_score(obs, idx, observations.len()))
+ .collect();
+ let mut selected =
+ select_observation_indices(observations, cfg.max_selected_observations, &scores);
+
+ let block_chars: Vec<usize> = observations
+ .iter()
+ .enumerate()
+ .map(|(idx, obs)| {
+ render_observation_block(observations.len(), idx, obs, cfg.per_body_excerpt_chars)
+ .text
+ .chars()
+ .count()
+ })
+ .collect();
+ let mut total_chars: usize = selected.iter().map(|idx| block_chars[*idx]).sum();
- while rendered.text.chars().count() > cfg.max_total_chars && selected.len() > 1 {
- let Some(remove_idx) = lowest_prunable_index(observations, &selected) else {
+ while total_chars > cfg.max_total_chars && selected.len() > 1 {
+ let Some(remove_idx) = lowest_prunable_index(observations, &selected, &scores) else {
break;
};
selected.retain(|idx| *idx != remove_idx);
- rendered = render_projection(observations, &selected, cfg.per_body_excerpt_chars);
+ total_chars = total_chars.saturating_sub(block_chars[remove_idx]);
}
+ let rendered = render_projection(observations, &selected, cfg.per_body_excerpt_chars);
let omitted_count = observations.len().saturating_sub(selected.len());
let mut text = rendered.text;
@@ -208,38 +232,11 @@ fn render_projection(
let Some(obs) = observations.get(*idx) else {
continue;
};
- let (body, truncated, omitted) = excerpt_body(&obs.body, per_body_excerpt_chars);
- if truncated {
+ let block = render_observation_block(observations.len(), *idx, obs, per_body_excerpt_chars);
+ if block.truncated {
truncated_bodies += 1;
}
- let title = cap_text_with_marker(&obs.title, MAX_RENDERED_TITLE_CHARS, "observation title");
- text.push_str(&format!(
- "\n--- observation {}/{} ---\nid: {}\nkind: {}\ntitle: {}\nimportance: {}\ncreated_at: {}\n",
- idx + 1,
- observations.len(),
- obs.id,
- obs.kind.as_str(),
- title,
- obs.importance,
- obs.created_at,
- ));
- if let Some(extension) = obs.extension.as_deref().filter(|s| !s.trim().is_empty()) {
- let extension = cap_text_with_marker(extension, MAX_RENDERED_SOURCE_CHARS, "extension");
- text.push_str(&format!("extension: {extension}\n"));
- }
- if let Some(source_event) = obs.source_event.as_deref().filter(|s| !s.trim().is_empty()) {
- let source_event =
- cap_text_with_marker(source_event, MAX_RENDERED_SOURCE_CHARS, "source event");
- text.push_str(&format!("source_event: {source_event}\n"));
- }
- text.push_str(&format!("body:\n{body}"));
- if truncated {
- text.push_str(&format!(
- "\n[observation body truncated; {omitted} chars omitted; full original remains in SQLite as observation id {}]",
- obs.id
- ));
- }
- text.push('\n');
+ text.push_str(&block.text);
}
RenderedProjection {
text,
@@ -247,6 +244,51 @@ fn render_projection(
}
}
+/// One observation's rendered block: header, optional provenance lines, body
+/// excerpt, and the truncation marker when the body was cut.
+struct RenderedBlock {
+ text: String,
+ truncated: bool,
+}
+
+fn render_observation_block(
+ total: usize,
+ idx: usize,
+ obs: &Observation,
+ per_body_excerpt_chars: usize,
+) -> RenderedBlock {
+ let (body, truncated, omitted) = excerpt_body(&obs.body, per_body_excerpt_chars);
+ let title = cap_text_with_marker(&obs.title, MAX_RENDERED_TITLE_CHARS, "observation title");
+ let mut text = format!(
+ "\n--- observation {}/{} ---\nid: {}\nkind: {}\ntitle: {}\nimportance: {}\ncreated_at: {}\n",
+ idx + 1,
+ total,
+ obs.id,
+ obs.kind.as_str(),
+ title,
+ obs.importance,
+ obs.created_at,
+ );
+ if let Some(extension) = obs.extension.as_deref().filter(|s| !s.trim().is_empty()) {
+ let extension = cap_text_with_marker(extension, MAX_RENDERED_SOURCE_CHARS, "extension");
+ text.push_str(&format!("extension: {extension}\n"));
+ }
+ if let Some(source_event) = obs.source_event.as_deref().filter(|s| !s.trim().is_empty()) {
+ let source_event =
+ cap_text_with_marker(source_event, MAX_RENDERED_SOURCE_CHARS, "source event");
+ text.push_str(&format!("source_event: {source_event}\n"));
+ }
+ text.push_str(&format!("body:\n{body}"));
+ if truncated {
+ text.push_str(&format!(
+ "\n[observation body truncated; {omitted} chars omitted; full original remains in SQLite as observation id {}]",
+ obs.id
+ ));
+ }
+ text.push('\n');
+ RenderedBlock { text, truncated }
+}
+
fn excerpt_body(body: &str, max_chars: usize) -> (String, bool, usize) {
let total = body.chars().count();
if total <= max_chars {
@@ -274,7 +316,11 @@ fn fit_text_to_budget(text: &str, max_chars: usize, marker: &str) -> String {
out
}
-fn select_observation_indices(observations: &[Observation], limit: usize) -> Vec<usize> {
+fn select_observation_indices(
+ observations: &[Observation],
+ limit: usize,
+ scores: &[i32],
+) -> Vec<usize> {
if observations.len() <= limit {
return (0..observations.len()).collect();
}
@@ -290,8 +336,8 @@ fn select_observation_indices(observations: &[Observation], limit: usize) -> Vec
.iter()
.enumerate()
.filter(|(idx, _)| !selected.contains(idx))
- .map(|(idx, obs)| {
- let mut score = observation_score(obs, idx, observations.len());
+ .map(|(idx, _)| {
+ let mut score = scores[idx];
if even.contains(&idx) {
score += EVEN_SAMPLE_SCORE;
}
@@ -326,17 +372,16 @@ fn even_sample_indices(total: usize) -> BTreeSet<usize> {
out
}
-fn lowest_prunable_index(observations: &[Observation], selected: &[usize]) -> Option<usize> {
+fn lowest_prunable_index(
+ observations: &[Observation],
+ selected: &[usize],
+ scores: &[i32],
+) -> Option<usize> {
selected
.iter()
.copied()
.filter(|idx| !is_hard_anchor(observations, *idx))
- .map(|idx| {
- (
- observation_score(&observations[idx], idx, observations.len()),
- idx,
- )
- })
+ .map(|idx| (scores[idx], idx))
.min_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)))
.map(|(_, idx)| idx)
}
diff --git a/crates/ai-memory-consolidate/tests/access_breadth_sweep.rs b/crates/ai-memory-consolidate/tests/suite/access_breadth_sweep.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/access_breadth_sweep.rs
rename to crates/ai-memory-consolidate/tests/suite/access_breadth_sweep.rs
diff --git a/crates/ai-memory-consolidate/tests/embed_backfill.rs b/crates/ai-memory-consolidate/tests/suite/embed_backfill.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/embed_backfill.rs
rename to crates/ai-memory-consolidate/tests/suite/embed_backfill.rs
diff --git a/crates/ai-memory-consolidate/tests/embeddings.rs b/crates/ai-memory-consolidate/tests/suite/embeddings.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/embeddings.rs
rename to crates/ai-memory-consolidate/tests/suite/embeddings.rs
diff --git a/crates/ai-memory-consolidate/tests/lifecycle.rs b/crates/ai-memory-consolidate/tests/suite/lifecycle.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/lifecycle.rs
rename to crates/ai-memory-consolidate/tests/suite/lifecycle.rs
diff --git a/crates/ai-memory-consolidate/tests/local_embeddings.rs b/crates/ai-memory-consolidate/tests/suite/local_embeddings.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/local_embeddings.rs
rename to crates/ai-memory-consolidate/tests/suite/local_embeddings.rs
diff --git a/crates/ai-memory-consolidate/tests/suite/mod.rs b/crates/ai-memory-consolidate/tests/suite/mod.rs
new file mode 100644
index 00000000..2c1c4825
--- /dev/null
+++ b/crates/ai-memory-consolidate/tests/suite/mod.rs
@@ -0,0 +1,14 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod access_breadth_sweep;
+mod embed_backfill;
+mod embeddings;
+mod lifecycle;
+mod local_embeddings;
+mod multi_machine;
+mod observation_retention;
+mod recall_eval;
+mod search_quality;
+mod typed_edges;
diff --git a/crates/ai-memory-consolidate/tests/multi_machine.rs b/crates/ai-memory-consolidate/tests/suite/multi_machine.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/multi_machine.rs
rename to crates/ai-memory-consolidate/tests/suite/multi_machine.rs
diff --git a/crates/ai-memory-consolidate/tests/observation_retention.rs b/crates/ai-memory-consolidate/tests/suite/observation_retention.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/observation_retention.rs
rename to crates/ai-memory-consolidate/tests/suite/observation_retention.rs
diff --git a/crates/ai-memory-consolidate/tests/recall_eval.rs b/crates/ai-memory-consolidate/tests/suite/recall_eval.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/recall_eval.rs
rename to crates/ai-memory-consolidate/tests/suite/recall_eval.rs
diff --git a/crates/ai-memory-consolidate/tests/search_quality.rs b/crates/ai-memory-consolidate/tests/suite/search_quality.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/search_quality.rs
rename to crates/ai-memory-consolidate/tests/suite/search_quality.rs
diff --git a/crates/ai-memory-consolidate/tests/typed_edges.rs b/crates/ai-memory-consolidate/tests/suite/typed_edges.rs
similarity index 100%
rename from crates/ai-memory-consolidate/tests/typed_edges.rs
rename to crates/ai-memory-consolidate/tests/suite/typed_edges.rs
diff --git a/crates/ai-memory-hooks/Cargo.toml b/crates/ai-memory-hooks/Cargo.toml
index e0b2bfc0..a460025f 100644
--- a/crates/ai-memory-hooks/Cargo.toml
+++ b/crates/ai-memory-hooks/Cargo.toml
@@ -7,6 +7,9 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "Hook payload schemas, sanitisation, and HTTP ingress for agent lifecycle hooks."
+# One integration-test binary instead of one per file. Each binary
+# statically links the whole dep graph and gets scanned by macOS on
+# first run.
[dependencies]
ai-memory-core.workspace = true
@@ -27,6 +30,7 @@ axum.workspace = true
sha2.workspace = true
[dev-dependencies]
+ai-memory-test-support.workspace = true
tempfile.workspace = true
git2.workspace = true
ai-memory-llm.workspace = true
diff --git a/crates/ai-memory-hooks/src/lib.rs b/crates/ai-memory-hooks/src/lib.rs
index 4204a054..efb83bb0 100644
--- a/crates/ai-memory-hooks/src/lib.rs
+++ b/crates/ai-memory-hooks/src/lib.rs
@@ -54,3 +54,13 @@ pub use router::{
};
pub use synth::synthesize_session_page;
pub use workstream::{WorkstreamState, workstream_router};
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_hooks;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-hooks/tests/suite/mod.rs b/crates/ai-memory-hooks/tests/suite/mod.rs
new file mode 100644
index 00000000..a1a0946b
--- /dev/null
+++ b/crates/ai-memory-hooks/tests/suite/mod.rs
@@ -0,0 +1,6 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod powershell_home;
+mod powershell_utf8;
diff --git a/crates/ai-memory-hooks/tests/powershell_home.rs b/crates/ai-memory-hooks/tests/suite/powershell_home.rs
similarity index 95%
rename from crates/ai-memory-hooks/tests/powershell_home.rs
rename to crates/ai-memory-hooks/tests/suite/powershell_home.rs
index 3462d982..3003c495 100644
--- a/crates/ai-memory-hooks/tests/powershell_home.rs
+++ b/crates/ai-memory-hooks/tests/suite/powershell_home.rs
@@ -28,7 +28,7 @@ fn marker_lookup_does_not_assign_to_powershell_home() {
[Console]::Error.Write(($Error | Out-String)); exit 17 \
}}; [Console]::Out.Write('ok')"
);
- let output = Command::new("powershell.exe")
+ let output = Command::new(ai_memory_test_support::powershell_exe())
.args([
"-NoLogo",
"-NoProfile",
diff --git a/crates/ai-memory-hooks/tests/powershell_utf8.rs b/crates/ai-memory-hooks/tests/suite/powershell_utf8.rs
similarity index 98%
rename from crates/ai-memory-hooks/tests/powershell_utf8.rs
rename to crates/ai-memory-hooks/tests/suite/powershell_utf8.rs
index 3a92e7f6..ce50c629 100644
--- a/crates/ai-memory-hooks/tests/powershell_utf8.rs
+++ b/crates/ai-memory-hooks/tests/suite/powershell_utf8.rs
@@ -94,7 +94,7 @@ fn powershell_hook_posts_json_as_utf8_bytes() {
". '{script}'; function Read-AiMemoryStdin {{ $env:AI_MEMORY_TEST_PAYLOAD }}; \
Invoke-AiMemoryHook -Event 'user-prompt' -Agent 'codex'"
);
- let output = Command::new("powershell.exe")
+ let output = Command::new(ai_memory_test_support::powershell_exe())
.args([
"-NoLogo",
"-NoProfile",
diff --git a/crates/ai-memory-llm/Cargo.toml b/crates/ai-memory-llm/Cargo.toml
index 1b0e75ba..97fbf180 100644
--- a/crates/ai-memory-llm/Cargo.toml
+++ b/crates/ai-memory-llm/Cargo.toml
@@ -7,6 +7,9 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "LLM provider trait with typed Anthropic, OpenAI, Gemini, OpenAI OAuth, GitHub Copilot and OpenAI-compat clients."
+# One integration-test binary instead of one per file. Each binary
+# statically links the whole dep graph and gets scanned by macOS on
+# first run.
[dependencies]
ai-memory-core.workspace = true
diff --git a/crates/ai-memory-llm/src/lib.rs b/crates/ai-memory-llm/src/lib.rs
index 134266dc..1d0db028 100644
--- a/crates/ai-memory-llm/src/lib.rs
+++ b/crates/ai-memory-llm/src/lib.rs
@@ -102,3 +102,13 @@ pub use stored_token::StoredOAuthToken;
pub use types::{
ChatMessage, ChatRequest, ChatResponse, LlmOperationId, ReasoningEffort, Role, Usage,
};
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_llm;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-llm/tests/suite/mod.rs b/crates/ai-memory-llm/tests/suite/mod.rs
new file mode 100644
index 00000000..a1e390b6
--- /dev/null
+++ b/crates/ai-memory-llm/tests/suite/mod.rs
@@ -0,0 +1,6 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod openai_compat_embedder;
+mod openai_compat_strict;
diff --git a/crates/ai-memory-llm/tests/openai_compat_embedder.rs b/crates/ai-memory-llm/tests/suite/openai_compat_embedder.rs
similarity index 100%
rename from crates/ai-memory-llm/tests/openai_compat_embedder.rs
rename to crates/ai-memory-llm/tests/suite/openai_compat_embedder.rs
diff --git a/crates/ai-memory-llm/tests/openai_compat_strict.rs b/crates/ai-memory-llm/tests/suite/openai_compat_strict.rs
similarity index 100%
rename from crates/ai-memory-llm/tests/openai_compat_strict.rs
rename to crates/ai-memory-llm/tests/suite/openai_compat_strict.rs
diff --git a/crates/ai-memory-mcp/Cargo.toml b/crates/ai-memory-mcp/Cargo.toml
index bcd0f5d3..6547f9e4 100644
--- a/crates/ai-memory-mcp/Cargo.toml
+++ b/crates/ai-memory-mcp/Cargo.toml
@@ -7,6 +7,9 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "MCP server transport and tool router for ai-memory."
+# One integration-test binary instead of one per file. Each binary
+# statically links the whole dep graph and gets scanned by macOS on
+# first run.
[dependencies]
ai-memory-core.workspace = true
diff --git a/crates/ai-memory-mcp/src/lib.rs b/crates/ai-memory-mcp/src/lib.rs
index 8c149cf7..e733be2e 100644
--- a/crates/ai-memory-mcp/src/lib.rs
+++ b/crates/ai-memory-mcp/src/lib.rs
@@ -24,3 +24,13 @@ pub use human_auth::{
require_dual_auth, session_auth_router,
};
pub use server::{AiMemoryServer, MEMORY_INSTRUCTIONS};
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_mcp;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-mcp/tests/admin_audit_log.rs b/crates/ai-memory-mcp/tests/suite/admin_audit_log.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/admin_audit_log.rs
rename to crates/ai-memory-mcp/tests/suite/admin_audit_log.rs
diff --git a/crates/ai-memory-mcp/tests/admin_backup.rs b/crates/ai-memory-mcp/tests/suite/admin_backup.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/admin_backup.rs
rename to crates/ai-memory-mcp/tests/suite/admin_backup.rs
diff --git a/crates/ai-memory-mcp/tests/admin_bootstrap.rs b/crates/ai-memory-mcp/tests/suite/admin_bootstrap.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/admin_bootstrap.rs
rename to crates/ai-memory-mcp/tests/suite/admin_bootstrap.rs
diff --git a/crates/ai-memory-mcp/tests/admin_move.rs b/crates/ai-memory-mcp/tests/suite/admin_move.rs
similarity index 99%
rename from crates/ai-memory-mcp/tests/admin_move.rs
rename to crates/ai-memory-mcp/tests/suite/admin_move.rs
index b09c9948..74be331b 100644
--- a/crates/ai-memory-mcp/tests/admin_move.rs
+++ b/crates/ai-memory-mcp/tests/suite/admin_move.rs
@@ -13,21 +13,20 @@
//! both versions survive), then purge the source — there the episodic rows
//! (sessions/observations/handoffs) are dropped by the purge.
+use super::common::post;
use ai_memory_core::{AgentKind, PagePath, Sanitized, Sanitizer, Tier};
-use ai_memory_mcp::{AdminState, admin_router};
+use ai_memory_mcp::AdminState;
use ai_memory_store::{DecayParams, PrepareWorkstreamRun, Store, WorkstreamSelection};
use ai_memory_wiki::{
AdmissionChain, AdmissionOp, FailurePolicy, WebhookConfig, Wiki, WritePageRequest,
};
-use axum::body::Body;
-use axum::http::{HeaderMap, Request, StatusCode};
+use axum::http::{HeaderMap, StatusCode};
use axum::routing::post as axum_post;
use axum::{Json, Router};
use serde_json::json;
use std::path::Path;
use std::sync::{Arc, Mutex};
use tempfile::TempDir;
-use tower::ServiceExt;
// ---------------------------------------------------------------------------
// Helpers
@@ -101,17 +100,6 @@ async fn body_json(resp: axum::response::Response) -> serde_json::Value {
serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
}
-async fn post(state: AdminState, uri: &str, body: serde_json::Value) -> axum::response::Response {
- let router = admin_router(state);
- let req = Request::builder()
- .method("POST")
- .uri(uri)
- .header("content-type", "application/json")
- .body(Body::from(serde_json::to_vec(&body).unwrap()))
- .unwrap();
- router.oneshot(req).await.unwrap()
-}
-
/// Seed `<ws>/<project>/<path>` with one page carrying `body`.
async fn seed_page(store: &Store, wiki: &Wiki, ws: &str, project: &str, path: &str, body: &str) {
seed_page_with_metadata(
diff --git a/crates/ai-memory-mcp/tests/admin_move_session.rs b/crates/ai-memory-mcp/tests/suite/admin_move_session.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/admin_move_session.rs
rename to crates/ai-memory-mcp/tests/suite/admin_move_session.rs
diff --git a/crates/ai-memory-mcp/tests/admin_phase3.rs b/crates/ai-memory-mcp/tests/suite/admin_phase3.rs
similarity index 97%
rename from crates/ai-memory-mcp/tests/admin_phase3.rs
rename to crates/ai-memory-mcp/tests/suite/admin_phase3.rs
index 26489e25..77fb0765 100644
--- a/crates/ai-memory-mcp/tests/admin_phase3.rs
+++ b/crates/ai-memory-mcp/tests/suite/admin_phase3.rs
@@ -7,24 +7,23 @@
//! tmpdir-backed store + wiki, drive the router with
//! `tower::ServiceExt::oneshot`.
+use super::common::{get, post};
use ai_memory_core::{
ActorContext, AgentKind, NewObservation, NewPage, NewSession, ObservationKind, PagePath,
Sanitized, Sanitizer, SessionId, Tier,
};
use ai_memory_llm::SyntheticEmbedder;
-use ai_memory_mcp::{AdminState, admin_router};
+use ai_memory_mcp::AdminState;
use ai_memory_store::{
AutoImproveProposalOperation, AutoImproveProposalStatus, DecayParams, NewAutoImproveProposal,
StageAutoImproveRun, Store,
};
use ai_memory_wiki::Wiki;
use ai_memory_wiki::WritePageRequest;
-use axum::body::Body;
-use axum::http::{Request, StatusCode};
+use axum::http::StatusCode;
use serde_json::json;
use std::sync::Arc;
use tempfile::TempDir;
-use tower::ServiceExt;
// ---------------------------------------------------------------------------
// Helpers
@@ -68,27 +67,6 @@ async fn body_json(resp: axum::response::Response) -> serde_json::Value {
serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
}
-async fn post(state: AdminState, uri: &str, body: serde_json::Value) -> axum::response::Response {
- let router = admin_router(state);
- let req = Request::builder()
- .method("POST")
- .uri(uri)
- .header("content-type", "application/json")
- .body(Body::from(serde_json::to_vec(&body).unwrap()))
- .unwrap();
- router.oneshot(req).await.unwrap()
-}
-
-async fn get(state: AdminState, uri: &str) -> axum::response::Response {
- let router = admin_router(state);
- let req = Request::builder()
- .method("GET")
- .uri(uri)
- .body(Body::empty())
- .unwrap();
- router.oneshot(req).await.unwrap()
-}
-
fn telemetry_stage_input(
ws: ai_memory_core::WorkspaceId,
proj: ai_memory_core::ProjectId,
diff --git a/crates/ai-memory-mcp/tests/admin_purge.rs b/crates/ai-memory-mcp/tests/suite/admin_purge.rs
similarity index 97%
rename from crates/ai-memory-mcp/tests/admin_purge.rs
rename to crates/ai-memory-mcp/tests/suite/admin_purge.rs
index 62a9369e..be4c600a 100644
--- a/crates/ai-memory-mcp/tests/admin_purge.rs
+++ b/crates/ai-memory-mcp/tests/suite/admin_purge.rs
@@ -4,23 +4,22 @@
//! [`AdminState`] over a tmpdir-backed store + wiki, drive the router
//! with `tower::ServiceExt::oneshot`.
+use super::common::post;
use ai_memory_core::{
AgentKind, NewHandoff, NewObservation, NewSession, ObservationKind, PagePath, ProjectId,
Sanitized, Sanitizer, SessionId, Tier, WorkspaceId,
};
-use ai_memory_mcp::{AdminState, admin_router};
+use ai_memory_mcp::AdminState;
use ai_memory_store::{DecayParams, PrepareWorkstreamRun, Store, WorkstreamSelection};
use ai_memory_wiki::{
AdmissionChain, AdmissionOp, FailurePolicy, WebhookConfig, Wiki, WritePageRequest,
};
use axum::Router;
-use axum::body::Body;
-use axum::http::{Request, StatusCode};
+use axum::http::StatusCode;
use axum::routing::post as route_post;
use serde_json::json;
use std::path::Path;
use tempfile::TempDir;
-use tower::ServiceExt;
// ---------------------------------------------------------------------------
// Helpers
@@ -61,17 +60,6 @@ async fn body_json(resp: axum::response::Response) -> serde_json::Value {
serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
}
-async fn post(state: AdminState, uri: &str, body: serde_json::Value) -> axum::response::Response {
- let router = admin_router(state);
- let req = Request::builder()
- .method("POST")
- .uri(uri)
- .header("content-type", "application/json")
- .body(Body::from(serde_json::to_vec(&body).unwrap()))
- .unwrap();
- router.oneshot(req).await.unwrap()
-}
-
/// Seed two projects (`default/keep` and `default/doomed`), each with one
/// page, one session, some observations, and a handoff. Returns IDs for
/// both projects so callers can construct the per-project wiki paths.
diff --git a/crates/ai-memory-mcp/tests/admin_read_page.rs b/crates/ai-memory-mcp/tests/suite/admin_read_page.rs
similarity index 95%
rename from crates/ai-memory-mcp/tests/admin_read_page.rs
rename to crates/ai-memory-mcp/tests/suite/admin_read_page.rs
index ddb64313..57e8f50a 100644
--- a/crates/ai-memory-mcp/tests/admin_read_page.rs
+++ b/crates/ai-memory-mcp/tests/suite/admin_read_page.rs
@@ -3,14 +3,13 @@
//! the handler serves the store's faithful copy instead of 404ing
//! (gotchas/read-page-by-query-misses), while real parse errors still surface.
+use super::common::get;
use ai_memory_core::{NewPage, PagePath, Tier};
-use ai_memory_mcp::{AdminState, admin_router};
+use ai_memory_mcp::AdminState;
use ai_memory_store::{DecayParams, Store};
use ai_memory_wiki::{Wiki, WritePageRequest};
-use axum::body::Body;
-use axum::http::{Request, StatusCode};
+use axum::http::StatusCode;
use tempfile::TempDir;
-use tower::ServiceExt;
async fn make_state(tmp: &TempDir) -> (AdminState, Store) {
let store = Store::open(tmp.path()).unwrap();
@@ -47,16 +46,6 @@ async fn body_json(resp: axum::response::Response) -> serde_json::Value {
serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
}
-async fn get(state: AdminState, uri: &str) -> axum::response::Response {
- let router = admin_router(state);
- let req = Request::builder()
- .method("GET")
- .uri(uri)
- .body(Body::empty())
- .unwrap();
- router.oneshot(req).await.unwrap()
-}
-
/// A page present in the store but NOT on disk (the index is ahead of the
/// filesystem) is still served — from the DB copy — rather than 404ing.
#[tokio::test]
diff --git a/crates/ai-memory-mcp/tests/admin_rename.rs b/crates/ai-memory-mcp/tests/suite/admin_rename.rs
similarity index 97%
rename from crates/ai-memory-mcp/tests/admin_rename.rs
rename to crates/ai-memory-mcp/tests/suite/admin_rename.rs
index 35dc84ab..2ef42c42 100644
--- a/crates/ai-memory-mcp/tests/admin_rename.rs
+++ b/crates/ai-memory-mcp/tests/suite/admin_rename.rs
@@ -4,6 +4,7 @@
//! [`AdminState`] over a tmpdir-backed store + wiki, drive the router
//! with `tower::ServiceExt::oneshot`.
+use super::common::post;
use ai_memory_core::{PagePath, Tier};
use ai_memory_mcp::{AdminState, admin_router};
use ai_memory_store::{DecayParams, Store};
@@ -53,17 +54,6 @@ async fn body_json(resp: axum::response::Response) -> serde_json::Value {
serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
}
-async fn post(state: AdminState, uri: &str, body: serde_json::Value) -> axum::response::Response {
- let router = admin_router(state);
- let req = Request::builder()
- .method("POST")
- .uri(uri)
- .header("content-type", "application/json")
- .body(Body::from(serde_json::to_vec(&body).unwrap()))
- .unwrap();
- router.oneshot(req).await.unwrap()
-}
-
/// Seed `default/old-name` with one page. Returns the page path.
async fn seed_page(store: &Store, wiki: &Wiki, project: &str) -> String {
let ws = store
diff --git a/crates/ai-memory-mcp/tests/admin_status_search.rs b/crates/ai-memory-mcp/tests/suite/admin_status_search.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/admin_status_search.rs
rename to crates/ai-memory-mcp/tests/suite/admin_status_search.rs
diff --git a/crates/ai-memory-mcp/tests/admin_write_page.rs b/crates/ai-memory-mcp/tests/suite/admin_write_page.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/admin_write_page.rs
rename to crates/ai-memory-mcp/tests/suite/admin_write_page.rs
diff --git a/crates/ai-memory-mcp/tests/autoscope_multiuser.rs b/crates/ai-memory-mcp/tests/suite/autoscope_multiuser.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/autoscope_multiuser.rs
rename to crates/ai-memory-mcp/tests/suite/autoscope_multiuser.rs
diff --git a/crates/ai-memory-mcp/tests/suite/common.rs b/crates/ai-memory-mcp/tests/suite/common.rs
new file mode 100644
index 00000000..a48ea594
--- /dev/null
+++ b/crates/ai-memory-mcp/tests/suite/common.rs
@@ -0,0 +1,33 @@
+//! Helpers shared by the admin route tests in this suite.
+
+use ai_memory_mcp::{AdminState, admin_router};
+use axum::body::Body;
+use axum::http::Request;
+use tower::ServiceExt;
+
+/// POST a JSON body to `uri` through a fresh admin router built from `state`.
+pub async fn post(
+ state: AdminState,
+ uri: &str,
+ body: serde_json::Value,
+) -> axum::response::Response {
+ let router = admin_router(state);
+ let req = Request::builder()
+ .method("POST")
+ .uri(uri)
+ .header("content-type", "application/json")
+ .body(Body::from(serde_json::to_vec(&body).unwrap()))
+ .unwrap();
+ router.oneshot(req).await.unwrap()
+}
+
+/// GET `uri` through a fresh admin router built from `state`.
+pub async fn get(state: AdminState, uri: &str) -> axum::response::Response {
+ let router = admin_router(state);
+ let req = Request::builder()
+ .method("GET")
+ .uri(uri)
+ .body(Body::empty())
+ .unwrap();
+ router.oneshot(req).await.unwrap()
+}
diff --git a/crates/ai-memory-mcp/tests/handoff_admission.rs b/crates/ai-memory-mcp/tests/suite/handoff_admission.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/handoff_admission.rs
rename to crates/ai-memory-mcp/tests/suite/handoff_admission.rs
diff --git a/crates/ai-memory-mcp/tests/handoff_identity.rs b/crates/ai-memory-mcp/tests/suite/handoff_identity.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/handoff_identity.rs
rename to crates/ai-memory-mcp/tests/suite/handoff_identity.rs
diff --git a/crates/ai-memory-mcp/tests/mcp_stateless_http.rs b/crates/ai-memory-mcp/tests/suite/mcp_stateless_http.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/mcp_stateless_http.rs
rename to crates/ai-memory-mcp/tests/suite/mcp_stateless_http.rs
diff --git a/crates/ai-memory-mcp/tests/suite/mod.rs b/crates/ai-memory-mcp/tests/suite/mod.rs
new file mode 100644
index 00000000..82784111
--- /dev/null
+++ b/crates/ai-memory-mcp/tests/suite/mod.rs
@@ -0,0 +1,23 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod common;
+
+mod admin_audit_log;
+mod admin_backup;
+mod admin_bootstrap;
+mod admin_move;
+mod admin_move_session;
+mod admin_phase3;
+mod admin_purge;
+mod admin_read_page;
+mod admin_rename;
+mod admin_status_search;
+mod admin_write_page;
+mod autoscope_multiuser;
+mod handoff_admission;
+mod handoff_identity;
+mod mcp_stateless_http;
+mod slot_identity;
+mod stress_autoscope;
diff --git a/crates/ai-memory-mcp/tests/slot_identity.rs b/crates/ai-memory-mcp/tests/suite/slot_identity.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/slot_identity.rs
rename to crates/ai-memory-mcp/tests/suite/slot_identity.rs
diff --git a/crates/ai-memory-mcp/tests/autoscope_stress.rs b/crates/ai-memory-mcp/tests/suite/stress_autoscope.rs
similarity index 100%
rename from crates/ai-memory-mcp/tests/autoscope_stress.rs
rename to crates/ai-memory-mcp/tests/suite/stress_autoscope.rs
diff --git a/crates/ai-memory-store/Cargo.toml b/crates/ai-memory-store/Cargo.toml
index f02f256c..4efb43ab 100644
--- a/crates/ai-memory-store/Cargo.toml
+++ b/crates/ai-memory-store/Cargo.toml
@@ -7,6 +7,8 @@ license.workspace = true
repository.workspace = true
authors.workspace = true
description = "SQLite storage layer with single-writer actor and FTS5/sqlite-vec indices."
+# One integration-test binary instead of one per file. Each binary statically
+# links the whole dep graph and gets scanned by macOS on first run.
[dependencies]
ai-memory-core.workspace = true
diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rs
index cfdb0b5c..b866390b 100644
--- a/crates/ai-memory-store/src/lib.rs
+++ b/crates/ai-memory-store/src/lib.rs
@@ -7000,3 +7000,13 @@ mod tests {
);
}
}
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_store;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index 49f98b97..01fd8bd7 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -4943,6 +4943,13 @@ pub(crate) mod tests {
let tmp = TempDir::new().unwrap();
let db_path = tmp.path().join("test.sqlite");
let mut conn = Connection::open(&db_path).unwrap();
+ // A fixture needs no durability. SQLite's defaults (rollback journal,
+ // synchronous=FULL) fsync every transaction, and nextest runs ~120 of
+ // these in parallel, so the suite was disk-bound: 0.3s per test alone,
+ // 2s+ under load. Production sets WAL + NORMAL in `Store::open`; these
+ // tests exercise SQL, not the journal.
+ conn.pragma_update(None, "journal_mode", "MEMORY").unwrap();
+ conn.pragma_update(None, "synchronous", "OFF").unwrap();
conn.pragma_update(None, "foreign_keys", "ON").unwrap();
crate::migrations::run(&mut conn).unwrap();
let ws = get_or_create_workspace(&mut conn, "default").unwrap();
diff --git a/crates/ai-memory-store/tests/access_breadth.rs b/crates/ai-memory-store/tests/suite/access_breadth.rs
similarity index 100%
rename from crates/ai-memory-store/tests/access_breadth.rs
rename to crates/ai-memory-store/tests/suite/access_breadth.rs
diff --git a/crates/ai-memory-store/tests/audit_contamination.rs b/crates/ai-memory-store/tests/suite/audit_contamination.rs
similarity index 100%
rename from crates/ai-memory-store/tests/audit_contamination.rs
rename to crates/ai-memory-store/tests/suite/audit_contamination.rs
diff --git a/crates/ai-memory-store/tests/audit_log.rs b/crates/ai-memory-store/tests/suite/audit_log.rs
similarity index 100%
rename from crates/ai-memory-store/tests/audit_log.rs
rename to crates/ai-memory-store/tests/suite/audit_log.rs
diff --git a/crates/ai-memory-store/tests/auto_improve_staging.rs b/crates/ai-memory-store/tests/suite/auto_improve_staging.rs
similarity index 100%
rename from crates/ai-memory-store/tests/auto_improve_staging.rs
rename to crates/ai-memory-store/tests/suite/auto_improve_staging.rs
diff --git a/crates/ai-memory-store/tests/client_activity.rs b/crates/ai-memory-store/tests/suite/client_activity.rs
similarity index 100%
rename from crates/ai-memory-store/tests/client_activity.rs
rename to crates/ai-memory-store/tests/suite/client_activity.rs
diff --git a/crates/ai-memory-store/tests/fts_drift_status.rs b/crates/ai-memory-store/tests/suite/fts_drift_status.rs
similarity index 100%
rename from crates/ai-memory-store/tests/fts_drift_status.rs
rename to crates/ai-memory-store/tests/suite/fts_drift_status.rs
diff --git a/crates/ai-memory-store/tests/handoff_ownership.rs b/crates/ai-memory-store/tests/suite/handoff_ownership.rs
similarity index 100%
rename from crates/ai-memory-store/tests/handoff_ownership.rs
rename to crates/ai-memory-store/tests/suite/handoff_ownership.rs
diff --git a/crates/ai-memory-store/tests/suite/mod.rs b/crates/ai-memory-store/tests/suite/mod.rs
new file mode 100644
index 00000000..46df6f92
--- /dev/null
+++ b/crates/ai-memory-store/tests/suite/mod.rs
@@ -0,0 +1,18 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod access_breadth;
+mod audit_contamination;
+mod audit_log;
+mod auto_improve_staging;
+mod client_activity;
+mod fts_drift_status;
+mod handoff_ownership;
+mod multi_session;
+mod session_ids_touching_scope;
+mod session_observations;
+mod session_scope_from_observations;
+mod sessions_by_agent;
+mod slot_visibility;
+mod stress_writer_throughput;
diff --git a/crates/ai-memory-store/tests/multi_session.rs b/crates/ai-memory-store/tests/suite/multi_session.rs
similarity index 100%
rename from crates/ai-memory-store/tests/multi_session.rs
rename to crates/ai-memory-store/tests/suite/multi_session.rs
diff --git a/crates/ai-memory-store/tests/session_ids_touching_scope.rs b/crates/ai-memory-store/tests/suite/session_ids_touching_scope.rs
similarity index 100%
rename from crates/ai-memory-store/tests/session_ids_touching_scope.rs
rename to crates/ai-memory-store/tests/suite/session_ids_touching_scope.rs
diff --git a/crates/ai-memory-store/tests/session_observations.rs b/crates/ai-memory-store/tests/suite/session_observations.rs
similarity index 100%
rename from crates/ai-memory-store/tests/session_observations.rs
rename to crates/ai-memory-store/tests/suite/session_observations.rs
diff --git a/crates/ai-memory-store/tests/session_scope_from_observations.rs b/crates/ai-memory-store/tests/suite/session_scope_from_observations.rs
similarity index 100%
rename from crates/ai-memory-store/tests/session_scope_from_observations.rs
rename to crates/ai-memory-store/tests/suite/session_scope_from_observations.rs
diff --git a/crates/ai-memory-store/tests/sessions_by_agent.rs b/crates/ai-memory-store/tests/suite/sessions_by_agent.rs
similarity index 100%
rename from crates/ai-memory-store/tests/sessions_by_agent.rs
rename to crates/ai-memory-store/tests/suite/sessions_by_agent.rs
diff --git a/crates/ai-memory-store/tests/slot_visibility.rs b/crates/ai-memory-store/tests/suite/slot_visibility.rs
similarity index 100%
rename from crates/ai-memory-store/tests/slot_visibility.rs
rename to crates/ai-memory-store/tests/suite/slot_visibility.rs
diff --git a/crates/ai-memory-store/tests/writer_throughput.rs b/crates/ai-memory-store/tests/suite/stress_writer_throughput.rs
similarity index 90%
rename from crates/ai-memory-store/tests/writer_throughput.rs
rename to crates/ai-memory-store/tests/suite/stress_writer_throughput.rs
index b3a2d4c5..133755a6 100644
--- a/crates/ai-memory-store/tests/writer_throughput.rs
+++ b/crates/ai-memory-store/tests/suite/stress_writer_throughput.rs
@@ -5,11 +5,13 @@
//! the right design for SQLite but does make one question worth answering with
//! a number: at what point does a shared server stop keeping up?
//!
-//! Run it on demand; it is `#[ignore]`d so a throughput measurement never
-//! becomes a flaky CI gate on a loaded runner:
+//! This is the `stress` tier: the everyday nextest profile skips it and
+//! `-P full`, the pre-push hook, and CI run it. The measurement itself is also
+//! `#[ignore]`d so a throughput number never becomes a flaky gate on a loaded
+//! runner; run it on demand:
//!
//! ```text
-//! cargo test -p ai-memory-store --test writer_throughput -- --ignored --nocapture
+//! cargo nextest run -p ai-memory-store -E 'test(/stress_writer_throughput/)' --run-ignored all --no-capture
//! ```
use std::time::Instant;
@@ -74,7 +76,7 @@ fn observation(
/// Reported as observations/second plus the implied budget in concurrent
/// agents, taking one tool call per agent-second as a deliberately pessimistic
/// stand-in for an actively working harness.
-#[tokio::test(flavor = "multi_thread")]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "throughput measurement; run explicitly"]
async fn writer_throughput_under_concurrent_load() {
let tmp = tempfile::tempdir().unwrap();
@@ -117,7 +119,7 @@ async fn writer_throughput_under_concurrent_load() {
/// queue applies backpressure rather than dropping work or growing without
/// limit. This asserts the property that matters operationally: every write in
/// an over-queue burst lands.
-#[tokio::test(flavor = "multi_thread")]
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_burst_larger_than_the_queue_applies_backpressure_and_loses_nothing() {
let tmp = tempfile::tempdir().unwrap();
let store = Store::open(tmp.path()).unwrap();
diff --git a/crates/ai-memory-test-support/Cargo.toml b/crates/ai-memory-test-support/Cargo.toml
new file mode 100644
index 00000000..8d0e8d7d
--- /dev/null
+++ b/crates/ai-memory-test-support/Cargo.toml
@@ -0,0 +1,20 @@
+[package]
+name = "ai-memory-test-support"
+description = "Helpers shared by this workspace's tests. Dev-dependency only; never shipped."
+version.workspace = true
+edition.workspace = true
+rust-version.workspace = true
+license.workspace = true
+repository.workspace = true
+authors.workspace = true
+publish = false
+
+[lib]
+# Helpers only; see the crate docs for why this ships no test binary.
+test = false
+doctest = false
+
+[dependencies]
+
+[lints]
+workspace = true
diff --git a/crates/ai-memory-test-support/src/lib.rs b/crates/ai-memory-test-support/src/lib.rs
new file mode 100644
index 00000000..e8fd0ae9
--- /dev/null
+++ b/crates/ai-memory-test-support/src/lib.rs
@@ -0,0 +1,75 @@
+//! Helpers shared by this workspace's tests.
+//!
+//! Dev-dependency only. Nothing here ships, and nothing here may depend on
+//! another workspace crate: a dev-dependency cycle makes cargo compile the
+//! depended-on crate twice and the two copies' types do not unify.
+//!
+//! No tests of its own either (`[lib] test = false`): every test binary in the
+//! workspace costs a link and, on macOS and Windows, a first-run scan, and the
+//! helpers here are exercised by every test that calls them.
+
+use std::net::TcpListener;
+
+/// A loopback HTTP endpoint that accepts every connection and closes it at
+/// once, so a request to it fails immediately with a connection error.
+///
+/// Tests that need "the server is down" used to point at a closed port.
+/// Linux and macOS refuse that connect in microseconds; Windows retransmits
+/// the SYN and takes about two seconds, which turned every hook test that
+/// posts to a dead server into a multi-second test. The listener thread lives
+/// for the rest of the process, which for a test binary is short.
+///
+/// Where binding is denied (a sandbox without network), this falls back to
+/// the closed reserved port, so such tests keep their old behaviour there
+/// instead of failing on the helper.
+pub fn dead_http_endpoint() -> String {
+ let Ok(listener) = TcpListener::bind("127.0.0.1:0") else {
+ return "http://127.0.0.1:1".to_string();
+ };
+ let Ok(addr) = listener.local_addr() else {
+ return "http://127.0.0.1:1".to_string();
+ };
+ std::thread::spawn(move || {
+ for conn in listener.incoming() {
+ drop(conn);
+ }
+ });
+ format!("http://127.0.0.1:{}", addr.port())
+}
+
+/// The first PowerShell on this machine that starts and exits cleanly,
+/// preferring Windows PowerShell and falling back to PowerShell 7 (`pwsh`).
+///
+/// Resolved once per process: probing spawns a shell, and the tests that need
+/// this spawn several themselves.
+///
+/// # Panics
+/// Panics if neither executable runs. The tests that call this cannot pass
+/// without one, so failing loudly beats a confusing spawn error later.
+#[cfg(windows)]
+pub fn powershell_exe() -> &'static str {
+ use std::process::{Command, Stdio};
+ use std::sync::OnceLock;
+
+ static RESOLVED: OnceLock<&'static str> = OnceLock::new();
+ RESOLVED.get_or_init(|| {
+ ["powershell.exe", "pwsh.exe"]
+ .into_iter()
+ .find(|exe| {
+ Command::new(exe)
+ .args([
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-Command",
+ "exit 0",
+ ])
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status()
+ .is_ok_and(|status| status.success())
+ })
+ .expect("PowerShell should be available")
+ })
+}
diff --git a/crates/ai-memory-web/build.rs b/crates/ai-memory-web/build.rs
index c8867697..cc984869 100644
--- a/crates/ai-memory-web/build.rs
+++ b/crates/ai-memory-web/build.rs
@@ -1,19 +1,18 @@
//! Build script for ai-memory-web.
//!
-//! Downloads the standalone Tailwind CSS CLI (pinned version) and
-//! compiles `static/input.css` → `OUT_DIR/tailwind.css`.
+//! The shipped stylesheet is the vendored `static/tailwind.css`, checked in
+//! and copied to `OUT_DIR` on every build, so a plain `cargo build` needs no
+//! network and no environment variable on any platform.
//!
-//! # Escape hatch
-//! Set `TAILWIND_SKIP=1` to skip the download entirely and use the
-//! vendored `static/tailwind.css` instead.
-//!
-//! # Incremental builds
-//! Also skips the download when `static/tailwind.css` is newer than
-//! every template file and `static/input.css`.
+//! # Regenerating the stylesheet
+//! `TAILWIND_BUILD=1 cargo build -p ai-memory-web` downloads the pinned
+//! standalone Tailwind CLI (checksum-verified), compiles `static/input.css`
+//! against `templates/`, and rewrites the vendored file for you to commit.
+//! CI runs that mode on Linux and fails if the committed file is stale, which
+//! is the only freshness check: mtimes on a fresh clone mean nothing.
use std::path::{Path, PathBuf};
use std::process::Command;
-use std::time::SystemTime;
use sha2::{Digest, Sha256};
@@ -30,89 +29,43 @@ const TAILWIND_WINDOWS_X64_SHA256: &str =
"67f1c5e3f5a03406a7bf5badf5ada09b79f3ae78ec43450c15f7e983068da346";
fn main() {
- // Re-run triggers.
println!("cargo:rerun-if-changed=build.rs");
- println!("cargo:rerun-if-changed=static/input.css");
- println!("cargo:rerun-if-changed=templates/");
- println!("cargo:rerun-if-env-changed=TAILWIND_SKIP");
+ println!("cargo:rerun-if-changed=static/tailwind.css");
+ println!("cargo:rerun-if-env-changed=TAILWIND_BUILD");
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let out_css = out_dir.join("tailwind.css");
let crate_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
-
- // Escape hatch: TAILWIND_SKIP=1 → use the vendored file.
- if std::env::var("TAILWIND_SKIP").as_deref() == Ok("1") {
- let src = crate_dir.join("static/tailwind.css");
- std::fs::copy(&src, &out_css)
- .unwrap_or_else(|e| panic!("TAILWIND_SKIP=1 but static/tailwind.css missing: {e}"));
- emit_env(&out_css);
- return;
- }
-
- // Incremental: skip if static/tailwind.css is newer than all sources.
let vendored = crate_dir.join("static/tailwind.css");
- if is_vendored_fresh(&vendored, &crate_dir) {
- std::fs::copy(&vendored, &out_css)
- .expect("failed to copy vendored tailwind.css to OUT_DIR");
- emit_env(&out_css);
- return;
- }
- // Download the tailwind binary (cached by version in OUT_DIR's parent).
- let binary = download_tailwind(&out_dir);
-
- // Run tailwind.
- compile_tailwind(&binary, &crate_dir, &out_css);
- // Also update the vendored copy so the next build is incremental.
- let _ = std::fs::copy(&out_css, &vendored);
+ if std::env::var("TAILWIND_BUILD").as_deref() == Ok("1") {
+ println!("cargo:rerun-if-changed=static/input.css");
+ println!("cargo:rerun-if-changed=templates/");
+ let binary = download_tailwind(&out_dir);
+ compile_tailwind(&binary, &crate_dir, &out_css);
+ // This is the explicit "regenerate" mode, so refreshing the vendored
+ // copy in the source tree is the point, not a side effect.
+ std::fs::copy(&out_css, &vendored)
+ .expect("failed to refresh the vendored static/tailwind.css");
+ } else {
+ std::fs::copy(&vendored, &out_css).unwrap_or_else(|e| {
+ panic!(
+ "static/tailwind.css missing ({e}); regenerate it with \
+ `TAILWIND_BUILD=1 cargo build -p ai-memory-web`"
+ )
+ });
+ }
emit_env(&out_css);
}
-fn emit_env(css_path: &Path) {
+fn emit_env(out_css: &Path) {
println!(
"cargo:rustc-env=AI_MEMORY_WEB_TAILWIND_CSS={}",
- css_path.display()
+ out_css.display()
);
}
-/// True when `static/tailwind.css` is newer than all template files
-/// and `static/input.css`.
-fn is_vendored_fresh(vendored: &Path, crate_dir: &Path) -> bool {
- let Ok(vmt) = mtime(vendored) else {
- return false;
- };
- // Check input.css.
- let input = crate_dir.join("static/input.css");
- if mtime(&input).map(|t| t > vmt).unwrap_or(false) {
- return false;
- }
- // Walk templates/.
- let tmpl_dir = crate_dir.join("templates");
- is_dir_older_than(&tmpl_dir, vmt)
-}
-
-fn is_dir_older_than(dir: &Path, threshold: SystemTime) -> bool {
- let Ok(entries) = std::fs::read_dir(dir) else {
- return true; // no templates → consider fresh
- };
- for entry in entries.flatten() {
- let path = entry.path();
- if path.is_dir() {
- if !is_dir_older_than(&path, threshold) {
- return false;
- }
- } else if mtime(&path).map(|t| t > threshold).unwrap_or(false) {
- return false;
- }
- }
- true
-}
-
-fn mtime(p: &Path) -> std::io::Result<SystemTime> {
- std::fs::metadata(p)?.modified()
-}
-
/// Platform-specific download URL for the Tailwind CLI binary.
fn tailwind_url() -> String {
let slug = tailwind_slug();
@@ -131,7 +84,7 @@ fn tailwind_slug() -> &'static str {
("macos", "aarch64") => "tailwindcss-macos-arm64",
("windows", "x86_64") => "tailwindcss-windows-x64.exe",
_ => panic!(
- "Unsupported platform {os}/{arch} — set TAILWIND_SKIP=1 and provide static/tailwind.css manually"
+ "Unsupported platform {os}/{arch} for TAILWIND_BUILD=1; regenerate static/tailwind.css on a supported one"
),
}
}
@@ -143,9 +96,7 @@ fn expected_tailwind_sha256() -> &'static str {
"tailwindcss-macos-x64" => TAILWIND_MACOS_X64_SHA256,
"tailwindcss-macos-arm64" => TAILWIND_MACOS_ARM64_SHA256,
"tailwindcss-windows-x64.exe" => TAILWIND_WINDOWS_X64_SHA256,
- other => panic!(
- "No pinned Tailwind SHA-256 for {other}. Set TAILWIND_SKIP=1 and provide static/tailwind.css manually."
- ),
+ other => panic!("No pinned Tailwind SHA-256 for {other}"),
}
}
@@ -199,9 +150,8 @@ fn download_tailwind(out_dir: &Path) -> PathBuf {
if !success {
panic!(
- "Could not download Tailwind CSS CLI — curl, wget, and PowerShell all failed.\n\
- Either install one of those download tools, OR set TAILWIND_SKIP=1 and place a compiled \
- tailwind.css in crates/ai-memory-web/static/tailwind.css."
+ "Could not download the Tailwind CSS CLI: curl, wget, and PowerShell all failed. \
+ Install one of them, or regenerate static/tailwind.css on another machine."
);
}
diff --git a/crates/ai-memory-web/src/lib.rs b/crates/ai-memory-web/src/lib.rs
index a1b25e17..aa0e3de7 100644
--- a/crates/ai-memory-web/src/lib.rs
+++ b/crates/ai-memory-web/src/lib.rs
@@ -64,3 +64,13 @@ pub fn api_router(reader: ReaderPool, wiki: Wiki) -> Router {
pub fn favicon_router() -> Router {
routes::build_favicon()
}
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_web;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-web/tests/suite/mod.rs b/crates/ai-memory-web/tests/suite/mod.rs
new file mode 100644
index 00000000..5ed2bee8
--- /dev/null
+++ b/crates/ai-memory-web/tests/suite/mod.rs
@@ -0,0 +1,5 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod routes;
diff --git a/crates/ai-memory-web/tests/routes.rs b/crates/ai-memory-web/tests/suite/routes.rs
similarity index 100%
rename from crates/ai-memory-web/tests/routes.rs
rename to crates/ai-memory-web/tests/suite/routes.rs
diff --git a/crates/ai-memory-wiki/src/lib.rs b/crates/ai-memory-wiki/src/lib.rs
index 85dae69d..37aea528 100644
--- a/crates/ai-memory-wiki/src/lib.rs
+++ b/crates/ai-memory-wiki/src/lib.rs
@@ -26,3 +26,13 @@ pub use markdown::{Markdown, derive_title, emit, parse};
pub use migrations::run_pending as run_wiki_migrations;
pub use watcher::{DEBOUNCE_WINDOW, RECONCILE_INTERVAL, WatcherHandle};
pub use wiki::{MoveSessionOutcome, SessionPageFile, Wiki, WritePageRequest};
+
+// Integration tests compile into this crate's test harness instead of a
+// separate binary: every test binary is another link and, on macOS and
+// Windows, another first-run malware scan. They still exercise only the
+// public API; `extern crate self` lets them keep addressing it by crate name.
+#[cfg(test)]
+extern crate self as ai_memory_wiki;
+#[cfg(test)]
+#[path = "../tests/suite/mod.rs"]
+mod integration;
diff --git a/crates/ai-memory-wiki/tests/admission.rs b/crates/ai-memory-wiki/tests/suite/admission.rs
similarity index 99%
rename from crates/ai-memory-wiki/tests/admission.rs
rename to crates/ai-memory-wiki/tests/suite/admission.rs
index 79e3d901..5b006aa6 100644
--- a/crates/ai-memory-wiki/tests/admission.rs
+++ b/crates/ai-memory-wiki/tests/suite/admission.rs
@@ -29,8 +29,9 @@ async fn spawn_server(app: Router) -> String {
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
- // Give the runtime a tick to start accepting.
- tokio::time::sleep(Duration::from_millis(20)).await;
+ // No settle needed: the socket is listening as soon as `bind` returns, so a
+ // client connecting before the serve task is polled just queues in the
+ // backlog.
format!("http://{addr}")
}
diff --git a/crates/ai-memory-wiki/tests/suite/mod.rs b/crates/ai-memory-wiki/tests/suite/mod.rs
new file mode 100644
index 00000000..579699a2
--- /dev/null
+++ b/crates/ai-memory-wiki/tests/suite/mod.rs
@@ -0,0 +1,5 @@
+//! This crate's integration tests. Every file here is a module of the lib's
+//! test harness (see the `integration` module in `src/lib.rs`), so they cost
+//! no extra binary; a new file must be declared below.
+
+mod admission;
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 8207b461..ad0629d8 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -28,7 +28,7 @@ COPY evals ./evals
COPY docs ./docs
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/work/target \
- TAILWIND_SKIP=1 cargo build --locked --release -p ai-memory-cli \
+ cargo build --locked --release -p ai-memory-cli \
&& cp target/release/ai-memory /usr/local/bin/ai-memory
FROM debian:bookworm-slim AS runtime-base
diff --git a/docs/managed-harness-contributions.md b/docs/managed-harness-contributions.md
index c8c74c91..32564e1a 100644
--- a/docs/managed-harness-contributions.md
+++ b/docs/managed-harness-contributions.md
@@ -167,6 +167,6 @@ Run the repository's complete Rust gate before requesting review:
```bash
cargo fmt --check
git diff --check
-TAILWIND_SKIP=1 cargo test --workspace
-TAILWIND_SKIP=1 cargo clippy --workspace --all-targets -- -D warnings
+cargo test --workspace
+cargo clippy --workspace --all-targets -- -D warnings
```
diff --git a/docs/windows.md b/docs/windows.md
index d16d5248..187f0423 100644
--- a/docs/windows.md
+++ b/docs/windows.md
@@ -265,10 +265,10 @@ cargo build --locked --release -p ai-memory-cli
The version output should match the package version for the checkout.
-The Tailwind build step supports the pinned
+Normal builds use the vendored stylesheet and download nothing. Regenerating
+it (`TAILWIND_BUILD=1 cargo build -p ai-memory-web`) supports the pinned
`tailwindcss-windows-x64.exe` binary and falls back to PowerShell
-`Invoke-WebRequest` when `curl`/`wget` are unavailable. You should not
-need `TAILWIND_SKIP=1` for normal Windows builds.
+`Invoke-WebRequest` when `curl`/`wget` are unavailable.
Keep Git for Windows' `git.exe` on `PATH` for native builds and hook runs. When
libgit2 hits a Windows path-resolution error while opening a newly initialized
diff --git a/scripts/install-git-hooks.sh b/scripts/install-git-hooks.sh
new file mode 100644
index 00000000..a23104e3
--- /dev/null
+++ b/scripts/install-git-hooks.sh
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+# Installs this repo's pre-push hook into .git/hooks without discarding an
+# existing user hook. Run once per clone (from Git Bash on Windows):
+#
+# scripts/install-git-hooks.sh
+#
+# The hook runs the full test tier (`cargo tf`) before a push. The everyday
+# `cargo t` skips the slow/stress tier, and skipping in the inner loop is only
+# safe if something catches it later. Bypass for a work-in-progress branch with
+# `git push --no-verify`.
+
+set -euo pipefail
+
+repo_root=$(git rev-parse --show-toplevel)
+hook="$repo_root/.git/hooks/pre-push"
+begin="# >>> ai-memory pre-push >>>"
+end="# <<< ai-memory pre-push <<<"
+tmp=$(mktemp "${hook}.XXXXXX")
+trap 'rm -f "$tmp"' EXIT
+
+if [[ -f "$hook" ]]; then
+ awk -v begin="$begin" -v end="$end" '
+ $0 == begin { skip = 1; next }
+ $0 == end { skip = 0; next }
+ !skip { print }
+ ' "$hook" > "$tmp"
+ if grep -q '[^[:space:]]' "$tmp"; then
+ printf '\n' >> "$tmp"
+ else
+ printf '%s\n\n' '#!/usr/bin/env bash' '# Installed by scripts/install-git-hooks.sh.' > "$tmp"
+ fi
+else
+ printf '%s\n\n' '#!/usr/bin/env bash' '# Installed by scripts/install-git-hooks.sh.' > "$tmp"
+fi
+
+cat >> "$tmp" <<'HOOK'
+# >>> ai-memory pre-push >>>
+# Runs the full test tier before a push. See scripts/install-git-hooks.sh.
+set -euo pipefail
+
+# macOS: stop reqwest re-reading the Keychain in every test process.
+if [ "$(uname -s 2>/dev/null || true)" = "Darwin" ] && [ -z "${SSL_CERT_FILE:-}" ] && [ -f /etc/ssl/cert.pem ]; then
+ export SSL_CERT_FILE=/etc/ssl/cert.pem
+fi
+
+if command -v cargo-nextest >/dev/null 2>&1; then
+ echo "pre-push: cargo nextest run --workspace -P full"
+ cargo nextest run --workspace -P full
+else
+ echo "pre-push: cargo test --workspace --all-targets (nextest not installed)"
+ cargo test --workspace --all-targets
+fi
+# <<< ai-memory pre-push <<<
+HOOK
+
+mv "$tmp" "$hook"
+trap - EXIT
+chmod +x "$hook"
+echo "installed $hook"
diff --git a/scripts/managed-workstream-acceptance.sh b/scripts/managed-workstream-acceptance.sh
index 6267e37a..ba043769 100755
--- a/scripts/managed-workstream-acceptance.sh
+++ b/scripts/managed-workstream-acceptance.sh
@@ -38,7 +38,7 @@ for command in cargo curl diff git jq script sqlite3; do
done
if [ ! -x "$BIN" ] || [ "${AI_MEMORY_ACCEPTANCE_REBUILD:-1}" = 1 ]; then
- (cd "$ROOT" && TAILWIND_SKIP=1 cargo build -p ai-memory-cli)
+ (cd "$ROOT" && cargo build -p ai-memory-cli)
fi
mkdir -p "$DATA" "$REPO" "$CONFIG" "$LOGS"
diff --git a/scripts/test-native-arch-systemd-distrobox.sh b/scripts/test-native-arch-systemd-distrobox.sh
index 40811e15..1c7134f8 100755
--- a/scripts/test-native-arch-systemd-distrobox.sh
+++ b/scripts/test-native-arch-systemd-distrobox.sh
@@ -78,7 +78,6 @@ run_inside() {
(cd packaging/aur && makepkg --printsrcinfo -p PKGBUILD-bin) >/tmp/ai-memory-bin.PKGBUILD.SRCINFO
log "Building ai-memory release binary from current working tree"
- export TAILWIND_SKIP=1
cargo build --release -p ai-memory-cli
log "Installing native package layout into the disposable distrobox"