feat(desktop,host-service): gate agent start on workspace setup

Merged
MI
4mo ago
+2371-98

What & why

Creating a workspace with both a setup script and an agent currently starts
two terminals at once: the "Workspace Setup" pane and a separate agent pane.
The agent therefore begins working before setup (dependency install, DB
seeding, auth copy) has finished, in a worktree that isn't ready yet.

Also, you end up with an extra terminal sticking around indefinitely that's no longer needed.

This adds an Experimental setting, "Wait for workspace setup before starting
agents"
(off by default, so existing behaviour is unchanged). When enabled:

  • the agent command is chained after the setup commands with && in the one
    Workspace Setup terminal, so the agent only starts once setup completes
    successfully, and a failed setup never launches the agent into a broken
    workspace;
  • only one terminal is created — the agent reuses the setup terminal instead
    of a second one starting alongside it.

The gate applies to both creation flows:

v1 (renderer launch)

  • New reuseExistingPane flag on the shared terminal launch config; the
    orchestrator's terminal adapter runs the command in the caller-owned pane
    (attaching it itself, never removing it on failure) instead of splitting.
  • buildSetupPaneLaunchRequest in @superset/shared owns all setup-pane
    targeting: chained reuse-pane request, split-pane request, or unchanged
    passthrough, with a chained flag telling WorkspaceInitEffects whether
    the launch consumed the setup commands.

v2 (host-side launch)

  • Host workspaces.create accepts waitForSetupBeforeAgents;
    useWorkspaceCreates injects it from the desktop setting on every
    agent-carrying create (dashboard modal, task popovers, retry flows). The
    CLI omits the flag, keeping its behaviour unchanged.
  • With exactly one terminal sugar agent and a resolvable setup command, the
    agent command (built via the extracted buildTerminalAgentLaunch, no
    terminal created) is chained into the setup terminal; the agent result
    reuses that terminal's id and appendLaunchesToPaneLayout dedupes it to a
    single tab.

Common to both: chat launches, manual-execute launches, multi-agent launches
and requests already targeting a pane keep the previous parallel behaviour —
chaining only applies to a single auto-executing terminal agent.

The setting is stored in local-db (wait_for_setup_before_agent, migration
0044, generated with drizzle-kit generate), exposed via
get/setWaitForSetupBeforeAgent tRPC procedures, with a toggle under
Settings → Experimental (shared v1/v2 variant). The toggle is a
self-contained component mounted once, so graduating or reverting it later is
a one-line change.

How I tested it

  • Unit tests: 6 cases for buildSetupPaneLaunchRequest, 2 orchestrator tests
    for the reuse-pane adapter path, buildTerminalAgentLaunch command
    composition + NOT_FOUND, and appendLaunchesToPaneLayout tab dedupe
    (mutation-checked).
  • bun test: packages/shared, packages/host-service (902 pass), and the
    desktop suites for the touched areas; bun run typecheck and
    bun run lint exit 0.
  • CDP-validated end-to-end in the running dev app, both ways in both UIs,
    using a test repo with an 8s setup script:
    • toggle off: setup and agent start in parallel in two terminals
      (v1 split pane / v2 two tabs) — unchanged;
    • toggle on: one Workspace Setup terminal runs setup && agent; the
      agent's prompt appears only after "setup done" (v1 and v2);
    • the Experimental toggle renders in both v1 and v2 settings, flips with
      optimistic update, persists, and the next create picks it up without a
      restart.

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs

Summary by CodeRabbit

  • New Features
    • Added an experimental settings toggle to wait for workspace setup before starting agents.
    • Agent launches can chain setup with terminal execution and reuse the existing setup pane when enabled.
    • Workspace creation now honors the toggle for agent-based workspaces.
  • Bug Fixes
    • Prevented duplicate tabs when setup and agent launches share the same session.
    • Improved reused-pane failure handling so existing panes aren’t removed when terminal writes fail.