prjct-cli 0.10.0 → 0.10.2
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.
- package/CHANGELOG.md +31 -0
- package/core/__tests__/agentic/memory-system.test.js +263 -0
- package/core/__tests__/agentic/plan-mode.test.js +336 -0
- package/core/agentic/chain-of-thought.js +578 -0
- package/core/agentic/command-executor.js +238 -4
- package/core/agentic/context-builder.js +208 -8
- package/core/agentic/ground-truth.js +591 -0
- package/core/agentic/loop-detector.js +406 -0
- package/core/agentic/memory-system.js +850 -0
- package/core/agentic/parallel-tools.js +366 -0
- package/core/agentic/plan-mode.js +572 -0
- package/core/agentic/prompt-builder.js +76 -1
- package/core/agentic/response-templates.js +290 -0
- package/core/agentic/semantic-compression.js +517 -0
- package/core/agentic/think-blocks.js +657 -0
- package/core/agentic/tool-registry.js +32 -0
- package/core/agentic/validation-rules.js +380 -0
- package/core/command-registry.js +48 -0
- package/core/commands.js +43 -1
- package/core/context-sync.js +183 -0
- package/package.json +7 -15
- package/templates/commands/done.md +7 -0
- package/templates/commands/feature.md +8 -0
- package/templates/commands/ship.md +8 -0
- package/templates/commands/spec.md +128 -0
- package/templates/global/CLAUDE.md +17 -0
- package/core/__tests__/agentic/agent-router.test.js +0 -398
- package/core/__tests__/agentic/command-executor.test.js +0 -223
- package/core/__tests__/agentic/context-builder.test.js +0 -160
- package/core/__tests__/agentic/context-filter.test.js +0 -494
- package/core/__tests__/agentic/prompt-builder.test.js +0 -204
- package/core/__tests__/agentic/template-loader.test.js +0 -164
- package/core/__tests__/agentic/tool-registry.test.js +0 -243
- package/core/__tests__/domain/agent-generator.test.js +0 -289
- package/core/__tests__/domain/agent-loader.test.js +0 -179
- package/core/__tests__/domain/analyzer.test.js +0 -324
- package/core/__tests__/infrastructure/author-detector.test.js +0 -103
- package/core/__tests__/infrastructure/config-manager.test.js +0 -454
- package/core/__tests__/infrastructure/path-manager.test.js +0 -412
- package/core/__tests__/setup.test.js +0 -15
- package/core/__tests__/utils/date-helper.test.js +0 -169
- package/core/__tests__/utils/file-helper.test.js +0 -258
- package/core/__tests__/utils/jsonl-helper.test.js +0 -387
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loop Detection & User Escalation
|
|
3
|
+
*
|
|
4
|
+
* Detects when commands are failing repeatedly and escalates to user
|
|
5
|
+
* instead of continuing in infinite loops.
|
|
6
|
+
*
|
|
7
|
+
* OPTIMIZATION (P2.4): Loop Detection
|
|
8
|
+
* - Track attempt counts per command/error type
|
|
9
|
+
* - Auto-escalate after 3 failed attempts
|
|
10
|
+
* - Provide specific help based on error patterns
|
|
11
|
+
*
|
|
12
|
+
* ANTI-HALLUCINATION: Detects contradictory/impossible outputs
|
|
13
|
+
* - Patterns that indicate Claude is hallucinating (saying file exists when it doesn't)
|
|
14
|
+
* - Contradictory statements in same response
|
|
15
|
+
* - Completing tasks that were never started
|
|
16
|
+
*
|
|
17
|
+
* Source: Augment Code pattern
|
|
18
|
+
* "If you notice yourself going around in circles... ask the user for help"
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* ANTI-HALLUCINATION: Patterns that indicate Claude may be hallucinating
|
|
23
|
+
* These patterns detect contradictory or impossible statements
|
|
24
|
+
*/
|
|
25
|
+
const HALLUCINATION_PATTERNS = [
|
|
26
|
+
// Contradictory file operations
|
|
27
|
+
{ pattern: /file.*not found.*created/i, type: 'contradiction', description: 'Claims file created but also not found' },
|
|
28
|
+
{ pattern: /created.*but.*error/i, type: 'contradiction', description: 'Claims success but also error' },
|
|
29
|
+
{ pattern: /successfully.*failed/i, type: 'contradiction', description: 'Contradictory success/failure' },
|
|
30
|
+
|
|
31
|
+
// Impossible task states
|
|
32
|
+
{ pattern: /already.*completed.*completing/i, type: 'state', description: 'Completing already-completed task' },
|
|
33
|
+
{ pattern: /no task.*marking complete/i, type: 'state', description: 'Completing non-existent task' },
|
|
34
|
+
{ pattern: /no.*active.*done with/i, type: 'state', description: 'Finishing task that doesnt exist' },
|
|
35
|
+
|
|
36
|
+
// Invented data
|
|
37
|
+
{ pattern: /version.*updated.*no package/i, type: 'invented', description: 'Version update without package.json' },
|
|
38
|
+
{ pattern: /committed.*nothing to commit/i, type: 'invented', description: 'Commit without changes' },
|
|
39
|
+
{ pattern: /pushed.*no remote/i, type: 'invented', description: 'Push without remote' }
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
class LoopDetector {
|
|
43
|
+
constructor() {
|
|
44
|
+
// Track attempts per command session
|
|
45
|
+
this._attempts = new Map()
|
|
46
|
+
|
|
47
|
+
// Track error patterns
|
|
48
|
+
this._errorPatterns = new Map()
|
|
49
|
+
|
|
50
|
+
// Configuration
|
|
51
|
+
this.maxAttempts = 3
|
|
52
|
+
this.sessionTimeout = 5 * 60 * 1000 // 5 minutes
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generate a unique key for tracking attempts
|
|
57
|
+
* @param {string} command - Command name
|
|
58
|
+
* @param {string} context - Additional context (e.g., file path, task)
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
_getKey(command, context = '') {
|
|
62
|
+
return `${command}:${context}`.toLowerCase()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Record an attempt for a command
|
|
67
|
+
* @param {string} command - Command name
|
|
68
|
+
* @param {string} context - Additional context
|
|
69
|
+
* @param {Object} result - Result of the attempt
|
|
70
|
+
* @returns {Object} Attempt tracking info
|
|
71
|
+
*/
|
|
72
|
+
recordAttempt(command, context = '', result = {}) {
|
|
73
|
+
const key = this._getKey(command, context)
|
|
74
|
+
const now = Date.now()
|
|
75
|
+
|
|
76
|
+
// Get or create attempt record
|
|
77
|
+
let record = this._attempts.get(key)
|
|
78
|
+
|
|
79
|
+
if (!record || (now - record.lastAttempt) > this.sessionTimeout) {
|
|
80
|
+
// New session or timed out
|
|
81
|
+
record = {
|
|
82
|
+
command,
|
|
83
|
+
context,
|
|
84
|
+
attempts: 0,
|
|
85
|
+
errors: [],
|
|
86
|
+
firstAttempt: now,
|
|
87
|
+
lastAttempt: now,
|
|
88
|
+
success: false
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Update record
|
|
93
|
+
record.attempts++
|
|
94
|
+
record.lastAttempt = now
|
|
95
|
+
record.success = result.success || false
|
|
96
|
+
|
|
97
|
+
if (result.error) {
|
|
98
|
+
record.errors.push({
|
|
99
|
+
message: result.error,
|
|
100
|
+
timestamp: now
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this._attempts.set(key, record)
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
attemptNumber: record.attempts,
|
|
108
|
+
isLooping: this.isLooping(command, context),
|
|
109
|
+
shouldEscalate: this.shouldEscalate(command, context)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Check if a command is in a loop (repeated failures)
|
|
115
|
+
* @param {string} command - Command name
|
|
116
|
+
* @param {string} context - Additional context
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
isLooping(command, context = '') {
|
|
120
|
+
const key = this._getKey(command, context)
|
|
121
|
+
const record = this._attempts.get(key)
|
|
122
|
+
|
|
123
|
+
if (!record) return false
|
|
124
|
+
|
|
125
|
+
// Check if multiple failures with same error
|
|
126
|
+
if (record.attempts >= 2 && !record.success) {
|
|
127
|
+
const recentErrors = record.errors.slice(-3)
|
|
128
|
+
if (recentErrors.length >= 2) {
|
|
129
|
+
// Check if errors are similar
|
|
130
|
+
const firstError = recentErrors[0]?.message || ''
|
|
131
|
+
const sameError = recentErrors.every(e =>
|
|
132
|
+
this._isSimilarError(e.message, firstError)
|
|
133
|
+
)
|
|
134
|
+
return sameError
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Check if we should escalate to user
|
|
143
|
+
* @param {string} command - Command name
|
|
144
|
+
* @param {string} context - Additional context
|
|
145
|
+
* @returns {boolean}
|
|
146
|
+
*/
|
|
147
|
+
shouldEscalate(command, context = '') {
|
|
148
|
+
const key = this._getKey(command, context)
|
|
149
|
+
const record = this._attempts.get(key)
|
|
150
|
+
|
|
151
|
+
if (!record) return false
|
|
152
|
+
|
|
153
|
+
// Escalate after max attempts without success
|
|
154
|
+
return record.attempts >= this.maxAttempts && !record.success
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get escalation message for user
|
|
159
|
+
* @param {string} command - Command name
|
|
160
|
+
* @param {string} context - Additional context
|
|
161
|
+
* @returns {Object} Escalation info
|
|
162
|
+
*/
|
|
163
|
+
getEscalationInfo(command, context = '') {
|
|
164
|
+
const key = this._getKey(command, context)
|
|
165
|
+
const record = this._attempts.get(key)
|
|
166
|
+
|
|
167
|
+
if (!record) {
|
|
168
|
+
return null
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Analyze error pattern
|
|
172
|
+
const errorPattern = this._analyzeErrorPattern(record.errors)
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
status: 'BLOCKED',
|
|
176
|
+
command,
|
|
177
|
+
context,
|
|
178
|
+
attempts: record.attempts,
|
|
179
|
+
duration: record.lastAttempt - record.firstAttempt,
|
|
180
|
+
errorPattern,
|
|
181
|
+
message: this._generateEscalationMessage(command, errorPattern),
|
|
182
|
+
suggestion: this._generateSuggestion(command, errorPattern),
|
|
183
|
+
lastError: record.errors[record.errors.length - 1]?.message || null
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Check if two errors are similar
|
|
189
|
+
* @private
|
|
190
|
+
*/
|
|
191
|
+
_isSimilarError(error1, error2) {
|
|
192
|
+
if (!error1 || !error2) return false
|
|
193
|
+
|
|
194
|
+
// Normalize errors
|
|
195
|
+
const normalize = (e) => e.toLowerCase()
|
|
196
|
+
.replace(/[0-9]+/g, 'N') // Replace numbers
|
|
197
|
+
.replace(/['"`]/g, '') // Remove quotes
|
|
198
|
+
.replace(/\s+/g, ' ') // Normalize whitespace
|
|
199
|
+
.trim()
|
|
200
|
+
|
|
201
|
+
return normalize(error1) === normalize(error2)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Analyze error pattern from history
|
|
206
|
+
* @private
|
|
207
|
+
*/
|
|
208
|
+
_analyzeErrorPattern(errors) {
|
|
209
|
+
if (!errors || errors.length === 0) {
|
|
210
|
+
return { type: 'unknown', description: 'No error information' }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const lastError = errors[errors.length - 1]?.message?.toLowerCase() || ''
|
|
214
|
+
|
|
215
|
+
// Detect common patterns (ORDER MATTERS - more specific patterns first)
|
|
216
|
+
if (lastError.includes('permission') || lastError.includes('access denied')) {
|
|
217
|
+
return { type: 'permission', description: 'File or directory permission issue' }
|
|
218
|
+
}
|
|
219
|
+
if (lastError.includes('not found') || lastError.includes('no such file')) {
|
|
220
|
+
return { type: 'not_found', description: 'File or resource not found' }
|
|
221
|
+
}
|
|
222
|
+
if (lastError.includes('syntax') || lastError.includes('parse')) {
|
|
223
|
+
return { type: 'syntax', description: 'Syntax or parsing error' }
|
|
224
|
+
}
|
|
225
|
+
if (lastError.includes('timeout') || lastError.includes('timed out')) {
|
|
226
|
+
return { type: 'timeout', description: 'Operation timed out' }
|
|
227
|
+
}
|
|
228
|
+
if (lastError.includes('network') || lastError.includes('connection')) {
|
|
229
|
+
return { type: 'network', description: 'Network or connection issue' }
|
|
230
|
+
}
|
|
231
|
+
// Config pattern MUST be checked before validation (since "invalid config" contains both)
|
|
232
|
+
if (lastError.includes('config') || lastError.includes('configuration')) {
|
|
233
|
+
return { type: 'config', description: 'Configuration issue' }
|
|
234
|
+
}
|
|
235
|
+
if (lastError.includes('validation') || lastError.includes('invalid')) {
|
|
236
|
+
return { type: 'validation', description: 'Validation failed' }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return { type: 'unknown', description: 'Unrecognized error pattern' }
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Generate user-friendly escalation message
|
|
244
|
+
* @private
|
|
245
|
+
*/
|
|
246
|
+
_generateEscalationMessage(command, errorPattern) {
|
|
247
|
+
const messages = {
|
|
248
|
+
permission: `I've tried ${command} ${this.maxAttempts} times but keep hitting permission issues.`,
|
|
249
|
+
not_found: `After ${this.maxAttempts} attempts, I still can't find the required file or resource.`,
|
|
250
|
+
syntax: `I'm encountering repeated syntax errors with ${command}.`,
|
|
251
|
+
timeout: `The operation keeps timing out after ${this.maxAttempts} attempts.`,
|
|
252
|
+
network: `Network issues are preventing ${command} from completing.`,
|
|
253
|
+
validation: `Validation keeps failing for ${command}.`,
|
|
254
|
+
config: `There seems to be a configuration issue affecting ${command}.`,
|
|
255
|
+
unknown: `I've tried ${command} ${this.maxAttempts} times without success.`
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return messages[errorPattern.type] || messages.unknown
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Generate actionable suggestion based on error pattern
|
|
263
|
+
* @private
|
|
264
|
+
*/
|
|
265
|
+
_generateSuggestion(command, errorPattern) {
|
|
266
|
+
const suggestions = {
|
|
267
|
+
permission: 'Check file permissions. Try: chmod -R u+w ~/.prjct-cli/',
|
|
268
|
+
not_found: 'Verify the file path exists. Run /p:init if project not initialized.',
|
|
269
|
+
syntax: 'Check the file format. There may be invalid JSON or markdown.',
|
|
270
|
+
timeout: 'Check your network connection or try again in a moment.',
|
|
271
|
+
network: 'Verify internet connection and try again.',
|
|
272
|
+
validation: 'Review the input parameters and try with different values.',
|
|
273
|
+
config: 'Check .prjct/prjct.config.json for issues. Try /p:init to reinitialize.',
|
|
274
|
+
unknown: 'Can you check the issue manually and provide more context?'
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return suggestions[errorPattern.type] || suggestions.unknown
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Mark a command as successful (resets tracking)
|
|
282
|
+
* @param {string} command - Command name
|
|
283
|
+
* @param {string} context - Additional context
|
|
284
|
+
*/
|
|
285
|
+
recordSuccess(command, context = '') {
|
|
286
|
+
const key = this._getKey(command, context)
|
|
287
|
+
const record = this._attempts.get(key)
|
|
288
|
+
|
|
289
|
+
if (record) {
|
|
290
|
+
record.success = true
|
|
291
|
+
record.attempts = 0
|
|
292
|
+
record.errors = []
|
|
293
|
+
this._attempts.set(key, record)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Clear all tracking for a command
|
|
299
|
+
* @param {string} command - Command name
|
|
300
|
+
* @param {string} context - Additional context
|
|
301
|
+
*/
|
|
302
|
+
clearTracking(command, context = '') {
|
|
303
|
+
const key = this._getKey(command, context)
|
|
304
|
+
this._attempts.delete(key)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Clear all tracking data
|
|
309
|
+
*/
|
|
310
|
+
clearAll() {
|
|
311
|
+
this._attempts.clear()
|
|
312
|
+
this._errorPatterns.clear()
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Get statistics for debugging
|
|
317
|
+
* @returns {Object}
|
|
318
|
+
*/
|
|
319
|
+
getStats() {
|
|
320
|
+
const stats = {
|
|
321
|
+
activeTracking: this._attempts.size,
|
|
322
|
+
commands: {}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
for (const [key, record] of this._attempts) {
|
|
326
|
+
stats.commands[key] = {
|
|
327
|
+
attempts: record.attempts,
|
|
328
|
+
success: record.success,
|
|
329
|
+
errorCount: record.errors.length
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return stats
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* ANTI-HALLUCINATION: Detect potential hallucination patterns in output
|
|
338
|
+
*
|
|
339
|
+
* @param {string} output - The output text to analyze
|
|
340
|
+
* @returns {Object} Detection result
|
|
341
|
+
*/
|
|
342
|
+
detectHallucination(output) {
|
|
343
|
+
if (!output || typeof output !== 'string') {
|
|
344
|
+
return { detected: false }
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
for (const { pattern, type, description } of HALLUCINATION_PATTERNS) {
|
|
348
|
+
if (pattern.test(output)) {
|
|
349
|
+
return {
|
|
350
|
+
detected: true,
|
|
351
|
+
type,
|
|
352
|
+
pattern: pattern.source,
|
|
353
|
+
description,
|
|
354
|
+
message: `Potential hallucination detected: ${description}`,
|
|
355
|
+
suggestion: this._getHallucinationSuggestion(type)
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return { detected: false }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Get suggestion for handling detected hallucination
|
|
365
|
+
* @private
|
|
366
|
+
*/
|
|
367
|
+
_getHallucinationSuggestion(type) {
|
|
368
|
+
const suggestions = {
|
|
369
|
+
contradiction: 'Verify file/resource state before reporting. Use Read tool to check actual state.',
|
|
370
|
+
state: 'Check current task state from now.md before assuming completion.',
|
|
371
|
+
invented: 'Verify prerequisites exist (package.json, git remote) before claiming actions.'
|
|
372
|
+
}
|
|
373
|
+
return suggestions[type] || 'Verify actual state before proceeding.'
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Analyze output and record if hallucination detected
|
|
378
|
+
* @param {string} command - Command name
|
|
379
|
+
* @param {string} output - Command output
|
|
380
|
+
* @returns {Object} Analysis result
|
|
381
|
+
*/
|
|
382
|
+
analyzeOutput(command, output) {
|
|
383
|
+
const hallucination = this.detectHallucination(output)
|
|
384
|
+
|
|
385
|
+
if (hallucination.detected) {
|
|
386
|
+
// Record as a special type of error
|
|
387
|
+
this.recordAttempt(command, 'hallucination', {
|
|
388
|
+
success: false,
|
|
389
|
+
error: `HALLUCINATION: ${hallucination.description}`
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
return {
|
|
393
|
+
...hallucination,
|
|
394
|
+
shouldBlock: true,
|
|
395
|
+
action: 'VERIFY_STATE'
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return { detected: false, shouldBlock: false }
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Singleton instance
|
|
404
|
+
const loopDetector = new LoopDetector()
|
|
405
|
+
|
|
406
|
+
module.exports = loopDetector
|