After this PR `molecule connect <id>` actually does something useful:
the default `--backend claude-code` runs `claude -p` as a subprocess
per inbound message, capturing stdout as the reply. The general
`--backend exec --backend-opt cmd="..."` lets operators bridge to any
program that reads stdin and writes stdout (Ollama, custom Python,
shell pipelines).
What's in:
- `internal/backends/exec/exec.go` — generic exec backend.
- cmd (required): shell command, run via /bin/sh -c (or cmd /c).
- timeout (default 60s): per-message wall clock; subprocess is
killed on timeout and the dispatcher keeps the message queued.
- pass_meta (default false): when true, populates env with
MOLECULE_WORKSPACE_ID / CALLER_ID / MESSAGE_ID / TASK_ID / METHOD.
- Stdin = joined text parts; stdout = reply text.
- Stderr is captured + surfaced in error messages so operators can
see what their command printed.
- Honours ctx cancellation — SIGTERM kills the subprocess immediately.
- `internal/backends/claudecode/claudecode.go` — thin shorthand.
Translates {bin, args, timeout, pass_meta} into the equivalent exec
config. Defaults: bin=claude, timeout=5m, pass_meta=true.
Reusing exec means timeout/stdin/env handling stays in one place.
- `internal/cmd/connect.go` — registers both backends so `--help`
shows them and the default `--backend claude-code` works without
flag tuning.
Test plan:
- exec: cmd-required, bad-timeout, zero-timeout, echo-stdin-to-stdout,
text-only-part-concat, non-zero-exit-surfaces-stderr, timeout-kills-
runaway, pass_meta-injects-env, no-pass_meta-leaves-env-untouched,
parent-env-inherited-when-pass_meta-off, ctx-cancel-kills-command.
- claude-code: registered, bin/args translation produces "echo -p hello",
bad-timeout-surfaces, default-config-builds.
- All Unix tests gated on `requireUnix(t)` — Windows-shell semantics
diverge; cross-platform coverage is M3 work (brew/scoop/winget).
- Full suite green under -race.
UX after this PR:
# default — Claude Code on PATH
molecule connect ws_01HF... --token $T
# custom handler
molecule connect ws_01HF... --backend exec \
--backend-opt cmd="python myhandler.py"
# different model via Claude Code
molecule connect ws_01HF... \
--backend-opt args="--model claude-sonnet-4-6"
# CI smoke
molecule connect ws_01HF... --backend mock --backend-opt reply=ok
Out of scope (M1.4 + later):
- M1.4: GoReleaser tag-triggered release.yml workflow
- stdio backend (one persistent subprocess + line-delimited JSON-RPC)
— moved to M2 since exec covers the common case
- openai / mcp backends — M4 per RFC
RFC: https://github.com/Molecule-AI/molecule-cli/issues/10
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package claudecode_test
|
|
|
|
import (
|
|
"context"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Molecule-AI/molecule-cli/internal/backends"
|
|
_ "github.com/Molecule-AI/molecule-cli/internal/backends/claudecode" // register
|
|
)
|
|
|
|
func requireUnix(t *testing.T) {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("Unix-only: shell command semantics differ on Windows")
|
|
}
|
|
}
|
|
|
|
func TestClaudeCode_Registered(t *testing.T) {
|
|
names := backends.Names()
|
|
found := false
|
|
for _, n := range names {
|
|
if n == "claude-code" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("claude-code missing from registry: %v", names)
|
|
}
|
|
}
|
|
|
|
// TestClaudeCode_BinArgsTranslation: the wrapper should compose bin +
|
|
// "-p" + extra-args into the underlying exec command. Use bin=echo
|
|
// args=hello so the command becomes "echo -p hello" and stdout is
|
|
// deterministic.
|
|
func TestClaudeCode_BinArgsTranslation(t *testing.T) {
|
|
requireUnix(t)
|
|
be, err := backends.Build("claude-code", backends.Config{
|
|
"bin": "echo",
|
|
"args": "hello",
|
|
"timeout": "5s",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer be.Close()
|
|
resp, err := be.HandleA2A(context.Background(), backends.Request{
|
|
Parts: []backends.Part{{Type: "text", Text: "ignored-stdin"}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := strings.TrimSpace(resp.Parts[0].Text)
|
|
if got != "-p hello" {
|
|
t.Errorf("got %q, want %q", got, "-p hello")
|
|
}
|
|
}
|
|
|
|
func TestClaudeCode_BadTimeoutSurfaces(t *testing.T) {
|
|
_, err := backends.Build("claude-code", backends.Config{"timeout": "nope"})
|
|
if err == nil {
|
|
t.Fatal("expected error on bad timeout")
|
|
}
|
|
}
|
|
|
|
// TestClaudeCode_LongTimeoutDefault: the default timeout (5m) should
|
|
// be longer than the bare exec backend default (60s). Verify by
|
|
// building with no timeout override and confirming a 90s sleep
|
|
// doesn't error from the wrapper's side. We don't actually wait —
|
|
// we just confirm building succeeds with no override (no error means
|
|
// the default parsed cleanly).
|
|
func TestClaudeCode_DefaultTimeoutAccepted(t *testing.T) {
|
|
be, err := backends.Build("claude-code", backends.Config{"bin": "true"})
|
|
if err != nil {
|
|
t.Fatalf("default config should build: %v", err)
|
|
}
|
|
be.Close()
|
|
}
|