Skip to content

Conversation

@ericcurtin
Copy link
Contributor

@ericcurtin ericcurtin commented Oct 15, 2025

So we can load models in the background without interaction

Summary by Sourcery

Add a --detach/-d option to docker model run to load models in the background without interaction, update command behavior, and include supporting tests and documentation.

New Features:

  • Introduce --detach/-d flag on the run command to enable background model loading without user input.

Enhancements:

  • Adjust run command logic to skip stdin reading and perform a silent preload when the --detach flag is set.

Documentation:

  • Document the --detach flag in the reference and provide an example for pre-loading models.

Tests:

  • Add TestRunCmdDetachFlag to validate the presence, default state, type, and behavior of the --detach flag.

Copilot AI review requested due to automatic review settings October 15, 2025 03:47
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 15, 2025

Reviewer's Guide

The PR introduces a new --detach/-d flag to the model run command, allowing models to be loaded in the background: it declares and registers the flag, adapts stdin-reading and execution flow for non-interactive mode, adds unit tests for the flag behavior, and updates the CLI documentation with flag details and an example.

Sequence diagram for model loading with --detach flag

sequenceDiagram
    actor User
    participant CLI
    participant Model
    User->>CLI: Run 'docker model run --detach <model>'
    CLI->>Model: Load model into memory (no prompt)
    Model-->>CLI: Model loaded
    CLI-->>User: (Optional) Success message if debug enabled
    CLI-->>User: Command returns immediately
Loading

Class diagram for updated Run command with detach flag

classDiagram
    class RunCmd {
        -backend string
        -ignoreRuntimeMemoryCheck bool
        -colorMode string
        -detach bool
        +newRunCmd()
    }
    RunCmd : +Flags()
    RunCmd : +Run()
    RunCmd : +chatWithMarkdown()
    RunCmd : +handleClientError()
    RunCmd : +desktopClient.Chat()
    RunCmd : +os.Stdin.Stat()
    RunCmd : +bufio.NewReader()
    RunCmd : +io.ReadAll()
    RunCmd : +cmd.Printf()
    RunCmd : +MarkHidden()
    RunCmd : +BoolVarP()
    RunCmd : +StringVar()
    RunCmd : +BoolVar()
    RunCmd : +StringVar()
    RunCmd : +Flags()
    RunCmd : +Run()
Loading

File-Level Changes

Change Details Files
Declare and register new --detach (-d) flag in newRunCmd
  • Add detach boolean variable
  • Bind flag with BoolVarP("detach", "d", false, ...)
cmd/cli/commands/run.go
Implement detach mode logic in run command
  • Skip stdin input when detach is true
  • Invoke desktopClient.Chat with empty prompt and discard output
  • Print debug message on successful load and return early
cmd/cli/commands/run.go
Add unit tests for detach flag
  • Verify flag existence and shorthand lookup
  • Check default value and flag type
  • Test setting and retrieving the detach flag value
cmd/cli/commands/run_test.go
Update CLI documentation for detach flag
  • Add detach entry to the reference table with alias
  • Include example usage snippet for background loading
cmd/cli/docs/reference/model_run.md

Possibly linked issues

  • #unknown: The PR adds the --detach flag to docker model run, enabling background model loading as requested by the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ericcurtin, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a --detach flag to the docker model run command, enabling users to load models into memory in the background without requiring immediate interaction. This enhancement is designed to improve the efficiency of model usage by allowing for pre-loading, thereby optimizing performance for subsequent requests.

Highlights

  • New --detach flag: A new --detach (shorthand -d) flag has been added to the docker model run command, allowing models to be loaded in the background without requiring immediate user interaction.
  • Background Model Loading: When the --detach flag is used, the command will perform a minimal request to load the specified model into memory and then exit, silently discarding any output. This is useful for pre-loading models to ensure maximum performance for subsequent requests.
  • Conditional Stdin Reading: The logic for reading input from stdin has been updated to only execute if the --detach flag is not present, preventing blocking behavior when loading models in the background.
  • Test Coverage: New test cases have been added to run_test.go to verify the correct behavior, existence, shorthand, default value, and type of the new --detach flag.
  • Documentation Update: The model_run.md documentation has been updated to include the new --detach flag in the command reference table and provides an example of how to use it for pre-loading models.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a --detach flag to the Docker model run command to enable loading models in the background without user interaction, useful for pre-loading models to ensure maximum performance for subsequent requests.

  • Adds --detach (-d) flag to the run command with proper flag configuration
  • Implements detach mode logic that loads the model without entering interactive chat
  • Updates documentation with usage examples and flag description

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
cmd/cli/commands/run.go Implements the core detach functionality and flag definition
cmd/cli/commands/run_test.go Adds comprehensive tests for the new detach flag
cmd/cli/docs/reference/model_run.md Updates documentation with flag description and usage example

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `cmd/cli/commands/run_test.go:117` </location>
<code_context>
 	}
 }
