openhermes 2.6.1 → 4.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 (158) hide show
  1. package/CONTEXT.md +18 -0
  2. package/ETHOS.md +15 -0
  3. package/README.md +135 -292
  4. package/bootstrap.mjs +174 -499
  5. package/harness/agents/openhermes.md +87 -0
  6. package/harness/codex/CONSTITUTION.md +70 -148
  7. package/harness/codex/ROUTING.md +126 -0
  8. package/harness/commands/oh-doctor.md +26 -0
  9. package/harness/instructions/CONVENTIONS.md +206 -206
  10. package/harness/instructions/RUNTIME.md +54 -31
  11. package/harness/skills/oh-builder/SKILL.md +98 -0
  12. package/harness/skills/oh-caveman/SKILL.md +33 -0
  13. package/harness/skills/oh-expert/SKILL.md +121 -0
  14. package/harness/skills/oh-freeze/SKILL.md +28 -0
  15. package/harness/skills/oh-gauntlet/SKILL.md +119 -0
  16. package/harness/skills/oh-grill/SKILL.md +77 -0
  17. package/harness/skills/oh-guard/SKILL.md +33 -0
  18. package/harness/skills/oh-handoff/SKILL.md +33 -0
  19. package/harness/skills/oh-health/SKILL.md +90 -0
  20. package/harness/skills/oh-init/SKILL.md +78 -0
  21. package/harness/skills/oh-investigate/SKILL.md +35 -0
  22. package/harness/skills/oh-issue/SKILL.md +36 -0
  23. package/harness/skills/oh-learn/SKILL.md +28 -0
  24. package/harness/skills/oh-manifest/SKILL.md +84 -0
  25. package/harness/skills/oh-plan-review/SKILL.md +128 -0
  26. package/harness/skills/oh-planner/SKILL.md +157 -0
  27. package/harness/skills/oh-prd/SKILL.md +35 -0
  28. package/harness/skills/oh-retro/SKILL.md +33 -0
  29. package/harness/skills/oh-review/SKILL.md +110 -0
  30. package/harness/skills/oh-security/SKILL.md +110 -0
  31. package/harness/skills/oh-ship/SKILL.md +39 -0
  32. package/harness/skills/oh-skill-craft/SKILL.md +107 -0
  33. package/harness/skills/oh-skills-link/SKILL.md +29 -0
  34. package/harness/skills/oh-skills-list/SKILL.md +31 -0
  35. package/harness/skills/oh-triage/SKILL.md +36 -0
  36. package/index.mjs +3 -58
  37. package/lib/harness-resolver.mjs +77 -0
  38. package/lib/logger.mjs +62 -0
  39. package/package.json +49 -53
  40. package/test/plugins-behavioral.test.mjs +64 -0
  41. package/test/plugins.test.mjs +62 -0
  42. package/autorecall.mjs +0 -237
  43. package/curator.mjs +0 -455
  44. package/harness/commands/build-fix.md +0 -60
  45. package/harness/commands/checkpoint.md +0 -68
  46. package/harness/commands/code-review.md +0 -71
  47. package/harness/commands/doctor.md +0 -42
  48. package/harness/commands/eval.md +0 -89
  49. package/harness/commands/go-build.md +0 -87
  50. package/harness/commands/go-review.md +0 -71
  51. package/harness/commands/harness-audit.md +0 -90
  52. package/harness/commands/learn.md +0 -37
  53. package/harness/commands/loop-start.md +0 -38
  54. package/harness/commands/loop-status.md +0 -30
  55. package/harness/commands/memory-search.md +0 -37
  56. package/harness/commands/model-route.md +0 -32
  57. package/harness/commands/ohc.md +0 -13
  58. package/harness/commands/orchestrate.md +0 -88
  59. package/harness/commands/plan.md +0 -53
  60. package/harness/commands/quality-gate.md +0 -35
  61. package/harness/commands/refactor-clean.md +0 -102
  62. package/harness/commands/rust-build.md +0 -78
  63. package/harness/commands/rust-review.md +0 -65
  64. package/harness/commands/security.md +0 -93
  65. package/harness/commands/setup-pm.md +0 -65
  66. package/harness/commands/skill-create.md +0 -99
  67. package/harness/commands/test-coverage.md +0 -80
  68. package/harness/commands/update-codemaps.md +0 -81
  69. package/harness/commands/update-docs.md +0 -67
  70. package/harness/commands/verify.md +0 -68
  71. package/harness/prompts/architect.txt +0 -189
  72. package/harness/prompts/build-cpp.md +0 -98
  73. package/harness/prompts/build-error-resolver.md +0 -44
  74. package/harness/prompts/build-go.md +0 -340
  75. package/harness/prompts/build-java.md +0 -140
  76. package/harness/prompts/build-kotlin.md +0 -137
  77. package/harness/prompts/build-rust.md +0 -108
  78. package/harness/prompts/code-reviewer.md +0 -40
  79. package/harness/prompts/doc-updater.md +0 -206
  80. package/harness/prompts/docs-lookup.md +0 -71
  81. package/harness/prompts/e2e-runner.txt +0 -317
  82. package/harness/prompts/explore.md +0 -42
  83. package/harness/prompts/harness-optimizer.md +0 -42
  84. package/harness/prompts/loop-operator.md +0 -53
  85. package/harness/prompts/planner.md +0 -37
  86. package/harness/prompts/refactor-cleaner.md +0 -256
  87. package/harness/prompts/review-cpp.md +0 -81
  88. package/harness/prompts/review-database.md +0 -261
  89. package/harness/prompts/review-go.md +0 -257
  90. package/harness/prompts/review-java.md +0 -113
  91. package/harness/prompts/review-kotlin.md +0 -143
  92. package/harness/prompts/review-python.md +0 -101
  93. package/harness/prompts/review-rust.md +0 -77
  94. package/harness/prompts/security-reviewer.md +0 -42
  95. package/harness/prompts/tdd-guide.md +0 -228
  96. package/harness/rules/audit.md +0 -84
  97. package/harness/rules/checkpointing.md +0 -75
  98. package/harness/rules/context-loading.md +0 -33
  99. package/harness/rules/credential-exposure.md +0 -0
  100. package/harness/rules/delegation.md +0 -80
  101. package/harness/rules/handoff.md +0 -267
  102. package/harness/rules/memory-management.md +0 -28
  103. package/harness/rules/precedence.md +0 -52
  104. package/harness/rules/promotion.md +0 -46
  105. package/harness/rules/ranking.md +0 -64
  106. package/harness/rules/retrieval.md +0 -94
  107. package/harness/rules/runtime-guards.md +0 -196
  108. package/harness/rules/self-heal.md +0 -79
  109. package/harness/rules/session-start.md +0 -34
  110. package/harness/rules/skills-management.md +0 -165
  111. package/harness/rules/state-drift.md +0 -192
  112. package/harness/rules/verification.md +0 -88
  113. package/harness/scripts/sync-commands.mjs +0 -259
  114. package/harness/skills/.bundled_manifest +0 -17
  115. package/harness/skills/.usage.json +0 -6
  116. package/harness/skills/api-design/SKILL.md +0 -523
  117. package/harness/skills/backend-patterns/SKILL.md +0 -598
  118. package/harness/skills/coding-standards/SKILL.md +0 -549
  119. package/harness/skills/e2e-testing/SKILL.md +0 -326
  120. package/harness/skills/frontend-patterns/SKILL.md +0 -642
  121. package/harness/skills/frontend-slides/SKILL.md +0 -184
  122. package/harness/skills/security-review/SKILL.md +0 -495
  123. package/harness/skills/strategic-compact/SKILL.md +0 -131
  124. package/harness/skills/tdd-workflow/SKILL.md +0 -463
  125. package/harness/skills/verification-loop/SKILL.md +0 -126
  126. package/lib/ambient-memory.mjs +0 -167
  127. package/lib/handoff.mjs +0 -176
  128. package/lib/hardening.mjs +0 -128
  129. package/lib/memory-tools-plugin.mjs +0 -365
  130. package/lib/ohc/block-sync.mjs +0 -69
  131. package/lib/ohc/compress/search.mjs +0 -152
  132. package/lib/ohc/compress/state.mjs +0 -76
  133. package/lib/ohc/config.mjs +0 -186
  134. package/lib/ohc/message-ids.mjs +0 -168
  135. package/lib/ohc/notify.mjs +0 -154
  136. package/lib/ohc/protected-patterns.mjs +0 -54
  137. package/lib/ohc/prune-apply.mjs +0 -134
  138. package/lib/ohc/pruner.mjs +0 -610
  139. package/lib/ohc/reaper.mjs +0 -70
  140. package/lib/ohc/state.mjs +0 -266
  141. package/lib/ohc/strategies/deduplication.mjs +0 -72
  142. package/lib/ohc/strategies/index.mjs +0 -2
  143. package/lib/ohc/strategies/purge-errors.mjs +0 -43
  144. package/lib/ohc/token-utils.mjs +0 -26
  145. package/lib/ohc/updater.mjs +0 -133
  146. package/lib/paths.mjs +0 -50
  147. package/lib/schema-validator.mjs +0 -77
  148. package/lib/search.mjs +0 -48
  149. package/schemas/audit.schema.json +0 -82
  150. package/schemas/backlog.schema.json +0 -63
  151. package/schemas/checkpoint.schema.json +0 -65
  152. package/schemas/constraint.schema.json +0 -62
  153. package/schemas/decision.schema.json +0 -63
  154. package/schemas/instinct.schema.json +0 -63
  155. package/schemas/loop-state.schema.json +0 -33
  156. package/schemas/mistake.schema.json +0 -64
  157. package/schemas/verification_receipt.schema.json +0 -88
  158. package/skill-builder.mjs +0 -88
