Build creates a multi-agent handoff artifact from git + daemon state.
(root string, opts BuildOptions)
| 58 | |
| 59 | // Build creates a multi-agent handoff artifact from git + daemon state. |
| 60 | func Build(root string, opts BuildOptions) (*Artifact, error) { |
| 61 | absRoot, err := filepath.Abs(root) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | state := opts.State |
| 67 | if state == nil { |
| 68 | state = watch.ReadState(absRoot) |
| 69 | } |
| 70 | |
| 71 | fileCount := resolveRepoFileCount(absRoot, state) |
| 72 | opts = normalizeOptions(opts, fileCount) |
| 73 | |
| 74 | branch, err := gitCurrentBranch(absRoot) |
| 75 | if err != nil { |
| 76 | return nil, fmt.Errorf("failed to read git branch: %w", err) |
| 77 | } |
| 78 | |
| 79 | entries, diffErr := collectChangedEntries(absRoot, opts.BaseRef) |
| 80 | if diffErr != nil { |
| 81 | return nil, diffErr |
| 82 | } |
| 83 | changedAll := entryPaths(entries) |
| 84 | |
| 85 | recentEvents := summarizeEvents(state, opts.Since, opts.MaxEvents) |
| 86 | if len(changedAll) == 0 && len(recentEvents) > 0 { |
| 87 | changedAll = changedFromEvents(recentEvents) |
| 88 | sort.Strings(changedAll) |
| 89 | entries = make([]changedEntry, 0, len(changedAll)) |
| 90 | for _, path := range changedAll { |
| 91 | entries = append(entries, changedEntry{Path: path, Status: "event"}) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | importers := dependencyImportersForHandoff(absRoot, state, fileCount) |
| 96 | riskFiles := summarizeRiskFiles(changedAll, importers, opts.MaxRisk) |
| 97 | selectedPaths := prioritizeChangedPaths(changedAll, riskFiles, opts.MaxChanged) |
| 98 | entries = selectEntries(entries, selectedPaths) |
| 99 | |
| 100 | changedStubs := buildFileStubs(absRoot, entries) |
| 101 | hubs := summarizeHubs(importers, opts.MaxHubs) |
| 102 | |
| 103 | nextSteps, openQuestions := deriveGuidance(selectedPaths, riskFiles, recentEvents, opts.BaseRef, state != nil, len(importers) > 0) |
| 104 | |
| 105 | prefix := PrefixSnapshot{ |
| 106 | FileCount: fileCount, |
| 107 | Hubs: nonNilHubs(hubs), |
| 108 | } |
| 109 | delta := DeltaSnapshot{ |
| 110 | Changed: nonNilStubs(changedStubs), |
| 111 | RiskFiles: nonNilRiskFiles(riskFiles), |
| 112 | RecentEvents: nonNilEvents(recentEvents), |
| 113 | NextSteps: nonNilStrings(nextSteps), |
| 114 | OpenQuestions: nonNilStrings(openQuestions), |
| 115 | } |
| 116 | |
| 117 | prefixHash, prefixBytes, err := hashCanonical(prefix) |