Skip to content

Guard Git file updates with the reader-observed blob SHA - #1044

Merged
sidneyswift merged 4 commits into
mainfrom
codex/guarded-git-file-updates
Oct 19, 2026
Merged

sidneyswift merged 4 commits into
mainfrom
codex/guarded-git-file-updates

Conversation

@sidneyswift

@sidneyswift sidneyswift commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Stale Git-backed draft uploads can overwrite a newer edit because the existing helper replaces the reader's revision with a freshly fetched SHA. Add an opt-in condition that keeps the reader's SHA unchanged through GitHub's atomic commit boundary.

  • GET /api/sandboxes/file?include_revision=true returns content and blob sha from one response on main. It supports regular files with inline base64 content, preserves known binary encodings, and rejects invalid UTF-8 rather than returning corrupted editable text.
  • POST /api/sandboxes/files accepts files[].expected_sha. Guarded updates use that SHA on main, with no fresh-SHA lookup or automatic conflict retry. The commit helper independently fails closed if a guarded update has no valid condition.
  • Successful uploads return new SHAs. All-failed conflict-only batches return 409 with conflicts; all-failed mixed failures return 500 with conflicts preserved; partial successes retain 200 and their successful receipts/errors/conflicts. Existing temporary-blob cleanup remains unchanged.
  • Omitting the new fields retains legacy raw reads/default-branch create/update behavior. Both legacy and guarded paths now reject noncanonical relative paths (dot/traversal/backslash/control/empty segments and malformed Unicode) before provider access. Empty root upload directory remains allowed. Legacy raw paths encode literal percent/query/hash characters. These are intentional input-validation repairs; legacy Git error strings are also sanitized while retaining server-side diagnostics and existing response shapes.

Owning work: recoupable/app#2116 / recoupable/app#2140, tracked through recoupable/mono#361. Route JSDoc and lib/sandbox/guarded-git-files.md document the contract, migration requirements and remaining decisions.

Validation:

  • 177 affected tests across 27 suites pass locally. Actual route exports/validators/handlers/Git helpers are exercised against synthetic auth/snapshot/submodule/blob/GitHub boundaries: two readers, successful first update/new SHA, stale second conflict with preserved content, unchanged conditions, no update-time GET, invalid guard rejection, per-file forwarding, partial/mixed conflicts, unsafe names/paths/Unicode, binary handling, encoding and generic errors/server diagnostics.
  • Repository lint, formatting, scoped changed-code/test typecheck and diff checks pass. Full local TypeScript output is byte-identical to an untouched archive of main e4d9b69 under the same dependencies (226 existing diagnostics); no clean full-repository typecheck is claimed.
  • Records' existing private stale-sandbox fixture was executed unchanged against this branch: ordinary control passes, stale-overwrite acceptance still fails. Its temporary test copy was removed; the regression was not weakened or added as a failing product test.
  • At exact head b0e0fa9, all six checks pass. Full CI reports 1,213 suites / 6,581 tests passed; 11 suites / 13 tests skipped, including response-contract types. Incremental review reports zero new issues. Earlier findings are implemented and all review threads resolved; the final stale missing-test thread links the actual POST cases. This is not human merge approval.

Remaining boundaries and release prerequisites:

  • This is the Git-backed portion of checkpoint D, not full D closure. editFileTool → Sandbox.writeFile → SDK writeFiles remains unconditional. The guide names the required atomic comparison/replacement, writer participation, crash/path/creation and adoption decisions. A second read, rename alone or per-process mutex is not presented as a fix.
  • No client is silently migrated: guarded clients must always carry their observed condition. Git commits do not synchronize already running sandboxes or accept Context facts. Draft-origin path/version linkage, rights and financial authority remain separate.
  • Existing repository/submodule authorization policy remains an independent boundary. Before public release, canonical Marketing OpenAPI and its pinned Docs copy must adopt this approved contract. Hosted acceptance requires an explicitly authorized isolated repository/account.
  • No customer/runtime file writes, provider actions, imports, new storage, migrations, permission/credential changes, merge or manual deployment were performed. Normal source push/automatic PR preview is distinct from hosted business-flow acceptance.

