Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/typescript-worker-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,7 @@ The new fixed read-only release-evidence receipt combines the existing qualified
## Qualification synchronisée : budget de lectures

Studio #125, main `ca599140cf9e3c2f135e4c85f043f402457874bd`: [offline build](https://github.com/Trigenys/trigenys-studio/actions/runs/47372776907) and [migration](https://github.com/Trigenys/trigenys-studio/actions/runs/47372776967) succeeded, but [joined qualification](https://github.com/Trigenys/trigenys-studio/actions/runs/47372776920) returned `STUDIO_BUILD_QUALIFICATION_UNAVAILABLE` HTTP 503. Independent download of DB artifact 15858414443: 695 bytes, CRC valid, SHA-256 `1c7213f69789cbd06ba310b26c580b48b96930bc8c116362375c67c306a70d1c`, correct exact revision, 15 migrations, 30 tables, expected journal digest, observation 2026-12-04T16:10:03.152Z. The exact cause of the 503 is not established. Cloudflare documents 50 external subrequests on the Free plan: https://developers.cloudflare.com/workers/platform/limits/. The joined code duplicated migration continuity and issued additional restricted installation tokens. This change uses one request-local read authority and one continuity proof for migration, resource inputs and fresh DB receipt; no process/global cache, no external credential transfer, no skipped final run/head check, no extra privileges. Standalone build/readiness APIs remain compatible. The build reader still independently qualifies its own artifact. Real runtime qualification is required before claiming the 503 fixed. No deployment authority.

## Qualification failure isolation — 2026-12-04

