Files
harness/runtime/builder_test.go
T
sausheong eb4fa67976 fix: harden runtime against panics/races, close path-traversal and fail-open gaps
- Runtime.Run rejects concurrent invocations instead of racing on shared state; panics in tools, hooks, and permission checkers are recovered and surfaced as errors/denials instead of crashing the loop
- AgentSpec.Loop now actually overrides RuntimeDeps.AgentLoop per-agent, as documented
- session.Store validates agent ID / session key as single path components, preventing traversal outside the session directory
- ValidatePathInWorkDir resolves symlinks on the target itself, not just its parent, closing a workspace-escape gap
- bash tool now fails closed on an unrecognized ExecPolicy.Level instead of allowing everything
- browser tool rejects unknown actions instead of silently no-op'ing

Bump README to v0.3.4 and document the AgentSpec.Loop override semantics.
2026-08-02 10:14:20 +08:00

137 lines
4.7 KiB
Go

package runtime
import (
"context"
"testing"
"github.com/sausheong/harness/tool"
"github.com/sausheong/harness/tools/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBuildRuntimeSetsProviderAndStaticPrompt(t *testing.T) {
spec := AgentSpec{ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5"}
deps := RuntimeDeps{ConfigSummary: "Configured channels: cli"}
inputs := RuntimeInputs{}
rt, err := BuildRuntime(deps, inputs, spec)
require.NoError(t, err)
require.Equal(t, "anthropic", rt.Provider)
require.Equal(t, "claude-sonnet-4-5", rt.Model)
require.NotEmpty(t, rt.StaticSystemPrompt)
require.Contains(t, rt.StaticSystemPrompt, `"A" agent (id: a)`)
require.Contains(t, rt.StaticSystemPrompt, "Configured channels: cli")
}
func TestBuildRuntimeLocalProvider(t *testing.T) {
spec := AgentSpec{ID: "x", Name: "X", Model: "local/qwen2.5:3b"}
rt, err := BuildRuntime(RuntimeDeps{}, RuntimeInputs{}, spec)
require.NoError(t, err)
require.Equal(t, "local", rt.Provider)
}
func TestBuildRuntimeMinimalSpecSafe(t *testing.T) {
spec := AgentSpec{ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5"}
rt, err := BuildRuntime(RuntimeDeps{}, RuntimeInputs{}, spec)
require.NoError(t, err)
require.Equal(t, "anthropic", rt.Provider)
require.NotEmpty(t, rt.StaticSystemPrompt)
}
func TestBuildRuntimeMergesSpecLoopOverSharedDefaults(t *testing.T) {
baseCalled := false
specCalled := false
baseStop := func(context.Context, string) { baseCalled = true }
specStop := func(context.Context, string) { specCalled = true }
rt, err := BuildRuntime(
RuntimeDeps{AgentLoop: LoopConfig{
MaxToolConcurrency: 3,
MaxAgentDepth: 2,
MaxToolResultLen: 8000,
Hooks: LifecycleHooks{OnStop: baseStop},
}},
RuntimeInputs{},
AgentSpec{
ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5",
Loop: LoopConfig{
MaxToolConcurrency: 7,
StreamingTools: true,
Hooks: LifecycleHooks{OnStop: specStop},
},
},
)
require.NoError(t, err)
assert.Equal(t, 7, rt.AgentLoop.MaxToolConcurrency)
assert.Equal(t, 2, rt.AgentLoop.MaxAgentDepth, "zero spec fields inherit shared defaults")
assert.Equal(t, 8000, rt.AgentLoop.MaxToolResultLen)
assert.True(t, rt.AgentLoop.StreamingTools)
require.NotNil(t, rt.AgentLoop.Hooks.OnStop)
rt.AgentLoop.Hooks.OnStop(context.Background(), "completed")
assert.False(t, baseCalled)
assert.True(t, specCalled)
}
func TestBuildRuntimeUsesCallerProvidedMemoryFiles(t *testing.T) {
// Caller composes memoryFiles however they want — typical patterns
// include walking AGENTS.md from workspace + $HOME, building from a
// wiki dump, or pulling from a doc index. Harness only concatenates
// it into the static prompt.
const sentinel = "MEMFILE_END_TO_END_SENTINEL"
deps := RuntimeDeps{
MemoryFiles: "\n\n## Project memory: AGENTS.md\n\n" + sentinel,
}
rt, err := BuildRuntime(deps, RuntimeInputs{}, AgentSpec{
ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5",
})
require.NoError(t, err)
require.Contains(t, rt.StaticSystemPrompt, sentinel)
require.Contains(t, rt.StaticSystemPrompt, "## Project memory:")
}
// TestBuildRuntime_MCPServers_BadConfigErrors confirms BuildRuntime
// surfaces MCP connection failures (here: invalid empty ServerConfig)
// rather than returning a partially-built Runtime.
func TestBuildRuntime_MCPServers_BadConfigErrors(t *testing.T) {
reg := tool.NewRegistry()
rt, err := BuildRuntime(
RuntimeDeps{},
RuntimeInputs{Tools: reg},
AgentSpec{
ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5",
MCPServers: []mcp.ServerConfig{{Name: ""}}, // invalid: name missing
},
)
require.Error(t, err)
assert.Nil(t, rt)
assert.Contains(t, err.Error(), "mcp server")
}
// TestBuildRuntime_MCPServers_RequiresRegistry confirms that declaring
// MCPServers when inputs.Tools is not a *tool.Registry is a clean error
// rather than a silent no-op.
func TestBuildRuntime_MCPServers_RequiresRegistry(t *testing.T) {
rt, err := BuildRuntime(
RuntimeDeps{},
RuntimeInputs{Tools: nil}, // not a *tool.Registry
AgentSpec{
ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5",
MCPServers: []mcp.ServerConfig{{Name: "x", Command: "true"}},
},
)
require.Error(t, err)
assert.Nil(t, rt)
assert.Contains(t, err.Error(), "*tool.Registry")
}
// TestRuntime_Close_NoOpWithoutMCP confirms Close on a runtime with no
// MCP clients is a safe no-op (returns nil, doesn't panic).
func TestRuntime_Close_NoOpWithoutMCP(t *testing.T) {
rt, err := BuildRuntime(RuntimeDeps{}, RuntimeInputs{}, AgentSpec{
ID: "a", Name: "A", Model: "anthropic/claude-sonnet-4-5",
})
require.NoError(t, err)
assert.NoError(t, rt.Close())
assert.NoError(t, rt.Close(), "second Close must also be a no-op")
}