1880
*/
1881
private async _materializeProvisional(sessionId: string, resolvedWorkingDirectory?: URI): Promise<CopilotAgentSession> {
1882
>
const provisional = this._provisionalSessions.get(sessionId);
copilotAgent.ts
1883
>
if (!provisional) {
1884
throw new Error(`Cannot materialize unknown provisional session: ${sessionId}`);
1885
}
1887
>
const sessionUri = provisional.sessionUri;
1888
>
1889
>
// The host hands us the resolved working directory (an isolated worktree for
1890
>
// worktree isolation) on the first send; use it so the SDK subprocess spawns
1891
>
// in the worktree. Falls back to the folder / scratch dir captured at create
1892
>
// time for folder / workspace-less sessions.
1893
>
const workingDirectory = resolvedWorkingDirectory ?? provisional.workingDirectory;
1894
>
// The customization anchor follows the working directory: once a worktree
1895
>
// is created the agent must discover skills/instructions/agents from the
1896
>
// worktree (not the user-picked folder) so the model reads and edits files
1897
>
// in the worktree it actually runs in.
1898
>
const customizationDirectory = workingDirectory ?? provisional.workingDirectory;
1899
>
// Always create an ActiveClient so the snapshot includes host +
1900
>
// session-discovered customizations, even when no client has
1901
>
// registered an active-client handle yet.
1902
>
const activeClient = this._getOrCreateActiveClient(sessionUri, customizationDirectory);
1903
>
// Re-anchor in case the provisional active client was already bound to the
1904
>
// user-picked folder before the worktree existed.
1905
>
activeClient.pluginController.reanchor(customizationDirectory);
1906
>
const snapshot = await activeClient.snapshot();
1907
>
const shellManager = this._instantiationService.createInstance(ShellManager, sessionUri, workingDirectory);
1908
>
1909
>
let agentSession: CopilotAgentSession | undefined;
1910
>
let agent: AgentSelection | undefined;
1911
>
try {
1912
>
const resolvedAgent = await this._resolveAgentWhenMaterializing(provisional, snapshot, workingDirectory);
1913
>
agent = resolvedAgent?.agent;
1914
>
const launchPlan: CopilotSessionLaunchPlan = {
1915
>
kind: 'create',
1916
>
client,
1917
>
sessionId,
1918
>
workingDirectory,
1919
>
resolvedAgentName: resolvedAgent?.name,
1920
>
snapshot,
1921
>
activeClientToolSet: activeClient.toolSet,
1922
>
shellManager,
1923
>
githubToken: this._githubToken,
1924
>
model: provisional.model,
1925
>
longContextWindow: this._longContextWindowFor(provisional.model?.id),
1926
>
freeLongContext: this._isFreeLongContext(provisional.model?.id),
1927
>
workspaceless: provisional.workspaceless,
1928
>
};
1929
>
agentSession = this._createAgentSession(launchPlan, customizationDirectory, activeClient);
1930
>
await agentSession.initializeSession();
1931
this._registerInitializedSession(sessionId, agentSession);
1933
agentSession?.dispose();
1934
throw error;
1935
}
1936
1937
>
const project = await projectFromCopilotContext({ cwd: workingDirectory?.fsPath }, this._gitService);
copilotAgent.ts
1938
1939
this._provisionalSessions.delete(sessionId);