Skip to content

osgrep proposal and idle detection for cli injection#127

Merged
khaliqgant merged 8 commits intomainfrom
claude/review-llm-tldr-ohh7E
Jan 10, 2026
Merged

osgrep proposal and idle detection for cli injection#127
khaliqgant merged 8 commits intomainfrom
claude/review-llm-tldr-ohh7E

Conversation

@khaliqgant
Copy link
Copy Markdown
Member

@khaliqgant khaliqgant commented Jan 10, 2026

Summary

  • Add UniversalIdleDetector for robust cross-CLI agent idle detection
  • Move idle detection to BaseWrapper for proper inheritance
  • Add proposals for code analysis tool integration (osgrep)

Implementation

Universal Idle Detection

Hybrid detection approach that works across Claude, Codex, Gemini, Aider, and any CLI:

  • Process state inspection via /proc/{pid}/stat (Linux, 95% confidence)
  • Output silence analysis (cross-platform, 60-80% confidence)
  • Natural ending detection for prompts/sentences (60% confidence)

Proper Inheritance

  • Idle detector lives in BaseWrapper, not duplicated in subclasses
  • Helper methods: setIdleDetectorPid(), feedIdleDetectorOutput(), checkIdleForInjection()
  • Added .claude/rules/wrapper-inheritance.md to enforce pattern

Proposals

  • osgrep integration for semantic code search
  • Agent retrospective questions for tooling decisions

Test plan

  • 25 new idle detector tests pass
  • All 1156 existing tests pass
  • Build succeeds

Detailed technical proposal for integrating llm-tldr code analysis
with agent-relay via direct Unix socket communication.

Key design decisions:
- Service as first-class entity type in relay protocol
- TldrBridge component for protocol translation
- Request-response pattern with timeouts
- Agent-facing ->relay:tldr syntax
- Continuity integration for startup context enrichment
- Caching layer with file watcher invalidation

Includes architecture diagrams, implementation phases, and
performance targets (<150ms round-trip).
Addresses the ecosystem mismatch problem:
- agent-relay: TypeScript/Node.js/npm
- llm-tldr: Python/pip with 2GB ML dependencies

Analyzes 6 options with pros/cons:
1. User responsibility (manual install)
2. Subprocess management (pip from Node)
3. Docker sidecar (recommended for prod)
4. npm wrapper package (fragile)
5. Cloud-hosted service (adds latency)
6. Native TypeScript port (long-term)

Proposes tiered strategy:
- Tier 1: Detection only (MVP)
- Tier 2: Docker integration
- Tier 3: Managed installation
- Tier 4: Evaluate native port
osgrep Integration (docs/proposals/OSGREP-INTEGRATION.md):
- TypeScript-native semantic code search via HTTP API
- 150MB footprint vs llm-tldr's 2GB Python stack
- ->relay:code pattern for agent queries
- Startup context injection for auto-discovery
- Defers structural analysis pending agent feedback

Agent Retrospectives (docs/AGENT-RETROSPECTIVE-QUESTIONS.md):
- Question bank for post-task agent reviews
- Specific questions to validate call graph need
- Decision criteria: >50% demand → build, <20% → defer
- Aggregation patterns for weekly tracking

Lead Agent Update (.claude/agents/lead.md):
- Added retrospective section with quick retro template
- Key questions table for tracking tooling needs
- Links to full question bank
Addresses unreliable startup context injection where agents
often ignore or never receive continuity data.

Key problems identified:
- Arbitrary 3s delay doesn't match agent readiness
- Idle check (1500ms) never satisfied during agent generation
- No feedback loop to confirm receipt
- Silent failure leaves agents unaware

Proposed solution:
1. Readiness detection - wait for actual prompt/ready signal
2. Priority queue with retry/backoff strategy
3. Acknowledgment loop - agent confirms receipt
4. File fallback - write to CLAUDE.md if injection fails
5. Notification fallback - tell agent context is available

Includes metrics, testing strategy, and migration path.
Replaces pattern-based readiness detection with hybrid approach:

1. Process state inspection (Linux):
   - Check /proc/{pid}/stat for sleep state
   - Check /proc/{pid}/wchan for input wait channels
   - 95% confidence when process blocked on read

2. Output silence analysis (cross-platform fallback):
   - Track time since last output
   - Confidence scales with silence duration

3. Natural ending detection:
   - Detect complete thoughts vs mid-sentence
   - Check for sentence endings, code blocks, prompts

Combines signals with confidence scoring. Works across all CLI
types (Claude, Codex, Gemini, Aider, Droid, etc.) without
requiring CLI-specific pattern matching.
Implements universal idle detection for all CLI-based agents using a
hybrid approach:

1. Process state inspection via /proc/{pid}/stat and /proc/{pid}/wchan
   on Linux (95% confidence) - detects when process is waiting for input

2. Output silence analysis (cross-platform, 60-80% confidence) - tracks
   time since last output

3. Natural ending detection (heuristic, 60% confidence) - checks if
   output ends with prompt/sentence vs mid-thought

This replaces the simple time-based idle check in TmuxWrapper with
multi-signal detection that works across Claude, Codex, Gemini, Aider,
and any other CLI tool.

Key components:
- src/wrapper/idle-detector.ts: UniversalIdleDetector class with
  process state inspection and output analysis
- getTmuxPanePid(): Helper to get process PID from tmux session
- Integration with TmuxWrapper.checkForInjectionOpportunity()

The detector is initialized with the tmux pane PID on session start
and receives output chunks during polling for accurate idle detection.

Addresses context injection timing issues by providing reliable
detection of when agents are ready for input.
Previously, the UniversalIdleDetector was added separately to both
TmuxWrapper and PtyWrapper, violating the DRY principle. This refactors
the implementation to follow proper inheritance patterns:

Changes:
- Move idleDetector property and initialization to BaseWrapper
- Add protected helper methods: setIdleDetectorPid(),
  feedIdleDetectorOutput(), checkIdleForInjection(), waitForIdleState()
- Update TmuxWrapper and PtyWrapper to use inherited methods
- Remove duplicate idleDetector declarations from subclasses
- Add idleBeforeInjectMs and idleConfidenceThreshold to BaseWrapperConfig

Also adds .claude/rules/wrapper-inheritance.md to enforce this pattern
for future development - any shared functionality between wrappers
should be added to BaseWrapper first.

This ensures consistent behavior across all wrapper types and prevents
code divergence.
@khaliqgant khaliqgant changed the title docs: Add llm-tldr direct socket integration proposal osgrep proposal and idle detection for cli injection Jan 10, 2026
Spawned agents were missing their initial task because the wrappers
used fixed delays (2-3 seconds) instead of detecting when the CLI
was actually ready for input.

Changes:
- PtyWrapper: Replace 2-second setTimeout with waitForAgentReady()
  that uses idle detection (waitForIdleState)
- TmuxWrapper: Replace 3-second setTimeout with waitForAgentReady()
  that uses idle detection

The new approach:
1. Waits minimum 500ms for CLI process to start
2. Uses UniversalIdleDetector to detect when CLI is idle
3. Times out after 10 seconds with fallback behavior

This fixes the issue where spawn tasks were sent before the agent
was ready to receive them, causing messages to be lost.
@khaliqgant khaliqgant merged commit cba72df into main Jan 10, 2026
6 checks passed
@khaliqgant khaliqgant deleted the claude/review-llm-tldr-ohh7E branch January 10, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants