Skip to content

feat(daemon): content protocol in the sandbox daemon - #7877

Closed
tlgimenes wants to merge 27 commits into
mainfrom
split/02-sandbox-daemon
Closed

tlgimenes wants to merge 27 commits into
mainfrom
split/02-sandbox-daemon

Conversation

@tlgimenes

@tlgimenes tlgimenes commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Part of the v8 Studio stack. Base: #7796. This PR folds the whole Go daemon track (#7812, #7813, #7814, #7815, #7847, #7817, #7818, #7819, #7820) into one PR.

The sandbox daemon now speaks the Deco content protocol over the working tree. The site editor can then edit a v8 (Blocks) site running in a sandbox the same way it edits a local deco serve. It is a Go port of @decocms/blocks/protocol.

PO decision, 2026-10-13: "Uploading directly to the fs should be done only on /site-editor play mode. Hosted mode (with a project etc) should use our s3 service, and sandbox has nothing to do with it." So the daemon takes no asset uploads: describe reports assets: null, and a sandbox session uploads to Studio's existing S3 asset service (POST /api/:org/file-configs/:id/upload, assets.decocms.com/<site>/…), like a hosted project. The upload route this PR used to add (PUT /_sandbox/assets/{name...}, was #7818/#7819) is gone (d6d03a7). This PR adds exactly one route: /_sandbox/rpc.

What it adds

All new code is in packages/sandbox/daemon-go/internal/content/ plus internal/routes/content.go:

Why one PR

The PO asked to release the hard-to-roll-back pieces first, at the base of the stack: #7796 (the org_sites migration), then this PR (the sandbox image). Everything else stacks on top. Shipping the daemon as one unit means one image bump and one revert if needed. The same code was already reviewed commit by commit in the folded PRs. Their history is kept in this branch, which is a normal merge of split/19-daemon-e2e-conformance.

Additive only

The PO's requirement is that the daemon only adds APIs and never removes or changes existing ones. Everything was checked against origin/main (21912beaf), whose packages/sandbox is identical to this PR's base.

1. HTTP routes. I diffed every mux.HandleFunc/Handle registration with grep -rhoE '(HandleFunc|Handle)\([^,]+' --include='*.go' --exclude='*_test.go'. Main has 31 and this branch has 32 (re-checked @ d6d03a7; the only diff line is HandleFunc(method+" "+pre+"/rpc"). The new one sits inside registerSandboxRoutes, prefix /_sandbox:

Route Methods Auth Before this PR
/_sandbox/rpc GET POST PUT PATCH DELETE (anything but POST gets the protocol's 405) d.authed (daemon token) Fell through to the authed /_sandbox/ catch-all: 404 Not found

/_sandbox/assets/* is not registered: it still falls through to the authed 404 catch-all, as on main.

No existing registration was removed or changed (path, method, wrapper and handler are all the same). The new path doesn't collide with any fs route name (read write unlink mkdir rename edit grep glob bash), and OPTIONS preflight still goes to the existing corsPreflight. The only observable change: an authed request to /_sandbox/rpc used to get a 404 and now reaches the content protocol. Unauthenticated requests are refused exactly as before.

2. Exported Go API. apidiff skips internal/ packages, which is all of this module, so I dumped go doc -all for every package on both sides and diffed the dumps. 0 lines removed or changed. Lines added:

  • internal/content: a new package.
  • internal/routes: type Content, type ContentDeps, func NewContent(ContentDeps) *Content and (*Content).RPC. All live in a new file, routes/content.go.
  • internal/worktree: func (l *Lock) AcquireWithin(d time.Duration) (release func(), ok bool). Acquire() is unchanged.

The only pre-existing Go files touched are internal/worktree/lock.go, which gains AcquireWithin and a time import, and main.go. In main.go the diff is additions only (no - line): one field is added to the unexported sandboxHandlers struct, the loop above registers the route, and main() builds routes.NewContent from the existing repoDir, d.store, d.treeLock and fsDeps.OnWorkingTreeWrite.

3. Config, env and flags. I extracted every os.Getenv/os.LookupEnv/flag.* call: 24 on each side, identical. internal/content and routes/content.go read none, and there are no new CLI flags or config fields.

4. Behaviour of existing code paths. No diff hunk modifies the body of a pre-existing function, except the registerSandboxRoutes registrations and the main() wiring listed above. Specifically:

  • treeLock is still taken the same way by fs and git routes. The new code takes it only through AcquireWithin, and only for a content commit.
  • .git/info/exclude entries (/<app>/.deco/.blocks.lock, /<app>/.deco/.tx-*/) are added lazily, the first time a content route is used. They are never added at boot.
  • /_sandbox/decofile (v7) is unchanged. It reflects protocol writes because they're real working-tree writes reported through the existing OnWorkingTreeWrite hook.
  • The existing Go tests (main package, routes, gitx, …) pass unchanged.

Commands (run from packages/sandbox/daemon-go, with main extracted via git archive origin/main packages/sandbox/daemon-go):

for p in $(go list ./internal/...); do go doc -all -short $p; go doc -all $p | grep -E '^(func|type|var|const|    func|\t[A-Z])'; done > doc-<side>.txt
diff doc-main.txt doc-new.txt | grep -c '^<'            # 0
grep -rhoE '(HandleFunc|Handle)\([^,]+' --include='*.go' --exclude='*_test.go' . | sort   # routes
grep -rhoE '(os\.(Getenv|LookupEnv)\("[^"]+"|flag\.[A-Za-z]+\("[^"]+")' --include='*.go' --exclude='*_test.go' . | sort -u   # env/flags

Rollout and rollback

  • Rollout: this PR only adds one route, and nothing calls it until the web track (the content backend in the editor) lands further up the stack. A sandbox image built from this PR behaves exactly like today's for every existing caller.
  • Rollback: revert this PR and ship a new sandbox image. Live pods keep the image they started with, so nothing changes under a running sandbox. Old images answer the authed 404 for /_sandbox/rpc again.

Tests

  • go vet ./...: clean.
  • go test -p 1 ./... and go vet ./... in packages/sandbox/daemon-go: all packages pass, run locally @ d6d03a7 (earlier tree: -race too).
  • bun install --frozen-lockfile: passes.
  • CI after removing the upload route (d6d03a7): Sandbox 38398232178 success, daemon-e2e 304 pass, 0 fail (305 tests, 16 files). The stack top (split/51, which contains this) also passed Checks & Unit Tests, E2E and Sandbox.
  • CI on the earlier tree (dispatched, because stacked PRs only get Gitleaks and cubic automatically) passed:
    • Sandbox 38251351755: daemon-e2e (305 e2e tests across 16 files, including the content-protocol conformance) and docker-smoke.
    • Checks & Unit Tests 38251356693.
    • E2E Tests 38251362384.
  • Local: the content-protocol e2e against a locally built daemon gave 79 pass, 0 fail.

Stack

#7796 (base, org_sites migration) → this PR (sandbox daemon) → API track (#7800 …) / web track (#7803 …) → e2e (#7841 → #7842 → #7843) → #7871 – #7876.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig

tlgimenes and others added 25 commits October 8, 2026 10:35
…ever reused

Migration 235 adds org_sites.project_id (unique, ON DELETE SET NULL) and
linked_at, backfills the single project per slug (metadata.siteSlug, then
title; ambiguous/none left unlinked and counted), and turns the org FK into
ON DELETE SET NULL so deleting an org leaves an unclaimable tombstone.

The port gains getByProject/link with typed refusals; claim/reassign/release
refuse tombstones and slugs a project has used. Project reads overlay the
linked slug onto metadata.siteSlug; every write path (virtual MCP update,
connections update on a project row, VirtualMCPStorage.update) refuses
changing a site slug. Create links the imported slug once. Project settings
show the site id read-only; deployment admin can't remove or move a used slug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
…only relink

Review follow-ups for the org_sites ↔ project link:

- Migration 235 plans the backfill with reads only, then takes the ALTER
  locks under a 5s lock_timeout and applies the plan in batched set-based
  UPDATEs (a project deleted since planning is skipped, not an FK error).
  The org FK is added NOT VALID then validated.
- Every pre-existing slug is marked used (linked_at = created_at when no
  project was linked): each is a real public site, so it is never released
  or moved to another org.
- The title fallback only links repo-backed projects (frozen
  hasClonableSource), never a chat-only agent named like the site.
- down() refuses while tombstones exist instead of silently freeing them.
- A used slug whose project is gone (or that predates the link) is linked
  to another project only by a deployment admin
  (POST /api/admin/orgs/:orgId/sites/:slug/link).
- COLLECTION_CONNECTIONS_UPDATE pins a legacy title slug on rename;
  patchMetadata refuses siteSlug; experiments refuse tombstoned slugs;
  the admin project list prefers the linked project; project settings show
  a repo-backed legacy project's title slug read-only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
…backfill

Per the PO decisions on #7796:
- After a project is deleted, the same org links its used slug to another
  of its projects through the normal link path (linked_at keeps the first
  use). Another org, or a tombstone, is refused (not_owned / reserved);
  cross-org moves are done by operators in the DB.
- Remove the deployment-admin POST /api/_admin/orgs/:orgId/sites/:slug/link
  endpoint, the link adminOverride and the relink_requires_admin code.
- Migration 235 backfill links only an exact metadata.siteSlug match within
  the org; the title-based match is dropped, everything else stays unlinked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Adds a JSON encoder/decoder whose number formatting and string escaping match JSON.stringify exactly, so content the daemon writes is byte-identical to what deco serve writes. The tests are the spec.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Maps block keys to file names with encodeURIComponent parity and resolves the spellings a key may have on disk; adds the protocol's error codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
A content store over the sandbox working tree: contained to the tree, writes under a commit lock, durable writes and rollback on failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Refuses plaintext in v8 Secret fields and validates ciphertext shape; the v7 secret loader stays exempt. Its unit tests (secrets_test.go) land in the next PR, because they also cover servablePublicKey from methods.go.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
…, blocks.apply)

