Skip to content

Commit 266e123

Browse files
sahrizviclaude
andcommitted
fix(workspace): the project boundary is the worktree root, and containment is by path segment
The previous commit's containment check regressed a valid case: it compared against the session's directory, but discovery walks up to the worktree root, so `skill publish x` run from `repo/models` refused `repo/.opencode/skills/x`. `publishSkill` takes a separate `projectRoot` boundary (the worktree on both surfaces; the session directory for a project with none) while the binding stays keyed on `projectDirectory`. `skillSource` contains by path segment (`path.relative`), not by string prefix: `~/.claude/skills-archive/x` is not inside `~/.claude/skills`, and the prefix check refused it as personal. The real path that passed the check is what `collectBundle` walks, so a root swapped after the check is not what uploads. The ledger-identity test now reaches the skill through a symlinked PARENT so it exercises canonicalisation on Linux too. Verified: 649 pass across the workspace, plugin, fork-guard and skill suites, typecheck clean. Mutation-checked: comparing against the session directory, and containing by prefix, each fail a test; reading the lexical root instead of the validated one has no observable difference without a concurrent writer, and is closed by construction. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 2346b7a commit 266e123

6 files changed

Lines changed: 87 additions & 30 deletions

File tree

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -593,29 +593,35 @@ async function attachToWorkspace(publicId: string, datamateId: number): Promise<
593593
* `privacy` is left unset: the server defaults to `private`. Publishing should
594594
* attach a skill to a workspace, not disclose it to the whole organisation as a
595595
* side effect of a command whose name says nothing about visibility. */
596-
export async function publishSkill(input: {
596+
export interface PublishInput {
597+
/** Where the workspace binding lives — the directory the session was
598+
* started in. `resolveBinding` is keyed on it. */
597599
projectDirectory: string
600+
/** The boundary a skill must lie within to count as this project's.
601+
* Discovery walks up to the git worktree root, so a skill under
602+
* `repo/.opencode/skills` is the project's even when the session started
603+
* in `repo/models` — and `projectDirectory` alone would refuse it.
604+
* Defaults to `projectDirectory` for a project with no worktree. */
605+
projectRoot?: string
598606
skillDirectory: string
599607
name: string
600608
description: string
601-
}): Promise<PublishReport> {
609+
}
610+
611+
export async function publishSkill(input: PublishInput): Promise<PublishReport> {
602612
return withPublishLock(input.skillDirectory, () => publishSkillUnlocked(input))
603613
}
604614

605-
async function publishSkillUnlocked(input: {
606-
projectDirectory: string
607-
skillDirectory: string
608-
name: string
609-
description: string
610-
}): Promise<PublishReport> {
615+
async function publishSkillUnlocked(input: PublishInput): Promise<PublishReport> {
611616
if (isManagedSkill(input.projectDirectory, input.skillDirectory))
612617
throw new ManagedSkillError(input.skillDirectory)
613618
// The root itself, resolved: `collectBundle` refuses links INSIDE the
614619
// skill, but a root that is a link is followed, and would publish whatever
615620
// it points at. And the resolved root must be inside the project: the
616621
// loader also serves personal skills from under the home directory, which
617-
// are not this workspace's to receive.
618-
assertProjectSkill(input.projectDirectory, input.skillDirectory)
622+
// are not this workspace's to receive. The REAL path that passed is what
623+
// the walk reads, so a root swapped after the check is not what uploads.
624+
const skillRoot = assertProjectSkill(input.projectRoot ?? input.projectDirectory, input.skillDirectory)
619625

620626
// Before the bundle is even read. An unlinked project has nowhere to attach
621627
// to, and uploading first would create the orphan this module exists to
@@ -636,7 +642,7 @@ async function publishSkillUnlocked(input: {
636642
// uploaded until the attach is known to be possible.
637643
await assertOwnsWorkspace(binding.datamateId, binding.datamateName, scope.userId)
638644

639-
const files = await collectBundle(input.skillDirectory)
645+
const files = await collectBundle(skillRoot)
640646
if (files.length === 0) throw new EmptyBundleError()
641647
const bytes = files.reduce((n, f) => n + Buffer.byteLength(f.content, "utf8"), 0)
642648

@@ -783,14 +789,15 @@ function updateConflict(err: ConflictError, skillName: string): Error {
783789
return err
784790
}
785791

786-
function assertProjectSkill(projectDirectory: string, skillDirectory: string): void {
792+
/** The skill's real directory, once it has passed. */
793+
function assertProjectSkill(projectRoot: string, skillDirectory: string): string {
787794
const lexical = path.resolve(skillDirectory)
788795
let real: string
789796
try {
790797
real = realpathSync(lexical)
791798
} catch {
792799
// Absent: `collectBundle` fails on it in a moment with a better message.
793-
return
800+
return lexical
794801
}
795802
// "The root is a link" is judged on the LAST component only: the parent's
796803
// real path plus the skill's own name must equal the skill's real path.
@@ -800,16 +807,18 @@ function assertProjectSkill(projectDirectory: string, skillDirectory: string): v
800807
try {
801808
parentReal = realpathSync(path.dirname(lexical))
802809
} catch {
803-
return
810+
return lexical
804811
}
805812
if (real !== path.join(parentReal, path.basename(lexical))) throw new NotProjectSkillError(skillDirectory)
806-
let project: string
813+
let root: string
807814
try {
808-
project = realpathSync(projectDirectory)
815+
root = realpathSync(projectRoot)
809816
} catch {
810-
project = path.resolve(projectDirectory)
817+
root = path.resolve(projectRoot)
811818
}
812-
if (real !== project && !real.startsWith(project + path.sep)) throw new NotProjectSkillError(skillDirectory)
819+
const rel = path.relative(root, real)
820+
if (rel.startsWith("..") || path.isAbsolute(rel)) throw new NotProjectSkillError(skillDirectory)
821+
return real
813822
}
814823

815824
/** Refuse before upload when the bound workspace is not the caller's. Read

‎packages/opencode/src/cli/cmd/skill-helpers.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,27 @@ export function skillSource(location: string): string {
6161
if (location.startsWith("builtin:")) return "builtin"
6262
const home = Global.Path.home
6363
// Builtin skills shipped with altimate-code
64-
if (location.startsWith(path.join(home, ".altimate", "builtin"))) return "builtin"
64+
if (isInside(location, path.join(home, ".altimate", "builtin"))) return "builtin"
6565
// Global user skills (~/.claude/skills/, ~/.agents/skills/, ~/.config/altimate-code/skills/)
6666
const globalDirs = [
6767
path.join(home, ".claude", "skills"),
6868
path.join(home, ".agents", "skills"),
6969
path.join(home, ".altimate-code", "skills"),
7070
path.join(Global.Path.config, "skills"),
7171
]
72-
if (globalDirs.some((dir) => location.startsWith(dir))) return "global"
72+
if (globalDirs.some((dir) => isInside(location, dir))) return "global"
7373
// Everything else is project-level
7474
return "project"
7575
}
7676

77+
/** Path containment by segments, not by string prefix: `~/.claude/skills-archive/x`
78+
* is not inside `~/.claude/skills`, and a raw `startsWith` said it was — which
79+
* refused a project skill as personal. */
80+
function isInside(location: string, dir: string): boolean {
81+
const rel = path.relative(dir, location)
82+
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel))
83+
}
84+
7785
/** Check if a tool is available on the current PATH (including .altimate-code/tools/ and .opencode/tools/). */
7886
export async function isToolOnPath(toolName: string, cwd: string): Promise<boolean> {
7987
// Check project tools/ in both cwd and worktree (they may differ in monorepos)

‎packages/opencode/src/cli/cmd/skill.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ const SkillPublishCommand = cmd({
500500
try {
501501
const report = await publishSkill({
502502
projectDirectory: Instance.directory,
503+
// Discovery walks up to the worktree; so must the boundary, or a
504+
// skill under the repository root is refused from a subdirectory.
505+
projectRoot: Instance.worktree !== "/" ? Instance.worktree : Instance.directory,
503506
skillDirectory: path.dirname(skill.location),
504507
name: skill.name,
505508
description: skill.description ?? "",

‎packages/opencode/src/plugin/tui/altimate/skill-ops.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,12 @@ export async function publishFromPicker(
531531
info: SkillInfo,
532532
skillName: string,
533533
projectDirectory: string,
534+
projectRoot: string,
534535
): Promise<{ message: string; variant: "success" | "warning" | "error"; duration: number }> {
535536
try {
536537
const report = await publishSkill({
537538
projectDirectory,
539+
projectRoot,
538540
skillDirectory: path.dirname(info.location),
539541
name: skillName,
540542
description: info.description ?? "",
@@ -570,6 +572,9 @@ function openActionPicker(api: TuiPluginApi, info: SkillInfo | undefined, skillN
570572
// managed snapshot live under `api.state.path.directory`, not the git root
571573
// `workdir` resolves to, and the two differ in a worktree subdirectory.
572574
const projectDirectory = api.state.path.directory || workdir(api)
575+
// The boundary a skill must lie within: the worktree, since discovery
576+
// walks up to it. `workdir` already resolves that.
577+
const projectRoot = workdir(api)
573578
const managed = !isBuiltin && isManagedSkill(projectDirectory, path.dirname(info!.location))
574579

575580
const actions: TuiDialogSelectOption<string>[] = (
@@ -642,7 +647,7 @@ function openActionPicker(api: TuiPluginApi, info: SkillInfo | undefined, skillN
642647
publishInFlight = skillName
643648
try {
644649
api.ui.toast({ message: `Publishing ${skillName}...`, variant: "info", duration: 120_000 })
645-
api.ui.toast(await publishFromPicker(info, skillName, projectDirectory))
650+
api.ui.toast(await publishFromPicker(info, skillName, projectDirectory, projectRoot))
646651
} finally {
647652
publishInFlight = null
648653
}

‎packages/opencode/test/altimate/plugin/skill-ops-builtin.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ describe("the action picker's notion of built-in", () => {
4141
expect(isGlobalLocation("/some/project/.opencode/skills/deploy/SKILL.md")).toBe(false)
4242
})
4343
})
44+
45+
describe("skillSource contains by path segment, not by string prefix", () => {
46+
test("a sibling directory sharing a global dir's prefix is a project skill", () => {
47+
// `~/.claude/skills-archive/x` starts with the string `~/.claude/skills`
48+
// but is not inside it; a raw prefix check refused it as personal.
49+
const sibling = path.join(Global.Path.home, ".claude", "skills-archive", "x", "SKILL.md")
50+
expect(skillSource(sibling)).toBe("project")
51+
expect(isGlobalLocation(sibling)).toBe(false)
52+
const builtinSibling = path.join(Global.Path.home, ".altimate", "builtin-old", "x", "SKILL.md")
53+
expect(skillSource(builtinSibling)).toBe("project")
54+
})
55+
})

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,17 +581,17 @@ describe("the published-id ledger", () => {
581581
// `/private/tmp`, a linked worktree) was two ledger keys, so the second
582582
// publish created again and 409'd on its own name — "published from
583583
// somewhere else", by this machine, a moment ago.
584-
// The two spellings: the sandbox's lexical path and its real path. On
585-
// macOS `os.tmpdir()` is under `/var`, a link to `/private/var`, so these
586-
// differ; elsewhere they are equal and the test still holds trivially.
587-
// (Not a symlinked skill root — that is refused on purpose, see "what
588-
// counts as a project skill".)
589-
const lexical = skillDir
590-
const real = realpathSync(skillDir)
591-
await publishSkill({ projectDirectory: project, skillDirectory: lexical, name: "deploy", description: "d" })
584+
// Two spellings of one directory, on every platform: the project's real
585+
// path, and the project reached through a symlinked PARENT. Only the
586+
// skill's own last component may not be a link, so a linked ancestor is
587+
// allowed — it is the `/var` → `/private/var` case, made explicit.
588+
const linkedParent = path.join(SANDBOX, `via-link-${Math.random().toString(36).slice(2)}`)
589+
symlinkSync(project, linkedParent)
590+
const viaLink = path.join(linkedParent, "skills", "deploy")
591+
await publishSkill({ projectDirectory: project, skillDirectory: viaLink, name: "deploy", description: "d" })
592592
requests = []
593593

594-
const report = await publishSkill({ projectDirectory: project, skillDirectory: real, name: "deploy", description: "d" })
594+
const report = await publishSkill({ projectDirectory: project, skillDirectory: skillDir, name: "deploy", description: "d" })
595595

596596
expect(report.action).toBe("updated")
597597
expect(requests.filter((r) => r.method === "POST")).toHaveLength(0)
@@ -657,6 +657,26 @@ describe("what counts as a project skill", () => {
657657
expect(requests.filter((r) => r.method === "POST")).toHaveLength(0)
658658
})
659659

660+
test("a skill under the repository root publishes from a subdirectory", async () => {
661+
// Discovery walks up to the worktree root, so a session started in
662+
// `repo/models` can reach `repo/.opencode/skills/x`. The binding stays
663+
// keyed on the session's directory; the containment boundary is the root.
664+
const sub = path.join(project, "models")
665+
mkdirSync(sub, { recursive: true })
666+
await link(42, "Growth", sub)
667+
requests = []
668+
669+
const report = await publishSkill({
670+
projectDirectory: sub,
671+
projectRoot: project,
672+
skillDirectory: skillDir, // repo/skills/deploy — above `sub`
673+
name: "deploy",
674+
description: "d",
675+
})
676+
677+
expect(report.action).toBe("created")
678+
})
679+
660680
test("a skill outside the project is refused", async () => {
661681
// A personal skill under the home directory is the user's, not this
662682
// project's, and publishing would share it with the whole workspace.

0 commit comments

Comments
 (0)