Skip to content

refactor(agent/workspace): up command#328

Merged
skevetter merged 2 commits intomainfrom
refactor-workspace-up
Jan 17, 2026
Merged

refactor(agent/workspace): up command#328
skevetter merged 2 commits intomainfrom
refactor-workspace-up

Conversation

@skevetter
Copy link
Owner

@skevetter skevetter commented Jan 17, 2026

Signed-off-by: Samuel K skevetter@pm.me

Summary by CodeRabbit

  • Refactor
    • Overhauled workspace startup for more reliable initialization, clearer separation of steps (tunnel, credentials, content, Docker), improved error handling and automatic cleanup on failures, and enhanced logging/diagnostics.
    • Better support for Git, local-folder, and image-based workspaces, improved daemon/install handling, and more robust result reporting back to the controller.

✏️ Tip: You can customize this high-level summary in your review settings.

Signed-off-by: Samuel K <skevetter@pm.me>
@coderabbitai
Copy link

coderabbitai bot commented Jan 17, 2026

📝 Walkthrough

Walkthrough

Refactored cmd/agent/workspace/up.go to extract a workspace initialization pipeline: workspace loading, tunnel initialization, credential setup, async Docker handling, and workspace content preparation; added decision helpers, explicit init error cleanup, and result-sending via tunnel.

Changes

Cohort / File(s) Summary
Entry & Decision APIs
cmd/agent/workspace/up.go
Added public helpers on UpCmd: loadWorkspaceInfo, shouldPreventDaemonShutdown, shouldInstallDaemon, handleInitError, cleanupCredentials, sendResult.
Initialization Pipeline
cmd/agent/workspace/up.go
Introduced workspaceInitializer and initWorkspace(...) orchestration returning tunnelClient, logger, credentialsDir; reorganized Run flow to defer credential cleanup and call centralized init.
Credentials Management
cmd/agent/workspace/up.go
Added credentialsConfig and configureCredentials(); moved credential setup into structured helper and ensured defer-based cleanup.
Content & Binaries
cmd/agent/workspace/up.go
Extracted content helpers: contentFolderExists, createContentFolder, downloadWorkspaceBinaries, preserved InitContentFolder signature semantics, added ensureLastDevContainerJson, and parameterized prepareWorkspace* flows (Git/Local/Image).
Docker Handling
cmd/agent/workspace/up.go
Added async Docker install/config pipeline: installDocker, installDockerAsync, waitForDocker, configureDockerDaemonAsync, configureDockerDaemon, writeDockerDaemonConfig, tryWriteRootlessDockerConfig, reloadDockerDaemon, and helpers to determine Docker path; daemon install/config driven by decision helpers.
Error Flow & Logging
cmd/agent/workspace/up.go
Centralized error wrapping and contextual logging across init steps; handleInitError performs workspace cleanup; sendResult marshals and sends results over tunnel.
Types & Params
cmd/agent/workspace/up.go
Added several internal param structs (e.g., prepareWorkspaceParams, credentialsConfig) to pass context, workspace data, and logger into helpers for testability and readability.

Sequence Diagram

sequenceDiagram
    participant User as UpCmd
    participant Init as workspaceInitializer
    participant Tunnel as Tunnel
    participant Creds as Credentials
    participant Docker as DockerInstaller
    participant WS as WorkspaceContent
    participant Daemon as DockerDaemon

    User->>Init: loadWorkspaceInfo(ctx)
    User->>Init: initWorkspace(ctx, cancel, workspaceInfo, debug, shouldInstallDaemon)
    Init->>Tunnel: initializeTunnel()
    Tunnel-->>Init: tunnelClient, logger
    Init->>Creds: configureCredentials(cfg)
    Creds-->>Init: credentialsDir

    par Async Docker install
        Init->>Docker: installDockerAsync()
        Docker-->>Init: install result
    and Prepare workspace content
        Init->>WS: prepareWorkspaceContent(params)
        WS-->>Init: content prepared
    end

    Init->>Docker: waitForDocker()
    Init->>Docker: configureDockerDaemonAsync() 
    Docker->>Daemon: write/reload config
    Daemon-->>Docker: ack

    Init-->>User: tunnelClient, logger, credentialsDir
    Note over User,Init: defer cleanupCredentials(credentialsDir)
    User->>Tunnel: sendResult(ctx, result, tunnelClient)
    alt init error
        User->>User: handleInitError(err, workspaceInfo, logger)
        User->>WS: DeleteWorkspaceFolder(...)
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(agent/workspace): up command' accurately describes the main change: a refactoring of the up command in the agent/workspace module.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@cmd/agent/workspace/up.go`:
- Around line 60-93: The init failure can leave credential files behind because
cleanupCredentials is only deferred after a successful init; change the
call/handling so credentialsDir is always captured and cleaned up even on error:
call initWorkspace(...) and immediately defer
cmd.cleanupCredentials(credentialsDir) after the call (before checking err) or
modify initWorkspace to return the credentialsDir even when it returns an error,
then ensure UpCmd.Run defers cleanupCredentials(credentialsDir) right after
initWorkspace returns so both the success path and the cmd.handleInitError(err,
workspaceInfo, logger) path always remove credentials; update any tests or
callers expecting empty credsDir on error accordingly.
- Around line 414-437: The shouldConfigureDockerDaemon method currently returns
err != nil && !local which skips daemon config when Local parsed successfully as
false; change the logic so it returns err == nil && !local (i.e., configure when
parsing succeeded and Local is false). Update
workspaceInitializer.shouldConfigureDockerDaemon to call
workspaceInfo.Agent.Local.Bool(), return true only when err == nil && !local,
and optionally log or handle the parse error from
workspaceInfo.Agent.Local.Bool() instead of treating it as a successful false.
- Around line 121-132: handleInitError can be called with a nil logger when
initializeTunnel fails, causing DeleteWorkspaceFolder to panic when it calls
log.Errorf; before calling clientimplementation.DeleteWorkspaceFolder in
handleInitError, guard against a nil logger (logger == nil) and substitute a
no-op or default logger instance (e.g., a NOP logger or a logger created via the
same log package used elsewhere) so DeleteWorkspaceFolder and its use of logger
(via log.Errorf) won’t panic; reference handleInitError, initializeTunnel,
DeleteWorkspaceFolder, DeleteWorkspaceFolderParams and workspaceInfo when
locating the change.

Signed-off-by: Samuel K <skevetter@pm.me>
@skevetter skevetter merged commit 0a9a151 into main Jan 17, 2026
66 of 70 checks passed
@skevetter skevetter deleted the refactor-workspace-up branch January 17, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant