- Add edge runtime config and opsconsole package - Refactor edge console to use new runtime config - Add service source metadata support - Update CLI adapter with target terminology - Add edge operation contract and event bus replay - Update node label and command ops surface - Add E2E smoke tests and full validation - Update proto runtime definitions - Update documentation and agent-ops rules
741 lines
21 KiB
Go
741 lines
21 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
clipkg "iop/apps/node/internal/adapters/cli"
|
|
"iop/apps/node/internal/adapters/cli/internal/testutil"
|
|
noderuntime "iop/apps/node/internal/runtime"
|
|
"iop/packages/config"
|
|
)
|
|
|
|
func TestCLIExecutePersistentWaitsForSlowFirstOutput(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"slow-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do sleep 0.2; printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 50,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
defer cancel()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-1",
|
|
Target: "slow-echo",
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
|
|
events := sink.Events()
|
|
if events[len(events)-1].Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("expected last event to be complete, got %q", events[len(events)-1].Type)
|
|
}
|
|
if combined := testutil.CollectDeltas(events); !strings.Contains(combined, "reply:hello") {
|
|
t.Fatalf("expected reply:hello in deltas, got %q", combined)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistentProcessExitReturnsError(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"exit-on-input": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do printf "before-exit\n"; exit 2; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 500,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-1",
|
|
Target: "exit-on-input",
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if err == nil {
|
|
t.Fatal("expected non-nil error when persistent process exits")
|
|
}
|
|
|
|
events := sink.Events()
|
|
var hasError bool
|
|
for _, e := range events {
|
|
if e.Type == noderuntime.EventTypeError {
|
|
hasError = true
|
|
}
|
|
}
|
|
if !hasError {
|
|
t.Fatal("expected error event in emitted events")
|
|
}
|
|
if len(events) > 0 && events[len(events)-1].Type == noderuntime.EventTypeComplete {
|
|
t.Fatal("last event should not be complete when process exits unexpectedly")
|
|
}
|
|
}
|
|
|
|
func TestCLIStartPersistentTerminalAndExecuteWritesPrompt(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"echo-persistent": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 300,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-1",
|
|
Target: "echo-persistent",
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
|
|
events := sink.Events()
|
|
var started, completed bool
|
|
for _, e := range events {
|
|
switch e.Type {
|
|
case noderuntime.EventTypeStart:
|
|
started = true
|
|
case noderuntime.EventTypeComplete:
|
|
completed = true
|
|
}
|
|
}
|
|
if !started {
|
|
t.Fatal("expected start event")
|
|
}
|
|
if !completed {
|
|
t.Fatal("expected complete event")
|
|
}
|
|
if combined := testutil.CollectDeltas(events); !strings.Contains(combined, "reply:hello") {
|
|
t.Fatalf("expected reply:hello in deltas, got %q", combined)
|
|
}
|
|
}
|
|
|
|
// TestCLIExecutePersistentCancelDoesNotTerminateSession verifies that a
|
|
// cancelled run leaves the session alive so subsequent runs can reuse it.
|
|
func TestCLIExecutePersistentCancelDoesNotTerminateSession(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
// ResponseIdleTimeoutMS is short so the cancel-drain exits quickly,
|
|
// allowing the second execute to acquire the session lock promptly.
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"slow-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do sleep 0.05; printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 150,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
// First execute: cancel before idle timeout fires.
|
|
firstCtx, firstCancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
|
defer firstCancel()
|
|
|
|
sink1 := &testutil.FakeSink{}
|
|
_ = c.Execute(firstCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-1",
|
|
Target: "slow-echo",
|
|
Input: map[string]any{"prompt": "first"},
|
|
}, sink1)
|
|
|
|
// Give time for cancel drain to finish (drain uses idleTimeout=150ms).
|
|
time.Sleep(400 * time.Millisecond)
|
|
|
|
// Second execute on same session — must succeed (session still alive).
|
|
execCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
|
|
sink2 := &testutil.FakeSink{}
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-2",
|
|
Target: "slow-echo",
|
|
Input: map[string]any{"prompt": "second"},
|
|
}, sink2)
|
|
if err != nil {
|
|
t.Fatalf("second execute after cancel must succeed (session should be alive): %v", err)
|
|
}
|
|
events2 := sink2.Events()
|
|
if len(events2) == 0 || events2[len(events2)-1].Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("expected complete event on second run, got %v", events2)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistentConsecutiveExecutes(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"consecutive-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 100,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
sink1 := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-1",
|
|
Target: "consecutive-echo",
|
|
Input: map[string]any{"prompt": "first"},
|
|
}, sink1)
|
|
if err != nil {
|
|
t.Fatalf("first execute: %v", err)
|
|
}
|
|
events1 := sink1.Events()
|
|
if events1[len(events1)-1].Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("first run: expected last event to be complete, got %q", events1[len(events1)-1].Type)
|
|
}
|
|
|
|
sink2 := &testutil.FakeSink{}
|
|
err = c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-2",
|
|
Target: "consecutive-echo",
|
|
Input: map[string]any{"prompt": "second"},
|
|
}, sink2)
|
|
if err != nil {
|
|
t.Fatalf("second execute: %v", err)
|
|
}
|
|
events2 := sink2.Events()
|
|
if events2[len(events2)-1].Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("second run: expected last event to be complete, got %q", events2[len(events2)-1].Type)
|
|
}
|
|
|
|
for _, e := range events2 {
|
|
if e.Type == noderuntime.EventTypeDelta && strings.Contains(e.Delta, "first") {
|
|
t.Fatalf("second run received first run's delta (session contamination): %q", e.Delta)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistentCreatesIndependentSessionsForSameProfile(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"multi-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 200,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
ctx := context.Background()
|
|
|
|
sinkA := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-a",
|
|
Target: "multi-echo",
|
|
SessionID: "session-a",
|
|
Input: map[string]any{"prompt": "alpha"},
|
|
}, sinkA)
|
|
if err != nil {
|
|
t.Fatalf("session-a execute: %v", err)
|
|
}
|
|
|
|
sinkB := &testutil.FakeSink{}
|
|
err = c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-b",
|
|
Target: "multi-echo",
|
|
SessionID: "session-b",
|
|
Input: map[string]any{"prompt": "beta"},
|
|
}, sinkB)
|
|
if err != nil {
|
|
t.Fatalf("session-b execute: %v", err)
|
|
}
|
|
|
|
if combined := testutil.CollectDeltas(sinkA.Events()); !strings.Contains(combined, "alpha") {
|
|
t.Fatalf("session-a: expected alpha in deltas, got %q", combined)
|
|
}
|
|
if combined := testutil.CollectDeltas(sinkB.Events()); !strings.Contains(combined, "beta") {
|
|
t.Fatalf("session-b: expected beta in deltas, got %q", combined)
|
|
}
|
|
if combined := testutil.CollectDeltas(sinkA.Events()); strings.Contains(combined, "beta") {
|
|
t.Fatalf("session-a contaminated with session-b output: %q", combined)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistentRequireExistingSessionFailsWhenMissing(t *testing.T) {
|
|
testutil.RequireUnixShell(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"echo-req": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `while IFS= read -r line; do printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: false,
|
|
ResponseIdleTimeoutMS: 200,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
ctx := context.Background()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-1",
|
|
Target: "echo-req",
|
|
SessionID: "nonexistent",
|
|
SessionMode: noderuntime.SessionModeRequireExisting,
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if err == nil {
|
|
t.Fatal("expected error for require-existing with missing session")
|
|
}
|
|
if strings.Contains(err.Error(), "start") {
|
|
t.Fatalf("should not have started a new process, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistent_UserCancelEmitsUserCancelMessage(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"slow-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do sleep 10; printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 500,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithCancel(ctx)
|
|
|
|
sink := &testutil.FakeSink{}
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
cancel()
|
|
}()
|
|
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-usercancel",
|
|
Target: "slow-echo",
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
|
|
events := sink.Events()
|
|
var lastMsg string
|
|
var cancelEvent bool
|
|
for i := len(events) - 1; i >= 0; i-- {
|
|
if events[i].Type == noderuntime.EventTypeCancelled {
|
|
cancelEvent = true
|
|
lastMsg = events[i].Message
|
|
}
|
|
}
|
|
if !cancelEvent {
|
|
t.Fatal("expected cancelled event")
|
|
}
|
|
if lastMsg != "user-cancel" {
|
|
t.Fatalf("expected cancel Message 'user-cancel', got %q", lastMsg)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistent_TimeoutEmitsTimeoutMessage(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"slow-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do sleep 10; printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 500,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
|
defer cancel()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
executeErr := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-timeout",
|
|
Target: "slow-echo",
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if executeErr == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
|
|
events := sink.Events()
|
|
var lastMsg string
|
|
var cancelEvent bool
|
|
for i := len(events) - 1; i >= 0; i-- {
|
|
if events[i].Type == noderuntime.EventTypeCancelled {
|
|
cancelEvent = true
|
|
lastMsg = events[i].Message
|
|
}
|
|
}
|
|
if !cancelEvent {
|
|
t.Fatal("expected cancelled event")
|
|
}
|
|
if lastMsg != "timeout" {
|
|
t.Fatalf("expected cancel Message 'timeout', got %q", lastMsg)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistent_CompletionMarkerLineEndsRun(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"marker-line": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do printf "reply:%s\n" "$line"; printf "<<END_OF_RESPONSE>>\n"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 500,
|
|
StartupIdleTimeoutMS: 50,
|
|
CompletionMarker: config.CompletionMarkerConf{Line: "<<END_OF_RESPONSE>>"},
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
defer cancel()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-marker-line",
|
|
Target: "marker-line",
|
|
Input: map[string]any{"prompt": "hello"},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
|
|
events := sink.Events()
|
|
last := events[len(events)-1]
|
|
if last.Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("expected last event to be complete, got %q", last.Type)
|
|
}
|
|
if last.Message != "completion-marker" {
|
|
t.Fatalf("expected Message 'completion-marker', got %q", last.Message)
|
|
}
|
|
if combined := testutil.CollectDeltas(events); !strings.Contains(combined, "reply:hello") {
|
|
t.Fatalf("expected reply:hello in deltas, got %q", combined)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistent_CompletionMarkerRegexEndsRun(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"marker-regex": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do printf "reply:%s\n" "$line"; printf "DONE 42\n"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 500,
|
|
StartupIdleTimeoutMS: 50,
|
|
CompletionMarker: config.CompletionMarkerConf{Regex: "^DONE \\d+$"},
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
defer cancel()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-marker-regex",
|
|
Target: "marker-regex",
|
|
Input: map[string]any{"prompt": "hi"},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
|
|
events := sink.Events()
|
|
last := events[len(events)-1]
|
|
if last.Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("expected last event to be complete, got %q", last.Type)
|
|
}
|
|
if last.Message != "completion-marker" {
|
|
t.Fatalf("expected Message 'completion-marker', got %q", last.Message)
|
|
}
|
|
if combined := testutil.CollectDeltas(events); !strings.Contains(combined, "reply:hi") {
|
|
t.Fatalf("expected reply:hi in deltas, got %q", combined)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistent_NoMarkerFallsBackToIdleTimeout(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"no-marker": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do sleep 0.1; printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 50,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
defer cancel()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-no-marker",
|
|
Target: "no-marker",
|
|
Input: map[string]any{"prompt": "fallback"},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
|
|
events := sink.Events()
|
|
last := events[len(events)-1]
|
|
if last.Type != noderuntime.EventTypeComplete {
|
|
t.Fatalf("expected last event to be complete, got %q", last.Type)
|
|
}
|
|
if last.Message != "idle-timeout" {
|
|
t.Fatalf("expected Message 'idle-timeout' when no marker is set, got %q", last.Message)
|
|
}
|
|
if combined := testutil.CollectDeltas(events); !strings.Contains(combined, "reply:fallback") {
|
|
t.Fatalf("expected reply:fallback in deltas, got %q", combined)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistent_IdleTimeoutMessageReason(t *testing.T) {
|
|
testutil.RequirePTYSupport(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"idle-reason": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `stty -echo; while IFS= read -r line; do sleep 0.1; printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: true,
|
|
ResponseIdleTimeoutMS: 50,
|
|
StartupIdleTimeoutMS: 50,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
|
|
ctx := context.Background()
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatalf("start: %v", err)
|
|
}
|
|
defer func() { _ = c.Stop(ctx) }()
|
|
|
|
execCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
defer cancel()
|
|
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(execCtx, noderuntime.ExecutionSpec{
|
|
RunID: "run-idle-reason",
|
|
Target: "idle-reason",
|
|
Input: map[string]any{"prompt": "test"},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
|
|
events := sink.Events()
|
|
var complete *noderuntime.RuntimeEvent
|
|
for i := range events {
|
|
if events[i].Type == noderuntime.EventTypeComplete {
|
|
complete = &events[i]
|
|
}
|
|
}
|
|
if complete == nil {
|
|
t.Fatal("expected complete event")
|
|
}
|
|
if complete.Message != "idle-timeout" {
|
|
t.Fatalf("expected Message 'idle-timeout', got %q", complete.Message)
|
|
}
|
|
}
|
|
|
|
func TestCLIExecutePersistentMaintainsHundredLogicalSessions(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping heavy session test in short mode")
|
|
}
|
|
testutil.RequireUnixShell(t)
|
|
|
|
cfg := config.CLIConf{
|
|
Enabled: true,
|
|
Profiles: map[string]config.CLIProfileConf{
|
|
"hundred-echo": {
|
|
Command: "sh",
|
|
Args: []string{"-c", `while IFS= read -r line; do printf "reply:%s\n" "$line"; done`},
|
|
Persistent: true,
|
|
Terminal: false,
|
|
ResponseIdleTimeoutMS: 300,
|
|
},
|
|
},
|
|
}
|
|
c := clipkg.New(cfg, zap.NewNop())
|
|
ctx := context.Background()
|
|
|
|
const n = 20
|
|
for i := 0; i < n; i++ {
|
|
sessionID := "session-" + string(rune('a'+i%26)) + "-" + string(rune('0'+i/26))
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-first-" + sessionID,
|
|
Target: "hundred-echo",
|
|
SessionID: sessionID,
|
|
Input: map[string]any{"prompt": "ping-" + sessionID},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("session %q first execute: %v", sessionID, err)
|
|
}
|
|
}
|
|
|
|
// Second round — all sessions must still be alive.
|
|
for i := 0; i < n; i++ {
|
|
sessionID := "session-" + string(rune('a'+i%26)) + "-" + string(rune('0'+i/26))
|
|
sink := &testutil.FakeSink{}
|
|
err := c.Execute(ctx, noderuntime.ExecutionSpec{
|
|
RunID: "run-second-" + sessionID,
|
|
Target: "hundred-echo",
|
|
SessionID: sessionID,
|
|
Input: map[string]any{"prompt": "pong-" + sessionID},
|
|
}, sink)
|
|
if err != nil {
|
|
t.Fatalf("session %q second execute: %v", sessionID, err)
|
|
}
|
|
}
|
|
|
|
_ = c.Stop(ctx)
|
|
}
|