@tarquinen/opencode-dcp 3.2.5-beta0 → 3.2.7-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 (74) hide show
  1. package/dist/lib/token-utils.js +2 -2
  2. package/dist/lib/token-utils.js.map +1 -1
  3. package/index.ts +141 -0
  4. package/lib/analysis/tokens.ts +225 -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/index.ts +3 -0
  16. package/lib/compress/message-utils.ts +250 -0
  17. package/lib/compress/message.ts +137 -0
  18. package/lib/compress/pipeline.ts +106 -0
  19. package/lib/compress/protected-content.ts +154 -0
  20. package/lib/compress/range-utils.ts +308 -0
  21. package/lib/compress/range.ts +180 -0
  22. package/lib/compress/search.ts +267 -0
  23. package/lib/compress/state.ts +268 -0
  24. package/lib/compress/timing.ts +77 -0
  25. package/lib/compress/types.ts +108 -0
  26. package/lib/compress-permission.ts +25 -0
  27. package/lib/config.ts +1071 -0
  28. package/lib/hooks.ts +378 -0
  29. package/lib/host-permissions.ts +101 -0
  30. package/lib/logger.ts +235 -0
  31. package/lib/message-ids.ts +172 -0
  32. package/lib/messages/index.ts +8 -0
  33. package/lib/messages/inject/inject.ts +215 -0
  34. package/lib/messages/inject/subagent-results.ts +82 -0
  35. package/lib/messages/inject/utils.ts +374 -0
  36. package/lib/messages/priority.ts +102 -0
  37. package/lib/messages/prune.ts +238 -0
  38. package/lib/messages/query.ts +56 -0
  39. package/lib/messages/reasoning-strip.ts +40 -0
  40. package/lib/messages/sync.ts +124 -0
  41. package/lib/messages/utils.ts +187 -0
  42. package/lib/prompts/compress-message.ts +42 -0
  43. package/lib/prompts/compress-range.ts +60 -0
  44. package/lib/prompts/context-limit-nudge.ts +18 -0
  45. package/lib/prompts/extensions/nudge.ts +43 -0
  46. package/lib/prompts/extensions/system.ts +32 -0
  47. package/lib/prompts/extensions/tool.ts +35 -0
  48. package/lib/prompts/index.ts +29 -0
  49. package/lib/prompts/iteration-nudge.ts +6 -0
  50. package/lib/prompts/store.ts +467 -0
  51. package/lib/prompts/system.ts +33 -0
  52. package/lib/prompts/turn-nudge.ts +10 -0
  53. package/lib/protected-patterns.ts +128 -0
  54. package/lib/state/index.ts +4 -0
  55. package/lib/state/persistence.ts +256 -0
  56. package/lib/state/state.ts +190 -0
  57. package/lib/state/tool-cache.ts +98 -0
  58. package/lib/state/types.ts +112 -0
  59. package/lib/state/utils.ts +334 -0
  60. package/lib/strategies/deduplication.ts +127 -0
  61. package/lib/strategies/index.ts +2 -0
  62. package/lib/strategies/purge-errors.ts +88 -0
  63. package/lib/subagents/subagent-results.ts +74 -0
  64. package/lib/token-utils.ts +162 -0
  65. package/lib/ui/notification.ts +346 -0
  66. package/lib/ui/utils.ts +287 -0
  67. package/package.json +12 -3
  68. package/tui/data/context.ts +177 -0
  69. package/tui/index.tsx +34 -0
  70. package/tui/routes/summary.tsx +175 -0
  71. package/tui/shared/names.ts +9 -0
  72. package/tui/shared/theme.ts +58 -0
  73. package/tui/shared/types.ts +38 -0
  74. package/tui/slots/sidebar-content.tsx +502 -0
