feat(hooks): token optimization — dedup, auto-route, prewarm, script-suggest#14
Merged
Conversation
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
… to 12h when idle
- 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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
[dedup]at 3x to hard[STOP]at 5+ reads — targets 62% read wasteToken economics (from 175 sessions / 7,353 tool calls)
Test plan