Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cowork): reconcile monitor rail so a matched watcher stops spinning

Akarshan Biswas2fd3726d19c0
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/build.rs b/src-tauri/plugins/tauri-plugin-agent-tools/build.rs
index b799ffa9f..022e23b94 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/build.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/build.rs
@@ -26,6 +26,7 @@ const COMMANDS: &[&str] = &[
"start_monitor",
"stop_monitor",
"list_monitors",
+ "session_monitor_ids",
"stop_session_monitors",
"cancel_thread_bash",
"preview_register_root",
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts b/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts
index daa45d57e..702387fbd 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts
@@ -492,6 +492,15 @@ export async function listMonitors(threadId: string): Promise<string> {
return await invoke('plugin:agent-tools|list_monitors', { threadId })
}
+/**
+ * The ids of a session's still-active monitors. Every monitor is one-shot, so a
+ * monitor no longer listed here has ended; the UI reconciles its rail against
+ * this so a row cannot stay 'running' after a missed terminal update.
+ */
+export async function sessionMonitorIds(threadId: string): Promise<string[]> {
+ return await invoke('plugin:agent-tools|session_monitor_ids', { threadId })
+}
+
/** Abort every monitor a session still has. Called at run end. */
export async function stopSessionMonitors(threadId: string): Promise<void> {
await invoke('plugin:agent-tools|stop_session_monitors', { threadId })
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/permissions/default.toml b/src-tauri/plugins/tauri-plugin-agent-tools/permissions/default.toml
index 203a7b8ea..c51171cc8 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/permissions/default.toml
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/permissions/default.toml
@@ -28,6 +28,7 @@ permissions = [
"allow-start-monitor",
"allow-stop-monitor",
"allow-list-monitors",
+ "allow-session-monitor-ids",
"allow-stop-session-monitors",
"allow-cancel-thread-bash",
"allow-preview-register-root",
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/permissions/schemas/schema.json b/src-tauri/plugins/tauri-plugin-agent-tools/permissions/schemas/schema.json
index 93be2cdc3..ca8fd4f87 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/permissions/schemas/schema.json
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/permissions/schemas/schema.json
@@ -450,6 +450,18 @@
"const": "deny-sandbox-status",
"markdownDescription": "Denies the sandbox_status command without any pre-configured scope."
},
+ {
+ "description": "Enables the session_monitor_ids command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-session-monitor-ids",
+ "markdownDescription": "Enables the session_monitor_ids command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the session_monitor_ids command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-session-monitor-ids",
+ "markdownDescription": "Denies the session_monitor_ids command without any pre-configured scope."
+ },
{
"description": "Enables the session_workspace_delete command without any pre-configured scope.",
"type": "string",
@@ -667,10 +679,10 @@
"markdownDescription": "Denies the workspace_path command without any pre-configured scope."
},
{
- "description": "Default permissions for the agent tools plugin: skill/memory management plus built-in tool execution\n#### This default permission set includes:\n\n- `allow-workspace-path`\n- `allow-thread-workspace-path`\n- `allow-thread-workspace-delete`\n- `allow-thread-workspace-sweep`\n- `allow-session-workspace-path`\n- `allow-session-workspace-delete`\n- `allow-session-workspace-sweep`\n- `allow-skill-list`\n- `allow-skill-read`\n- `allow-skill-write`\n- `allow-skill-delete`\n- `allow-skill-invoke`\n- `allow-memory-list`\n- `allow-memory-read`\n- `allow-memory-write`\n- `allow-memory-catalog`\n- `allow-memory-delete`\n- `allow-tool-schemas`\n- `allow-sandbox-status`\n- `allow-subagent-result-reserve`\n- `allow-subagent-result-fill`\n- `allow-attachment-import`\n- `allow-execute-tool`\n- `allow-execute-tool-streaming`\n- `allow-start-monitor`\n- `allow-stop-monitor`\n- `allow-list-monitors`\n- `allow-stop-session-monitors`\n- `allow-cancel-thread-bash`\n- `allow-preview-register-root`\n- `allow-preview-unregister-root`",
+ "description": "Default permissions for the agent tools plugin: skill/memory management plus built-in tool execution\n#### This default permission set includes:\n\n- `allow-workspace-path`\n- `allow-thread-workspace-path`\n- `allow-thread-workspace-delete`\n- `allow-thread-workspace-sweep`\n- `allow-session-workspace-path`\n- `allow-session-workspace-delete`\n- `allow-session-workspace-sweep`\n- `allow-skill-list`\n- `allow-skill-read`\n- `allow-skill-write`\n- `allow-skill-delete`\n- `allow-skill-invoke`\n- `allow-memory-list`\n- `allow-memory-read`\n- `allow-memory-write`\n- `allow-memory-catalog`\n- `allow-memory-delete`\n- `allow-tool-schemas`\n- `allow-sandbox-status`\n- `allow-subagent-result-reserve`\n- `allow-subagent-result-fill`\n- `allow-attachment-import`\n- `allow-execute-tool`\n- `allow-execute-tool-streaming`\n- `allow-start-monitor`\n- `allow-stop-monitor`\n- `allow-list-monitors`\n- `allow-session-monitor-ids`\n- `allow-stop-session-monitors`\n- `allow-cancel-thread-bash`\n- `allow-preview-register-root`\n- `allow-preview-unregister-root`",
"type": "string",
"const": "default",
- "markdownDescription": "Default permissions for the agent tools plugin: skill/memory management plus built-in tool execution\n#### This default permission set includes:\n\n- `allow-workspace-path`\n- `allow-thread-workspace-path`\n- `allow-thread-workspace-delete`\n- `allow-thread-workspace-sweep`\n- `allow-session-workspace-path`\n- `allow-session-workspace-delete`\n- `allow-session-workspace-sweep`\n- `allow-skill-list`\n- `allow-skill-read`\n- `allow-skill-write`\n- `allow-skill-delete`\n- `allow-skill-invoke`\n- `allow-memory-list`\n- `allow-memory-read`\n- `allow-memory-write`\n- `allow-memory-catalog`\n- `allow-memory-delete`\n- `allow-tool-schemas`\n- `allow-sandbox-status`\n- `allow-subagent-result-reserve`\n- `allow-subagent-result-fill`\n- `allow-attachment-import`\n- `allow-execute-tool`\n- `allow-execute-tool-streaming`\n- `allow-start-monitor`\n- `allow-stop-monitor`\n- `allow-list-monitors`\n- `allow-stop-session-monitors`\n- `allow-cancel-thread-bash`\n- `allow-preview-register-root`\n- `allow-preview-unregister-root`"
+ "markdownDescription": "Default permissions for the agent tools plugin: skill/memory management plus built-in tool execution\n#### This default permission set includes:\n\n- `allow-workspace-path`\n- `allow-thread-workspace-path`\n- `allow-thread-workspace-delete`\n- `allow-thread-workspace-sweep`\n- `allow-session-workspace-path`\n- `allow-session-workspace-delete`\n- `allow-session-workspace-sweep`\n- `allow-skill-list`\n- `allow-skill-read`\n- `allow-skill-write`\n- `allow-skill-delete`\n- `allow-skill-invoke`\n- `allow-memory-list`\n- `allow-memory-read`\n- `allow-memory-write`\n- `allow-memory-catalog`\n- `allow-memory-delete`\n- `allow-tool-schemas`\n- `allow-sandbox-status`\n- `allow-subagent-result-reserve`\n- `allow-subagent-result-fill`\n- `allow-attachment-import`\n- `allow-execute-tool`\n- `allow-execute-tool-streaming`\n- `allow-start-monitor`\n- `allow-stop-monitor`\n- `allow-list-monitors`\n- `allow-session-monitor-ids`\n- `allow-stop-session-monitors`\n- `allow-cancel-thread-bash`\n- `allow-preview-register-root`\n- `allow-preview-unregister-root`"
}
]
}
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs
index 4d74ca88b..b0de4b729 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs
@@ -610,6 +610,23 @@ pub async fn list_monitors(thread_id: String) -> Result<String, AgentToolsError>
})
}
+/// The ids of a session's still-active monitors, for the UI to reconcile its
+/// rail against. Every monitor is one-shot and retires (match, timeout, or an
+/// explicit stop) after emitting its terminal update, so a monitor Rust no
+/// longer lists has ended; a rail row still marked running for it is stale
+/// (a dropped terminal update) and the UI can settle it.
+#[tauri::command]
+pub async fn session_monitor_ids(thread_id: String) -> Result<Vec<String>, AgentToolsError> {
+ let set = session_monitors()
+ .lock()
+ .unwrap()
+ .get(&thread_id)
+ .map(|entry| entry.set.clone());
+ Ok(set
+ .map(|s| s.snapshot().into_iter().map(|m| m.monitor_id).collect())
+ .unwrap_or_default())
+}
+
/// Abort every monitor a session still has. Called at run end (and session
/// teardown), the Cowork counterpart of the run-scoped drop on the CLI.
#[tauri::command]
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs
index 8503f863e..933f9cd3c 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs
@@ -61,6 +61,7 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
commands::start_monitor,
commands::stop_monitor,
commands::list_monitors,
+ commands::session_monitor_ids,
commands::stop_session_monitors,
commands::cancel_thread_bash,
commands::preview_register_root,
diff --git a/web-app/src/containers/CoworkTasksPanel.tsx b/web-app/src/containers/CoworkTasksPanel.tsx
index 3e5863541..04b89f881 100644
--- a/web-app/src/containers/CoworkTasksPanel.tsx
+++ b/web-app/src/containers/CoworkTasksPanel.tsx
@@ -61,6 +61,7 @@ const MONITOR_OUTCOME_KEYS = {
matched: 'common:monitorMatched',
timeout: 'common:monitorTimedOut',
stopped: 'common:monitorStopped',
+ ended: 'common:monitorEnded',
} as const
/** One monitor: its name, how it ended (or that it is still polling), and the
diff --git a/web-app/src/hooks/__tests__/useCoworkRun.test.ts b/web-app/src/hooks/__tests__/useCoworkRun.test.ts
index 9a1ec6280..90508393e 100644
--- a/web-app/src/hooks/__tests__/useCoworkRun.test.ts
+++ b/web-app/src/hooks/__tests__/useCoworkRun.test.ts
@@ -193,6 +193,38 @@ describe('useCoworkRun - monitors and parking', () => {
})
})
+ it('reconcile settles a running monitor Rust no longer lists as active', () => {
+ const s = useCoworkRun.getState()
+ s.startMonitor('sid', view)
+ // Rust reports no active monitors: the one-shot watcher retired but its
+ // terminal update never flipped the rail.
+ s.reconcileMonitors('sid', [])
+ expect(useCoworkRun.getState().monitors.sid[0]).toMatchObject({
+ status: 'done',
+ outcome: 'ended',
+ })
+ })
+
+ it('reconcile leaves a still-active monitor running', () => {
+ const s = useCoworkRun.getState()
+ s.startMonitor('sid', view)
+ s.reconcileMonitors('sid', ['mon-1'])
+ expect(useCoworkRun.getState().monitors.sid[0]).toMatchObject({
+ status: 'running',
+ })
+ })
+
+ it('reconcile does not reopen or relabel an already-closed monitor', () => {
+ const s = useCoworkRun.getState()
+ s.startMonitor('sid', view)
+ s.updateMonitor('sid', update(true))
+ s.reconcileMonitors('sid', [])
+ expect(useCoworkRun.getState().monitors.sid[0]).toMatchObject({
+ status: 'done',
+ outcome: 'matched',
+ })
+ })
+
it('a new run keeps the session monitors and clears the parked flag', () => {
const s = useCoworkRun.getState()
s.startMonitor('sid', view)
diff --git a/web-app/src/hooks/useCoworkRun.ts b/web-app/src/hooks/useCoworkRun.ts
index 7f697f945..7be2d1641 100644
--- a/web-app/src/hooks/useCoworkRun.ts
+++ b/web-app/src/hooks/useCoworkRun.ts
@@ -202,6 +202,10 @@ type CoworkRunState = {
updateMonitor: (sid: string, update: MonitorUpdate) => void
/** An explicit `stop`: closed with the progress it had. */
stopMonitor: (sid: string, monitorId: string) => void
+ /** Settle any rail row still marked running that Rust no longer lists as
+ * active (its terminal update went undelivered), so a matched monitor cannot
+ * be left spinning. `activeIds` is Rust's authoritative active set. */
+ reconcileMonitors: (sid: string, activeIds: string[]) => void
/** Session teardown: the watchers are stopped in Rust alongside. */
clearMonitors: (sid: string) => void
setParked: (sid: string, parked: boolean) => void
@@ -402,6 +406,25 @@ export const useCoworkRun = create<CoworkRunState>()((set, get) => ({
},
})),
+ reconcileMonitors: (sid, activeIds) =>
+ set((s) => {
+ const rows = s.monitors[sid]
+ if (!rows?.length) return {}
+ const active = new Set(activeIds)
+ let changed = false
+ const next = rows.map((m) => {
+ if (m.status !== 'running' || active.has(m.monitorId)) return m
+ changed = true
+ return {
+ ...m,
+ status: 'done' as const,
+ outcome: 'ended' as const,
+ endedAt: Date.now(),
+ }
+ })
+ return changed ? { monitors: { ...s.monitors, [sid]: next } } : {}
+ }),
+
clearMonitors: (sid) => set((s) => ({ monitors: omitKey(s.monitors, sid) })),
setParked: (sid, parked) =>
diff --git a/web-app/src/lib/agentTools.ts b/web-app/src/lib/agentTools.ts
index 2343f21cb..94b5f2ff9 100644
--- a/web-app/src/lib/agentTools.ts
+++ b/web-app/src/lib/agentTools.ts
@@ -12,6 +12,7 @@ import {
startMonitor,
stopMonitor,
listMonitors,
+ sessionMonitorIds,
stopSessionMonitors,
cancelThreadBash,
type MemoryCatalogEntry,
@@ -358,6 +359,20 @@ export async function listAgentMonitors(sessionId: string): Promise<string> {
}
}
+/**
+ * The ids of a session's still-active monitors, for the rail to reconcile
+ * against. Best-effort: on failure the caller keeps its current rail rather
+ * than clearing them, so a transient error never wrongly settles a live watcher.
+ */
+export async function activeAgentMonitorIds(sessionId: string): Promise<string[] | null> {
+ try {
+ return await sessionMonitorIds(sessionId)
+ } catch (e) {
+ console.warn('[agentTools] Failed to list active monitors:', messageOf(e))
+ return null
+ }
+}
+
/**
* Abort every monitor a session still has. Best-effort teardown when the
* session is deleted: a failure leaves watchers whose updates go nowhere, not
diff --git a/web-app/src/locales/en/common.json b/web-app/src/locales/en/common.json
index d8d020d15..ef6c82a76 100644
--- a/web-app/src/locales/en/common.json
+++ b/web-app/src/locales/en/common.json
@@ -671,6 +671,7 @@
"monitorMatched": "Matched",
"monitorTimedOut": "Timed out",
"monitorStopped": "Stopped",
+ "monitorEnded": "Ended",
"coworkWatching_one": "Watching {{count}} monitor…",
"coworkWatching_other": "Watching {{count}} monitors…",
"coworkWaitingBackground": "Waiting on background tasks…",
diff --git a/web-app/src/routes/cowork.tsx b/web-app/src/routes/cowork.tsx
index 11f2f5969..945705a45 100644
--- a/web-app/src/routes/cowork.tsx
+++ b/web-app/src/routes/cowork.tsx
@@ -99,6 +99,7 @@ import {
importAttachment,
reserveSubagentResult,
cancelAgentThreadBash,
+ activeAgentMonitorIds,
} from '@/lib/agentTools'
import { monitorLaneFor, type MonitorLane } from '@/lib/coworkMonitor'
import { CoworkParkedNotice } from '@/containers/CoworkParkedNotice'
@@ -1339,6 +1340,23 @@ function CoworkPage() {
}
}, [session?.id, running])
+ // Reconcile the rail against Rust's authoritative active set when no run is
+ // up (mount, and each run end): a one-shot monitor that matched but whose
+ // terminal update never reached the rail would otherwise spin forever. A live
+ // run is left alone -- it delivers through the channel.
+ useEffect(() => {
+ const sid = session?.id
+ if (!sid || running) return
+ let cancelled = false
+ void activeAgentMonitorIds(sid).then((ids) => {
+ if (cancelled || ids === null) return
+ useCoworkRun.getState().reconcileMonitors(sid, ids)
+ })
+ return () => {
+ cancelled = true
+ }
+ }, [session?.id, running])
+
// Nothing to show once the session changes: both panels describe the session
// they were opened from.
useEffect(() => setRail(null), [session?.id])
diff --git a/web-app/src/types/coworkSession.ts b/web-app/src/types/coworkSession.ts
index 3d951f74c..cbdbb4386 100644
--- a/web-app/src/types/coworkSession.ts
+++ b/web-app/src/types/coworkSession.ts
@@ -96,8 +96,10 @@ export type MonitorView = {
name: string
script: string
status: 'running' | 'done'
- /** How a done monitor ended. */
- outcome?: 'matched' | 'timeout' | 'stopped'
+ /** How a done monitor ended. `ended` is a monitor the rail reconciled as
+ * retired in Rust after a terminal update went undelivered, so match vs
+ * timeout is no longer known. */
+ outcome?: 'matched' | 'timeout' | 'stopped' | 'ended'
startedAt: number
endedAt?: number
}