@tarquinen/opencode-dcp 3.1.12 → 3.1.14

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 (106) hide show
  1. package/README.md +14 -9
  2. package/dist/index.js +105 -39
  3. package/dist/index.js.map +1 -1
  4. package/dist/lib/commands/context.d.ts +15 -0
  5. package/dist/lib/commands/context.d.ts.map +1 -1
  6. package/dist/lib/commands/help.d.ts +1 -0
  7. package/dist/lib/commands/help.d.ts.map +1 -1
  8. package/dist/lib/commands/manual.d.ts +1 -1
  9. package/dist/lib/commands/manual.d.ts.map +1 -1
  10. package/dist/lib/commands/stats.d.ts +10 -0
  11. package/dist/lib/commands/stats.d.ts.map +1 -1
  12. package/dist/lib/compress/pipeline.d.ts.map +1 -1
  13. package/dist/lib/hooks.d.ts.map +1 -1
  14. package/dist/lib/state/persistence.d.ts +3 -0
  15. package/dist/lib/state/persistence.d.ts.map +1 -1
  16. package/dist/lib/state/state.d.ts +1 -0
  17. package/dist/lib/state/state.d.ts.map +1 -1
  18. package/dist/lib/tui/commands.d.ts +3 -0
  19. package/dist/lib/tui/commands.d.ts.map +1 -0
  20. package/dist/lib/tui/data.d.ts +14 -0
  21. package/dist/lib/tui/data.d.ts.map +1 -0
  22. package/dist/lib/tui/dialogs.d.ts +30 -0
  23. package/dist/lib/tui/dialogs.d.ts.map +1 -0
  24. package/dist/lib/tui/format.d.ts +4 -0
  25. package/dist/lib/tui/format.d.ts.map +1 -0
  26. package/dist/lib/tui/modals.d.ts +10 -0
  27. package/dist/lib/tui/modals.d.ts.map +1 -0
  28. package/dist/lib/tui/types.d.ts +15 -0
  29. package/dist/lib/tui/types.d.ts.map +1 -0
  30. package/dist/lib/tui/ui.d.ts +48 -0
  31. package/dist/lib/tui/ui.d.ts.map +1 -0
  32. package/dist/tui.d.ts +7 -0
  33. package/dist/tui.d.ts.map +1 -0
  34. package/lib/auth.ts +37 -0
  35. package/lib/commands/compression-targets.ts +137 -0
  36. package/lib/commands/context.ts +305 -0
  37. package/lib/commands/decompress.ts +275 -0
  38. package/lib/commands/help.ts +76 -0
  39. package/lib/commands/index.ts +11 -0
  40. package/lib/commands/manual.ts +127 -0
  41. package/lib/commands/recompress.ts +224 -0
  42. package/lib/commands/stats.ts +159 -0
  43. package/lib/commands/sweep.ts +268 -0
  44. package/lib/compress/index.ts +3 -0
  45. package/lib/compress/message-utils.ts +250 -0
  46. package/lib/compress/message.ts +145 -0
  47. package/lib/compress/pipeline.ts +108 -0
  48. package/lib/compress/protected-content.ts +208 -0
  49. package/lib/compress/range-utils.ts +308 -0
  50. package/lib/compress/range.ts +192 -0
  51. package/lib/compress/search.ts +267 -0
  52. package/lib/compress/state.ts +268 -0
  53. package/lib/compress/timing.ts +77 -0
  54. package/lib/compress/types.ts +108 -0
  55. package/lib/compress-permission.ts +25 -0
  56. package/lib/config.ts +1007 -0
  57. package/lib/hooks.ts +375 -0
  58. package/lib/host-permissions.ts +101 -0
  59. package/lib/logger.ts +226 -0
  60. package/lib/message-ids.ts +172 -0
  61. package/lib/messages/index.ts +8 -0
  62. package/lib/messages/inject/inject.ts +215 -0
  63. package/lib/messages/inject/subagent-results.ts +82 -0
  64. package/lib/messages/inject/utils.ts +374 -0
  65. package/lib/messages/priority.ts +102 -0
  66. package/lib/messages/prune.ts +233 -0
  67. package/lib/messages/query.ts +72 -0
  68. package/lib/messages/reasoning-strip.ts +40 -0
  69. package/lib/messages/shape.ts +50 -0
  70. package/lib/messages/sync.ts +124 -0
  71. package/lib/messages/utils.ts +185 -0
  72. package/lib/prompts/compress-message.ts +43 -0
  73. package/lib/prompts/compress-range.ts +60 -0
  74. package/lib/prompts/context-limit-nudge.ts +18 -0
  75. package/lib/prompts/extensions/nudge.ts +43 -0
  76. package/lib/prompts/extensions/system.ts +32 -0
  77. package/lib/prompts/extensions/tool.ts +35 -0
  78. package/lib/prompts/index.ts +29 -0
  79. package/lib/prompts/iteration-nudge.ts +6 -0
  80. package/lib/prompts/store.ts +467 -0
  81. package/lib/prompts/system.ts +33 -0
  82. package/lib/prompts/turn-nudge.ts +10 -0
  83. package/lib/protected-patterns.ts +128 -0
  84. package/lib/state/index.ts +4 -0
  85. package/lib/state/persistence.ts +305 -0
  86. package/lib/state/state.ts +208 -0
  87. package/lib/state/tool-cache.ts +98 -0
  88. package/lib/state/types.ts +111 -0
  89. package/lib/state/utils.ts +345 -0
  90. package/lib/strategies/deduplication.ts +127 -0
  91. package/lib/strategies/index.ts +2 -0
  92. package/lib/strategies/purge-errors.ts +88 -0
  93. package/lib/subagents/subagent-results.ts +74 -0
  94. package/lib/token-utils.ts +164 -0
  95. package/lib/tui/commands.ts +31 -0
  96. package/lib/tui/data.ts +73 -0
  97. package/lib/tui/dialogs.tsx +235 -0
  98. package/lib/tui/format.ts +25 -0
  99. package/lib/tui/modals.tsx +92 -0
  100. package/lib/tui/types.ts +16 -0
  101. package/lib/tui/ui.tsx +219 -0
  102. package/lib/ui/notification.ts +347 -0
  103. package/lib/ui/utils.ts +304 -0
  104. package/lib/update.ts +185 -0
  105. package/package.json +21 -5
  106. package/tui.tsx +26 -0
