Skip to content

feat(primitives): stage browser uploads for tier 2 actions - #448

Merged
allisonmahmood merged 3 commits into
mainfrom
tier2/409-staged-uploads
Oct 1, 2026
Merged

allisonmahmood merged 3 commits into
mainfrom
tier2/409-staged-uploads

Conversation

@allisonmahmood

Copy link
Copy Markdown
Owner

Tier 2 pages need to upload files without sending their bytes through the isolate.

Add patchy.files.stage and discard, plus single-use adoption through action arguments declared with t.upload(). Adoption uses stored metadata and writes a file pointer without copying bytes. Quotas, expiry, and sweep locking cover abandoned uploads and concurrent adoption or discard. Hosted and local dev paths share the implementation; docs and SDK skills describe partial outcomes when a later mutation fails.

Stacked on tier2/408-file-handles. File handles and the download UI come from that base branch; this PR adds staged uploads for #409.

Closes #409.

Verification

  • pnpm build, pnpm typecheck, and pnpm lint passed.
  • pnpm test: 134 files, 1,525 tests passed.
  • Chromium tests and manual browser smoke covered staging, authoritative metadata, oversize refusal, single-use adoption, discard, and file retention after a failed follow-up mutation.
  • Spec and standards reviews completed with no blocking findings.

Implemented with OpenAI GPT-6 Astra using the omp harness.

@github-actions github-actions Bot added area:server Touches the server area area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:docs Touches the docs area area:primitives Patch tables and files size:xl Over 1,000 changed lines; review in multiple passes labels Sep 30, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Astra review

Reviewed git diff origin/tier2/408-file-handles...HEAD, base cb2283e to PR head ebf6bcb, against #409 and the applicable requirements in #384. One pass, no subagents; merge blockers only.

Standards

No merge-blocking method or YAGNI findings.

Spec

No merge-blocking intent findings. The reviewed paths cover viewer/patch/version binding, authoritative upload metadata, single-use adoption, discard races, expiry and sweep protection, quotas, and the documented partial outcome when a later mutation fails.

Verification

  • 85 tests passed across Files.test.ts, OrphanSweep.test.ts, RuntimeApi.test.ts, CallbackGateway.test.ts, CallbackGatewayApi.test.ts, and devExecution.test.ts.
  • The Chromium staged-upload scenario passed with real server artifacts and workerd. It exercises another viewer's refusal, forged metadata, adoption reuse, discard, a failed follow-up mutation, and downloading the adopted bytes.

Standards: 0 blocking findings. Spec: 0 blocking findings. No merge blockers found in this pass.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Opus review

Reviewed origin/tier2/408-file-handles...HEAD against #409, spec #384 §10/§16, ADR-0009 and the Effect service spec. Staging, adoption, discard, the binding checks, metadata resolution, bounds, the key layout, the idempotent upgrade test and the listed surfaces all hold up. Three findings, most severe first.

1. Correctness/method: the sweep deletes blobs while holding the company-wide object lock and a company lease

packages/company-database/src/OrphanSweep.ts:186-207 (reclaimUpload) calls store.delete(...) inside withFileObjectsLock, and the call runs inside scan = withCompany(...) (:266). reclaimFile (:228-240) now also holds the same lock across store.delete(key), on top of the platform row and patch locks. After this PR, every files.put, files.stage, files.discard, adoption and inspectUpload in the company takes that lock (Files.ts:419, 474, 537, 552, 592, 615, 631, 659, 681). So one slow or retrying object-store DELETE stalls all file writes and stages for the company, and it also holds a connection from the company pool while it waits.

This breaks the company-resources rule in CLAUDE.md ("blob I/O outside leases"). It also contradicts the ADR-0009 text this PR adds (:74), which says the object-state transactions that serialize with reclamation are "short". The PR already has the right pattern in discard (Files.ts:620-644 → removeDiscardedObject): under the row lock, move the stage to discarded, commit, then delete the blob, then delete the row. reclaimUpload should do the same. Once a row is not staged, no adoption can win, so the blob delete does not need the lock. reclaimFile does not need the object lock at all. Only adoption attaches an existing object, adoption needs a live stage row, and nothing makes a row live for an existing object id.

2. YAGNI: ordinary byte puts now take reservations and leave permanent adopted rows

#409 changes staged uploads. It does not touch plain put(name, bytes). Even so, Files.ts:678-690 now sends every tier 1 browser put and every action byte put through reserveObject(..., false). That is two company-wide lock transactions per put where there used to be none, plus an expiry borrowed from files.stage.lifetime. The result also leaves a file_uploads row in state adopted for every stored file, copying size/content_type/sha256 from patchy.files. The sweep then needs an hourly anti-join delete for those rows (OrphanSweep.ts:272-274) and a branch that can never run (OrphanSweep.ts:199, re-marking an expired row adopted; plain puts and adoption both set adopted in the same transaction as the pointer insert).

