MCPcopy Create free account
hub / github.com/arctic-cli/interface / apply

Function apply

packages/arctic/src/session/benchmark.ts:222–274  ·  view source on GitHub ↗
(input: { sessionID: string; targetSessionID?: string; allowDirty?: boolean })

Source from the content-addressed store, hash-verified

220 * 2. Restore AI's exact file states to workspace
221 */
222 export async function apply(input: { sessionID: string; targetSessionID?: string; allowDirty?: boolean }) {
223 return withWorktreeLock(async () => {
224 const child = await resolveChild(input.sessionID, input.targetSessionID)
225 if (!child) return Session.get(input.sessionID)
226 const parent = await Session.get(child.benchmark.parentID)
227 if (!isParent(parent) || !parent.benchmark.enabled) return parent
228
229 const dirty = await isDirty()
230 if (!input.allowDirty && dirty) {
231 throw new WorkingTreeDirtyError({ message: "Working tree has uncommitted changes." })
232 }
233
234 const childSnapshot = child.benchmark.lastSnapshot ?? (await latestSnapshot(child.id))
235 if (!childSnapshot) {
236 throw new SnapshotUnavailableError({
237 message: "No snapshot available for this benchmark session.",
238 })
239 }
240
241 // Step 1: Create checkpoint of current workspace state
242 const checkpoint = await Snapshot.track()
243 if (!checkpoint) {
244 throw new SnapshotUnavailableError({
245 message: "Benchmark mode requires snapshots (git) to be enabled.",
246 })
247 }
248
249 const currentAppliedID = parent.benchmark.appliedSessionID
250
251 // If we are currently in a child session, save its state before switching
252 if (currentAppliedID) {
253 const currentChild = await Session.get(currentAppliedID)
254 if (isChild(currentChild) && currentChild.benchmark.parentID === parent.id) {
255 await updateChildSnapshot(currentChild.id, checkpoint)
256 const currentWorktree = await ensureChildWorktree(parent, currentChild)
257 await Snapshot.restoreTo(checkpoint, currentWorktree, { cleanUntracked: true })
258 }
259 }
260
261 // Step 2: Restore AI's exact file states to workspace
262 await Snapshot.restoreTo(childSnapshot, Instance.worktree, { cleanUntracked: true })
263
264 return Session.update(parent.id, (draft) => {
265 if (draft.benchmark?.type === "parent") {
266 draft.benchmark.appliedSessionID = child.id
267 // Only update checkpointSnapshot if we are switching from the parent session
268 if (!currentAppliedID) {
269 draft.benchmark.checkpointSnapshot = checkpoint
270 }
271 }
272 })
273 })
274 }
275
276 /**
277 * Undo applied benchmark changes.

Callers

nothing calls this directly

Calls 10

withWorktreeLockFunction · 0.85
resolveChildFunction · 0.85
isParentFunction · 0.85
isDirtyFunction · 0.85
latestSnapshotFunction · 0.85
isChildFunction · 0.85
updateChildSnapshotFunction · 0.85
ensureChildWorktreeFunction · 0.85
getMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected