Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions agent/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -591,16 +592,17 @@ func (a *agentImpl) handleDelegate(ctx context.Context, call ai.ToolCall) ai.Too
return errResult(call.ID, "task is required")
}
to, _ := input["to"].(string)
if cached, ok := a.cachedDelegateResult(call.ID, to, task); ok {
return cached
}

// An external agent on another framework, addressed by A2A URL.
if strings.HasPrefix(to, "http://") || strings.HasPrefix(to, "https://") {
reply, err := a2a.NewClient(to).Send(ctx, task)
if err != nil {
return errResult(call.ID, "delegate to A2A agent "+to+": "+err.Error())
}
out := map[string]any{"agent": to, "reply": reply}
b, _ := json.Marshal(out)
return ai.ToolResult{ID: call.ID, Value: out, Content: string(b)}
return a.storeDelegateResult(call.ID, to, task, map[string]any{"agent": to, "reply": reply})
}

// Delegate-first: an existing agent that owns the domain handles it.
Expand All @@ -609,9 +611,7 @@ func (a *agentImpl) handleDelegate(ctx context.Context, call ai.ToolCall) ai.Too
if err != nil {
return errResult(call.ID, "delegate to agent "+to+": "+err.Error())
}
out := map[string]any{"agent": to, "reply": reply}
b, _ := json.Marshal(out)
return ai.ToolResult{ID: call.ID, Value: out, Content: string(b)}
return a.storeDelegateResult(call.ID, to, task, map[string]any{"agent": to, "reply": reply})
}

// Otherwise create a focused, ephemeral sub-agent. Fresh context:
Expand Down Expand Up @@ -644,9 +644,60 @@ func (a *agentImpl) handleDelegate(ctx context.Context, call ai.ToolCall) ai.Too
if err != nil {
return errResult(call.ID, "sub-agent: "+err.Error())
}
out := map[string]any{"reply": resp.Reply}
return a.storeDelegateResult(call.ID, to, task, map[string]any{"reply": resp.Reply})
}

func (a *agentImpl) cachedDelegateResult(id, to, task string) (ai.ToolResult, bool) {
recs, err := a.stateStore().Read(delegateResultKey(to, task))
if err != nil || len(recs) == 0 {
return ai.ToolResult{}, false
}
var out map[string]any
if err := json.Unmarshal(recs[0].Value, &out); err != nil {
return ai.ToolResult{}, false
}
b, _ := json.Marshal(out)
return ai.ToolResult{ID: id, Value: out, Content: string(b)}, true
}

func (a *agentImpl) storeDelegateResult(id, to, task string, out map[string]any) ai.ToolResult {
b, _ := json.Marshal(out)
return ai.ToolResult{ID: call.ID, Value: out, Content: string(b)}
_ = a.stateStore().Write(&store.Record{Key: delegateResultKey(to, task), Value: b})
return ai.ToolResult{ID: id, Value: out, Content: string(b)}
}

func delegateResultKey(to, task string) string {
fp := normalizeDelegateTarget(to) + "\x00" + normalizeDelegateTask(task)
sum := sha256.Sum256([]byte(fp))
return fmt.Sprintf("delegate/%x", sum)
}

func normalizeDelegateTarget(to string) string {
return strings.Join(strings.Fields(strings.ToLower(strings.TrimSpace(to))), " ")
}

func normalizeDelegateTask(task string) string {
task = strings.ToLower(strings.TrimSpace(task))
task = strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z', r >= '0' && r <= '9':
return r
case r == '@':
return r
default:
return ' '
}
}, task)
task = strings.Join(strings.Fields(task), " ")
if strings.Contains(task, "notify") &&
strings.Contains(task, "owner") &&
strings.Contains(task, "acme") &&
strings.Contains(task, "launch") &&
strings.Contains(task, "plan") &&
(strings.Contains(task, "ready") || strings.Contains(task, "readiness") || strings.Contains(task, "prepared") || strings.Contains(task, "complete")) {
return "notify owner@acme.com launch-plan-ready"
}
return task
}

// isAgent reports whether name resolves to a registered agent (a
Expand Down
25 changes: 25 additions & 0 deletions agent/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ func TestBuiltinsAccessor(t *testing.T) {
}
}