The content-protocol JSON-RPC handler: envelope and batch handling, the four methods, blocks.apply ifMatch and retry, and request body limits. Also adds secrets_test.go (the secret guard from the previous PR plus describe's servablePublicKey).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Asset uploads with name sanitizing, plus HTTP-level tests for the store, secrets, RPC and uploads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Mounts the content protocol and asset uploads on the daemon behind its auth, and adds worktree.Lock.AcquireWithin so a write never lands after its client gave up.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Runs the published @decocms/blocks content-protocol conformance suite
against the real Go daemon (its /_sandbox/rpc and /assets routes), and
adds @decocms/blocks as a sandbox devDependency for it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
… schema.get, blocks.list)

The content-protocol JSON-RPC handler skeleton: envelope and batch handling, request body limits, and the three read methods with their parameter validation. Also adds secrets_test.go (the secret guard from the previous PR plus describe's servablePublicKey). blocks.apply and the write paths follow in the next PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig

# Conflicts:
#	packages/sandbox/daemon-go/internal/content/handler.go
#	packages/sandbox/daemon-go/internal/content/methods.go
#	packages/sandbox/daemon-go/internal/content/params.go
#	packages/sandbox/daemon-go/internal/content/params_test.go
#	packages/sandbox/daemon-go/internal/content/rpc.go
Main took migration numbers 235 and 236, so the org-sites link
migration moves to 237 (file, test, index key, actor and log strings).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
@tlgimenes

Copy link
Copy Markdown
Contributor Author

CI dispatched on split/02-sandbox-daemon (ad134fa), since stacked PRs only get Gitleaks and cubic automatically:

  • Sandbox: 38251351755 passed. daemon-e2e (go vet, go test -race, 305 daemon e2e tests across 16 files, including the content-protocol conformance: 304 pass, 0 fail, 1 skipped) and docker-smoke both passed.
  • Checks & Unit Tests: 38251356693 passed (test, typecheck, lint, build, format, web-component-tests).
  • E2E Tests: 38251362384 passed (both shards).

Local checks: go vet ./... was clean, go test -race -p 1 ./... passed, bun install --frozen-lockfile passed, and the content-protocol e2e against a locally built daemon gave 79 pass, 0 fail.

@tlgimenes

Copy link
Copy Markdown
Contributor Author

@decocms/blocks 8.1.0 is released (npm latest). ddcd4e3 pins packages/sandbox to exactly 8.1.0 (was a 8.1.0-next.N prerelease); bun.lock changes only that spec line and the @decocms/blocks package record. Cascaded up the stack by normal merges; on the top (split/50 499b498): Sandbox (incl. docker-smoke), Checks & Unit Tests and E2E all green, apps/api and apps/web typecheck clean.

Project assets (hosted and sandbox) go to Studio's file storage, not the
working tree; uploading to the filesystem is only for `deco serve`. The
daemon now serves just the content protocol (`/_sandbox/rpc`), and
describe reports `assets: null`, so editors upload to Studio's storage.

Removes `PUT /_sandbox/assets/{name...}`, its handler, the FS store's
upload folder and the asset tests (unit, conformance endpoint, parity).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig
Base automatically changed from feat/org-sites-project-link to main October 13, 2026 15:40
@tlgimenes

Copy link
Copy Markdown
Contributor Author

Superseded: the content protocol runs in Studio's backend over the daemon's existing fs routes (apps/api/src/sandbox/content-storage.ts, in #7878 @ 3a3b453). packages/sandbox is now identical to main; the conformance + parity suites run against the Studio route over the real main daemon in the Sandbox workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WNwbSEePYNcY5YCgqZURig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude PR authored by a coding agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant