(root string, state *watch.State)
| 1227 | } |
| 1228 | |
| 1229 | func writeSessionHandoff(root string, state *watch.State) error { |
| 1230 | baseRef := resolveHandoffBaseRef(root) |
| 1231 | artifact, err := handoff.Build(root, handoff.BuildOptions{ |
| 1232 | State: state, |
| 1233 | BaseRef: baseRef, |
| 1234 | }) |
| 1235 | if err != nil { |
| 1236 | return err |
| 1237 | } |
| 1238 | |
| 1239 | // Record this agent session in handoff history |
| 1240 | agentEntry := handoff.AgentEntry{ |
| 1241 | AgentID: detectAgentID(), |
| 1242 | StartedAt: sessionStartTime(state), |
| 1243 | EndedAt: time.Now(), |
| 1244 | } |
| 1245 | if state != nil { |
| 1246 | seen := make(map[string]bool) |
| 1247 | for _, e := range state.RecentEvents { |
| 1248 | if !seen[e.Path] { |
| 1249 | seen[e.Path] = true |
| 1250 | agentEntry.FilesEdited = append(agentEntry.FilesEdited, e.Path) |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | // Carry over history from previous artifact and append current session |
| 1256 | if prev, err := handoff.ReadLatest(root); err == nil && prev != nil { |
| 1257 | artifact.AgentHistory = prev.AgentHistory |
| 1258 | } |
| 1259 | artifact.AgentHistory = append(artifact.AgentHistory, agentEntry) |
| 1260 | |
| 1261 | // Cap history to last 20 entries |
| 1262 | if len(artifact.AgentHistory) > 20 { |
| 1263 | artifact.AgentHistory = artifact.AgentHistory[len(artifact.AgentHistory)-20:] |
| 1264 | } |
| 1265 | |
| 1266 | return handoff.WriteLatest(root, artifact) |
| 1267 | } |
| 1268 | |
| 1269 | // detectAgentID returns the current AI agent based on environment signals. |
| 1270 | func detectAgentID() string { |
no test coverage detected