+
+func TestRunCmdDetachFlag(t *testing.T) {
+	// Create the run command
+	cmd := newRunCmd()
</code_context>

<issue_to_address>
**suggestion (testing):** Missing test for actual command execution with --detach flag.

Please add a test that simulates running the command with --detach, verifying that model loading occurs and no interaction (prompt or output) happens. Use a mock for desktopClient to assert correct Chat invocation and output handling.

Suggested implementation:

```golang
func TestRunCmdDetachFlag(t *testing.T) {
	// Create the run command
	cmd := newRunCmd()

	// Verify the --detach flag exists
	detachFlag := cmd.Flags().Lookup("detach")
	if detachFlag == nil {
		t.Fatal("--detach flag not found")
	}

	// Verify the shorthand flag exists
	detachFlagShort := cmd.Flags().ShorthandLookup("d")
	if detachFlagShort == nil {
		t.Fatal("shorthand -d flag not found")
	}

	// Mock desktopClient
	type mockDesktopClient struct {
		chatCalled bool
		modelLoaded bool
	}

	mockClient := &mockDesktopClient{}

	// Replace the actual desktopClient with the mock
	originalDesktopClient := desktopClient
	desktopClient = mockClient
	defer func() { desktopClient = originalDesktopClient }()

	// Implement the mock Chat method
	mockChat := func(model string, prompt string, opts ...interface{}) error {
		mockClient.chatCalled = true
		if model == "expected-model" {
			mockClient.modelLoaded = true
		}
		return nil
	}

	// Patch the Chat method if needed
	// If desktopClient is an interface, assign mockChat to the method
	// Otherwise, adjust as per your codebase

	// Simulate running the command with --detach
	cmd.SetArgs([]string{"--detach", "--model", "expected-model", "--prompt", "ignored"})
	err := cmd.Execute()
	if err != nil {
		t.Fatalf("cmd.Execute() failed: %v", err)
	}

	// Assert that model loading occurred and Chat was called
	if !mockClient.modelLoaded {
		t.Error("model was not loaded in detached mode")
	}
	if !mockClient.chatCalled {
		t.Error("Chat was not called in detached mode")
	}

	// Assert that no output or prompt was produced
	// This depends on your output handling; for example, check stdout/stderr buffers if used
	// If you use a custom output writer, assert it is empty here
}

```

- You may need to adjust the mock implementation to match your actual `desktopClient` type and method signatures.
- If your codebase uses dependency injection or a different way to set the client, update the assignment accordingly.
- If you capture output via buffers or custom writers, add assertions to ensure no output is produced.
- Ensure the mock `Chat` method is correctly hooked into the command execution.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a --detach flag to the docker model run command, which is a useful feature for pre-loading models. The implementation is mostly correct, but there's a minor issue with argument handling where providing a prompt with --detach leads to the prompt being silently ignored. I've suggested adding validation to prevent this confusing behavior. Additionally, while new tests have been added for the flag, I've recommended expanding them to cover the command's behavior for better test coverage. The documentation updates are clear and well-written.

So we can load models in the background without interaction

Signed-off-by: Eric Curtin <[email protected]>
@ericcurtin ericcurtin force-pushed the add-docker-model-run-detach branch from a805612 to b330b1c Compare October 15, 2025 03:52
// Handle --detach flag: just load the model without interaction
if detach {
// Make a minimal request to load the model into memory
err := desktopClient.Chat(backend, model, "", apiKey, func(content string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The request will be recorded, not a big deal, but to keep in mind

Copy link
Contributor Author

@ericcurtin ericcurtin Oct 15, 2025

Choose a reason for hiding this comment

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

Makes me think do we have a way of forking without chatting? 🤔

@ericcurtin ericcurtin merged commit a3f4c9b into main Oct 15, 2025
9 checks passed
@ericcurtin ericcurtin deleted the add-docker-model-run-detach branch October 15, 2025 10:06
}

// Handle --detach flag: just load the model without interaction
if detach {
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't this have been placed at the beginning of the function so we don't need another if !detach?

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.

5 participants