Skip to content

feat: establish shared file ownership foundation - #8609

Draft
mzxchandra wants to merge 183 commits into
stagingfrom
codex/file-ownership-foundation
Draft

mzxchandra wants to merge 183 commits into
stagingfrom
codex/file-ownership-foundation

Conversation

@mzxchandra

@mzxchandra mzxchandra commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Prepare shared file infrastructure for Project files while preserving workspace behavior. Files retain native workspace, Project or organization ownership with foreign keys and exclusive-owner constraints; non-workspace personal attachments retain their existing policy.
  • Apply canonical ownership across folders, retained versions, billing, search, public sharing, realtime admission and cleanup. Preserve creator succession and the distinction between the acting principal and billing identity.
  • Preserve bytes after uncertain commits, clean staged content after proven rollback, and retry transient public-share backfill contention without losing committed progress.
  • Add six SQL migrations, 0422–0427, plus script migrations 0033–0035 for ownership validation, resumable public-share backfill and workspace-binding validation. Creator-lifetime enforcement is included in the folder/version migration.

This PR targets staging and includes the landed #8590 and #8762 prerequisites. Project backend/API/CLI work follows in #8610; browser and Mothership integration follows in #8781. Those dependent changes are outside this PR.

Rollout and rollback

  • Deploy the foundation with Project file admission disabled. SQL and registered script migrations run before image promotion, while the preceding app still serves traffic; verify that serving release is compatible. Repair missing ownership from authoritative records before validation. Unresolved public-share targets require explicit review; never infer workspace ownership from the uploader.
  • Before enabling dependent Project-file operations, verify the compatible app, realtime and background consumers are deployed and relevant incompatible work has drained. Verify staging and production independently using the established deployment process; this PR adds no custom image-digest readiness gate or readiness variables.
  • App traffic switches all at once. Retired app servers receive no fresh normal requests, but in-flight file registration/finalization, content/version writes, copies, deletion and creator handoff may continue. Check affected realtime sockets/persist work and independently scheduled search, cleanup and outbox workers, including retries/resumptions. Unrelated background work need not stop; elapsed time alone is not drain evidence.
  • Existing upload sessions/provider transfers may outlive requests. Preserve compatible finalization/recovery and durable cleanup. After Project-owned rows exist, this foundation is the minimum file-aware rollback target: disable admission, retain the expanded schema and compatible realtime/background consumers, and retain backend recovery where pending Project uploads require it. Flag-off does not prove previously admitted work has stopped.

Type of Change

  • Other: shared file ownership and rollout compatibility foundation

Testing

  • Staging integration: 270 focused local checks passed (70 PostgreSQL database, 148 PostgreSQL application, 52 unit); one existing opt-in search benchmark skipped. Application/database type checks, fresh migrations, migration safety and schema-generation drift checks passed.
  • Migration consolidation: fresh migration and no-op replay passed; 70 PostgreSQL ownership/backfill cases and 91 PostgreSQL search cases passed, with 1 existing opt-in benchmark skipped. Migration safety and Drizzle no-drift checks passed.
  • Full repository validation runs in hosted CI. Current-head CI and fresh reviews are pending; earlier results are historical evidence, not current-head approval.
  • Operational fixture kit and environment evidence remain outside the repository. Deployment provenance and post-deploy verification are separate from these local checks.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Relevant tests updated and passing
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…ty-enforcement

# Conflicts:
#	apps/sim/lib/projects/__integration__/foundation.integration.ts
@vercel

vercel Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Oct 14, 2026 3:18am UTC

Request Review

@mzxchandra

Copy link
Copy Markdown
Contributor Author

@greptileai review — d43cdd4 corrects the branch boundary: foundation owned-file creation uses its existing billing API; the newer accounting helper remains in #8610.

@mzxchandra

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review — d43cdd4 corrects the branch boundary: foundation owned-file creation uses its existing billing API; the newer accounting helper remains in #8610.

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review — d43cdd4 corrects the branch boundary: foundation owned-file creation uses its existing billing API; the newer accounting helper remains in #8610.

@mzxchandra I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 220 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

Note: This PR contains a large number of files. cubic selects up to 200 of the highest-priority eligible files for this review, so some files may not have been reviewed.
You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Turn on auto-fix | Re-trigger cubic

Base automatically changed from codex/project-entity-enforcement to staging October 13, 2026 01:42
Preserve the upstream storage lock order and migration 0413; renumber pending file migrations to 0414-0420 without changing SQL. Correct aggregate file-delivery types and stale test fixtures, remove tests for the retired rollout gate, and retain the landed lifecycle requirement that ownership successors already have access.
@mzxchandra

Copy link
Copy Markdown
Contributor Author

@greptile

@mzxchandra

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@mzxchandra I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 222 files

Confidence score: 3/5

  • In resource-handoff.ts, Project creator references can block membership removal or account deletion in an ownerless organization, even when the Project has a valid successor. Adjust the unions so those handoffs remain possible.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/sim/lib/billing/organizations/resource-handoff.ts">

<violation number="1" location="apps/sim/lib/billing/organizations/resource-handoff.ts:22">
P2: These unions make Project creator references require an organization owner, so membership removal or account deletion is blocked in an ownerless organization even when the Project has a valid successor. Keep these references on the Project-scoped succession path instead of applying the organization-owner gate.</violation>
</file>

Note: This PR contains a large number of files. cubic selects up to 200 of the highest-priority eligible files for this review, so some files may not have been reviewed.
You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/core/config/env.ts
Comment thread apps/sim/lib/billing/organizations/resource-handoff.ts Outdated
Comment thread apps/sim/lib/workspace-files/search/source.ts Outdated
Comment thread apps/sim/lib/billing/cleanup-dispatcher.ts Outdated
Comment thread apps/sim/lib/workspace-files/search/index-state.ts
@mzxchandra

Copy link
Copy Markdown
Contributor Author

@greptile

@mzxchandra

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@mzxchandra I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 233 files

Confidence score: 3/5

  • In fetch-servable-workspace-file-buffer.ts, a generated document can render successfully but then fail to read or download when its owned-file contributor has a non-workspace context. Ensure this check handles that contributor context.
  • In compiled-store.ts, rollback cleanup can delete a content-addressed cache object used by another successful render. Make sure rollback does not remove a shared object.
  • In search-workspace-file-content.ts, ordinary UI and API-key searches can return a conflict if a file changes or is deleted between the index read and snapshot validation. Restrict this validation so routine searches can return their point-in-time results.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/sim/lib/workspace-files/application/search-workspace-file-content.ts">

<violation number="1" location="apps/sim/lib/workspace-files/application/search-workspace-file-content.ts:89">
P2: Ordinary UI and API-key searches now run this snapshot-validation transaction too, so an edit or deletion between the index read and this check turns a valid point-in-time result into a conflict. Restrict snapshot revalidation to classified Chat searches while retaining any owned-input access checks required for other callers.</violation>
</file>

<file name="apps/sim/lib/uploads/documents/compiled-store.ts">

<violation number="1" location="apps/sim/lib/uploads/documents/compiled-store.ts:178">
P2: Rollback cleanup can delete another successful render’s shared cache object: this callback records the content-addressed key before upload, and the Project caller later deletes recorded keys after a failed commit. Make cleanup conditional on a write-specific ownership/version, or track only keys proven newly created.</violation>
</file>

<file name="apps/sim/lib/workspace-files/application/fetch-servable-workspace-file-buffer.ts">

<violation number="1" location="apps/sim/lib/workspace-files/application/fetch-servable-workspace-file-buffer.ts:52">
P2: A generated document can successfully render with an owned-file contributor, but this check throws because its context is not `workspace`, failing the read or download after rendering succeeds. Skip the search-artifact receipt for unsupported dependency contexts instead of failing the render.</violation>
</file>

Note: This PR contains a large number of files. cubic selects up to 200 of the highest-priority eligible files for this review, so some files may not have been reviewed.
You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Turn on auto-fix | Re-trigger cubic

