@@ -26,6 +26,7 @@ import { mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSyn
2626import { tmpdir } from 'node:os' ;
2727import { dirname , join , relative , resolve , sep } from 'node:path' ;
2828
29+ import { SCENARIO_TIERS , type ScenarioTier } from '../schema.js' ;
2930import { parseStreamJson , type ParsedTurn , type TurnProcess } from './transcript.js' ;
3031
3132/** Which run step a turn belongs to; the replay key is built from it. */
@@ -46,6 +47,8 @@ export interface AuthorTask extends StepRef {
4647 /** The project directory the agent edits. */
4748 cwd : string ;
4849 prompt : string ;
50+ /** The card's tier: it sizes the live driver's turn cap ({@link DEFAULT_AUTHOR_MAX_TURNS}). Not part of the replay key. */
51+ tier : ScenarioTier ;
4952}
5053
5154export interface AskTask extends StepRef {
@@ -71,6 +74,10 @@ export interface DriverInfo {
7174 apiBilled : false ;
7275 /** The live driver's wall-clock budget per authoring turn (build and iterate), ms. */
7376 authorTimeoutMs ?: number ;
77+ /** The live driver's turn cap per authoring turn, by card tier (`--author-max-turns` sets every tier alike). */
78+ authorMaxTurns ?: Record < ScenarioTier , number > ;
79+ /** The live driver's turn cap per question or operation turn. */
80+ askMaxTurns ?: number ;
7481}
7582
7683export interface AgentDriver {
@@ -121,13 +128,29 @@ export function assertSubscriptionLogin(env: NodeJS.ProcessEnv, authStatus: unkn
121128 */
122129export const DEFAULT_AUTHOR_TIMEOUT_MIN = 60 ;
123130
131+ /**
132+ * The agentic turns one authoring turn (build or iterate) may take unless
133+ * `--author-max-turns` says otherwise, by the card's tier. A `control` card's
134+ * 2–3-object build finished in 17–21 turns under the old flat cap of 80, which
135+ * stays. Every `starter` build measured hit that 80 (`error_max_turns`) after
136+ * 15–24 minutes, 11–18 s a turn: the 60-minute author timeout holds 200–325
137+ * such turns, so 300 puts the turn cap and the wall clock at about the same
138+ * point. A `delivery` card (none in the library yet) gets twice that; at the
139+ * same pace 600 turns outlast the default timeout, so its run raises
140+ * `--author-timeout` too, or the clock ends the turn first.
141+ */
142+ export const DEFAULT_AUTHOR_MAX_TURNS : Readonly < Record < ScenarioTier , number > > = { control : 80 , starter : 300 , delivery : 600 } ;
143+
144+ /** The agentic turns one question or operation turn may take unless `--ask-max-turns` says otherwise, every tier alike. */
145+ export const DEFAULT_ASK_MAX_TURNS = 12 ;
146+
124147export interface ClaudeCodeDriverOptions {
125148 /** The CLI binary; default `claude`. */
126149 bin ?: string ;
127- /** Agentic turns allowed for one question or operation; default 12 . */
128- maxTurnsAsk ?: number ;
129- /** Agentic turns allowed for one authoring task; default 80 . */
130- maxTurnsAuthor ?: number ;
150+ /** Agentic turns allowed for one question or operation; default { @link DEFAULT_ASK_MAX_TURNS} . */
151+ askMaxTurns ?: number ;
152+ /** Agentic turns allowed for one authoring task, every tier alike ; default by the card's tier, { @link DEFAULT_AUTHOR_MAX_TURNS} . */
153+ authorMaxTurns ?: number ;
131154 /** Wall-clock budget per authoring turn (build and iterate), ms; default {@link DEFAULT_AUTHOR_TIMEOUT_MIN}. */
132155 authorTimeoutMs ?: number ;
133156 /** Wall-clock budget per question or operation, ms; default 5 min. */
@@ -266,8 +289,24 @@ export class ClaudeCodeDriver implements AgentDriver {
266289 return this . opts . authorTimeoutMs ?? DEFAULT_AUTHOR_TIMEOUT_MIN * 60_000 ;
267290 }
268291
292+ private authorMaxTurns ( tier : ScenarioTier ) : number {
293+ return this . opts . authorMaxTurns ?? DEFAULT_AUTHOR_MAX_TURNS [ tier ] ;
294+ }
295+
296+ private get askMaxTurns ( ) : number {
297+ return this . opts . askMaxTurns ?? DEFAULT_ASK_MAX_TURNS ;
298+ }
299+
269300 info ( ) : DriverInfo {
270- return { driver : 'claude-code-cli' , version : this . version , authMethod : this . authMethod , apiBilled : false , authorTimeoutMs : this . authorTimeoutMs } ;
301+ return {
302+ driver : 'claude-code-cli' ,
303+ version : this . version ,
304+ authMethod : this . authMethod ,
305+ apiBilled : false ,
306+ authorTimeoutMs : this . authorTimeoutMs ,
307+ authorMaxTurns : Object . fromEntries ( SCENARIO_TIERS . map ( ( t ) => [ t , this . authorMaxTurns ( t ) ] ) ) as Record < ScenarioTier , number > ,
308+ askMaxTurns : this . askMaxTurns ,
309+ } ;
271310 }
272311
273312 private base ( maxTurns : number ) : string [ ] {
@@ -280,16 +319,17 @@ export class ClaudeCodeDriver implements AgentDriver {
280319 const empty = join ( mkdtempSync ( join ( tmpdir ( ) , 'scenario-mcp-' ) ) , 'none.json' ) ;
281320 writeFileSync ( empty , JSON . stringify ( { mcpServers : { } } ) ) ;
282321 try {
322+ const maxTurns = this . authorMaxTurns ( task . tier ) ;
283323 const args = [
284324 '-p' ,
285- ...this . base ( this . opts . maxTurnsAuthor ?? 80 ) ,
325+ ...this . base ( maxTurns ) ,
286326 '--mcp-config' , empty , '--strict-mcp-config' ,
287327 '--permission-mode' , 'acceptEdits' ,
288328 '--allowedTools' , 'Bash' , 'Read' , 'Edit' , 'Write' , 'Glob' , 'Grep' , 'Skill' ,
289329 ] ;
290330 const run = await runCli ( this . bin , args , task . cwd , task . prompt , this . authorTimeoutMs ) ;
291331 const turn = parseStreamJson ( run . stdout ) ;
292- turn . meta . process = run . process ;
332+ turn . meta . process = { ... run . process , maxTurns } ;
293333 if ( turn . meta . resultSubtype === undefined ) turn . meta . resultSubtype = noResult ( run . process ) ;
294334 return { turn, files : snapshotSources ( task . cwd ) } ;
295335 } finally {
@@ -308,10 +348,11 @@ export class ClaudeCodeDriver implements AgentDriver {
308348 { mode : 0o600 } ,
309349 ) ;
310350 try {
311- const args = [ '-p' , ...this . base ( this . opts . maxTurnsAsk ?? 12 ) , '--mcp-config' , cfg , '--strict-mcp-config' , '--allowedTools' , 'mcp__app' ] ;
351+ const maxTurns = this . askMaxTurns ;
352+ const args = [ '-p' , ...this . base ( maxTurns ) , '--mcp-config' , cfg , '--strict-mcp-config' , '--allowedTools' , 'mcp__app' ] ;
312353 const run = await runCli ( this . bin , args , dir , task . prompt , this . opts . askTimeoutMs ?? 5 * 60_000 ) ;
313354 const turn = parseStreamJson ( run . stdout ) ;
314- turn . meta . process = run . process ;
355+ turn . meta . process = { ... run . process , maxTurns } ;
315356 if ( turn . meta . resultSubtype === undefined ) turn . meta . resultSubtype = noResult ( run . process ) ;
316357 return turn ;
317358 } finally {
0 commit comments