After #259, Studio run 47372776920 job 145267060105 still returned generic HTTP 503 despite successful database receipt (migration run 47372776967 attempt 3) and offline build. The precise root cause remains unconfirmed. Combined qualification now shares one request-local scoped GitHub read authority across build, historical migration, resources and current DB receipt. All source and freshness checks remain. Dependency failures return a fixed phase AUTHORITY/BUILD/MIGRATION/RESOURCES/DATABASE and a safe allowlisted error code; unknown errors become fixed phase-specific UNAVAILABLE codes, never provider messages, URLs, credentials or response bodies. This improves the diagnosis and removes three redundant authority requests without granting release authority. Studio must run the new broker on the exact current revision, including fresh DB evidence, before any staging deployment claim. No new permission, resource mutation or bypass.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { authenticateStudioStagingMigration, validateStudioStagingMigrationInten
import { issueDatabaseLease, DatabaseLeaseError } from "./database-lease";
import { authenticateStudioBuildQualification } from "./auth";
import { qualifyStudioStagingBuildFromGitHub } from "./studio-staging-build-reader";
import { prepareStudioStagingReadiness, prepareStudioStagingReleaseEvidence } from "./studio-staging-readiness";
import { StudioQualificationPhaseError, prepareStudioStagingReadiness, prepareStudioStagingReleaseEvidence } from "./studio-staging-readiness";
import { hasEmptyRequestBody } from "./empty-request-body";
import { provisionApprovedNeon, NeonProvisioningError } from "./neon-provisioning";
import { listSecretNames, putSecret } from "./brownfield-worker";
Expand Down Expand Up @@ -1454,6 +1454,7 @@ export default {
"STUDIO_STAGING_REVISION_INVALID","STUDIO_STAGING_REVISION_STALE","STUDIO_STAGING_REPOSITORY_INVALID",
"STUDIO_STAGING_R2_MARKER_INVALID","STAGING_CONFIG_NOT_ISOLATED",
"STUDIO_DB_RECEIPT_UNQUALIFIED","STUDIO_RELEASE_INPUTS_MISMATCH"]);
if(error instanceof StudioQualificationPhaseError)return receipt({error:error.message,phase:error.phase},503);
return receipt({error:known.has(code)?code:"STUDIO_BUILD_QUALIFICATION_UNAVAILABLE"},503);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/roadmap-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,12 @@ async function studioDigest(bytes:Uint8Array){
* Result is raw evidence for the independent verifier, not an authorization.
*/
export async function readStudioBuildEvidence(revision:string,mintAppJwt:()=>Promise<string>,fetcher:typeof fetch=(...a)=>fetch(...a),now=Date.now()){
const path="/repos/"+STUDIO_BUILD_REPO;
if(!SHA.test(revision))throw new ReadError("invalid");
const {token,repository}=await studioReadAuthority(mintAppJwt,fetcher);
return readStudioBuildEvidenceWithAuthority(revision,await studioReadAuthority(mintAppJwt,fetcher),fetcher,now);
}
async function readStudioBuildEvidenceWithAuthority(revision:string,authority:StudioReadAuthority,fetcher:typeof fetch,now:number){
const path="/repos/"+STUDIO_BUILD_REPO;
const {token,repository}=authority;
const current=async()=>{
const ref=object((await request(fetcher,token,path+"/git/ref/heads/main")).data);
if(object(ref.object).sha!==revision)throw new ReadError("invalid");
Expand Down Expand Up @@ -777,6 +780,7 @@ async function readStudioFreshDbReceiptWithAuthority(revision:string,authority:S
* Fresh evidence always follows the independent continuity check.
*/
export async function withStudioDbEvidenceReader<T>(revision:string,mintAppJwt:()=>Promise<string>,use:(reader:{
build:(now?:number)=>ReturnType<typeof readStudioBuildEvidenceWithAuthority>;
continuity:()=>ReturnType<typeof readStudioMigrationContinuityWithAuthority>;
fresh:(now?:number)=>ReturnType<typeof readStudioFreshDbReceiptWithAuthority>;
inputs:<R>(read:(token:string)=>Promise<R>)=>Promise<R>;
Expand All @@ -786,6 +790,7 @@ export async function withStudioDbEvidenceReader<T>(revision:string,mintAppJwt:(
let checked:ReturnType<typeof readStudioMigrationContinuityWithAuthority>|undefined;
const continuity=()=>checked??=readStudioMigrationContinuityWithAuthority(revision,authority,fetcher);
return use({
build:(now=Date.now())=>readStudioBuildEvidenceWithAuthority(revision,authority,fetcher,now),
continuity,
fresh:async(now=Date.now())=>{await continuity();return readStudioFreshDbReceiptWithAuthority(revision,authority,fetcher,now);},
inputs:read=>read(authority.token)
Expand Down
51 changes: 44 additions & 7 deletions src/studio-staging-readiness.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
import type { Env } from "./types";
import { createGitHubAppJwt } from "./github";
import { qualifyStudioStagingBuildFromGitHub } from "./studio-staging-build-reader";
import { verifyStudioStagingBuildEvidence } from "./studio-staging-build-evidence";
import { prepareStudioStagingReleaseInputs } from "./studio-staging-release-preflight";
import { readStudioMigrationContinuity, readStudioFreshDbReceipt, withStudioReadAuthority, withStudioDbEvidenceReader } from "./roadmap-reader";

type QualificationPhase = "AUTHORITY" | "BUILD" | "MIGRATION" | "RESOURCES" | "DATABASE";
const SAFE_PHASE_CODES = new Set([
"STUDIO_BUILD_AUTHORITY_INVALID", "STUDIO_BUILD_RUN_UNQUALIFIED", "STUDIO_BUILD_ARTIFACT_UNQUALIFIED",
"STUDIO_BUILD_WRAPPER_MISMATCH", "STUDIO_BUILD_MANIFEST_MISMATCH", "STUDIO_BUILD_ARCHIVE_MISMATCH",
"STUDIO_BUILD_EVIDENCE_INVALID", "ROADMAP_DENIED", "ROADMAP_INVALID", "ROADMAP_UNAVAILABLE",
"STUDIO_RELEASE_REVISION_INVALID", "STUDIO_RELEASE_BUILD_MISMATCH", "STUDIO_RELEASE_MIGRATION_MISMATCH",
"STUDIO_RELEASE_CONFIG_MISMATCH", "STUDIO_MIGRATION_SOURCE_CHANGED", "STUDIO_STAGING_FILE_UNQUALIFIED",
"STUDIO_STAGING_REVISION_INVALID", "STUDIO_STAGING_REVISION_STALE", "STUDIO_STAGING_REPOSITORY_INVALID",
"STUDIO_STAGING_R2_MARKER_INVALID", "STAGING_CONFIG_NOT_ISOLATED", "STUDIO_DB_RECEIPT_UNQUALIFIED",
"STUDIO_RELEASE_INPUTS_MISMATCH", "HYPERDRIVE_STAGING_RECOVERY_REQUIRED",
"HYPERDRIVE_STAGING_PROFILE_NOT_QUALIFIED", "HYPERDRIVE_DATABASE_PROFILE_NOT_FOUND",
"HYPERDRIVE_MARKER_INVALID", "HYPERDRIVE_MARKER_MISMATCH", "HYPERDRIVE_NAME_AMBIGUOUS",
"R2_STAGING_RECOVERY_REQUIRED", "R2_STAGING_OWNERSHIP_REQUIRED", "R2_STAGING_BUCKET_NOT_QUALIFIED",
"R2_STAGING_PUBLIC_ACCESS_UNQUALIFIED", "CLOUDFLARE_R2_ACCESS_DENIED", "CLOUDFLARE_R2_API_ERROR",
"CLOUDFLARE_R2_TOKEN_REQUIRED", "CLOUDFLARE_RESOURCE_TOKEN_REQUIRED", "CLOUDFLARE_ACCOUNT_ID_REQUIRED"
]);
export class StudioQualificationPhaseError extends Error {
constructor(readonly phase:QualificationPhase, error:unknown) {
const candidate=error instanceof Error?error.message:"";
const typedCode=error instanceof Error?(error as Error & {code?:unknown}).code:undefined;
super(typeof typedCode==="string"&&SAFE_PHASE_CODES.has(typedCode)?typedCode:
SAFE_PHASE_CODES.has(candidate)?candidate:"STUDIO_QUALIFICATION_"+phase+"_UNAVAILABLE");
this.name="StudioQualificationPhaseError";
}
}
async function inPhase<T>(phase:QualificationPhase, work:()=>Promise<T>):Promise<T>{
try{return await work();}
catch(error){
if(error instanceof StudioQualificationPhaseError)throw error;
throw new StudioQualificationPhaseError(phase,error);
}
}
type Build=Awaited<ReturnType<typeof qualifyStudioStagingBuildFromGitHub>>;
type Migration=Awaited<ReturnType<typeof readStudioMigrationContinuity>>;
type Inputs=Awaited<ReturnType<typeof prepareStudioStagingReleaseInputs>>;
Expand All @@ -19,16 +52,16 @@ interface ReadinessDependencies {
*/
export async function qualifyStudioStagingReadiness(revision:string,deps:ReadinessDependencies){
if(!/^[a-f0-9]{40}$/.test(revision))throw Error("STUDIO_RELEASE_REVISION_INVALID");
const build=await deps.build(revision);
const build=await inPhase("BUILD",()=>deps.build(revision));
if(build.status!=="VERIFIED_OFFLINE_BUILD_ONLY"||build.revision!==revision||
build.repository!=="Trigenys/trigenys-studio"||build.deploymentAuthorized!==false||
build.liveBindingsVerified!==false||!/^[a-f0-9]{64}$/.test(build.sourceConfigSha256))
throw Error("STUDIO_RELEASE_BUILD_MISMATCH");
const migration=await deps.migration(revision);
const migration=await inPhase("MIGRATION",()=>deps.migration(revision));
if(migration.status!=="MIGRATION_SOURCE_CONTINUITY_ONLY"||migration.revision!==revision||migration.liveDatabaseVerified!==false)
throw Error("STUDIO_RELEASE_MIGRATION_MISMATCH");
// Live provider readback last; preflight rechecks current main before return.
const inputs=await deps.inputs(revision);
const inputs=await inPhase("RESOURCES",()=>deps.inputs(revision));
if(inputs.status!=="QUALIFIED_INPUTS_ONLY"||inputs.revision!==revision||inputs.repository!==build.repository||
inputs.environment!=="staging"||inputs.worker!=="trigenys-studio-staging-api"||
inputs.sourceConfigSha256!==build.sourceConfigSha256||!/^[a-f0-9]{64}$/.test(inputs.configSha256))
Expand Down Expand Up @@ -69,7 +102,7 @@ export async function qualifyStudioStagingReleaseEvidence(revision:string,deps:{
ready.worker!=="trigenys-studio-staging-api"||ready.resourceReadbackVerified!==true||
ready.liveBindingsVerified!==false||ready.deploymentAuthorized!==false)
throw Error("STUDIO_RELEASE_INPUTS_MISMATCH");
const database=await deps.database(revision);
const database=await inPhase("DATABASE",()=>deps.database(revision));
const checked=typeof database.checkedAt==="string"?Date.parse(database.checkedAt):NaN;
const observed=now();
if(database.status!=="VERIFIED_FRESH_STAGING_DB_RECEIPT"||database.revision!==revision||
Expand All @@ -83,14 +116,18 @@ export async function qualifyStudioStagingReleaseEvidence(revision:string,deps:{
liveBindingsVerified:false as const,deploymentAuthorized:false as const};
}
export async function prepareStudioStagingReleaseEvidence(revision:string,env:Env){
return withStudioDbEvidenceReader(revision,()=>createGitHubAppJwt(env),reader=>
return inPhase("AUTHORITY",()=>withStudioDbEvidenceReader(revision,()=>createGitHubAppJwt(env),reader=>
qualifyStudioStagingReleaseEvidence(revision,{
readiness:sha=>qualifyStudioStagingReadiness(sha,{
build:ref=>qualifyStudioStagingBuildFromGitHub(ref,env),
build:async()=>{
const evidence=await reader.build();
const receipt=await verifyStudioStagingBuildEvidence(evidence.authority,evidence.bytes);
return {...receipt,sourceConfigSha256:evidence.authority.sourceConfigSha256};
},
migration:()=>reader.continuity(),
inputs:ref=>reader.inputs(token=>prepareStudioStagingReleaseInputs(token,env,ref))
}),
database:()=>reader.fresh()
})
);
));
}
12 changes: 12 additions & 0 deletions test/typescript-staging-recipe.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ const never= data("export const createGitHubAppJwt=()=>{throw Error('fixture onl
const {qualifyStudioStagingReadiness,qualifyStudioStagingReleaseEvidence}=await import(data(compile(readinessSource)
.replaceAll('from "./github"','from "'+never+'"')
.replaceAll('from "./studio-staging-build-reader"','from "'+never+'"')
.replaceAll('from "./studio-staging-build-evidence"','from "'+data("export const verifyStudioStagingBuildEvidence=()=>{throw Error('fixture only')}")+'"')
.replaceAll('from "./studio-staging-release-preflight"','from "'+never+'"')
.replaceAll('from "./roadmap-reader"','from "'+never+'"')));
function readinessFixture({build={},migration={},inputs={}}={}){
Expand Down Expand Up @@ -295,3 +296,14 @@ test("final release evidence joins independently qualified database observation
}
await assert.rejects(qualifyStudioStagingReleaseEvidence(releaseRevision,{...deps,readiness:async()=>({...ready,revision:"f".repeat(40)})},()=>now));
});


test("qualification dependency failure returns fixed phase and never provider details",async()=>{
for(const [key,phase] of [["build","BUILD"],["migration","MIGRATION"],["inputs","RESOURCES"]]){
const {deps}=readinessFixture();
deps[key]=async()=>{throw Error("fixture-private-provider-message")};
await assert.rejects(qualifyStudioStagingReadiness(releaseRevision,deps),e=>e.phase===phase&&e.message==="STUDIO_QUALIFICATION_"+phase+"_UNAVAILABLE"&&!String(e).includes("fixture-private"));
}
const ready=await qualifyStudioStagingReadiness(releaseRevision,readinessFixture().deps);
await assert.rejects(qualifyStudioStagingReleaseEvidence(releaseRevision,{readiness:async()=>ready,database:async()=>{throw Error("ROADMAP_DENIED")}}),e=>e.phase==="DATABASE"&&e.message==="ROADMAP_DENIED");
});