oh-my-claude-sisyphus 3.3.10 → 3.4.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 (282) hide show
  1. package/README.md +3 -3
  2. package/agents/explore-high.md +195 -0
  3. package/agents/qa-tester-high.md +141 -0
  4. package/agents/templates/README.md +110 -0
  5. package/agents/templates/base-agent.md +54 -0
  6. package/agents/templates/tier-instructions.md +94 -0
  7. package/commands/ecomode.md +60 -0
  8. package/commands/help.md +1 -1
  9. package/commands/mcp-setup.md +7 -6
  10. package/commands/ralplan.md +8 -1
  11. package/commands/ultrapilot.md +158 -0
  12. package/commands/ultrawork.md +1 -0
  13. package/dist/__tests__/delegation-enforcer-integration.test.d.ts +6 -0
  14. package/dist/__tests__/delegation-enforcer-integration.test.d.ts.map +1 -0
  15. package/dist/__tests__/delegation-enforcer-integration.test.js +136 -0
  16. package/dist/__tests__/delegation-enforcer-integration.test.js.map +1 -0
  17. package/dist/__tests__/delegation-enforcer.test.d.ts +5 -0
  18. package/dist/__tests__/delegation-enforcer.test.d.ts.map +1 -0
  19. package/dist/__tests__/delegation-enforcer.test.js +208 -0
  20. package/dist/__tests__/delegation-enforcer.test.js.map +1 -0
  21. package/dist/__tests__/hooks.test.js +2 -2
  22. package/dist/__tests__/hooks.test.js.map +1 -1
  23. package/dist/__tests__/installer.test.js +4 -6
  24. package/dist/__tests__/installer.test.js.map +1 -1
  25. package/dist/__tests__/ralph-prd.test.js +1 -1
  26. package/dist/__tests__/ralph-prd.test.js.map +1 -1
  27. package/dist/__tests__/ralph-progress.test.js +1 -1
  28. package/dist/__tests__/ralph-progress.test.js.map +1 -1
  29. package/dist/__tests__/skills.test.js +31 -24
  30. package/dist/__tests__/skills.test.js.map +1 -1
  31. package/dist/agents/analyst.d.ts.map +1 -1
  32. package/dist/agents/analyst.js +1 -0
  33. package/dist/agents/analyst.js.map +1 -1
  34. package/dist/agents/architect.d.ts.map +1 -1
  35. package/dist/agents/architect.js +1 -0
  36. package/dist/agents/architect.js.map +1 -1
  37. package/dist/agents/critic.d.ts.map +1 -1
  38. package/dist/agents/critic.js +1 -0
  39. package/dist/agents/critic.js.map +1 -1
  40. package/dist/agents/definitions.d.ts +5 -1
  41. package/dist/agents/definitions.d.ts.map +1 -1
  42. package/dist/agents/definitions.js +53 -155
  43. package/dist/agents/definitions.js.map +1 -1
  44. package/dist/agents/designer.d.ts.map +1 -1
  45. package/dist/agents/designer.js +1 -0
  46. package/dist/agents/designer.js.map +1 -1
  47. package/dist/agents/executor.d.ts.map +1 -1
  48. package/dist/agents/executor.js +1 -0
  49. package/dist/agents/executor.js.map +1 -1
  50. package/dist/agents/explore.d.ts.map +1 -1
  51. package/dist/agents/explore.js +1 -0
  52. package/dist/agents/explore.js.map +1 -1
  53. package/dist/agents/planner.d.ts.map +1 -1
  54. package/dist/agents/planner.js +1 -0
  55. package/dist/agents/planner.js.map +1 -1
  56. package/dist/agents/qa-tester.d.ts.map +1 -1
  57. package/dist/agents/qa-tester.js +1 -0
  58. package/dist/agents/qa-tester.js.map +1 -1
  59. package/dist/agents/researcher.d.ts.map +1 -1
  60. package/dist/agents/researcher.js +1 -0
  61. package/dist/agents/researcher.js.map +1 -1
  62. package/dist/agents/scientist.d.ts.map +1 -1
  63. package/dist/agents/scientist.js +1 -0
  64. package/dist/agents/scientist.js.map +1 -1
  65. package/dist/agents/types.d.ts +2 -0
  66. package/dist/agents/types.d.ts.map +1 -1
  67. package/dist/agents/types.js.map +1 -1
  68. package/dist/agents/vision.d.ts.map +1 -1
  69. package/dist/agents/vision.js +1 -0
  70. package/dist/agents/vision.js.map +1 -1
  71. package/dist/agents/writer.d.ts.map +1 -1
  72. package/dist/agents/writer.js +1 -0
  73. package/dist/agents/writer.js.map +1 -1
  74. package/dist/cli/index.js +0 -3
  75. package/dist/cli/index.js.map +1 -1
  76. package/dist/config/loader.d.ts.map +1 -1
  77. package/dist/config/loader.js +1 -6
  78. package/dist/config/loader.js.map +1 -1
  79. package/dist/features/delegation-enforcer.d.ts +71 -0
  80. package/dist/features/delegation-enforcer.d.ts.map +1 -0
  81. package/dist/features/delegation-enforcer.js +128 -0
  82. package/dist/features/delegation-enforcer.js.map +1 -0
  83. package/dist/features/index.d.ts +3 -0
  84. package/dist/features/index.d.ts.map +1 -1
  85. package/dist/features/index.js +18 -0
  86. package/dist/features/index.js.map +1 -1
  87. package/dist/features/state-manager/index.d.ts +100 -0
  88. package/dist/features/state-manager/index.d.ts.map +1 -0
  89. package/dist/features/state-manager/index.js +407 -0
  90. package/dist/features/state-manager/index.js.map +1 -0
  91. package/dist/features/state-manager/types.d.ts +147 -0
  92. package/dist/features/state-manager/types.d.ts.map +1 -0
  93. package/dist/features/state-manager/types.js +30 -0
  94. package/dist/features/state-manager/types.js.map +1 -0
  95. package/dist/features/task-decomposer/index.d.ts +33 -0
  96. package/dist/features/task-decomposer/index.d.ts.map +1 -0
  97. package/dist/features/task-decomposer/index.js +698 -0
  98. package/dist/features/task-decomposer/index.js.map +1 -0
  99. package/dist/features/task-decomposer/types.d.ts +133 -0
  100. package/dist/features/task-decomposer/types.d.ts.map +1 -0
  101. package/dist/features/task-decomposer/types.js +8 -0
  102. package/dist/features/task-decomposer/types.js.map +1 -0
  103. package/dist/features/verification/example.d.ts +49 -0
  104. package/dist/features/verification/example.d.ts.map +1 -0
  105. package/dist/features/verification/example.js +235 -0
  106. package/dist/features/verification/example.js.map +1 -0
  107. package/dist/features/verification/index.d.ts +97 -0
  108. package/dist/features/verification/index.d.ts.map +1 -0
  109. package/dist/features/verification/index.js +439 -0
  110. package/dist/features/verification/index.js.map +1 -0
  111. package/dist/features/verification/types.d.ts +142 -0
  112. package/dist/features/verification/types.d.ts.map +1 -0
  113. package/dist/features/verification/types.js +7 -0
  114. package/dist/features/verification/types.js.map +1 -0
  115. package/dist/hooks/agent-usage-reminder/constants.d.ts.map +1 -1
  116. package/dist/hooks/agent-usage-reminder/constants.js +0 -1
  117. package/dist/hooks/agent-usage-reminder/constants.js.map +1 -1
  118. package/dist/hooks/autopilot/__tests__/cancel.test.js +4 -4
  119. package/dist/hooks/autopilot/__tests__/cancel.test.js.map +1 -1
  120. package/dist/hooks/autopilot/__tests__/summary.test.js +1 -1
  121. package/dist/hooks/autopilot/__tests__/summary.test.js.map +1 -1
  122. package/dist/hooks/autopilot/__tests__/transition.test.js +1 -2
  123. package/dist/hooks/autopilot/__tests__/transition.test.js.map +1 -1
  124. package/dist/hooks/autopilot/cancel.js +2 -2
  125. package/dist/hooks/autopilot/cancel.js.map +1 -1
  126. package/dist/hooks/autopilot/enforcement.d.ts +16 -2
  127. package/dist/hooks/autopilot/enforcement.d.ts.map +1 -1
  128. package/dist/hooks/autopilot/enforcement.js +78 -4
  129. package/dist/hooks/autopilot/enforcement.js.map +1 -1
  130. package/dist/hooks/autopilot/index.d.ts +3 -6
  131. package/dist/hooks/autopilot/index.d.ts.map +1 -1
  132. package/dist/hooks/autopilot/index.js +6 -12
  133. package/dist/hooks/autopilot/index.js.map +1 -1
  134. package/dist/hooks/autopilot/state.d.ts +36 -2
  135. package/dist/hooks/autopilot/state.d.ts.map +1 -1
  136. package/dist/hooks/autopilot/state.js +216 -2
  137. package/dist/hooks/autopilot/state.js.map +1 -1
  138. package/dist/hooks/autopilot/validation.d.ts +23 -2
  139. package/dist/hooks/autopilot/validation.d.ts.map +1 -1
  140. package/dist/hooks/autopilot/validation.js +158 -1
  141. package/dist/hooks/autopilot/validation.js.map +1 -1
  142. package/dist/hooks/bridge.js +3 -3
  143. package/dist/hooks/bridge.js.map +1 -1
  144. package/dist/hooks/index.d.ts +5 -9
  145. package/dist/hooks/index.d.ts.map +1 -1
  146. package/dist/hooks/index.js +31 -21
  147. package/dist/hooks/index.js.map +1 -1
  148. package/dist/hooks/learner/promotion.js +1 -1
  149. package/dist/hooks/learner/promotion.js.map +1 -1
  150. package/dist/hooks/persistent-mode/index.d.ts.map +1 -1
  151. package/dist/hooks/persistent-mode/index.js +2 -3
  152. package/dist/hooks/persistent-mode/index.js.map +1 -1
  153. package/dist/hooks/ralph/index.d.ts +11 -0
  154. package/dist/hooks/ralph/index.d.ts.map +1 -0
  155. package/dist/hooks/ralph/index.js +53 -0
  156. package/dist/hooks/ralph/index.js.map +1 -0
  157. package/dist/hooks/ralph/loop.d.ts +116 -0
  158. package/dist/hooks/ralph/loop.d.ts.map +1 -0
  159. package/dist/hooks/ralph/loop.js +322 -0
  160. package/dist/hooks/ralph/loop.js.map +1 -0
  161. package/dist/hooks/ralph/prd.d.ts +130 -0
  162. package/dist/hooks/ralph/prd.d.ts.map +1 -0
  163. package/dist/hooks/ralph/prd.js +310 -0
  164. package/dist/hooks/ralph/prd.js.map +1 -0
  165. package/dist/hooks/ralph/progress.d.ts +102 -0
  166. package/dist/hooks/ralph/progress.d.ts.map +1 -0
  167. package/dist/hooks/ralph/progress.js +408 -0
  168. package/dist/hooks/ralph/progress.js.map +1 -0
  169. package/dist/hooks/ralph/verifier.d.ts +72 -0
  170. package/dist/hooks/ralph/verifier.d.ts.map +1 -0
  171. package/dist/hooks/ralph/verifier.js +223 -0
  172. package/dist/hooks/ralph/verifier.js.map +1 -0
  173. package/dist/hooks/recovery/constants.d.ts +114 -0
  174. package/dist/hooks/recovery/constants.d.ts.map +1 -0
  175. package/dist/hooks/recovery/constants.js +227 -0
  176. package/dist/hooks/recovery/constants.js.map +1 -0
  177. package/dist/hooks/recovery/context-window.d.ts +24 -0
  178. package/dist/hooks/recovery/context-window.d.ts.map +1 -0
  179. package/dist/hooks/recovery/context-window.js +321 -0
  180. package/dist/hooks/recovery/context-window.js.map +1 -0
  181. package/dist/hooks/recovery/edit-error.d.ts +24 -0
  182. package/dist/hooks/recovery/edit-error.d.ts.map +1 -0
  183. package/dist/hooks/recovery/edit-error.js +56 -0
  184. package/dist/hooks/recovery/edit-error.js.map +1 -0
  185. package/dist/hooks/recovery/index.d.ts +102 -0
  186. package/dist/hooks/recovery/index.d.ts.map +1 -0
  187. package/dist/hooks/recovery/index.js +166 -0
  188. package/dist/hooks/recovery/index.js.map +1 -0
  189. package/dist/hooks/recovery/session-recovery.d.ts +24 -0
  190. package/dist/hooks/recovery/session-recovery.d.ts.map +1 -0
  191. package/dist/hooks/recovery/session-recovery.js +281 -0
  192. package/dist/hooks/recovery/session-recovery.js.map +1 -0
  193. package/dist/hooks/recovery/storage.d.ts +75 -0
  194. package/dist/hooks/recovery/storage.d.ts.map +1 -0
  195. package/dist/hooks/recovery/storage.js +382 -0
  196. package/dist/hooks/recovery/storage.js.map +1 -0
  197. package/dist/hooks/recovery/types.d.ts +196 -0
  198. package/dist/hooks/recovery/types.d.ts.map +1 -0
  199. package/dist/hooks/recovery/types.js +32 -0
  200. package/dist/hooks/recovery/types.js.map +1 -0
  201. package/dist/hooks/ultrapilot/index.d.ts +90 -0
  202. package/dist/hooks/ultrapilot/index.d.ts.map +1 -0
  203. package/dist/hooks/ultrapilot/index.js +310 -0
  204. package/dist/hooks/ultrapilot/index.js.map +1 -0
  205. package/dist/hooks/ultrapilot/state.d.ts +72 -0
  206. package/dist/hooks/ultrapilot/state.d.ts.map +1 -0
  207. package/dist/hooks/ultrapilot/state.js +258 -0
  208. package/dist/hooks/ultrapilot/state.js.map +1 -0
  209. package/dist/hooks/ultrapilot/types.d.ts +113 -0
  210. package/dist/hooks/ultrapilot/types.d.ts.map +1 -0
  211. package/dist/hooks/ultrapilot/types.js +36 -0
  212. package/dist/hooks/ultrapilot/types.js.map +1 -0
  213. package/dist/hooks/ultraqa/index.d.ts +94 -0
  214. package/dist/hooks/ultraqa/index.d.ts.map +1 -0
  215. package/dist/hooks/ultraqa/index.js +216 -0
  216. package/dist/hooks/ultraqa/index.js.map +1 -0
  217. package/dist/hooks/ultrawork/index.d.ts +62 -0
  218. package/dist/hooks/ultrawork/index.d.ts.map +1 -0
  219. package/dist/hooks/ultrawork/index.js +208 -0
  220. package/dist/hooks/ultrawork/index.js.map +1 -0
  221. package/dist/index.d.ts.map +1 -1
  222. package/dist/index.js +1 -2
  223. package/dist/index.js.map +1 -1
  224. package/dist/installer/index.d.ts +1 -1
  225. package/dist/installer/index.d.ts.map +1 -1
  226. package/dist/installer/index.js +1 -1
  227. package/dist/installer/index.js.map +1 -1
  228. package/dist/mcp/index.d.ts +1 -1
  229. package/dist/mcp/index.d.ts.map +1 -1
  230. package/dist/mcp/index.js +1 -1
  231. package/dist/mcp/index.js.map +1 -1
  232. package/dist/mcp/servers.d.ts +2 -18
  233. package/dist/mcp/servers.d.ts.map +1 -1
  234. package/dist/mcp/servers.js +4 -36
  235. package/dist/mcp/servers.js.map +1 -1
  236. package/dist/shared/types.d.ts +1 -3
  237. package/dist/shared/types.d.ts.map +1 -1
  238. package/docs/CLAUDE.md +170 -44
  239. package/docs/DELEGATION-ENFORCER.md +282 -0
  240. package/docs/FULL-README.md +1 -1
  241. package/docs/LOCAL_PLUGIN_INSTALL.md +1 -1
  242. package/docs/MIGRATION.md +298 -104
  243. package/package.json +2 -2
  244. package/scripts/test-mutual-exclusion.ts +1 -1
  245. package/skills/analyze/SKILL.md +0 -1
  246. package/skills/autopilot/SKILL.md +0 -1
  247. package/skills/cancel/SKILL.md +580 -0
  248. package/skills/cancel-autopilot/SKILL.md +0 -1
  249. package/skills/cancel-ecomode/SKILL.md +41 -0
  250. package/skills/cancel-ralph/SKILL.md +0 -1
  251. package/skills/cancel-ultraqa/SKILL.md +0 -1
  252. package/skills/cancel-ultrawork/SKILL.md +0 -1
  253. package/skills/deepinit/SKILL.md +0 -1
  254. package/skills/deepsearch/SKILL.md +0 -1
  255. package/skills/doctor/SKILL.md +0 -1
  256. package/skills/ecomode/SKILL.md +128 -0
  257. package/skills/frontend-ui-ux/SKILL.md +0 -1
  258. package/skills/git-master/SKILL.md +0 -1
  259. package/skills/help/SKILL.md +1 -2
  260. package/skills/hud/SKILL.md +0 -1
  261. package/skills/learner/SKILL.md +0 -1
  262. package/skills/mcp-setup/SKILL.md +8 -7
  263. package/skills/note/SKILL.md +0 -1
  264. package/skills/omc-default/SKILL.md +0 -1
  265. package/skills/omc-default-global/SKILL.md +0 -1
  266. package/skills/omc-setup/SKILL.md +176 -10
  267. package/skills/orchestrate/SKILL.md +0 -1
  268. package/skills/pipeline/SKILL.md +417 -0
  269. package/skills/plan/SKILL.md +138 -21
  270. package/skills/planner/SKILL.md +0 -1
  271. package/skills/ralph/SKILL.md +65 -1
  272. package/skills/ralph-init/SKILL.md +0 -1
  273. package/skills/ralplan/SKILL.md +0 -1
  274. package/skills/release/SKILL.md +0 -1
  275. package/skills/research/SKILL.md +0 -1
  276. package/skills/review/SKILL.md +0 -1
  277. package/skills/swarm/SKILL.md +349 -0
  278. package/skills/tdd/SKILL.md +0 -1
  279. package/skills/ultrapilot/SKILL.md +563 -0
  280. package/skills/ultraqa/SKILL.md +0 -1
  281. package/skills/ultrawork/SKILL.md +0 -1
  282. package/docs/MIGRATION-v3.md +0 -186
