runtime: implement Review with snapshot/build/run/extract/origin + spec-override tests
This commit is contained in:
1 parent
fd16dcefee
commit
d9580eee8f
2 files changed
+682
-2
No files matched your search
+237
-2
@@ -21,15 +21,25 @@
|
||||
//
|
||||
// Note: pass context.Background() — the parent's ctx is canceled by
|
||||
// the time OnStop fires.
|
||||
//
|
||||
// Concurrency: if your OnStop loop fires Review concurrently (e.g.,
|
||||
// back-to-back turns where reviewer N is still running when turn N+1
|
||||
// completes), ensure your MemoryStore/SkillStore are concurrency-safe.
|
||||
// The bundled tool/memory/jsonl and tool/skills/disk implementations
|
||||
// are; custom backends must guarantee safe concurrent writes.
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/sausheong/harness/llm"
|
||||
"github.com/sausheong/harness/session"
|
||||
"github.com/sausheong/harness/tool"
|
||||
"github.com/sausheong/harness/tool/memory"
|
||||
"github.com/sausheong/harness/tool/skills"
|
||||
)
|
||||
|
||||
// ReviewSpec describes one reviewer pass over the parent Runtime's
|
||||
@@ -113,7 +123,232 @@ var ErrReviewRecursion = errors.New("runtime: review recursion (parent is itself
|
||||
// to recurse.
|
||||
const reviewerAgentID = "__review__"
|
||||
|
||||
// Review is a stub that later tasks fill in.
|
||||
// Review runs a one-shot reviewer Runtime against parent's just-
|
||||
// finished session. See package doc for the typical OnStop wiring.
|
||||
//
|
||||
// Required spec fields: Prompt, Tools. All other fields have sensible
|
||||
// defaults (see ReviewSpec doc-comments).
|
||||
//
|
||||
// Returns ReviewResult with non-nil Err for setup failures (nil parent,
|
||||
// recursion guard, nil Tools, empty Prompt, build errors). Mid-Run
|
||||
// failures populate Turns/ToolCalls but leave Err nil.
|
||||
//
|
||||
// Cancelation: pass context.Background() when calling from OnStop —
|
||||
// the parent's ctx is canceled by the time OnStop fires.
|
||||
func Review(ctx context.Context, parent *Runtime, spec ReviewSpec) ReviewResult {
|
||||
return ReviewResult{Err: errors.New("not implemented")}
|
||||
start := time.Now()
|
||||
if parent == nil {
|
||||
return ReviewResult{Err: errors.New("runtime.Review: parent Runtime is nil"), Duration: time.Since(start)}
|
||||
}
|
||||
if parent.AgentID == reviewerAgentID {
|
||||
return ReviewResult{Err: ErrReviewRecursion, Duration: time.Since(start)}
|
||||
}
|
||||
if spec.Tools == nil {
|
||||
return ReviewResult{Err: errors.New("runtime.Review: spec.Tools is required"), Duration: time.Since(start)}
|
||||
}
|
||||
if spec.Prompt == "" {
|
||||
return ReviewResult{Err: errors.New("runtime.Review: spec.Prompt is required"), Duration: time.Since(start)}
|
||||
}
|
||||
|
||||
rt, err := buildReviewerRuntime(parent, spec)
|
||||
if err != nil {
|
||||
return ReviewResult{Err: err, Duration: time.Since(start)}
|
||||
}
|
||||
defer rt.Close()
|
||||
|
||||
// Capture the inherited prefix length BEFORE the reviewer runs so
|
||||
// extractActions can skip parent-session entries and only extract
|
||||
// tool-result envelopes appended by the reviewer itself.
|
||||
inheritedCount := len(rt.Session.View())
|
||||
|
||||
// Apply hard timeout. If spec.Timeout is 0, default to 60s.
|
||||
timeout := spec.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = 60 * time.Second
|
||||
}
|
||||
runCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
// Provenance: tag every memory/skills write the reviewer makes
|
||||
// with origin "review". Both keys are independent (per-package
|
||||
// design); set both so whichever tool the reviewer calls picks up
|
||||
// the right value without coordination.
|
||||
runCtx = context.WithValue(runCtx, memory.OriginKey, "review")
|
||||
runCtx = context.WithValue(runCtx, skills.OriginKey, "review")
|
||||
|
||||
events, err := rt.Run(runCtx, spec.Prompt, nil)
|
||||
if err != nil {
|
||||
return ReviewResult{Err: err, Duration: time.Since(start)}
|
||||
}
|
||||
|
||||
// Drain events. Forward to OnEvent if set; count Turns and ToolCalls.
|
||||
var turns, toolCalls int
|
||||
for ev := range events {
|
||||
if spec.OnEvent != nil {
|
||||
spec.OnEvent(ev)
|
||||
}
|
||||
switch ev.Type {
|
||||
case EventToolCallStart:
|
||||
toolCalls++
|
||||
case EventDone:
|
||||
turns++
|
||||
}
|
||||
}
|
||||
|
||||
actions := extractActions(rt.Session, inheritedCount)
|
||||
|
||||
return ReviewResult{
|
||||
Actions: actions,
|
||||
Turns: turns,
|
||||
ToolCalls: toolCalls,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
}
|
||||
|
||||
// extractActions walks sess.View() starting at startIdx and returns
|
||||
// the Message field of every tool_result entry whose Output parses as
|
||||
// the canonical action-result envelope with Success=true.
|
||||
//
|
||||
// Convention is over schema: tools that don't emit the envelope
|
||||
// (e.g. list returns a raw array, get returns the entry struct, edit
|
||||
// tools return plain text) are silently skipped.
|
||||
//
|
||||
// Note: SessionEntry stores the per-type payload as a json.RawMessage
|
||||
// in the Data field, so we first unmarshal Data into ToolResultData to
|
||||
// recover Output, then unmarshal Output into the action envelope.
|
||||
func extractActions(sess *session.Session, startIdx int) []string {
|
||||
view := sess.View()
|
||||
if startIdx >= len(view) {
|
||||
return nil
|
||||
}
|
||||
var actions []string
|
||||
for _, e := range view[startIdx:] {
|
||||
if e.Type != session.EntryTypeToolResult {
|
||||
continue
|
||||
}
|
||||
var tr session.ToolResultData
|
||||
if err := json.Unmarshal(e.Data, &tr); err != nil {
|
||||
continue
|
||||
}
|
||||
var env struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(tr.Output), &env); err != nil {
|
||||
continue
|
||||
}
|
||||
if !env.Success || env.Message == "" {
|
||||
continue
|
||||
}
|
||||
actions = append(actions, env.Message)
|
||||
}
|
||||
return actions
|
||||
}
|
||||
|
||||
// buildReviewerRuntime constructs a Runtime configured for a one-shot
|
||||
// review pass over parent's just-finished session.
|
||||
//
|
||||
// Inheritance defaults:
|
||||
// - LLM: spec.Provider, falling back to parent.LLM
|
||||
// - Model: spec.Model, falling back to parent.Provider+"/"+parent.Model
|
||||
// - ContextWindow: spec.ContextWindow, falling back to parent.ContextWindow
|
||||
// - Workspace: parent.Workspace (for trace/spill paths)
|
||||
// - Memory: spec.Memory, falling back to parent.Memory (SHARED, not snapshotted)
|
||||
// - Skills: spec.Skills, falling back to parent.Skills (SHARED)
|
||||
// - Permission: spec.Permission (NOT inherited from parent — defaults to nil)
|
||||
// - SystemPrompt: spec.SystemPrompt (override) or empty (default identity built from tool names)
|
||||
// - MaxTurns: spec.MaxTurns or 16
|
||||
//
|
||||
// Always-set on reviewer:
|
||||
// - AgentID = "__review__" (recursion guard)
|
||||
// - IngestSource = "review" (KG ingest skips)
|
||||
// - Hooks zeroed (no OnStop reentry, no observation noise)
|
||||
// - KG nil (reviewer doesn't recall or ingest)
|
||||
// - Compaction nil (short-lived; MaxTurns 16 keeps context small)
|
||||
func buildReviewerRuntime(parent *Runtime, spec ReviewSpec) (*Runtime, error) {
|
||||
provider := spec.Provider
|
||||
if provider == nil {
|
||||
provider = parent.LLM
|
||||
}
|
||||
model := spec.Model
|
||||
if model == "" {
|
||||
// Re-compose parent.Provider/parent.Model since runtime stores
|
||||
// them split (after ParseProviderModel).
|
||||
if parent.Provider != "" && parent.Model != "" {
|
||||
model = parent.Provider + "/" + parent.Model
|
||||
} else {
|
||||
model = parent.Model // best-effort fallback when parent has no provider
|
||||
}
|
||||
}
|
||||
ctxWindow := spec.ContextWindow
|
||||
if ctxWindow == 0 {
|
||||
ctxWindow = parent.ContextWindow
|
||||
}
|
||||
mem := spec.Memory
|
||||
if mem == nil {
|
||||
mem = parent.Memory
|
||||
}
|
||||
skillProv := spec.Skills
|
||||
if skillProv == nil {
|
||||
skillProv = parent.Skills
|
||||
}
|
||||
maxTurns := spec.MaxTurns
|
||||
if maxTurns == 0 {
|
||||
maxTurns = 16
|
||||
}
|
||||
|
||||
deps := RuntimeDeps{
|
||||
Skills: skillProv,
|
||||
Memory: mem,
|
||||
Permission: spec.Permission, // defaults to nil = allow-all
|
||||
// KGFn nil — reviewer never recalls or ingests
|
||||
// AgentLoop zeroed — Hooks left nil so no OnStop reentry
|
||||
}
|
||||
inputs := RuntimeInputs{
|
||||
Provider: provider,
|
||||
Tools: spec.Tools,
|
||||
Session: snapshotSession(parent.Session),
|
||||
Compaction: nil, // reviewer is short-lived
|
||||
IngestSource: "review",
|
||||
}
|
||||
agentSpec := AgentSpec{
|
||||
ID: reviewerAgentID,
|
||||
Name: "Review",
|
||||
Model: model,
|
||||
Workspace: parent.Workspace,
|
||||
SystemPrompt: spec.SystemPrompt,
|
||||
MaxTurns: maxTurns,
|
||||
ContextWindow: ctxWindow,
|
||||
}
|
||||
rt, err := BuildRuntime(deps, inputs, agentSpec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rt, nil
|
||||
}
|
||||
|
||||
// snapshotSession returns a fresh *session.Session that contains a
|
||||
// copy of parent's current View. The clone preserves AgentID (so the
|
||||
// reviewer sees the same agent identity in entries) but rewrites Key
|
||||
// to "review-" + parent.Key — this ensures the reviewer's compaction
|
||||
// locks don't contend with the parent's (locks are keyed on
|
||||
// AgentID/Key) and that the JSONL store (if attached) writes to a
|
||||
// separate file.
|
||||
//
|
||||
// The clone is in-memory only; SetStore is NOT called on it.
|
||||
//
|
||||
// Mirror of inheritParentHistory in subagent.go: the FIRST inherited
|
||||
// entry gets its ParentID cleared so the fresh session's empty leaf
|
||||
// doesn't leave a dangling pointer; subsequent entries chain naturally
|
||||
// as Append re-stitches them.
|
||||
func snapshotSession(parent *session.Session) *session.Session {
|
||||
clone := session.NewSession(parent.AgentID, "review-"+parent.Key)
|
||||
view := parent.View()
|
||||
for i, e := range view {
|
||||
if i == 0 {
|
||||
e.ParentID = ""
|
||||
}
|
||||
clone.Append(e)
|
||||
}
|
||||
return clone
|
||||
}
|
||||
Reference in new issue
Block a user