@tuanhung303/opencode-dcp 2.0.0

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 (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +249 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +71 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/commands/context.d.ts +49 -0
  8. package/dist/lib/commands/context.d.ts.map +1 -0
  9. package/dist/lib/commands/context.js +191 -0
  10. package/dist/lib/commands/context.js.map +1 -0
  11. package/dist/lib/commands/help.d.ts +15 -0
  12. package/dist/lib/commands/help.d.ts.map +1 -0
  13. package/dist/lib/commands/help.js +26 -0
  14. package/dist/lib/commands/help.js.map +1 -0
  15. package/dist/lib/commands/stats.d.ts +15 -0
  16. package/dist/lib/commands/stats.d.ts.map +1 -0
  17. package/dist/lib/commands/stats.js +44 -0
  18. package/dist/lib/commands/stats.js.map +1 -0
  19. package/dist/lib/commands/sweep.d.ts +23 -0
  20. package/dist/lib/commands/sweep.d.ts.map +1 -0
  21. package/dist/lib/commands/sweep.js +191 -0
  22. package/dist/lib/commands/sweep.js.map +1 -0
  23. package/dist/lib/config.d.ts +69 -0
  24. package/dist/lib/config.d.ts.map +1 -0
  25. package/dist/lib/config.js +754 -0
  26. package/dist/lib/config.js.map +1 -0
  27. package/dist/lib/hooks.d.ts +17 -0
  28. package/dist/lib/hooks.d.ts.map +1 -0
  29. package/dist/lib/hooks.js +121 -0
  30. package/dist/lib/hooks.js.map +1 -0
  31. package/dist/lib/logger.d.ts +31 -0
  32. package/dist/lib/logger.d.ts.map +1 -0
  33. package/dist/lib/logger.js +189 -0
  34. package/dist/lib/logger.js.map +1 -0
  35. package/dist/lib/messages/index.d.ts +3 -0
  36. package/dist/lib/messages/index.d.ts.map +1 -0
  37. package/dist/lib/messages/index.js +3 -0
  38. package/dist/lib/messages/index.js.map +1 -0
  39. package/dist/lib/messages/inject.d.ts +5 -0
  40. package/dist/lib/messages/inject.d.ts.map +1 -0
  41. package/dist/lib/messages/inject.js +127 -0
  42. package/dist/lib/messages/inject.js.map +1 -0
  43. package/dist/lib/messages/prune.d.ts +5 -0
  44. package/dist/lib/messages/prune.d.ts.map +1 -0
  45. package/dist/lib/messages/prune.js +86 -0
  46. package/dist/lib/messages/prune.js.map +1 -0
  47. package/dist/lib/messages/utils.d.ts +31 -0
  48. package/dist/lib/messages/utils.d.ts.map +1 -0
  49. package/dist/lib/messages/utils.js +228 -0
  50. package/dist/lib/messages/utils.js.map +1 -0
  51. package/dist/lib/prompts/discard-tool-spec.d.ts +2 -0
  52. package/dist/lib/prompts/discard-tool-spec.d.ts.map +1 -0
  53. package/dist/lib/prompts/discard-tool-spec.js +41 -0
  54. package/dist/lib/prompts/discard-tool-spec.js.map +1 -0
  55. package/dist/lib/prompts/extract-tool-spec.d.ts +2 -0
  56. package/dist/lib/prompts/extract-tool-spec.d.ts.map +1 -0
  57. package/dist/lib/prompts/extract-tool-spec.js +48 -0
  58. package/dist/lib/prompts/extract-tool-spec.js.map +1 -0
  59. package/dist/lib/prompts/index.d.ts +2 -0
  60. package/dist/lib/prompts/index.d.ts.map +1 -0
  61. package/dist/lib/prompts/index.js +34 -0
  62. package/dist/lib/prompts/index.js.map +1 -0
  63. package/dist/lib/prompts/nudge/both.d.ts +2 -0
  64. package/dist/lib/prompts/nudge/both.d.ts.map +1 -0
  65. package/dist/lib/prompts/nudge/both.js +11 -0
  66. package/dist/lib/prompts/nudge/both.js.map +1 -0
  67. package/dist/lib/prompts/nudge/discard.d.ts +2 -0
  68. package/dist/lib/prompts/nudge/discard.d.ts.map +1 -0
  69. package/dist/lib/prompts/nudge/discard.js +10 -0
  70. package/dist/lib/prompts/nudge/discard.js.map +1 -0
  71. package/dist/lib/prompts/nudge/extract.d.ts +2 -0
  72. package/dist/lib/prompts/nudge/extract.d.ts.map +1 -0
  73. package/dist/lib/prompts/nudge/extract.js +10 -0
  74. package/dist/lib/prompts/nudge/extract.js.map +1 -0
  75. package/dist/lib/prompts/system/both.d.ts +2 -0
  76. package/dist/lib/prompts/system/both.d.ts.map +1 -0
  77. package/dist/lib/prompts/system/both.js +61 -0
  78. package/dist/lib/prompts/system/both.js.map +1 -0
  79. package/dist/lib/prompts/system/discard.d.ts +2 -0
  80. package/dist/lib/prompts/system/discard.d.ts.map +1 -0
  81. package/dist/lib/prompts/system/discard.js +52 -0
  82. package/dist/lib/prompts/system/discard.js.map +1 -0
  83. package/dist/lib/prompts/system/extract.d.ts +2 -0
  84. package/dist/lib/prompts/system/extract.d.ts.map +1 -0
  85. package/dist/lib/prompts/system/extract.js +52 -0
  86. package/dist/lib/prompts/system/extract.js.map +1 -0
  87. package/dist/lib/protected-file-patterns.d.ts +12 -0
  88. package/dist/lib/protected-file-patterns.d.ts.map +1 -0
  89. package/dist/lib/protected-file-patterns.js +69 -0
  90. package/dist/lib/protected-file-patterns.js.map +1 -0
  91. package/dist/lib/shared-utils.d.ts +4 -0
  92. package/dist/lib/shared-utils.d.ts.map +1 -0
  93. package/dist/lib/shared-utils.js +14 -0
  94. package/dist/lib/shared-utils.js.map +1 -0
  95. package/dist/lib/state/index.d.ts +4 -0
  96. package/dist/lib/state/index.d.ts.map +1 -0
  97. package/dist/lib/state/index.js +4 -0
  98. package/dist/lib/state/index.js.map +1 -0
  99. package/dist/lib/state/persistence.d.ts +22 -0
  100. package/dist/lib/state/persistence.d.ts.map +1 -0
  101. package/dist/lib/state/persistence.js +107 -0
  102. package/dist/lib/state/persistence.js.map +1 -0
  103. package/dist/lib/state/state.d.ts +8 -0
  104. package/dist/lib/state/state.d.ts.map +1 -0
  105. package/dist/lib/state/state.js +115 -0
  106. package/dist/lib/state/state.js.map +1 -0
  107. package/dist/lib/state/tool-cache.d.ts +13 -0
  108. package/dist/lib/state/tool-cache.d.ts.map +1 -0
  109. package/dist/lib/state/tool-cache.js +77 -0
  110. package/dist/lib/state/tool-cache.js.map +1 -0
  111. package/dist/lib/state/types.d.ts +33 -0
  112. package/dist/lib/state/types.d.ts.map +1 -0
  113. package/dist/lib/state/types.js +2 -0
  114. package/dist/lib/state/types.js.map +1 -0
  115. package/dist/lib/state/utils.d.ts +2 -0
  116. package/dist/lib/state/utils.d.ts.map +1 -0
  117. package/dist/lib/state/utils.js +10 -0
  118. package/dist/lib/state/utils.js.map +1 -0
  119. package/dist/lib/strategies/deduplication.d.ts +10 -0
  120. package/dist/lib/strategies/deduplication.d.ts.map +1 -0
  121. package/dist/lib/strategies/deduplication.js +94 -0
  122. package/dist/lib/strategies/deduplication.js.map +1 -0
  123. package/dist/lib/strategies/index.d.ts +7 -0
  124. package/dist/lib/strategies/index.d.ts.map +1 -0
  125. package/dist/lib/strategies/index.js +7 -0
  126. package/dist/lib/strategies/index.js.map +1 -0
  127. package/dist/lib/strategies/placeholder-compression.d.ts +5 -0
  128. package/dist/lib/strategies/placeholder-compression.d.ts.map +1 -0
  129. package/dist/lib/strategies/placeholder-compression.js +148 -0
  130. package/dist/lib/strategies/placeholder-compression.js.map +1 -0
  131. package/dist/lib/strategies/prune-thinking.d.ts +15 -0
  132. package/dist/lib/strategies/prune-thinking.d.ts.map +1 -0
  133. package/dist/lib/strategies/prune-thinking.js +79 -0
  134. package/dist/lib/strategies/prune-thinking.js.map +1 -0
  135. package/dist/lib/strategies/purge-errors.d.ts +13 -0
  136. package/dist/lib/strategies/purge-errors.d.ts.map +1 -0
  137. package/dist/lib/strategies/purge-errors.js +59 -0
  138. package/dist/lib/strategies/purge-errors.js.map +1 -0
  139. package/dist/lib/strategies/supersede-writes.d.ts +13 -0
  140. package/dist/lib/strategies/supersede-writes.d.ts.map +1 -0
  141. package/dist/lib/strategies/supersede-writes.js +84 -0
  142. package/dist/lib/strategies/supersede-writes.js.map +1 -0
  143. package/dist/lib/strategies/tools.d.ts +14 -0
  144. package/dist/lib/strategies/tools.d.ts.map +1 -0
  145. package/dist/lib/strategies/tools.js +135 -0
  146. package/dist/lib/strategies/tools.js.map +1 -0
  147. package/dist/lib/strategies/utils.d.ts +11 -0
  148. package/dist/lib/strategies/utils.d.ts.map +1 -0
  149. package/dist/lib/strategies/utils.js +75 -0
  150. package/dist/lib/strategies/utils.js.map +1 -0
  151. package/dist/lib/ui/notification.d.ts +9 -0
  152. package/dist/lib/ui/notification.d.ts.map +1 -0
  153. package/dist/lib/ui/notification.js +77 -0
  154. package/dist/lib/ui/notification.js.map +1 -0
  155. package/dist/lib/ui/utils.d.ts +10 -0
  156. package/dist/lib/ui/utils.d.ts.map +1 -0
  157. package/dist/lib/ui/utils.js +87 -0
  158. package/dist/lib/ui/utils.js.map +1 -0
  159. package/package.json +63 -0
@@ -0,0 +1,61 @@
1
+ export const SYSTEM_PROMPT_BOTH = `<system-reminder>
2
+ <instruction name=context_management_protocol policy_level=critical>
3
+
4
+ ENVIRONMENT
5
+ You are operating in a context-constrained environment and thus must proactively manage your context window using the \`discard\` and \`extract\` tools. The environment calls the \`context_info\` tool to provide an up-to-date <prunable-tools> list after each turn. Use this information when deciding what to prune.
6
+
7
+ IMPORTANT: The \`context_info\` tool is only available to the environment - you do not have access to it and must not attempt to call it.
8
+
9
+ TWO TOOLS FOR CONTEXT MANAGEMENT
10
+ - \`discard\`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.
11
+ - \`extract\`: Extract key findings into distilled knowledge before removing raw outputs. Use when you need to preserve information.
12
+
13
+ CHOOSING THE RIGHT TOOL
14
+ Ask: "Do I need to preserve any information from this output?"
15
+ - **No** → \`discard\` (default for cleanup)
16
+ - **Yes** → \`extract\` (preserves distilled knowledge)
17
+ - **Uncertain** → \`extract\` (safer, preserves signal)
18
+
19
+ Common scenarios:
20
+ - Task complete, no valuable context → \`discard\`
21
+ - Task complete, insights worth remembering → \`extract\`
22
+ - Noise, irrelevant, or superseded outputs → \`discard\`
23
+ - Valuable context needed later but raw output too large → \`extract\`
24
+
25
+ PRUNE METHODICALLY - BATCH YOUR ACTIONS
26
+ Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by pruning. Batch your prunes for efficiency; it is rarely worth pruning a single tiny tool output unless it is pure noise. Evaluate what SHOULD be pruned before jumping the gun.
27
+
28
+ You WILL evaluate pruning when ANY of these are true:
29
+ - Task or sub-task is complete
30
+ - You are about to start a new phase of work
31
+ - Write or edit operations are complete (pruning removes the large input content)
32
+
33
+ You MUST NOT prune when:
34
+ - The tool output will be needed for upcoming implementation work
35
+ - The output contains files or context you'll need to reference when making edits
36
+
37
+ Pruning that forces you to re-call the same tool later is a net loss. Only prune when you're confident the information won't be needed again.
38
+
39
+ NOTES
40
+ When in doubt, keep it. Batch your actions and aim for high-impact prunes that significantly reduce context size.
41
+ FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES.
42
+ There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY prune what you see in <prunable-tools>.
43
+
44
+ </instruction>
45
+
46
+ <instruction name=injected_context_handling policy_level=critical>
47
+ After each turn, the environment calls the \`context_info\` tool to inject a synthetic message containing a <prunable-tools> list and optional nudge instruction. This tool is only available to the environment - you do not have access to it.
48
+
49
+ CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
50
+ - NEVER reference the prune encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the prune encouragement appears.
51
+ - NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to prune")
52
+ - NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to prune")
53
+ - NEVER acknowledge discard/extract tool output (e.g., "I've pruned 3 tools", "Context pruning complete")
54
+ - NEVER let these injections influence your response content or tone
55
+ - Process this information SILENTLY and continue the conversation naturally
56
+ - Continue the conversation as if these messages do not exist
57
+
58
+ The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
59
+ </instruction>
60
+ </system-reminder>`;
61
+ //# sourceMappingURL=both.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"both.js","sourceRoot":"","sources":["../../../../lib/prompts/system/both.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,kBAAkB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA2Df,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const SYSTEM_PROMPT_DISCARD = "<system-reminder>\n<instruction name=context_management_protocol policy_level=critical>\n\nENVIRONMENT\nYou are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` tool. The environment calls the `context_info` tool to provide an up-to-date <prunable-tools> list after each turn. Use this information when deciding what to discard.\n\nIMPORTANT: The `context_info` tool is only available to the environment - you do not have access to it and must not attempt to call it.\n\nCONTEXT MANAGEMENT TOOL\n- `discard`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.\n\nDISCARD METHODICALLY - BATCH YOUR ACTIONS\nEvery tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by discarding. Batch your discards for efficiency; it is rarely worth discarding a single tiny tool output unless it is pure noise. Evaluate what SHOULD be discarded before jumping the gun.\n\nWHEN TO DISCARD\n- **Task Completion:** When work is done, discard the tools that aren't needed anymore.\n- **Noise Removal:** If outputs are irrelevant, unhelpful, or superseded by newer info, discard them.\n\nYou WILL evaluate discarding when ANY of these are true:\n- Task or sub-task is complete\n- You are about to start a new phase of work\n- Write or edit operations are complete (discarding removes the large input content)\n\nYou MUST NOT discard when:\n- The tool output will be needed for upcoming implementation work\n- The output contains files or context you'll need to reference when making edits\n\nDiscarding that forces you to re-call the same tool later is a net loss. Only discard when you're confident the information won't be needed again.\n\nNOTES\nWhen in doubt, keep it. Batch your actions and aim for high-impact discards that significantly reduce context size.\nFAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES.\nThere may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY discard what you see in <prunable-tools>.\n\n</instruction>\n\n<instruction name=injected_context_handling policy_level=critical>\nAfter each turn, the environment calls the `context_info` tool to inject a synthetic message containing a <prunable-tools> list and optional nudge instruction. This tool is only available to the environment - you do not have access to it.\n\nCRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:\n- NEVER reference the discard encouragement or context management instructions. Do not reply with \"I agree\" or \"Great idea\" when the discard encouragement appears.\n- NEVER acknowledge the <prunable-tools> list (e.g., \"I see the prunable tools list\", \"Looking at the available tools to discard\")\n- NEVER reference the nudge instruction (e.g., \"As the nudge suggests\", \"The system is reminding me to discard\")\n- NEVER acknowledge discard tool output (e.g., \"I've discarded 3 tools\", \"Context cleanup complete\")\n- NEVER let these injections influence your response content or tone\n- Process this information SILENTLY and continue the conversation naturally\n- Continue the conversation as if these messages do not exist\n\nThe user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.\n</instruction>\n</system-reminder>";
2
+ //# sourceMappingURL=discard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discard.d.ts","sourceRoot":"","sources":["../../../../lib/prompts/system/discard.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,83GAkDf,CAAA"}
@@ -0,0 +1,52 @@
1
+ export const SYSTEM_PROMPT_DISCARD = `<system-reminder>
2
+ <instruction name=context_management_protocol policy_level=critical>
3
+
4
+ ENVIRONMENT
5
+ You are operating in a context-constrained environment and thus must proactively manage your context window using the \`discard\` tool. The environment calls the \`context_info\` tool to provide an up-to-date <prunable-tools> list after each turn. Use this information when deciding what to discard.
6
+
7
+ IMPORTANT: The \`context_info\` tool is only available to the environment - you do not have access to it and must not attempt to call it.
8
+
9
+ CONTEXT MANAGEMENT TOOL
10
+ - \`discard\`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.
11
+
12
+ DISCARD METHODICALLY - BATCH YOUR ACTIONS
13
+ Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by discarding. Batch your discards for efficiency; it is rarely worth discarding a single tiny tool output unless it is pure noise. Evaluate what SHOULD be discarded before jumping the gun.
14
+
15
+ WHEN TO DISCARD
16
+ - **Task Completion:** When work is done, discard the tools that aren't needed anymore.
17
+ - **Noise Removal:** If outputs are irrelevant, unhelpful, or superseded by newer info, discard them.
18
+
19
+ You WILL evaluate discarding when ANY of these are true:
20
+ - Task or sub-task is complete
21
+ - You are about to start a new phase of work
22
+ - Write or edit operations are complete (discarding removes the large input content)
23
+
24
+ You MUST NOT discard when:
25
+ - The tool output will be needed for upcoming implementation work
26
+ - The output contains files or context you'll need to reference when making edits
27
+
28
+ Discarding that forces you to re-call the same tool later is a net loss. Only discard when you're confident the information won't be needed again.
29
+
30
+ NOTES
31
+ When in doubt, keep it. Batch your actions and aim for high-impact discards that significantly reduce context size.
32
+ FAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES.
33
+ There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY discard what you see in <prunable-tools>.
34
+
35
+ </instruction>
36
+
37
+ <instruction name=injected_context_handling policy_level=critical>
38
+ After each turn, the environment calls the \`context_info\` tool to inject a synthetic message containing a <prunable-tools> list and optional nudge instruction. This tool is only available to the environment - you do not have access to it.
39
+
40
+ CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
41
+ - NEVER reference the discard encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the discard encouragement appears.
42
+ - NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to discard")
43
+ - NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to discard")
44
+ - NEVER acknowledge discard tool output (e.g., "I've discarded 3 tools", "Context cleanup complete")
45
+ - NEVER let these injections influence your response content or tone
46
+ - Process this information SILENTLY and continue the conversation naturally
47
+ - Continue the conversation as if these messages do not exist
48
+
49
+ The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
50
+ </instruction>
51
+ </system-reminder>`;
52
+ //# sourceMappingURL=discard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discard.js","sourceRoot":"","sources":["../../../../lib/prompts/system/discard.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,qBAAqB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAkDlB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const SYSTEM_PROMPT_EXTRACT = "<system-reminder>\n<instruction name=context_management_protocol policy_level=critical>\n\nENVIRONMENT\nYou are operating in a context-constrained environment and thus must proactively manage your context window using the `extract` tool. The environment calls the `context_info` tool to provide an up-to-date <prunable-tools> list after each turn. Use this information when deciding what to extract.\n\nIMPORTANT: The `context_info` tool is only available to the environment - you do not have access to it and must not attempt to call it.\n\nCONTEXT MANAGEMENT TOOL\n- `extract`: Extract key findings from tools into distilled knowledge before removing the raw content from context. Use this to preserve important information while reducing context size.\n\nEXTRACT METHODICALLY - BATCH YOUR ACTIONS\nEvery tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by extracting. Batch your extractions for efficiency; it is rarely worth extracting a single tiny tool output. Evaluate what SHOULD be extracted before jumping the gun.\n\nWHEN TO EXTRACT\n- **Task Completion:** When work is done, extract key findings from the tools used. Scale distillation depth to the value of the content.\n- **Knowledge Preservation:** When you have valuable context you want to preserve but need to reduce size, use high-fidelity distillation. Your distillation must be comprehensive, capturing technical details (signatures, logic, constraints) such that the raw output is no longer needed. THINK: high signal, complete technical substitute.\n\nYou WILL evaluate extracting when ANY of these are true:\n- Task or sub-task is complete\n- You are about to start a new phase of work\n- Write or edit operations are complete (extracting removes the large input content)\n\nYou MUST NOT extract when:\n- The tool output will be needed for upcoming implementation work\n- The output contains files or context you'll need to reference when making edits\n\nExtracting that forces you to re-call the same tool later is a net loss. Only extract when you're confident the raw information won't be needed again.\n\nNOTES\nWhen in doubt, keep it. Batch your actions and aim for high-impact extractions that significantly reduce context size.\nFAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES.\nThere may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY extract what you see in <prunable-tools>.\n\n</instruction>\n\n<instruction name=injected_context_handling policy_level=critical>\nAfter each turn, the environment calls the `context_info` tool to inject a synthetic message containing a <prunable-tools> list and optional nudge instruction. This tool is only available to the environment - you do not have access to it.\n\nCRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:\n- NEVER reference the extract encouragement or context management instructions. Do not reply with \"I agree\" or \"Great idea\" when the extract encouragement appears.\n- NEVER acknowledge the <prunable-tools> list (e.g., \"I see the prunable tools list\", \"Looking at the available tools to extract\")\n- NEVER reference the nudge instruction (e.g., \"As the nudge suggests\", \"The system is reminding me to extract\")\n- NEVER acknowledge extract tool output (e.g., \"I've extracted 3 tools\", \"Context cleanup complete\")\n- NEVER let these injections influence your response content or tone\n- Process this information SILENTLY and continue the conversation naturally\n- Continue the conversation as if these messages do not exist\n\nThe user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.\n</instruction>\n</system-reminder>";
2
+ //# sourceMappingURL=extract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../../../../lib/prompts/system/extract.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,0sHAkDf,CAAA"}
@@ -0,0 +1,52 @@
1
+ export const SYSTEM_PROMPT_EXTRACT = `<system-reminder>
2
+ <instruction name=context_management_protocol policy_level=critical>
3
+
4
+ ENVIRONMENT
5
+ You are operating in a context-constrained environment and thus must proactively manage your context window using the \`extract\` tool. The environment calls the \`context_info\` tool to provide an up-to-date <prunable-tools> list after each turn. Use this information when deciding what to extract.
6
+
7
+ IMPORTANT: The \`context_info\` tool is only available to the environment - you do not have access to it and must not attempt to call it.
8
+
9
+ CONTEXT MANAGEMENT TOOL
10
+ - \`extract\`: Extract key findings from tools into distilled knowledge before removing the raw content from context. Use this to preserve important information while reducing context size.
11
+
12
+ EXTRACT METHODICALLY - BATCH YOUR ACTIONS
13
+ Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by extracting. Batch your extractions for efficiency; it is rarely worth extracting a single tiny tool output. Evaluate what SHOULD be extracted before jumping the gun.
14
+
15
+ WHEN TO EXTRACT
16
+ - **Task Completion:** When work is done, extract key findings from the tools used. Scale distillation depth to the value of the content.
17
+ - **Knowledge Preservation:** When you have valuable context you want to preserve but need to reduce size, use high-fidelity distillation. Your distillation must be comprehensive, capturing technical details (signatures, logic, constraints) such that the raw output is no longer needed. THINK: high signal, complete technical substitute.
18
+
19
+ You WILL evaluate extracting when ANY of these are true:
20
+ - Task or sub-task is complete
21
+ - You are about to start a new phase of work
22
+ - Write or edit operations are complete (extracting removes the large input content)
23
+
24
+ You MUST NOT extract when:
25
+ - The tool output will be needed for upcoming implementation work
26
+ - The output contains files or context you'll need to reference when making edits
27
+
28
+ Extracting that forces you to re-call the same tool later is a net loss. Only extract when you're confident the raw information won't be needed again.
29
+
30
+ NOTES
31
+ When in doubt, keep it. Batch your actions and aim for high-impact extractions that significantly reduce context size.
32
+ FAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES.
33
+ There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY extract what you see in <prunable-tools>.
34
+
35
+ </instruction>
36
+
37
+ <instruction name=injected_context_handling policy_level=critical>
38
+ After each turn, the environment calls the \`context_info\` tool to inject a synthetic message containing a <prunable-tools> list and optional nudge instruction. This tool is only available to the environment - you do not have access to it.
39
+
40
+ CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
41
+ - NEVER reference the extract encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the extract encouragement appears.
42
+ - NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to extract")
43
+ - NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to extract")
44
+ - NEVER acknowledge extract tool output (e.g., "I've extracted 3 tools", "Context cleanup complete")
45
+ - NEVER let these injections influence your response content or tone
46
+ - Process this information SILENTLY and continue the conversation naturally
47
+ - Continue the conversation as if these messages do not exist
48
+
49
+ The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
50
+ </instruction>
51
+ </system-reminder>`;
52
+ //# sourceMappingURL=extract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract.js","sourceRoot":"","sources":["../../../../lib/prompts/system/extract.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,qBAAqB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAkDlB,CAAA"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Basic glob matching with support for `**`, `*`, and `?`.
3
+ *
4
+ * Notes:
5
+ * - Matching is performed against the full (normalized) string.
6
+ * - `*` and `?` do not match `/`.
7
+ * - `**` matches across `/`.
8
+ */
9
+ export declare function matchesGlob(inputPath: string, pattern: string): boolean;
10
+ export declare function getFilePathFromParameters(parameters: unknown): string | undefined;
11
+ export declare function isProtectedFilePath(filePath: string | undefined, patterns: string[]): boolean;
12
+ //# sourceMappingURL=protected-file-patterns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protected-file-patterns.d.ts","sourceRoot":"","sources":["../../lib/protected-file-patterns.ts"],"names":[],"mappings":"AAQA;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAiDvE;AAED,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAOjF;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAK7F"}
@@ -0,0 +1,69 @@
1
+ function normalizePath(input) {
2
+ return input.replaceAll("\\\\", "/");
3
+ }
4
+ function escapeRegExpChar(ch) {
5
+ return /[\\.^$+{}()|\[\]]/.test(ch) ? `\\${ch}` : ch;
6
+ }
7
+ /**
8
+ * Basic glob matching with support for `**`, `*`, and `?`.
9
+ *
10
+ * Notes:
11
+ * - Matching is performed against the full (normalized) string.
12
+ * - `*` and `?` do not match `/`.
13
+ * - `**` matches across `/`.
14
+ */
15
+ export function matchesGlob(inputPath, pattern) {
16
+ if (!pattern)
17
+ return false;
18
+ const input = normalizePath(inputPath);
19
+ const pat = normalizePath(pattern);
20
+ let regex = "^";
21
+ for (let i = 0; i < pat.length; i++) {
22
+ const ch = pat[i];
23
+ if (ch === "*") {
24
+ const next = pat[i + 1];
25
+ if (next === "*") {
26
+ const after = pat[i + 2];
27
+ if (after === "/") {
28
+ // **/ (zero or more directories)
29
+ regex += "(?:.*/)?";
30
+ i += 2;
31
+ continue;
32
+ }
33
+ // **
34
+ regex += ".*";
35
+ i++;
36
+ continue;
37
+ }
38
+ // *
39
+ regex += "[^/]*";
40
+ continue;
41
+ }
42
+ if (ch === "?") {
43
+ regex += "[^/]";
44
+ continue;
45
+ }
46
+ if (ch === "/") {
47
+ regex += "/";
48
+ continue;
49
+ }
50
+ regex += escapeRegExpChar(ch);
51
+ }
52
+ regex += "$";
53
+ return new RegExp(regex).test(input);
54
+ }
55
+ export function getFilePathFromParameters(parameters) {
56
+ if (typeof parameters !== "object" || parameters === null) {
57
+ return undefined;
58
+ }
59
+ const filePath = parameters.filePath;
60
+ return typeof filePath === "string" && filePath.length > 0 ? filePath : undefined;
61
+ }
62
+ export function isProtectedFilePath(filePath, patterns) {
63
+ if (!filePath)
64
+ return false;
65
+ if (!patterns || patterns.length === 0)
66
+ return false;
67
+ return patterns.some((pattern) => matchesGlob(filePath, pattern));
68
+ }
69
+ //# sourceMappingURL=protected-file-patterns.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protected-file-patterns.js","sourceRoot":"","sources":["../../lib/protected-file-patterns.ts"],"names":[],"mappings":"AAAA,SAAS,aAAa,CAAC,KAAa;IAChC,OAAO,KAAK,CAAC,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AACxC,CAAC;AAED,SAAS,gBAAgB,CAAC,EAAU;IAChC,OAAO,mBAAmB,CAAC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QAED,KAAK,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAA;IACjC,CAAC;IAED,KAAK,IAAI,GAAG,CAAA;IAEZ,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACxC,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,UAAmB;IACzD,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;QACxD,OAAO,SAAS,CAAA;IACpB,CAAC;IAED,MAAM,QAAQ,GAAI,UAAsC,CAAC,QAAQ,CAAA;IACjE,OAAO,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAA;AACrF,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,QAA4B,EAAE,QAAkB;IAChF,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3B,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAEpD,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,WAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAA;AACrE,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { SessionState, WithParts } from "./state";
2
+ export declare const isMessageCompacted: (state: SessionState, msg: WithParts) => boolean;
3
+ export declare const getLastUserMessage: (messages: WithParts[]) => WithParts | null;
4
+ //# sourceMappingURL=shared-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-utils.d.ts","sourceRoot":"","sources":["../../lib/shared-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAGjD,eAAO,MAAM,kBAAkB,GAAI,OAAO,YAAY,EAAE,KAAK,SAAS,KAAG,OAExE,CAAA;AAED,eAAO,MAAM,kBAAkB,GAAI,UAAU,SAAS,EAAE,KAAG,SAAS,GAAG,IAQtE,CAAA"}
@@ -0,0 +1,14 @@
1
+ import { isIgnoredUserMessage } from "./messages/utils";
2
+ export const isMessageCompacted = (state, msg) => {
3
+ return msg.info.time.created < state.lastCompaction;
4
+ };
5
+ export const getLastUserMessage = (messages) => {
6
+ for (let i = messages.length - 1; i >= 0; i--) {
7
+ const msg = messages[i];
8
+ if (msg.info.role === "user" && !isIgnoredUserMessage(msg)) {
9
+ return msg;
10
+ }
11
+ }
12
+ return null;
13
+ };
14
+ //# sourceMappingURL=shared-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-utils.js","sourceRoot":"","sources":["../../lib/shared-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAEvD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAmB,EAAE,GAAc,EAAW,EAAE;IAC/E,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,cAAc,CAAA;AACvD,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,QAAqB,EAAoB,EAAE;IAC1E,KAAK,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5C,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;QACvB,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,EAAE,CAAC;YACzD,OAAO,GAAG,CAAA;QACd,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAA;AACf,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from "./persistence";
2
+ export * from "./types";
3
+ export * from "./state";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from "./persistence";
2
+ export * from "./types";
3
+ export * from "./state";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../lib/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * State persistence module for DCP plugin.
3
+ * Persists pruned tool IDs across sessions so they survive OpenCode restarts.
4
+ * Storage location: ~/.local/share/opencode/storage/plugin/dcp/{sessionId}.json
5
+ */
6
+ import type { SessionState, SessionStats, Prune } from "./types";
7
+ import type { Logger } from "../logger";
8
+ export interface PersistedSessionState {
9
+ sessionName?: string;
10
+ prune: Prune;
11
+ stats: SessionStats;
12
+ lastUpdated: string;
13
+ }
14
+ export declare function saveSessionState(sessionState: SessionState, logger: Logger, sessionName?: string): Promise<void>;
15
+ export declare function loadSessionState(sessionId: string, logger: Logger): Promise<PersistedSessionState | null>;
16
+ export interface AggregatedStats {
17
+ totalTokens: number;
18
+ totalTools: number;
19
+ sessionCount: number;
20
+ }
21
+ export declare function loadAllSessionStats(logger: Logger): Promise<AggregatedStats>;
22
+ //# sourceMappingURL=persistence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../../../lib/state/persistence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAEvC,MAAM,WAAW,qBAAqB;IAClC,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,KAAK,CAAA;IACZ,KAAK,EAAE,YAAY,CAAA;IACnB,WAAW,EAAE,MAAM,CAAA;CACtB;AAcD,wBAAsB,gBAAgB,CAClC,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,EACd,WAAW,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,IAAI,CAAC,CA6Bf;AAED,wBAAsB,gBAAgB,CAClC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC,CA8BvC;AAED,MAAM,WAAW,eAAe;IAC5B,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,YAAY,EAAE,MAAM,CAAA;CACvB;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAqClF"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * State persistence module for DCP plugin.
3
+ * Persists pruned tool IDs across sessions so they survive OpenCode restarts.
4
+ * Storage location: ~/.local/share/opencode/storage/plugin/dcp/{sessionId}.json
5
+ */
6
+ import * as fs from "fs/promises";
7
+ import { existsSync } from "fs";
8
+ import { homedir } from "os";
9
+ import { join } from "path";
10
+ const STORAGE_DIR = join(homedir(), ".local", "share", "opencode", "storage", "plugin", "dcp");
11
+ async function ensureStorageDir() {
12
+ if (!existsSync(STORAGE_DIR)) {
13
+ await fs.mkdir(STORAGE_DIR, { recursive: true });
14
+ }
15
+ }
16
+ function getSessionFilePath(sessionId) {
17
+ return join(STORAGE_DIR, `${sessionId}.json`);
18
+ }
19
+ export async function saveSessionState(sessionState, logger, sessionName) {
20
+ try {
21
+ if (!sessionState.sessionId) {
22
+ return;
23
+ }
24
+ await ensureStorageDir();
25
+ const state = {
26
+ sessionName: sessionName,
27
+ prune: sessionState.prune,
28
+ stats: sessionState.stats,
29
+ lastUpdated: new Date().toISOString(),
30
+ };
31
+ const filePath = getSessionFilePath(sessionState.sessionId);
32
+ const content = JSON.stringify(state, null, 2);
33
+ await fs.writeFile(filePath, content, "utf-8");
34
+ logger.info("Saved session state to disk", {
35
+ sessionId: sessionState.sessionId,
36
+ totalTokensSaved: state.stats.totalPruneTokens,
37
+ });
38
+ }
39
+ catch (error) {
40
+ logger.error("Failed to save session state", {
41
+ sessionId: sessionState.sessionId,
42
+ error: error?.message,
43
+ });
44
+ }
45
+ }
46
+ export async function loadSessionState(sessionId, logger) {
47
+ try {
48
+ const filePath = getSessionFilePath(sessionId);
49
+ if (!existsSync(filePath)) {
50
+ return null;
51
+ }
52
+ const content = await fs.readFile(filePath, "utf-8");
53
+ const state = JSON.parse(content);
54
+ if (!state || !state.prune || !Array.isArray(state.prune.toolIds) || !state.stats) {
55
+ logger.warn("Invalid session state file, ignoring", {
56
+ sessionId: sessionId,
57
+ });
58
+ return null;
59
+ }
60
+ logger.info("Loaded session state from disk", {
61
+ sessionId: sessionId,
62
+ });
63
+ return state;
64
+ }
65
+ catch (error) {
66
+ logger.warn("Failed to load session state", {
67
+ sessionId: sessionId,
68
+ error: error?.message,
69
+ });
70
+ return null;
71
+ }
72
+ }
73
+ export async function loadAllSessionStats(logger) {
74
+ const result = {
75
+ totalTokens: 0,
76
+ totalTools: 0,
77
+ sessionCount: 0,
78
+ };
79
+ try {
80
+ if (!existsSync(STORAGE_DIR)) {
81
+ return result;
82
+ }
83
+ const files = await fs.readdir(STORAGE_DIR);
84
+ const jsonFiles = files.filter((f) => f.endsWith(".json"));
85
+ for (const file of jsonFiles) {
86
+ try {
87
+ const filePath = join(STORAGE_DIR, file);
88
+ const content = await fs.readFile(filePath, "utf-8");
89
+ const state = JSON.parse(content);
90
+ if (state?.stats?.totalPruneTokens && state?.prune?.toolIds) {
91
+ result.totalTokens += state.stats.totalPruneTokens;
92
+ result.totalTools += state.prune.toolIds.length;
93
+ result.sessionCount++;
94
+ }
95
+ }
96
+ catch {
97
+ // Skip invalid files
98
+ }
99
+ }
100
+ logger.debug("Loaded all-time stats", result);
101
+ }
102
+ catch (error) {
103
+ logger.warn("Failed to load all-time stats", { error: error?.message });
104
+ }
105
+ return result;
106
+ }
107
+ //# sourceMappingURL=persistence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistence.js","sourceRoot":"","sources":["../../../lib/state/persistence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAA;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAW3B,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAA;AAE9F,KAAK,UAAU,gBAAgB;IAC3B,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC;QAC3B,MAAM,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;IACpD,CAAC;AACL,CAAC;AAED,SAAS,kBAAkB,CAAC,SAAiB;IACzC,OAAO,IAAI,CAAC,WAAW,EAAE,GAAG,SAAS,OAAO,CAAC,CAAA;AACjD,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAClC,YAA0B,EAC1B,MAAc,EACd,WAAoB;IAEpB,IAAI,CAAC;QACD,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,CAAC;YAC1B,OAAM;QACV,CAAC;QAED,MAAM,gBAAgB,EAAE,CAAA;QAExB,MAAM,KAAK,GAA0B;YACjC,WAAW,EAAE,WAAW;YACxB,KAAK,EAAE,YAAY,CAAC,KAAK;YACzB,KAA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K,EAAE,gBAAgB,IAAI,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;oBAC1D,MAAM,CAAC,WAAW,IAAI,KAAK,CAAC,KAAK,CAAC,gBAAgB,CAAA;oBAClD,MAAM,CAAC,UAAU,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAA;oBAC/C,MAAM,CAAC,YAAY,EAAE,CAAA;gBACzB,CAAC;YACL,CAAC;YAAC,MAAM,CAAC;gBACL,qBAAqB;YACzB,CAAC;QACL,CAAC;QAED,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAA;IACjD,CAAC;IAAC,OAAO,KAAU,EAAE,CAAC;QAClB,MAAM,CAAC,IAAI,CAAC,+BAA+B,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;IAC3E,CAAC;IAED,OAAO,MAAM,CAAA;AACjB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { SessionState, WithParts } from "./types";
2
+ import type { Logger } from "../logger";
3
+ export declare const checkSession: (client: any, state: SessionState, logger: Logger, messages: WithParts[]) => Promise<void>;
4
+ export declare function createSessionState(): SessionState;
5
+ export declare function resetSessionState(state: SessionState): void;
6
+ export declare function ensureSessionInitialized(client: any, state: SessionState, sessionId: string, logger: Logger, messages: WithParts[]): Promise<void>;
7
+ export declare function countTurns(state: SessionState, messages: WithParts[]): number;
8
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../lib/state/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAsB,SAAS,EAAE,MAAM,SAAS,CAAA;AAC1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAKvC,eAAO,MAAM,YAAY,GACrB,QAAQ,GAAG,EACX,OAAO,YAAY,EACnB,QAAQ,MAAM,EACd,UAAU,SAAS,EAAE,KACtB,OAAO,CAAC,IAAI,CA4Bd,CAAA;AAED,wBAAgB,kBAAkB,IAAI,YAAY,CAkBjD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAgB3D;AAED,wBAAsB,wBAAwB,CAC1C,MAAM,EAAE,GAAG,EACX,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,SAAS,EAAE,GACtB,OAAO,CAAC,IAAI,CAAC,CA8Bf;AAYD,wBAAgB,UAAU,CAAC,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM,CAc7E"}
@@ -0,0 +1,115 @@
1
+ import { loadSessionState } from "./persistence";
2
+ import { isSubAgentSession } from "./utils";
3
+ import { getLastUserMessage, isMessageCompacted } from "../shared-utils";
4
+ export const checkSession = async (client, state, logger, messages) => {
5
+ const lastUserMessage = getLastUserMessage(messages);
6
+ if (!lastUserMessage) {
7
+ return;
8
+ }
9
+ const lastSessionId = lastUserMessage.info.sessionID;
10
+ if (state.sessionId === null || state.sessionId !== lastSessionId) {
11
+ logger.info(`Session changed: ${state.sessionId} -> ${lastSessionId}`);
12
+ try {
13
+ await ensureSessionInitialized(client, state, lastSessionId, logger, messages);
14
+ }
15
+ catch (err) {
16
+ logger.error("Failed to initialize session state", { error: err.message });
17
+ }
18
+ }
19
+ const lastCompactionTimestamp = findLastCompactionTimestamp(messages);
20
+ if (lastCompactionTimestamp > state.lastCompaction) {
21
+ state.lastCompaction = lastCompactionTimestamp;
22
+ state.toolParameters.clear();
23
+ state.prune.toolIds = [];
24
+ logger.info("Detected compaction from messages - cleared tool cache", {
25
+ timestamp: lastCompactionTimestamp,
26
+ });
27
+ }
28
+ state.currentTurn = countTurns(state, messages);
29
+ };
30
+ export function createSessionState() {
31
+ return {
32
+ sessionId: null,
33
+ isSubAgent: false,
34
+ prune: {
35
+ toolIds: [],
36
+ },
37
+ stats: {
38
+ pruneTokenCounter: 0,
39
+ totalPruneTokens: 0,
40
+ },
41
+ toolParameters: new Map(),
42
+ nudgeCounter: 0,
43
+ lastToolPrune: false,
44
+ lastCompaction: 0,
45
+ currentTurn: 0,
46
+ variant: undefined,
47
+ };
48
+ }
49
+ export function resetSessionState(state) {
50
+ state.sessionId = null;
51
+ state.isSubAgent = false;
52
+ state.prune = {
53
+ toolIds: [],
54
+ };
55
+ state.stats = {
56
+ pruneTokenCounter: 0,
57
+ totalPruneTokens: 0,
58
+ };
59
+ state.toolParameters.clear();
60
+ state.nudgeCounter = 0;
61
+ state.lastToolPrune = false;
62
+ state.lastCompaction = 0;
63
+ state.currentTurn = 0;
64
+ state.variant = undefined;
65
+ }
66
+ export async function ensureSessionInitialized(client, state, sessionId, logger, messages) {
67
+ if (state.sessionId === sessionId) {
68
+ return;
69
+ }
70
+ logger.info("session ID = " + sessionId);
71
+ logger.info("Initializing session state", { sessionId: sessionId });
72
+ resetSessionState(state);
73
+ state.sessionId = sessionId;
74
+ const isSubAgent = await isSubAgentSession(client, sessionId);
75
+ state.isSubAgent = isSubAgent;
76
+ logger.info("isSubAgent = " + isSubAgent);
77
+ state.lastCompaction = findLastCompactionTimestamp(messages);
78
+ state.currentTurn = countTurns(state, messages);
79
+ const persisted = await loadSessionState(sessionId, logger);
80
+ if (persisted === null) {
81
+ return;
82
+ }
83
+ state.prune = {
84
+ toolIds: persisted.prune.toolIds || [],
85
+ };
86
+ state.stats = {
87
+ pruneTokenCounter: persisted.stats?.pruneTokenCounter || 0,
88
+ totalPruneTokens: persisted.stats?.totalPruneTokens || 0,
89
+ };
90
+ }
91
+ function findLastCompactionTimestamp(messages) {
92
+ for (let i = messages.length - 1; i >= 0; i--) {
93
+ const msg = messages[i];
94
+ if (msg.info.role === "assistant" && msg.info.summary === true) {
95
+ return msg.info.time.created;
96
+ }
97
+ }
98
+ return 0;
99
+ }
100
+ export function countTurns(state, messages) {
101
+ let turnCount = 0;
102
+ for (const msg of messages) {
103
+ if (isMessageCompacted(state, msg)) {
104
+ continue;
105
+ }
106
+ const parts = Array.isArray(msg.parts) ? msg.parts : [];
107
+ for (const part of parts) {
108
+ if (part.type === "step-start") {
109
+ turnCount++;
110
+ }
111
+ }
112
+ }
113
+ return turnCount;
114
+ }
115
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../lib/state/state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAExE,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAC7B,MAAW,EACX,KAAmB,EACnB,MAAc,EACd,QAAqB,EACR,EAAE;IACf,MAAM,eAAe,GAAG,kBAAkB,CAAC,QAAQ,CAAC,CAAA;IACpD,IAAI,CAAC,eAAe,EAAE,CAAC;QACnB,OAAM;IACV,CAAC;IAED,MAAM,aAAa,GAAG,eAAe,CAAC,IAAI,CAAC,SAAS,CAAA;IAEpD,IAAI,KAAK,CAAC,SAAS,KAAK,IAAI,IAAI,KAAK,CAAC,SAAS,KAAK,aAAa,EAAE,CAAC;QAChE,MAAM,CAAC,IAAI,CAAC,oBAAoB,KAAK,CAAC,SAAS,OAAO,aAAa,EAAE,CAAC,CAAA;QACtE,IAAI,CAAC;YACD,MAAM,wBAAwB,CAAC,MAAM,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;QAClF,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAChB,MAAM,CAAC,KAAK,CAAC,oCAAoC,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAA;QAC9E,CAAC;IACL,CAAC;IAED,MAAM,uBAAuB,GAAG,2BAA2B,CAAC,QAAQ,CA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