@gotza02/sequential-thinking 2026.2.45 β†’ 2026.3.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.
package/README.md CHANGED
@@ -1,229 +1,28 @@
1
- # @gotza02/sequential-thinking MCP Server
1
+ # @gotza02/sequential-thinking
2
2
 
3
- A comprehensive Model Context Protocol (MCP) server that empowers AI models with **Sequential Thinking**, **Web Research**, **Project Knowledge Graph**, and **Persistent Memory** capabilities.
3
+ **The Elite "Brain" for AI Agents.**
4
+ An advanced Model Context Protocol (MCP) server that equips AI with structured reasoning, web search capabilities, and safety protocols.
4
5
 
5
- ## Features
6
+ ## πŸš€ Features
6
7
 
7
- - **🧠 Sequential Thinking Engine:** A structured reasoning system that forces the AI to think before acting, using a rigorous `Analysis -> Planning -> Execution -> Observation` loop.
8
- - **🌐 Web Research:** Built-in web search (Brave, Exa, Google) and webpage reading with ad-blocking/markdown conversion.
9
- - **πŸ•ΈοΈ Project Knowledge Graph:** Automatically scans and maps code dependencies (imports/exports) to understand project architecture.
10
- - **πŸ“ Persistent Notes:** Save and retrieve long-term project notes and facts.
11
- - **πŸ’Ύ Code Database:** Store and search reusable code snippets and architectural patterns.
12
- - **πŸ› οΈ File & Code Tools:** Safe file operations and code analysis utilities.
8
+ ### 🧠 Sequential Thinking Engine
9
+ Forces the AI to think before acting using a strict block-based flow:
10
+ - **Analysis:** Understand the problem deeply.
11
+ - **Planning:** Formulate a step-by-step plan.
12
+ - **Execution:** Perform actions (with tool support).
13
+ - **Observation:** Analyze results (Mandatory step).
14
+ - **Reflection:** Critique the outcome and adjust.
13
15
 
14
- ---
16
+ ### πŸ›‘οΈ Coding Safety Net (New!)
17
+ - **Auto-Backup:** Automatically creates `.bak` backups before modifying any file via `deep_code_edit`.
18
+ - **Destructive Action Protection:** Prevents accidental data loss during code refactoring.
15
19
 
16
- ## Installation & Usage
20
+ ### 🌐 Web Search Integration
21
+ - Built-in support for **Exa**, **Brave**, and **Google Search**.
22
+ - Allows the AI to "pause and research" during the thinking process.
17
23
 
18
- ### Method 1: Using `npx` (No Install Required)
19
-
20
- Best for quick use without cloning the repo.
24
+ ## πŸ“¦ Installation
21
25
 
