Skip to content

Commit ec0e450

Browse files
wesbillmanBrain
andauthored
fix(managed-agents): stale harness pin shadows runtime edits; add Restart quick action (#2252)
Signed-off-by: Wes <[email protected]> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
1 parent 083f6d6 commit ec0e450

5 files changed

Lines changed: 154 additions & 46 deletions

File tree

‎desktop/src-tauri/src/managed_agents/persona_events.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,25 @@ pub fn apply_persona_snapshot(record: &mut ManagedAgentRecord, persona: &AgentDe
478478
record.model = snapshot.model;
479479
record.provider = snapshot.provider;
480480
record.runtime = snapshot.runtime;
481+
// Drop a stale create-time harness pin when the definition names a
482+
// different known runtime; custom commands stay pinned.
483+
if let Some(def_runtime) = persona
484+
.runtime
485+
.as_deref()
486+
.map(str::trim)
487+
.filter(|r| !r.is_empty())
488+
.and_then(crate::managed_agents::known_acp_runtime_exact)
489+
{
490+
if let Some(pin_runtime) = record
491+
.agent_command_override
492+
.as_deref()
493+
.and_then(crate::managed_agents::known_acp_runtime)
494+
{
495+
if !std::ptr::eq(pin_runtime, def_runtime) {
496+
record.agent_command_override = None;
497+
}
498+
}
499+
}
481500
// env_vars stay overrides-only. Self-heal records written before the env
482501
// refresh: persona env used to be baked into `record.env_vars`, turning
483502
// inherited values into pseudo-overrides that shadow later persona edits.

‎desktop/src-tauri/src/managed_agents/spawn_hash/tests.rs‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,21 +366,39 @@ fn definition_runtime_edit_changes_hash_for_materialized_record() {
366366
);
367367
}
368368

369-
/// (c) An explicit agent_command_override (ladder step 1) must beat a
370-
/// changed definition runtime — the badge must NOT fire for a pinned instance.
369+
/// (c) A pin naming a KNOWN runtime no longer beats a changed definition
370+
/// runtime — apply_persona_snapshot clears the stale pin, so the badge fires.
371371
#[test]
372-
fn agent_command_override_beats_definition_runtime_change() {
372+
fn known_runtime_pin_yields_to_definition_runtime_change() {
373373
let mut rec = record();
374374
rec.persona_id = Some("pers".into());
375375
rec.runtime = Some("goose".into()); // materialized runtime
376-
rec.agent_command_override = Some("goose".into()); // explicit per-instance pin
376+
rec.agent_command_override = Some("goose".into()); // create-time pin
377+
378+
let before = [persona("pers", Some("goose"), "prompt")];
379+
let after = [persona("pers", Some("claude"), "prompt")];
380+
assert_ne!(
381+
spawn_config_hash(&rec, &before, &[], "wss://ws.example", &Default::default()),
382+
spawn_config_hash(&rec, &after, &[], "wss://ws.example", &Default::default()),
383+
"stale known-runtime pin must not shadow a definition runtime edit"
384+
);
385+
}
386+
387+
/// (c2) A custom-command override (no matching known runtime) still beats a
388+
/// changed definition runtime — the badge must NOT fire for such a pin.
389+
#[test]
390+
fn custom_command_override_beats_definition_runtime_change() {
391+
let mut rec = record();
392+
rec.persona_id = Some("pers".into());
393+
rec.runtime = Some("goose".into()); // materialized runtime
394+
rec.agent_command_override = Some("/opt/custom/my-agent".into());
377395

378396
let before = [persona("pers", Some("goose"), "prompt")];
379397
let after = [persona("pers", Some("claude"), "prompt")];
380398
assert_eq!(
381399
spawn_config_hash(&rec, &before, &[], "wss://ws.example", &Default::default()),
382400
spawn_config_hash(&rec, &after, &[], "wss://ws.example", &Default::default()),
383-
"explicit override must win regardless of definition runtime change"
401+
"custom command override must win regardless of definition runtime change"
384402
);
385403
}
386404

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

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,9 @@ import {
3232
buildInstanceInputForDefinition,
3333
resolveStartRuntimeForDefinition,
3434
} from "@/features/agents/lib/instanceInputForDefinition";
35-
import {
36-
isManagedAgentActive,
37-
startManagedAgentWithRules,
38-
stopManagedAgentWithRules,
39-
} from "@/features/agents/lib/managedAgentControlActions";
4035
import { describeLogFile } from "@/features/agents/ui/agentUi";
4136
import { AgentDialog } from "@/features/agents/ui/AgentDialog";
37+
import { useAgentLifecycleActions } from "@/features/profile/ui/useAgentLifecycleActions";
4238
import {
4339
consumePendingOpenEditAgent,
4440
type EditAgentFocusTarget,
@@ -451,42 +447,14 @@ export function UserProfilePanel({
451447
],
452448
);
453449

454-
const handleAgentPrimaryAction = React.useCallback(async () => {
455-
if (!managedAgent) return;
456-
457-
try {
458-
if (isManagedAgentActive(managedAgent)) {
459-
const result = await stopManagedAgentWithRules({
460-
agent: managedAgent,
461-
channels: channelsQuery.data ?? [],
462-
relayAgents: relayAgentsQuery.data ?? [],
463-
stopManagedAgent: stopAgentMutation.mutateAsync,
464-
});
465-
toast.success(result.noticeMessage ?? `Stopped ${managedAgent.name}.`);
466-
return;
467-
}
468-
469-
await startManagedAgentWithRules({
470-
agent: managedAgent,
471-
startManagedAgent: startAgentMutation.mutateAsync,
472-
});
473-
toast.success(
474-
managedAgent.backend.type === "provider"
475-
? `Deploying ${managedAgent.name}.`
476-
: `Started ${managedAgent.name}.`,
477-
);
478-
} catch (error) {
479-
toast.error(
480-
error instanceof Error ? error.message : "Agent action failed.",
481-
);
482-
}
483-
}, [
484-
channelsQuery.data,
485-
managedAgent,
486-
relayAgentsQuery.data,
487-
startAgentMutation.mutateAsync,
488-
stopAgentMutation.mutateAsync,
489-
]);
450+
const { handleAgentPrimaryAction, handleAgentRestart } =
451+
useAgentLifecycleActions({
452+
channels: channelsQuery.data,
453+
managedAgent,
454+
relayAgents: relayAgentsQuery.data,
455+
startManagedAgent: startAgentMutation.mutateAsync,
456+
stopManagedAgent: stopAgentMutation.mutateAsync,
457+
});
490458

491459
const handleInstantiateAgent = React.useCallback(async () => {
492460
if (!resolvedPersona) return;
@@ -828,6 +796,7 @@ export function UserProfilePanel({
828796
followMutation={followMutation}
829797
agentInstruction={agentInstruction}
830798
handleAgentPrimaryAction={handleAgentPrimaryAction}
799+
handleAgentRestart={handleAgentRestart}
831800
handleEditAgent={handleEditAgent}
832801
handleEditPersona={canEditPersona ? handleEditPersona : undefined}
833802
handleInstantiateAgent={handleInstantiateAgent}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
MessageSquare,
99
Pencil,
1010
Play,
11+
RefreshCw,
1112
Square,
1213
UserMinus,
1314
UserPlus,
@@ -77,6 +78,7 @@ export type ProfileSummaryViewProps = {
7778
canInstantiateAgent: boolean;
7879
agentInstruction: string | null;
7980
handleAgentPrimaryAction: () => void;
81+
handleAgentRestart: () => void;
8082
handleEditAgent: () => void;
8183
handleEditPersona?: () => void;
8284
handleInstantiateAgent: () => void;
@@ -189,6 +191,7 @@ export function ProfileSummaryView({
189191
canInstantiateAgent,
190192
agentInstruction,
191193
handleAgentPrimaryAction,
194+
handleAgentRestart,
192195
handleEditAgent,
193196
handleEditPersona,
194197
handleInstantiateAgent,
@@ -369,6 +372,14 @@ export function ProfileSummaryView({
369372
? handleAgentPrimaryAction
370373
: undefined
371374
}
375+
onAgentRestart={
376+
isOwner === true &&
377+
managedAgent?.backend.type === "local" &&
378+
(managedAgent.status === "running" ||
379+
managedAgent.status === "deployed")
380+
? handleAgentRestart
381+
: undefined
382+
}
372383
isFollowing={isFollowing}
373384
messagePending={isMessagePending}
374385
onMessage={onOpenDm ? handleMessage : undefined}
@@ -629,6 +640,7 @@ function ProfilePrimaryActions({
629640
isFollowing,
630641
messagePending,
631642
onAgentPrimaryAction,
643+
onAgentRestart,
632644
onEditAgent,
633645
onMessage,
634646
pubkey,
@@ -642,6 +654,7 @@ function ProfilePrimaryActions({
642654
isFollowing: boolean;
643655
messagePending?: boolean;
644656
onAgentPrimaryAction?: () => void;
657+
onAgentRestart?: () => void;
645658
onEditAgent: () => void;
646659
onMessage?: () => void;
647660
pubkey: string;
@@ -698,6 +711,15 @@ function ProfilePrimaryActions({
698711
testId="user-profile-agent-primary-action"
699712
/>
700713
) : null}
714+
{onAgentRestart ? (
715+
<ProfileQuickAction
716+
disabled={agentActionDisabled}
717+
icon={RefreshCw}
718+
label="Restart"
719+
onClick={onAgentRestart}
720+
testId="user-profile-agent-restart"
721+
/>
722+
) : null}
701723
</div>
702724
);
703725
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import * as React from "react";
2+
import { toast } from "sonner";
3+
4+
import {
5+
isManagedAgentActive,
6+
respawnManagedAgentWithRules,
7+
startManagedAgentWithRules,
8+
stopManagedAgentWithRules,
9+
} from "@/features/agents/lib/managedAgentControlActions";
10+
import type { Channel, ManagedAgent, RelayAgent } from "@/shared/api/types";
11+
12+
export function useAgentLifecycleActions({
13+
channels,
14+
managedAgent,
15+
relayAgents,
16+
startManagedAgent,
17+
stopManagedAgent,
18+
}: {
19+
channels: readonly Channel[] | undefined;
20+
managedAgent: ManagedAgent | undefined;
21+
relayAgents: readonly RelayAgent[] | undefined;
22+
startManagedAgent: (pubkey: string) => Promise<unknown>;
23+
stopManagedAgent: (pubkey: string) => Promise<unknown>;
24+
}) {
25+
const handleAgentPrimaryAction = React.useCallback(async () => {
26+
if (!managedAgent) return;
27+
28+
try {
29+
if (isManagedAgentActive(managedAgent)) {
30+
const result = await stopManagedAgentWithRules({
31+
agent: managedAgent,
32+
channels: channels ?? [],
33+
relayAgents: relayAgents ?? [],
34+
stopManagedAgent,
35+
});
36+
toast.success(result.noticeMessage ?? `Stopped ${managedAgent.name}.`);
37+
return;
38+
}
39+
40+
await startManagedAgentWithRules({
41+
agent: managedAgent,
42+
startManagedAgent,
43+
});
44+
toast.success(
45+
managedAgent.backend.type === "provider"
46+
? `Deploying ${managedAgent.name}.`
47+
: `Started ${managedAgent.name}.`,
48+
);
49+
} catch (error) {
50+
toast.error(
51+
error instanceof Error ? error.message : "Agent action failed.",
52+
);
53+
}
54+
}, [
55+
channels,
56+
managedAgent,
57+
relayAgents,
58+
startManagedAgent,
59+
stopManagedAgent,
60+
]);
61+
62+
const handleAgentRestart = React.useCallback(async () => {
63+
if (!managedAgent) return;
64+
65+
try {
66+
await respawnManagedAgentWithRules({
67+
agent: managedAgent,
68+
startManagedAgent,
69+
stopManagedAgent,
70+
});
71+
toast.success(`Restarted ${managedAgent.name}.`);
72+
} catch (error) {
73+
toast.error(
74+
error instanceof Error ? error.message : "Agent restart failed.",
75+
);
76+
}
77+
}, [managedAgent, startManagedAgent, stopManagedAgent]);
78+
79+
return { handleAgentPrimaryAction, handleAgentRestart };
80+
}

0 commit comments

Comments
 (0)