Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]

### Added

- Strip residual `<dcp-message-id>` and `<dcp-system-reminder>` fragments and bare `m####` refs that some models (notably `MiniMax-M3`) leak into assistant output. Notify the user when a turn produces no tool call but still contains residual metadata, so silent stops become visible.

## [0.5.0] - 2026-08-01

### Added
Expand Down
357 changes: 179 additions & 178 deletions docs/superpowers/plans/2026-08-16-strip-residual-metadata.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
},
"devDependencies": {
"@biomejs/biome": "^2.5.6",
"@earendil-works/pi-agent-core": "^0.83.0",
"@earendil-works/pi-coding-agent": "^0.83.0",
"@earendil-works/pi-agent-core": "^0.84.2",
"@earendil-works/pi-coding-agent": "^0.84.2",
"@types/node": "^26.1.1",
"tsx": "^4.23.1",
"typescript": "^6.0.3",
Expand Down
138 changes: 64 additions & 74 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 37 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,49 @@ export default function createExtension(pi: ExtensionAPI): void {
logger.info("dcp", "session shutdown");
});

pi.on("message_end", async (event, _ctx) => {
pi.on("message_end", async (event, ctx) => {
if (!config.enabled) return;
if (event.message.role !== "assistant") return;

const stripped = mapText(event.message, stripHallucinationsFromString);
const knownRefs = new Set(state.messageIds.byRawId.values());
const stripped = mapText(event.message, (t) => stripHallucinationsFromString(t, knownRefs));

if (stripped !== event.message) {
if (ctx.hasUI) {
ctx.ui.notify("dcp: stripped residual metadata from model output", "info");
}
return { message: stripped };
}

// Defense-in-depth: if stripping returned the message unchanged but the
// visible text still carries a dcp-* shape AND the turn produced no
// tool call, the regex set missed a case. Notify so silent stops become
// visible. Per docs/06: notify-only, not provider fail-closed.
const msg = event.message;
if (msg.role === "assistant" && msg.stopReason === "stop" && ctx.hasUI) {
const content = msg.content;
const hasToolCall =
Array.isArray(content) &&
content.some(
(p) =>
typeof p === "object" && p !== null && (p as { type?: string }).type === "toolCall",
);
const text = Array.isArray(content)
? content
.filter(
(p): p is { type: "text"; text: string } =>
typeof p === "object" && p !== null && (p as { type?: unknown }).type === "text",
)
.map((p) => p.text)
.join("\n")
: "";
if (!hasToolCall && /-?dcp-(message-id|system-reminder)/.test(text)) {
ctx.ui.notify(
"dcp: model output looked malformed (no tool call, residual metadata present). Try re-prompting.",
"warning",
);
}
}
});

pi.on("tool_call", async (event, _ctx) => {
Expand Down
Loading