(
cwd: string | undefined,
baseSessionState: SessionState,
overrides: {
projectFiles?: Record<string, string>
knowledgeFiles?: Record<string, string>
agentDefinitions?: AgentDefinition[]
customToolDefinitions?: CustomToolDefinition[]
maxAgentSteps?: number
},
)
| 688 | * even when continuing from a previous run. |
| 689 | */ |
| 690 | export async function applyOverridesToSessionState( |
| 691 | cwd: string | undefined, |
| 692 | baseSessionState: SessionState, |
| 693 | overrides: { |
| 694 | projectFiles?: Record<string, string> |
| 695 | knowledgeFiles?: Record<string, string> |
| 696 | agentDefinitions?: AgentDefinition[] |
| 697 | customToolDefinitions?: CustomToolDefinition[] |
| 698 | maxAgentSteps?: number |
| 699 | }, |
| 700 | ): Promise<SessionState> { |
| 701 | // Deep clone to avoid mutating the original session state |
| 702 | const sessionState = JSON.parse( |
| 703 | JSON.stringify(baseSessionState), |
| 704 | ) as SessionState |
| 705 | |
| 706 | // Apply maxAgentSteps override |
| 707 | if (overrides.maxAgentSteps !== undefined) { |
| 708 | sessionState.mainAgentState.stepsRemaining = overrides.maxAgentSteps |
| 709 | } |
| 710 | |
| 711 | // Apply projectFiles override (recomputes file tree and token scores) |
| 712 | if (overrides.projectFiles !== undefined) { |
| 713 | if (cwd) { |
| 714 | const projectIndex = getProjectIndexInput({ |
| 715 | cwd, |
| 716 | projectFiles: overrides.projectFiles, |
| 717 | }) |
| 718 | if (projectIndex) { |
| 719 | const { fileTree, fileTokenScores, tokenCallers } = |
| 720 | await computeProjectIndex(projectIndex) |
| 721 | sessionState.fileContext.fileTree = fileTree |
| 722 | sessionState.fileContext.fileTokenScores = fileTokenScores |
| 723 | sessionState.fileContext.tokenCallers = tokenCallers |
| 724 | } |
| 725 | } else { |
| 726 | // If projectFiles are provided but no cwd, reset file context fields |
| 727 | sessionState.fileContext.fileTree = [] |
| 728 | sessionState.fileContext.fileTokenScores = {} |
| 729 | sessionState.fileContext.tokenCallers = {} |
| 730 | } |
| 731 | |
| 732 | // Auto-derive knowledgeFiles if not explicitly provided |
| 733 | if (overrides.knowledgeFiles === undefined) { |
| 734 | sessionState.fileContext.knowledgeFiles = deriveKnowledgeFiles( |
| 735 | overrides.projectFiles, |
| 736 | ) |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // Apply knowledgeFiles override |
| 741 | if (overrides.knowledgeFiles !== undefined) { |
| 742 | sessionState.fileContext.knowledgeFiles = overrides.knowledgeFiles |
| 743 | } |
| 744 | |
| 745 | // Apply agentDefinitions override (merge by id, last-in wins) |
| 746 | if (overrides.agentDefinitions !== undefined) { |
| 747 | const processedAgentTemplates = processAgentDefinitions( |
no test coverage detected