@vercel

vercel Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated
api Ready Ready Preview Oct 19, 2026 5:56pm UTC

Request Review

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 12 files

Architecture diagram
sequenceDiagram
    participant Client as API Client
    participant Route as Sandbox Routes
    participant Auth as Auth Validator
    participant Resolver as Submodule Resolver
    participant GitHub as GitHub API
    participant Blob as Blob Storage

    Note over Client,Blob: Guarded Git File Flow

    Client->>Route: GET /api/sandboxes/file?path=draft.txt&include_revision=true
    Route->>Auth: validateAuthContext()
    Auth-->>Route: accountId
    
    alt Include revision requested
        Route->>Resolver: resolveSubmodulePath()
        Resolver-->>Route: githubRepo + path
        Route->>GitHub: GET contents/{path}?ref=main
        GitHub-->>Route: content + sha (same response)
        Route-->>Client: 200 { content, sha }
    else Raw read (no include_revision)
        Route->>GitHub: GET raw file
        GitHub-->>Route: raw content
        Route-->>Client: 200 { content }
    end

    Note over Client,Blob: Guarded Write Flow

    Client->>Route: POST /api/sandboxes/files
    Route->>Auth: validateAuthContext()
    Auth-->>Route: accountId
    
    Route->>Blob: Fetch uploaded blob
    Blob-->>Route: file content
    
    alt expected_sha provided (guarded)
        Route->>GitHub: PUT contents/{path} (sha + branch: main)
        GitHub-->>Route: New blob sha
        Route-->>Client: 200 { uploaded: [{sha}] }
        
        alt Conflict (stale sha)
            Route->>GitHub: PUT contents/{path} (stale sha)
            GitHub-->>Route: 409 Conflict
            Route-->>Client: 409 { conflicts: [file] }
        end
        
        alt Partial success
            Route->>GitHub: PUT file A (valid sha)
            GitHub-->>Route: sha A
            Route->>GitHub: PUT file B (stale sha)
            GitHub-->>Route: 409 Conflict
            Route-->>Client: 200 { uploaded: [A], conflicts: [B] }
        end
    else No expected_sha (legacy)
        Route->>GitHub: GET contents/{path} (fetch latest sha)
        GitHub-->>Route: current sha
        Route->>GitHub: PUT contents/{path} (sha, default branch)
        GitHub-->>Route: New blob sha
        Route-->>Client: 200 { uploaded: [{sha}] }
    end

    Note over Route,Blob: Temporary blob deletion occurs after every write attempt

    Note over Client,GitHub: Authorization: x-api-key or Bearer token required
    Note over Client,GitHub: Access: Account snapshots + submodule resolution before GitHub calls
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread lib/sandbox/getSandboxesFileHandler.ts Resolved
Comment thread lib/github/createOrUpdateFileContent.ts Resolved
Comment thread lib/github/getVersionedFileContent.ts Resolved
Comment thread lib/github/createOrUpdateFileContent.ts Resolved
Comment thread lib/sandbox/guarded-git-files.md Resolved
Comment thread app/api/sandboxes/file/route.ts Resolved
@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: Repository: recoupable/api/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9266b491-4670-421a-bcc1-47f8f829f1eb
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 17 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread lib/github/commitFileContent.ts Resolved
Comment thread lib/github/isRelativeRepoFilePath.ts Resolved

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 9 files (changes from recent commits).

Confidence score: 5/5

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

Requires human review: Auto-approval blocked by 1 unresolved issue from previous reviews.

View guided diff | Turn on auto-fix | Re-trigger cubic

@sidneyswift
sidneyswift merged commit 1564985 into main Oct 19, 2026
7 checks passed

This branch was successfully deployed

1 active deployment
Preview — b0e0fa93 Deployed Oct 19, 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.

1 participant