fix(hooks): dedupe native agent instructions

This commit is contained in:
YeonGyu-Kim
2026-05-11 17:21:58 +09:00
parent c849d0cbbc
commit 78d52872f2
2 changed files with 201 additions and 3 deletions
@@ -173,6 +173,71 @@ describe("processFilePathForAgentsInjection", () => {
expect(output.output.split("[Directory Context:").length - 1).toBe(2)
})
it("dedupes native global Instructions from blocks across reads", async () => {
// given
const { processFilePathForAgentsInjection } = await import("./injector")
const sessionCaches = new Map<string, Set<string>>()
const filePath = join(testRoot, "file.ts")
const globalAgentsPath = "/Users/example/.config/opencode/AGENTS.md"
const globalAgentsContent = "# GLOBAL AGENTS\nglobal directives"
const nativeOutput = () => ({
title: "Read result",
output: `base output\n\nAdditional project instructions matched for ${filePath}:\n\nInstructions from: ${globalAgentsPath}\n${globalAgentsContent}`,
metadata: {},
})
const firstOutput = nativeOutput()
const secondOutput = nativeOutput()
// when
await processFilePathForAgentsInjection({
ctx: { directory: testRoot } as PluginInput,
truncator,
sessionCaches,
filePath,
sessionID: "session-native-global-dedupe",
output: firstOutput,
})
await processFilePathForAgentsInjection({
ctx: { directory: testRoot } as PluginInput,
truncator,
sessionCaches,
filePath,
sessionID: "session-native-global-dedupe",
output: secondOutput,
})
// then
expect(firstOutput.output).toContain(`Instructions from: ${globalAgentsPath}`)
expect(secondOutput.output).toBe("base output")
})
it("does not add Directory Context when native output already included the same AGENTS.md", async () => {
// given
const { processFilePathForAgentsInjection } = await import("./injector")
const filePath = join(srcDirectory, "file.ts")
const srcAgentsPath = join(srcDirectory, "AGENTS.md")
const output = {
title: "Read result",
output: `base output\n\nAdditional project instructions matched for ${filePath}:\n\nInstructions from: ${srcAgentsPath}\n${srcAgentsContent}`,
metadata: {},
}
// when
await processFilePathForAgentsInjection({
ctx: { directory: testRoot } as PluginInput,
truncator,
sessionCaches: new Map(),
filePath,
sessionID: "session-native-local-dedupe",
output,
})
// then
expect(output.output).toContain(`Instructions from: ${srcAgentsPath}`)
expect(output.output).not.toContain(`[Directory Context: ${srcAgentsPath}]`)
expect(output.output.split(srcAgentsContent).length - 1).toBe(1)
})
it("shows truncation notice when content is truncated", async () => {
// given
const { processFilePathForAgentsInjection } = await import("./injector")