Repository navigation
feat: add experimental turn-level file history snapshots #3339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
aa325d2
feat(agent-core-v2): add experimental turn-level file history snapshots
8ad21db
feat(kap-server): serve turn-level file history over REST
207bd21
fix(agent-core-v2): drain queued snapshots on agent teardown, single-…
f6ac257
fix(agent-core-v2): bound aggregate LCS work per changes() call
aab9499
refactor(agent-core-v2): store per-turn checkpoint deltas instead of …
30c65c9
fix(agent-core-v2): gate live reads on the actually active turn
e1782a5
fix(agent-core-v2): reset file history in turn-sliced forks
73a47a7
fix(agent-core-v2): settle snapshots before fork copy, prune sliced-f…
13d078e
fix(agent-core-v2): capture subagent edits into the main agent's file…
6ee18e4
fix(agent-core-v2): hold loop quiescence on source agents while forking
98ef154
Merge remote-tracking branch 'origin/main' into feat/turn-file-history
5c65f2d
feat(agent-core-v2): retain file history for a five-turn sliding window
afa5624
feat(agent-core-v2): capture a per-turn baseline so turns are self-co…
745afb0
refactor(agent-core-v2): turns record only their own tool snapshots, …
bf39829
feat(kap-server): report file-history availability on the changes res…
8b5ccab
fix(agent-core-v2): retain the last five recorded turns, not five tur…
b0a0095
Merge remote-tracking branch 'origin/main' into feat/turn-file-history
96e07ae
Merge remote-tracking branch 'origin/main' into feat/turn-file-history
7258fe2
fix(agent-core-v2): count only completed turns in the file-history wi…
34dee75
Merge remote-tracking branch 'origin/main' into feat/turn-file-history
0de862d
fix(agent-core-v2): flush the end checkpoint before evicting displace…
e2babf4
fix(agent-core-v2): size snapshot reads to the statted file
c6d74b8
fix(agent-core-v2): keep file-history blobs out of session exports, s…
68bd19d
Merge remote-tracking branch 'origin/main' into feat/turn-file-history
73e4fb7
feat(agent-core-v2): retain file history for the thirty most recently…
04b3789
fix(agent-core-v2): keep the retention ledger out of the session listing
19367ec
fix(agent-core-v2): review round — editing-turn window, full blob has…
0240171
feat(agent-core-v2): report whether a turn's history is recorded
b56c812
fix(agent-core-v2): recorded probes blob availability, content falls …
209cf39
fix(agent-core-v2): satisfy lint on the recorded probe
10dd4b4
fix(agent-core-v2): keep the retention ledger honest across eviction …
bb8e41a
Merge remote-tracking branch 'origin/main' into feat/turn-file-history
98ec48b
chore(agent-core-v2): regenerate the wire manifest after the merge
0230f82
fix(agent-core-v2): count a trailing-newline-only change in authorita…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Add experimental turn-level file snapshots (enable with KIMI_CODE_EXPERIMENTAL_FILE_HISTORY=1): the daemon backs up every file it edits and versions all tracked files at each turn boundary. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/agent-core-v2/src/features/fileHistory/fileHistory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { createDecorator, type ServiceIdentifier } from '#/_base/di/instantiation'; | ||
|
|
||
| export interface FileBackupEntry { | ||
| readonly key: string | null; | ||
| readonly version: number; | ||
| readonly contentHash?: string; | ||
| readonly size?: number; | ||
| readonly oversize?: boolean; | ||
| readonly mtimeMs?: number; | ||
| } | ||
|
|
||
| export type FileHistoryCheckpointPhase = 'start' | 'end'; | ||
|
|
||
| export interface FileHistoryCheckpointRecord { | ||
| readonly turnId: number; | ||
| readonly phase?: FileHistoryCheckpointPhase; | ||
| readonly entries: Readonly<Record<string, FileBackupEntry>>; | ||
| } | ||
|
|
||
| export interface FileHistoryState { | ||
| readonly checkpoints: readonly FileHistoryCheckpointRecord[]; | ||
| readonly tracked: readonly string[]; | ||
| } | ||
|
|
||
| export type FileHistoryChangeStatus = 'added' | 'modified' | 'deleted'; | ||
|
|
||
| export interface FileHistoryChange { | ||
| readonly path: string; | ||
| readonly status: FileHistoryChangeStatus; | ||
| readonly additions: number; | ||
| readonly deletions: number; | ||
| readonly binary?: boolean; | ||
| readonly oversize?: boolean; | ||
| } | ||
|
|
||
| export interface FileHistoryContent { | ||
| readonly version: number; | ||
| readonly content?: string; | ||
| readonly binary?: boolean; | ||
| } | ||
|
|
||
| export interface IAgentFileHistoryService { | ||
| readonly _serviceBrand: undefined; | ||
|
|
||
| enabled(): boolean; | ||
| history(): FileHistoryState; | ||
| settled(): Promise<void>; | ||
| captureForActiveTurn(path: string): Promise<void>; | ||
| changes(turnId: number): Promise<FileHistoryChange[]>; | ||
| contentAt( | ||
| turnId: number, | ||
| path: string, | ||
| phase?: FileHistoryCheckpointPhase, | ||
| ): Promise<FileHistoryContent | undefined>; | ||
| } | ||
|
|
||
| export const IAgentFileHistoryService: ServiceIdentifier<IAgentFileHistoryService> = | ||
| createDecorator<IAgentFileHistoryService>('agentFileHistoryService'); |
17 changes: 17 additions & 0 deletions
17
packages/agent-core-v2/src/features/fileHistory/fileHistoryFeature.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Feature } from '#/features/feature'; | ||
| import { registerFeature } from '#/features/featureRegistry'; | ||
|
|
||
| import './flag'; | ||
| import { IAgentFileHistoryService } from './fileHistory'; | ||
| import { AgentFileHistoryService } from './fileHistoryService'; | ||
|
|
||
| export class FileHistoryFeature extends Feature { | ||
| static override readonly name = 'fileHistory'; | ||
|
|
||
| constructor() { | ||
| super(); | ||
| this.contributeAgentService(IAgentFileHistoryService, AgentFileHistoryService); | ||
| } | ||
| } | ||
|
|
||
| registerFeature(FileHistoryFeature); |
120 changes: 120 additions & 0 deletions
120
packages/agent-core-v2/src/features/fileHistory/fileHistoryOps.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* oxlint-disable typescript-eslint/no-unsafe-declaration-merging, eslint-plugin-import/namespace -- Event2 class+payload-interface declaration merging is the sanctioned event-declaration idiom. */ | ||
| import { z } from 'zod'; | ||
|
|
||
| import { AgentEvent2 } from '#/app/event/event2'; | ||
| import { defineState } from '#/state/state'; | ||
|
|
||
| import type { | ||
| FileBackupEntry, | ||
| FileHistoryCheckpointPhase, | ||
| FileHistoryState, | ||
| } from './fileHistory'; | ||
|
|
||
| export const FILE_HISTORY_CHECKPOINT_CAP = 400; | ||
| export const FILE_HISTORY_RECORD_PREFIX = 'file_history.'; | ||
|
|
||
| const backupEntrySchema = z.object({ | ||
| key: z.string().nullable(), | ||
| version: z.number(), | ||
| contentHash: z.string().optional(), | ||
| size: z.number().optional(), | ||
| oversize: z.boolean().optional(), | ||
| mtimeMs: z.number().optional(), | ||
| }); | ||
|
|
||
| const fileHistoryTrackedSchema = z.object({ | ||
| agentId: z.string(), | ||
| turnId: z.number(), | ||
| path: z.string(), | ||
| entry: backupEntrySchema, | ||
| }); | ||
|
|
||
| export class FileHistoryTracked extends AgentEvent2<z.infer<typeof fileHistoryTrackedSchema>> { | ||
| static override readonly type = 'file_history.tracked'; | ||
| static override readonly durable = true; | ||
| static override readonly schema = fileHistoryTrackedSchema; | ||
| } | ||
| export interface FileHistoryTracked { | ||
| readonly agentId: string; | ||
| readonly turnId: number; | ||
| readonly path: string; | ||
| readonly entry: FileBackupEntry; | ||
| } | ||
|
|
||
| const fileHistoryCheckpointedSchema = z.object({ | ||
| agentId: z.string(), | ||
| turnId: z.number(), | ||
| phase: z.enum(['start', 'end']).optional(), | ||
| entries: z.record(z.string(), backupEntrySchema), | ||
| }); | ||
|
|
||
| export class FileHistoryCheckpointed extends AgentEvent2< | ||
| z.infer<typeof fileHistoryCheckpointedSchema> | ||
| > { | ||
| static override readonly type = 'file_history.checkpoint'; | ||
| static override readonly durable = true; | ||
| static override readonly schema = fileHistoryCheckpointedSchema; | ||
| } | ||
| export interface FileHistoryCheckpointed { | ||
| readonly agentId: string; | ||
| readonly turnId: number; | ||
| readonly phase?: FileHistoryCheckpointPhase; | ||
| readonly entries: Readonly<Record<string, FileBackupEntry>>; | ||
| } | ||
|
|
||
| export function checkpointPhaseOf(record: { | ||
| readonly phase?: FileHistoryCheckpointPhase; | ||
| }): FileHistoryCheckpointPhase { | ||
| return record.phase ?? 'start'; | ||
| } | ||
|
|
||
| function cloneEntries( | ||
| entries: Readonly<Record<string, FileBackupEntry>>, | ||
| ): Record<string, FileBackupEntry> { | ||
| const clone: Record<string, FileBackupEntry> = Object.create(null) as Record< | ||
| string, | ||
| FileBackupEntry | ||
| >; | ||
| for (const [path, entry] of Object.entries(entries)) clone[path] = entry; | ||
| return clone; | ||
| } | ||
|
|
||
| export const fileHistoryKey = defineState( | ||
| 'fileHistory', | ||
| (): FileHistoryState => ({ checkpoints: [], tracked: [] }), | ||
| ) | ||
| .replayable({ schema: z.custom<FileHistoryState>() }) | ||
| .on(FileHistoryCheckpointed, (s, e) => { | ||
| const phase = checkpointPhaseOf(e); | ||
| const existing = s.checkpoints.find( | ||
| (c) => c.turnId === e.turnId && checkpointPhaseOf(c) === phase, | ||
| ); | ||
| if (existing !== undefined) { | ||
| existing.entries = cloneEntries(e.entries); | ||
| return; | ||
| } | ||
| s.checkpoints.push({ turnId: e.turnId, phase, entries: cloneEntries(e.entries) }); | ||
| if (s.checkpoints.length > FILE_HISTORY_CHECKPOINT_CAP) { | ||
| const removed = s.checkpoints.splice(0, s.checkpoints.length - FILE_HISTORY_CHECKPOINT_CAP); | ||
| const head = s.checkpoints[0]!; | ||
| const folded = cloneEntries({}); | ||
| for (const record of removed) { | ||
| for (const [path, entry] of Object.entries(record.entries)) folded[path] = entry; | ||
| } | ||
| for (const [path, entry] of Object.entries(head.entries)) folded[path] = entry; | ||
| head.entries = folded; | ||
| } | ||
| }) | ||
| .on(FileHistoryTracked, (s, e) => { | ||
| if (!s.tracked.includes(e.path)) s.tracked.push(e.path); | ||
| let checkpoint = s.checkpoints.find( | ||
| (c) => c.turnId === e.turnId && checkpointPhaseOf(c) === 'start', | ||
| ); | ||
| if (checkpoint === undefined) { | ||
| s.checkpoints.push({ turnId: e.turnId, phase: 'start', entries: {} }); | ||
| checkpoint = s.checkpoints.at(-1); | ||
| } | ||
| if (checkpoint !== undefined && !Object.hasOwn(checkpoint.entries, e.path)) { | ||
| checkpoint.entries[e.path] = { ...e.entry }; | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.