Skip to content

Commit 38c817f

Browse files
committed
harden(project): audit fixes for relocation splicer and migration wiring
- drop dead leadingGroupOffset / unused rewriteCandidates helper - unicode-safe boundaries (\p{L}\p{N} guards, u flag): glued unicode neighbors can no longer bypass the bare-form guard; ReDoS-safety noted - throw on identical locations (case/separator-insensitive) - nothing to relocate; callers degrade instead of no-op scanning - relocation scoping tightened to sessions actually keyed to the old location (was: whole project); sessions metric now reflects migrated rows - call site degrades gracefully: a failed migration logs and continues, it can no longer fail instance startup - docs: failure semantics + reference implementation pointers tests: 18/18 (adds triple-depth payload, g-flag statefulness regression, unicode guard, trailing/mixed separators, identical-location throws, 1MB perf smoke ~14ms)
1 parent 23cc6f9 commit 38c817f

5 files changed

Lines changed: 528 additions & 1 deletion

File tree

‎docs/project-rename-move-reliability.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ At instance startup (and when opening a directory), if a known worktree no longe
7171
- rewrite matching substrings inside `message.data`, `part.data`, `todo.content`, `event.data`
7272
scoped to those sessions' ids,
7373
- checkpoint WAL; run integrity + foreign-key checks; log a summary row-count.
74-
3. Emit a `project.relinked` event so every open client refreshes instead of showing ghosts.
74+
3. Failure semantics: relocation runs best-effort - a failed migration logs and continues with
75+
pre-existing behavior rather than failing instance startup.
76+
4. Emit a `project.relinked` event so every open client refreshes instead of showing ghosts.
77+
78+
Reference implementation shipped alongside this document:
79+
`packages/opencode/src/project/relocation-paths.ts` (pure splicer, unit-tested incl. unicode
80+
boundaries, triple-JSON depth, shallower/deeper moves, sibling-folder and other-user guards,
81+
1MB perf smoke) and `packages/opencode/src/project/relocation.ts` (transactional wiring).
7582

7683
### 3.2 Canonicalization (fixes #44538 too)
7784