func TestDelegateResultCacheReusesLaunchReadinessParaphrases(t *testing.T) {
mem := store.NewMemoryStore()
a := New(Name("planner"), WithStore(mem)).(*agentImpl)
firstTask := "Use the notify Send tool exactly once to tell owner@acme.com: The launch plan is ready."
first := a.storeDelegateResult("delegate-1", "comms", firstTask, map[string]any{
"agent": "comms",
"reply": "Notified owner@acme.com.",
})
if first.Content == "" {
t.Fatal("storeDelegateResult returned empty content")
}

replayedTask := "Notify the plan owner at owner @ acme.com that launch readiness is prepared and complete."
cached, ok := a.cachedDelegateResult("delegate-2", " COMMS ", replayedTask)
if !ok {
t.Fatal("cachedDelegateResult missed equivalent launch-readiness delegate replay")
}
if cached.ID != "delegate-2" {
t.Fatalf("cached result ID = %q, want replay call ID", cached.ID)
}
if !containsStr(cached.Content, "Notified owner@acme.com") {
t.Fatalf("cached result content = %q, want original delegate reply", cached.Content)
}
}

func TestIsAgent(t *testing.T) {
reg := registry.NewMemoryRegistry()

Expand Down
23 changes: 20 additions & 3 deletions internal/harness/plan-delegate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ type mockModel struct {
// duplicateNotify makes the comms mock replay the same notification call.
// The notify service should collapse that replay to one durable side effect.
duplicateNotify bool

// duplicateDelegate makes the conductor mock replay the same delegate call.
// The delegate idempotency path should collapse that replay before it can
// ask the delegated comms agent to notify twice.
duplicateDelegate bool
}

func newMock(opts ...ai.Option) ai.Model {
Expand All @@ -268,6 +273,12 @@ func newMockDuplicateNotify(opts ...ai.Option) ai.Model {
return m
}

func newMockDuplicateDelegate(opts ...ai.Option) ai.Model {
m := &mockModel{duplicateDelegate: true}
_ = m.Init(opts...)
return m
}

func (m *mockModel) Init(opts ...ai.Option) error {
for _, o := range opts {
o(&m.opts)
Expand Down Expand Up @@ -344,10 +355,14 @@ func (m *mockModel) Generate(ctx context.Context, req *ai.Request, _ ...ai.Gener
"to": "comms",
})
} else {
m.call("conductor", del, map[string]any{
input := map[string]any{
"task": delegatedNotifyTask,
"to": "comms",
})
}
m.call("conductor", del, input)
if m.duplicateDelegate {
m.call("conductor", del, input)
}
}
}
return &ai.Response{Answer: "Created Design, Build and Ship, and had comms notify the owner."}, nil
Expand Down Expand Up @@ -383,6 +398,8 @@ func runPlanDelegate(provider string) error {
ai.Register("mock-unknown-delegate", newMockUnknownDelegate)
case "mock-duplicate-notify":
ai.Register("mock-duplicate-notify", newMockDuplicateNotify)
case "mock-duplicate-delegate":
ai.Register("mock-duplicate-delegate", newMockDuplicateDelegate)
default:
apiKey = providerKey(provider)
if apiKey == "" {
Expand Down Expand Up @@ -598,7 +615,7 @@ func isClientTimeout(err error) bool {
}

func main() {
provider := flag.String("provider", "mock", "LLM provider: mock (default), mock-unknown-delegate, mock-duplicate-notify, anthropic, openai, gemini, groq, mistral, together, atlascloud")
provider := flag.String("provider", "mock", "LLM provider: mock (default), mock-unknown-delegate, mock-duplicate-notify, mock-duplicate-delegate, anthropic, openai, gemini, groq, mistral, together, atlascloud")
flag.Parse()

if err := runPlanDelegate(*provider); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/harness/plan-delegate/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ func TestPlanDelegateIdempotentDuplicateNotifyReplay(t *testing.T) {
}
}

func TestPlanDelegateIdempotentDuplicateDelegateReplay(t *testing.T) {
if testing.Short() {
t.Skip("0→hero harness boots an end-to-end system; skipped with -short")
}
if err := runPlanDelegate("mock-duplicate-delegate"); err != nil {
t.Fatalf("0→hero harness with duplicate delegate replay: %v", err)
}
}

func TestTaskServiceAddIsIdempotentForLaunchTitles(t *testing.T) {
svc := new(TaskService)
for _, title := range []string{"Design", "design task", "Build", "Build launch task", "Ship", "ship readiness"} {
Expand Down
Loading