fix(lifecycle): publish agentctl events on workspace restore (#731)

This commit is contained in:
Carlos Florêncio
2026-04-29 09:57:16 +01:00
committed by GitHub
parent 4277ef495a
commit 8fb2e5ba52
2 changed files with 14 additions and 3 deletions
@@ -11,6 +11,7 @@ import (
"github.com/kandev/kandev/internal/agentctl/tracing"
"github.com/kandev/kandev/internal/common/appctx"
"github.com/kandev/kandev/internal/events"
"github.com/kandev/kandev/internal/secrets"
)
@@ -88,8 +89,16 @@ func (m *Manager) EnsureWorkspaceExecutionForSession(ctx context.Context, taskID
return nil, err
}
// Notify subscribers that agentctl is starting. createExecution already
// spawns waitForAgentctlReady which publishes AgentctlReady/AgentctlError,
// but AgentctlStarting is only published in the Launch path — add it here
// so workspace-only executions also notify the frontend.
m.eventPublisher.PublishAgentctlEvent(ctx, events.AgentctlStarting, execution, "")
// For workspace-only executions (no agent), wait for agentctl to be ready
// then connect the workspace stream so process output can be received
// then connect the workspace stream so process output can be received.
// Note: AgentctlReady/Error events are already handled by waitForAgentctlReady
// (started by createExecution), so this goroutine only connects the stream.
go func() {
// Use detached context that respects stopCh for graceful shutdown
waitCtx, cancel := appctx.Detached(ctx, m.stopCh, 60*time.Second)
@@ -101,13 +101,15 @@ export function PassthroughTerminal(props: PassthroughTerminalProps) {
const storeSessionId = useAppStore((state) => state.tasks.activeSessionId);
const sessionId = propSessionId ?? storeSessionId;
const { session, isActive } = useSession(sessionId);
const { session } = useSession(sessionId);
const agentctlStatus = useSessionAgentctl(sessionId);
const taskId = session?.task_id ?? null;
// Gate WS connection on agentctl readiness. During a long prepare script
// the backend terminal endpoint isn't accepting connections yet, and
// spamming reconnects burns cycles and confuses the loading state.
const canConnect = Boolean(sessionId && isActive && agentctlStatus.isReady);
// Don't require isActive — restored workspaces (terminal-state sessions)
// have agentctl running but the session state stays COMPLETED/CANCELLED.
const canConnect = Boolean(sessionId && agentctlStatus.isReady);
const wsBaseUrl = useWsBaseUrl();
const [isTerminalReady, setIsTerminalReady] = useState(false);