Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions .grok/PR-COMMAND-PALETTE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
## Summary

Adds interactive command palette with autocomplete, keyboard navigation, fuzzy matching, and "did you mean" suggestions.

**Problem**: Typing `/` in Grok TUI shows nothing - users must already know command names. No autocomplete, no command discovery, no typo assistance.

**Solution**: Interactive command palette that appears when typing `/`, with smart filtering and keyboard navigation.

## Features

### 1. Live Command Filtering

Type `/` to see all commands, then filter as you type:

```
> /
┌─ Commands (7 matches) ─┐
▶ /auth - Authentication management
/clear - Clear conversation
/exit - Exit application
/help - Show help
/history - Show conversation history
/model - Set AI model
/prompt - Load prompt from file
└─────────────────────────┘
Tab: autocomplete • ↑/↓: navigate • Esc: close • Enter: submit
```

### 2. Smart Matching

**Prefix match**:
```
> /pr
┌─ Commands (1 match) ─┐
▶ /prompt - Load prompt from file
└───────────────────────┘
```

**Alias match**:
```
> /pf
┌─ Commands (1 match) ─┐
▶ /prompt (via pf) - Load prompt from file
└────────────────────────────────────────┘
```

**Fuzzy match** (typo tolerance):
```
> /promt
┌─ Commands (1 match) ─┐
▶ /prompt - Load prompt from file
└───────────────────────┘
```

### 3. Keyboard Navigation

- **↑/↓**: Navigate suggestions (only when palette visible)
- **Tab**: Autocomplete to selected command + add space
- **Esc**: Close palette
- **Enter**: Submit command as usual

### 4. "Did You Mean" for Unknown Commands

```
> /promt myfile.txt
[Enter]

Error: Unknown command: /promt

Did you mean: /prompt?

Use /help to see all available commands.
```

## Implementation

### Phase 1: Suggestion Algorithm (`src/ui/utils/command-suggestions.ts`)

**Levenshtein Distance**:
- Calculates edit distance for fuzzy matching
- Tolerates up to 2 character edits
- No external dependencies (30 lines, pure algorithm)

**Matching Types** (ranked):
1. **Exact** (1000 points): `/prompt` → `prompt`
2. **Prefix** (900 points): `/pr` → `prompt`
3. **Alias Exact** (800 points): `/pf` → `prompt`
4. **Alias Prefix** (700 points): `/promptf` → `prompt` (via `promptfile`)
5. **Substring** (600 points): `/odel` → `model`
6. **Fuzzy** (500-distance*100): `/promt` → `prompt` (1 edit = 400 points)

### Phase 2: UI Component (`src/ui/components/command-palette.tsx`)

- Cyan bordered box (consistent with tool selection)
- Selected item: inverse text + bold + arrow indicator
- Shows matched alias when applicable
- Keyboard hint at bottom

### Phase 3: Input Integration (`src/ui/components/input.tsx`)

- Computes suggestions in real-time (`useMemo`)
- Shows palette when `value.startsWith('/')` and not pasting
- Arrow keys captured when palette visible (priority over tool navigation)
- Tab autocompletes and preserves any args already typed
- Escape closes palette without clearing input

### Phase 4: Error Enhancement (`src/commands/index.ts`)

- Unknown command errors now include fuzzy suggestion
- Uses `getDidYouMeanSuggestion()` for best match
- Shows alias if matched via alias

## Compatibility

**No conflicts with existing features**:
- ✅ Bracketed paste: Palette hidden during `isPasting`
- ✅ Tool navigation: Only captures arrows when palette visible (tool nav when `isActive=false`)
- ✅ Multi-line input: Palette respects multi-line display
- ✅ Command execution: Unchanged behavior

## Test Coverage

**New Test File**: `tests/unit/command-suggestions.test.ts` (25 tests)

- Levenshtein distance correctness (7 tests)
- Suggestion matching (exact, prefix, alias, substring, fuzzy) (12 tests)
- Ranking and sorting (2 tests)
- "Did you mean" logic (4 tests)

**Test Results**: 238/238 pass (+25 from 213)

## Verification

```bash
✅ npm run build - TypeScript compilation clean
✅ npm test - 238/238 tests pass (+25 new tests)
✅ No conflicts with paste/navigation modes
```

## Manual Testing

After merge, verify in TUI:

1. **Discovery**: Type `/` → see all 7 commands
2. **Filter**: Type `/pr` → see `/prompt`
3. **Alias**: Type `/pf` → matches `/prompt (via pf)`
4. **Fuzzy**: Type `/promt` → matches `/prompt`
5. **Navigate**: Press ↓/↑ → selection moves
6. **Autocomplete**: Press Tab → completes to `/prompt `
7. **Typo Help**: Submit `/promt` → error shows "Did you mean: /prompt?"

---

