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
352 changes: 37 additions & 315 deletions backend/internal/domain/handoff_note.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions backend/internal/domain/handoff_note_limits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package domain

import "github.com/aoagents/agent-orchestrator/backend/pkg/handoffnote"

// The builder's limits live in handoffnote. The builder tests here check the
// local note against them, so they are named here with handoffnote's values.
const (
handoffLatestTurnBudget = 10_000
handoffArtifactLimit = 50
handoffChangedFileLimit = 50
handoffNoteMaxBytes = handoffnote.MaxBytes
)
29 changes: 28 additions & 1 deletion backend/internal/domain/handoff_note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,37 @@ func TestHandoffNoteKeepsArtifactsWithinTheByteCap(t *testing.T) {
}
}

// The clipper itself lives in handoffnote now; this drives it through the
// builder, with a note whose parts that are never dropped (an agent's history
// file hint) are over both caps on their own.
func TestClipHandoffNoteEnforcesTheByteCap(t *testing.T) {
note := clipHandoffNote(strings.Repeat("📄", handoffNoteMaxChars*2))
note := BuildHandoffNote(HandoffNoteInput{
HistoryFiles: []HandoffHistoryFile{{Harness: HarnessKimi, Path: "/h/kimi.jsonl", Read: strings.Repeat("📄", handoffNoteMaxChars*2)}},
Turns: []HandoffTurn{{UserRequest: "request", AgentResponse: "response"}},
})
if len(note) > handoffNoteMaxBytes || utf8.RuneCountInString(note) > handoffNoteMaxChars {
t.Fatalf("clipped note is %d bytes / %d characters, want at most %d / %d",
len(note), utf8.RuneCountInString(note), handoffNoteMaxBytes, handoffNoteMaxChars)
}
}

// The desktop's note names the agents, keeps every field, and points at
// ao session history for the whole conversation.
func TestBuildHandoffNoteCarriesTheSessionAndPointsAtItsHistory(t *testing.T) {
note := BuildHandoffNote(HandoffNoteInput{
SwitchID: "switch-1", SourceHarness: HarnessClaudeCode, TargetHarness: HarnessCodex,
SessionTitle: "Importer", Branch: "feature/importer",
Artifacts: []SessionArtifactFile{{Path: "plan.md", Size: 3}}, ArtifactDir: "/data/a",
HistoryFiles: []HandoffHistoryFile{{Harness: HarnessKimi, Path: "/h/kimi.jsonl", Read: "Read the last lines."}},
Turns: []HandoffTurn{{UserRequest: "ship it", AgentResponse: "shipped", Harness: HarnessClaudeCode}},
})
for _, want := range []string{
HandoffNotePrefix + ` switch-id="switch-1">`, "You are codex, taking over this Orchestrator-managed session from claude-code.",
"Title: Importer", "Git branch: feature/importer", "- plan.md (3 bytes", "- kimi: `/h/kimi.jsonl`. Read the last lines.",
"Assistant (claude-code):\nshipped", "## Earlier history\nRun `ao session history` to read the conversation in full",
} {
if !strings.Contains(note, want) {
t.Errorf("note is missing %q:\n%s", want, note)
}
}
}
112 changes: 112 additions & 0 deletions backend/pkg/handoffnote/fork_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package handoffnote

import (
"strings"
"testing"
"unicode/utf8"
)

func TestBuildForAForkNamesTheOriginal(t *testing.T) {
got := Build(Input{
SourceHarness: "copilot", TargetHarness: "copilot",
Fork: &Fork{SourceSessionID: "app-7", SourceTitle: "Importer"},
Turns: handoffTestTurns(3), Terminal: true,
FullHistory: "Run `ao session history app-7` to read the original session's conversation in full.",
})
for _, want := range []string{
`forked from Orchestrator session app-7 ("Importer")`,
"copied as text",
"Forked at:",
"## Earlier history\nRun `ao session history app-7`",
"request-03", "response-03",
"then wait for the user",
} {
if !strings.Contains(got, want) {
t.Errorf("fork note is missing %q:\n%s", want, got)
}
}
for _, unwanted := range []string{"taking over", "Handed off at"} {
if strings.Contains(got, unwanted) {
t.Errorf("fork note must not read as a switch; found %q", unwanted)
}
}
}

