Skip to content

Commit c600aca

Browse files
sahrizviclaude
andcommitted
fix(workspace): an in-flight empty sync cannot outlive an unlink (review)
- After writing the known-empty record, the sync re-resolves the binding and removes the record if the project is no longer linked to that workspace. An unlink forgets the binding before it clears the record, so an unlink in another thread that ran before the write is always seen here. - When document names cannot be loaded, the knowledge line also says how many selected ids were past the lookup limit and never checked. - `snapshotKnownEmpty` doc comment updated for the on-disk record. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018fJ3X7pcGT4R9yzjsJnqsV
1 parent 500a676 commit c600aca

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎packages/opencode/src/altimate/workspace/contents.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ function renderWith(contents: WorkspaceContents, detail: SkillDetail): string {
297297
else if (k?.kind === "all")
298298
lines.push("Knowledge: every document in the organization's knowledge hub (the workspace is not limited to specific documents).")
299299
else if (k?.kind === "selected" && k.names === null)
300-
lines.push(`Knowledge: ${k.selected} selected document${k.selected === 1 ? "" : "s"} (their names could not be loaded).`)
300+
lines.push(
301+
`Knowledge: ${k.selected} selected document${k.selected === 1 ? "" : "s"} (their names could not be loaded` +
302+
`${k.unchecked > 0 ? `; ${k.unchecked} of them are past the lookup limit and were not checked` : ""}).`,
303+
)
301304
else if (k?.kind === "selected" && k.names) {
302305
const more = k.unchecked > 0 ? `${k.unchecked} more selected, not checked` : ""
303306
if (k.names.length > 0) lines.push(`Knowledge documents (${k.names.length}): ${k.names.join(", ")}${more ? `; ${more}` : ""}.`)

‎packages/opencode/src/altimate/workspace/skill-sync.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ export async function snapshotWorkspaceId(directory: string): Promise<number | n
158158
return (await ownManifest(directory))?.datamateId ?? null
159159
}
160160

161-
/** True when the last sync in this process found that workspace `datamateId` has no custom
162-
* skills, for the account configured now. An empty workspace leaves no snapshot (and so no
161+
/** True when the last sync, in any thread or process, found that workspace `datamateId` has no
162+
* custom skills, for the account configured now. The record is a file under the app state
163+
* directory (see `emptyRecordPath`). An empty workspace leaves no snapshot (and so no
163164
* manifest), so without this it reads exactly like one that was never synced. */
164165
export async function snapshotKnownEmpty(directory: string, datamateId: number): Promise<boolean> {
165166
const recorded = await readEmptyRecord(path.resolve(directory))
@@ -1265,6 +1266,11 @@ export async function syncSkills(directory: string): Promise<SyncResult> {
12651266
if (remote.length === 0) {
12661267
await removeManaged(canon)
12671268
await writeEmptyRecord(canon, currentEmptyKey)
1269+
// Not serialized with an unlink in another thread. An unlink forgets the binding before it
1270+
// clears this record, so confirming the link after the write means an unlink that cleared
1271+
// it first is seen here, and the write is undone rather than outliving the link.
1272+
const after = await resolveBindingOutcome(canon).catch(() => null)
1273+
if (after?.status !== "bound" || after.binding.datamateId !== binding.datamateId) await clearEmptyRecord(canon)
12681274
changed = true
12691275
log.info("workspace has no custom skills; removed the local snapshot")
12701276
return

‎packages/opencode/test/altimate/workspace/contents.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ describe("workspace contents section", () => {
101101
expect(capped).not.toContain("(25)")
102102
expect(Contents.render({ ...base, knowledge: sel(2, []) })).toContain("no longer exist")
103103
expect(Contents.render({ ...base, knowledge: sel(23, [], 3) })).toContain("3 more selected, not checked")
104+
expect(Contents.render({ ...base, knowledge: sel(25, null, 5) })).toContain(
105+
"Knowledge: 25 selected documents (their names could not be loaded; 5 of them are past the lookup limit and were not checked).",
106+
)
104107
expect(Contents.render({ ...base, knowledge: null })).not.toContain("Knowledge")
105108
})
106109
})

‎packages/opencode/test/altimate/workspace/skill-sync.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,26 @@ describe("workspace skill sync", () => {
303303
expect(await snapshotKnownEmpty(project, 1)).toBe(false)
304304
})
305305

306+
test("an unlink that lands while an empty sync is in flight is not undone by that sync", async () => {
307+
// The sync and the unlink can run on different threads, where nothing orders them: the
308+
// unlink here completes after the sync read its binding and before it writes the record.
309+
// Only the binding is forgotten: the record does not exist yet, so the unlink's clear of it
310+
// is a no-op, and a purge from this thread would wait on the very sync it interrupts.
311+
serve({})
312+
const inner = globalThis.fetch
313+
let unlinked = false
314+
globalThis.fetch = (async (input: string | URL, init?: RequestInit) => {
315+
if (!unlinked && String(input).includes("datamate_id")) {
316+
unlinked = true
317+
unbind()
318+
}
319+
return inner(input as never, init as never)
320+
}) as unknown as typeof fetch
321+
await syncSkills(project)
322+
expect(unlinked).toBe(true)
323+
expect(await snapshotKnownEmpty(project, 1)).toBe(false)
324+
})
325+
306326
test("the known-empty record is shared on disk, so another thread's unlink clears it here", async () => {
307327
// Threads do not share `globalThis`. The record must live where an unlink in the TUI thread
308328
// and a sync in the prompt worker both see it: the state directory, not the repository.

0 commit comments

Comments
 (0)