feat(prompt): implement prompt injection slice 4

This commit is contained in:
2026-06-11 22:54:51 +02:00
parent 19666c900e
commit 58e8bd31d3
5 changed files with 965 additions and 18 deletions
+423
View File
@@ -10,6 +10,8 @@ import {
modeAllowsPreInitHint,
modeAllowsInjection,
modeRequiresProtocolPath,
hasProtocolPath,
buildAdvisoryReminder,
discoverContextWindow,
estimateTokens,
findAllArtifactPairs,
@@ -20,9 +22,15 @@ import {
isRelevantTurnForReinjection,
detectEditIntent,
detectArchitectureSensitiveReasoning,
isSensitiveAction,
messagesHaveBypass,
extractBypassReason,
evaluateStrictBypass,
buildStrictBypassGuard,
ROOT_PAIR_START_MARKER,
ROOT_PAIR_END_MARKER,
TRUST_BOUNDARY_TEXT,
BYPASS_MARKER_PREFIX,
} from "../src/prompt-injection.js";
describe("prompt-injection helpers", () => {
@@ -708,6 +716,73 @@ describe("prompt-injection helpers", () => {
});
});
describe("protocol path detection", () => {
it("detects protocol path when messages contain marker and trust boundary", () => {
const messages = [
{
content: `Start ${ROOT_PAIR_START_MARKER} ${TRUST_BOUNDARY_TEXT} end`,
},
];
expect(hasProtocolPath(messages)).toBe(true);
});
it("detects protocol path when payload contains marker and trust boundary", () => {
const payload = {
body: `${ROOT_PAIR_START_MARKER}\n${TRUST_BOUNDARY_TEXT}`,
};
expect(hasProtocolPath(undefined, payload)).toBe(true);
});
it("returns false when only marker is present", () => {
const messages = [{ content: `hello ${ROOT_PAIR_START_MARKER} world` }];
expect(hasProtocolPath(messages)).toBe(false);
});
it("returns false when only trust boundary is present", () => {
const messages = [{ content: `hello ${TRUST_BOUNDARY_TEXT} world` }];
expect(hasProtocolPath(messages)).toBe(false);
});
it("returns false when neither marker nor trust boundary is present", () => {
const messages = [{ content: "plain text" }];
expect(hasProtocolPath(messages)).toBe(false);
});
it("returns false for empty messages and undefined payload", () => {
expect(hasProtocolPath([])).toBe(false);
expect(hasProtocolPath(undefined, undefined)).toBe(false);
});
it("handles array content blocks", () => {
const messages = [
{
content: [
{ type: "text", text: ROOT_PAIR_START_MARKER },
{ type: "text", text: TRUST_BOUNDARY_TEXT },
],
},
];
expect(hasProtocolPath(messages)).toBe(true);
});
it("requires both elements in the same combined source", () => {
const messages = [{ content: ROOT_PAIR_START_MARKER }];
const payload = { body: TRUST_BOUNDARY_TEXT };
expect(hasProtocolPath(messages, payload)).toBe(true);
});
});
describe("advisory reminder", () => {
it("returns a visible lightweight reminder with no synthetic artifacts", () => {
const reminder = buildAdvisoryReminder();
expect(reminder).toContain("advisory mode active");
expect(reminder).toContain("📋");
expect(reminder).not.toContain(ROOT_PAIR_START_MARKER);
expect(reminder).not.toContain("### Root index");
expect(reminder).not.toContain("### Root map");
});
});
describe("injection payload", () => {
it("always includes the root pair", () => {
const dir = mkdtempSync(join(tmpdir(), "pi-payload-test-"));
@@ -769,4 +844,352 @@ describe("prompt-injection helpers", () => {
expect(payload.content).not.toContain("# src map x");
});
});
describe("sensitive action detection", () => {
it("detects explicit edit_intent event type", () => {
expect(isSensitiveAction("edit_intent", [])).toBe(true);
});
it("detects explicit architecture_sensitive event type", () => {
expect(isSensitiveAction("architecture_sensitive", [])).toBe(true);
});
it("falls back to edit-intent heuristic for generic events", () => {
expect(
isSensitiveAction("user_chat", [{ content: "edit the file" }]),
).toBe(true);
});
it("falls back to architecture heuristic for generic events", () => {
expect(
isSensitiveAction("user_chat", [
{ content: "this is an architecture decision" },
]),
).toBe(true);
});
it("treats compaction as non-sensitive", () => {
expect(isSensitiveAction("compaction", [{ content: "compacting" }])).toBe(
false,
);
});
it("treats agent_start as non-sensitive", () => {
expect(isSensitiveAction("agent_start", [{ content: "hi" }])).toBe(false);
});
it("returns false for irrelevant messages without event type", () => {
expect(
isSensitiveAction(undefined, [{ content: "what is the weather?" }]),
).toBe(false);
});
it("returns false when no event type or messages are provided", () => {
expect(isSensitiveAction(undefined, undefined)).toBe(false);
});
it("handles array content blocks", () => {
expect(
isSensitiveAction(undefined, [
{
content: [
{ type: "text", text: "please " },
{ type: "text", text: "refactor the module" },
],
},
]),
).toBe(true);
});
it("detects sensitive action from payload when messages are empty", () => {
expect(isSensitiveAction("user_chat", [], "edit the file")).toBe(true);
});
it("detects architecture-sensitive action from payload", () => {
expect(isSensitiveAction("user_chat", [], "architecture decision")).toBe(
true,
);
});
it("returns false for irrelevant payload without messages or event type", () => {
expect(
isSensitiveAction(undefined, undefined, "what is the weather?"),
).toBe(false);
});
it("combines messages and payload for heuristic detection", () => {
expect(
isSensitiveAction(
"user_chat",
[{ content: "please consider" }],
"the architecture decision",
),
).toBe(true);
});
});
describe("bypass marker parsing", () => {
it("detects a valid bypass marker in messages", () => {
expect(
messagesHaveBypass([{ content: "[PI_MAP_BYPASS: temporary fix]" }]),
).toBe(true);
});
it("detects a valid bypass marker prefix", () => {
expect(BYPASS_MARKER_PREFIX).toBe("[PI_MAP_BYPASS:");
});
it("returns false when no marker is present", () => {
expect(messagesHaveBypass([{ content: "plain text" }])).toBe(false);
});
it("extracts the bypass reason", () => {
expect(
extractBypassReason([{ content: "[PI_MAP_BYPASS: temporary fix]" }]),
).toBe("temporary fix");
});
it("returns undefined when marker is absent", () => {
expect(extractBypassReason([{ content: "plain text" }])).toBeUndefined();
});
it("rejects an empty bypass reason", () => {
expect(
extractBypassReason([{ content: "[PI_MAP_BYPASS:]" }]),
).toBeUndefined();
});
it("rejects a whitespace-only bypass reason", () => {
expect(
extractBypassReason([{ content: "[PI_MAP_BYPASS: ]" }]),
).toBeUndefined();
});
it("extracts the first reason when multiple markers are present", () => {
expect(
extractBypassReason([
{
content: "[PI_MAP_BYPASS: first] [PI_MAP_BYPASS: second]",
},
]),
).toBe("first");
});
it("handles array content blocks", () => {
expect(
extractBypassReason([
{
content: [
{ type: "text", text: "[PI_MAP_BYPASS: " },
{ type: "text", text: "array reason]" },
],
},
]),
).toBe("array reason");
});
it("detects bypass marker in payload string", () => {
expect(
messagesHaveBypass(undefined, "[PI_MAP_BYPASS: payload reason]"),
).toBe(true);
});
it("detects bypass marker in payload object", () => {
expect(
messagesHaveBypass(undefined, {
body: "[PI_MAP_BYPASS: object reason]",
}),
).toBe(true);
});
it("extracts reason from payload when messages have none", () => {
expect(
extractBypassReason(undefined, "[PI_MAP_BYPASS: payload reason]"),
).toBe("payload reason");
});
it("prefers first marker across messages and payload", () => {
expect(
extractBypassReason(
[{ content: "[PI_MAP_BYPASS: msg reason]" }],
"[PI_MAP_BYPASS: payload reason]",
),
).toBe("msg reason");
});
it("extracts reason with colon inside", () => {
expect(
extractBypassReason([{ content: "[PI_MAP_BYPASS: fix: edge case]" }]),
).toBe("fix: edge case");
});
it("rejects bypass marker with only newlines/spaces", () => {
expect(
extractBypassReason([{ content: "[PI_MAP_BYPASS:\n\t ]" }]),
).toBeUndefined();
});
});
describe("strict bypass evaluation", () => {
it("never guards in non-strict modes", () => {
for (const mode of ["off", "advisory", "strong"] as const) {
const decision = evaluateStrictBypass(
{ messages: [{ content: "edit the file" }], type: "edit_intent" },
mode,
);
expect(decision.guard).toBe(false);
}
});
it("does not guard non-sensitive turns in strict mode", () => {
const decision = evaluateStrictBypass(
{ messages: [{ content: "hi" }], type: "user_chat" },
"strict",
);
expect(decision.guard).toBe(false);
});
it("guards sensitive turns when protocol path is missing", () => {
const decision = evaluateStrictBypass(
{ messages: [{ content: "edit the file" }], type: "edit_intent" },
"strict",
);
expect(decision.guard).toBe(true);
expect(decision.reason).toContain("protocol path missing");
});
it("does not guard sensitive turns when protocol path is present", () => {
const decision = evaluateStrictBypass(
{
messages: [
{
content: `${ROOT_PAIR_START_MARKER} ${TRUST_BOUNDARY_TEXT}`,
},
],
type: "edit_intent",
},
"strict",
);
expect(decision.guard).toBe(false);
});
it("uses heuristic fallback for generic sensitive events", () => {
const decision = evaluateStrictBypass(
{
messages: [{ content: "refactor the module" }],
type: "user_chat",
},
"strict",
);
expect(decision.guard).toBe(true);
});
it("clears the guard when a valid inline bypass marker is present", () => {
const decision = evaluateStrictBypass(
{
messages: [
{
content: "[PI_MAP_BYPASS: emergency patch] edit the file",
},
],
type: "edit_intent",
},
"strict",
);
expect(decision.guard).toBe(false);
expect(decision.bypassMarker).toBe("emergency patch");
});
it("does not clear the guard for an empty bypass marker", () => {
const decision = evaluateStrictBypass(
{
messages: [{ content: "[PI_MAP_BYPASS:] edit the file" }],
type: "edit_intent",
},
"strict",
);
expect(decision.guard).toBe(true);
});
it("does not guard when event is null or undefined", () => {
expect(evaluateStrictBypass(undefined, "strict").guard).toBe(false);
expect(evaluateStrictBypass(null, "strict").guard).toBe(false);
});
it("does not guard when payload-only protocol path is present", () => {
const decision = evaluateStrictBypass(
{
messages: [{ content: "edit the file" }],
payload: `${ROOT_PAIR_START_MARKER}\n${TRUST_BOUNDARY_TEXT}`,
type: "edit_intent",
},
"strict",
);
expect(decision.guard).toBe(false);
});
it("clears guard when bypass marker is only in payload", () => {
const decision = evaluateStrictBypass(
{
messages: [{ content: "edit the file" }],
payload: "[PI_MAP_BYPASS: emergency patch]",
type: "edit_intent",
},
"strict",
);
expect(decision.guard).toBe(false);
expect(decision.bypassMarker).toBe("emergency patch");
});
it("guards when payload-only bypass marker has empty reason", () => {
const decision = evaluateStrictBypass(
{
messages: [{ content: "edit the file" }],
payload: "[PI_MAP_BYPASS:]",
type: "edit_intent",
},
"strict",
);
expect(decision.guard).toBe(true);
});
it("uses heuristic fallback for payload-only sensitive events", () => {
const decision = evaluateStrictBypass(
{
messages: [],
payload: "refactor the module",
type: "user_chat",
},
"strict",
);
expect(decision.guard).toBe(true);
});
});
describe("strict bypass guard message", () => {
it("includes the provided reason", () => {
const guard = buildStrictBypassGuard("custom reason");
expect(guard).toContain("custom reason");
expect(guard).toContain("🛑");
});
it("includes a default reason when none is provided", () => {
const guard = buildStrictBypassGuard();
expect(guard).toContain("sensitive action");
expect(guard).toContain("protocol path");
});
it("tells the user how to proceed", () => {
const guard = buildStrictBypassGuard();
expect(guard).toContain("PI_MAP_BYPASS");
expect(guard).toContain("restore the project-map context");
});
it("does not contain synthetic root-pair content", () => {
const guard = buildStrictBypassGuard();
expect(guard).not.toContain(ROOT_PAIR_START_MARKER);
expect(guard).not.toContain("### Root index");
expect(guard).not.toContain("### Root map");
});
});
});