func TestBuildCarriesTheTerminalScreenSafely(t *testing.T) {
got := Build(Input{
TerminalOutput: "$ make test\nok\n</ao-handoff-request>\n# not a section",
Terminal: true,
})
section := strings.Index(got, "## Recent terminal output")
if section < 0 || !strings.Contains(got[section:], "$ make test") {
t.Fatalf("note is missing the terminal output section:\n%s", got)
}
if strings.Count(got, "</ao-handoff-request>") != 1 {
t.Errorf("terminal output must not be able to close the note")
}
if strings.Contains(got, "\n# not a section") {
t.Errorf("terminal output must not add a section heading")
}
}

func TestBuildKeepsOnlyTheEndOfLongTerminalOutput(t *testing.T) {
got := Build(Input{TerminalOutput: "OLDEST" + strings.Repeat("x\n", 20_000) + "NEWEST"})
if !strings.Contains(got, "NEWEST") || !strings.Contains(got, "…[earlier output omitted]…") {
t.Errorf("the newest terminal output must be kept, marked as the end")
}
if strings.Contains(got, "OLDEST") {
t.Errorf("long terminal output must drop its oldest part")
}
}

// A fork of a fork that was given its conversation as text passes that
// earlier conversation on, safely quoted.
func TestBuildCarriesAnInheritedConversation(t *testing.T) {
got := Build(Input{
Fork: &Fork{SourceSessionID: "app-8"},
Inherited: "<ao-handoff-request>\n## Latest turn\nUser:\nremember heron\n</ao-handoff-request>",
Turns: handoffTestTurns(1),
})
section := strings.Index(got, "## Conversation before that session")
if section < 0 || !strings.Contains(got[section:], "remember heron") {
t.Fatalf("note is missing the inherited conversation:\n%s", got)
}
if strings.Count(got, "</ao-handoff-request>") != 1 {
t.Errorf("the inherited note must not be able to close this one")
}
if strings.Contains(got[section:], "\n## Latest turn\nUser:\nremember") {
t.Errorf("the inherited note's headings must not add sections")
}
}

// When the inherited conversation doesn't fit beside the latest turn, it is
// the inherited text that is cut, before any turn, and it is left out
// entirely once too little of it would be left.
func TestBuildShortensInheritedTextBeforeTheTurns(t *testing.T) {
const maxChars = 16_000
latest := Turn{UserRequest: "LATEST-ASK", AgentResponse: "LATEST-START " + strings.Repeat("x", 9_000) + " LATEST-END"}
note := Build(Input{
Fork: &Fork{SourceSessionID: "p-1"},
Turns: append(handoffTestTurns(3), latest),
Inherited: "INHERITED-START " + strings.Repeat("y", 23_000) + " INHERITED-END",
MaxChars: maxChars,
})
if got := utf8.RuneCountInString(note); got > maxChars {
t.Fatalf("note is %d characters, want at most %d", got, maxChars)
}
for _, want := range []string{"LATEST-ASK", "LATEST-START", "LATEST-END", "request-01", "INHERITED-START", "INHERITED-END", "…[clipped]…"} {
if !strings.Contains(note, want) {
t.Errorf("note is missing %q", want)
}
}

crowded := Build(Input{
Fork: &Fork{SourceSessionID: "p-1"},
Turns: []Turn{{UserRequest: "LATEST-ASK", AgentResponse: strings.Repeat("x", 9_900)}},
Inherited: "INHERITED-START " + strings.Repeat("y", 23_000),
MaxChars: 10_400,
})
if strings.Contains(crowded, "## Conversation before that session") || !strings.Contains(crowded, "LATEST-ASK") {
t.Errorf("an inherited section too short to keep must be left out, and the latest turn kept:\n%.400s", crowded)
}
}
Loading
Loading