mirror of
https://gh-proxy.org/https://github.com/klinsmaya/multica.git
synced 2026-11-08 01:16:45 +08:00
* fix(agent): own the runtime process tree on every backend Cancelling a task on Windows left the agent running. The daemon marked the task cancelled, revoked its mat_ token, and killed what it thought was the CLI — but on Windows the direct child is usually a cmd.exe shim and the real CLI is already a grandchild, so it survived. One reporter's orphan kept working for 40 minutes, read the daemon owner's profile PAT after its own token started returning 401, and posted comments as the member (GH #7522). Whole-tree ownership already existed on both platforms. It was opt-in, and opt-in rotted: 8 of the 27 places this package starts a process asked for a Unix process group, and 3 of the 23 backends took Windows Job Object ownership. Adding claude to that list would have left 19 backends with the same hole, so the fix is structural instead. - newRuntimeCmd, at the single point where a runtime process is built, now puts every command in its own process group and replaces os/exec's leader-only Cancel with a group-wide kill. A backend that wants a graceful shutdown still assigns its own cmd.Cancel and wins. This also covers the --version and model-discovery probes, which cmd.Output() runs without ever reaching a start helper. - startOwnedProcessTree is now the only way this package starts a process, so Windows Job Object ownership is not something a backend can forget. Each start is paired with releaseProcessGroup after the reap, which drops the job handle and kills whatever outlived it. - Two structural tests enforce both halves, the way TestOnlyLaunchGoSpawnsRuntimeProcesses already enforces the argv boundary: a new backend calling cmd.Start fails, and one that takes ownership without releasing it fails. The Unix half is #5918 for the 20 backends the original fix did not reach: their CLI was never in its own group at all, so no group-wide signal could have reached it. Co-authored-by: multica-agent <[email protected]> * fix(agent): own the process tree for synchronous probes too Review found the ownership boundary had a hole the PR claimed to cover. os/exec's Run, Output and CombinedOutput call Start themselves, so a probe written with cmd.Output() never reaches startOwnedProcessTree and owns nothing on Windows — where the direct child of a `--version` probe is typically the npm shim and the real CLI is already a grandchild. That is the same escape #7522 was reported for, in 15 call sites nobody was looking at; detectCLIVersion's own comment already describes a broken CLI leaving grandchildren that hold the stdout pipe. - runOwned / outputOwned / combinedOutputOwned give a synchronous probe the same start, reap and release a task launch gets. They keep the stdlib contract: stdout returned, stderr attached to the ExitError, combined output through a single writer so os/exec gives it one pipe. The stderr sample is bounded, as os/exec bounds its own. - All 15 probe sites route through them, and the structural guard now rejects Run/Output/CombinedOutput alongside Start. The release guard did not hold its invariant either: it only asked whether a file contained any releaseProcessGroup, so a second unreleased start in a file that already had a pair would pass — and models.go is exactly that shape. It now works per function: one owned start each, paired with at least one release. Equal counts would be the wrong rule, since dsh's Execute legitimately releases on two exit paths. Both guards are now pure functions over an AST, with tests that feed them synthetic sources — including the second-unreleased-start case that the file-level version missed — so the guards fail closed without needing a real regression committed to find out. Adds the Windows probe regression (descendant must be in the job and die with a cancelled probe) plus unix equivalents, and names the consequence in the degraded-ownership warning, which is the only signal an operator gets that cancellation on that host is back to killing the leader alone. Co-authored-by: multica-agent <[email protected]> * fix(agent): bound a finished probe's wait on pipes its descendants hold Review caught that runOwned could wait for its own cleanup and never return, and it reproduces: a descendant that inherited the output pipes holds them open after the leader exits, cmd.Wait blocks until the copy goroutines see EOF, and what would close those pipes is the release that runs after Wait. Cancellation breaks the cycle, which is why every test in the last round missed this — they all cancelled first, and they all set WaitDelay by hand. Normal exit has nothing to break it, and checkOpenclawVersion runs on the caller's context before runContext establishes any task timeout, so a version wrapper that exits cleanly while leaving a pipe-holding descendant would hang the task until a human cancelled it. runOwned now fills in a bounded WaitDelay when the caller left it zero — Go defines WaitDelay for precisely "a child process that exits but leaves its I/O pipes unclosed" — and signals the group before dropping ownership, so the descendant is reaped on Unix too, where releasing a process group is a no-op. A caller with its own bound keeps it; detectCLIVersion has set one by hand since MUL-3812 for this same shape. Adds the normal-exit regression on both platforms: the leader exits holding nothing, the descendant keeps the pipes, and outputOwned has to return in bounded time with the descendant gone. The Windows descendant spawner gains a spawn-and-exit mode and now hands its stdio to the descendant, which is what makes it hold the pipes at all. Co-authored-by: multica-agent <[email protected]> --------- Co-authored-by: J <[email protected]> Co-authored-by: multica-agent <[email protected]>
413 lines
12 KiB
Go
413 lines
12 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// mcodeBlockedArgs protect the ACP stdio transport selected by the daemon.
|
|
// The public CLI currently exposes no options on `mcode acp`; its only child
|
|
// command is the interactive `login` flow, which cannot run inside a claimed
|
|
// headless task. Keeping these tokens out of custom_args prevents a profile or
|
|
// agent from replacing the protocol process with help or authentication UI.
|
|
var mcodeBlockedArgs = map[string]blockedArgMode{
|
|
"acp": blockedStandalone,
|
|
"login": blockedStandalone,
|
|
"--region": blockedWithValue,
|
|
"-h": blockedStandalone,
|
|
"--help": blockedStandalone,
|
|
}
|
|
|
|
// mcodeBackend runs MiniMax Code as an ACP v1 agent server via `mcode acp`.
|
|
// MiniMax Code owns its Runtime, Session, permission, and questionnaire loops;
|
|
// this adapter only maps the shared ACP stream into Multica's Backend contract.
|
|
//
|
|
// The current public ACP surface deliberately declares loadSession:false and
|
|
// does not expose session-scoped model selection. Resume therefore returns a
|
|
// typed rejection so the daemon can retry on a fresh session, and model choice
|
|
// stays managed by MiniMax Code. If a later version advertises loadSession,
|
|
// the standard session/load path below begins working without a compatibility
|
|
// guess or a version-string gate.
|
|
type mcodeBackend struct {
|
|
cfg Config
|
|
}
|
|
|
|
var mcodeReaderDrainGrace = 2 * time.Second
|
|
var mcodeSessionStartupReadyDelay = 100 * time.Millisecond
|
|
|
|
var errMcodeProcessExited = errors.New("mcode process exited")
|
|
|
|
type mcodeMessageStream struct {
|
|
ch chan Message
|
|
mu sync.Mutex
|
|
closed bool
|
|
}
|
|
|
|
func newMcodeMessageStream(size int) *mcodeMessageStream {
|
|
return &mcodeMessageStream{ch: make(chan Message, size)}
|
|
}
|
|
|
|
func (s *mcodeMessageStream) send(message Message) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return
|
|
}
|
|
trySend(s.ch, message)
|
|
}
|
|
|
|
func (s *mcodeMessageStream) close() {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return
|
|
}
|
|
s.closed = true
|
|
close(s.ch)
|
|
}
|
|
|
|
func (b *mcodeBackend) Execute(ctx context.Context, prompt string, opts ExecOptions) (*Session, error) {
|
|
execPath := b.cfg.ExecutablePath
|
|
if execPath == "" {
|
|
execPath = "mcode"
|
|
}
|
|
if _, err := exec.LookPath(execPath); err != nil {
|
|
return nil, fmt.Errorf("mcode executable not found at %q: %w", execPath, err)
|
|
}
|
|
|
|
mcpServers, err := buildACPMcpServers(opts.McpConfig, b.cfg.Logger)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("mcode: invalid mcp_config: %w", err)
|
|
}
|
|
|
|
runCtx, cancel := runContext(ctx, opts.Timeout)
|
|
args := []string{"acp"}
|
|
args = append(args, filterCustomArgs(opts.ExtraArgs, mcodeBlockedArgs, b.cfg.Logger)...)
|
|
args = append(args, filterCustomArgs(opts.CustomArgs, mcodeBlockedArgs, b.cfg.Logger)...)
|
|
cmd := b.cfg.commandAt(execPath).exec(runCtx, args...)
|
|
hideAgentWindow(cmd)
|
|
cmd.Cancel = func() error {
|
|
if cmd.Process != nil {
|
|
signalProcessGroup(cmd, syscall.SIGKILL)
|
|
}
|
|
return nil
|
|
}
|
|
cmd.WaitDelay = mcodeReaderDrainGrace
|
|
b.cfg.logAgentCommand(cmd, newAgentCommandLogArgs(args, trustAgentCommandPositional(0, "acp")))
|
|
if opts.Cwd != "" {
|
|
cmd.Dir = opts.Cwd
|
|
}
|
|
cmd.Env = buildEnv(b.cfg.Env)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
cancel()
|
|
return nil, fmt.Errorf("mcode stdout pipe: %w", err)
|
|
}
|
|
stdin, err := cmd.StdinPipe()
|
|
if err != nil {
|
|
cancel()
|
|
return nil, fmt.Errorf("mcode stdin pipe: %w", err)
|
|
}
|
|
providerErr := newACPProviderErrorSniffer("mcode")
|
|
stderr, err := cmd.StderrPipe()
|
|
if err != nil {
|
|
cancel()
|
|
return nil, fmt.Errorf("mcode stderr pipe: %w", err)
|
|
}
|
|
if err := startOwnedProcessTree(cmd, b.cfg.Logger); err != nil {
|
|
cancel()
|
|
return nil, fmt.Errorf("start mcode: %w", err)
|
|
}
|
|
|
|
stderrSink := io.MultiWriter(newLogWriter(b.cfg.Logger, "[mcode:stderr] "), providerErr)
|
|
stderrDone := make(chan struct{})
|
|
go func() {
|
|
defer close(stderrDone)
|
|
_, _ = io.Copy(stderrSink, stderr)
|
|
}()
|
|
|
|
b.cfg.Logger.Info("mcode acp started", "pid", cmd.Process.Pid, "cwd", opts.Cwd)
|
|
msgStream := newMcodeMessageStream(256)
|
|
resCh := make(chan Result, 1)
|
|
var deliverable acpDeliverableTracker
|
|
var streamingCurrentTurn atomic.Bool
|
|
promptDone := make(chan hermesPromptResult, 1)
|
|
activity := make(chan struct{}, 1)
|
|
|
|
c := &hermesClient{
|
|
cfg: b.cfg,
|
|
stdin: stdin,
|
|
pending: make(map[int]*pendingRPC),
|
|
pendingTools: make(map[string]*pendingToolCall),
|
|
acceptNotification: func(string) bool {
|
|
return streamingCurrentTurn.Load()
|
|
},
|
|
onActivity: func() {
|
|
select {
|
|
case activity <- struct{}{}:
|
|
default:
|
|
}
|
|
},
|
|
onMessage: func(message Message) {
|
|
if !streamingCurrentTurn.Load() {
|
|
return
|
|
}
|
|
deliverable.observe(message)
|
|
msgStream.send(message)
|
|
},
|
|
onPromptDone: func(result hermesPromptResult) {
|
|
if !streamingCurrentTurn.Load() {
|
|
return
|
|
}
|
|
select {
|
|
case promptDone <- result:
|
|
default:
|
|
}
|
|
},
|
|
}
|
|
|
|
readerDone := make(chan struct{})
|
|
go func() {
|
|
defer close(readerDone)
|
|
scanner := newAgentStreamScanner(stdout)
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line != "" {
|
|
c.handleLine(line)
|
|
}
|
|
}
|
|
c.closeAllPending(errMcodeProcessExited)
|
|
}()
|
|
|
|
go func() {
|
|
defer msgStream.close()
|
|
defer close(resCh)
|
|
defer func() {
|
|
_ = stdin.Close()
|
|
// Cancellation must remain reachable before Wait. MCode or a tool
|
|
// descendant may ignore stdin EOF, so waiting first can deadlock every
|
|
// early-return path, including a rejected session resume.
|
|
cancel()
|
|
_ = cmd.Wait()
|
|
releaseProcessGroup(cmd)
|
|
}()
|
|
|
|
startTime := time.Now()
|
|
finalStatus := "completed"
|
|
var finalError string
|
|
var sessionID string
|
|
var resumeRejected bool
|
|
|
|
initResult, err := c.request(runCtx, "initialize", map[string]any{
|
|
"protocolVersion": 1,
|
|
"clientInfo": map[string]any{
|
|
"name": "multica-agent-sdk",
|
|
"version": "0.2.0",
|
|
},
|
|
"clientCapabilities": map[string]any{},
|
|
})
|
|
if err != nil {
|
|
finalStatus, finalError = mcodeRequestFailure(runCtx, opts.Timeout, "initialize", err)
|
|
resCh <- Result{Status: finalStatus, Error: finalError, DurationMs: time.Since(startTime).Milliseconds()}
|
|
return
|
|
}
|
|
|
|
mcpServers = filterACPMcpServersByCapability(
|
|
mcpServers,
|
|
extractACPMcpCapabilities(initResult),
|
|
"mcode",
|
|
b.cfg,
|
|
)
|
|
cwd := opts.Cwd
|
|
if cwd == "" {
|
|
cwd = "."
|
|
}
|
|
|
|
if opts.ResumeSessionID != "" {
|
|
if !mcodeLoadSessionSupported(initResult) {
|
|
finalStatus = "failed"
|
|
finalError = "mcode ACP does not support session loading; retry with a fresh session"
|
|
resumeRejected = true
|
|
resCh <- Result{
|
|
Status: finalStatus,
|
|
Error: finalError,
|
|
DurationMs: time.Since(startTime).Milliseconds(),
|
|
ResumeRejected: true,
|
|
}
|
|
return
|
|
}
|
|
if err := mcodeWaitForSessionStartup(runCtx); err != nil {
|
|
finalStatus, finalError = mcodeRequestFailure(runCtx, opts.Timeout, "session/load", err)
|
|
resCh <- Result{
|
|
Status: finalStatus,
|
|
Error: finalError,
|
|
DurationMs: time.Since(startTime).Milliseconds(),
|
|
ResumeRejected: resumeRejected,
|
|
}
|
|
return
|
|
}
|
|
result, loadErr := c.request(runCtx, "session/load", map[string]any{
|
|
"cwd": cwd,
|
|
"sessionId": opts.ResumeSessionID,
|
|
"mcpServers": mcpServers,
|
|
})
|
|
if loadErr != nil {
|
|
finalStatus, finalError = mcodeRequestFailure(runCtx, opts.Timeout, "session/load", loadErr)
|
|
if finalStatus == "failed" && isACPSessionNotFound(loadErr) {
|
|
resumeRejected = true
|
|
}
|
|
resCh <- Result{
|
|
Status: finalStatus,
|
|
Error: finalError,
|
|
DurationMs: time.Since(startTime).Milliseconds(),
|
|
ResumeRejected: resumeRejected,
|
|
}
|
|
return
|
|
}
|
|
sessionID, _ = resolveResumedSessionID(opts.ResumeSessionID, result)
|
|
} else {
|
|
if err := mcodeWaitForSessionStartup(runCtx); err != nil {
|
|
finalStatus, finalError = mcodeRequestFailure(runCtx, opts.Timeout, "session/new", err)
|
|
resCh <- Result{Status: finalStatus, Error: finalError, DurationMs: time.Since(startTime).Milliseconds()}
|
|
return
|
|
}
|
|
result, newErr := c.request(runCtx, "session/new", map[string]any{
|
|
"cwd": cwd,
|
|
"mcpServers": mcpServers,
|
|
})
|
|
if newErr != nil {
|
|
finalStatus, finalError = mcodeRequestFailure(runCtx, opts.Timeout, "session/new", newErr)
|
|
resCh <- Result{Status: finalStatus, Error: finalError, DurationMs: time.Since(startTime).Milliseconds()}
|
|
return
|
|
}
|
|
sessionID = extractACPSessionID(result)
|
|
if sessionID == "" {
|
|
resCh <- Result{
|
|
Status: "failed",
|
|
Error: "mcode session/new returned no session ID",
|
|
DurationMs: time.Since(startTime).Milliseconds(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
c.sessionID = sessionID
|
|
if opts.SystemPrompt != "" {
|
|
b.cfg.Logger.Debug("mcode ignoring ExecOptions.SystemPrompt; using cwd-scoped AGENTS.md", "cwd", opts.Cwd)
|
|
}
|
|
streamingCurrentTurn.Store(true)
|
|
_, err = c.request(runCtx, "session/prompt", map[string]any{
|
|
"sessionId": sessionID,
|
|
"prompt": []map[string]any{
|
|
{"type": "text", "text": prompt},
|
|
},
|
|
})
|
|
if err != nil {
|
|
finalStatus, finalError = mcodeRequestFailure(runCtx, opts.Timeout, "session/prompt", err)
|
|
if opts.ResumeSessionID != "" && isACPSessionNotFound(err) {
|
|
resumeRejected = true
|
|
sessionID = ""
|
|
}
|
|
} else {
|
|
select {
|
|
case result := <-promptDone:
|
|
c.mergeUsage(result.usage)
|
|
if result.stopReason == "cancelled" {
|
|
finalStatus = "aborted"
|
|
finalError = "execution cancelled"
|
|
} else if result.stopReason == "max_turn_requests" {
|
|
finalStatus = "failed"
|
|
finalError = "mcode reached its maximum turn requests"
|
|
}
|
|
default:
|
|
}
|
|
waitForACPNotificationQuiescence(runCtx, activity, readerDone, acpNotificationQuietTime, mcodeReaderDrainGrace)
|
|
}
|
|
streamingCurrentTurn.Store(false)
|
|
|
|
duration := time.Since(startTime)
|
|
_ = stdin.Close()
|
|
cancel()
|
|
<-readerDone
|
|
<-stderrDone
|
|
|
|
finalOutput, fullOutput := deliverable.result()
|
|
finalStatus, finalError = promoteACPResultOnProviderError(
|
|
finalStatus,
|
|
finalError,
|
|
fullOutput,
|
|
providerErr,
|
|
)
|
|
var usage map[string]TokenUsage
|
|
if accumulated := c.accumulatedUsage(); acpUsagePresent(accumulated) {
|
|
usage = map[string]TokenUsage{"unknown": accumulated}
|
|
}
|
|
resCh <- Result{
|
|
Status: finalStatus,
|
|
Output: finalOutput,
|
|
Error: finalError,
|
|
DurationMs: duration.Milliseconds(),
|
|
SessionID: sessionID,
|
|
ResumeRejected: resumeRejected,
|
|
Usage: usage,
|
|
}
|
|
}()
|
|
|
|
return &Session{Messages: msgStream.ch, Result: resCh}, nil
|
|
}
|
|
|
|
func mcodeLoadSessionSupported(result json.RawMessage) bool {
|
|
var payload struct {
|
|
AgentCapabilities struct {
|
|
LoadSession bool `json:"loadSession"`
|
|
} `json:"agentCapabilities"`
|
|
}
|
|
return json.Unmarshal(result, &payload) == nil && payload.AgentCapabilities.LoadSession
|
|
}
|
|
|
|
func mcodeWaitForSessionStartup(ctx context.Context) error {
|
|
timer := time.NewTimer(mcodeSessionStartupReadyDelay)
|
|
defer timer.Stop()
|
|
select {
|
|
case <-timer.C:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
func mcodeRequestFailure(ctx context.Context, timeout time.Duration, operation string, err error) (string, string) {
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return "timeout", fmt.Sprintf("mcode timed out during %s after %s", operation, timeout)
|
|
}
|
|
if ctx.Err() == context.Canceled {
|
|
return "aborted", "execution cancelled"
|
|
}
|
|
if mcodeSessionStartupExited(operation, err) {
|
|
return "failed", "mcode session/new failed after initialize: MiniMax Code ACP exited before starting a session; retry agent creation, and if it persists upgrade MiniMax Code or verify `mcode acp` starts successfully"
|
|
}
|
|
return "failed", fmt.Sprintf("mcode %s failed: %v", operation, err)
|
|
}
|
|
|
|
func mcodeSessionStartupExited(operation string, err error) bool {
|
|
if operation != "session/new" {
|
|
return false
|
|
}
|
|
// Only trust transport-level signals. A still-running MCode reports
|
|
// session/new failures as a JSON-RPC error, and session/new is where
|
|
// mcpServers are launched, so text matching on "process exited" or
|
|
// "broken pipe" would rewrite a caller's real MCP startup error into
|
|
// an MCode-exited message and drop its root cause.
|
|
return errors.Is(err, errMcodeProcessExited) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, syscall.EPIPE)
|
|
}
|