@@ -1,186 +0,0 @@
1
- import fs from "node:fs"
2
- import path from "node:path"
3
- import os from "node:os"
4
-
5
- const GLOBAL_DIR = path.join(os.homedir(), ".config", "opencode")
6
- const GLOBAL_PATH = path.join(GLOBAL_DIR, "ohc.json")
7
- const GLOBAL_PATH_JSONC = path.join(GLOBAL_DIR, "ohc.jsonc")
8
-
9
- const DEFAULTS = {
10
- enabled: true,
11
- notification: "chat",
12
- notificationMode: "detailed",
13
- max: 150000,
14
- min: 50000,
15
- manualMode: { enabled: false, automaticStrategies: true },
16
- turnProtection: { enabled: false, turns: 4 },
17
- protectedFilePatterns: [],
18
- compress: {
19
- maxContextLimit: 150000,
20
- minContextLimit: 50000,
21
- nudgeFrequency: 5,
22
- iterationNudgeThreshold: 15,
23
- nudgeForce: "soft",
24
- protectedTools: ["task", "skill", "todowrite", "todoread"],
25
- protectUserMessages: false,
26
- summaryBuffer: true,
27
- },
28
- strategies: {
29
- deduplication: { enabled: true, protectedTools: [] },
30
- purgeErrors: { enabled: true, turns: 4, protectedTools: [] },
31
- },
32
- }
33
-
34
- function findOpencodeDir(startDir) {
35
- let current = startDir
36
- while (current && current.length > 3) {
37
- const candidate = path.join(current, ".opencode")
38
- if (fs.existsSync(candidate) && fs.statSync(candidate).isDirectory()) {
39
- return candidate
40
- }
41
- const parent = path.dirname(current)
42
- if (parent === current) break
43
- current = parent
44
- }
45
- return null
46
- }
47
-
48
- function loadFile(filePath) {
49
- try {
50
- const content = fs.readFileSync(filePath, "utf8").trim()
51
- if (!content) return null
52
- if (filePath.endsWith(".jsonc")) {
53
- const stripped = content
54
- .replace(/"(?:[^"\\]|\\.)*"|\/\/.*/gm, m => m.startsWith('"') ? m : "")
55
- .replace(/\/\*[\s\S]*?\*\//g, "")
56
- return JSON.parse(stripped)
57
- }
58
- return JSON.parse(content)
59
- } catch {
60
- return null
61
- }
62
- }
63
-
64
- function mergeDeep(base, override) {
65
- if (!override || typeof override !== "object") return base
66
- const result = { ...base }
67
- for (const key of Object.keys(override)) {
68
- if (override[key] === undefined) continue
69
- if (typeof override[key] === "object" && !Array.isArray(override[key]) && override[key] !== null) {
70
- result[key] = mergeDeep(base[key] || {}, override[key])
71
- } else {
72
- result[key] = override[key]
73
- }
74
- }
75
- return result
76
- }
77
-
78
- function mergeArrays(base, override) {
79
- if (!override || !Array.isArray(override)) return base
80
- return [...new Set([...base, ...override])]
81
- }
82
-
83
- function mergeManualMode(base, override) {
84
- if (!override || typeof override !== "object") return base
85
- return {
86
- enabled: override.enabled ?? base.enabled,
87
- automaticStrategies: override.automaticStrategies ?? base.automaticStrategies,
88
- }
89
- }
90
-
91
- function mergeTurnProtection(base, override) {
92
- if (!override || typeof override !== "object") return base
93
- return {
94
- enabled: override.enabled ?? base.enabled,
95
- turns: override.turns ?? base.turns,
96
- }
97
- }
98
-
99
- function mergeCompress(base, override) {
100
- if (!override || typeof override !== "object") return base
101
- return {
102
- maxContextLimit: override.maxContextLimit ?? base.maxContextLimit,
103
- minContextLimit: override.minContextLimit ?? base.minContextLimit,
104
- nudgeFrequency: override.nudgeFrequency ?? base.nudgeFrequency,
105
- iterationNudgeThreshold: override.iterationNudgeThreshold ?? base.iterationNudgeThreshold,
106
- nudgeForce: override.nudgeForce ?? base.nudgeForce,
107
- protectedTools: mergeArrays(base.protectedTools, override.protectedTools),
108
- protectUserMessages: override.protectUserMessages ?? base.protectUserMessages,
109
- summaryBuffer: override.summaryBuffer ?? base.summaryBuffer,
110
- }
111
- }
112
-
113
- function mergeStrategies(base, override) {
114
- if (!override || typeof override !== "object") return base
115
- return {
116
- deduplication: {
117
- enabled: override.deduplication?.enabled ?? base.deduplication.enabled,
118
- protectedTools: mergeArrays(base.deduplication.protectedTools, override.deduplication?.protectedTools),
119
- },
120
- purgeErrors: {
121
- enabled: override.purgeErrors?.enabled ?? base.purgeErrors.enabled,
122
- turns: override.purgeErrors?.turns ?? base.purgeErrors.turns,
123
- protectedTools: mergeArrays(base.purgeErrors.protectedTools, override.purgeErrors?.protectedTools),
124
- },
125
- }
126
- }
127
-
128
- function mergeLayer(base, data) {
129
- if (!data) return base
130
- return {
131
- enabled: data.enabled ?? base.enabled,
132
- notification: data.notification ?? base.notification,
133
- notificationMode: data.notificationMode ?? base.notificationMode,
134
- max: data.max ?? base.max,
135
- min: data.min ?? base.min,
136
- manualMode: mergeManualMode(base.manualMode, data.manualMode),
137
- turnProtection: mergeTurnProtection(base.turnProtection, data.turnProtection),
138
- protectedFilePatterns: mergeArrays(base.protectedFilePatterns, data.protectedFilePatterns),
139
- compress: mergeCompress(base.compress, data.compress),
140
- strategies: mergeStrategies(base.strategies, data.strategies),
141
- }
142
- }
143
-
144
- export function loadConfig(ctx) {
145
- let config = { ...DEFAULTS }
146
-
147
- const layers = [
148
- { path: fs.existsSync(GLOBAL_PATH_JSONC) ? GLOBAL_PATH_JSONC : (fs.existsSync(GLOBAL_PATH) ? GLOBAL_PATH : null), name: "global" },
149
- ]
150
-
151
- const opencodeConfigDir = process.env.OPENCODE_CONFIG_DIR
152
- if (opencodeConfigDir) {
153
- const cdJsonc = path.join(opencodeConfigDir, "ohc.jsonc")
154
- const cdJson = path.join(opencodeConfigDir, "ohc.json")
155
- const cdPath = fs.existsSync(cdJsonc) ? cdJsonc : (fs.existsSync(cdJson) ? cdJson : null)
156
- if (cdPath) layers.push({ path: cdPath, name: "configDir" })
157
- }
158
-
159
- if (ctx?.directory) {
160
- const opencodeDir = findOpencodeDir(ctx.directory)
161
- if (opencodeDir) {
162
- const pjJsonc = path.join(opencodeDir, "ohc.jsonc")
163
- const pjJson = path.join(opencodeDir, "ohc.json")
164
- const pjPath = fs.existsSync(pjJsonc) ? pjJsonc : (fs.existsSync(pjJson) ? pjJson : null)
165
- if (pjPath) layers.push({ path: pjPath, name: "project" })
166
- }
167
- }
168
-
169
- for (const layer of layers) {
170
- if (!layer.path) continue
171
- const data = loadFile(layer.path)
172
- if (data) config = mergeLayer(config, data)
173
- }
174
-
175
- config.max = typeof config.max === "number" && config.max > 0 ? config.max : DEFAULTS.max
176
- config.min = typeof config.min === "number" ? Math.max(10000, Math.min(config.min, config.max - 10000)) : DEFAULTS.min
177
-
178
- return config
179
- }
180
-
181
- function writeDefaults() {
182
- fs.mkdirSync(GLOBAL_DIR, { recursive: true })
183
- if (!fs.existsSync(GLOBAL_PATH) && !fs.existsSync(GLOBAL_PATH_JSONC)) {
184
- fs.writeFileSync(GLOBAL_PATH, JSON.stringify(DEFAULTS, null, 2) + "\n", "utf8")
185
- }
186
- }
@@ -1,168 +0,0 @@
1
- const MESSAGE_REF_REGEX = /^ohc(\d{4})$/
2
- const BLOCK_REF_REGEX = /^bk([1-9]\d*)$/
3
- const OHCTAG = "ohc-ref"
4
-
5
- export function formatMessageRef(index) {
6
- if (!Number.isInteger(index) || index < 1 || index > 9999) {
7
- throw new Error(`OHC ref index out of bounds: ${index}`)
8
- }
9
- return `ohc${index.toString().padStart(4, "0")}`
10
- }
11
-
12
- export function formatBlockRef(blockId) {
13
- if (!Number.isInteger(blockId) || blockId < 1) {
14
- throw new Error(`Invalid block ID: ${blockId}`)
15
- }
16
- return `bk${blockId}`
17
- }
18
-
19
- export function parseMessageRef(ref) {
20
- const m = (ref || "").trim().toLowerCase().match(MESSAGE_REF_REGEX)
21
- if (!m) return null
22
- const idx = parseInt(m[1], 10)
23
- return Number.isInteger(idx) && idx >= 1 && idx <= 9999 ? idx : null
24
- }
25
-
26
- export function parseBlockRef(ref) {
27
- const m = (ref || "").trim().toLowerCase().match(BLOCK_REF_REGEX)
28
- if (!m) return null
29
- const id = parseInt(m[1], 10)
30
- return Number.isInteger(id) ? id : null
31
- }
32
-
33
- export function parseBoundaryId(id) {
34
- const norm = (id || "").trim().toLowerCase()
35
- const msgIdx = parseMessageRef(norm)
36
- if (msgIdx !== null) {
37
- return { kind: "message", ref: formatMessageRef(msgIdx), index: msgIdx }
38
- }
39
- const bkId = parseBlockRef(norm)
40
- if (bkId !== null) {
41
- return { kind: "compressed-block", ref: formatBlockRef(bkId), blockId: bkId }
42
- }
43
- return null
44
- }
45
-
46
- function escapeXml(s) {
47
- return String(s).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
48
- }
49
-
50
- export function formatOhcTag(ref, attrs) {
51
- const serialized = Object.entries(attrs || {})
52
- .filter(([, v]) => typeof v === "string" && v.length > 0)
53
- .sort(([a], [b]) => a.localeCompare(b))
54
- .map(([k, v]) => ` ${k}="${escapeXml(v)}"`)
55
- .join("")
56
- return `\n<${OHCTAG}${serialized}>${ref}</${OHCTAG}>`
57
- }
58
-
59
- export function isIgnoredUserMessage(message) {
60
- if (!message?.info || message.info.role !== "user") return false
61
- const parts = Array.isArray(message.parts) ? message.parts : []
62
- if (parts.length === 0) return true
63
- return parts.every(p => p.ignored)
64
- }
65
-
66
- export function assignMessageRefs(state, messages) {
67
- let assigned = 0
68
- let skippedFirstUser = false
69
-
70
- for (const msg of messages) {
71
- if (isIgnoredUserMessage(msg)) continue
72
-
73
- if (state.isSubAgent && !skippedFirstUser && msg.info.role === "user") {
74
- skippedFirstUser = true
75
- continue
76
- }
77
-
78
- const rawId = msg.info?.id
79
- if (typeof rawId !== "string" || !rawId) continue
80
-
81
- const existing = state.messageIds.byRawId.get(rawId)
82
- if (existing) {
83
- if (state.messageIds.byRef.get(existing) !== rawId) {
84
- state.messageIds.byRef.set(existing, rawId)
85
- }
86
- continue
87
- }
88
-
89
- const ref = allocateNextRef(state)
90
- state.messageIds.byRawId.set(rawId, ref)
91
- state.messageIds.byRef.set(ref, rawId)
92
- assigned++
93
- }
94
-
95
- return assigned
96
- }
97
-
98
- function allocateNextRef(state) {
99
- let candidate = Number.isInteger(state.messageIds.nextRef)
100
- ? Math.max(1, state.messageIds.nextRef)
101
- : 1
102
-
103
- for (let attempt = 0; attempt < 9999; attempt++) {
104
- if (candidate > 9999) candidate = 1
105
- const ref = formatMessageRef(candidate)
106
- if (!state.messageIds.byRef.has(ref)) {
107
- state.messageIds.nextRef = candidate + 1
108
- return ref
109
- }
110
- candidate++
111
- }
112
-
113
- state.messageIds.nextRef = 1
114
- return formatMessageRef(1)
115
- }
116
-
117
- export function injectMessageIds(state, messages) {
118
- for (const msg of messages) {
119
- if (isIgnoredUserMessage(msg)) continue
120
- const msgRef = state.messageIds.byRawId.get(msg.info?.id)
121
- if (!msgRef) continue
122
- const tag = formatOhcTag(msgRef)
123
-
124
- if (msg.info.role === "user") {
125
- let injected = false
126
- for (const part of msg.parts) {
127
- if (part.type === "text") {
128
- part.text += tag
129
- injected = true
130
- break
131
- }
132
- }
133
- if (!injected) {
134
- msg.parts.push({ type: "text", text: tag })
135
- }
136
- continue
137
- }
138
-
139
- if (msg.info.role !== "assistant") continue
140
-
141
- let hasContent = Array.isArray(msg.parts) && msg.parts.some(p => p.type === "text" || p.type === "tool")
142
- if (!hasContent) continue
143
-
144
- let injected = false
145
- for (const part of msg.parts) {
146
- if (part.type !== "tool" || typeof part.state?.output !== "string") continue
147
- part.state.output = (part.state.output || "") + tag
148
- injected = true
149
- break
150
- }
151
-
152
- if (injected) continue
153
-
154
- const lastText = [...msg.parts].reverse().find(p => p.type === "text")
155
- if (lastText) {
156
- lastText.text += tag
157
- continue
158
- }
159
-
160
- const firstToolIdx = msg.parts.findIndex(p => p.type === "tool")
161
- const synth = { type: "text", text: tag }
162
- if (firstToolIdx === -1) {
163
- msg.parts.push(synth)
164
- } else {
165
- msg.parts.splice(firstToolIdx, 0, synth)
166
- }
167
- }
168
- }
@@ -1,154 +0,0 @@
1
- function formatTokenCount(tokens) {
2
- if (tokens >= 1000) return `${(tokens / 1000).toFixed(1)}K`.replace(".0K", "K")
3
- return String(tokens)
4
- }
5
-
6
- function buildProgressBar(totalMessagesRemoved, currentMessageCount, width) {
7
- width = width || 30
8
- const total = totalMessagesRemoved + currentMessageCount
9
- if (total === 0) return `\u2502${"\u2591".repeat(width)}\u2502 0% active`
10
- const activeRatio = currentMessageCount / total
11
- const activeW = Math.round(activeRatio * width)
12
- const prunedW = width - activeW
13
- const bar = "\u2588".repeat(Math.min(activeW, width)) + "\u2591".repeat(Math.min(prunedW, width))
14
- return `\u2502${bar.slice(0, width)}\u2502 ${Math.round(activeRatio * 100)}% active`
15
- }
16
-
17
- function buildMinimal(count, tokensRemoved, savedTotal, blockCount) {
18
- const label = "Compression"
19
- return `\u25A3 OHC | ~${formatTokenCount(savedTotal)} saved total \u2014 ${label}`
20
- }
21
-
22
- function buildDetailed(count, tokensRemoved, savedTotal, blockCount, totalMessagesRemoved, currentMessageCount, summary) {
23
- const label = "Compression"
24
- let msg = `\u25A3 OHC | ~${formatTokenCount(savedTotal)} saved total`
25
- if (totalMessagesRemoved + currentMessageCount > 0) {
26
- msg += `\n\n${buildProgressBar(totalMessagesRemoved, currentMessageCount)}`
27
- }
28
- msg += `\n\n\u25A3 ${label} #${blockCount}`
29
- msg += `\n\u2192 ${count} message${count === 1 ? "" : "s"} removed`
30
- if (summary) msg += `\n\u2192 Summary: ${summary}`
31
- return msg
32
- }
33
-
34
- function buildStrategyNotification(strategy, count, detail) {
35
- return `\u25A3 OHC | ${strategy}: ${count} pruned${detail ? ` (${detail})` : ""}`
36
- }
37
-
38
- export async function sendCompressNotification(client, sessionId, config, count, summary, tokensRemoved, ss, currentMessageCount) {
39
- if (count === 0) return false
40
-
41
- const savedTotal = ss?.totalTokensSaved || 0
42
- const blockCount = ss?.blockCount || 0
43
- const totalMessagesRemoved = ss?.totalMessagesRemoved || 0
44
-
45
- const notifType = config.notification ?? "toast"
46
- const notifMode = config.notificationMode ?? "minimal"
47
-
48
- if (notifType === "off") return false
49
-
50
- if (notifType === "toast") {
51
- const message = notifMode === "minimal"
52
- ? buildMinimal(count, tokensRemoved, savedTotal, blockCount)
53
- : buildDetailed(count, tokensRemoved, savedTotal, blockCount, totalMessagesRemoved, currentMessageCount, summary)
54
- try {
55
- await client.tui.showToast({
56
- body: {
57
- title: "OHC: Compression",
58
- message,
59
- variant: "info",
60
- duration: 5000,
61
- },
62
- })
63
- } catch {}
64
- return true
65
- }
66
-
67
- try {
68
- await client.session.prompt({
69
- path: { id: sessionId },
70
- body: {
71
- noReply: true,
72
- parts: [{ type: "text", text: buildDetailed(count, tokensRemoved, savedTotal, blockCount, totalMessagesRemoved, currentMessageCount, summary), ignored: true }],
73
- },
74
- })
75
- } catch {}
76
- return true
77
- }
78
-
79
- export async function sendStrategyNotification(client, sessionId, config, strategy, count, detail) {
80
- if (count === 0) return false
81
- const notifType = config.notification ?? "toast"
82
- if (notifType === "off") return false
83
-
84
- const message = buildStrategyNotification(strategy, count, detail)
85
-
86
- if (notifType === "toast") {
87
- try {
88
- await client.tui.showToast({
89
- body: {
90
- title: `OHC: ${strategy}`,
91
- message,
92
- variant: "info",
93
- duration: 3000,
94
- },
95
- })
96
- } catch {}
97
- return true
98
- }
99
-
100
- try {
101
- await client.session.prompt({
102
- path: { id: sessionId },
103
- body: {
104
- noReply: true,
105
- parts: [{ type: "text", text: message, ignored: true }],
106
- },
107
- })
108
- } catch {}
109
- return true
110
- }
111
-
112
- export async function sendMemoryNotification(client, sessionId, config, action, cls, id, summary) {
113
- const notifType = config.notification ?? "toast"
114
- const notifMode = config.notificationMode ?? "minimal"
115
-
116
- if (notifType === "off") return false
117
-
118
- const buildHeader = (action, summary) => `[Memory] ${summary}`
119
- const buildDetailed = (action, cls, id, summary) => {
120
- let msg = `\u25A3 Memory ${action}`
121
- if (cls) msg += `\n\u2192 Class: ${cls}`
122
- if (id) msg += `\n\u2192 ID: ${id}`
123
- if (summary) msg += `\n\u2192 ${summary}`
124
- return msg
125
- }
126
-
127
- if (notifType === "toast") {
128
- const message = notifMode === "minimal"
129
- ? buildHeader(action, summary)
130
- : buildDetailed(action, cls, id, summary)
131
- try {
132
- await client.tui.showToast({
133
- body: {
134
- title: "Memory",
135
- message,
136
- variant: "info",
137
- duration: 4000,
138
- },
139
- })
140
- } catch {}
141
- return true
142
- }
143
-
144
- try {
145
- await client.session.prompt({
146
- path: { id: sessionId },
147
- body: {
148
- noReply: true,
149
- parts: [{ type: "text", text: buildDetailed(action, cls, id, summary), ignored: true }],
150
- },
151
- })
152
- } catch {}
153
- return true
154
- }
@@ -1,54 +0,0 @@
1
- const DEFAULT_PROTECTED_TOOLS = new Set([
2
- "task", "skill", "todowrite", "todoread",
3
- "compress", "batch", "plan_enter", "plan_exit",
4
- "write", "edit",
5
- ])
6
-
7
- export function isToolNameProtected(toolName, extraProtected = []) {
8
- if (DEFAULT_PROTECTED_TOOLS.has(toolName)) return true
9
- return extraProtected.includes(toolName)
10
- }
11
-
12
- export function getFilePathsFromParameters(tool, params) {
13
- if (!params || typeof params !== "object") return []
14
- if (params.filePath) return [params.filePath]
15
- if (params.file_path) return [params.file_path]
16
- if (params.path) return [params.path]
17
- if (params.target) return [params.target]
18
- if (params.directory) return [params.directory]
19
- if (tool === "glob" && params.pattern) return [params.pattern]
20
- return []
21
- }
22
-
23
- export function isFilePathProtected(filePaths, protectedPatterns = []) {
24
- if (!protectedPatterns?.length) return false
25
- if (!filePaths?.length) return false
26
- for (const fp of filePaths) {
27
- for (const pattern of protectedPatterns) {
28
- if (globMatch(fp, pattern)) return true
29
- }
30
- }
31
- return false
32
- }
33
-
34
- function globMatch(filePath, pattern) {
35
- const regexStr = pattern
36
- .replace(/[.+^${}()|[\]\\]/g, "\\$&")
37
- .replace(/\*/g, ".*")
38
- .replace(/\?/g, ".")
39
- try {
40
- return new RegExp(`^${regexStr}$`, "i").test(filePath)
41
- } catch {
42
- return filePath.toLowerCase() === pattern.toLowerCase()
43
- }
44
- }
45
-
46
- export function getTurnProtectionTags(state) {
47
- const config = state.protectedTurns || {}
48
- if (!config.enabled || !config.turns) return []
49
- const threshold = state.currentTurn - config.turns
50
- return [...state.toolIdList].filter(id => {
51
- const entry = state.toolParameters.get(id)
52
- return entry && entry.turn > threshold
53
- })
54
- }
@@ -1,134 +0,0 @@
1
- const PRUNED_OUTPUT_PLACEHOLDER = "[Output removed — information superseded or no longer needed]"
2
- const PRUNED_ERROR_INPUT_PLACEHOLDER = "[input removed — failed tool call]"
3
- const PRUNED_QUESTION_INPUT_PLACEHOLDER = "[questions removed — see output for answers]"
4
-
5
- export function applyPruneTools(state, messages) {
6
- if (!state.prune.tools.size) return 0
7
-
8
- let prunedCount = 0
9
-
10
- for (const msg of messages) {
11
- if (!Array.isArray(msg.parts)) continue
12
-
13
- for (const part of msg.parts) {
14
- if (part.type !== "tool" || !part.callID) continue
15
- if (!state.prune.tools.has(part.callID)) continue
16
-
17
- prunedCount++
18
-
19
- if (part.state?.status === "completed") {
20
- if (part.tool === "question") {
21
- if (part.state.input?.questions !== undefined) {
22
- part.state.input.questions = PRUNED_QUESTION_INPUT_PLACEHOLDER
23
- }
24
- } else if (part.tool !== "edit" && part.tool !== "write") {
25
- part.state = {
26
- ...part.state,
27
- output: PRUNED_OUTPUT_PLACEHOLDER,
28
- }
29
- }
30
- }
31
-
32
- if (part.state?.status === "error") {
33
- const input = part.state.input
34
- if (input && typeof input === "object") {
35
- for (const key of Object.keys(input)) {
36
- if (typeof input[key] === "string") {
37
- input[key] = PRUNED_ERROR_INPUT_PLACEHOLDER
38
- }
39
- }
40
- }
41
- }
42
- }
43
- }
44
-
45
- return prunedCount
46
- }
47
-
48
- export function filterCompressedBlocks(state, messages) {
49
- const ms = state.prune?.messages
50
- if (!ms || !ms.activeBlockIds?.size || !ms.blocksById?.size) return { removed: 0, injected: 0 }
51
-
52
- const coveredMessageIds = new Set()
53
- for (const [rawId, entry] of ms.byMessageId) {
54
- const hasActive = Array.isArray(entry.activeBlockIds) && entry.activeBlockIds.some(id => ms.activeBlockIds.has(id))
55
- if (hasActive) coveredMessageIds.add(rawId)
56
- }
57
-
58
- if (!coveredMessageIds.size) return { removed: 0, injected: 0 }
59
-
60
- const activeBlocks = [...ms.activeBlockIds].map(id => ms.blocksById.get(id)).filter(Boolean)
61
- const summaryByAnchor = new Map()
62
- for (const block of activeBlocks) {
63
- if (block.anchorMessageId && !summaryByAnchor.has(block.anchorMessageId)) {
64
- summaryByAnchor.set(block.anchorMessageId, block)
65
- }
66
- }
67
-
68
- let removed = 0
69
- let injected = 0
70
- const result = []
71
- const blocksDeployed = new Set()
72
-
73
- for (const msg of messages) {
74
- const mid = msg.info?.id
75
-
76
- if (mid && coveredMessageIds.has(mid)) {
77
- if (!blocksDeployed.has(mid)) {
78
- blocksDeployed.add(mid)
79
- const block = summaryByAnchor.get(mid)
80
- if (block) {
81
- result.push({
82
- parts: [{ type: "text", text: block.summary }],
83
- info: { role: "system" },
84
- })
85
- injected++
86
- }
87
- }
88
- removed++
89
- continue
90
- }
91
-
92
- result.push(msg)
93
- }
94
-
95
- messages.length = 0
96
- messages.push(...result)
97
-
98
- return { removed, injected }
99
- }
100
-
101
- export function applyFullToolRemoval(state, messages) {
102
- if (!state.prune.tools.size) return 0
103
-
104
- const removed = []
105
-
106
- for (const msg of messages) {
107
- if (!Array.isArray(msg.parts)) continue
108
-
109
- const toRemove = []
110
- for (const part of msg.parts) {
111
- if (part.type !== "tool" || !part.callID) continue
112
- if (!state.prune.tools.has(part.callID)) continue
113
- if (part.tool !== "edit" && part.tool !== "write") continue
114
- toRemove.push(part.callID)
115
- }
116
-
117
- if (!toRemove.length) continue
118
-
119
- const before = msg.parts.length
120
- msg.parts = msg.parts.filter(p => p.type !== "tool" || !toRemove.includes(p.callID))
121
- const after = msg.parts.length
122
- if (after === 0 && before > 0) {
123
- removed.push(msg.info.id)
124
- }
125
- }
126
-
127
- if (removed.length) {
128
- const result = messages.filter(m => !removed.includes(m.info.id))
129
- messages.length = 0
130
- messages.push(...result)
131
- }
132
-
133
- return removed.length
134
- }