-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostcommit.go
More file actions
158 lines (135 loc) · 4.19 KB
/
postcommit.go
File metadata and controls
158 lines (135 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package hooks
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"time"
"github.com/partio-io/cli/internal/agent/claude"
"github.com/partio-io/cli/internal/attribution"
"github.com/partio-io/cli/internal/checkpoint"
"github.com/partio-io/cli/internal/config"
"github.com/partio-io/cli/internal/git"
)
// PostCommit runs post-commit hook logic.
func (r *Runner) PostCommit() error {
slog.Debug("post-commit hook running")
return runPostCommit(r.repoRoot, r.cfg)
}
func runPostCommit(repoRoot string, cfg config.Config) error {
// Read pre-commit state
stateFile := filepath.Join(repoRoot, config.PartioDir, "state", "pre-commit.json")
data, err := os.ReadFile(stateFile)
if err != nil {
slog.Debug("no pre-commit state found, skipping checkpoint")
return nil
}
// Remove immediately to prevent re-entry (amend triggers post-commit again)
_ = os.Remove(stateFile)
var state preCommitState
if err := json.Unmarshal(data, &state); err != nil {
return fmt.Errorf("parsing pre-commit state: %w", err)
}
if !state.AgentActive {
slog.Debug("no agent was active, skipping checkpoint")
return nil
}
// Get current commit hash
commitHash, err := git.CurrentCommit()
if err != nil {
return fmt.Errorf("getting current commit: %w", err)
}
// Calculate attribution
attr, err := attribution.Calculate(commitHash, state.AgentActive)
if err != nil {
slog.Warn("could not calculate attribution", "error", err)
attr = &attribution.Result{AgentPercent: 100}
}
// Parse agent session data
detector := claude.New()
sessionPath, sessionData, err := detector.FindLatestSession(repoRoot)
if err != nil {
slog.Warn("could not read agent session", "error", err)
}
// Generate checkpoint ID and amend commit with trailers BEFORE writing
// the checkpoint, so we capture the post-amend commit hash.
cpID := checkpoint.NewID()
trailers := map[string]string{
"Partio-Checkpoint": cpID,
"Partio-Attribution": fmt.Sprintf("%d%% agent", attr.AgentPercent),
}
if err := git.AmendTrailers(trailers); err != nil {
slog.Warn("could not add trailers to commit", "error", err)
}
// Get the post-amend commit hash (this is the hash that gets pushed)
commitHash, err = git.CurrentCommit()
if err != nil {
return fmt.Errorf("getting post-amend commit: %w", err)
}
// Create checkpoint with the post-amend hash
cp := &checkpoint.Checkpoint{
ID: cpID,
CommitHash: commitHash,
Branch: state.Branch,
CreatedAt: time.Now(),
Agent: cfg.Agent,
AgentPct: attr.AgentPercent,
ContentHash: commitHash,
}
if sessionData != nil {
cp.SessionID = sessionData.SessionID
cp.PlanSlug = sessionData.PlanSlug
}
// Collect modified files, filtering out gitignored paths.
var filesModified []string
if modFiles, err := git.DiffFiles(commitHash); err == nil {
filtered, ferr := git.FilterGitIgnored(repoRoot, modFiles)
if ferr == nil {
filesModified = filtered
} else {
filesModified = modFiles
}
}
// Prepare session files
sessionFiles := &checkpoint.SessionFiles{
ContentHash: commitHash,
Context: "",
FullJSONL: "",
Metadata: checkpoint.SessionMetadata{
Agent: cfg.Agent,
FilesModified: filesModified,
},
Prompt: "",
}
if d, err := git.Diff(commitHash); err == nil {
sessionFiles.Diff = d
}
if sessionData != nil {
sessionFiles.Context = sessionData.Context
sessionFiles.Prompt = sessionData.Prompt
sessionFiles.Metadata.TotalTokens = sessionData.TotalTokens
sessionFiles.Metadata.Duration = sessionData.Duration.String()
}
if sessionData != nil && sessionData.PlanSlug != "" {
planContent, err := claude.ReadPlanFile(sessionData.PlanSlug)
if err != nil {
slog.Warn("could not read plan file", "slug", sessionData.PlanSlug, "error", err)
} else {
sessionFiles.Plan = planContent
}
}
if sessionPath != "" {
rawJSONL, err := claude.ReadRawJSONL(sessionPath)
if err == nil {
sessionFiles.FullJSONL = string(rawJSONL)
}
}
// Write checkpoint to orphan branch
store := checkpoint.NewStore(repoRoot)
if err := store.Write(cp, sessionFiles); err != nil {
return fmt.Errorf("writing checkpoint: %w", err)
}
slog.Debug("checkpoint created", "id", cpID, "agent_pct", attr.AgentPercent)
return nil
}