Plain puts did not need any of this. They always write a fresh object id, and the listing sweep's one-day grace already protected an in-flight object, as the base ADR-0009 text said. This change is the reason finding 1 reaches tier 1 file writes. A smaller model:

  • plain put stays as it was on the base branch;
  • adoption deletes the stage row (or marks it consumed) in the pointer-write transaction, under the stage row lock;
  • the company lock covers only the stage quota reservation.

Single-use still holds (a missing row is not_found), and "no store references it and no live stage holds it" still decides the sweep. ADR-0009 :74 and its new staged-upload paragraph would need to follow.

3. Standards: Files.make gets a dependency as a constructor argument

Files.ts:215 makeWithCompanyQuota(companyQuota) receives OperatingLimits.get as a callback that returns an Effect. make (:952) and makeLocal (:962) then pick the implementation. .agents/skills/effect-service-conventions/SKILL.md §36 says service dependencies come from the environment and says to flag a "plain object whose methods return Effect" passed as an implementation dependency. make should yield OperatingLimits.OperatingLimits, and local dev should supply a registry-defaults OperatingLimits layer. That removes makeLocal and the second constructor.

@allisonmahmood

allisonmahmood commented Sep 30, 2026 •

Copy link
Copy Markdown
Owner Author

Addressed the review feedback in 0352028 and e528ba8 (current head).

Fixed

  • Opus 1 — sweep holds locks across blob deletion: confirmed. Expired-upload and old-object cleanup now claim reclamation in short transactions, release database leases and locks, then delete the blob. Failed staged-upload deletion keeps a discarded row for retry. Cleanup compares the claimed row version so an older DELETE acknowledgement cannot erase a late writer's newer retry tombstone.
  • Opus 2 — ordinary puts and permanent adopted rows: confirmed. Ordinary byte puts again write a fresh immutable object and update the pointer under the per-name lock, without upload reservations or the company object lock. The public mutation deadline is shorter than the one-day orphan grace. Adoption atomically deletes the consumed upload row; the adopted-row anti-join cleanup is gone.
  • Retained inventories: focused re-review caught a remaining upgrade gap. Initialization now removes legacy adopted metadata and replaces the old state constraint and expiry index transactionally. Live stages, file pointers and blob bytes remain intact; repeated initialization is safe.

Kept, with reasons

  • Opus 3 — private quota callback strategy: retained. The Effect service spec explicitly permits deliberate callback strategies. Production Files.make acquires OperatingLimits from the Effect environment; the private helper selects live effective quota versus the local registry default. This is not an injected service instance. Adding a local OperatingLimits service would introduce unrelated override-management operations without a platform database.
  • The short object-state lock still serializes reclamation claims against adoption. No blob I/O runs under it, and ordinary puts do not use it.

Astra reported no blockers. Conversation comments, Reviews-tab bodies and inline comments were rechecked after the final push and CI completion; no new feedback or Grok review was present. Both focused Standards and Spec re-reviews finished without remaining findings.

Verification

  • Final-head pnpm test: 134 files / 1,526 tests passed.
  • pnpm build, pnpm typecheck, pnpm lint: passed.
  • Regressions cover slow DELETE without blocking unrelated file operations, late PUT/tombstone races, and retained-schema upgrades on PostgreSQL and PGlite. The slow-delete and retained-upgrade cases failed before their fixes.
  • Disposable real-server smoke verified stage/adopt, consumed-row removal, expiry cleanup, readable adopted bytes after sweep, and retained-schema upgrade across two restarts while preserving live stages and file bytes.
  • All nine reported GitHub checks pass on e528ba8a1a63ca463ded44a482f0584a4d4e8e91: Node 22/24 tests, lint/typecheck/format, Clerk live, CLI smoke, Postgres concurrency, dependency review, server image and labels. CI run. GitHub reports no separately configured required-check set; the PR is CLEAN and MERGEABLE.
  • Node 24 initially hit the same 30-second Fleet.test.ts isolation timeout seen on the original PR head. Its dependencies do not exercise the changed file-storage code. Rerunning only the failed job passed on the unchanged commit; no test or timeout was weakened.

Company-resource semantics, retained inventory initialization, development verification docs, product docs and ADR-0009 were updated. Wire/CLI contracts, numbered platform migrations, served pages, reverse-state APIs and package layout are unchanged.

No unresolved review blockers. PR remains open and unmerged.

@allisonmahmood
allisonmahmood force-pushed the tier2/408-file-handles branch from cb2283e to 4c3a86f Compare October 1, 2026 05:32
Base automatically changed from tier2/408-file-handles to main October 1, 2026 05:45
@allisonmahmood
allisonmahmood force-pushed the tier2/409-staged-uploads branch from e528ba8 to fc5197d Compare October 1, 2026 05:46
@allisonmahmood
allisonmahmood merged commit 9f03f7b into main Oct 1, 2026
15 of 16 checks passed
@allisonmahmood
allisonmahmood deleted the tier2/409-staged-uploads branch October 1, 2026 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:docs Touches the docs area area:primitives Patch tables and files area:server Touches the server area size:xl Over 1,000 changed lines; review in multiple passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(primitives): staged uploads, discard, adoption, the files/<patchId>/<objectId> key layout and its sweep

1 participant