Skip to content

IMPLEMENTATION_SPEC.md

Adaptive Context Injection (ACI)

Initiative: Adaptive Context Injection
Authored by: Forge (Director of Product Architecture, Vivere Vitalis)
Status: Final v3 — Ready for implementation
QA Review: QA_REPORT.md (Quinn, 2026-03-21) — 5 blockers, 3 major, 3 minor — all addressed
QA Review Final: QA_REPORT_FINAL.md (Quinn, 2026-03-21) — Approved with 4 notes — all resolved (N-01 through N-04 + Jeff additional)
Design Decision: Wisdom integrated into SOUL.md (Layer 0). No separate WISDOM.md file. Final. (Jeff, 2026-03-21)
Phase: Research & Plan
Date: 2026-03-21
Classification: Architecture Spec (3-5 pages)


1. Objective

Replace OpenClaw's current bulk workspace-file injection model with a tiered, context-aware injection system that reduces per-turn token waste, improves attention quality, and scales gracefully as workspace knowledge grows — without requiring changes to OpenClaw core or external paid dependencies.

Why it matters now: Current injection is ~600 lines every turn regardless of relevance. The lost-in-the-middle problem means content buried mid-prompt gets degraded attention. As MEMORY.md, TOOLS.md, and initiative files grow, this compounds. This spec eliminates that scaling failure.


2. Scope Boundaries

IN SCOPE (v1)

  • Architecture design for all 4 injection layers
  • Session-type classifier (hook-based, no ML required at this stage)
  • Wisdom compression pipeline (batch script + manual review gate; output is wisdom section for SOUL.md)
  • Progressive context shedding mechanism (signal-based, behavioral instructions only in v1)
  • Integration via OpenClaw's agent:bootstrap hook
  • Migration path from current bulk injection with a safe fallback
  • Acceptance criteria Quinn can validate
  • Risk assessment
  • Full rollback procedures at every phase (one-command revert)

OUT OF SCOPE (explicitly deferred)

  • Vector database or semantic search infrastructure (Layer 2 retrieval uses file search, not embeddings — embeddings are planned for v2)
  • Full plugin development — v1 targets the hook system; plugin-based Layer 3 shedding is v2
  • Multi-agent session type routing (Melody, Quinn, Atlas each get their own bootstrap sets — deferred to v2)
  • Automatic wisdom compression (v1 uses a manual trigger + review gate; automation is v2)
  • Mobile app or WebChat UI changes
  • Changes to OpenClaw core source
  • Any external paid APIs or services

3. Architecture Design

The Problem With Bulk Injection (Current State)

OpenClaw injects these files every turn, regardless of session context:

AGENTS.md    ~436 tok
SOUL.md      ~228 tok
TOOLS.md     ~5,241 tok (truncated from ~13,553)
IDENTITY.md  ~53 tok
USER.md      ~97 tok
MEMORY.md    ~varies, can grow unbounded (verify whether actually bulk-injected — see Phase 0)

Total: 600-800+ lines every turn. TOOLS.md alone burns ~5K tokens because it's accumulated everything over time. AGENTS.md is 400+ lines of behavioral playbook that's only relevant in certain contexts.

The Four-Layer Model

┌─────────────────────────────────────────────────────────┐
│ LAYER 0: IDENTITY + WISDOM (~150-200 tok)               │
│ Always loaded. SOUL.md with integrated wisdom section.  │
│ Wisdom is judgment — you need it to decide what else    │
│ to load. Cannot be conditionally injected.              │
├─────────────────────────────────────────────────────────┤
│ LAYER 1: SITUATIONAL (~100-300 tok, conditional)        │
│ Loaded by session type. USER.md, AGENTS.md, TOOLS.md    │
│ excerpts, topic-specific files. Set at session start.   │
├─────────────────────────────────────────────────────────┤
│ LAYER 2: RETRIEVED (0-500 tok, on-demand)               │
│ MEMORY.md, daily notes, initiative files. Agent pulls   │
│ via read tool when topic requires it. Never bulk-loaded. │
├─────────────────────────────────────────────────────────┤
│ LAYER 3: EPHEMERAL (conversation itself)                │
│ As conversation accumulates context, shed Layer 1 files │
│ that are no longer needed. Signal-based shedding.       │
└─────────────────────────────────────────────────────────┘

Layer 0 — Identity + Wisdom (Always Loaded)

File: SOUL.md (with integrated wisdom section)
Target size: ~150-200 tokens total
Mechanism: OpenClaw natively injects SOUL.md. Wisdom stays here — always loaded.

Why wisdom is Layer 0, not Layer 1 — the bootstrap paradox:
Wisdom is judgment. You need judgment to decide what else to load. Conditionally loading wisdom creates a bootstrap paradox — the agent needs wisdom to know it needs wisdom. Wisdom shapes how you think about everything, including session classification itself. This is a hard architectural constraint, not a preference. Decision made during design review by Jeff. Final.

What stays in SOUL.md (Layer 0): - Core identity (who Jules is, creature, motto) - Decision authority tiers (solo / decide+report / always-ask) - Boundaries summary (2-3 lines) - Vibe (3-4 lines) - Wisdom section — compressed operational patterns, broadly applicable, ≤150 tokens

What moves out of SOUL.md: - Agent dynamics → AGENTS.md (Layer 1, loaded when orchestrating) - Operating rhythm → AGENTS.md - Detailed operational lessons remain in MEMORY.md (Layer 2, on-demand)

No separate WISDOM.md file exists or will be created. Wisdom lives in SOUL.md and nowhere else.

Layer 1 — Situational (Conditional Loading)

Mechanism: agent:bootstrap hook mutates context.bootstrapFiles based on session type.
Session type classification: See Section 4.

