Skip to content

Commit 8969745

Browse files
authored
fix(desktop): enforce observer archive policy reconciliation on internal builds (#1923)
Signed-off-by: Will Pfleger <[email protected]>
1 parent 1eae857 commit 8969745

13 files changed

Lines changed: 564 additions & 211 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ jobs:
169169
run: just desktop-tauri-test
170170
env:
171171
CMAKE_POLICY_VERSION_MINIMUM: "3.5"
172+
- name: Desktop Tauri compiled-flag verification
173+
run: just desktop-tauri-test-compiled-flags
174+
env:
175+
CMAKE_POLICY_VERSION_MINIMUM: "3.5"
172176
- name: Upload desktop e2e artifacts
173177
if: failure()
174178
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7

‎Justfile‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ desktop-tauri-check: _ensure-sidecar-stubs
193193
desktop-tauri-test: _ensure-sidecar-stubs
194194
cd desktop/src-tauri && cargo test
195195

196+
# Verify compiled-flag behavior under both compile states (clean + internal).
197+
# Runs the observer_archive focused test twice with independently supplied
198+
# expected values; build.rs rerun-if-env-changed triggers recompilation.
199+
desktop-tauri-test-compiled-flags: _ensure-sidecar-stubs
200+
#!/usr/bin/env bash
201+
set -euo pipefail
202+
cd desktop/src-tauri
203+
echo "=== Clean build (no flag) → expect false ==="
204+
env -u BUZZ_BUILD_OBSERVER_ARCHIVE_DEFAULT \
205+
BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT=false \
206+
cargo test observer_archive_default_enabled_matches_expected -- --ignored --nocapture
207+
echo "=== Internal build (flag set) → expect true ==="
208+
BUZZ_BUILD_OBSERVER_ARCHIVE_DEFAULT=1 \
209+
BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT=true \
210+
cargo test observer_archive_default_enabled_matches_expected -- --ignored --nocapture
211+
echo "Both compiled states verified."
212+
196213
# Build the full desktop Tauri app locally (unsigned, for testing)
197214
# Sidecar binary list must stay in sync with _ensure-sidecar-stubs above.
198215
# pnpm install is unconditional here: release builds must start from a clean dep tree.

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default defineConfig({
107107
"**/agent-lifecycle-feedback.spec.ts",
108108
"**/inbox-live-update.spec.ts",
109109
"**/mesh-compute.spec.ts",
110+
"**/observer-archive-policy.spec.ts",
110111
],
111112
use: {
112113
...devices["Desktop Chrome"],
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
//! Build-time flag and runtime dev-nest check for observer-feed archive default.
1+
//! Build-time flag and runtime dev-nest check for observer-feed archive policy.
22
//!
33
//! `observer_archive_default_enabled()` returns `true` when either:
44
//! - `BUZZ_BUILD_OBSERVER_ARCHIVE_DEFAULT` was set at build time (internal
55
//! builds bake in the flag via `build.rs`), **or**
66
//! - the running binary is using the dev nest (`~/.buzz-dev`), which is the
77
//! case for all dev builds launched with `just staging` or `just dev`.
88
//!
9-
//! When `true`, the frontend auto-seeds an `owner_p` save subscription for the
10-
//! current identity on first run, so the observer-feed archive is on by default.
9+
//! When `true`, the frontend reconciles the observer archive subscription
10+
//! every startup — unconditionally ensuring kind 24200 exists in the DB
11+
//! regardless of stale localStorage markers.
1112
//!
1213
//! OSS prod builds (baked flag unset, prod nest `~/.buzz`) return `false` —
13-
//! no auto-seeding; the user opts in manually via the Local Archive settings card.
14+
//! no reconciliation; the user manages the subscription via Settings.
1415
15-
/// Returns `true` when observer-feed archive should default to on.
16+
/// Returns `true` when observer-feed archive policy is enforced.
1617
///
1718
/// True when the build has the internal baked flag set, or when the running
18-
/// binary is using the dev nest (`~/.buzz-dev`). The frontend calls this once
19-
/// at startup to decide whether to seed the `owner_p` save subscription.
19+
/// binary is using the dev nest (`~/.buzz-dev`). The frontend calls this
20+
/// every startup to decide whether to reconcile the `owner_p` subscription.
2021
#[tauri::command]
2122
pub fn observer_archive_default_enabled() -> bool {
2223
option_env!("BUZZ_DESKTOP_BUILD_OBSERVER_ARCHIVE_DEFAULT").is_some()
@@ -27,15 +28,27 @@ pub fn observer_archive_default_enabled() -> bool {
2728
mod tests {
2829
use super::*;
2930

31+
// `nest_is_dev()` is deterministic-false in unit tests: NEST_DIR OnceLock
32+
// is uninitialized → falls back to prod `~/.buzz` (nest.rs:101-106), so
33+
// the compiled flag is the sole variable. No runner normalization needed.
34+
//
35+
// #[ignore]: requires BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT to be
36+
// set — `just desktop-tauri-test-compiled-flags` runs it explicitly with
37+
// `--ignored` under both compile states; general `cargo test` skips it.
3038
#[test]
31-
fn test_observer_archive_default_enabled_returns_bool() {
32-
// The command must return a plain bool without panicking.
33-
// Whether it's true or false depends on the build environment;
34-
// what we assert here is just that the return type is correct and
35-
// the function is callable.
39+
#[ignore]
40+
fn test_observer_archive_default_enabled_matches_expected() {
3641
let result = observer_archive_default_enabled();
37-
// In a standard OSS/test build (no BUZZ_DESKTOP_BUILD_OBSERVER_ARCHIVE_DEFAULT
38-
// baked in), this should be false.
39-
assert!(!result, "expected false in OSS/test build");
42+
let expected_str = std::env::var("BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT").expect(
43+
"BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT must be set — \
44+
the dual-compile CI step supplies it; bare `cargo test` is \
45+
not sufficient to validate compiled-flag behavior",
46+
);
47+
let expected = expected_str == "true" || expected_str == "1";
48+
assert_eq!(
49+
result, expected,
50+
"observer_archive_default_enabled() returned {result}, \
51+
expected {expected} (BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT={expected_str:?})"
52+
);
4053
}
4154
}

‎desktop/src/app/AppShell.tsx‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
} from "@/features/user-status/hooks";
5555
import { useCommunityEmojiLiveUpdates } from "@/features/custom-emoji/hooks";
5656
import { useArchiveSync } from "@/features/local-archive/archiveSyncManager";
57-
import { useObserverArchiveSeed } from "@/features/local-archive/useObserverArchiveSeed";
57+
import { useObserverArchiveReconciliation } from "@/features/local-archive/useObserverArchiveSeed";
5858
import { useAgentMetricArchiveSeed } from "@/features/local-archive/useAgentMetricArchiveSeed";
5959
import { useProfileQuery } from "@/features/profile/hooks";
6060
import { SendFeedbackController } from "@/features/settings/ui/SendFeedbackController";
@@ -186,15 +186,18 @@ export function AppShell() {
186186
// relay-owned agents join automatically once identity arrives. Adding a
187187
// guard here would drop managed-agent coverage during startup.
188188
useAgentObserverIngestion();
189-
useArchiveSync();
190-
// Defer the archive *seeds* until startup is idle: they're first-run catch-up
191-
// config (a one-shot mergeSaveSubscriptionKinds), not live-ingest — that's
192-
// useArchiveSync's job, which stays eager above. Passing deferredPubkey makes
193-
// each seed hook wait on its own `if (!pubkey) return` guard until the shell
194-
// is interactive, so their IPC + sqlite archive open doesn't compete with
195-
// first paint. The explicit-choice guard inside each hook is unchanged.
189+
// Kind 24200 is relay-ephemeral, so reconciliation runs eagerly (not
190+
// deferred) and unconditionally repairs the DB subscription on internal
191+
// builds — otherwise frames emitted before the listener opens are lost.
192+
const observerReconciled = useObserverArchiveReconciliation(
193+
identityQuery.data?.pubkey,
194+
);
195+
// useArchiveSync must wait for reconciliation, or listeners could open
196+
// before kind 24200 is guaranteed present in the subscription.
197+
useArchiveSync(observerReconciled);
198+
// Kind 44200 is relay-persisted (durable) and stays deferred: missed
199+
// startup frames can be replayed, so there's no ordering constraint.
196200
const deferredPubkey = startupReady ? identityQuery.data?.pubkey : undefined;
197-
useObserverArchiveSeed(deferredPubkey);
198201
useAgentMetricArchiveSeed(deferredPubkey);
199202
const profileQuery = useProfileQuery();
200203
useRelayAutoHeal();

‎desktop/src/features/local-archive/archiveSyncManager.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,19 @@ export class ArchiveSyncManager {
303303
import * as React from "react";
304304

305305
/**
306-
* Starts the ArchiveSyncManager at app-shell mount and tears it down when the
307-
* component unmounts (community switch). No return value needed — the manager
308-
* runs entirely in the background.
306+
* Starts the ArchiveSyncManager once `ready` is true and tears it down on
307+
* unmount. The `ready` gate ensures observer reconciliation completes before
308+
* any relay listeners open — kind 24200 is relay-ephemeral, so frames emitted
309+
* before the listener opens are permanently lost.
309310
*/
310-
export function useArchiveSync(): void {
311+
export function useArchiveSync(ready: boolean): void {
311312
React.useEffect(() => {
313+
if (!ready) return;
314+
312315
const manager = new ArchiveSyncManager();
313316
void manager.start();
314317
return () => {
315318
manager.destroy();
316319
};
317-
}, []);
320+
}, [ready]);
318321
}

‎desktop/src/features/local-archive/ui/LocalArchiveSettingsCard.tsx‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
SettingsOptionRow,
2626
} from "@/features/settings/ui/SettingsOptionGroup";
2727
import { SettingsSectionHeader } from "@/features/settings/ui/SettingsSectionHeader";
28+
import { observerArchiveDefaultEnabled } from "@/shared/api/tauriArchive";
2829
import { setExplicitObserverArchiveChoice } from "../observerArchivePreference";
2930
import { setExplicitAgentMetricArchiveChoice } from "../agentMetricArchivePreference";
3031

@@ -66,15 +67,18 @@ function kindSummary(kinds: number[]): string {
6667

6768
type ObserverSectionProps = {
6869
enabled: boolean;
70+
policy: boolean | undefined;
6971
toggling: boolean;
7072
onToggle: (checked: boolean) => void;
7173
};
7274

7375
function ObserverArchiveSection({
7476
enabled,
77+
policy,
7578
toggling,
7679
onToggle,
7780
}: ObserverSectionProps) {
81+
const toggleDisabled = toggling || policy === undefined || policy === true;
7882
return (
7983
<div className="space-y-3" data-testid="local-archive-observer-section">
8084
<h2 className="text-lg font-semibold tracking-tight">
@@ -90,15 +94,15 @@ function ObserverArchiveSection({
9094
Archive my agents' observer frames
9195
</label>
9296
<p className="text-sm font-normal text-muted-foreground">
93-
Saves kind {KIND_AGENT_OBSERVER_FRAME} observer frames addressed
94-
to your pubkey. These are ephemeral — not stored by the relay — so
95-
local archiving is the only way to retain them.
97+
{policy === true
98+
? `Always on for internal builds. Kind ${KIND_AGENT_OBSERVER_FRAME} observer frames are ephemeral — not stored by the relay — so local archiving is the only way to retain them.`
99+
: `Saves kind ${KIND_AGENT_OBSERVER_FRAME} observer frames addressed to your pubkey. These are ephemeral — not stored by the relay — so local archiving is the only way to retain them.`}
96100
</p>
97101
</div>
98102
<Switch
99103
checked={enabled}
100104
data-testid="local-archive-observer-toggle"
101-
disabled={toggling}
105+
disabled={toggleDisabled}
102106
id="local-archive-observer-toggle"
103107
onCheckedChange={onToggle}
104108
/>
@@ -389,6 +393,17 @@ export function LocalArchiveSettingsCard() {
389393
const [isAddingOpen, setIsAddingOpen] = React.useState(false);
390394
const [observerToggling, setObserverToggling] = React.useState(false);
391395
const [metricToggling, setMetricToggling] = React.useState(false);
396+
const [observerPolicy, setObserverPolicy] = React.useState<
397+
boolean | undefined
398+
>(undefined);
399+
400+
React.useEffect(() => {
401+
observerArchiveDefaultEnabled()
402+
.then((on) => setObserverPolicy(on))
403+
.catch(() => {
404+
// Fail closed: leave as undefined so toggle stays disabled.
405+
});
406+
}, []);
392407

393408
const pubkey = identityQuery.data?.pubkey ?? "";
394409

@@ -451,6 +466,7 @@ export function LocalArchiveSettingsCard() {
451466
const handleObserverToggle = React.useCallback(
452467
async (checked: boolean) => {
453468
if (!pubkey) return;
469+
if (!checked && observerPolicy !== false) return;
454470
setObserverToggling(true);
455471
try {
456472
if (checked) {
@@ -475,7 +491,7 @@ export function LocalArchiveSettingsCard() {
475491
setObserverToggling(false);
476492
}
477493
},
478-
[pubkey, reload],
494+
[pubkey, observerPolicy, reload],
479495
);
480496

481497
const handleMetricToggle = React.useCallback(
@@ -525,6 +541,7 @@ export function LocalArchiveSettingsCard() {
525541
<ObserverArchiveSection
526542
enabled={observerEnabled}
527543
onToggle={(checked) => void handleObserverToggle(checked)}
544+
policy={observerPolicy}
528545
toggling={observerToggling}
529546
/>
530547

0 commit comments

Comments
 (0)