feat: dedup and format the output
This commit is contained in:
8 files changed
+153
-54
No files matched your search
@@ -1,6 +1,6 @@
|
||||
// Package archive optionally saves a local copy of each run's fetched data
|
||||
// and analysis, for debugging prompt/parsing issues without reproducing a
|
||||
// live run.
|
||||
// Package archive saves a local copy of each run's fetched data and
|
||||
// analysis. It doubles as the source of the previous run's report, which
|
||||
// the prompt uses to dedup successive briefings against each other.
|
||||
package archive
|
||||
|
||||
import (
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -31,13 +32,9 @@ type Run struct {
|
||||
// path. Callers should treat a non-nil error as non-fatal — archiving is a
|
||||
// debugging aid, not part of the report pipeline.
|
||||
func Save(cfg config.ArchiveConfig, run Run, now time.Time) (string, error) {
|
||||
base := cfg.Dir
|
||||
if base == "" {
|
||||
base = defaultDir
|
||||
}
|
||||
base, err := expandHome(base)
|
||||
base, err := resolveDir(cfg)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("archive: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
dir := filepath.Join(base, now.UTC().Format("20060102T150405Z"))
|
||||
@@ -70,6 +67,59 @@ func Save(cfg config.ArchiveConfig, run Run, now time.Time) (string, error) {
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
// LoadLatest reads back the most recent previously-archived run's
|
||||
// report.md, for the prompt to dedup the next briefing against. Returns
|
||||
// ("", nil) if no prior run exists yet (e.g. first-ever run) — callers
|
||||
// should treat that as "no previous report" rather than an error.
|
||||
func LoadLatest(cfg config.ArchiveConfig) (string, error) {
|
||||
base, err := resolveDir(cfg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(base)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
return "", fmt.Errorf("archive: list %q: %w", base, err)
|
||||
}
|
||||
|
||||
var runDirs []string
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
runDirs = append(runDirs, e.Name())
|
||||
}
|
||||
}
|
||||
if len(runDirs) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
sort.Strings(runDirs) // timestamp-named dirs sort chronologically
|
||||
latest := runDirs[len(runDirs)-1]
|
||||
|
||||
data, err := os.ReadFile(filepath.Join(base, latest, "report.md"))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
return "", fmt.Errorf("archive: read previous report: %w", err)
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// resolveDir returns cfg.Dir (or defaultDir if empty) with "~" expanded.
|
||||
func resolveDir(cfg config.ArchiveConfig) (string, error) {
|
||||
base := cfg.Dir
|
||||
if base == "" {
|
||||
base = defaultDir
|
||||
}
|
||||
base, err := expandHome(base)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("archive: %w", err)
|
||||
}
|
||||
return base, nil
|
||||
}
|
||||
|
||||
// expandHome replaces a leading "~" with the current user's home directory.
|
||||
func expandHome(path string) (string, error) {
|
||||
if path != "~" && !strings.HasPrefix(path, "~/") {
|
||||
|
||||
Reference in new issue
Block a user