Skip to content

Commit b999903

Browse files
Fix reload-required spam; add auto-reload + resume (anomalyco#467)
* fix(app): scope and defer reload prompts Avoid reload-required spam by ignoring .opencode/openwork.json, scoping detection to the active workspace, and deferring prompts until sessions go idle. Add per-workspace auto-reload + optional resume so agent/skill/command updates apply without tearing down the UX. * chore(desktop): sync Cargo.lock Keep Cargo.lock consistent with the desktop crate version so CI can run cargo test --locked on Linux.
1 parent 947da80 commit b999903

18 files changed

Lines changed: 440 additions & 25 deletions

File tree

‎AGENTS.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@ Read INFRASTRUCTURE.md
1414

1515
* **Purpose-first UI**: prioritize clarity, safety, and approachability for non-technical users.
1616
* **Parity with OpenCode**: anything the UI can do must map cleanly to OpenCode tools.
17-
* **Prefer OpenCode primitives**: represent concepts using OpenCode’s native surfaces first (folders/projects, `.opencode`, `opencode.json`, skills, plugins) before introducing new abstractions.
17+
* **Prefer OpenCode primitives**: represent concepts using OpenCode's native surfaces first (folders/projects, `.opencode`, `opencode.json`, skills, plugins) before introducing new abstractions.
1818
* **Self-referential**: maintain a gitignored mirror of OpenCode at `vendor/opencode` for inspection.
1919
* **Self-building**: prefer prompts, skills, and composable primitives over bespoke logic.
2020
* **Open source**: keep the repo portable; no secrets committed.
2121
* **Slick and fluid**: 60fps animations, micro-interactions, premium feel.
2222
* **Mobile-native**: touch targets, gestures, and layouts optimized for small screens.
2323

24+
## Living Systems
25+
26+
OpenWork aims to be a **living system**: agents, skills, commands, and config are hot-reloadable while sessions are running. This enables agents to create new skills or update their own configuration and have changes take effect immediately, without tearing down active sessions.
27+
28+
Design principles for hot reload:
29+
30+
* **Conservative triggers**: only reload when a file that OpenCode reads at startup actually changes inside `.opencode/` or `opencode.json`. Ignore metadata files like `openwork.json`, `.DS_Store`, etc.
31+
* **Workspace-scoped**: reload state is keyed per workspace. Switching workspaces never leaks reload signals from one workspace to another.
32+
* **Session-aware**: when sessions are actively running, queue reload signals. Promote to visible reload (toast or auto-reload) only after all active sessions finish. This avoids interrupting in-flight tool calls.
33+
* **Auto-reload setting**: each workspace can opt into automatic reload via `.opencode/openwork.json` (`reload.auto`). When enabled, the engine reloads automatically once queued signals are ready and no sessions are active.
34+
* **Session continuity**: before reload, capture running session IDs, agents, and models. After reload, optionally relaunch those sessions so the user experiences seamless continuity.
35+
* **Per-workspace isolation**: the desktop file watcher only watches the active workspace root and its `.opencode/` directory. The server reload event store is already keyed by `workspaceId`.
36+
2437
## Technology Stack
2538

2639
| Layer | Technology |

‎packages/app/src/app/app.tsx‎

Lines changed: 195 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ export default function App() {
598598

599599
const sessionStore = createSessionStore({
600600
client,
601+
activeWorkspaceRoot: () => workspaceStore.activeWorkspaceRoot().trim(),
601602
selectedSessionId,
602603
setSelectedSessionId,
603604
sessionModelState: () => ({
@@ -1716,6 +1717,7 @@ export default function App() {
17161717
const [openworkReloadUnsupported, setOpenworkReloadUnsupported] = createSignal(false);
17171718

17181719
const resolveOpenworkReloadTarget = () => {
1720+
if (workspaceStore.activeWorkspaceDisplay().workspaceType !== "remote") return null;
17191721
if (openworkServerStatus() !== "connected") return null;
17201722
const client = openworkServerClient();
17211723
if (!client) return null;
@@ -1826,11 +1828,111 @@ export default function App() {
18261828
anyActiveRuns,
18271829
} = systemState;
18281830

1831+
const [pendingReloadReasons, setPendingReloadReasons] = createSignal<ReloadReason[]>([]);
1832+
const [pendingReloadTrigger, setPendingReloadTrigger] = createSignal<ReloadTrigger | null>(null);
1833+
const [pendingReloadResume, setPendingReloadResume] = createSignal<
1834+
Array<{ sessionID: string; model: ModelRef; agent: string | null }>
1835+
>([]);
1836+
const [autoReloadInFlight, setAutoReloadInFlight] = createSignal(false);
1837+
1838+
const workspaceAutoReloadAvailable = createMemo(() =>
1839+
isTauriRuntime() && workspaceStore.activeWorkspaceDisplay().workspaceType === "local",
1840+
);
1841+
1842+
const workspaceAutoReloadEnabled = createMemo(() => {
1843+
if (!workspaceAutoReloadAvailable()) return false;
1844+
const cfg = workspaceStore.workspaceConfig();
1845+
return Boolean(cfg?.reload?.auto);
1846+
});
1847+
1848+
const workspaceAutoReloadResumeEnabled = createMemo(() => {
1849+
if (!workspaceAutoReloadAvailable()) return false;
1850+
const cfg = workspaceStore.workspaceConfig();
1851+
return Boolean(cfg?.reload?.resume);
1852+
});
1853+
1854+
const setWorkspaceAutoReloadEnabled = async (next: boolean) => {
1855+
if (!workspaceAutoReloadAvailable()) return;
1856+
const cfg = workspaceStore.workspaceConfig();
1857+
const resume = Boolean(cfg?.reload?.resume);
1858+
await workspaceStore.persistReloadSettings({ auto: next, resume: next ? resume : false });
1859+
};
1860+
1861+
const setWorkspaceAutoReloadResumeEnabled = async (next: boolean) => {
1862+
if (!workspaceAutoReloadAvailable()) return;
1863+
const cfg = workspaceStore.workspaceConfig();
1864+
const auto = Boolean(cfg?.reload?.auto);
1865+
await workspaceStore.persistReloadSettings({ auto, resume: auto ? next : false });
1866+
};
1867+
1868+
const resumeSessionsAfterReload = async (entries: Array<{ sessionID: string; model: ModelRef; agent: string | null }>) => {
1869+
if (!entries.length) return;
1870+
const c = client();
1871+
if (!c) return;
1872+
const reasons = reloadReasons();
1873+
const label = reasons.length ? reasons.join(", ") : "config";
1874+
const text = `Hot reload applied (${label}). Continue where you left off.`;
1875+
for (const entry of entries) {
1876+
try {
1877+
await c.session.promptAsync({
1878+
sessionID: entry.sessionID,
1879+
model: entry.model,
1880+
agent: entry.agent ?? undefined,
1881+
variant: modelVariant() ?? undefined,
1882+
parts: [{ type: "text", text }],
1883+
});
1884+
} catch {
1885+
// ignore
1886+
}
1887+
}
1888+
};
1889+
1890+
const reloadWorkspaceEngineAndResume = async () => {
1891+
const snapshot = pendingReloadResume();
1892+
await reloadWorkspaceEngine();
1893+
1894+
// If reload failed, keep the snapshot for a later retry.
1895+
if (reloadRequired() || reloadError()) return;
1896+
1897+
if (workspaceAutoReloadResumeEnabled() && snapshot.length) {
1898+
await resumeSessionsAfterReload(snapshot);
1899+
}
1900+
setPendingReloadResume([]);
1901+
};
1902+
18291903
const markReloadRequired = (
18301904
reason: ReloadReason,
18311905
options?: { force?: boolean; trigger?: ReloadTrigger },
18321906
) => {
18331907
if (booting() || reloadBusy()) return;
1908+
1909+
if (!options?.force && anyActiveRuns()) {
1910+
setPendingReloadReasons((current) => (current.includes(reason) ? current : [...current, reason]));
1911+
if (options?.trigger) {
1912+
setPendingReloadTrigger(options.trigger);
1913+
}
1914+
1915+
const statuses = sessionStatusById();
1916+
const running = sessions().filter(
1917+
(s) => statuses[s.id] === "running" || statuses[s.id] === "retry",
1918+
);
1919+
if (running.length) {
1920+
setPendingReloadResume((current) => {
1921+
const existing = new Set(current.map((entry) => entry.sessionID));
1922+
const next = current.slice();
1923+
for (const s of running) {
1924+
if (!s?.id || existing.has(s.id)) continue;
1925+
const model = sessionModelOverrideById()[s.id] ?? sessionModelById()[s.id] ?? defaultModel();
1926+
const agent = sessionAgentById()[s.id] ?? null;
1927+
existing.add(s.id);
1928+
next.push({ sessionID: s.id, model, agent });
1929+
}
1930+
return next;
1931+
});
1932+
}
1933+
return;
1934+
}
1935+
18341936
if (!options?.force) {
18351937
const existingReasons = reloadReasons();
18361938
if (reloadRequired() && existingReasons.includes(reason)) return;
@@ -1856,10 +1958,63 @@ export default function App() {
18561958
markReloadRequiredRaw(reason, options?.trigger);
18571959
};
18581960

1961+
createEffect(() => {
1962+
if (anyActiveRuns()) return;
1963+
if (reloadBusy()) return;
1964+
if (booting()) return;
1965+
1966+
const pending = pendingReloadReasons();
1967+
if (!pending.length) return;
1968+
1969+
// Promote queued reload signals only after active sessions finish.
1970+
const trigger = pendingReloadTrigger();
1971+
for (let i = 0; i < pending.length; i += 1) {
1972+
markReloadRequiredRaw(pending[i], i === 0 ? trigger ?? undefined : undefined);
1973+
}
1974+
setPendingReloadReasons([]);
1975+
setPendingReloadTrigger(null);
1976+
});
1977+
1978+
createEffect(() => {
1979+
if (typeof window === "undefined") return;
1980+
if (!workspaceAutoReloadEnabled()) return;
1981+
if (anyActiveRuns()) return;
1982+
if (!reloadRequired()) return;
1983+
if (!canReloadEngine()) return;
1984+
if (reloadBusy() || autoReloadInFlight()) return;
1985+
1986+
const lastTriggeredAt = reloadLastTriggeredAt();
1987+
if (lastTriggeredAt && Date.now() - lastTriggeredAt < 600) return;
1988+
1989+
const timer = window.setTimeout(() => {
1990+
if (autoReloadInFlight()) return;
1991+
setAutoReloadInFlight(true);
1992+
void reloadWorkspaceEngineAndResume().finally(() => setAutoReloadInFlight(false));
1993+
}, 450);
1994+
1995+
onCleanup(() => {
1996+
window.clearTimeout(timer);
1997+
});
1998+
});
1999+
18592000
const openworkReloadKey = createMemo(
1860-
() => `${openworkServerWorkspaceId() ?? ""}|${openworkServerUrl().trim()}`,
2001+
() => `${workspaceStore.activeWorkspaceDisplay().workspaceType}|${openworkServerWorkspaceId() ?? ""}|${openworkServerUrl().trim()}`,
18612002
);
18622003

2004+
const reloadScopeKey = createMemo(() => {
2005+
const workspaceId = workspaceStore.activeWorkspaceId() ?? "";
2006+
return `${workspaceId}|${openworkReloadKey()}`;
2007+
});
2008+
2009+
createEffect(() => {
2010+
// Prevent reload toasts / queued reloads from bleeding across workspaces.
2011+
reloadScopeKey();
2012+
clearReloadRequired();
2013+
setPendingReloadReasons([]);
2014+
setPendingReloadTrigger(null);
2015+
setPendingReloadResume([]);
2016+
});
2017+
18632018
createEffect(() => {
18642019
openworkReloadKey();
18652020
setOpenworkReloadCursor(null);
@@ -1870,9 +2025,10 @@ export default function App() {
18702025
if (typeof window === "undefined") return;
18712026
if (!documentVisible()) return;
18722027
if (openworkReloadUnsupported()) return;
1873-
const client = openworkServerClient();
1874-
const workspaceId = openworkServerWorkspaceId();
1875-
if (!client || openworkServerStatus() !== "connected" || !workspaceId) return;
2028+
const target = resolveOpenworkReloadTarget();
2029+
if (!target || openworkServerStatus() !== "connected") return;
2030+
const client = target.client;
2031+
const workspaceId = target.workspaceId;
18762032

18772033
let active = true;
18782034
let busy = false;
@@ -1956,6 +2112,26 @@ export default function App() {
19562112
};
19572113
}
19582114

2115+
if (reason === "agents") {
2116+
const match = normalized.match(/\/\.opencode\/(?:agent|agents)\/([^/]+)/i);
2117+
return {
2118+
type: "agent",
2119+
name: match?.[1],
2120+
action: "updated",
2121+
path: rawPath,
2122+
};
2123+
}
2124+
2125+
if (reason === "commands") {
2126+
const match = normalized.match(/\/\.opencode\/(?:command|commands)\/([^/]+)/i);
2127+
return {
2128+
type: "command",
2129+
name: match?.[1]?.replace(/\.md$/i, ""),
2130+
action: "updated",
2131+
path: rawPath,
2132+
};
2133+
}
2134+
19592135
return {
19602136
type: "config",
19612137
name: fileName,
@@ -2020,7 +2196,9 @@ export default function App() {
20202196
rawReason === "plugins" ||
20212197
rawReason === "skills" ||
20222198
rawReason === "config" ||
2023-
rawReason === "mcp"
2199+
rawReason === "mcp" ||
2200+
rawReason === "agents" ||
2201+
rawReason === "commands"
20242202
? rawReason
20252203
: "config";
20262204

@@ -2048,7 +2226,7 @@ export default function App() {
20482226
const trigger =
20492227
extractReloadTriggerFromPath(reason, event.payload?.path) ??
20502228
{
2051-
type: reason === "plugins" ? "plugin" : reason === "skills" ? "skill" : reason,
2229+
type: reason === "plugins" ? "plugin" : reason === "skills" ? "skill" : reason === "agents" ? "agent" : reason === "commands" ? "command" : reason,
20522230
action: "updated",
20532231
};
20542232

@@ -2766,7 +2944,7 @@ export default function App() {
27662944
}
27672945
mcpEntryConfig["command"] = entry.command;
27682946
}
2769-
if (canUseOpenworkServer) {
2947+
if (canUseOpenworkServer && openworkClient && openworkWorkspaceId) {
27702948
await openworkClient.addMcp(openworkWorkspaceId, {
27712949
name: slug,
27722950
config: mcpEntryConfig,
@@ -3685,9 +3863,14 @@ export default function App() {
36853863
resetOpenworkServerSettings,
36863864
testOpenworkServerConnection,
36873865
canReloadWorkspace: canReloadWorkspace(),
3688-
reloadWorkspaceEngine,
3866+
reloadWorkspaceEngine: reloadWorkspaceEngineAndResume,
36893867
reloadBusy: reloadBusy(),
36903868
reloadError: reloadError(),
3869+
workspaceAutoReloadAvailable: workspaceAutoReloadAvailable(),
3870+
workspaceAutoReloadEnabled: workspaceAutoReloadEnabled(),
3871+
setWorkspaceAutoReloadEnabled,
3872+
workspaceAutoReloadResumeEnabled: workspaceAutoReloadResumeEnabled(),
3873+
setWorkspaceAutoReloadResumeEnabled,
36913874
activeWorkspaceDisplay: activeWorkspaceDisplay(),
36923875
workspaces: workspaceStore.workspaces(),
36933876
activeWorkspaceId: workspaceStore.activeWorkspaceId(),
@@ -3806,7 +3989,7 @@ export default function App() {
38063989
refreshMcpServers,
38073990
showMcpReloadBanner: reloadRequired() && reloadReasons().includes("mcp"),
38083991
mcpReloadBlocked: anyActiveRuns(),
3809-
reloadMcpEngine: () => reloadWorkspaceEngine(),
3992+
reloadMcpEngine: () => reloadWorkspaceEngineAndResume(),
38103993
language: currentLocale(),
38113994
setLanguage: setLocale,
38123995
};
@@ -3836,6 +4019,7 @@ export default function App() {
38364019
const sessionProps = () => ({
38374020
selectedSessionId: activeSessionId(),
38384021
setView,
4022+
tab: tab(),
38394023
setTab,
38404024
setSettingsTab,
38414025
activeWorkspaceDisplay: activeWorkspaceDisplay(),
@@ -4104,7 +4288,7 @@ export default function App() {
41044288
setMcpAuthEntry(null);
41054289
await refreshMcpServers();
41064290
}}
4107-
onReloadEngine={() => reloadWorkspaceEngine()}
4291+
onReloadEngine={() => reloadWorkspaceEngineAndResume()}
41084292
/>
41094293

41104294
<ReloadWorkspaceToast
@@ -4120,7 +4304,7 @@ export default function App() {
41204304
busy={reloadBusy()}
41214305
canReload={canReloadEngine()}
41224306
hasActiveRuns={anyActiveRuns()}
4123-
onReload={() => reloadWorkspaceEngine()}
4307+
onReload={() => reloadWorkspaceEngineAndResume()}
41244308
onDismiss={() => setReloadToastDismissedAt(Date.now())}
41254309
/>
41264310

‎packages/app/src/app/components/reload-workspace-toast.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ export default function ReloadWorkspaceToast(props: ReloadWorkspaceToastProps) {
5959
: "Config changed. Reload to apply.";
6060
}
6161

62+
if (type === "agent") {
63+
return trimmedName
64+
? `Agent '${trimmedName}' ${verb}. Reload to use it.`
65+
: "Agents changed. Reload to apply.";
66+
}
67+
68+
if (type === "command") {
69+
return trimmedName
70+
? `Command '${trimmedName}' ${verb}. Reload to use it.`
71+
: "Commands changed. Reload to apply.";
72+
}
73+
6274
return "Config changed. Reload to apply.";
6375
};
6476

‎packages/app/src/app/components/session/composer.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ export default function Composer(props: ComposerProps) {
277277
const mentionGroups = createMemo<MentionGroup[]>(() => {
278278
if (!mentionOpen()) return [];
279279
const query = mentionQuery().trim().toLowerCase();
280-
const agents = agentOptions().map((agent: Agent) => ({
280+
const agents: MentionOption[] = agentOptions().map((agent: Agent) => ({
281281
id: `agent:${agent.name}`,
282282
kind: "agent" as const,
283283
label: agent.name,
284284
value: agent.name,
285285
display: agent.name,
286286
}));
287287
const seen = new Set<string>();
288-
const recentFiles = props.recentFiles
288+
const recentFiles: MentionOption[] = props.recentFiles
289289
.filter((file: string) => {
290290
if (!file) return false;
291291
if (seen.has(file)) return false;
@@ -300,7 +300,7 @@ export default function Composer(props: ComposerProps) {
300300
display: file,
301301
recent: true,
302302
}));
303-
const searchFiles = searchResults()
303+
const searchFiles: MentionOption[] = searchResults()
304304
.filter((file: string) => file && !seen.has(file))
305305
.map((file: string) => ({
306306
id: `file:${file}`,

0 commit comments

Comments
 (0)