context.workspaceId,
found.sources.map((source) => source.identity),
'excerpt'
const provenance = await db.transaction(async (tx) => {

@cubic-dev-ai cubic-dev-ai Bot Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Ordinary UI and API-key searches now run this snapshot-validation transaction too, so an edit or deletion between the index read and this check turns a valid point-in-time result into a conflict. Restrict snapshot revalidation to classified Chat searches while retaining any owned-input access checks required for other callers.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At apps/sim/lib/workspace-files/application/search-workspace-file-content.ts, line 89:

<comment>Ordinary UI and API-key searches now run this snapshot-validation transaction too, so an edit or deletion between the index read and this check turns a valid point-in-time result into a conflict. Restrict snapshot revalidation to classified Chat searches while retaining any owned-input access checks required for other callers.</comment>

<file context>
@@ -63,34 +77,68 @@ export const searchWorkspaceFileContent = defineAuthorizedWorkspaceFileUseCase({
-        context.workspaceId,
-        found.sources.map((source) => source.identity),
-        'excerpt'
+      const provenance = await db.transaction(async (tx) => {
+        await lockWorkspaceProject(tx, context.workspaceId)
+        const [current] = await tx
</file context>
Fix with cubic

if (existing?.referencedInputIdentity === referencedInputIdentity) return
const pointer: PublishedArtifactPointer = { version: 1, referencedInputIdentity }
try {
onArtifactWrite?.(key)

@cubic-dev-ai cubic-dev-ai Bot Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Rollback cleanup can delete another successful render’s shared cache object: this callback records the content-addressed key before upload, and the Project caller later deletes recorded keys after a failed commit. Make cleanup conditional on a write-specific ownership/version, or track only keys proven newly created.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At apps/sim/lib/uploads/documents/compiled-store.ts, line 178:

<comment>Rollback cleanup can delete another successful render’s shared cache object: this callback records the content-addressed key before upload, and the Project caller later deletes recorded keys after a failed commit. Make cleanup conditional on a write-specific ownership/version, or track only keys proven newly created.</comment>

<file context>
@@ -152,24 +161,26 @@ export async function loadCompiledDoc(
   if (existing?.referencedInputIdentity === referencedInputIdentity) return
   const pointer: PublishedArtifactPointer = { version: 1, referencedInputIdentity }
   try {
+    onArtifactWrite?.(key)
     await uploadFile({
       file: Buffer.from(JSON.stringify(pointer), 'utf-8'),
</file context>
Fix with cubic

contentUpdatedAt: fileRecord.contentUpdatedAt ?? fileRecord.updatedAt,
},
dependencies: (result.contributingFiles ?? []).map((file) => {
if (!file.contentUpdatedAt || file.context !== 'workspace')

@cubic-dev-ai cubic-dev-ai Bot Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A generated document can successfully render with an owned-file contributor, but this check throws because its context is not workspace, failing the read or download after rendering succeeds. Skip the search-artifact receipt for unsupported dependency contexts instead of failing the render.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At apps/sim/lib/workspace-files/application/fetch-servable-workspace-file-buffer.ts, line 52:

<comment>A generated document can successfully render with an owned-file contributor, but this check throws because its context is not `workspace`, failing the read or download after rendering succeeds. Skip the search-artifact receipt for unsupported dependency contexts instead of failing the render.</comment>

<file context>
@@ -36,4 +38,28 @@ export async function fetchAuthorizedServableWorkspaceFileBuffer(
+          contentUpdatedAt: fileRecord.contentUpdatedAt ?? fileRecord.updatedAt,
+        },
+        dependencies: (result.contributingFiles ?? []).map((file) => {
+          if (!file.contentUpdatedAt || file.context !== 'workspace')
+            throw new Error('Artifact input has no canonical workspace revision')
+          return {
</file context>
Fix with cubic

This branch was successfully deployed

1 active deployment
Preview — 6b4a58f2 Deployed Oct 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants