@@ -84,16 +84,29 @@ export class SessionRunner {
8484 public lastContextTokens = 0;
8585 // Cache the system prompt so the file-system reads in buildSystemPrompt
8686 // (context files, skills index) are not repeated on every processUserInput call.
87- // Re-built only when extraSystemPrompt changes.
8887 private _extraSystemPrompt: string | undefined;
8988 private _systemPrompt: string | undefined;
9089
9190 public get extraSystemPrompt(): string | undefined {
9291 return this._extraSystemPrompt;
9392 }
93+
9494 public set extraSystemPrompt(value: string | undefined) {
9595 this._extraSystemPrompt = value;
96- this._systemPrompt = undefined; // invalidate cache
96+ this.rebuildSystemPrompt();
97+ }
98+
99+ private rebuildSystemPrompt(): void {
100+ if (!this.sessionTimeAnchor) {
101+ this._systemPrompt = undefined;
102+ return;
103+ }
104+ this._systemPrompt = buildSystemPrompt(
105+ this.sessionTimeAnchor,
106+ this.cwd,
107+ this._extraSystemPrompt,
108+ this.isSubagent,
109+ );
97110 }
98111
99112 private isSubagent: boolean | undefined;
@@ -112,9 +125,9 @@ export class SessionRunner {
112125 this.promptCachingEnabled = opts.initialPromptCachingEnabled;
113126 this.openaiPromptCacheRetention = opts.initialOpenAIPromptCacheRetention;
114127 this.googleCachedContent = opts.initialGoogleCachedContent;
115- this.extraSystemPrompt = opts.extraSystemPrompt;
116128 this.isSubagent = opts.isSubagent;
117129 this.killSubprocesses = opts.killSubprocesses;
130+ this.extraSystemPrompt = opts.extraSystemPrompt;
118131 this.initSession(opts.sessionId);
119132 }
120133
@@ -142,6 +155,7 @@ export class SessionRunner {
142155
143156 this.sessionTimeAnchor = new Date(this.session.createdAt).toISOString();
144157 this.coreHistory = [...this.session.messages];
158+ this.rebuildSystemPrompt();
145159 }
146160
147161 public startNewSession() {
@@ -153,7 +167,7 @@ export class SessionRunner {
153167 this.totalOut = 0;
154168 this.lastContextTokens = 0;
155169 this.sessionTimeAnchor = new Date(this.session.createdAt).toISOString();
156- this._systemPrompt = undefined; // invalidate cached prompt (new session anchor)
170+ this.rebuildSystemPrompt();
157171 this.snapshotStack.length = 0;
158172 }
159173
@@ -199,15 +213,8 @@ export class SessionRunner {
199213 this.coreHistory.push(userMsg);
200214
201215 const llm = resolveModel(this.currentModel);
202- // Use cached system prompt; rebuild only on first use or after
203- // extraSystemPrompt / sessionTimeAnchor change.
204216 if (!this._systemPrompt) {
205- this._systemPrompt = buildSystemPrompt(
206- this.sessionTimeAnchor,
207- this.cwd,
208- this._extraSystemPrompt,
209- this.isSubagent,
210- );
217+ this.rebuildSystemPrompt();
211218 }
212219 const systemPrompt = this._systemPrompt;
213220
@@ -223,7 +230,7 @@ export class SessionRunner {
223230 tools: this.planMode
224231 ? [...buildReadOnlyToolSet({ cwd: this.cwd }), ...this.mcpTools]
225232 : this.tools,
226- systemPrompt,
233+ ...(systemPrompt ? { systemPrompt } : {}),
227234 signal: abortController.signal,
228235 pruningMode: this.pruningMode,
229236 toolResultPayloadCapBytes: this.toolResultPayloadCapBytes,