Skip to content

Commit 2be242d

Browse files
sahrizviclaude
andcommitted
fix(workspace): scope the published-skill ledger by account and serialise its writes
Two more from the cubic review on #1280. Both are the ledger describing something other than what is actually on the server. **One directory, two accounts, one id.** The ledger keyed on the resolved skill directory alone, so publishing the same skill under a second account overwrote the first account's record. Switching back found a row scoped to the other tenant, treated the skill as unpublished, created it again — and 409'd on the name that was already there, with the original id no longer reachable from this machine. The key now carries tenant and API URL alongside the directory, so each account keeps its own id. Reads still fall back to the old directory-only key, so ids written by an earlier version are not stranded into a needless re-create; the tenant check stays, because that fallback can return another account's row. **Concurrent publishes dropped each other's ids.** Each publish read the whole ledger, mutated its copy and wrote it back, so of two publishes in flight the later write carried the earlier one away, and that skill created again on its next run. Writes go through a promise chain now, and the re-read happens INSIDE the chain — reusing a copy read before the previous write landed would lose it just the same. Same shape `memory-index` already uses for the same reason. Tests: 16 in this file, 2 new, whole altimate suite green (5799 tests). Mutation-checked: keying by directory alone fails one, dropping the chain fails four. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 77256d0 commit 2be242d

2 files changed

Lines changed: 92 additions & 14 deletions

File tree

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,53 @@ async function readLedger(): Promise<Record<string, PublishedRecord>> {
226226
}
227227
}
228228

229-
/** Keyed on the resolved skill directory so a rename of the skill's *name* does
230-
* not orphan its id, and two skills in different projects cannot collide. */
229+
/** Keyed on the resolved skill directory AND the account it was published
230+
* under, so a rename of the skill's *name* does not orphan its id, two skills in
231+
* different projects cannot collide, and — the reason the account is in the key
232+
* — publishing one directory to two accounts keeps an id for each.
233+
*
234+
* A bare directory key held one record, so switching accounts overwrote the
235+
* previous account's id: switching back created a second skill and then 409'd on
236+
* the name that was already there, with no way to reach the original. */
237+
function ledgerKey(skillDir: string, scope: { tenant: string; apiUrl: string }): string {
238+
return `${scope.tenant}|${scope.apiUrl}|${path.resolve(skillDir)}`
239+
}
240+
241+
/** Serialises ledger writes, the same way `memory-index` serialises its own.
242+
* Two publishes running at once each read, mutate and write the whole file, so
243+
* the later write dropped the earlier one's id — and that skill's next publish
244+
* created again and 409'd on its own name. */
245+
let ledgerWriteChain: Promise<void> = Promise.resolve()
246+
231247
async function recordPublished(skillDir: string, record: PublishedRecord): Promise<void> {
232-
const ledger = await readLedger()
233-
ledger[path.resolve(skillDir)] = record
234-
try {
235-
await Filesystem.writeJson(ledgerPath(), ledger)
236-
} catch (err) {
237-
// Best-effort. Losing the id costs a 409 on the next publish, not data.
238-
log.warn("could not record the published skill id", { err: String(err) })
239-
}
248+
const task = ledgerWriteChain.then(async () => {
249+
try {
250+
// Re-read INSIDE the chain: a copy read before the previous write landed
251+
// would carry that write away again when this one persists.
252+
const ledger = await readLedger()
253+
ledger[ledgerKey(skillDir, { tenant: record.tenant, apiUrl: record.apiUrl })] = record
254+
await Filesystem.writeJson(ledgerPath(), ledger)
255+
} catch (err) {
256+
// Best-effort. Losing the id costs a 409 on the next publish, not data.
257+
log.warn("could not record the published skill id", { err: String(err) })
258+
}
259+
})
260+
ledgerWriteChain = task.catch(() => {})
261+
return task
240262
}
241263

242264
async function knownPublicId(skillDir: string): Promise<string | null> {
243265
const creds = await AltimateApi.getCredentials().catch(() => null)
244266
if (!creds) return null
245-
const record = (await readLedger())[path.resolve(skillDir)]
267+
const scope = { tenant: creds.altimateInstanceName, apiUrl: creds.altimateUrl }
268+
const ledger = await readLedger()
269+
// Composite key first; fall back to the old directory-only key so ids written
270+
// by an earlier version are not stranded into a needless re-create.
271+
const record = ledger[ledgerKey(skillDir, scope)] ?? ledger[path.resolve(skillDir)]
246272
if (!record) return null
247-
// Scoped to the account it was published under. The same checkout pointed at a
248-
// different tenant must not update an id that does not exist there.
249-
if (record.tenant !== creds.altimateInstanceName || record.apiUrl !== creds.altimateUrl) return null
273+
// Still checked, not implied by the key: the fallback lookup above can return
274+
// a legacy row belonging to another account.
275+
if (record.tenant !== scope.tenant || record.apiUrl !== scope.apiUrl) return null
250276
return record.publicId
251277
}
252278

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,55 @@ describe("the bundle size guard", () => {
281281
expect(err).toBeInstanceOf(SkillNameConflictError)
282282
})
283283
})
284+
285+
describe("the published-id ledger", () => {
286+
test("keeps a separate id per account for the same skill directory", async () => {
287+
// A bare directory key held ONE record, so publishing to a second account
288+
// overwrote the first account's id. Switching back created a second skill
289+
// and 409'd on the name already there, with no way to reach the original.
290+
await publish() // account "acme" -> pub-1
291+
expect(requests.filter((r) => r.method === "POST")).toHaveLength(1)
292+
293+
// Switch accounts, publish the same directory.
294+
;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = async () =>
295+
({ altimateInstanceName: "other", altimateUrl: "https://api.example.com", altimateApiKey: "k" }) as Creds
296+
requests = []
297+
await publish() // must CREATE for "other", not update acme's id
298+
expect(requests.filter((r) => r.method === "POST")).toHaveLength(1)
299+
300+
// Back to the first account: its id must still be there, so this UPDATES.
301+
;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = async () =>
302+
({ altimateInstanceName: "acme", altimateUrl: "https://api.example.com", altimateApiKey: "k" }) as Creds
303+
requests = []
304+
const report = await publish()
305+
306+
expect(report.action).toBe("updated")
307+
expect(requests.filter((r) => r.method === "POST")).toHaveLength(0)
308+
})
309+
310+
test("concurrent publishes do not drop each other's id", async () => {
311+
// Each publish read, mutated and wrote the whole ledger, so the later write
312+
// carried the earlier one away and that skill re-created on its next run.
313+
const other = path.join(project, "skills", "second")
314+
mkdirSync(other, { recursive: true })
315+
writeFileSync(path.join(other, "SKILL.md"), "---\nname: second\n---\n")
316+
317+
await Promise.all([
318+
publish(),
319+
publishSkill({ projectDirectory: project, skillDirectory: other, name: "second", description: "d" }),
320+
])
321+
322+
// Both ids survived: neither directory creates again.
323+
requests = []
324+
const a = await publish()
325+
const b = await publishSkill({
326+
projectDirectory: project,
327+
skillDirectory: other,
328+
name: "second",
329+
description: "d",
330+
})
331+
expect(a.action).toBe("updated")
332+
expect(b.action).toBe("updated")
333+
expect(requests.filter((r) => r.method === "POST")).toHaveLength(0)
334+
})
335+
})

0 commit comments

Comments
 (0)