@@ -0,0 +1,321 @@
1
+ /**
2
+ * Context Window Limit Recovery
3
+ *
4
+ * Detects context window limit errors and injects recovery messages
5
+ * to help Claude recover gracefully.
6
+ */
7
+ import * as fs from 'fs';
8
+ import { TOKEN_LIMIT_PATTERNS, TOKEN_LIMIT_KEYWORDS, CONTEXT_LIMIT_RECOVERY_MESSAGE, CONTEXT_LIMIT_SHORT_MESSAGE, NON_EMPTY_CONTENT_RECOVERY_MESSAGE, RECOVERY_FAILED_MESSAGE, DEBUG, DEBUG_FILE, } from './constants.js';
9
+ import { RETRY_CONFIG } from './types.js';
10
+ function debugLog(...args) {
11
+ if (DEBUG) {
12
+ const msg = `[${new Date().toISOString()}] [context-window-recovery] ${args
13
+ .map((a) => typeof a === 'object' ? JSON.stringify(a, null, 2) : String(a))
14
+ .join(' ')}\n`;
15
+ fs.appendFileSync(DEBUG_FILE, msg);
16
+ }
17
+ }
18
+ const sessionStates = new Map();
19
+ const STATE_TTL = 300_000; // 5 minutes
20
+ /**
21
+ * Patterns indicating thinking block structure errors (NOT token limit)
22
+ */
23
+ const THINKING_BLOCK_ERROR_PATTERNS = [
24
+ /thinking.*first block/i,
25
+ /first block.*thinking/i,
26
+ /must.*start.*thinking/i,
27
+ /thinking.*redacted_thinking/i,
28
+ /expected.*thinking.*found/i,
29
+ /thinking.*disabled.*cannot.*contain/i,
30
+ ];
31
+ /**
32
+ * Check if error is a thinking block structure error
33
+ */
34
+ function isThinkingBlockError(text) {
35
+ return THINKING_BLOCK_ERROR_PATTERNS.some((pattern) => pattern.test(text));
36
+ }
37
+ /**
38
+ * Check if text indicates a token limit error
39
+ */
40
+ function isTokenLimitError(text) {
41
+ if (isThinkingBlockError(text)) {
42
+ return false;
43
+ }
44
+ const lower = text.toLowerCase();
45
+ return TOKEN_LIMIT_KEYWORDS.some((kw) => lower.includes(kw.toLowerCase()));
46
+ }
47
+ /**
48
+ * Extract token counts from error message
49
+ */
50
+ function extractTokensFromMessage(message) {
51
+ for (const pattern of TOKEN_LIMIT_PATTERNS) {
52
+ const match = message.match(pattern);
53
+ if (match) {
54
+ const num1 = parseInt(match[1], 10);
55
+ const num2 = parseInt(match[2], 10);
56
+ return num1 > num2
57
+ ? { current: num1, max: num2 }
58
+ : { current: num2, max: num1 };
59
+ }
60
+ }
61
+ return null;
62
+ }
63
+ /**
64
+ * Extract message index from error text
65
+ */
66
+ function extractMessageIndex(text) {
67
+ const match = text.match(/messages\.(\d+)/);
68
+ if (match) {
69
+ return parseInt(match[1], 10);
70
+ }
71
+ return undefined;
72
+ }
73
+ /**
74
+ * Parse an error to detect if it's a token limit error
75
+ */
76
+ export function parseTokenLimitError(err) {
77
+ // Handle string errors
78
+ if (typeof err === 'string') {
79
+ if (err.toLowerCase().includes('non-empty content')) {
80
+ return {
81
+ currentTokens: 0,
82
+ maxTokens: 0,
83
+ errorType: 'non-empty content',
84
+ messageIndex: extractMessageIndex(err),
85
+ };
86
+ }
87
+ if (isTokenLimitError(err)) {
88
+ const tokens = extractTokensFromMessage(err);
89
+ return {
90
+ currentTokens: tokens?.current ?? 0,
91
+ maxTokens: tokens?.max ?? 0,
92
+ errorType: 'token_limit_exceeded_string',
93
+ };
94
+ }
95
+ return null;
96
+ }
97
+ // Handle non-object errors
98
+ if (!err || typeof err !== 'object')
99
+ return null;
100
+ const errObj = err;
101
+ // Collect all text sources from the error object
102
+ const textSources = [];
103
+ const dataObj = errObj.data;
104
+ const responseBody = dataObj?.responseBody;
105
+ const errorMessage = errObj.message;
106
+ const errorData = errObj.error;
107
+ const nestedError = errorData?.error;
108
+ if (typeof responseBody === 'string')
109
+ textSources.push(responseBody);
110
+ if (typeof errorMessage === 'string')
111
+ textSources.push(errorMessage);
112
+ if (typeof errorData?.message === 'string')
113
+ textSources.push(errorData.message);
114
+ if (typeof errObj.body === 'string')
115
+ textSources.push(errObj.body);
116
+ if (typeof errObj.details === 'string')
117
+ textSources.push(errObj.details);
118
+ if (typeof errObj.reason === 'string')
119
+ textSources.push(errObj.reason);
120
+ if (typeof errObj.description === 'string')
121
+ textSources.push(errObj.description);
122
+ if (typeof nestedError?.message === 'string')
123
+ textSources.push(nestedError.message);
124
+ if (typeof dataObj?.message === 'string')
125
+ textSources.push(dataObj.message);
126
+ if (typeof dataObj?.error === 'string')
127
+ textSources.push(dataObj.error);
128
+ // Try JSON stringification if no text sources found
129
+ if (textSources.length === 0) {
130
+ try {
131
+ const jsonStr = JSON.stringify(errObj);
132
+ if (isTokenLimitError(jsonStr)) {
133
+ textSources.push(jsonStr);
134
+ }
135
+ }
136
+ catch {
137
+ // Ignore JSON errors
138
+ }
139
+ }
140
+ const combinedText = textSources.join(' ');
141
+ if (!isTokenLimitError(combinedText))
142
+ return null;
143
+ // Try to parse structured response body
144
+ if (typeof responseBody === 'string') {
145
+ try {
146
+ const jsonPatterns = [
147
+ /data:\s*(\{[\s\S]*\})\s*$/m,
148
+ /(\{"type"\s*:\s*"error"[\s\S]*\})/,
149
+ /(\{[\s\S]*"error"[\s\S]*\})/,
150
+ ];
151
+ for (const pattern of jsonPatterns) {
152
+ const dataMatch = responseBody.match(pattern);
153
+ if (dataMatch) {
154
+ try {
155
+ const jsonData = JSON.parse(dataMatch[1]);
156
+ const message = jsonData.error?.message || '';
157
+ const tokens = extractTokensFromMessage(message);
158
+ if (tokens) {
159
+ return {
160
+ currentTokens: tokens.current,
161
+ maxTokens: tokens.max,
162
+ requestId: jsonData.request_id,
163
+ errorType: jsonData.error?.type || 'token_limit_exceeded',
164
+ };
165
+ }
166
+ }
167
+ catch {
168
+ // Ignore parse errors
169
+ }
170
+ }
171
+ }
172
+ // Check for Bedrock-style errors
173
+ const bedrockJson = JSON.parse(responseBody);
174
+ if (typeof bedrockJson.message === 'string' &&
175
+ isTokenLimitError(bedrockJson.message)) {
176
+ return {
177
+ currentTokens: 0,
178
+ maxTokens: 0,
179
+ errorType: 'bedrock_input_too_long',
180
+ };
181
+ }
182
+ }
183
+ catch {
184
+ // Ignore parse errors
185
+ }
186
+ }
187
+ // Extract tokens from any text source
188
+ for (const text of textSources) {
189
+ const tokens = extractTokensFromMessage(text);
190
+ if (tokens) {
191
+ return {
192
+ currentTokens: tokens.current,
193
+ maxTokens: tokens.max,
194
+ errorType: 'token_limit_exceeded',
195
+ };
196
+ }
197
+ }
198
+ // Check for non-empty content error
199
+ if (combinedText.toLowerCase().includes('non-empty content')) {
200
+ return {
201
+ currentTokens: 0,
202
+ maxTokens: 0,
203
+ errorType: 'non-empty content',
204
+ messageIndex: extractMessageIndex(combinedText),
205
+ };
206
+ }
207
+ // Generic token limit error
208
+ if (isTokenLimitError(combinedText)) {
209
+ return {
210
+ currentTokens: 0,
211
+ maxTokens: 0,
212
+ errorType: 'token_limit_exceeded_unknown',
213
+ };
214
+ }
215
+ return null;
216
+ }
217
+ /**
218
+ * Check if text contains a context limit error
219
+ */
220
+ export function containsTokenLimitError(text) {
221
+ return isTokenLimitError(text);
222
+ }
223
+ /**
224
+ * Get or create session state
225
+ */
226
+ function getSessionState(sessionId) {
227
+ let state = sessionStates.get(sessionId);
228
+ const now = Date.now();
229
+ // Reset stale state
230
+ if (state && now - state.lastErrorTime > STATE_TTL) {
231
+ state = undefined;
232
+ }
233
+ if (!state) {
234
+ state = {
235
+ retryState: { attempt: 0, lastAttemptTime: 0 },
236
+ truncateState: { truncateAttempt: 0 },
237
+ lastErrorTime: now,
238
+ errorCount: 0,
239
+ };
240
+ sessionStates.set(sessionId, state);
241
+ }
242
+ return state;
243
+ }
244
+ /**
245
+ * Generate appropriate recovery message based on error and state
246
+ */
247
+ function generateRecoveryMessage(parsed, state, config) {
248
+ // Use custom message if provided
249
+ if (config?.customMessages?.context_window_limit) {
250
+ return {
251
+ message: config.customMessages.context_window_limit,
252
+ errorType: parsed?.errorType,
253
+ };
254
+ }
255
+ // Handle non-empty content error
256
+ if (parsed?.errorType?.includes('non-empty content')) {
257
+ return {
258
+ message: NON_EMPTY_CONTENT_RECOVERY_MESSAGE,
259
+ errorType: 'non-empty content',
260
+ };
261
+ }
262
+ // Check retry limits
263
+ state.retryState.attempt++;
264
+ state.retryState.lastAttemptTime = Date.now();
265
+ if (state.retryState.attempt > RETRY_CONFIG.maxAttempts) {
266
+ return {
267
+ message: RECOVERY_FAILED_MESSAGE,
268
+ errorType: 'recovery_exhausted',
269
+ };
270
+ }
271
+ // Return detailed or short message based on config
272
+ if (config?.detailed !== false) {
273
+ let message = CONTEXT_LIMIT_RECOVERY_MESSAGE;
274
+ // Add token info if available
275
+ if (parsed?.currentTokens && parsed?.maxTokens) {
276
+ message += `\nToken Details:
277
+ - Current: ${parsed.currentTokens.toLocaleString()} tokens
278
+ - Maximum: ${parsed.maxTokens.toLocaleString()} tokens
279
+ - Over limit by: ${(parsed.currentTokens - parsed.maxTokens).toLocaleString()} tokens
280
+ `;
281
+ }
282
+ return {
283
+ message,
284
+ errorType: parsed?.errorType || 'token_limit_exceeded',
285
+ };
286
+ }
287
+ return {
288
+ message: CONTEXT_LIMIT_SHORT_MESSAGE,
289
+ errorType: parsed?.errorType || 'token_limit_exceeded',
290
+ };
291
+ }
292
+ /**
293
+ * Handle context window limit recovery
294
+ */
295
+ export function handleContextWindowRecovery(sessionId, error, config) {
296
+ const parsed = parseTokenLimitError(error);
297
+ if (!parsed) {
298
+ return {
299
+ attempted: false,
300
+ success: false,
301
+ };
302
+ }
303
+ debugLog('detected token limit error', { sessionId, parsed });
304
+ const state = getSessionState(sessionId);
305
+ state.lastErrorTime = Date.now();
306
+ state.errorCount++;
307
+ const recovery = generateRecoveryMessage(parsed, state, config);
308
+ return {
309
+ attempted: true,
310
+ success: !!recovery.message,
311
+ message: recovery.message,
312
+ errorType: recovery.errorType,
313
+ };
314
+ }
315
+ /**
316
+ * Check if text contains a context limit error
317
+ */
318
+ export function detectContextLimitError(text) {
319
+ return containsTokenLimitError(text);
320
+ }
321
+ //# sourceMappingURL=context-window.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../../src/hooks/recovery/context-window.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,8BAA8B,EAC9B,2BAA2B,EAC3B,kCAAkC,EAClC,uBAAuB,EACvB,KAAK,EACL,UAAU,GACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAS1C,SAAS,QAAQ,CAAC,GAAG,IAAe;IAClC,IAAI,KAAK,EAAE,CAAC;QACV,MAAM,GAAG,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,+BAA+B,IAAI;aACxE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CACT,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAC/D;aACA,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;QACjB,EAAE,CAAC,cAAc,CAAC,UAAU,EAAE,GAAG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AC;IAED,QAAQ,CAAC,4BAA4B,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;IAE9D,MAAM,KAAK,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IACzC,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACjC,KAAK,CAAC,UAAU,EAAE,CAAC;IAEnB,MAAM,QAAQ,GAAG,uBAAuB,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAEhE,OAAO;QACL,SAAS,EAAE,IAAI;QACf,OAAO,EAAE,CAAC,CAAC,QAAQ,CAAC,OAAO;QAC3B,OAAO,EAAE,QAAQ,CAAC,OAAO;QACzB,SAAS,EAAE,QAAQ,CAAC,SAAS;KAC9B,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAY;IAClD,OAAO,uBAAuB,CAAC,IAAI,CAAC,CAAC;AACvC,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Edit Error Recovery
3
+ *
4
+ * Detects Edit tool errors caused by AI mistakes and injects
5
+ * a recovery reminder to guide corrective action.
6
+ */
7
+ import type { RecoveryResult } from './types.js';
8
+ /**
9
+ * Check if an output contains an edit error pattern
10
+ */
11
+ export declare function detectEditError(output: string): boolean;
12
+ /**
13
+ * Inject the edit error recovery reminder into the output
14
+ */
15
+ export declare function injectEditErrorRecovery(output: string): string;
16
+ /**
17
+ * Handle edit error recovery
18
+ */
19
+ export declare function handleEditErrorRecovery(toolName: string, output: string): RecoveryResult;
20
+ /**
21
+ * Process edit tool output and inject recovery if needed.
22
+ */
23
+ export declare function processEditOutput(toolName: string, output: string): string;
24
+ //# sourceMappingURL=edit-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit-error.d.ts","sourceRoot":"","sources":["../../../src/hooks/recovery/edit-error.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAKvD;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAK9D;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,cAAc,CAqBhB;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAK1E"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Edit Error Recovery
3
+ *
4
+ * Detects Edit tool errors caused by AI mistakes and injects
5
+ * a recovery reminder to guide corrective action.
6
+ */
7
+ import { EDIT_ERROR_PATTERNS, EDIT_ERROR_REMINDER, } from './constants.js';
8
+ /**
9
+ * Check if an output contains an edit error pattern
10
+ */
11
+ export function detectEditError(output) {
12
+ const outputLower = output.toLowerCase();
13
+ return EDIT_ERROR_PATTERNS.some((pattern) => outputLower.includes(pattern.toLowerCase()));
14
+ }
15
+ /**
16
+ * Inject the edit error recovery reminder into the output
17
+ */
18
+ export function injectEditErrorRecovery(output) {
19
+ if (detectEditError(output)) {
20
+ return output + EDIT_ERROR_REMINDER;
21
+ }
22
+ return output;
23
+ }
24
+ /**
25
+ * Handle edit error recovery
26
+ */
27
+ export function handleEditErrorRecovery(toolName, output) {
28
+ if (toolName.toLowerCase() !== 'edit') {
29
+ return {
30
+ attempted: false,
31
+ success: false,
32
+ };
33
+ }
34
+ if (detectEditError(output)) {
35
+ return {
36
+ attempted: true,
37
+ success: true,
38
+ message: EDIT_ERROR_REMINDER,
39
+ errorType: 'edit_error',
40
+ };
41
+ }
42
+ return {
43
+ attempted: false,
44
+ success: false,
45
+ };
46
+ }
47
+ /**
48
+ * Process edit tool output and inject recovery if needed.
49
+ */
50
+ export function processEditOutput(toolName, output) {
51
+ if (toolName.toLowerCase() !== 'edit') {
52
+ return output;
53
+ }
54
+ return injectEditErrorRecovery(output);
55
+ }
56
+ //# sourceMappingURL=edit-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit-error.js","sourceRoot":"","sources":["../../../src/hooks/recovery/edit-error.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AAGxB;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAc;IAC5C,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;IACzC,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAC1C,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAC5C,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAAc;IACpD,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,MAAM,GAAG,mBAAmB,CAAC;IACtC,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CACrC,QAAgB,EAChB,MAAc;IAEd,IAAI,QAAQ,CAAC,WAAW,EAAE,KAAK,MAAM,EAAE,CAAC;QACtC,OAAO;YACL,SAAS,EAAE,KAAK;YAChB,OAAO,EAAE,KAAK;SACf,CAAC;IACJ,CAAC;IAED,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO;YACL,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,IAAI;YACb,OAAO,EAAE,mBAAmB;YAC5B,SAAS,EAAE,YAAY;SACxB,CAAC;IACJ,CAAC;IAED,OAAO;QACL,SAAS,EAAE,KAAK;QAChB,OAAO,EAAE,KAAK;KACf,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAAgB,EAAE,MAAc;IAChE,IAAI,QAAQ,CAAC,WAAW,EAAE,KAAK,MAAM,EAAE,CAAC;QACtC,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,OAAO,uBAAuB,CAAC,MAAM,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Unified Recovery Module
3
+ *
4
+ * Consolidates all recovery mechanisms into a single, coordinated system.
5
+ * Handles context window limits, edit errors, and session recovery.
6
+ *
7
+ * Recovery Priority (checked in order):
8
+ * 1. Context Window Limit - Most critical, blocks all progress
9
+ * 2. Edit Errors - Immediate user feedback needed
10
+ * 3. Session Recovery - Structural errors that need fixing
11
+ */
12
+ export type { RecoveryErrorType, RecoveryResult, RecoveryConfig, ParsedTokenLimitError, RetryState, TruncateState, MessageData, StoredMessageMeta, StoredPart, StoredTextPart, StoredToolPart, StoredReasoningPart, } from './types.js';
13
+ export { RETRY_CONFIG, TRUNCATE_CONFIG } from './types.js';
14
+ export { CONTEXT_LIMIT_RECOVERY_MESSAGE, CONTEXT_LIMIT_SHORT_MESSAGE, NON_EMPTY_CONTENT_RECOVERY_MESSAGE, TRUNCATION_APPLIED_MESSAGE, RECOVERY_FAILED_MESSAGE, TOKEN_LIMIT_PATTERNS, TOKEN_LIMIT_KEYWORDS, EDIT_ERROR_PATTERNS, EDIT_ERROR_REMINDER, RECOVERY_MESSAGES, PLACEHOLDER_TEXT, } from './constants.js';
15
+ export { readMessages, readParts, findEmptyMessages, findMessagesWithThinkingBlocks, findMessagesWithOrphanThinking, injectTextPart, prependThinkingPart, stripThinkingParts, replaceEmptyTextParts, } from './storage.js';
16
+ export { handleContextWindowRecovery, detectContextLimitError, parseTokenLimitError, containsTokenLimitError, } from './context-window.js';
17
+ export { handleEditErrorRecovery, detectEditError, processEditOutput, } from './edit-error.js';
18
+ export { handleSessionRecovery, detectErrorType as detectSessionErrorType, isRecoverableError, } from './session-recovery.js';
19
+ import type { RecoveryResult, RecoveryConfig, MessageData } from './types.js';
20
+ /**
21
+ * Unified recovery handler
22
+ *
23
+ * Attempts recovery in priority order:
24
+ * 1. Context Window Limit (most critical)
25
+ * 2. Session Recovery (structural errors)
26
+ * 3. Edit Errors (handled during tool execution)
27
+ *
28
+ * @param input Recovery input
29
+ * @returns Recovery result
30
+ */
31
+ export declare function handleRecovery(input: {
32
+ sessionId: string;
33
+ error?: unknown;
34
+ toolName?: string;
35
+ toolOutput?: string;
36
+ message?: MessageData;
37
+ config?: RecoveryConfig;
38
+ }): Promise<RecoveryResult>;
39
+ /**
40
+ * Detect if an error is recoverable
41
+ *
42
+ * Checks all recovery mechanisms to see if the error can be handled.
43
+ */
44
+ export declare function detectRecoverableError(error: unknown): {
45
+ recoverable: boolean;
46
+ type?: string;
47
+ };
48
+ /**
49
+ * Detect if output contains an edit error
50
+ */
51
+ export declare function detectEditErrorInOutput(output: string): boolean;
52
+ /**
53
+ * Create unified recovery hook for Claude Code
54
+ *
55
+ * This hook provides a single entry point for all recovery mechanisms.
56
+ */
57
+ export declare function createRecoveryHook(config?: RecoveryConfig): {
58
+ /**
59
+ * Check for errors during tool execution or message processing
60
+ */
61
+ onError: (input: {
62
+ session_id: string;
63
+ error: unknown;
64
+ message?: MessageData;
65
+ }) => Promise<RecoveryResult>;
66
+ /**
67
+ * Post-tool execution hook for edit error recovery
68
+ */
69
+ afterToolExecute: (input: {
70
+ tool: string;
71
+ output: string;
72
+ sessionId: string;
73
+ }) => {
74
+ output: string;
75
+ recovery?: RecoveryResult;
76
+ };
77
+ /**
78
+ * Check if an error is recoverable
79
+ */
80
+ isRecoverable: (error: unknown) => boolean;
81
+ /**
82
+ * Get recovery type for an error
83
+ */
84
+ getRecoveryType: (error: unknown) => string | undefined;
85
+ };
86
+ /**
87
+ * Parse context limit error for detailed information
88
+ */
89
+ export declare function parseContextLimitError(error: unknown): import("./types.js").ParsedTokenLimitError | null;
90
+ /**
91
+ * Detect if text contains a context limit error
92
+ */
93
+ export declare function detectContextLimitErrorInText(text: string): boolean;
94
+ /**
95
+ * Detect if text contains an edit error
96
+ */
97
+ export declare function detectEditErrorInText(text: string): boolean;
98
+ /**
99
+ * Check if session error is recoverable
100
+ */
101
+ export declare function isSessionRecoverable(error: unknown): boolean;
102
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/recovery/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAoBH,YAAY,EACV,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,qBAAqB,EACrB,UAAU,EACV,aAAa,EACb,WAAW,EACX,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,cAAc,EACd,mBAAmB,GACpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,EAC3B,kCAAkC,EAClC,0BAA0B,EAC1B,uBAAuB,EACvB,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,8BAA8B,EAC9B,8BAA8B,EAC9B,cAAc,EACd,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,2BAA2B,EAC3B,uBAAuB,EACvB,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,qBAAqB,EACrB,eAAe,IAAI,sBAAsB,EACzC,kBAAkB,GACnB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,Y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