feat: avoid duplicate project-map hint injection by checking context

The before_agent_start handler now inspects the current session context
and skips injection if a pi-project-map-hint custom message is already
present in the active branch. This prevents duplicate hints on every
prompt while still re-injecting after compaction or tree navigation.

Also removes the unused hooks/on-prompt.ts prompt-text injector.
This commit is contained in:
Developer
2026-06-14 08:53:26 +00:00
parent 5c6719817f
commit 6bc7be4c21
4 changed files with 68 additions and 16 deletions
+42
View File
@@ -48,6 +48,9 @@ describe("pi-extension", () => {
getApiKeyAndHeaders: vi.fn(async () => ({ apiKey: "test-key", headers: {} })),
},
ui: { notify: mockNotify },
sessionManager: {
buildSessionContext: vi.fn(() => ({ messages: [] })),
},
};
const mockPi = {
@@ -225,5 +228,44 @@ describe("pi-extension", () => {
expect(result).toEqual({});
});
it("does not inject hint when it is already in context", async () => {
const dir = mkdtempSync(join(tmpdir(), "pi-ext-test-"));
writeFileSync(join(dir, ".pi-map.md"), "# .\n## role\nTest\n");
mockCtx.cwd = dir;
mockCtx.sessionManager.buildSessionContext = vi.fn(() => ({
messages: [
{
role: "custom",
customType: "pi-project-map-hint",
content: "existing hint",
display: false,
},
],
}));
const handler = registeredEvents.before_agent_start;
const result = await handler(null, mockCtx);
expect(result).toEqual({});
});
it("injects hint again when previous context did not contain it", async () => {
const dir = mkdtempSync(join(tmpdir(), "pi-ext-test-"));
writeFileSync(join(dir, ".pi-map.md"), "# .\n## role\nTest\n");
mockCtx.cwd = dir;
mockCtx.sessionManager.buildSessionContext = vi.fn(() => ({
messages: [
{ role: "user", content: "hello" },
{ role: "assistant", content: [{ type: "text", text: "hi" }] },
],
}));
const handler = registeredEvents.before_agent_start;
const result = await handler(null, mockCtx);
expect(result).toHaveProperty("message");
expect(result.message.customType).toBe("pi-project-map-hint");
});
});
});