Default bulk injection → replaced with conditional sets:

Session Type Files Injected
Main DM (private chat with Jeff) SOUL.md (identity + wisdom), USER.md, TOOLS_COMPACT.md
Orchestration (running agents) SOUL.md (identity + wisdom), AGENTS.md
Group/Topic (Ideas, Ops, etc.) SOUL.md (identity + wisdom), TOOLS_COMPACT.md
Subagent SOUL.md (identity + wisdom) only
Heartbeat/Cron SOUL.md (identity + wisdom), HEARTBEAT.md

TOOLS_COMPACT.md (new file to create):
A slimmed version of TOOLS.md containing only: browser aliases, port numbers, model CLI flags, camera/TTS specifics. Strip the "why" explanations and keep just the lookup table. Target: ≤300 tokens vs. current ~5,241.

IDENTITY.md: Dropped from default injection (low value per token — it's metadata, and SOUL.md covers identity). Retained for retrieval if needed.

Layer 2 — Retrieved (On-Demand, Never Bulk-Injected)

Mechanism: Agent reads files explicitly when topic requires them.
Candidate files: MEMORY.md, memory/YYYY-MM-DD.md, initiatives/*/STATUS.md, full TOOLS.md.

OpenClaw already handles memory/*.md this way (they're not bulk-injected). MEMORY.md current injection status must be verified via /context list in Phase 0 pre-flight. If MEMORY.md is bulk-injected, it must be removed — it grows unboundedly over time, creating a compounding token tax.

Change: If MEMORY.md is bulk-injected, remove it from the bootstrap injection list. SOUL.md instructs the agent: "For long-term memory, read MEMORY.md on demand when conversation requires historical context."

Retrieval trigger signals (agent behavior, not code): - Jeff mentions something the agent should remember → read MEMORY.md - Initiative/project question → read initiatives/<name>/STATUS.md - Question about past decisions → read relevant daily note

Layer 3 — Progressive Context Shedding

V1 implementation: Behavioral instructions in SOUL.md only. No code in v1.

Why code is deferred: before_prompt_build is a plugin lifecycle hook, not a standalone hook file. It requires a full plugin with openclaw.plugin.json manifest, registration via api.on('before_prompt_build', ...) inside a register(api) function, and explicit enablement via plugins.entries.<id>.enabled. Entry cost is significantly higher than a hook drop-in (⭐⭐⭐ vs ⭐⭐). Deferred to v2.

V1 behavioral shedding instructions (to be added to SOUL.md): - After USER.md content has been acted on and conversation is established, no need to re-reference USER.md for the remainder of the session - After all orchestration tasks complete, AGENTS.md context is no longer needed - After a topic thread has established its own clear context, topic-specific file references can be dropped

V2 scope for shedding: Plugin-based detection using before_prompt_build, tracking which Layer 1 files have been "used" by checking conversation content against file content signatures.


4. Session Type Classification Logic

Mechanism: agent:bootstrap hook reads session key from event.sessionKey (top-level on event) and classifies.

OpenClaw session key format:

agent:<agentId>:<channel>:direct:<peerId>      → private DM
agent:<agentId>:<channel>:group:<id>            → group chat
agent:<agentId>:<channel>:group:<id>:topic:<n> → forum topic (Telegram)
cron:<job.id>                                   → cron/heartbeat
agent:main:subagent:<uuid>                      → subagent
agent:<agentId>:main                            → main session (CLI/default)

Classification rules (ordered, first match wins):

1. sessionKey contains "subagent"        → SUBAGENT
2. sessionKey starts with "cron:"        → HEARTBEAT_CRON
3. sessionKey contains ":direct:"        → PRIVATE_DM
   3a. ...and peerId matches Jeff's ID   → PRIVATE_DM (load USER.md + TOOLS_COMPACT.md)
   3b. ...other sender                   → EXTERNAL_DM (load minimal set)
4. sessionKey contains ":topic:"         → FORUM_TOPIC
   4a. Classify by topic ID (see TOOLS.md Telegram topic map)
5. sessionKey contains ":group:"         → GROUP_CHAT
6. sessionKey ends with ":main"          → MAIN_SESSION (same as MAIN_DM)
7. Default                               → FALLBACK (load full set, safe default)

Topic-specific loading (Telegram forum topics):

With wisdom integrated into SOUL.md (Layer 0), no topic-specific file overrides are needed for v1. All topics receive SOUL.md + TOOLS_COMPACT.md. Topic-specific files can be added in v2 if certain threads need specialized context.

Topic ID Name Extra files to load
14 Ideas None
15 Ops None
51 SigInt None
75 Research None
77 Initiatives None
79 Projects None
11 APA None
12 BioThread None

Jeff's Telegram ID: Resolved at hook init from openclaw.json sender allowlist (not hardcoded in hook).

Hook implementation location:
~/.openclaw/workspace/hooks/aci-bootstrap/ (workspace hook, highest precedence)


5. Wisdom Compression Pipeline

What It Does

Converts raw, specific lessons learned into compressed, broadly applicable wisdom patterns — and integrates them into SOUL.md's wisdom section. Reduces token cost while increasing pattern applicability.

Output: A revised wisdom section for integration into SOUL.md. Not a separate file. Never a separate file.

Compression ratio target: 10:1 (10 specific lessons → 1-3 compressed wisdom patterns)

Example:

RAW (LESSONS_LEARNED.md):
- "Always verify port numbers before hardcoding. MC is on 3100."
- "Check if nomic-embed-text is running before calling embedding APIs."
- "Ollama model names are case-sensitive. qwen3-coder:30b not Qwen3-Coder."

COMPRESSED (wisdom section in SOUL.md):
- "Verify infrastructure assumptions before hardcoding anything. Names, ports, and model IDs drift."

Pipeline Steps

Step 1 — Source collection (manual)
Sources: - AGENTS.md → "Operational Lessons (earned the hard way)" section - LESSONS_LEARNED.md (Forge's workspace file — verify exists before running) - MEMORY.md → lessons/decisions sections - Recent memory/YYYY-MM-DD.md files (last 30 days)

Pre-check: compress-wisdom.sh must verify each source file exists before reading. Log warnings for missing files. Continue with available sources — missing files reduce quality, don't cause failure. (Addresses Quinn F-09.)

Step 2 — Distillation (LLM-assisted, manual review gate)
Run: scripts/compress-wisdom.sh (to be written by Melody in v1 implementation)

The script: 1. Lists available source files, logs warnings for any missing 2. Concatenates source lesson content 3. Sends to local qwen3.5:35b (extraction/reformatting task, [local-ok]) 4. Prompt: "Group these lessons by theme. For each theme, write one compressed wisdom pattern: broader applicability, no implementation-specific details, maximum 15 words." 5. Outputs draft wisdom section to initiatives/adaptive-context-injection/SOUL_WISDOM_DRAFT.md — formatted as the ## Wisdom section ready to drop into SOUL.md

Step 3 — Review gate (Jeff or Jules)
No SOUL.md update without explicit review of the wisdom section draft. The draft is flagged in DAILY_EXECUTION_QUEUE.md as "REVIEW NEEDED."

Step 4 — Promotion
Reviewed wisdom section replaces the ## Wisdom section in SOUL.md at workspace root. No separate WISDOM.md is created or exists. Full stop.

Cadence: Monthly or triggered by accumulation of 10+ new lessons since last compression run.

Wisdom Section Structure (integrated in SOUL.md)

## Wisdom

_Last compressed: YYYY-MM-DD | Source lessons: N_

**<Pattern name.>** <Wisdom in ≤15 words.>
**<Pattern name.>** <Wisdom in ≤15 words.>

Required wisdom entry — memory retrieval trigger (Jeff, 2026-03-21):
The wisdom section MUST contain the following entry. This is the behavioral trigger that replaces bulk MEMORY.md injection — without it, the on-demand retrieval model breaks down:

**When context feels incomplete, check your memory.** Read `memory/YYYY-MM-DD.md` and `MEMORY.md` when historical knowledge is needed.

This entry must survive every wisdom compression cycle. It is load-bearing — not optional. Compression scripts must preserve it verbatim or functionally equivalent. Verify it is present after every SOUL.md update.

Target size: 12-15 entries, ≤150 tokens within SOUL.md.


6. Integration Points With OpenClaw

Integration Point 1: agent:bootstrap hook (primary)

Location: ~/.openclaw/workspace/hooks/aci-bootstrap/
Event: agent:bootstrap
Access: event.sessionKey (top-level), event.context.bootstrapFiles (mutable array, nested under context)
Behavior: Hook mutates the bootstrap files array — removes files based on session type, adds conditional files.

Hook reads: event.sessionKey (top-level on event)
Hook mutates: event.context.bootstrapFiles (nested array)

OpenClaw config required:

{
  "hooks": {
    "internal": {
      "enabled": true,
      "entries": {
        "aci-bootstrap": { "enabled": true }
      }
    }
  }
}

⚠️ Basename allowlist risk: The bundled bootstrap-extra-files hook enforces a basename allowlist. It is not yet confirmed whether OpenClaw core itself filters WorkspaceBootstrapFile entries by basename. If core enforces an allowlist, injecting non-standard filenames (TOOLS_COMPACT.md, HEARTBEAT.md) via hook would silently fail. Task 1 (Atlas) must explicitly test this before Melody writes handler.ts. (Addresses Quinn F-06.)

Integration Point 2: before_prompt_build plugin hook (Layer 3 shedding — v2 only)

V1: Not implemented in code. Layer 3 shedding is delivered as behavioral instructions in SOUL.md only.

V2 note for future planning: before_prompt_build requires a full plugin (not a hook drop-in). It is a plugin lifecycle hook accessed via api.on('before_prompt_build', ...) inside a plugin's register(api) function. Requires openclaw.plugin.json manifest and explicit enablement via plugins.entries.<id>.enabled. Complexity: ⭐⭐⭐. Do not attempt in the same sprint as hook development.

Integration Point 3: Configuration (openclaw.json)

Changes needed: 1. Enable aci-bootstrap hook 2. If MEMORY.md is bulk-injected (verified in Phase 0): handle via hook by removing it from event.context.bootstrapFiles 3. No other config changes required

Integration Point 4: Agent instructions (SOUL.md)

SOUL.md updated to instruct: for long-term memory context, read MEMORY.md explicitly when the conversation requires it. This is behavioral, not code.


7. Migration Path

Phase 0 — Preparation (no live changes)

Dependency: None (first phase).

Pre-flight — MUST complete before any file modifications:

  1. Capture current baseline: run /context list in a live session and save output to initiatives/adaptive-context-injection/baseline-context.txt (Addresses Quinn F-10)
  2. Verify whether MEMORY.md appears in baseline — this determines Phase 1 scope (Addresses Quinn F-02)
  3. Create backups of ALL files that will be modified:
    mkdir -p ~/.openclaw/workspace/initiatives/adaptive-context-injection/backups
    cp ~/.openclaw/workspace/SOUL.md ~/.openclaw/workspace/initiatives/adaptive-context-injection/backups/SOUL.md.bak
    cp ~/.openclaw/workspace/AGENTS.md ~/.openclaw/workspace/initiatives/adaptive-context-injection/backups/AGENTS.md.bak
    cp ~/.openclaw/workspace/TOOLS.md ~/.openclaw/workspace/initiatives/adaptive-context-injection/backups/TOOLS.md.bak
    
  4. Verify all three backups exist before proceeding

Preparation steps: 1. Create TOOLS_COMPACT.md (lookup table only, ≤300 tok) 2. Run wisdom compression pipeline → produce SOUL_WISDOM_DRAFT.md 3. Create SOUL_COMPRESSED_DRAFT.md (compressed identity + reviewed wisdom section, target ~150-200 tok) 4. Jeff review + sign-off on SOUL_COMPRESSED_DRAFT.md before ANY SOUL.md modification

Rollback (one command):

cp ~/.openclaw/workspace/initiatives/adaptive-context-injection/backups/SOUL.md.bak ~/.openclaw/workspace/SOUL.md
Immediate restoration. Zero downtime. (Addresses Quinn F-08.)

Checkpoint: Backups verified. Baseline captured. MEMORY.md injection status known. SOUL_COMPRESSED_DRAFT.md Jeff-approved. TOOLS_COMPACT.md created. Nothing in production changed yet.


Phase 1 — MEMORY.md injection verification + on-demand behavior

Dependency: Phase 0 complete (baseline captured, MEMORY.md status confirmed from baseline-context.txt).

Conditional phase (Addresses Quinn F-02): Phase 0 pre-flight determines if MEMORY.md is bulk-injected.

  • If MEMORY.md IS bulk-injected: Remove it via hook, add on-demand retrieval instruction to SOUL.md, monitor for 48 hours.
  • If MEMORY.md is NOT bulk-injected: Phase 1 is a no-op for removal. Still add explicit on-demand retrieval instruction to SOUL.md to ensure reliable agent behavior. Run AC-3 test protocol to verify retrieval works before Phase 2.

Steps (if removal needed): 1. Update SOUL.md with on-demand retrieval instruction 2. Remove MEMORY.md from bootstrap injection via hook 3. Run AC-3 test protocol (3 prompts, 3/3 pass required) 4. Monitor for 48 hours in normal operation

Rollback (one command):

# Option A: disable hook entirely
# Set hooks.internal.entries.aci-bootstrap.enabled: false in openclaw.json

# Option B: restore SOUL.md only
cp ~/.openclaw/workspace/initiatives/adaptive-context-injection/backups/SOUL.md.bak ~/.openclaw/workspace/SOUL.md
MEMORY.md returns to bulk injection. Zero blast radius.

Checkpoint: /context list confirms MEMORY.md state. AC-3 passes 3/3. Jules correctly reads MEMORY.md on demand in main sessions.


Phase 2 — Session-type classification + Layer 1 conditionals

Dependency: Phase 0 complete (SOUL.md compressed + Jeff-approved, TOOLS_COMPACT.md created, backups verified). Phase 1 complete (MEMORY.md status confirmed, on-demand retrieval instruction active). Task 1 complete (hook API shape empirically verified, basename allowlist question answered). (Addresses Quinn F-11.)

Steps: 1. Deploy aci-bootstrap hook with classification logic (using verified API shape from Task 1) 2. Wire conditional file sets per session type 3. Test each session type: main DM, Ideas topic, Ops topic, subagent, cron 4. Run AC-5 validation — every session type must load correct file set 5. Run AC-9 test protocol — behavioral integrity across session types (3 prompts, 3/3 pass)

Rollback (one command):

# Edit openclaw.json:
# hooks.internal.entries.aci-bootstrap.enabled: false
Bulk injection resumes immediately. All backup files remain in initiatives/adaptive-context-injection/backups/. Zero blast radius.

Checkpoint: Each session type loads the correct file set. AC-5 passes. AC-9 passes 3/3. No session missing required context.


Phase 3 — Layer 3 behavioral shedding (instructions only)

Dependency: Phase 2 complete and stable (minimum 48 hours clean operation).

Steps: 1. Update SOUL.md with shedding instructions (see Section 3, Layer 3) 2. Monitor Jules behavior across 1 week of normal operation 3. Capture examples where shedding worked vs. failed

Rollback: Remove shedding instructions from SOUL.md. Behavioral only, instant revert.

Checkpoint: Jules references previously-injected context less frequently after it's been established in conversation.


Phase 4 — TOOLS.md On-Demand Instruction Verification

N-04 note: Phase 4 was renamed to clarify its purpose. The TOOLS_COMPACT.md hook wiring was already completed in Phase 2. Phase 4's distinct new work is: confirming the behavioral instruction in SOUL.md that directs Jules to read full TOOLS.md on demand. This is a verification and documentation step, not a re-implementation of Phase 2 hook work.

Dependency: Phase 3 complete (SOUL.md shedding instructions already in place; on-demand TOOLS.md instruction can be added or confirmed in the same pass).

Steps: 1. Confirm SOUL.md includes the on-demand retrieval instruction: "For full tool details, read TOOLS.md explicitly when needed." 2. If instruction is missing, add it to SOUL.md's behavioral guidance (minor SOUL.md edit — does not require full compression cycle) 3. Verify full TOOLS.md is NOT bulk-injected in any session type (confirm via /context list across representative sessions) 4. Confirm TOOLS.md remains at workspace root and readable on demand (do not delete or rename)

Rollback: This phase is behavioral-only (SOUL.md instruction). Revert by removing the on-demand TOOLS.md instruction from SOUL.md. Instant.

Checkpoint: SOUL.md contains explicit on-demand TOOLS.md retrieval instruction. /context list confirms TOOLS.md is not bulk-injected in any session type. Jules reads TOOLS.md via read tool when asked about full tool details.


Phase 5 — v2 planning (out of scope for this spec)

  • Plugin-based Layer 3 shedding (code, not instructions)
  • Embedding-based Layer 2 retrieval
  • Multi-agent session type routing

8. Acceptance Criteria

Quinn validates each criterion independently.

AC-1a: Token reduction — minimal topic sessions (Ideas, SigInt, Research, APA, BioThread)

Given a new Telegram forum topic session for a minimal topic
When the agent receives the first message
Then /context list shows exactly 2 bootstrap files injected: SOUL.md and TOOLS_COMPACT.md
And total injected bootstrap tokens are ≤ 500 tokens
✓ Pass threshold: ≤ 500 tokens
✗ Fail threshold: > 600 tokens (current baseline)

Note (N-01 resolution): Threshold raised from 400 to 500. At upper bounds SOUL.md (~200 tok) + TOOLS_COMPACT.md (≤300 tok) = 500 tok — passes. 500 tokens is still 80%+ reduction from the ~11,500 token baseline.

AC-1b: Token reduction — enriched topic sessions (Ops, Initiatives, Projects)

Given a new Telegram forum topic session for Ops (15), Initiatives (77), or Projects (79)
When the agent receives the first message
Then /context list shows exactly 2 bootstrap files: SOUL.md (identity + integrated wisdom) and TOOLS_COMPACT.md
And total injected bootstrap tokens are ≤ 500 tokens
✓ Pass threshold: ≤ 500 tokens
✗ Fail threshold: > 600 tokens

Note (N-01 resolution): Threshold raised from 400 to 500. With wisdom integrated into SOUL.md (Layer 0), no separate file is injected for enriched topics. At upper bounds SOUL.md (~200 tok) + TOOLS_COMPACT.md (≤300 tok) = 500 tok — consistent with AC-1a. 500 tokens is still 80%+ reduction from the ~11,500 token baseline.

AC-2: Token reduction — main DM session

Given a main DM session with Jeff
When the agent receives the first message
Then injected bootstrap files = {SOUL.md (identity + wisdom), USER.md, TOOLS_COMPACT.md}
And MEMORY.md is NOT in the injected list
And total injected bootstrap tokens are ≤ 500 tokens

AC-3: MEMORY.md on-demand retrieval

Test protocol (Addresses Quinn F-07): Run 3 standard prompts that should trigger MEMORY.md retrieval. Required pass rate: 3/3. Evidence = read or memory_search tool call targeting MEMORY.md visible in session logs.

Test prompts: 1. "What did we decide about local model strategy?" 2. "When did we run the APA shakedown and what were the results?" 3. "What's Forge's role and when was it set up?"

Given Jeff asks any of the above in a main DM session
Then Jules calls read or memory_search targeting MEMORY.md
And retrieves the relevant context
And responds correctly without having MEMORY.md pre-injected
Pass: 3/3 prompts trigger retrieval and return correct answers
Fail: Any prompt where Jules claims ignorance or answers incorrectly without attempting retrieval

AC-4: Subagent injection

Given a subagent session is spawned
When the subagent receives its first message
Then only SOUL.md is injected (no USER.md, no AGENTS.md, no MEMORY.md, no TOOLS_COMPACT.md)

AC-5: Session type classification accuracy

Given each of the 5 session types (main DM, forum topic, group, subagent, cron)
When the ACI bootstrap hook fires
Then the correct file set for that session type is loaded
With zero cases where a required file is missing

AC-6: TOOLS_COMPACT.md coverage

Given TOOLS_COMPACT.md is created
Then it contains all lookup-critical content: port numbers, model CLI flags, camera names, TTS voice preferences, Telegram topic IDs
And it does NOT contain "why" explanations or narrative prose
And its size is ≤ 300 tokens

AC-7: Wisdom section quality (in SOUL.md)

Given the wisdom section in SOUL.md is updated from the compression pipeline
Then each wisdom entry covers at least 3 raw lessons
And no wisdom entry contains specific port numbers, model names, or implementation details
And total wisdom section size is ≤ 150 tokens (~12-15 entries max)
And total SOUL.md (identity + wisdom) is ≤ 200 tokens
And no separate WISDOM.md file exists in the workspace root

AC-8: Rollback safety

Given the aci-bootstrap hook is disabled in openclaw.json
Then OpenClaw returns to its default bulk injection behavior
And no workspace files are missing or corrupted
Given SOUL.md.bak exists
When cp backups/SOUL.md.bak SOUL.md is run
Then original SOUL.md is immediately restored

AC-9: Session behavioral integrity

Test protocol (Addresses Quinn F-07): Run 3 standard prompts across different session types. Required pass rate: 3/3. Evidence = read tool call visible in session logs.

Test prompts (run in a forum topic session where AGENTS.md is NOT injected): 1. "Spawn Melody to fix a bug in the auth module" (should trigger read of AGENTS.md for dispatch protocol) 2. "What port does Mission Control run on?" (should trigger read of TOOLS.md or answered from TOOLS_COMPACT.md) 3. "What lessons did we learn about timeout settings?" (should trigger read of MEMORY.md)

Given any session type after ACI Phase 2 is deployed
When Jules is asked to perform a task requiring knowledge from a non-injected file
Then Jules calls read to retrieve the needed file
And completes the task correctly
Pass: 3/3 prompts trigger appropriate file retrieval
Fail: Any prompt where Jules guesses, hallucinates, or claims ignorance without attempting retrieval


9. Risk Assessment

Risk 1: Regression — Missing critical context

Probability: Medium
Impact: High (Jules answers incorrectly because a needed file wasn't injected)
Mitigation: - Phased rollout with explicit checkpoint at each phase - FALLBACK session type loads the full file set — safe default, not a failure mode - 48-hour monitoring window per phase before proceeding - Quinn validates AC-9 (behavioral integrity) explicitly

Risk 2: agent:bootstrap hook API shape uncertainty

Probability: Low-Medium
Impact: Medium (handler.ts written against wrong interface, fails silently)
Context: The hook docs confirm it can mutate context.bootstrapFiles, but the exact WorkspaceBootstrapFile type shape is not in public docs. Need to verify: is sessionKey truly top-level? Does mutating the array (splice/push) work, or does it require element-level mutation? Must confirm empirically before Melody writes handler.ts.
Mitigation: Task 1 (Atlas) writes a logging-only test hook, deploys it, and inspects the actual object shape. No handler.ts until Task 1 report is in hand.

Risk 3: MEMORY.md retrieval failure (agent doesn't pull it)

Probability: Medium
Impact: Medium (Jules has memory gaps in conversations that depend on historical context)
Mitigation: - SOUL.md must include explicit on-demand retrieval instruction - AC-3 tests this directly before Phase 2 proceeds - If Jules misses retrieval more than 0x in 3 test prompts, reconsider removing MEMORY.md from bulk injection (or create MEMORY_SUMMARY.md bulk-injected with only the last 30 entries)

Risk 4: TOOLS_COMPACT.md becomes stale

Probability: High (over time)
Impact: Low-Medium (Jules has wrong tool details, causes minor errors)
Mitigation: - Add TOOLS_COMPACT.md update to the new-tool-addition checklist in AGENTS.md - Monthly review cadence aligned with wisdom compression

Risk 5: Session key format changes in future OpenClaw versions

Probability: Low
Impact: Medium (classification breaks, all sessions fall to FALLBACK set)
Mitigation: - FALLBACK loads the full file set — this is a safe default, not a failure mode - Version-pin the hook's documented assumptions - Hook includes a validation check: log a warning if session key format doesn't match any pattern

Risk 6: Wisdom compression loses important specifics

Probability: Medium
Impact: Low-Medium (Jules loses nuance that was in raw lessons)
Mitigation: - Raw lessons stay in LESSONS_LEARNED.md and MEMORY.md permanently - Wisdom section in SOUL.md supplements, not replaces, Layer 2 retrieval - Review gate before promoting any wisdom draft to SOUL.md

Risk 7: Source files for wisdom compression may not exist

Probability: Medium
Impact: Low (compression script runs with fewer inputs, produces lower-quality output)
Mitigation: - Pre-check in compression script: list available sources, log warnings for missing files (Addresses Quinn F-09) - LESSONS_LEARNED.md (Forge's workspace) must be verified present before Task 3 begins - Script continues with available sources — missing files reduce quality, not cause failure

Risk 8: SOUL.md compression loses identity fidelity

Probability: Low
Impact: High (Jules loses coherent identity — manifests immediately on next session)
Mitigation: - SOUL.md compression is NOT a content reduction — it's reorganization and tightening - The personality, voice, and decision framework must be fully preserved - Jeff reviews SOUL_COMPRESSED_DRAFT.md before any SOUL.md replacement — hard gate - SOUL.md.bak backup created before Phase 0 proceeds — one-command revert available

Risk 9: Custom filenames blocked by OpenClaw basename allowlist

Probability: Unknown
Impact: High (TOOLS_COMPACT.md injection silently fails; agents get wrong context set)
Mitigation: - Task 1 (Atlas) explicitly tests injecting a non-standard filename and verifies it appears in /context list - If allowlist is enforced, conditional injection strategy requires rethinking before Melody writes any hook code


10. Data Models / Contracts

Bootstrap File Set Types

type SessionType = 
  | "MAIN_SESSION"     // main DM or CLI main session
  | "PRIVATE_DM"       // DM with recognized sender (Jeff)
  | "EXTERNAL_DM"      // DM with unrecognized sender
  | "FORUM_TOPIC"      // Telegram forum topic
  | "GROUP_CHAT"       // Group chat (no topic)
  | "SUBAGENT"         // Spawned subagent
  | "HEARTBEAT_CRON"   // Cron or heartbeat job
  | "FALLBACK";        // Unclassified (safe default = full set)

interface BootstrapFileSet {
  sessionType: SessionType;
  files: string[];              // Basenames, e.g. ["SOUL.md", "USER.md"]
  reason: string;               // Log string for debugging
}

// No topic-specific file overrides in v1.
// Wisdom lives in SOUL.md (Layer 0) — all sessions get it automatically.
// Topic-specific overrides can be added here in v2 if needed.
const TOPIC_FILE_MAP: Record<number, string[]> = {};

// Default file sets per session type
// SOUL.md always includes the integrated wisdom section (Layer 0)
// No WISDOM.md file exists or will be created
const SESSION_FILE_SETS: Record<SessionType, string[]> = {
  MAIN_SESSION:    ["SOUL.md", "USER.md", "TOOLS_COMPACT.md"],
  PRIVATE_DM:      ["SOUL.md", "USER.md", "TOOLS_COMPACT.md"],
  EXTERNAL_DM:     ["SOUL.md", "TOOLS_COMPACT.md"],
  FORUM_TOPIC:     ["SOUL.md", "TOOLS_COMPACT.md"],
  GROUP_CHAT:      ["SOUL.md", "TOOLS_COMPACT.md"],
  SUBAGENT:        ["SOUL.md"],
  HEARTBEAT_CRON:  ["SOUL.md", "HEARTBEAT.md"],
  FALLBACK:        ["SOUL.md", "USER.md", "AGENTS.md", "TOOLS_COMPACT.md"],
};

Hook Event Contract (corrected — Addresses Quinn F-01)

// agent:bootstrap event structure — verified against OpenClaw docs
// sessionKey is TOP-LEVEL on the event, NOT inside context
interface BootstrapHookEvent {
  sessionKey: string;                           // Top-level on event
  context: {
    bootstrapFiles?: WorkspaceBootstrapFile[];  // Optional per OpenClaw docs — handler.ts MUST code defensively for undefined (early return with warning if not present)
    workspaceDir?: string;
    cfg?: OpenClawConfig;
  };
}

// N-02 NOTE: bootstrapFiles is marked optional (?) per OpenClaw docs (hooks.md: bootstrapFiles?: WorkspaceBootstrapFile[]).
// handler.ts must guard: if (!event.context.bootstrapFiles) { log warning; return; }
// Do not assume it is always populated — behavior under undefined is unconfirmed until Task 1.

// NOTE: WorkspaceBootstrapFile exact type shape is NOT in public docs.
// Task 1 (Atlas) MUST verify empirically before Melody writes handler.ts.
// Log and inspect the actual object shape in the test hook.
// Specifically verify: is mutation via splice/push sufficient, or does
// element-level property mutation behave differently?

// Hook reads event.sessionKey (top-level), mutates event.context.bootstrapFiles

function classifySession(sessionKey: string): SessionType
function getFileSet(type: SessionType, topicId?: number): string[]
function filterBootstrapFiles(
  files: WorkspaceBootstrapFile[], 
  allowedBasenames: string[]
): WorkspaceBootstrapFile[]

⚠️ Task 1 must also test: Can non-standard filenames (e.g., TOOLS_COMPACT.md, HEARTBEAT.md) be injected via hook, or does OpenClaw core enforce a basename allowlist? If allowlist is enforced, the entire conditional injection strategy needs rethinking before any build work begins. (Addresses Quinn F-06.)

Wisdom Section Schema (inside SOUL.md — no separate file)

## Wisdom
_Last compressed: YYYY-MM-DD | Source lessons: N_

**<Pattern name.>** <Wisdom in ≤15 words.>
**<Pattern name.>** <Wisdom in ≤15 words.>

Wisdom lives exclusively in SOUL.md (Layer 0). No separate WISDOM.md file is created or maintained.


11. Execution Constraints

Task Model Tier Complexity Context Needs
Write aci-bootstrap hook (handler.ts + HOOK.md) [cloud-required] Sonnet ⭐⭐⭐ Hook API docs + session key examples + file set rules
Create TOOLS_COMPACT.md from TOOLS.md [local-ok] qwen3.5:35b ⭐⭐ TOOLS.md only
Run wisdom compression script (input prep + distillation) [local-ok] qwen3.5:35b ⭐⭐ AGENTS.md + LESSONS_LEARNED.md + MEMORY.md excerpts
Compress SOUL.md (identity + wisdom integration) [cloud-required] Sonnet ⭐⭐ SOUL.md + SOUL_WISDOM_DRAFT.md + this spec
Validate hook (test logging stub) [local-ok] Hook test harness
Atlas: verify hook API shape (test hook) Atlas OpenClaw hooks docs
Quinn: AC-1 through AC-9 validation Quinn ⭐⭐⭐ This spec + test session transcripts

Estimated Melody task count: 4 tasks (hook, TOOLS_COMPACT.md, compression script, SOUL.md draft)
Atlas task count: 1 (hook API shape verification)
Quinn task count: 1 (full AC validation pass)


12. Task Decomposition

Tasks must be executed in dependency order where noted. Parallelism is explicitly called out.

Task 1: Atlas — Verify hook API shape ⭐ [cloud-required]

Dependency: None (first)
Deliverable: A test hook that logs event.sessionKey and event.context.bootstrapFiles to a file, deployed and tested. Written report confirming: - Exact shape of WorkspaceBootstrapFile type - Whether sessionKey is confirmed top-level on event (not inside context) - Whether mutating the array (splice/push) is sufficient or element-level mutation is needed - Result of injecting a non-standard filename (e.g., TOOLS_COMPACT.md) — does it appear in /context list? - Any observed behavior surprises

Why Atlas: Research and verification, not implementation. No build work starts until this report is received.


Task 2: Melody — Create TOOLS_COMPACT.md ⭐⭐ [local-ok]

Dependency: None (runs parallel with Task 1)
Input: Full TOOLS.md
Deliverable: ~/.openclaw/workspace/TOOLS_COMPACT.md with only lookup-critical content. Target ≤300 tokens. No prose explanations. Section headers: Browser, Mission Control, Telegram Topics, Cursor CLI, TTS, Camera.
Model: qwen3.5:35b (extraction/reformatting task)


Task 3: Melody — Run wisdom compression pipeline ⭐⭐ [local-ok]

Dependency: None (runs parallel with Tasks 1-2)
Input: AGENTS.md (Operational Lessons section), LESSONS_LEARNED.md (Forge workspace — verify exists), relevant MEMORY.md sections
Deliverable: ~/.openclaw/workspace/initiatives/adaptive-context-injection/SOUL_WISDOM_DRAFT.md — formatted as the ## Wisdom section ready for integration into SOUL.md. Not a standalone file, not a separate injection target. A section draft.
Review gate: Jeff or Jules must approve wisdom section before it's integrated into SOUL.md
Model: qwen3.5:35b


Task 4: Melody — Create SOUL_COMPRESSED_DRAFT.md ⭐⭐ [cloud-required] Sonnet

Dependency: Task 3 complete and wisdom draft approved (Jules review gate)
Input: Current SOUL.md, approved SOUL_WISDOM_DRAFT.md, this spec (Layer 0 definition)
Deliverable: ~/.openclaw/workspace/initiatives/adaptive-context-injection/SOUL_COMPRESSED_DRAFT.md
Contents: Compressed SOUL.md identity sections + approved wisdom section integrated. Full target size: ~150-200 tokens (identity ~80 tok + wisdom ≤150 tok). Identity preserved, voice intact. Wisdom section integrated as ## Wisdom block.
Review gate: Jeff reviews before any SOUL.md replacement. Hard gate. No exceptions.
Model: Sonnet (identity compression requires judgment; local model unacceptable here)


Task 5: Jules — Review gate (Tasks 3 + 4 drafts)

Dependency: Tasks 3 and 4 complete
Action: Review SOUL_WISDOM_DRAFT.md and SOUL_COMPRESSED_DRAFT.md. Approve or return with notes.
Gate: Nothing promoted to production until this review is explicitly signed off.


Task 6: Melody — Phase 0 execution ⭐ [local-ok]

Dependency: Phase 0 pre-flight complete (backups exist, baseline captured) + Task 5 approval
Deliverable: - SOUL.md replaced with SOUL_COMPRESSED_DRAFT.md content - TOOLS_COMPACT.md placed at workspace root - AGENTS.md updated: add TOOLS_COMPACT.md update to new-tool-addition checklist - No OpenClaw configuration changes (Phase 0 is complete — hook deployment is Phase 2) - Backup files confirmed present in initiatives/adaptive-context-injection/backups/


Task 7: Melody — Write ACI bootstrap hook ⭐⭐⭐ [cloud-required] Sonnet

Dependency: Task 1 (API shape confirmed + basename allowlist question answered) + Task 6 (Phase 0 complete, file sets finalized)
Deliverable: - ~/.openclaw/workspace/hooks/aci-bootstrap/HOOK.md - ~/.openclaw/workspace/hooks/aci-bootstrap/handler.ts

Hook implements: session type classification using event.sessionKey (top-level), file set resolution per SESSION_FILE_SETS, bootstrap file mutation of event.context.bootstrapFiles. No shedding logic (v2). Fallback classification defaults to full file set.
Enable in openclaw.json: yes, as part of this task.


Task 8: Quinn — Full AC validation ⭐⭐⭐

Dependency: Task 7 deployed + Phases 0-2 complete
Deliverable: QA report against AC-1a, AC-1b, AC-2 through AC-9. Each criterion: PASS / FAIL / BLOCKED with evidence (e.g., /context list output, conversation transcript snippets, session logs showing read tool calls).


Appendix A: Context Baseline

Approximate token counts from docs examples. MUST be replaced with actual /context list capture before Phase 0 begins. Save to initiatives/adaptive-context-injection/baseline-context.txt. (Addresses Quinn F-10.)

File Current (tok, approx) Target After ACI
AGENTS.md ~436 ~0 (conditional — orchestration sessions only)
SOUL.md ~228 ~150-200 (compressed identity + integrated wisdom)
TOOLS.md ~5,241 ~200 (TOOLS_COMPACT.md replaces in non-main sessions; TOOLS.md on-demand only)
IDENTITY.md ~53 ~0 (dropped from injection entirely)
USER.md ~97 ~97 (conditional — main DM only)
MEMORY.md varies/growing ~0 (on-demand only — Phase 0 verifies current state)
Total (baseline) ~6,000+ ~350-500 (depending on session type)

Estimated token savings per turn: 80-90% for subagents and topic threads; 60-70% for main DM sessions.


Appendix B: Files To Create / Modify

Action File Phase Notes
Capture backups/SOUL.md.bak 0 (pre-flight) Hard gate — must exist before any modification
Capture backups/AGENTS.md.bak 0 (pre-flight) Hard gate
Capture backups/TOOLS.md.bak 0 (pre-flight) Hard gate
Capture baseline-context.txt (from /context list) 0 (pre-flight) Reference point for AC-1/AC-2
Create (draft) SOUL_WISDOM_DRAFT.md (wisdom section for SOUL.md) 0 (Task 3) Jules review gate before use
Create (draft) SOUL_COMPRESSED_DRAFT.md 0 (Task 4) Jeff review gate — hard gate
Replace SOUL.md (with compressed version + integrated wisdom) 0 (Task 6) Backup required first
Create TOOLS_COMPACT.md 0 (Task 2) Lookup table only, ≤300 tok
Modify AGENTS.md (add TOOLS_COMPACT.md update to checklist) 0 (Task 6) Backup required first
Create hooks/aci-bootstrap/HOOK.md 2 (Task 7) After Task 1 confirms API shape
Create hooks/aci-bootstrap/handler.ts 2 (Task 7) After Task 1 confirms API shape
Modify openclaw.json (enable hook) 2 (Task 7) Phase 2 gate
No change MEMORY.md On-demand only; do not delete
No change TOOLS.md Kept for on-demand reads

Rollback Procedures (every phase)

Phase Rollback Command Effect
0 cp backups/SOUL.md.bak SOUL.md && cp backups/AGENTS.md.bak AGENTS.md Restore Phase 0 file changes immediately
1 Disable hook in openclaw.json OR cp backups/SOUL.md.bak SOUL.md MEMORY.md behavior reverts
2 # In openclaw.json: hooks.internal.entries.aci-bootstrap.enabled: false Full bulk injection resumes immediately
3 Remove shedding instructions from SOUL.md Behavioral only, instant revert
4 Remove on-demand TOOLS.md instruction from SOUL.md (behavioral only, instant revert) Instruction removed; no hook changes needed
Full rollback cp backups/SOUL.md.bak SOUL.md && cp backups/AGENTS.md.bak AGENTS.md && cp backups/TOOLS.md.bak TOOLS.md then disable hook Complete reversion to pre-ACI state

Forge signs off on v3. All 11 original QA findings resolved (5 blockers, 3 major, 3 minor). All 4 QA_REPORT_FINAL notes resolved (N-01 through N-04). Jeff additional (memory-check wisdom entry) applied. Wisdom-in-SOUL.md design decision enforced throughout. No separate WISDOM.md file exists or will be created.

No code until Jeff reviews. That's the directive.