fix(coding-agent): refresh advisor context prompt on reload
Rebuilt advisor runtimes with the rediscovered context files so advisor turns stop evaluating against stale AGENTS.md instructions after /reload-plugins. Fixes #7258
This commit is contained in:
@@ -2792,6 +2792,7 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
||||
...(settings.get("disabledExtensions") ?? []),
|
||||
]);
|
||||
toolSession.contextFiles = contextFiles;
|
||||
session.setAdvisorContextPrompt(formatAdvisorContextPrompt(contextFiles));
|
||||
}
|
||||
const memoryBackend = restrictToolNames ? undefined : await resolveMemoryBackend(settings);
|
||||
const memoryInstructions = memoryBackend
|
||||
|
||||
@@ -8719,6 +8719,15 @@ export class AgentSession {
|
||||
return this.#advisors.applyAdvisorConfigs(advisors, sharedInstructions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the project context prompt advisor sessions run against after
|
||||
* context files change on `/reload-plugins`. Rebuilds live advisor runtimes so
|
||||
* they stop evaluating turns against stale `AGENTS.md` instructions.
|
||||
*/
|
||||
setAdvisorContextPrompt(contextPrompt: string | undefined): void {
|
||||
this.#advisors.setContextPrompt(contextPrompt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the advisor setting is enabled for this session.
|
||||
*/
|
||||
|
||||
@@ -1579,6 +1579,20 @@ export class SessionAdvisors {
|
||||
return this.#advisors.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap the project context prompt handed to advisor sessions after context
|
||||
* files change (`/reload-plugins` edit/disable). Rebuilds live runtimes in
|
||||
* place so the next advisor turn evaluates against the current instructions;
|
||||
* a no-op when the rendered prompt is unchanged.
|
||||
*/
|
||||
setContextPrompt(contextPrompt: string | undefined): void {
|
||||
if (contextPrompt === this.#advisorContextPrompt) return;
|
||||
this.#advisorContextPrompt = contextPrompt;
|
||||
if (!this.#advisorEnabled || this.#advisors.length === 0) return;
|
||||
this.#stopAdvisorRuntime();
|
||||
this.#buildAdvisorRuntime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the advisor setting is enabled for this session.
|
||||
*/
|
||||
|
||||
@@ -16,8 +16,15 @@ const PROJECT_CONTEXT_EXTENSION_ID = "context-file:project:AGENTS.md";
|
||||
async function createContextSession(
|
||||
cwd: string,
|
||||
settings: Settings,
|
||||
options: { advisor?: boolean } = {},
|
||||
): Promise<{ session: AgentSession; authStorage: AuthStorage }> {
|
||||
const authStorage = await AuthStorage.create(`${cwd}/auth.db`);
|
||||
const model = getBundledModel("openai", "gpt-4o-mini");
|
||||
if (options.advisor) {
|
||||
authStorage.setRuntimeApiKey("openai", "test-key");
|
||||
settings.set("advisor.enabled", true);
|
||||
settings.setModelRole("advisor", `${model.provider}/${model.id}`);
|
||||
}
|
||||
const modelRegistry = new ModelRegistry(authStorage);
|
||||
const { session } = await createAgentSession({
|
||||
cwd,
|
||||
@@ -25,7 +32,7 @@ async function createContextSession(
|
||||
modelRegistry,
|
||||
sessionManager: SessionManager.inMemory(cwd),
|
||||
settings,
|
||||
model: getBundledModel("openai", "gpt-4o-mini"),
|
||||
model,
|
||||
disableExtensionDiscovery: true,
|
||||
promptTemplates: [],
|
||||
slashCommands: [],
|
||||
@@ -99,4 +106,29 @@ describe("context-file prompt refresh", () => {
|
||||
authStorage.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("refreshes the advisor context prompt when context files change", async () => {
|
||||
using tempDir = TempDir.createSync("@omp-context-refresh-advisor-");
|
||||
const contextPath = tempDir.join("AGENTS.md");
|
||||
await Bun.write(contextPath, INITIAL_CONTEXT);
|
||||
const { session, authStorage } = await createContextSession(tempDir.path(), Settings.isolated({}), {
|
||||
advisor: true,
|
||||
});
|
||||
|
||||
try {
|
||||
const advisorPrompt = () => session.getAdvisorAgent()?.state.systemPrompt.join("\n") ?? "";
|
||||
expect(session.isAdvisorActive()).toBe(true);
|
||||
expect(advisorPrompt()).toContain(INITIAL_CONTEXT);
|
||||
|
||||
await Bun.write(contextPath, UPDATED_CONTEXT);
|
||||
await session.refreshSkills();
|
||||
|
||||
const refreshed = advisorPrompt();
|
||||
expect(refreshed).toContain(UPDATED_CONTEXT);
|
||||
expect(refreshed).not.toContain(INITIAL_CONTEXT);
|
||||
} finally {
|
||||
await session.dispose();
|
||||
authStorage.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user