Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

### Bug Fixes

- [Bug Fix] **`coi tool spec` launches claude and codex with prompts that start with `-` and prompts of 128 KiB or more** — the printed command passed the prompt as a command-line argument (`"$(cat <file>)"`), so a prompt starting with `-` (e.g. front matter `---` or a list item `- `) was read as tool options, and a prompt of 128 KiB or more failed with `Argument list too long`. For claude the command now feeds the prompt file on stdin (`<file`); claude reads it as the first message and still opens the interactive session. For codex the prompt follows `--`; codex refuses a prompt on stdin in an interactive session, so a prompt of 128 KiB or more is left out of the command and its staged path comes back in the `prompt` field, as for opencode, for the orchestrator to deliver after launch.
- [Bug Fix] **The security monitor no longer flags firewall-allowed LAN traffic at session start in `restricted` and `allowlist` mode** — its first poll could run before it had read the container's firewall rules, and until then only `[[network.hosts]]` counted as allowed. A connection the firewall accepts, such as a gateway port other than DNS (53) and DHCP (67) or the LAN under `allow_local_network_access`, was flagged as a private-network leak and could pause the container under `auto_pause_on_high`. The first poll now waits for that read, at most 5 seconds; `coi monitor --watch` waits the same way before its first frame.
- [Bug Fix] **`coi validate profile` names the unknown key in a nested section** — an unknown key below the top level (e.g. `[network] foo = 1`) was reported only as `/network: validation failed`; it now says `/network: additional properties 'foo' not allowed`.
- [Bug Fix] **Claude Code's "Autocompact now defaults to 400k tokens" dialog no longer blocks every new container** — Claude Code 2.1.3xx shows this announcement once at startup and waits for an answer, which stalls unattended `coi run` and tmux workflows. It records the answer per account in `~/.claude.json`, but each new ephemeral container gets a fresh copy of the host's file, so the dialog came back every session unless it had been answered on the host. coi now marks the announcement as seen (`lowerAutoWindowAskedOf`, `lowerAutoWindowToldTo`) for the account in the copied file, which is what pressing Esc in the dialog does. The autocompact window itself is not changed: set `autoCompactWindow` or use `/autocompact` to change it.
Expand Down
59 changes: 35 additions & 24 deletions internal/cli/tool_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -48,16 +49,17 @@ owning the terminal, streaming, input, and lifecycle.
coi builds the command from the profile's tool via the tool.Tool abstraction
(session id, resume, model, permission), so it is correct for any supported
tool (claude/codex/…). The prompt (and optional system prompt) are staged into
the container as files and referenced via "$(cat <file>)", keeping arbitrary
prompt content off the command line. Only tool-derived env is emitted: model
and effort settings, goose's GOOSE_MODE, plus the session-dir redirects to the
workspace for pi and omp. Secrets and auth stay with the caller, which adds its
own --env when it execs.
the container as files and referenced from the command ("<file" on stdin for
claude, "$(cat <file>)" otherwise), keeping arbitrary prompt content off the
command line. Only tool-derived env is emitted: model and effort settings,
goose's GOOSE_MODE, plus the session-dir redirects to the workspace for pi and
omp. Secrets and auth stay with the caller, which adds its own --env when it
execs.

Tools that can't embed the initial prompt in their launch command (e.g.
opencode) instead get a "prompt" field: the in-container path to the staged
prompt file, for the orchestrator to deliver out-of-band after launch (e.g.
tmux load-buffer + paste-buffer).
opencode, or codex with a prompt of 128 KiB or more) instead get a "prompt"
field: the in-container path to the staged prompt file, for the orchestrator
to deliver out-of-band after launch (e.g. tmux load-buffer + paste-buffer).

