@tarquinen/opencode-dcp 3.2.4-beta0 → 3.2.6-beta0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dcp.schema.json +329 -0
  2. package/dist/lib/config.js +2 -2
  3. package/dist/lib/config.js.map +1 -1
  4. package/index.ts +141 -0
  5. package/lib/auth.ts +37 -0
  6. package/lib/commands/compression-targets.ts +137 -0
  7. package/lib/commands/context.ts +132 -0
  8. package/lib/commands/decompress.ts +275 -0
  9. package/lib/commands/help.ts +76 -0
  10. package/lib/commands/index.ts +11 -0
  11. package/lib/commands/manual.ts +125 -0
  12. package/lib/commands/recompress.ts +224 -0
  13. package/lib/commands/stats.ts +148 -0
  14. package/lib/commands/sweep.ts +268 -0
  15. package/lib/compress-permission.ts +25 -0
  16. package/lib/config.ts +2 -2
  17. package/lib/hooks.ts +378 -0
  18. package/lib/host-permissions.ts +101 -0
  19. package/lib/messages/index.ts +8 -0
  20. package/lib/messages/inject/inject.ts +215 -0
  21. package/lib/messages/inject/subagent-results.ts +82 -0
  22. package/lib/messages/inject/utils.ts +374 -0
  23. package/lib/messages/priority.ts +102 -0
  24. package/lib/messages/prune.ts +238 -0
  25. package/lib/messages/reasoning-strip.ts +40 -0
  26. package/lib/messages/sync.ts +124 -0
  27. package/lib/messages/utils.ts +187 -0
  28. package/lib/prompts/compress-message.ts +42 -0
  29. package/lib/prompts/compress-range.ts +60 -0
  30. package/lib/prompts/context-limit-nudge.ts +18 -0
  31. package/lib/prompts/extensions/nudge.ts +43 -0
  32. package/lib/prompts/extensions/system.ts +32 -0
  33. package/lib/prompts/extensions/tool.ts +35 -0
  34. package/lib/prompts/index.ts +29 -0
  35. package/lib/prompts/iteration-nudge.ts +6 -0
  36. package/lib/prompts/store.ts +467 -0
  37. package/lib/prompts/system.ts +33 -0
  38. package/lib/prompts/turn-nudge.ts +10 -0
  39. package/lib/protected-patterns.ts +128 -0
  40. package/lib/strategies/deduplication.ts +127 -0
  41. package/lib/strategies/index.ts +2 -0
  42. package/lib/strategies/purge-errors.ts +88 -0
  43. package/lib/subagents/subagent-results.ts +74 -0
  44. package/lib/ui/notification.ts +346 -0
  45. package/lib/ui/utils.ts +287 -0
  46. package/package.json +14 -19
