-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Restore cloud session diffs on import #10948
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 all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1565516
feat(cli): restore cloud session diffs on import
iscekic faae71e
fix(cli): hide git diff restore subprocesses
iscekic 71600b2
fix(cli): harden session diff path guard
iscekic 9be85f1
test(cli): keep diff restore test synchronous
iscekic 6c57172
test(cli): normalize diff restore line endings
iscekic eba8769
fix(cli): preserve imported diffs across session forks
iscekic 5ecd805
chore: remove session ingest findings doc
iscekic 8454629
fix(cli): avoid duplicate cumulative session diffs
iscekic a6a3ce6
test(cli): isolate session export test failures
iscekic b87d863
fix(cli): preserve cumulative cloud fork diffs
iscekic 0c28e05
fix(cli): avoid cumulative diff prefix duplication
iscekic bbf7278
test(cli): relax prompt busy checks on windows
iscekic 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,6 @@ | ||
| --- | ||
| "@kilocode/cli": minor | ||
| "kilo-code": minor | ||
| --- | ||
|
|
||
| Restore cloud session filesystem changes from synced session diffs when importing sessions, including inherited changes across imported session forks. |
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
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
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
49 changes: 49 additions & 0 deletions
49
packages/opencode/src/kilocode/session-portability/cumulative-diff.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,49 @@ | ||
| import { Effect } from "effect" | ||
| import { Snapshot } from "@/snapshot" | ||
| import { Storage } from "@/storage/storage" | ||
| import type { SessionID } from "@/session/schema" | ||
|
|
||
| export type PortableDiff = Snapshot.FileDiff & { | ||
| after?: string | ||
| } | ||
|
|
||
| export const baseKey = (id: SessionID | string) => ["session_diff_base", String(id)] | ||
|
|
||
| function equal(left: unknown, right: unknown) { | ||
| return JSON.stringify(left) === JSON.stringify(right) | ||
| } | ||
|
|
||
| function starts(base: PortableDiff[], local: PortableDiff[]) { | ||
| if (local.length < base.length) return false | ||
| return base.every((diff, index) => equal(diff, local[index])) | ||
| } | ||
|
|
||
| function ends(base: PortableDiff[], local: PortableDiff[]) { | ||
| if (base.length < local.length) return false | ||
| const start = base.length - local.length | ||
| return local.every((diff, index) => equal(diff, base[start + index])) | ||
| } | ||
|
|
||
| export function mergeSessionDiffs(input: { base: PortableDiff[]; local: PortableDiff[] }) { | ||
| if (input.base.length === 0) return input.local | ||
| if (input.local.length === 0) return input.base | ||
| if (starts(input.base, input.local)) return input.local | ||
| return [...input.base, ...input.local] | ||
| } | ||
|
|
||
| export function appendSessionDiffs(input: { existing: PortableDiff[]; next: PortableDiff[] }) { | ||
| if (input.existing.length === 0) return input.next | ||
| if (input.next.length === 0) return input.existing | ||
| if (starts(input.existing, input.next)) return input.next | ||
| if (starts(input.next, input.existing)) return input.existing | ||
| if (ends(input.existing, input.next)) return input.existing | ||
| return [...input.existing, ...input.next] | ||
| } | ||
|
|
||
| export function readSessionDiffBase(storage: Storage.Interface, id: SessionID | string) { | ||
| return storage.read<PortableDiff[]>(baseKey(id)).pipe(Effect.catch(() => Effect.succeed([] as PortableDiff[]))) | ||
| } | ||
|
|
||
| export function cumulativeSessionDiff(storage: Storage.Interface, id: SessionID | string, local: PortableDiff[]) { | ||
| return readSessionDiffBase(storage, id).pipe(Effect.map((base) => mergeSessionDiffs({ base, local }))) | ||
| } | ||
113 changes: 113 additions & 0 deletions
113
packages/opencode/src/kilocode/session-portability/session-diff-restore.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,113 @@ | ||
| import fs from "node:fs" | ||
| import os from "node:os" | ||
| import path from "node:path" | ||
|
|
||
| type Diff = { | ||
| file?: string | ||
| patch?: string | ||
| after?: string | ||
| additions?: number | ||
| deletions?: number | ||
| status?: string | ||
| } | ||
|
|
||
| export type RestoreResult = { | ||
| applied: number | ||
| skipped: number | ||
| total: number | ||
| } | ||
|
|
||
| function diffs(value: unknown): Diff[] { | ||
| if (!Array.isArray(value)) return [] | ||
| return value.filter((item): item is Diff => typeof item === "object" && item !== null) | ||
| } | ||
|
|
||
| export function extractSessionDiffs(data: unknown): Diff[] { | ||
| if (typeof data !== "object" || data === null) return [] | ||
| const root = data as { sessionDiff?: unknown; session_diff?: unknown; messages?: unknown } | ||
| const top = diffs(root.sessionDiff).length > 0 ? diffs(root.sessionDiff) : diffs(root.session_diff) | ||
| if (top.length > 0) return top | ||
| if (!Array.isArray(root.messages)) return [] | ||
|
|
||
| const map = new Map<string, Diff>() | ||
| for (const msg of root.messages) { | ||
| if (typeof msg !== "object" || msg === null) continue | ||
| const info = (msg as { info?: unknown }).info | ||
| if (typeof info !== "object" || info === null) continue | ||
| const summary = (info as { summary?: unknown }).summary | ||
| if (typeof summary !== "object" || summary === null) continue | ||
| for (const diff of diffs((summary as { diffs?: unknown }).diffs)) { | ||
| if (typeof diff.file === "string") map.set(diff.file, diff) | ||
| } | ||
| } | ||
| return Array.from(map.values()) | ||
| } | ||
|
|
||
| function safe(root: string, file: string) { | ||
| const fp = path.resolve(root, file) | ||
| const rel = path.relative(root, fp) | ||
| if (rel === "" || rel.startsWith("..") || path.isAbsolute(rel)) return | ||
| return fp | ||
| } | ||
|
|
||
| function apply(dir: string, diff: Diff) { | ||
| if (!diff.patch) return false | ||
| const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "kilo-session-diff-")) | ||
| const file = path.join(tmp, "change.patch") | ||
| try { | ||
| const text = diff.patch.endsWith("\n") ? diff.patch : diff.patch + "\n" | ||
| fs.writeFileSync(file, text) | ||
| const proc = Bun.spawnSync(["git", "apply", "--3way", "--whitespace=nowarn", file], { | ||
|
iscekic marked this conversation as resolved.
|
||
| cwd: dir, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| windowsHide: true, | ||
| }) | ||
| return proc.exitCode === 0 | ||
| } finally { | ||
| fs.rmSync(tmp, { recursive: true, force: true }) | ||
| } | ||
| } | ||
|
|
||
| export function restoreSessionDiffs(input: { directory: string; diffs: Diff[] }): RestoreResult { | ||
|
iscekic marked this conversation as resolved.
|
||
| const root = path.resolve(input.directory) | ||
| const total = input.diffs.length | ||
| const result = { applied: 0, skipped: 0, total } | ||
|
|
||
| for (const diff of input.diffs) { | ||
| if (diff.patch) { | ||
| if (apply(root, diff)) { | ||
| result.applied++ | ||
| continue | ||
| } | ||
| result.skipped++ | ||
| continue | ||
| } | ||
|
|
||
| if (typeof diff.file !== "string") { | ||
| result.skipped++ | ||
| continue | ||
| } | ||
| const fp = safe(root, diff.file) | ||
| if (!fp) { | ||
| result.skipped++ | ||
| continue | ||
| } | ||
|
|
||
| if (diff.status === "deleted") { | ||
| fs.rmSync(fp, { force: true }) | ||
| result.applied++ | ||
| continue | ||
| } | ||
|
|
||
| if (typeof diff.after !== "string" || diff.after.length === 0) { | ||
| result.skipped++ | ||
| continue | ||
| } | ||
| fs.mkdirSync(path.dirname(fp), { recursive: true }) | ||
| fs.writeFileSync(fp, diff.after) | ||
| result.applied++ | ||
| } | ||
|
|
||
| return result | ||
| } | ||
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
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.