Merge: short essential memory per stage; full history on demand
This commit is contained in:
@@ -88,7 +88,7 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ
|
||||
}
|
||||
// Mandatory runtime evidence must not disappear behind hundreds of private
|
||||
// captures/public deliverables in the bounded inventory.
|
||||
for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} {
|
||||
for _, name := range []string{"input.md", "memory.md", "history.md", "output.md", "session.html"} {
|
||||
full := path.Join(rel, name)
|
||||
if info, err := root.Lstat(full); err == nil && info.Mode().IsRegular() && info.Mode()&fs.ModeSymlink == 0 && info.Size() <= maxArtifactFileBytes {
|
||||
add(full, name, info)
|
||||
|
||||
@@ -972,7 +972,7 @@ type runState struct {
|
||||
artifactDirs []string
|
||||
// prompt is the original run prompt; lastOutput is the previous visit's
|
||||
// markdown and turns is every completed turn in order. Together they render
|
||||
// the next model visit's input.md and memory.md.
|
||||
// the next model visit's input.md, memory.md and history.md.
|
||||
prompt string
|
||||
lastOutput string
|
||||
turns []turn.MemoryEntry
|
||||
@@ -1018,7 +1018,7 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS
|
||||
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
|
||||
}
|
||||
state.completed++
|
||||
state.turns = append(state.turns, turn.MemoryEntry{Stage: step.Stage, Visit: state.visits[step.Stage], Output: turnOutput(step.Output), PublicFiles: added})
|
||||
state.turns = append(state.turns, turn.MemoryEntry{Stage: step.Stage, Visit: state.visits[step.Stage], Outcome: step.Outcome, Output: turnOutput(step.Output), PublicFiles: added})
|
||||
state.lastOutput = turnOutput(step.Output)
|
||||
}
|
||||
// Seed the UI-surface accumulator from the persisted evidence so a
|
||||
@@ -1062,7 +1062,7 @@ func rebuildRetryState(detail *protocol.RunDetail, failed protocol.RunStep, hand
|
||||
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
|
||||
}
|
||||
state.completed++
|
||||
state.turns = append(state.turns, turn.MemoryEntry{Stage: step.Stage, Visit: state.visits[step.Stage], Output: turnOutput(step.Output), PublicFiles: added})
|
||||
state.turns = append(state.turns, turn.MemoryEntry{Stage: step.Stage, Visit: state.visits[step.Stage], Outcome: step.Outcome, Output: turnOutput(step.Output), PublicFiles: added})
|
||||
state.lastOutput = turnOutput(step.Output)
|
||||
}
|
||||
state.recordPersistedStep(step)
|
||||
@@ -1735,8 +1735,8 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
return
|
||||
}
|
||||
useAgent := stage.EffectiveType(def.Type) == stage.ModelType && (len(tools) > 0 || !explicitTools)
|
||||
// Every model visit gets a file-based turn: input.md and memory.md are
|
||||
// written before execution, output.md after it. Creation is part of the
|
||||
// Every model visit gets a file-based turn: input.md, memory.md and
|
||||
// history.md are written before execution, output.md after it. Creation is part of the
|
||||
// protocol, so an unusable turn directory fails the run instead of
|
||||
// silently degrading to the retired JSON envelope.
|
||||
artifactsDir := ""
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"git.a-wan.net/alan/pipelines/internal/turn"
|
||||
)
|
||||
|
||||
// TestFeedbackLoopRevisitGetsReviewerHandoffInEssentialMemory runs a real
|
||||
// 4-stage pipeline with a review loop (plan, code, review -> changes -> code,
|
||||
// review, land) and checks what each visit's memory.md and history.md hold:
|
||||
// memory.md stays within its cap and leads the re-entered code stage with the
|
||||
// reviewer's handoff, while history.md keeps every earlier turn in full.
|
||||
func TestFeedbackLoopRevisitGetsReviewerHandoffInEssentialMemory(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "loop.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
source := strings.Replace(pipeline.Template([]string{"plan", "code", "review", "land"}, true), "success -> land\n", "success -> land\nchanges -> code\n", 1)
|
||||
if _, err := store.Submit(ctx, "loop", source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := store.List(ctx)
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatalf("submit/list: %+v %v", items, err)
|
||||
}
|
||||
runner, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
filler := strings.Repeat("Long log line with commands, output and reasoning.\n", 200)
|
||||
type seen struct{ memory, history string }
|
||||
visits := map[string][]seen{}
|
||||
reviews := 0
|
||||
runner.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) {
|
||||
memory, err := os.ReadFile(def.Turn.Memory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
history, err := os.ReadFile(def.Turn.History)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
visits[def.Key] = append(visits[def.Key], seen{string(memory), string(history)})
|
||||
body := "# handoff\n\n" + def.Key + " handoff\n\n# log\n\n" + def.Key + " full log\n" + filler
|
||||
outcome := "success"
|
||||
if def.Key == "review" {
|
||||
reviews++
|
||||
if reviews == 1 {
|
||||
outcome = "changes"
|
||||
body = "# notes\n\n" + filler + "\n# handoff\n\nFINDING: lock leaks on the error path in cache.go.\n"
|
||||
}
|
||||
}
|
||||
return stage.Result{Outcome: outcome, Output: stage.Values{"markdown": body}}, nil
|
||||
}
|
||||
run, err := runner.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "loop", Workspace: t.TempDir(), Input: map[string]any{"prompt": "Add a cache."}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner.wg.Wait()
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != "succeeded" || len(detail.Steps) != 6 {
|
||||
t.Fatalf("run = %q steps=%d: %+v", detail.Status, len(detail.Steps), detail.RunSummary)
|
||||
}
|
||||
for key, list := range visits {
|
||||
for i, visit := range list {
|
||||
if len(visit.memory) > turn.EssentialCap {
|
||||
t.Fatalf("%s visit %d memory.md is %d bytes, over the cap", key, i+1, len(visit.memory))
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(visits["code"]) != 2 {
|
||||
t.Fatalf("code ran %d times, want 2", len(visits["code"]))
|
||||
}
|
||||
revisit := visits["code"][1]
|
||||
for _, want := range []string{
|
||||
"Add a cache.",
|
||||
"## Previous stage: review · visit 1 · outcome changes",
|
||||
"FINDING: lock leaks on the error path in cache.go.",
|
||||
"- code · visit 1 · outcome success (this stage's previous visit; detail in history.md)",
|
||||
} {
|
||||
if !strings.Contains(revisit.memory, want) {
|
||||
t.Fatalf("re-entered code memory.md missing %q:\n%s", want, revisit.memory)
|
||||
}
|
||||
}
|
||||
if strings.Contains(revisit.memory, "Long log line") {
|
||||
t.Fatalf("re-entered code memory.md carried a full log:\n%s", revisit.memory)
|
||||
}
|
||||
for _, want := range []string{"## plan · visit 1", "plan full log", "## code · visit 1", "code full log", "## review · visit 1", "FINDING: lock leaks"} {
|
||||
if !strings.Contains(revisit.history, want) {
|
||||
t.Fatalf("re-entered code history.md missing %q", want)
|
||||
}
|
||||
}
|
||||
land := visits["land"][0]
|
||||
if !strings.Contains(land.memory, "## Previous stage: review · visit 2 · outcome success") || !strings.Contains(land.memory, "review handoff") {
|
||||
t.Fatalf("land memory.md lacks the last review handoff:\n%s", land.memory)
|
||||
}
|
||||
if len(land.history) < 10*len(land.memory) {
|
||||
t.Fatalf("land history %d bytes vs memory %d bytes: essential memory is not compact", len(land.history), len(land.memory))
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func TestTurnsAccumulateMemoryAndListFilesAsArtifacts(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var secondInput, secondMemory string
|
||||
var secondInput, secondMemory, secondHistory string
|
||||
runner.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) {
|
||||
if def.Turn.Input == "" || def.Turn.Memory == "" || def.Turn.Output == "" || def.Turn.Private == "" || def.Turn.Public == "" {
|
||||
t.Fatalf("runner did not provide the turn paths: %+v", def.Turn)
|
||||
@@ -63,6 +63,14 @@ func TestTurnsAccumulateMemoryAndListFilesAsArtifacts(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondMemory = string(data)
|
||||
if def.Turn.History == "" {
|
||||
t.Fatalf("runner did not provide the history path: %+v", def.Turn)
|
||||
}
|
||||
data, err = os.ReadFile(def.Turn.History)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondHistory = string(data)
|
||||
if _, err := os.Stat(filepath.Join(def.Turn.Private, "secret.txt")); !os.IsNotExist(err) {
|
||||
t.Fatalf("a later turn received an earlier private file: %v", err)
|
||||
}
|
||||
@@ -93,11 +101,19 @@ func TestTurnsAccumulateMemoryAndListFilesAsArtifacts(t *testing.T) {
|
||||
t.Fatalf("second input.md = %q, want the run task and the previous turn's output", secondInput)
|
||||
}
|
||||
for _, want := range []string{"## first · visit 1", "first body", "Files added to public/:\n- public/shared.txt"} {
|
||||
if !strings.Contains(secondHistory, want) {
|
||||
t.Fatalf("second history.md missing %q:\n%s", want, secondHistory)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"# Essential memory", "original task", "## Previous stage: first · visit 1 · outcome success", "first body", "Added to public/: public/shared.txt", "- first · visit 1 · outcome success"} {
|
||||
if !strings.Contains(secondMemory, want) {
|
||||
t.Fatalf("second memory.md missing %q:\n%s", want, secondMemory)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"input.md", "memory.md", "output.md"} {
|
||||
if len(secondMemory) > turn.EssentialCap {
|
||||
t.Fatalf("memory.md is %d bytes, over the %d cap", len(secondMemory), turn.EssentialCap)
|
||||
}
|
||||
for _, name := range []string{"input.md", "memory.md", "history.md", "output.md"} {
|
||||
if _, ok := artifactNamed(detail.Steps[1].Artifacts, name); !ok {
|
||||
t.Fatalf("second visit did not list %s: %+v", name, detail.Steps[1].Artifacts)
|
||||
}
|
||||
@@ -125,7 +141,7 @@ func TestRebuildRunStateRestoresPublicFileLists(t *testing.T) {
|
||||
},
|
||||
}
|
||||
state := rebuildRunState(detail, protocol.RunStep{Sequence: 2, Stage: "second", Output: map[string]any{"markdown": "second body\n"}})
|
||||
memory := turn.RenderMemory(state.turns)
|
||||
memory := turn.RenderHistory(state.turns)
|
||||
for _, want := range []string{
|
||||
"## first · visit 1",
|
||||
"Files added to public/:\n- public/a.png",
|
||||
@@ -157,7 +173,7 @@ func TestRebuildRetryStateRestoresPublicFileLists(t *testing.T) {
|
||||
"prefix_provenance": []any{map[string]any{"stage": "first", "sequence": float64(1)}},
|
||||
}
|
||||
state := rebuildRetryState(detail, detail.Steps[1], handoff)
|
||||
memory := turn.RenderMemory(state.turns)
|
||||
memory := turn.RenderHistory(state.turns)
|
||||
if !strings.Contains(memory, "Files added to public/:\n- public/report.pdf") {
|
||||
t.Fatalf("retry memory lost the inherited public file list:\n%s", memory)
|
||||
}
|
||||
@@ -248,7 +264,7 @@ func TestRetryCarriesPublicFilesAcrossAttempts(t *testing.T) {
|
||||
t.Fatalf("transitive retry did not carry the public file: %q %v", data, err)
|
||||
}
|
||||
paths := runner.turnPaths(second.ID, 2, "b")
|
||||
memory, err := os.ReadFile(paths.MemoryPath)
|
||||
memory, err := os.ReadFile(paths.HistoryPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -40,14 +40,16 @@ func stageTurnPaths(paths turn.Paths) stage.TurnPaths {
|
||||
Dir: paths.TurnDir,
|
||||
Input: paths.InputPath,
|
||||
Memory: paths.MemoryPath,
|
||||
History: paths.HistoryPath,
|
||||
Output: paths.OutputPath,
|
||||
Private: paths.PrivateDir,
|
||||
Public: paths.PublicDir,
|
||||
}
|
||||
}
|
||||
|
||||
// prepareTurn creates the visit layout and writes the model's input.md and
|
||||
// memory.md before the visit starts. A failure here is a run failure: the file
|
||||
// prepareTurn creates the visit layout and writes the model's input.md,
|
||||
// memory.md (essential memory, capped at turn.EssentialCap) and history.md (every
|
||||
// completed turn in full) before the visit starts. A failure here is a run failure: the file
|
||||
// protocol is the stage contract, not best-effort evidence.
|
||||
//
|
||||
// retryInput is the complete merged input for a selected-stage retry and nil for
|
||||
@@ -59,12 +61,23 @@ func (r *runner) prepareTurn(paths turn.Paths, state *runState, retryInput stage
|
||||
return err
|
||||
}
|
||||
input := turnInputWithRetry(state.turnInput(), retryInput)
|
||||
memory := turn.RenderMemory(state.turns)
|
||||
if err := os.WriteFile(paths.InputPath, []byte(input), 0o600); err != nil {
|
||||
return fmt.Errorf("write %s: %w", paths.InputPath, err)
|
||||
}
|
||||
if err := os.WriteFile(paths.MemoryPath, []byte(memory), 0o600); err != nil {
|
||||
return fmt.Errorf("write %s: %w", paths.MemoryPath, err)
|
||||
history := turn.RenderHistory(state.turns)
|
||||
memory := turn.RenderEssential(turn.Essential{
|
||||
Task: state.prompt,
|
||||
StageKey: paths.StageKey,
|
||||
Entries: state.turns,
|
||||
ChangedFiles: state.capturedList(),
|
||||
InputPath: paths.InputPath,
|
||||
HistoryPath: paths.HistoryPath,
|
||||
})
|
||||
for _, file := range []struct{ path, content string }{
|
||||
{paths.InputPath, input},
|
||||
{paths.MemoryPath, memory},
|
||||
{paths.HistoryPath, history},
|
||||
} {
|
||||
if err := os.WriteFile(file.path, []byte(file.content), 0o600); err != nil {
|
||||
return fmt.Errorf("write %s: %w", file.path, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -126,6 +139,7 @@ func (s *runState) recordTurn(step protocol.RunStep, publicFiles []string) {
|
||||
s.turns = append(s.turns, turn.MemoryEntry{
|
||||
Stage: step.Stage,
|
||||
Visit: s.visits[step.Stage],
|
||||
Outcome: step.Outcome,
|
||||
Output: output,
|
||||
PublicFiles: publicFiles,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user