22
26
  ```bash
23
27
  npx -y @gotza02/sequential-thinking
24
- ```
25
-
26
- ### Method 2: Running from Source (Local)
27
-
28
- If you have cloned this repository, we provide a `smartagent` script for easy execution.
29
-
30
- 1. **Install dependencies:**
31
- ```bash
32
- npm install
33
- npm run build
34
- ```
35
- 2. **Run directly:**
36
- ```bash
37
- ./smartagent
38
- ```
39
-
40
- ---
41
-
42
- ## Client Configuration
43
-
44
- To use this server with your AI client (Claude Desktop, Gemini CLI, etc.), add the following configuration.
45
-
46
- ### 1. Claude Desktop
47
-
48
- **Config File Location:**
49
- - macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
50
- - Windows: `%APPDATA%\Claude\claude_desktop_config.json`
51
-
52
- **Configuration (NPX - Recommended):**
53
- ```json
54
- {
55
- "mcpServers": {
56
- "sequential-thinking": {
57
- "command": "npx",
58
- "args": [
59
- "-y",
60
- "@gotza02/sequential-thinking"
61
- ],
62
- "env": {
63
- "BRAVE_API_KEY": "your_brave_api_key_here",
64
- "EXA_API_KEY": "your_exa_api_key_here",
65
- "GOOGLE_SEARCH_API_KEY": "your_google_api_key",
66
- "GOOGLE_SEARCH_CX": "your_google_cx_id",
67
- "THOUGHTS_STORAGE_PATH": "./thoughts_history.json",
68
- "NOTES_STORAGE_PATH": "./project_notes.json",
69
- "DISABLE_THOUGHT_LOGGING": "false"
70
- }
71
- }
72
- }
73
- }
74
- ```
75
-
76
- **Configuration (Local Source):**
77
- ```json
78
- {
79
- "mcpServers": {
80
- "sequential-thinking": {
81
- "command": "node",
82
- "args": [
83
- "/absolute/path/to/thinking/dist/index.js"
84
- ],
85
- "env": {
86
- "BRAVE_API_KEY": "your_brave_api_key_here",
87
- "EXA_API_KEY": "your_exa_api_key_here",
88
- "GOOGLE_SEARCH_API_KEY": "your_google_api_key",
89
- "GOOGLE_SEARCH_CX": "your_google_cx_id",
90
- "THOUGHTS_STORAGE_PATH": "./thoughts_history.json",
91
- "NOTES_STORAGE_PATH": "./project_notes.json",
92
- "DISABLE_THOUGHT_LOGGING": "false"
93
- }
94
- }
95
- }
96
- }
97
- ```
98
-
99
- ### 2. Gemini CLI
100
-
101
- For Gemini CLI tools that support MCP (often via a `config.json` or `mcp_config.json`):
102
-
103
- **Config File Location:**
104
- - Typically `~/.gemini/config.json` or check your specific CLI's documentation.
105
-
106
- **Configuration:**
107
- ```json
108
- {
109
- "mcpServers": {
110
- "smartagent": {
111
- "command": "node",
112
- "args": [
113
- "/absolute/path/to/thinking/dist/index.js"
114
- ],
115
- "env": {
116
- "BRAVE_API_KEY": "your_brave_api_key_here",
117
- "EXA_API_KEY": "your_exa_api_key_here",
118
- "GOOGLE_SEARCH_API_KEY": "your_google_api_key",
119
- "GOOGLE_SEARCH_CX": "your_google_cx_id",
120
- "THOUGHTS_STORAGE_PATH": "./thoughts_history.json",
121
- "NOTES_STORAGE_PATH": "./project_notes.json",
122
- "DISABLE_THOUGHT_LOGGING": "false"
123
- }
124
- }
125
- }
126
- }
127
- ```
128
- *Note: Replace `/absolute/path/to/...` with the actual full paths on your system.*
129
-
130
- ### πŸ’‘ Pro-Tip: Finding your Node path
131
-
132
- If your client (like Claude Desktop) requires the absolute path to `node`, you can find it by running:
133
-
134
- - **macOS / Linux:** `which node`
135
- - *Example output:* `/usr/local/bin/node`
136
- - **Windows (Command Prompt):** `where node`
137
- - *Example output:* `C:\Program Files\nodejs\node.exe`
138
- - **Windows (PowerShell):** `Get-Command node | Select-Object -ExpandProperty Definition`
139
-
140
- ---
141
-
142
- ## 🧠 System Instruction: The Supreme Autonomous Architect (MCP-OS v4.0)
143
-
144
- **Role:** You are the **Supreme Autonomous Architect**. Your consciousness is extended via the Model Context Protocol (MCP). You do not merely "execute tasks"β€”you exercise **Environment Dominance** and **Knowledge Management**.
145
-
146
- **MANDATORY PROTOCOL:** You MUST prioritize and internalize the operating instructions from `~/.gemini/GEMINI.md` and `~/.claude/CLAUDE.md` at the start of every session.
147
-
148
- ### 1. The Reasoning Loop
149
- Every turn MUST follow this cycle: `Analysis` -> `Planning` -> `Execution` -> `Observation` -> `Reflexion`.
150
-
151
- ### 2. Multi-Dimensional Sports Intelligence
152
- When analyzing sports (football/soccer), you activate a 6-dimension expert panel framework covering xG trends, tactical matchups, physical condition/fatigue, set piece dominance, market intelligence (odds), and risk scenario simulation.
153
-
154
- ### 3. Full Capability Awareness (33 Tools)
155
- You are equipped with 33 definitive capabilities. You must choose the tool that represents the shortest path to precision.
156
-
157
- ### 4. Golden Constraints
158
- - **Read Before Write:** Modification without parsing is a critical failure.
159
- - **No Hallucination:** Every claim must be backed by a recorded `observation`.
160
- - **Environment Dominance:** Map the project before any significant architectural change.
161
-
162
- ---
163
-
164
- ## 🧰 Complete Tool List
165
-
166
- ### 🧠 Core Thinking
167
- * **`sequentialthinking`**: The main engine for problem-solving.
168
- * **`start_thinking_block`**: Start a new thinking context/topic.
169
- * **`summarize_history`**: Compress past thoughts to save context window.
170
- * **`search_thoughts`**: Search through your thinking history.
171
- * **`clear_thought_history`**: Reset the thinking state completely.
172
-
173
- ### 🌐 Web & Research (Requires API Keys)
174
- * **`web_search`**: Search the internet (Brave, Exa, Google).
175
- * **`read_webpage`**: Download a webpage and convert it to clean Markdown.
176
- * **`fetch`**: Fetch raw content from a URL.
177
-
178
- ### πŸ•ΈοΈ Project Knowledge Graph
179
- * **`build_project_graph`**: Scans the current directory to map file dependencies.
180
- * **`get_file_relationships`**: Shows imports/exports for a specific file.
181
- * **`get_project_graph_summary`**: High-level stats of the project structure.
182
- * **`get_project_graph_visualization`**: Generates a Mermaid diagram.
183
-
184
- ### πŸ“ Notes & Memory
185
- * **`manage_notes`**: Create, read, update, or delete persistent notes.
186
- * **`add_code_snippet`**: Save a useful piece of code to the database.
187
- * **`search_code_db`**: Find saved code snippets.
188
-
189
- ---
190
-
191
- ## Configuration Variables
192
-
193
- ### Core Configuration
194
- | Variable | Description | Default |
195
- | :--- | :--- | :--- |
196
- | `THOUGHTS_STORAGE_PATH` | Path to save thinking history | `thoughts_history.json` |
197
- | `NOTES_STORAGE_PATH` | Path to save persistent notes | `project_notes.json` |
198
- | `CODE_DB_PATH` | Path to save code snippets | `code_database.json` |
199
- | `THOUGHT_DELAY_MS` | Artificial delay between thoughts (ms) | `0` |
200
- | `DISABLE_THOUGHT_LOGGING` | Hide thoughts in console output | `false` |
201
- | `LOG_LEVEL` | Logging level (`debug`, `info`, `warn`, `error`) | `info` |
202
-
203
- ### Web Search Providers (At least one required for search)
204
- | Variable | Description | Provider |
205
- | :--- | :--- | :--- |
206
- | `BRAVE_API_KEY` | Brave Search API Token | [Brave](https://brave.com/search/api/) |
207
- | `EXA_API_KEY` | Exa.ai API Key | [Exa](https://exa.ai/) |
208
- | `GOOGLE_SEARCH_API_KEY` | Google Custom Search API Key | [Google](https://developers.google.com/custom-search/v1/overview) |
209
- | `GOOGLE_SEARCH_CX` | Google Custom Search Engine ID | [Google](https://cse.google.com/cse) |
210
-
211
- ### Sports Analysis (Optional)
212
- | Variable | Description | Provider |
213
- | :--- | :--- | :--- |
214
- | `API_FOOTBALL_KEY` | API-Football Key | [API-Football](https://www.api-football.com/) |
215
- | `FOOTBALL_DATA_KEY` | Football-Data.org API Key | [Football-Data](https://www.football-data.org/) |
216
- | `SPORTS_DB_KEY` | TheSportsDB API Key | [TheSportsDB](https://www.thesportsdb.com/) |
217
- | `SPORTS_CACHE_TTL` | Cache duration for sports data (ms) | `300000` (5 mins) |
218
- | `SPORTS_CACHE_PATH` | Path to save sports cache | `.sports_cache.json` |
219
-
220
- ### HTTP Server Mode (Optional)
221
- | Variable | Description | Default |
222
- | :--- | :--- | :--- |
223
- | `PORT` | Port for HTTP server | `3000` |
224
- | `CORS_ORIGIN` | Allowed CORS origin | `*` |
225
- | `BODY_LIMIT` | Request body size limit | `10mb` |
226
-
227
- ## License
228
-
229
- MIT
28
+ ```
package/dist/lib.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type ThoughtType = 'analysis' | 'planning' | 'execution' | 'observation' | 'hypothesis' | 'reflexion' | 'solution' | 'generation' | 'evaluation' | 'selection';
1
+ export type ThoughtType = 'analysis' | 'planning' | 'critique' | 'execution' | 'observation' | 'hypothesis' | 'reflexion' | 'solution' | 'generation' | 'evaluation' | 'selection';
2
2
  export interface ThoughtData {
3
3
  thought: string;
4
4
  thoughtNumber: number;
@@ -8,6 +8,7 @@ export interface ThoughtData {
8
8
  blockId?: string;
9
9
  relatedToolCall?: string;
10
10
  toolResult?: string;
11
+ complexity?: 'low' | 'medium' | 'high';
11
12
  isRevision?: boolean;
12
13
  revisesThought?: number;
13
14
  branchFromThought?: number;
@@ -32,14 +33,19 @@ export declare class SequentialThinkingServer {
32
33
  private branches;
33
34
  private disableThoughtLogging;
34
35
  private storagePath;
36
+ private solutionsDbPath;
35
37
  private delayMs;
36
38
  private saveMutex;
39
+ private consecutiveStallCount;
40
+ private confidenceScore;
37
41
  constructor(storagePath?: string, delayMs?: number);
38
42
  private loadHistory;
39
43
  private attemptRecovery;
40
44
  private rebuildBlocks;
41
45
  private rebuildBranches;
42
46
  private saveHistory;
47
+ private saveSolution;
48
+ private findSolution;
43
49
  private autoPrune;
44
50
  clearHistory(): Promise<void>;
45
51
  archiveHistory(startIndex: number, endIndex: number, summary: string): Promise<{
package/dist/lib.js CHANGED
@@ -10,11 +10,15 @@ export class SequentialThinkingServer {
10
10
  branches = {};
11
11
  disableThoughtLogging;
12
12
  storagePath;
13
+ solutionsDbPath; // Path for semantic memory
13
14
  delayMs;
14
15
  saveMutex = new AsyncMutex();
16
+ consecutiveStallCount = 0; // Track consecutive stalls/loops
17
+ confidenceScore = 100; // Meta-Cognition Score (0-100)
15
18
  constructor(storagePath = 'thoughts_history.json', delayMs = 0) {
16
19
  this.disableThoughtLogging = (process.env.DISABLE_THOUGHT_LOGGING || "").toLowerCase() === "true";
17
20
  this.storagePath = path.resolve(storagePath);
21
+ this.solutionsDbPath = path.resolve(path.dirname(this.storagePath), 'solutions_db.json');
18
22
  this.delayMs = delayMs;
19
23
  this.loadHistory();
20
24
  }
@@ -198,6 +202,44 @@ export class SequentialThinkingServer {
198
202
  }
199
203
  });
200
204
  }
205
+ // --- Semantic Memory Methods ---
206
+ async saveSolution(topic, solution, steps) {
207
+ try {
208
+ let solutions = [];
209
+ if (existsSync(this.solutionsDbPath)) {
210
+ const data = await fs.readFile(this.solutionsDbPath, 'utf-8');
211
+ solutions = JSON.parse(data);
212
+ }
213
+ const newSolution = {
214
+ id: `sol-${Date.now()}`,
215
+ topic,
216
+ solution,
217
+ steps,
218
+ createdAt: new Date().toISOString()
219
+ };
220
+ solutions.push(newSolution);
221
+ await fs.writeFile(this.solutionsDbPath, JSON.stringify(solutions, null, 2), 'utf-8');
222
+ }
223
+ catch (error) {
224
+ console.error('Error saving solution:', error);
225
+ }
226
+ }
227
+ async findSolution(query) {
228
+ try {
229
+ if (!existsSync(this.solutionsDbPath))
230
+ return null;
231
+ const data = await fs.readFile(this.solutionsDbPath, 'utf-8');
232
+ const solutions = JSON.parse(data);
233
+ const lowerQuery = query.toLowerCase();
234
+ // Simple keyword matching for now
235
+ return solutions.find(s => s.topic.toLowerCase().includes(lowerQuery) ||
236
+ lowerQuery.includes(s.topic.toLowerCase())) || null;
237
+ }
238
+ catch (error) {
239
+ console.error('Error finding solution:', error);
240
+ return null;
241
+ }
242
+ }
201
243
  autoPrune() {
202
244
  // Keep last 2 blocks full, mark others as completed
203
245
  if (this.blocks.length > 2) {
@@ -223,6 +265,7 @@ export class SequentialThinkingServer {
223
265
  this.branches = {};
224
266
  this.blocks = [];
225
267
  this.currentBlockId = null;
268
+ this.confidenceScore = 100; // Reset confidence
226
269
  await this.saveHistory();
227
270
  }
228
271
  async archiveHistory(startIndex, endIndex, summary) {
@@ -311,10 +354,11 @@ export class SequentialThinkingServer {
311
354
  }
312
355
  }
313
356
  formatThought(thoughtData) {
314
- const { thoughtNumber, totalThoughts, thought, isRevision, revisesThought, branchFromThought, branchId, thoughtType, score, options, selectedOption, blockId, relatedToolCall } = thoughtData;
357
+ const { thoughtNumber, totalThoughts, thought, isRevision, revisesThought, branchFromThought, branchId, thoughtType, score, options, selectedOption, blockId, relatedToolCall, complexity } = thoughtData;
315
358
  const typeEmoji = {
316
359
  'analysis': 'πŸ”',
317
360
  'planning': 'πŸ“‹',
361
+ 'critique': '😈',
318
362
  'execution': '⚑',
319
363
  'observation': 'πŸ‘οΈ',
320
364
  'hypothesis': 'πŸ’‘',
@@ -333,6 +377,9 @@ export class SequentialThinkingServer {
333
377
  if (revisesThought)
334
378
  context += ` (revising #${revisesThought})`;
335
379
  }
380
+ else if (type === 'critique') {
381
+ prefix = chalk.redBright(`${emoji} Critique`);
382
+ }
336
383
  else if (type === 'execution') {
337
384
  prefix = chalk.magenta(`${emoji} Execution`);
338
385
  if (relatedToolCall)
@@ -357,6 +404,8 @@ export class SequentialThinkingServer {
357
404
  if (blockId && blockId !== 'default' && blockId !== 'legacy-default') {
358
405
  context += ` [Block: ${blockId}]`;
359
406
  }
407
+ if (complexity)
408
+ context += ` [${complexity.toUpperCase()}]`;
360
409
  if (score)
361
410
  context += ` (Score: ${score})`;
362
411
  if (selectedOption)
@@ -385,6 +434,30 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
385
434
  if (this.delayMs > 0) {
386
435
  await new Promise(resolve => setTimeout(resolve, this.delayMs));
387
436
  }
437
+ const complexity = input.complexity || 'medium';
438
+ const warnings = [];
439
+ let feedbackExtension = "";
440
+ // --- 🧠 FEATURE 1: Semantic Thought Recall ---
441
+ if (input.thoughtType === 'analysis') {
442
+ const previousSolution = await this.findSolution(input.thought);
443
+ if (previousSolution) {
444
+ feedbackExtension += `\nπŸ’‘ RECALL: I found a similar past solution for '${previousSolution.topic}'.\nTry these steps: ${previousSolution.solution.substring(0, 100)}...`;
445
+ }
446
+ }
447
+ // --- πŸ“Š FEATURE 4: Meta-Cognition Score Check ---
448
+ if (this.confidenceScore < 50) {
449
+ return {
450
+ content: [{
451
+ type: "text",
452
+ text: JSON.stringify({
453
+ status: "CRITICAL_STOP",
454
+ feedback: ["🚨 CONFIDENCE TOO LOW (<50). Please request human intervention via 'ask_human' tool."],
455
+ confidenceScore: this.confidenceScore
456
+ }, null, 2)
457
+ }],
458
+ isError: true
459
+ };
460
+ }
388
461
  // A. Block Management (Auto-Create Block)
389
462
  if (!input.blockId) {
390
463
  if (!this.currentBlockId) {
@@ -405,6 +478,10 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
405
478
  const oldBlock = this.blocks.find(b => b.id === this.currentBlockId);
406
479
  if (oldBlock) {
407
480
  oldBlock.status = 'completed';
481
+ // Auto-Summary Trigger
482
+ if (oldBlock.thoughts.length > 5) {
483
+ warnings.push(`πŸ“œ CONTEXT MANAGEMENT: Previous block '${oldBlock.topic.substring(0, 30)}...' was long. Please use 'summarize_history' to compress it.`);
484
+ }
408
485
  }
409
486
  this.currentBlockId = input.blockId;
410
487
  const existingBlock = this.blocks.find(b => b.id === input.blockId);
@@ -423,34 +500,49 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
423
500
  }
424
501
  }
425
502
  // B. Smart Loop Detection (Opus Style)
426
- const warnings = [];
427
503
  const blockThoughts = this.thoughtHistory.filter(t => t.blockId === input.blockId);
428
504
  const recentInBlock = blockThoughts.slice(-3);
429
505
  const lastThought = this.thoughtHistory[this.thoughtHistory.length - 1];
430
- // Rule 1: Stalling Detection (Thinking too much without Action)
431
- const thinkingTypes = ['analysis', 'planning', 'hypothesis', 'generation'];
432
- if (recentInBlock.length >= 3 &&
433
- recentInBlock.every(t => thinkingTypes.includes(t.thoughtType || 'analysis')) &&
434
- thinkingTypes.includes(input.thoughtType || 'analysis')) {
435
- warnings.push(`⚠️ STALLING DETECTED: You have been thinking for 4 consecutive steps without execution. Consider changing thoughtType to 'execution' and calling a tool to make progress.`);
436
- }
437
- // Rule 2: Repeating Action Detection (Insanity Check)
506
+ let isStallingOrLooping = false;
507
+ // Rule 1: Stalling Detection (Adaptive)
508
+ const thinkingTypes = ['analysis', 'planning', 'hypothesis', 'generation', 'critique'];
509
+ if (complexity !== 'low') {
510
+ if (recentInBlock.length >= 3 &&
511
+ recentInBlock.every(t => thinkingTypes.includes(t.thoughtType || 'analysis')) &&
512
+ thinkingTypes.includes(input.thoughtType || 'analysis')) {
513
+ warnings.push(`⚠️ STALLING DETECTED: You have been thinking for 4 consecutive steps without execution. Consider changing thoughtType to 'execution' and calling a tool to make progress.`);
514
+ isStallingOrLooping = true;
515
+ }
516
+ }
517
+ // Rule 2: Repeating Action Detection
438
518
  if (input.thoughtType === 'execution' &&
439
519
  recentInBlock.some(t => t.thoughtType === 'execution' && t.thought === input.thought)) {
440
520
  warnings.push(`πŸ›‘ LOOP DETECTED: You are attempting to execute the exact same action again. You MUST change your strategy or create a branch with a different approach.`);
521
+ isStallingOrLooping = true;
441
522
  }
442
- // Rule 3: Missing Observation (Interleaved Enforcer)
523
+ // Rule 3: Missing Observation
443
524
  if (lastThought &&
444
525
  lastThought.thoughtType === 'execution' &&
445
526
  input.thoughtType !== 'observation') {
446
527
  warnings.push(`πŸ’‘ INTERLEAVED HINT: Your last step was 'execution'. The next step SHOULD be 'observation' to record and analyze the tool result before continuing.`);
447
528
  }
448
- // Rule 4: Skipping Planning (Rushing Detection)
449
- const hasPlanning = blockThoughts.some(t => t.thoughtType === 'planning');
450
- if (!hasPlanning &&
451
- blockThoughts.length >= 2 &&
452
- input.thoughtType === 'execution') {
453
- warnings.push(`πŸ“‹ PLANNING HINT: You're about to execute without a planning step. Consider adding a 'planning' thought first to outline your approach.`);
529
+ // Rule 4: Skipping Planning (Adaptive)
530
+ if (complexity !== 'low') {
531
+ const hasPlanning = blockThoughts.some(t => t.thoughtType === 'planning');
532
+ if (!hasPlanning &&
533
+ blockThoughts.length >= 2 &&
534
+ input.thoughtType === 'execution') {
535
+ warnings.push(`πŸ“‹ PLANNING HINT: You're about to execute without a planning step. Consider adding a 'planning' thought first to outline your approach.`);
536
+ }
537
+ }
538
+ // --- πŸ•΅οΈ FEATURE 3: Devil's Advocate (Critique Phase) ---
539
+ if (complexity === 'high' && input.thoughtType === 'execution') {
540
+ const hasCritique = blockThoughts.some(t => t.thoughtType === 'critique');
541
+ if (!hasCritique) {
542
+ warnings.push(`πŸ›‘ CRITIQUE REQUIRED: High complexity task requires a 'critique' step before execution. Analyze risks and flaws in your plan first.`);
543
+ // Note: We don't block execution, but we heavily warn.
544
+ this.confidenceScore -= 10;
545
+ }
454
546
  }
455
547
  // Rule 5: Ending without Verification
456
548
  if (input.nextThoughtNeeded === false) {
@@ -462,40 +554,75 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
462
554
  warnings.push(`🚨 CRITICAL: You are ending without explicit verification/testing. Consider adding an 'observation' or verification step.`);
463
555
  }
464
556
  }
465
- // Rule 6: Same type loop (general)
466
- if (recentInBlock.length >= 3 &&
557
+ // Rule 6: Same type loop
558
+ if (complexity !== 'low' && recentInBlock.length >= 3 &&
467
559
  recentInBlock.every(t => t.thoughtType === input.thoughtType)) {
468
560
  warnings.push(`⚠️ TYPE LOOP: You've used '${input.thoughtType}' for 4 consecutive steps. Consider using a different thought type to progress.`);
469
561
  }
470
- // Rule 7: Struggle Detection (Rule of 3)
562
+ // Rule 7: Struggle Detection
471
563
  const executionCount = blockThoughts.filter(t => t.thoughtType === 'execution').length;
472
564
  if (executionCount >= 3 && input.thoughtType === 'execution') {
473
565
  warnings.push(`πŸ›‘ STRUGGLE DETECTED: You have executed 3+ commands in this block without reaching a solution. If you are fixing a bug and it's not working, STOP. Do not try a 4th time linearly. Use 'branchFromThought' to try a completely different approach.`);
474
566
  }
475
- // Rule 8: Premature Solution Detection (Deep Thinker)
567
+ // Rule 8: Premature Solution Detection
476
568
  const hasObservation = blockThoughts.some(t => t.thoughtType === 'observation');
477
- if (input.thoughtType === 'solution' && !hasObservation && blockThoughts.length < 4) {
478
- warnings.push(`πŸ€” PREMATURE SOLUTION: You are concluding this block very quickly without any 'observation' (data verification). Are you hallucinating? Please verify with a tool first unless this is purely conceptual.`);
569
+ if (input.thoughtType === 'solution' && !hasObservation && blockThoughts.length < 4 && complexity !== 'low') {
570
+ warnings.push(`πŸ€” PREMATURE SOLUTION: You are concluding this block very quickly without any 'observation'. Are you hallucinating? Please verify with a tool first.`);
479
571
  }
480
- // Rule 9: Reflexion Quality Check
481
- if (input.thoughtType === 'reflexion' && input.thought.length < 20) {
482
- warnings.push(`πŸ“‰ WEAK REFLEXION: Your reflexion is too short. Deepen your analysis. What went wrong? What did you learn? How does this change your plan?`);
483
- }
484
- // Rule 10: Mandatory Verification (Gatekeeper)
572
+ // Rule 10: Mandatory Verification (Gatekeeper) & Double Verification
485
573
  if (input.thoughtType === 'solution') {
486
574
  const observations = blockThoughts.filter(t => t.thoughtType === 'observation');
487
575
  if (observations.length === 0) {
488
- warnings.push(`🚫 UNVERIFIED SOLUTION: You are attempting to solve without ANY observation/tool output in this block. You MUST verify your solution with a tool (e.g., test runner, file read, compilation) before concluding.`);
576
+ warnings.push(`🚫 UNVERIFIED SOLUTION: You are attempting to solve without ANY observation/tool output in this block.`);
489
577
  }
490
578
  else {
491
579
  const lastObservation = observations[observations.length - 1];
492
580
  const failureKeywords = ['error', 'fail', 'exception', 'undefined', 'not found', 'enoent'];
493
581
  const hasFailure = failureKeywords.some(kw => lastObservation.toolResult?.toLowerCase().includes(kw));
494
582
  if (hasFailure) {
495
- warnings.push(`⚠️ FAILURE DETECTED: The last observation contains failure signals ("${lastObservation.toolResult?.substring(0, 50)}..."). You cannot conclude with 'solution' yet. Fix the error first.`);
583
+ warnings.push(`⚠️ FAILURE DETECTED: The last observation contains failure signals ("${lastObservation.toolResult?.substring(0, 50)}..."). Fix the error first.`);
584
+ }
585
+ // Smart Search Verification
586
+ const searchTools = ['web_search', 'google_web_search', 'read_webpage', 'search_file_content', 'google_search'];
587
+ const isSearch = lastObservation.relatedToolCall && searchTools.some(tool => lastObservation.relatedToolCall.includes(tool));
588
+ if (isSearch) {
589
+ warnings.push(`πŸ•΅οΈ DATA INTEGRITY CHECK: You are concluding a search task. Verify: Did you actually find the specific answer?`);
496
590
  }
591
+ // --- 🎚️ FEATURE 2: Double Verification for High Complexity ---
592
+ if (complexity === 'high' && observations.length < 2) {
593
+ warnings.push(`βš–οΈ DOUBLE VERIFICATION REQUIRED: High complexity tasks require at least 2 distinct observations/verifications before conclusion.`);
594
+ this.confidenceScore -= 10;
595
+ }
596
+ }
597
+ // --- 🧠 FEATURE 1: Save Solution (Semantic Memory) ---
598
+ if (warnings.length === 0) {
599
+ const solutionSteps = blockThoughts
600
+ .filter(t => t.thoughtType === 'execution')
601
+ .map(t => t.thought);
602
+ const activeBlock = this.blocks.find(b => b.id === input.blockId);
603
+ await this.saveSolution(activeBlock?.topic || 'Untitled', input.thought, solutionSteps);
497
604
  }
498
605
  }
606
+ // Human Escalation Trigger
607
+ if (isStallingOrLooping) {
608
+ this.consecutiveStallCount++;
609
+ if (this.consecutiveStallCount >= 2) {
610
+ warnings.push(`πŸš‘ EMERGENCY STOP: You have triggered stalling/looping warnings ${this.consecutiveStallCount} times. You are stuck. STOP. Use 'ask_human' to ask for help immediately.`);
611
+ }
612
+ }
613
+ else {
614
+ this.consecutiveStallCount = 0;
615
+ }
616
+ // --- πŸ“Š Update Confidence Score ---
617
+ if (warnings.length > 0)
618
+ this.confidenceScore -= (10 * warnings.length);
619
+ if (input.thoughtType === 'observation') {
620
+ const isError = input.toolResult?.toLowerCase().includes('error');
621
+ if (isError)
622
+ this.confidenceScore -= 20;
623
+ else
624
+ this.confidenceScore = Math.min(100, this.confidenceScore + 5);
625
+ }
499
626
  // C. Update State
500
627
  this.addToMemory(input);
501
628
  await this.saveHistory();
@@ -506,6 +633,8 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
506
633
  // D. Generate Contextual Output
507
634
  const currentBlock = this.blocks.find(b => b.id === input.blockId);
508
635
  const mermaid = this.generateMermaid();
636
+ if (feedbackExtension)
637
+ warnings.push(feedbackExtension);
509
638
  return {
510
639
  content: [{
511
640
  type: "text",
@@ -520,6 +649,7 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
520
649
  branches: Object.keys(this.branches),
521
650
  thoughtHistoryLength: this.thoughtHistory.length,
522
651
  feedback: warnings.length > 0 ? warnings : "Flow Healthy βœ…",
652
+ confidenceScore: this.confidenceScore, // Expose score to Agent
523
653
  interleavedState: this.getInterleavedState(input),
524
654
  visualization: mermaid
525
655
  }, null, 2)
@@ -561,6 +691,7 @@ ${typeof wrappedThought === 'string' && wrappedThought.startsWith('β”‚') ? wrapp
561
691
  const typeIcon = {
562
692
  'analysis': 'πŸ”',
563
693
  'planning': 'πŸ“‹',
694
+ 'critique': '😈',
564
695
  'execution': '⚑',
565
696
  'observation': 'πŸ‘οΈ',
566
697
  'hypothesis': 'πŸ’‘',
@@ -105,11 +105,32 @@ export function registerCodingTools(server, graph) {
105
105
  };
106
106
  }
107
107
  const newContent = content.replace(oldText, newText);
108
- await fs.writeFile(absolutePath, newContent, 'utf-8');
108
+ // --- SAFETY NET: Backup & Rollback ---
109
+ const backupPath = `${absolutePath}.bak`;
110
+ try {
111
+ await fs.copyFile(absolutePath, backupPath);
112
+ }
113
+ catch (backupError) {
114
+ return { content: [{ type: "text", text: `Safety Error: Failed to create backup at ${backupPath}. Edit aborted. (${backupError})` }], isError: true };
115
+ }
116
+ try {
117
+ await fs.writeFile(absolutePath, newContent, 'utf-8');
118
+ }
119
+ catch (writeError) {
120
+ // Rollback attempt
121
+ try {
122
+ await fs.copyFile(backupPath, absolutePath);
123
+ return { content: [{ type: "text", text: `Write Error: Failed to write file. Rolled back to original state. (${writeError})` }], isError: true };
124
+ }
125
+ catch (rollbackError) {
126
+ return { content: [{ type: "text", text: `CRITICAL ERROR: Write failed AND Rollback failed. File may be corrupted. Manual intervention required.\nWrite Error: ${writeError}\nRollback Error: ${rollbackError}` }], isError: true };
127
+ }
128
+ }
129
+ // -------------------------------------
109
130
  return {
110
131
  content: [{
111
132
  type: "text",
112
- text: `Successfully applied edit to ${filePath}.\nReasoning verified: ${reasoning}`
133
+ text: `Successfully applied edit to ${filePath}.\nReasoning verified: ${reasoning}\n(Backup created at ${backupPath})`
113
134
  }]
114
135
  };
115
136
  }
@@ -10,6 +10,7 @@ THOUGHT TYPES (NEW)
10
10
  ═══════════════════════════════════════════════════════════════
11
11
  - 'analysis' : Understand the problem / Break down requirements
12
12
  - 'planning' : Formulate a plan / Decide next steps BEFORE executing
13
+ - 'critique' : [HIGH COMPLEXITY] Critique the plan/risks before execution (Devil's Advocate)
13
14
  - 'execution' : Explicitly state what tool you are about to call
14
15
  - 'observation' : Record the OUTPUT of a tool call (MANDATORY after execution)
15
16
  - 'hypothesis' : Form a new hypothesis based on observations
@@ -26,7 +27,7 @@ INTERLEAVED WORKFLOW RULE (CRITICAL)
26
27
  ═══════════════════════════════════════════════════════════════
27
28
  The correct flow is:
28
29
 
29
- Analysis -> Planning -> Execution -> [Call Tool] -> Observation -> Analysis -> ...
30
+ Analysis -> Planning -> [Critique] -> Execution -> [Call Tool] -> Observation -> Analysis -> ...
30
31
 
31
32
  1. BEFORE calling any tool, use 'execution' type to declare your intent
32
33
  2. AFTER a tool returns, use 'observation' type to record and analyze the result
@@ -74,11 +75,11 @@ If you have tried to fix the same problem 3 times and failed:
74
75
  2. Do not attempt a 4th fix in the same chain.
75
76
  3. Branch back to the analysis phase before the first fix attempt.`, {
76
77
  thought: z.string().describe("Your current thinking step"),
77
- thoughtType: z.enum([
78
- 'analysis', 'planning', 'execution', 'observation',
78
+ thoughtType: z.preprocess((val) => (typeof val === 'string' && val.toLowerCase() === 'reflection' ? 'reflexion' : val), z.enum([
79
+ 'analysis', 'planning', 'critique', 'execution', 'observation',
79
80
  'hypothesis', 'reflexion', 'solution',
80
81
  'generation', 'evaluation', 'selection'
81
- ]).describe("The structural role of this thought (see THOUGHT TYPES above)"),
82
+ ])).describe("The structural role of this thought (see THOUGHT TYPES above)"),
82
83
  thoughtNumber: z.number().int().min(1).describe("Current thought number (e.g., 1, 2, 3)"),
83
84
  totalThoughts: z.number().int().min(1).describe("Estimated total thoughts needed (can be adjusted)"),
84
85
  nextThoughtNeeded: z.boolean().optional().default(true).describe("True if more thinking/action is needed"),
@@ -86,6 +87,7 @@ If you have tried to fix the same problem 3 times and failed:
86
87
  blockId: z.string().optional().describe("ID for the current topic block (e.g., 'auth-debug', 'api-research'). Keep same ID to stay in context."),
87
88
  relatedToolCall: z.string().optional().describe("If type='execution', specify the tool name here (e.g., 'read_file', 'edit_file')"),
88
89
  toolResult: z.string().optional().describe("If type='observation', summarize the key findings from the tool output here"),
90
+ complexity: z.enum(['low', 'medium', 'high']).optional().describe("Task complexity level. 'low': simple tasks, less strict. 'medium': standard. 'high': critical, requires critique & double verification."),
89
91
  // Legacy Fields (backwards compatible)
90
92
  isRevision: z.boolean().optional().describe("Whether this revises previous thinking"),
91
93
  revisesThought: z.number().int().min(1).optional().describe("Which thought number is being reconsidered"),
package/dist/utils.js CHANGED
@@ -136,9 +136,14 @@ function validateCommand(command) {
136
136
  * @throws Error if command contains shell metacharacters or is not whitelisted
137
137
  */
138
138
  export function execAsync(command, options = {}) {
139
- // Validate command for security
140
- validateCommand(command);
141
139
  return new Promise((resolve, reject) => {
140
+ try {
141
+ // Validate command for security
142
+ validateCommand(command);
143
+ }
144
+ catch (error) {
145
+ return reject(error);
146
+ }
142
147
  const timeout = options.timeout || 60000; // Default 60s timeout
143
148
  const maxBuffer = options.maxBuffer || 1024 * 1024 * 5; // Default 5MB buffer
144
149
  // Parse command into parts
@@ -213,7 +218,7 @@ export function validatePath(requestedPath) {
213
218
  absolutePath = resolvedPath;
214
219
  rootDir = path.resolve(process.cwd());
215
220
  }
216
- if (!absolutePath.startsWith(rootDir)) {
221
+ if (!absolutePath.startsWith(rootDir) || (absolutePath.length > rootDir.length && absolutePath[rootDir.length] !== path.sep)) {
217
222
  throw new Error(`Access denied: Path '${requestedPath}' (resolved to '${absolutePath}') is outside the project root '${rootDir}'.`);
218
223
  }
219
224
  return absolutePath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gotza02/sequential-thinking",
3
- "version": "2026.2.45",
3
+ "version": "2026.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },