Merge remote-tracking branch 'origin/main' into feat/projects-copy-link

Signed-off-by: Thomas Petersen <thomasp@squareup.com>
This commit is contained in:
Thomas Petersen
2026-08-10 18:16:39 -04:00
152 changed files with 15835 additions and 1930 deletions
+20 -2
View File
@@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
fetch-depth: 2
- uses: dorny/paths-filter@7b450fff21473bca461d4b92ce414b9d0420d706 # v4.0.2
- uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
id: filter
with:
token: ''
@@ -78,6 +78,10 @@ jobs:
run: scripts/test-release-ref-contract.sh
- name: Desktop release candidate contract
run: scripts/test-desktop-release-candidate.sh
- name: OSS desktop promotion contract
run: |
scripts/test-oss-desktop-promotion.sh
scripts/test-oss-desktop-promotion-behavior.sh
- name: Mobile release contract
run: |
scripts/test-mobile-release-contract.sh
@@ -317,6 +321,9 @@ jobs:
if: github.event_name == 'push' || needs.changes.outputs.desktop == 'true' || needs.changes.outputs.desktop-rust == 'true' || needs.changes.outputs.rust == 'true'
permissions:
contents: read
env:
SCCACHE_GHA_ENABLED: "true"
SCCACHE_GHA_RW_MODE: ${{ (github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.number == 5224)) && 'READ_WRITE' || 'READ_ONLY' }}
steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
- uses: cashapp/activate-hermit@cea9af7913204a965fd488637a8d1811bba2e616 # v1
@@ -341,6 +348,13 @@ jobs:
.
desktop/src-tauri
save-if: ${{ github.event_name != 'pull_request' }}
# Cache rustc outputs for unchanged workspace crates. Trusted pushes write;
# the bounded PR 5224 trial writes only to its isolated merge-ref scope.
- name: Set up sccache
if: steps.relay-artifacts-cache.outputs.cache-hit != 'true'
uses: Mozilla-Actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 # zizmor: ignore[cache-poisoning] Bounded trial: only PR 5224 writes to its isolated merge-ref scope; trusted pushes retain production writes.
with:
version: v0.16.0
- name: Install cargo-nextest
if: steps.relay-artifacts-cache.outputs.cache-hit != 'true'
uses: taiki-e/install-action@0fd46367812ee04360509b4169d9f659d6892bb2 # v2.79.15
@@ -348,6 +362,8 @@ jobs:
tool: cargo-nextest@0.9.136
- name: Build relay artifacts
if: steps.relay-artifacts-cache.outputs.cache-hit != 'true'
env:
RUSTC_WRAPPER: sccache
run: |
cargo build --profile ci -p buzz-relay -p git-credential-nostr
cargo nextest archive \
@@ -359,7 +375,9 @@ jobs:
--test e2e_event_reminder \
--archive-file target/ci/backend-integration-tests.tar.zst
- name: Save relay artifacts cache
if: steps.relay-artifacts-cache.outputs.cache-hit != 'true'
# PR-scoped exact-source entries cannot warm main or other PRs and churn
# the shared cache pool. sccache provides read-only PR reuse instead.
if: steps.relay-artifacts-cache.outputs.cache-hit != 'true' && github.event_name == 'push'
uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5
with:
path: |
@@ -0,0 +1,45 @@
name: Promote OSS Desktop Auto-Update
run-name: Promote desktop-v${{ inputs.version }} to auto-update
on:
workflow_dispatch:
inputs:
version:
description: Stable desktop version to promote (X.Y.Z)
required: true
type: string
concurrency:
group: oss-desktop-auto-update-promotion
cancel-in-progress: false
permissions:
contents: read
jobs:
promote:
if: github.repository == 'block/buzz'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
steps:
- name: Require the reviewed workflow from main
env:
DISPATCH_REF: ${{ github.ref }}
run: |
if [ "$DISPATCH_REF" != "refs/heads/main" ]; then
echo "::error::OSS desktop promotion must be dispatched from main, not $DISPATCH_REF"
exit 1
fi
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
ref: ${{ github.sha }}
persist-credentials: false
- name: Validate and promote exact release manifest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ inputs.version }}
run: scripts/promote-oss-desktop-release.sh "$VERSION"
+1 -4
View File
@@ -907,6 +907,7 @@ jobs:
[ "${#TRIPLES[@]}" -ge 3 ] || { echo "::error::too few platforms (${#TRIPLES[@]})"; exit 1; }
bash desktop/scripts/generate-oss-latest-json.sh "$VERSION" "${TRIPLES[@]}" > latest.json
cat latest.json
cp latest.json staged/updater-manifest.json
- name: Create or verify versioned draft
run: |
@@ -946,7 +947,3 @@ jobs:
- name: Publish complete versioned release
if: env.already_published != 'true'
run: gh release edit "desktop-v${VERSION}" --draft=false
- name: Upload latest.json to rolling release last
if: ${{ !contains(needs.setup.outputs.version, '-') }}
run: gh release upload buzz-desktop-latest latest.json --clobber
Generated
+46 -34
View File
@@ -117,7 +117,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -128,14 +128,14 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
name = "anyhow"
version = "1.0.103"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "appattest"
@@ -172,9 +172,9 @@ dependencies = [
[[package]]
name = "arc-swap"
version = "1.9.1"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a3a1fd6f75306b68087b831f025c712524bcb19aad54e557b1129cfa0a2b207"
checksum = "c049c0be4daef0b145cb3555416b3b8ef5b7888a38aea1a3a155801fe7b0810b"
dependencies = [
"rustversion",
]
@@ -235,9 +235,9 @@ dependencies = [
[[package]]
name = "async-compression"
version = "0.4.42"
version = "0.4.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e79b3f8a79cccc2898f31920fc69f304859b3bd567490f75ebf51ae1c792a9ac"
checksum = "3976abdc8fe7d1133d43d304afd42abdf5bc3e1319d263d223bde07b5efc4be8"
dependencies = [
"compression-codecs",
"compression-core",
@@ -329,13 +329,13 @@ checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de"
[[package]]
name = "async-trait"
version = "0.1.89"
version = "0.1.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
checksum = "ae36dc4177970ef04fde5178d3e2429882def40e57a451f919c098f72baa6cec"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
"syn 3.0.3",
]
[[package]]
@@ -876,6 +876,7 @@ dependencies = [
"tokio",
"tracing",
"tracing-subscriber",
"url",
"urlencoding",
"webbrowser",
]
@@ -1557,9 +1558,9 @@ dependencies = [
[[package]]
name = "clap"
version = "4.6.1"
version = "4.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
checksum = "473c7e07f409a8d772161724aa8db6a765a2532a70f9667eeb7b49d3d02fbdca"
dependencies = [
"clap_builder",
"clap_derive",
@@ -1567,9 +1568,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.6.0"
version = "4.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
checksum = "7b48fea5a88e9ae728a2dcbedbfc0e730f7d60da42e1cb049a83c9fb8b789889"
dependencies = [
"anstream",
"anstyle",
@@ -1579,14 +1580,14 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "4.6.1"
version = "4.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
checksum = "d012d2b9d65aca7f18f4d9878a045bc17899bba951561ba5ec3c2ba1eed9a061"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.117",
"syn 3.0.3",
]
[[package]]
@@ -1637,7 +1638,7 @@ version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -2285,7 +2286,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccc2776f0c61eca1ca32528f85548abd1a4be8fb53d1b21c013e4f18da1e7090"
dependencies = [
"data-encoding",
"syn 2.0.117",
"syn 1.0.109",
]
[[package]]
@@ -2448,9 +2449,9 @@ checksum = "ab03c107fafeb3ee9f5925686dbb7a73bc76e3932abb0d2b365cb64b169cf04c"
[[package]]
name = "diffy"
version = "0.5.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05264ab2aab4fb952fc4b0f3f6eff1ddfb4563064053a4ea174d91537584a769"
checksum = "10aec8f7f9393bd6a4f2762be0ceb012d3cbe2478987258cc9960de148561914"
dependencies = [
"hashbrown 0.17.1",
]
@@ -2496,7 +2497,7 @@ dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@@ -2734,7 +2735,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -3140,7 +3141,7 @@ dependencies = [
"libc",
"log",
"rustversion",
"windows-link 0.2.1",
"windows-link 0.1.3",
"windows-result 0.4.1",
]
@@ -5902,7 +5903,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@@ -8042,7 +8043,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -8101,7 +8102,7 @@ dependencies = [
"security-framework 3.7.0",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -8373,7 +8374,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b55fb86dfd3a2f5f76ea78310a88f96c4ea21a3031f8d212443d56123fd0521"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -8868,7 +8869,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -9380,6 +9381,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "syn"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "sync_wrapper"
version = "1.0.2"
@@ -9485,7 +9497,7 @@ dependencies = [
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.4",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -9498,7 +9510,7 @@ dependencies = [
"parking_lot",
"rustix 1.1.4",
"signal-hook",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -10234,7 +10246,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e"
dependencies = [
"memoffset",
"tempfile",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -10812,7 +10824,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
+2 -2
View File
@@ -148,8 +148,8 @@ buzz-relay-mesh = { path = "crates/buzz-relay-mesh" }
# release optimization (warm from main's cache; they carry the runtime hot
# path: tokio/sqlx/axum). Workspace crates build at opt-level 1 — enough for
# stable e2e timing (PR #307 flakiness was opt-0 + debug-assertions) at
# roughly half the codegen cost. `incremental` is irrelevant in CI:
# rust-cache exports CARGO_INCREMENTAL=0 and never caches member artifacts.
# roughly half the codegen cost. Incremental stays disabled: rust-cache exports
# CARGO_INCREMENTAL=0, and the CI compiler cache requires non-incremental units.
[profile.ci]
inherits = "release"
lto = false
+23 -6
View File
@@ -173,10 +173,10 @@ for distributable builds or builds from an immutable release tag.
`release.yml` has no manual dispatch and cannot build from `main` or another
caller-selected ref. If a run for an existing immutable
`desktop-v<version>` tag fails, rerun that failed workflow from GitHub Actions
(or use `gh run rerun <run-id> --failed --repo block/buzz`). A stable rerun also
repairs `buzz-desktop-latest/latest.json` if the original run published the
versioned release but failed during that final rolling-manifest upload. Do not
recreate, move, or push the immutable tag again.
(or use `gh run rerun <run-id> --failed --repo block/buzz`). A rerun
repairs the versioned draft if publication did not complete. It does not
promote that version to the auto-updater; promotion is a separate manual
action. Do not recreate, move, or push the immutable tag again.
Mobile intentionally has no branch or arbitrary-ref fallback. The private
Buildkite pipeline accepts only an exact candidate tag.
@@ -200,8 +200,25 @@ for the rest of the private pipeline contract.
Desktop publishes two GitHub releases:
1. **`desktop-v<version>`**: the user-facing release with installers.
2. **`buzz-desktop-latest`**: the rolling auto-updater release.
1. **`desktop-v<version>`**: the user-facing release with installers and the
exact `updater-manifest.json` promotion candidate. Publishing this release
does not expose it through in-app auto-update.
2. **`buzz-desktop-latest`**: the rolling auto-updater release. Its
`latest.json` changes only through the manual promotion workflow.
### Promote an OSS desktop release to auto-update
After installing and testing the published `desktop-v<version>` artifacts, run
**Promote OSS Desktop Auto-Update** from the `main` branch and enter the exact
stable `X.Y.Z` version. The workflow validates the immutable tag and release,
the retained manifest and every referenced updater asset, and requires the
version to be newer than the currently promoted version before replacing
`buzz-desktop-latest/latest.json`. Same-version retries succeed only when the
manifest is identical; downgrades are rejected.
Withholding promotion leaves existing clients on the previous version. If a
promoted release is bad, ship and promote a higher patch version; changing the
manifest to an older version does not downgrade clients that already updated.
Mobile publishes only annotated `mobile-vX.Y.Z-rc.N` git tags. Store artifacts
and rollout records retain the exact tag they used. Mobile does not publish a
+1 -1
View File
@@ -39,7 +39,7 @@ The relay enforces all access control. Channel membership is the only gate.
| Type | Visibility | Join | Create |
|------|-----------|------|--------|
| **Open channels** | Searchable by all members | Self-join | Any member |
| **Private channels** | Hidden, invite-only | Invited by an owner/admin | Any member |
| **Private channels** | Hidden, invite-only | Invited by member | Any member |
| **DMs** | Participants only | N/A (up to 9) | Any member |
| **Guests** | Scoped to specific channels | Invited | N/A |
+26 -12
View File
@@ -881,6 +881,16 @@ impl AcpClient {
self.goose_usage.take()
}
/// Notify the usage tracker that buzz-acp just spawned a new session.
///
/// Seeds a zero baseline so the first usage notification for `session_id`
/// produces `delta_reliable: true` (turn delta == cumulative from zero).
/// Must be called only when buzz-acp created the session via `session/new`;
/// never when attaching to a pre-existing session.
pub(crate) fn notify_session_spawned(&mut self, session_id: &str) {
self.goose_usage.seed_zero_baseline(session_id);
}
/// Install a per-turn steer request channel for goose-native
/// non-cancelling mid-turn delivery.
///
@@ -1607,7 +1617,9 @@ impl AcpClient {
"steer accepted as {STEER_OUTCOME_STARTED_NEW_TURN}: \
awaited turn had ended — hard deadline not renewed"
);
crate::pool::SteerAck::Success
crate::pool::SteerAck::Success {
session_id: session_id.to_owned(),
}
}
Some(_) => {
let renew_now = Instant::now();
@@ -1619,7 +1631,9 @@ impl AcpClient {
"steer success: renewed hard deadline ({max_duration:?} from now)"
);
}
crate::pool::SteerAck::Success
crate::pool::SteerAck::Success {
session_id: session_id.to_owned(),
}
}
None => {
// Report the raw string when
@@ -1849,8 +1863,8 @@ impl AcpClient {
tracing::debug!(
target: "acp::usage",
session_id = %notif.session_id,
input = payload.accumulated_input_tokens,
output = payload.accumulated_output_tokens,
input = ?payload.accumulated_input_tokens,
output = ?payload.accumulated_output_tokens,
// A subset of `input`, logged so downstream accounting can
// price it at the provider's cached rate. Always emitted,
// including as 0, so a parser can tell "no cache hits"
@@ -3789,7 +3803,7 @@ mod tests {
.await
.expect("ack oneshot must have received a SteerAck");
match ack {
crate::pool::SteerAck::Success => {}
crate::pool::SteerAck::Success { .. } => {}
other => panic!("expected SteerAck::Success, got {other:?}"),
}
}
@@ -3850,7 +3864,7 @@ mod tests {
.await
.expect("ack oneshot must have received a SteerAck");
match ack {
crate::pool::SteerAck::Success => {}
crate::pool::SteerAck::Success { .. } => {}
other => panic!("expected SteerAck::Success, got {other:?}"),
}
}
@@ -4034,7 +4048,7 @@ mod tests {
"_session/steering must not carry expectedRunId; wrote: {written}"
);
assert!(
matches!(ack, crate::pool::SteerAck::Success),
matches!(ack, crate::pool::SteerAck::Success { .. }),
"injected outcome must ack Success, got {ack:?}"
);
}
@@ -4066,7 +4080,7 @@ mod tests {
// no `outcome`) — the OutcomeRejected guard applies only to
// `_session/steering`.
assert!(
matches!(ack, crate::pool::SteerAck::Success),
matches!(ack, crate::pool::SteerAck::Success { .. }),
"goose success result must ack Success, got {ack:?}"
);
}
@@ -4171,7 +4185,7 @@ mod tests {
assert_eq!(result.unwrap()["done"], serde_json::json!(true));
let ack = ack_rx.await.expect("ack must be received");
assert!(
matches!(ack, crate::pool::SteerAck::Success),
matches!(ack, crate::pool::SteerAck::Success { .. }),
"injected must ack Success, got {ack:?}"
);
}
@@ -4228,7 +4242,7 @@ mod tests {
// rather than released — hence Success, not an Err.
let ack = ack_rx.await.expect("ack must be received");
assert!(
matches!(ack, crate::pool::SteerAck::Success),
matches!(ack, crate::pool::SteerAck::Success { .. }),
"startedNewTurn is a delivery success, got {ack:?}"
);
}
@@ -4306,8 +4320,8 @@ mod tests {
assert_eq!(usage.session_id, "s1");
assert_eq!(usage.turn_seq, 1);
assert!(!usage.delta_reliable, "first turn must be unreliable");
assert_eq!(usage.cumulative_input_tokens, 1000);
assert_eq!(usage.cumulative_output_tokens, 200);
assert_eq!(usage.cumulative_input_tokens, Some(1000));
assert_eq!(usage.cumulative_output_tokens, Some(200));
assert_eq!(usage.cumulative_cost_usd, Some(0.01));
// Second take must be None.
+314 -5
View File
@@ -2821,7 +2821,7 @@ async fn tokio_main() -> Result<()> {
// treat as PromptCompletedNeutral to avoid leaking
// the withheld event in `withheld_native_steer`.
let (release_withheld, drop_withheld, signal_fallback) = match &ack {
Ok(pool::SteerAck::Success) => (false, true, false),
Ok(pool::SteerAck::Success { .. }) => (false, true, false),
// -32601 = method_not_found: agent does not implement the
// steer extension. Fire cancel+merge so the message still
// reaches the agent.
@@ -2854,8 +2854,19 @@ async fn tokio_main() -> Result<()> {
signal_fallback,
"non-cancelling steer ack received"
);
if matches!(ack, Ok(pool::SteerAck::Success)) {
if let Ok(pool::SteerAck::Success { session_id }) = &ack {
queue.extend_in_flight_deadline(channel_id, config.max_turn_duration_secs);
if !pool.record_successful_steer(
channel_id,
event_id.clone(),
session_id.clone(),
) {
tracing::warn!(
channel = %channel_id,
event_id = %event_id,
"successful steer lost its in-flight delivery ledger"
);
}
}
if drop_withheld {
queue.remove_event(channel_id, &event_id);
@@ -3318,6 +3329,7 @@ fn dispatch_pending(
recoverable_batch,
control_tx: Some(control_tx),
steer_tx,
successful_steer_deliveries: HashSet::new(),
},
);
dispatched_channels.push((channel_id, typing_scope));
@@ -3399,9 +3411,30 @@ fn handle_prompt_result(
) -> LoopAction {
let before = pool.task_map().len();
let agent_index = result.agent.index;
let successful_steer_deliveries = pool
.task_map()
.values()
.find(|meta| meta.agent_index == agent_index)
.map(|meta| meta.successful_steer_deliveries.clone())
.unwrap_or_default();
pool.task_map_mut()
.retain(|_, meta| meta.agent_index != agent_index);
debug_assert_eq!(before, pool.task_map().len() + 1);
if let PromptSource::Channel(channel_id) = &result.source {
// The task may have invalidated this session before returning. Never
// resurrect delivery state for a dead session; its replacement must
// receive fresh standing context and history.
if let Some(live_session_id) = result.agent.state.sessions.get(channel_id).cloned() {
let event_ids = successful_steer_deliveries
.into_iter()
.filter(|delivery| delivery.session_id == live_session_id)
.map(|delivery| delivery.event_id);
result
.agent
.state
.mark_channel_delivery_success(*channel_id, false, event_ids);
}
}
// The hard-timeout death_message (below) must describe the batch's
// *actual* fate, not just the `recently_active` eligibility flag — a
@@ -3932,6 +3965,7 @@ fn dispatch_heartbeat(
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
*heartbeat_in_flight = true;
@@ -4574,17 +4608,23 @@ mod heartbeat_base_prompt_tests {
// heartbeat user message, composed as `[Base]\n{bp}\n\n{prompt}`. This is
// the second half of the round-2 regression (the first being initial_message).
fn heartbeat_standing() -> queue::StandingContext<'static> {
queue::StandingContext {
base_prompt: Some("you are a helpful agent"),
..Default::default()
}
}
#[test]
fn test_heartbeat_legacy_agent_gets_base_prepended() {
// protocol_version 1 + Some(base_prompt): heartbeat prompt is prefixed
// with the [Base] section exactly as the legacy session/new path would.
let prompt = "[System: Heartbeat]\nrun feed get";
let composed = pool::prepend_base_for_legacy(1, Some("you are a helpful agent"), prompt);
let composed = pool::prepend_standing_for_legacy(1, &heartbeat_standing(), prompt);
assert_eq!(
composed,
"[Base]\nyou are a helpful agent\n\n[System: Heartbeat]\nrun feed get"
);
assert!(composed.starts_with("[Base]\nyou are a helpful agent\n\n"));
}
#[test]
@@ -4592,7 +4632,7 @@ mod heartbeat_base_prompt_tests {
// protocol_version 2 gets base_prompt via session/new; the heartbeat
// prompt is sent verbatim.
let prompt = "[System: Heartbeat]\nrun feed get";
let composed = pool::prepend_base_for_legacy(2, Some("you are a helpful agent"), prompt);
let composed = pool::prepend_standing_for_legacy(2, &heartbeat_standing(), prompt);
assert_eq!(composed, prompt);
}
}
@@ -4703,6 +4743,7 @@ mod owner_control_command_tests {
recoverable_batch: None,
control_tx: Some(control_tx),
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
@@ -6473,6 +6514,263 @@ mod error_outcome_emission_tests {
}
}
#[tokio::test]
async fn successful_native_steer_is_transferred_to_live_session_delivery_state() {
let channel_id = Uuid::new_v4();
let steer_event_id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let mut agent = dummy_agent(0).await;
agent
.state
.sessions
.insert(channel_id, "live-session".into());
agent
.state
.deliveries
.insert(channel_id, Default::default());
let mut pool = AgentPool::from_slots(vec![None]);
let task_id = pool.join_set.spawn(async {}).id();
pool.task_map_mut().insert(
task_id,
crate::pool::TaskMeta {
agent_index: 0,
channel_id: Some(channel_id),
turn_id: "test-turn-id".into(),
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::from([
crate::pool::SuccessfulSteerDelivery {
event_id: steer_event_id.into(),
session_id: "live-session".into(),
},
]),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
let config = test_config();
let mut heartbeat_in_flight = false;
let removed_channels = HashSet::new();
let mut crash_history = vec![SlotCircuit {
crash_times: Vec::new(),
open_until: None,
respawn_in_flight: false,
}];
let (respawn_tx, _respawn_rx) = mpsc::channel(8);
let mut respawn_tasks = tokio::task::JoinSet::new();
let result = PromptResult {
agent,
source: PromptSource::Channel(channel_id),
turn_id: "test-turn-id".into(),
outcome: PromptOutcome::Ok(crate::acp::StopReason::EndTurn),
batch: None,
};
handle_prompt_result(
&mut pool,
&mut queue,
&config,
result,
&mut heartbeat_in_flight,
&removed_channels,
&mut crash_history,
&respawn_tx,
&mut respawn_tasks,
None,
None,
);
let returned = pool.agents_mut()[0].as_ref().expect("returned agent");
assert!(returned.state.deliveries[&channel_id]
.delivered_event_ids
.contains(steer_event_id));
}
#[tokio::test]
async fn in_flight_stale_native_steer_ack_cannot_update_replacement_session() {
let channel_id = Uuid::new_v4();
let mut agent = dummy_agent(0).await;
agent
.state
.sessions
.insert(channel_id, "replacement-session".into());
agent
.state
.deliveries
.insert(channel_id, Default::default());
let mut pool = AgentPool::from_slots(vec![None]);
let task_id = pool.join_set.spawn(async {}).id();
pool.task_map_mut().insert(
task_id,
crate::pool::TaskMeta {
agent_index: 0,
channel_id: Some(channel_id),
turn_id: "test-turn-id".into(),
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::from([
crate::pool::SuccessfulSteerDelivery {
event_id: "stale-event".into(),
session_id: "old-session".into(),
},
]),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
let config = test_config();
let mut heartbeat_in_flight = false;
let removed_channels = HashSet::new();
let mut crash_history = vec![SlotCircuit {
crash_times: Vec::new(),
open_until: None,
respawn_in_flight: false,
}];
let (respawn_tx, _respawn_rx) = mpsc::channel(8);
let mut respawn_tasks = tokio::task::JoinSet::new();
let result = PromptResult {
agent,
source: PromptSource::Channel(channel_id),
turn_id: "test-turn-id".into(),
outcome: PromptOutcome::Ok(crate::acp::StopReason::EndTurn),
batch: None,
};
handle_prompt_result(
&mut pool,
&mut queue,
&config,
result,
&mut heartbeat_in_flight,
&removed_channels,
&mut crash_history,
&respawn_tx,
&mut respawn_tasks,
None,
None,
);
let returned = pool.agents_mut()[0].as_ref().expect("returned agent");
assert!(returned.state.deliveries[&channel_id]
.delivered_event_ids
.is_empty());
}
#[tokio::test]
async fn successful_native_steer_ack_after_task_return_updates_matching_live_session() {
let channel_id = Uuid::new_v4();
let steer_event_id = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
let mut agent = dummy_agent(0).await;
agent
.state
.sessions
.insert(channel_id, "live-session".into());
agent
.state
.deliveries
.insert(channel_id, Default::default());
let mut pool = AgentPool::from_slots(vec![Some(agent)]);
assert!(pool.record_successful_steer(
channel_id,
steer_event_id.into(),
"live-session".into(),
));
let returned = pool.agents_mut()[0].as_ref().expect("idle returned agent");
assert!(returned.state.deliveries[&channel_id]
.delivered_event_ids
.contains(steer_event_id));
}
#[tokio::test]
async fn late_native_steer_ack_cannot_update_replacement_session() {
let channel_id = Uuid::new_v4();
let mut agent = dummy_agent(0).await;
agent
.state
.sessions
.insert(channel_id, "replacement-session".into());
agent
.state
.deliveries
.insert(channel_id, Default::default());
let mut pool = AgentPool::from_slots(vec![Some(agent)]);
assert!(!pool.record_successful_steer(
channel_id,
"stale-event".into(),
"old-session".into(),
));
let returned = pool.agents_mut()[0].as_ref().expect("replacement agent");
assert!(returned.state.deliveries[&channel_id]
.delivered_event_ids
.is_empty());
}
#[tokio::test]
async fn invalidated_session_does_not_resurrect_successful_steer_delivery_state() {
let channel_id = Uuid::new_v4();
let agent = dummy_agent(0).await;
// No live session: simulates the prompt task invalidating before return.
let mut pool = AgentPool::from_slots(vec![None]);
let task_id = pool.join_set.spawn(async {}).id();
pool.task_map_mut().insert(
task_id,
crate::pool::TaskMeta {
agent_index: 0,
channel_id: Some(channel_id),
turn_id: "test-turn-id".into(),
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::from([
crate::pool::SuccessfulSteerDelivery {
event_id: "stale-event".into(),
session_id: "invalidated-session".into(),
},
]),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
let config = test_config();
let mut heartbeat_in_flight = false;
let removed_channels = HashSet::new();
let mut crash_history = vec![SlotCircuit {
crash_times: Vec::new(),
open_until: None,
respawn_in_flight: false,
}];
let (respawn_tx, _respawn_rx) = mpsc::channel(8);
let mut respawn_tasks = tokio::task::JoinSet::new();
let result = PromptResult {
agent,
source: PromptSource::Channel(channel_id),
turn_id: "test-turn-id".into(),
outcome: PromptOutcome::Ok(crate::acp::StopReason::EndTurn),
batch: None,
};
handle_prompt_result(
&mut pool,
&mut queue,
&config,
result,
&mut heartbeat_in_flight,
&removed_channels,
&mut crash_history,
&respawn_tx,
&mut respawn_tasks,
None,
None,
);
let returned = pool.agents_mut()[0].as_ref().expect("returned agent");
assert!(!returned.state.deliveries.contains_key(&channel_id));
}
/// Drive one error outcome through `handle_prompt_result` and return how
/// many `turn_error` events it emitted to the observer feed.
async fn turn_errors_emitted_for(outcome: PromptOutcome) -> usize {
@@ -6493,6 +6791,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
@@ -6569,6 +6868,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
started_rx.await.unwrap();
@@ -6661,6 +6961,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -6752,6 +7053,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -6857,6 +7159,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -6933,6 +7236,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -7027,6 +7331,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let config = test_config();
@@ -7143,6 +7448,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -7282,6 +7588,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -7470,6 +7777,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
@@ -7555,6 +7863,7 @@ mod error_outcome_emission_tests {
recoverable_batch: None,
control_tx: None,
steer_tx: None,
successful_steer_deliveries: HashSet::new(),
},
);
let mut queue = EventQueue::new(config::DedupMode::Queue);
+1065 -145
View File
File diff suppressed because it is too large Load Diff
+240 -40
View File
@@ -990,6 +990,9 @@ pub enum ConversationContext {
/// A single message in a conversation context section.
#[derive(Debug, Clone)]
pub struct ContextMessage {
/// Nostr event ID. Legacy REST fixtures may omit it, in which case it is
/// empty and cannot participate in delivery deduplication.
pub event_id: String,
pub pubkey: String,
pub timestamp: String,
pub content: String,
@@ -1241,6 +1244,7 @@ fn format_context_hints(
thread_tags: &ThreadTags,
is_dm: bool,
has_conversation_context: bool,
conversation_context_had_delivered_events: bool,
reply_anchor: Option<&str>,
) -> String {
let channel_display = match channel_info {
@@ -1258,6 +1262,10 @@ fn format_context_hints(
"Thread context included below. Use `buzz messages thread --channel <UUID> --event <ID>` for full history if truncated."
} else if has_conversation_context {
"Conversation context included below. Use `buzz messages get --channel <UUID>` for full history if truncated."
} else if conversation_context_had_delivered_events && is_reply {
"Earlier thread context was already delivered in this session. Use `buzz messages thread --channel <UUID> --event <ID>` to re-read the reply chain."
} else if conversation_context_had_delivered_events {
"Earlier conversation context was already delivered in this session. Use `buzz messages get --channel <UUID>` to re-read it."
} else if is_reply {
"Use `buzz messages thread --channel <UUID> --event <ID>` to fetch the reply chain."
} else {
@@ -1285,6 +1293,8 @@ fn format_context_hints(
} else if let Some(ref root) = thread_tags.root_event_id {
let ctx_hint = if has_conversation_context {
"Thread context included below. Use `buzz messages thread --channel <UUID> --event <ID>` for full history if truncated."
} else if conversation_context_had_delivered_events {
"Earlier thread context was already delivered in this session. Use `buzz messages thread --channel <UUID> --event <ID>` to re-read it."
} else {
"Use `buzz messages thread --channel <UUID> --event <ID>` to fetch thread context."
};
@@ -1359,6 +1369,9 @@ pub struct FormatPromptArgs<'a> {
pub agent_core: Option<&'a str>,
pub channel_info: Option<&'a PromptChannelInfo>,
pub conversation_context: Option<&'a ConversationContext>,
/// True when delivery-delta filtering removed at least one event that this
/// live session had already received. Trigger-only context does not set it.
pub conversation_context_had_delivered_events: bool,
pub profile_lookup: Option<&'a PromptProfileLookup>,
/// When true, base_prompt and system_prompt are delivered via the system
/// role (session/new) and omitted from the user message. When false
@@ -1374,9 +1387,62 @@ pub struct FormatPromptArgs<'a> {
///
/// For modern agents (protocol_version >= 2) the section is delivered via
/// the system role in session/new; omit here to avoid duplication.
/// For legacy agents it rides in the user message on every turn of the
/// session, alongside `[Base]`/`[System]`/`[Agent Memory — core]`.
pub agent_canvas: Option<&'a str>,
/// Set once this session's standing context has already been delivered —
/// see [`StandingContext`]. Only meaningful for legacy agents; modern
/// agents are gated by `has_system_prompt_support` regardless.
///
/// Defaults to `false` so a caller that never sets it behaves as if this
/// were the session's first message.
pub standing_context_sent: bool,
}
/// The prompt sections that do not change for the life of a session: base
/// prompt, persona, team instructions, core memory, and channel canvas.
///
/// Protocol-v2 agents receive all of this through the system role at
/// `session/new`, once. Legacy agents (`protocol_version < 2`) have no system
/// role, so it has to ride in a user message — but only in the session's
/// *first* one. Re-sending it every turn makes the standing framing the newest
/// and most-repeated text in the window, outweighing the conversation it exists
/// to frame, and evicting real channel history that much sooner.
///
/// Both legacy dispatch paths (initial message, batch flush) render through
/// this one type so their section set and ordering cannot drift apart.
#[derive(Default)]
pub(crate) struct StandingContext<'a> {
pub base_prompt: Option<&'a str>,
pub system_prompt: Option<&'a str>,
pub team_instructions: Option<&'a str>,
pub agent_core: Option<&'a str>,
pub agent_canvas: Option<&'a str>,
}
impl StandingContext<'_> {
/// Render the sections in the order legacy agents have always seen them.
pub(crate) fn sections(&self) -> Vec<String> {
let mut sections = Vec::with_capacity(5);
if let Some(bp) = self.base_prompt {
sections.push(base_section(bp));
}
if let Some(sp) = self.system_prompt {
sections.push(format!("[System]\n{sp}"));
}
if let Some(team) = self
.team_instructions
.map(str::trim)
.filter(|value| !value.is_empty())
{
sections.push(format!("[Team Instructions]\n{team}"));
}
if let Some(core) = self.agent_core {
sections.push(core.to_string());
}
if let Some(canvas) = self.agent_canvas {
sections.push(canvas.to_string());
}
sections
}
}
/// Format the `[Base]` section for the base prompt.
@@ -1391,12 +1457,12 @@ pub(crate) fn base_section(base_prompt: &str) -> String {
/// Format a [`FlushBatch`] into the per-section prompt blocks for the agent.
///
/// Produces a stable prompt with these sections (in order):
/// 0. `[Base]` — base prompt (only for legacy agents without systemPrompt support)
/// 1. `[System]` — system prompt (only for legacy agents without systemPrompt support)
/// 2. `[Agent Memory — core]` — if agent core memory is set
/// 3. `[Context]` — scope, channel name, and contextual hints for the agent
/// 4. `[Thread Context]` or `[Conversation Context]` — if fetched
/// 5. `[Event]` / `[Buzz events]` — the triggering event(s)
/// 0. [`StandingContext`] — `[Base]`, `[System]`, `[Team Instructions]`,
/// `[Agent Memory — core]`, `[Channel Canvas]`. Legacy agents only, and only
/// on the session's first message (see `standing_context_sent`)
/// 1. `[Context]` — scope, channel name, and contextual hints for the agent
/// 2. `[Thread Context]` or `[Conversation Context]` — if fetched
/// 3. `[Event]` / `[Buzz events]` — the triggering event(s)
///
/// Each section is returned as its own block rather than one joined string so
/// the observer frame's size trimmer (`fit_observer_event_to_budget`) elides
@@ -1428,38 +1494,22 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
let mut sections: Vec<String> = Vec::with_capacity(7);
// For legacy agents (protocol_version < 2), inject base_prompt and
// system_prompt as user-message sections. Modern agents receive these
// via the system role in session/new.
if !args.has_system_prompt_support {
if let Some(bp) = args.base_prompt {
sections.push(base_section(bp));
}
if let Some(sp) = args.system_prompt {
sections.push(format!("[System]\n{sp}"));
}
if let Some(team) = args
.team_instructions
.map(str::trim)
.filter(|value| !value.is_empty())
{
sections.push(format!("[Team Instructions]\n{team}"));
}
}
// NIP-AE agent core memory (rendered by `engram_fetch::build_core_section`).
// For modern agents (protocol_version >= 2), core is delivered via the
// system role in session/new, so it is omitted here to avoid duplication.
// Legacy agents have no system role, so core rides in the user message
// alongside `[Base]`/`[System]`.
if !args.has_system_prompt_support {
if let Some(core) = args.agent_core {
sections.push(core.to_string());
}
// Channel canvas metadata — same delivery semantics as core for legacy agents.
if let Some(canvas) = args.agent_canvas {
sections.push(canvas.to_string());
}
// Standing context — base prompt, persona, team instructions, core memory
// and canvas. Modern agents received all of it via the system role in
// session/new. Legacy agents get it here, in the session's first message
// only; `standing_context_sent` means an earlier message in this session
// already carried it.
if !args.has_system_prompt_support && !args.standing_context_sent {
sections.extend(
StandingContext {
base_prompt: args.base_prompt,
system_prompt: args.system_prompt,
team_instructions: args.team_instructions,
agent_core: args.agent_core,
agent_canvas: args.agent_canvas,
}
.sections(),
);
}
// 2. Context hints (with a human-aware reply anchor).
@@ -1489,6 +1539,7 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
&thread_tags,
is_dm,
args.conversation_context.is_some(),
args.conversation_context_had_delivered_events,
reply_anchor.as_deref(),
));
@@ -2408,6 +2459,60 @@ mod tests {
);
}
#[test]
fn test_format_prompt_legacy_agent_omits_standing_after_first_message() {
// The defect this pins: standing context was re-sent on every turn of a
// legacy session, so the largest and least informative part of the
// prompt was also the most recent — crowding out the conversation and
// evicting real channel history sooner.
let ch = Uuid::new_v4();
let batch = FlushBatch {
channel_id: ch,
events: vec![BatchEvent {
event: make_event("hello"),
prompt_tag: "test".into(),
received_at: Instant::now(),
}],
cancelled_events: vec![],
cancel_reason: None,
};
let canvas = "[Channel Canvas]\ncanvas content";
let core = "[Agent Memory — core]\nremember this";
let args = |sent| FormatPromptArgs {
has_system_prompt_support: false,
base_prompt: Some("test base prompt"),
system_prompt: Some("test system prompt"),
team_instructions: Some("ship small"),
agent_core: Some(core),
agent_canvas: Some(canvas),
standing_context_sent: sent,
..Default::default()
};
let first = format_prompt(&batch, &args(false)).join("\n\n");
let later = format_prompt(&batch, &args(true)).join("\n\n");
for section in [
"[Base]",
"[System]",
"[Team Instructions]",
"[Agent Memory — core]",
"[Channel Canvas]",
] {
assert!(first.contains(section), "first message missing {section}");
assert!(!later.contains(section), "turn 2 repeated {section}");
}
// What the turn is actually about survives, and now leads.
assert!(later.starts_with("[Context]"), "got: {later}");
assert!(later.contains("hello"));
assert!(
later.len() < first.len(),
"later turns must be smaller: {} vs {}",
later.len(),
first.len()
);
}
#[test]
fn test_format_prompt_modern_agent_suppresses_base_and_system() {
let ch = Uuid::new_v4();
@@ -2464,6 +2569,7 @@ mod tests {
let ctx = ConversationContext::Thread {
messages: vec![ContextMessage {
event_id: String::new(),
pubkey: "npub1test".into(),
content: "prior message".into(),
timestamp: "2024-01-01T00:00:00Z".into(),
@@ -3078,11 +3184,13 @@ mod tests {
let ctx = ConversationContext::Thread {
messages: vec![
ContextMessage {
event_id: String::new(),
pubkey: "npub1xyz".into(),
timestamp: "2026-03-15T16:30:00Z".into(),
content: "Let's refactor auth".into(),
},
ContextMessage {
event_id: String::new(),
pubkey: "npub1def".into(),
timestamp: "2026-03-15T16:35:00Z".into(),
content: "yes go ahead".into(),
@@ -3125,6 +3233,7 @@ mod tests {
};
let ctx = ConversationContext::Dm {
messages: vec![ContextMessage {
event_id: String::new(),
pubkey: "npub1abc".into(),
timestamp: "2026-03-15T16:00:00Z".into(),
content: "Can you deploy?".into(),
@@ -3170,6 +3279,7 @@ mod tests {
};
let ctx = ConversationContext::Thread {
messages: vec![ContextMessage {
event_id: String::new(),
pubkey: author_hex.clone(),
timestamp: "2026-03-25T05:51:25Z".into(),
content: "follow up".into(),
@@ -3382,6 +3492,7 @@ mod tests {
// Thread context fetched (as the fetch path does for DM replies).
let ctx = ConversationContext::Thread {
messages: vec![ContextMessage {
event_id: String::new(),
pubkey: "npub1xyz".into(),
timestamp: "2026-03-15T16:30:00Z".into(),
content: "Should I deploy?".into(),
@@ -3418,6 +3529,95 @@ mod tests {
assert!(prompt.contains("Should I deploy?"));
}
#[test]
fn test_format_prompt_empty_thread_delta_distinguishes_trigger_only_from_delivered() {
let ch = Uuid::new_v4();
let event = make_event_with_tags(
"follow up",
vec![vec![
"e".into(),
"root123".into(),
"".into(),
"reply".into(),
]],
);
let batch = FlushBatch {
channel_id: ch,
events: vec![BatchEvent {
event,
prompt_tag: "test".into(),
received_at: Instant::now(),
}],
cancelled_events: vec![],
cancel_reason: None,
};
let trigger_only_prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
assert!(trigger_only_prompt.contains("fetch thread context"));
assert!(!trigger_only_prompt.contains("already delivered in this session"));
let prompt = format_prompt(
&batch,
&FormatPromptArgs {
conversation_context_had_delivered_events: true,
..Default::default()
},
)
.join("\n\n");
assert!(prompt.contains("Earlier thread context was already delivered in this session"));
assert!(prompt.contains("buzz messages thread"));
assert!(!prompt.contains("Thread context included below"));
assert!(!prompt.contains("[Thread Context"));
}
#[test]
fn test_format_prompt_empty_dm_delta_distinguishes_trigger_only_from_delivered() {
let ch = Uuid::new_v4();
let batch = FlushBatch {
channel_id: ch,
events: vec![BatchEvent {
event: make_event("follow up"),
prompt_tag: "dm".into(),
received_at: Instant::now(),
}],
cancelled_events: vec![],
cancel_reason: None,
};
let ci = PromptChannelInfo {
name: "DM".into(),
channel_type: "dm".into(),
};
let trigger_only_prompt = format_prompt(
&batch,
&FormatPromptArgs {
channel_info: Some(&ci),
..Default::default()
},
)
.join("\n\n");
assert!(trigger_only_prompt.contains("for conversation context"));
assert!(!trigger_only_prompt.contains("already delivered in this session"));
let prompt = format_prompt(
&batch,
&FormatPromptArgs {
channel_info: Some(&ci),
conversation_context_had_delivered_events: true,
..Default::default()
},
)
.join("\n\n");
assert!(
prompt.contains("Earlier conversation context was already delivered in this session")
);
assert!(prompt.contains("buzz messages get"));
assert!(!prompt.contains("Conversation context included below"));
assert!(!prompt.contains("[Conversation Context"));
}
#[test]
fn test_format_prompt_dm_non_reply_hints_get_messages() {
let ch = Uuid::new_v4();
File diff suppressed because it is too large Load Diff
+1
View File
@@ -41,6 +41,7 @@ axum = { workspace = true }
base64 = "0.22"
hex = { workspace = true }
sha2 = { workspace = true }
url = { workspace = true }
urlencoding = "2"
webbrowser = "1"
dirs = "6"
+257 -37
View File
@@ -6,7 +6,9 @@ use tokio::task::JoinSet;
use tracing::Instrument as _;
use crate::builtin;
use crate::config::{Config, MAX_PROMPT_BYTES, MAX_TOOL_CALLS_PER_TURN, MAX_TOOL_RESULT_BYTES};
use crate::config::{
pricing_authority, Config, MAX_PROMPT_BYTES, MAX_TOOL_CALLS_PER_TURN, MAX_TOOL_RESULT_BYTES,
};
use crate::handoff::{ContextRecovery, HandoffOutcome};
use crate::hints::SkillEntry;
use crate::llm::Llm;
@@ -14,8 +16,9 @@ use crate::mcp::McpRegistry;
use crate::mcp::ResultBudget;
use crate::types::{
AgentError, ContentBlock, HistoryItem, ProviderStop, SessionUsageBaseline, StopReason,
ToolCall, ToolResult, ToolResultContent, TurnTotalState,
AgentError, CacheTotalState, ContentBlock, HistoryItem, PricingIdentity, ProviderStop,
SessionUsageBaseline, StopReason, ToolCall, ToolResult, ToolResultContent, TurnIOState,
TurnTotalState,
};
use crate::wire::{self, WireSender};
@@ -175,16 +178,38 @@ pub struct RunCtx<'a> {
/// preserved in lockstep with `last_request_input_tokens`.
pub last_request_history_bytes: &'a mut Option<usize>,
/// Accumulated input tokens across all LLM rounds in this turn, for
/// NIP-AM metric publishing. Reset to `None` at turn start in `run()`.
pub turn_input_tokens: &'a mut Option<u64>,
/// NIP-AM metric publishing. Reset to `Unseen` at turn start in `run()`.
pub turn_input_tokens: &'a mut TurnIOState,
/// Accumulated output tokens across all LLM rounds in this turn, for
/// NIP-AM metric publishing. Reset to `None` at turn start in `run()`.
pub turn_output_tokens: &'a mut Option<u64>,
/// NIP-AM metric publishing. Reset to `Unseen` at turn start in `run()`.
pub turn_output_tokens: &'a mut TurnIOState,
/// The cache-served subset of `turn_input_tokens`, accumulated across all
/// LLM rounds in this turn. Reset to `None` at turn start in `run()`.
/// LLM rounds in this turn. Reset to `Unseen` at turn start in `run()`.
/// Consumers price this slice at the provider's cached rate; without it
/// every round of a growing conversation is billed at full price.
pub turn_cached_input_tokens: &'a mut Option<u64>,
///
/// `CacheTotalState` enforces the D1 rule: any usage-bearing round that
/// omits this category poisons the accumulator permanently for the turn.
pub turn_cached_input_tokens: &'a mut CacheTotalState,
/// The cache-written subset of `turn_input_tokens`, accumulated across all
/// LLM rounds in this turn. Reset to `Unseen` at turn start in `run()`.
/// Consumers need this to price cache-creation at the provider's write rate
/// (distinct from both the standard input rate and the cached-read rate).
///
/// Same D1 tri-state contract as `turn_cached_input_tokens`.
pub turn_cache_write_tokens: &'a mut CacheTotalState,
/// Per-turn billing identity accumulator.
///
/// - `None`: no usage-bearing response observed yet this turn (initial state).
/// - `Some(Some(pi))`: all usage-bearing responses so far carry the same
/// proven identity `pi`. If a subsequent response carries a different or
/// unproven identity, this transitions to `Some(None)` (poisoned).
/// - `Some(None)`: poisoned — mixed identities, unproven response, mesh
/// retry across models, or no identity derived. Never heals within the turn.
///
/// Reset to `None` at turn start in `run()`. The wire payload emits the
/// proven identity when `Some(Some(pi))`, omits it otherwise.
pub turn_pricing_identity: &'a mut Option<Option<PricingIdentity>>,
/// Tri-state total-token accumulator for this turn.
///
/// - `Unseen`: no usage-bearing response observed yet this turn (initial state).
@@ -202,6 +227,41 @@ pub struct RunCtx<'a> {
pub usage_baseline: SessionUsageBaseline,
}
/// Fold one round's proven identity into the per-turn identity accumulator.
///
/// Accumulator tri-state (NIP-AM §pricingIdentity):
/// - `None`: no usage-bearing round observed yet this turn.
/// - `Some(Some(pi))`: every usage-bearing round so far carries the same
/// proven identity `pi`.
/// - `Some(None)`: poisoned — mixed identities or unproven round seen.
/// Never heals within the turn.
///
/// `round`: `Some(pi)` when this round's `(base_url, request_model)` resolve
/// to a proven identity; `None` when the endpoint is unallowlisted or the
/// model is unknown.
#[inline]
fn fold_pricing_identity(
acc: Option<Option<PricingIdentity>>,
round: Option<PricingIdentity>,
) -> Option<Option<PricingIdentity>> {
match acc {
// First usage-bearing round: record whatever was derived.
None => Some(round),
// Already consistent: keep only if this round matches exactly.
Some(Some(ref existing)) => {
if Some(existing) == round.as_ref() {
Some(round)
} else {
// Mismatch (different model, different authority,
// or this round had no proven identity) → poison.
Some(None)
}
}
// Already poisoned: stays poisoned forever this turn.
poisoned @ Some(None) => poisoned,
}
}
impl RunCtx<'_> {
/// Send a session-cumulative `usage_update` reflecting everything observed
/// up to and including the most recent LLM response.
@@ -213,15 +273,30 @@ impl RunCtx<'_> {
/// everything but its final in-flight request.
async fn emit_usage_update(&self) {
let base = self.usage_baseline;
// Combine session baseline CacheTotalState with the per-turn delta:
// merge_session produces Exact when both sides are Exact, Unknown when
// either is Unknown, and leaves Unseen when both sides are Unseen.
let cached_total = base
.cached_input_tokens
.merge_session(*self.turn_cached_input_tokens);
let write_total = base
.cache_write_tokens
.merge_session(*self.turn_cache_write_tokens);
let payload = wire::usage_update_payload(
base.input_tokens
.saturating_add(self.turn_input_tokens.unwrap_or(0)),
.merge_session(*self.turn_input_tokens)
.exact_value(),
base.output_tokens
.saturating_add(self.turn_output_tokens.unwrap_or(0)),
base.cached_input_tokens
.saturating_add(self.turn_cached_input_tokens.unwrap_or(0)),
.merge_session(*self.turn_output_tokens)
.exact_value(),
cached_total.exact_value(),
write_total.exact_value(),
base.total_state.merge_session(*self.turn_total_state),
self.effective_model,
// Extract the proven identity if this turn is consistent so far.
self.turn_pricing_identity
.as_ref()
.and_then(|inner| inner.as_ref()),
);
wire::send(
self.wire,
@@ -243,9 +318,11 @@ impl RunCtx<'_> {
self.history.push(HistoryItem::User(user_text));
// Reset per-turn token accumulators for this prompt.
*self.turn_input_tokens = None;
*self.turn_output_tokens = None;
*self.turn_cached_input_tokens = None;
*self.turn_input_tokens = TurnIOState::Unseen;
*self.turn_output_tokens = TurnIOState::Unseen;
*self.turn_cached_input_tokens = CacheTotalState::Unseen;
*self.turn_cache_write_tokens = CacheTotalState::Unseen;
*self.turn_pricing_identity = None;
*self.turn_total_state = TurnTotalState::Unseen;
// Per-turn handoff-attempt counter. Scoped here (not persisted in the
// session) so `BUZZ_AGENT_MAX_HANDOFFS` bounds compactions per
@@ -423,7 +500,17 @@ impl RunCtx<'_> {
// a response omits usage (`None`) rather than clobbering — a
// one-off missing field shouldn't blind the gate or zero the
// growth baseline.
if let Some(tokens) = response.input_tokens {
if response.input_tokens_overflowed {
// The Anthropic-style inclusive sum (input_tokens +
// cache_read_input_tokens + cache_creation_input_tokens)
// overflowed u64::MAX during parsing. Permanently poison the
// turn accumulator so wire emission omits this value and ACP
// marks the delta unreliable. Do NOT update
// last_request_input_tokens — freeze the context-gate
// baseline at its prior reading rather than poisoning it with
// a clamped value, exactly as the absent-usage path does.
*self.turn_input_tokens = TurnIOState::Poisoned;
} else if let Some(tokens) = response.input_tokens {
*self.last_request_input_tokens = Some(tokens);
*self.last_request_history_bytes = Some(
self.history
@@ -432,29 +519,30 @@ impl RunCtx<'_> {
.sum(),
);
// Accumulate per-turn input tokens for NIP-AM metric publishing.
*self.turn_input_tokens =
Some(self.turn_input_tokens.unwrap_or(0).saturating_add(tokens));
// fold_round uses checked_add; overflow permanently poisons the
// turn accumulator (and, via merge_session, the session cumulative).
*self.turn_input_tokens = self.turn_input_tokens.fold_round(tokens);
}
// Accumulate per-turn output tokens for NIP-AM metric publishing.
if let Some(out) = response.output_tokens {
*self.turn_output_tokens =
Some(self.turn_output_tokens.unwrap_or(0).saturating_add(out));
*self.turn_output_tokens = self.turn_output_tokens.fold_round(out);
}
// Accumulate the cache-served subset of this turn's input. Tracked
// separately from `turn_input_tokens` rather than subtracted from
// it: the input total must stay inclusive for the handoff gate,
// which cares how much context was sent, not what it cost.
if let Some(cached) = response.cached_input_tokens {
*self.turn_cached_input_tokens = Some(
self.turn_cached_input_tokens
.unwrap_or(0)
.saturating_add(cached),
);
}
// Fold the provider-reported total into the turn tri-state, but only
// when this response was usage-bearing (had input or output tokens).
// A response with no usage at all is not evidence of a missing total
// and must not poison the accumulator.
// Fold the provider-reported total, cache subsets, and billing
// identity — only when this response was usage-bearing (had input
// or output tokens). A response with no usage at all is not
// evidence of a missing cache field or total and must not poison
// either accumulator.
//
// `input_tokens_overflowed` counts as usage-bearing: the provider
// reported an input total (which overflowed) so cache fields and
// the total are meaningful and must be folded.
//
// D1: absent cache field on a usage-bearing round permanently
// poisons the turn accumulator. Some(0) stays Exact(0) (explicit
// zero is distinct from absent). Cache-read and cache-write are
// tracked separately from `turn_input_tokens` rather than
// subtracted from it: the input total must stay inclusive for the
// handoff gate, which cares how much context was sent, not cost.
//
// Shape assumption: documented OpenAI-compatible responses that carry
// `total_tokens` always co-report at least one of `prompt_tokens` /
@@ -462,8 +550,45 @@ impl RunCtx<'_> {
// with neither category is therefore not a supported shape and would
// be silently ignored here. If that shape is ever encountered, extend
// this gate rather than representing absent categories as zero.
if response.input_tokens.is_some() || response.output_tokens.is_some() {
if response.input_tokens.is_some()
|| response.input_tokens_overflowed
|| response.output_tokens.is_some()
{
*self.turn_total_state = self.turn_total_state.fold(response.total_tokens);
// Cache-read: the cache-served subset of input tokens.
*self.turn_cached_input_tokens = self
.turn_cached_input_tokens
.fold(response.cached_input_tokens);
// Cache-write: the cache-creation subset of input tokens.
*self.turn_cache_write_tokens = self
.turn_cache_write_tokens
.fold(response.cache_write_tokens);
// Derive billing identity for this round and fold it into the
// per-turn accumulator. Rules (NIP-AM §pricingIdentity):
//
// 1. Attempt to derive identity from (base_url, request_model).
// - base_url must canonically match an official allowlisted host.
// - request_model must be Some (mesh-auto with unknown model
// cannot prove identity).
// 2. Fold the round identity into the turn accumulator:
// - Unseen (None): record this round's identity (or poison if None).
// - Consistent: if it matches, keep; otherwise poison.
// - Poisoned: stays poisoned forever this turn.
{
let round_identity: Option<PricingIdentity> =
response.request_model.as_deref().and_then(|model| {
pricing_authority(&self.cfg.base_url).map(|auth| PricingIdentity {
authority: auth.to_string(),
model: model.to_string(),
cache_class: None, // no cache-class derivation yet
})
});
*self.turn_pricing_identity =
fold_pricing_identity(self.turn_pricing_identity.take(), round_identity);
}
// Report what the turn has burned SO FAR, before running the
// next round. A turn is many provider round-trips over many
// minutes, and until this point the only report was the one
@@ -1378,4 +1503,99 @@ mod tests {
"under budget must not evict anything"
);
}
// ── fold_pricing_identity: turn discipline ────────────────────────────────
fn pi(authority: &str, model: &str) -> PricingIdentity {
PricingIdentity {
authority: authority.to_string(),
model: model.to_string(),
cache_class: None,
}
}
/// Case 1: two usage-bearing rounds with different proven identities in one
/// turn must poison the accumulator. The wire payload omits `pricingIdentity`
/// when `Some(None)`.
#[test]
fn fold_pricing_identity_mismatch_poisons() {
let round_a = Some(pi("api.anthropic.com", "claude-opus-4-5"));
let round_b = Some(pi("api.openai.com", "gpt-4o"));
// Start: unseen.
let acc = None;
// After round A: consistent — Some(Some(claude-opus-4-5)).
let acc = fold_pricing_identity(acc, round_a);
assert!(
matches!(acc, Some(Some(_))),
"after one round must be consistent"
);
// After round B (different authority + model): poisoned.
let acc = fold_pricing_identity(acc, round_b);
assert_eq!(
acc,
Some(None),
"different proven identities in one turn must poison"
);
}
/// Case 2: proven identity followed by a usage-bearing round with no proven
/// identity (request_model absent or non-allowlisted endpoint) must poison.
/// An unpaired cumulative snapshot also produces round=None (no model known)
/// and hits this same path — case 4 collapses into case 2.
#[test]
fn fold_pricing_identity_unproven_round_poisons() {
let round_a = Some(pi("api.anthropic.com", "claude-3-7-sonnet"));
let round_unproven: Option<PricingIdentity> = None; // absent request_model or custom endpoint
let acc = None;
let acc = fold_pricing_identity(acc, round_a);
assert!(
matches!(acc, Some(Some(_))),
"after one proven round must be consistent"
);
let acc = fold_pricing_identity(acc, round_unproven);
assert_eq!(
acc,
Some(None),
"an unproven round after a proven round must poison (no-model / custom-endpoint path)"
);
}
/// Case 3: a poisoned accumulator must not heal, even if a later round
/// carries an identity matching the original.
#[test]
fn fold_pricing_identity_poisoned_never_heals() {
let round_a = Some(pi("api.openai.com", "gpt-4o"));
let round_unproven: Option<PricingIdentity> = None;
let round_a_again = Some(pi("api.openai.com", "gpt-4o")); // identical to round_a
let acc = None;
let acc = fold_pricing_identity(acc, round_a);
let acc = fold_pricing_identity(acc, round_unproven); // poisons
assert_eq!(acc, Some(None), "must be poisoned before heal attempt");
let acc = fold_pricing_identity(acc, round_a_again); // must not heal
assert_eq!(
acc,
Some(None),
"poisoned accumulator must stay poisoned even when the next round matches the original identity"
);
}
/// Baseline: a turn where every round carries the same proven identity
/// stays consistent and emits the identity on the wire.
#[test]
fn fold_pricing_identity_consistent_rounds_stay_proven() {
let identity = pi("api.openrouter.ai", "meta-llama/llama-4-scout");
let acc = None;
let acc = fold_pricing_identity(acc, Some(identity.clone()));
let acc = fold_pricing_identity(acc, Some(identity.clone()));
let acc = fold_pricing_identity(acc, Some(identity.clone()));
assert_eq!(
acc,
Some(Some(identity)),
"three identical rounds must remain consistently proven"
);
}
}
+196
View File
@@ -1189,6 +1189,82 @@ pub fn is_openai_host(base_url: &str) -> bool {
host == "api.openai.com" || host.ends_with(".openai.com")
}
/// Return the NIP-AM registered billing-authority token for `base_url` when it
/// canonically matches one of the official allowlisted endpoints. Returns `None`
/// for any custom, gateway, or lookalike URL.
///
/// Rules (per NIP-AM publisher behavior):
/// - HTTPS only (no HTTP).
/// - Exact allowlisted host — lookalike-safe: `api.openai.com.evil.example` is rejected.
/// - Default port only (no `:8443` etc.).
/// - No userinfo, query string, or fragment.
/// - Required API base path present and exact (where applicable — OpenRouter requires `/api/v1`).
/// - No path prefix lookalikes (`/api/v10` is not `/api/v1`).
///
/// The wire token itself is the registered bare-host identifier (e.g.
/// `"api.anthropic.com"`), not a URL — the path check is publisher-side only.
///
/// Allowlist (registered values; set extends only by NIP-AM amendment):
/// - `https://api.anthropic.com/` → `"api.anthropic.com"`
/// - `https://api.openai.com/v1` → `"api.openai.com"` (path `/v1` required)
/// - `https://openrouter.ai/api/v1` → `"openrouter.ai"` (path `/api/v1` required)
pub fn pricing_authority(base_url: &str) -> Option<&'static str> {
let parsed = url::Url::parse(base_url).ok()?;
// Require HTTPS only.
if parsed.scheme() != "https" {
return None;
}
// Reject userinfo (username or password present).
if !parsed.username().is_empty() || parsed.password().is_some() {
return None;
}
// Reject query strings and fragments.
if parsed.query().is_some() || parsed.fragment().is_some() {
return None;
}
// Require either no port or the default HTTPS port (443). Both forms are
// equivalent canonical origins; rejecting explicit :443 would create false
// negatives for providers that include the default port in their base URL.
if let Some(port) = parsed.port() {
if port != 443 {
return None;
}
}
let host = parsed.host_str()?.to_ascii_lowercase();
// Normalise trailing slashes for path comparison.
let path = parsed.path().trim_end_matches('/');
match host.as_str() {
"api.anthropic.com" => {
// Anthropic: path must be empty or "/" — no required prefix.
if path.is_empty() {
Some("api.anthropic.com")
} else {
None
}
}
"api.openai.com" => {
// OpenAI: path must be exactly "/v1".
if path == "/v1" {
Some("api.openai.com")
} else {
None
}
}
"openrouter.ai" => {
// OpenRouter: path must be exactly "/api/v1".
if path == "/api/v1" {
Some("openrouter.ai")
} else {
None
}
}
_ => None,
}
}
fn parse_env<T: std::str::FromStr>(key: &str, default: T) -> Result<T, String>
where
T::Err: std::fmt::Display,
@@ -2970,4 +3046,124 @@ mod tests {
let err = resolve_provider(Some("openrouter"), None, None, None).unwrap_err();
assert!(err.contains("OPENROUTER_API_KEY"));
}
// ── pricing_authority: canonical URL → bare-host registry token ──────────
#[test]
fn pricing_authority_anthropic_returns_registry_token() {
assert_eq!(
pricing_authority("https://api.anthropic.com/"),
Some("api.anthropic.com")
);
// No trailing slash
assert_eq!(
pricing_authority("https://api.anthropic.com"),
Some("api.anthropic.com")
);
}
#[test]
fn pricing_authority_openai_requires_v1_path() {
assert_eq!(
pricing_authority("https://api.openai.com/v1"),
Some("api.openai.com")
);
// With trailing slash
assert_eq!(
pricing_authority("https://api.openai.com/v1/"),
Some("api.openai.com")
);
// Root-only: no path → must return None
assert_eq!(pricing_authority("https://api.openai.com/"), None);
assert_eq!(pricing_authority("https://api.openai.com"), None);
// Wrong path
assert_eq!(pricing_authority("https://api.openai.com/v2"), None);
}
#[test]
fn pricing_authority_openrouter_requires_api_v1_path() {
assert_eq!(
pricing_authority("https://openrouter.ai/api/v1"),
Some("openrouter.ai")
);
assert_eq!(
pricing_authority("https://openrouter.ai/api/v1/"),
Some("openrouter.ai")
);
assert_eq!(pricing_authority("https://openrouter.ai/"), None);
assert_eq!(pricing_authority("https://openrouter.ai"), None);
}
#[test]
fn pricing_authority_rejects_http_scheme() {
assert_eq!(pricing_authority("http://api.anthropic.com/"), None);
assert_eq!(pricing_authority("http://api.openai.com/v1"), None);
}
#[test]
fn pricing_authority_accepts_explicit_default_port() {
// Explicit :443 is the default HTTPS port — both omitted and explicit
// forms resolve to the same canonical origin and must both be accepted.
assert_eq!(
pricing_authority("https://api.anthropic.com:443/"),
Some("api.anthropic.com"),
"explicit :443 must be accepted for Anthropic"
);
assert_eq!(
pricing_authority("https://api.openai.com:443/v1"),
Some("api.openai.com"),
"explicit :443 must be accepted for OpenAI"
);
assert_eq!(
pricing_authority("https://openrouter.ai:443/api/v1"),
Some("openrouter.ai"),
"explicit :443 must be accepted for OpenRouter"
);
// Non-default port must still be rejected.
assert_eq!(pricing_authority("https://api.openai.com:8080/v1"), None);
}
#[test]
fn pricing_authority_rejects_userinfo() {
assert_eq!(
pricing_authority("https://user:pass@api.anthropic.com/"),
None
);
}
#[test]
fn pricing_authority_rejects_query_and_fragment() {
assert_eq!(
pricing_authority("https://api.anthropic.com/?debug=1"),
None
);
assert_eq!(
pricing_authority("https://api.anthropic.com/#section"),
None
);
assert_eq!(pricing_authority("https://api.openai.com/v1?key=1"), None);
}
#[test]
fn pricing_authority_rejects_lookalike_hosts() {
// Subdomain: must NOT match
assert_eq!(
pricing_authority("https://subdomain.api.anthropic.com/"),
None
);
// Superset: must NOT match
assert_eq!(pricing_authority("https://notapi.anthropic.com/"), None);
assert_eq!(
pricing_authority("https://api.anthropic.com.evil.com/"),
None
);
// Path-prefix lookalike for OpenAI: /v1extra must not match /v1
assert_eq!(pricing_authority("https://api.openai.com/v1extra"), None);
}
#[test]
fn pricing_authority_unknown_host_returns_none() {
assert_eq!(pricing_authority("https://api.databricks.com/v1"), None);
assert_eq!(pricing_authority("https://custom.llm.corp/v1"), None);
}
}
+70 -23
View File
@@ -97,14 +97,26 @@ struct Session {
/// Session-cumulative input tokens across all turns. Sent in the
/// `_goose/unstable/session/update` usage notification so buzz-acp's
/// `UsageTracker` can compute per-turn deltas symmetrically with goose.
accumulated_input_tokens: u64,
/// `TurnIOState`: `Unseen` before any turn reports; `Exact(n)` while running;
/// `Poisoned` if any turn's sum overflowed — permanently poisons the session.
accumulated_input_tokens: crate::types::TurnIOState,
/// Session-cumulative output tokens across all turns.
accumulated_output_tokens: u64,
/// Same `Unseen`/`Exact(n)`/`Poisoned` contract as `accumulated_input_tokens`.
accumulated_output_tokens: crate::types::TurnIOState,
/// Session-cumulative cache-served input tokens across all turns — a subset
/// of `accumulated_input_tokens`, not an addition to it. Emitted alongside
/// it so a consumer can price the cached slice at the provider's discounted
/// rate instead of assuming every input token cost full price.
accumulated_cached_input_tokens: u64,
/// of `accumulated_input_tokens`, not an addition to it. Tri-state:
///
/// - `Unseen`: no turn has ever reported this category.
/// - `Exact(n)`: every usage-bearing response in every turn reported this
/// category; `n` is the cumulative sum.
/// - `Unknown`: at least one usage-bearing response ever omitted the
/// category — permanently poisoned for this session.
accumulated_cached_input_tokens: crate::types::CacheTotalState,
/// Session-cumulative cache-written input tokens across all turns — also a
/// subset of `accumulated_input_tokens`, not an addition to it.
/// Same `Unseen`/`Exact`/`Unknown` tri-state contract as
/// `accumulated_cached_input_tokens`.
accumulated_cache_write_tokens: crate::types::CacheTotalState,
/// Session-cumulative total-token state across all turns.
///
/// Mirrors the per-turn `TurnTotalState` tri-state: starts `Unseen`,
@@ -477,9 +489,10 @@ async fn session_new(app: &Arc<App>, id: Value, params: Value, wire_tx: &WireSen
last_request_history_bytes: None,
effective_system_prompt,
effective_model: None,
accumulated_input_tokens: 0,
accumulated_output_tokens: 0,
accumulated_cached_input_tokens: 0,
accumulated_input_tokens: crate::types::TurnIOState::Unseen,
accumulated_output_tokens: crate::types::TurnIOState::Unseen,
accumulated_cached_input_tokens: crate::types::CacheTotalState::Unseen,
accumulated_cache_write_tokens: crate::types::CacheTotalState::Unseen,
accumulated_total_state: crate::types::TurnTotalState::Unseen,
},
);
@@ -696,10 +709,20 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
let effective_model_str = effective_model_override
.as_deref()
.unwrap_or(&app.cfg.model);
let mut turn_input_tokens: Option<u64> = None;
let mut turn_output_tokens: Option<u64> = None;
let mut turn_cached_input_tokens: Option<u64> = None;
let mut turn_input_tokens: crate::types::TurnIOState = crate::types::TurnIOState::Unseen;
let mut turn_output_tokens: crate::types::TurnIOState = crate::types::TurnIOState::Unseen;
let mut turn_cached_input_tokens: crate::types::CacheTotalState =
crate::types::CacheTotalState::Unseen;
let mut turn_cache_write_tokens: crate::types::CacheTotalState =
crate::types::CacheTotalState::Unseen;
let mut turn_total_state = crate::types::TurnTotalState::Unseen;
// Per-turn billing identity accumulator — three-state:
// None = no usage-bearing response seen yet (initial)
// Some(Some(pi))= all usage-bearing responses carry the same proven identity
// Some(None) = poisoned (mixed identities, unproven response, etc.)
// Not stored in Session (not session-cumulative); used only for the final
// end-of-turn wire emission.
let mut turn_pricing_identity: Option<Option<crate::types::PricingIdentity>> = None;
let mut ctx = RunCtx {
cfg: &app.cfg,
effective_model: effective_model_str,
@@ -720,7 +743,9 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
turn_input_tokens: &mut turn_input_tokens,
turn_output_tokens: &mut turn_output_tokens,
turn_cached_input_tokens: &mut turn_cached_input_tokens,
turn_cache_write_tokens: &mut turn_cache_write_tokens,
turn_total_state: &mut turn_total_state,
turn_pricing_identity: &mut turn_pricing_identity,
usage_baseline,
};
let result = ctx.run(p.prompt).await;
@@ -744,19 +769,29 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
// Only emit when at least one token count was observed — a turn with no
// provider response (validation failure, pre-response cancellation) carries
// no information and must not produce a kind 44200 record per NIP-AM.
if turn_input_tokens.is_some() || turn_output_tokens.is_some() {
if !matches!(turn_input_tokens, crate::types::TurnIOState::Unseen)
|| !matches!(turn_output_tokens, crate::types::TurnIOState::Unseen)
{
let accumulated = {
let mut sessions = app.sessions.lock().await;
if let Some(s) = sessions.get_mut(&sid) {
s.accumulated_input_tokens = s
.accumulated_input_tokens
.saturating_add(turn_input_tokens.unwrap_or(0));
// merge_session: Poisoned poisons permanently; Exact sums with
// overflow-check → Poisoned on wrap; Unseen leaves unchanged.
s.accumulated_input_tokens =
s.accumulated_input_tokens.merge_session(turn_input_tokens);
s.accumulated_output_tokens = s
.accumulated_output_tokens
.saturating_add(turn_output_tokens.unwrap_or(0));
.merge_session(turn_output_tokens);
// D1 tri-state merge: merge_session propagates Unknown when
// the turn was poisoned (any usage-bearing round omitted the
// category), and is a no-op when the turn was Unseen (no
// usage-bearing response at all).
s.accumulated_cached_input_tokens = s
.accumulated_cached_input_tokens
.saturating_add(turn_cached_input_tokens.unwrap_or(0));
.merge_session(turn_cached_input_tokens);
s.accumulated_cache_write_tokens = s
.accumulated_cache_write_tokens
.merge_session(turn_cache_write_tokens);
// Fold the per-turn total state into the session cumulative.
// Unknown poisons the session permanently; Exact adds to running sum;
// Unseen (turn emitted no usage) leaves the cumulative unchanged.
@@ -768,6 +803,7 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
s.accumulated_input_tokens,
s.accumulated_output_tokens,
s.accumulated_cached_input_tokens,
s.accumulated_cache_write_tokens,
s.accumulated_total_state,
))
} else {
@@ -776,18 +812,28 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
None
}
};
if let Some((accumulated_in, accumulated_out, accumulated_cached, accumulated_total)) =
accumulated
if let Some((
accumulated_in,
accumulated_out,
accumulated_cached,
accumulated_written,
accumulated_total,
)) = accumulated
{
// Same builder the run loop uses for its per-round reports, so the
// final notification is shape-identical to the ones that preceded
// it and a consumer taking the high-water mark lands on this one.
let update = wire::usage_update_payload(
accumulated_in,
accumulated_out,
accumulated_cached,
accumulated_in.exact_value(),
accumulated_out.exact_value(),
accumulated_cached.exact_value(),
accumulated_written.exact_value(),
accumulated_total,
effective_model_str,
// Pass the proven per-turn identity if consistent; absent otherwise.
turn_pricing_identity
.as_ref()
.and_then(|inner| inner.as_ref()),
);
wire::send(&wire_tx, goose_session_update(&sid, update)).await;
}
@@ -874,6 +920,7 @@ async fn acquire_session(
input_tokens: s.accumulated_input_tokens,
output_tokens: s.accumulated_output_tokens,
cached_input_tokens: s.accumulated_cached_input_tokens,
cache_write_tokens: s.accumulated_cache_write_tokens,
total_state: s.accumulated_total_state,
},
))
+200 -13
View File
@@ -227,6 +227,17 @@ impl Llm {
.await
}
};
// Stamp the actually-requested model into the response so the usage
// accumulation path can derive pricingIdentity. For OpenAi/Databricks
// this is done per-response inside openai_request_for_model (where the
// resolved request_model is known after mesh/auto resolution). For the
// remaining providers the effective_model IS the request model.
let result = result.map(|mut r| {
if r.request_model.is_none() {
r.request_model = Some(effective_model.to_string());
}
r
});
// Stamp the effective model into Llm errors so log lines carry
// `llm: (model-name) 404 Not Found: …` instead of the bare status.
// The `llm: ` prefix comes from `Display for AgentError::Llm`; the
@@ -618,6 +629,10 @@ impl Llm {
self.post_openai(cfg, "/responses", &body, request_model)
.await?,
)
.map(|mut r| {
r.request_model = Some(request_model.to_string());
r
})
.map_err(PostError::from);
}
let (body, parse) = build(false, request_model);
@@ -625,7 +640,12 @@ impl Llm {
.post_openai(cfg, "/chat/completions", &body, request_model)
.await
{
Ok(value) => parse(value).map_err(PostError::from),
Ok(value) => parse(value)
.map(|mut r| {
r.request_model = Some(request_model.to_string());
r
})
.map_err(PostError::from),
Err(PostError::Agent(error))
if cfg.openai_api == OpenAiApi::Auto && self.try_upgrade(&error) =>
{
@@ -634,6 +654,10 @@ impl Llm {
self.post_openai(cfg, "/responses", &body, request_model)
.await?,
)
.map(|mut r| {
r.request_model = Some(request_model.to_string());
r
})
.map_err(PostError::from)
}
Err(error) => Err(error),
@@ -1330,27 +1354,33 @@ fn parse_responses(v: Value) -> Result<LlmResponse, AgentError> {
Some("completed") => ProviderStop::EndTurn,
_ => ProviderStop::Other,
};
let input_tokens = sum_usage(&v, &["input_tokens"]);
let output_tokens = sum_usage(&v, &["output_tokens"]);
let input_tokens = sum_usage(&v, &["input_tokens"]).and_then(SumUsageResult::into_exact);
let output_tokens = sum_usage(&v, &["output_tokens"]).and_then(SumUsageResult::into_exact);
// The Responses API nests the cache split under `input_tokens_details`.
let cached_input_tokens = usage_first(
&v,
&["cache_read_input_tokens"],
&[("input_tokens_details", "cached_tokens")],
);
// Responses API does not expose cache-write tokens today; stays None.
let cache_write_tokens: Option<u64> = None;
// Responses API reports a genuine provider total. Read it directly —
// never derived, so it stays None when the provider omits it.
let total_tokens = sum_usage(&v, &["total_tokens"]);
let total_tokens = sum_usage(&v, &["total_tokens"]).and_then(SumUsageResult::into_exact);
Ok(LlmResponse {
text,
tool_calls,
stop,
input_tokens,
input_tokens_overflowed: false, // input_tokens is a single field; cannot overflow
cached_input_tokens,
cache_write_tokens,
output_tokens,
total_tokens,
reasoning,
reasoning_details: None,
// Stamped by the dispatch layer (openai_request_for_model) after parse.
request_model: None,
})
}
@@ -1364,28 +1394,70 @@ fn map_stop(s: Option<&str>) -> ProviderStop {
}
}
/// Result of a multi-field token sum — distinguishes a successful total from an
/// arithmetic overflow so callers can propagate the overflow signal rather than
/// silently clamping to `u64::MAX`.
#[derive(Debug, PartialEq)]
enum SumUsageResult {
/// At least one field was present and the total did not overflow.
Exact(u64),
/// At least one field was present but the cumulative sum overflowed `u64`.
Overflow,
}
impl SumUsageResult {
/// Returns `Some(n)` when exact, `None` when overflow or when the caller
/// needs to signal an explicit absence. Single-field callers whose sums
/// cannot overflow use this to convert back to `Option<u64>`.
fn into_exact(self) -> Option<u64> {
match self {
SumUsageResult::Exact(n) => Some(n),
SumUsageResult::Overflow => None,
}
}
}
/// Sum a set of `usage` token fields, returning `None` only when the `usage`
/// object is absent or carries none of the requested fields. A field that is
/// present is added; a field that is missing contributes 0. This keeps the
/// result an inclusive total (so cached tokens are never silently dropped)
/// while still distinguishing "no usage reported" from "usage was zero".
fn sum_usage(v: &Value, fields: &[&str]) -> Option<u64> {
///
/// Returns [`SumUsageResult::Overflow`] if the cumulative total exceeds `u64::MAX`.
/// Single-field callers whose sums cannot overflow may call `.into_exact()` to
/// convert back to `Option<u64>` without loss.
fn sum_usage(v: &Value, fields: &[&str]) -> Option<SumUsageResult> {
let usage = v.get("usage")?;
let mut total: u64 = 0;
let mut saw_any = false;
let mut overflowed = false;
for f in fields {
if let Some(n) = usage.get(*f).and_then(Value::as_u64) {
total = total.saturating_add(n);
match total.checked_add(n) {
Some(t) => total = t,
None => overflowed = true,
}
saw_any = true;
}
}
saw_any.then_some(total)
if !saw_any {
return None;
}
if overflowed {
Some(SumUsageResult::Overflow)
} else {
Some(SumUsageResult::Exact(total))
}
}
/// Input-token total for Anthropic / Databricks (Anthropic-style) responses.
/// `input_tokens` alone EXCLUDES cached tokens, so we sum it with the two
/// cache fields to get the inclusive total the context budget must gate on.
fn anthropic_input_tokens(v: &Value) -> Option<u64> {
///
/// Returns [`SumUsageResult::Overflow`] when the sum of the three fields
/// exceeds `u64::MAX` — the caller must propagate the overflow signal rather
/// than clamping.
fn anthropic_input_tokens(v: &Value) -> Option<SumUsageResult> {
sum_usage(
v,
&[
@@ -1415,7 +1487,7 @@ fn anthropic_input_tokens(v: &Value) -> Option<u64> {
/// The two never collide here: the router sends `claude*` models to the
/// Anthropic route, so `parse_openai` only ever sees inclusive `prompt_tokens`.
fn openai_chat_input_tokens(v: &Value) -> Option<u64> {
sum_usage(v, &["prompt_tokens"])
sum_usage(v, &["prompt_tokens"]).and_then(SumUsageResult::into_exact)
}
/// First present value among `usage.<field>` and `usage.<nested>.<leaf>` pairs.
@@ -1576,23 +1648,38 @@ fn parse_anthropic(v: Value) -> Result<LlmResponse, AgentError> {
}
}
}
let input_tokens = anthropic_input_tokens(&v);
let output_tokens = sum_usage(&v, &["output_tokens"]);
// anthropic_input_tokens() returns Option<SumUsageResult> because it sums
// three fields that can collectively overflow u64. Propagate the overflow
// signal via `input_tokens_overflowed` so the run loop can poison the
// turn accumulator rather than treating u64::MAX as an exact reading.
let (input_tokens, input_tokens_overflowed) = match anthropic_input_tokens(&v) {
Some(SumUsageResult::Exact(n)) => (Some(n), false),
Some(SumUsageResult::Overflow) => (None, true),
None => (None, false),
};
let output_tokens = sum_usage(&v, &["output_tokens"]).and_then(SumUsageResult::into_exact);
// Anthropic reports the cache split flat on `usage`. Note this is already
// part of `input_tokens` above, which sums it in deliberately.
let cached_input_tokens = usage_first(&v, &["cache_read_input_tokens"], &[]);
// Anthropic reports cache-creation tokens as `cache_creation_input_tokens`;
// also a subset of `input_tokens` (already folded in above).
let cache_write_tokens = usage_first(&v, &["cache_creation_input_tokens"], &[]);
Ok(LlmResponse {
text,
tool_calls,
stop,
input_tokens,
input_tokens_overflowed,
cached_input_tokens,
cache_write_tokens,
output_tokens,
// Anthropic reports only category counts; NIP-AM forbids deriving a
// total from them. Always None for this provider.
total_tokens: None,
reasoning,
reasoning_details: None,
// Stamped by the dispatch layer (complete) after parse.
request_model: None,
})
}
@@ -1690,21 +1777,29 @@ fn parse_openai(v: Value) -> Result<LlmResponse, AgentError> {
}
dedupe_provider_ids(&mut tool_calls);
let input_tokens = openai_chat_input_tokens(&v);
let output_tokens = sum_usage(&v, &["completion_tokens"]);
let output_tokens = sum_usage(&v, &["completion_tokens"]).and_then(SumUsageResult::into_exact);
let cached_input_tokens = openai_chat_cached_tokens(&v);
// OpenAI Chat Completions reports cache-write tokens under
// `prompt_tokens_details.cache_write_tokens`. A subset of `prompt_tokens`.
let cache_write_tokens =
usage_first(&v, &[], &[("prompt_tokens_details", "cache_write_tokens")]);
// OpenAI Chat Completions reports a genuine provider total. Read it
// directly — never derived, so it stays None when the provider omits it.
let total_tokens = sum_usage(&v, &["total_tokens"]);
let total_tokens = sum_usage(&v, &["total_tokens"]).and_then(SumUsageResult::into_exact);
Ok(LlmResponse {
text,
tool_calls,
stop,
input_tokens,
input_tokens_overflowed: false, // prompt_tokens is a single field; cannot overflow
cached_input_tokens,
cache_write_tokens,
output_tokens,
total_tokens,
reasoning,
reasoning_details: None,
// Stamped by the dispatch layer (openai_request_for_model) after parse.
request_model: None,
})
}
@@ -6124,6 +6219,98 @@ mod tests {
assert_eq!(sum_usage(&v, &["input_tokens", "prompt_tokens"]), None);
}
#[test]
fn sum_usage_exact_single_field() {
let v = serde_json::json!({"usage": {"input_tokens": 42}});
assert_eq!(
sum_usage(&v, &["input_tokens"]),
Some(SumUsageResult::Exact(42))
);
}
#[test]
fn sum_usage_exact_two_fields() {
let v = serde_json::json!({"usage": {"input_tokens": 100, "cache_read_input_tokens": 50}});
assert_eq!(
sum_usage(&v, &["input_tokens", "cache_read_input_tokens"]),
Some(SumUsageResult::Exact(150))
);
}
#[test]
fn sum_usage_overflow_two_fields_signals_overflow() {
// Reproducer from Thufir's finding: input_tokens = u64::MAX, cache_read = 1.
// Must return Overflow, not Exact(u64::MAX).
let v = serde_json::json!({
"usage": {
"input_tokens": u64::MAX,
"cache_read_input_tokens": 1_u64,
"cache_creation_input_tokens": 0_u64
}
});
assert_eq!(
sum_usage(
&v,
&[
"input_tokens",
"cache_read_input_tokens",
"cache_creation_input_tokens"
]
),
Some(SumUsageResult::Overflow)
);
}
#[test]
fn parse_anthropic_input_overflow_sets_flag_and_clears_value() {
// The inclusive sum overflows; input_tokens must be None and the flag set.
let v = serde_json::json!({
"stop_reason": "end_turn",
"content": [{"type": "text", "text": "hi"}],
"usage": {
"input_tokens": u64::MAX,
"cache_read_input_tokens": 1_u64,
"cache_creation_input_tokens": 0_u64,
"output_tokens": 7
}
});
let r = parse_anthropic(v).unwrap();
assert_eq!(r.input_tokens, None, "overflowed value must be discarded");
assert!(r.input_tokens_overflowed, "overflow flag must be set");
// output_tokens unaffected by input overflow
assert_eq!(r.output_tokens, Some(7));
}
#[test]
fn parse_anthropic_normal_sum_does_not_set_flag() {
// Normal (non-overflow) path: flag stays false.
let v = serde_json::json!({
"stop_reason": "end_turn",
"content": [{"type": "text", "text": "hi"}],
"usage": {
"input_tokens": 100_u64,
"cache_read_input_tokens": 900_u64,
"cache_creation_input_tokens": 50_u64,
"output_tokens": 7
}
});
let r = parse_anthropic(v).unwrap();
assert_eq!(r.input_tokens, Some(1050));
assert!(!r.input_tokens_overflowed);
}
#[test]
fn parse_anthropic_absent_usage_does_not_set_flag() {
// Absent usage: flag stays false, input_tokens stays None.
let v = serde_json::json!({
"stop_reason": "end_turn",
"content": [{"type": "text", "text": "hi"}]
});
let r = parse_anthropic(v).unwrap();
assert_eq!(r.input_tokens, None);
assert!(!r.input_tokens_overflowed);
}
/// A token source whose `bearer()` always hands back the same stale
/// token and whose `refresh_now()` mints a distinct fresh one, counting
/// each refresh. Lets a test assert exactly how many forced refreshes a
+451 -8
View File
@@ -167,6 +167,14 @@ pub struct LlmResponse {
/// tokens, so reading it alone would undercount). Used to gate handoff on
/// the real token budget rather than a byte estimate.
pub input_tokens: Option<u64>,
/// `true` when the Anthropic-style inclusive input sum (`input_tokens +
/// cache_read_input_tokens + cache_creation_input_tokens`) overflowed
/// `u64::MAX` during parsing. When set, `input_tokens` is `None` (the
/// clamped value is discarded) and the run loop must poison the input
/// `TurnIOState` and freeze the context-gate baseline rather than treating
/// the response as absent-usage. Always `false` for OpenAI / Responses
/// API parsers, whose input field is a single scalar that cannot overflow.
pub input_tokens_overflowed: bool,
/// The portion of `input_tokens` the provider served from its prompt cache,
/// or `None` when the response reported no cache split. Providers bill this
/// slice at a large discount (roughly 10x for both OpenAI and Anthropic),
@@ -178,6 +186,15 @@ pub struct LlmResponse {
/// provider we speak to reports an inclusive input total, so adding this
/// would double-count.
pub cached_input_tokens: Option<u64>,
/// The portion of `input_tokens` the provider consumed to WRITE to its
/// prompt cache (i.e. cache-creation tokens), or `None` when the provider
/// did not report a cache-write split.
///
/// This is also a subset of `input_tokens`, not an addition. On Anthropic
/// this is `cache_creation_input_tokens`; on OpenAI Chat it is
/// `prompt_tokens_details.cache_write_tokens`. The Responses API does not
/// expose this field today — it stays `None` for that route.
pub cache_write_tokens: Option<u64>,
/// Output tokens the provider reported for this request, or `None` if the
/// response carried no usage. Used to accumulate per-turn output counts
/// for NIP-AM metric publishing.
@@ -202,6 +219,32 @@ pub struct LlmResponse {
/// Replayed on subsequent turns so the model can continue its chain-of-thought.
/// `None` for all non-OpenRouter providers.
pub reasoning_details: Option<Value>,
/// The model id that was actually sent in the request body — the
/// actually-requested model after any auto/mesh resolution. Populated by
/// the LLM dispatch layer, not the JSON parser. `None` only for routes
/// where the model is unknown or irrelevant (should not occur in practice).
///
/// Used by the usage accumulation path to stamp `pricingIdentity.model`;
/// distinct from `effective_model`, which is the configured/session model.
pub request_model: Option<String>,
}
/// Publisher-side billing identity for a turn.
///
/// Mirrors `buzz_core::agent_turn_metric::PricingIdentity` but is local to
/// `buzz-agent` (which does not depend on `buzz-core`). The two structs have
/// the same camelCase wire representation and are deserialized identically by
/// `buzz-acp`.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PricingIdentity {
/// Registered billing-namespace identifier (bare lowercase hostname).
pub authority: String,
/// Actually-requested billable model identifier.
pub model: String,
/// Cache-write class when applicable; omitted otherwise.
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_class: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -220,11 +263,76 @@ pub struct ToolDef {
pub input_schema: Value,
}
/// Tri-state accumulator for provider-reported total tokens within one ACP turn.
/// Per-turn and per-session accumulator for a single cache token category
/// (cache-read or cache-write).
///
/// Tracks whether every usage-bearing LLM response in the turn supplied a genuine
/// provider total. Used to accumulate a reliable per-turn total and contribute to
/// the session-cumulative total.
/// Analogous to [`TurnTotalState`] but for a cache category that is optional
/// at the provider level:
///
/// - `Unseen`: no usage-bearing response has reported this category yet (initial
/// state for each turn / session). Emitted as absent on the wire.
/// - `Exact(n)`: every usage-bearing response so far reported the category;
/// `n` is their sum. Emitted as `Some(n)` on the wire.
/// - `Unknown`: at least one usage-bearing response omitted the category while
/// reporting input/output tokens — permanently poisoned. Emitted as absent.
///
/// A `Some(0)` reported by the provider folds to `Exact(0)` — explicit zero
/// is distinct from absence and must be preserved.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CacheTotalState {
#[default]
Unseen,
Exact(u64),
Unknown,
}
impl CacheTotalState {
/// Fold one provider-reported cache category value into the current state.
///
/// Called on every usage-bearing response:
/// - `Some(n)` → `Exact(acc + n)` (or `Unknown` on overflow)
/// - `None` → `Unknown` (category absent on a usage-bearing response → poison)
pub fn fold(self, value: Option<u64>) -> CacheTotalState {
match (self, value) {
(CacheTotalState::Unknown, _) => CacheTotalState::Unknown,
(_, None) => CacheTotalState::Unknown,
(CacheTotalState::Unseen, Some(n)) => CacheTotalState::Exact(n),
(CacheTotalState::Exact(acc), Some(n)) => match acc.checked_add(n) {
Some(sum) => CacheTotalState::Exact(sum),
None => CacheTotalState::Unknown,
},
}
}
/// Merge a completed turn's cache state into the session-cumulative state.
///
/// - `Unseen` turn → cumulative unchanged (category never seen this turn)
/// - Any `Unknown` side → session permanently poisoned
/// - Two `Exact` values → summed with overflow → `Unknown`
pub fn merge_session(self, turn: CacheTotalState) -> CacheTotalState {
match (self, turn) {
(CacheTotalState::Unknown, _) | (_, CacheTotalState::Unknown) => {
CacheTotalState::Unknown
}
(acc, CacheTotalState::Unseen) => acc,
(CacheTotalState::Unseen, CacheTotalState::Exact(n)) => CacheTotalState::Exact(n),
(CacheTotalState::Exact(acc), CacheTotalState::Exact(n)) => match acc.checked_add(n) {
Some(sum) => CacheTotalState::Exact(sum),
None => CacheTotalState::Unknown,
},
}
}
/// Return `Some(n)` only when `Exact`; `None` for `Unseen` or `Unknown`.
pub fn exact_value(self) -> Option<u64> {
match self {
CacheTotalState::Exact(n) => Some(n),
_ => None,
}
}
}
/// Tri-state accumulator for provider-reported total tokens within one ACP turn.
///
/// - `Unseen`: no usage-bearing response observed yet (initial state for each turn).
/// - `Exact(n)`: every response so far reported a total; `n` is their sum.
@@ -308,6 +416,74 @@ impl TurnTotalState {
}
}
/// Per-turn and per-session accumulator for input or output token counts.
///
/// Unlike [`CacheTotalState`] and [`TurnTotalState`], absence of a field on a
/// usage-bearing response does NOT poison this accumulator — that behaviour was
/// reviewed and explicitly cleared (absence means the provider did not report
/// it, not that the value is unknown). Only arithmetic overflow transitions
/// the state to `Poisoned`.
///
/// States:
/// - `Unseen`: no usage-bearing response has contributed a value yet (initial
/// state for each turn / session). Emitted as absent on the wire.
/// - `Exact(n)`: every contributing response supplied a value; `n` is their
/// sum. Emitted as the value on the wire.
/// - `Poisoned`: the running sum overflowed `u64::MAX` — permanently poisoned
/// for this turn. The session-cumulative also transitions to `Poisoned` when
/// any turn lands `Poisoned`, and stays there until a new session resets it.
/// Emitted as absent (the wire field is omitted, never `null`, never MAX).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TurnIOState {
#[default]
Unseen,
Exact(u64),
Poisoned,
}
impl TurnIOState {
/// Fold one provider-reported value into the current state.
///
/// Called for each usage-bearing response that carries this category.
/// Absence (`None`) is handled by the caller; only present values reach
/// here. Overflow → `Poisoned`.
pub fn fold_round(self, n: u64) -> TurnIOState {
match self {
TurnIOState::Poisoned => TurnIOState::Poisoned,
TurnIOState::Unseen => TurnIOState::Exact(n),
TurnIOState::Exact(acc) => match acc.checked_add(n) {
Some(sum) => TurnIOState::Exact(sum),
None => TurnIOState::Poisoned,
},
}
}
/// Merge a completed turn's state into the session-cumulative state.
///
/// - `Unseen` turn (no provider response contributed) → cumulative unchanged.
/// - Either side `Poisoned` → session permanently poisoned.
/// - Two `Exact` values → summed with overflow check; overflow → `Poisoned`.
pub fn merge_session(self, turn: TurnIOState) -> TurnIOState {
match (self, turn) {
(TurnIOState::Poisoned, _) | (_, TurnIOState::Poisoned) => TurnIOState::Poisoned,
(acc, TurnIOState::Unseen) => acc,
(TurnIOState::Unseen, TurnIOState::Exact(n)) => TurnIOState::Exact(n),
(TurnIOState::Exact(acc), TurnIOState::Exact(n)) => match acc.checked_add(n) {
Some(sum) => TurnIOState::Exact(sum),
None => TurnIOState::Poisoned,
},
}
}
/// Return `Some(n)` only when `Exact`; `None` for `Unseen` or `Poisoned`.
pub fn exact_value(self) -> Option<u64> {
match self {
TurnIOState::Exact(n) => Some(n),
_ => None,
}
}
}
/// The session-cumulative usage counters as of the START of a turn.
///
/// Copied out of the session under the lock when a turn begins and handed to
@@ -325,10 +501,14 @@ impl TurnTotalState {
/// terminated mid-turn by design.
#[derive(Debug, Clone, Copy, Default)]
pub struct SessionUsageBaseline {
pub input_tokens: u64,
pub output_tokens: u64,
/// The cache-served subset of `input_tokens`, not an addition to it.
pub cached_input_tokens: u64,
pub input_tokens: TurnIOState,
pub output_tokens: TurnIOState,
/// Tri-state for the cache-served subset of `input_tokens`.
/// `Unseen` = never observed; `Exact(n)` = cumulative known; `Unknown` = poisoned.
pub cached_input_tokens: CacheTotalState,
/// Tri-state for the cache-written subset of `input_tokens`.
/// `Unseen` = never observed; `Exact(n)` = cumulative known; `Unknown` = poisoned.
pub cache_write_tokens: CacheTotalState,
pub total_state: TurnTotalState,
}
@@ -701,3 +881,266 @@ mod turn_total_state_tests {
);
}
}
#[cfg(test)]
mod cache_total_state_tests {
use super::CacheTotalState;
// ── CacheTotalState::fold ─────────────────────────────────────────────
#[test]
fn fold_first_some_zero_is_exact_zero() {
// Some(0) is an explicit provider report of zero — distinct from absence.
assert_eq!(
CacheTotalState::Unseen.fold(Some(0)),
CacheTotalState::Exact(0),
);
}
#[test]
fn fold_first_some_value_becomes_exact() {
assert_eq!(
CacheTotalState::Unseen.fold(Some(100)),
CacheTotalState::Exact(100),
);
}
#[test]
fn fold_absent_on_first_usage_bearing_round_becomes_unknown() {
assert_eq!(CacheTotalState::Unseen.fold(None), CacheTotalState::Unknown,);
}
#[test]
fn fold_present_then_missing_poisons_and_never_heals() {
// present → missing → present: must stay Unknown the whole way.
let state = CacheTotalState::Unseen;
let state = state.fold(Some(100)); // Exact(100)
let state = state.fold(None); // absent → Unknown
assert_eq!(state, CacheTotalState::Unknown);
// A later present value must not un-poison.
let state = state.fold(Some(50));
assert_eq!(
state,
CacheTotalState::Unknown,
"present→missing→present must stay Unknown (no healing)"
);
}
#[test]
fn fold_multiple_present_values_sum_correctly() {
let state = CacheTotalState::Unseen;
let state = state.fold(Some(100));
let state = state.fold(Some(200));
let state = state.fold(Some(0)); // explicit zero round
assert_eq!(state, CacheTotalState::Exact(300));
}
#[test]
fn fold_overflow_poisons_not_saturates() {
let state = CacheTotalState::Exact(u64::MAX);
assert_eq!(
state.fold(Some(1)),
CacheTotalState::Unknown,
"overflow in fold() must produce Unknown, not Exact(u64::MAX)"
);
}
#[test]
fn unknown_stays_unknown_regardless_of_subsequent_values() {
assert_eq!(
CacheTotalState::Unknown.fold(Some(999)),
CacheTotalState::Unknown,
);
assert_eq!(
CacheTotalState::Unknown.fold(None),
CacheTotalState::Unknown,
);
}
// ── CacheTotalState::exact_value ──────────────────────────────────────
#[test]
fn exact_value_returns_some_only_for_exact_variant() {
// exact_value() drives wire omission: Unseen and Unknown must not emit.
assert_eq!(CacheTotalState::Unseen.exact_value(), None);
assert_eq!(CacheTotalState::Unknown.exact_value(), None);
assert_eq!(CacheTotalState::Exact(42).exact_value(), Some(42));
assert_eq!(CacheTotalState::Exact(0).exact_value(), Some(0));
}
// ── CacheTotalState::merge_session ────────────────────────────────────
#[test]
fn merge_session_unseen_turn_leaves_cumulative_unchanged() {
// A turn with no usage-bearing responses (Unseen) must not alter the cumulative.
assert_eq!(
CacheTotalState::Exact(100).merge_session(CacheTotalState::Unseen),
CacheTotalState::Exact(100),
);
assert_eq!(
CacheTotalState::Unseen.merge_session(CacheTotalState::Unseen),
CacheTotalState::Unseen,
);
}
#[test]
fn merge_session_exact_turn_adds_to_exact_cumulative() {
assert_eq!(
CacheTotalState::Exact(100).merge_session(CacheTotalState::Exact(50)),
CacheTotalState::Exact(150),
);
}
#[test]
fn merge_session_first_exact_turn_from_unseen_adopts_value() {
assert_eq!(
CacheTotalState::Unseen.merge_session(CacheTotalState::Exact(200)),
CacheTotalState::Exact(200),
);
}
#[test]
fn merge_session_unknown_turn_poisons_cumulative_permanently() {
// A single unknown (poisoned) turn permanently poisons the session cumulative.
assert_eq!(
CacheTotalState::Exact(100).merge_session(CacheTotalState::Unknown),
CacheTotalState::Unknown,
);
// Poisoned cumulative stays poisoned even with an Unseen turn.
assert_eq!(
CacheTotalState::Unknown.merge_session(CacheTotalState::Unseen),
CacheTotalState::Unknown,
);
// Poisoned cumulative stays poisoned even with a later Exact turn.
assert_eq!(
CacheTotalState::Unknown.merge_session(CacheTotalState::Exact(999)),
CacheTotalState::Unknown,
);
}
#[test]
fn merge_session_overflow_poisons_not_saturates() {
assert_eq!(
CacheTotalState::Exact(u64::MAX).merge_session(CacheTotalState::Exact(1)),
CacheTotalState::Unknown,
"overflow in merge_session() must produce Unknown, not Exact(u64::MAX)"
);
}
}
#[cfg(test)]
mod turn_io_state_tests {
use super::*;
/// Folding a value into Unseen yields Exact.
#[test]
fn fold_round_unseen_becomes_exact() {
assert_eq!(TurnIOState::Unseen.fold_round(100), TurnIOState::Exact(100));
}
/// Folding a second value into Exact sums them.
#[test]
fn fold_round_exact_accumulates() {
assert_eq!(
TurnIOState::Exact(300).fold_round(200),
TurnIOState::Exact(500)
);
}
/// Overflow on fold_round → Poisoned, never u64::MAX.
#[test]
fn fold_round_overflow_within_turn_produces_poisoned_not_saturates() {
let near_max = u64::MAX - 10;
assert_eq!(
TurnIOState::Exact(near_max).fold_round(20),
TurnIOState::Poisoned,
"within-turn overflow must produce Poisoned, not Exact(u64::MAX)"
);
}
/// Poisoned stays Poisoned on further folds.
#[test]
fn fold_round_poisoned_stays_poisoned() {
assert_eq!(TurnIOState::Poisoned.fold_round(1), TurnIOState::Poisoned);
}
/// Two rounds each with ~u64::MAX/2 + 1 must poison.
#[test]
fn fold_round_two_large_values_poison() {
let half = u64::MAX / 2 + 1;
let state = TurnIOState::Unseen.fold_round(half).fold_round(half);
assert_eq!(
state,
TurnIOState::Poisoned,
"two rounds summing past u64::MAX must produce Poisoned"
);
}
/// Unseen turn leaves cumulative unchanged.
#[test]
fn merge_session_unseen_turn_leaves_cumulative_unchanged() {
assert_eq!(
TurnIOState::Exact(500).merge_session(TurnIOState::Unseen),
TurnIOState::Exact(500)
);
}
/// First exact turn from Unseen cumulative adopts the value.
#[test]
fn merge_session_first_exact_turn_from_unseen_adopts_value() {
assert_eq!(
TurnIOState::Unseen.merge_session(TurnIOState::Exact(200)),
TurnIOState::Exact(200)
);
}
/// Exact turn adds to exact cumulative.
#[test]
fn merge_session_exact_turn_adds_to_exact_cumulative() {
assert_eq!(
TurnIOState::Exact(1000).merge_session(TurnIOState::Exact(200)),
TurnIOState::Exact(1200)
);
}
/// Session-cumulative overflow → Poisoned permanently.
#[test]
fn merge_session_overflow_poisons_session_not_saturates() {
let near_max = u64::MAX - 100;
assert_eq!(
TurnIOState::Exact(near_max).merge_session(TurnIOState::Exact(200)),
TurnIOState::Poisoned,
"turn-to-session overflow must produce Poisoned, not Exact(u64::MAX)"
);
}
/// Poisoned cumulative stays Poisoned regardless of the turn.
#[test]
fn merge_session_poisoned_cumulative_stays_poisoned() {
assert_eq!(
TurnIOState::Poisoned.merge_session(TurnIOState::Exact(100)),
TurnIOState::Poisoned
);
assert_eq!(
TurnIOState::Poisoned.merge_session(TurnIOState::Unseen),
TurnIOState::Poisoned
);
}
/// A poisoned turn permanently poisons the session cumulative.
#[test]
fn merge_session_poisoned_turn_poisons_cumulative_permanently() {
assert_eq!(
TurnIOState::Exact(1000).merge_session(TurnIOState::Poisoned),
TurnIOState::Poisoned
);
}
/// exact_value returns Some only for Exact.
#[test]
fn exact_value_only_for_exact_variant() {
assert_eq!(TurnIOState::Exact(42).exact_value(), Some(42));
assert_eq!(TurnIOState::Unseen.exact_value(), None);
assert_eq!(TurnIOState::Poisoned.exact_value(), None);
}
}
+213 -11
View File
@@ -160,33 +160,90 @@ pub fn goose_session_update(sid: &str, update: Value) -> Value {
///
/// All counts are SESSION-cumulative, matching goose, so buzz-acp's
/// `UsageTracker` can compute per-turn deltas symmetrically for both agents.
///
/// ## `_goose/unstable/session/update` contract (ACP)
///
/// | Field | Type | Semantics |
/// |---|---|---|
/// | `sessionUpdate` | `"usage_update"` | Discriminant |
/// | `used` | `u64` | `input + output`; context-usage proxy |
/// | `contextLimit` | `u64` | `0` — buzz-agent has no context limit tracking |
/// | `accumulatedInputTokens` | `u64?` | Session-cumulative inclusive input tokens; **absent** when overflow-poisoned |
/// | `accumulatedOutputTokens` | `u64?` | Session-cumulative output tokens; **absent** when overflow-poisoned |
/// | `accumulatedCachedInputTokens` | `u64?` | Session-cumulative cache-read tokens; **absent** when never observed (harness restart, goose, or first turn); `0` when provider confirmed no cache hits |
/// | `accumulatedCacheWriteTokens` | `u64?` | Session-cumulative cache-write tokens; **absent** when never observed (same rules as cached-read) |
/// | `accumulatedTotalTokens` | `u64?` | Session-cumulative provider total; absent unless every turn reported one |
/// | `model` | `string` | Effective model id |
///
/// **Absence vs explicit zero**: both cache fields are omitted (never `null`)
/// when the running session has never observed a value for them. An old-harness
/// consumer that does not recognise these fields ignores them cleanly; a new
/// consumer that receives them absent treats them as unknown, not zero.
///
/// **Overflow poison**: `accumulatedInputTokens` and `accumulatedOutputTokens`
/// are omitted (never `null`, never `u64::MAX`) when the session-cumulative sum
/// has overflowed. A consumer that receives them absent treats them as
/// incomplete, consistent with the `accumulatedTotalTokens` contract.
///
/// **Monotonic within a session**: each notification carries a cumulative
/// snapshot that can only increase. A consumer that takes the high-water mark
/// always ends up at the final value.
///
/// **Per-category independent**: a session where `accumulatedCacheWriteTokens`
/// is absent but `accumulatedCachedInputTokens` is present is valid — the two
/// categories are tracked independently. Either can become absent (e.g. after
/// a provider switch) without poisoning the other.
pub fn usage_update_payload(
accumulated_input_tokens: u64,
accumulated_output_tokens: u64,
accumulated_cached_input_tokens: u64,
accumulated_input_tokens: Option<u64>,
accumulated_output_tokens: Option<u64>,
accumulated_cached_input_tokens: Option<u64>,
accumulated_cache_write_tokens: Option<u64>,
accumulated_total: crate::types::TurnTotalState,
model: &str,
pricing_identity: Option<&crate::types::PricingIdentity>,
) -> Value {
let mut update = json!({
"sessionUpdate": "usage_update",
// used: total tokens as a context-usage proxy;
// used: total tokens as a context-usage proxy; saturate when either
// side is absent or poisoned (display-only, ACP treats it as dead code).
// contextLimit: 0 (buzz-agent has no context limit tracking).
"used": accumulated_input_tokens.saturating_add(accumulated_output_tokens),
"used": accumulated_input_tokens.unwrap_or(0).saturating_add(accumulated_output_tokens.unwrap_or(0)),
"contextLimit": 0u64,
"accumulatedInputTokens": accumulated_input_tokens,
"accumulatedOutputTokens": accumulated_output_tokens,
// A subset of accumulatedInputTokens, not an addition to it. Extends
// goose's usage_update shape; a consumer that does not know the field
// ignores it and prices exactly as it did before.
"accumulatedCachedInputTokens": accumulated_cached_input_tokens,
"model": model,
});
// accumulatedInputTokens / accumulatedOutputTokens: omitted (never null,
// never u64::MAX) when the session-cumulative sum has overflowed.
if let Some(input) = accumulated_input_tokens {
update["accumulatedInputTokens"] = json!(input);
}
if let Some(output) = accumulated_output_tokens {
update["accumulatedOutputTokens"] = json!(output);
}
// accumulatedCachedInputTokens: a subset of accumulatedInputTokens, not an
// addition to it. Extends goose's usage_update shape; a consumer that does
// not know the field ignores it and prices exactly as it did before.
// Absent (never emitted as null) when the session has never observed a
// cached-input value — goose never emits it, so old-harness compat is clean.
if let Some(cached) = accumulated_cached_input_tokens {
update["accumulatedCachedInputTokens"] = json!(cached);
}
// accumulatedCacheWriteTokens: a subset of accumulatedInputTokens, not an
// addition to it. Absent when never observed (same provenance rules as
// accumulatedCachedInputTokens).
if let Some(written) = accumulated_cache_write_tokens {
update["accumulatedCacheWriteTokens"] = json!(written);
}
// Only when the cumulative is exactly known — never when Unseen (no total
// ever observed) or Unknown (at least one turn lacked a total). A goose
// consumer that doesn't recognise the field ignores it.
if let Some(total) = accumulated_total.exact_value() {
update["accumulatedTotalTokens"] = json!(total);
}
// pricingIdentity: present only when the publisher proved applicability.
// Absent (never null) when unproven — old consumers ignore the field.
if let Some(pi) = pricing_identity {
update["pricingIdentity"] = serde_json::to_value(pi).unwrap_or(serde_json::Value::Null);
}
update
}
@@ -332,4 +389,149 @@ mod tests {
let params: SessionNewParams = serde_json::from_value(json).unwrap();
assert_eq!(params.system_prompt, Some(String::new()));
}
// ── usage_update_payload: pricingIdentity emission ───────────────────────
fn make_pi(authority: &str, model: &str) -> crate::types::PricingIdentity {
crate::types::PricingIdentity {
authority: authority.to_string(),
model: model.to_string(),
cache_class: None,
}
}
/// When a proven identity is passed, the wire payload MUST include a
/// `pricingIdentity` object with camelCase keys, and it MUST NOT be null.
#[test]
fn usage_update_payload_includes_pricing_identity_when_proven() {
let pi = make_pi("api.anthropic.com", "claude-opus-4-5");
let payload = usage_update_payload(
Some(1000),
Some(200),
None,
None,
crate::types::TurnTotalState::Unseen,
"claude-opus-4-5",
Some(&pi),
);
let pi_wire = &payload["pricingIdentity"];
assert!(
!pi_wire.is_null(),
"pricingIdentity must be present when identity is proven"
);
assert_eq!(pi_wire["authority"], serde_json::json!("api.anthropic.com"));
assert_eq!(pi_wire["model"], serde_json::json!("claude-opus-4-5"));
// cacheClass absent when None (skip_serializing_if)
assert!(pi_wire.get("cacheClass").is_none() || pi_wire["cacheClass"].is_null());
}
/// When `pricing_identity` is `None` (unproven: custom endpoint, mixed
/// identities, etc.), the field MUST be absent from the wire payload.
/// It must never appear as `null`.
#[test]
fn usage_update_payload_omits_pricing_identity_when_absent() {
let payload = usage_update_payload(
Some(500),
Some(100),
None,
None,
crate::types::TurnTotalState::Unseen,
"some-model",
None, // no proven identity
);
assert!(
payload.get("pricingIdentity").is_none(),
"pricingIdentity must be absent (never null) when identity is unproven"
);
}
/// A custom endpoint (Databricks, corporate proxy) must not produce a
/// wire `pricingIdentity` — it must be absent.
///
/// This relies on the caller passing `None`; the payload builder must not
/// inject a default. This test documents the contract.
#[test]
fn usage_update_payload_no_pricing_identity_for_custom_endpoint() {
// Custom endpoint → caller passes None (pricing_authority returned None).
let payload = usage_update_payload(
Some(800),
Some(150),
Some(200),
None,
crate::types::TurnTotalState::Unseen,
"databricks-llama-4",
None,
);
assert!(
payload.get("pricingIdentity").is_none(),
"custom endpoint: pricingIdentity must not appear on the wire"
);
// Cache field must still be present (independent).
assert_eq!(
payload["accumulatedCachedInputTokens"],
serde_json::json!(200)
);
}
/// When input is overflow-poisoned (None), the wire payload must omit
/// `accumulatedInputTokens` entirely — never null, never u64::MAX.
#[test]
fn usage_update_payload_omits_input_when_poisoned() {
let payload = usage_update_payload(
None, // overflow-poisoned
Some(150),
None,
None,
crate::types::TurnTotalState::Unseen,
"model",
None,
);
assert!(
payload.get("accumulatedInputTokens").is_none(),
"poisoned accumulatedInputTokens must be absent, not null or MAX"
);
// output still present
assert_eq!(payload["accumulatedOutputTokens"], serde_json::json!(150));
}
/// When output is overflow-poisoned (None), the wire payload must omit
/// `accumulatedOutputTokens` entirely — never null, never u64::MAX.
#[test]
fn usage_update_payload_omits_output_when_poisoned() {
let payload = usage_update_payload(
Some(800),
None, // overflow-poisoned
None,
None,
crate::types::TurnTotalState::Unseen,
"model",
None,
);
assert!(
payload.get("accumulatedOutputTokens").is_none(),
"poisoned accumulatedOutputTokens must be absent, not null or MAX"
);
// input still present
assert_eq!(payload["accumulatedInputTokens"], serde_json::json!(800));
}
/// When both are present and exact, the wire payload emits both at their
/// values (unchanged goose-compatible behavior).
#[test]
fn usage_update_payload_emits_both_when_exact() {
let payload = usage_update_payload(
Some(1000),
Some(200),
None,
None,
crate::types::TurnTotalState::Unseen,
"model",
None,
);
assert_eq!(payload["accumulatedInputTokens"], serde_json::json!(1000));
assert_eq!(payload["accumulatedOutputTokens"], serde_json::json!(200));
}
}
@@ -760,6 +760,114 @@ async fn test_no_reasoning_no_thought_chunk() {
h.shutdown().await;
}
/// An Anthropic Messages API response whose inclusive input sum overflows u64.
/// `input_tokens: u64::MAX` + `cache_read_input_tokens: 1` → overflow.
/// buzz-agent must emit a `usage_update` notification with `accumulatedInputTokens`
/// **absent** (never null, never u64::MAX) and `accumulatedOutputTokens` present.
fn anthropic_input_overflow_response() -> Value {
json!({
"id": "msg_overflow",
"type": "message",
"role": "assistant",
"model": "claude-fake",
"stop_reason": "end_turn",
"content": [{ "type": "text", "text": "overflow" }],
"usage": {
"input_tokens": u64::MAX,
"cache_read_input_tokens": 1,
"output_tokens": 7,
},
})
}
/// Collect every frame that arrives before the frame matching `pred`, then
/// return (frames_before, matching_frame). Used to inspect notifications
/// emitted before a specific response.
async fn drain_until<F>(h: &mut Harness, mut pred: F) -> (Vec<Value>, Value)
where
F: FnMut(&Value) -> bool,
{
let mut before = Vec::new();
loop {
let v = h.recv().await;
if pred(&v) {
return (before, v);
}
before.push(v);
}
}
/// When the Anthropic parser produces an input-sum overflow, buzz-agent must
/// omit `accumulatedInputTokens` from the `_goose/unstable/session/update`
/// `usage_update` notification — never null, never u64::MAX — while still
/// emitting `accumulatedOutputTokens` normally.
///
/// This is an end-to-end regression test: the canned response flows through
/// parse_anthropic → run loop → wire emission via the real production code
/// with no logic duplication.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_anthropic_input_overflow_omits_accumulated_input_tokens() {
let url = spawn_fake_llm(vec![anthropic_input_overflow_response()]).await;
let mut h = Harness::spawn(&[
("BUZZ_AGENT_PROVIDER", "anthropic"),
("ANTHROPIC_API_KEY", "test"),
("ANTHROPIC_MODEL", "claude-fake"),
("ANTHROPIC_BASE_URL", &url),
("OPENAI_COMPAT_BASE_URL", ""),
])
.await;
let sid = handshake(&mut h).await;
let p = h
.send(
"session/prompt",
json!({
"sessionId": sid,
"prompt": [{ "type": "text", "text": "overflow test" }],
}),
)
.await;
let (frames, final_resp) = drain_until(&mut h, |v| v.get("id") == Some(&json!(p))).await;
assert_eq!(
final_resp["result"]["stopReason"], "end_turn",
"turn must complete normally despite input overflow"
);
// Find the usage_update notification emitted before the response.
let usage = frames
.iter()
.find(|v| {
v.get("method") == Some(&json!("_goose/unstable/session/update"))
&& v["params"]["update"]["sessionUpdate"] == "usage_update"
})
.unwrap_or_else(|| {
panic!(
"expected _goose/unstable/session/update usage_update before response; frames: {frames:#?}"
)
});
let update = &usage["params"]["update"];
// Core regression: input overflow → accumulatedInputTokens ABSENT.
// A present value (even u64::MAX) would mean the saturated clamped sum
// leaked through the parse layer as an exact reading.
assert!(
update.get("accumulatedInputTokens").is_none(),
"accumulatedInputTokens must be absent when input sum overflows; got: {:?}",
update.get("accumulatedInputTokens")
);
// Output tokens are unaffected by the input overflow and must still emit.
assert_eq!(
update["accumulatedOutputTokens"],
json!(7u64),
"accumulatedOutputTokens must be present and exact despite input overflow"
);
h.shutdown().await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_cancel_notification_no_reply() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+40 -9
View File
@@ -829,11 +829,27 @@ fn validate_ttl_seconds(secs: i64) -> Result<i32, CliError> {
.map_err(|_| CliError::Usage(format!("--ttl is too large (max {} seconds)", i32::MAX)))
}
fn validate_update_channel_fields(
name: Option<&str>,
description: Option<&str>,
visibility: Option<&str>,
ttl_change: Option<Option<i32>>,
) -> Result<(), CliError> {
if name.is_none() && description.is_none() && visibility.is_none() && ttl_change.is_none() {
return Err(CliError::Usage(
"at least one field required (--name, --description, --visibility, --ttl, --no-ttl)"
.into(),
));
}
Ok(())
}
pub async fn cmd_update_channel(
client: &BuzzClient,
channel_id: &str,
name: Option<&str>,
description: Option<&str>,
visibility: Option<&str>,
ttl: Option<i64>,
no_ttl: bool,
) -> Result<(), CliError> {
@@ -845,15 +861,12 @@ pub async fn cmd_update_channel(
(None, false) => None,
};
if name.is_none() && description.is_none() && ttl_change.is_none() {
return Err(CliError::Usage(
"at least one field required (--name, --description, --ttl, --no-ttl)".into(),
));
}
validate_update_channel_fields(name, description, visibility, ttl_change)?;
let channel_uuid = parse_uuid(channel_id)?;
let builder = buzz_sdk::build_update_channel(channel_uuid, name, description, None, ttl_change)
.map_err(|e| CliError::Other(format!("build_update_channel failed: {e}")))?;
let builder =
buzz_sdk::build_update_channel(channel_uuid, name, description, visibility, ttl_change)
.map_err(|e| CliError::Other(format!("build_update_channel failed: {e}")))?;
let event = client.sign_event(builder)?;
let resp = client.submit_event(event).await?;
@@ -1128,14 +1141,17 @@ pub async fn dispatch(
channel,
name,
description,
visibility,
ttl,
no_ttl,
} => {
let visibility = visibility.as_ref().map(|v| v.to_string());
cmd_update_channel(
client,
&channel,
name.as_deref(),
description.as_deref(),
visibility.as_deref(),
ttl,
no_ttl,
)
@@ -1178,8 +1194,8 @@ mod tests {
use super::{
apply_cardinality_rule, build_template_report, cmd_set_add_policy,
finalize_roster_resolution, name_matches, resolve_roster_with_archive_filter,
validate_ttl_seconds, ArchivedExclusion, ChannelSummary, ResolvedAgent, RosterResolution,
SkippedSlug,
validate_ttl_seconds, validate_update_channel_fields, ArchivedExclusion, ChannelSummary,
ResolvedAgent, RosterResolution, SkippedSlug,
};
use crate::client::BuzzClient;
use crate::CliError;
@@ -1291,6 +1307,21 @@ mod tests {
assert!(validate_ttl_seconds(i32::MAX as i64 + 1).is_err());
}
#[test]
fn update_channel_fields_rejects_empty_update() {
let result = validate_update_channel_fields(None, None, None, None);
assert!(matches!(result, Err(CliError::Usage(_))));
let msg = result.unwrap_err().to_string();
assert!(msg.contains("at least one field required"));
assert!(msg.contains("--visibility"));
}
#[test]
fn update_channel_fields_accepts_visibility_only_update() {
let result = validate_update_channel_fields(None, None, Some("open"), None);
assert!(result.is_ok(), "visibility-only update should be accepted");
}
// --- BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES gate ---
fn check_allowed_channel_add_policy(allowed_raw: &str, policy: &str) -> Result<(), CliError> {
+7 -1
View File
@@ -596,7 +596,10 @@ pub enum ChannelsCmd {
#[arg(long, value_name = "PATH")]
templates_file: Option<String>,
},
/// Update channel name, description, or ephemeral TTL
/// Update channel name, description, visibility, or ephemeral TTL
#[command(
after_help = "Examples:\n buzz channels update --channel <uuid> --name general\n buzz channels update --channel <uuid> --visibility open\n buzz channels update --channel <uuid> --visibility private"
)]
Update {
/// Channel UUID
#[arg(long)]
@@ -607,6 +610,9 @@ pub enum ChannelsCmd {
/// New channel description
#[arg(long)]
description: Option<String>,
/// New channel visibility
#[arg(long, value_enum)]
visibility: Option<ChannelVisibility>,
/// Make the channel ephemeral (or change its lifetime): seconds until
/// the relay archives it after the last message. Conflicts with --no-ttl.
#[arg(long, value_name = "SECONDS", conflicts_with = "no_ttl")]
+37 -2
View File
@@ -77,9 +77,33 @@ impl<'de> Deserialize<'de> for StopReason {
}
}
/// Decrypted payload of a `kind:44200` Agent Turn Metric event.
/// Billing identity for a turn — present only when the publisher can prove
/// applicability from the actual endpoint and actually-requested model.
///
/// `harness` and `timestamp` are REQUIRED. All other fields are optional or
/// NIP-AM: this is OPTIONAL but NOT nullable. When present, `authority` and
/// `model` MUST be non-null strings; `cache_class` is omitted (not null) when
/// not applicable.
///
/// Consumers MUST treat omission as "price unknown" and MUST NOT infer a price
/// from the session `model` field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PricingIdentity {
/// Registered billing-namespace identifier: exact lowercase hostname, no
/// scheme, no path, no trailing slash. Registered values: `api.anthropic.com`,
/// `api.openai.com`, `openrouter.ai`. Set extends only by NIP amendment.
pub authority: String,
/// The billable model identifier as resolved at request time — the
/// actually-requested model, not the configured/session model alias.
pub model: String,
/// Cache-write class (e.g. `"ephemeral"`). Omitted when not applicable.
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_class: Option<String>,
}
/// Decrypted payload of a `kind:44200` Agent Turn Metric event.
/// nullable unless constrained by the NIP (e.g. `session_id` + `turn_seq`
/// are required whenever `cumulative` is present).
///
@@ -125,6 +149,14 @@ pub struct AgentTurnMetricPayload {
/// Why the turn ended. Unrecognized values MUST be treated as `Unknown`.
pub stop_reason: Option<StopReason>,
/// Billing identity, present only when the publisher can prove it from the
/// actual endpoint (official provider API) and the actually-requested model.
///
/// Omit (never null) when applicability cannot be proven. Consumers MUST
/// treat omission as "price unknown".
#[serde(skip_serializing_if = "Option::is_none")]
pub pricing_identity: Option<PricingIdentity>,
}
fn default_delta_reliable() -> bool {
@@ -223,6 +255,7 @@ mod tests {
}),
delta_reliable: true,
stop_reason: Some(StopReason::EndTurn),
pricing_identity: None,
}
}
@@ -368,6 +401,7 @@ mod tests {
cumulative: None,
delta_reliable: true,
stop_reason: None,
pricing_identity: None,
}
}
@@ -391,6 +425,7 @@ mod tests {
}),
delta_reliable: true,
stop_reason: None,
pricing_identity: None,
}
}
+8 -10
View File
@@ -371,9 +371,9 @@ async fn acquire_channel_membership_lock(
/// Role enforcement:
/// - Open channels: `invited_by` is optional; role is forced to `Member` regardless of
/// what the caller passes — callers cannot self-assign elevated roles.
/// - Private channels: requires an `invited_by` who is an active owner/admin, the channel
/// creator bootstrapping their own first membership, or the target adding themselves
/// (idempotent re-add — an active member's *role* still cannot change this way).
/// - Private channels: requires an `invited_by` who is an active member, or the channel
/// creator bootstrapping their own first membership. Any active member may add an
/// ordinary member, guest, or bot; only owners/admins may grant elevated roles.
/// - Elevated roles (`Owner`, `Admin`) may only be granted by an existing owner/admin,
/// even on open channels.
///
@@ -421,14 +421,12 @@ pub async fn add_member(
DbError::InvalidData(format!("invalid role in database: {inviter_role_str}"))
})?;
// Only owners/admins may extend private-channel access to another
// identity. `inviter == pubkey` keeps a member's own idempotent
// re-add working; it is not a role-escalation hole, because the
// active-role-change guard below still rejects a self-targeted
// promotion from any non-elevated caller.
if !inviter_role.is_elevated() && inviter != pubkey {
// Any active member may extend private-channel access with an
// ordinary role. Granting owner/admin remains reserved for an
// existing owner/admin.
if role.is_elevated() && !inviter_role.is_elevated() {
return Err(DbError::AccessDenied(
"only owners/admins may add private-channel members".to_string(),
"only owners/admins may grant elevated roles".to_string(),
));
}
}
+16 -17
View File
@@ -358,23 +358,22 @@ pub async fn validate_admin_event(
let target_pubkey =
extract_p_tag(event).ok_or_else(|| anyhow::anyhow!("missing p tag"))?;
// PUT_USER: open channels allow any authenticated user. Private
// channels only let owners/admins add another identity; otherwise
// any compromised member could extend access to channel history.
//
// A self-targeted add skips this check so an idempotent re-add
// still works. That is not a way into a private channel: ingest's
// `check_channel_membership` rejects a non-member (and a
// soft-removed member) before this validator runs, and `add_member`
// independently requires the self-inviter to hold an active role.
// Self-promotion is caught by the role-change guard below.
if channel.visibility == "private"
&& target_pubkey != actor_bytes
&& !actor_role.is_some_and(|r| r.is_elevated())
{
return Err(anyhow::anyhow!(
"only owners/admins may add private-channel members"
));
// PUT_USER: open channels allow any authenticated user; private channels
// require the actor to be an existing active member. Any active member may
// add an ordinary member, guest, or bot, but only owners/admins may grant
// an elevated role.
if channel.visibility == "private" {
if actor_role.is_none() {
return Err(anyhow::anyhow!("actor not authorized"));
}
if requested_role.is_some_and(|role| role.is_elevated())
&& !actor_role.is_some_and(|role| role.is_elevated())
{
return Err(anyhow::anyhow!(
"only owners/admins may grant elevated roles"
));
}
}
// Changing an ACTIVE existing member's role is privileged in both
+15 -1
View File
@@ -229,6 +229,14 @@ pub async fn search(pool: &PgPool, query: &SearchQuery) -> Result<SearchResult,
};
let page = query.page.clamp(1, PAGE_MAX);
let offset = ((page - 1) as i64) * (per_page_actual as i64);
// Profile typeahead uses broad prefix matching. For one- and two-character
// queries, a busy community can have enough newer prefix matches to push a
// short exact display name off the bounded first page. Keep the same result
// set and pagination contract, but put rows containing the whole lexeme
// first for this one narrow caller shape.
let prioritize_exact_profile_lexeme = query.mode == SearchMode::Prefix
&& query.kinds.as_deref() == Some(&[0][..])
&& search_text.chars().count() <= 2;
let mut qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new(
"SELECT id, kind, pubkey, channel_id, \
@@ -292,7 +300,13 @@ pub async fn search(pool: &PgPool, query: &SearchQuery) -> Result<SearchResult,
qb.push(")");
}
qb.push(" ORDER BY rank DESC, created_at DESC, id LIMIT ");
if prioritize_exact_profile_lexeme {
qb.push(" ORDER BY search_tsv @@ websearch_to_tsquery('simple', ");
qb.push_bind(&search_text);
qb.push(") DESC, rank DESC, created_at DESC, id LIMIT ");
} else {
qb.push(" ORDER BY rank DESC, created_at DESC, id LIMIT ");
}
qb.push_bind(per_page_actual as i64);
qb.push(" OFFSET ");
qb.push_bind(offset);
@@ -303,6 +303,67 @@ async fn kind0_search_by_display_name_works_without_flattening() {
teardown(pool, &schema).await;
}
#[tokio::test]
#[ignore = "requires Postgres"]
async fn short_kind0_prefix_prioritizes_exact_lexeme_on_a_noisy_page() {
let (pool, schema) = setup().await;
let c = mk_community(&pool, "short-profile-prefix.example").await;
let exact_id = rand_bytes32();
insert_event(
&pool,
c,
exact_id,
rand_bytes32(),
0,
r#"{"display_name":"jm"}"#,
None,
1_700_000_000,
)
.await;
// These profiles all match jm:* and are newer than the exact name. Without
// exact-lexeme priority they consume the entire bounded first page.
for (i, display_name) in ["jma", "jmbravo", "jmcharlie", "jmdelta"]
.iter()
.enumerate()
{
insert_event(
&pool,
c,
rand_bytes32(),
rand_bytes32(),
0,
&format!(r#"{{"display_name":"{display_name}"}}"#),
None,
1_700_000_100 + i as i64,
)
.await;
}
let svc = SearchService::new(pool.clone());
let first_page = svc
.search(&SearchQuery {
community: c,
q: "jm".into(),
channel_scope: ChannelScope::Any,
kinds: Some(vec![0]),
authors: None,
since: None,
until: None,
page: 1,
per_page: 3,
mode: buzz_search::SearchMode::Prefix,
})
.await
.expect("short profile prefix search ok");
assert_eq!(first_page.hits.len(), 3);
assert_eq!(first_page.hits[0].event_id, exact_id);
teardown(pool, &schema).await;
}
#[tokio::test]
#[ignore = "requires Postgres"]
async fn prefix_mode_matches_final_token_prefix_without_changing_full_text() {
+66 -47
View File
@@ -2249,14 +2249,17 @@ async fn add_member_with_role_ws(
(ok.accepted, ok.message)
}
/// Only owners/admins can add another identity to a private channel.
/// Any active member can add any ordinary role to a private channel.
#[tokio::test]
#[ignore]
async fn test_private_channel_member_cannot_invite() {
async fn test_private_channel_any_member_can_invite() {
let url = relay_url();
let owner_keys = Keys::generate();
let member_keys = Keys::generate();
let invitee_keys = Keys::generate();
let actors = [
("member", Keys::generate()),
("guest", Keys::generate()),
("bot", Keys::generate()),
];
// Connect as owner and create a private channel.
let mut owner_client = BuzzTestClient::connect(&url, &owner_keys)
@@ -2264,54 +2267,70 @@ async fn test_private_channel_member_cannot_invite() {
.expect("connect as owner");
let channel_id = create_private_channel_ws(&mut owner_client, &owner_keys).await;
// Owner adds member_keys as a regular member.
let (accepted, msg) = add_member_ws(
&mut owner_client,
&channel_id,
&member_keys.public_key().to_hex(),
&owner_keys,
)
.await;
assert!(accepted, "owner should add member, got: {msg}");
// Seed one actor for each ordinary active role.
for (role, keys) in &actors {
let (accepted, msg) = add_member_with_role_ws(
&mut owner_client,
&channel_id,
&keys.public_key().to_hex(),
role,
&owner_keys,
)
.await;
assert!(accepted, "owner should add {role} actor, got: {msg}");
}
// Connect as the regular member.
let mut member_client = BuzzTestClient::connect(&url, &member_keys)
.await
.expect("connect as member");
// Exercise the full ordinary-role target matrix. Relay and DB authorization
// both run here, unlike the Desktop/mobile policy-unit-test mirrors.
for (actor_role, actor_keys) in &actors {
let mut actor_client = BuzzTestClient::connect(&url, actor_keys)
.await
.unwrap_or_else(|err| panic!("connect as {actor_role}: {err}"));
// Regular member tries to invite a third user.
let (accepted, msg) = add_member_ws(
&mut member_client,
&channel_id,
&invitee_keys.public_key().to_hex(),
&member_keys,
)
.await;
assert!(
!accepted,
"regular member must not add another private-channel identity: {msg}"
);
assert!(
msg.contains("owners/admins"),
"rejection should name the owner/admin requirement, got: {msg}"
);
for target_role in ["member", "guest", "bot"] {
let target_keys = Keys::generate();
let target_pubkey_hex = target_keys.public_key().to_hex();
let (accepted, msg) = add_member_with_role_ws(
&mut actor_client,
&channel_id,
&target_pubkey_hex,
target_role,
actor_keys,
)
.await;
assert!(
accepted,
"private-channel {actor_role} should add {target_role}, got: {msg}"
);
assert_eq!(
member_role(&url, &owner_keys, &channel_id, &target_pubkey_hex).await,
Some(target_role.to_string()),
"private-channel {actor_role} add must persist the {target_role} role"
);
}
// The same member re-adding *themselves* stays idempotent — the huddle
// bot-add and kind:9021 paths depend on a self-targeted PUT_USER working.
let (accepted, msg) = add_member_ws(
&mut member_client,
&channel_id,
&member_keys.public_key().to_hex(),
&member_keys,
)
.await;
assert!(
accepted,
"self-targeted re-add must stay idempotent, got: {msg}"
);
// Re-adding oneself stays idempotent — the huddle bot-add and kind:9021
// paths depend on a self-targeted PUT_USER working.
let (accepted, msg) = add_member_with_role_ws(
&mut actor_client,
&channel_id,
&actor_keys.public_key().to_hex(),
actor_role,
actor_keys,
)
.await;
assert!(
accepted,
"self-targeted {actor_role} re-add must stay idempotent, got: {msg}"
);
actor_client
.disconnect()
.await
.unwrap_or_else(|err| panic!("disconnect {actor_role}: {err}"));
}
owner_client.disconnect().await.expect("disconnect owner");
member_client.disconnect().await.expect("disconnect member");
}
/// An admin — not just the owner — can still add to a private channel.
+4
View File
@@ -15,6 +15,10 @@ ignore = [
# remove these when upstream catches up.
{ id = "RUSTSEC-2026-0194", reason = "transitive via rust-s3 and mesh-llm→plist; trusted-input XML only; no upstream fix available yet" },
{ id = "RUSTSEC-2026-0195", reason = "transitive via rust-s3 and mesh-llm→plist; trusted-input XML only; no upstream fix available yet" },
# nostr-relay-pool 0.44.3 — informational/unmaintained, not a vulnerability.
# Transitive via mesh-llm 0.74 → nostr-sdk 0.44.1. Remove after mesh-llm
# migrates to nostr-sdk >= 0.45, which absorbed the standalone relay pool.
{ id = "RUSTSEC-2026-0243", reason = "transitive via mesh-llm; upstream nostr-sdk 0.45 migration requires API changes" },
]
[licenses]
+3 -2
View File
@@ -226,9 +226,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.103"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "approx"
@@ -1034,6 +1034,7 @@ dependencies = [
"tokio",
"tracing",
"tracing-subscriber",
"url",
"urlencoding",
"webbrowser",
]
@@ -0,0 +1,886 @@
//! Pure NIP-AM usage accounting: request validation, wire types, and the
//! per-field cumulative/direct-fallback ladder.
//!
//! No Tauri or filesystem dependency — every function here takes already
//! loaded rows (or plain values) and returns plain values. The caller
//! (`mod.rs`'s `get_agent_usage_series` command, Phase 2) owns SQLite access
//! (`metric_store.rs`) and glues the two together: load window rows, compute
//! [`window_probe_keys`], load those exact-key rows, then call
//! [`compute_series`].
//!
//! Accounting contract (Rev 3, frozen plan + amendments A1/A2/A4/A9/A11–A13):
//! see `docs/nips/NIP-AM.md:119-160` for the NIP itself.
use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use super::metric_store::AgentMetricIndexRow;
// ── Request ──────────────────────────────────────────────────────────────────
/// Request for [`compute_series`]'s caller. `bucket_boundaries` are exact
/// local-midnight Unix-second boundaries built by the frontend (inclusive
/// start / exclusive end per adjacent pair): N entries = N - 1 buckets, so
/// 2 entries = 1 bucket (`1d`), 8 = 7 buckets, 31 = 30 buckets, and an
/// arbitrary custom range falls anywhere in between up to
/// [`MAX_BOUNDARIES`].
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentUsageSeriesRequest {
pub bucket_boundaries: Vec<i64>,
pub agent_pubkey: Option<String>,
}
/// Widest interval NIP-AM query validation admits (A9): wide enough to admit
/// every real civil-day transition (ordinary DST, 30-minute-offset zones,
/// historical calendar skips) while still rejecting arbitrary bins.
const MAX_INTERVAL_SECS: i64 = 48 * 3600;
/// Largest boundary count a request may carry: 367 boundaries = 366 daily
/// buckets = one leap year, the product ceiling on the custom date-range
/// picker. Bounds the SQLite window scan and keeps the rendered bar chart
/// legible; the frontend clamps the picker to the same span so a user never
/// reaches this check, which stays as fail-closed defense in depth.
const MAX_BOUNDARIES: usize = 367;
/// Smallest boundary count that describes a real window: 2 boundaries =
/// 1 bucket, the `1d` case.
const MIN_BOUNDARIES: usize = 2;
/// Validate a request per the frozen contract + A9 (drops the 23–25h band
/// for a `> 0 && <= 48h` sanity band) and A13 pubkey normalization.
///
/// Fails closed before any SQLite work. Returns the normalized (lowercased)
/// agent pubkey, if one was supplied.
pub(super) fn validate_request(req: &AgentUsageSeriesRequest) -> Result<Option<String>, String> {
let n = req.bucket_boundaries.len();
if !(MIN_BOUNDARIES..=MAX_BOUNDARIES).contains(&n) {
return Err(format!(
"bucket_boundaries must have between {MIN_BOUNDARIES} and {MAX_BOUNDARIES} entries, got {n}"
));
}
for i in 0..n - 1 {
let (a, b) = (req.bucket_boundaries[i], req.bucket_boundaries[i + 1]);
if b <= a {
return Err(format!(
"bucket_boundaries must be strictly increasing (index {i}: {a} >= {b})"
));
}
let interval = b - a;
if interval > MAX_INTERVAL_SECS {
return Err(format!(
"bucket_boundaries interval at index {i} is {interval}s, exceeds {MAX_INTERVAL_SECS}s"
));
}
}
// Finite Unix-second bounds: must be representable as an RFC 3339
// instant (chrono's timestamp range), independent of local timezone.
for &t in &req.bucket_boundaries {
if chrono::DateTime::from_timestamp(t, 0).is_none() {
return Err(format!("bucket boundary {t} is out of representable range"));
}
}
let normalized_pubkey = match &req.agent_pubkey {
None => None,
Some(pk) => {
if pk.len() != 64 || !pk.chars().all(|c| c.is_ascii_hexdigit()) {
return Err("agent_pubkey must be exactly 64 hex characters".to_string());
}
Some(pk.to_lowercase())
}
};
Ok(normalized_pubkey)
}
// ── Wire types ───────────────────────────────────────────────────────────────
/// Per-field completeness (A2): `value: null` means no known increment in
/// scope; `incomplete: true` on a non-null value means the value is a
/// reported lower bound, not full coverage.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UsageField {
pub value: Option<String>,
pub incomplete: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CostField {
pub value: Option<f64>,
pub incomplete: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ReportedUsage {
pub input_tokens: UsageField,
pub output_tokens: UsageField,
pub total_tokens: UsageField,
pub estimated_cost_usd: CostField,
/// Cache-read (served) token count. `None` value + `incomplete: false`
/// means no events in this scope reported the field; in practice this
/// only happens for empty scopes. A non-empty scope where ALL events had
/// absent `turn_cache_read_tokens` (old harness) produces `incomplete: true`
/// (unknown, not zero).
pub cache_read_tokens: UsageField,
/// Cache-write (creation) token count. Same absence semantics as
/// `cache_read_tokens`.
pub cache_write_tokens: UsageField,
/// Input tokens minus cache-served and cache-write subsets. Computed only
/// when all three inputs are complete and the arithmetic succeeds
/// (`cacheRead + cacheWrite ≤ input`). Otherwise `incomplete: true`.
pub fresh_input_tokens: UsageField,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SeriesBucket {
pub start: i64,
pub end: i64,
pub usage: ReportedUsage,
pub report_count: i64,
pub has_unknown_usage: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelUsage {
pub harness: Option<String>,
pub model: Option<String>,
pub usage: ReportedUsage,
pub report_count: i64,
pub has_unknown_usage: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentUsage {
pub agent_pubkey: String,
pub usage: ReportedUsage,
pub buckets: Vec<SeriesBucket>,
pub models: Vec<ModelUsage>,
pub report_count: i64,
pub has_unknown_usage: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Coverage {
pub first_archived_at: Option<i64>,
pub last_archived_at: Option<i64>,
pub first_reported_at: Option<i64>,
pub last_reported_at: Option<i64>,
pub report_count: i64,
pub invalid_report_count: i64,
pub has_unknown_usage: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentUsageSeries {
pub collection_enabled: bool,
pub buckets: Vec<SeriesBucket>,
pub agents: Vec<AgentUsage>,
pub coverage: Coverage,
/// A13: `null` when the request had no `agentPubkey` filter; otherwise
/// `true` iff at least one surviving `agent_metric_index` row (either
/// `parse_status`) exists for that author, with no bucket-boundary
/// restriction. Callers compute this (DB access) and pass it through.
pub has_archived_evidence: Option<bool>,
}
// ── Per-event field ladder (A1, A4, A11, A12) ───────────────────────────────
#[derive(Debug, Clone, Copy)]
enum FieldValue<T> {
Known(T),
Unknown,
}
struct EventOutcome {
input: FieldValue<u64>,
output: FieldValue<u64>,
total: FieldValue<u64>,
cost: FieldValue<f64>,
cache_read: FieldValue<u64>,
cache_write: FieldValue<u64>,
}
/// Per-field ladder for token counters (A1): adjacent nondecreasing cumulative
/// pair → diff; adjacent decreasing pair → unknown, terminal, no fallback;
/// no usable baseline for this field → `deltaReliable` direct value or
/// unknown.
fn ladder_token(
baseline_cumulative: Option<u64>,
current_cumulative: Option<u64>,
current_turn: Option<u64>,
delta_reliable: bool,
) -> FieldValue<u64> {
if let (Some(prev), Some(cur)) = (baseline_cumulative, current_cumulative) {
return if cur >= prev {
FieldValue::Known(cur - prev)
} else {
FieldValue::Unknown
};
}
if delta_reliable {
if let Some(v) = current_turn {
return FieldValue::Known(v);
}
}
FieldValue::Unknown
}
/// Same ladder for `costUsd` (f64).
fn ladder_cost(
baseline_cumulative: Option<f64>,
current_cumulative: Option<f64>,
current_turn: Option<f64>,
delta_reliable: bool,
) -> FieldValue<f64> {
if let (Some(prev), Some(cur)) = (baseline_cumulative, current_cumulative) {
return if cur >= prev {
FieldValue::Known(cur - prev)
} else {
FieldValue::Unknown
};
}
if delta_reliable {
if let Some(v) = current_turn {
return FieldValue::Known(v);
}
}
FieldValue::Unknown
}
/// Resolve the exact-`S-1` baseline row for `row`, or `None` if no usable
/// baseline exists (A11/A12): missing session/sequence key, duplicate row at
/// `row`'s own sequence (A4 — no cumulative delta for any row at a
/// duplicated sequence), sequence `0` (no predecessor, `checked_sub`
/// underflow), no predecessor row (gap), or duplicate rows at the
/// predecessor sequence (ambiguous baseline).
///
/// `probe_by_key` must contain, for every key queried, ALL valid rows at
/// that exact `(agent, session, turnSeq)` — used for both this baseline
/// lookup and the A4/A11 duplicate-cardinality check, which is why absence
/// of a key from the map is treated identically to an empty group.
fn resolve_baseline<'a>(
row: &AgentMetricIndexRow,
probe_by_key: &HashMap<(String, String, u64), Vec<&'a AgentMetricIndexRow>>,
) -> Option<&'a AgentMetricIndexRow> {
let (agent, session, seq) = row.accounting_key()?;
let own_group = probe_by_key.get(&(agent.clone(), session.clone(), seq))?;
if own_group.len() > 1 {
return None; // A4: duplicate at own sequence — no cumulative delta.
}
let pred_seq = seq.checked_sub(1)?; // A12: seq == 0 has no baseline.
let pred_group = probe_by_key.get(&(agent, session, pred_seq))?;
if pred_group.len() != 1 {
return None; // Missing (gap) or ambiguous (duplicate) predecessor.
}
Some(pred_group[0])
}
fn compute_event_outcome(
row: &AgentMetricIndexRow,
probe_by_key: &HashMap<(String, String, u64), Vec<&AgentMetricIndexRow>>,
) -> EventOutcome {
let baseline = resolve_baseline(row, probe_by_key);
let delta_reliable = row.delta_reliable.unwrap_or(false);
EventOutcome {
input: ladder_token(
baseline.and_then(|b| b.cumulative_input_tokens),
row.cumulative_input_tokens,
row.turn_input_tokens,
delta_reliable,
),
output: ladder_token(
baseline.and_then(|b| b.cumulative_output_tokens),
row.cumulative_output_tokens,
row.turn_output_tokens,
delta_reliable,
),
total: ladder_token(
baseline.and_then(|b| b.cumulative_total_tokens),
row.cumulative_total_tokens,
row.turn_total_tokens,
delta_reliable,
),
cost: ladder_cost(
baseline.and_then(|b| b.cumulative_cost_usd),
row.cumulative_cost_usd,
row.turn_cost_usd,
delta_reliable,
),
cache_read: ladder_token(
baseline.and_then(|b| b.cumulative_cache_read_tokens),
row.cumulative_cache_read_tokens,
row.turn_cache_read_tokens,
delta_reliable,
),
cache_write: ladder_token(
baseline.and_then(|b| b.cumulative_cache_write_tokens),
row.cumulative_cache_write_tokens,
row.turn_cache_write_tokens,
delta_reliable,
),
}
}
/// The exact `(agent, session, turnSeq)` keys the caller must load via
/// `metric_store::load_rows_at_exact_keys` before calling [`compute_series`]
/// (A11): each in-window row's own key, plus its checked predecessor key
/// (`turnSeq - 1`) when one exists. Pure and DB-free so it is unit-testable
/// without SQLite.
pub(super) fn window_probe_keys(
window_rows: &[AgentMetricIndexRow],
) -> HashSet<(String, String, u64)> {
let mut keys = HashSet::new();
for row in window_rows {
if let Some((agent, session, seq)) = row.accounting_key() {
if let Some(pred) = seq.checked_sub(1) {
keys.insert((agent.clone(), session.clone(), pred));
}
keys.insert((agent, session, seq));
}
}
keys
}
// ── Accumulators ─────────────────────────────────────────────────────────────
/// Sums known per-event increments with `checked_add`; an event with an
/// unknown value, or an overflow, marks the scope `incomplete` (A2) without
/// ever wrapping (overflow freezes the sum at its last valid value).
#[derive(Debug, Default, Clone)]
struct TokenAccumulator {
value: Option<u64>,
incomplete: bool,
overflowed: bool,
}
impl TokenAccumulator {
fn add(&mut self, v: FieldValue<u64>) {
match v {
FieldValue::Unknown => self.incomplete = true,
FieldValue::Known(x) => {
if self.overflowed {
self.incomplete = true;
return;
}
self.value = Some(match self.value {
None => x,
Some(cur) => match cur.checked_add(x) {
Some(sum) => sum,
None => {
self.overflowed = true;
self.incomplete = true;
cur
}
},
});
}
}
}
fn has_unknown(&self) -> bool {
self.incomplete
}
fn finish(self) -> UsageField {
UsageField {
value: self.value.map(|v| v.to_string()),
incomplete: self.incomplete,
}
}
}
/// Same contract as [`TokenAccumulator`] for `f64` costs: "checked finite
/// addition" means a sum that would become non-finite is rejected and the
/// scope freezes at its last valid value, flagged incomplete.
#[derive(Debug, Default, Clone)]
struct CostAccumulator {
value: Option<f64>,
incomplete: bool,
overflowed: bool,
}
impl CostAccumulator {
fn add(&mut self, v: FieldValue<f64>) {
match v {
FieldValue::Unknown => self.incomplete = true,
FieldValue::Known(x) => {
if self.overflowed {
self.incomplete = true;
return;
}
let candidate = match self.value {
None => x,
Some(cur) => cur + x,
};
if candidate.is_finite() {
self.value = Some(candidate);
} else {
self.overflowed = true;
self.incomplete = true;
}
}
}
}
fn has_unknown(&self) -> bool {
self.incomplete
}
fn finish(self) -> CostField {
CostField {
value: self.value,
incomplete: self.incomplete,
}
}
}
#[derive(Debug, Default, Clone)]
struct UsageAccumulator {
input: TokenAccumulator,
output: TokenAccumulator,
total: TokenAccumulator,
cost: CostAccumulator,
cache_read: TokenAccumulator,
cache_write: TokenAccumulator,
}
impl UsageAccumulator {
fn add(&mut self, outcome: &EventOutcome) {
self.input.add(outcome.input);
self.output.add(outcome.output);
self.total.add(outcome.total);
self.cost.add(outcome.cost);
self.cache_read.add(outcome.cache_read);
self.cache_write.add(outcome.cache_write);
}
fn has_unknown(&self) -> bool {
self.input.has_unknown()
|| self.output.has_unknown()
|| self.total.has_unknown()
|| self.cost.has_unknown()
|| self.cache_read.has_unknown()
|| self.cache_write.has_unknown()
|| self.fresh_input_incomplete()
}
/// Returns true when `derive_fresh_input()` would produce `incomplete: true`.
///
/// Mirrors the fail-closed conditions of `derive_fresh_input()` without
/// consuming `self`, so `has_unknown()` can be called before `finish()` at
/// all six call sites. The two functions must stay in sync.
fn fresh_input_incomplete(&self) -> bool {
// Input unknown or incomplete → fresh_input incomplete.
if self.input.incomplete {
return true;
}
// No input events at all (value = None, not incomplete) → no fresh_input
// to derive; not a failure, not incomplete.
let Some(input) = self.input.value else {
return false;
};
// Any cache accumulator incomplete → fresh_input incomplete.
if self.cache_read.incomplete || self.cache_write.incomplete {
return true;
}
let cache_read = self.cache_read.value.unwrap_or(0);
let cache_write = self.cache_write.value.unwrap_or(0);
// Subset sum overflow → incomplete.
let Some(subset_sum) = cache_read.checked_add(cache_write) else {
return true;
};
// Subsets exceed input → incomplete.
input < subset_sum
}
/// D6 sort value: provider `totalTokens` when known, else `input+output`
/// when BOTH are known (no overflow), else `None` (unknown-last). This is
/// the sole sort key; provenance (whether total came from a single field vs
/// two) is not distinguished — both are "known" and rank before unknown.
fn sort_value(&self) -> Option<u64> {
// Prefer the reported total if present and exact.
if let Some(t) = self.total.value {
if !self.total.incomplete {
return Some(t);
}
}
// Fall back to input+output when both are complete and neither
// overflowed. An incomplete accumulator may still carry a partial
// value; we must not use it (treat as unknown).
match (self.input.value, self.output.value) {
(Some(i), Some(o)) if !self.input.incomplete && !self.output.incomplete => {
i.checked_add(o)
}
_ => None,
}
}
/// Derive `freshInputTokens` from accumulated input, cache-read, and
/// cache-write: checked arithmetic — fail-closed on any unknown input or
/// cache delta, overflow, or subsets-exceed-input condition.
///
/// Cache-read and cache-write follow the same Unknown semantics as the
/// main token ladder: a turn or cumulative value that was absent (not
/// reported by the harness) produces `FieldValue::Unknown` in
/// `compute_event_outcome`, which poisons the accumulator as `incomplete`.
/// An absent cache field is therefore "unknown, not zero," and always
/// produces `incomplete: true` here.
///
/// The only case where the cache accumulator can be `{ value: None,
/// incomplete: false }` without any events is an empty scope, which
/// cannot occur in practice (scopes are created on first event).
///
/// Conditions that produce `incomplete: true`:
/// - input is unknown (None value or incomplete = true)
/// - any cache category's accumulator is incomplete
/// - checked_sub(input - (cacheRead + cacheWrite)) would underflow
/// (cacheRead + cacheWrite > input)
/// - overflow in cacheRead + cacheWrite sum
fn derive_fresh_input(&self) -> UsageField {
// If input is unknown or incomplete, fresh_input is incomplete too.
if self.input.incomplete {
return UsageField {
value: None,
incomplete: true,
};
}
let input = match self.input.value {
Some(v) => v,
None => {
// No input events observed — no fresh_input to compute.
return UsageField {
value: None,
incomplete: false,
};
}
};
// Cache-read and cache-write: absent means zero (no provider reported it),
// but incomplete (unknown delta) means fail-closed.
if self.cache_read.incomplete || self.cache_write.incomplete {
return UsageField {
value: None,
incomplete: true,
};
}
let cache_read = self.cache_read.value.unwrap_or(0);
let cache_write = self.cache_write.value.unwrap_or(0);
// Sum of subsets must not overflow and must not exceed input.
let subset_sum = match cache_read.checked_add(cache_write) {
Some(s) => s,
None => {
return UsageField {
value: None,
incomplete: true,
};
}
};
match input.checked_sub(subset_sum) {
Some(fresh) => UsageField {
value: Some(fresh.to_string()),
incomplete: false,
},
None => UsageField {
value: None,
incomplete: true,
},
}
}
fn finish(self) -> ReportedUsage {
let fresh_input = self.derive_fresh_input();
ReportedUsage {
input_tokens: self.input.finish(),
output_tokens: self.output.finish(),
total_tokens: self.total.finish(),
estimated_cost_usd: self.cost.finish(),
cache_read_tokens: self.cache_read.finish(),
cache_write_tokens: self.cache_write.finish(),
fresh_input_tokens: fresh_input,
}
}
}
// ── Bucket assignment ────────────────────────────────────────────────────────
/// Which `[boundaries[i], boundaries[i+1])` bucket `t` falls in, or `None`
/// if outside every bucket (defensive; callers scope their row query to
/// `[boundaries[0], boundaries[last])` so this should never miss).
fn assign_bucket_index(boundaries: &[i64], t: i64) -> Option<usize> {
(0..boundaries.len().saturating_sub(1)).find(|&i| t >= boundaries[i] && t < boundaries[i + 1])
}
// ── Sort helpers ─────────────────────────────────────────────────────────────
/// D6 sort comparator: known values descending, unknown (`None`) last; equal
/// within unknowns (callers chain further tiebreaks). Value is derived by
/// [`UsageAccumulator::sort_value`]: provider total when available, else
/// `input+output`, else `None`.
fn cmp_sort_value(a: Option<u64>, b: Option<u64>) -> std::cmp::Ordering {
match (a, b) {
(Some(av), Some(bv)) => bv.cmp(&av), // descending
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
}
/// Ordinal (byte-order) comparison of two optional strings, with `None`
/// sorting after any `Some` value — used as a stable tiebreak for harness and
/// model names so ordering is locale-independent and matches the Rust backend.
fn cmp_option_str_none_last(a: &Option<String>, b: &Option<String>) -> std::cmp::Ordering {
match (a, b) {
(Some(av), Some(bv)) => av.cmp(bv),
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
}
// ── compute_series ───────────────────────────────────────────────────────────
/// Per-agent accumulation scope, built while walking `window_rows` once.
struct AgentScope {
buckets: Vec<UsageAccumulator>,
bucket_counts: Vec<i64>,
total: UsageAccumulator,
report_count: i64,
/// Keyed by `(harness, model)` — same model via two harnesses → two rows.
models: HashMap<(Option<String>, Option<String>), (UsageAccumulator, i64)>,
}
/// Compute the full [`AgentUsageSeries`] from already-loaded rows.
///
/// - `window_rows`: valid rows with `reported_at` in `[boundaries[0],
/// boundaries[last])` (optionally pre-filtered to one agent), from
/// `metric_store::load_window_valid_rows`.
/// - `probe_rows`: valid rows at the exact keys from [`window_probe_keys`],
/// from `metric_store::load_rows_at_exact_keys` — used for baseline
/// resolution and A4/A11 duplicate-cardinality checks, unrestricted by the
/// window.
/// - `invalid_report_count`: from `metric_store::count_invalid_rows_in_window`.
/// - `has_archived_evidence`: from `metric_store::has_archived_evidence`,
/// already resolved to `None` when the request has no `agentPubkey` filter
/// (A13) — this function does not decide that; it only carries the value.
pub(super) fn compute_series(
window_rows: &[AgentMetricIndexRow],
probe_rows: &[AgentMetricIndexRow],
invalid_report_count: i64,
boundaries: &[i64],
has_archived_evidence: Option<bool>,
collection_enabled: bool,
) -> AgentUsageSeries {
let bucket_count = boundaries.len().saturating_sub(1);
let mut probe_by_key: HashMap<(String, String, u64), Vec<&AgentMetricIndexRow>> =
HashMap::new();
for r in probe_rows {
if let Some(key) = r.accounting_key() {
probe_by_key.entry(key).or_default().push(r);
}
}
let mut overall_buckets: Vec<UsageAccumulator> = (0..bucket_count)
.map(|_| UsageAccumulator::default())
.collect();
let mut overall_bucket_counts: Vec<i64> = vec![0; bucket_count];
let mut agents: HashMap<String, AgentScope> = HashMap::new();
let mut first_reported_at: Option<i64> = None;
let mut last_reported_at: Option<i64> = None;
let mut first_archived_at: Option<i64> = None;
let mut last_archived_at: Option<i64> = None;
for row in window_rows {
// Defensive: the loader already scopes to reported_at in-window and
// parse_status = 'valid'; a miss here means the caller passed rows
// it should not have, so skip rather than panic or miscount.
let Some(reported_at) = row.reported_at else {
continue;
};
let Some(bucket_idx) = assign_bucket_index(boundaries, reported_at) else {
continue;
};
first_reported_at = Some(first_reported_at.map_or(reported_at, |v| v.min(reported_at)));
last_reported_at = Some(last_reported_at.map_or(reported_at, |v| v.max(reported_at)));
first_archived_at =
Some(first_archived_at.map_or(row.archived_at, |v| v.min(row.archived_at)));
last_archived_at =
Some(last_archived_at.map_or(row.archived_at, |v| v.max(row.archived_at)));
let outcome = compute_event_outcome(row, &probe_by_key);
overall_buckets[bucket_idx].add(&outcome);
overall_bucket_counts[bucket_idx] += 1;
let scope = agents
.entry(row.agent_pubkey.clone())
.or_insert_with(|| AgentScope {
buckets: (0..bucket_count)
.map(|_| UsageAccumulator::default())
.collect(),
bucket_counts: vec![0; bucket_count],
total: UsageAccumulator::default(),
report_count: 0,
models: HashMap::new(),
});
scope.buckets[bucket_idx].add(&outcome);
scope.bucket_counts[bucket_idx] += 1;
scope.total.add(&outcome);
scope.report_count += 1;
let model_entry = scope
.models
.entry((row.harness.clone(), row.model.clone()))
.or_insert_with(|| (UsageAccumulator::default(), 0i64));
model_entry.0.add(&outcome);
model_entry.1 += 1;
}
let overall_report_count: i64 = overall_bucket_counts.iter().sum();
let buckets: Vec<SeriesBucket> = overall_buckets
.into_iter()
.zip(overall_bucket_counts)
.enumerate()
.map(|(i, (acc, count))| {
let has_unknown_usage = acc.has_unknown();
SeriesBucket {
start: boundaries[i],
end: boundaries[i + 1],
usage: acc.finish(),
report_count: count,
has_unknown_usage,
}
})
.collect();
let any_overall_bucket_unknown = buckets.iter().any(|b| b.has_unknown_usage);
// Build agent rows, then apply the D6 ranking rule: known sort value
// (provider total, else input+output) descending, unknown-value agents
// after, pubkey as the final tiebreak for determinism.
let mut agent_rows: Vec<(Option<u64>, String, AgentUsage)> = agents
.into_iter()
.map(|(agent_pubkey, scope)| {
let sort_value = scope.total.sort_value();
let has_unknown_usage = scope.total.has_unknown();
let buckets: Vec<SeriesBucket> = scope
.buckets
.into_iter()
.zip(scope.bucket_counts)
.enumerate()
.map(|(i, (acc, count))| {
let has_unknown_usage = acc.has_unknown();
SeriesBucket {
start: boundaries[i],
end: boundaries[i + 1],
usage: acc.finish(),
report_count: count,
has_unknown_usage,
}
})
.collect();
// Named sort key so the 4-tuple doesn't exceed clippy's type_complexity
// threshold and the ordering intent reads at a glance.
struct ModelSortKey {
sort_val: Option<u64>,
harness: Option<String>,
model: Option<String>,
usage: ModelUsage,
}
let mut model_rows: Vec<ModelSortKey> = scope
.models
.into_iter()
.map(|((harness, model), (acc, count))| {
let sort_val = acc.sort_value();
let has_unknown_usage = acc.has_unknown();
ModelSortKey {
sort_val,
harness: harness.clone(),
model: model.clone(),
usage: ModelUsage {
harness,
model,
usage: acc.finish(),
report_count: count,
has_unknown_usage,
},
}
})
.collect();
// D6 ranking: known sort value (provider total, else input+output)
// descending, unknown-value rows after; tiebreak: harness ascending
// (None last), then model ascending (None last), for determinism.
model_rows.sort_by(|a, b| {
cmp_sort_value(a.sort_val, b.sort_val)
.then_with(|| cmp_option_str_none_last(&a.harness, &b.harness))
.then_with(|| cmp_option_str_none_last(&a.model, &b.model))
});
let models = model_rows.into_iter().map(|k| k.usage).collect();
(
sort_value,
agent_pubkey.clone(),
AgentUsage {
agent_pubkey,
usage: scope.total.finish(),
buckets,
models,
report_count: scope.report_count,
has_unknown_usage,
},
)
})
.collect();
agent_rows.sort_by(|a, b| cmp_sort_value(a.0, b.0).then_with(|| a.1.cmp(&b.1)));
let any_agent_unknown = agent_rows.iter().any(|(_, _, a)| a.has_unknown_usage);
let agents: Vec<AgentUsage> = agent_rows.into_iter().map(|(_, _, a)| a).collect();
AgentUsageSeries {
collection_enabled,
buckets,
agents,
coverage: Coverage {
first_archived_at,
last_archived_at,
first_reported_at,
last_reported_at,
report_count: overall_report_count,
invalid_report_count,
has_unknown_usage: any_overall_bucket_unknown
|| any_agent_unknown
|| invalid_report_count > 0,
},
has_archived_evidence,
}
}
// ── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
#[path = "agent_usage_tests.rs"]
mod agent_usage_tests;
#[cfg(test)]
#[path = "agent_usage_p4a_tests.rs"]
mod agent_usage_p4a_tests;
@@ -0,0 +1,512 @@
//! Tests for P4a additions: cache-read/write ladder (§S-1 extension),
//! freshInputTokens derivation, and D6 comparator. Split from
//! `agent_usage_tests.rs` to keep both files under the 1 000-line ratchet.
use super::*;
use crate::archive::metric_store::{AgentMetricIndexRow, ParseStatus};
// ── Row builder ───────────────────────────────────────────────────────────────
/// Minimal valid row for P4a test scenarios.
fn row(id: &str, agent: &str, session: &str, seq: u64, reported_at: i64) -> AgentMetricIndexRow {
AgentMetricIndexRow {
id: id.to_string(),
agent_pubkey: agent.to_string(),
event_created_at: reported_at,
archived_at: reported_at,
reported_at: Some(reported_at),
session_id: Some(session.to_string()),
turn_seq: Some(seq),
harness: None,
model: None,
delta_reliable: Some(true),
turn_input_tokens: None,
turn_output_tokens: None,
turn_total_tokens: None,
turn_cost_usd: None,
turn_cache_read_tokens: None,
cumulative_input_tokens: None,
cumulative_output_tokens: None,
cumulative_total_tokens: None,
cumulative_cost_usd: None,
cumulative_cache_read_tokens: None,
cumulative_cache_write_tokens: None,
turn_cache_write_tokens: None,
pricing_authority: None,
pricing_model: None,
pricing_cache_class: None,
parse_status: ParseStatus::Valid,
}
}
/// Standard 8-boundary window covering one day, seconds since epoch.
const DAY: i64 = 86_400;
fn boundaries_7() -> Vec<i64> {
(0..=7).map(|i| i * DAY).collect()
}
// ── Cache-read / cache-write ladder (P4a.1) ──────────────────────────────────
#[test]
fn cache_read_ladder_uses_direct_turn_value_when_no_baseline() {
// A single event with only turn_cache_read_tokens; no cumulative, no
// baseline → direct turn value flows through as Known.
let r = AgentMetricIndexRow {
turn_cache_read_tokens: Some(80),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.cache_read_tokens,
UsageField {
value: Some("80".to_string()),
incomplete: false
}
);
}
#[test]
fn cache_write_ladder_uses_direct_turn_value_when_no_baseline() {
let r = AgentMetricIndexRow {
turn_cache_write_tokens: Some(40),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.cache_write_tokens,
UsageField {
value: Some("40".to_string()),
incomplete: false
}
);
}
#[test]
fn omitted_cache_field_stays_unknown_not_zero_through_pipeline() {
// Old-harness compat: a row that never reports cache_write_tokens
// must produce incomplete=true (unknown), NOT value=Some("0").
// The ladder returns Unknown for any absent turn/cumulative field, which
// poisons the TokenAccumulator as incomplete — absence is NOT treated as zero.
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
delta_reliable: Some(true),
turn_cache_write_tokens: None, // explicitly omitted
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.cache_write_tokens,
UsageField {
value: None,
incomplete: true
},
"absent cache_write_tokens must produce incomplete=true (unknown), not zero"
);
}
#[test]
fn explicit_zero_cache_field_survives_as_zero_not_absent() {
// A row reporting cache_write = 0 (explicit confirmed-zero) must produce
// value=Some("0"), not None.
let r = AgentMetricIndexRow {
turn_cache_write_tokens: Some(0),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.cache_write_tokens,
UsageField {
value: Some("0".to_string()),
incomplete: false
},
"explicit zero must survive as '0' not absent"
);
}
#[test]
fn cache_read_adjacent_cumulative_preferred_over_direct() {
// Two adjacent events with cumulative_cache_read_tokens: the second
// event's diff (200 - 100 = 100) is used, not the direct turn value.
let baseline = AgentMetricIndexRow {
cumulative_cache_read_tokens: Some(100),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let current = AgentMetricIndexRow {
turn_cache_read_tokens: Some(999), // direct — should NOT be used
cumulative_cache_read_tokens: Some(200),
delta_reliable: Some(true),
..row("e2", "agent1", "s1", 2, 1)
};
let boundaries = boundaries_7();
let rows = vec![baseline, current];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
// baseline contributes Unknown (no baseline before it, no turn value)
// then current contributes cumulative diff = 100
// Total = Unknown accumulation → incomplete
assert!(series.agents[0].usage.cache_read_tokens.incomplete);
}
// ── freshInputTokens derivation (P4a.2) ──────────────────────────────────────
#[test]
fn fresh_input_is_input_minus_cache_subsets() {
// input=100, cache_read=30, cache_write=20 → fresh=50
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
turn_cache_read_tokens: Some(30),
turn_cache_write_tokens: Some(20),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.fresh_input_tokens,
UsageField {
value: Some("50".to_string()),
incomplete: false
}
);
}
#[test]
fn fresh_input_fail_closed_when_cache_fields_absent() {
// cache_read and cache_write absent → ladder returns Unknown → accumulator
// is incomplete → fresh_input fails closed (incomplete: true).
// Old-harness compat: absent cache fields are unknown, not zero.
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
turn_cache_read_tokens: None,
turn_cache_write_tokens: None,
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.fresh_input_tokens,
UsageField {
value: None,
incomplete: true
},
"absent cache fields produce unknown fresh_input, not input-verbatim"
);
}
#[test]
fn fresh_input_fail_closed_when_subsets_exceed_input() {
// cache_read=60 + cache_write=60 = 120 > input=100 → incomplete
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
turn_cache_read_tokens: Some(60),
turn_cache_write_tokens: Some(60),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.fresh_input_tokens,
UsageField {
value: None,
incomplete: true
},
"cacheRead+cacheWrite > input must produce incomplete fresh_input"
);
}
#[test]
fn fresh_input_fail_closed_when_subset_sum_overflows() {
// Both cache fields are u64::MAX/2 + 1 → their sum overflows u64 → incomplete
let half_plus_one = u64::MAX / 2 + 1;
let r = AgentMetricIndexRow {
turn_input_tokens: Some(u64::MAX),
turn_cache_read_tokens: Some(half_plus_one),
turn_cache_write_tokens: Some(half_plus_one),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.fresh_input_tokens,
UsageField {
value: None,
incomplete: true
},
"cache subset sum overflow must produce incomplete fresh_input"
);
}
#[test]
fn fresh_input_fail_closed_when_input_unknown() {
// Unknown input (unreliable delta, no cumulative) → fresh_input incomplete
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
delta_reliable: Some(false), // unreliable, no cumulative → Unknown
turn_cache_read_tokens: Some(10),
turn_cache_write_tokens: Some(5),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].usage.fresh_input_tokens,
UsageField {
value: None,
incomplete: true
},
"unknown input must make fresh_input incomplete"
);
}
// ── D6 comparator test vector (P4a.3) ────────────────────────────────────────
//
// This vector pins the Rust sort order that the TS layer must match for
// model-breakdown rendering (P5). Each scenario documents the expected
// position in the sorted slice.
#[test]
fn d6_agent_with_total_ranks_before_input_output_only() {
// Agent A: total=200 (known from provider)
// Agent B: input=150, output=100 (total unknown) → sort value = 250
// D6: A has total=200 < 250, so B ranks first (higher sort value)
let a = AgentMetricIndexRow {
turn_total_tokens: Some(200),
delta_reliable: Some(true),
..row("e1", "agent_a", "s1", 1, 0)
};
let b = AgentMetricIndexRow {
turn_input_tokens: Some(150),
turn_output_tokens: Some(100),
delta_reliable: Some(true),
..row("e2", "agent_b", "s2", 1, 1)
};
let boundaries = boundaries_7();
let rows = vec![a, b];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
// B: input+output = 250 > A: total = 200 → B ranks first
assert_eq!(series.agents[0].agent_pubkey, "agent_b");
assert_eq!(series.agents[1].agent_pubkey, "agent_a");
}
#[test]
fn d6_input_output_fallback_used_when_total_unknown() {
// Agent A: total unknown, input=100 only (output=None) → sort value = None
// Agent B: total unknown, input=80, output=40 → sort value = 120
// D6: B has a sort value, A does not → B ranks first
let a = AgentMetricIndexRow {
turn_input_tokens: Some(100),
delta_reliable: Some(true),
..row("e1", "agent_a", "s1", 1, 0)
};
let b = AgentMetricIndexRow {
turn_input_tokens: Some(80),
turn_output_tokens: Some(40),
delta_reliable: Some(true),
..row("e2", "agent_b", "s2", 1, 1)
};
let boundaries = boundaries_7();
let rows = vec![a, b];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(
series.agents[0].agent_pubkey, "agent_b",
"input+output fallback must rank before unknown-sort-value agent"
);
assert_eq!(series.agents[1].agent_pubkey, "agent_a");
}
#[test]
fn d6_model_total_ranks_before_input_output_fallback_within_agent() {
// Same agent, two model rows:
// model-a: total=300 (known)
// model-b: input=200, output=150, total unknown → sort value = 350
// D6: model-b sort value 350 > model-a total 300 → model-b ranks first
let ra = AgentMetricIndexRow {
model: Some("model-a".to_string()),
turn_total_tokens: Some(300),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let rb = AgentMetricIndexRow {
model: Some("model-b".to_string()),
turn_input_tokens: Some(200),
turn_output_tokens: Some(150),
delta_reliable: Some(true),
..row("e2", "agent1", "s2", 1, 1)
};
let boundaries = boundaries_7();
let rows = vec![ra, rb];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
let models = &series.agents[0].models;
assert_eq!(models[0].model.as_deref(), Some("model-b"));
assert_eq!(models[1].model.as_deref(), Some("model-a"));
}
#[test]
fn d6_unknown_sort_value_ranks_last() {
// Three agents:
// agent_c: total=100 (known)
// agent_b: input=50, output=60 (total unknown) → sort value = 110
// agent_a: input only, no output, no total → sort value = None (unknown)
// Expected order: agent_b (110), agent_c (100), agent_a (None)
let c = AgentMetricIndexRow {
turn_total_tokens: Some(100),
delta_reliable: Some(true),
..row("e1", "agent_c", "s1", 1, 0)
};
let b = AgentMetricIndexRow {
turn_input_tokens: Some(50),
turn_output_tokens: Some(60),
delta_reliable: Some(true),
..row("e2", "agent_b", "s2", 1, 1)
};
let a = AgentMetricIndexRow {
turn_input_tokens: Some(200), // large input but no output → no sort value
delta_reliable: Some(true),
..row("e3", "agent_a", "s3", 1, 2)
};
let boundaries = boundaries_7();
let rows = vec![c, b, a];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(series.agents[0].agent_pubkey, "agent_b");
assert_eq!(series.agents[1].agent_pubkey, "agent_c");
assert_eq!(series.agents[2].agent_pubkey, "agent_a");
}
// ── hasUnknownUsage propagation (P4b: Wes's CHANGES_REQUESTED fixes) ─────────
/// Wes's escape (a): `derive_fresh_input()` returns `incomplete: true` when
/// subsets exceed input, but `hasUnknownUsage` was false because the four
/// original checked fields (input, output, total, cost) were all complete.
/// This pins that `hasUnknownUsage` is true at bucket, agent, model, and
/// top-level-coverage levels whenever derivation fails.
#[test]
fn has_unknown_usage_true_when_fresh_input_derivation_fails() {
// All four fields that the original has_unknown() checked are complete:
// input=100, output=50, total=150, cost=0.01 — no incomplete values there.
// But cache_read=60 + cache_write=60 = 120 > input=100 → derive fails.
// Before the fix, has_unknown() returned false (none of the four original
// fields were incomplete). After the fix it returns true.
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
turn_output_tokens: Some(50),
turn_total_tokens: Some(150),
turn_cost_usd: Some(0.01),
turn_cache_read_tokens: Some(60),
turn_cache_write_tokens: Some(60),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
// Bucket-level flag.
assert!(
series.buckets[0].has_unknown_usage,
"bucket hasUnknownUsage must be true when fresh_input derivation fails"
);
// Agent-level flag.
assert!(
series.agents[0].has_unknown_usage,
"agent hasUnknownUsage must be true when fresh_input derivation fails"
);
// Model-level flag.
assert!(
series.agents[0].models[0].has_unknown_usage,
"model hasUnknownUsage must be true when fresh_input derivation fails"
);
// Top-level coverage flag.
assert!(
series.coverage.has_unknown_usage,
"coverage hasUnknownUsage must be true when fresh_input derivation fails"
);
// Confirm the fresh_input value reflects the derivation failure.
assert_eq!(
series.agents[0].usage.fresh_input_tokens,
super::UsageField {
value: None,
incomplete: true,
},
"fresh_input_tokens must be incomplete when subsets exceed input"
);
}
/// Wes's escape (b): old harness omits cache fields → cache/fresh accumulators
/// are incomplete while `hasUnknownUsage` was false (the four original checked
/// fields were complete). This pins that `hasUnknownUsage` is true whenever
/// cache fields are absent.
#[test]
fn has_unknown_usage_true_when_cache_fields_absent() {
// All four fields that the original has_unknown() checked are complete:
// input=100, output=50, total=150, cost=0.01 — no incomplete values there.
// But cache_read and cache_write are absent (old harness) → cache
// accumulators are incomplete → fresh_input is incomplete → has_unknown true.
// Before the fix, has_unknown() returned false because input/output/total/cost
// were complete. After the fix it returns true (cache accumulators are incomplete).
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
turn_output_tokens: Some(50),
turn_total_tokens: Some(150),
turn_cost_usd: Some(0.01),
turn_cache_read_tokens: None, // absent — old harness
turn_cache_write_tokens: None, // absent — old harness
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
// Bucket-level flag.
assert!(
series.buckets[0].has_unknown_usage,
"bucket hasUnknownUsage must be true when cache fields absent"
);
// Agent-level flag.
assert!(
series.agents[0].has_unknown_usage,
"agent hasUnknownUsage must be true when cache fields absent"
);
// Model-level flag.
assert!(
series.agents[0].models[0].has_unknown_usage,
"model hasUnknownUsage must be true when cache fields absent"
);
// Top-level coverage flag.
assert!(
series.coverage.has_unknown_usage,
"coverage hasUnknownUsage must be true when cache fields absent"
);
// Sanity: the cache and fresh_input fields are all incomplete.
assert!(
series.agents[0].usage.cache_read_tokens.incomplete,
"cache_read_tokens must be incomplete when absent"
);
assert!(
series.agents[0].usage.cache_write_tokens.incomplete,
"cache_write_tokens must be incomplete when absent"
);
assert!(
series.agents[0].usage.fresh_input_tokens.incomplete,
"fresh_input_tokens must be incomplete when cache fields absent"
);
}
@@ -0,0 +1,837 @@
//! Tests for the pure NIP-AM accounting ladder, accumulators, and request
//! validation. No SQLite — rows are constructed directly.
use super::*;
use crate::archive::metric_store::ParseStatus;
// ── Row builder ──────────────────────────────────────────────────────────────
/// Build a fully-specified valid row for test scenarios. Defaults every
/// optional field to `None`/appropriate zero; callers override what a
/// scenario needs via struct-update syntax.
fn row(id: &str, agent: &str, session: &str, seq: u64, reported_at: i64) -> AgentMetricIndexRow {
AgentMetricIndexRow {
id: id.to_string(),
agent_pubkey: agent.to_string(),
event_created_at: reported_at,
archived_at: reported_at,
reported_at: Some(reported_at),
session_id: Some(session.to_string()),
turn_seq: Some(seq),
harness: None,
model: None,
delta_reliable: Some(true),
turn_input_tokens: None,
turn_output_tokens: None,
turn_total_tokens: None,
turn_cost_usd: None,
turn_cache_read_tokens: None,
cumulative_input_tokens: None,
cumulative_output_tokens: None,
cumulative_total_tokens: None,
cumulative_cost_usd: None,
cumulative_cache_read_tokens: None,
cumulative_cache_write_tokens: None,
turn_cache_write_tokens: None,
pricing_authority: None,
pricing_model: None,
pricing_cache_class: None,
parse_status: ParseStatus::Valid,
}
}
/// Standard 8-boundary window covering one day, seconds since epoch.
const DAY: i64 = 86_400;
fn boundaries_7() -> Vec<i64> {
(0..=7).map(|i| i * DAY).collect()
}
fn probe_map(
rows: &[AgentMetricIndexRow],
) -> std::collections::HashMap<(String, String, u64), Vec<&AgentMetricIndexRow>> {
let mut m = std::collections::HashMap::new();
for r in rows {
if let Some(key) = r.accounting_key() {
m.entry(key).or_insert_with(Vec::new).push(r);
}
}
m
}
// ── Ladder: direct / cumulative / fallback ──────────────────────────────────
#[test]
fn direct_turn_value_used_when_no_baseline() {
let r = AgentMetricIndexRow {
turn_input_tokens: Some(100),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let __probe_rows = [r.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&r, &probes);
assert!(matches!(outcome.input, FieldValue::Known(100)));
}
#[test]
fn adjacent_cumulative_preferred_over_direct() {
let prev = AgentMetricIndexRow {
cumulative_input_tokens: Some(1000),
..row("e0", "agent1", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(1300),
turn_input_tokens: Some(999), // deliberately wrong direct value
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 2, 10)
};
let __probe_rows = [prev, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Known(300)));
}
#[test]
fn direct_fallback_when_baseline_missing() {
// seq 5 has no row at seq 4 in the probe set → gap, direct-reliable only.
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(500),
turn_input_tokens: Some(42),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 5, 0)
};
let __probe_rows = [cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Known(42)));
}
#[test]
fn unreliable_delta_with_no_baseline_is_unknown() {
let cur = AgentMetricIndexRow {
turn_input_tokens: Some(42),
delta_reliable: Some(false),
..row("e1", "agent1", "s1", 5, 0)
};
let __probe_rows = [cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Unknown));
}
#[test]
fn sequence_gap_no_diff_but_direct_reliable_may_count() {
// predecessor exists at seq 1 but current is seq 3 (gap at seq 2) —
// predecessor lookup requires exact S-1, so seq 3's predecessor probe is
// for seq 2, which is absent. Direct fallback applies.
let baseline = AgentMetricIndexRow {
cumulative_input_tokens: Some(100),
..row("e0", "agent1", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(400),
turn_input_tokens: Some(50),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 3, 10)
};
let __probe_rows = [baseline, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Known(50)));
}
// ── A1: counter decrease is terminal, no fallback ───────────────────────────
#[test]
fn adjacent_decrease_with_reliable_direct_present_is_unknown() {
// Required A1 test: decreasing cumulative pair + deltaReliable true +
// direct turn value present → field unknown, NOT the direct value.
let prev = AgentMetricIndexRow {
cumulative_input_tokens: Some(1000),
..row("e0", "agent1", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(600), // decreased
turn_input_tokens: Some(77), // present, but must NOT be used
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 2, 10)
};
let __probe_rows = [prev, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Unknown));
}
#[test]
fn decrease_taints_only_the_affected_field() {
// Interpretation note in A1: a decrease on one field must not zero out
// sibling fields whose own adjacent pair is nondecreasing.
let prev = AgentMetricIndexRow {
cumulative_input_tokens: Some(1000),
cumulative_output_tokens: Some(200),
..row("e0", "agent1", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(600), // decreased
cumulative_output_tokens: Some(250), // increased, still valid
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 2, 10)
};
let __probe_rows = [prev, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Unknown));
assert!(matches!(outcome.output, FieldValue::Known(50)));
}
// ── Cost ladder (mirrors the token ladder tests above, f64-specific) ───────
#[test]
fn ladder_cost_direct_value_used_when_no_baseline() {
let r = AgentMetricIndexRow {
turn_cost_usd: Some(0.05),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let __probe_rows = [r.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&r, &probes);
assert!(matches!(outcome.cost, FieldValue::Known(v) if v == 0.05));
}
#[test]
fn ladder_cost_adjacent_cumulative_preferred_over_direct() {
let prev = AgentMetricIndexRow {
cumulative_cost_usd: Some(1.0),
..row("e0", "agent1", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_cost_usd: Some(1.3),
turn_cost_usd: Some(999.0), // deliberately wrong direct value
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 2, 10)
};
let __probe_rows = [prev, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.cost, FieldValue::Known(v) if (v - 0.3).abs() < f64::EPSILON));
}
#[test]
fn ladder_cost_adjacent_decrease_is_unknown_not_direct() {
// A1 applies identically to the cost field: a decreasing cumulative
// pair is terminal-unknown even with a present direct value.
let prev = AgentMetricIndexRow {
cumulative_cost_usd: Some(5.0),
..row("e0", "agent1", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_cost_usd: Some(3.0), // decreased
turn_cost_usd: Some(1.0), // present, but must NOT be used
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 2, 10)
};
let __probe_rows = [prev, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.cost, FieldValue::Unknown));
}
// ── A4/A11: duplicate sequence quarantine ───────────────────────────────────
#[test]
fn duplicate_at_sequence_quarantines_successor() {
// Two rows at N, one at N+1: N+1 must not cumulative-diff against
// either N candidate.
let dup_a = AgentMetricIndexRow {
cumulative_input_tokens: Some(100),
..row("e0a", "agent1", "s1", 5, 0)
};
let dup_b = AgentMetricIndexRow {
cumulative_input_tokens: Some(150),
..row("e0b", "agent1", "s1", 5, 1)
};
let successor = AgentMetricIndexRow {
cumulative_input_tokens: Some(400),
turn_input_tokens: Some(30),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 6, 10)
};
let __probe_rows = [dup_a, dup_b, successor.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&successor, &probes);
// No usable baseline (ambiguous predecessor) → direct-reliable fallback.
assert!(matches!(outcome.input, FieldValue::Known(30)));
}
#[test]
fn duplicate_row_itself_gets_no_cumulative_delta() {
let baseline = AgentMetricIndexRow {
cumulative_input_tokens: Some(100),
..row("e_base", "agent1", "s1", 4, 0)
};
let dup_a = AgentMetricIndexRow {
cumulative_input_tokens: Some(200),
turn_input_tokens: Some(99),
delta_reliable: Some(true),
..row("e5a", "agent1", "s1", 5, 1)
};
let dup_b = AgentMetricIndexRow {
cumulative_input_tokens: Some(250),
..row("e5b", "agent1", "s1", 5, 2)
};
let __probe_rows = [baseline, dup_a.clone(), dup_b];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&dup_a, &probes);
// Own sequence has >1 row → no cumulative delta; direct-reliable value
// for dup_a specifically still counts (A4: "only an independently
// reliable direct turn value may count").
assert!(matches!(outcome.input, FieldValue::Known(99)));
}
#[test]
fn duplicate_out_of_window_peer_still_quarantines_in_window_row() {
// A11: an out-of-window duplicate at the same sequence still poisons the
// in-window row's cumulative eligibility, because the probe set has no
// reported_at restriction.
let out_of_window_dup = AgentMetricIndexRow {
cumulative_input_tokens: Some(500),
..row("e_old", "agent1", "s1", 5, -1000)
};
let baseline = AgentMetricIndexRow {
cumulative_input_tokens: Some(100),
..row("e_base", "agent1", "s1", 4, 0)
};
let in_window = AgentMetricIndexRow {
cumulative_input_tokens: Some(200),
turn_input_tokens: Some(11),
delta_reliable: Some(true),
..row("e5", "agent1", "s1", 5, 1)
};
let __probe_rows = [out_of_window_dup, baseline, in_window.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&in_window, &probes);
assert!(matches!(outcome.input, FieldValue::Known(11)));
}
// ── A12: checked_sub sequence arithmetic ────────────────────────────────────
#[test]
fn adjacent_pair_at_u64_max_computes_normally() {
let prev = AgentMetricIndexRow {
cumulative_input_tokens: Some(1000),
..row("e_prev", "agent1", "s1", u64::MAX - 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(1500),
..row("e_max", "agent1", "s1", u64::MAX, 10)
};
let __probe_rows = [prev, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Known(500)));
}
#[test]
fn duplicate_at_u64_max_needs_no_successor_probe() {
// u64::MAX has no successor sequence to quarantine — verify duplicate
// handling at MAX itself still works (own-sequence cardinality check).
let dup_a = AgentMetricIndexRow {
cumulative_input_tokens: Some(100),
turn_input_tokens: Some(5),
delta_reliable: Some(true),
..row("e_max_a", "agent1", "s1", u64::MAX, 0)
};
let dup_b = AgentMetricIndexRow {
cumulative_input_tokens: Some(150),
..row("e_max_b", "agent1", "s1", u64::MAX, 1)
};
let __probe_rows = [dup_a.clone(), dup_b];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&dup_a, &probes);
assert!(matches!(outcome.input, FieldValue::Known(5)));
}
#[test]
fn seq_zero_has_no_baseline_underflow() {
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(100),
turn_input_tokens: Some(100),
delta_reliable: Some(true),
..row("e0", "agent1", "s1", 0, 0)
};
let __probe_rows = [cur.clone()];
let probes = probe_map(&__probe_rows);
// Must not panic (checked_sub) and must fall back to direct.
let outcome = compute_event_outcome(&cur, &probes);
assert!(matches!(outcome.input, FieldValue::Known(100)));
}
// ── Null total / per-field independence ─────────────────────────────────────
#[test]
fn null_total_tokens_stays_unknown_even_when_input_output_known() {
let r = AgentMetricIndexRow {
turn_input_tokens: Some(10),
turn_output_tokens: Some(20),
turn_total_tokens: None,
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let __probe_rows = [r.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&r, &probes);
assert!(matches!(outcome.input, FieldValue::Known(10)));
assert!(matches!(outcome.output, FieldValue::Known(20)));
assert!(matches!(outcome.total, FieldValue::Unknown));
}
// ── Cross-session / cross-agent isolation ───────────────────────────────────
#[test]
fn cumulative_diff_never_crosses_session_boundary() {
let other_session = AgentMetricIndexRow {
cumulative_input_tokens: Some(999_999),
..row("e_other", "agent1", "s_other", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(50),
turn_input_tokens: Some(50),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 10)
};
let __probe_rows = [other_session, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
// seq 1 has no predecessor (seq 0) in ANY session — direct fallback.
assert!(matches!(outcome.input, FieldValue::Known(50)));
}
#[test]
fn cumulative_diff_never_crosses_agent_boundary() {
let other_agent = AgentMetricIndexRow {
cumulative_input_tokens: Some(999_999),
..row("e_other", "agent2", "s1", 1, 0)
};
let cur = AgentMetricIndexRow {
cumulative_input_tokens: Some(60),
turn_input_tokens: Some(60),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 2, 10)
};
let __probe_rows = [other_agent, cur.clone()];
let probes = probe_map(&__probe_rows);
let outcome = compute_event_outcome(&cur, &probes);
// seq 2's predecessor (seq 1) does not exist for agent1 — direct.
assert!(matches!(outcome.input, FieldValue::Known(60)));
}
// ── window_probe_keys ────────────────────────────────────────────────────────
#[test]
fn window_probe_keys_includes_own_and_predecessor() {
let r = row("e1", "agent1", "s1", 5, 0);
let keys = window_probe_keys(&[r]);
assert!(keys.contains(&("agent1".to_string(), "s1".to_string(), 5)));
assert!(keys.contains(&("agent1".to_string(), "s1".to_string(), 4)));
assert_eq!(keys.len(), 2);
}
#[test]
fn window_probe_keys_skips_predecessor_at_seq_zero() {
let r = row("e1", "agent1", "s1", 0, 0);
let keys = window_probe_keys(&[r]);
assert_eq!(keys.len(), 1);
assert!(keys.contains(&("agent1".to_string(), "s1".to_string(), 0)));
}
#[test]
fn window_probe_keys_skips_rows_without_session_or_seq() {
let r = AgentMetricIndexRow {
session_id: None,
turn_seq: None,
..row("e1", "agent1", "s1", 5, 0)
};
let keys = window_probe_keys(&[r]);
assert!(keys.is_empty());
}
// ── assign_bucket_index: boundary edges ─────────────────────────────────────
#[test]
fn assign_bucket_index_start_is_inclusive_end_is_exclusive() {
let boundaries = boundaries_7();
// Exactly on bucket 1's start boundary → bucket 1, not bucket 0.
assert_eq!(assign_bucket_index(&boundaries, DAY), Some(1));
// One second before bucket 1's start → still bucket 0 (end-exclusive).
assert_eq!(assign_bucket_index(&boundaries, DAY - 1), Some(0));
}
#[test]
fn assign_bucket_index_returns_none_outside_every_bucket() {
let boundaries = boundaries_7();
assert_eq!(assign_bucket_index(&boundaries, -1), None);
assert_eq!(assign_bucket_index(&boundaries, boundaries[7]), None); // last boundary is exclusive end
}
// ── compute_series: bucketing, overflow, ranking, models ────────────────────
#[test]
fn compute_series_buckets_by_reported_at_not_created_at() {
let r = AgentMetricIndexRow {
turn_input_tokens: Some(10),
delta_reliable: Some(true),
event_created_at: 999_999_999, // deliberately wrong/misleading
..row("e1", "agent1", "s1", 1, DAY + 100) // reported_at lands in bucket 1
};
let boundaries = boundaries_7();
let series = compute_series(
std::slice::from_ref(&r),
std::slice::from_ref(&r),
0,
&boundaries,
None,
true,
);
assert_eq!(series.buckets[0].report_count, 0);
assert_eq!(series.buckets[1].report_count, 1);
}
#[test]
fn compute_series_checked_add_overflow_marks_incomplete_without_wrapping() {
let r1 = AgentMetricIndexRow {
turn_input_tokens: Some(u64::MAX),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 10, 0)
};
let r2 = AgentMetricIndexRow {
turn_input_tokens: Some(5),
delta_reliable: Some(true),
..row("e2", "agent1", "s2", 10, 1) // different session avoids adjacency
};
let boundaries = boundaries_7();
let rows = vec![r1, r2];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
let bucket = &series.buckets[0];
assert!(bucket.has_unknown_usage);
// Value freezes at the last valid sum (u64::MAX) rather than wrapping.
assert_eq!(bucket.usage.input_tokens.value, Some(u64::MAX.to_string()));
assert!(bucket.usage.input_tokens.incomplete);
}
#[test]
fn compute_series_cost_non_finite_marks_incomplete() {
let r1 = AgentMetricIndexRow {
turn_cost_usd: Some(f64::MAX),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 10, 0)
};
let r2 = AgentMetricIndexRow {
turn_cost_usd: Some(f64::MAX),
delta_reliable: Some(true),
..row("e2", "agent1", "s2", 10, 1)
};
let boundaries = boundaries_7();
let rows = vec![r1, r2];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert!(series.buckets[0].usage.estimated_cost_usd.incomplete);
}
#[test]
fn compute_series_ranks_known_total_before_unknown_total() {
let known = AgentMetricIndexRow {
turn_total_tokens: Some(500),
delta_reliable: Some(true),
..row("e1", "agent_known", "s1", 1, 0)
};
let unknown = AgentMetricIndexRow {
turn_total_tokens: None,
delta_reliable: Some(true),
..row("e2", "agent_unknown", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![known, unknown];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(series.agents[0].agent_pubkey, "agent_known");
assert_eq!(series.agents[1].agent_pubkey, "agent_unknown");
}
#[test]
fn compute_series_ties_on_known_total_break_by_pubkey_ascending() {
// Equal totalTokens for two agents: the A2 tiebreak must be
// deterministic pubkey order, not insertion/hash order.
let agent_z = AgentMetricIndexRow {
turn_total_tokens: Some(100),
delta_reliable: Some(true),
..row("e1", "agent_zzz", "s1", 1, 0)
};
let agent_a = AgentMetricIndexRow {
turn_total_tokens: Some(100),
delta_reliable: Some(true),
..row("e2", "agent_aaa", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![agent_z, agent_a];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(series.agents[0].agent_pubkey, "agent_aaa");
assert_eq!(series.agents[1].agent_pubkey, "agent_zzz");
}
#[test]
fn compute_series_model_breakdown_attributes_per_event_model() {
let r1 = AgentMetricIndexRow {
model: Some("model-a".to_string()),
turn_input_tokens: Some(10),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let r2 = AgentMetricIndexRow {
model: Some("model-b".to_string()),
turn_input_tokens: Some(20),
delta_reliable: Some(true),
..row("e2", "agent1", "s2", 1, 1)
};
let boundaries = boundaries_7();
let rows = vec![r1, r2];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(series.agents.len(), 1);
assert_eq!(series.agents[0].models.len(), 2);
}
#[test]
fn compute_series_same_model_two_harnesses_produces_two_rows() {
// Collapse-fix requirement: same model via two different harnesses must
// NOT collapse into one row — (harness, model) is the grouping key.
let r1 = AgentMetricIndexRow {
harness: Some("goose".to_string()),
model: Some("claude-sonnet".to_string()),
turn_input_tokens: Some(100),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let r2 = AgentMetricIndexRow {
harness: Some("claude-code".to_string()),
model: Some("claude-sonnet".to_string()),
turn_input_tokens: Some(200),
delta_reliable: Some(true),
..row("e2", "agent1", "s2", 1, 1)
};
let boundaries = boundaries_7();
let rows = vec![r1, r2];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(series.agents.len(), 1);
// Two distinct harnesses → two rows, not one collapsed row.
assert_eq!(
series.agents[0].models.len(),
2,
"same model under two harnesses must produce two rows"
);
let harneses: Vec<Option<&str>> = series.agents[0]
.models
.iter()
.map(|m| m.harness.as_deref())
.collect();
assert!(
harneses.contains(&Some("claude-code")),
"claude-code row must be present"
);
assert!(
harneses.contains(&Some("goose")),
"goose row must be present"
);
}
#[test]
fn compute_series_single_harness_data_looks_unchanged_with_harness_label() {
// Single-harness data: still exactly one row per model, harness label present.
let r = AgentMetricIndexRow {
harness: Some("goose".to_string()),
model: Some("claude-sonnet".to_string()),
turn_total_tokens: Some(500),
delta_reliable: Some(true),
..row("e1", "agent1", "s1", 1, 0)
};
let boundaries = boundaries_7();
let rows = vec![r];
let series = compute_series(&rows, &rows, 0, &boundaries, None, true);
assert_eq!(series.agents[0].models.len(), 1);
assert_eq!(
series.agents[0].models[0].harness,
Some("goose".to_string())
);
assert_eq!(
series.agents[0].models[0].model,
Some("claude-sonnet".to_string())
);
}
#[test]
fn compute_series_invalid_report_count_passed_through_and_not_bucketed() {
let boundaries = boundaries_7();
let series = compute_series(&[], &[], 3, &boundaries, None, true);
assert_eq!(series.coverage.invalid_report_count, 3);
assert_eq!(series.coverage.report_count, 0);
assert!(series.coverage.has_unknown_usage);
}
#[test]
fn compute_series_zero_invalid_and_no_unknown_rows_is_not_unknown() {
let boundaries = boundaries_7();
let series = compute_series(&[], &[], 0, &boundaries, None, true);
assert_eq!(series.coverage.invalid_report_count, 0);
assert!(!series.coverage.has_unknown_usage);
}
#[test]
fn compute_series_collection_enabled_passthrough() {
let boundaries = boundaries_7();
let series = compute_series(&[], &[], 0, &boundaries, None, false);
assert!(!series.collection_enabled);
}
#[test]
fn compute_series_has_archived_evidence_passthrough() {
let boundaries = boundaries_7();
let series = compute_series(&[], &[], 0, &boundaries, Some(true), true);
assert_eq!(series.has_archived_evidence, Some(true));
let series_none = compute_series(&[], &[], 0, &boundaries, None, true);
assert_eq!(series_none.has_archived_evidence, None);
}
// ── validate_request ─────────────────────────────────────────────────────────
fn req(boundaries: Vec<i64>, agent_pubkey: Option<String>) -> AgentUsageSeriesRequest {
AgentUsageSeriesRequest {
bucket_boundaries: boundaries,
agent_pubkey,
}
}
#[test]
fn validate_request_accepts_boundary_counts_across_the_supported_range() {
// 2 boundaries = the `1d` single-bucket case.
assert!(validate_request(&req(vec![0, DAY], None)).is_ok());
assert!(validate_request(&req(boundaries_7(), None)).is_ok());
let b31: Vec<i64> = (0..=30).map(|i| i * DAY).collect();
assert!(validate_request(&req(b31, None)).is_ok());
// 367 boundaries = 366 daily buckets = one leap year, the ceiling.
let b367: Vec<i64> = (0..=366).map(|i| i * DAY).collect();
assert_eq!(b367.len(), 367);
assert!(validate_request(&req(b367, None)).is_ok());
}
#[test]
fn validate_request_rejects_boundary_count_outside_the_supported_range() {
// A single boundary describes no bucket at all.
assert!(validate_request(&req(vec![0], None)).is_err());
assert!(validate_request(&req(vec![], None)).is_err());
// 368 boundaries = 367 buckets, one past the one-leap-year ceiling.
let b368: Vec<i64> = (0..=367).map(|i| i * DAY).collect();
assert_eq!(b368.len(), 368);
assert!(validate_request(&req(b368, None)).is_err());
}
#[test]
fn validate_request_rejects_non_increasing_boundaries() {
let mut b = boundaries_7();
b[3] = b[2]; // zero-width interval
assert!(validate_request(&req(b, None)).is_err());
}
#[test]
fn validate_request_rejects_interval_over_48h() {
let mut b = boundaries_7();
b[1] = b[0] + 49 * 3600;
assert!(validate_request(&req(b, None)).is_err());
}
#[test]
fn validate_request_rejects_boundary_out_of_chrono_representable_range() {
// All 7 intervals stay well within the 48h band (exactly one day each)
// so only the finite-range check can be responsible for the rejection;
// the window is shifted to straddle chrono's actual max representable
// instant rather than a hardcoded guess.
let max_ts = chrono::DateTime::<chrono::Utc>::MAX_UTC.timestamp();
let base = max_ts - 6 * DAY;
let b: Vec<i64> = (0..=7).map(|i| base + i * DAY).collect();
assert!(
b[7] > max_ts,
"test setup must push the last boundary out of range"
);
assert!(validate_request(&req(b, None)).is_err());
}
#[test]
fn validate_request_accepts_30_minute_dst_interval() {
// Lord Howe Island: 30-minute DST offset. A day-boundary pair differing
// by 23.5h must be accepted under the 48h sanity band (A9).
let mut b = boundaries_7();
b[1] = b[0] + 23 * 3600 + 1800;
assert!(validate_request(&req(b, None)).is_ok());
}
#[test]
fn validate_request_normalizes_pubkey_to_lowercase() {
let pk = "AB".repeat(32);
let result = validate_request(&req(boundaries_7(), Some(pk.clone())));
assert_eq!(result.unwrap(), Some(pk.to_lowercase()));
}
#[test]
fn validate_request_rejects_malformed_pubkey() {
let short = "ab".repeat(10);
assert!(validate_request(&req(boundaries_7(), Some(short))).is_err());
let non_hex = "zz".repeat(32);
assert!(validate_request(&req(boundaries_7(), Some(non_hex))).is_err());
}
// ── Tauri camelCase key-shape contract ────────────────────────────────────────
/// Verify that a fully-populated `ReportedUsage` serializes to exactly the
/// seven camelCase keys the TS `ReportedUsage` type declares. Any Rust-side
/// rename drift will fail this test before reaching the TypeScript consumer.
#[test]
fn reported_usage_serializes_with_all_seven_camel_case_keys() {
let field = || UsageField {
value: Some("1".to_string()),
incomplete: false,
};
let usage = ReportedUsage {
input_tokens: field(),
output_tokens: field(),
total_tokens: field(),
estimated_cost_usd: CostField {
value: Some(0.001),
incomplete: false,
},
cache_read_tokens: field(),
cache_write_tokens: field(),
fresh_input_tokens: field(),
};
let obj = serde_json::to_value(&usage).expect("ReportedUsage must serialize");
let keys: std::collections::BTreeSet<&str> = obj
.as_object()
.expect("must be a JSON object")
.keys()
.map(String::as_str)
.collect();
let expected: std::collections::BTreeSet<&str> = [
"inputTokens",
"outputTokens",
"totalTokens",
"estimatedCostUsd",
"cacheReadTokens",
"cacheWriteTokens",
"freshInputTokens",
]
.iter()
.copied()
.collect();
assert_eq!(
keys, expected,
"ReportedUsage camelCase key contract changed — update tauriArchive.ts to match"
);
}
@@ -0,0 +1,630 @@
//! Parsed index of kind 44200 (NIP-AM agent turn metric) archive rows.
//!
//! `agent_metric_index` is a derived, rebuildable cache of parsed NIP-AM
//! payload fields, keyed by `(identity_pubkey, relay_url, id)` exactly like
//! `archived_events`. It exists so the accounting algorithm in
//! `agent_usage.rs` can query parsed columns instead of re-parsing JSON on
//! every render. The canonical source of truth remains
//! `archived_events.raw_json`; every row here is reproducible from it alone
//! via [`AgentMetricIndexRow::from_payload`].
//!
//! Kept in a sibling file (not `store.rs`) to keep that file under the
//! 1000-line gate, per the existing `pipeline.rs` precedent.
use rusqlite::{params, Connection, OptionalExtension};
use buzz_core_pkg::agent_turn_metric::AgentTurnMetricPayload;
// ── u64-safe sortable encoding ───────────────────────────────────────────────
/// Fixed-width digit count for the lexicographically order-preserving decimal
/// encoding of a `u64`. `u64::MAX` = 18446744073709551615 is 20 digits.
const U64_SORTABLE_WIDTH: usize = 20;
/// Encode a `u64` as a fixed-width zero-padded decimal string so SQLite TEXT
/// ordering matches numeric ordering, and so the full `u64` range survives
/// SQLite's signed-`i64` INTEGER column type (rusqlite has no unsigned
/// binding). Used for both token counters and `turn_seq`.
pub(super) fn encode_u64_sortable(value: u64) -> String {
format!("{value:0U64_SORTABLE_WIDTH$}")
}
/// Decode a value written by [`encode_u64_sortable`]. Returns `None` if the
/// string is not a well-formed same-width decimal `u64` — defensive only;
/// every value written by this module is always well-formed.
pub(super) fn decode_u64_sortable(text: &str) -> Option<u64> {
if text.len() != U64_SORTABLE_WIDTH {
return None;
}
text.parse::<u64>().ok()
}
fn parse_rfc3339_secs(timestamp: &str) -> Option<i64> {
chrono::DateTime::parse_from_rfc3339(timestamp)
.ok()
.map(|dt| dt.timestamp())
}
// ── Row type ──────────────────────────────────────────────────────────────
/// Parse status of a stored `agent_metric_index` row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ParseStatus {
Valid,
Invalid,
}
impl ParseStatus {
fn as_str(self) -> &'static str {
match self {
ParseStatus::Valid => "valid",
ParseStatus::Invalid => "invalid",
}
}
fn from_str(s: &str) -> Self {
match s {
"valid" => ParseStatus::Valid,
_ => ParseStatus::Invalid,
}
}
}
/// One fully parsed `agent_metric_index` row.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct AgentMetricIndexRow {
pub id: String,
pub agent_pubkey: String,
pub event_created_at: i64,
pub archived_at: i64,
pub reported_at: Option<i64>,
pub session_id: Option<String>,
pub turn_seq: Option<u64>,
pub harness: Option<String>,
pub model: Option<String>,
pub delta_reliable: Option<bool>,
pub turn_input_tokens: Option<u64>,
pub turn_output_tokens: Option<u64>,
pub turn_total_tokens: Option<u64>,
pub turn_cost_usd: Option<f64>,
pub turn_cache_read_tokens: Option<u64>,
pub turn_cache_write_tokens: Option<u64>,
pub cumulative_input_tokens: Option<u64>,
pub cumulative_output_tokens: Option<u64>,
pub cumulative_total_tokens: Option<u64>,
pub cumulative_cost_usd: Option<f64>,
pub cumulative_cache_read_tokens: Option<u64>,
pub cumulative_cache_write_tokens: Option<u64>,
/// Billing identity fields. All three are `None` when the publisher did
/// not include a `pricingIdentity` object (unrecognised endpoint, mixed
/// identities, old harness). Stored as separate columns for indexing.
pub pricing_authority: Option<String>,
pub pricing_model: Option<String>,
pub pricing_cache_class: Option<String>,
pub parse_status: ParseStatus,
}
impl AgentMetricIndexRow {
/// Parse a decrypted NIP-AM payload (the plaintext JSON already stored in
/// `archived_events.raw_json` for kind 44200 rows) into an index row.
///
/// New ingest and backfill share this single parser so validity rules
/// cannot drift between the two paths (frozen plan requirement).
///
/// "Invalid" per Rev 2 A5: the payload's JSON decodes but fails a
/// semantic check this layer owns: unparseable RFC3339 `timestamp`, or
/// `cumulative` present without both `sessionId` and `turnSeq` (NIP-AM
/// REQUIREs both whenever `cumulative` is present — a row missing either
/// cannot supply the complete `(agent, session, seq)` key needed to
/// compete as a cumulative snapshot). The upstream fail-closed ingest
/// path (`pipeline.rs::commit_archive`) has already decrypted,
/// deserialized, and numeric-validated (non-negative/finite `costUsd`)
/// before this row is ever produced — a raw-JSON parse failure here
/// would indicate on-disk corruption, not a normal producer error, but
/// is still handled fail-closed rather than panicking.
pub(super) fn from_payload(
raw_json: &str,
id: &str,
agent_pubkey: &str,
event_created_at: i64,
archived_at: i64,
) -> Self {
let Ok(payload) = serde_json::from_str::<AgentTurnMetricPayload>(raw_json) else {
return Self::invalid(id, agent_pubkey, event_created_at, archived_at);
};
let reported_at = parse_rfc3339_secs(&payload.timestamp);
let cumulative_requires_session_seq = payload.cumulative.is_some();
let has_session_and_seq = payload.session_id.is_some() && payload.turn_seq.is_some();
if reported_at.is_none() || (cumulative_requires_session_seq && !has_session_and_seq) {
return Self::invalid(id, agent_pubkey, event_created_at, archived_at);
}
let turn = payload.turn.as_ref();
let cumulative = payload.cumulative.as_ref();
Self {
id: id.to_string(),
agent_pubkey: agent_pubkey.to_string(),
event_created_at,
archived_at,
reported_at,
session_id: payload.session_id,
turn_seq: payload.turn_seq,
harness: Some(payload.harness),
model: payload.model,
delta_reliable: Some(payload.delta_reliable),
turn_input_tokens: turn.and_then(|t| t.input_tokens),
turn_output_tokens: turn.and_then(|t| t.output_tokens),
turn_total_tokens: turn.and_then(|t| t.total_tokens),
turn_cost_usd: turn.and_then(|t| t.cost_usd),
turn_cache_read_tokens: turn.and_then(|t| t.cache_read_tokens),
turn_cache_write_tokens: turn.and_then(|t| t.cache_write_tokens),
cumulative_input_tokens: cumulative.and_then(|c| c.input_tokens),
cumulative_output_tokens: cumulative.and_then(|c| c.output_tokens),
cumulative_total_tokens: cumulative.and_then(|c| c.total_tokens),
cumulative_cost_usd: cumulative.and_then(|c| c.cost_usd),
cumulative_cache_read_tokens: cumulative.and_then(|c| c.cache_read_tokens),
cumulative_cache_write_tokens: cumulative.and_then(|c| c.cache_write_tokens),
pricing_authority: payload
.pricing_identity
.as_ref()
.map(|pi| pi.authority.clone()),
pricing_model: payload.pricing_identity.as_ref().map(|pi| pi.model.clone()),
pricing_cache_class: payload
.pricing_identity
.as_ref()
.and_then(|pi| pi.cache_class.clone()),
parse_status: ParseStatus::Valid,
}
}
fn invalid(id: &str, agent_pubkey: &str, event_created_at: i64, archived_at: i64) -> Self {
Self {
id: id.to_string(),
agent_pubkey: agent_pubkey.to_string(),
event_created_at,
archived_at,
reported_at: None,
session_id: None,
turn_seq: None,
harness: None,
model: None,
delta_reliable: None,
turn_input_tokens: None,
turn_output_tokens: None,
turn_total_tokens: None,
turn_cost_usd: None,
turn_cache_read_tokens: None,
turn_cache_write_tokens: None,
cumulative_input_tokens: None,
cumulative_output_tokens: None,
cumulative_total_tokens: None,
cumulative_cost_usd: None,
cumulative_cache_read_tokens: None,
cumulative_cache_write_tokens: None,
pricing_authority: None,
pricing_model: None,
pricing_cache_class: None,
parse_status: ParseStatus::Invalid,
}
}
/// The `(agent_pubkey, session_id, turn_seq)` cumulative-accounting key,
/// or `None` if this row cannot participate in cumulative delta
/// recomputation (missing session/sequence).
pub(super) fn accounting_key(&self) -> Option<(String, String, u64)> {
match (&self.session_id, self.turn_seq) {
(Some(sid), Some(seq)) => Some((self.agent_pubkey.clone(), sid.clone(), seq)),
_ => None,
}
}
}
fn row_from_sql(row: &rusqlite::Row) -> rusqlite::Result<AgentMetricIndexRow> {
let turn_seq_text: Option<String> = row.get("turn_seq")?;
let turn_input_text: Option<String> = row.get("turn_input_tokens")?;
let turn_output_text: Option<String> = row.get("turn_output_tokens")?;
let turn_total_text: Option<String> = row.get("turn_total_tokens")?;
let turn_cache_read_text: Option<String> = row.get("turn_cache_read_tokens")?;
let turn_cache_write_text: Option<String> = row.get("turn_cache_write_tokens")?;
let cum_input_text: Option<String> = row.get("cumulative_input_tokens")?;
let cum_output_text: Option<String> = row.get("cumulative_output_tokens")?;
let cum_total_text: Option<String> = row.get("cumulative_total_tokens")?;
let cum_cache_read_text: Option<String> = row.get("cumulative_cache_read_tokens")?;
let cum_cache_write_text: Option<String> = row.get("cumulative_cache_write_tokens")?;
let delta_reliable_int: Option<i64> = row.get("delta_reliable")?;
let parse_status_str: String = row.get("parse_status")?;
Ok(AgentMetricIndexRow {
id: row.get("id")?,
agent_pubkey: row.get("agent_pubkey")?,
event_created_at: row.get("event_created_at")?,
archived_at: row.get("archived_at")?,
reported_at: row.get("reported_at")?,
session_id: row.get("session_id")?,
turn_seq: turn_seq_text.as_deref().and_then(decode_u64_sortable),
harness: row.get("harness")?,
model: row.get("model")?,
delta_reliable: delta_reliable_int.map(|v| v != 0),
turn_input_tokens: turn_input_text.as_deref().and_then(decode_u64_sortable),
turn_output_tokens: turn_output_text.as_deref().and_then(decode_u64_sortable),
turn_total_tokens: turn_total_text.as_deref().and_then(decode_u64_sortable),
turn_cost_usd: row.get("turn_cost_usd")?,
turn_cache_read_tokens: turn_cache_read_text
.as_deref()
.and_then(decode_u64_sortable),
turn_cache_write_tokens: turn_cache_write_text
.as_deref()
.and_then(decode_u64_sortable),
cumulative_input_tokens: cum_input_text.as_deref().and_then(decode_u64_sortable),
cumulative_output_tokens: cum_output_text.as_deref().and_then(decode_u64_sortable),
cumulative_total_tokens: cum_total_text.as_deref().and_then(decode_u64_sortable),
cumulative_cost_usd: row.get("cumulative_cost_usd")?,
cumulative_cache_read_tokens: cum_cache_read_text.as_deref().and_then(decode_u64_sortable),
cumulative_cache_write_tokens: cum_cache_write_text
.as_deref()
.and_then(decode_u64_sortable),
pricing_authority: row.get("pricing_authority")?,
pricing_model: row.get("pricing_model")?,
pricing_cache_class: row.get("pricing_cache_class")?,
parse_status: ParseStatus::from_str(&parse_status_str),
})
}
const ROW_COLUMNS: &str = "id, agent_pubkey, event_created_at, archived_at, reported_at, \
session_id, turn_seq, harness, model, delta_reliable, turn_input_tokens, turn_output_tokens, \
turn_total_tokens, turn_cost_usd, turn_cache_read_tokens, turn_cache_write_tokens, \
cumulative_input_tokens, cumulative_output_tokens, cumulative_total_tokens, \
cumulative_cost_usd, cumulative_cache_read_tokens, cumulative_cache_write_tokens, \
pricing_authority, pricing_model, pricing_cache_class, parse_status";
// ── Write path ───────────────────────────────────────────────────────────────
/// Insert one metric index row inside the caller's transaction.
///
/// Called from `pipeline::commit_archive` ONLY when the corresponding
/// `archived_events` row was newly inserted (never for a duplicate), and
/// from the backfill driver for pre-existing unindexed rows. `INSERT OR
/// IGNORE` on the shared PK makes a second call for the same
/// `(identity, relay, id)` a safe no-op (defensive; callers already guard
/// against re-indexing).
pub(super) fn insert_metric_index_row(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
row: &AgentMetricIndexRow,
) -> Result<bool, String> {
let turn_seq = row.turn_seq.map(encode_u64_sortable);
let turn_input = row.turn_input_tokens.map(encode_u64_sortable);
let turn_output = row.turn_output_tokens.map(encode_u64_sortable);
let turn_total = row.turn_total_tokens.map(encode_u64_sortable);
let turn_cache_read = row.turn_cache_read_tokens.map(encode_u64_sortable);
let turn_cache_write = row.turn_cache_write_tokens.map(encode_u64_sortable);
let cum_input = row.cumulative_input_tokens.map(encode_u64_sortable);
let cum_output = row.cumulative_output_tokens.map(encode_u64_sortable);
let cum_total = row.cumulative_total_tokens.map(encode_u64_sortable);
let cum_cache_read = row.cumulative_cache_read_tokens.map(encode_u64_sortable);
let cum_cache_write = row.cumulative_cache_write_tokens.map(encode_u64_sortable);
let delta_reliable = row.delta_reliable.map(|b| b as i64);
let affected = conn
.execute(
"INSERT INTO agent_metric_index
(identity_pubkey, relay_url, id, agent_pubkey, event_created_at,
archived_at, reported_at, session_id, turn_seq, harness, model,
delta_reliable, turn_input_tokens, turn_output_tokens,
turn_total_tokens, turn_cost_usd, turn_cache_read_tokens,
turn_cache_write_tokens, cumulative_input_tokens, cumulative_output_tokens,
cumulative_total_tokens, cumulative_cost_usd,
cumulative_cache_read_tokens, cumulative_cache_write_tokens,
pricing_authority, pricing_model, pricing_cache_class, parse_status)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28)
ON CONFLICT (identity_pubkey, relay_url, id) DO NOTHING",
params![
identity_pubkey,
relay_url,
row.id,
row.agent_pubkey,
row.event_created_at,
row.archived_at,
row.reported_at,
row.session_id,
turn_seq,
row.harness,
row.model,
delta_reliable,
turn_input,
turn_output,
turn_total,
row.turn_cost_usd,
turn_cache_read,
turn_cache_write,
cum_input,
cum_output,
cum_total,
row.cumulative_cost_usd,
cum_cache_read,
cum_cache_write,
row.pricing_authority,
row.pricing_model,
row.pricing_cache_class,
row.parse_status.as_str(),
],
)
.map_err(|e| format!("failed to insert agent_metric_index row: {e}"))?;
Ok(affected > 0)
}
// ── Backfill ─────────────────────────────────────────────────────────────────
/// Backfill existing `archived_events` kind-44200 rows that have no matching
/// `agent_metric_index` row yet, for the given identity + relay.
///
/// Runs in bounded chunks (~500 rows per transaction) so a large existing
/// archive never holds one unbounded write lock; each chunk is independently
/// atomic and the whole backfill is idempotent (anti-join + index PK is the
/// source of truth) and restartable — interruption between chunks loses
/// nothing, and a later run simply resumes against the still-missing rows.
///
/// Returns the total number of newly indexed rows.
pub(super) fn backfill_agent_metric_index(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
) -> Result<usize, String> {
const CHUNK_SIZE: i64 = 500;
let mut total = 0usize;
loop {
let mut stmt = conn
.prepare(
"SELECT ae.id, ae.pubkey, ae.created_at, ae.archived_at, ae.raw_json
FROM archived_events ae
WHERE ae.identity_pubkey = ?1
AND ae.relay_url = ?2
AND ae.kind = 44200
AND ae.id NOT IN (
SELECT id FROM agent_metric_index
WHERE identity_pubkey = ?1
AND relay_url = ?2
)
ORDER BY ae.created_at ASC, ae.id ASC
LIMIT ?3",
)
.map_err(|e| format!("prepare backfill_agent_metric_index select: {e}"))?;
let chunk: Vec<(String, String, i64, i64, String)> = stmt
.query_map(params![identity_pubkey, relay_url, CHUNK_SIZE], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, i64>(2)?,
row.get::<_, i64>(3)?,
row.get::<_, String>(4)?,
))
})
.map_err(|e| format!("query backfill_agent_metric_index select: {e}"))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("read backfill_agent_metric_index row: {e}"))?;
drop(stmt);
if chunk.is_empty() {
break;
}
let chunk_len = chunk.len();
let tx = conn
.unchecked_transaction()
.map_err(|e| format!("failed to begin backfill chunk transaction: {e}"))?;
for (id, pubkey, created_at, archived_at, raw_json) in &chunk {
let parsed =
AgentMetricIndexRow::from_payload(raw_json, id, pubkey, *created_at, *archived_at);
insert_metric_index_row(&tx, identity_pubkey, relay_url, &parsed)?;
}
tx.commit()
.map_err(|e| format!("failed to commit backfill chunk: {e}"))?;
total += chunk_len;
if (chunk_len as i64) < CHUNK_SIZE {
break;
}
}
Ok(total)
}
// ── GC / orphan repair ───────────────────────────────────────────────────────
/// Delete `agent_metric_index` rows whose canonical `archived_events` row no
/// longer exists. Called from `store::gc_orphaned_events` inside the SAME
/// SQLite transaction as the canonical delete (A6) so the index can never
/// observe a canonical row as gone while its own row survives.
pub(super) fn delete_orphaned_metric_index_rows(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
) -> Result<usize, String> {
let affected = conn
.execute(
"DELETE FROM agent_metric_index
WHERE identity_pubkey = ?1
AND relay_url = ?2
AND id NOT IN (
SELECT id FROM archived_events
WHERE identity_pubkey = ?1
AND relay_url = ?2
)",
params![identity_pubkey, relay_url],
)
.map_err(|e| format!("failed to gc orphaned agent_metric_index rows: {e}"))?;
Ok(affected)
}
/// Read-time orphan repair: same anti-join delete as
/// [`delete_orphaned_metric_index_rows`], run defensively before every read
/// so a planted/legacy orphan is self-healed even if a future deletion path
/// forgets to call the GC cascade. Defense in depth, not an atomicity
/// substitute for A6.
pub(super) fn repair_orphaned_metric_index_rows(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
) -> Result<usize, String> {
delete_orphaned_metric_index_rows(conn, identity_pubkey, relay_url)
}
// ── Read path ────────────────────────────────────────────────────────────────
/// Load all VALID rows whose `reported_at` falls in `[start, end)`, optionally
/// filtered to one agent author. This is the exact set of rows that may ever
/// be counted into a bucket (A11 step 1).
pub(super) fn load_window_valid_rows(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
start: i64,
end: i64,
agent_pubkey: Option<&str>,
) -> Result<Vec<AgentMetricIndexRow>, String> {
let sql = format!(
"SELECT {ROW_COLUMNS} FROM agent_metric_index
WHERE identity_pubkey = ?1 AND relay_url = ?2 AND parse_status = 'valid'
AND reported_at >= ?3 AND reported_at < ?4
AND (?5 IS NULL OR agent_pubkey = ?5)
ORDER BY reported_at ASC, id ASC"
);
let mut stmt = stmt_prepare(conn, &sql)?;
let rows = stmt
.query_map(
params![identity_pubkey, relay_url, start, end, agent_pubkey],
row_from_sql,
)
.map_err(|e| format!("query load_window_valid_rows: {e}"))?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("read load_window_valid_rows row: {e}"))
}
/// Count INVALID rows whose `event_created_at` falls in `[start, end)`
/// (invalid rows have no trustworthy `reported_at`, so coarse signed
/// `created_at` is the only available time signal), optionally filtered to
/// one agent author.
pub(super) fn count_invalid_rows_in_window(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
start: i64,
end: i64,
agent_pubkey: Option<&str>,
) -> Result<i64, String> {
conn.query_row(
"SELECT COUNT(*) FROM agent_metric_index
WHERE identity_pubkey = ?1 AND relay_url = ?2 AND parse_status = 'invalid'
AND event_created_at >= ?3 AND event_created_at < ?4
AND (?5 IS NULL OR agent_pubkey = ?5)",
params![identity_pubkey, relay_url, start, end, agent_pubkey],
|row| row.get(0),
)
.map_err(|e| format!("count_invalid_rows_in_window: {e}"))
}
/// For the given set of exact `(agent_pubkey, session_id, turn_seq)` keys,
/// load ALL valid rows matching those exact keys with NO `reported_at`
/// restriction (A11). Used both for duplicate-cardinality checks at a
/// sequence and for exact-predecessor baseline lookups — a single probe
/// covers both, since the predecessor key is included in the request set
/// alongside each window row's own key.
///
/// Keys are grouped by `(agent_pubkey, session_id)` and queried with a
/// `turn_seq IN (...)` clause per group (typically few groups per window),
/// served by `idx_agent_metric_session`.
pub(super) fn load_rows_at_exact_keys(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
keys: &std::collections::HashSet<(String, String, u64)>,
) -> Result<Vec<AgentMetricIndexRow>, String> {
use std::collections::HashMap;
// Group by (agent, session) so each group becomes one IN-list query.
let mut groups: HashMap<(String, String), Vec<u64>> = HashMap::new();
for (agent, session, seq) in keys {
groups
.entry((agent.clone(), session.clone()))
.or_default()
.push(*seq);
}
let mut out = Vec::new();
for ((agent, session), seqs) in groups {
let encoded: Vec<String> = seqs.into_iter().map(encode_u64_sortable).collect();
let sql = format!(
"SELECT {ROW_COLUMNS} FROM agent_metric_index
WHERE identity_pubkey = ?1 AND relay_url = ?2 AND parse_status = 'valid'
AND agent_pubkey = ?3 AND session_id = ?4
AND turn_seq IN ({})",
encoded
.iter()
.enumerate()
.map(|(i, _)| format!("?{}", i + 5))
.collect::<Vec<_>>()
.join(",")
);
let mut stmt = stmt_prepare(conn, &sql)?;
let mut bound: Vec<Box<dyn rusqlite::ToSql>> = vec![
Box::new(identity_pubkey.to_owned()),
Box::new(relay_url.to_owned()),
Box::new(agent.clone()),
Box::new(session.clone()),
];
for e in &encoded {
bound.push(Box::new(e.clone()));
}
let refs: Vec<&dyn rusqlite::ToSql> = bound.iter().map(|b| b.as_ref()).collect();
let rows = stmt
.query_map(refs.as_slice(), row_from_sql)
.map_err(|e| format!("query load_rows_at_exact_keys: {e}"))?;
for r in rows {
out.push(r.map_err(|e| format!("read load_rows_at_exact_keys row: {e}"))?);
}
}
Ok(out)
}
/// `hasArchivedEvidence` (A13): does at least one surviving `agent_metric_index`
/// row (either `parse_status`) exist for this author under the active
/// identity+relay, with NO bucket-boundary restriction? Computed after
/// backfill + orphan repair by the caller.
pub(super) fn has_archived_evidence(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
agent_pubkey: &str,
) -> Result<bool, String> {
let exists: Option<i64> = conn
.query_row(
"SELECT 1 FROM agent_metric_index
WHERE identity_pubkey = ?1 AND relay_url = ?2 AND agent_pubkey = ?3
LIMIT 1",
params![identity_pubkey, relay_url, agent_pubkey],
|row| row.get(0),
)
.optional()
.map_err(|e| format!("has_archived_evidence: {e}"))?;
Ok(exists.is_some())
}
fn stmt_prepare<'a>(conn: &'a Connection, sql: &str) -> Result<rusqlite::Statement<'a>, String> {
conn.prepare(sql)
.map_err(|e| format!("prepare failed: {e} — sql: {sql}"))
}
// ── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
#[path = "metric_store_tests.rs"]
mod metric_store_tests;
@@ -0,0 +1,864 @@
//! Unit tests for `archive/metric_store.rs`.
//!
//! Kept in a sibling file so `metric_store.rs` stays under the file-size
//! gate; `#[path]`-included from there.
use super::*;
use crate::archive::store::{self, SCHEMA};
fn in_memory() -> Connection {
let conn = Connection::open_in_memory().unwrap();
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
conn.pragma_update(None, "busy_timeout", 5000).unwrap();
conn.execute_batch(SCHEMA).unwrap();
conn
}
fn valid_payload_json(session_id: &str, seq: u64, timestamp: &str) -> String {
format!(
r#"{{"harness":"goose","model":"claude","channelId":null,"sessionId":"{session_id}","turnId":null,"turnSeq":{seq},"timestamp":"{timestamp}","turn":{{"inputTokens":10,"outputTokens":20,"totalTokens":30,"costUsd":0.01}},"cumulative":{{"inputTokens":100,"outputTokens":200,"totalTokens":300,"costUsd":0.1}},"deltaReliable":true,"stopReason":"end_turn"}}"#
)
}
#[allow(clippy::too_many_arguments)]
fn insert_archived_event(
conn: &Connection,
identity: &str,
relay: &str,
id: &str,
kind: i64,
pubkey: &str,
created_at: i64,
raw_json: &str,
archived_at: i64,
) {
store::upsert_archived_event(
conn,
identity,
relay,
id,
kind,
pubkey,
created_at,
raw_json,
archived_at,
)
.unwrap();
}
// ── u64 sortable encoding ────────────────────────────────────────────────────
#[test]
fn u64_sortable_round_trips_zero_and_max() {
for v in [0u64, 1, 12345, u64::MAX - 1, u64::MAX] {
let encoded = encode_u64_sortable(v);
assert_eq!(encoded.len(), U64_SORTABLE_WIDTH);
assert_eq!(decode_u64_sortable(&encoded), Some(v));
}
}
#[test]
fn u64_sortable_encoding_preserves_numeric_order_across_i64_max() {
let below = i64::MAX as u64;
let above = (i64::MAX as u64) + 1;
let e_below = encode_u64_sortable(below);
let e_above = encode_u64_sortable(above);
assert!(
e_below < e_above,
"lexicographic order must match numeric order"
);
}
#[test]
fn decode_rejects_wrong_width() {
assert_eq!(decode_u64_sortable("123"), None);
assert_eq!(decode_u64_sortable(""), None);
}
// ── from_payload parsing ─────────────────────────────────────────────────────
#[test]
fn from_payload_parses_valid_row() {
let json = valid_payload_json("s1", 7, "2026-07-01T20:11:03.213Z");
let row = AgentMetricIndexRow::from_payload(&json, "eid1", "agent1", 100, 200);
assert_eq!(row.parse_status, ParseStatus::Valid);
assert_eq!(row.session_id, Some("s1".to_string()));
assert_eq!(row.turn_seq, Some(7));
assert_eq!(row.turn_input_tokens, Some(10));
assert_eq!(row.cumulative_input_tokens, Some(100));
assert_eq!(row.model, Some("claude".to_string()));
assert_eq!(row.harness, Some("goose".to_string()));
}
#[test]
fn from_payload_parses_harness_field() {
let json = r#"{"harness":"claude-code","model":"claude-sonnet","timestamp":"2026-07-01T20:11:03Z","turn":{"inputTokens":5,"outputTokens":null,"totalTokens":null,"costUsd":null}}"#;
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
assert_eq!(row.parse_status, ParseStatus::Valid);
assert_eq!(row.harness, Some("claude-code".to_string()));
}
#[test]
fn from_payload_marks_unparseable_json_invalid() {
let row = AgentMetricIndexRow::from_payload("not json", "eid1", "agent1", 100, 200);
assert_eq!(row.parse_status, ParseStatus::Invalid);
assert_eq!(row.turn_input_tokens, None);
}
#[test]
fn from_payload_marks_unparseable_timestamp_invalid() {
let json = r#"{"harness":"goose","timestamp":"not-a-timestamp"}"#;
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
assert_eq!(row.parse_status, ParseStatus::Invalid);
}
#[test]
fn from_payload_marks_cumulative_without_session_seq_invalid() {
// cumulative present but sessionId/turnSeq missing — semantic-invalid per A5.
let json = r#"{"harness":"goose","timestamp":"2026-07-01T20:11:03Z","cumulative":{"inputTokens":1,"outputTokens":null,"totalTokens":null,"costUsd":null}}"#;
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
assert_eq!(row.parse_status, ParseStatus::Invalid);
}
#[test]
fn from_payload_accepts_missing_cumulative_without_session_seq() {
// No cumulative object at all — session/seq are not required.
let json = r#"{"harness":"goose","timestamp":"2026-07-01T20:11:03Z","turn":{"inputTokens":5,"outputTokens":null,"totalTokens":null,"costUsd":null}}"#;
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
assert_eq!(row.parse_status, ParseStatus::Valid);
assert_eq!(row.turn_input_tokens, Some(5));
}
// ── insert / idempotence ──────────────────────────────────────────────────────
#[test]
fn insert_metric_index_row_is_idempotent_on_pk() {
let conn = in_memory();
let row = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"eid1",
"agent1",
100,
200,
);
let first = insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let second = insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
assert!(first);
assert!(!second, "second insert of the same PK must be a no-op");
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(count, 1);
}
#[test]
fn insert_metric_index_row_round_trips_u64_max() {
let conn = in_memory();
let row = AgentMetricIndexRow {
turn_seq: Some(u64::MAX),
turn_input_tokens: Some(u64::MAX),
cumulative_input_tokens: Some(u64::MAX),
..AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"eid1",
"agent1",
100,
200,
)
};
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let loaded = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(loaded.len(), 1);
assert_eq!(loaded[0].turn_seq, Some(u64::MAX));
assert_eq!(loaded[0].turn_input_tokens, Some(u64::MAX));
assert_eq!(loaded[0].cumulative_input_tokens, Some(u64::MAX));
}
#[test]
fn insert_invalid_row_preserves_null_parsed_columns() {
let conn = in_memory();
let row = AgentMetricIndexRow::from_payload("bad json", "eid1", "agent1", 100, 200);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let count = count_invalid_rows_in_window(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(count, 1);
}
// ── Identity/relay isolation ─────────────────────────────────────────────────
#[test]
fn load_window_valid_rows_scoped_to_identity_and_relay() {
let conn = in_memory();
let row_a = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"eidA",
"agent1",
100,
200,
);
let row_b = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"eidB",
"agent1",
100,
200,
);
insert_metric_index_row(&conn, "identityA", "relay1", &row_a).unwrap();
insert_metric_index_row(&conn, "identityB", "relay1", &row_b).unwrap();
let loaded_a = load_window_valid_rows(&conn, "identityA", "relay1", 0, i64::MAX, None).unwrap();
assert_eq!(loaded_a.len(), 1);
assert_eq!(loaded_a[0].id, "eidA");
let loaded_b = load_window_valid_rows(&conn, "identityB", "relay1", 0, i64::MAX, None).unwrap();
assert_eq!(loaded_b.len(), 1);
assert_eq!(loaded_b[0].id, "eidB");
}
#[test]
fn load_window_valid_rows_filters_by_agent_pubkey() {
let conn = in_memory();
let row_a = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"eidA",
"agentA",
100,
200,
);
let row_b = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"eidB",
"agentB",
100,
200,
);
insert_metric_index_row(&conn, "id", "relay", &row_a).unwrap();
insert_metric_index_row(&conn, "id", "relay", &row_b).unwrap();
let loaded = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, Some("agentA")).unwrap();
assert_eq!(loaded.len(), 1);
assert_eq!(loaded[0].agent_pubkey, "agentA");
}
#[test]
fn load_window_valid_rows_excludes_out_of_window_reported_at() {
let conn = in_memory();
let in_window = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-01-02T00:00:00Z"),
"eid_in",
"agent1",
0,
0,
);
let out_of_window = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 2, "2020-01-01T00:00:00Z"),
"eid_out",
"agent1",
0,
0,
);
insert_metric_index_row(&conn, "id", "relay", &in_window).unwrap();
insert_metric_index_row(&conn, "id", "relay", &out_of_window).unwrap();
let start = chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")
.unwrap()
.timestamp();
let end = chrono::DateTime::parse_from_rfc3339("2026-01-03T00:00:00Z")
.unwrap()
.timestamp();
let loaded = load_window_valid_rows(&conn, "id", "relay", start, end, None).unwrap();
assert_eq!(loaded.len(), 1);
assert_eq!(loaded[0].id, "eid_in");
}
// ── load_rows_at_exact_keys ───────────────────────────────────────────────────
#[test]
fn load_rows_at_exact_keys_matches_multiple_groups() {
let conn = in_memory();
let r1 = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 5, "2026-07-01T00:00:00Z"),
"e1",
"agent1",
0,
0,
);
let r2 = AgentMetricIndexRow::from_payload(
&valid_payload_json("s2", 9, "2026-07-01T00:00:00Z"),
"e2",
"agent1",
0,
0,
);
let r3_not_requested = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 99, "2026-07-01T00:00:00Z"),
"e3",
"agent1",
0,
0,
);
insert_metric_index_row(&conn, "id", "relay", &r1).unwrap();
insert_metric_index_row(&conn, "id", "relay", &r2).unwrap();
insert_metric_index_row(&conn, "id", "relay", &r3_not_requested).unwrap();
let mut keys = std::collections::HashSet::new();
keys.insert(("agent1".to_string(), "s1".to_string(), 5u64));
keys.insert(("agent1".to_string(), "s2".to_string(), 9u64));
let loaded = load_rows_at_exact_keys(&conn, "id", "relay", &keys).unwrap();
let mut ids: Vec<&str> = loaded.iter().map(|r| r.id.as_str()).collect();
ids.sort();
assert_eq!(ids, vec!["e1", "e2"]);
}
#[test]
fn load_rows_at_exact_keys_returns_all_duplicates_at_one_key() {
let conn = in_memory();
let dup_a = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 5, "2026-07-01T00:00:00Z"),
"eA",
"agent1",
0,
0,
);
let dup_b = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 5, "2026-07-01T00:00:01Z"),
"eB",
"agent1",
0,
1,
);
insert_metric_index_row(&conn, "id", "relay", &dup_a).unwrap();
insert_metric_index_row(&conn, "id", "relay", &dup_b).unwrap();
let mut keys = std::collections::HashSet::new();
keys.insert(("agent1".to_string(), "s1".to_string(), 5u64));
let loaded = load_rows_at_exact_keys(&conn, "id", "relay", &keys).unwrap();
assert_eq!(loaded.len(), 2);
}
// ── has_archived_evidence ─────────────────────────────────────────────────────
#[test]
fn has_archived_evidence_true_for_either_parse_status() {
let conn = in_memory();
let invalid_row = AgentMetricIndexRow::from_payload("bad", "eid1", "agentX", 0, 0);
insert_metric_index_row(&conn, "id", "relay", &invalid_row).unwrap();
assert!(has_archived_evidence(&conn, "id", "relay", "agentX").unwrap());
assert!(!has_archived_evidence(&conn, "id", "relay", "agentY").unwrap());
}
#[test]
fn has_archived_evidence_ignores_bucket_boundaries() {
let conn = in_memory();
// A very old row (outside any realistic window) still counts as evidence.
let old_row = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "1999-01-01T00:00:00Z"),
"eid1",
"agentX",
0,
0,
);
insert_metric_index_row(&conn, "id", "relay", &old_row).unwrap();
assert!(has_archived_evidence(&conn, "id", "relay", "agentX").unwrap());
}
// ── Backfill ──────────────────────────────────────────────────────────────────
#[test]
fn backfill_indexes_existing_unindexed_kind_44200_rows() {
let conn = in_memory();
let json = valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, &json, 200,
);
let indexed = backfill_agent_metric_index(&conn, "id", "relay").unwrap();
assert_eq!(indexed, 1);
let loaded = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(loaded.len(), 1);
assert_eq!(loaded[0].id, "eid1");
}
#[test]
fn backfill_is_idempotent_anti_join() {
let conn = in_memory();
let json = valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, &json, 200,
);
backfill_agent_metric_index(&conn, "id", "relay").unwrap();
let second_run = backfill_agent_metric_index(&conn, "id", "relay").unwrap();
assert_eq!(second_run, 0, "second backfill run must index nothing new");
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(count, 1);
}
#[test]
fn backfill_processes_chunks_larger_than_500_rows() {
let conn = in_memory();
// 501 rows exercises the CHUNK_SIZE=500 boundary.
for i in 0..501 {
let json = valid_payload_json(&format!("s{i}"), 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn,
"id",
"relay",
&format!("eid{i}"),
44200,
"agent1",
100 + i as i64,
&json,
200,
);
}
let indexed = backfill_agent_metric_index(&conn, "id", "relay").unwrap();
assert_eq!(indexed, 501);
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(count, 501);
}
#[test]
fn backfill_ignores_non_44200_rows() {
let conn = in_memory();
insert_archived_event(&conn, "id", "relay", "eid1", 1, "author1", 100, "{}", 200);
let indexed = backfill_agent_metric_index(&conn, "id", "relay").unwrap();
assert_eq!(indexed, 0);
}
// ── GC / orphan repair ────────────────────────────────────────────────────────
#[test]
fn delete_orphaned_metric_index_rows_removes_rows_with_no_canonical_event() {
let conn = in_memory();
// Planted orphan: index row with no matching archived_events row.
let orphan = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"orphan_id",
"agent1",
0,
0,
);
insert_metric_index_row(&conn, "id", "relay", &orphan).unwrap();
let deleted = delete_orphaned_metric_index_rows(&conn, "id", "relay").unwrap();
assert_eq!(deleted, 1);
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(count, 0);
}
#[test]
fn delete_orphaned_metric_index_rows_preserves_rows_with_canonical_event() {
let conn = in_memory();
let json = valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, &json, 200,
);
let row = AgentMetricIndexRow::from_payload(&json, "eid1", "agent1", 100, 200);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let deleted = delete_orphaned_metric_index_rows(&conn, "id", "relay").unwrap();
assert_eq!(deleted, 0);
}
#[test]
fn repair_orphaned_metric_index_rows_self_heals_planted_orphan_before_read() {
let conn = in_memory();
let orphan = AgentMetricIndexRow::from_payload(
&valid_payload_json("s1", 1, "2026-07-01T00:00:00Z"),
"orphan_id",
"agent1",
0,
0,
);
insert_metric_index_row(&conn, "id", "relay", &orphan).unwrap();
repair_orphaned_metric_index_rows(&conn, "id", "relay").unwrap();
let loaded = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert!(
loaded.is_empty(),
"planted orphan must never be reported after repair"
);
}
#[test]
fn gc_orphaned_events_cascades_to_metric_index_atomically() {
let conn = in_memory();
let json = valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, &json, 200,
);
let row = AgentMetricIndexRow::from_payload(&json, "eid1", "agent1", 100, 200);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
// Remove the last scope row so the event becomes orphaned, then GC.
// (No scope row was ever added in this test, so the event is already
// orphaned by construction — gc_orphaned_events should delete both the
// canonical row and its index row in one transaction.)
store::gc_orphaned_events(&conn, "id", "relay").unwrap();
let event_count: i64 = conn
.query_row("SELECT COUNT(*) FROM archived_events", [], |r| r.get(0))
.unwrap();
let index_count: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(event_count, 0);
assert_eq!(
index_count, 0,
"index row must not outlive its canonical event"
);
}
// ── EXPLAIN QUERY PLAN index assertions (A7) ─────────────────────────────────
fn query_plan(conn: &Connection, sql: &str, params: &[&dyn rusqlite::ToSql]) -> String {
let explain_sql = format!("EXPLAIN QUERY PLAN {sql}");
let mut stmt = conn.prepare(&explain_sql).unwrap();
let mut rows = stmt.query(params).unwrap();
let mut plan = String::new();
while let Some(row) = rows.next().unwrap() {
let detail: String = row.get(3).unwrap();
plan.push_str(&detail);
plan.push('\n');
}
plan
}
#[test]
fn backfill_anti_join_uses_partial_index() {
let conn = in_memory();
let plan = query_plan(
&conn,
"SELECT ae.id FROM archived_events ae
WHERE ae.identity_pubkey = ?1 AND ae.relay_url = ?2 AND ae.kind = 44200
AND ae.id NOT IN (SELECT id FROM agent_metric_index WHERE identity_pubkey = ?1 AND relay_url = ?2)",
&[&"id", &"relay"],
);
assert!(
plan.contains("idx_archived_events_agent_metric"),
"backfill anti-join must use the partial index, plan was:\n{plan}"
);
}
#[test]
fn window_scan_uses_reported_index() {
let conn = in_memory();
let plan = query_plan(
&conn,
"SELECT * FROM agent_metric_index
WHERE identity_pubkey = ?1 AND relay_url = ?2 AND parse_status = 'valid'
AND reported_at >= ?3 AND reported_at < ?4",
&[&"id", &"relay", &0i64, &100i64],
);
assert!(
plan.contains("idx_agent_metric_reported"),
"window scan must use the reported-time index, plan was:\n{plan}"
);
}
#[test]
fn predecessor_lookup_uses_session_index() {
let conn = in_memory();
let plan = query_plan(
&conn,
"SELECT * FROM agent_metric_index
WHERE identity_pubkey = ?1 AND relay_url = ?2 AND parse_status = 'valid'
AND agent_pubkey = ?3 AND session_id = ?4 AND turn_seq IN (?5)",
&[&"id", &"relay", &"agent1", &"s1", &"00000000000000000005"],
);
assert!(
plan.contains("idx_agent_metric_session"),
"predecessor lookup must use the session index, plan was:\n{plan}"
);
}
// ── Migration: old-shape rows get harness populated on rebuild ────────────────
/// Simulate a pre-harness archive row by inserting an archived_event and an
/// index row with harness = NULL (as the old schema would have stored it),
/// then running the backfill rebuild path to verify harness is populated.
#[test]
fn migration_old_shape_rows_get_harness_populated_after_rebuild() {
let conn = in_memory();
let json = valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
// Seed an archived event.
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, &json, 200,
);
// Seed an index row with harness explicitly NULL (simulates pre-migration row).
conn.execute(
"INSERT INTO agent_metric_index
(identity_pubkey, relay_url, id, agent_pubkey, event_created_at,
archived_at, reported_at, session_id, turn_seq, harness, model,
delta_reliable, turn_input_tokens, turn_output_tokens,
turn_total_tokens, turn_cost_usd, cumulative_input_tokens,
cumulative_output_tokens, cumulative_total_tokens,
cumulative_cost_usd, parse_status)
VALUES ('id','relay','eid1','agent1',100,200,1751414400000,
's1','00000000000000000001',NULL,'claude',
1,'00000000000000000010','00000000000000000020',
'00000000000000000030',0.01,
'00000000000000000100','00000000000000000200',
'00000000000000000300',0.1,'valid')",
[],
)
.unwrap();
// Verify the row is there with NULL harness.
let harness_before: Option<String> = conn
.query_row(
"SELECT harness FROM agent_metric_index WHERE id = 'eid1'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
harness_before, None,
"old-shape row should start with NULL harness"
);
// Simulate the migration: delete index rows and re-run backfill.
conn.execute_batch("DELETE FROM agent_metric_index")
.unwrap();
backfill_agent_metric_index(&conn, "id", "relay").unwrap();
// The rebuilt row must have harness populated.
let harness_after: Option<String> = conn
.query_row(
"SELECT harness FROM agent_metric_index WHERE id = 'eid1'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
harness_after,
Some("goose".to_string()),
"rebuilt row must have harness populated from raw_json"
);
}
// ── cache_read_tokens parse and persist ───────────────────────────────────────
/// A payload carrying `cacheReadTokens` in both `turn` and `cumulative` must
/// round-trip through `from_payload` → `insert_metric_index_row` → `row_from_sql`.
#[test]
fn from_payload_parses_cache_read_tokens_when_present() {
let json = r#"{"harness":"goose","model":"claude","channelId":null,"sessionId":"s1","turnId":null,"turnSeq":1,"timestamp":"2026-07-01T00:00:00Z","turn":{"inputTokens":100,"outputTokens":20,"totalTokens":120,"costUsd":0.01,"cacheReadTokens":80},"cumulative":{"inputTokens":500,"outputTokens":100,"totalTokens":600,"costUsd":0.05,"cacheReadTokens":400},"deltaReliable":true,"stopReason":"end_turn"}"#;
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
assert_eq!(
row.turn_cache_read_tokens,
Some(80),
"turn_cache_read_tokens must be parsed from turn.cacheReadTokens"
);
assert_eq!(
row.cumulative_cache_read_tokens,
Some(400),
"cumulative_cache_read_tokens must be parsed from cumulative.cacheReadTokens"
);
}
/// A payload WITHOUT `cacheReadTokens` must produce `None` in both fields.
#[test]
fn from_payload_cache_read_tokens_none_when_absent() {
let json = valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
let row = AgentMetricIndexRow::from_payload(&json, "eid1", "agent1", 100, 200);
assert_eq!(
row.turn_cache_read_tokens, None,
"turn_cache_read_tokens must be None when cacheReadTokens absent"
);
assert_eq!(
row.cumulative_cache_read_tokens, None,
"cumulative_cache_read_tokens must be None when cacheReadTokens absent"
);
}
/// `insert_metric_index_row` persists nonzero cache columns; `row_from_sql`
/// (via `load_window_valid_rows`) reads them back correctly.
#[test]
fn insert_and_load_round_trips_cache_read_tokens() {
let conn = in_memory();
let json = r#"{"harness":"goose","model":"claude","channelId":null,"sessionId":"s1","turnId":null,"turnSeq":1,"timestamp":"2026-07-01T00:00:00Z","turn":{"inputTokens":100,"outputTokens":20,"totalTokens":120,"costUsd":0.01,"cacheReadTokens":80},"cumulative":{"inputTokens":500,"outputTokens":100,"totalTokens":600,"costUsd":0.05,"cacheReadTokens":400},"deltaReliable":true,"stopReason":"end_turn"}"#;
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, json, 200,
);
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
// Use load_window_valid_rows to exercise the full read path.
let rows = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(
rows[0].turn_cache_read_tokens,
Some(80),
"turn_cache_read_tokens must survive insert + load round trip"
);
assert_eq!(
rows[0].cumulative_cache_read_tokens,
Some(400),
"cumulative_cache_read_tokens must survive insert + load round trip"
);
}
/// A row inserted without cache tokens loads back with `None` for both fields —
/// columns default to NULL and the decoder handles NULL correctly.
#[test]
fn insert_and_load_cache_read_tokens_null_when_absent() {
let conn = in_memory();
let json = &valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn, "id", "relay", "eid1", 44200, "agent1", 100, json, 200,
);
let row = AgentMetricIndexRow::from_payload(json, "eid1", "agent1", 100, 200);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let rows = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(
rows[0].turn_cache_read_tokens, None,
"turn_cache_read_tokens must be None when not present in payload"
);
assert_eq!(
rows[0].cumulative_cache_read_tokens, None,
"cumulative_cache_read_tokens must be None when not present in payload"
);
}
// ── M3 cache-write and pricing columns ────────────────────────────────────────
/// M3 positive full-row round trip: a payload carrying nonzero cache-write
/// tokens and a pricingIdentity must survive `from_payload` → `insert` → `load`
/// with all five M3 columns intact.
#[test]
fn m3_insert_and_load_round_trips_cache_write_and_pricing() {
let conn = in_memory();
let json = r#"{"harness":"buzz-agent","model":"claude-opus-4-5","channelId":null,"sessionId":"s1","turnId":null,"turnSeq":1,"timestamp":"2026-07-01T00:00:00Z","turn":{"inputTokens":200,"outputTokens":50,"totalTokens":250,"costUsd":0.02,"cacheReadTokens":120,"cacheWriteTokens":30},"cumulative":{"inputTokens":600,"outputTokens":150,"totalTokens":750,"costUsd":0.06,"cacheReadTokens":360,"cacheWriteTokens":90},"deltaReliable":true,"stopReason":"end_turn","pricingIdentity":{"authority":"api.anthropic.com","model":"claude-opus-4-5"}}"#;
insert_archived_event(
&conn,
"id",
"relay",
"eid-m3-pos",
44200,
"agent1",
100,
json,
200,
);
let row = AgentMetricIndexRow::from_payload(json, "eid-m3-pos", "agent1", 100, 200);
// Verify from_payload extracted the M3 fields.
assert_eq!(
row.turn_cache_write_tokens,
Some(30),
"from_payload: turn_cache_write_tokens must parse cacheWriteTokens"
);
assert_eq!(
row.cumulative_cache_write_tokens,
Some(90),
"from_payload: cumulative_cache_write_tokens must parse cumulative.cacheWriteTokens"
);
assert_eq!(
row.pricing_authority.as_deref(),
Some("api.anthropic.com"),
"from_payload: pricing_authority must parse pricingIdentity.authority"
);
assert_eq!(
row.pricing_model.as_deref(),
Some("claude-opus-4-5"),
"from_payload: pricing_model must parse pricingIdentity.model"
);
assert!(
row.pricing_cache_class.is_none(),
"from_payload: pricing_cache_class must be None when absent"
);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let rows = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(rows.len(), 1);
let loaded = &rows[0];
assert_eq!(
loaded.turn_cache_write_tokens,
Some(30),
"round-trip: turn_cache_write_tokens must survive insert + load"
);
assert_eq!(
loaded.cumulative_cache_write_tokens,
Some(90),
"round-trip: cumulative_cache_write_tokens must survive insert + load"
);
assert_eq!(
loaded.pricing_authority.as_deref(),
Some("api.anthropic.com"),
"round-trip: pricing_authority must survive insert + load"
);
assert_eq!(
loaded.pricing_model.as_deref(),
Some("claude-opus-4-5"),
"round-trip: pricing_model must survive insert + load"
);
assert!(
loaded.pricing_cache_class.is_none(),
"round-trip: pricing_cache_class must be None (absent in payload)"
);
}
/// M3 omission/explicit-zero: a payload with no cache-write and no pricingIdentity
/// must produce NULL for all five M3 columns — never inferred zeros.
#[test]
fn m3_insert_and_load_m3_columns_null_when_absent() {
let conn = in_memory();
// Standard payload without cacheWriteTokens or pricingIdentity.
let json = &valid_payload_json("s1", 1, "2026-07-01T00:00:00Z");
insert_archived_event(
&conn,
"id",
"relay",
"eid-m3-null",
44200,
"agent1",
100,
json,
200,
);
let row = AgentMetricIndexRow::from_payload(json, "eid-m3-null", "agent1", 100, 200);
insert_metric_index_row(&conn, "id", "relay", &row).unwrap();
let rows = load_window_valid_rows(&conn, "id", "relay", 0, i64::MAX, None).unwrap();
assert_eq!(rows.len(), 1);
let loaded = &rows[0];
assert_eq!(
loaded.turn_cache_write_tokens, None,
"M3: turn_cache_write_tokens must be None when absent in payload"
);
assert_eq!(
loaded.cumulative_cache_write_tokens, None,
"M3: cumulative_cache_write_tokens must be None when absent in payload"
);
assert_eq!(
loaded.pricing_authority, None,
"M3: pricing_authority must be None when pricingIdentity absent"
);
assert_eq!(
loaded.pricing_model, None,
"M3: pricing_model must be None when pricingIdentity absent"
);
assert_eq!(
loaded.pricing_cache_class, None,
"M3: pricing_cache_class must be None when pricingIdentity absent"
);
}
+101
View File
@@ -17,8 +17,11 @@
//! validation (sig/id + kind + p-tag + agent tag + frame=telemetry + author
//! == agent) is applied fail-closed.
mod agent_usage;
mod metric_store;
mod pipeline;
pub mod store;
mod store_migrations;
use pipeline::{commit_archive, plan_archive, query_buckets};
@@ -116,9 +119,17 @@ pub struct MatchedScope {
/// Result of a batch archive call.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ArchiveBatchResult {
/// Events successfully written to the store (event + scope rows).
pub persisted: u32,
/// Newly-indexed `agent_metric_index` rows (valid or invalid) written in
/// this call — the count the frontend uses to decide whether an
/// agent-usage query needs to be invalidated. Distinct from `persisted`:
/// a re-ingested duplicate can be `persisted` (event/scope rows upserted,
/// no-op) without incrementing this counter, since the index row for
/// that id was already inserted by whichever earlier batch first saw it.
pub persisted_agent_metrics: u32,
/// Events dropped due to access denial or invalid payload (not an error).
pub dropped: u32,
}
@@ -674,6 +685,96 @@ pub async fn read_archived_events(
.await
}
// ── get_agent_usage_series ───────────────────────────────────────────────────
/// Compute the locally archived NIP-AM usage series for one identity/relay.
///
/// Synchronous SQLite core of [`get_agent_usage_series`], split out so tests
/// can drive it directly against an in-memory `Connection` without a Tauri
/// `AppState`. Backfills any unindexed kind-44200 rows, repairs orphaned
/// index rows (defense in depth alongside A6's GC-time cascade), validates
/// the request, loads the window + exact-key probe rows, and hands them to
/// the pure `agent_usage::compute_series`.
fn agent_usage_series(
conn: &Connection,
identity_pk: &str,
relay_url: &str,
request: &agent_usage::AgentUsageSeriesRequest,
) -> Result<agent_usage::AgentUsageSeries, String> {
// Fail-closed request validation happens before any SQLite work.
let agent_pubkey = agent_usage::validate_request(request)?;
metric_store::backfill_agent_metric_index(conn, identity_pk, relay_url)?;
metric_store::repair_orphaned_metric_index_rows(conn, identity_pk, relay_url)?;
let collection_enabled = {
let kinds_json =
store::get_subscription_kinds(conn, identity_pk, relay_url, "owner_p", identity_pk)?
.unwrap_or_else(|| "[]".to_string());
let kinds: Vec<u64> = serde_json::from_str(&kinds_json).unwrap_or_default();
kinds.contains(&(KIND_AGENT_TURN_METRIC as u64))
};
// A13: only meaningful when the request is scoped to one author.
let has_archived_evidence = match &agent_pubkey {
None => None,
Some(pk) => Some(metric_store::has_archived_evidence(
conn,
identity_pk,
relay_url,
pk,
)?),
};
let start = request.bucket_boundaries[0];
let end = *request
.bucket_boundaries
.last()
.expect("validate_request already rejected fewer than 2 boundaries");
let window_rows = metric_store::load_window_valid_rows(
conn,
identity_pk,
relay_url,
start,
end,
agent_pubkey.as_deref(),
)?;
let invalid_report_count = metric_store::count_invalid_rows_in_window(
conn,
identity_pk,
relay_url,
start,
end,
agent_pubkey.as_deref(),
)?;
let probe_keys = agent_usage::window_probe_keys(&window_rows);
let probe_rows =
metric_store::load_rows_at_exact_keys(conn, identity_pk, relay_url, &probe_keys)?;
Ok(agent_usage::compute_series(
&window_rows,
&probe_rows,
invalid_report_count,
&request.bucket_boundaries,
has_archived_evidence,
collection_enabled,
))
}
/// Compute the locally archived NIP-AM usage series for the active identity
/// + relay (Rev 3 frozen contract). See [`agent_usage_series`] for the logic.
#[tauri::command]
pub async fn get_agent_usage_series(
state: State<'_, AppState>,
request: agent_usage::AgentUsageSeriesRequest,
) -> Result<agent_usage::AgentUsageSeries, String> {
let identity_pk = identity_pubkey(&state)?;
let relay_url = relay_ws_url_with_override(&state);
run_archive_db_task(move |conn| agent_usage_series(conn, &identity_pk, &relay_url, &request))
.await
}
// ── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
@@ -0,0 +1,411 @@
//! Kind-44200 (NIP-AM agent turn metric) archive and `get_agent_usage_series`
//! integration tests for `archive/mod.rs`.
//!
//! Kept in a sibling file so `mod_tests.rs` stays under the 1000-line gate;
//! `#[path]`-included from there so the shared fixtures (`in_memory`,
//! `add_sub`, `candidate`, `make_observer_frame`, `run_batch_sync_with_keys`)
//! stay private to `mod_tests`.
use super::*;
// ── Kind-44200 agent-turn-metric archive tests ───────────────────────────
fn make_turn_metric_event(owner_keys: &Keys, agent_keys: &Keys) -> Event {
use buzz_core_pkg::agent_turn_metric::{
encrypt_agent_turn_metric, AgentTurnMetricPayload, TokenCounts,
};
let owner_pk = owner_keys.public_key().to_hex();
let payload = AgentTurnMetricPayload {
harness: "test-harness".to_string(),
model: Some("test-model".to_string()),
channel_id: None,
session_id: Some("sess-1".to_string()),
turn_id: Some("turn-1".to_string()),
turn_seq: Some(1),
timestamp: "2026-07-01T00:00:00Z".to_string(),
turn: Some(TokenCounts {
input_tokens: Some(100),
output_tokens: Some(50),
total_tokens: Some(150),
cost_usd: Some(0.001),
cache_read_tokens: None,
cache_write_tokens: None,
}),
cumulative: None,
delta_reliable: true,
stop_reason: None,
pricing_identity: None,
};
let ciphertext =
encrypt_agent_turn_metric(agent_keys, &owner_keys.public_key(), &payload).unwrap();
let tags = vec![
Tag::parse(["p", &owner_pk]).unwrap(),
Tag::parse(["agent", &agent_keys.public_key().to_hex()]).unwrap(),
];
EventBuilder::new(Kind::Custom(44200), &ciphertext)
.tags(tags)
.sign_with_keys(agent_keys)
.unwrap()
}
/// A kind-44200 event with `owner_p` scope must route to the persistent
/// (relay-query) path, NOT the ephemeral path.
#[test]
fn test_owner_p_44200_routes_to_persistent_path() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
// Subscription for kind 44200 under owner_p.
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let plan = plan_archive(vec![cand], &owner_pk, relay_url, &conn).unwrap();
// Must be in persistent buckets, NOT ephemeral list.
assert_eq!(plan.buckets.len(), 1, "kind-44200 must land in a bucket");
assert_eq!(
plan.ephemeral.len(),
0,
"kind-44200 must NOT be on the ephemeral path"
);
assert_eq!(
plan.buckets[0].scope_type_str, "owner_p",
"bucket scope_type must be owner_p"
);
}
/// A kind-24200 event with `owner_p` scope must still route to ephemeral.
#[test]
fn test_owner_p_24200_still_routes_to_ephemeral() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[24200]");
let ev = make_observer_frame(&owner_keys, &agent_keys, OBSERVER_FRAME_TELEMETRY);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let plan = plan_archive(vec![cand], &owner_pk, relay_url, &conn).unwrap();
assert_eq!(
plan.buckets.len(),
0,
"kind-24200 must NOT land in a bucket"
);
assert_eq!(
plan.ephemeral.len(),
1,
"kind-24200 must be on the ephemeral path"
);
}
/// Decrypt success: plaintext payload JSON is stored, not raw ciphertext.
#[test]
fn test_turn_metric_decrypt_success_stores_plaintext() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let result = run_batch_sync_with_keys(
vec![cand],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&owner_keys,
);
assert_eq!(result.persisted, 1, "event must be persisted");
assert_eq!(result.dropped, 0, "no drops on successful decrypt");
assert_eq!(
result.persisted_agent_metrics, 1,
"one newly-indexed agent_metric_index row on first ingest"
);
// The stored raw_json must be plaintext JSON, not NIP-44 ciphertext.
let raw_json: String = conn
.query_row("SELECT raw_json FROM archived_events", [], |r| r.get(0))
.unwrap();
// Plaintext JSON should be a valid object with "harness" key.
let parsed: serde_json::Value =
serde_json::from_str(&raw_json).expect("stored raw_json must be valid JSON");
assert_eq!(
parsed["harness"], "test-harness",
"stored plaintext must decode to AgentTurnMetricPayload"
);
// Sanity: must NOT be the original NIP-44 ciphertext (which is not JSON).
assert_ne!(
raw_json, ev.content,
"stored content must differ from original ciphertext"
);
}
/// Decrypt fail: event is dropped, nothing written to the store (fail-closed).
#[test]
fn test_turn_metric_decrypt_fail_drops_fail_closed() {
let conn = in_memory();
let owner_keys = Keys::generate();
let wrong_keys = Keys::generate(); // wrong owner key — decrypt will fail
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
// Register subscription under owner_pk so the event passes plan-phase,
// but use `wrong_keys` in commit so decrypt fails.
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let result = run_batch_sync_with_keys(
vec![cand],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&wrong_keys, // wrong key → decrypt fails
);
assert_eq!(
result.persisted, 0,
"decrypt failure must not persist the event"
);
assert_eq!(result.dropped, 1, "decrypt failure must count as dropped");
let event_count: i64 = conn
.query_row("SELECT COUNT(*) FROM archived_events", [], |r| r.get(0))
.unwrap();
assert_eq!(
event_count, 0,
"no rows must be written to archived_events on decrypt failure"
);
}
/// Re-ingesting a batch containing an already-archived kind-44200 event must
/// no-op the metric index insert: `persisted` still counts the (idempotent)
/// event/scope upsert, but `persisted_agent_metrics` must be 0 for the
/// duplicate — the row was already indexed by the first ingest (A5: this is
/// exactly the signal the frontend uses to skip a redundant query
/// invalidation).
#[test]
fn test_reingest_of_same_metric_event_does_not_double_count_persisted_agent_metrics() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand1 = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let first = run_batch_sync_with_keys(
vec![cand1],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&owner_keys,
);
assert_eq!(first.persisted, 1);
assert_eq!(first.persisted_agent_metrics, 1);
// Same event re-ingested in a second batch (e.g. relay redelivery).
let cand2 = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let second = run_batch_sync_with_keys(
vec![cand2],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&owner_keys,
);
assert_eq!(
second.persisted, 1,
"re-ingest of a duplicate is still an accepted (idempotent) write"
);
assert_eq!(
second.persisted_agent_metrics, 0,
"re-ingest must NOT double-count the metric index row"
);
let index_count: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(index_count, 1, "exactly one index row must exist total");
}
// ── get_agent_usage_series integration ──────────────────────────────────────
//
// Exercises `agent_usage_series` (the sync core `get_agent_usage_series`
// delegates to) end to end: ingest a real encrypted turn-metric event
// through the full archive pipeline, then read it back through the command
// core, proving backfill/indexing, collection-enabled detection, and the
// pure accounting ladder are wired together correctly — not just each in
// isolation.
/// A freshly ingested single turn-metric event surfaces in the series with
/// its direct (delta-reliable, no-baseline) token counts, and
/// `collectionEnabled` reflects the active owner_p/44200 subscription.
#[test]
fn test_agent_usage_series_surfaces_freshly_ingested_event() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let agent_pk = agent_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let batch = run_batch_sync_with_keys(
vec![cand],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&owner_keys,
);
assert_eq!(batch.persisted_agent_metrics, 1, "event must be indexed");
// `make_turn_metric_event`'s payload timestamp is 2026-07-01T00:00:00Z.
const EVENT_DAY_START: i64 = 1_782_864_000;
let boundaries: Vec<i64> = (0..=7).map(|i| EVENT_DAY_START + i * 86_400).collect();
let request = agent_usage::AgentUsageSeriesRequest {
bucket_boundaries: boundaries,
agent_pubkey: None,
};
let series = agent_usage_series(&conn, &owner_pk, relay_url, &request).unwrap();
assert!(
series.collection_enabled,
"owner_p subscription includes kind 44200"
);
assert_eq!(series.coverage.report_count, 1);
assert_eq!(series.coverage.invalid_report_count, 0);
assert_eq!(series.agents.len(), 1, "exactly one agent reported usage");
let agent = &series.agents[0];
assert_eq!(agent.agent_pubkey, agent_pk);
// No baseline row exists, so the ladder falls back to the direct
// (delta-reliable) turn values from the payload: 100/50/150.
assert_eq!(agent.usage.input_tokens.value.as_deref(), Some("100"));
assert_eq!(agent.usage.output_tokens.value.as_deref(), Some("50"));
assert_eq!(agent.usage.total_tokens.value.as_deref(), Some("150"));
assert!(!agent.usage.input_tokens.incomplete);
assert_eq!(
series.has_archived_evidence, None,
"no agentPubkey filter was supplied"
);
}
/// Filtering by `agentPubkey` scopes both the returned series and
/// `hasArchivedEvidence` (A13) to that one author; an unrelated agent's
/// events must not leak into either.
#[test]
fn test_agent_usage_series_filters_by_agent_pubkey_and_sets_has_archived_evidence() {
let conn = in_memory();
let owner_keys = Keys::generate();
let target_agent = Keys::generate();
let other_agent = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let target_pk = target_agent.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let target_ev = make_turn_metric_event(&owner_keys, &target_agent);
let other_ev = make_turn_metric_event(&owner_keys, &other_agent);
let cands = vec![
candidate(&target_ev, ScopeType::OwnerP, &owner_pk),
candidate(&other_ev, ScopeType::OwnerP, &owner_pk),
];
let batch = run_batch_sync_with_keys(
cands,
&owner_pk,
relay_url,
&conn,
vec![target_ev.clone(), other_ev.clone()],
&owner_keys,
);
assert_eq!(batch.persisted_agent_metrics, 2);
const EVENT_DAY_START: i64 = 1_782_864_000;
let boundaries: Vec<i64> = (0..=7).map(|i| EVENT_DAY_START + i * 86_400).collect();
let request = agent_usage::AgentUsageSeriesRequest {
bucket_boundaries: boundaries,
agent_pubkey: Some(target_pk.clone()),
};
let series = agent_usage_series(&conn, &owner_pk, relay_url, &request).unwrap();
assert_eq!(
series.agents.len(),
1,
"only the filtered agent's usage must be returned"
);
assert_eq!(series.agents[0].agent_pubkey, target_pk);
assert_eq!(
series.has_archived_evidence,
Some(true),
"A13: evidence exists for the filtered author"
);
}
/// An unindexed pre-existing kind-44200 row (simulating an event archived
/// by a prior build before `agent_metric_index` existed) is picked up by
/// the command's backfill step before the window is read.
#[test]
fn test_agent_usage_series_backfills_unindexed_row_before_reading() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
// Insert directly into `archived_events`, bypassing `commit_archive`, so
// no `agent_metric_index` row is created — the exact state a fresh
// backfill must repair.
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let plaintext = r#"{"harness":"test-harness","model":"test-model","sessionId":"sess-1","turnId":"turn-1","turnSeq":1,"timestamp":"2026-07-01T00:00:00Z","turn":{"inputTokens":100,"outputTokens":50,"totalTokens":150,"costUsd":0.001},"deltaReliable":true}"#;
store::upsert_archived_event(
&conn,
&owner_pk,
relay_url,
&ev.id.to_hex(),
44200,
&agent_keys.public_key().to_hex(),
ev.created_at.as_secs() as i64,
plaintext,
0,
)
.unwrap();
let index_count_before: i64 = conn
.query_row("SELECT COUNT(*) FROM agent_metric_index", [], |r| r.get(0))
.unwrap();
assert_eq!(index_count_before, 0, "no index row before backfill");
const EVENT_DAY_START: i64 = 1_782_864_000;
let boundaries: Vec<i64> = (0..=7).map(|i| EVENT_DAY_START + i * 86_400).collect();
let request = agent_usage::AgentUsageSeriesRequest {
bucket_boundaries: boundaries,
agent_pubkey: None,
};
let series = agent_usage_series(&conn, &owner_pk, relay_url, &request).unwrap();
assert_eq!(
series.coverage.report_count, 1,
"backfill must index the pre-existing row before the window read"
);
}
+5 -176
View File
@@ -621,182 +621,11 @@ fn test_commit_archive_rolls_back_when_scope_write_would_fail() {
);
}
// ── Kind-44200 agent-turn-metric archive tests ───────────────────────────
fn make_turn_metric_event(owner_keys: &Keys, agent_keys: &Keys) -> Event {
use buzz_core_pkg::agent_turn_metric::{
encrypt_agent_turn_metric, AgentTurnMetricPayload, TokenCounts,
};
let owner_pk = owner_keys.public_key().to_hex();
let payload = AgentTurnMetricPayload {
harness: "test-harness".to_string(),
model: Some("test-model".to_string()),
channel_id: None,
session_id: Some("sess-1".to_string()),
turn_id: Some("turn-1".to_string()),
turn_seq: Some(1),
timestamp: "2026-07-01T00:00:00Z".to_string(),
turn: Some(TokenCounts {
input_tokens: Some(100),
output_tokens: Some(50),
total_tokens: Some(150),
cost_usd: Some(0.001),
cache_read_tokens: None,
cache_write_tokens: None,
}),
cumulative: None,
delta_reliable: true,
stop_reason: None,
};
let ciphertext =
encrypt_agent_turn_metric(agent_keys, &owner_keys.public_key(), &payload).unwrap();
let tags = vec![
Tag::parse(["p", &owner_pk]).unwrap(),
Tag::parse(["agent", &agent_keys.public_key().to_hex()]).unwrap(),
];
EventBuilder::new(Kind::Custom(44200), &ciphertext)
.tags(tags)
.sign_with_keys(agent_keys)
.unwrap()
}
/// A kind-44200 event with `owner_p` scope must route to the persistent
/// (relay-query) path, NOT the ephemeral path.
#[test]
fn test_owner_p_44200_routes_to_persistent_path() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
// Subscription for kind 44200 under owner_p.
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let plan = plan_archive(vec![cand], &owner_pk, relay_url, &conn).unwrap();
// Must be in persistent buckets, NOT ephemeral list.
assert_eq!(plan.buckets.len(), 1, "kind-44200 must land in a bucket");
assert_eq!(
plan.ephemeral.len(),
0,
"kind-44200 must NOT be on the ephemeral path"
);
assert_eq!(
plan.buckets[0].scope_type_str, "owner_p",
"bucket scope_type must be owner_p"
);
}
/// A kind-24200 event with `owner_p` scope must still route to ephemeral.
#[test]
fn test_owner_p_24200_still_routes_to_ephemeral() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[24200]");
let ev = make_observer_frame(&owner_keys, &agent_keys, OBSERVER_FRAME_TELEMETRY);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let plan = plan_archive(vec![cand], &owner_pk, relay_url, &conn).unwrap();
assert_eq!(
plan.buckets.len(),
0,
"kind-24200 must NOT land in a bucket"
);
assert_eq!(
plan.ephemeral.len(),
1,
"kind-24200 must be on the ephemeral path"
);
}
/// Decrypt success: plaintext payload JSON is stored, not raw ciphertext.
#[test]
fn test_turn_metric_decrypt_success_stores_plaintext() {
let conn = in_memory();
let owner_keys = Keys::generate();
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let result = run_batch_sync_with_keys(
vec![cand],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&owner_keys,
);
assert_eq!(result.persisted, 1, "event must be persisted");
assert_eq!(result.dropped, 0, "no drops on successful decrypt");
// The stored raw_json must be plaintext JSON, not NIP-44 ciphertext.
let raw_json: String = conn
.query_row("SELECT raw_json FROM archived_events", [], |r| r.get(0))
.unwrap();
// Plaintext JSON should be a valid object with "harness" key.
let parsed: serde_json::Value =
serde_json::from_str(&raw_json).expect("stored raw_json must be valid JSON");
assert_eq!(
parsed["harness"], "test-harness",
"stored plaintext must decode to AgentTurnMetricPayload"
);
// Sanity: must NOT be the original NIP-44 ciphertext (which is not JSON).
assert_ne!(
raw_json, ev.content,
"stored content must differ from original ciphertext"
);
}
/// Decrypt fail: event is dropped, nothing written to the store (fail-closed).
#[test]
fn test_turn_metric_decrypt_fail_drops_fail_closed() {
let conn = in_memory();
let owner_keys = Keys::generate();
let wrong_keys = Keys::generate(); // wrong owner key — decrypt will fail
let agent_keys = Keys::generate();
let owner_pk = owner_keys.public_key().to_hex();
let relay_url = "wss://relay.example";
// Register subscription under owner_pk so the event passes plan-phase,
// but use `wrong_keys` in commit so decrypt fails.
add_sub(&conn, &owner_pk, relay_url, "owner_p", &owner_pk, "[44200]");
let ev = make_turn_metric_event(&owner_keys, &agent_keys);
let cand = candidate(&ev, ScopeType::OwnerP, &owner_pk);
let result = run_batch_sync_with_keys(
vec![cand],
&owner_pk,
relay_url,
&conn,
vec![ev.clone()],
&wrong_keys, // wrong key → decrypt fails
);
assert_eq!(
result.persisted, 0,
"decrypt failure must not persist the event"
);
assert_eq!(result.dropped, 1, "decrypt failure must count as dropped");
let event_count: i64 = conn
.query_row("SELECT COUNT(*) FROM archived_events", [], |r| r.get(0))
.unwrap();
assert_eq!(
event_count, 0,
"no rows must be written to archived_events on decrypt failure"
);
}
// Kind-44200 agent-turn-metric coverage lives in a sibling file to keep this
// one under the 1000-line gate; nested here (not in `mod.rs`) so it inherits
// the shared fixtures above through `use super::*`.
#[path = "mod_agent_metric_tests.rs"]
mod agent_metric;
// ── Real-relay integration tests ──────────────────────────────────────────
//
+36 -1
View File
@@ -268,6 +268,7 @@ pub(super) fn commit_archive(
conn: &Connection,
) -> Result<ArchiveBatchResult, String> {
let mut persisted: u32 = 0;
let mut persisted_agent_metrics: u32 = 0;
let mut dropped: u32 = pre_dropped;
// Collect writes; count drops first, then execute inside a single
@@ -399,6 +400,36 @@ pub(super) fn commit_archive(
&w.scope_value,
now,
)?;
// Index kind-44200 rows in the SAME transaction as the canonical
// insert (Rev 2 F5): the plaintext payload was already decrypted
// above into `w.raw_json`, so this is parse-only, no re-decrypt.
// `insert_metric_index_row`'s own `ON CONFLICT DO NOTHING` makes
// a duplicate call for an already-indexed id (e.g. re-ingest of
// a row seen in an earlier batch) a safe no-op — its `bool`
// return tells us whether this call actually inserted a new
// index row, which is exactly what `persisted_agent_metrics`
// counts (A5: newly-indexed rows, valid or invalid, not raw
// write attempts).
if w.kind == super::KIND_AGENT_TURN_METRIC as i64 {
let index_row = super::metric_store::AgentMetricIndexRow::from_payload(
&w.raw_json,
&w.eid,
&w.pubkey,
w.created_at,
now,
);
let index_inserted = super::metric_store::insert_metric_index_row(
&tx,
identity_pk,
relay_url,
&index_row,
)?;
if index_inserted {
persisted_agent_metrics += 1;
}
}
persisted += 1;
}
@@ -450,5 +481,9 @@ pub(super) fn commit_archive(
.map_err(|e| format!("failed to commit archive transaction: {e}"))?;
}
Ok(ArchiveBatchResult { persisted, dropped })
Ok(ArchiveBatchResult {
persisted,
persisted_agent_metrics,
dropped,
})
}
+110 -23
View File
@@ -13,6 +13,8 @@ use std::path::Path;
use rusqlite::{params, Connection, OptionalExtension};
use std::time::{Duration, Instant};
use super::store_migrations::apply_schema_migrations;
// ── Schema ─────────────────────────────────────────────────────────────────
pub(super) const SCHEMA: &str = "
@@ -75,6 +77,67 @@ CREATE TABLE IF NOT EXISTS archive_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
);
-- Parsed index of kind 44200 (NIP-AM agent turn metric) archive rows.
--
-- Rebuildable from `archived_events.raw_json` — never the source of truth.
-- Every archived kind-44200 row gets exactly one row here, keyed by
-- (identity, relay, id), with `parse_status` 'valid' or 'invalid'. Token
-- counters are stored as fixed-width 20-digit zero-padded decimal TEXT
-- (order-preserving lexicographically) because SQLite INTEGER is signed
-- i64 and NIP-AM counters are full-range u64. `turn_seq` uses the same
-- encoding so it survives sequence values above i64::MAX.
CREATE TABLE IF NOT EXISTS agent_metric_index (
identity_pubkey TEXT NOT NULL,
relay_url TEXT NOT NULL,
id TEXT NOT NULL,
agent_pubkey TEXT NOT NULL,
event_created_at INTEGER NOT NULL,
archived_at INTEGER NOT NULL,
reported_at INTEGER,
session_id TEXT,
turn_seq TEXT,
model TEXT,
delta_reliable INTEGER,
turn_input_tokens TEXT,
turn_output_tokens TEXT,
turn_total_tokens TEXT,
turn_cost_usd REAL,
turn_cache_read_tokens TEXT,
turn_cache_write_tokens TEXT,
cumulative_input_tokens TEXT,
cumulative_output_tokens TEXT,
cumulative_total_tokens TEXT,
cumulative_cost_usd REAL,
cumulative_cache_read_tokens TEXT,
cumulative_cache_write_tokens TEXT,
pricing_authority TEXT,
pricing_model TEXT,
pricing_cache_class TEXT,
harness TEXT,
parse_status TEXT NOT NULL CHECK (parse_status IN ('valid','invalid')),
PRIMARY KEY (identity_pubkey, relay_url, id)
);
-- Backfill/anti-join source: scoped partial index so the per-read backfill
-- scan over archived_events only touches kind-44200 rows.
CREATE INDEX IF NOT EXISTS idx_archived_events_agent_metric
ON archived_events (identity_pubkey, relay_url, id)
WHERE kind = 44200;
-- Predecessor/baseline lookup: exact-sequence probes (A11) and duplicate
-- cardinality checks key on this prefix.
CREATE INDEX IF NOT EXISTS idx_agent_metric_session
ON agent_metric_index (identity_pubkey, relay_url, agent_pubkey, session_id, turn_seq, id);
-- Window scan by reported time.
CREATE INDEX IF NOT EXISTS idx_agent_metric_reported
ON agent_metric_index (identity_pubkey, relay_url, reported_at);
-- Coarse-time scan for invalid-row coverage (invalid rows lack a trustworthy
-- reported_at, so their window membership is judged by event_created_at).
CREATE INDEX IF NOT EXISTS idx_agent_metric_created
ON agent_metric_index (identity_pubkey, relay_url, event_created_at, parse_status);
";
// ── Open / init ─────────────────────────────────────────────────────────────
@@ -100,6 +163,8 @@ pub fn open_archive_db(path: &Path) -> Result<Connection, String> {
conn.execute_batch(SCHEMA)
.map_err(|e| format!("failed to initialize archive schema: {e}"))?;
apply_schema_migrations(&conn)?;
Ok(conn)
}
@@ -442,6 +507,8 @@ pub fn get_subscription_kinds(
/// Upsert an event row (idempotent on the PK).
///
/// Does nothing if the event is already archived (same identity/relay/id).
/// Returns `true` iff this call inserted a new row (`false` if the row
/// already existed and the `ON CONFLICT DO NOTHING` no-op'd).
// Args mirror the archived_events columns; a params struct would just rename them.
#[allow(clippy::too_many_arguments)]
pub fn upsert_archived_event(
@@ -454,25 +521,26 @@ pub fn upsert_archived_event(
created_at: i64,
raw_json: &str,
archived_at: i64,
) -> Result<(), String> {
conn.execute(
"INSERT INTO archived_events
(identity_pubkey, relay_url, id, kind, pubkey, created_at, raw_json, archived_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
ON CONFLICT (identity_pubkey, relay_url, id) DO NOTHING",
params![
identity_pubkey,
relay_url,
event_id,
kind,
pubkey,
created_at,
raw_json,
archived_at
],
)
.map_err(|e| format!("failed to upsert archived event: {e}"))?;
Ok(())
) -> Result<bool, String> {
let affected = conn
.execute(
"INSERT INTO archived_events
(identity_pubkey, relay_url, id, kind, pubkey, created_at, raw_json, archived_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
ON CONFLICT (identity_pubkey, relay_url, id) DO NOTHING",
params![
identity_pubkey,
relay_url,
event_id,
kind,
pubkey,
created_at,
raw_json,
archived_at
],
)
.map_err(|e| format!("failed to upsert archived event: {e}"))?;
Ok(affected > 0)
}
/// Upsert a scope membership row for an event.
@@ -771,17 +839,28 @@ pub fn read_archived_observer_events_for_channel(
.map_err(|e| format!("read read_archived_observer_events_for_channel row: {e}"))
}
/// GC: delete orphaned event rows whose last scope row was just removed.
/// GC: delete orphaned event rows whose last scope row was just removed, and
/// atomically cascade-delete any `agent_metric_index` rows whose canonical
/// `archived_events` row no longer exists.
///
/// Called after any batch deletion of scope rows. Uses a LEFT JOIN so only
/// events with zero remaining scope rows are deleted.
/// Both deletes run inside ONE SQLite transaction (not two autocommit
/// statements) so the derived index can never observe a canonical row as
/// gone while the index row it produced still exists — an index row must
/// never outlive the event it was parsed from (A6).
///
/// Called after any batch deletion of scope rows. Uses a LEFT JOIN-equivalent
/// anti-join so only events with zero remaining scope rows are deleted.
#[allow(dead_code)] // Used by P4 purge commands; not yet wired to a Tauri command.
pub fn gc_orphaned_events(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
) -> Result<usize, String> {
let affected = conn
let tx = conn
.unchecked_transaction()
.map_err(|e| format!("failed to begin gc_orphaned_events transaction: {e}"))?;
let affected = tx
.execute(
"DELETE FROM archived_events
WHERE identity_pubkey = ?1
@@ -794,11 +873,19 @@ pub fn gc_orphaned_events(
params![identity_pubkey, relay_url],
)
.map_err(|e| format!("failed to gc orphaned events: {e}"))?;
super::metric_store::delete_orphaned_metric_index_rows(&tx, identity_pubkey, relay_url)?;
tx.commit()
.map_err(|e| format!("failed to commit gc_orphaned_events transaction: {e}"))?;
Ok(affected)
}
// ── Tests ───────────────────────────────────────────────────────────────────
#[cfg(test)]
#[path = "store_migration_tests.rs"]
mod store_migration_tests;
#[cfg(test)]
#[path = "store_tests.rs"]
mod store_tests;
@@ -0,0 +1,867 @@
//! Migration tests for `archive/store.rs` — M1: harness column.
//!
//! Kept in a sibling file so `store_tests.rs` stays under the 1000-line gate;
//! `#[path]`-included from `store.rs`.
use super::*;
// ── Migration M1: harness column + open_archive_db reopen tests ──────────────
/// Build a DB file that looks like a pre-harness archive: schema without the
/// `harness` column, a seeded `archived_events` row and a corresponding
/// `agent_metric_index` row with `harness = NULL`, and no `archive_migrations`
/// marker. Return the path to the temp file.
fn build_old_schema_db(path: &std::path::Path) {
// Old schema has no `harness TEXT` column and no `archive_migrations` table.
const OLD_SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS archived_events (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
kind INTEGER NOT NULL, pubkey TEXT NOT NULL, created_at INTEGER NOT NULL,
raw_json TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archived_event_scopes (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
scope_type TEXT NOT NULL, scope_value TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS save_subscriptions (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, scope_type TEXT NOT NULL,
scope_value TEXT NOT NULL, kinds TEXT NOT NULL, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS observer_channel_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
channel_id TEXT, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS agent_metric_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
agent_pubkey TEXT NOT NULL, event_created_at INTEGER NOT NULL,
archived_at INTEGER NOT NULL, reported_at INTEGER, session_id TEXT,
turn_seq TEXT, model TEXT, delta_reliable INTEGER,
turn_input_tokens TEXT, turn_output_tokens TEXT, turn_total_tokens TEXT,
turn_cost_usd REAL, cumulative_input_tokens TEXT, cumulative_output_tokens TEXT,
cumulative_total_tokens TEXT, cumulative_cost_usd REAL,
parse_status TEXT NOT NULL CHECK (parse_status IN ('valid','invalid')),
PRIMARY KEY (identity_pubkey, relay_url, id)
);
";
let conn = Connection::open(path).unwrap();
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
conn.pragma_update(None, "busy_timeout", 5000).unwrap();
conn.execute_batch(OLD_SCHEMA).unwrap();
// A valid payload that carries harness="goose".
let raw_json = r#"{"harness":"goose","model":"claude","channelId":null,"sessionId":"s1","turnId":null,"turnSeq":1,"timestamp":"2026-07-01T00:00:00Z","turn":{"inputTokens":10,"outputTokens":20,"totalTokens":30,"costUsd":0.01},"cumulative":{"inputTokens":100,"outputTokens":200,"totalTokens":300,"costUsd":0.1},"deltaReliable":true,"stopReason":"end_turn"}"#;
conn.execute(
"INSERT INTO archived_events
(identity_pubkey, relay_url, id, kind, pubkey, created_at, raw_json, archived_at)
VALUES ('id', 'relay', 'eid1', 44200, 'agent1', 100, ?1, 200)",
rusqlite::params![raw_json],
)
.unwrap();
// Seed the index row with harness absent (old schema has no column).
conn.execute(
"INSERT INTO agent_metric_index
(identity_pubkey, relay_url, id, agent_pubkey, event_created_at,
archived_at, reported_at, session_id, turn_seq, model,
delta_reliable, turn_input_tokens, turn_output_tokens,
turn_total_tokens, turn_cost_usd, cumulative_input_tokens,
cumulative_output_tokens, cumulative_total_tokens,
cumulative_cost_usd, parse_status)
VALUES ('id','relay','eid1','agent1',100,200,1751414400000,
's1','00000000000000000001','claude',
1,'00000000000000000010','00000000000000000020',
'00000000000000000030',0.01,
'00000000000000000100','00000000000000000200',
'00000000000000000300',0.1,'valid')",
[],
)
.unwrap();
}
/// Opening a pre-harness DB via `open_archive_db` must:
///
/// 1. Add the `harness` column.
/// 2. Rebuild all index rows so harness is populated from `archived_events`.
/// 3. Record the migration marker in `archive_migrations`.
///
/// This proves M1 fires on a real legacy file, not just a hand-rolled
/// DELETE+backfill as in the metric_store migration test.
#[test]
fn migration_m1_reopen_old_schema_db_populates_harness_and_records_marker() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
build_old_schema_db(&db_path);
// Reopen via the real migration path.
let conn = open_archive_db(&db_path).expect("open_archive_db must succeed on legacy DB");
// Harness must be populated.
let harness: Option<String> = conn
.query_row(
"SELECT harness FROM agent_metric_index WHERE id = 'eid1'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
harness,
Some("goose".to_string()),
"M1: harness must be populated from raw_json after reopen"
);
// Marker must be recorded.
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations \
WHERE name = 'add_harness_to_metric_index'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(marker_count, 1, "M1: migration marker must be recorded");
}
/// Re-opening the same DB a second time must be a no-op: marker already present,
/// harness already populated, no error.
#[test]
fn migration_m1_reopen_twice_is_idempotent() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
build_old_schema_db(&db_path);
// First open: runs M1.
open_archive_db(&db_path).expect("first open must succeed");
// Second open: M1 is already marked; must also succeed without error.
let conn2 = open_archive_db(&db_path).expect("second open must succeed (M1 is idempotent)");
let count: i64 = conn2
.query_row(
"SELECT COUNT(*) FROM agent_metric_index WHERE harness = 'goose'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
count, 1,
"after idempotent second open, harness row must still be present"
);
}
/// If the migration is aborted after DELETE but before COMMIT (simulated by
/// manually applying each sub-step without committing), the DB must be in its
/// pre-migration state and the marker must be absent, so the next
/// `open_archive_db` can re-run M1 from scratch and complete successfully.
///
/// We simulate the "crash before commit" scenario by:
/// 1. Building a pre-harness DB on disk.
/// 2. Opening a raw connection, running ALTER + DELETE but rolling back before
/// inserting the marker — leaving the DB in pre-migration state.
/// 3. Calling `open_archive_db` on the same file and asserting M1 completes.
#[test]
fn migration_m1_crashed_before_commit_reruns_fully_on_next_open() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
build_old_schema_db(&db_path);
// Simulate a crash: start a transaction, ALTER + DELETE, then ROLLBACK
// (leaving DB exactly as-built — pre-migration, no marker).
{
let conn = Connection::open(&db_path).unwrap();
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
conn.execute_batch("BEGIN IMMEDIATE").unwrap();
// ALTER inside the transaction.
conn.execute_batch("ALTER TABLE agent_metric_index ADD COLUMN harness TEXT")
.unwrap();
// DELETE index rows (simulating the mid-migration state).
conn.execute_batch("DELETE FROM agent_metric_index")
.unwrap();
// Rollback — no marker inserted, no new rows.
conn.execute_batch("ROLLBACK").unwrap();
}
// After rollback: marker must be absent and the original index row still there.
{
let verify = Connection::open(&db_path).unwrap();
// archive_migrations table may not exist at all (old schema). If it does,
// the marker must not be present.
let marker_count: i64 = verify
.query_row(
"SELECT COUNT(*) FROM sqlite_master \
WHERE type='table' AND name='archive_migrations'",
[],
|r| r.get(0),
)
.unwrap();
if marker_count > 0 {
let applied: i64 = verify
.query_row(
"SELECT COUNT(*) FROM archive_migrations \
WHERE name = 'add_harness_to_metric_index'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(applied, 0, "marker must be absent after rollback");
}
}
// The next open must run M1 fully and succeed.
let conn =
open_archive_db(&db_path).expect("open_archive_db must succeed after simulated crash");
// Harness must be populated.
let harness: Option<String> = conn
.query_row(
"SELECT harness FROM agent_metric_index WHERE id = 'eid1'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
harness,
Some("goose".to_string()),
"M1 must re-run after simulated crash and populate harness"
);
// Marker must now be recorded.
let marker: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations \
WHERE name = 'add_harness_to_metric_index'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
marker, 1,
"M1 marker must be recorded after successful re-run"
);
}
// ── Migration M2: cache_read_tokens columns ───────────────────────────────────
/// Opening a fresh DB (created by `open_archive_db`) must have both cache
/// columns present and the M2 marker recorded.
#[test]
fn migration_m2_fresh_db_has_cache_columns_and_marker() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let conn = open_archive_db(db_file.path()).expect("open_archive_db must succeed");
// Both columns must exist (PRAGMA table_info returns one row per column).
let columns: Vec<String> = {
let mut stmt = conn
.prepare("PRAGMA table_info(agent_metric_index)")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
};
assert!(
columns.iter().any(|c| c == "turn_cache_read_tokens"),
"turn_cache_read_tokens must exist on a fresh DB"
);
assert!(
columns.iter().any(|c| c == "cumulative_cache_read_tokens"),
"cumulative_cache_read_tokens must exist on a fresh DB"
);
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_read_tokens'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(marker_count, 1, "M2 marker must be recorded on fresh DB");
}
/// Opening a post-M1, pre-M2 DB (i.e., has `harness` column and M1 marker,
/// but no `turn_cache_read_tokens` column) must add both cache columns,
/// leaving all pre-existing rows with NULL for those columns, and record the
/// M2 marker.
#[test]
fn migration_m2_upgrades_post_m1_db_and_leaves_nulls() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
// Build a DB that looks like it ran M1 but not M2: the schema has
// `harness` but not the cache columns, and M1 marker is present.
{
let conn = Connection::open(&db_path).unwrap();
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
conn.pragma_update(None, "busy_timeout", 5000).unwrap();
// Use M1-era schema: harness present, no cache columns, no archive_migrations table yet.
const M1_SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS archived_events (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
kind INTEGER NOT NULL, pubkey TEXT NOT NULL, created_at INTEGER NOT NULL,
raw_json TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archived_event_scopes (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
scope_type TEXT NOT NULL, scope_value TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS save_subscriptions (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, scope_type TEXT NOT NULL,
scope_value TEXT NOT NULL, kinds TEXT NOT NULL, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS observer_channel_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
channel_id TEXT, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS agent_metric_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
agent_pubkey TEXT NOT NULL, event_created_at INTEGER NOT NULL,
archived_at INTEGER NOT NULL, reported_at INTEGER, session_id TEXT,
turn_seq TEXT, model TEXT, delta_reliable INTEGER,
turn_input_tokens TEXT, turn_output_tokens TEXT, turn_total_tokens TEXT,
turn_cost_usd REAL, cumulative_input_tokens TEXT, cumulative_output_tokens TEXT,
cumulative_total_tokens TEXT, cumulative_cost_usd REAL,
harness TEXT,
parse_status TEXT NOT NULL CHECK (parse_status IN ('valid','invalid')),
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archive_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
);
";
conn.execute_batch(M1_SCHEMA).unwrap();
// Mark M1 as already run so `open_archive_db` skips it.
conn.execute(
"INSERT INTO archive_migrations (name, applied_at) VALUES ('add_harness_to_metric_index', 1)",
[],
)
.unwrap();
// Seed one existing index row — it should get NULL cache columns.
let raw_json = r#"{"harness":"goose","model":"claude","channelId":null,"sessionId":"s1","turnId":null,"turnSeq":1,"timestamp":"2026-07-01T00:00:00Z","turn":{"inputTokens":10,"outputTokens":20,"totalTokens":30,"costUsd":0.01},"cumulative":{"inputTokens":100,"outputTokens":200,"totalTokens":300,"costUsd":0.1},"deltaReliable":true,"stopReason":"end_turn"}"#;
conn.execute(
"INSERT INTO archived_events
(identity_pubkey, relay_url, id, kind, pubkey, created_at, raw_json, archived_at)
VALUES ('id', 'relay', 'eid1', 44200, 'agent1', 100, ?1, 200)",
rusqlite::params![raw_json],
)
.unwrap();
conn.execute(
"INSERT INTO agent_metric_index
(identity_pubkey, relay_url, id, agent_pubkey, event_created_at,
archived_at, reported_at, session_id, turn_seq, model,
delta_reliable, turn_input_tokens, turn_output_tokens,
turn_total_tokens, turn_cost_usd, cumulative_input_tokens,
cumulative_output_tokens, cumulative_total_tokens,
cumulative_cost_usd, harness, parse_status)
VALUES ('id','relay','eid1','agent1',100,200,1751414400,
's1','00000000000000000001','claude',
1,'00000000000000000010','00000000000000000020',
'00000000000000000030',0.01,
'00000000000000000100','00000000000000000200',
'00000000000000000300',0.1,'goose','valid')",
[],
)
.unwrap();
}
// Open via migration path — M1 is already marked, M2 should run.
let conn = open_archive_db(&db_path).expect("open_archive_db must succeed on post-M1 DB");
// Both cache columns must now exist.
let columns: Vec<String> = {
let mut stmt = conn
.prepare("PRAGMA table_info(agent_metric_index)")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
};
assert!(
columns.iter().any(|c| c == "turn_cache_read_tokens"),
"M2: turn_cache_read_tokens must be present after upgrade"
);
assert!(
columns.iter().any(|c| c == "cumulative_cache_read_tokens"),
"M2: cumulative_cache_read_tokens must be present after upgrade"
);
// Pre-existing row gets NULLs — not estimated, not backfilled.
let (turn_cr, cum_cr): (Option<String>, Option<String>) = conn
.query_row(
"SELECT turn_cache_read_tokens, cumulative_cache_read_tokens \
FROM agent_metric_index WHERE id = 'eid1'",
[],
|r| Ok((r.get(0)?, r.get(1)?)),
)
.unwrap();
assert!(
turn_cr.is_none(),
"M2: pre-existing row turn_cache_read_tokens must be NULL"
);
assert!(
cum_cr.is_none(),
"M2: pre-existing row cumulative_cache_read_tokens must be NULL"
);
// Marker must be recorded.
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_read_tokens'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(marker_count, 1, "M2: migration marker must be recorded");
}
/// Re-opening after M2 has run is a no-op — marker present, columns present, no error.
#[test]
fn migration_m2_reopen_twice_is_idempotent() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
// First open runs both M1 and M2.
open_archive_db(&db_path).expect("first open must succeed");
// Second open must not error even though both columns already exist.
let conn2 = open_archive_db(&db_path).expect("second open must succeed (M2 is idempotent)");
let marker_count: i64 = conn2
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_read_tokens'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
marker_count, 1,
"M2: marker must still be present after idempotent second open"
);
}
/// Partial-schema repair: only `turn_cache_read_tokens` present (no cumulative).
/// M2 must add the missing cumulative column and commit the marker.
#[test]
fn migration_m2_repairs_turn_only_partial_schema_and_records_marker() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
// Build a DB that has `turn_cache_read_tokens` but NOT `cumulative_cache_read_tokens`
// (simulates a crash between the two ALTER TABLE statements in old M2).
{
let conn = Connection::open(&db_path).unwrap();
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
conn.pragma_update(None, "busy_timeout", 5000).unwrap();
// Use M1-era schema + add only the turn column manually.
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS archived_events (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
kind INTEGER NOT NULL, pubkey TEXT NOT NULL, created_at INTEGER NOT NULL,
raw_json TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archived_event_scopes (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
scope_type TEXT NOT NULL, scope_value TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS save_subscriptions (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, scope_type TEXT NOT NULL,
scope_value TEXT NOT NULL, kinds TEXT NOT NULL, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS observer_channel_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
channel_id TEXT, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS agent_metric_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
agent_pubkey TEXT NOT NULL, event_created_at INTEGER NOT NULL,
archived_at INTEGER NOT NULL, reported_at INTEGER, session_id TEXT,
turn_seq TEXT, model TEXT, delta_reliable INTEGER,
turn_input_tokens TEXT, turn_output_tokens TEXT, turn_total_tokens TEXT,
turn_cost_usd REAL, cumulative_input_tokens TEXT, cumulative_output_tokens TEXT,
cumulative_total_tokens TEXT, cumulative_cost_usd REAL,
harness TEXT,
parse_status TEXT NOT NULL CHECK (parse_status IN ('valid','invalid')),
turn_cache_read_tokens TEXT,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archive_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
);",
)
.unwrap();
// Mark M1 as done so open_archive_db skips it.
conn.execute(
"INSERT INTO archive_migrations (name, applied_at) VALUES ('add_harness_to_metric_index', 1)",
[],
)
.unwrap();
// Do NOT mark M2 — let open_archive_db run it.
}
// Opening must run M2: it finds turn_cache_read_tokens already present and
// only adds the missing cumulative column, then commits the marker.
let conn =
open_archive_db(&db_path).expect("open_archive_db must repair turn-only partial M2 schema");
let columns: Vec<String> = {
let mut stmt = conn
.prepare("PRAGMA table_info(agent_metric_index)")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
};
assert!(
columns.iter().any(|c| c == "turn_cache_read_tokens"),
"M2 partial repair: turn_cache_read_tokens must be present"
);
assert!(
columns.iter().any(|c| c == "cumulative_cache_read_tokens"),
"M2 partial repair: cumulative_cache_read_tokens must have been added"
);
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_read_tokens'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
marker_count, 1,
"M2 partial repair: marker must be committed after full repair"
);
}
/// Partial-schema repair: only `cumulative_cache_read_tokens` present (no turn).
/// M2 must add the missing turn column and commit the marker.
#[test]
fn migration_m2_repairs_cumulative_only_partial_schema_and_records_marker() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
{
let conn = Connection::open(&db_path).unwrap();
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
conn.pragma_update(None, "busy_timeout", 5000).unwrap();
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS archived_events (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
kind INTEGER NOT NULL, pubkey TEXT NOT NULL, created_at INTEGER NOT NULL,
raw_json TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archived_event_scopes (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
scope_type TEXT NOT NULL, scope_value TEXT NOT NULL, archived_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS save_subscriptions (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, scope_type TEXT NOT NULL,
scope_value TEXT NOT NULL, kinds TEXT NOT NULL, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, scope_type, scope_value)
);
CREATE TABLE IF NOT EXISTS observer_channel_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
channel_id TEXT, created_at INTEGER NOT NULL,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS agent_metric_index (
identity_pubkey TEXT NOT NULL, relay_url TEXT NOT NULL, id TEXT NOT NULL,
agent_pubkey TEXT NOT NULL, event_created_at INTEGER NOT NULL,
archived_at INTEGER NOT NULL, reported_at INTEGER, session_id TEXT,
turn_seq TEXT, model TEXT, delta_reliable INTEGER,
turn_input_tokens TEXT, turn_output_tokens TEXT, turn_total_tokens TEXT,
turn_cost_usd REAL, cumulative_input_tokens TEXT, cumulative_output_tokens TEXT,
cumulative_total_tokens TEXT, cumulative_cost_usd REAL,
harness TEXT,
parse_status TEXT NOT NULL CHECK (parse_status IN ('valid','invalid')),
cumulative_cache_read_tokens TEXT,
PRIMARY KEY (identity_pubkey, relay_url, id)
);
CREATE TABLE IF NOT EXISTS archive_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
);",
)
.unwrap();
conn.execute(
"INSERT INTO archive_migrations (name, applied_at) VALUES ('add_harness_to_metric_index', 1)",
[],
)
.unwrap();
}
let conn = open_archive_db(&db_path)
.expect("open_archive_db must repair cumulative-only partial M2 schema");
let columns: Vec<String> = {
let mut stmt = conn
.prepare("PRAGMA table_info(agent_metric_index)")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
};
assert!(
columns.iter().any(|c| c == "turn_cache_read_tokens"),
"M2 partial repair: turn_cache_read_tokens must have been added"
);
assert!(
columns.iter().any(|c| c == "cumulative_cache_read_tokens"),
"M2 partial repair: cumulative_cache_read_tokens must be present"
);
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_read_tokens'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
marker_count, 1,
"M2 partial repair: marker must be committed after full repair"
);
}
/// A fresh DB must include the M3 columns (`turn_cache_write_tokens`,
/// `cumulative_cache_write_tokens`, `pricing_authority`, `pricing_model`,
/// `pricing_cache_class`) in the initial schema.
#[test]
fn migration_m3_fresh_db_has_all_m3_columns() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
let conn = open_archive_db(&db_path).expect("fresh open must succeed");
let columns: Vec<String> = {
let mut stmt = conn
.prepare("PRAGMA table_info(agent_metric_index)")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
};
for col in &[
"turn_cache_write_tokens",
"cumulative_cache_write_tokens",
"pricing_authority",
"pricing_model",
"pricing_cache_class",
] {
assert!(
columns.iter().any(|c| c == col),
"M3: {col} must exist on a fresh DB"
);
}
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_write_and_pricing'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(marker_count, 1, "M3: marker must be recorded on fresh DB");
}
/// Opening a post-M2, pre-M3 DB (has cache-read columns and M2 marker, but
/// no cache-write or pricing columns) must add all five M3 columns and
/// leave pre-existing rows as NULL in those columns.
#[test]
fn migration_m3_upgrades_post_m2_database() {
use rusqlite::Connection;
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
// Build a post-M2, pre-M3 DB by hand (bypassing the normal open path
// so the M3 migration has not yet run).
{
let conn = Connection::open(&db_path).unwrap();
conn.execute_batch(
"PRAGMA journal_mode=WAL;
CREATE TABLE IF NOT EXISTS archive_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
);
CREATE TABLE agent_metric_index (
identity_pubkey TEXT NOT NULL,
relay_url TEXT NOT NULL,
id TEXT NOT NULL,
agent_pubkey TEXT NOT NULL,
event_created_at INTEGER NOT NULL,
archived_at INTEGER NOT NULL,
reported_at INTEGER,
session_id TEXT,
turn_seq TEXT,
model TEXT,
delta_reliable INTEGER,
turn_input_tokens TEXT,
turn_output_tokens TEXT,
turn_total_tokens TEXT,
turn_cost_usd REAL,
turn_cache_read_tokens TEXT,
cumulative_input_tokens TEXT,
cumulative_output_tokens TEXT,
cumulative_total_tokens TEXT,
cumulative_cost_usd REAL,
cumulative_cache_read_tokens TEXT,
harness TEXT,
parse_status TEXT NOT NULL CHECK (parse_status IN ('valid','invalid')),
PRIMARY KEY (identity_pubkey, relay_url, id)
);",
)
.unwrap();
// Mark M1 and M2 as applied, but NOT M3.
conn.execute_batch(
"INSERT OR IGNORE INTO archive_migrations (name, applied_at) VALUES ('add_harness_to_metric_index', 0);
INSERT OR IGNORE INTO archive_migrations (name, applied_at) VALUES ('add_cache_read_tokens', 0);",
)
.unwrap();
// Insert a pre-migration row.
conn.execute(
"INSERT INTO agent_metric_index
(identity_pubkey, relay_url, id, agent_pubkey, event_created_at,
archived_at, reported_at, session_id, turn_seq, model,
delta_reliable, turn_input_tokens, turn_output_tokens,
turn_total_tokens, turn_cost_usd, turn_cache_read_tokens,
cumulative_input_tokens, cumulative_output_tokens,
cumulative_total_tokens, cumulative_cost_usd,
cumulative_cache_read_tokens, harness, parse_status)
VALUES ('id','relay','eid-m3','agent1',100,200,1751414400,
's1','00000000000000000001','model1',
1,'00000000000000000010','00000000000000000020',
'00000000000000000030',0.01,'00000000000000000050',
'00000000000000000100','00000000000000000200',
'00000000000000000300',0.1,'00000000000000000500',
'buzz-agent','valid')",
[],
)
.unwrap();
}
// Open via the production path — M3 should run.
let conn = open_archive_db(&db_path).expect("open_archive_db must succeed on post-M2 DB");
// All five M3 columns must now exist.
let columns: Vec<String> = {
let mut stmt = conn
.prepare("PRAGMA table_info(agent_metric_index)")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
};
for col in &[
"turn_cache_write_tokens",
"cumulative_cache_write_tokens",
"pricing_authority",
"pricing_model",
"pricing_cache_class",
] {
assert!(
columns.iter().any(|c| c == col),
"M3: {col} must be present after upgrade"
);
}
// Pre-existing row gets NULLs for all new columns.
type M3NullRow = (
Option<String>,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
);
let (tcw, ccw, pa, pm, pcc): M3NullRow = conn
.query_row(
"SELECT turn_cache_write_tokens, cumulative_cache_write_tokens, \
pricing_authority, pricing_model, pricing_cache_class \
FROM agent_metric_index WHERE id = 'eid-m3'",
[],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?)),
)
.unwrap();
assert!(
tcw.is_none(),
"M3: pre-existing row turn_cache_write_tokens must be NULL"
);
assert!(
ccw.is_none(),
"M3: pre-existing row cumulative_cache_write_tokens must be NULL"
);
assert!(
pa.is_none(),
"M3: pre-existing row pricing_authority must be NULL"
);
assert!(
pm.is_none(),
"M3: pre-existing row pricing_model must be NULL"
);
assert!(
pcc.is_none(),
"M3: pre-existing row pricing_cache_class must be NULL"
);
// Marker must be recorded.
let marker_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_write_and_pricing'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(marker_count, 1, "M3: migration marker must be recorded");
}
/// Re-opening after M3 has run is a no-op.
#[test]
fn migration_m3_reopen_twice_is_idempotent() {
use tempfile::NamedTempFile;
let db_file = NamedTempFile::new().unwrap();
let db_path = db_file.path().to_path_buf();
open_archive_db(&db_path).expect("first open must succeed");
let conn2 = open_archive_db(&db_path).expect("second open must succeed (M3 is idempotent)");
let marker_count: i64 = conn2
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_write_and_pricing'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
marker_count, 1,
"M3: marker must still be present after idempotent second open"
);
}
@@ -0,0 +1,325 @@
//! One-shot schema migrations for the archive database.
//!
//! Applied by `store::open_archive_db` on every open and guarded by markers in
//! `archive_migrations`, so a migration that already ran is a no-op.
//!
//! Kept in a sibling file (not `store.rs`) to keep that file under the
//! 1000-line gate, per the existing `metric_store.rs` / `pipeline.rs`
//! precedent.
use rusqlite::{params, Connection};
/// One-shot, idempotent schema migrations recorded in `archive_migrations`.
///
/// Each migration is guarded by a `SELECT 1 FROM archive_migrations WHERE
/// name = '...'` check so it is safe to call on every open — a migration
/// that already ran is a no-op.
///
/// Ordering: M2 (column additions) runs before M1 (index rebuild) so that
/// the M1 rebuild, which calls `insert_metric_index_row`, always operates
/// against a schema that includes the cache-read columns.
pub(super) fn apply_schema_migrations(conn: &Connection) -> Result<(), String> {
migrate_add_cache_read_tokens(conn)?;
migrate_add_cache_write_and_pricing(conn)?;
migrate_add_harness_to_metric_index(conn)
}
/// M1: add `harness TEXT` column to `agent_metric_index` and rebuild index
/// rows so pre-existing rows gain harness populated from `archived_events`.
///
/// The entire migration — column addition, full DELETE of stale index rows,
/// fresh re-insertion from `archived_events.raw_json`, and the
/// `archive_migrations` marker — is wrapped in a single `unchecked_transaction`
/// (BEGIN DEFERRED). A crash anywhere before COMMIT leaves the DB in its
/// pre-migration state and the marker absent, so the next open re-runs the
/// migration from scratch.
///
/// The worklist is built from `archived_events` (the canonical store) rather
/// than from `agent_metric_index` so that a partially-rebuilt or entirely
/// empty index never causes us to forget which (identity, relay) scopes exist.
fn migrate_add_harness_to_metric_index(conn: &Connection) -> Result<(), String> {
let already_run: bool = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_harness_to_metric_index'",
[],
|r| r.get::<_, i64>(0),
)
.map_err(|e| format!("migration M1: guard check: {e}"))?
> 0;
if already_run {
return Ok(());
}
// All steps run inside one transaction so a crash before COMMIT leaves the
// DB fully pre-migration and the marker absent — the next open re-runs.
let tx = conn
.unchecked_transaction()
.map_err(|e| format!("migration M1: begin transaction: {e}"))?;
// 1. Add the `harness` column only when it is genuinely absent, so we
// propagate unexpected DDL errors instead of swallowing them.
let harness_exists: bool = {
let mut stmt = tx
.prepare("PRAGMA table_info(agent_metric_index)")
.map_err(|e| format!("migration M1: PRAGMA table_info prepare: {e}"))?;
let names: Vec<String> = stmt
.query_map([], |row| row.get::<_, String>(1))
.map_err(|e| format!("migration M1: PRAGMA table_info query: {e}"))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("migration M1: PRAGMA table_info read: {e}"))?;
names.iter().any(|n| n == "harness")
};
if !harness_exists {
tx.execute_batch("ALTER TABLE agent_metric_index ADD COLUMN harness TEXT")
.map_err(|e| format!("migration M1: ALTER TABLE failed: {e}"))?;
}
// 2. Collect all (identity, relay) scopes from `archived_events` — the
// canonical store — so the worklist is correct even if the index was
// partially rebuilt or empty before this migration runs.
let scopes: Vec<(String, String)> = {
let mut stmt = tx
.prepare(
"SELECT DISTINCT identity_pubkey, relay_url
FROM archived_events
WHERE kind = 44200",
)
.map_err(|e| format!("migration M1: prepare scope query: {e}"))?;
let result = stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.map_err(|e| format!("migration M1: query scopes: {e}"))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("migration M1: read scopes: {e}"))?;
result
};
if !scopes.is_empty() {
// 3. Delete all stale index rows; re-insert them with harness populated
// from the shared `from_payload` parser below. Both the DELETE and
// all inserts run inside the same outer transaction — no intermediate
// commit, so readers never observe an empty index.
tx.execute_batch("DELETE FROM agent_metric_index")
.map_err(|e| format!("migration M1: delete index rows: {e}"))?;
for (identity, relay) in &scopes {
rebuild_metric_index_in_tx(&tx, identity, relay)?;
}
}
// 4. Record migration as applied — written last so the marker is only
// present in a fully committed transaction.
tx.execute(
"INSERT OR IGNORE INTO archive_migrations (name, applied_at) \
VALUES ('add_harness_to_metric_index', ?1)",
params![std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64],
)
.map_err(|e| format!("migration M1: record marker: {e}"))?;
tx.commit()
.map_err(|e| format!("migration M1: commit: {e}"))?;
Ok(())
}
/// Re-insert all kind-44200 rows for `(identity, relay)` from
/// `archived_events.raw_json` through the shared `from_payload` parser.
///
/// Unlike `metric_store::backfill_agent_metric_index`, this function runs
/// entirely inside the caller's transaction — no nested `BEGIN`/`COMMIT`.
/// It is used exclusively by M1 where the outer transaction provides atomicity.
fn rebuild_metric_index_in_tx(
conn: &Connection,
identity_pubkey: &str,
relay_url: &str,
) -> Result<(), String> {
let mut stmt = conn
.prepare(
"SELECT id, pubkey, created_at, archived_at, raw_json
FROM archived_events
WHERE identity_pubkey = ?1
AND relay_url = ?2
AND kind = 44200",
)
.map_err(|e| format!("migration M1: prepare rebuild select: {e}"))?;
let rows: Vec<(String, String, i64, i64, String)> = stmt
.query_map(params![identity_pubkey, relay_url], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, i64>(2)?,
row.get::<_, i64>(3)?,
row.get::<_, String>(4)?,
))
})
.map_err(|e| format!("migration M1: query rebuild rows: {e}"))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("migration M1: read rebuild row: {e}"))?;
drop(stmt);
for (id, pubkey, created_at, archived_at, raw_json) in &rows {
let parsed = super::metric_store::AgentMetricIndexRow::from_payload(
raw_json,
id,
pubkey,
*created_at,
*archived_at,
);
super::metric_store::insert_metric_index_row(conn, identity_pubkey, relay_url, &parsed)
.map_err(|e| format!("migration M1: insert rebuilt row: {e}"))?;
}
Ok(())
}
/// M2: add `turn_cache_read_tokens TEXT` and `cumulative_cache_read_tokens TEXT`
/// columns to `agent_metric_index`.
///
/// These columns persist the optional NIP-AM `cacheReadTokens` fields (turn +
/// cumulative). They are informational subsets of `inputTokens`, not additions
/// to it. All pre-migration rows receive `NULL` — fail-closed, never estimated.
///
/// Each column is added independently so a crash between the two ALTERs leaves
/// the DB in a self-repairable partial state: the marker is only committed after
/// BOTH columns are present.
fn migrate_add_cache_read_tokens(conn: &Connection) -> Result<(), String> {
let already_run: bool = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_read_tokens'",
[],
|r| r.get::<_, i64>(0),
)
.map_err(|e| format!("migration M2: guard check: {e}"))?
> 0;
if already_run {
return Ok(());
}
let tx = conn
.unchecked_transaction()
.map_err(|e| format!("migration M2: begin transaction: {e}"))?;
// Query existing columns once; add each column only if absent (idempotent:
// a crash between the two ALTERs leaves a partial schema that self-repairs
// on the next open — exactly the M3 pattern).
let names: Vec<String> = {
let mut stmt = tx
.prepare("PRAGMA table_info(agent_metric_index)")
.map_err(|e| format!("migration M2: PRAGMA table_info prepare: {e}"))?;
let ns: Vec<String> = stmt
.query_map([], |row| row.get::<_, String>(1))
.map_err(|e| format!("migration M2: PRAGMA table_info query: {e}"))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("migration M2: PRAGMA table_info read: {e}"))?;
ns
};
let cols = [
("turn_cache_read_tokens", "TEXT"),
("cumulative_cache_read_tokens", "TEXT"),
];
for (col, ty) in &cols {
if !names.iter().any(|n| n == col) {
tx.execute_batch(&format!(
"ALTER TABLE agent_metric_index ADD COLUMN {col} {ty}"
))
.map_err(|e| format!("migration M2: ALTER TABLE ({col}) failed: {e}"))?;
}
}
tx.execute(
"INSERT OR IGNORE INTO archive_migrations (name, applied_at) \
VALUES ('add_cache_read_tokens', ?1)",
params![std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64],
)
.map_err(|e| format!("migration M2: record marker: {e}"))?;
tx.commit()
.map_err(|e| format!("migration M2: commit: {e}"))?;
Ok(())
}
/// M3: add `turn_cache_write_tokens TEXT`, `cumulative_cache_write_tokens TEXT`,
/// `pricing_authority TEXT`, `pricing_model TEXT`, and `pricing_cache_class TEXT`
/// to `agent_metric_index`.
///
/// Cache-write columns store per-session-turn and session-cumulative write tokens
/// (same encode/decode contract as the existing cache-read columns). The pricing
/// columns store the three components of `pricingIdentity` from NIP-AM § billing
/// identity, denormalized for index queries.
///
/// New columns default to NULL for all pre-migration rows (they had no cache-write
/// reporting and no publisher-side identity proof). No rows are deleted: the columns
/// are additive and all pre-existing rows remain valid as "field not reported".
fn migrate_add_cache_write_and_pricing(conn: &Connection) -> Result<(), String> {
// Guard: skip if already applied.
let already_applied: i64 = conn
.query_row(
"SELECT COUNT(*) FROM archive_migrations WHERE name = 'add_cache_write_and_pricing'",
[],
|row| row.get(0),
)
.map_err(|e| format!("migration M3: guard check: {e}"))?;
if already_applied > 0 {
return Ok(());
}
let tx = conn
.unchecked_transaction()
.map_err(|e| format!("migration M3: begin transaction: {e}"))?;
// Check which columns already exist (idempotent: ALTER TABLE is skipped for
// each column that already exists, e.g. from a partial previous run that
// committed the column but not the migration marker).
let names: Vec<String> = {
let mut stmt = tx
.prepare("PRAGMA table_info(agent_metric_index)")
.map_err(|e| format!("migration M3: PRAGMA table_info prepare: {e}"))?;
let ns: Vec<String> = stmt
.query_map([], |row| row.get::<_, String>(1))
.map_err(|e| format!("migration M3: PRAGMA table_info query: {e}"))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("migration M3: PRAGMA table_info read: {e}"))?;
ns
};
let cols = [
("turn_cache_write_tokens", "TEXT"),
("cumulative_cache_write_tokens", "TEXT"),
("pricing_authority", "TEXT"),
("pricing_model", "TEXT"),
("pricing_cache_class", "TEXT"),
];
for (col, ty) in &cols {
if !names.iter().any(|n| n == col) {
tx.execute_batch(&format!(
"ALTER TABLE agent_metric_index ADD COLUMN {col} {ty}"
))
.map_err(|e| format!("migration M3: ALTER TABLE ({col}) failed: {e}"))?;
}
}
tx.execute(
"INSERT OR IGNORE INTO archive_migrations (name, applied_at) \
VALUES ('add_cache_write_and_pricing', ?1)",
rusqlite::params![std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64],
)
.map_err(|e| format!("migration M3: record marker: {e}"))?;
tx.commit()
.map_err(|e| format!("migration M3: commit: {e}"))?;
Ok(())
}
+7 -3
View File
@@ -285,9 +285,13 @@ fn test_remove_owner_p_kind_noop_when_kind_absent() {
#[test]
fn test_upsert_archived_event_is_idempotent() {
let conn = in_memory();
upsert_archived_event(&conn, "pk", "wss://r", "id1", 1, "author", 100, "{}", 200).unwrap();
// Second call must not error or duplicate.
upsert_archived_event(&conn, "pk", "wss://r", "id1", 1, "author", 100, "{}", 201).unwrap();
let first =
upsert_archived_event(&conn, "pk", "wss://r", "id1", 1, "author", 100, "{}", 200).unwrap();
assert!(first, "first insert of a new id must report newly-inserted");
// Second call must not error or duplicate, and must report false (no new row).
let second =
upsert_archived_event(&conn, "pk", "wss://r", "id1", 1, "author", 100, "{}", 201).unwrap();
assert!(!second, "duplicate insert must report NOT newly-inserted");
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM archived_events", [], |r| r.get(0))
.unwrap();
+2 -2
View File
@@ -195,8 +195,7 @@ pub fn run() {
.plugin(tauri_plugin_process::init());
// The global-shortcut plugin is omitted from test builds: linking it into
// the lib-test binary makes it fail to load on Windows
// (STATUS_ENTRYPOINT_NOT_FOUND) before any test runs.
// the lib-test binary makes it fail to load on Windows (STATUS_ENTRYPOINT_NOT_FOUND) before any test runs.
#[cfg(not(test))]
let builder = builder.plugin({
use tauri_plugin_global_shortcut::ShortcutState;
@@ -904,6 +903,7 @@ pub fn run() {
archive::read_archived_observer_events_for_channel,
archive::index_observer_channel_id,
archive::read_unindexed_observer_rows,
archive::get_agent_usage_series,
is_auto_update_supported,
set_window_vibrancy,
#[cfg(target_os = "macos")]
@@ -6,6 +6,10 @@ import {
compareObserverEvents,
} from "@/features/agents/observerRelayStore";
import { normalizePubkey } from "@/shared/lib/pubkey";
import {
isDocumentVisible,
subscribeDocumentVisibility,
} from "@/shared/lib/useDocumentVisible";
import type { ObserverEvent } from "./ui/agentSessionTypes";
/** Harness emits turn_liveness every ~10s (BUZZ_ACP_TURN_LIVENESS_SECS). */
@@ -134,6 +138,7 @@ function watermarkChannelKey(event: ObserverEvent): string {
const terminalAtByAgent = new Map<string, Map<string, number>>();
let pruneInterval: ReturnType<typeof setInterval> | null = null;
let unsubscribePruneVisibility: (() => void) | null = null;
function invalidateCache(agentKey: string) {
cachedTurnSummaries.delete(agentKey);
@@ -440,16 +445,31 @@ function processEvent(agentPubkey: string, event: ObserverEvent) {
}
}
function ensurePruneInterval() {
if (pruneInterval) return;
function startPruneInterval() {
if (pruneInterval || !isDocumentVisible()) return;
pruneExpired();
pruneInterval = setInterval(pruneExpired, PRUNE_INTERVAL_MS);
}
function pausePruneInterval() {
if (!pruneInterval) return;
clearInterval(pruneInterval);
pruneInterval = null;
}
function ensurePruneInterval() {
if (unsubscribePruneVisibility) return;
startPruneInterval();
unsubscribePruneVisibility = subscribeDocumentVisibility((visible) => {
if (visible) startPruneInterval();
else pausePruneInterval();
});
}
function stopPruneInterval() {
if (pruneInterval) {
clearInterval(pruneInterval);
pruneInterval = null;
}
pausePruneInterval();
unsubscribePruneVisibility?.();
unsubscribePruneVisibility = null;
}
export function subscribeActiveAgentTurns(listener: () => void) {
+16 -4
View File
@@ -18,6 +18,10 @@ import {
} from "@/features/channels/hooks";
import { updateCachedChannelMemberDisplayName } from "@/features/channels/channelMemberProfileCache";
import { evictUsersBatchEntries } from "@/features/profile/hooks";
import {
useAppFocused,
useFocusedRefetchInterval,
} from "@/shared/lib/useDocumentVisible";
import {
createManagedAgent,
deleteManagedAgent,
@@ -322,6 +326,7 @@ export function useManagedAgentPrereqsQuery(
}
export function useRelayAgentsQuery(options?: { enabled?: boolean }) {
const refetchInterval = useFocusedRefetchInterval(5 * 60_000);
return useQuery({
queryKey: relayAgentsQueryKey,
queryFn: listRelayAgents,
@@ -334,19 +339,21 @@ export function useRelayAgentsQuery(options?: { enabled?: boolean }) {
// the `agents-data-changed` event fires only for local persona/team/managed
// reconcile (kinds PERSONA/TEAM/MANAGED_AGENT), never for kind:10100. So we
// keep polling but at a relaxed cadence and pause it while backgrounded.
refetchInterval: 5 * 60_000,
refetchIntervalInBackground: false,
refetchInterval,
refetchOnWindowFocus: true,
enabled: options?.enabled,
});
}
export function useManagedAgentsQuery(options?: { enabled?: boolean }) {
const appFocused = useAppFocused();
return useQuery({
enabled: options?.enabled ?? true,
queryKey: managedAgentsQueryKey,
queryFn: listManagedAgents,
staleTime: 5_000,
refetchInterval: (query) => {
if (!appFocused) return false;
const agents = query.state.data as ManagedAgent[] | undefined;
// Only local "running" agents need polling: process state can change
// with no relay event to signal it, so this poll is the only liveness
@@ -357,6 +364,7 @@ export function useManagedAgentsQuery(options?: { enabled?: boolean }) {
? 5_000
: false;
},
refetchOnWindowFocus: true,
});
}
@@ -895,13 +903,15 @@ export function useManagedAgentLogQuery(
pubkey: string | null,
lineCount = 120,
) {
const refetchInterval = useFocusedRefetchInterval(pubkey ? 30_000 : false);
return useQuery({
queryKey: ["managed-agent-log", pubkey, lineCount],
queryFn: () => getManagedAgentLog(pubkey as string, lineCount),
enabled: pubkey !== null,
retry: false,
staleTime: 3_000,
refetchInterval: pubkey ? 30_000 : false,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -909,12 +919,14 @@ export const agentConfigSurfaceQueryKey = (pubkey: string) =>
["agent-config-surface", pubkey] as const;
export function useAgentConfigSurface(pubkey: string | null) {
const refetchInterval = useFocusedRefetchInterval(30_000);
return useQuery({
queryKey: agentConfigSurfaceQueryKey(pubkey ?? ""),
queryFn: () => getAgentConfigSurface(pubkey ?? ""),
enabled: !!pubkey,
staleTime: 10_000,
refetchInterval: 30_000,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -12,6 +12,7 @@ import {
} from "@/shared/api/tauriManagedAgents";
import { listManagedAgents } from "@/shared/api/tauri";
import type { ManagedAgent } from "@/shared/api/types";
import { useDocumentVisible } from "@/shared/lib/useDocumentVisible";
import { getAgentObserverSnapshot } from "../observerRelayStore";
import { getAgentWorkingState } from "../agentWorkingSignal";
import {
@@ -40,13 +41,17 @@ export function useAutoRestartPolicy() {
const edgesRef = React.useRef(new Map<string, AutoRestartEdgeState>());
const inFlightRef = React.useRef(new Set<string>());
const [, setTick] = React.useState(0);
const documentVisible = useDocumentVisible();
// Re-evaluate on an interval so the quiescence clock advances even when
// summaries and observer stores are quiet.
React.useEffect(() => {
if (!documentVisible) return;
setTick((t) => t + 1);
const timer = setInterval(() => setTick((t) => t + 1), POLICY_TICK_MS);
return () => clearInterval(timer);
}, []);
}, [documentVisible]);
// No dependency array by design: the tick pattern re-runs this effect
// every render so it reads live store state; all mutation is ref-local.
@@ -7,6 +7,7 @@ import {
} from "@/features/agents/lib/personaCatalogRelay";
import { invalidatePersonaEditCaches } from "@/features/agents/lib/personaEditCaches";
import { relayClient } from "@/shared/api/relayClient";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import {
setPersonaShared,
updatePersonaAndPublish,
@@ -19,12 +20,14 @@ export function personaCatalogQueryKey(communityId: string | null) {
}
export function usePersonaCatalogQuery(communityId: string | null) {
const refetchInterval = useFocusedRefetchInterval(120_000);
return useQuery<PersonaCatalogPublication[]>({
enabled: communityId !== null,
queryKey: personaCatalogQueryKey(communityId),
queryFn: fetchPersonaCatalogPublications,
staleTime: 30_000,
refetchInterval: 120_000,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -1,5 +1,7 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import {
createChannelTemplate,
deleteChannelTemplate,
@@ -16,11 +18,14 @@ import type {
export const channelTemplatesQueryKey = ["channel-templates"] as const;
export function useChannelTemplatesQuery() {
const refetchInterval = useFocusedRefetchInterval(30_000);
return useQuery({
queryKey: channelTemplatesQueryKey,
queryFn: listChannelTemplates,
staleTime: 30_000,
refetchInterval: 30_000,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -3,7 +3,9 @@ import test from "node:test";
import {
addForcedUnreadSource,
boundForcedUnreadMap,
forcedUnreadMarker,
MAX_FORCED_UNREAD_ENTRIES,
removeForcedUnreadSource,
} from "./forcedUnreadStore.ts";
@@ -49,6 +51,22 @@ test("clearing the only force owner removes the entry", () => {
assert.equal(removeForcedUnreadSource(entry, "inbox"), undefined);
});
test("forced unread map keeps the newest 500 insertion-ordered entries", () => {
const map = Object.fromEntries(
Array.from({ length: MAX_FORCED_UNREAD_ENTRIES + 2 }, (_, index) => [
`channel-${index}`,
index,
]),
);
const bounded = boundForcedUnreadMap(map);
assert.equal(Object.keys(bounded).length, MAX_FORCED_UNREAD_ENTRIES);
assert.equal(bounded["channel-0"], undefined);
assert.equal(bounded["channel-1"], undefined);
assert.equal(bounded[`channel-${MAX_FORCED_UNREAD_ENTRIES + 1}`], 501);
});
test("legacy persisted entries retain their read-marker baseline", () => {
assert.equal(forcedUnreadMarker(120), 120);
assert.equal(forcedUnreadMarker(null), null);
@@ -74,8 +74,16 @@ export function removeForcedUnreadSource(
}
const STORAGE_PREFIX = "buzz-forced-unread.v1";
export const MAX_FORCED_UNREAD_ENTRIES = 500;
const storageKey = (pubkey: string) => `${STORAGE_PREFIX}:${pubkey}`;
export function boundForcedUnreadMap(map: ForcedUnreadMap): ForcedUnreadMap {
const entries = Object.entries(map);
return entries.length <= MAX_FORCED_UNREAD_ENTRIES
? map
: Object.fromEntries(entries.slice(-MAX_FORCED_UNREAD_ENTRIES));
}
export const forcedUnreadStore = {
read(pubkey: string): ForcedUnreadMap {
try {
@@ -113,14 +121,17 @@ export const forcedUnreadStore = {
}
}
}
return result;
return boundForcedUnreadMap(result);
} catch {
return {};
}
},
write(pubkey: string, map: ForcedUnreadMap): void {
try {
window.localStorage.setItem(storageKey(pubkey), JSON.stringify(map));
window.localStorage.setItem(
storageKey(pubkey),
JSON.stringify(boundForcedUnreadMap(map)),
);
} catch {
// Ignore storage errors (private browsing, quota exceeded).
}
@@ -148,7 +159,9 @@ export function useForcedUnreadActions(
source,
);
if (next === current) return;
delete forcedUnreadRef.current[channelId];
forcedUnreadRef.current[channelId] = next;
forcedUnreadRef.current = boundForcedUnreadMap(forcedUnreadRef.current);
persist();
},
[forcedUnreadRef, getOwnTimestamp, persist],
+4 -2
View File
@@ -33,6 +33,7 @@ import type {
UpdateChannelInput,
} from "@/shared/api/types";
import { useIdentityQuery } from "@/shared/api/hooks";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import { useCommunities } from "@/features/communities/useCommunities";
import { canAddChannelMembers } from "@/features/channels/lib/channelMemberAdmission";
import {
@@ -193,6 +194,7 @@ function setChannelArchivedState(
export function useChannelsQuery(options?: { enabled?: boolean }) {
const { activeCommunity } = useCommunities();
const relayUrl = activeCommunity?.relayUrl ?? null;
const refetchInterval = useFocusedRefetchInterval(60_000);
return useQuery({
enabled: options?.enabled ?? true,
@@ -215,8 +217,8 @@ export function useChannelsQuery(options?: { enabled?: boolean }) {
: undefined,
initialDataUpdatedAt: 0,
staleTime: 60_000,
refetchInterval: 60_000,
refetchIntervalInBackground: false,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -22,8 +22,8 @@ test("open channels accept adds from anyone, member or not", () => {
);
});
test("private channels accept adds only from owners/admins", () => {
for (const selfRole of ["owner", "admin"]) {
test("private channels accept adds from every active member role", () => {
for (const selfRole of ["owner", "admin", "member", "bot", "guest"]) {
assert.equal(
canAddChannelMembers({
channelType: "stream",
@@ -35,17 +35,15 @@ test("private channels accept adds only from owners/admins", () => {
);
}
for (const selfRole of ["member", "bot", "guest", null]) {
assert.equal(
canAddChannelMembers({
channelType: "stream",
visibility: "private",
selfRole,
}),
false,
`${selfRole} must not be able to add`,
);
}
assert.equal(
canAddChannelMembers({
channelType: "stream",
visibility: "private",
selfRole: null,
}),
false,
"a non-member must not be able to add",
);
});
test("DMs never accept adds, even from an owner", () => {
@@ -67,13 +65,13 @@ test("DMs never accept adds, even from an owner", () => {
);
});
test("unknown visibility fails closed for non-elevated callers", () => {
test("unknown visibility fails closed", () => {
assert.equal(
canAddChannelMembers({ channelType: "stream", selfRole: "member" }),
false,
);
assert.equal(
canAddChannelMembers({ channelType: "stream", selfRole: "owner" }),
true,
false,
);
});
@@ -4,9 +4,8 @@
*
* - DMs: nobody — membership is fixed at creation.
* - Open channels: anyone, member or not.
* - Private channels: owners/admins only. A plain member extending access to
* channel history is exactly what the relay now rejects, so the affordance
* must not be offered.
* - Private channels: any active member. The relay separately reserves elevated
* role grants and active-role changes for owners/admins.
*
* Unknown visibility fails closed — the relay is the authority and a hidden
* button is cheaper than an opaque rejection.
@@ -28,9 +27,13 @@ export function canAddChannelMembers({
return true;
}
return selfRole === "owner" || selfRole === "admin";
if (visibility === "private") {
return selfRole != null;
}
return false;
}
/** Explains a denied add so the user isn't left guessing at a missing button. */
export const PRIVATE_CHANNEL_ADD_DENIED_MESSAGE =
"Only channel owners and admins can add people to a private channel.";
"Only channel members can add people to a private channel.";
@@ -216,7 +216,11 @@ export const ChannelPane = React.memo(function ChannelPane({
bannerState: welcomeComposerBannerState,
completeBanner: completeWelcomeComposerBanner,
dismissBanner: handleDismissWelcomeBanner,
} = useWelcomeComposerBanner(activeChannelId, isActiveWelcomeChannel);
} = useWelcomeComposerBanner(
activeChannelId,
isActiveWelcomeChannel,
currentPubkey ?? null,
);
const isEditInThread =
editTarget != null &&
threadHeadMessage != null &&
@@ -251,8 +251,8 @@ export function MembersSidebar({
visibility: channel?.visibility,
selfRole: selfMember?.role,
});
// Distinguish "you can't add here" from "nothing to add" so a plain member of
// a private channel gets the reason instead of a silently missing affordance.
// Distinguish "you can't add here" from "nothing to add" so a non-member
// viewing a private channel gets the reason instead of a silently missing affordance.
const showPrivateAddDeniedNotice =
!canAddMembers &&
selfMember !== null &&
@@ -443,6 +443,10 @@ export function WelcomeComposerGuidanceLayer({
settingUp,
state,
}: WelcomeComposerGuidanceLayerProps) {
if (state === "hidden") {
return null;
}
return (
<div className="relative" data-testid="welcome-composer-guidance-layer">
<ComposerDockGlassBackdrop
@@ -8,11 +8,14 @@ import {
type WelcomeComposerBannerState,
} from "@/features/channels/ui/WelcomeComposerBanner";
const completedWelcomeComposerIdentityPubkeys = new Set<string>();
/**
* Manages the Welcome-channel composer hint banner's state machine.
*
* Tracks which channels have been completed within the session so the banner
* stays hidden on re-entry. Exposes three transitions:
* Remembers completion across the Welcome experience per identity for this app
* session, so the hint stays hidden while moving between the private and
* starter Welcome channels without leaking dismissal to another identity.
* - `completeBanner`: agent-mention path — plays the "Nice work." success
* animation before auto-dismissing.
* - `dismissBanner`: manual X-button path — immediately begins the slide-down
@@ -21,12 +24,12 @@ import {
export function useWelcomeComposerBanner(
activeChannelId: string | null,
isActiveWelcomeChannel: boolean,
identityPubkey: string | null,
): {
bannerState: WelcomeComposerBannerState;
completeBanner: () => void;
dismissBanner: () => void;
} {
const completedChannelIdsRef = React.useRef(new Set<string>());
const dismissTimerRef = React.useRef<number | null>(null);
const hideTimerRef = React.useRef<number | null>(null);
const [bannerState, setBannerState] =
@@ -48,15 +51,15 @@ export function useWelcomeComposerBanner(
React.useEffect(() => {
clearTimers();
if (
activeChannelId &&
isActiveWelcomeChannel &&
completedChannelIdsRef.current.has(activeChannelId)
identityPubkey &&
completedWelcomeComposerIdentityPubkeys.has(identityPubkey)
) {
setBannerState("hidden");
return;
}
setBannerState("prompt");
}, [activeChannelId, clearTimers, isActiveWelcomeChannel]);
}, [clearTimers, identityPubkey, isActiveWelcomeChannel]);
const scheduleHide = React.useCallback(() => {
hideTimerRef.current = window.setTimeout(
@@ -70,30 +73,42 @@ export function useWelcomeComposerBanner(
}, []);
const completeBanner = React.useCallback(() => {
if (!activeChannelId || !isActiveWelcomeChannel) {
if (!activeChannelId || !isActiveWelcomeChannel || !identityPubkey) {
return;
}
clearTimers();
completedChannelIdsRef.current.add(activeChannelId);
completedWelcomeComposerIdentityPubkeys.add(identityPubkey);
setBannerState("complete");
dismissTimerRef.current = window.setTimeout(() => {
setBannerState("dismissing");
dismissTimerRef.current = null;
scheduleHide();
}, WELCOME_PERSONA_ROTATION_MS + WELCOME_COMPOSER_BANNER_SUCCESS_SETTLE_MS);
}, [activeChannelId, clearTimers, isActiveWelcomeChannel, scheduleHide]);
}, [
activeChannelId,
clearTimers,
identityPubkey,
isActiveWelcomeChannel,
scheduleHide,
]);
const dismissBanner = React.useCallback(() => {
if (!activeChannelId || !isActiveWelcomeChannel) {
if (!activeChannelId || !isActiveWelcomeChannel || !identityPubkey) {
return;
}
clearTimers();
completedChannelIdsRef.current.add(activeChannelId);
completedWelcomeComposerIdentityPubkeys.add(identityPubkey);
setBannerState("dismissing");
scheduleHide();
}, [activeChannelId, clearTimers, isActiveWelcomeChannel, scheduleHide]);
}, [
activeChannelId,
clearTimers,
identityPubkey,
isActiveWelcomeChannel,
scheduleHide,
]);
return { bannerState, completeBanner, dismissBanner };
}
@@ -0,0 +1,42 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
boundCommunityIconCache,
loadCachedCommunityIcon,
MAX_CACHED_COMMUNITY_ICON_LENGTH,
MAX_CACHED_COMMUNITY_ICONS,
saveCachedCommunityIcon,
} from "./communityIconCache.ts";
test("community icon cache caps entries and rejects oversized icons", () => {
const cache = Object.fromEntries(
Array.from({ length: MAX_CACHED_COMMUNITY_ICONS + 1 }, (_, index) => [
`relay-${index}`,
`icon-${index}`,
]),
);
cache.oversized = "x".repeat(MAX_CACHED_COMMUNITY_ICON_LENGTH + 1);
const bounded = boundCommunityIconCache(cache);
assert.equal(Object.keys(bounded).length, MAX_CACHED_COMMUNITY_ICONS);
assert.equal(bounded["relay-0"], undefined);
assert.equal(bounded.oversized, undefined);
assert.equal(bounded[`relay-${MAX_CACHED_COMMUNITY_ICONS}`], "icon-32");
});
test("community icon cache accepts relay-sized icons above 64 KiB", () => {
const values = new Map([
["buzz-community-icons", JSON.stringify({ relay: "prior-icon" })],
]);
globalThis.localStorage = {
getItem: (key) => values.get(key) ?? null,
setItem: (key, value) => values.set(key, String(value)),
};
const acceptedIcon = "x".repeat(80 * 1024);
saveCachedCommunityIcon("relay", acceptedIcon);
assert.equal(loadCachedCommunityIcon("relay"), acceptedIcon);
});
@@ -5,13 +5,28 @@
*/
const ICON_CACHE_KEY = "buzz-community-icons";
export const MAX_CACHED_COMMUNITY_ICONS = 32;
// Keep aligned with MAX_WORKSPACE_ICON_DATA_URL_LEN in
// crates/buzz-relay/src/handlers/relay_admin.rs.
export const MAX_CACHED_COMMUNITY_ICON_LENGTH = 98_304;
export function boundCommunityIconCache(
cache: Record<string, string>,
): Record<string, string> {
const entries = Object.entries(cache).filter(
([, icon]) =>
typeof icon === "string" &&
icon.length <= MAX_CACHED_COMMUNITY_ICON_LENGTH,
);
return Object.fromEntries(entries.slice(-MAX_CACHED_COMMUNITY_ICONS));
}
function loadCache(): Record<string, string> {
try {
const raw = localStorage.getItem(ICON_CACHE_KEY);
const parsed: unknown = raw ? JSON.parse(raw) : null;
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
return parsed as Record<string, string>;
return boundCommunityIconCache(parsed as Record<string, string>);
}
} catch {
// Corrupt cache — fall through to empty.
@@ -28,13 +43,17 @@ export function saveCachedCommunityIcon(
icon: string | null,
): void {
const cache = loadCache();
if (icon) {
if (icon && icon.length <= MAX_CACHED_COMMUNITY_ICON_LENGTH) {
delete cache[relayUrl];
cache[relayUrl] = icon;
} else {
delete cache[relayUrl];
}
try {
localStorage.setItem(ICON_CACHE_KEY, JSON.stringify(cache));
localStorage.setItem(
ICON_CACHE_KEY,
JSON.stringify(boundCommunityIconCache(cache)),
);
} catch {
// Quota exceeded — the icon still renders from the in-memory query.
}
@@ -5,10 +5,7 @@ import { HostedCommunityOnboarding } from "@/features/communities/ui/HostedCommu
import { useCommunityOnboarding } from "@/features/onboarding/communityOnboarding";
import { InviteRedeemForm } from "@/features/onboarding/ui/InviteRedeemForm";
import { OnboardingChrome } from "@/features/onboarding/ui/OnboardingChrome";
import {
OnboardingFooter,
OnboardingFooterProvider,
} from "@/features/onboarding/ui/OnboardingFooter";
import { OnboardingFooterProvider } from "@/features/onboarding/ui/OnboardingFooter";
import {
type OnboardingTransitionDirection,
OnboardingSlideTransition,
@@ -92,27 +89,49 @@ export function WelcomeSetup({
[communityOnboarding, page],
);
const beginHostedCommunity = React.useCallback(
() => setIsHostedSignInOpen(true),
[],
);
const transitionDirection =
transitionMode === "backward" ? "backward" : "forward";
const welcomeEffect =
transitionMode === "backward" ? "line-slide" : "mask-reveal-up";
const backAction =
page === "welcome" && onBack
? { onClick: onBack, testId: "welcome-setup-back" }
: page === "existing"
? {
onClick: () => showPage("welcome"),
testId: "existing-back",
}
: page === "join"
? {
onClick: () => showPage("welcome"),
testId: "welcome-join-back",
}
: page === "member"
? {
onClick: () => showPage("existing"),
testId: "welcome-member-back",
}
: undefined;
return (
<div
className="buzz-onboarding-neutral-theme buzz-startup-shell flex h-dvh items-start justify-center overflow-y-auto bg-background px-4 pb-36 pt-[106px] text-foreground"
data-system-color-scheme={systemColorScheme}
data-testid="welcome-setup"
>
<StartupWindowDragRegion />
<OnboardingChrome current={5} />
<OnboardingFooterProvider>
<OnboardingFooterProvider backAction={backAction}>
<div className="relative flex min-h-0 w-full max-w-[920px] flex-1 flex-col items-center text-center">
{page === "welcome" ? (
<OnboardingSlideTransition
className="flex h-full min-h-0 w-full flex-col items-center text-center"
containerClassName="h-full min-h-0 [&>.buzz-onboarding-transition-line]:h-full"
direction={transitionDirection}
effect={welcomeEffect}
transitionKey={`welcome-${welcomeEffect}-${transitionDirection}`}
transitionKey={`welcome-${transitionDirection}`}
>
<div className="w-full max-w-[760px]">
<h1 className="text-title font-normal">
@@ -144,7 +163,7 @@ export function WelcomeSetup({
>
<button
data-testid="community-choice-create"
onClick={() => setIsHostedSignInOpen(true)}
onClick={beginHostedCommunity}
type="button"
>
Create a community
@@ -164,19 +183,6 @@ export function WelcomeSetup({
</button>
</Card>
</div>
{onBack ? (
<OnboardingFooter>
<Button
className="h-9 rounded-full bg-foreground/10 px-6 hover:bg-foreground/15"
data-testid="welcome-setup-back"
onClick={onBack}
type="button"
variant="ghost"
>
Back
</Button>
</OnboardingFooter>
) : null}
</OnboardingSlideTransition>
) : page === "existing" ? (
<OnboardingSlideTransition
@@ -201,7 +207,7 @@ export function WelcomeSetup({
>
<button
data-testid="existing-choice-owner"
onClick={() => setIsHostedSignInOpen(true)}
onClick={beginHostedCommunity}
type="button"
>
I own the community
@@ -221,17 +227,6 @@ export function WelcomeSetup({
</button>
</Card>
</div>
<OnboardingFooter>
<Button
className="h-9 rounded-full bg-foreground/10 px-6 hover:bg-foreground/15"
data-testid="existing-back"
onClick={() => showPage("welcome")}
type="button"
variant="ghost"
>
Back
</Button>
</OnboardingFooter>
</OnboardingSlideTransition>
) : page === "owned" ? (
<OnboardingSlideTransition
+5 -1
View File
@@ -10,6 +10,7 @@ import {
setCustomEmoji,
} from "@/shared/api/customEmoji";
import { relayClient } from "@/shared/api/relayClient";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import type { CustomEmoji } from "@/shared/lib/remarkCustomEmoji";
/**
@@ -28,13 +29,16 @@ export const customEmojiQueryKey = ["custom-emoji"] as const;
export const ownCustomEmojiQueryKey = ["custom-emoji-own"] as const;
export function useCustomEmojiQuery() {
const refetchInterval = useFocusedRefetchInterval(120_000);
return useQuery<CustomEmoji[]>({
queryKey: customEmojiQueryKey,
queryFn: listCustomEmoji,
// The palette changes rarely; avoid refetch storms while the picker is open,
// but poll every 2 minutes as a backstop for any missed live event.
staleTime: 60_000,
refetchInterval: 120_000,
refetchInterval,
refetchOnWindowFocus: true,
});
}
+9 -2
View File
@@ -1,6 +1,7 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { getForumPosts, getForumThread } from "@/shared/api/forum";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import { useRelaySelfQuery } from "@/features/moderation/hooks";
import { deleteMessage, sendChannelMessage } from "@/shared/api/tauri";
import type {
@@ -19,6 +20,8 @@ export function forumThreadQueryKey(channelId: string, eventId: string) {
}
export function useForumPostsQuery(channel: Channel | null) {
const refetchInterval = useFocusedRefetchInterval(15_000);
const channelId = channel?.id ?? "";
const enabled = channel !== null && channel.channelType === "forum";
const relaySelfPubkey = useRelaySelfQuery(enabled).data;
@@ -28,7 +31,8 @@ export function useForumPostsQuery(channel: Channel | null) {
queryKey: [...forumPostsQueryKey(channelId), relaySelfPubkey ?? null],
queryFn: () => getForumPosts(channelId, 50, undefined, relaySelfPubkey),
staleTime: 15_000,
refetchInterval: 15_000,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -36,6 +40,8 @@ export function useForumThreadQuery(
channelId: string | null,
eventId: string | null,
) {
const refetchInterval = useFocusedRefetchInterval(10_000);
const enabled = channelId !== null && eventId !== null;
const relaySelfPubkey = useRelaySelfQuery(enabled).data;
@@ -54,7 +60,8 @@ export function useForumThreadQuery(
relaySelfPubkey,
),
staleTime: 10_000,
refetchInterval: 10_000,
refetchInterval,
refetchOnWindowFocus: true,
});
}
+4 -1
View File
@@ -2,10 +2,12 @@ import { useQuery } from "@tanstack/react-query";
import { getHomeFeed } from "@/shared/api/tauri";
import { useRelayConnection } from "@/shared/api/useRelayConnection";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
export function useHomeFeedQuery() {
const connectionState = useRelayConnection();
const connected = connectionState === "connected";
const refetchInterval = useFocusedRefetchInterval(connected ? 30_000 : false);
return useQuery({
queryKey: ["home-feed"],
@@ -19,6 +21,7 @@ export function useHomeFeedQuery() {
// Pause background polling on degraded/stalled/disconnected connections.
// The relay can't serve the request anyway, and the spurious failures
// consume quota that the recovery path needs.
refetchInterval: connected ? 30_000 : false,
refetchInterval,
refetchOnWindowFocus: true,
});
}
+2 -11
View File
@@ -4,6 +4,7 @@ import * as React from "react";
import { setupAudioWorklet, type AudioWorkletHandle } from "./lib/audioWorklet";
import { type AudioInputDevice, useAudioDevices } from "./lib/useAudioDevices";
import { usePipelineHotstart } from "./lib/usePipelineHotstart";
import { formatHuddleActionError } from "./lib/huddleError";
import {
type VoiceInputMode,
@@ -47,7 +48,6 @@ const HUDDLE_AUDIO_STATE_EVENT = "huddle-audio-state";
const HUDDLE_AUDIO_LEVEL_EVENT = "huddle-audio-level";
const MIC_ANALYSER_UPDATE_INTERVAL_MS = 33;
const PIPELINE_HOTSTART_INTERVAL_MS = 15_000;
const MIC_INITIAL_NOISE_FLOOR = 0.01;
const MIC_VOICE_GATE_ON_RMS = 0.018;
const MIC_VOICE_GATE_OFF_RMS = 0.012;
@@ -778,16 +778,7 @@ export function HuddleProvider({
selfPubkeyRef,
);
// Pipeline hot-start — check if voice models finished downloading mid-huddle
React.useEffect(() => {
if (!ephemeralChannelId) return;
const id = window.setInterval(() => {
invoke("check_pipeline_hotstart").catch(() => {
/* best-effort */
});
}, PIPELINE_HOTSTART_INTERVAL_MS);
return () => window.clearInterval(id);
}, [ephemeralChannelId]);
usePipelineHotstart(ephemeralChannelId);
// Mic level analyser — drives the voice activity indicator
React.useEffect(() => {
@@ -22,6 +22,7 @@ import type { RelayEvent } from "@/shared/api/types";
import { KIND_HUDDLE_REACTION } from "@/shared/constants/kinds";
import { cn } from "@/shared/lib/cn";
import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
import { useDocumentVisible } from "@/shared/lib/useDocumentVisible";
import { Button } from "@/shared/ui/button";
import { useEmojiBurst } from "@/shared/ui/EmojiBurstProvider";
import { Popover, PopoverContent, PopoverTrigger } from "@/shared/ui/popover";
@@ -150,6 +151,7 @@ export function HuddleBar({
onOpenHuddleWindow,
onVisibilityChange,
}: HuddleBarProps) {
const documentVisible = useDocumentVisible();
const {
leaveHuddle,
micConnected,
@@ -238,7 +240,7 @@ export function HuddleBar({
}
}
void fetchState();
if (documentVisible) void fetchState();
// Primary: listen for Rust-emitted state change events
listen<HuddleState>("huddle-state-changed", (event) => {
@@ -253,21 +255,27 @@ export function HuddleBar({
// Fallback in case events are missed; keep it slow so normal huddle use is
// event-driven and does not keep a sync IPC command warm on the main thread.
const id = window.setInterval(
() => void fetchState(),
HUDDLE_STATE_FALLBACK_INTERVAL_MS,
);
const id = documentVisible
? window.setInterval(
() => void fetchState(),
HUDDLE_STATE_FALLBACK_INTERVAL_MS,
)
: null;
return () => {
cancelled = true;
unlisten?.();
window.clearInterval(id);
if (id !== null) window.clearInterval(id);
};
}, [applyIncomingState]);
}, [applyIncomingState, documentVisible]);
const huddlePhase = state?.phase;
React.useEffect(() => {
if (huddlePhase !== "active" && huddlePhase !== "connected") return;
if (
!documentVisible ||
(huddlePhase !== "active" && huddlePhase !== "connected")
)
return;
let cancelled = false;
@@ -310,8 +318,12 @@ export function HuddleBar({
return () => {
cancelled = true;
window.clearInterval(id);
setModelStatus(null); // Clear stale status on huddle end/phase change.
};
}, [documentVisible, huddlePhase]);
React.useEffect(() => {
if (huddlePhase === "active" || huddlePhase === "connected") return;
setModelStatus(null);
}, [huddlePhase]);
const isHuddleVisible = isVisibleHuddleState(state);
@@ -0,0 +1,22 @@
import { invoke } from "@tauri-apps/api/core";
import * as React from "react";
const PIPELINE_HOTSTART_INTERVAL_MS = 15_000;
/** Check if voice models finished downloading mid-huddle. */
export function usePipelineHotstart(ephemeralChannelId: string | null) {
React.useEffect(() => {
if (!ephemeralChannelId) return;
const checkPipelineHotstart = () => {
invoke("check_pipeline_hotstart").catch(() => {
/* best-effort */
});
};
checkPipelineHotstart();
const id = window.setInterval(
checkPipelineHotstart,
PIPELINE_HOTSTART_INTERVAL_MS,
);
return () => window.clearInterval(id);
}, [ephemeralChannelId]);
}
@@ -2,6 +2,10 @@ import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import * as React from "react";
import {
isDocumentVisible,
subscribeDocumentVisibility,
} from "@/shared/lib/useDocumentVisible";
import { buildHuddleTtsLiveFilter } from "@/shared/api/relayChannelFilters";
import { relayClient } from "@/shared/api/relayClient";
import {
@@ -209,10 +213,29 @@ export function useTtsSubscription(
}
// Initial load + periodic refresh (catches mid-huddle agent additions).
// Keep the live subscription installed while hidden, but quiesce its REST
// membership backstop and refresh immediately when the window returns.
let agentRefreshId: number | null = null;
const startAgentRefresh = (refreshNow: boolean) => {
if (agentRefreshId !== null) window.clearInterval(agentRefreshId);
agentRefreshId = null;
if (!isDocumentVisible()) return;
if (refreshNow) void loadAgentPubkeys();
agentRefreshId = window.setInterval(() => {
void loadAgentPubkeys();
}, AGENT_PUBKEY_REFRESH_INTERVAL_MS);
};
void loadAgentPubkeys(true);
const agentRefreshId = window.setInterval(() => {
void loadAgentPubkeys();
}, AGENT_PUBKEY_REFRESH_INTERVAL_MS);
startAgentRefresh(false);
const unsubscribeDocumentVisibility = subscribeDocumentVisibility(
(visible) => {
if (visible) startAgentRefresh(true);
else if (agentRefreshId !== null) {
window.clearInterval(agentRefreshId);
agentRefreshId = null;
}
},
);
// Install the state listener before requesting a snapshot. If a newer
// event arrives while IPC is pending, it supersedes the stale snapshot.
@@ -302,7 +325,8 @@ export function useTtsSubscription(
speakInOrder.setEnabled(false);
cleanup?.();
unlistenHuddleState?.();
window.clearInterval(agentRefreshId);
unsubscribeDocumentVisibility();
if (agentRefreshId !== null) window.clearInterval(agentRefreshId);
if (agentVerificationRetryId !== null) {
window.clearTimeout(agentVerificationRetryId);
}
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import test from "node:test";
import { onAgentMetricsChanged } from "@/shared/api/tauriArchive";
import { ArchiveSyncManager } from "./archiveSyncManager.ts";
// ── Fakes ────────────────────────────────────────────────────────────────────
@@ -41,6 +42,7 @@ function makeFakeArchive() {
let subs = [];
const archiveCalls = [];
const listeners = new Set();
let nextPersistedAgentMetrics = 0;
return {
async listSaveSubscriptions() {
@@ -81,7 +83,13 @@ function makeFakeArchive() {
},
async archiveEvents(candidates) {
archiveCalls.push(candidates);
return { persisted: candidates.length, dropped: 0 };
const persistedAgentMetrics = nextPersistedAgentMetrics;
nextPersistedAgentMetrics = 0;
return {
persisted: candidates.length,
persistedAgentMetrics,
dropped: 0,
};
},
onSubscriptionChange(listener) {
listeners.add(listener);
@@ -91,6 +99,10 @@ function makeFakeArchive() {
setSubs(s) {
subs = s;
},
/** Next archiveEvents() call reports this many newly persisted agent metrics. */
setNextPersistedAgentMetrics(n) {
nextPersistedAgentMetrics = n;
},
};
}
@@ -803,6 +815,177 @@ test("manager_handles_out_of_order_list_resolution", async () => {
);
});
test("manager_notifies_agent_metrics_changed_when_persisted_agent_metrics_positive", async () => {
const relay = makeFakeRelayClient();
const archive = makeFakeArchive();
archive.setSubs([
{
scopeType: "owner_p",
scopeValue: "agent-pk",
kinds: [44200],
identityPubkey: "pk",
relayUrl: "wss://r",
createdAt: 0,
},
]);
const mgr = makeManager(relay, archive, { flushBatchSize: 1 });
await mgr.start();
await tick();
let fired = 0;
const off = onAgentMetricsChanged(() => {
fired++;
});
archive.setNextPersistedAgentMetrics(1);
const filter = JSON.parse([...relay.subs.keys()][0]);
relay.push(filter, {
id: "metric-1",
kind: 44200,
pubkey: "agent-pk",
created_at: 1,
content: "encrypted",
tags: [],
});
await tick();
assert.equal(fired, 1, "notifier fires once when persistedAgentMetrics > 0");
off();
mgr.destroy();
});
test("manager_does_not_notify_agent_metrics_changed_when_persisted_agent_metrics_zero", async () => {
const relay = makeFakeRelayClient();
const archive = makeFakeArchive();
archive.setSubs([
{
scopeType: "channel_h",
scopeValue: "chan-1",
kinds: [9],
identityPubkey: "pk",
relayUrl: "wss://r",
createdAt: 0,
},
]);
const mgr = makeManager(relay, archive, { flushBatchSize: 1 });
await mgr.start();
await tick();
let fired = 0;
const off = onAgentMetricsChanged(() => {
fired++;
});
// Non-metric event; archiveEvents defaults to persistedAgentMetrics: 0.
const filter = JSON.parse([...relay.subs.keys()][0]);
relay.push(filter, {
id: "ev1",
kind: 9,
pubkey: "pk",
created_at: 1,
content: "hi",
tags: [],
});
await tick();
assert.equal(fired, 0, "notifier must not fire for persistedAgentMetrics: 0");
off();
mgr.destroy();
});
test("manager_does_not_notify_agent_metrics_changed_on_archive_events_rejection", async () => {
const relay = makeFakeRelayClient();
const archive = makeFakeArchive();
archive.setSubs([
{
scopeType: "owner_p",
scopeValue: "agent-pk",
kinds: [44200],
identityPubkey: "pk",
relayUrl: "wss://r",
createdAt: 0,
},
]);
const mgr = makeManager(relay, archive, {
flushBatchSize: 1,
archiveEvents: async () => {
throw new Error("simulated archive_events failure");
},
});
await mgr.start();
await tick();
let fired = 0;
const off = onAgentMetricsChanged(() => {
fired++;
});
const filter = JSON.parse([...relay.subs.keys()][0]);
relay.push(filter, {
id: "metric-1",
kind: 44200,
pubkey: "agent-pk",
created_at: 1,
content: "encrypted",
tags: [],
});
await tick();
assert.equal(fired, 0, "a rejected archiveEvents call must never notify");
off();
mgr.destroy();
});
test("manager_notifies_agent_metrics_changed_on_destroy_flush", async () => {
const relay = makeFakeRelayClient();
const archive = makeFakeArchive();
archive.setSubs([
{
scopeType: "owner_p",
scopeValue: "agent-pk",
kinds: [44200],
identityPubkey: "pk",
relayUrl: "wss://r",
createdAt: 0,
},
]);
// Large batch size / idle so the event stays buffered until destroy() flushes it.
const mgr = makeManager(relay, archive, {
flushBatchSize: 100,
flushIdleMs: 10000,
});
await mgr.start();
await tick();
let fired = 0;
const off = onAgentMetricsChanged(() => {
fired++;
});
archive.setNextPersistedAgentMetrics(1);
const filter = JSON.parse([...relay.subs.keys()][0]);
relay.push(filter, {
id: "metric-1",
kind: 44200,
pubkey: "agent-pk",
created_at: 1,
content: "encrypted",
tags: [],
});
assert.equal(archive.archiveCalls.length, 0, "buffered, not yet flushed");
mgr.destroy();
await tick();
assert.equal(archive.archiveCalls.length, 1, "destroy() flushes the buffer");
assert.equal(
fired,
1,
"destroy flush notifies when persistedAgentMetrics > 0",
);
off();
});
test("manager_flushes_buffer_on_destroy", async () => {
const relay = makeFakeRelayClient();
const archive = makeFakeArchive();
@@ -4,7 +4,9 @@ import type { RelayEvent } from "@/shared/api/types";
import {
archiveEvents as defaultArchiveEvents,
listSaveSubscriptions as defaultListSaveSubscriptions,
notifyAgentMetricsChanged,
onSubscriptionChange as defaultOnSubscriptionChange,
type ArchiveBatchResult,
type SaveSubscription,
type ScopeType,
} from "@/shared/api/tauriArchive";
@@ -30,7 +32,7 @@ export interface ArchiveSyncDeps {
rawEventJson: string;
matchedScope: { scopeType: ScopeType; scopeValue: string };
}>,
) => Promise<unknown>;
) => Promise<ArchiveBatchResult>;
onSubscriptionChange: (listener: () => void) => () => void;
flushBatchSize?: number;
flushIdleMs?: number;
@@ -135,9 +137,7 @@ export class ArchiveSyncManager {
// Flush any buffered events before tearing down.
if (this.buffer.length > 0) {
const toFlush = this.buffer.splice(0);
void this.deps.archiveEvents(toFlush).catch((err: unknown) => {
console.warn("[archiveSyncManager] flush on destroy failed:", err);
});
this.sendBatch(toFlush, "flush on destroy failed");
}
for (const [, unsub] of this.active) {
void unsub();
@@ -292,9 +292,33 @@ export class ArchiveSyncManager {
}
if (this.buffer.length === 0) return;
const batch = this.buffer.splice(0);
void this.deps.archiveEvents(batch).catch((err: unknown) => {
console.warn("[archiveSyncManager] archive_events failed:", err);
});
this.sendBatch(batch, "archive_events failed");
}
/**
* Fire-and-forget `archiveEvents(batch)`, shared by the idle/size-triggered
* flush and the destroy-time flush. Notifies `onAgentMetricsChanged`
* subscribers only when the backend confirms `persistedAgentMetrics > 0` —
* the backend is authoritative, so a rejected call, a duplicate-only batch,
* or a batch with no kind-44200 events never notifies.
*/
private sendBatch(
batch: Array<{
rawEventJson: string;
matchedScope: { scopeType: ScopeType; scopeValue: string };
}>,
errLabel: string,
): void {
void this.deps
.archiveEvents(batch)
.then((result) => {
if (result.persistedAgentMetrics > 0) {
notifyAgentMetricsChanged();
}
})
.catch((err: unknown) => {
console.warn(`[archiveSyncManager] ${errLabel}:`, err);
});
}
}
@@ -0,0 +1,91 @@
import { getMentionOffsets } from "./hasMention";
export type MentionPubkeyCandidate = {
displayName: string | null;
isMember: boolean;
pubkey?: string;
};
type MentionMatch = {
displayName: string;
pubkey?: string;
};
function normalizeDisplayName(name: string): string {
return name.trim().toLowerCase();
}
/**
* Returns explicit selected mention pubkeys and manually typed channel-member
* mentions. At each `@` offset, only the longest valid display name wins so a
* member whose name prefixes another member is not spuriously tagged.
*/
export function extractMentionPubkeys({
text,
selectedMentions,
selectedDisplayNames,
memberCandidates,
}: {
text: string;
selectedMentions: ReadonlyMap<string, string>;
selectedDisplayNames?: Iterable<string>;
memberCandidates: readonly MentionPubkeyCandidate[];
}): string[] {
const selectedNames = new Set(
[...selectedMentions.keys(), ...(selectedDisplayNames ?? [])].map(
normalizeDisplayName,
),
);
const matchesByOffset = new Map<number, MentionMatch[]>();
const addMatches = (displayName: string, pubkey?: string) => {
const trimmedName = displayName.trim();
if (!trimmedName) return;
for (const offset of getMentionOffsets(text, trimmedName)) {
const matches = matchesByOffset.get(offset) ?? [];
matches.push({ displayName: trimmedName, pubkey });
matchesByOffset.set(offset, matches);
}
};
for (const [displayName, pubkey] of selectedMentions) {
addMatches(displayName, pubkey);
}
for (const displayName of selectedDisplayNames ?? []) {
addMatches(displayName);
}
for (const candidate of memberCandidates) {
if (
candidate.pubkey &&
candidate.isMember &&
candidate.displayName &&
!selectedNames.has(normalizeDisplayName(candidate.displayName))
) {
addMatches(candidate.displayName, candidate.pubkey);
}
}
const winningPubkeys = new Set<string>();
for (const matches of matchesByOffset.values()) {
const longestNameLength = Math.max(
...matches.map((match) => match.displayName.length),
);
for (const match of matches) {
if (match.pubkey && match.displayName.length === longestNameLength) {
winningPubkeys.add(match.pubkey);
}
}
}
const pubkeys: string[] = [];
for (const [, pubkey] of selectedMentions) {
if (winningPubkeys.delete(pubkey)) pubkeys.push(pubkey);
}
for (const candidate of memberCandidates) {
if (candidate.pubkey && winningPubkeys.delete(candidate.pubkey)) {
pubkeys.push(candidate.pubkey);
}
}
return pubkeys;
}
@@ -140,14 +140,24 @@ function maskMarkdownCode(text: string): string {
*
* Exported separately so it can be unit-tested without importing React.
*/
export function getMentionOffset(text: string, name: string): number | null {
export function getMentionOffsets(text: string, name: string): number[] {
const escaped = escapeRegExp(name);
const pattern = new RegExp(
`(^|\\s|\\(|[*_]{1,3}|\\|\\|)(@${escaped})(?=\\|\\||[\\s,;.!?:)\\]}*_]|$)`,
"i",
"gi",
);
const match = pattern.exec(maskMarkdownCode(text));
return match ? match.index + match[1].length : null;
const maskedText = maskMarkdownCode(text);
const offsets: number[] = [];
let match = pattern.exec(maskedText);
while (match !== null) {
offsets.push(match.index + match[1].length);
match = pattern.exec(maskedText);
}
return offsets;
}
export function getMentionOffset(text: string, name: string): number | null {
return getMentionOffsets(text, name)[0] ?? null;
}
export function hasMention(text: string, name: string): boolean {
@@ -1,11 +1,14 @@
import assert from "node:assert/strict";
import test from "node:test";
function createStorage() {
function createStorage(onSetItem = () => {}) {
const values = new Map();
return {
getItem: (key) => values.get(key) ?? null,
setItem: (key, value) => values.set(key, String(value)),
setItem: (key, value) => {
onSetItem(key, value);
values.set(key, String(value));
},
};
}
@@ -207,6 +210,114 @@ test("new recipients retain explicit mention order", async () => {
assert.deepEqual(savedAudiences(), { [scope]: [agentB, agentA] });
});
test("persistent audiences retain only the 200 most recently touched scopes", async () => {
const store = await loadStore(11);
for (
let index = 0;
index < store.MAX_PERSISTENT_AGENT_AUDIENCES + 2;
index++
) {
store.setPersistentAgentAudience(`scope-${index}`, [agentA]);
}
const saved = savedAudiences();
assert.equal(Object.keys(saved).length, store.MAX_PERSISTENT_AGENT_AUDIENCES);
assert.equal(saved["scope-0"], undefined);
assert.equal(saved["scope-1"], undefined);
assert.deepEqual(saved["scope-201"], [agentA]);
store.setPersistentAgentAudience("scope-2", [agentB]);
store.setPersistentAgentAudience("scope-new", [agentC]);
const retouched = savedAudiences();
assert.equal(retouched["scope-3"], undefined);
assert.deepEqual(retouched["scope-2"], [agentB]);
assert.deepEqual(retouched["scope-new"], [agentC]);
});
test("an unchanged touch refreshes LRU without revision or emit", async () => {
const { JSDOM } = await import("jsdom");
const dom = new JSDOM(
"<!doctype html><html><body><div id='root'></div></body></html>",
{
url: "http://localhost",
},
);
const writes = [];
Object.defineProperty(dom.window, "localStorage", {
configurable: true,
value: createStorage((key, value) => writes.push([key, String(value)])),
});
Object.assign(globalThis, {
document: dom.window.document,
HTMLElement: dom.window.HTMLElement,
IS_REACT_ACT_ENVIRONMENT: true,
window: dom.window,
});
loadSequence += 1;
const store = await import(
`./persistentAgentAudience.ts?test=${Date.now()}-touch-${loadSequence}`
);
const touchedScope = "scope-0";
store.setPersistentAgentAudience(touchedScope, [agentA]);
for (let index = 1; index < store.MAX_PERSISTENT_AGENT_AUDIENCES; index++) {
store.setPersistentAgentAudience(`scope-${index}`, [agentA]);
}
const React = await import("react");
const { createRoot } = await import("react-dom/client");
const root = createRoot(document.getElementById("root"));
let renderCount = 0;
function Probe() {
store.usePersistentAgentAudience(touchedScope);
renderCount += 1;
return null;
}
await React.act(async () => root.render(React.createElement(Probe)));
const revision = store.getPersistentAgentAudienceRevision(touchedScope);
const renderCountBeforeTouch = renderCount;
writes.length = 0;
await React.act(async () => {
store.setPersistentAgentAudience(touchedScope, [agentA]);
});
assert.equal(writes.length, 1);
assert.equal(writes[0][0], storageKey);
assert.deepEqual(JSON.parse(writes[0][1])[touchedScope], [agentA]);
assert.equal(Object.keys(JSON.parse(writes[0][1])).at(-1), touchedScope);
assert.equal(
store.getPersistentAgentAudienceRevision(touchedScope),
revision,
);
assert.equal(renderCount, renderCountBeforeTouch);
writes.length = 0;
await React.act(async () => {
store.setPersistentAgentAudience(touchedScope, [agentA]);
});
assert.equal(writes.length, 0);
assert.equal(
store.getPersistentAgentAudienceRevision(touchedScope),
revision,
);
assert.equal(renderCount, renderCountBeforeTouch);
await React.act(async () => {
store.setPersistentAgentAudience("scope-new", [agentB]);
});
const saved = savedAudiences();
assert.deepEqual(saved[touchedScope], [agentA]);
assert.equal(saved["scope-1"], undefined);
assert.deepEqual(saved["scope-new"], [agentB]);
assert.equal(
store.getPersistentAgentAudienceRevision(touchedScope),
revision,
);
await React.act(async () => root.unmount());
dom.window.close();
});
test("timeline scope is intentionally unsupported", async () => {
const store = await loadStore(7);
assert.equal(
@@ -2,6 +2,7 @@ import * as React from "react";
const ENABLED_STORAGE_KEY = "buzz:keep-addressed-agents-active";
const AUDIENCES_STORAGE_KEY = "buzz:persistent-agent-audiences:v2";
export const MAX_PERSISTENT_AGENT_AUDIENCES = 200;
const listeners = new Set<() => void>();
const revisions = new Map<string, number>();
@@ -39,6 +40,15 @@ function readEnabled(): boolean {
}
}
function boundAudiences(
value: Record<string, string[]>,
): Record<string, string[]> {
const entries = Object.entries(value);
return entries.length <= MAX_PERSISTENT_AGENT_AUDIENCES
? value
: Object.fromEntries(entries.slice(-MAX_PERSISTENT_AGENT_AUDIENCES));
}
function readAudiences(): Record<string, string[]> {
if (typeof window === "undefined") return {};
try {
@@ -56,7 +66,7 @@ function readAudiences(): Record<string, string[]> {
);
}
}
return result;
return boundAudiences(result);
} catch {
return {};
}
@@ -129,8 +139,11 @@ export function initializePersistentAgentAudience(
scope: string,
pubkeys: Iterable<string>,
): void {
if (!enabled || !scope || Object.hasOwn(audiences, scope)) return;
setPersistentAgentAudience(scope, pubkeys);
if (!enabled || !scope) return;
setPersistentAgentAudience(
scope,
Object.hasOwn(audiences, scope) ? audiences[scope] : pubkeys,
);
}
export function setPersistentAgentAudience(
@@ -145,10 +158,20 @@ export function setPersistentAgentAudience(
current.length === normalized.length &&
current.every((pubkey, index) => pubkey === normalized[index])
) {
if (Object.keys(audiences).at(-1) === scope) return;
const nextAudiences = { ...audiences };
delete nextAudiences[scope];
audiences = boundAudiences({ ...nextAudiences, [scope]: current });
persistAudiences();
return;
}
audiences = { ...audiences, [scope]: normalized };
const nextAudiences = { ...audiences };
delete nextAudiences[scope];
audiences = boundAudiences({ ...nextAudiences, [scope]: normalized });
for (const revisedScope of revisions.keys()) {
if (!Object.hasOwn(audiences, revisedScope)) revisions.delete(revisedScope);
}
advanceRevision(scope);
persistAudiences();
emit();
@@ -1,7 +1,12 @@
import assert from "node:assert/strict";
import test from "node:test";
import { getMentionOffset, hasMention } from "./hasMention.ts";
import {
getMentionOffset,
getMentionOffsets,
hasMention,
} from "./hasMention.ts";
import { extractMentionPubkeys } from "./extractMentionPubkeys.ts";
// ── Plain @mention ────────────────────────────────────────────────────
@@ -95,6 +100,111 @@ test("does not false-positive on partial name match", () => {
assert.equal(hasMention("@Alice", "Al"), false);
});
test("returns every mention offset", () => {
const text = "@Fast Fizz Codex and @Fast Fizz";
assert.deepEqual(getMentionOffsets(text, "Fast Fizz"), [0, 21]);
});
test("selected longer mention excludes a prefix member at the same offset", () => {
const pubkeys = extractMentionPubkeys({
text: "Hive Codex(@Fast Fizz Codex)",
selectedMentions: new Map([["Fast Fizz Codex", "codex-pubkey"]]),
memberCandidates: [
{
kind: "identity",
pubkey: "fast-fizz-pubkey",
displayName: "Fast Fizz",
isMember: true,
isAgent: true,
},
{
kind: "identity",
pubkey: "codex-pubkey",
displayName: "Fast Fizz Codex",
isMember: true,
isAgent: true,
},
],
});
assert.deepEqual(pubkeys, ["codex-pubkey"]);
});
test("manual longer mention excludes its prefix member", () => {
const pubkeys = extractMentionPubkeys({
text: "@Fast Fizz Codex",
selectedMentions: new Map(),
memberCandidates: [
{
kind: "identity",
pubkey: "fast-fizz-pubkey",
displayName: "Fast Fizz",
isMember: true,
isAgent: true,
},
{
kind: "identity",
pubkey: "codex-pubkey",
displayName: "Fast Fizz Codex",
isMember: true,
isAgent: true,
},
],
});
assert.deepEqual(pubkeys, ["codex-pubkey"]);
});
test("manual prefix mentions choose the longest name at each offset", () => {
const pubkeys = extractMentionPubkeys({
text: "@Fast Fizz Codex, please pair with @Fast Fizz.",
selectedMentions: new Map(),
memberCandidates: [
{
kind: "identity",
pubkey: "fast-fizz-pubkey",
displayName: "Fast Fizz",
isMember: true,
isAgent: true,
},
{
kind: "identity",
pubkey: "codex-pubkey",
displayName: "Fast Fizz Codex",
isMember: true,
isAgent: true,
},
],
});
assert.deepEqual(pubkeys, ["fast-fizz-pubkey", "codex-pubkey"]);
});
test("keeps manually typed prefix member mentions at distinct offsets", () => {
const pubkeys = extractMentionPubkeys({
text: "@Fast Fizz Codex, please pair with @Fast Fizz.",
selectedMentions: new Map([["Fast Fizz Codex", "codex-pubkey"]]),
memberCandidates: [
{
kind: "identity",
pubkey: "fast-fizz-pubkey",
displayName: "Fast Fizz",
isMember: true,
isAgent: true,
},
{
kind: "identity",
pubkey: "codex-pubkey",
displayName: "Fast Fizz Codex",
isMember: true,
isAgent: true,
},
],
});
assert.deepEqual(pubkeys, ["codex-pubkey", "fast-fizz-pubkey"]);
});
// ── Markdown code ─────────────────────────────────────────────────────
test("ignores mentions in inline code", () => {
@@ -40,6 +40,7 @@ import { normalizePubkey } from "@/shared/lib/pubkey";
import { trimMapToSize } from "@/shared/lib/trimMapToSize";
import { flushMentionDebounce } from "./flushMentionDebounce";
import { hasMention } from "./hasMention";
import { extractMentionPubkeys } from "./extractMentionPubkeys";
import { useDraftMentionRouting } from "./useDraftMentionRouting";
import { rankMentionCandidates } from "./mentionRanking";
import { mapMentionCandidateToSuggestion } from "./mentionSuggestionMapping";
@@ -77,6 +78,7 @@ function appendUniqueName(current: string[], name: string): string[] {
? current
: [...current, name];
}
export function useMentions(
channelId: string | null,
externalMembers?: ChannelMember[],
@@ -791,43 +793,14 @@ export function useMentions(
[],
);
const extractMentionPubkeys = React.useCallback(
(text: string): string[] => {
const pubkeys: string[] = [];
const selectedDisplayNames = new Set(
[
...mentionMapRef.current.keys(),
...personaMentionMapRef.current.keys(),
].map((name) => name.trim().toLowerCase()),
);
for (const [displayName, pubkey] of mentionMapRef.current) {
if (hasMention(text, displayName)) {
pubkeys.push(pubkey);
}
}
for (const candidate of mentionCandidates) {
if (!candidate.pubkey) {
continue;
}
if (!candidate.isMember) {
continue;
}
if (pubkeys.includes(candidate.pubkey)) {
continue;
}
const name = candidate.displayName;
if (name && selectedDisplayNames.has(name.trim().toLowerCase())) {
continue;
}
if (name && hasMention(text, name)) {
pubkeys.push(candidate.pubkey);
}
}
return [...new Set(pubkeys)];
},
const extractMentionPubkeysForCurrentMentions = React.useCallback(
(text: string): string[] =>
extractMentionPubkeys({
text,
selectedMentions: mentionMapRef.current,
selectedDisplayNames: personaMentionMapRef.current.keys(),
memberCandidates: mentionCandidates,
}),
[mentionCandidates],
);
@@ -972,7 +945,7 @@ export function useMentions(
cancelMentionAutocomplete,
clearMentions,
extractMentionPersonas,
extractMentionPubkeys,
extractMentionPubkeys: extractMentionPubkeysForCurrentMentions,
getDraftMentionRefs,
getMentionDisplayName,
handleMentionKeyDown,
@@ -30,6 +30,7 @@ type AvatarStepProps = {
direction: OnboardingTransitionDirection;
/** When true, a ghost "Skip for now" button is always visible (not just on error). */
showAlwaysSkip?: boolean;
showBack?: boolean;
state: Pick<
ProfileStepState,
"avatar" | "isSaving" | "isUploadingAvatar" | "name" | "saveRecovery"
@@ -130,6 +131,7 @@ function AvatarStepActions({
onSkipForNow,
onSubmit,
saveRecovery,
showBack,
showAlwaysSkip,
}: {
canSubmit: boolean;
@@ -141,6 +143,7 @@ function AvatarStepActions({
onSkipForNow: () => void;
onSubmit: () => void;
saveRecovery: ProfileStepState["saveRecovery"];
showBack: boolean;
showAlwaysSkip: boolean;
}) {
const areNavigationActionsDisabled = isSaving || isUploadingAvatar;
@@ -222,16 +225,18 @@ function AvatarStepActions({
</Button>
) : null}
<Button
className="h-10 w-full text-muted-foreground hover:text-accent-foreground"
data-testid="onboarding-back"
disabled={areNavigationActionsDisabled}
onClick={onBack}
type="button"
variant="ghost"
>
Back
</Button>
{showBack ? (
<Button
className="h-10 w-full text-muted-foreground hover:text-accent-foreground"
data-testid="onboarding-back"
disabled={areNavigationActionsDisabled}
onClick={onBack}
type="button"
variant="ghost"
>
Back
</Button>
) : null}
</motion.div>
)}
</AnimatePresence>
@@ -243,6 +248,7 @@ export function AvatarStep({
actions,
direction,
showAlwaysSkip = false,
showBack = true,
state,
}: AvatarStepProps) {
const {
@@ -407,6 +413,7 @@ export function AvatarStep({
onSkipForNow={skipForNow}
onSubmit={submit}
saveRecovery={saveRecovery}
showBack={showBack}
showAlwaysSkip={showAlwaysSkip}
/>
</motion.div>
@@ -49,7 +49,6 @@ export function backupNextDisabled(): boolean {
type BackupStepProps = {
direction: OnboardingTransitionDirection;
identityStorage?: IdentityStorage;
onBack: () => void;
onNext: () => void;
onOpenPasswordBackup: () => void;
onShowOptions: () => void;
@@ -66,7 +65,6 @@ type BackupStepProps = {
export function BackupStep({
direction,
identityStorage,
onBack,
onNext,
onOpenPasswordBackup,
onShowOptions,
@@ -185,7 +183,6 @@ export function BackupStep({
className="flex min-h-0 w-full flex-col items-center"
data-testid="onboarding-page-backup-options"
direction={direction}
effect={direction === "forward" ? "mask-reveal-up" : "line-slide"}
transitionKey={`backup-options-${direction}`}
>
<div className="flex w-full max-w-140 shrink-0 flex-col text-center">
@@ -297,8 +294,7 @@ export function BackupStep({
className="flex min-h-0 w-full flex-col items-center"
data-testid="onboarding-page-backup"
direction={direction}
effect={returningFromSecurity ? "mask-reveal-down" : "line-slide"}
transitionKey={`backup-${direction}-${returningFromSecurity ? "down" : "line"}`}
transitionKey={`backup-${direction}-${returningFromSecurity ? "security" : "line"}`}
>
<div className="flex w-full max-w-[500px] shrink-0 flex-col text-center">
{/* Plain string concat: cn()'s tailwind-merge misreads the custom
@@ -420,16 +416,6 @@ export function BackupStep({
>
Next
</Button>
<Button
className={ONBOARDING_SECONDARY_CTA_CLASS}
data-testid="onboarding-back"
onClick={onBack}
type="button"
variant="ghost"
>
Back
</Button>
</OnboardingFooter>
</OnboardingSlideTransition>
);
@@ -38,6 +38,10 @@ import {
OnboardingChrome,
} from "./OnboardingChrome";
import { OnboardingFooter, OnboardingFooterProvider } from "./OnboardingFooter";
import {
type OnboardingTransitionDirection,
OnboardingSlideTransition,
} from "./OnboardingSlideTransition";
function isRelayMembershipDeniedError(error: unknown): boolean {
if (!(error instanceof Error)) return false;
@@ -153,9 +157,22 @@ export function CommunityOnboardingFlow({
const systemColorScheme = useSystemColorScheme();
const [displayName, setDisplayName] = React.useState("");
const [avatarUrl, setAvatarUrl] = React.useState("");
const [localAvatarPreviewUrl, setLocalAvatarPreviewUrl] = React.useState<
string | null
>(null);
const [avatarSquishKey, setAvatarSquishKey] = React.useState(0);
const [transitionDirection, setTransitionDirection] =
React.useState<OnboardingTransitionDirection>("forward");
const avatarPresentation = useAvatarPresentation(avatarUrl);
const [isUploadingAvatar, setIsUploadingAvatar] = React.useState(false);
const [isAvatarEditorOpen, setIsAvatarEditorOpen] = React.useState(false);
const [animatedPreviewEl, setAnimatedPreviewEl] =
React.useState<HTMLDivElement | null>(null);
const [isAnimatedPreviewActive, setIsAnimatedPreviewActive] =
React.useState(false);
const [animatedPreviewCaption, setAnimatedPreviewCaption] = React.useState<
string | null
>(null);
const [starterPersonas, setStarterPersonas] = React.useState<AgentPersona[]>(
[],
);
@@ -173,6 +190,9 @@ export function CommunityOnboardingFlow({
const avatarEditorContentRef = React.useRef<HTMLDivElement | null>(null);
const [avatarEditorDialogHeight, setAvatarEditorDialogHeight] =
React.useState<number | null>(null);
const animateEmojiAvatarChange = React.useCallback(() => {
setAvatarSquishKey((key) => key + 1);
}, []);
// Also fetch on "entering": the curtain is a fresh mount of this component,
// so the team-intro fetch from the pre-curtain instance isn't in this state.
@@ -280,6 +300,7 @@ export function CommunityOnboardingFlow({
const backToProfile = React.useCallback(() => {
if (isPending) return;
setStarterChannelFailureCount(0);
setTransitionDirection("backward");
update({ stage: "profile", error: undefined });
}, [isPending, update]);
@@ -292,6 +313,7 @@ export function CommunityOnboardingFlow({
void getProfile()
.then((profile) => {
if (profile.hasProfileEvent) {
setTransitionDirection("forward");
update({ stage: "team-intro", error: undefined }, transaction.id);
}
})
@@ -427,6 +449,7 @@ export function CommunityOnboardingFlow({
deferredAvatar?.cancel();
throw error;
}
setTransitionDirection("forward");
update({ stage: "team-intro", error: undefined });
} catch (error) {
if (isRelayMembershipDeniedError(error)) {
@@ -467,250 +490,339 @@ export function CommunityOnboardingFlow({
{isProfileStage || isTeamStage ? (
<OnboardingChrome current={isTeamStage ? 7 : 6} />
) : null}
<OnboardingFooterProvider>
<div
className={cn(
"relative w-full text-center",
isProfileStage
? "buzz-onboarding-step-frame flex max-w-[500px] flex-col items-center"
: isTeamStage
? "buzz-onboarding-step-frame flex max-w-[760px] flex-col items-center"
: "flex min-h-dvh max-w-[560px] flex-col justify-center py-8",
)}
data-testid="community-onboarding-body"
<OnboardingFooterProvider
backAction={
isProfileStage
? {
disabled: isPending || isUploadingAvatar,
onClick: onCancel,
testId: "community-profile-back",
}
: isTeamStage
? {
disabled: isPending || transaction.stage === "entering",
onClick: backToProfile,
testId: "community-team-intro-back",
}
: undefined
}
>
<OnboardingSlideTransition
direction={transitionDirection}
transitionKey={`community-${isProfileStage ? "profile" : isTeamStage ? "team" : transaction.stage}-${transitionDirection}`}
>
{transaction.stage === "claiming" ||
transaction.stage === "connecting" ? (
<>
<Users className="mx-auto h-10 w-10" />
<h1 className="mt-5 text-title font-normal">
Joining {transaction.communityName}
</h1>
<p className="mt-3 text-sm text-foreground/80">
{transaction.error ??
(transaction.stage === "claiming"
? "Accepting your invite…"
: "Connecting securely…")}
</p>
<div className="mt-6 flex justify-center gap-3">
{transaction.error ? (
<Button className="rounded-full px-6" onClick={retry}>
Retry
</Button>
) : null}
<Button
className="rounded-full bg-foreground/10 px-5 hover:bg-foreground/15"
onClick={onCancel}
variant="ghost"
>
Cancel
</Button>
</div>
</>
) : isProfileStage ? (
<>
<div
className={cn(
"flex min-h-0 w-full flex-1 flex-col transition-[filter,opacity] duration-200 ease-out",
isAvatarEditorOpen &&
"pointer-events-none opacity-45 blur-[3px]",
)}
data-testid="community-profile-main"
>
<div className="shrink-0">
<h1 className="text-title font-normal">Build your profile</h1>
<p className="mx-auto mt-3 max-w-[380px] text-sm leading-6 text-foreground/80">
Add a name and avatar. They’ll show up on your messages,
reactions, and agent handoffs.
</p>
</div>
<div className="flex min-h-0 w-full flex-1 flex-col items-center justify-center pt-8">
<AvatarCircle
avatarUrl={avatarUrl}
onClick={() => setIsAvatarEditorOpen(true)}
previewName={displayName.trim() || "Your profile"}
triggerRef={avatarTriggerRef}
/>
<label
className="mt-7 block w-full max-w-[412px] text-left"
htmlFor="community-display-name"
>
<span className="mb-2 block pl-4 text-sm text-foreground">
Your username
</span>
<Input
aria-label="Community username"
autoCapitalize="none"
autoComplete="username"
autoCorrect="off"
className="h-14 rounded-2xl border-[color:rgb(var(--buzz-onboarding-avatar-control-fg)_/_0.28)] bg-[rgb(var(--buzz-onboarding-avatar-dialog-bg)/0.95)] px-5 text-sm shadow-none placeholder:text-muted-foreground/60 focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-[color:rgb(var(--buzz-onboarding-avatar-control-fg)_/_0.5)] md:text-sm"
data-testid="community-profile-name-key"
disabled={isPending || isUploadingAvatar}
id="community-display-name"
onChange={(event) => setDisplayName(event.target.value)}
placeholder="Enter your username here"
ref={nameInputRef}
spellCheck={false}
type="text"
value={displayName}
/>
</label>
</div>
{transaction.error ? (
<p className="mt-4 text-sm text-destructive">
{transaction.error}
</p>
) : null}
</div>
<OnboardingFooter
className={cn(
"transition-[filter,opacity] duration-200 ease-out",
isAvatarEditorOpen &&
"pointer-events-none opacity-45 blur-[3px]",
)}
>
<Button
className={`${ONBOARDING_PRIMARY_CTA_CLASS} w-20`}
data-testid="community-profile-next"
disabled={
!displayName.trim() || isPending || isUploadingAvatar
}
onClick={() => void saveProfile()}
type="button"
>
Next
</Button>
<Button
className="h-9 w-20 rounded-full bg-foreground/10 px-6 hover:bg-foreground/15"
data-testid="community-profile-back"
disabled={isPending || isUploadingAvatar}
onClick={onCancel}
type="button"
variant="ghost"
>
Back
</Button>
</OnboardingFooter>
<Dialog
onOpenChange={(open) => setIsAvatarEditorOpen(open)}
open={isAvatarEditorOpen}
>
<DialogContent
className="buzz-onboarding-neutral-theme w-[min(calc(100vw-2rem),560px)] max-w-[560px] gap-0 overflow-hidden rounded-[18px] bg-[rgb(var(--buzz-onboarding-avatar-dialog-bg))] px-8 pb-6 pt-10 text-sm text-foreground shadow-[0_28px_90px_rgb(var(--buzz-onboarding-avatar-dialog-shadow)_/_0.28),0_8px_28px_rgb(var(--buzz-onboarding-avatar-dialog-shadow)_/_0.18)] transition-[height] duration-[250ms] ease-out"
closeButtonClassName="right-6 top-6 h-10 w-10 rounded-full bg-[rgb(var(--buzz-onboarding-avatar-action-bg))] text-[rgb(var(--buzz-onboarding-avatar-action-fg))] hover:bg-[rgb(var(--buzz-onboarding-avatar-action-bg)/0.9)] hover:text-[rgb(var(--buzz-onboarding-avatar-action-fg))]"
data-system-color-scheme="light"
data-testid="community-avatar-editor-key-frame"
onCloseAutoFocus={(event) => {
event.preventDefault();
avatarTriggerRef.current?.focus();
}}
overlayVariant="transparent"
style={
avatarEditorDialogHeight === null
? undefined
: { height: avatarEditorDialogHeight }
}
>
<DialogTitle className="sr-only">
Edit your avatar
</DialogTitle>
<div ref={avatarEditorContentRef}>
<ProfileAvatarEditor
avatarUrl={avatarUrl}
disabled={isPending}
donePending={isUploadingAvatar}
emojiPickerTheme="auto"
emojiPickerThemeVars={NEUTRAL_EMOJI_PICKER_THEME_VARS}
onDone={() => setIsAvatarEditorOpen(false)}
onUploadingChange={setIsUploadingAvatar}
onUrlChange={setAvatarUrl}
presentation="onboarding-modal"
previewName={displayName.trim() || "Your profile"}
testIdPrefix="community-avatar"
/>
</div>
</DialogContent>
</Dialog>
</>
) : (
<>
<h1 className="text-title font-normal">Meet your starter team</h1>
<p className="mx-auto mt-3 max-w-[400px] text-sm leading-6 text-foreground/80">
Buzz lets you bring multiple agents into the same workspace.
Your team will help you get started using Buzz.
</p>
<div className="flex w-full flex-1 items-center justify-center py-10">
{starterPersonas.length > 0 ? (
<div className="flex flex-wrap justify-center gap-8">
{starterPersonas.map((persona) => {
const animationUrl =
STARTER_PERSONA_ANIMATIONS[persona.displayName];
return (
<div
className="flex w-40 flex-col items-center gap-3"
key={persona.id}
>
{animationUrl ? (
<img
alt={`${persona.displayName} animated character`}
className="h-40 w-40 object-contain"
data-testid={`starter-persona-${persona.displayName.toLowerCase()}`}
src={animationUrl}
/>
) : (
<ProfileAvatar
avatarUrl={persona.avatarUrl}
className="h-28 w-28 text-3xl"
label={persona.displayName}
/>
)}
<span className="font-mono text-xs font-medium uppercase tracking-[0.15em]">
{persona.displayName}
</span>
</div>
);
})}
</div>
) : null}
</div>
{transaction.error ? (
<p className="text-sm text-destructive">
{transaction.error}
{starterChannelFailureCount === 1 ? " Try again." : null}
<div
className={cn(
"relative mx-auto w-full text-center",
isProfileStage
? "buzz-onboarding-step-frame flex max-w-[500px] flex-col items-center"
: isTeamStage
? "buzz-onboarding-step-frame flex max-w-[760px] flex-col items-center"
: "flex min-h-dvh max-w-[560px] flex-col justify-center py-8",
)}
data-testid="community-onboarding-body"
>
{transaction.stage === "claiming" ||
transaction.stage === "connecting" ? (
<>
<Users className="mx-auto h-10 w-10" />
<h1 className="mt-5 text-title font-normal">
Joining {transaction.communityName}
</h1>
<p className="mt-3 text-sm text-foreground/80">
{transaction.error ??
(transaction.stage === "claiming"
? "Accepting your invite…"
: "Connecting securely…")}
</p>
) : null}
<OnboardingFooter>
<Button
className={ONBOARDING_PRIMARY_CTA_CLASS}
data-testid="community-team-intro-enter"
disabled={isPending || transaction.stage === "entering"}
onClick={() =>
void (starterChannelFailureCount >= 2
? finish()
: finalize())
}
>
{isPending || transaction.stage === "entering" ? (
<LoadingDots label="Preparing Welcome" />
) : starterChannelFailureCount >= 2 ? (
"Skip for now"
) : (
"Take me to Buzz"
<div className="mt-6 flex justify-center gap-3">
{transaction.error ? (
<Button className="rounded-full px-6" onClick={retry}>
Retry
</Button>
) : null}
<Button
className="rounded-full bg-foreground/10 px-5 hover:bg-foreground/15"
onClick={onCancel}
variant="ghost"
>
Cancel
</Button>
</div>
</>
) : isProfileStage ? (
<>
<div
className={cn(
"flex min-h-0 w-full flex-1 flex-col transition-[filter,opacity] duration-200 ease-out",
isAvatarEditorOpen &&
"pointer-events-none opacity-45 blur-[3px]",
)}
</Button>
<Button
className="h-9 rounded-full bg-foreground/10 px-5 hover:bg-foreground/15"
data-testid="community-team-intro-back"
disabled={isPending || transaction.stage === "entering"}
onClick={backToProfile}
variant="ghost"
data-testid="community-profile-main"
>
Back
</Button>
</OnboardingFooter>
</>
)}
</div>
<div className="shrink-0">
<h1 className="text-title font-normal">
Build your profile
</h1>
<p className="mx-auto mt-3 max-w-[380px] text-sm leading-6 text-foreground/80">
Add a name and avatar. They’ll show up on your messages,
reactions, and agent handoffs.
</p>
</div>
<div className="flex min-h-0 w-full flex-1 flex-col items-center justify-center pt-8">
<AvatarCircle
avatarUrl={avatarUrl}
onClick={() => setIsAvatarEditorOpen(true)}
previewName={displayName.trim() || "Your profile"}
triggerRef={avatarTriggerRef}
/>
<label
className="mt-7 block w-full max-w-[412px] text-left"
htmlFor="community-display-name"
>
<span className="mb-2 block pl-4 text-sm text-foreground">
Your username
</span>
<Input
aria-label="Community username"
autoCapitalize="none"
autoComplete="username"
autoCorrect="off"
className="h-14 rounded-2xl border-[color:rgb(var(--buzz-onboarding-avatar-control-fg)_/_0.28)] bg-[rgb(var(--buzz-onboarding-avatar-dialog-bg)/0.95)] px-5 text-sm shadow-none placeholder:text-muted-foreground/60 focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-[color:rgb(var(--buzz-onboarding-avatar-control-fg)_/_0.5)] md:text-sm"
data-testid="community-profile-name-key"
disabled={isPending || isUploadingAvatar}
id="community-display-name"
onChange={(event) => setDisplayName(event.target.value)}
placeholder="Enter your username here"
ref={nameInputRef}
spellCheck={false}
type="text"
value={displayName}
/>
</label>
</div>
{transaction.error ? (
<p className="mt-4 text-sm text-destructive">
{transaction.error}
</p>
) : null}
</div>
<OnboardingFooter
className={cn(
"transition-[filter,opacity] duration-200 ease-out",
isAvatarEditorOpen &&
"pointer-events-none opacity-45 blur-[3px]",
)}
>
<Button
className={`${ONBOARDING_PRIMARY_CTA_CLASS} w-20`}
data-testid="community-profile-next"
disabled={
!displayName.trim() || isPending || isUploadingAvatar
}
onClick={() => void saveProfile()}
type="button"
>
Next
</Button>
</OnboardingFooter>
<Dialog
onOpenChange={(open) => setIsAvatarEditorOpen(open)}
open={isAvatarEditorOpen}
>
<DialogContent
className="buzz-onboarding-neutral-theme w-[min(calc(100vw-2rem),920px)] max-w-[920px] gap-0 overflow-hidden rounded-[18px] bg-[rgb(var(--buzz-onboarding-avatar-dialog-bg))] px-8 pb-6 pt-10 text-sm text-foreground shadow-[0_28px_90px_rgb(var(--buzz-onboarding-avatar-dialog-shadow)_/_0.28),0_8px_28px_rgb(var(--buzz-onboarding-avatar-dialog-shadow)_/_0.18)] transition-[height] duration-[250ms] ease-out"
closeButtonClassName="right-6 top-6 h-10 w-10 rounded-full bg-[rgb(var(--buzz-onboarding-avatar-action-bg))] text-[rgb(var(--buzz-onboarding-avatar-action-fg))] hover:bg-[rgb(var(--buzz-onboarding-avatar-action-bg)/0.9)] hover:text-[rgb(var(--buzz-onboarding-avatar-action-fg))]"
data-system-color-scheme="light"
data-testid="community-avatar-editor-key-frame"
onCloseAutoFocus={(event) => {
event.preventDefault();
avatarTriggerRef.current?.focus();
}}
overlayVariant="transparent"
style={
avatarEditorDialogHeight === null
? undefined
: { height: avatarEditorDialogHeight }
}
>
<DialogTitle className="sr-only">
Edit your avatar
</DialogTitle>
<div
className="grid items-center gap-8 md:grid-cols-[240px_minmax(0,1fr)]"
ref={avatarEditorContentRef}
>
<div
className="flex min-h-[320px] flex-col items-center justify-center gap-3 px-6 py-8"
data-testid="community-avatar-live-preview-panel"
>
<div className="relative h-48 w-48">
<div
className="pointer-events-none absolute inset-0 z-10"
data-testid="community-avatar-animated-preview-slot"
ref={setAnimatedPreviewEl}
/>
{isAnimatedPreviewActive
? null
: (() => {
if (localAvatarPreviewUrl) {
return (
<ProfileAvatar
avatarUrl={localAvatarPreviewUrl}
className="h-full w-full rounded-full text-5xl"
label={
displayName.trim() || "Your profile"
}
testId="community-avatar-live-preview"
/>
);
}
const emojiAvatar =
parseEmojiAvatarDataUrl(avatarUrl);
return emojiAvatar ? (
<div
aria-label={`${displayName.trim() || "Your profile"} avatar`}
className="flex h-full w-full items-center justify-center overflow-hidden rounded-full text-6xl shadow-xs"
data-testid="community-avatar-live-preview"
role="img"
style={{
backgroundColor: emojiAvatar.color,
}}
>
<span
className={cn(
avatarSquishKey > 0 &&
"buzz-avatar-squish",
)}
data-testid="community-avatar-live-preview-emoji"
key={avatarSquishKey}
>
{emojiAvatar.emoji}
</span>
</div>
) : (
<ProfileAvatar
avatarUrl={
localAvatarPreviewUrl || avatarUrl || null
}
className="h-full w-full rounded-full text-5xl"
label={displayName.trim() || "Your profile"}
testId="community-avatar-live-preview"
/>
);
})()}
</div>
{animatedPreviewCaption ? (
<p className="text-center text-sm text-muted-foreground">
{animatedPreviewCaption}
</p>
) : null}
</div>
<ProfileAvatarEditor
animatedPreviewContainer={animatedPreviewEl}
avatarUrl={avatarUrl}
disabled={isPending}
donePending={isUploadingAvatar}
emojiPickerTheme="auto"
emojiPickerThemeVars={NEUTRAL_EMOJI_PICKER_THEME_VARS}
onDone={() => setIsAvatarEditorOpen(false)}
onAnimatedPreviewActiveChange={
setIsAnimatedPreviewActive
}
onAnimatedPreviewCaptionChange={
setAnimatedPreviewCaption
}
onEmojiAvatarChange={animateEmojiAvatarChange}
onLocalPreviewChange={setLocalAvatarPreviewUrl}
onUploadingChange={setIsUploadingAvatar}
onUrlChange={setAvatarUrl}
presentation="onboarding-modal"
previewName={displayName.trim() || "Your profile"}
testIdPrefix="community-avatar"
/>
</div>
</DialogContent>
</Dialog>
</>
) : (
<>
<h1 className="text-title font-normal">
Meet your starter team
</h1>
<p className="mx-auto mt-3 max-w-[400px] text-sm leading-6 text-foreground/80">
Buzz lets you bring multiple agents into the same workspace.
Your team will help you get started using Buzz.
</p>
<div className="flex w-full flex-1 items-center justify-center py-10">
{starterPersonas.length > 0 ? (
<div className="flex flex-wrap justify-center gap-8">
{starterPersonas.map((persona) => {
const animationUrl =
STARTER_PERSONA_ANIMATIONS[persona.displayName];
return (
<div
className="flex w-40 flex-col items-center gap-3"
key={persona.id}
>
{animationUrl ? (
<img
alt={`${persona.displayName} animated character`}
className="h-40 w-40 object-contain"
data-testid={`starter-persona-${persona.displayName.toLowerCase()}`}
src={animationUrl}
/>
) : (
<ProfileAvatar
avatarUrl={persona.avatarUrl}
className="h-28 w-28 text-3xl"
label={persona.displayName}
/>
)}
<span className="font-mono text-xs font-medium uppercase tracking-[0.15em]">
{persona.displayName}
</span>
</div>
);
})}
</div>
) : null}
</div>
{transaction.error ? (
<p className="text-sm text-destructive">
{transaction.error}
{starterChannelFailureCount === 1 ? " Try again." : null}
</p>
) : null}
<OnboardingFooter>
<Button
className={ONBOARDING_PRIMARY_CTA_CLASS}
data-testid="community-team-intro-enter"
disabled={isPending || transaction.stage === "entering"}
onClick={() => void finalize()}
>
{isPending || transaction.stage === "entering" ? (
<LoadingDots label="Preparing Welcome" />
) : (
"Take me to Buzz"
)}
</Button>
{starterChannelFailureCount >= 2 ? (
<Button
className="h-9 rounded-full px-5 hover:bg-foreground/10"
data-testid="community-team-intro-skip"
disabled={isPending || transaction.stage === "entering"}
onClick={() => void finish()}
variant="ghost"
>
Skip for now
</Button>
) : null}
</OnboardingFooter>
</>
)}
</div>
</OnboardingSlideTransition>
</OnboardingFooterProvider>
</div>
);
@@ -37,6 +37,7 @@ type DefaultConfigStepProps = {
actions: DefaultConfigStepActions;
direction: OnboardingTransitionDirection;
draft: DefaultConfigDraft | null;
onSavingChange?: (isSaving: boolean) => void;
readyRuntimeIds: readonly string[];
};
@@ -305,6 +306,7 @@ export function DefaultConfigStep({
actions,
direction,
draft,
onSavingChange,
readyRuntimeIds,
}: DefaultConfigStepProps) {
const [persistenceState, setPersistenceState] = React.useState<{
@@ -314,6 +316,11 @@ export function DefaultConfigStep({
const [isSaving, setIsSaving] = React.useState(false);
const [saveError, setSaveError] = React.useState<string | null>(null);
React.useEffect(() => {
onSavingChange?.(isSaving);
return () => onSavingChange?.(false);
}, [isSaving, onSavingChange]);
const handleComplete = React.useCallback(async () => {
if (isSaving) return;
setIsSaving(true);
@@ -369,38 +376,24 @@ export function DefaultConfigStep({
</div>
<OnboardingFooter>
{/* Keep Next centered while the optional action sits beside it. */}
<div className="relative flex items-center justify-center">
<Button
className={`${ONBOARDING_PRIMARY_CTA_CLASS} text-sm`}
data-testid="onboarding-finish"
disabled={!persistenceState.canComplete || isSaving}
onClick={() => void handleComplete()}
type="button"
>
{isSaving ? "Saving…" : "Next"}
</Button>
<Button
className="absolute left-full ml-3 h-9 animate-in whitespace-nowrap rounded-full px-6 text-sm fade-in fill-mode-backwards [animation-delay:1000ms] animation-duration-[500ms] hover:bg-foreground/10 motion-reduce:animate-none"
data-testid="onboarding-config-skip"
disabled={isSaving}
onClick={handleSkip}
type="button"
variant="ghost"
>
Skip for now
</Button>
</div>
<Button
className="h-9 rounded-full bg-foreground/10 px-6 text-sm hover:bg-foreground/15"
data-testid="onboarding-back"
className={`${ONBOARDING_PRIMARY_CTA_CLASS} text-sm`}
data-testid="onboarding-finish"
disabled={!persistenceState.canComplete || isSaving}
onClick={() => void handleComplete()}
type="button"
>
{isSaving ? "Saving…" : "Next"}
</Button>
<Button
className="h-9 whitespace-nowrap rounded-full px-6 text-sm hover:bg-foreground/10"
data-testid="onboarding-config-skip"
disabled={isSaving}
onClick={actions.back}
onClick={handleSkip}
type="button"
variant="ghost"
>
Back
Skip for now
</Button>
{saveError ? (
@@ -412,12 +405,6 @@ export function DefaultConfigStep({
Couldn’t save model settings. {saveError} Try again.
</p>
) : null}
<p className="text-xs text-foreground/50">
Configure default models in{" "}
<span className="text-foreground/70">Settings → Agents</span> after
setup.
</p>
</OnboardingFooter>
</OnboardingSlideTransition>
);
@@ -120,19 +120,23 @@ export function DownloadKeyStep({
}
ref={setPrimaryActionSlot}
/>
<Button
className={
hasVerifiedBackup
? ONBOARDING_SECURITY_PRIMARY_CTA_CLASS
: ONBOARDING_SECONDARY_CTA_CLASS
}
data-testid="onboarding-back"
onClick={onBack}
type="button"
variant="ghost"
>
{hasVerifiedBackup ? "Finish" : hasCreated ? "Skip for now" : "Back"}
</Button>
{hasCreated ? (
<Button
className={
hasVerifiedBackup
? ONBOARDING_SECURITY_PRIMARY_CTA_CLASS
: ONBOARDING_SECONDARY_CTA_CLASS
}
data-testid={
hasVerifiedBackup ? "onboarding-finish" : "onboarding-skip"
}
onClick={onBack}
type="button"
variant="ghost"
>
{hasVerifiedBackup ? "Finish" : "Skip for now"}
</Button>
) : null}
</OnboardingFooter>
</OnboardingSlideTransition>
);
@@ -530,10 +530,7 @@ export function InviteRedeemForm({
</AnimatePresence>
{isOnboardingSpotlight ? (
<OnboardingFooter>
{submitButton}
{cancelButton}
</OnboardingFooter>
<OnboardingFooter>{submitButton}</OnboardingFooter>
) : isAddCommunity ? (
<div className="flex justify-end pt-1">{submitButton}</div>
) : (
@@ -1,6 +1,5 @@
import * as React from "react";
import type { QueryClient } from "@tanstack/react-query";
import { ArrowUp } from "lucide-react";
import { motion, useReducedMotion } from "motion/react";
import {
@@ -39,7 +38,10 @@ import {
OnboardingChrome,
} from "./OnboardingChrome";
import { OnboardingFooterProvider } from "./OnboardingFooter";
import { OnboardingSlideTransition } from "./OnboardingSlideTransition";
import {
type OnboardingTransitionDirection,
OnboardingSlideTransition,
} from "./OnboardingSlideTransition";
import { SetupStep } from "./SetupStep";
import type { DefaultConfigDraft } from "./types";
@@ -84,11 +86,15 @@ export function MachineOnboardingFlow({
const [page, setPage] = React.useState<MachineOnboardingPage>(
identityLost ? "key-import" : (initialPage ?? "identity"),
);
const [transitionDirection, setTransitionDirection] =
React.useState<OnboardingTransitionDirection>("forward");
const [error, setError] = React.useState<string | null>(null);
const [isPending, setIsPending] = React.useState(false);
const [identityWasImported, setIdentityWasImported] = React.useState(false);
const [keyImportStage, setKeyImportStage] =
React.useState<NostrKeyImportStage>("key-entry");
const [isKeyImporting, setIsKeyImporting] = React.useState(false);
const [keyImportFormKey, setKeyImportFormKey] = React.useState(0);
const [keyImportDialog, setKeyImportDialog] = React.useState<
"backup" | "phone" | null
>(null);
@@ -102,6 +108,8 @@ export function MachineOnboardingFlow({
const [readyRuntimeIds, setReadyRuntimeIds] = React.useState<string[]>([]);
const [defaultConfigDraft, setDefaultConfigDraft] =
React.useState<DefaultConfigDraft | null>(null);
const [isDefaultConfigSaving, setIsDefaultConfigSaving] =
React.useState(false);
const [backupSubview, setBackupSubview] =
React.useState<BackupSubview>("created");
const [backupDirection, setBackupDirection] = React.useState<
@@ -130,6 +138,7 @@ export function MachineOnboardingFlow({
setSelectedPubkey(identity.pubkey);
setIdentityStorage(identity.storage);
setBackupDirection("forward");
setTransitionDirection("forward");
setReturningFromSecurity(false);
setBackupSubview("created");
setPage("backup");
@@ -152,6 +161,7 @@ export function MachineOnboardingFlow({
setIdentityWasImported(true);
setSelectedPubkey(identity.pubkey);
setIdentityStorage(identity.storage);
setTransitionDirection("forward");
setPage("setup");
} catch (cause) {
setError(
@@ -176,6 +186,7 @@ export function MachineOnboardingFlow({
setSelectedPubkey(identity.pubkey);
setIdentityStorage(identity.storage);
setBackupDirection("forward");
setTransitionDirection("forward");
setReturningFromSecurity(false);
setBackupSubview("created");
setPage("backup");
@@ -195,11 +206,81 @@ export function MachineOnboardingFlow({
queryClient.setQueryData(["identity"], identity);
setIdentityWasImported(true);
setSelectedPubkey(identity.pubkey);
setTransitionDirection("forward");
setPage("setup");
},
[continueWithIdentity, queryClient],
);
const backFromKeyImport = React.useCallback(() => {
if (keyImportStage === "backup-password") {
setKeyImportFormKey((current) => current + 1);
setKeyImportStage("key-entry");
return;
}
setTransitionDirection("backward");
setPage("identity");
}, [keyImportStage]);
const returnToCreatedKey = React.useCallback(() => {
setBackupDirection("backward");
setReturningFromSecurity(true);
setBackupSubview("created");
}, []);
const backFromPasswordBackup = React.useCallback(() => {
resetEncryptedBackupSession(backupSession);
setBackupDirection("backward");
setReturningFromSecurity(false);
setBackupSubview("options");
}, [backupSession]);
const backFromSetup = React.useCallback(() => {
if (identityWasImported) {
setKeyImportFormKey((current) => current + 1);
setKeyImportStage("key-entry");
setTransitionDirection("backward");
setPage("key-import");
return;
}
if (backupSubview === "password") {
backupSessionToPasswordEntry(backupSession);
}
setBackupDirection("backward");
setTransitionDirection("backward");
setReturningFromSecurity(false);
setPage("backup");
}, [backupSession, backupSubview, identityWasImported]);
const chromeBackAction =
page === "key-import" &&
(!identityLost || keyImportStage === "backup-password")
? { disabled: isKeyImporting, onClick: backFromKeyImport }
: page === "backup" && backupSubview !== "created"
? {
label: "Return to onboarding",
onClick: returnToCreatedKey,
testId: "backup-return-to-onboarding",
}
: page === "backup"
? {
onClick: () => {
setTransitionDirection("backward");
setPage("identity");
},
}
: page === "setup"
? { onClick: backFromSetup }
: page === "config"
? {
disabled: isDefaultConfigSaving,
onClick: () => {
setTransitionDirection("backward");
setPage("setup");
},
}
: undefined;
return (
<div
className={`buzz-onboarding-neutral-theme buzz-startup-shell flex max-h-dvh items-start justify-center overflow-x-hidden overflow-y-auto px-4 text-foreground ${
@@ -213,29 +294,12 @@ export function MachineOnboardingFlow({
>
<StartupWindowDragRegion />
{page === "identity" ? <LandingBees /> : null}
{isSecuritySubview ? (
<div className="fixed inset-x-0 top-8 z-20 flex justify-center px-6">
<Button
className={`${ONBOARDING_SECONDARY_CTA_CLASS} gap-2 px-5`}
data-testid="backup-return-to-onboarding"
onClick={() => {
setBackupDirection("backward");
setReturningFromSecurity(true);
setBackupSubview("created");
}}
type="button"
variant="ghost"
>
<ArrowUp className="h-4 w-4" aria-hidden="true" />
Return to onboarding
</Button>
</div>
) : page !== "identity" ? (
{page !== "identity" && !isSecuritySubview ? (
<OnboardingChrome
current={page === "config" ? 4 : page === "setup" ? 3 : 2}
/>
) : null}
<OnboardingFooterProvider>
<OnboardingFooterProvider backAction={chromeBackAction}>
<div
className={`relative flex w-full max-w-[1040px] flex-col items-center text-center ${
page === "identity" ? "my-auto" : "buzz-onboarding-step-frame"
@@ -244,9 +308,8 @@ export function MachineOnboardingFlow({
{page === "identity" ? (
<OnboardingSlideTransition
className="flex w-full max-w-[720px] flex-col items-center text-center"
direction="forward"
effect="mask-reveal-up"
transitionKey="machine-identity"
direction={transitionDirection}
transitionKey={`machine-identity-${transitionDirection}`}
>
<img
alt="Buzz"
@@ -279,6 +342,7 @@ export function MachineOnboardingFlow({
onClick={() => {
setKeyImportDialog(null);
setKeyImportStage("key-entry");
setTransitionDirection("forward");
setPage("key-import");
}}
type="button"
@@ -294,9 +358,8 @@ export function MachineOnboardingFlow({
) : page === "key-import" ? (
<OnboardingSlideTransition
className="flex min-h-[calc(100dvh-13.25rem)] w-full max-w-[837px] flex-col items-center text-center"
direction="forward"
effect="fade"
transitionKey="machine-key-import"
direction={transitionDirection}
transitionKey={`machine-key-import-${transitionDirection}`}
>
<motion.div
animate={{ opacity: 1, y: 0 }}
@@ -347,22 +410,19 @@ export function MachineOnboardingFlow({
<div className="buzz-onboarding-key-import-position w-full">
<div className="flex flex-col items-center">
<NostrKeyImportForm
backLabel="Back"
onBack={() => {
setKeyImportStage("key-entry");
if (identityLost) {
return;
}
setPage("identity");
}}
key={keyImportFormKey}
onBack={backFromKeyImport}
onImport={importExistingIdentity}
onImportingChange={setIsKeyImporting}
onStageChange={setKeyImportStage}
showBack={!identityLost}
showBack={false}
showPasswordStageBack={false}
variant="spotlight"
/>
{identityLost && keyImportStage === "key-entry" ? (
<Button
className={`${ONBOARDING_SECONDARY_CTA_CLASS} mt-2 px-5`}
disabled={isPending || isKeyImporting}
onClick={() => void replaceLostIdentity()}
type="button"
variant="ghost"
@@ -442,20 +502,17 @@ export function MachineOnboardingFlow({
backupSubview === "password" ? (
<DownloadKeyStep
direction={backupDirection}
onBack={() => {
resetEncryptedBackupSession(backupSession);
setBackupDirection("backward");
setReturningFromSecurity(false);
setBackupSubview("options");
}}
onBack={backFromPasswordBackup}
session={backupSession}
/>
) : (
<BackupStep
direction={backupDirection}
identityStorage={identityStorage}
onBack={() => setPage("identity")}
onNext={() => setPage("setup")}
onNext={() => {
setTransitionDirection("forward");
setPage("setup");
}}
onOpenPasswordBackup={() => {
resetEncryptedBackupSession(backupSession);
setBackupDirection("forward");
@@ -477,17 +534,7 @@ export function MachineOnboardingFlow({
// Fresh-key users return to whichever identity backup subview
// they used to reach setup; imported keys skip backup entirely.
back: () => {
if (identityWasImported) {
setKeyImportStage("key-entry");
setPage("key-import");
return;
}
if (backupSubview === "password") {
backupSessionToPasswordEntry(backupSession);
}
setBackupDirection("backward");
setReturningFromSecurity(false);
setPage("backup");
backFromSetup();
},
next: (runtimeIds) => {
const ids = Array.from(runtimeIds);
@@ -498,6 +545,7 @@ export function MachineOnboardingFlow({
complete(selectedPubkey ?? undefined);
return;
}
setTransitionDirection("forward");
setPage("config");
},
navigateToAgentSettings: () => {
@@ -512,19 +560,23 @@ export function MachineOnboardingFlow({
});
},
}}
direction="forward"
direction={transitionDirection}
onReadyRuntimeIdsChange={handleReadyRuntimeIdsChange}
/>
) : (
<DefaultConfigStep
actions={{
back: () => setPage("setup"),
back: () => {
setTransitionDirection("backward");
setPage("setup");
},
complete: () => complete(selectedPubkey ?? undefined),
discardDraft: () => setDefaultConfigDraft(null),
updateDraft: setDefaultConfigDraft,
}}
direction="forward"
direction={transitionDirection}
draft={defaultConfigDraft}
onSavingChange={setIsDefaultConfigSaving}
readyRuntimeIds={readyRuntimeIds}
/>
)}
@@ -32,8 +32,13 @@ type NostrKeyImportFormProps = {
errorMessage?: string | null;
onBack: () => void;
onImport: (nsec: string, password?: string) => Promise<void>;
/** Reports whether an import is in flight so host-owned navigation can be disabled. */
onImportingChange?: (isImporting: boolean) => void;
onStageChange?: (stage: NostrKeyImportStage) => void;
/** Hide the inline back control when the host renders navigation elsewhere. */
showBack?: boolean;
/** Keep password-stage navigation out of the form when the host owns Back. */
showPasswordStageBack?: boolean;
/** Restrict this instance to selecting a backup file instead of typing a key. */
mode?: "key" | "backup";
/** Dialogs keep their actions inside the surface instead of the onboarding dock. */
@@ -55,8 +60,10 @@ export function NostrKeyImportForm({
errorMessage: externalErrorMessage = null,
onBack,
onImport,
onImportingChange,
onStageChange,
showBack = true,
showPasswordStageBack = true,
mode = "key",
footerMode = "onboarding",
variant = "default",
@@ -64,6 +71,7 @@ export function NostrKeyImportForm({
const [nsecInput, setNsecInput] = React.useState("");
const [passphrase, setPassphrase] = React.useState("");
const [isImporting, setIsImporting] = React.useState(false);
const importInFlightRef = React.useRef(false);
const [importError, setImportError] = React.useState<string | null>(null);
const [isDragging, setIsDragging] = React.useState(false);
const dragDepthRef = React.useRef(0);
@@ -186,8 +194,9 @@ export function NostrKeyImportForm({
// Guard here, not just on the submit button: the button now lives in the
// portaled footer as type="button", so the single-field form still submits
// on Enter. Without this, pressing Enter during an in-flight import fires a
// second concurrent onImport (double keyring write).
if (isInteractionDisabled) {
// second concurrent onImport (double keyring write). A ref closes the
// same-tick gap before React commits `isImporting`.
if (isInteractionDisabled || importInFlightRef.current) {
return;
}
@@ -202,6 +211,8 @@ export function NostrKeyImportForm({
return;
}
importInFlightRef.current = true;
onImportingChange?.(true);
setIsImporting(true);
setImportError(null);
@@ -212,6 +223,8 @@ export function NostrKeyImportForm({
error instanceof Error ? error.message : "Couldn't import this key.",
);
} finally {
importInFlightRef.current = false;
onImportingChange?.(false);
setIsImporting(false);
}
}, [
@@ -220,6 +233,7 @@ export function NostrKeyImportForm({
isPasswordStage,
isValid,
onImport,
onImportingChange,
passphrase,
trimmedInput,
]);
@@ -611,7 +625,7 @@ export function NostrKeyImportForm({
</Button>
) : null}
{showBack || isPasswordStage ? (
{showBack || (isPasswordStage && showPasswordStageBack) ? (
<Button
className={
variant === "spotlight"
@@ -20,7 +20,10 @@ import { AvatarStep } from "./AvatarStep";
import { OnboardingChrome } from "./OnboardingChrome";
import { OnboardingFooterProvider } from "./OnboardingFooter";
import { MembershipDenied } from "./MembershipDenied";
import { NostrKeyImportForm } from "./NostrKeyImportForm";
import {
NostrKeyImportForm,
type NostrKeyImportStage,
} from "./NostrKeyImportForm";
import { useCommunities } from "@/features/communities/useCommunities";
import { CommunityChangeOverlay } from "@/features/communities/ui/CommunityChangeOverlay";
import {
@@ -165,6 +168,10 @@ export function OnboardingFlow({
const [currentPage, setCurrentPage] = React.useState<OnboardingPage>(
identityLost ? "key-import" : "profile",
);
const [keyImportStage, setKeyImportStage] =
React.useState<NostrKeyImportStage>("key-entry");
const [isKeyImporting, setIsKeyImporting] = React.useState(false);
const [keyImportFormKey, setKeyImportFormKey] = React.useState(0);
const [profileDraft, setProfileDraft] =
React.useState<OnboardingProfileValues>(savedProfile);
const [deniedPubkey, setDeniedPubkey] = React.useState<string>("");
@@ -424,6 +431,47 @@ export function OnboardingFlow({
}
}, [queryClient]);
const handleKeyImportBack = React.useCallback(() => {
if (keyImportStage === "backup-password") {
setKeyImportFormKey((current) => current + 1);
setKeyImportStage("key-entry");
return;
}
if (identityLost) {
void handleLostModeBack();
return;
}
showProfilePage();
}, [handleLostModeBack, identityLost, keyImportStage, showProfilePage]);
const chromeBackAction =
currentPage === "profile"
? {
disabled: profileStepState.isSaving,
onClick: () => {
setMembershipError(null);
setIsCommunityChangeOpen(true);
},
}
: currentPage === "key-import"
? {
label:
keyImportStage === "backup-password"
? "Back"
: identityLost
? "Start new identity"
: "Back",
disabled: isKeyImporting,
onClick: handleKeyImportBack,
}
: currentPage === "avatar"
? {
disabled:
avatarStepState.isSaving || avatarStepState.isUploadingAvatar,
onClick: showProfilePage,
}
: undefined;
if (currentPage === "membership-denied") {
return (
<>
@@ -458,7 +506,7 @@ export function OnboardingFlow({
>
<StartupWindowDragRegion />
<OnboardingChrome current={currentStep} total={totalOnboardingSteps} />
<OnboardingFooterProvider>
<OnboardingFooterProvider backAction={chromeBackAction}>
<div
className={`relative flex w-full flex-col items-center text-center ${
currentPage === "avatar" ? "max-w-[1080px]" : "max-w-[500px]"
@@ -501,10 +549,6 @@ export function OnboardingFlow({
<ProfileStep
actions={{
advanceWithoutSaving: advanceFromProfileWithoutSaving,
back: () => {
setMembershipError(null);
setIsCommunityChangeOpen(true);
},
clearAvatarDraft: resetAvatarDraft,
importExistingKey: showKeyImportPage,
onUploadingChange: setIsUploadingAvatar,
@@ -559,9 +603,13 @@ export function OnboardingFlow({
) : null}
<NostrKeyImportForm
backLabel={identityLost ? "Start new identity" : undefined}
onBack={identityLost ? handleLostModeBack : showProfilePage}
key={keyImportFormKey}
onBack={handleKeyImportBack}
onImport={importExistingKey}
onImportingChange={setIsKeyImporting}
onStageChange={setKeyImportStage}
showBack={false}
showPasswordStageBack={false}
/>
</OnboardingSlideTransition>
) : (
@@ -578,6 +626,7 @@ export function OnboardingFlow({
}}
direction={transitionDirection}
showAlwaysSkip={true}
showBack={false}
state={avatarStepState}
/>
)}
@@ -2,11 +2,20 @@ import * as React from "react";
import { createPortal } from "react-dom";
import { cn } from "@/shared/lib/cn";
import { Button } from "@/shared/ui/button";
const OnboardingFooterTargetContext = React.createContext<HTMLElement | null>(
null,
);
/** Configuration for the provider-rendered, bottom-docked Back button. */
export type OnboardingBackAction = {
disabled?: boolean;
label?: string;
onClick: () => void;
testId?: string;
};
/**
* Renders the shared, bottom-docked CTA slot for an onboarding shell and
* exposes it to descendant steps via context.
@@ -21,8 +30,10 @@ const OnboardingFooterTargetContext = React.createContext<HTMLElement | null>(
* docked buttons.
*/
export function OnboardingFooterProvider({
backAction,
children,
}: {
backAction?: OnboardingBackAction;
children: React.ReactNode;
}) {
const [target, setTarget] = React.useState<HTMLElement | null>(null);
@@ -38,6 +49,20 @@ export function OnboardingFooterProvider({
aria-hidden
className="pointer-events-none fixed inset-x-0 bottom-0 z-10 h-36 bg-[linear-gradient(to_top,var(--buzz-onboarding-shell-bottom)_35%,transparent)]"
/>
{backAction ? (
<div className="fixed bottom-5 left-6 z-20">
<Button
className="h-9 rounded-full bg-foreground/10 px-6 text-sm text-foreground hover:bg-foreground/15 hover:text-foreground"
data-testid={backAction.testId ?? "onboarding-back"}
disabled={backAction.disabled}
onClick={backAction.onClick}
type="button"
variant="ghost"
>
{backAction.label ?? "Back"}
</Button>
</div>
) : null}
<div
className="pointer-events-none fixed inset-x-0 bottom-5 z-20 flex justify-center px-4"
data-testid="onboarding-footer-slot"
@@ -1,6 +1,6 @@
import * as React from "react";
import { openUrl } from "@tauri-apps/plugin-opener";
import { Check } from "lucide-react";
import { Check, Info } from "lucide-react";
import {
useAcpAuthMethodsQuery,
@@ -602,10 +602,12 @@ function RuntimeProvidersLoadingState() {
function RuntimeProvidersSection({
installResults,
navigateToAgentSettings,
onInstallResultsChange,
runtimeProviders,
}: {
installResults: InstallResultsState;
navigateToAgentSettings?: () => void;
onInstallResultsChange: React.Dispatch<
React.SetStateAction<InstallResultsState>
>;
@@ -655,6 +657,26 @@ function RuntimeProvidersSection({
{errorMessage}
</p>
) : null}
<p className="mx-auto flex max-w-[440px] items-start justify-center gap-1.5 text-center text-xs leading-5 text-[var(--buzz-onboarding-backup-ink)]">
<Info aria-hidden className="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span>
More harnesses (Cursor, Grok, Amp&hellip;){" "}
{navigateToAgentSettings ? (
<button
className="underline underline-offset-2 hover:text-foreground"
data-testid="onboarding-setup-more-harnesses"
onClick={navigateToAgentSettings}
type="button"
>
Settings → Agents
</button>
) : (
<span>Settings → Agents</span>
)}{" "}
after setup.
</span>
</p>
</div>
</section>
);
@@ -693,60 +715,30 @@ function SetupStepContent({
>
<RuntimeProvidersSection
installResults={installResults}
navigateToAgentSettings={actions.navigateToAgentSettings}
onInstallResultsChange={setInstallResults}
runtimeProviders={runtimeProviders}
/>
<OnboardingFooter>
{/* Relative row keeps the primary CTA truly centered while Skip
hangs off its right edge without shifting the center. */}
<div className="relative flex items-center justify-center">
<Button
className={`${ONBOARDING_PRIMARY_CTA_CLASS} text-sm`}
data-testid="onboarding-setup-next"
disabled={readyRuntimeIds.length === 0}
onClick={() => actions.next(readyRuntimeIds)}
type="button"
>
Next
</Button>
<Button
className="absolute left-full ml-3 h-9 animate-in whitespace-nowrap rounded-full px-6 text-sm fade-in fill-mode-backwards [animation-delay:1000ms] animation-duration-[500ms] hover:bg-foreground/10 motion-reduce:animate-none"
data-testid="onboarding-setup-skip"
onClick={() => actions.next([])}
type="button"
variant="ghost"
>
Skip for now
</Button>
</div>
<Button
className="h-9 rounded-full bg-foreground/10 px-6 text-sm hover:bg-foreground/15"
data-testid="onboarding-back"
onClick={actions.back}
className={`${ONBOARDING_PRIMARY_CTA_CLASS} text-sm`}
data-testid="onboarding-setup-next"
disabled={readyRuntimeIds.length === 0}
onClick={() => actions.next(readyRuntimeIds)}
type="button"
>
Next
</Button>
<Button
className="h-9 whitespace-nowrap rounded-full px-6 text-sm hover:bg-foreground/10"
data-testid="onboarding-setup-skip"
onClick={() => actions.next([])}
type="button"
variant="ghost"
>
Back
Skip for now
</Button>
<p className="text-xs text-foreground/50">
More harnesses (Cursor, Grok, Amp&hellip;){" "}
{actions.navigateToAgentSettings ? (
<button
className="text-foreground/70 underline underline-offset-2 hover:text-foreground"
data-testid="onboarding-setup-more-harnesses"
onClick={actions.navigateToAgentSettings}
type="button"
>
Settings → Agents
</button>
) : (
<span className="text-foreground/70">Settings → Agents</span>
)}{" "}
after setup.
</p>
</OnboardingFooter>
</OnboardingSlideTransition>
);
@@ -758,7 +750,6 @@ export function SetupStep({
onReadyRuntimeIdsChange,
}: SetupStepProps) {
const state = useSetupStepState();
return (
<SetupStepContent
actions={actions}
+4 -1
View File
@@ -7,6 +7,7 @@ import { useRelayConnection } from "@/shared/api/useRelayConnection";
import { getOsIdleSeconds } from "@/shared/api/osIdle";
import { getPresence } from "@/shared/api/tauri";
import { normalizePubkey } from "@/shared/lib/pubkey";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import {
mergePresenceUpdate,
parseLivePresenceEvent,
@@ -83,6 +84,7 @@ export function usePresenceQuery(
const enabled = (options?.enabled ?? true) && normalizedPubkeys.length > 0;
const connectionState = useRelayConnection();
const connected = connectionState === "connected";
const refetchInterval = useFocusedRefetchInterval(connected ? 60_000 : false);
return useQuery<PresenceLookup>({
enabled,
@@ -92,7 +94,8 @@ export function usePresenceQuery(
// Backstop poll: catches REST-only writers (ACP agents) and TTL expiry
// (crashed clients). WS events handle the fast path. Pause on degraded
// connections — HTTP presence calls fail anyway and consume relay quota.
refetchInterval: connected ? 60_000 : false,
refetchInterval,
refetchOnWindowFocus: true,
});
}
@@ -0,0 +1,71 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
beginAvatarPresentation,
getAvatarPresentation,
resetAvatarPresentations,
} from "./avatarPresentationStore.ts";
import { buildAnimatedAvatarUrl } from "../../shared/lib/animatedAvatar.ts";
const POSTER_URL = "https://media.example.com/avatar-poster.png";
const ANIMATION_URL = "https://media.example.com/avatar-animation.png";
test("animated avatar presentation waits for both remote assets", async (t) => {
const originalImage = globalThis.Image;
const originalRequestAnimationFrame = globalThis.requestAnimationFrame;
const originalWindow = globalThis.window;
const originalCreateObjectURL = URL.createObjectURL;
const originalRevokeObjectURL = URL.revokeObjectURL;
const requestedPaths = [];
class ProbeImage {
onerror = null;
onload = null;
referrerPolicy = "";
set src(value) {
const path = new URL(value).pathname;
requestedPaths.push(path);
queueMicrotask(() => {
if (path === "/avatar-poster.png") this.onload?.();
else this.onerror?.();
});
}
}
globalThis.Image = ProbeImage;
globalThis.requestAnimationFrame = (callback) =>
setTimeout(() => callback(performance.now()), 0);
globalThis.window = globalThis;
URL.createObjectURL = () => "blob:local-poster";
URL.revokeObjectURL = () => {};
t.after(() => {
resetAvatarPresentations();
globalThis.Image = originalImage;
globalThis.requestAnimationFrame = originalRequestAnimationFrame;
globalThis.window = originalWindow;
URL.createObjectURL = originalCreateObjectURL;
URL.revokeObjectURL = originalRevokeObjectURL;
});
const avatarUrl = buildAnimatedAvatarUrl(POSTER_URL, ANIMATION_URL);
beginAvatarPresentation(avatarUrl, new Blob(["poster"]));
await assert.doesNotReject(async () => {
for (let attempt = 0; attempt < 20; attempt += 1) {
if (requestedPaths.length >= 2) return;
await new Promise((resolve) => setTimeout(resolve, 5));
}
throw new Error("avatar presentation did not probe both assets");
});
assert.deepEqual(requestedPaths.slice(0, 2).sort(), [
"/avatar-animation.png",
"/avatar-poster.png",
]);
assert.deepEqual(getAvatarPresentation(avatarUrl), {
displayUrl: "blob:local-poster",
state: "pending",
});
});
@@ -1,6 +1,10 @@
import * as React from "react";
import { toast } from "sonner";
import {
buildAnimatedAvatarUrl,
parseAnimatedAvatarUrl,
} from "@/shared/lib/animatedAvatar";
import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
export type AvatarPresentationState = "failed" | "pending" | "ready";
@@ -84,6 +88,37 @@ function probeImage(
});
}
function presentationAssetUrls(remoteUrl: string): string[] {
const animated = parseAnimatedAvatarUrl(remoteUrl);
return animated ? [animated.posterUrl, animated.animationUrl] : [remoteUrl];
}
function verifiedPresentationUrl(
remoteUrl: string,
verifiedUrls: Array<string | null>,
): string | null {
if (verifiedUrls.some((verifiedUrl) => !verifiedUrl)) return null;
const animated = parseAnimatedAvatarUrl(remoteUrl);
if (!animated) return verifiedUrls[0] ?? null;
const [posterUrl, animationUrl] = verifiedUrls;
return posterUrl && animationUrl
? buildAnimatedAvatarUrl(posterUrl, animationUrl)
: null;
}
async function probePresentation(
remoteUrl: string,
attempt: number,
): Promise<string | null> {
const verifiedUrls = await Promise.all(
presentationAssetUrls(remoteUrl).map((assetUrl) =>
probeImage(assetUrl, attempt),
),
);
return verifiedPresentationUrl(remoteUrl, verifiedUrls);
}
async function verifyPresentation(
entry: AvatarPresentationEntry,
): Promise<void> {
@@ -91,7 +126,7 @@ async function verifyPresentation(
await wait(delayMs);
if (!isCurrent(entry) || entry.snapshot.state !== "pending") return;
const verifiedUrl = await probeImage(entry.remoteUrl, attempt);
const verifiedUrl = await probePresentation(entry.remoteUrl, attempt);
if (!isCurrent(entry) || entry.snapshot.state !== "pending") return;
if (!verifiedUrl) continue;
@@ -3,9 +3,12 @@ import test from "node:test";
import {
parseSelfProfileCache,
MAX_SELF_PROFILE_CACHES,
MAX_SELF_PROFILE_CACHES_PER_RELAY,
resolveAvatarDataUrl,
shouldFetchAvatar,
storageKey,
writeSelfProfileCache,
} from "./selfProfileStorage.ts";
test("storageKey: includes pubkey in result", () => {
@@ -48,6 +51,75 @@ test("storageKey: different pubkeys produce different keys", () => {
assert.notEqual(a, b);
});
function installStorage(onGetItem = () => {}) {
const values = new Map();
globalThis.window = {
dispatchEvent: () => true,
localStorage: {
get length() {
return values.size;
},
getItem: (key) => {
onGetItem(key);
return values.get(key) ?? null;
},
key: (index) => [...values.keys()][index] ?? null,
removeItem: (key) => values.delete(key),
setItem: (key, value) => values.set(key, String(value)),
},
};
globalThis.CustomEvent ??= class CustomEvent {};
return values;
}
test("writeSelfProfileCache caps each relay and the global cache by updatedAt", () => {
const values = installStorage();
const relayA = "wss://relay-a.example";
for (let index = 0; index < MAX_SELF_PROFILE_CACHES_PER_RELAY + 1; index++) {
assert.equal(
writeSelfProfileCache(
relayA,
`pubkey-${index}`,
makeCache({ updatedAt: index }),
),
true,
);
}
assert.equal(values.has(storageKey(relayA, "pubkey-0")), false);
assert.equal(values.has(storageKey(relayA, "pubkey-8")), true);
for (let index = 0; index < MAX_SELF_PROFILE_CACHES + 1; index++) {
writeSelfProfileCache(
`wss://relay-${index}.example`,
`global-${index}`,
makeCache({ updatedAt: index + 100 }),
);
}
const profileKeys = [...values.keys()].filter((key) =>
key.startsWith("buzz-self-profile.v1:"),
);
assert.equal(profileKeys.length, MAX_SELF_PROFILE_CACHES);
assert.equal(values.has(storageKey(relayA, "pubkey-1")), false);
});
test("writeSelfProfileCache does not read existing payloads below both caps", () => {
const readKeys = [];
const values = installStorage((key) => readKeys.push(key));
const relay = "wss://relay.example";
const existingKey = storageKey(relay, "existing");
const writtenKey = storageKey(relay, "written");
values.set(existingKey, JSON.stringify(makeCache({ updatedAt: 1 })));
assert.equal(
writeSelfProfileCache(relay, "written", makeCache({ updatedAt: 2 })),
true,
);
assert.deepEqual(readKeys, [writtenKey]);
assert.equal(values.has(existingKey), true);
assert.equal(values.has(writtenKey), true);
});
test("parseSelfProfileCache: valid v1 payload round-trips", () => {
const payload = {
version: 1,
@@ -15,6 +15,8 @@ export { normalizeRelayUrl } from "@/shared/lib/normalizeRelayUrl";
import { normalizeRelayUrl } from "@/shared/lib/normalizeRelayUrl";
const STORAGE_KEY_PREFIX = "buzz-self-profile.v1";
export const MAX_SELF_PROFILE_CACHES_PER_RELAY = 8;
export const MAX_SELF_PROFILE_CACHES = 32;
/**
* Dispatched on window after a successful writeSelfProfileCache so that any
@@ -127,6 +129,63 @@ export function readSelfProfileCache(
}
}
function trimSelfProfileCaches(relayUrl: string, preservedKey: string): void {
const relayPrefix = `${STORAGE_KEY_PREFIX}:${normalizeRelayUrl(relayUrl)}:`;
let totalEntryCount = 0;
let relayEntryCount = 0;
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (!key?.startsWith(`${STORAGE_KEY_PREFIX}:`)) continue;
totalEntryCount += 1;
if (key.startsWith(relayPrefix)) relayEntryCount += 1;
}
if (
totalEntryCount <= MAX_SELF_PROFILE_CACHES &&
relayEntryCount <= MAX_SELF_PROFILE_CACHES_PER_RELAY
) {
return;
}
const entries: Array<{ key: string; updatedAt: number }> = [];
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (!key?.startsWith(`${STORAGE_KEY_PREFIX}:`)) continue;
const raw = window.localStorage.getItem(key);
if (!raw) continue;
try {
entries.push({
key,
updatedAt: parseSelfProfileCache(JSON.parse(raw))?.updatedAt ?? 0,
});
} catch {
entries.push({ key, updatedAt: 0 });
}
}
const relayEntries = entries.filter((entry) =>
entry.key.startsWith(relayPrefix),
);
const keysToRemove = new Set<string>();
for (const candidates of [relayEntries, entries]) {
const maxEntries =
candidates === relayEntries
? MAX_SELF_PROFILE_CACHES_PER_RELAY
: MAX_SELF_PROFILE_CACHES;
const removable = candidates
.filter((entry) => entry.key !== preservedKey)
.sort((left, right) => left.updatedAt - right.updatedAt);
let removeCount =
candidates.filter((entry) => !keysToRemove.has(entry.key)).length -
maxEntries;
for (const entry of removable) {
if (removeCount <= 0) break;
if (keysToRemove.has(entry.key)) continue;
keysToRemove.add(entry.key);
removeCount -= 1;
}
}
for (const key of keysToRemove) window.localStorage.removeItem(key);
}
/**
* Writes the cache to localStorage and fires SELF_PROFILE_CACHE_EVENT so
* mounted components can re-read without polling.
@@ -146,6 +205,7 @@ export function writeSelfProfileCache(
// when nothing changed. Skip the write and event entirely when identical.
if (window.localStorage.getItem(key) === serialized) return true;
window.localStorage.setItem(key, serialized);
trimSelfProfileCaches(relayUrl, key);
// localStorage is not reactive — dispatch a custom event so any mounted
// listeners (e.g. useEffect with addEventListener) can re-read the cache
// without a polling interval.
@@ -58,7 +58,7 @@ export function AnimatedAvatarCameraControls({
{helpText}
</p>
) : null}
<div className="h-14 pt-2">
<div className={cn("h-14 pt-2", compact && "flex justify-center")}>
{onRetry ? (
<Button
className={cn(
@@ -77,9 +77,9 @@ export function AnimatedAvatarCameraControls({
<Button
asChild
className={cn(
"h-12 w-full rounded-xl",
compact &&
"bg-[rgb(var(--buzz-onboarding-avatar-accent-bg))] text-[rgb(var(--buzz-onboarding-avatar-accent-fg))] hover:bg-[rgb(var(--buzz-onboarding-avatar-accent-bg))]",
compact
? "h-[2.375rem] rounded-full bg-[rgb(var(--buzz-onboarding-avatar-action-bg))] px-6 text-sm font-medium text-[rgb(var(--buzz-onboarding-avatar-action-fg))] hover:bg-[color:rgb(var(--buzz-onboarding-avatar-action-bg)_/_0.9)]"
: "h-12 w-full rounded-xl",
)}
data-testid={`${testIdPrefix}-animated-record`}
disabled={disabled}
@@ -6,7 +6,9 @@ import {
composeAvatarFrame,
DEFAULT_PERSON_SCALE,
} from "@/features/profile/lib/animatedAvatarCapture";
import { beginAvatarPresentation } from "@/features/profile/avatarPresentationStore";
import { AVATAR_COLORS } from "@/features/profile/ui/ProfileAvatarEditor.utils";
import { buildAnimatedAvatarUrl } from "@/shared/lib/animatedAvatar";
export type CapturePhase =
| "idle"
@@ -30,6 +32,23 @@ export const ENTRANCE_TRANSITION = {
ease: [0.23, 1, 0.32, 1],
} as const;
/**
* Builds the composite animated-avatar URL and starts a temporary local-poster
* presentation while the uploaded poster and animation become available.
*/
export function presentAnimatedAvatar(
poster: { url: string },
animation: { url: string },
posterBytes: Uint8Array,
): string {
const avatarUrl = buildAnimatedAvatarUrl(poster.url, animation.url);
beginAvatarPresentation(
avatarUrl,
new Blob([Uint8Array.from(posterBytes).buffer], { type: "image/png" }),
);
return avatarUrl;
}
/** Keep things draggable but never fully lost off-frame. */
const MAX_OFFSET = 192;
@@ -49,6 +49,7 @@ import {
defaultPersonScaleForSource,
PERSON_SIZE_TIP,
preferredCameraDevice,
presentAnimatedAvatar,
randomBackdropColor,
} from "@/features/profile/ui/AnimatedAvatarCapture.helpers";
import {
@@ -63,7 +64,6 @@ import {
normalizeHue,
} from "@/features/profile/ui/ProfileAvatarEditor.utils";
import { uploadMediaBytes } from "@/shared/api/tauri";
import { buildAnimatedAvatarUrl } from "@/shared/lib/animatedAvatar";
import { cn } from "@/shared/lib/cn";
import { Button } from "@/shared/ui/button";
import { Spinner } from "@/shared/ui/spinner";
@@ -519,7 +519,9 @@ export function AnimatedAvatarCapture({
setErrorMessage("The relay rejected the recording. Try again.");
return false;
}
onApply(buildAnimatedAvatarUrl(posterUpload.url, animationUpload.url));
onApply(
presentAnimatedAvatar(posterUpload, animationUpload, posterBytes),
);
return true;
} catch (error) {
setErrorMessage(
@@ -7,7 +7,6 @@ import { flushSync } from "react-dom";
import { AnimatedAvatarCapture } from "@/features/profile/ui/AnimatedAvatarCapture";
import { AvatarCustomColorPanel } from "@/features/profile/ui/AvatarCustomColorPanel";
import { ProfileAvatarUploadPreview } from "@/features/profile/ui/ProfileAvatarUploadPreview";
import { ProfileAvatarModeTabs } from "@/features/profile/ui/ProfileAvatarModeTabs";
import { useAvatarSelection } from "@/features/profile/avatarPresentationStore";
import { useAvatarUpload } from "@/features/profile/useAvatarUpload";
@@ -72,12 +71,12 @@ export function ProfileAvatarEditor({
onCustomColorPickerOpenChange,
onEmojiAvatarChange,
onModeChange,
onLocalPreviewChange,
onUploadedAvatarChange,
onUrlChange,
onAnimatedAvatarApply,
onDone,
onUploadingChange,
previewName,
showEmojiColorControlsWhenEmpty = false,
disabled,
testIdPrefix = "profile-avatar",
@@ -97,6 +96,9 @@ export function ProfileAvatarEditor({
const [isDragging, setIsDragging] = React.useState(false);
const [urlDraft, setUrlDraft] = React.useState("");
const localPreview = useLocalAvatarPreview();
React.useEffect(() => {
onLocalPreviewChange?.(localPreview.previewUrl);
}, [localPreview.previewUrl, onLocalPreviewChange]);
const [selectedEmoji, setSelectedEmoji] = React.useState<string | null>(
() => initialEmojiAvatar?.emoji ?? null,
);
@@ -502,7 +504,9 @@ export function ProfileAvatarEditor({
<fieldset
className={cn(
"mx-auto w-full border-0 p-0 text-sm",
isOnboardingModal ? "max-w-[456px]" : "max-w-[576px]",
isOnboardingModal
? "max-w-[456px] md:ml-0 md:mr-auto"
: "max-w-[576px]",
)}
data-testid={`${testIdPrefix}-editor`}
disabled={isInputDisabled}
@@ -612,14 +616,6 @@ export function ProfileAvatarEditor({
onClick={openPicker}
type="button"
>
{isOnboardingModal &&
(localPreview.previewUrl || avatarUrl) ? (
<ProfileAvatarUploadPreview
avatarUrl={localPreview.previewUrl || avatarUrl || ""}
label={previewName}
testId={`${testIdPrefix}-upload-preview`}
/>
) : null}
<span
aria-hidden="true"
className={cn(
@@ -745,7 +741,6 @@ export function ProfileAvatarEditor({
onPreviewCaptionChange={onAnimatedPreviewCaptionChange}
previewContainer={animatedPreviewContainer}
registerApply={registerAnimatedApply}
autoStartCamera={isOnboardingModal}
compactReview={isOnboardingModal}
showApplyButton={!onDone}
testIdPrefix={testIdPrefix}
@@ -753,7 +748,11 @@ export function ProfileAvatarEditor({
) : (
<div className="relative grid content-start gap-3">
<div
className="buzz-emoji-mart relative z-0 h-[316px] overflow-hidden rounded-xl bg-muted transition-colors duration-[250ms] ease-out"
className={cn(
"buzz-emoji-mart relative z-0 overflow-hidden rounded-xl bg-muted transition-colors duration-[250ms] ease-out",
isOnboardingModal ? "h-[316px]" : "h-[384px]",
)}
data-testid={`${testIdPrefix}-emoji-picker`}
ref={emojiPickerContainerRef}
style={emojiMartThemeVars}
>
@@ -788,9 +787,9 @@ export function ProfileAvatarEditor({
applyEmojiAvatar(emoji.native, nextColor);
}}
previewPosition="none"
searchPosition="none"
searchPosition="sticky"
set="native"
skinTonePosition="none"
skinTonePosition="search"
theme={emojiPickerTheme}
/>
</div>
@@ -12,6 +12,11 @@ export type ProfileAvatarEditorProps = {
onEmojiAvatarChange?: () => void;
onCustomColorPickerOpenChange?: (isOpen: boolean) => void;
onModeChange?: (mode: AvatarMode) => void;
/**
* Reports a temporary local object URL while an image upload is pending, then
* emits `null` when that preview is cleared or replaced by the remote URL.
*/
onLocalPreviewChange?: (url: string | null) => void;
onUploadedAvatarChange?: (url: string | null) => void;
onUploadingChange?: (isUploading: boolean) => void;
onAnimatedAvatarApply?: (url: string) => void;
@@ -72,6 +72,7 @@ const EMOJI_MART_SHADOW_CSS = `
#root {
--padding: var(--buzz-emoji-picker-padding, 16px);
--buzz-emoji-picker-search-control-height: 48px;
--sidebar-width: 0px;
display: flex;
flex-direction: column;
@@ -127,6 +128,47 @@ const EMOJI_MART_SHADOW_CSS = `
display: none;
}
/* Match the app's member-search controls: a distinct resting surface and
* border make both the emoji search and its adjacent skin-tone control easy
* to find before either receives focus. */
.search input[type="search"],
.search + .flex {
background-color: rgb(var(--em-rgb-input));
box-shadow: inset 0 0 0 1px rgba(var(--em-rgb-color), 0.16);
}
.search input[type="search"] {
border-radius: 12px;
height: var(--buzz-emoji-picker-search-control-height);
padding-bottom: 0;
padding-top: 0;
}
.search input[type="search"]:focus {
box-shadow: inset 0 0 0 1px rgb(var(--em-rgb-accent));
}
.search + .flex {
border-radius: 12px;
flex: 0 0 auto;
height: var(--buzz-emoji-picker-search-control-height) !important;
margin-left: 8px;
width: var(--buzz-emoji-picker-search-control-height) !important;
}
.skin-tone-button {
background-color: transparent !important;
border: 0 !important;
border-radius: 8px;
box-shadow: none !important;
height: calc(var(--buzz-emoji-picker-search-control-height) - 8px) !important;
width: calc(var(--buzz-emoji-picker-search-control-height) - 8px) !important;
}
.skin-tone-button[aria-selected] {
background-color: transparent !important;
}
.category button .background {
background-color: transparent;
transition: background-color var(--duration) var(--easing);
@@ -593,14 +635,12 @@ export function useEmojiMartThemeVars() {
const foreground = hslToRgbString(
styles.getPropertyValue("--foreground"),
);
const background = hslToRgbString(
styles.getPropertyValue("--background"),
);
const input = hslToRgbString(styles.getPropertyValue("--input"));
setThemeVars({
"--buzz-emoji-picker-rgb-background": muted ?? "54, 58, 79",
"--buzz-emoji-picker-rgb-color": foreground ?? "245, 247, 255",
"--buzz-emoji-picker-rgb-input": background ?? "47, 51, 68",
"--buzz-emoji-picker-rgb-input": input ?? "47, 51, 68",
} as React.CSSProperties);
};
@@ -42,25 +42,28 @@ export function ProfileAvatarModeTabs({
aria-label="Avatar type"
className={cn(
isOnboardingModal
? "flex h-10 w-auto gap-2 rounded-none bg-transparent p-0 text-muted-foreground"
? "relative isolate grid h-10 w-full max-w-[320px] grid-cols-3 overflow-hidden rounded-full bg-[color:rgb(var(--buzz-onboarding-avatar-control-fg)_/_0.12)] p-1 text-muted-foreground"
: "relative isolate grid h-14 w-full grid-cols-3 overflow-hidden rounded-full bg-muted p-1 text-muted-foreground",
)}
>
{isOnboardingModal ? null : (
<div
aria-hidden="true"
className="absolute bottom-1 left-1 top-1 z-0 rounded-full bg-background shadow transition-transform duration-[250ms] ease-out"
style={{
transform: `translateX(${MODE_TAB_ORDER.indexOf(mode) * 100}%)`,
width: "calc((100% - 8px) / 3)",
}}
/>
)}
<div
aria-hidden="true"
className={cn(
"absolute bottom-1 left-1 top-1 z-0 rounded-full shadow transition-transform duration-[250ms] ease-out",
isOnboardingModal
? "bg-[rgb(var(--buzz-onboarding-avatar-action-bg))]"
: "bg-background",
)}
style={{
transform: `translateX(${MODE_TAB_ORDER.indexOf(mode) * 100}%)`,
width: "calc((100% - 8px) / 3)",
}}
/>
{MODE_TAB_ORDER.map((tabMode) => (
<TabsTrigger
className={cn(
isOnboardingModal
? "relative z-10 h-10 rounded-[6px] px-4 text-sm font-normal shadow-none transition-colors data-[state=active]:bg-[rgb(var(--buzz-onboarding-avatar-action-bg))] data-[state=active]:text-[rgb(var(--buzz-onboarding-avatar-action-fg))] data-[state=active]:shadow-none"
? "relative z-10 h-full rounded-full bg-transparent px-4 text-sm font-normal shadow-none transition-colors data-[state=active]:bg-transparent data-[state=active]:text-[rgb(var(--buzz-onboarding-avatar-action-fg))] data-[state=active]:shadow-none"
: "relative z-10 h-full rounded-full bg-transparent text-sm font-medium shadow-none transition-colors data-[state=active]:bg-transparent data-[state=active]:text-foreground data-[state=active]:shadow-none",
)}
disabled={disabled}
@@ -11,6 +11,7 @@ import type {
Repository as Project,
} from "@/features/projects/hooks";
import { useProjectRepoHost } from "@/features/projects/useProjectRepoHost";
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
import { publishProjectPullRequestUpdate } from "./pullRequestMutations";
/** Local-vs-remote git sync status for a project checkout (ahead/behind
@@ -24,6 +25,7 @@ export function useProjectRepoSyncStatusQuery(
baseBranch?: string | null,
) {
const selectedBranch = branchName ?? project?.defaultBranch ?? null;
const refetchInterval = useFocusedRefetchInterval(60_000);
const selectedBaseBranch = baseBranch ?? project?.defaultBranch ?? null;
const host = useProjectRepoHost(project);
@@ -48,7 +50,7 @@ export function useProjectRepoSyncStatusQuery(
});
},
staleTime: 10_000,
refetchInterval: 60_000,
refetchInterval,
refetchOnWindowFocus: true,
retry: 1,
});

Some files were not shown because too many files have changed in this diff Show More