Skip to content

feat(hooks): token optimization — dedup, auto-route, prewarm, script-suggest#14

Merged
jonathanpeterwu merged 15 commits into
mainfrom
feat/token-optimization-hooks
May 17, 2026
Merged

feat(hooks): token optimization — dedup, auto-route, prewarm, script-suggest#14
jonathanpeterwu merged 15 commits into
mainfrom
feat/token-optimization-hooks

Conversation

@jonathanpeterwu
Copy link
Copy Markdown
Collaborator

Summary

  • dedup-reads: Escalates from soft [dedup] at 3x to hard [STOP] at 5+ reads — targets 62% read waste
  • desire-path-hook: Auto-routes Bash→Glob/Read/Grep with inline systemMessage suggestions — targets 20% replaceable Bash
  • prewarm-tools: SessionStart hook analyzes action-stream, emits ToolSearch pre-fetch hint for top 8 deferred tools
  • script-suggest: Detects multi-tool patterns (3+ git cmds, gh run, WebFetch/Search) and suggests matching scripts with 5min cooldown

Token economics (from 175 sessions / 7,353 tool calls)

  • 62.4% read waste rate → dedup escalation
  • 20% Bash replaceable → auto-route
  • 106 ToolSearch calls → prewarm
  • Script usage 0.5% → script-suggest

Test plan

  • dedup-reads: verified 3x soft, 5x hard STOP
  • auto-route: ls→Glob, cat→Read, grep→Grep suggestions
  • prewarm: emits correct ToolSearch select query
  • script-suggest: git pattern → git-ops.ts with cooldown

StackMemory Bot (CLI) added 15 commits May 5, 2026 19:11
stackmemory scaffold creates company/, wiki/, skills/, clients/, raw/,
and .stackmemory/config.yml. Enables local context management with
file-based skill rot detection and tenant isolation.
New DaemonTelemetryService collects anonymous usage snapshots:
- Daemon health (uptime, context saves, memory triggers, errors)
- Session counts (total heartbeats, active now)
- Skill audit entries, handoff counts
- No PII — instance ID is random hex

Runs daily (default 24h interval, first at boot+30s).
Stores rolling 90-snapshot history in ~/.stackmemory/telemetry.json.
Opt out: STACKMEMORY_TELEMETRY=0 or telemetry.enabled: false in config.
…gest skills

Three-component system in DaemonDesirePathService:
1. ActionStreamLogger — PostToolUse hook captures tool:target pairs
   to ~/.stackmemory/desire-paths/action-stream.jsonl (no data/content)
2. PatternDetector — sliding window extracts repeated sequences,
   filters by min 3 occurrences across 2+ sessions, scores by freq×sessions
3. SkillSuggester — generates skill.md files from top patterns with
   inputs/outputs inferred from sequence endpoints

- 10MB JSONL rotation, 10K entry scan cap for performance
- Opt out: STACKMEMORY_DESIRE_PATHS=0 or desirePaths.enabled: false
- Scans every 6h, first at boot+2m
- Suggestions written to ~/.stackmemory/desire-paths/suggestions/
- 3 adversarial review rounds: fixed separator injection, added scan cap,
  improved skill naming with target directory context
- Auto-starts daemon on session boot
- Writes session heartbeats for telemetry tracking
- Restores handoff context from previous sessions
- Sets STACKMEMORY_SESSION env for desire-path hook
- Determinism watcher + tracing
- bin/hermes-sm and bin/hermes-smd registered in package.json
Centralizes token estimation across 14 files through
src/core/cache/token-estimator.ts and packages/sdk/src/token-estimator.ts.
Lazy-loads cl100k_base encoder with char/4 fallback if WASM fails.

Also ports context-budget hook to codex-sm exit handler for
compact/restart nudges matching Claude Code behavior.
Skills are often prompt text (content) not code — content licenses like
CC-BY-4.0 fit better than MIT for these. Adds KnownLicenseSchema enum
with both code (MIT, Apache-2.0, ISC, BSD) and content (CC-BY-4.0,
CC-BY-SA-4.0, CC0-1.0) licenses while keeping the field open for custom
SPDX identifiers.
Markdown table parser + CLI commands + MCP tools for local-first task
steering. Tasks live in master-tasks.md, optionally sync to Linear/GH.

- Parser: parse/serialize/update/add/getNext for pipe-delimited md tables
- CLI: stackmemory tasks init/md list/md next/md add/md update
- MCP: get_next_master_task, update_master_task, create_master_task
- 19 tests covering parse, round-trip, priority sorting, file ops
…rm, script-suggest

- dedup-reads: escalate to [STOP] at 5+ reads (was soft-only at 3+)
- desire-path-hook: auto-route Bash→Glob/Read/Grep with inline suggestions
- prewarm-tools: SessionStart hook emits top deferred tool pre-fetch hint
- script-suggest: detects multi-tool patterns matching existing scripts
Replays 7,589 action-stream entries through hook logic.
Result: 324K token savings projected (22% waste reduction).
@jonathanpeterwu jonathanpeterwu merged commit 6b45c80 into main May 17, 2026
3 of 7 checks passed
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.

1 participant