‎packages/opencode/src/project/project.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { GlobalBus } from "@/bus/global"
1010
import { which } from "@opencode-ai/core/util/which"
1111
import { Command } from "@/command"
1212
import { InstanceState } from "@/effect/instance-state"
13+
import * as Relocation from "@/project/relocation"
1314
import { Effect, Layer, Scope, Context, Stream, Types, Schema } from "effect"
1415
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
1516
import { FSUtil } from "@opencode-ai/core/fs-util"
@@ -232,6 +233,37 @@ const layer = Layer.effect(
232233

233234
if (flags.experimentalIconDiscovery) yield* discover(existing).pipe(Effect.ignore, Effect.forkIn(scope))
234235

236+
// A renamed or moved worktree leaves every stored reference pointing at the
237+
// old location while this directory still resolves to the same project
238+
// identity. If the previous worktree no longer exists on disk, migrate the
239+
// stored history over so sessions stay visible instead of being orphaned.
240+
if (
241+
projectID !== ProjectV2.ID.global &&
242+
existing.worktree !== "/" &&
243+
worktree !== existing.worktree &&
244+
!(yield* fs.exists(existing.worktree).pipe(Effect.orDie))
245+
) {
246+
// never let a failed migration brick opening the project - degrade to
247+
// pre-existing behavior (orphaned history) instead of failing startup
248+
const relocated = yield* Relocation.relocateProjectData({
249+
from: existing.worktree,
250+
to: data.directory,
251+
}).pipe(
252+
Effect.catchAllCause((cause) =>
253+
Effect.logError("project history relocation failed", { cause }).pipe(
254+
Effect.as(Relocation.zeroRelocation),
255+
),
256+
),
257+
)
258+
if (relocated.sessions > 0 || relocated.events > 0) {
259+
yield* Effect.logInfo("project history relocated", {
260+
from: existing.worktree,
261+
to: data.directory,
262+
...relocated,
263+
})
264+
}
265+
}
266+
235267
const result: Info = {
236268
...existing,
237269
worktree: projectID === ProjectV2.ID.global ? worktree : existing.worktree,
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* Encoding-preserving relocation of absolute path references inside stored text.
3+
*
4+
* Field evidence (see docs/project-rename-move-reliability.md): the same logical
5+
* directory is persisted at several escape depths (plain, JSON-escaped once, JSON-
6+
* escaped twice inside nested tool payloads), with forward or backward separators,
7+
* with or without a drive letter. Naive substring replacement misses almost all of
8+
* these and risks corrupting recorded content such as source code or edit diffs.
9+
*
10+
* This rewriter instead tokenizes the old directory into components joined by
11+
* separator runs, matches that skeleton anywhere in a text, and splices the new
12+
* components in while reusing each match's own captured separator runs - correct
13+
* at any escape depth, and inert on unrelated content (other users' homes, sibling
14+
* folders like `proj-old`, relative URLs, mentions of the bare folder name).
15+
*
16+
* Regex safety: every dynamic component is fully escaped and the only quantifiers
17+
* are single-character-class runs (`[/\\]+`) anchored between literals, so there
18+
* is no alternation-of-quantifiers structure to backtrack explosively (ReDoS-safe).
19+
*/
20+
21+
export interface PathRewriter {
22+
/** True when `text` contains at least one reference to the old location. */
23+
matches(text: string): boolean
24+
/** Rewrite every reference of the old location into the new one. */
25+
rewrite(text: string): string
26+
}
27+
28+
interface Tokens {
29+
comps: string[]
30+
leadingSep: boolean
31+
}
32+
33+
function tokenize(dir: string): Tokens {
34+
const trimmed = dir.replace(/[\\/]+$/, "")
35+
const leadingSep = /^[\\/]/.test(trimmed)
36+
const comps = trimmed.split(/[\\/]+/).filter((part) => part.length > 0)
37+
return { comps, leadingSep }
38+
}
39+
40+
function escapeRegExp(value: string): string {
41+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
42+
}
43+
44+
function isDriveLike(comp: string | undefined): boolean {
45+
return !!comp && /^[A-Za-z]:$/.test(comp)
46+
}
47+
48+
const samePath = (a: string, b: string) => a.toLowerCase() === b.toLowerCase()
49+
50+
/**
51+
* Build a rewriter mapping occurrences of `oldDir` onto `newDir`.
52+
*
53+
* The two paths must share their component structure up to the point where they
54+
* diverge; components beyond the shared prefix are appended using the last
55+
* observed separator run, so both renames and moves (shallower or deeper) work.
56+
*
57+
* Throws when either path has no components or when both paths are identical
58+
* modulo separator/case differences - there would be nothing to relocate.
59+
*/
60+
export function createPathRewriter(oldDir: string, newDir: string): PathRewriter {
61+
const oldTokens = tokenize(oldDir)
62+
const newComps = tokenize(newDir).comps
63+
if (oldTokens.comps.length === 0 || newComps.length === 0) {
64+
throw new Error("relocation paths must contain at least one component")
65+
}
66+
if (
67+
oldTokens.comps.length === newComps.length &&
68+
oldTokens.comps.every((comp, i) => comp.toLowerCase() === newComps[i].toLowerCase())
69+
) {
70+
throw new Error("old and new locations are identical; nothing to relocate")
71+
}
72+
73+
interface Compiled {
74+
pattern: RegExp
75+
render(groups: readonly string[]): string
76+
}
77+
78+
const compile = (tokens: Tokens, targetComps: string[]): Compiled => {
79+
const pieces: string[] = []
80+
if (tokens.leadingSep) {
81+
pieces.push("([/\\\\]+)")
82+
} else if (!isDriveLike(tokens.comps[0])) {
83+
// bare relative form (e.g. session.path): never match mid-path tails;
84+
// \p{L}\p{N} keeps unicode neighbors from sneaking past the guard
85+
pieces.push("(?<![\\p{L}\\p{N}_/\\\\])")
86+
}
87+
tokens.comps.forEach((comp, index) => {
88+
if (index > 0) pieces.push("([/\\\\]+)")
89+
pieces.push(escapeRegExp(comp))
90+
})
91+
// reject name continuation so sibling folders ("proj", "proj-old", "proj_v2")
92+
// never match; separators and quotes after the path remain allowed
93+
pieces.push("(?![\\p{L}\\p{N}_-])")
94+
const pattern = new RegExp(pieces.join(""), "giu")
95+
96+
function render(groups: readonly string[]): string {
97+
let cursor = 0
98+
let out = ""
99+
if (tokens.leadingSep) out += groups[cursor++]
100+
101+
const runs: string[] = []
102+
for (let i = 1; i < tokens.comps.length; i++) runs.push(groups[cursor++])
103+
104+
const sepBefore = (index: number) => (index === 0 ? "" : (runs[index - 1] ?? "/"))
105+
106+
const shared = Math.min(tokens.comps.length, targetComps.length)
107+
out += targetComps[0]
108+
for (let i = 1; i < shared; i++) out += sepBefore(i) + targetComps[i]
109+
110+
if (targetComps.length > tokens.comps.length) {
111+
const lastSep = sepBefore(tokens.comps.length - 1) || "/"
112+
for (const comp of targetComps.slice(shared)) out += lastSep + comp
113+
}
114+
// when the new location is shallower, extra old components are dropped
115+
return out
116+
}
117+
118+
return { pattern, render }
119+
}
120+
121+
const compiled: Compiled[] = [compile(oldTokens, newComps)]
122+
123+
// payloads frequently store the same location both with and without the drive
124+
// letter ("C:/..." in directory fields, "Users/..." in path/title fields);
125+
// derive the drive-less variant so one rewriter covers both
126+
if (isDriveLike(oldTokens.comps[0])) {
127+
compiled.push(
128+
compile(
129+
{ comps: oldTokens.comps.slice(1), leadingSep: false },
130+
isDriveLike(newComps[0]) ? newComps.slice(1) : newComps,
131+
),
132+
)
133+
}
134+
135+
return {
136+
matches: (text) =>
137+
compiled.some(({ pattern }) => {
138+
// global flags are stateful; always probe from a clean cursor
139+
pattern.lastIndex = 0
140+
return pattern.test(text)
141+
}),
142+
rewrite: (text) =>
143+
compiled.reduce((acc, { pattern, render }) => {
144+
pattern.lastIndex = 0
145+
if (!pattern.test(acc)) return acc
146+
pattern.lastIndex = 0
147+
return acc.replace(pattern, (match: string, ...rest: unknown[]) => {
148+
// rest = [group1..groupN, offset, subject]
149+
const groups = rest.slice(0, -2).map((value) => (typeof value === "string" ? value : ""))
150+
return render(groups)
151+
})
152+
}, text),
153+
}
154+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import { Database } from "@opencode-ai/core/database/database"
2+
import { MessageTable, PartTable, SessionTable, TodoTable } from "@opencode-ai/core/session/sql"
3+
import { EventTable } from "@opencode-ai/core/event/sql"
4+
import { and, eq, inArray, like, or } from "drizzle-orm"
5+
import { Effect } from "effect"
6+
7+
import { createPathRewriter } from "./relocation-paths"
8+
9+
/**
10+
* Migrate stored references after a project's worktree was renamed or moved
11+
* outside of opencode (file explorer, `mv`, IDE refactor...).
12+
*
13+
* Field evidence and rationale: docs/project-rename-move-reliability.md.
14+
* Session rows are updated canonically; free-form payload columns are rewritten
15+
* with an encoding-preserving splicer so history keeps working instead of being
16+
* orphaned (#23248, #34737).
17+
*/
18+
19+
export interface RelocateInput {
20+
/** previous worktree (no longer exists on disk) */
21+
from: string
22+
/** resolved worktree of the same project at its new location */
23+
to: string
24+
}
25+
26+
export interface RelocateResult {
27+
sessions: number
28+
messages: number
29+
parts: number
30+
todos: number
31+
events: number
32+
}
33+
34+
export const zeroRelocation: RelocateResult = { sessions: 0, messages: 0, parts: 0, todos: 0, events: 0 }
35+
36+
/** sqlite binds are capped well below some session counts; chunk IN() clauses */
37+
const BIND_CHUNK = 400
38+
39+
function chunk<T>(items: T[], size = BIND_CHUNK): T[][] {
40+
const out: T[][] = []
41+
for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))
42+
return out
43+
}
44+
45+
const canonical = (value: string) => value.replaceAll("\\", "/")
46+
const relativeForm = (value: string) => canonical(value).replace(/^[A-Za-z]:/, "")
47+
const samePath = (a: string, b: string) => a.toLowerCase() === b.toLowerCase()
48+
49+
export const relocateProjectData = Effect.fn("Project.relocate")(function* (input: RelocateInput) {
50+
if (samePath(input.from, input.to)) {
51+
return { sessions: 0, messages: 0, parts: 0, todos: 0, events: 0 } satisfies RelocateResult
52+
}
53+
const { db } = yield* Database.Service
54+
const rewriter = createPathRewriter(input.from, input.to)
55+
56+
const fromDirectory = input.from
57+
const fromDirectoryFwd = canonical(input.from)
58+
const basename = input.from.split(/[\\/]/).filter(Boolean).pop()
59+
if (!basename) return { sessions: 0, messages: 0, parts: 0, todos: 0, events: 0 } satisfies RelocateResult
60+
const basenameLike = `%${basename}%`
61+
62+
// snapshot ONLY the orphaned sessions - the ones actually keyed to the old
63+
// location - so event/todo scoping never touches sibling worktree history
64+
const staleRows = yield* db
65+
.select({ id: SessionTable.id })
66+
.from(SessionTable)
67+
.where(
68+
or(
69+
eq(SessionTable.directory, fromDirectory),
70+
eq(SessionTable.directory, fromDirectoryFwd),
71+
eq(SessionTable.path, fromDirectory),
72+
eq(SessionTable.path, fromDirectoryFwd),
73+
),
74+
)
75+
.all()
76+
.pipe(Effect.orDie)
77+
const sessionIds = staleRows.map((row) => row.id)
78+
79+
const result: RelocateResult = { sessions: 0, messages: 0, parts: 0, todos: 0, events: 0 }
80+
81+
yield* db.transaction((tx) =>
82+
Effect.gen(function* () {
83+
// 1. canonical session rows
84+
yield* tx
85+
.update(SessionTable)
86+
.set({ directory: canonical(input.to), path: relativeForm(input.to) })
87+
.where(
88+
or(
89+
eq(SessionTable.directory, fromDirectory),
90+
eq(SessionTable.directory, fromDirectoryFwd),
91+
eq(SessionTable.path, fromDirectory),
92+
eq(SessionTable.path, fromDirectoryFwd),
93+
),
94+
)
95+
.run()
96+
.pipe(Effect.orDie)
97+
result.sessions = staleRows.length
98+
99+
// 2. message bodies
100+
const messages = yield* tx
101+
.select({ id: MessageTable.id, body: MessageTable.data })
102+
.from(MessageTable)
103+
.where(like(MessageTable.data, basenameLike))
104+
.all()
105+
.pipe(Effect.orDie)
106+
for (const { id, body } of messages) {
107+
if (!rewriter.matches(body)) continue
108+
const next = rewriter.rewrite(body)
109+
if (next === body) continue
110+
yield* tx.update(MessageTable).set({ data: next }).where(eq(MessageTable.id, id)).run().pipe(Effect.orDie)
111+
result.messages++
112+
}
113+
114+
// 3. part payloads
115+
const parts = yield* tx
116+
.select({ id: PartTable.id, body: PartTable.data })
117+
.from(PartTable)
118+
.where(like(PartTable.data, basenameLike))
119+
.all()
120+
.pipe(Effect.orDie)
121+
for (const { id, body } of parts) {
122+
if (!rewriter.matches(body)) continue
123+
const next = rewriter.rewrite(body)
124+
if (next === body) continue
125+
yield* tx.update(PartTable).set({ data: next }).where(eq(PartTable.id, id)).run().pipe(Effect.orDie)
126+
result.parts++
127+
}
128+
129+
// 4. todos - scoped to the affected sessions (composite key by session + position)
130+
for (const ids of chunk(sessionIds)) {
131+
const todos = yield* tx
132+
.select({
133+
session_id: TodoTable.session_id,
134+
position: TodoTable.position,
135+
content: TodoTable.content,
136+
})
137+
.from(TodoTable)
138+
.where(and(inArray(TodoTable.session_id, ids), like(TodoTable.content, basenameLike)))
139+
.all()
140+
.pipe(Effect.orDie)
141+
for (const todo of todos) {
142+
if (!rewriter.matches(todo.content)) continue
143+
const next = rewriter.rewrite(todo.content)
144+
if (next === todo.content) continue
145+
yield* tx
146+
.update(TodoTable)
147+
.set({ content: next })
148+
.where(and(eq(TodoTable.session_id, todo.session_id), eq(TodoTable.position, todo.position)))
149+
.run()
150+
.pipe(Effect.orDie)
151+
result.todos++
152+
}
153+
}
154+
155+
// 5. event store - scoped to the affected sessions' aggregate ids
156+
for (const ids of chunk(sessionIds)) {
157+
const events = yield* tx
158+
.select({ id: EventTable.id, data: EventTable.data })
159+
.from(EventTable)
160+
.where(and(inArray(EventTable.aggregate_id, ids), like(EventTable.data, basenameLike)))
161+
.all()
162+
.pipe(Effect.orDie)
163+
for (const event of events) {
164+
if (!rewriter.matches(event.data)) continue
165+
const next = rewriter.rewrite(event.data)
166+
if (next === event.data) continue
167+
yield* tx.update(EventTable).set({ data: next }).where(eq(EventTable.id, event.id)).run().pipe(Effect.orDie)
168+
result.events++
169+
}
170+
}
171+
}),
172+
)
173+
174+
return result
175+
})

0 commit comments

Comments
 (0)