Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(gfs): preserve idempotent replay after policy lowering

Josué65997d8e2622
diff --git a/gfs-controller/src/upload/uploadSession.test.ts b/gfs-controller/src/upload/uploadSession.test.ts
index d983c8dda..331079950 100644
--- a/gfs-controller/src/upload/uploadSession.test.ts
+++ b/gfs-controller/src/upload/uploadSession.test.ts
@@ -23,6 +23,7 @@ type Row = Record<string, unknown>
class MemoryDb {
sessions: Row[] = []
parts: Row[] = []
+ idempotencyLookupCount = 0
failReconciliationReads = false
failSessionReconciliationReads = false
@@ -31,6 +32,7 @@ class MemoryDb {
if (sql === 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ') return { rows: [] }
if (sql.startsWith('SELECT pg_advisory_xact_lock')) return { rows: [] }
if (sql.startsWith('SELECT * FROM gfs_upload_sessions WHERE owner_subject')) {
+ this.idempotencyLookupCount += 1
const [owner, drive, key] = values.map(String)
return {
rows: this.sessions.filter(
@@ -465,7 +467,7 @@ afterEach(async () => {
function service(
db: MemoryDb,
config: GfsUploadConfig = TEST_CONFIG,
- overrides: Partial<Pick<UploadSessionServiceDeps, 'now' | 'finalize' | 'tx'>> = {}
+ overrides: Partial<Pick<UploadSessionServiceDeps, 'now' | 'finalize' | 'tx' | 'authorize'>> = {}
): GfsUploadSessionService {
return new GfsUploadSessionService({
db,
@@ -721,6 +723,69 @@ describe('GfsUploadSessionService', () => {
}
)
+ it('replays an admitted create after the runtime product policy is lowered', async () => {
+ tempRoot = await mkdtemp(join(tmpdir(), 'gfsc-upload-policy-replay-'))
+ const db = new MemoryDb()
+ const request = {
+ ...principal,
+ operation: 'create' as const,
+ parentRid: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
+ name: 'admitted-before-policy-lowering.bin',
+ sizeBytes: 250 * 1024 * 1024,
+ idempotencyKey: '31111111-1111-4111-8111-111111111111',
+ }
+ const admitted = await service(db, {
+ ...CONFIG,
+ productMaxFileBytes: 300 * 1024 * 1024,
+ }).create(request)
+ expect(admitted.created).toBe(true)
+ expect(db.sessions).toHaveLength(1)
+
+ const lowered = service(db, { ...CONFIG, productMaxFileBytes: 100 * 1024 * 1024 })
+ const replay = await lowered.create(request)
+ expect(replay).toMatchObject({
+ created: false,
+ session: { uploadId: admitted.session.uploadId, expectedBytes: request.sizeBytes },
+ })
+ expect(db.sessions).toHaveLength(1)
+
+ await expect(
+ lowered.create({
+ ...request,
+ idempotencyKey: '31111111-1111-4111-8111-111111111112',
+ })
+ ).rejects.toMatchObject({ code: 'payload_too_large' })
+ expect(db.sessions).toHaveLength(1)
+
+ await expect(
+ lowered.create({ ...request, name: 'conflicting-name.bin' })
+ ).rejects.toMatchObject({
+ code: 'idempotency_conflict',
+ })
+ expect(db.sessions).toHaveLength(1)
+
+ const lookupsBeforeUnauthorizedAttempt = db.idempotencyLookupCount
+ const guarded = service(
+ db,
+ { ...CONFIG, productMaxFileBytes: 100 * 1024 * 1024 },
+ {
+ authorize: async input => {
+ if (input.ownerSubject !== principal.ownerSubject) throw new Error('not authorized')
+ },
+ }
+ )
+ await expect(
+ guarded.create({
+ ...request,
+ ownerSubject: 'user-2',
+ primarySubject: 'user-2',
+ sizeBytes: 1,
+ })
+ ).rejects.toThrow('not authorized')
+ expect(db.idempotencyLookupCount).toBe(lookupsBeforeUnauthorizedAttempt)
+ expect(db.sessions).toHaveLength(1)
+ })
+
it('does not double-decrement active parts after an unknown commit and failed reconciliation', async () => {
tempRoot = await mkdtemp(join(tmpdir(), 'gfsc-upload-'))
const db = new MemoryDb()
diff --git a/gfs-controller/src/upload/uploadSession.ts b/gfs-controller/src/upload/uploadSession.ts
index d051855a8..e12eb7818 100644
--- a/gfs-controller/src/upload/uploadSession.ts
+++ b/gfs-controller/src/upload/uploadSession.ts
@@ -15,6 +15,7 @@ import type { GfsBrokeredAuthority } from '../auth/verify'
import {
GFS_UPLOAD_V2_COMPLETE_BODY_MAX_BYTES,
GFS_UPLOAD_V2_METADATA_BODY_MAX_BYTES,
+ GFS_UPLOAD_V2_PROTOCOL_MAX_BYTES,
GfsUploadGeometryError,
type GfsUploadPartGeometry,
disabledGfsUploadV2Capability,
@@ -539,11 +540,11 @@ export class GfsUploadSessionService {
if (
!Number.isSafeInteger(input.sizeBytes) ||
input.sizeBytes < 0 ||
- input.sizeBytes > this.deps.config.productMaxFileBytes
+ input.sizeBytes > GFS_UPLOAD_V2_PROTOCOL_MAX_BYTES
) {
throw new GfsError(
'payload_too_large',
- `sizeBytes must be between 0 and ${this.deps.config.productMaxFileBytes}`
+ `sizeBytes must be between 0 and ${GFS_UPLOAD_V2_PROTOCOL_MAX_BYTES}`
)
}
const name = normalizedName(input.name)
@@ -564,8 +565,6 @@ export class GfsUploadSessionService {
)
const partBytes = this.deps.config.preferredPartBytes
const partCount = partCountFor({ expectedBytes: input.sizeBytes, partBytes })
- if (partCount > this.deps.config.maxPartCount)
- throw new GfsError('payload_too_large', 'upload part count exceeds the configured maximum')
const canonicalInput = { ...input, name, parentRid, resourceRid, wholeSha256 }
const requestFingerprint = canonicalFingerprint(canonicalInput, partBytes, partCount)
const existing = await this.deps.db.query(
@@ -581,6 +580,14 @@ export class GfsUploadSessionService {
)
return { created: false, session: toReceipt(row) }
}
+ if (input.sizeBytes > this.deps.config.productMaxFileBytes) {
+ throw new GfsError(
+ 'payload_too_large',
+ `sizeBytes must be between 0 and ${this.deps.config.productMaxFileBytes}`
+ )
+ }
+ if (partCount > this.deps.config.maxPartCount)
+ throw new GfsError('payload_too_large', 'upload part count exceeds the configured maximum')
const uploadId = requireUuid(input.uploadId ?? randomUUID(), 'uploadId')
const expiresAt = new Date(this.now() + this.deps.config.sessionTtlSeconds * 1000)
const available = this.deps.blobs.availableBytes ? await this.deps.blobs.availableBytes() : null