Skip to content

Commit bf7e986

Browse files
Duncanwpfleger96
andcommitted
fix(desktop): split archived instances into a labeled Runtime subsection
The Runtime > Instances roster rendered every persona instance raw, so relay-archived instances appeared mixed in with live ones as if active — the original Layer-2 confusion the archive-aware selector work set out to fix. Bucket the roster off the same fail-open useIsArchivedPredicate the selector uses (one policy, no duplication) and render archived rows under a labeled Archived subsection. Archived rows keep the explicit-pubkey click so unarchive stays UI-reachable. While the archive snapshot loads the predicate returns false, so every row renders live — nothing hidden or labeled. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 0722267 commit bf7e986

7 files changed

Lines changed: 372 additions & 68 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { bucketPersonaInstances } from "./useCanonicalManagedAgentProfile.ts";
5+
6+
const LIVE_PK = "b".repeat(64);
7+
const SECOND_LIVE_PK = "c".repeat(64);
8+
const ARCHIVED_PK = "a".repeat(64);
9+
10+
function agent(overrides = {}) {
11+
return {
12+
name: "Instance",
13+
pubkey: LIVE_PK,
14+
personaId: "persona-1",
15+
status: "stopped",
16+
...overrides,
17+
};
18+
}
19+
20+
test("mixed roster splits archived from live, preserving order", () => {
21+
const archived = agent({ pubkey: ARCHIVED_PK });
22+
const live = agent({ pubkey: LIVE_PK });
23+
const secondLive = agent({ pubkey: SECOND_LIVE_PK });
24+
25+
const { live: liveBucket, archived: archivedBucket } = bucketPersonaInstances(
26+
[archived, live, secondLive],
27+
(pubkey) => pubkey === ARCHIVED_PK,
28+
);
29+
30+
assert.deepEqual(liveBucket, [live, secondLive]);
31+
assert.deepEqual(archivedBucket, [archived]);
32+
});
33+
34+
test("an all-archived persona puts every instance in the archived bucket", () => {
35+
const first = agent({ pubkey: ARCHIVED_PK });
36+
const second = agent({ pubkey: LIVE_PK });
37+
38+
const { live, archived } = bucketPersonaInstances(
39+
[first, second],
40+
() => true,
41+
);
42+
43+
assert.deepEqual(live, []);
44+
assert.deepEqual(archived, [first, second]);
45+
});
46+
47+
test("fail-open while loading keeps every instance live", () => {
48+
// The predicate returns `false` for all pubkeys until the archive snapshot
49+
// loads; nothing is bucketed as archived, so nothing is labeled or hidden.
50+
const archived = agent({ pubkey: ARCHIVED_PK });
51+
const live = agent({ pubkey: LIVE_PK });
52+
53+
const { live: liveBucket, archived: archivedBucket } = bucketPersonaInstances(
54+
[archived, live],
55+
() => false,
56+
);
57+
58+
assert.deepEqual(liveBucket, [archived, live]);
59+
assert.deepEqual(archivedBucket, []);
60+
});

‎desktop/src/features/profile/lib/useCanonicalManagedAgentProfile.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ export function resolveCanonicalManagedAgent(input: {
6363
return pickProfileAgent(personaInstances, isArchived) ?? directManagedAgent;
6464
}
6565

66+
/**
67+
* Split a persona's instances into live and archived buckets off the same
68+
* archive predicate the selector uses — one policy, no duplication. Fail-open
69+
* is inherited: while the archive snapshot loads `isArchived` returns `false`,
70+
* so every instance lands in `live` and nothing is labeled or hidden.
71+
*/
72+
export function bucketPersonaInstances(
73+
personaInstances: readonly ManagedAgent[],
74+
isArchived: (pubkey: string) => boolean,
75+
): { live: ManagedAgent[]; archived: ManagedAgent[] } {
76+
const live: ManagedAgent[] = [];
77+
const archived: ManagedAgent[] = [];
78+
for (const instance of personaInstances) {
79+
(isArchived(instance.pubkey) ? archived : live).push(instance);
80+
}
81+
return { live, archived };
82+
}
83+
6684
export function useCanonicalManagedAgentProfile(input: {
6785
currentPubkey: string | undefined;
6886
managedAgents: readonly ManagedAgent[] | undefined;
@@ -126,6 +144,16 @@ export function useCanonicalManagedAgentProfile(input: {
126144
pubkey,
127145
],
128146
);
147+
// Split the roster for the Instances list off the same predicate the selector
148+
// uses — see `bucketPersonaInstances` for the fail-open semantics.
149+
const instanceBuckets = React.useMemo(
150+
() => bucketPersonaInstances(personaInstances, isArchived),
151+
[isArchived, personaInstances],
152+
);
129153

130-
return { linkedPersonaId, managedAgent, personaInstances };
154+
return {
155+
instanceBuckets,
156+
linkedPersonaId,
157+
managedAgent,
158+
};
131159
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* Archived-instances subsection regression: the profile panel's Instances list
3+
* splits live and relay-archived siblings, rendering archived rows under a
4+
* clearly labeled "Archived" header so unarchive stays UI-reachable for
5+
* channel-less agents. The section appears whenever live OR archived instances
6+
* exist, self-omits when neither does, shows only the Archived subsection for
7+
* an all-archived persona, and archived rows keep the deliberate explicit-
8+
* pubkey click that feeds selector matrix rule 3.
9+
*
10+
* Mounts the shipping ProfileInstancesSection (owned by the Runtime tab) and
11+
* drives the real expand toggle rather than reimplementing it.
12+
*/
13+
14+
import assert from "node:assert/strict";
15+
import { after, afterEach, before, test } from "node:test";
16+
17+
import { JSDOM } from "jsdom";
18+
19+
const dom = new JSDOM("<!doctype html><html><body></body></html>", {
20+
url: "http://localhost",
21+
});
22+
23+
let cleanup;
24+
let fireEvent;
25+
let render;
26+
let screen;
27+
let createElement;
28+
let ProfileInstancesSection;
29+
30+
const LIVE_PK = "b".repeat(64);
31+
const SECOND_LIVE_PK = "c".repeat(64);
32+
const ARCHIVED_PK = "a".repeat(64);
33+
34+
function agent(overrides = {}) {
35+
return {
36+
pubkey: LIVE_PK,
37+
name: "Instance",
38+
personaId: "persona-1",
39+
status: "stopped",
40+
...overrides,
41+
};
42+
}
43+
44+
function baseProps(overrides = {}) {
45+
return {
46+
currentPubkey: null,
47+
instances: [],
48+
archivedInstances: [],
49+
onOpenInstance: () => {},
50+
...overrides,
51+
};
52+
}
53+
54+
function renderSection(props) {
55+
return render(createElement(ProfileInstancesSection, baseProps(props)));
56+
}
57+
58+
before(async () => {
59+
Object.assign(globalThis, {
60+
document: dom.window.document,
61+
HTMLElement: dom.window.HTMLElement,
62+
window: dom.window,
63+
IS_REACT_ACT_ENVIRONMENT: true,
64+
});
65+
Object.defineProperty(globalThis, "navigator", {
66+
configurable: true,
67+
value: dom.window.navigator,
68+
writable: true,
69+
});
70+
dom.window.matchMedia = () => ({
71+
matches: true,
72+
addEventListener() {},
73+
removeEventListener() {},
74+
});
75+
76+
({ cleanup, fireEvent, render, screen } = await import(
77+
"@testing-library/react"
78+
));
79+
({ createElement } = await import("react"));
80+
({ ProfileInstancesSection } = await import("./ProfileInstancesSection.tsx"));
81+
});
82+
83+
afterEach(() => cleanup?.());
84+
after(() => dom.window.close());
85+
86+
test("test_archived_and_live_instances_render_archived_subsection", () => {
87+
renderSection({
88+
instances: [agent({ pubkey: LIVE_PK, name: "Live" })],
89+
archivedInstances: [agent({ pubkey: ARCHIVED_PK, name: "Archived one" })],
90+
});
91+
fireEvent.click(screen.getByTestId("user-profile-instances"));
92+
93+
assert.equal(
94+
screen.getByTestId("user-profile-instances").textContent,
95+
"2 instances",
96+
);
97+
assert.ok(screen.getByTestId("user-profile-instances-archived-header"));
98+
assert.ok(screen.getByTestId(`user-profile-instance-${LIVE_PK}`));
99+
assert.ok(screen.getByTestId(`user-profile-instance-${ARCHIVED_PK}`));
100+
});
101+
102+
test("test_no_archived_instances_omits_archived_subsection", () => {
103+
renderSection({
104+
instances: [
105+
agent({ pubkey: LIVE_PK, name: "Live" }),
106+
agent({ pubkey: SECOND_LIVE_PK, name: "Live two" }),
107+
],
108+
archivedInstances: [],
109+
});
110+
fireEvent.click(screen.getByTestId("user-profile-instances"));
111+
112+
assert.equal(screen.queryByTestId("user-profile-instances-archived"), null);
113+
assert.ok(screen.getByTestId(`user-profile-instance-${LIVE_PK}`));
114+
});
115+
116+
test("test_all_archived_persona_shows_only_archived_subsection", () => {
117+
renderSection({
118+
instances: [],
119+
archivedInstances: [agent({ pubkey: ARCHIVED_PK, name: "Archived only" })],
120+
});
121+
// Section renders even with an empty live list.
122+
fireEvent.click(screen.getByTestId("user-profile-instances"));
123+
124+
assert.equal(
125+
screen.getByTestId("user-profile-instances").textContent,
126+
"1 instance",
127+
);
128+
assert.ok(screen.getByTestId("user-profile-instances-archived-header"));
129+
assert.ok(screen.getByTestId(`user-profile-instance-${ARCHIVED_PK}`));
130+
});
131+
132+
test("test_no_instances_at_all_omits_instances_section", () => {
133+
renderSection({ instances: [], archivedInstances: [] });
134+
assert.equal(screen.queryByTestId("user-profile-instances-section"), null);
135+
});
136+
137+
test("test_archived_row_click_opens_that_explicit_pubkey", () => {
138+
const opened = [];
139+
renderSection({
140+
instances: [],
141+
archivedInstances: [agent({ pubkey: ARCHIVED_PK, name: "Archived only" })],
142+
onOpenInstance: (pubkey) => opened.push(pubkey),
143+
});
144+
fireEvent.click(screen.getByTestId("user-profile-instances"));
145+
fireEvent.click(screen.getByTestId(`user-profile-instance-${ARCHIVED_PK}`));
146+
147+
// The archived row keeps the deliberate explicit-pubkey path (unarchive).
148+
assert.deepEqual(opened, [ARCHIVED_PK]);
149+
});
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import * as React from "react";
2+
import { ChevronRight } from "lucide-react";
3+
4+
import { ProfileSectionGroup } from "@/features/profile/ui/UserProfilePanelFields";
5+
import type { ManagedAgent } from "@/shared/api/types";
6+
import { cn } from "@/shared/lib/cn";
7+
8+
function ProfileInstanceRow({
9+
archived = false,
10+
currentPubkey,
11+
instance,
12+
onOpenInstance,
13+
}: {
14+
archived?: boolean;
15+
currentPubkey: string | null;
16+
instance: ManagedAgent;
17+
onOpenInstance: (pubkey: string) => void;
18+
}) {
19+
const isCurrent = instance.pubkey === currentPubkey;
20+
return (
21+
<button
22+
className="flex min-h-16 w-full items-center gap-3 px-4 py-3 text-left transition-colors hover:bg-muted/40 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
23+
data-testid={`user-profile-instance-${instance.pubkey}`}
24+
onClick={() => onOpenInstance(instance.pubkey)}
25+
type="button"
26+
>
27+
<span className="min-w-0 flex-1 truncate text-sm font-medium">
28+
{instance.name}
29+
</span>
30+
<span className="text-xs capitalize text-muted-foreground">
31+
{archived
32+
? "Archived"
33+
: isCurrent
34+
? "Current"
35+
: instance.status.replace("_", " ")}
36+
</span>
37+
<ChevronRight className="h-4 w-4 shrink-0 text-muted-foreground" />
38+
</button>
39+
);
40+
}
41+
42+
/**
43+
* The persona's managed-agent instances, split into live rows and a labeled
44+
* "Archived" subsection. Archived rows keep the explicit-pubkey click so
45+
* unarchive stays UI-reachable for channel-less agents — the deliberate-
46+
* navigation path (selector matrix rule 3) that lets a click land on the exact
47+
* archived identity. The count reflects both buckets; the section renders only
48+
* when at least one instance (live or archived) exists.
49+
*/
50+
export function ProfileInstancesSection({
51+
archivedInstances,
52+
currentPubkey,
53+
instances,
54+
onOpenInstance,
55+
}: {
56+
archivedInstances: ManagedAgent[];
57+
currentPubkey: string | null;
58+
instances: ManagedAgent[];
59+
onOpenInstance: (pubkey: string) => void;
60+
}) {
61+
const [expanded, setExpanded] = React.useState(false);
62+
const totalCount = instances.length + archivedInstances.length;
63+
if (totalCount === 0) return null;
64+
const instanceCountLabel = `${totalCount} instance${totalCount === 1 ? "" : "s"}`;
65+
66+
return (
67+
<ProfileSectionGroup
68+
testId="user-profile-instances-section"
69+
title="Instances"
70+
>
71+
<button
72+
aria-expanded={expanded}
73+
className="flex min-h-16 w-full items-center gap-3 px-4 py-3 text-left transition-colors hover:bg-muted/40 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
74+
data-testid="user-profile-instances"
75+
onClick={() => setExpanded((value) => !value)}
76+
type="button"
77+
>
78+
<span className="min-w-0 flex-1 text-sm font-medium">
79+
{instanceCountLabel}
80+
</span>
81+
<ChevronRight
82+
className={cn(
83+
"h-4 w-4 shrink-0 text-muted-foreground transition-transform",
84+
expanded && "rotate-90",
85+
)}
86+
/>
87+
</button>
88+
{expanded ? (
89+
<>
90+
{instances.map((instance) => (
91+
<ProfileInstanceRow
92+
currentPubkey={currentPubkey}
93+
instance={instance}
94+
key={instance.pubkey}
95+
onOpenInstance={onOpenInstance}
96+
/>
97+
))}
98+
{archivedInstances.length > 0 ? (
99+
<div data-testid="user-profile-instances-archived">
100+
<p
101+
className="px-4 pb-1 pt-3 text-xs font-semibold uppercase tracking-wide text-muted-foreground"
102+
data-testid="user-profile-instances-archived-header"
103+
>
104+
Archived
105+
</p>
106+
{archivedInstances.map((instance) => (
107+
<ProfileInstanceRow
108+
archived
109+
currentPubkey={currentPubkey}
110+
instance={instance}
111+
key={instance.pubkey}
112+
onOpenInstance={onOpenInstance}
113+
/>
114+
))}
115+
</div>
116+
) : null}
117+
</>
118+
) : null}
119+
</ProfileSectionGroup>
120+
);
121+
}

‎desktop/src/features/profile/ui/UserProfilePanel.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function UserProfilePanel({
189189
);
190190
const personasQuery = usePersonasQuery();
191191
const managedAgentsQuery = useManagedAgentsQuery({ enabled: true });
192-
const { linkedPersonaId, managedAgent, personaInstances } =
192+
const { instanceBuckets, linkedPersonaId, managedAgent } =
193193
useCanonicalManagedAgentProfile({
194194
currentPubkey,
195195
managedAgents: managedAgentsQuery.data,
@@ -812,7 +812,7 @@ export function UserProfilePanel({
812812
isFollowing={isFollowing}
813813
isOwner={viewerIsOwner}
814814
isSelf={isSelf}
815-
instances={personaInstances}
815+
instanceBuckets={instanceBuckets}
816816
activityAgent={activityAgent}
817817
managedAgent={managedAgent}
818818
agentInfoFields={agentInfoFields}

0 commit comments

Comments
 (0)