Files
harness/runtime/subagent.go
sausheong 123a233cb7 Drop Felix references; harness stands as a platform on its own
Removes every Felix mention from public-facing surfaces so the framework
reads as a standalone agent platform rather than an extracted module.

User-visible changes:
- HTTP User-Agent on web_fetch / web_search: Felix/1.0 → harness/1.0
- bash tool path-substitution prefix: '[felix]' → '[harness]'
- On-disk paths: <workspace>/.felix/ → <workspace>/.harness/
  (todos.json + spill/ tree); tests updated in lockstep.
- OTel trace attributes: felix.trace_id / felix.total_ms /
  felix.phase_count → harness.* equivalents.

Doc-comment changes:
- Examples (lta-agent, data-agent) lose 'non-Felix consumer' framing.
- README drops the 'extracted from Felix' lede and the 'What was lifted
  from Felix' section, replaced with a 'Design' overview that points
  to developer.md.
- Runtime / tool / compaction doc comments rewritten so behaviour
  notes describe what the harness does without naming the prior origin.
- Test-fixture text 'you are felix' → 'you are an assistant'.

Verified: zero 'felix' (case-insensitive) in tracked .go / .md files.
All 16 packages build and pass tests.
2026-05-08 00:24:06 +08:00

146 lines
5.3 KiB
Go

// Package runtime — subagent factory + adapter.
//
// This file wires the per-Runtime task tool: it constructs a tool.SubagentFactory
// that builds a fresh Runtime for the named subagent, sets its Parent
// pointer (for event forwarding) and Depth (for the recursion cap), and adapts
// Runtime.Run to tool.SubagentRunner so the task tool can drain it.
//
// The factory enforces the depth cap (maxAgentDepth()) before constructing
// anything — a subagent that would exceed the limit returns an error that
// TaskTool surfaces as a tool result error, which the parent LLM then sees.
package runtime
import (
"context"
"fmt"
"github.com/sausheong/harness/session"
"github.com/sausheong/harness/tool"
)
// SubagentBuildFn constructs the per-subagent RuntimeInputs given the resolved
// AgentSpec. Each call site provides this closure because tool-registry
// construction (file/bash/web tools, MCP, send_message) is environment-specific.
//
// Implementations MUST:
// - Build a fresh tool.Executor for the subagent (workspace = spec.Workspace)
// - Resolve the LLM provider from spec.Model
// - Create a fresh in-memory Session via NewSubagentSession
// - Set IngestSource to "" (subagents are short-lived; no KG ingest)
// - Set Compaction to whatever the call site's chat path uses for this agent
type SubagentBuildFn func(spec AgentSpec) (RuntimeInputs, error)
// SubagentSpec carries the per-call-site policy (whether to inherit parent
// history, registration check) the framework needs to honour without
// importing consumer config types.
type SubagentSpec struct {
Spec AgentSpec
// Registered reports whether the consumer considers this subagent ID
// valid for parent-driven invocation. False prevents a parent agent
// from spawning this child via the task tool.
Registered bool
// InheritContext, when true, copies the parent session's view into
// the child session before BuildRuntime fires.
InheritContext bool
}
// MakeSubagentFactory returns a tool.SubagentFactory that builds a Runtime
// for the named subagent and adapts it to tool.SubagentRunner. Enforces
// the recursion depth cap before constructing anything.
//
// resolve looks up the subagent's spec at call-time so consumers can
// hot-reload registrations.
func MakeSubagentFactory(
resolve func(agentID string) (SubagentSpec, bool),
deps RuntimeDeps,
buildInputs SubagentBuildFn,
parent *Runtime,
) tool.SubagentFactory {
return func(ctx context.Context, agentID string, parentDepth int) (tool.SubagentRunner, error) {
// Depth-cap enforcement.
if parentDepth+1 > parent.maxAgentDepth() {
return nil, fmt.Errorf("subagent depth limit %d reached", parent.maxAgentDepth())
}
ss, ok := resolve(agentID)
if !ok {
return nil, fmt.Errorf("subagent %q not found", agentID)
}
if !ss.Registered {
return nil, fmt.Errorf("agent %q is not registered as a subagent", agentID)
}
inputs, err := buildInputs(ss.Spec)
if err != nil {
return nil, fmt.Errorf("subagent %q: build inputs: %w", agentID, err)
}
// InheritContext: pre-populate the subagent's fresh session with
// copies of parent's session entries. The subagent's first LLM
// call then sees the parent's history; the existing
// CacheLastMessage path naturally caches the inherited prefix on
// the subagent's subsequent turns.
if ss.InheritContext && parent != nil && parent.Session != nil && inputs.Session != nil {
inheritParentHistory(inputs.Session, parent.Session)
}
rt, err := BuildRuntime(deps, inputs, ss.Spec)
if err != nil {
return nil, fmt.Errorf("subagent %q: build runtime: %w", agentID, err)
}
rt.Parent = parent
rt.Depth = parentDepth + 1
return &subagentRunnerAdapter{rt: rt}, nil
}
}
// inheritParentHistory copies the parent session's current view (post any
// compaction) into the destination subagent session via Append.
//
// Operates outside Append's normal "stitch into leaf" path: the FIRST
// inherited entry gets its ParentID cleared so the subagent's empty leaf
// doesn't cause Append to leave a dangling pointer to a parent entry.
func inheritParentHistory(dst, src *session.Session) {
view := src.View()
for i, e := range view {
if i == 0 {
e.ParentID = ""
}
dst.Append(e)
}
}
// subagentRunnerAdapter satisfies tool.SubagentRunner by adapting Runtime.Run.
type subagentRunnerAdapter struct{ rt *Runtime }
func (s *subagentRunnerAdapter) Run(ctx context.Context, prompt string) (<-chan tool.AgentEventLike, error) {
raw, err := s.rt.Run(ctx, prompt, nil)
if err != nil {
return nil, err
}
out := make(chan tool.AgentEventLike, 16)
go func() {
defer close(out)
for ev := range raw {
out <- adaptEvent(ev)
}
}()
return out, nil
}
// adaptEvent translates an AgentEvent into the tool.AgentEventLike shape
// that TaskTool understands. Only the fields TaskTool actually inspects
// are filled.
func adaptEvent(ev AgentEvent) tool.AgentEventLike {
return tool.AgentEventLike{
Type: int(ev.Type),
Text: ev.Text,
Done: ev.Type == EventDone,
Aborted: ev.Type == EventAborted,
Err: ev.Error,
}
}
// NewSubagentSession returns a fresh in-memory Session for a subagent run.
// SetStore is NOT called — subagent sessions are ephemeral and do not
// write JSONL to disk. The parent's session is the durable record.
func NewSubagentSession(agentID string) *session.Session {
return session.NewSession(agentID, "subagent")
}