Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cloud-session-diff-restore.md
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.
16 changes: 14 additions & 2 deletions packages/opencode/src/kilo-sessions/kilo-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { Telemetry } from "@kilocode/kilo-telemetry"
import { Question } from "@/question"
import { Permission } from "@/permission"
import { withTimeout } from "@/util/timeout"
import { Snapshot } from "@/snapshot"
import { cumulativeSessionDiff } from "@/kilocode/session-portability/cumulative-diff"

async function provide<R>(input: { directory: string; fn: () => R }): Promise<R> {
const { WithInstance } = await import("@/project/with-instance")
Expand Down Expand Up @@ -225,6 +227,13 @@ export namespace KiloSessions {
await ingest.sync(sessionID, [{ type: "session_status", data: { status } }])
}

async function cumulative(sessionId: string, local: Snapshot.FileDiff[]) {
const { AppRuntime } = await import("@/effect/app-runtime")
return AppRuntime.runPromise(
Storage.Service.use((storage) => cumulativeSessionDiff(storage, SessionID.make(sessionId), local)),
)
}

export const layer = Layer.effect(
Service,
Effect.gen(function* () {
Expand Down Expand Up @@ -273,7 +282,9 @@ export namespace KiloSessions {
ingest.sync(evt.properties.part.sessionID, [{ type: "part", data: evt.properties.part }]),
)
yield* watch(Session.Event.Diff, (evt) =>
ingest.sync(evt.properties.sessionID, [{ type: "session_diff", data: evt.properties.diff }]),
cumulative(evt.properties.sessionID, evt.properties.diff).then((diff) =>
ingest.sync(evt.properties.sessionID, [{ type: "session_diff", data: diff }]),
),
)
yield* watch(Session.Event.TurnOpen, (evt) =>
ingest.sync(evt.properties.sessionID, [{ type: "session_open", data: {} }]),
Expand Down Expand Up @@ -673,7 +684,7 @@ export namespace KiloSessions {
log.info("full sync", { sessionId })

const { AppRuntime } = await import("@/effect/app-runtime")
const [session, diffs] = await AppRuntime.runPromise(
const [session, local] = await AppRuntime.runPromise(
Effect.gen(function* () {
const sessions = yield* Session.Service
const summary = yield* SessionSummary.Service
Expand All @@ -683,6 +694,7 @@ export namespace KiloSessions {
])
}),
)
const diffs = await cumulative(sessionId, local)
const messages = await Array.fromAsync(MessageV2.stream(SessionID.make(sessionId)))
messages.reverse()
const mdls = await models(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import { InstanceHttpApi } from "@/server/routes/instance/httpapi/api"
import { MessageTable, PartTable, SessionTable } from "@/session/session.sql"
import { Session } from "@/session/session"
import { Database } from "@/storage/db"
import { Storage } from "@/storage/storage"
import { AudioTranscriptionsBody, ClawStatus, EditBody, FimBody } from "../groups/kilo-gateway"
import { baseKey } from "../../../session-portability/cumulative-diff"
import { extractSessionDiffs, restoreSessionDiffs } from "../../../session-portability/session-diff-restore"

const FIM_TIMEOUT_MS = 30_000
const log = Log.create({ service: "kilo-gateway" })
Expand Down Expand Up @@ -440,23 +443,57 @@ export const kiloGatewayHandlers = HttpApiBuilder.group(InstanceHttpApi, "kilo",
if (!fetched.ok) return jsonError(fetched.error, fetched.status)
if (!fetched.data?.info?.id) return yield* Effect.fail(new HttpApiError.BadRequest({}))

const diffs = extractSessionDiffs(fetched.data)
const bridge = yield* EffectBridge.make()
return yield* Effect.tryPromise({
try: () =>
bridge.promise(
Effect.sync(() =>
importSessionToDb(fetched.data, {
Database,
Instance,
SessionTable,
MessageTable,
PartTable,
SessionToRow: Session.toRow,
Bus,
SessionCreatedEvent: Session.Event.Created,
Identifier,
}),
),
Effect.gen(function* () {
if (diffs.length > 0) {
yield* Effect.try({
try: () => restoreSessionDiffs({ directory: Instance.directory, diffs }),
catch: (err) => err,
}).pipe(
Effect.catch((err) =>
Effect.sync(() => {
logError("cloud/session/import/restore", err)
return undefined
}),
),
)
}

const imported = yield* Effect.sync(() =>
importSessionToDb(fetched.data, {
Database,
Instance,
SessionTable,
MessageTable,
PartTable,
SessionToRow: Session.toRow,
Bus,
SessionCreatedEvent: Session.Event.Created,
Identifier,
}),
)

if (diffs.length > 0) {
yield* Storage.Service.use((storage) =>
Effect.all([
storage.write(baseKey(imported.id), diffs),
storage.write(["session_diff", imported.id], diffs),
]),
).pipe(
Effect.catch((err) =>
Effect.sync(() => {
logError("cloud/session/import/diff", err)
}),
),
)
}

return imported
}),
),
catch: () => new HttpApiError.BadRequest({}),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 same(left: PortableDiff[], right: PortableDiff[]) {
return JSON.stringify(left) === JSON.stringify(right)
}

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 (same(input.base, input.local)) return input.base
Comment thread
iscekic marked this conversation as resolved.
Resolved
return [...input.base, ...input.local]
}

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 })))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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 {
fs.writeFileSync(file, diff.patch)
const proc = Bun.spawnSync(["git", "apply", "--3way", "--whitespace=nowarn", file], {
Comment thread
iscekic marked this conversation as resolved.
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 {
Comment thread
iscekic marked this conversation as resolved.
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { SessionShare } from "@/share/session"
import { ShareNext } from "@/share/share-next"
import { Skill } from "@/skill"
import { Snapshot } from "@/snapshot"
import { Storage } from "@/storage/storage" // kilocode_change
import { SyncEvent } from "@/sync"
import { ToolRegistry } from "@/tool/registry"
import { lazy } from "@/util/lazy"
Expand Down Expand Up @@ -218,6 +219,7 @@ export function createRoutes(corsOptions?: CorsOptions) {
SessionSummary.defaultLayer,
ShareNext.defaultLayer,
Snapshot.defaultLayer,
Storage.defaultLayer, // kilocode_change
SyncEvent.defaultLayer,
Skill.defaultLayer,
Todo.defaultLayer,
Expand Down
11 changes: 11 additions & 0 deletions packages/opencode/src/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Global } from "@opencode-ai/core/global"
import { BackgroundProcess } from "@/kilocode/background-process"
import { KiloSession, kiloSessionFork } from "@/kilocode/session"
import { SessionExport } from "@/kilocode/session-export"
import { baseKey, cumulativeSessionDiff } from "@/kilocode/session-portability/cumulative-diff" // kilocode_change
// kilocode_change end
import { Effect, Layer, Option, Context, Schema, Types } from "effect"
import { zod } from "@opencode-ai/core/effect-zod"
Expand Down Expand Up @@ -740,6 +741,16 @@ export const layer: Layer.Layer<Service, never, Bus.Service | Storage.Service |
yield* updatePart(p)
}
}
// kilocode_change start - preserve imported/cumulative diffs when forking sessions
const local = yield* storage
.read<Snapshot.FileDiff[]>(["session_diff", input.sessionID])
.pipe(Effect.orElseSucceed((): Snapshot.FileDiff[] => []))
const base = yield* cumulativeSessionDiff(storage, input.sessionID, local)
if (base.length > 0) {
yield* storage.write(baseKey(session.id), base).pipe(Effect.ignore)
yield* storage.write(["session_diff", session.id], base).pipe(Effect.ignore)
}
// kilocode_change end
return session
})

Expand Down
Loading
Loading