Resume strategies (at most one): --continue[=<id>] discovers coi's host-side
session store and resumes if found, else starts fresh; --resume-id <id> asserts
Expand Down Expand Up @@ -93,11 +95,12 @@ func init() {
// Secrets/auth are deliberately absent — the caller adds those itself.
//
// Prompt is set only for tools that can't embed the initial prompt in their
// launch command (they don't implement ToolWithPrompt, e.g. opencode). It is the
// launch command (they don't implement ToolWithPrompt, e.g. opencode, or the
// prompt is too large for it, e.g. codex at 128 KiB or more). It is the
// in-container path to the staged prompt file — the same file embedding tools
// reference via "$(cat …)" in Command — so the orchestrator delivers it
// out-of-band after launch (e.g. `tmux load-buffer <prompt>` + `paste-buffer`,
// which is file-based and safe for arbitrary content). Empty when the prompt is
// reference in Command — so the orchestrator delivers it out-of-band after
// launch (e.g. `tmux load-buffer <prompt>` + `paste-buffer`, which is
// file-based and safe for arbitrary content). Empty when the prompt is
// embedded in Command, or when no prompt was given.
type toolSpecResult struct {
Command []string `json:"command"`
Expand All @@ -118,9 +121,9 @@ func (a *App) toolSpecCommand(cmd *cobra.Command) error {
return &ExitCodeError{Code: 2, Message: "--session-id is required"}
}
// The session id is a caller-supplied value that becomes a filename
// component and is joined into the shell-run launch command (referenced via
// "$(cat …/<id>.prompt)"); reject anything that isn't a safe token so it
// can't break or inject the command.
// component and is joined into the shell-run launch command (referenced as
// "<…/<id>.prompt" or "$(cat …/<id>.prompt)"); reject anything that isn't a
// safe token so it can't break or inject the command.
if err := session.ValidateSessionID(toolSpecSessionID); err != nil {
return &ExitCodeError{Code: 2, Message: err.Error()}
}
Expand Down Expand Up @@ -188,11 +191,11 @@ func (a *App) toolSpecCommand(cmd *cobra.Command) error {

// Stage prompt / system prompt as in-container files so their (arbitrary)
// content never touches the launch command line.
promptPath, err := a.stageSpecFile(mgr, toolSpecPromptFile, filepath.Join(runsDir, toolSpecSessionID+".prompt"), uid)
promptPath, promptSize, err := a.stageSpecFile(mgr, toolSpecPromptFile, filepath.Join(runsDir, toolSpecSessionID+".prompt"), uid)
if err != nil {
return err
}
systemPromptPath, err := a.stageSpecFile(mgr, toolSpecSystemPromptFile, filepath.Join(runsDir, toolSpecSessionID+".sys"), uid)
systemPromptPath, _, err := a.stageSpecFile(mgr, toolSpecSystemPromptFile, filepath.Join(runsDir, toolSpecSessionID+".sys"), uid)
if err != nil {
return err
}
Expand All @@ -201,6 +204,7 @@ func (a *App) toolSpecCommand(cmd *cobra.Command) error {
SessionID: toolSpecSessionID,
PromptFile: promptPath,
SystemPromptFile: systemPromptPath,
PromptSize: promptSize,
}

// Apply the resume strategy resolved above.
Expand Down Expand Up @@ -277,19 +281,20 @@ func ensureContainerRunsDir(mgr container.ContainerManager, homeDir string, uid
}

// stageSpecFile copies a host prompt file into the container at destPath and
// returns destPath; an empty hostPath returns "" (no file, no path emitted).
func (a *App) stageSpecFile(mgr container.ContainerManager, hostPath, destPath string, uid int) (string, error) {
// returns destPath and the content's size; an empty hostPath returns "" (no
// file, no path emitted).
func (a *App) stageSpecFile(mgr container.ContainerManager, hostPath, destPath string, uid int) (string, int, error) {
if hostPath == "" {
return "", nil
return "", 0, nil
}
content, err := os.ReadFile(hostPath)
if err != nil {
return "", fmt.Errorf("failed to read %q: %w", hostPath, err)
return "", 0, fmt.Errorf("failed to read %q: %w", hostPath, err)
}
if err := mgr.CreateFileWithOwner(destPath, string(content), uid, uid, "0600"); err != nil {
return "", fmt.Errorf("failed to stage prompt file in container: %w", err)
return "", 0, fmt.Errorf("failed to stage prompt file in container: %w", err)
}
return destPath, nil
return destPath, len(content), nil
}

// buildToolLaunchArgv returns the launch argv for the tool and, when the tool
Expand All @@ -300,7 +305,8 @@ func (a *App) stageSpecFile(mgr container.ContainerManager, hostPath, destPath s
// prompt is surfaced rather than silently dropped. A system prompt on a tool
// without one is still rejected loudly (those tools have no such concept).
// A print-mode spec on a ToolWithHeadlessPrompt (opencode) uses the tool's
// headless form instead, which carries the prompt itself.
// headless form instead, which carries the prompt itself. A prompt too large
// for the tool's command (tool.ErrPromptOutOfBand, codex) goes out-of-band too.
//
// It does NOT apply the COI_USE_DUMMY test override: `coi run --prompt` runs the
// tool by its real binary name (the dummy test image installs the stub AS
Expand All @@ -314,6 +320,11 @@ func buildToolLaunchArgv(t tool.Tool, spec tool.LaunchSpec) (argv []string, outO
}
} else if twp, ok := t.(tool.ToolWithPrompt); ok {
argv, err = twp.BuildCommandLaunch(spec)
if errors.Is(err, tool.ErrPromptOutOfBand) {
outOfBandPrompt = spec.PromptFile
spec.PromptFile = ""
argv, err = twp.BuildCommandLaunch(spec)
}
if err != nil {
return nil, "", err
}
Expand Down
29 changes: 24 additions & 5 deletions internal/cli/tool_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ func TestBuildToolSpecCommand_ClaudeNative(t *testing.T) {
if !strings.Contains(joined, `--append-system-prompt "$(cat /run/s)"`) {
t.Errorf("missing system prompt: %q", joined)
}
// Initial prompt is the trailing positional, staged via "$(cat …)".
if argv[len(argv)-1] != `"$(cat /run/p)"` {
t.Errorf("prompt must be trailing cat-subst: %q", joined)
if argv[len(argv)-1] != "</run/p" {
t.Errorf("prompt must be the trailing stdin redirect: %q", joined)
}
// Claude embeds the prompt, so there's no out-of-band prompt.
if prompt != "" {
Expand Down Expand Up @@ -87,8 +86,8 @@ func TestBuildToolSpecCommand_CodexPrompt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if argv[len(argv)-1] != `"$(cat /run/p)"` {
t.Errorf("codex prompt must be trailing cat-subst: %v", argv)
if got := argv[len(argv)-2:]; got[0] != "--" || got[1] != `"$(cat /run/p)"` {
t.Errorf("codex prompt must be trailing cat-subst after --: %v", argv)
}
if prompt != "" {
t.Errorf("codex embeds the prompt; no out-of-band prompt expected, got %q", prompt)
Expand All @@ -100,6 +99,26 @@ func TestBuildToolSpecCommand_CodexPrompt(t *testing.T) {
}
}

// A codex prompt over one argument's limit (128 KiB) can't ride in the
// command, and interactive codex refuses a non-terminal stdin: the command
// goes without it and the staged path comes back out-of-band.
func TestBuildToolSpecCommand_CodexLargePromptOutOfBand(t *testing.T) {
argv, prompt, err := buildToolSpecCommand(tool.NewCodex(), tool.LaunchSpec{
SessionID: "sid", PromptFile: "/run/p", PromptSize: 200 * 1024,
})
if err != nil {
t.Fatal(err)
}
for _, arg := range argv {
if arg == "--" || strings.Contains(arg, "/run/p") {
t.Errorf("codex command must not carry the large prompt: %v", argv)
}
}
if prompt != "/run/p" {
t.Errorf("expected out-of-band prompt path /run/p, got %q", prompt)
}
}

func TestBuildToolSpecCommand_ToolWithoutPromptOutOfBand(t *testing.T) {
// opencode doesn't implement ToolWithPrompt: the prompt can't ride in argv,
// so it comes back as the out-of-band prompt (the staged file path), not in
Expand Down
9 changes: 7 additions & 2 deletions internal/tool/codex.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,19 @@ func (c *CodexTool) BuildCommand(sessionID string, resume bool, resumeSessionID
// a future release drops it. Codex has no first-class system-prompt
// flag (instructions live in AGENTS.md), so a SystemPromptFile is rejected
// rather than silently dropped. The prompt is passed as `"$(cat <file>)"` so
// arbitrary content stays in the file.
// arbitrary content stays in the file, after `--` so a prompt starting with
// "-" is not parsed as options. Interactive codex refuses a non-terminal
// stdin, so a prompt too large for one argument returns ErrPromptOutOfBand.
func (c *CodexTool) BuildCommandLaunch(spec LaunchSpec) ([]string, error) {
if spec.SystemPromptFile != "" {
return nil, fmt.Errorf("codex has no system-prompt flag; use AGENTS.md instead of --system-prompt-file")
}
if spec.PromptFile != "" && spec.PromptSize > maxArgLen {
return nil, ErrPromptOutOfBand
}
cmd := c.BuildCommand(spec.SessionID, spec.Resume, spec.ResumeSessionID)
if spec.PromptFile != "" {
cmd = append(cmd, catSubst(spec.PromptFile))
cmd = append(cmd, "--", catSubst(spec.PromptFile))
}
return cmd, nil
}
Expand Down
15 changes: 15 additions & 0 deletions internal/tool/launch.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tool

import "errors"

// LaunchSpec carries the session-specific dynamics an external orchestrator
// supplies when it asks coi for a tool's launch spec (`coi tool spec`, #751).
// The tool owns the translation into its CLI; the caller owns only these
Expand All @@ -17,6 +19,9 @@ type LaunchSpec struct {
ResumeSessionID string
PromptFile string // in-container path; empty = no initial prompt
SystemPromptFile string // in-container path; empty = no system prompt
// PromptSize is the byte size of PromptFile's content; 0 = unknown. A tool
// that passes the prompt as an argument checks it against maxArgLen.
PromptSize int
// Print requests a headless "run to completion and exit" launch (fire and
// forget) rather than an interactive session — for Claude, this adds `-p`
// (`--print`) so the agent runs the prompt, prints its response, and exits
Expand Down Expand Up @@ -52,6 +57,16 @@ type ToolWithHeadlessPrompt interface {
BuildHeadlessCommand(spec LaunchSpec) ([]string, error)
}

// maxArgLen is the most bytes one argv string can hold on Linux:
// MAX_ARG_STRLEN (131072) less its NUL. A longer one fails the exec with
// "Argument list too long".
const maxArgLen = 131071

// ErrPromptOutOfBand is returned by BuildCommandLaunch when the prompt can't
// ride in the command (too large for one argument). The caller builds the
// launch without it and delivers the staged prompt file out-of-band.
var ErrPromptOutOfBand = errors.New("prompt too large for the launch command; deliver it out-of-band")

// catSubst returns a `"$(cat <path>)"` token whose value the container shell
// expands to the file's contents at launch. path is a coi-controlled file
// (~/.coi/runs/<id>.*) with no shell metacharacters, so the arbitrary prompt
Expand Down
Loading
Loading