Skip to content

Commit a5234e5

Browse files
committed
refactor: extract write() catch-block into handleSessionScriptWriteFailure
Pure structural refactor, no behavior change: moves the diagnose + classify-and-return / AppError-rethrow logic out of SessionScriptWriter.write into a module-level helper. Drops write()'s cyclomatic below Fallow's threshold (the finding CI flagged on #1266) — the extracted throw still propagates out of the catch exactly as before.
1 parent bb4cfa0 commit a5234e5

1 file changed

Lines changed: 42 additions & 28 deletions

File tree

‎src/daemon/session-script-writer.ts‎

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -94,34 +94,7 @@ export class SessionScriptWriter {
9494
if (repairArmed) session.saveScriptCommitted = true;
9595
return { written: true, path: scriptPath };
9696
} catch (error) {
97-
emitDiagnostic({
98-
level: 'warn',
99-
phase: 'session_script_write_failed',
100-
data: {
101-
session: session.name,
102-
path: scriptPath,
103-
error: error instanceof Error ? error.message : String(error),
104-
},
105-
});
106-
// ADR 0012 decision 6, R4 + BLOCKER 2: a repair COMMIT failure must be
107-
// SURFACED (no-clobber refusal, bare-`@ref`, or a filesystem error alike)
108-
// so close/teardown can report it and keep the session for retry — never
109-
// swallowed into a silent `{written:false}`. Ordinary (non-repair)
110-
// recording keeps its existing SHAPE of behavior: an AppError still
111-
// fails loud (thrown, not swallowed into `{written:false}`) and any other
112-
// fs error is a quiet skip — but an AppError is no longer only
113-
// theoretical here. Since `publishHealedScriptAtomically` refuses ANY
114-
// pre-existing target uniformly (maintainer-approved: refuse-on-exist
115-
// applies to ordinary recording too, not just repair heals), an ordinary
116-
// `open`/`close --save-script` write against an existing target now
117-
// throws that same no-clobber AppError, surfacing here as a genuine
118-
// "fails loud" case rather than the "none is raised on that path" it was
119-
// before that change.
120-
if (repairArmed) {
121-
return { written: false, error: toRepairCommitFailure(error, scriptPath) };
122-
}
123-
if (error instanceof AppError) throw error;
124-
return { written: false };
97+
return handleSessionScriptWriteFailure({ session, error, scriptPath, repairArmed });
12598
}
12699
}
127100

@@ -136,6 +109,47 @@ export class SessionScriptWriter {
136109
}
137110
}
138111

112+
/**
113+
* `write()`'s catch-block classifier, extracted verbatim (no behavior change):
114+
* diagnose, then route by whether the session is repair-armed.
115+
*
116+
* ADR 0012 decision 6, R4 + BLOCKER 2: a repair COMMIT failure must be
117+
* SURFACED (no-clobber refusal, bare-`@ref`, or a filesystem error alike) so
118+
* close/teardown can report it and keep the session for retry — never
119+
* swallowed into a silent `{written:false}`. Ordinary (non-repair) recording
120+
* keeps its existing SHAPE of behavior: an AppError still fails loud (thrown,
121+
* not swallowed into `{written:false}`) and any other fs error is a quiet
122+
* skip — but an AppError is no longer only theoretical here. Since
123+
* `publishHealedScriptAtomically` refuses ANY pre-existing target uniformly
124+
* (maintainer-approved: refuse-on-exist applies to ordinary recording too, not
125+
* just repair heals), an ordinary `open`/`close --save-script` write against
126+
* an existing target now throws that same no-clobber AppError, surfacing here
127+
* as a genuine "fails loud" case rather than the "none is raised on that path"
128+
* it was before that change.
129+
*/
130+
function handleSessionScriptWriteFailure(params: {
131+
session: SessionState;
132+
error: unknown;
133+
scriptPath: string | undefined;
134+
repairArmed: boolean;
135+
}): SessionScriptWriteResult {
136+
const { session, error, scriptPath, repairArmed } = params;
137+
emitDiagnostic({
138+
level: 'warn',
139+
phase: 'session_script_write_failed',
140+
data: {
141+
session: session.name,
142+
path: scriptPath,
143+
error: error instanceof Error ? error.message : String(error),
144+
},
145+
});
146+
if (repairArmed) {
147+
return { written: false, error: toRepairCommitFailure(error, scriptPath) };
148+
}
149+
if (error instanceof AppError) throw error;
150+
return { written: false };
151+
}
152+
139153
/**
140154
* ADR 0012 decision 6 (BLOCKER 2c): normalizes a repair-commit failure into a
141155
* distinct, surfaceable AppError. A no-clobber refusal or a bare-`@ref` failure

0 commit comments

Comments
 (0)