Skip to content

Commit 236e73c

Browse files
sahrizviclaude
andauthored
feat(workspace): publish a locally-authored skill to the linked workspace (#1280)
* feat(workspace): publish a locally-authored skill to the linked workspace (#1271) The upload half of `skill-sync.ts`, which only ever pulls. A skill written locally had no route to the workspace, and nothing in the CLI said so. Shaped so agents and commands can ride the same path later: a workspace skill is a named bundle of files, and nothing here is skill-specific except the endpoint it posts to. `collectBundle` and the binary guard take a directory, not a skill. Three rules the module exists to enforce, each a bug if skipped: **Refuse non-UTF-8 files, naming the path.** The wire format is `{path, content}` with content as a STRING — the server does `content.encode("utf-8")` inbound and returns a decoded string outbound. A bundle carrying a PNG cannot round-trip: the declared byte size stops matching after the re-encode and `skill-sync` skips the whole skill, logging a warning nobody sees. Caught at publish it is one clear local error; uncaught, the upload succeeds and the skill silently vanishes from every OTHER machine, days later, with nothing tying symptom to cause. Decoding is strict (`fatal: true`) because the default substitutes U+FFFD and would hand back a "valid" string that reassembles into a different file. **Never publish from the managed snapshot.** `.altimate-code/skill/_workspace` holds skills the workspace sent us and sits under the same `{skill,skills}/**` glob as the user's own — deliberately, since that is how they load. A publish that walked "every skill in this project" would send the workspace's own skills back to it. The check compares against a separator-terminated prefix, so `_workspace-notes` is not mistaken for something inside `_workspace`. **Remember the server's id, so a second publish updates.** Names are unique per creator server-side, so a blind re-create answers 409 rather than duplicating — but that turns an ordinary second publish into an error the user has to interpret. The id lives in a local ledger, not `SKILL.md` frontmatter. Frontmatter is committed, so the id would travel with the skill: a colleague cloning the repo and publishing would UPDATE the original author's bundle rather than create their own. It is keyed on the resolved directory and scoped to the account it was published under, and rows are shape-checked on read rather than cast, so a corrupt entry costs its own skill a re-create instead of a PATCH against a garbage id. `privacy` is left unset — the server defaults to `private`. Publishing should attach a skill to a workspace, not disclose it org-wide as a side effect of a command whose name says nothing about visibility. A 404 on update falls through to create: the skill was deleted in the workspace since we published it, and failing would strand the user with a local id they can neither see nor clear. Tests: 11 new, 443 across `test/altimate/workspace`. Mutation-checked — 8 mutations, 8 killed: non-fatal decoding, dropping the managed-snapshot guard, prefix-matching without the separator, always creating, swallowing the 409, not re-creating after a 404, not recording the id, and defaulting privacy to public each fail a test. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): three review findings on skill publish **A symlinked skill directory defeated the managed-snapshot check.** `path.resolve` is lexical: it normalises `..` and absolutises, but it does not follow links. So a skill directory that IS a link into `.altimate-code/skill/ _workspace` resolved to its own path, passed `isManagedSkill`, and the bundle walk then followed the link — publishing the workspace's own skills back to it under the user's name. Compared through `realpathSync` now, falling back to the lexical form for a path that does not exist, which cannot be a link into the snapshot anyway. **The bundle size guard could not stop the thing it exists to stop.** `collectBundle` read each file with `readFile` and only then checked the running total, so a single oversized file was pulled entirely into memory before being rejected. Size is checked before the read now; the cumulative check stays for many small files and as a backstop if the file grows in between. **A conflicting rename on the update path surfaced a raw API envelope.** The POST path maps 409 to `SkillNameConflictError`; the PATCH path only handled `NotFoundError`, so renaming a skill onto a name this creator already uses reached the caller as the server's own error shape — the exact outcome the typed errors in this module exist to prevent, and invisible from the create path. Tests: 14 in this file, 3 new, whole altimate suite green. Worth recording how the tests were arrived at, because the first versions were worthless: all three mutations SURVIVED. Asserting that an oversized bundle is rejected does not test this fix — the post-read check rejects it too — so the test now patches `readFile` and asserts the oversized file is never read at all. The other two had no coverage whatsoever. All three mutations fail now. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * 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 * fix(workspace): attach a published skill to the linked workspace Creating a skill and attaching it to a workspace are two calls on the server, and only the first was ever made. A skill that is created but attached to nothing appears in no workspace: the CLI lists workspace skills with `GET /skills?datamate_id=`, and so does the web UI. From the user's side, "publish" had done nothing visible — the exact report from workspaces UAT that this feature exists to close. The binding is resolved BEFORE anything is uploaded, and an unlinked project is refused with a typed `NotLinkedError`. Uploading first and failing to attach would create precisely the orphan being fixed. Attachment goes through `PUT /skills/{id}/datamates`, which REPLACES the whole set. A bare put of one id would silently detach the skill from every other workspace it is already on, so the current set is read from `GET /skills/{id}` (`attached_datamate_ids`) and merged. Already attached: no write. Attached on the update path as well: a skill published before this project was linked to its current workspace was otherwise refreshed but still absent from it. On the create path the attach runs AFTER the id is recorded, so a failed attach is retried by the next publish via the update path rather than creating a second copy and 409ing on the name. `AttachFailedError` carries the id so the caller can say exactly that. `PublishReport` gains `datamateId`, so a caller can name the workspace it went to. There is no caller yet — the command wiring is a follow-up — so no error-mapping changes here. Tests: 21 in the file, 5 new. Mutation-checked: never attaching on create, dropping the merge, uploading while unlinked, and skipping the attach on update each fail a test. Two existing tests needed the project linked under each account they switch to, which is what a real account switch resolves. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): the attach envelope, bounded reads, and ledger identity Eleven findings from the bot reviews on #1280. The first is the one that mattered, and the test fixture is how it got through. - `attachToWorkspace` read `attached_datamate_ids` at the top level of `GET /skills/{id}`. The server answers `{skill: {...}}`, so it found nothing, and the replace-set PUT then detached the skill from every workspace it was already on. The test stub served a flat body — a hand-written envelope, not the real one — and passed. The stub now serves the server's shape, and the merge test fails without the unwrap. - Symbolic links inside a skill are refused by name (`SymlinkError`) instead of being silently dropped from the bundle. Local discovery follows links, so the skill worked here and arrived everywhere else incomplete. - The file read is bounded: a stat for the cheap refusal, then a chunked handle read that stops the moment the budget is exceeded, so a file that grew after it was measured cannot be pulled into memory whole. - Uploads get a 120s budget (`timeoutMs` on `altimateRequest`); the shared 15s one covered the request body and could not carry a legal 10MB bundle on an ordinary uplink. - The ledger key uses the skill directory's real path, so one directory reached through a link is one skill rather than a create-then-409; and it carries a digest of the account key, so two users of one tenant do not share an id the server would 403 the second on. - Ledger reads go through the same chain as its writes, the write is atomic (write-then-rename), and a publish holds a per-directory lock, so two publishes of the same skill at once create it once. - An empty skill directory is `EmptyBundleError`, not a size error; `BundleTooLargeError` sets its name like its siblings. Tests: credentials are restored after every test, every bind awaits its detached work, and the update-path attach test re-links the project to a second workspace so it covers the case its comment described. Verified: `test/altimate/workspace` + `test/altimate/plugin` green (500), typecheck clean. Mutation-checked: reverting the envelope unwrap, the symlink refusal, the chunk bound, the stat refusal, the real-path identity, the key digest, the publish lock and the empty-bundle type each fail a test. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): a recorded id the server says is not ours falls through to create The legacy ledger keys predate creator scoping, so on a shared machine a row another user of the same tenant wrote can be found; the server answers the PATCH with 403. That skill is theirs — publish creates our own under the scoped key, the same way a 404 does. The size-guard test now asserts that the oversized file specifically was never read, rather than that nothing was, since directory order is the filesystem's. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): publish the whole bundle as a replacement, and tell the update path's conflicts apart Sarav's review. `files` replaces the bundle server-side, and a path it omits is a deletion the server refuses unless the caller says so — so deleting a file locally and publishing again 409'd, forever, and the 409 was reported as a name conflict: "it was published from somewhere else, rename this one". The name was never the problem, renaming does not help, and the real cause was never mentioned. - The PATCH sends `replace_bundle: true`. `collectBundle` walks the whole directory, so what it sends IS the bundle and a missing path is the user's own deletion — which is the case the flag exists for. - The update path's 409s are told apart by the server's detail: a name collision stays `SkillNameConflictError`, a lost compare-and-swap (the skill was edited in the workspace mid-upload) is the new `SkillChangedElsewhereError` — "publish again", not "rename" — and anything unrecognised keeps the server's own words rather than being relabelled. - `collectBundle` skips what a working directory accumulates: `.env` and `.env.*`, `.DS_Store`, editor backups and swap files, and never walks `.git`, `node_modules` or `__pycache__`. A public skill's bundle is readable tenant-wide and a secret that reaches it cannot be recalled by deleting the local file. - The file-count ceiling is checked before the read, so file 201 is no longer read and decoded before being rejected. Verified: 505 pass across the workspace + plugin suites, typecheck clean. Mutation-checked: dropping `replace_bundle`, collapsing the 409s, and skipping either junk guard each fail a test. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): junk-file names are case-folded, and a worktree's .git file is junk too - `.ENV` and `.ENV.production` are the same file as `.env` on the case-insensitive file systems Windows and macOS default to, and slipped past a case-sensitive match. - `git worktree add` leaves `.git` as a regular file, which the directory skip did not see. - The update-path conflict test now actually renames: the second publish carries a new name, and the assertions pin that it went out as a PATCH rename rather than a create. Verified: 506 pass across the workspace + plugin suites, typecheck clean. Mutation-checked: dropping the case fold and dropping the `.git` file rule each fail a test. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): the server's file ceiling, no orphan on a workspace we do not own, a ledger that survives key rotation Ralph's review, verified against altimate-backend origin/development. - `MAX_BUNDLE_FILES` is 100, the server's (`bundle.py:28`). At 200 a 101–200 file bundle passed locally, uploaded in full, and was refused with a 400 — the case the constant exists to prevent. The ceiling is now checked before file 101 is opened. - A project linked to a workspace the caller does not own is refused before anything is uploaded (`NotWorkspaceOwnerError`). Linking needs only visibility (`bind_existing`), attaching needs ownership (`_lock_workspaces`, 404 otherwise), so such a project created a skill the CLI could never attach — on every publish. The workspace list carries each owner and `GET /users/me` says who we are. When the list cannot say (an older server) and the attach 404s straight after a create, the skill just created is deleted and its id forgotten, so nothing is left behind. - The ledger is scoped by user id (`GET /users/me`), not a digest of the API key: a rotated key made every published id on this machine unreachable, and the next publish created again — 409 on the name, "published from somewhere else". Rows carry `created_by`; a legacy row is trusted only once `GET /skills/{id}` confirms the skill is this user's, and is re-homed under the current key. All three earlier key shapes are still read. - `.envrc` joins the junk list, and the list's comment says outright that it is a blocklist and therefore incomplete. Verified: 511 pass across the workspace + plugin suites, typecheck clean. Mutation-checked: the 200 ceiling, dropping the ownership pre-flight, dropping the create-path compensation, keying on the digest, trusting a legacy row without asking, and dropping `.envrc` each fail a test. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 * fix(workspace): find a legacy ledger row by directory, not by a digest nobody can recompute Found end-to-end against prod, not by the suite: the rotation test seeded the digest of the CURRENT key, so the digest-shaped fallback matched and the test passed — while a real rotation leaves on disk the digest of a key nobody has any more, which nothing can recompute. Ralph's R-C still reproduced: create, 409 on the name, "published from somewhere else". The fallback now scans for any row for this directory under this account, whatever key shape an earlier version wrote it with, and the server decides whose skill it is (the `created_by` check already in place). The digest helper is gone; it served nothing. The test seeds a foreign digest. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6 --------- Co-authored-by: Claude Opus 5 <[email protected]>
1 parent 2bd6632 commit 236e73c

3 files changed

Lines changed: 1609 additions & 4 deletions

File tree

‎packages/opencode/src/altimate/workspace/api-client.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export interface DatamateRef {
2121
* toggle in the workspace app, so callers that write memory must respect it.
2222
* Undefined when the backend omitted the field. */
2323
memoryEnabled?: boolean
24+
/** The user who owns the workspace. Linking needs only visibility, but
25+
* attaching a skill is a write against the workspace and needs ownership —
26+
* a caller that can see a colleague's shared workspace may link to it and
27+
* still not publish into it. Undefined when the backend omitted the field. */
28+
ownerId?: number
2429
}
2530

2631
export interface Binding {
@@ -153,14 +158,19 @@ async function req<T>(
153158
* instead of throwing. Only set for endpoints known to return 204 or a
154159
* bare 200 with no payload. */
155160
allowEmptyBody?: boolean
161+
/** Override the shared 15s budget. That budget was sized for small JSON
162+
* exchanges and covers the request body too, so a call that uploads
163+
* megabytes (a skill bundle) needs its own. */
164+
timeoutMs?: number
156165
} = {},
157166
): Promise<T> {
167+
const timeoutMs = opts.timeoutMs ?? REQUEST_TIMEOUT_MS
158168
const { url, instance, apiKey } = await creds()
159169
const qs = opts.query ? "?" + new URLSearchParams(opts.query).toString() : ""
160170
const basePath = opts.base ?? "/datamate-project-bindings"
161171
const target = `${url}${basePath}${subpath}${qs}`
162172
const controller = new AbortController()
163-
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
173+
const timeout = setTimeout(() => controller.abort(), timeoutMs)
164174
let res: Response
165175
let text: string
166176
try {
@@ -204,7 +214,7 @@ async function req<T>(
204214
const name = (err as { name?: string } | undefined)?.name
205215
if (name === "AbortError") {
206216
throw new WorkspaceApiError(
207-
`Request to ${target} timed out after ${Math.round(REQUEST_TIMEOUT_MS / 1000)}s`,
217+
`Request to ${target} timed out after ${Math.round(timeoutMs / 1000)}s`,
208218
)
209219
}
210220
const msg = err instanceof Error ? err.message : String(err)
@@ -454,7 +464,7 @@ export namespace WorkspaceApi {
454464
// bare ``[...]``, and a generic ``{data: [...]}`` — so a backend
455465
// contract change (or compat layer) doesn't silently empty the picker.
456466
// (cubic-dev-ai round 3.)
457-
type Row = { id: number | string; name: string; memory_enabled?: boolean }
467+
type Row = { id: number | string; name: string; memory_enabled?: boolean; user_id?: number }
458468
const body = await req<Row[] | { datamates?: Row[]; data?: Row[] }>("GET", "/", {
459469
base: "/datamates",
460470
})
@@ -482,7 +492,22 @@ export namespace WorkspaceApi {
482492
// per-element rather than per-envelope malformed value.
483493
return rows
484494
.filter((d): d is Row => d !== null && typeof d === "object")
485-
.map((d) => ({ id: Number(d.id), name: d.name, memoryEnabled: d.memory_enabled }))
495+
.map((d) => ({
496+
id: Number(d.id),
497+
name: d.name,
498+
memoryEnabled: d.memory_enabled,
499+
ownerId: Number.isInteger(d.user_id) ? d.user_id : undefined,
500+
}))
486501
.filter((d) => Number.isInteger(d.id) && d.id > 0 && typeof d.name === "string")
487502
}
503+
504+
/** The caller's own user id, from ``GET /users/me``. Needed wherever the
505+
* client must compare ownership — skill attachment requires the caller to
506+
* OWN the workspace, and the credentials carry no user id of their own. */
507+
export async function whoami(): Promise<number> {
508+
const me = await req<{ id?: unknown }>("GET", "/me", { base: "/users" })
509+
const id = Number(me?.id)
510+
if (!Number.isInteger(id) || id <= 0) throw new WorkspaceApiError("The server did not say who this account is.")
511+
return id
512+
}
488513
}

0 commit comments

Comments
 (0)