288 lines
30 KiB
TypeScript
288 lines
30 KiB
TypeScript
/** Opt-in v2 storage boundary. No imports initialize a database or scheduler.
|
|
* Admission is committed BEFORE an anchor claim is attempted. Its permanent
|
|
* unique identity survives payload retention. Metadata expiry GC requires
|
|
* trusted original-expiry entry gates and the guarded v2 schema proposal. */
|
|
import { randomUUID } from "node:crypto"
|
|
import { probeMetadataGcEligible } from "./server-probe-journal-maintenance-model.js"
|
|
import { admitPartitionWriter } from "./partition-write-transaction.js"
|
|
|
|
export interface ProbeStorageSql { query(text:string,values?:unknown[],options?:{signal?:AbortSignal;timeoutMs:number}):Promise<{rows:Record<string,unknown>[]}> }
|
|
export interface ProbeStorageConnection extends ProbeStorageSql {
|
|
/** Revocation only: ends when this physical borrow closes, never authority. */
|
|
readonly lifetimeSignal?:AbortSignal
|
|
release(unsafe?:boolean):void
|
|
}
|
|
export interface ProbeStorageScope {
|
|
ownerId:string; receiptId:string; identityKind:"rest"|"mcp"; executionId:string; bindingProof:string
|
|
authorityExpiresAt:string
|
|
}
|
|
export interface ProbeStoragePolicy {
|
|
retentionDays:number; globalBytes:number; ownerBytes:number; globalFiles:number; ownerFiles:number
|
|
globalAdmissions:number; ownerAdmissions:number
|
|
}
|
|
export interface ProbeStorageOptions { signal?:AbortSignal; deadlineAt:number }
|
|
export interface ProbeStorageDependencies {
|
|
connect(options:ProbeStorageOptions):Promise<ProbeStorageConnection>
|
|
/** Fresh real owner/policy/source/protection checks BEFORE/AFTER each SQL.
|
|
* Purge requires global storage maintenance plus this exact owner scope.
|
|
* Return a private authority/epoch proof stable for the whole operation. */
|
|
authorize(ownerId:string,operation:"claim"|"publish"|"read"|"purge"|"cancel",scope:Readonly<ProbeStorageScope>|undefined,options:ProbeStorageOptions):Promise<string>
|
|
/** Explicit root mapping, e.g. requestsRetentionDays. No new UI/settings.
|
|
* Include this policy revision in authorize's proof when mutable. */
|
|
policy:ProbeStoragePolicy
|
|
safetyWindowMs:number
|
|
now?:()=>number
|
|
}
|
|
export class ProbeStorageRefused extends Error { readonly automaticReplay=false;constructor(){super("Probe storage refused: scope, budget, authority or permanent admission mismatch; no replay")} }
|
|
export class ProbeStorageUnknown extends Error { readonly automaticReplay=false;constructor(){super("Probe storage outcome unknown; no retry, claim or payload publication permitted")} }
|
|
export const PROBE_PAYLOAD_MAX_BYTES=262144
|
|
/** Conservative logical metadata allowance per attempt (anchor, admission,
|
|
* <=11 stages, <=1 quarantine and <=1 cancellation). PostgreSQL physical/WAL/index
|
|
* bloat requires independent disk monitoring/vacuum, not this logical quota. */
|
|
export const PROBE_PERMANENT_RESERVATION_BYTES=32768
|
|
const reservation=PROBE_PAYLOAD_MAX_BYTES
|
|
const hash=/^[a-f0-9]{64}$/,uuid=/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i
|
|
function must(value:unknown):asserts value{if(!value)throw new ProbeStorageRefused()}
|
|
function owner(value:string){must(typeof value==="string"&&value.length>0&&value.length<=200&&value.trim()===value&&!/[\x00-\x1f\x7f]/.test(value))}
|
|
function scope(value:ProbeStorageScope):ProbeStorageScope{
|
|
owner(value.ownerId);must(uuid.test(value.receiptId)&&uuid.test(value.executionId)&&hash.test(value.bindingProof)&&["mcp","rest"].includes(value.identityKind)&&Number.isFinite(Date.parse(value.authorityExpiresAt)))
|
|
return {...value,authorityExpiresAt:new Date(value.authorityExpiresAt).toISOString()}
|
|
}
|
|
export function validateProbeStoragePolicy(raw:Record<string,unknown>):ProbeStoragePolicy{
|
|
const keys={retentionDays:"retention_days",globalBytes:"global_bytes",ownerBytes:"owner_bytes",globalFiles:"global_files",ownerFiles:"owner_files",globalAdmissions:"global_admissions",ownerAdmissions:"owner_admissions"} as const
|
|
const result={} as ProbeStoragePolicy
|
|
for(const [key,column]of Object.entries(keys)){const value=Number(raw[column]);must(Number.isSafeInteger(value)&&value>0);result[key as keyof ProbeStoragePolicy]=value}
|
|
must(result.retentionDays<=3650&&result.ownerBytes<=result.globalBytes&&result.ownerFiles<=result.globalFiles&&result.ownerAdmissions<=result.globalAdmissions)
|
|
return result
|
|
}
|
|
/** Pure overflow-safe admission check, used after locking both SQL counters.
|
|
* Retained admissions bound metadata. Payload purge never refunds metadata.
|
|
* Expired metadata GC refunds these retained caps, never a callable attempt. */
|
|
export function probeStorageFits(policy:ProbeStoragePolicy,global:Record<string,unknown>,perOwner:Record<string,unknown>):boolean{
|
|
const check=(row:Record<string,unknown>,bytes:number,files:number,admissions:number)=>{
|
|
const count=Number(row.admissions),reserved=Number(row.reserved_bytes),live=Number(row.reserved_files)
|
|
return [count,reserved,live].every(value=>Number.isSafeInteger(value)&&value>=0)&&count<admissions&&live<files&&reserved<=bytes-reservation-PROBE_PERMANENT_RESERVATION_BYTES
|
|
}
|
|
return check(global,policy.globalBytes,policy.globalFiles,policy.globalAdmissions)&&check(perOwner,policy.ownerBytes,policy.ownerFiles,policy.ownerAdmissions)
|
|
}
|
|
|
|
export function createProbeJournalStorage(deps:ProbeStorageDependencies){
|
|
must(typeof deps.connect==="function"&&typeof deps.authorize==="function");const now=deps.now??Date.now
|
|
const configured=deps.policy
|
|
must(configured)
|
|
must(Number.isSafeInteger(deps.safetyWindowMs)&&deps.safetyWindowMs>=60000&&deps.safetyWindowMs<=86400000)
|
|
const policy=validateProbeStoragePolicy({retention_days:configured.retentionDays,global_bytes:configured.globalBytes,owner_bytes:configured.ownerBytes,global_files:configured.globalFiles,owner_files:configured.ownerFiles,global_admissions:configured.globalAdmissions,owner_admissions:configured.ownerAdmissions})
|
|
const run=async<T>(ownerId:string,operation:"claim"|"publish"|"read"|"purge"|"cancel",bound:ProbeStorageScope|undefined,options:ProbeStorageOptions,work:(sql:ProbeStorageSql,policy:ProbeStoragePolicy,bind:(value:ProbeStorageScope)=>Promise<void>)=>Promise<T>,mode:"default"|"expiry-write"="default"):Promise<T>=>{
|
|
owner(ownerId);must(Number.isFinite(options.deadlineAt)&&options.deadlineAt>now())
|
|
let authority:string|undefined
|
|
const fence=async()=>{options.signal?.throwIfAborted();must(now()<options.deadlineAt);const proof=await deps.authorize(ownerId,operation,bound,options);must(hash.test(proof)&&(!authority||authority===proof));authority=proof;options.signal?.throwIfAborted();must(now()<options.deadlineAt)}
|
|
await fence();let connection:ProbeStorageConnection
|
|
try{connection=await deps.connect(options)}catch{throw new ProbeStorageUnknown()}
|
|
let commitSent=false,committed=false,began=false,destroy=false
|
|
const query:ProbeStorageSql["query"]=(text,values)=>connection.query(text,values,{signal:options.signal,timeoutMs:Math.max(1,Math.min(10000,options.deadlineAt-now()))})
|
|
const sql:ProbeStorageSql={query:async(text,values)=>{await fence();const result=await query(text,values);await fence();return result}}
|
|
try{
|
|
if(mode==="expiry-write"||operation==="claim"||operation==="publish"||operation==="cancel"){
|
|
// Acquire the common key before authority checks can retain a relation
|
|
// lock on this backend and before the secondary probe-storage lock.
|
|
await fence()
|
|
await query("BEGIN")
|
|
try{await admitPartitionWriter({query},{signal:options.signal,deadlineAt:Date.now()+Math.min(options.deadlineAt-now(),1000)})}
|
|
catch{began=true;throw new ProbeStorageRefused()}
|
|
if(mode!=="expiry-write"){
|
|
began=true
|
|
await query("SET LOCAL lock_timeout TO DEFAULT")
|
|
}
|
|
await fence()
|
|
}else await sql.query("BEGIN")
|
|
began=true
|
|
await sql.query("SET LOCAL TimeZone='UTC'")
|
|
await sql.query(`SET LOCAL statement_timeout='${Math.max(1,Math.min(10000,options.deadlineAt-now()))}ms'`)
|
|
// Shared ordering across EVERY replica, admission, finish and purge.
|
|
await sql.query("/* probe-storage:lock */ SELECT pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0))")
|
|
const result=await work(sql,policy,async value=>{must(value.ownerId===ownerId);bound=value;await fence()});await fence();commitSent=true;await sql.query("COMMIT");committed=true;return result
|
|
}catch(error){
|
|
if(commitSent&&!committed||!began){destroy=true;throw new ProbeStorageUnknown()}
|
|
try{await connection.query("ROLLBACK")}catch{destroy=true}
|
|
throw error instanceof ProbeStorageRefused?error:new ProbeStorageRefused()
|
|
}finally{connection.release(destroy)}
|
|
}
|
|
const admission=async(sql:ProbeStorageSql,bound:ProbeStorageScope)=>{
|
|
const rows=(await sql.query("/* probe-storage:admission */ SELECT * FROM server_probe_storage_admissions WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) FOR UPDATE",[bound.receiptId,bound.identityKind,bound.executionId])).rows
|
|
must(rows.length===1);const row=rows[0]
|
|
must(row.receipt_id===bound.receiptId&&row.owner_id===bound.ownerId&&row.identity_kind===bound.identityKind&&row.execution_id===bound.executionId&&row.binding_proof===bound.bindingProof&&new Date(row.authority_expires_at as string|Date).toISOString()===bound.authorityExpiresAt&&row.purged_at===null)
|
|
return row
|
|
}
|
|
const counters=async(sql:ProbeStorageSql,ownerId:string)=>{
|
|
await sql.query("/* probe-storage:owner-counter */ INSERT INTO server_probe_storage_usage(scope) VALUES($1) ON CONFLICT DO NOTHING",["owner:"+ownerId])
|
|
const rows=(await sql.query("/* probe-storage:counters */ SELECT * FROM server_probe_storage_usage WHERE scope=ANY($1::text[]) ORDER BY scope FOR UPDATE",[["global","owner:"+ownerId]])).rows
|
|
must(rows.length===2);const global=rows.find(row=>row.scope==="global"),local=rows.find(row=>row.scope==="owner:"+ownerId);must(global&&local);return {global,local}
|
|
}
|
|
return {
|
|
/** Resolve authenticated ORIGINAL authority under its operation lock only
|
|
* AFTER the shared storage lock. Both callbacks use this same transaction.
|
|
* null is a terminal-source no-op. Expired original authority may cancel.
|
|
* An exact existing identity spends no further quota, even after purge. */
|
|
async cancel<T>(ownerId:string,options:ProbeStorageOptions,resolveScope:(sql:ProbeStorageSql)=>Promise<ProbeStorageScope|null>,writeCancellation:(sql:ProbeStorageSql,bound:ProbeStorageScope)=>Promise<T>):Promise<T|undefined>{
|
|
return run(ownerId,"cancel",undefined,options,async(sql,policy,bind)=>{
|
|
const resolved=await resolveScope(sql);if(resolved===null)return undefined
|
|
const bound=scope(resolved);await bind(bound)
|
|
const existing=(await sql.query("/* probe-storage:admission */ SELECT * FROM server_probe_storage_admissions WHERE receipt_id=$1 OR (identity_kind=$2 AND execution_id=$3) FOR UPDATE",[bound.receiptId,bound.identityKind,bound.executionId])).rows
|
|
if(existing.length){
|
|
must(existing.length===1);const row=existing[0]
|
|
must(row.receipt_id===bound.receiptId&&row.owner_id===bound.ownerId&&row.identity_kind===bound.identityKind&&row.execution_id===bound.executionId&&row.binding_proof===bound.bindingProof&&new Date(row.authority_expires_at as string|Date).toISOString()===bound.authorityExpiresAt)
|
|
if(row.cancelled_at==null){
|
|
const marked=(await sql.query("/* probe-storage:cancel-existing */ UPDATE server_probe_storage_admissions SET cancelled_at=clock_timestamp(),claim_token=NULL,claim_attempted_at=COALESCE(claim_attempted_at,clock_timestamp()) WHERE receipt_id=$1 AND cancelled_at IS NULL RETURNING receipt_id",[bound.receiptId])).rows
|
|
must(marked.length===1&&marked[0].receipt_id===bound.receiptId)
|
|
}
|
|
}else{
|
|
const usage=await counters(sql,ownerId);must(probeStorageFits(policy,usage.global,usage.local))
|
|
const rows=(await sql.query("/* probe-storage:cancel-reserve */ INSERT INTO server_probe_storage_admissions(receipt_id,owner_id,identity_kind,execution_id,binding_proof,authority_expires_at,claim_token,claim_attempted_at,cancelled_at,retention_days,safety_ms,gc_not_before) VALUES($1,$2,$3,$4,$5,$6::timestamptz,NULL,clock_timestamp(),clock_timestamp(),$7::integer,$8::integer,GREATEST($6::timestamptz,clock_timestamp())+make_interval(days=>$7::integer)+$8::integer*interval '1 millisecond') ON CONFLICT DO NOTHING RETURNING receipt_id",[bound.receiptId,bound.ownerId,bound.identityKind,bound.executionId,bound.bindingProof,bound.authorityExpiresAt,policy.retentionDays,deps.safetyWindowMs])).rows
|
|
must(rows.length===1&&rows[0].receipt_id===bound.receiptId)
|
|
const charged=(await sql.query("/* probe-storage:charge */ UPDATE server_probe_storage_usage SET admissions=admissions+1,reserved_files=reserved_files+1,reserved_bytes=reserved_bytes+$2 WHERE scope=ANY($1::text[]) RETURNING scope",[["global","owner:"+ownerId],reservation+PROBE_PERMANENT_RESERVATION_BYTES])).rows;must(charged.length===2)
|
|
}
|
|
return writeCancellation(sql,bound)
|
|
})
|
|
},
|
|
/** One invocation only. Admission COMMIT precedes callback/anchor SQL.
|
|
* A crash or lost COMMIT ACK permanently spends this identity. No API to
|
|
* recreate a claim token exists. Callback uses only this guarded SQL. */
|
|
async claim<T>(raw:ProbeStorageScope,options:ProbeStorageOptions,writeAnchor:(sql:ProbeStorageSql)=>Promise<T>):Promise<T>{
|
|
const bound=scope(raw);must(now()<Date.parse(bound.authorityExpiresAt));const token=randomUUID()
|
|
await run(bound.ownerId,"claim",bound,options,async(sql,policy)=>{
|
|
const usage=await counters(sql,bound.ownerId);must(probeStorageFits(policy,usage.global,usage.local))
|
|
const rows=(await sql.query("/* probe-storage:reserve */ INSERT INTO server_probe_storage_admissions(receipt_id,owner_id,identity_kind,execution_id,binding_proof,authority_expires_at,claim_token,retention_days,safety_ms,gc_not_before) SELECT $1,$2,$3,$4,$5,$6::timestamptz,$7,$8::integer,$9::integer,$6::timestamptz+make_interval(days=>$8::integer)+($9::integer*interval '1 millisecond') WHERE $6::timestamptz>clock_timestamp() ON CONFLICT DO NOTHING RETURNING receipt_id",[bound.receiptId,bound.ownerId,bound.identityKind,bound.executionId,bound.bindingProof,bound.authorityExpiresAt,token,policy.retentionDays,deps.safetyWindowMs])).rows
|
|
must(rows.length===1&&rows[0].receipt_id===bound.receiptId)
|
|
const changed=(await sql.query("/* probe-storage:charge */ UPDATE server_probe_storage_usage SET admissions=admissions+1,reserved_files=reserved_files+1,reserved_bytes=reserved_bytes+$2 WHERE scope=ANY($1::text[]) RETURNING scope",[["global","owner:"+bound.ownerId],reservation+PROBE_PERMANENT_RESERVATION_BYTES])).rows;must(changed.length===2)
|
|
})
|
|
return run(bound.ownerId,"claim",bound,options,async sql=>{
|
|
const row=await admission(sql,bound);must(row.cancelled_at==null&&now()<Date.parse(bound.authorityExpiresAt))
|
|
const consumed=(await sql.query("/* probe-storage:consume */ UPDATE server_probe_storage_admissions SET claim_token=NULL,claim_attempted_at=clock_timestamp() WHERE receipt_id=$1 AND claim_token=$2 AND purged_at IS NULL AND cancelled_at IS NULL AND authority_expires_at>clock_timestamp() RETURNING receipt_id",[bound.receiptId,token])).rows
|
|
must(consumed.length===1);return writeAnchor(sql)
|
|
})
|
|
},
|
|
/** Call inside the finish boundary instead of storing ciphertext in the
|
|
* immutable anchor. The supplied terminal update and payload INSERT commit
|
|
* atomically. Full maximum payload capacity was reserved before claim. */
|
|
async publish<T>(raw:ProbeStorageScope,ciphertext:string,proof:string,options:ProbeStorageOptions,writeTerminal:(sql:ProbeStorageSql)=>Promise<T>):Promise<T>{
|
|
const bound=scope(raw);must(typeof ciphertext==="string"&&Buffer.byteLength(ciphertext)>0&&Buffer.byteLength(ciphertext)<=reservation&&hash.test(proof))
|
|
return run(bound.ownerId,"publish",bound,options,async sql=>{
|
|
const row=await admission(sql,bound);must(row.cancelled_at==null&&row.claim_token===null&&row.claim_attempted_at!==null&&now()<Date.parse(bound.authorityExpiresAt))
|
|
const result=await writeTerminal(sql)
|
|
const rows=(await sql.query("/* probe-storage:publish */ INSERT INTO server_probe_execution_payloads(receipt_id,receipt_ciphertext,receipt_proof) VALUES($1,$2,$3) RETURNING receipt_id",[bound.receiptId,ciphertext,proof])).rows
|
|
must(rows.length===1&&rows[0].receipt_id===bound.receiptId);return result
|
|
})
|
|
},
|
|
/** Missing payload after purge is a permanent expired receipt, NEVER an
|
|
* absent attempt. Root read() must return this tombstone without execution. */
|
|
async read(raw:ProbeStorageScope,options:ProbeStorageOptions){
|
|
const bound=scope(raw)
|
|
return run(bound.ownerId,"read",bound,options,async sql=>{
|
|
const rows=(await sql.query("/* probe-storage:read */ SELECT a.owner_id,a.binding_proof,a.identity_kind,a.execution_id,a.purged_at,p.receipt_ciphertext,p.receipt_proof FROM server_probe_storage_admissions a LEFT JOIN server_probe_execution_payloads p USING(receipt_id) WHERE a.receipt_id=$1",[bound.receiptId])).rows
|
|
must(rows.length===1);const row=rows[0];must(row.owner_id===bound.ownerId&&row.binding_proof===bound.bindingProof&&row.identity_kind===bound.identityKind&&row.execution_id===bound.executionId)
|
|
if(row.purged_at!==null)return {kind:"expired" as const,automaticReplay:false as const}
|
|
if(row.receipt_ciphertext===null)return {kind:"unavailable" as const,automaticReplay:false as const}
|
|
must(typeof row.receipt_ciphertext==="string"&&Buffer.byteLength(row.receipt_ciphertext)<=reservation&&typeof row.receipt_proof==="string"&&hash.test(row.receipt_proof))
|
|
return {kind:"payload" as const,ciphertext:row.receipt_ciphertext,proof:row.receipt_proof,automaticReplay:false as const}
|
|
})
|
|
},
|
|
/** Small owner-scoped batch. Stale claimed/orphan admissions are safe only
|
|
* after authority expiration AND retention: they remain permanent unknown
|
|
* attempts. Existing stages, anchors and quarantines are never deleted. */
|
|
async purge(ownerId:string,batchSize:number,options:ProbeStorageOptions){
|
|
must(Number.isSafeInteger(batchSize)&&batchSize>=1&&batchSize<=100)
|
|
return run(ownerId,"purge",undefined,options,async(sql,policy)=>{
|
|
await counters(sql,ownerId)
|
|
const rows=(await sql.query(`/* probe-storage:purge-candidates */ SELECT a.receipt_id FROM server_probe_storage_admissions a
|
|
LEFT JOIN server_probe_executions e USING(receipt_id) LEFT JOIN server_probe_execution_quarantines q USING(receipt_id)
|
|
WHERE a.owner_id=$1 AND a.purged_at IS NULL AND a.authority_expires_at<clock_timestamp()-make_interval(days=>$2)
|
|
AND COALESCE(e.finished_at,q.created_at,a.authority_expires_at)<clock_timestamp()-make_interval(days=>$2)
|
|
ORDER BY a.created_at,a.receipt_id LIMIT $3 FOR UPDATE OF a`,[ownerId,policy.retentionDays,batchSize])).rows
|
|
must(rows.length<=batchSize&&rows.every(row=>typeof row.receipt_id==="string"&&uuid.test(row.receipt_id))&&new Set(rows.map(row=>row.receipt_id)).size===rows.length)
|
|
const ids=rows.map(row=>row.receipt_id);if(!ids.length)return {purged:0,automaticReplay:false as const}
|
|
const changed=(await sql.query("/* probe-storage:mark-purged */ UPDATE server_probe_storage_admissions SET purged_at=clock_timestamp(),claim_token=NULL WHERE owner_id=$1 AND receipt_id=ANY($2::uuid[]) AND purged_at IS NULL RETURNING receipt_id",[ownerId,ids])).rows
|
|
must(changed.length===ids.length&&changed.every(row=>ids.includes(row.receipt_id)))
|
|
await sql.query("/* probe-storage:delete-payload */ DELETE FROM server_probe_execution_payloads WHERE receipt_id=ANY($1::uuid[]) RETURNING receipt_id",[ids])
|
|
const absent=(await sql.query("/* probe-storage:purge-readback */ SELECT receipt_id FROM server_probe_execution_payloads WHERE receipt_id=ANY($1::uuid[])",[ids])).rows;must(absent.length===0)
|
|
const released=(await sql.query("/* probe-storage:release */ UPDATE server_probe_storage_usage SET reserved_files=reserved_files-$2,reserved_bytes=reserved_bytes-$3 WHERE scope=ANY($1::text[]) AND reserved_files>=$2 AND reserved_bytes>=$3 RETURNING scope",[["global","owner:"+ownerId],ids.length,ids.length*reservation])).rows;must(released.length===2)
|
|
return {purged:ids.length,automaticReplay:false as const}
|
|
})
|
|
},
|
|
/** Approved bounded metadata retention, never native execution.
|
|
* Original authority expiry must be validated before all claim attempts,
|
|
* even after this collector removes the old identity tombstone. */
|
|
async collectExpired(ownerId:string,batchSize:number,options:ProbeStorageOptions){
|
|
must(Number.isSafeInteger(batchSize)&&batchSize>=1&&batchSize<=100)
|
|
return run(ownerId,"purge",undefined,options,async(sql,policy)=>{
|
|
await counters(sql,ownerId)
|
|
await sql.query("SET LOCAL mm.probe_storage_gc='on'")
|
|
const rows=(await sql.query(`/* probe-storage:gc-candidates */ SELECT a.receipt_id,a.purged_at,a.authority_expires_at,
|
|
GREATEST(a.authority_expires_at,a.created_at,COALESCE(a.cancelled_at,a.created_at),COALESCE(e.finished_at,a.created_at),COALESCE(q.created_at,a.created_at),
|
|
COALESCE((SELECT max(GREATEST(s.created_at,COALESCE(s.received_at,s.created_at))) FROM server_probe_execution_stages s WHERE s.receipt_id=a.receipt_id),a.created_at)) AS last_activity
|
|
FROM server_probe_storage_admissions a LEFT JOIN server_probe_executions e USING(receipt_id)
|
|
LEFT JOIN server_probe_execution_quarantines q USING(receipt_id)
|
|
WHERE a.owner_id=$1 AND a.gc_not_before<clock_timestamp() AND server_probe_storage_gc_allowed(a.receipt_id)
|
|
AND a.authority_expires_at<clock_timestamp()-($4::bigint*interval '1 millisecond')
|
|
AND GREATEST(a.authority_expires_at,a.created_at,COALESCE(a.cancelled_at,a.created_at),COALESCE(e.finished_at,a.created_at),COALESCE(q.created_at,a.created_at),
|
|
COALESCE((SELECT max(GREATEST(s.created_at,COALESCE(s.received_at,s.created_at))) FROM server_probe_execution_stages s WHERE s.receipt_id=a.receipt_id),a.created_at))
|
|
<clock_timestamp()-make_interval(days=>GREATEST(a.retention_days,$2))- (GREATEST(a.safety_ms,$4)::bigint*interval '1 millisecond')
|
|
ORDER BY a.gc_not_before,a.receipt_id LIMIT $3 FOR UPDATE OF a`,[ownerId,policy.retentionDays,batchSize,deps.safetyWindowMs])).rows
|
|
must(rows.length<=batchSize&&rows.every(row=>typeof row.receipt_id==="string"&&uuid.test(row.receipt_id)&&typeof row.purged_at!=="undefined")&&new Set(rows.map(row=>row.receipt_id)).size===rows.length)
|
|
must(rows.every(row=>probeMetadataGcEligible({authorityExpiresAt:new Date(row.authority_expires_at as string|Date).toISOString(),lastActivityAt:new Date(row.last_activity as string|Date).toISOString()},now(),policy.retentionDays,deps.safetyWindowMs)))
|
|
const ids=rows.map(row=>row.receipt_id);if(!ids.length)return {collected:0,automaticReplay:false as const}
|
|
// The trigger validates the persisted deadline and original authority;
|
|
// this flag ALONE cannot authorize any deletion.
|
|
await sql.query("SET LOCAL mm.probe_storage_gc='on'")
|
|
await sql.query("/* probe-storage:gc-cancellations */ DELETE FROM mcp_diagnostic_cancellations c USING server_probe_storage_admissions a WHERE a.receipt_id=ANY($1::uuid[]) AND a.identity_kind='mcp' AND c.operation_id=a.execution_id AND c.actor_id=a.owner_id",[ids])
|
|
await sql.query("/* probe-storage:gc-payload */ DELETE FROM server_probe_execution_payloads WHERE receipt_id=ANY($1::uuid[])",[ids])
|
|
await sql.query("/* probe-storage:gc-stages */ DELETE FROM server_probe_execution_stages WHERE receipt_id=ANY($1::uuid[])",[ids])
|
|
await sql.query("/* probe-storage:gc-quarantines */ DELETE FROM server_probe_execution_quarantines WHERE receipt_id=ANY($1::uuid[])",[ids])
|
|
await sql.query("/* probe-storage:gc-anchors */ DELETE FROM server_probe_executions WHERE receipt_id=ANY($1::uuid[])",[ids])
|
|
const removed=(await sql.query("/* probe-storage:gc-admissions */ DELETE FROM server_probe_storage_admissions WHERE owner_id=$1 AND receipt_id=ANY($2::uuid[]) AND gc_not_before<clock_timestamp() RETURNING receipt_id",[ownerId,ids])).rows
|
|
must(removed.length===ids.length&&removed.every(row=>ids.includes(row.receipt_id))&&new Set(removed.map(row=>row.receipt_id)).size===ids.length)
|
|
const files=rows.filter(row=>row.purged_at===null).length,bytes=files*reservation+ids.length*PROBE_PERMANENT_RESERVATION_BYTES
|
|
const released=(await sql.query("/* probe-storage:gc-release */ UPDATE server_probe_storage_usage SET admissions=admissions-$2,reserved_files=reserved_files-$3,reserved_bytes=reserved_bytes-$4 WHERE scope=ANY($1::text[]) AND admissions>=$2 AND reserved_files>=$3 AND reserved_bytes>=$4 RETURNING scope",[["global","owner:"+ownerId],ids.length,files,bytes])).rows;must(released.length===2)
|
|
await sql.query("/* probe-storage:gc-owner-counter */ DELETE FROM server_probe_storage_usage WHERE scope=$1 AND admissions=0 AND reserved_files=0 AND reserved_bytes=0",["owner:"+ownerId])
|
|
const absent=(await sql.query("/* probe-storage:gc-readback */ SELECT a.receipt_id FROM server_probe_storage_admissions a WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT e.receipt_id FROM server_probe_executions e WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT s.receipt_id FROM server_probe_execution_stages s WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT q.receipt_id FROM server_probe_execution_quarantines q WHERE receipt_id=ANY($1::uuid[]) UNION ALL SELECT p.receipt_id FROM server_probe_execution_payloads p WHERE receipt_id=ANY($1::uuid[])",[ids])).rows;must(absent.length===0)
|
|
return {collected:ids.length,automaticReplay:false as const}
|
|
},"expiry-write")
|
|
},
|
|
/** DRAFT: only expired cancellation records of terminal real own diagnostic
|
|
* operations. Does not delete operations, plans, connections or audit. */
|
|
async collectExpiredCancellations(ownerId:string,batchSize:number,options:ProbeStorageOptions){
|
|
must(Number.isSafeInteger(batchSize)&&batchSize>=1&&batchSize<=100)
|
|
return run(ownerId,"purge",undefined,options,async(sql,policy)=>{
|
|
await counters(sql,ownerId)
|
|
await sql.query("SET LOCAL mm.probe_storage_gc='on'")
|
|
const rows=(await sql.query(`/* probe-storage:cancel-gc-candidates */ SELECT c.operation_id,c.connection_id,c.actor_id,
|
|
p.expires_at AS authority_expires_at,GREATEST(p.expires_at,o.finished_at,c.created_at) AS last_activity
|
|
FROM mcp_diagnostic_cancellations c JOIN mcp_operations o ON o.id=c.operation_id AND o.connection_id=c.connection_id
|
|
JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id
|
|
JOIN mcp_connections owner_connection ON owner_connection.id=o.connection_id
|
|
WHERE owner_connection.owner_id=$1 AND c.actor_id=$1 AND p.operation_id='servers.probes.run'
|
|
AND EXISTS(SELECT 1 FROM server_probe_storage_admissions a WHERE a.identity_kind='mcp' AND a.execution_id=c.operation_id AND a.owner_id=$1 AND server_probe_storage_gc_allowed(a.receipt_id))
|
|
AND o.state IN ('applied','failed','unknown') AND o.finished_at IS NOT NULL
|
|
AND GREATEST(p.expires_at,o.finished_at,c.created_at)<clock_timestamp()-make_interval(days=>$2)-($4::bigint*interval '1 millisecond')
|
|
ORDER BY c.created_at,c.operation_id LIMIT $3 FOR UPDATE OF c,o,p,owner_connection`,[ownerId,policy.retentionDays,batchSize,deps.safetyWindowMs])).rows
|
|
must(rows.length<=batchSize&&rows.every(row=>typeof row.operation_id==="string"&&uuid.test(row.operation_id)&&row.actor_id===ownerId&&typeof row.connection_id==="string"&&uuid.test(row.connection_id))&&new Set(rows.map(row=>row.operation_id)).size===rows.length)
|
|
must(rows.every(row=>probeMetadataGcEligible({authorityExpiresAt:new Date(row.authority_expires_at as string|Date).toISOString(),lastActivityAt:new Date(row.last_activity as string|Date).toISOString()},now(),policy.retentionDays,deps.safetyWindowMs)))
|
|
const ids=rows.map(row=>row.operation_id);if(!ids.length)return {collected:0,automaticReplay:false as const}
|
|
await sql.query("SET LOCAL mm.probe_storage_gc='on'")
|
|
// Trigger independently rechecks terminal real operation and persisted
|
|
// cancellation gc_not_before; app SQL rechecks the current owner join.
|
|
const removed=(await sql.query(`/* probe-storage:cancel-gc-delete */ DELETE FROM mcp_diagnostic_cancellations c USING mcp_operations o,mcp_plans p,mcp_connections owner_connection
|
|
WHERE c.operation_id=ANY($1::uuid[]) AND c.actor_id=$2 AND o.id=c.operation_id AND o.connection_id=c.connection_id
|
|
AND p.id=o.plan_id AND p.connection_id=o.connection_id AND owner_connection.id=o.connection_id AND owner_connection.owner_id=$2
|
|
AND p.operation_id='servers.probes.run' AND o.state IN ('applied','failed','unknown') AND o.finished_at IS NOT NULL
|
|
AND GREATEST(p.expires_at,o.finished_at,c.created_at)<clock_timestamp()-make_interval(days=>$3)-($4::bigint*interval '1 millisecond') RETURNING c.operation_id`,[ids,ownerId,policy.retentionDays,deps.safetyWindowMs])).rows
|
|
must(removed.length===ids.length&&removed.every(row=>ids.includes(row.operation_id))&&new Set(removed.map(row=>row.operation_id)).size===ids.length)
|
|
// Cancellation metadata shares the original admission reservation.
|
|
// Only collectExpired refunds that reservation after deleting admission.
|
|
await sql.query("/* probe-storage:gc-owner-counter */ DELETE FROM server_probe_storage_usage WHERE scope=$1 AND admissions=0 AND reserved_files=0 AND reserved_bytes=0",["owner:"+ownerId])
|
|
const absent=(await sql.query("/* probe-storage:cancel-gc-readback */ SELECT operation_id FROM mcp_diagnostic_cancellations WHERE operation_id=ANY($1::uuid[])",[ids])).rows;must(absent.length===0)
|
|
return {collected:ids.length,automaticReplay:false as const}
|
|
})
|
|
},
|
|
}
|
|
}
|