🤖 Generated with [Claude Code](https://claude.com/claude-code)
98 changes: 98 additions & 0 deletions .grok/PR-EXPLORE-QUALITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
## Summary

Fixes explore subagent output quality - it now reliably finds code evidence using token-based searches instead of failing with phrase-based searches.

**Problem**: When asked to find code like "where taskSuccesses increments", the explore subagent searched for English phrases ("successful Task", "count successful") instead of code tokens (`taskSuccesses`, `call.tool === 'Task'`), resulting in "no matches found."

**Solution**: Enhanced explore.md system prompt with explicit operating rules for code evidence gathering.

## Root Cause

**Current behavior** (broken):
```
User: "Find where taskSuccesses increments"
Subagent: Grep: "count successful" → no matches
```

**Expected behavior** (fixed):
```
User: "Find where taskSuccesses increments"
Subagent: Grep: "taskSuccesses" → finds definition + uses
Subagent: Grep: "taskSuccesses.*\+=" → finds increment line
Subagent: Quotes file:line:content
```

## Changes

**File**: `.grok/agents/explore.md`

### Added Operating Rules

**1. Token-Based Code Searches** (CRITICAL)
- Use code tokens/identifiers: `taskSuccesses`, `call.tool`, `runSubagent`
- NOT English phrases: "successful Task", "count successful"
- Includes examples: variable names, function names, class names, operators

**2. File:Line Evidence Requirements**
- Use Grep with identifiers
- Include actual grep output (file:line:content)
- Provide exact line numbers and exact code
- Read file if grep output is long

**3. Search Scope Preferences**
- **Prefer `src/**` over `tests/**`** unless tests explicitly requested
- Use patterns: `Glob: "src/**/*.ts"`, `Grep: "pattern" --glob "src/**"`

**4. Follow-Up Strategy**
- When finding a function: do second Grep for key identifiers
- Search same file for related tokens
- Read for context if needed

**5. Quality Checklist**
- Verify token/identifier searches used
- Verify file:line:content included
- Verify src/** searched first
- Verify exact matching code quoted

## Regression Tests

**File**: `tests/unit/truthfulness.test.ts` (+5 tests)

Tests verify explore.md system prompt contains:
- ✅ Token/identifier search guidance
- ✅ File:line evidence requirements
- ✅ `src/**` preference over `tests/**`
- ✅ Follow-up strategy for function findings
- ✅ Quality checklist

## Impact

**Before** (phrase search fails):
```
User: "Find where taskSuccesses increments"
=== SUBAGENT TRACE ===
[1] ✓ Grep: "successful Task"
No matches found
```

**After** (token search succeeds):
```
User: "Find where taskSuccesses increments"
=== SUBAGENT TRACE ===
[1] ✓ Grep: "taskSuccesses"
src/agent/grok-agent.ts:106: taskSuccesses += 1;
[2] ✓ Read: src/agent/grok-agent.ts
[context around line 106]
```

## Verification

```bash
✅ npm run build - TypeScript compilation clean
✅ npm test - 209/209 tests pass (+5 new tests)
✅ explore.md contains all required guidance strings
```

---

🤖 Generated with [Claude Code](https://claude.com/claude-code)
98 changes: 98 additions & 0 deletions .grok/PR-EXPLORE-TOKEN-RELIABILITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
## Summary

Fixes explore subagent token search reliability by eliminating fragile operator regex patterns that miss code with whitespace.

**Problem**: explore subagent searches for `taskSuccesses++` or `taskSuccesses+=` (no whitespace), but actual code is `taskSuccesses += 1;` (with spaces), causing "no matches found."

**Root Cause**:
```
Grep: "taskSuccesses\+\+|taskSuccesses\+=" ← Misses whitespace
Actual code: taskSuccesses += 1; ← Has space before =
Result: No matches found ❌
```

**Solution**: Use simple token-only searches that find ALL uses (including increments/assignments), then quote the relevant lines.

## Changes

**File**: `.grok/agents/explore.md`

### New Critical Guidance (added to Section 1)

**For increment/assignment questions**:
- ✅ **ALWAYS start with token ONLY**: `Grep: "taskSuccesses"`
- ❌ **DO NOT use operator patterns**: `taskSuccesses++`, `taskSuccesses+=` (fragile)
- ❌ **DO NOT use regex alternation**: `\+\+|\+=` (fragile)
- ❌ **DO NOT use word boundaries**: `\b`, `\s` (fragile)

**Why this works**:
- Simple `Grep: "taskSuccesses"` finds **ALL** uses including:
- `taskSuccesses += 1;` (increment with spaces)
- `const x = taskSuccesses;` (assignment)
- `if (taskSuccesses > 0)` (condition)
- Then quote the relevant lines from grep output

## Before vs After

### Before (broken)

**Subagent**:
```
=== SUBAGENT TRACE ===
[1] ✓ Grep: "taskSuccesses\+\+|taskSuccesses\+="
No matches found
```

**User sees**: "no matches found" and thinks subagent failed

### After (fixed)

**Subagent**:
```
=== SUBAGENT TRACE ===
[1] ✓ Grep: "taskSuccesses"
src/agent/grok-agent.ts:106: taskSuccesses += 1;
src/agent/grok-agent.ts:112: const subagentsSpawned = taskSuccesses;

[2] ✓ Read: src/agent/grok-agent.ts
[context around increment...]
```

**User sees**: Actual file:line:content evidence ✅

## Regression Tests

**File**: `tests/unit/truthfulness.test.ts` (+3 tests in "Explore Subagent Prompt Quality")

Tests enforce explore.md contains:
1. ✅ "ALWAYS start with" + "token ONLY" for increments
2. ✅ "DO NOT use operator patterns" warning
3. ✅ "DO NOT use regex alternation" warning

## Impact

**Search Reliability**:
- Before: Fragile patterns miss whitespace variations
- After: Token-only search finds all uses reliably

**User Experience**:
- Before: "no matches found" (confusing)
- After: Actual `file:line:content` evidence (clear)

## Verification

```bash
✅ npm run build - TypeScript compilation clean
✅ npm test - 212/212 tests pass (+3 new tests)
✅ explore.md contains all anti-fragile guidance
```

**Manual smoke test** (after merge):
```
User: "Find where taskSuccesses increments"
Expected: Grep returns src/agent/grok-agent.ts:106: taskSuccesses += 1;
```

---

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Loading