@@ -0,0 +1,40 @@
1
+ import type { WithParts } from "../state"
2
+ import { getLastUserMessage } from "./query"
3
+
4
+ /**
5
+ * Mirrors opencode's differentModel handling by preserving part content while
6
+ * dropping provider metadata on assistant parts that came from a different
7
+ * model/provider than the current turn's user message.
8
+ */
9
+ export function stripStaleMetadata(messages: WithParts[]): void {
10
+ const lastUserMessage = getLastUserMessage(messages)
11
+ if (lastUserMessage?.info.role !== "user") {
12
+ return
13
+ }
14
+
15
+ const modelID = lastUserMessage.info.model.modelID
16
+ const providerID = lastUserMessage.info.model.providerID
17
+
18
+ messages.forEach((message) => {
19
+ if (message.info.role !== "assistant") {
20
+ return
21
+ }
22
+
23
+ if (message.info.modelID === modelID && message.info.providerID === providerID) {
24
+ return
25
+ }
26
+
27
+ message.parts = message.parts.map((part) => {
28
+ if (part.type !== "text" && part.type !== "tool" && part.type !== "reasoning") {
29
+ return part
30
+ }
31
+
32
+ if (!("metadata" in part)) {
33
+ return part
34
+ }
35
+
36
+ const { metadata: _metadata, ...rest } = part
37
+ return rest
38
+ })
39
+ })
40
+ }
@@ -0,0 +1,124 @@
1
+ import type { SessionState, WithParts } from "../state"
2
+ import type { Logger } from "../logger"
3
+
4
+ function sortBlocksByCreation(
5
+ a: { createdAt: number; blockId: number },
6
+ b: { createdAt: number; blockId: number },
7
+ ): number {
8
+ const createdAtDiff = a.createdAt - b.createdAt
9
+ if (createdAtDiff !== 0) {
10
+ return createdAtDiff
11
+ }
12
+ return a.blockId - b.blockId
13
+ }
14
+
15
+ export const syncCompressionBlocks = (
16
+ state: SessionState,
17
+ logger: Logger,
18
+ messages: WithParts[],
19
+ ): void => {
20
+ const messagesState = state.prune.messages
21
+ if (!messagesState?.blocksById?.size) {
22
+ return
23
+ }
24
+
25
+ const messageIds = new Set(messages.map((msg) => msg.info.id))
26
+ const previousActiveBlockIds = new Set<number>(
27
+ Array.from(messagesState.blocksById.values())
28
+ .filter((block) => block.active)
29
+ .map((block) => block.blockId),
30
+ )
31
+
32
+ messagesState.activeBlockIds.clear()
33
+ messagesState.activeByAnchorMessageId.clear()
34
+
35
+ const now = Date.now()
36
+ const missingOriginBlockIds: number[] = []
37
+ const orderedBlocks = Array.from(messagesState.blocksById.values()).sort(sortBlocksByCreation)
38
+
39
+ for (const block of orderedBlocks) {
40
+ const hasOriginMessage =
41
+ typeof block.compressMessageId === "string" &&
42
+ block.compressMessageId.length > 0 &&
43
+ messageIds.has(block.compressMessageId)
44
+
45
+ if (!hasOriginMessage) {
46
+ block.active = false
47
+ block.deactivatedAt = now
48
+ block.deactivatedByBlockId = undefined
49
+ missingOriginBlockIds.push(block.blockId)
50
+ continue
51
+ }
52
+
53
+ if (block.deactivatedByUser) {
54
+ block.active = false
55
+ if (block.deactivatedAt === undefined) {
56
+ block.deactivatedAt = now
57
+ }
58
+ block.deactivatedByBlockId = undefined
59
+ continue
60
+ }
61
+
62
+ for (const consumedBlockId of block.consumedBlockIds) {
63
+ if (!messagesState.activeBlockIds.has(consumedBlockId)) {
64
+ continue
65
+ }
66
+
67
+ const consumedBlock = messagesState.blocksById.get(consumedBlockId)
68
+ if (consumedBlock) {
69
+ consumedBlock.active = false
70
+ consumedBlock.deactivatedAt = now
71
+ consumedBlock.deactivatedByBlockId = block.blockId
72
+
73
+ const mappedBlockId = messagesState.activeByAnchorMessageId.get(
74
+ consumedBlock.anchorMessageId,
75
+ )
76
+ if (mappedBlockId === consumedBlock.blockId) {
77
+ messagesState.activeByAnchorMessageId.delete(consumedBlock.anchorMessageId)
78
+ }
79
+ }
80
+
81
+ messagesState.activeBlockIds.delete(consumedBlockId)
82
+ }
83
+
84
+ block.active = true
85
+ block.deactivatedAt = undefined
86
+ block.deactivatedByBlockId = undefined
87
+ messagesState.activeBlockIds.add(block.blockId)
88
+ if (messageIds.has(block.anchorMessageId)) {
89
+ messagesState.activeByAnchorMessageId.set(block.anchorMessageId, block.blockId)
90
+ }
91
+ }
92
+
93
+ for (const entry of messagesState.byMessageId.values()) {
94
+ const allBlockIds = Array.isArray(entry.allBlockIds)
95
+ ? [...new Set(entry.allBlockIds.filter((id) => Number.isInteger(id) && id > 0))]
96
+ : []
97
+
98
+ entry.allBlockIds = allBlockIds
99
+ entry.activeBlockIds = allBlockIds.filter((id) => messagesState.activeBlockIds.has(id))
100
+ }
101
+
102
+ const nextActiveBlockIds = messagesState.activeBlockIds
103
+ let deactivatedCount = 0
104
+ let reactivatedCount = 0
105
+
106
+ for (const blockId of previousActiveBlockIds) {
107
+ if (!nextActiveBlockIds.has(blockId)) {
108
+ deactivatedCount++
109
+ }
110
+ }
111
+ for (const blockId of nextActiveBlockIds) {
112
+ if (!previousActiveBlockIds.has(blockId)) {
113
+ reactivatedCount++
114
+ }
115
+ }
116
+
117
+ if (missingOriginBlockIds.length > 0 || deactivatedCount > 0 || reactivatedCount > 0) {
118
+ logger.info("Synced compress block state", {
119
+ missingOriginCount: missingOriginBlockIds.length,
120
+ deactivatedCount,
121
+ reactivatedCount,
122
+ })
123
+ }
124
+ }
@@ -0,0 +1,187 @@
1
+ import { createHash } from "node:crypto"
2
+ import type { SessionState, WithParts } from "../state"
3
+ import { isMessageCompacted } from "../state/utils"
4
+ import type { UserMessage } from "@opencode-ai/sdk/v2"
5
+
6
+ const SUMMARY_ID_HASH_LENGTH = 16
7
+ const DCP_BLOCK_ID_TAG_REGEX = /(<dcp-message-id(?=[\s>])[^>]*>)b\d+(<\/dcp-message-id>)/g
8
+ const DCP_PAIRED_TAG_REGEX = /<dcp[^>]*>[\s\S]*?<\/dcp[^>]*>/gi
9
+ const DCP_UNPAIRED_TAG_REGEX = /<\/?dcp[^>]*>/gi
10
+
11
+ const generateStableId = (prefix: string, seed: string): string => {
12
+ const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH)
13
+ return `${prefix}_${hash}`
14
+ }
15
+
16
+ export const createSyntheticUserMessage = (
17
+ baseMessage: WithParts,
18
+ content: string,
19
+ variant?: string,
20
+ stableSeed?: string,
21
+ ): WithParts => {
22
+ const userInfo = baseMessage.info as UserMessage
23
+ const now = Date.now()
24
+ const deterministicSeed = stableSeed?.trim() || userInfo.id
25
+ const messageId = generateStableId("msg_dcp_summary", deterministicSeed)
26
+ const partId = generateStableId("prt_dcp_summary", deterministicSeed)
27
+
28
+ return {
29
+ info: {
30
+ id: messageId,
31
+ sessionID: userInfo.sessionID,
32
+ role: "user" as const,
33
+ agent: userInfo.agent,
34
+ model: userInfo.model,
35
+ time: { created: now },
36
+ ...(variant !== undefined && { variant }),
37
+ },
38
+ parts: [
39
+ {
40
+ id: partId,
41
+ sessionID: userInfo.sessionID,
42
+ messageID: messageId,
43
+ type: "text" as const,
44
+ text: content,
45
+ },
46
+ ],
47
+ }
48
+ }
49
+
50
+ export const createSyntheticTextPart = (
51
+ baseMessage: WithParts,
52
+ content: string,
53
+ stableSeed?: string,
54
+ ) => {
55
+ const userInfo = baseMessage.info as UserMessage
56
+ const deterministicSeed = stableSeed?.trim() || userInfo.id
57
+ const partId = generateStableId("prt_dcp_text", deterministicSeed)
58
+
59
+ return {
60
+ id: partId,
61
+ sessionID: userInfo.sessionID,
62
+ messageID: userInfo.id,
63
+ type: "text" as const,
64
+ text: content,
65
+ }
66
+ }
67
+
68
+ type MessagePart = WithParts["parts"][number]
69
+ type ToolPart = Extract<MessagePart, { type: "tool" }>
70
+ type TextPart = Extract<MessagePart, { type: "text" }>
71
+
72
+ export const appendToLastTextPart = (message: WithParts, injection: string): boolean => {
73
+ const textPart = findLastTextPart(message)
74
+ if (!textPart) {
75
+ return false
76
+ }
77
+
78
+ return appendToTextPart(textPart, injection)
79
+ }
80
+
81
+ const findLastTextPart = (message: WithParts): TextPart | null => {
82
+ for (let i = message.parts.length - 1; i >= 0; i--) {
83
+ const part = message.parts[i]
84
+ if (part.type === "text") {
85
+ return part
86
+ }
87
+ }
88
+
89
+ return null
90
+ }
91
+
92
+ export const appendToTextPart = (part: TextPart, injection: string): boolean => {
93
+ if (typeof part.text !== "string") {
94
+ return false
95
+ }
96
+
97
+ const normalizedInjection = injection.replace(/^\n+/, "")
98
+ if (!normalizedInjection.trim()) {
99
+ return false
100
+ }
101
+ if (part.text.includes(normalizedInjection)) {
102
+ return true
103
+ }
104
+
105
+ const baseText = part.text.replace(/\n*$/, "")
106
+ part.text = baseText.length > 0 ? `${baseText}\n\n${normalizedInjection}` : normalizedInjection
107
+ return true
108
+ }
109
+
110
+ export const appendToAllToolParts = (message: WithParts, tag: string): boolean => {
111
+ let injected = false
112
+ for (const part of message.parts) {
113
+ if (part.type === "tool") {
114
+ injected = appendToToolPart(part, tag) || injected
115
+ }
116
+ }
117
+ return injected
118
+ }
119
+
120
+ export const appendToToolPart = (part: ToolPart, tag: string): boolean => {
121
+ if (part.state?.status !== "completed" || typeof part.state.output !== "string") {
122
+ return false
123
+ }
124
+ if (part.state.output.includes(tag)) {
125
+ return true
126
+ }
127
+
128
+ part.state.output = `${part.state.output}${tag}`
129
+ return true
130
+ }
131
+
132
+ export const hasContent = (message: WithParts): boolean => {
133
+ return message.parts.some(
134
+ (part) =>
135
+ (part.type === "text" &&
136
+ typeof part.text === "string" &&
137
+ part.text.trim().length > 0) ||
138
+ (part.type === "tool" &&
139
+ part.state?.status === "completed" &&
140
+ typeof part.state.output === "string"),
141
+ )
142
+ }
143
+
144
+ export function buildToolIdList(state: SessionState, messages: WithParts[]): string[] {
145
+ const toolIds: string[] = []
146
+ for (const msg of messages) {
147
+ if (isMessageCompacted(state, msg)) {
148
+ continue
149
+ }
150
+ const parts = Array.isArray(msg.parts) ? msg.parts : []
151
+ if (parts.length > 0) {
152
+ for (const part of parts) {
153
+ if (part.type === "tool" && part.callID && part.tool) {
154
+ toolIds.push(part.callID)
155
+ }
156
+ }
157
+ }
158
+ }
159
+ state.toolIdList = toolIds
160
+ return toolIds
161
+ }
162
+
163
+ export const replaceBlockIdsWithBlocked = (text: string): string => {
164
+ return text.replace(DCP_BLOCK_ID_TAG_REGEX, "$1BLOCKED$2")
165
+ }
166
+
167
+ export const stripHallucinationsFromString = (text: string): string => {
168
+ return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
169
+ }
170
+
171
+ export const stripHallucinations = (messages: WithParts[]): void => {
172
+ for (const message of messages) {
173
+ for (const part of message.parts) {
174
+ if (part.type === "text" && typeof part.text === "string") {
175
+ part.text = stripHallucinationsFromString(part.text)
176
+ }
177
+
178
+ if (
179
+ part.type === "tool" &&
180
+ part.state?.status === "completed" &&
181
+ typeof part.state.output === "string"
182
+ ) {
183
+ part.state.output = stripHallucinationsFromString(part.state.output)
184
+ }
185
+ }
186
+ }
187
+ }
@@ -0,0 +1,42 @@
1
+ export const COMPRESS_MESSAGE = `Collapse selected individual messages in the conversation into detailed summaries.
2
+
3
+ THE SUMMARY
4
+ Your summary must be EXHAUSTIVE. Capture file paths, function signatures, decisions made, constraints discovered, key findings, tool outcomes, and user intent details that matter... EVERYTHING that preserves the value of the selected message after the raw message is removed.
5
+
6
+ USER INTENT FIDELITY
7
+ When a selected message contains user intent, preserve that intent with extra care. Do not change scope, constraints, priorities, acceptance criteria, or requested outcomes.
8
+ Directly quote short user instructions when that best preserves exact meaning.
9
+
10
+ Yet be LEAN. Strip away the noise: failed attempts that led nowhere, verbose tool output, and repetition. What remains should be pure signal - golden nuggets of detail that preserve full understanding with zero ambiguity.
11
+ If a message contains no significant technical decisions, code changes, or user requirements, produce a minimal one-line summary rather than a detailed one.
12
+
13
+ MESSAGE IDS
14
+ You specify individual raw messages by ID using the injected IDs visible in the conversation:
15
+
16
+ - \`mNNNN\` IDs identify raw messages
17
+
18
+ Each message has an ID inside XML metadata tags like \`<dcp-message-id priority="high">m0007</dcp-message-id>\`.
19
+ The same ID tag appears in every tool output of the message it belongs to — each unique ID identifies one complete message.
20
+ Treat these tags as message metadata only, not as content to summarize. Use only the inner \`mNNNN\` value as the \`messageId\`.
21
+ The \`priority\` attribute indicates relative context cost. You MUST compress high-priority messages when their full text is no longer necessary for the active task.
22
+ If prior compress-tool results are present, always compress and summarize them minimally only as part of a broader compression pass. Do not invoke the compress tool solely to re-compress an earlier compression result.
23
+ Messages marked as \`<dcp-message-id>BLOCKED</dcp-message-id>\` cannot be compressed.
24
+
25
+ Rules:
26
+
27
+ - Pick each \`messageId\` directly from injected IDs visible in context.
28
+ - Only use raw message IDs of the form \`mNNNN\`.
29
+ - Ignore XML attributes such as \`priority\` when copying the ID; use only the inner \`mNNNN\` value.
30
+ - Do not invent IDs. Use only IDs that are present in context.
31
+
32
+ BATCHING
33
+ Select MANY messages in a single tool call when they are safe to compress.
34
+ Each entry should summarize exactly one message, and the tool can receive as many entries as needed in one batch.
35
+
36
+ GENERAL CLEANUP
37
+ Use the topic "general cleanup" for broad cleanup passes.
38
+ During general cleanup, compress all medium and high-priority messages that are not relevant to the active task.
39
+ Optimize for reducing context footprint, not for grouping messages by topic.
40
+ Do not compress away still-active instructions, unresolved questions, or constraints that are likely to matter soon.
41
+ Prioritize the earliest messages in the context as they will be the least relevant to the active task.
42
+ `
@@ -0,0 +1,60 @@
1
+ export const COMPRESS_RANGE = `Collapse a range in the conversation into a detailed summary.
2
+
3
+ THE SUMMARY
4
+ Your summary must be EXHAUSTIVE. Capture file paths, function signatures, decisions made, constraints discovered, key findings... EVERYTHING that maintains context integrity. This is not a brief note - it is an authoritative record so faithful that the original conversation adds no value.
5
+
6
+ USER INTENT FIDELITY
7
+ When the compressed range includes user messages, preserve the user's intent with extra care. Do not change scope, constraints, priorities, acceptance criteria, or requested outcomes.
8
+ Directly quote user messages when they are short enough to include safely. Direct quotes are preferred when they best preserve exact meaning.
9
+
10
+ Yet be LEAN. Strip away the noise: failed attempts that led nowhere, verbose tool outputs, back-and-forth exploration. What remains should be pure signal - golden nuggets of detail that preserve full understanding with zero ambiguity.
11
+
12
+ COMPRESSED BLOCK PLACEHOLDERS
13
+ When the selected range includes previously compressed blocks, use this exact placeholder format when referencing one:
14
+
15
+ - \`(bN)\`
16
+
17
+ Compressed block sections in context are clearly marked with a header:
18
+
19
+ - \`[Compressed conversation section]\`
20
+
21
+ Compressed block IDs always use the \`bN\` form (never \`mNNNN\`) and are represented in the same XML metadata tag format.
22
+
23
+ Rules:
24
+
25
+ - Include every required block placeholder exactly once.
26
+ - Do not invent placeholders for blocks outside the selected range.
27
+ - Treat \`(bN)\` placeholders as RESERVED TOKENS. Do not emit \`(bN)\` text anywhere except intentional placeholders.
28
+ - If you need to mention a block in prose, use plain text like \`compressed bN\` (not as a placeholder).
29
+ - Preflight check before finalizing: the set of \`(bN)\` placeholders in your summary must exactly match the required set, with no duplicates.
30
+
31
+ These placeholders are semantic references. They will be replaced with the full stored compressed block content when the tool processes your output.
32
+
33
+ FLOW PRESERVATION WITH PLACEHOLDERS
34
+ When you use compressed block placeholders, write the surrounding summary text so it still reads correctly AFTER placeholder expansion.
35
+
36
+ - Treat each placeholder as a stand-in for a full conversation segment, not as a short label.
37
+ - Ensure transitions before and after each placeholder preserve chronology and causality.
38
+ - Do not write text that depends on the placeholder staying literal (for example, "as noted in \`(b2)\`").
39
+ - Your final meaning must be coherent once each placeholder is replaced with its full compressed block content.
40
+
41
+ BOUNDARY IDS
42
+ You specify boundaries by ID using the injected IDs visible in the conversation:
43
+
44
+ - \`mNNNN\` IDs identify raw messages
45
+ - \`bN\` IDs identify previously compressed blocks
46
+
47
+ Each message has an ID inside XML metadata tags like \`<dcp-message-id>...</dcp-message-id>\`.
48
+ The same ID tag appears in every tool output of the message it belongs to — each unique ID identifies one complete message.
49
+ Treat these tags as boundary metadata only, not as tool result content.
50
+
51
+ Rules:
52
+
53
+ - Pick \`startId\` and \`endId\` directly from injected IDs in context.
54
+ - IDs must exist in the current visible context.
55
+ - \`startId\` must appear before \`endId\`.
56
+ - Do not invent IDs. Use only IDs that are present in context.
57
+
58
+ BATCHING
59
+ When multiple independent ranges are ready and their boundaries do not overlap, include all of them as separate entries in the \`content\` array of a single tool call. Each entry should have its own \`startId\`, \`endId\`, and \`summary\`.
60
+ `
@@ -0,0 +1,18 @@
1
+ export const CONTEXT_LIMIT_NUDGE = `<dcp-system-reminder>
2
+ CRITICAL WARNING: MAX CONTEXT LIMIT REACHED
3
+
4
+ You are at or beyond the configured max context threshold. This is an emergency context-recovery moment.
5
+
6
+ You MUST use the \`compress\` tool now. Do not continue normal exploration until compression is handled.
7
+
8
+ If you are in the middle of a critical atomic operation, finish that atomic step first, then compress immediately.
9
+
10
+ SELECTION PROCESS
11
+ Start from older, resolved history and capture as much stale context as safely possible in one pass.
12
+ Avoid the newest active working messages unless it is clearly closed.
13
+
14
+ SUMMARY REQUIREMENTS
15
+ Your summary MUST cover all essential details from the selected messages so work can continue.
16
+ If the compressed range includes user messages, preserve user intent exactly. Prefer direct quotes for short user messages to avoid semantic drift.
17
+ </dcp-system-reminder>
18
+ `
@@ -0,0 +1,43 @@
1
+ import type { SessionState } from "../../state"
2
+
3
+ export function buildCompressedBlockGuidance(state: SessionState): string {
4
+ const refs = Array.from(state.prune.messages.activeBlockIds)
5
+ .filter((id) => Number.isInteger(id) && id > 0)
6
+ .sort((a, b) => a - b)
7
+ .map((id) => `b${id}`)
8
+ const blockCount = refs.length
9
+ const blockList = blockCount > 0 ? refs.join(", ") : "none"
10
+
11
+ return [
12
+ "Compressed block context:",
13
+ `- Active compressed blocks in this session: ${blockCount} (${blockList})`,
14
+ "- If your selected compression range includes any listed block, include each required placeholder exactly once in the summary using `(bN)`.",
15
+ ].join("\n")
16
+ }
17
+
18
+ export function renderMessagePriorityGuidance(priorityLabel: string, refs: string[]): string {
19
+ const refList = refs.length > 0 ? refs.join(", ") : "none"
20
+
21
+ return [
22
+ "Message priority context:",
23
+ "- Higher-priority older messages consume more context and should be compressed right away if it is safe to do so.",
24
+ `- ${priorityLabel}-priority message IDs before this point: ${refList}`,
25
+ ].join("\n")
26
+ }
27
+
28
+ export function appendGuidanceToDcpTag(nudgeText: string, guidance: string): string {
29
+ if (!guidance.trim()) {
30
+ return nudgeText
31
+ }
32
+
33
+ const closeTag = "</dcp-system-reminder>"
34
+ const closeTagIndex = nudgeText.lastIndexOf(closeTag)
35
+
36
+ if (closeTagIndex === -1) {
37
+ return nudgeText
38
+ }
39
+
40
+ const beforeClose = nudgeText.slice(0, closeTagIndex).trimEnd()
41
+ const afterClose = nudgeText.slice(closeTagIndex)
42
+ return `${beforeClose}\n\n${guidance}\n${afterClose}`
43
+ }
@@ -0,0 +1,32 @@
1
+ export const MANUAL_MODE_SYSTEM_EXTENSION = `<dcp-system-reminder>
2
+ Manual mode is enabled. Do NOT use compress unless the user has explicitly triggered it through a manual marker.
3
+
4
+ Only use the compress tool after seeing \`<compress triggered manually>\` in the current user instruction context.
5
+
6
+ Issue exactly ONE compress tool per manual trigger. Do NOT launch multiple compress tools in parallel. Each trigger grants a single compression; after it completes, wait for the next trigger.
7
+
8
+ After completing a manually triggered context-management action, STOP IMMEDIATELY. Do NOT continue with any task execution. End your response right after the tool use completes and wait for the next user input.
9
+ </dcp-system-reminder>
10
+ `
11
+
12
+ export const SUBAGENT_SYSTEM_EXTENSION = `<dcp-system-reminder>
13
+ You are operating in a subagent environment.
14
+
15
+ The initial subagent instruction is imperative and must be followed exactly.
16
+ It is the only user message intentionally not assigned a message ID, and therefore is not eligible for compression.
17
+ All subsequent messages in the session will have IDs.
18
+ </dcp-system-reminder>
19
+ `
20
+
21
+ export function buildProtectedToolsExtension(protectedTools: string[]): string {
22
+ if (protectedTools.length === 0) {
23
+ return ""
24
+ }
25
+
26
+ const toolList = protectedTools.map((t) => `\`${t}\``).join(", ")
27
+ return `<dcp-system-reminder>
28
+ The following tools are environment-managed: ${toolList}.
29
+ Their outputs are automatically preserved during compression.
30
+ Do not include their content in compress tool summaries — the environment retains it independently.
31
+ </dcp-system-reminder>`
32
+ }
@@ -0,0 +1,35 @@
1
+ // These format schemas are kept separate from the editable compress prompts
2
+ // so they cannot be modified via custom prompt overrides. The schemas must
3
+ // match the tool's input validation and are not safe to change independently.
4
+
5
+ export const RANGE_FORMAT_EXTENSION = `
6
+ THE FORMAT OF COMPRESS
7
+
8
+ \`\`\`
9
+ {
10
+ topic: string, // Short label (3-5 words) - e.g., "Auth System Exploration"
11
+ content: [ // One or more ranges to compress
12
+ {
13
+ startId: string, // Boundary ID at range start: mNNNN or bN
14
+ endId: string, // Boundary ID at range end: mNNNN or bN
15
+ summary: string // Complete technical summary replacing all content in range
16
+ }
17
+ ]
18
+ }
19
+ \`\`\``
20
+
21
+ export const MESSAGE_FORMAT_EXTENSION = `
22
+ THE FORMAT OF COMPRESS
23
+
24
+ \`\`\`
25
+ {
26
+ topic: string, // Short label (3-5 words) for the overall batch
27
+ content: [ // One or more messages to compress independently
28
+ {
29
+ messageId: string, // Raw message ID only: mNNNN (ignore metadata attributes like priority)
30
+ topic: string, // Short label (3-5 words) for this one message summary
31
+ summary: string // Complete technical summary replacing that one message
32
+ }
33
+ ]
34
+ }
35
+ \`\`\``
@@ -0,0 +1,29 @@
1
+ import type { RuntimePrompts } from "./store"
2
+ export type { PromptStore, RuntimePrompts } from "./store"
3
+
4
+ export function renderSystemPrompt(
5
+ prompts: RuntimePrompts,
6
+ protectedToolsExtension?: string,
7
+ manual?: boolean,
8
+ subagent?: boolean,
9
+ ): string {
10
+ const extensions: string[] = []
11
+
12
+ if (protectedToolsExtension) {
13
+ extensions.push(protectedToolsExtension.trim())
14
+ }
15
+
16
+ if (manual) {
17
+ extensions.push(prompts.manualExtension.trim())
18
+ }
19
+
20
+ if (subagent) {
21
+ extensions.push(prompts.subagentExtension.trim())
22
+ }
23
+
24
+ return [prompts.system.trim(), ...extensions]
25
+ .filter(Boolean)
26
+ .join("\n\n")
27
+ .replace(/\n([ \t]*\n)+/g, "\n\n")
28
+ .trim()
29
+ }
@@ -0,0 +1,6 @@
1
+ export const ITERATION_NUDGE = `<dcp-system-reminder>
2
+ You've been iterating for a while after the last user message.
3
+
4
+ If there is a closed portion that is unlikely to be referenced immediately (for example, finished research before implementation), use the compress tool on it now.
5
+ </dcp-system-reminder>
6
+ `