@@ -0,0 +1,467 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, statSync } from "fs"
2
+ import { join, dirname } from "path"
3
+ import { homedir } from "os"
4
+ import type { Logger } from "../logger"
5
+ import { SYSTEM as SYSTEM_PROMPT } from "./system"
6
+ import { COMPRESS_RANGE as COMPRESS_RANGE_PROMPT } from "./compress-range"
7
+ import { COMPRESS_MESSAGE as COMPRESS_MESSAGE_PROMPT } from "./compress-message"
8
+ import { CONTEXT_LIMIT_NUDGE } from "./context-limit-nudge"
9
+ import { TURN_NUDGE } from "./turn-nudge"
10
+ import { ITERATION_NUDGE } from "./iteration-nudge"
11
+ import { MANUAL_MODE_SYSTEM_EXTENSION, SUBAGENT_SYSTEM_EXTENSION } from "./extensions/system"
12
+
13
+ export type PromptKey =
14
+ | "system"
15
+ | "compress-range"
16
+ | "compress-message"
17
+ | "context-limit-nudge"
18
+ | "turn-nudge"
19
+ | "iteration-nudge"
20
+
21
+ type EditablePromptField =
22
+ | "system"
23
+ | "compressRange"
24
+ | "compressMessage"
25
+ | "contextLimitNudge"
26
+ | "turnNudge"
27
+ | "iterationNudge"
28
+
29
+ interface PromptDefinition {
30
+ key: PromptKey
31
+ fileName: string
32
+ label: string
33
+ description: string
34
+ usage: string
35
+ runtimeField: EditablePromptField
36
+ }
37
+
38
+ interface PromptOverrideCandidate {
39
+ path: string
40
+ }
41
+
42
+ interface PromptPaths {
43
+ defaultsDir: string
44
+ globalOverridesDir: string
45
+ configDirOverridesDir: string | null
46
+ projectOverridesDir: string | null
47
+ }
48
+
49
+ export interface RuntimePrompts {
50
+ system: string
51
+ compressRange: string
52
+ compressMessage: string
53
+ contextLimitNudge: string
54
+ turnNudge: string
55
+ iterationNudge: string
56
+ manualExtension: string
57
+ subagentExtension: string
58
+ }
59
+
60
+ const PROMPT_DEFINITIONS: PromptDefinition[] = [
61
+ {
62
+ key: "system",
63
+ fileName: "system.md",
64
+ label: "System",
65
+ description: "Core system-level DCP instruction block",
66
+ usage: "Injected into the model system prompt on every request",
67
+ runtimeField: "system",
68
+ },
69
+ {
70
+ key: "compress-range",
71
+ fileName: "compress-range.md",
72
+ label: "Compress Range",
73
+ description: "range-mode compress tool instructions and summary constraints",
74
+ usage: "Registered as the range-mode compress tool description",
75
+ runtimeField: "compressRange",
76
+ },
77
+ {
78
+ key: "compress-message",
79
+ fileName: "compress-message.md",
80
+ label: "Compress Message",
81
+ description: "message-mode compress tool instructions and summary constraints",
82
+ usage: "Registered as the message-mode compress tool description",
83
+ runtimeField: "compressMessage",
84
+ },
85
+ {
86
+ key: "context-limit-nudge",
87
+ fileName: "context-limit-nudge.md",
88
+ label: "Context Limit Nudge",
89
+ description: "High-priority nudge when context is over max threshold",
90
+ usage: "Injected when context usage is beyond configured max limits",
91
+ runtimeField: "contextLimitNudge",
92
+ },
93
+ {
94
+ key: "turn-nudge",
95
+ fileName: "turn-nudge.md",
96
+ label: "Turn Nudge",
97
+ description: "Nudge to compress closed ranges at turn boundaries",
98
+ usage: "Injected when context is between min and max limits at a new user turn",
99
+ runtimeField: "turnNudge",
100
+ },
101
+ {
102
+ key: "iteration-nudge",
103
+ fileName: "iteration-nudge.md",
104
+ label: "Iteration Nudge",
105
+ description: "Nudge after many iterations without user input",
106
+ usage: "Injected when iteration threshold is crossed",
107
+ runtimeField: "iterationNudge",
108
+ },
109
+ ]
110
+
111
+ export const PROMPT_KEYS: PromptKey[] = [
112
+ "system",
113
+ "compress-range",
114
+ "compress-message",
115
+ "context-limit-nudge",
116
+ "turn-nudge",
117
+ "iteration-nudge",
118
+ ]
119
+
120
+ const HTML_COMMENT_REGEX = /<!--[\s\S]*?-->/g
121
+ const LEGACY_INLINE_COMMENT_LINE_REGEX = /^[ \t]*\/\/.*?\/\/[ \t]*$/gm
122
+ const DCP_SYSTEM_REMINDER_TAG_REGEX =
123
+ /^\s*<dcp-system-reminder\b[^>]*>[\s\S]*<\/dcp-system-reminder>\s*$/i
124
+ const DEFAULTS_README_FILE = "README.md"
125
+
126
+ const BUNDLED_EDITABLE_PROMPTS: Record<EditablePromptField, string> = {
127
+ system: SYSTEM_PROMPT,
128
+ compressRange: COMPRESS_RANGE_PROMPT,
129
+ compressMessage: COMPRESS_MESSAGE_PROMPT,
130
+ contextLimitNudge: CONTEXT_LIMIT_NUDGE,
131
+ turnNudge: TURN_NUDGE,
132
+ iterationNudge: ITERATION_NUDGE,
133
+ }
134
+
135
+ const INTERNAL_PROMPT_EXTENSIONS = {
136
+ manualExtension: MANUAL_MODE_SYSTEM_EXTENSION,
137
+ subagentExtension: SUBAGENT_SYSTEM_EXTENSION,
138
+ }
139
+
140
+ function createBundledRuntimePrompts(): RuntimePrompts {
141
+ return {
142
+ system: BUNDLED_EDITABLE_PROMPTS.system,
143
+ compressRange: BUNDLED_EDITABLE_PROMPTS.compressRange,
144
+ compressMessage: BUNDLED_EDITABLE_PROMPTS.compressMessage,
145
+ contextLimitNudge: BUNDLED_EDITABLE_PROMPTS.contextLimitNudge,
146
+ turnNudge: BUNDLED_EDITABLE_PROMPTS.turnNudge,
147
+ iterationNudge: BUNDLED_EDITABLE_PROMPTS.iterationNudge,
148
+ manualExtension: INTERNAL_PROMPT_EXTENSIONS.manualExtension,
149
+ subagentExtension: INTERNAL_PROMPT_EXTENSIONS.subagentExtension,
150
+ }
151
+ }
152
+
153
+ function findOpencodeDir(startDir: string): string | null {
154
+ let current = startDir
155
+ while (current !== "/") {
156
+ const candidate = join(current, ".opencode")
157
+ if (existsSync(candidate)) {
158
+ try {
159
+ if (statSync(candidate).isDirectory()) {
160
+ return candidate
161
+ }
162
+ } catch {
163
+ // ignore inaccessible entries while walking upward
164
+ }
165
+ }
166
+ const parent = dirname(current)
167
+ if (parent === current) {
168
+ break
169
+ }
170
+ current = parent
171
+ }
172
+ return null
173
+ }
174
+
175
+ function resolvePromptPaths(workingDirectory: string): PromptPaths {
176
+ const configHome = process.env.XDG_CONFIG_HOME || join(homedir(), ".config")
177
+ const globalRoot = join(configHome, "opencode", "dcp-prompts")
178
+ const defaultsDir = join(globalRoot, "defaults")
179
+ const globalOverridesDir = join(globalRoot, "overrides")
180
+
181
+ const configDirOverridesDir = process.env.OPENCODE_CONFIG_DIR
182
+ ? join(process.env.OPENCODE_CONFIG_DIR, "dcp-prompts", "overrides")
183
+ : null
184
+
185
+ const opencodeDir = findOpencodeDir(workingDirectory)
186
+ const projectOverridesDir = opencodeDir ? join(opencodeDir, "dcp-prompts", "overrides") : null
187
+
188
+ return {
189
+ defaultsDir,
190
+ globalOverridesDir,
191
+ configDirOverridesDir,
192
+ projectOverridesDir,
193
+ }
194
+ }
195
+
196
+ function stripConditionalTag(content: string, tagName: string): string {
197
+ const regex = new RegExp(`<${tagName}>[\\s\\S]*?<\/${tagName}>`, "gi")
198
+ return content.replace(regex, "")
199
+ }
200
+
201
+ function unwrapDcpTagIfWrapped(content: string): string {
202
+ const trimmed = content.trim()
203
+
204
+ if (DCP_SYSTEM_REMINDER_TAG_REGEX.test(trimmed)) {
205
+ return trimmed
206
+ .replace(/^\s*<dcp-system-reminder\b[^>]*>\s*/i, "")
207
+ .replace(/\s*<\/dcp-system-reminder>\s*$/i, "")
208
+ .trim()
209
+ }
210
+
211
+ return trimmed
212
+ }
213
+
214
+ function normalizeReminderPromptContent(content: string): string {
215
+ const normalized = content.trim()
216
+
217
+ if (!normalized) {
218
+ return ""
219
+ }
220
+
221
+ const startsWrapped = /^\s*<dcp-system-reminder\b[^>]*>/i.test(normalized)
222
+ const endsWrapped = /<\/dcp-system-reminder>\s*$/i.test(normalized)
223
+
224
+ if (startsWrapped !== endsWrapped) {
225
+ return ""
226
+ }
227
+
228
+ return unwrapDcpTagIfWrapped(normalized)
229
+ }
230
+
231
+ function stripPromptComments(content: string): string {
232
+ return content
233
+ .replace(/^\uFEFF/, "")
234
+ .replace(/\r\n?/g, "\n")
235
+ .replace(HTML_COMMENT_REGEX, "")
236
+ .replace(LEGACY_INLINE_COMMENT_LINE_REGEX, "")
237
+ }
238
+
239
+ function toEditablePromptText(definition: PromptDefinition, rawContent: string): string {
240
+ let normalized = stripPromptComments(rawContent).trim()
241
+ if (!normalized) {
242
+ return ""
243
+ }
244
+
245
+ if (definition.key === "system") {
246
+ normalized = stripConditionalTag(normalized, "manual")
247
+ normalized = stripConditionalTag(normalized, "subagent")
248
+ }
249
+
250
+ if (definition.key !== "compress-range" && definition.key !== "compress-message") {
251
+ normalized = normalizeReminderPromptContent(normalized)
252
+ }
253
+
254
+ return normalized.trim()
255
+ }
256
+
257
+ function wrapRuntimePromptContent(definition: PromptDefinition, editableText: string): string {
258
+ const trimmed = editableText.trim()
259
+ if (!trimmed) {
260
+ return ""
261
+ }
262
+
263
+ if (definition.key === "compress-range" || definition.key === "compress-message") {
264
+ return trimmed
265
+ }
266
+
267
+ return `<dcp-system-reminder>\n${trimmed}\n</dcp-system-reminder>`
268
+ }
269
+
270
+ function buildDefaultPromptFileContent(bundledEditableText: string): string {
271
+ return `${bundledEditableText.trim()}\n`
272
+ }
273
+
274
+ function buildDefaultsReadmeContent(): string {
275
+ const lines: string[] = []
276
+ lines.push("# DCP Prompt Defaults")
277
+ lines.push("")
278
+ lines.push("This directory stores the DCP prompts.")
279
+ lines.push("Each prompt file here should contain plain text only (no XML wrappers).")
280
+ lines.push("")
281
+ lines.push("## Creating Overrides")
282
+ lines.push("")
283
+ lines.push(
284
+ "1. Copy a prompt file from this directory into an overrides directory using the same filename.",
285
+ )
286
+ lines.push("2. Edit the copied file using plain text.")
287
+ lines.push("3. Restart OpenCode.")
288
+ lines.push("")
289
+ lines.push("To reset an override, delete the matching file from your overrides directory.")
290
+ lines.push("")
291
+ lines.push(
292
+ "Do not edit the default prompt files directly, they are just for reference, only files in the overrides directory are used.",
293
+ )
294
+ lines.push("")
295
+ lines.push("Override precedence (highest first):")
296
+ lines.push("1. `.opencode/dcp-prompts/overrides/` (project)")
297
+ lines.push("2. `$OPENCODE_CONFIG_DIR/dcp-prompts/overrides/` (config dir)")
298
+ lines.push("3. `~/.config/opencode/dcp-prompts/overrides/` (global)")
299
+ lines.push("")
300
+ lines.push("## Prompt Files")
301
+ lines.push("")
302
+
303
+ for (const definition of PROMPT_DEFINITIONS) {
304
+ lines.push(`- \`${definition.fileName}\``)
305
+ lines.push(` - Purpose: ${definition.description}.`)
306
+ lines.push(` - Runtime use: ${definition.usage}.`)
307
+ }
308
+
309
+ return `${lines.join("\n")}\n`
310
+ }
311
+
312
+ function readFileIfExists(filePath: string): string | null {
313
+ if (!existsSync(filePath)) {
314
+ return null
315
+ }
316
+
317
+ try {
318
+ return readFileSync(filePath, "utf-8")
319
+ } catch {
320
+ return null
321
+ }
322
+ }
323
+
324
+ export class PromptStore {
325
+ private readonly logger: Logger
326
+ private readonly paths: PromptPaths
327
+ private readonly customPromptsEnabled: boolean
328
+ private runtimePrompts: RuntimePrompts
329
+
330
+ constructor(logger: Logger, workingDirectory: string, customPromptsEnabled = false) {
331
+ this.logger = logger
332
+ this.paths = resolvePromptPaths(workingDirectory)
333
+ this.customPromptsEnabled = customPromptsEnabled
334
+ this.runtimePrompts = createBundledRuntimePrompts()
335
+
336
+ if (this.customPromptsEnabled) {
337
+ this.ensureDefaultFiles()
338
+ }
339
+ this.reload()
340
+ }
341
+
342
+ getRuntimePrompts(): RuntimePrompts {
343
+ return { ...this.runtimePrompts }
344
+ }
345
+
346
+ reload(): void {
347
+ const nextPrompts = createBundledRuntimePrompts()
348
+
349
+ if (!this.customPromptsEnabled) {
350
+ this.runtimePrompts = nextPrompts
351
+ return
352
+ }
353
+
354
+ for (const definition of PROMPT_DEFINITIONS) {
355
+ const bundledSource = BUNDLED_EDITABLE_PROMPTS[definition.runtimeField]
356
+ const bundledEditable = toEditablePromptText(definition, bundledSource)
357
+ const bundledRuntime = wrapRuntimePromptContent(definition, bundledEditable)
358
+ const fallbackValue = bundledRuntime || bundledSource.trim()
359
+ let effectiveValue = fallbackValue
360
+
361
+ for (const candidate of this.getOverrideCandidates(definition.fileName)) {
362
+ const rawOverride = readFileIfExists(candidate.path)
363
+ if (rawOverride === null) {
364
+ continue
365
+ }
366
+
367
+ const editableOverride = toEditablePromptText(definition, rawOverride)
368
+ if (!editableOverride) {
369
+ this.logger.warn("Prompt override is empty or invalid after normalization", {
370
+ key: definition.key,
371
+ path: candidate.path,
372
+ })
373
+ continue
374
+ }
375
+
376
+ const wrappedOverride = wrapRuntimePromptContent(definition, editableOverride)
377
+ if (!wrappedOverride) {
378
+ this.logger.warn("Prompt override could not be wrapped for runtime", {
379
+ key: definition.key,
380
+ path: candidate.path,
381
+ })
382
+ continue
383
+ }
384
+
385
+ effectiveValue = wrappedOverride
386
+ break
387
+ }
388
+
389
+ nextPrompts[definition.runtimeField] = effectiveValue
390
+ }
391
+
392
+ this.runtimePrompts = nextPrompts
393
+ }
394
+
395
+ private getOverrideCandidates(fileName: string): PromptOverrideCandidate[] {
396
+ const candidates: PromptOverrideCandidate[] = []
397
+
398
+ if (this.paths.projectOverridesDir) {
399
+ candidates.push({
400
+ path: join(this.paths.projectOverridesDir, fileName),
401
+ })
402
+ }
403
+
404
+ if (this.paths.configDirOverridesDir) {
405
+ candidates.push({
406
+ path: join(this.paths.configDirOverridesDir, fileName),
407
+ })
408
+ }
409
+
410
+ candidates.push({
411
+ path: join(this.paths.globalOverridesDir, fileName),
412
+ })
413
+
414
+ return candidates
415
+ }
416
+
417
+ private ensureDefaultFiles(): void {
418
+ try {
419
+ mkdirSync(this.paths.defaultsDir, { recursive: true })
420
+ mkdirSync(this.paths.globalOverridesDir, { recursive: true })
421
+ } catch {
422
+ this.logger.warn("Failed to initialize prompt directories", {
423
+ defaultsDir: this.paths.defaultsDir,
424
+ globalOverridesDir: this.paths.globalOverridesDir,
425
+ })
426
+ return
427
+ }
428
+
429
+ for (const definition of PROMPT_DEFINITIONS) {
430
+ const bundledEditable = toEditablePromptText(
431
+ definition,
432
+ BUNDLED_EDITABLE_PROMPTS[definition.runtimeField],
433
+ )
434
+ const managedContent = buildDefaultPromptFileContent(
435
+ bundledEditable || BUNDLED_EDITABLE_PROMPTS[definition.runtimeField],
436
+ )
437
+ const filePath = join(this.paths.defaultsDir, definition.fileName)
438
+
439
+ try {
440
+ const existing = readFileIfExists(filePath)
441
+ if (existing === managedContent) {
442
+ continue
443
+ }
444
+ writeFileSync(filePath, managedContent, "utf-8")
445
+ } catch {
446
+ this.logger.warn("Failed to write default prompt file", {
447
+ key: definition.key,
448
+ path: filePath,
449
+ })
450
+ }
451
+ }
452
+
453
+ const readmePath = join(this.paths.defaultsDir, DEFAULTS_README_FILE)
454
+ const readmeContent = buildDefaultsReadmeContent()
455
+
456
+ try {
457
+ const existing = readFileIfExists(readmePath)
458
+ if (existing !== readmeContent) {
459
+ writeFileSync(readmePath, readmeContent, "utf-8")
460
+ }
461
+ } catch {
462
+ this.logger.warn("Failed to write defaults README", {
463
+ path: readmePath,
464
+ })
465
+ }
466
+ }
467
+ }
@@ -0,0 +1,33 @@
1
+ export const SYSTEM = `
2
+ You operate in a context-constrained environment. Manage context continuously to avoid buildup and preserve retrieval quality. Efficient context management is paramount for your agentic performance.
3
+
4
+ The ONLY tool you have for context management is \`compress\`. It replaces older conversation content with technical summaries you produce.
5
+
6
+ \`<dcp-message-id>\` and \`<dcp-system-reminder>\` tags are environment-injected metadata. Do not output them.
7
+
8
+ THE PHILOSOPHY OF COMPRESS
9
+ \`compress\` transforms conversation content into dense, high-fidelity summaries. This is not cleanup - it is crystallization. Your summary becomes the authoritative record of what transpired.
10
+
11
+ Think of compression as phase transitions: raw exploration becomes refined understanding. The original context served its purpose; your summary now carries that understanding forward.
12
+
13
+ COMPRESS WHEN
14
+
15
+ A section is genuinely closed and the raw conversation has served its purpose:
16
+
17
+ - Research concluded and findings are clear
18
+ - Implementation finished and verified
19
+ - Exploration exhausted and patterns understood
20
+ - Dead-end noise can be discarded without waiting for a whole chapter to close
21
+
22
+ DO NOT COMPRESS IF
23
+
24
+ - Raw context is still relevant and needed for edits or precise references
25
+ - The target content is still actively in progress
26
+ - You may need exact code, error messages, or file contents in the immediate next steps
27
+
28
+ Before compressing, ask: _"Is this section closed enough to become summary-only right now?"_
29
+
30
+ Evaluate conversation signal-to-noise REGULARLY. Use \`compress\` deliberately with quality-first summaries. Prioritize stale content intelligently to maintain a high-signal context window that supports your agency.
31
+
32
+ It is of your responsibility to keep a sharp, high-quality context window for optimal performance.
33
+ `
@@ -0,0 +1,10 @@
1
+ export const TURN_NUDGE = `<dcp-system-reminder>
2
+ Evaluate the conversation for compressible ranges.
3
+
4
+ If any messages are cleanly closed and unlikely to be needed again, use the compress tool on them.
5
+ If direction has shifted, compress earlier ranges that are now less relevant.
6
+
7
+ The goal is to filter noise and distill key information so context accumulation stays under control.
8
+ Keep active context uncompressed.
9
+ </dcp-system-reminder>
10
+ `
@@ -0,0 +1,128 @@
1
+ function normalizePath(input: string): string {
2
+ return input.replaceAll("\\\\", "/")
3
+ }
4
+
5
+ function escapeRegExpChar(ch: string): string {
6
+ return /[\\.^$+{}()|\[\]]/.test(ch) ? `\\${ch}` : ch
7
+ }
8
+
9
+ export function matchesGlob(inputPath: string, pattern: string): boolean {
10
+ if (!pattern) return false
11
+
12
+ const input = normalizePath(inputPath)
13
+ const pat = normalizePath(pattern)
14
+
15
+ let regex = "^"
16
+
17
+ for (let i = 0; i < pat.length; i++) {
18
+ const ch = pat[i]
19
+
20
+ if (ch === "*") {
21
+ const next = pat[i + 1]
22
+ if (next === "*") {
23
+ const after = pat[i + 2]
24
+ if (after === "/") {
25
+ // **/ (zero or more directories)
26
+ regex += "(?:.*/)?"
27
+ i += 2
28
+ continue
29
+ }
30
+
31
+ // **
32
+ regex += ".*"
33
+ i++
34
+ continue
35
+ }
36
+
37
+ // *
38
+ regex += "[^/]*"
39
+ continue
40
+ }
41
+
42
+ if (ch === "?") {
43
+ regex += "[^/]"
44
+ continue
45
+ }
46
+
47
+ if (ch === "/") {
48
+ regex += "/"
49
+ continue
50
+ }
51
+
52
+ regex += escapeRegExpChar(ch)
53
+ }
54
+
55
+ regex += "$"
56
+
57
+ return new RegExp(regex).test(input)
58
+ }
59
+
60
+ export function getFilePathsFromParameters(tool: string, parameters: unknown): string[] {
61
+ if (typeof parameters !== "object" || parameters === null) {
62
+ return []
63
+ }
64
+
65
+ const paths: string[] = []
66
+ const params = parameters as Record<string, any>
67
+
68
+ // 1. apply_patch uses patchText with embedded paths
69
+ if (tool === "apply_patch" && typeof params.patchText === "string") {
70
+ const pathRegex = /\*\*\* (?:Add|Delete|Update) File: ([^\n\r]+)/g
71
+ let match
72
+ while ((match = pathRegex.exec(params.patchText)) !== null) {
73
+ paths.push(match[1].trim())
74
+ }
75
+ }
76
+
77
+ // 2. multiedit uses top-level filePath and nested edits array
78
+ if (tool === "multiedit") {
79
+ if (typeof params.filePath === "string") {
80
+ paths.push(params.filePath)
81
+ }
82
+ if (Array.isArray(params.edits)) {
83
+ for (const edit of params.edits) {
84
+ if (edit && typeof edit.filePath === "string") {
85
+ paths.push(edit.filePath)
86
+ }
87
+ }
88
+ }
89
+ }
90
+
91
+ // 3. Default check for common filePath parameter (read, write, edit, etc)
92
+ if (typeof params.filePath === "string") {
93
+ paths.push(params.filePath)
94
+ }
95
+
96
+ // Return unique non-empty paths
97
+ return [...new Set(paths)].filter((p) => p.length > 0)
98
+ }
99
+
100
+ export function isFilePathProtected(filePaths: string[], patterns: string[]): boolean {
101
+ if (!filePaths || filePaths.length === 0) return false
102
+ if (!patterns || patterns.length === 0) return false
103
+
104
+ return filePaths.some((path) => patterns.some((pattern) => matchesGlob(path, pattern)))
105
+ }
106
+
107
+ const GLOB_CHARS = /[*?]/
108
+
109
+ export function isToolNameProtected(toolName: string, patterns: string[]): boolean {
110
+ if (!toolName || !patterns || patterns.length === 0) return false
111
+
112
+ const exactPatterns: Set<string> = new Set()
113
+ const globPatterns: string[] = []
114
+
115
+ for (const pattern of patterns) {
116
+ if (GLOB_CHARS.test(pattern)) {
117
+ globPatterns.push(pattern)
118
+ } else {
119
+ exactPatterns.add(pattern)
120
+ }
121
+ }
122
+
123
+ if (exactPatterns.has(toolName)) {
124
+ return true
125
+ }
126
+
127
+ return globPatterns.some((pattern) => matchesGlob(toolName, pattern))
128
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./persistence"
2
+ export * from "./types"
3
+ export * from "./state"
4
+ export * from "./tool-cache"