@@ -0,0 +1,72 @@
1
+ import type { PluginConfig } from "../config"
2
+ import type { WithParts } from "../state"
3
+ import { isMessageWithInfo } from "./shape"
4
+
5
+ export const getLastUserMessage = (
6
+ messages: WithParts[],
7
+ startIndex?: number,
8
+ ): WithParts | null => {
9
+ const start = startIndex ?? messages.length - 1
10
+ for (let i = start; i >= 0; i--) {
11
+ const msg = messages[i]
12
+ if (!isMessageWithInfo(msg)) {
13
+ continue
14
+ }
15
+ if (msg.info.role === "user" && !isIgnoredUserMessage(msg)) {
16
+ return msg
17
+ }
18
+ }
19
+ return null
20
+ }
21
+
22
+ export const messageHasCompress = (message: WithParts): boolean => {
23
+ if (!isMessageWithInfo(message)) {
24
+ return false
25
+ }
26
+
27
+ if (message.info.role !== "assistant") {
28
+ return false
29
+ }
30
+
31
+ const parts = Array.isArray(message.parts) ? message.parts : []
32
+ return parts.some(
33
+ (part) =>
34
+ part.type === "tool" && part.tool === "compress" && part.state?.status === "completed",
35
+ )
36
+ }
37
+
38
+ export const isIgnoredUserMessage = (message: WithParts): boolean => {
39
+ if (!isMessageWithInfo(message)) {
40
+ return false
41
+ }
42
+
43
+ if (message.info.role !== "user") {
44
+ return false
45
+ }
46
+
47
+ const parts = Array.isArray(message.parts) ? message.parts : []
48
+ if (parts.length === 0) {
49
+ return true
50
+ }
51
+
52
+ for (const part of parts) {
53
+ if (!(part as any).ignored) {
54
+ return false
55
+ }
56
+ }
57
+
58
+ return true
59
+ }
60
+
61
+ export function isProtectedUserMessage(config: PluginConfig, message: WithParts): boolean {
62
+ if (!isMessageWithInfo(message)) {
63
+ return false
64
+ }
65
+
66
+ return (
67
+ config.compress.mode === "message" &&
68
+ config.compress.protectUserMessages &&
69
+ message.info.role === "user" &&
70
+ !isIgnoredUserMessage(message)
71
+ )
72
+ }
@@ -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,50 @@
1
+ import type { WithParts } from "../state"
2
+
3
+ export function isMessageWithInfo(message: unknown): message is WithParts {
4
+ if (!message || typeof message !== "object") {
5
+ return false
6
+ }
7
+
8
+ const info = (message as any).info
9
+ const parts = (message as any).parts
10
+ if (!info || typeof info !== "object") {
11
+ return false
12
+ }
13
+
14
+ return (
15
+ typeof info.id === "string" &&
16
+ info.id.length > 0 &&
17
+ typeof info.sessionID === "string" &&
18
+ info.sessionID.length > 0 &&
19
+ (info.role === "user" || info.role === "assistant") &&
20
+ info.time &&
21
+ typeof info.time === "object" &&
22
+ typeof info.time.created === "number" &&
23
+ Array.isArray(parts)
24
+ )
25
+ }
26
+
27
+ export function filterMessages(messages: unknown): WithParts[] {
28
+ if (!Array.isArray(messages)) {
29
+ return []
30
+ }
31
+
32
+ return messages.filter(isMessageWithInfo)
33
+ }
34
+
35
+ export function filterMessagesInPlace(messages: unknown): WithParts[] {
36
+ if (!Array.isArray(messages)) {
37
+ return []
38
+ }
39
+
40
+ let writeIndex = 0
41
+
42
+ for (const message of messages) {
43
+ if (isMessageWithInfo(message)) {
44
+ messages[writeIndex++] = message
45
+ }
46
+ }
47
+
48
+ messages.length = writeIndex
49
+ return messages as WithParts[]
50
+ }
@@ -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,185 @@
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
+ stableSeed?: string,
20
+ ): WithParts => {
21
+ const userInfo = baseMessage.info as UserMessage
22
+ const now = Date.now()
23
+ const deterministicSeed = stableSeed?.trim() || userInfo.id
24
+ const messageId = generateStableId("msg_dcp_summary", deterministicSeed)
25
+ const partId = generateStableId("prt_dcp_summary", deterministicSeed)
26
+
27
+ return {
28
+ info: {
29
+ id: messageId,
30
+ sessionID: userInfo.sessionID,
31
+ role: "user" as const,
32
+ agent: userInfo.agent,
33
+ model: userInfo.model,
34
+ time: { created: now },
35
+ },
36
+ parts: [
37
+ {
38
+ id: partId,
39
+ sessionID: userInfo.sessionID,
40
+ messageID: messageId,
41
+ type: "text" as const,
42
+ text: content,
43
+ },
44
+ ],
45
+ }
46
+ }
47
+
48
+ export const createSyntheticTextPart = (
49
+ baseMessage: WithParts,
50
+ content: string,
51
+ stableSeed?: string,
52
+ ) => {
53
+ const userInfo = baseMessage.info as UserMessage
54
+ const deterministicSeed = stableSeed?.trim() || userInfo.id
55
+ const partId = generateStableId("prt_dcp_text", deterministicSeed)
56
+
57
+ return {
58
+ id: partId,
59
+ sessionID: userInfo.sessionID,
60
+ messageID: userInfo.id,
61
+ type: "text" as const,
62
+ text: content,
63
+ }
64
+ }
65
+
66
+ type MessagePart = WithParts["parts"][number]
67
+ type ToolPart = Extract<MessagePart, { type: "tool" }>
68
+ type TextPart = Extract<MessagePart, { type: "text" }>
69
+
70
+ export const appendToLastTextPart = (message: WithParts, injection: string): boolean => {
71
+ const textPart = findLastTextPart(message)
72
+ if (!textPart) {
73
+ return false
74
+ }
75
+
76
+ return appendToTextPart(textPart, injection)
77
+ }
78
+
79
+ const findLastTextPart = (message: WithParts): TextPart | null => {
80
+ for (let i = message.parts.length - 1; i >= 0; i--) {
81
+ const part = message.parts[i]
82
+ if (part.type === "text") {
83
+ return part
84
+ }
85
+ }
86
+
87
+ return null
88
+ }
89
+
90
+ export const appendToTextPart = (part: TextPart, injection: string): boolean => {
91
+ if (typeof part.text !== "string") {
92
+ return false
93
+ }
94
+
95
+ const normalizedInjection = injection.replace(/^\n+/, "")
96
+ if (!normalizedInjection.trim()) {
97
+ return false
98
+ }
99
+ if (part.text.includes(normalizedInjection)) {
100
+ return true
101
+ }
102
+
103
+ const baseText = part.text.replace(/\n*$/, "")
104
+ part.text = baseText.length > 0 ? `${baseText}\n\n${normalizedInjection}` : normalizedInjection
105
+ return true
106
+ }
107
+
108
+ export const appendToAllToolParts = (message: WithParts, tag: string): boolean => {
109
+ let injected = false
110
+ for (const part of message.parts) {
111
+ if (part.type === "tool") {
112
+ injected = appendToToolPart(part, tag) || injected
113
+ }
114
+ }
115
+ return injected
116
+ }
117
+
118
+ export const appendToToolPart = (part: ToolPart, tag: string): boolean => {
119
+ if (part.state?.status !== "completed" || typeof part.state.output !== "string") {
120
+ return false
121
+ }
122
+ if (part.state.output.includes(tag)) {
123
+ return true
124
+ }
125
+
126
+ part.state.output = `${part.state.output}${tag}`
127
+ return true
128
+ }
129
+
130
+ export const hasContent = (message: WithParts): boolean => {
131
+ return message.parts.some(
132
+ (part) =>
133
+ (part.type === "text" &&
134
+ typeof part.text === "string" &&
135
+ part.text.trim().length > 0) ||
136
+ (part.type === "tool" &&
137
+ part.state?.status === "completed" &&
138
+ typeof part.state.output === "string"),
139
+ )
140
+ }
141
+
142
+ export function buildToolIdList(state: SessionState, messages: WithParts[]): string[] {
143
+ const toolIds: string[] = []
144
+ for (const msg of messages) {
145
+ if (isMessageCompacted(state, msg)) {
146
+ continue
147
+ }
148
+ const parts = Array.isArray(msg.parts) ? msg.parts : []
149
+ if (parts.length > 0) {
150
+ for (const part of parts) {
151
+ if (part.type === "tool" && part.callID && part.tool) {
152
+ toolIds.push(part.callID)
153
+ }
154
+ }
155
+ }
156
+ }
157
+ state.toolIdList = toolIds
158
+ return toolIds
159
+ }
160
+
161
+ export const replaceBlockIdsWithBlocked = (text: string): string => {
162
+ return text.replace(DCP_BLOCK_ID_TAG_REGEX, "$1BLOCKED$2")
163
+ }
164
+
165
+ export const stripHallucinationsFromString = (text: string): string => {
166
+ return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
167
+ }
168
+
169
+ export const stripHallucinations = (messages: WithParts[]): void => {
170
+ for (const message of messages) {
171
+ for (const part of message.parts) {
172
+ if (part.type === "text" && typeof part.text === "string") {
173
+ part.text = stripHallucinationsFromString(part.text)
174
+ }
175
+
176
+ if (
177
+ part.type === "tool" &&
178
+ part.state?.status === "completed" &&
179
+ typeof part.state.output === "string"
180
+ ) {
181
+ part.state.output = stripHallucinationsFromString(part.state.output)
182
+ }
183
+ }
184
+ }
185
+ }
@@ -0,0 +1,43 @@
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
+ General cleanup should be done periodically between other normal compression tool passes, not as the primary form of compression.
43
+ `
@@ -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
+ }