@@ -116,3 +116,154 @@ describe("HyperFrames skill helpers", () => {
116116 }
117117 } ) ;
118118} ) ;
119+
120+ // ── Transient-init retry (the zero-duration false-fail fix) ─────────────────
121+ // A valid modular project's sub-composition timelines register asynchronously;
122+ // the first initializeSession can time out with the transient "zero duration /
123+ // Runtime ready: false" diagnostic. The render pipeline closes the crashed
124+ // session and retries once with a fresh browser (probeStage) — the standalone
125+ // helpers must do the same instead of reporting the project as zero-duration.
126+
127+ /** Write a fake node_modules with the given producer index.mjs source. */
128+ function writeFakeEnv ( root , producerIndexSource ) {
129+ const packageDir = join ( root , "node_modules" , "@hyperframes" , "producer" ) ;
130+ const corePackageDir = join ( root , "node_modules" , "@hyperframes" , "core" ) ;
131+ const sharpPackageDir = join ( root , "node_modules" , "sharp" ) ;
132+ const compositionDir = join ( root , "composition" ) ;
133+ mkdirSync ( packageDir , { recursive : true } ) ;
134+ mkdirSync ( corePackageDir , { recursive : true } ) ;
135+ mkdirSync ( sharpPackageDir , { recursive : true } ) ;
136+ mkdirSync ( compositionDir , { recursive : true } ) ;
137+ writeFileSync (
138+ join ( packageDir , "package.json" ) ,
139+ JSON . stringify ( { name : "@hyperframes/producer" , type : "module" , exports : "./index.mjs" } ) ,
140+ ) ;
141+ writeFileSync ( join ( packageDir , "index.mjs" ) , producerIndexSource ) ;
142+ writeFileSync (
143+ join ( corePackageDir , "package.json" ) ,
144+ JSON . stringify ( {
145+ name : "@hyperframes/core" ,
146+ type : "module" ,
147+ exports : { "." : "./index.mjs" , "./compiler" : "./compiler.mjs" } ,
148+ } ) ,
149+ ) ;
150+ writeFileSync (
151+ join ( corePackageDir , "index.mjs" ) ,
152+ "export function parseFps(input) { return { ok: true, value: { num: Number(input), den: 1 } }; }" ,
153+ ) ;
154+ writeFileSync (
155+ join ( corePackageDir , "compiler.mjs" ) ,
156+ 'export async function bundleToSingleHtml() { return "<!doctype html><main>x</main>"; }' ,
157+ ) ;
158+ writeFileSync (
159+ join ( sharpPackageDir , "package.json" ) ,
160+ JSON . stringify ( { name : "sharp" , type : "module" , exports : "./index.mjs" } ) ,
161+ ) ;
162+ writeFileSync ( join ( sharpPackageDir , "index.mjs" ) , "export default function sharp() {}\n" ) ;
163+ return compositionDir ;
164+ }
165+
166+ function runHelper ( helper , root , compositionDir ) {
167+ const result = spawnSync ( process . execPath , [ helper , compositionDir , "--out" , join ( root , "out" ) ] , {
168+ encoding : "utf8" ,
169+ env : { ...process . env , HYPERFRAMES_SKILL_NODE_MODULES : join ( root , "node_modules" ) } ,
170+ } ) ;
171+ return `${ result . stdout } \n${ result . stderr } ` ;
172+ }
173+
174+ const FAKE_PRODUCER_COMMON = [
175+ 'export async function createFileServer() { return { url: "http://test", close() {} }; }' ,
176+ 'export async function createCaptureSession() { console.error("SESSION_CREATED"); return {}; }' ,
177+ 'export async function closeCaptureSession() { console.error("SESSION_CLOSED"); }' ,
178+ "export async function getCompositionDuration() { return 0; }" ,
179+ ] . join ( "\n" ) ;
180+
181+ describe ( "transient-init retry" , ( ) => {
182+ for ( const helper of HELPERS ) {
183+ it ( `${ helper . split ( "/" ) . at ( - 1 ) } retries a transient zero-duration init once with a fresh session` , ( ) => {
184+ const root = mkdtempSync ( join ( tmpdir ( ) , "hyperframes-skill-retry-test-" ) ) ;
185+ try {
186+ const compositionDir = writeFakeEnv (
187+ root ,
188+ [
189+ FAKE_PRODUCER_COMMON ,
190+ "let initCalls = 0;" ,
191+ "export async function initializeSession() {" ,
192+ " initCalls++;" ,
193+ " if (initCalls === 1) {" ,
194+ // The transient shape: readiness deadline hit before async
195+ // sub-composition timelines landed (Runtime ready: false).
196+ ' throw new Error("Composition has zero duration after initialization.\\nRuntime ready: false");' ,
197+ " }" ,
198+ ' throw new Error("INIT_ATTEMPT_2_REACHED");' ,
199+ "}" ,
200+ ] . join ( "\n" ) ,
201+ ) ;
202+
203+ const output = runHelper ( helper , root , compositionDir ) ;
204+
205+ // Retried: fresh session created for attempt 2, crashed one closed.
206+ assert . match ( output , / r e t r y i n g w i t h a f r e s h b r o w s e r s e s s i o n / ) ;
207+ assert . equal ( ( output . match ( / S E S S I O N _ C R E A T E D / g) ?? [ ] ) . length , 2 ) ;
208+ assert . equal ( ( output . match ( / S E S S I O N _ C L O S E D / g) ?? [ ] ) . length , 2 ) ;
209+ // ...and the retry genuinely re-ran init (bounded: no third attempt).
210+ assert . match ( output , / I N I T _ A T T E M P T _ 2 _ R E A C H E D / ) ;
211+ } finally {
212+ rmSync ( root , { recursive : true , force : true } ) ;
213+ }
214+ } ) ;
215+
216+ it ( `${ helper . split ( "/" ) . at ( - 1 ) } does NOT retry a genuine authoring failure (Runtime ready: true)` , ( ) => {
217+ const root = mkdtempSync ( join ( tmpdir ( ) , "hyperframes-skill-retry-test-" ) ) ;
218+ try {
219+ const compositionDir = writeFakeEnv (
220+ root ,
221+ [
222+ FAKE_PRODUCER_COMMON ,
223+ "export async function initializeSession() {" ,
224+ // The fast-fail shape: runtime IS ready, there is genuinely no
225+ // timeline/duration — an authoring bug retries can't fix.
226+ ' throw new Error("Composition has zero duration after initialization.\\nRuntime ready: true");' ,
227+ "}" ,
228+ ] . join ( "\n" ) ,
229+ ) ;
230+
231+ const output = runHelper ( helper , root , compositionDir ) ;
232+
233+ assert . doesNotMatch ( output , / r e t r y i n g w i t h a f r e s h b r o w s e r s e s s i o n / ) ;
234+ assert . equal ( ( output . match ( / S E S S I O N _ C R E A T E D / g) ?? [ ] ) . length , 1 ) ;
235+ assert . match ( output , / C o m p o s i t i o n h a s z e r o d u r a t i o n / ) ;
236+ } finally {
237+ rmSync ( root , { recursive : true , force : true } ) ;
238+ }
239+ } ) ;
240+ }
241+
242+ it ( "prefers the producer's own isTransientBrowserError classifier when exported" , ( ) => {
243+ const root = mkdtempSync ( join ( tmpdir ( ) , "hyperframes-skill-retry-test-" ) ) ;
244+ try {
245+ const compositionDir = writeFakeEnv (
246+ root ,
247+ [
248+ FAKE_PRODUCER_COMMON ,
249+ // A message the frozen fallback patterns would NOT match — only the
250+ // producer-provided classifier can mark it transient.
251+ "export function isTransientBrowserError(err) { return String(err && err.message).includes('CUSTOM_TRANSIENT'); }" ,
252+ "let initCalls = 0;" ,
253+ "export async function initializeSession() {" ,
254+ " initCalls++;" ,
255+ ' if (initCalls === 1) throw new Error("CUSTOM_TRANSIENT flake");' ,
256+ ' throw new Error("INIT_ATTEMPT_2_REACHED");' ,
257+ "}" ,
258+ ] . join ( "\n" ) ,
259+ ) ;
260+
261+ const output = runHelper ( HELPERS [ 0 ] , root , compositionDir ) ;
262+
263+ assert . match ( output , / r e t r y i n g w i t h a f r e s h b r o w s e r s e s s i o n / ) ;
264+ assert . match ( output , / I N I T _ A T T E M P T _ 2 _ R E A C H E D / ) ;
265+ } finally {
266+ rmSync ( root , { recursive : true , force : true } ) ;
267+ }
268+ } ) ;
269+ } ) ;
0 commit comments