diff --git a/scripts/cockpit-daemon.sh b/scripts/cockpit-daemon.sh index 942721e41..00e91c306 100755 --- a/scripts/cockpit-daemon.sh +++ b/scripts/cockpit-daemon.sh @@ -780,6 +780,29 @@ _build_state_json() { continue fi + # Resolve the canonical project key to the live Zellij tab. Many sessions + # run as "Project Claude" / "Project Codex" while the registered project key + # remains "Project". Runtime state is stored under the canonical key, but + # sentinels, current prompt files, and session handoffs use the live tab. + local live_tab="$tab" exact_tab="" suffix_tab="" + local tab_lower="${tab,,}" + while IFS= read -r open_tab; do + [ -z "$open_tab" ] && continue + local open_lower="${open_tab,,}" + if [ "$open_lower" = "$tab_lower" ]; then + exact_tab="$open_tab" + break + fi + if [ -z "$suffix_tab" ] && { [[ "$open_lower" == "$tab_lower "* ]] || [[ "$open_lower" == "$tab_lower-"* ]]; }; then + suffix_tab="$open_tab" + fi + done <<< "$all_open_tabs" + if [ -n "$exact_tab" ]; then + live_tab="$exact_tab" + elif [ -n "$suffix_tab" ]; then + live_tab="$suffix_tab" + fi + # Determine agent running + active agent names for this dir local running="false" agents_json="[]" while IFS= read -r line; do @@ -796,20 +819,20 @@ _build_state_json() { # Check if this project's Zellij tab is actually open (case-insensitive exact line) local tab_open="false" - if echo "$all_open_tabs" | grep -qixF "$tab"; then + if echo "$all_open_tabs" | grep -qixF "$live_tab"; then tab_open="true" fi # Sentinel timestamps local ready_at closing_at closed_at lock_at - ready_at=$(_sentinel "/tmp/agent-ready-${tab}") - closing_at=$(_sentinel "/tmp/agent-closing-${tab}") - closed_at=$(_sentinel "/tmp/agent-closed-${tab}") - lock_at=$(_sentinel "/tmp/agent-stop-active-${tab}") + ready_at=$(_sentinel "/tmp/agent-ready-${live_tab}") + closing_at=$(_sentinel "/tmp/agent-closing-${live_tab}") + closed_at=$(_sentinel "/tmp/agent-closed-${live_tab}") + lock_at=$(_sentinel "/tmp/agent-stop-active-${live_tab}") # Current prompt JSON local cpk="" cpl="" cpsat="null" - local pf="/tmp/agent-current-prompt-${tab}" + local pf="/tmp/agent-current-prompt-${live_tab}" if [ -f "$pf" ]; then local pj pj=$(cat "$pf" 2>/dev/null) @@ -850,22 +873,15 @@ _build_state_json() { fi fi fi - # A stop/notification hook is authoritative evidence of the configured - # agent in this open tab even when the CLI launches through a wrapper whose - # process basename is not recognized by /proc scanning. - if [ "$tab_open" = "true" ] && [ "$agents_json" = "[]" ] && [ "$ready_at" != "null" ]; then - local ready_age - ready_age=$(( $(date +%s) - ready_at )) - if [ "$ready_age" -ge 0 ] && [ "$ready_age" -lt 900 ]; then - agents_json=$(jq -nc --arg a "$adapter" '[$a]') - running="true" - fi - fi + # A ready sentinel is lifecycle evidence, not process evidence. Do not + # manufacture activeAgents/running from it; otherwise the cloud UI can show + # "Codex ready" because Codex is configured even when Claude is the live + # process in the tab. local sf if type _session_file >/dev/null 2>&1; then - sf="$(_session_file "$tab" "$adapter")" + sf="$(_session_file "$live_tab" "$adapter")" else - sf="$HOME/.claude/sessions/${tab}.md" + sf="$HOME/.claude/sessions/${live_tab}.md" fi local sess_status="" sess_done="" sess_next="" sess_tests="" sess_todos="" sess_health="" sess_mtime="null" if [ -f "$sf" ]; then diff --git a/scripts/test/control-presenter.ts b/scripts/test/control-presenter.ts index 4132847df..84668d272 100644 --- a/scripts/test/control-presenter.ts +++ b/scripts/test/control-presenter.ts @@ -9,6 +9,7 @@ import { findProjectForOpenTab, formatAgentRuntimeLabel, getProjectDisplayState, + isProjectTabOpen, isCurrentPromptStale, } from "@/components/control/control-presenter"; import type { ProjectState } from "@/lib/control-types"; @@ -62,6 +63,12 @@ function runTests(): void { assert(findProjectForOpenTab("Cockpit Claude", projects)?.tab === "Cockpit", "expected prefix match"); }); + check("isProjectTabOpen accepts agent-suffixed live tabs", () => { + const project = stubProject({ tab: "Cockpit", liveTab: "Cockpit" }); + assert(isProjectTabOpen(project, ["Cockpit Claude"]), "expected suffix tab to count as open"); + assert(!isProjectTabOpen(project, ["Cockpit2 Claude"]), "must not match unrelated prefixes"); + }); + check("buildLiveTabRows sorts Working before Open", () => { const nowS = 1_700_000_000; const projects = [ @@ -132,9 +139,24 @@ function runTests(): void { assert(snapshot.evidenceKind === "historical", "handoff provenance must be historical"); }); - check("open session waiting for input has one explicit user-facing label", () => { - const state = getProjectDisplayState(stubProject({ tab: "Cockpit", agentRunning: true }), ["Cockpit"], 1_700_000_000); - assert(state.stateLabel === "Waiting for you", "open inactive agent must explain the required action"); + check("open session is not mislabeled as waiting for input", () => { + const nowS = 1_700_000_000; + const project = stubProject({ tab: "Cockpit", agentRunning: true }); + const state = getProjectDisplayState(project, ["Cockpit"], nowS); + const snapshot = buildProjectOperationsSnapshot(project, ["Cockpit"], nowS); + assert(state.stateLabel === "Agent shell open", "open inactive agent must describe the observed shell"); + assert(snapshot.phase === "open_idle", "open inactive agent must not count as waiting for input"); + assert(snapshot.evidenceLabel === "Agent shell open; no active task detected", "evidence should explain the live signal"); + }); + + check("ready sentinel is a next-step state, not generic waiting", () => { + const nowS = 1_700_000_000; + const project = stubProject({ tab: "Cockpit", readyAt: nowS - 5 }); + const state = getProjectDisplayState(project, ["Cockpit"], nowS); + const snapshot = buildProjectOperationsSnapshot(project, ["Cockpit"], nowS); + assert(state.stateLabel === "Ready for next step", "ready signal must name the action state"); + assert(snapshot.phase === "waiting_for_user", "ready signal remains actionable"); + assert(snapshot.evidenceLabel === "Agent signaled ready on connected computer", "ready evidence should identify the signal"); }); check("operations list prioritizes projects waiting for user action", () => { diff --git a/src/components/control/ProjectCard.tsx b/src/components/control/ProjectCard.tsx index 18b8548d4..1a3059c5d 100644 --- a/src/components/control/ProjectCard.tsx +++ b/src/components/control/ProjectCard.tsx @@ -7,7 +7,7 @@ import { postJson, patchJson } from "@/lib/api/fetch"; import type { ProjectState } from "@/lib/control-types"; import type { PromptMeta } from "@/lib/agent-config"; import type { OrchestrationTaskIntentId } from "@/lib/orchestration"; -import { getProjectDisplayState, type ProjectOperationsSnapshot } from "./control-presenter"; +import { getProjectDisplayState, isProjectTabOpen, type ProjectOperationsSnapshot } from "./control-presenter"; import { ProjectProfile } from "./ProjectProfile"; import { LatestOrchestrationPanel } from "./project-card-helpers"; import { ProjectCardHeader, SessionSummary } from "./project-card-sections"; @@ -72,7 +72,7 @@ export function ProjectCard({ patchJson(`/api/user-projects/${project.id}`, { agentPref: agentId ?? undefined }).catch(() => {}); } const workspaceTab = project.liveTab ?? project.tab; - const tabIsOpen = zellijTabs.some((t) => t.toLowerCase() === workspaceTab.toLowerCase()); + const tabIsOpen = isProjectTabOpen(project, zellijTabs); if (agentId && agentId !== currentAgent && tabIsOpen && project.dir) { setSwitchingAgent(true); try { diff --git a/src/components/control/ProjectOperationsView.tsx b/src/components/control/ProjectOperationsView.tsx index 5963068f0..41dd5218a 100644 --- a/src/components/control/ProjectOperationsView.tsx +++ b/src/components/control/ProjectOperationsView.tsx @@ -59,6 +59,7 @@ export function ProjectOperationsView({ const selected = snapshots.find((snapshot) => snapshot.project.tab === selectedTab) ?? snapshots[0]; const readyCount = snapshots.filter((snapshot) => snapshot.phase === "waiting_for_user").length; const workingCount = snapshots.filter((snapshot) => snapshot.phase === "working").length; + const openIdleCount = snapshots.filter((snapshot) => snapshot.phase === "open_idle").length; return (
@@ -66,7 +67,7 @@ export function ProjectOperationsView({

Projects

- {workingCount} working · {readyCount} waiting for you + {workingCount} working · {readyCount} ready · {openIdleCount} open

@@ -76,6 +77,8 @@ export function ProjectOperationsView({ ? "bg-accent-primary animate-pulse" : snapshot.phase === "waiting_for_user" || snapshot.phase === "completed" ? "bg-status-positive" + : snapshot.phase === "open_idle" + ? "bg-border-strong" : snapshot.phase === "offline" ? "bg-status-warning" : "bg-border-default"; diff --git a/src/components/control/ProjectStatusChips.tsx b/src/components/control/ProjectStatusChips.tsx index cce9a943d..66b972813 100644 --- a/src/components/control/ProjectStatusChips.tsx +++ b/src/components/control/ProjectStatusChips.tsx @@ -63,6 +63,13 @@ export function ProjectStatusChips({ const [commitState, setCommitState] = useState<"idle" | "committing" | "done" | "error">("idle"); const [commitResult, setCommitResult] = useState<{ sha?: string; error?: string } | null>(null); const runtimeLabel = runtimeStateKnown ? formatAgentRuntimeLabel(project) : ""; + const runtimeStateLabel = working + ? `${runtimeLabel} working` + : project.readyAt + ? `${runtimeLabel} ready` + : project.agentRunning + ? `${runtimeLabel} open` + : `${runtimeLabel} detected`; const git = project.git; const workspaceTab = project.liveTab ?? project.tab; const changesLabel = pendingChangesLabel(project); @@ -133,7 +140,7 @@ export function ProjectStatusChips({ > {switchingAgent ? <>Switching… - : <>{working ? `${runtimeLabel} working` : `${runtimeLabel} ready`} + : <>{runtimeStateLabel} } @@ -149,9 +156,9 @@ export function ProjectStatusChips({ ) : ( - {working ? `${runtimeLabel} working` : `${runtimeLabel} ready`} + {runtimeStateLabel} ) )} diff --git a/src/components/control/control-presenter.ts b/src/components/control/control-presenter.ts index cb6c43712..cd0fdb3d3 100644 --- a/src/components/control/control-presenter.ts +++ b/src/components/control/control-presenter.ts @@ -67,7 +67,7 @@ export type ProjectDisplayState = { | "closed" | "idle"; /** Human-readable label for the state badge — single source of truth */ - stateLabel: "Offline" | "Working" | "Waiting for you" | "Closing" | "Completed" | "Not running"; + stateLabel: "Offline" | "Working" | "Ready for next step" | "Agent shell open" | "Closing" | "Completed" | "Not running"; /** Tailwind classes for the ui-tag badge */ stateTagClass: string; }; @@ -77,6 +77,7 @@ export type ControlPhase = | "not_running" | "working" | "waiting_for_user" + | "open_idle" | "closing" | "completed"; @@ -123,7 +124,8 @@ type LiveTabRankLabel = LiveTabRow["stateLabel"]; const LIVE_TAB_RANK: Record = { Offline: 0, Working: 0, - "Waiting for you": 1, + "Ready for next step": 1, + "Agent shell open": 3, Closing: 2, Completed: 3, "Not running": 4, @@ -146,6 +148,14 @@ export function findProjectForOpenTab(openTab: string, projects: ProjectState[]) return prefix ?? null; } +export function isProjectTabOpen(project: ProjectState, zellijTabs: string[]): boolean { + const canonical = (project.liveTab ?? project.tab).toLowerCase(); + return zellijTabs.some((tab) => { + const open = tab.toLowerCase(); + return open === canonical || open.startsWith(`${canonical} `) || open.startsWith(`${canonical}-`); + }); +} + export function getTabActivityText( project: ProjectState | null, display: ProjectDisplayState | null, @@ -158,7 +168,7 @@ export function getTabActivityText( return project.session.next.trim(); } if (display?.isReady || display?.isOrchestrationReady) { - return "Waiting for your next instruction"; + return "Agent finished the last task; choose the next action"; } if (project.session?.next?.trim()) return `Saved handoff: ${project.session.next.trim()}`; if (project.session?.done?.trim()) { @@ -317,9 +327,7 @@ export function getProjectDisplayState( !isOrchestrationReady && !isClosing && !isClosed; - const tabOpen = zellijTabs.some( - (tab) => tab.toLowerCase() === (project.liveTab ?? project.tab).toLowerCase(), - ); + const tabOpen = isProjectTabOpen(project, zellijTabs); const isActive = isRunning || isOrchestrationReady || @@ -346,9 +354,9 @@ export function getProjectDisplayState( const STATE_LABEL: Record = { offline: "Offline", running: "Working", - "session-open": "Waiting for you", - ready: "Waiting for you", - "orchestration-ready": "Waiting for you", + "session-open": "Agent shell open", + ready: "Ready for next step", + "orchestration-ready": "Ready for next step", closing: "Closing", closed: "Completed", idle: "Not running", @@ -398,8 +406,10 @@ export function buildProjectOperationsSnapshot( ? "closing" : display.isClosed ? "completed" - : display.isReady || display.isOrchestrationReady || display.isSessionOpen + : display.isReady || display.isOrchestrationReady ? "waiting_for_user" + : display.isSessionOpen + ? "open_idle" : "not_running"; const attention = attentionScore(project); const handoffAt = project.session?.mtime ?? null; @@ -410,7 +420,18 @@ export function buildProjectOperationsSnapshot( : project.session?.done?.trim() ? project.session.done.trim() : null; - const liveObserved = runtimeStateKnown && (display.isRunning || display.isSessionOpen || display.tabOpen); + const liveObserved = runtimeStateKnown && (display.isRunning || display.isReady || display.isOrchestrationReady || display.isSessionOpen || display.tabOpen); + const liveEvidenceLabel = display.isRunning + ? "Live agent process detected" + : display.isReady + ? "Agent signaled ready on connected computer" + : display.isOrchestrationReady + ? "Last run completed" + : display.isSessionOpen + ? "Agent shell open; no active task detected" + : display.tabOpen + ? "Workspace tab open" + : "No live observation"; return { project, @@ -419,7 +440,7 @@ export function buildProjectOperationsSnapshot( evidenceLabel: !runtimeStateKnown ? "Live status unavailable" : liveObserved - ? "Observed on connected computer" + ? liveEvidenceLabel : handoffAt ? "Saved agent context" : "No live observation", @@ -481,11 +502,7 @@ export function buildControlPageState( const state = getProjectDisplayState(project, data.zellijTabs, nowS, false, runtimeStateKnown); return state.isReady || state.isOrchestrationReady; }).length; - const openTabCount = data.projects.filter((project) => - data.zellijTabs.some( - (tab) => tab.toLowerCase() === (project.liveTab ?? project.tab).toLowerCase(), - ), - ).length; + const openTabCount = data.projects.filter((project) => isProjectTabOpen(project, data.zellijTabs)).length; const controlProjectCount = data.inventory.controlProjectCount ?? 0; const idleCount = data.projects.filter((project) => { const state = getProjectDisplayState(project, data.zellijTabs, nowS, false, runtimeStateKnown); diff --git a/src/lib/agent-config.ts b/src/lib/agent-config.ts index 87c5b2d8c..ced6ffcee 100644 --- a/src/lib/agent-config.ts +++ b/src/lib/agent-config.ts @@ -131,6 +131,12 @@ export function resolveEffectiveTab(canonical: string, activeTabs: string[]): st const findAlive = (name: string) => activeTabs.find((t) => t.toLowerCase() === name.toLowerCase()); const liveMatch = findAlive(canonical); if (liveMatch) return liveMatch; + const lower = canonical.toLowerCase(); + const suffixedMatch = activeTabs.find((tab) => { + const active = tab.toLowerCase(); + return active.startsWith(`${lower} `) || active.startsWith(`${lower}-`); + }); + if (suffixedMatch) return suffixedMatch; // Canonical not open; try a conf alias pointing to the same directory const all = parseProjectsConf(); const canonicalDir = all.find((p) => p.tab.toLowerCase() === canonical.toLowerCase())?.dir;