diff --git a/internal/app/agentworker/persistent_runner.go b/internal/app/agentworker/persistent_runner.go index b5d935b54..1d76eac94 100644 --- a/internal/app/agentworker/persistent_runner.go +++ b/internal/app/agentworker/persistent_runner.go @@ -1,10 +1,7 @@ package agentworker import ( - "bufio" "context" - "encoding/json" - "errors" "fmt" "os" "strings" @@ -30,32 +27,18 @@ type memoryRecord struct { func readProjectMemory(path string) (string, error) { const maxEntries = 100 - f, err := os.Open(path) - if errors.Is(err, os.ErrNotExist) { - return "", nil - } - if err != nil { - return "", err - } - defer f.Close() - var entries []string - scanner := bufio.NewScanner(f) - scanner.Buffer(make([]byte, 64*1024), 8*1024*1024) - for scanner.Scan() { - var record memoryRecord - if err := json.Unmarshal(scanner.Bytes(), &record); err != nil { - return "", fmt.Errorf("decode memory record: %w", err) - } + err := storage.ReadJSONLines(path, func(record memoryRecord) error { if strings.TrimSpace(record.Content) != "" { entries = append(entries, "- "+record.Content) if len(entries) > maxEntries { entries = entries[len(entries)-maxEntries:] } } - } - if err := scanner.Err(); err != nil { - return "", fmt.Errorf("scan memory records: %w", err) + return nil + }) + if err != nil { + return "", fmt.Errorf("read project memory: %w", err) } if len(entries) == 0 { return "", nil diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 01ab9c2b0..6ab4591b6 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -27,6 +27,7 @@ package storage import ( + "bufio" "context" "encoding/json" "errors" @@ -91,6 +92,42 @@ func AppendJSONLine(path string, value any) error { return nil } +// ReadJSONLines decodes each line of the JSONL file at path as a T, calling +// fn for each. A path that does not exist yet is not an error -- no log +// written and an empty log mean the same thing to a caller -- so fn is +// simply never called. A malformed line, or an error fn itself returns, +// stops the scan and is returned wrapped. +// +// This is AppendJSONLine's read-side counterpart, kept in the package that +// owns the format: see organisation.md's "a package owns its own format end +// to end." +func ReadJSONLines[T any](path string, fn func(T) error) error { + f, err := os.Open(path) + if errors.Is(err, os.ErrNotExist) { + return nil + } + if err != nil { + return fmt.Errorf("open JSONL file: %w", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Buffer(make([]byte, 64*1024), 8*1024*1024) + for scanner.Scan() { + var record T + if err := json.Unmarshal(scanner.Bytes(), &record); err != nil { + return fmt.Errorf("decode JSONL record: %w", err) + } + if err := fn(record); err != nil { + return fmt.Errorf("handle JSONL record: %w", err) + } + } + if err := scanner.Err(); err != nil { + return fmt.Errorf("scan JSONL file: %w", err) + } + return nil +} + // StagePlugin writes a bundled plugin unless persistent project storage // already contains a plugin with the same filename. Persistent copies are // operator-owned overrides and therefore win over daemon-staged source. diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index cc2728991..b6e52754e 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -3,6 +3,7 @@ package storage import ( "context" "encoding/json" + "errors" "fmt" "go/format" "io" @@ -362,6 +363,80 @@ func TestAppendJSONLineAppendsCompleteRecords(t *testing.T) { } } +type jsonlTestRecord struct { + Stage int `json:"stage"` +} + +// The reader is AppendJSONLine's counterpart: a value written by one must +// come back unchanged through the other, in the order written. +func TestReadJSONLinesRoundTripsAppendJSONLine(t *testing.T) { + path := filepath.Join(t.TempDir(), "session.jsonl") + for _, v := range []jsonlTestRecord{{Stage: 1}, {Stage: 2}, {Stage: 3}} { + if err := AppendJSONLine(path, v); err != nil { + t.Fatal(err) + } + } + + var got []jsonlTestRecord + if err := ReadJSONLines(path, func(v jsonlTestRecord) error { + got = append(got, v) + return nil + }); err != nil { + t.Fatal(err) + } + + want := []jsonlTestRecord{{Stage: 1}, {Stage: 2}, {Stage: 3}} + if len(got) != len(want) { + t.Fatalf("records = %v, want %v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("records = %v, want %v", got, want) + } + } +} + +// A log that has never been written is not an error: no memory yet and no +// file yet mean the same thing to a caller. +func TestReadJSONLinesMissingFileIsNotAnError(t *testing.T) { + path := filepath.Join(t.TempDir(), "never-written.jsonl") + called := false + if err := ReadJSONLines(path, func(jsonlTestRecord) error { called = true; return nil }); err != nil { + t.Fatalf("ReadJSONLines: %v", err) + } + if called { + t.Error("fn was called for a file that does not exist") + } +} + +// A malformed line must fail loudly rather than silently skip a record -- +// project memory is meant to be trusted context for later sessions, so a +// corrupt entry must not vanish unnoticed. +func TestReadJSONLinesMalformedLineIsAnError(t *testing.T) { + path := filepath.Join(t.TempDir(), "session.jsonl") + if err := os.WriteFile(path, []byte("not json\n"), 0o600); err != nil { + t.Fatal(err) + } + if err := ReadJSONLines(path, func(jsonlTestRecord) error { return nil }); err == nil { + t.Fatal("expected an error for a malformed line") + } +} + +// fn's own error must stop the scan and propagate, so a caller can bail out +// early (e.g. once it has enough records) without ReadJSONLines masking a +// real failure as success. +func TestReadJSONLinesPropagatesCallbackError(t *testing.T) { + path := filepath.Join(t.TempDir(), "session.jsonl") + if err := AppendJSONLine(path, jsonlTestRecord{Stage: 1}); err != nil { + t.Fatal(err) + } + sentinel := errors.New("stop here") + err := ReadJSONLines(path, func(jsonlTestRecord) error { return sentinel }) + if !errors.Is(err, sentinel) { + t.Fatalf("err = %v, want it to wrap the callback's own error", err) + } +} + func TestStagePluginPreservesPersistentOverride(t *testing.T) { dir := t.TempDir() if err := StagePlugin(dir, "check.go", []byte("bundled")); err != nil {