selfware 0.6.7

Your personal AI workshop — software you own, software that lasts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
{
  "component": "cognitive",
  "tier": "full",
  "loop_stage": "learn",
  "summary": "The cognitive component is the engine's memory and reflection substrate for the learn stage. It runs a 4-tier HierarchicalMemory (Working, ShortTerm, LongTerm, Archive) over MemoryEntry items, records Episodes with EpisodeOutcome and related_episodes links, indexes the codebase as a knowledge graph of Entities and RelationTypes plus a SymbolIndex, maintains a SelfModel with an IntrospectionEngine and ModificationEngine, ranks improvement strategies via a MetaLearner of StrategyScores, retrieves code context through the RagEngine, and splits the token window by TaskType with the TokenBudgetAllocator. On the loop it closes the cycle: CognitiveSystem.record_episode captures what happened, memory tiers promote what mattered, introspection grounds the agent's own state, and CognitiveSystem.build_context feeds an LlmContext back into the next perceive/reason pass.",
  "loop_objects": ["Episode", "MemoryEntry", "HierarchicalMemory", "SelfModel", "ReferenceState", "IntrospectionTarget", "ProposedModification", "CodeModification", "Entity", "RelationType", "SymbolIndex", "TokenBudget", "LlmContext", "CodeContext", "MetaLearner", "StrategyScore", "Lesson", "CognitiveState", "CyclePhase", "WorkingContext"],
  "context_basis": "Recommendations formed with src/cognitive/ read in the context of the full engine under a ~600k budget framing; TokenBudgetAllocator ratios (e.g. SelfImprovement 10/10/70 with a 10% reserve) and memory-tier capacities assume a large context that must be actively partitioned across working/episodic/semantic recall.",
  "examples": [
    {
      "id": "cognitive-01",
      "title": "Retrieve the top relevant Episodes for the current query",
      "loop_stage": "perceive",
      "pattern": "relevance-recall",
      "intent": "Bring past experiences that match the query into working context before reasoning.",
      "how_it_shapes_the_loop": "EpisodicMemory.retrieve_relevant ranks Episodes by relevance_score and Importance, injecting top-k into the LlmContext so the perceive stage arrives pre-loaded with prior outcomes.",
      "loop_objects_touched": ["Episode", "LlmContext"],
      "wiring": {
        "inputs_from": ["query text", "EpisodicMemory store"],
        "outputs_to": ["CognitiveSystem.build_context", "LlmContext"]
      },
      "touch_interaction": {
        "gesture": "spread",
        "canvas_action": "Spreading over the episodic node fans out matching Episode cards ranked by importance, closest matches pulling toward the center.",
        "visual": "Episode cards bloom outward; high-Importance ones glow gold and pull inward, low ones fade to the canvas edge."
      },
      "mini_scenario": "The user spreads the episodic node on a debugging query and the three highest-importance error Episodes bloom in, seeding the LlmContext before the reason pass.",
      "pitfall": "Retrieval must respect the importance threshold; flooding context with low-Importance Episodes crowds out the semantic code the reasoning actually needs."
    },
    {
      "id": "cognitive-02",
      "title": "Record an Episode with its outcome after acting",
      "loop_stage": "learn",
      "pattern": "experience-capture",
      "intent": "Persist what just happened, its EpisodeOutcome, and any lessons for future recall.",
      "how_it_shapes_the_loop": "After the act stage, CognitiveSystem.record_episode writes an Episode (EpisodeType, content, EpisodeOutcome, related_episodes), giving the learn stage a durable trace to promote and mine.",
      "loop_objects_touched": ["Episode", "Lesson"],
      "wiring": {
        "inputs_from": ["act stage result", "verify outcome (Success/Error)"],
        "outputs_to": ["EpisodicMemory", "MetaLearner"]
      },
      "touch_interaction": {
        "gesture": "tap",
        "canvas_action": "Tapping the learn node stamps a new Episode tile onto the memory timeline at the current cycle position.",
        "visual": "A fresh tile drops in with a green check (Success) or red cross (Error) badge and a pulsing 'new' glow that slowly fades."
      },
      "mini_scenario": "A tool run fixes a failing test; a Success Episode is stamped with the lesson 'checked lock ordering', linked via related_episodes to the prior Error Episode.",
      "pitfall": "Record the true EpisodeOutcome, not the intended one; logging Success on an unverified action poisons future relevance recall with false confidence."
    },
    {
      "id": "cognitive-03",
      "title": "Promote a ShortTerm MemoryEntry to LongTerm",
      "loop_stage": "learn",
      "pattern": "tier-promotion",
      "intent": "Move memories that keep proving useful into durable storage.",
      "how_it_shapes_the_loop": "HierarchicalMemory.promote moves a MemoryEntry from ShortTermMemory to LongTermMemory when check_promotion sees access count and Importance cross their thresholds, reshaping what survives across sessions.",
      "loop_objects_touched": ["MemoryEntry", "HierarchicalMemory"],
      "wiring": {
        "inputs_from": ["ShortTermMemory.check_promotion", "MemoryEntry access count"],
        "outputs_to": ["LongTermMemory.store"]
      },
      "touch_interaction": {
        "gesture": "drag",
        "canvas_action": "Dragging a MemoryEntry tile upward across the tier boundary promotes it; the boundary flexes and accepts it only if thresholds are met.",
        "visual": "The tile brightens as it crosses; the LongTerm band ripples and locks it in with a subtle anchor icon."
      },
      "mini_scenario": "A pattern recalled five times this session drags up past the promotion threshold into LongTerm, so it persists into tomorrow's run.",
      "pitfall": "Promotion is gated by both access and Importance; promoting on access alone lets high-frequency noise clog LongTerm."
    },
    {
      "id": "cognitive-04",
      "title": "Split the token budget by detected TaskType",
      "loop_stage": "control",
      "pattern": "budget-by-task-type",
      "intent": "Allocate the context window to working/episodic/semantic recall according to what the task needs.",
      "how_it_shapes_the_loop": "TokenBudgetAllocator.allocation_ratios maps the detected TaskType to an AllocationRatios split (SelfImprovement = working 10 / episodic 10 / semantic 70 / reserve 10), so the control layer partitions the window before context assembly.",
      "loop_objects_touched": ["TokenBudget", "LlmContext"],
      "wiring": {
        "inputs_from": ["TokenBudgetAllocator.suggest_task_type(query)"],
        "outputs_to": ["CognitiveSystem.build_context", "context assembly"]
      },
      "touch_interaction": {
        "gesture": "two-finger-rotate",
        "canvas_action": "Rotating the budget wheel redistributes its three colored arcs (working/episodic/semantic) to the TaskType preset ratios.",
        "visual": "The pie wheel spins to the preset split; the semantic arc swells green for a SelfImprovement task while a thin reserve ring stays locked at the rim."
      },
      "mini_scenario": "The allocator detects a self-improvement query and rotates the budget to 70% semantic, giving the agent room to read its own code via read_own_code.",
      "pitfall": "Always keep the reserve arc; a split that spends 100% of the window on recall leaves no room to generate an answer."
    },
    {
      "id": "cognitive-05",
      "title": "Assemble a single LlmContext from all memory sources",
      "loop_stage": "foundation",
      "pattern": "context-merge",
      "intent": "Fuse working, episodic, semantic, and self context into one prompt within budget.",
      "how_it_shapes_the_loop": "CognitiveSystem.build_context merges the WorkingContext, retrieved Episodes, RAG CodeContext, and SelfImprovementContext into one LlmContext with estimate_tokens, and is_within_budget lets the loop gate on it before the model call.",
      "loop_objects_touched": ["LlmContext", "CodeContext", "WorkingContext", "Episode"],
      "wiring": {
        "inputs_from": ["WorkingContext", "EpisodicMemory", "SemanticMemory", "SelfModel"],
        "outputs_to": ["reason stage (model call)"]
      },
      "touch_interaction": {
        "gesture": "pinch",
        "canvas_action": "Pinching the four source nodes together fuses them into one LlmContext tile stamped with its estimated token count.",
        "visual": "Four streams braid into a single glowing tile; a token meter fills along its edge, green under budget, amber near the limit."
      },
      "mini_scenario": "The user pinches working, episodic, semantic, and self nodes into one LlmContext reading 340k estimated tokens, and is_within_budget returns true.",
      "pitfall": "The estimate must reflect the assembled content; a stale estimate lets an over-budget context reach the model and truncate silently."
    },
    {
      "id": "cognitive-06",
      "title": "Introspect the agent's current state on demand",
      "loop_stage": "reason",
      "pattern": "self-query",
      "intent": "Answer 'what is my state / what can I do' from the self-reference system rather than guessing.",
      "how_it_shapes_the_loop": "IntrospectionEngine.introspect resolves an IntrospectionTarget (CurrentState, Capabilities, Memory, Performance, Decisions) against the ReferenceState, feeding the reason stage grounded facts about itself instead of confabulation.",
      "loop_objects_touched": ["IntrospectionTarget", "ReferenceState", "SelfModel"],
      "wiring": {
        "inputs_from": ["introspection query", "SelfReferenceSystem"],
        "outputs_to": ["reason stage", "ProposedModification generation"]
      },
      "touch_interaction": {
        "gesture": "double-tap",
        "canvas_action": "Double-tapping the self node opens a mirror panel listing the chosen IntrospectionTarget's answer.",
        "visual": "A reflective mirror-surface node flips open; the target's facts scroll up in a cool blue introspection panel."
      },
      "mini_scenario": "Before proposing a change, the agent introspects Capabilities and confirms it has the tool-dispatch ability the plan needs.",
      "pitfall": "Introspection reflects the recorded SelfModel; if recent CodeModifications were not tracked, the agent introspects a stale self."
    },
    {
      "id": "cognitive-07",
      "title": "Climb ReferenceLevels from Direct to Reflective",
      "loop_stage": "reason",
      "pattern": "meta-reasoning-ascent",
      "intent": "Reason about the agent's own reasoning at increasing depth.",
      "how_it_shapes_the_loop": "ReferenceState.set_level moves the current ReferenceLevel (Direct, Meta, MetaMeta, Reflective); ascending a level reframes the reason stage from 'what' to 'why I decided that way'.",
      "loop_objects_touched": ["ReferenceState", "SelfModel"],
      "wiring": {
        "inputs_from": ["current reasoning trace"],
        "outputs_to": ["strategy adjustment", "MetaLearner"]
      },
      "touch_interaction": {
        "gesture": "spread",
        "canvas_action": "Spreading vertically on the self node steps it up a ReferenceLevel, nesting a new reflective frame around the current one.",
        "visual": "Concentric rings nest outward labeled Direct, Meta, MetaMeta, Reflective, the active ring pulsing."
      },
      "mini_scenario": "After a failed plan the agent ascends to Meta level to ask why it preferred the failing strategy, then adjusts its priors.",
      "pitfall": "Deeper is not always better; ascending to Reflective on every trivial query burns budget on navel-gazing instead of acting."
    },
    {
      "id": "cognitive-08",
      "title": "Rank improvement strategies with the MetaLearner",
      "loop_stage": "learn",
      "pattern": "strategy-ranking",
      "intent": "Choose which kind of self-improvement to attempt next based on what has worked.",
      "how_it_shapes_the_loop": "MetaLearner.analyze_strategies maintains a StrategyScore per ImprovementCategory via an EMA of success_rate and in_cooldown flags, biasing the reason stage toward historically successful categories.",
      "loop_objects_touched": ["MetaLearner", "StrategyScore"],
      "wiring": {
        "inputs_from": ["improvement outcome records"],
        "outputs_to": ["next improvement selection", "reason stage"]
      },
      "touch_interaction": {
        "gesture": "flick",
        "canvas_action": "Flicking the strategy panel reorders category bars by their current StrategyScore effectiveness.",
        "visual": "Horizontal bars re-sort with a smooth slide; the top category glows green, a cooling-down one greys out with a clock icon."
      },
      "mini_scenario": "ErrorHandling holds the highest EMA this week, so the MetaLearner ranks it first and the agent picks an error-recovery improvement.",
      "pitfall": "Respect in_cooldown; re-picking a category that just failed before its cooldown expires ignores the very signal the MetaLearner recorded."
    },
    {
      "id": "cognitive-09",
      "title": "Index a new Entity into the knowledge graph",
      "loop_stage": "perceive",
      "pattern": "structural-indexing",
      "intent": "Add a code symbol to the queryable graph so cross-file references resolve.",
      "how_it_shapes_the_loop": "A new Entity (Function, Struct, Trait...) is inserted with its RelationTypes (Calls, Uses, Implements), extending the knowledge graph the perceive stage queries to ground reasoning in real code.",
      "loop_objects_touched": ["Entity", "RelationType", "SymbolIndex"],
      "wiring": {
        "inputs_from": ["source scan", "SymbolIndex"],
        "outputs_to": ["knowledge graph", "RAG grounding"]
      },
      "touch_interaction": {
        "gesture": "drag",
        "canvas_action": "Dragging a new symbol onto the graph canvas snaps it into place and auto-draws its RelationType edges.",
        "visual": "The Entity node lands and edges fan out to callers and callees, each edge labeled by its RelationType."
      },
      "mini_scenario": "A newly written function is dragged into the graph; Calls edges auto-connect it to its callees so future queries can traverse it.",
      "pitfall": "Relations must reflect actual code, not naming similarity; inferring an Implements edge from a matching name creates false graph paths."
    },
    {
      "id": "cognitive-10",
      "title": "Traverse RelationTypes to answer a cross-file question",
      "loop_stage": "reason",
      "pattern": "graph-traversal",
      "intent": "Follow Calls/Uses/DependsOn edges to gather the Entities a question spans.",
      "how_it_shapes_the_loop": "The reason stage walks RelationType edges from a seed Entity to collect a connected subgraph, grounding multi-hop reasoning in real dependencies rather than guesses.",
      "loop_objects_touched": ["Entity", "RelationType"],
      "wiring": {
        "inputs_from": ["seed Entity", "knowledge graph"],
        "outputs_to": ["LlmContext", "reason stage"]
      },
      "touch_interaction": {
        "gesture": "draw-connection",
        "canvas_action": "Drawing along an edge from a node walks the graph, lighting each Entity reached hop by hop.",
        "visual": "A traveling spark runs the edges; visited nodes light up in sequence and dim behind the spark's trail."
      },
      "mini_scenario": "Asked what breaks if a struct changes, the agent draws out from it along DependsOn edges and lights every dependent module.",
      "pitfall": "Bound the traversal depth; an unbounded walk over a densely connected utility Entity pulls half the graph into context."
    },
    {
      "id": "cognitive-11",
      "title": "Run RagEngine retrieval to fetch relevant code chunks",
      "loop_stage": "perceive",
      "pattern": "retrieval-augmentation",
      "intent": "Surface the code most relevant to the query within the token budget.",
      "how_it_shapes_the_loop": "RagEngine.retrieve searches the built index, applies RagConfig (top_k, min relevance), and returns a RetrievedContext the perceive stage folds into the LlmContext as a CodeContext.",
      "loop_objects_touched": ["CodeContext", "LlmContext"],
      "wiring": {
        "inputs_from": ["query", "RagConfig (top_k, thresholds)"],
        "outputs_to": ["SemanticMemory", "LlmContext"]
      },
      "touch_interaction": {
        "gesture": "long-press",
        "canvas_action": "Long-pressing the RAG node runs a retrieval sweep; matching chunks surface as tiles ranked by score.",
        "visual": "Chunk tiles rise from the node ranked top-down by score; below-threshold chunks stay submerged and greyed out."
      },
      "mini_scenario": "On a query about caching, RagEngine.retrieve surfaces the three highest-scoring cache chunks and drops a near-duplicate before assembly.",
      "pitfall": "Dedup before the budget cut; returning two near-identical chunks wastes budget that a distinct relevant chunk needed."
    },
    {
      "id": "cognitive-12",
      "title": "Propose a self-modification with a RiskLevel",
      "loop_stage": "reason",
      "pattern": "risk-tagged-proposal",
      "intent": "Suggest a concrete change to the agent's own code, tagged by risk before review.",
      "how_it_shapes_the_loop": "ModificationEngine.propose emits a ProposedModification (ModificationType, target, risk_level: RiskLevel), so the reason stage hands the act stage a change carrying its own risk gate.",
      "loop_objects_touched": ["ProposedModification", "SelfModel"],
      "wiring": {
        "inputs_from": ["IntrospectionEngine result", "MetaLearner ranking"],
        "outputs_to": ["validate / execute review path", "act stage"]
      },
      "touch_interaction": {
        "gesture": "long-press",
        "canvas_action": "Long-pressing a target module conjures a ProposedModification card with a risk dial the user can read before approving.",
        "visual": "The proposal card floats up with a risk dial (Low green, Critical red); a high-RiskLevel dial pulses to demand attention."
      },
      "mini_scenario": "The agent proposes refactoring its context assembler, tagged Medium risk, and the change routes through validate before any edit executes.",
      "pitfall": "RiskLevel must gate execution; treating a Critical proposal like a Low one lets a dangerous self-edit skip the review it warrants."
    },
    {
      "id": "cognitive-13",
      "title": "Track a CodeModification against the SelfModel",
      "loop_stage": "learn",
      "pattern": "self-model-update",
      "intent": "Keep the agent's self-representation current after it changes its own code.",
      "how_it_shapes_the_loop": "SelfReferenceSystem.track_modification records an applied CodeModification (file_path, change_type, success) into the SelfModel's recent_changes, so subsequent introspection reflects the agent's true, evolved self.",
      "loop_objects_touched": ["CodeModification", "SelfModel"],
      "wiring": {
        "inputs_from": ["applied change", "act stage"],
        "outputs_to": ["SelfModel.recent_changes", "introspection"]
      },
      "touch_interaction": {
        "gesture": "tap",
        "canvas_action": "Tapping the applied-change badge writes it into the self node's change log, incrementing the version.",
        "visual": "The self node's version chip ticks up; a change entry slides into its log with an Add/Refactor icon."
      },
      "mini_scenario": "After splitting a module, track_modification logs the CodeModification so the next introspection knows the new structure.",
      "pitfall": "Record failures too; logging only successful modifications makes the SelfModel believe every attempt worked."
    },
    {
      "id": "cognitive-14",
      "title": "Advance the PDVR CyclePhase",
      "loop_stage": "control",
      "pattern": "cycle-phase-advance",
      "intent": "Drive the cognitive state machine through Plan, Do, Verify, Reflect.",
      "how_it_shapes_the_loop": "CognitiveState.advance_phase steps cycle_phase through CyclePhase (Plan -> Do -> Verify -> Reflect), giving the control layer an explicit phase that dictates what the loop does next.",
      "loop_objects_touched": ["CognitiveState", "CyclePhase"],
      "wiring": {
        "inputs_from": ["phase completion signal"],
        "outputs_to": ["next stage dispatch"]
      },
      "touch_interaction": {
        "gesture": "flick",
        "canvas_action": "Flicking the phase ring rotates it to the next PDVR segment, arming the corresponding stage.",
        "visual": "A four-segment ring rotates; the active segment (Plan/Do/Verify/Reflect) lights, the others dim to grey."
      },
      "mini_scenario": "The Do phase completes, the user flicks the ring to Verify, and the loop dispatches the verification checks.",
      "pitfall": "Never skip Verify to reach Reflect; reflecting on an unverified outcome captures lessons from a result that may be wrong."
    },
    {
      "id": "cognitive-15",
      "title": "Adapt the budget allocation from observed usage",
      "loop_stage": "learn",
      "pattern": "usage-driven-adaptation",
      "intent": "Correct the token split when real usage drifts from the TaskType preset.",
      "how_it_shapes_the_loop": "TokenBudgetAllocator.record_usage accumulates UsageSnapshots and adapt() shifts future AllocationRatios toward the observed distribution, so the learn stage tunes control-layer allocation over time.",
      "loop_objects_touched": ["TokenBudget"],
      "wiring": {
        "inputs_from": ["UsageSnapshot history"],
        "outputs_to": ["next AllocationRatios split"]
      },
      "touch_interaction": {
        "gesture": "two-finger-rotate",
        "canvas_action": "Rotating the budget wheel while a drift overlay is showing nudges the preset toward the observed distribution.",
        "visual": "A ghost overlay shows actual usage; the wheel eases toward it, the arcs settling on a corrected split."
      },
      "mini_scenario": "Debugging tasks kept overspending episodic recall; adapt nudges the Debugging preset to give episodic more of the window next time.",
      "pitfall": "Adapt on a stable sample, not one outlier run; a single anomalous session should not rewrite the standing allocation."
    },
    {
      "id": "cognitive-16",
      "title": "Link related Episodes to surface a pattern",
      "loop_stage": "learn",
      "pattern": "episode-linking",
      "intent": "Connect Episodes that share a cause so recurring patterns become visible.",
      "how_it_shapes_the_loop": "Setting an Episode's related_episodes (or with_related) stitches experiences into chains the learn stage can mine, turning isolated events into a detectable pattern.",
      "loop_objects_touched": ["Episode", "Lesson"],
      "wiring": {
        "inputs_from": ["new Episode", "prior Episodes"],
        "outputs_to": ["pattern detection", "MetaLearner"]
      },
      "touch_interaction": {
        "gesture": "draw-connection",
        "canvas_action": "Drawing a line between two Episode tiles links them; a shared-cause label appears on the connecting edge.",
        "visual": "A thread ties the tiles; when a third joins the chain, the whole chain flashes to signal a detected pattern."
      },
      "mini_scenario": "Three timeout Episodes get linked; the chain flashes and the MetaLearner flags a recurring latency pattern to address.",
      "pitfall": "Link by genuine causal relation, not mere co-occurrence; false links invent patterns that mislead the next planning pass."
    },
    {
      "id": "cognitive-17",
      "title": "Evict a stale MemoryEntry under capacity pressure",
      "loop_stage": "control",
      "pattern": "capacity-eviction",
      "intent": "Free tier capacity by demoting or dropping the least valuable memory.",
      "how_it_shapes_the_loop": "When a tier hits its MemoryConfig capacity, HierarchicalMemory demotes the lowest Importance/access MemoryEntry to ArchiveMemory or drops it, keeping the perceive stage's recall lean.",
      "loop_objects_touched": ["MemoryEntry", "HierarchicalMemory"],
      "wiring": {
        "inputs_from": ["tier capacity limit (MemoryConfig)"],
        "outputs_to": ["ArchiveMemory / drop"]
      },
      "touch_interaction": {
        "gesture": "flick",
        "canvas_action": "Flicking a full tier ejects its weakest tile downward toward Archive or off the canvas.",
        "visual": "The tier flashes 'full'; the weakest tile slides down into the Archive band or dissolves at the edge."
      },
      "mini_scenario": "ShortTerm fills up, so the least-accessed entry flicks down to Archive, making room for the current session's memories.",
      "pitfall": "Never evict by recency alone; a rarely accessed but Critical-importance entry must survive over frequent trivia."
    },
    {
      "id": "cognitive-18",
      "title": "Detect the TaskType from the query up front",
      "loop_stage": "perceive",
      "pattern": "task-type-detection",
      "intent": "Classify the query so budget and recall strategy fit its shape.",
      "how_it_shapes_the_loop": "TokenBudgetAllocator.suggest_task_type inspects the query and returns a TaskType (Conversation, CodeAnalysis, SelfImprovement, Debugging...), steering the control layer's budget split and the perceive stage's recall mix.",
      "loop_objects_touched": ["TokenBudget", "LlmContext"],
      "wiring": {
        "inputs_from": ["raw query"],
        "outputs_to": ["TokenBudgetAllocator", "recall strategy"]
      },
      "touch_interaction": {
        "gesture": "tap",
        "canvas_action": "Tapping the incoming query node classifies it, stamping a TaskType badge that recolors the downstream budget wheel.",
        "visual": "A type badge (e.g. 'Debugging') snaps onto the query; the budget wheel downstream recolors to match the preset."
      },
      "mini_scenario": "The query 'why does this panic' is tapped and classified Debugging, so episodic recall of past errors is boosted.",
      "pitfall": "Misclassification cascades; tagging a SelfImprovement query as Conversation starves the semantic recall it depended on."
    },
    {
      "id": "cognitive-19",
      "title": "Extract a Lesson from a verified outcome",
      "loop_stage": "learn",
      "pattern": "lesson-distillation",
      "intent": "Distill a reusable insight from what just succeeded or failed.",
      "how_it_shapes_the_loop": "During the Reflect CyclePhase, a Lesson is distilled from the verified Episode outcome and stored, so the learn stage turns a single event into guidance the next reason pass can apply.",
      "loop_objects_touched": ["Lesson", "Episode", "CognitiveState", "CyclePhase"],
      "wiring": {
        "inputs_from": ["verified Episode outcome"],
        "outputs_to": ["LongTermMemory", "future reasoning"]
      },
      "touch_interaction": {
        "gesture": "long-press",
        "canvas_action": "Long-pressing a completed Episode squeezes out a distilled Lesson token that docks to the LongTerm shelf.",
        "visual": "A droplet of insight condenses from the Episode tile and slides onto a lesson shelf, glowing when later reused."
      },
      "mini_scenario": "A verified fix yields the Lesson 'validate path before write', which docks to LongTerm and surfaces on the next file task.",
      "pitfall": "Distill only from verified outcomes; a Lesson drawn from an unverified success encodes a false rule the agent will repeat."
    },
    {
      "id": "cognitive-20",
      "title": "Refresh the WorkingContext at the start of a turn",
      "loop_stage": "foundation",
      "pattern": "working-set-refresh",
      "intent": "Reset the immediate context to the current conversation, task, and active code.",
      "how_it_shapes_the_loop": "The WorkingContext (messages, current task, ActiveCodeContext) is refreshed each turn via add_message and active-code updates, so the foundation layer always assembles the LlmContext around the true present state.",
      "loop_objects_touched": ["WorkingContext", "LlmContext", "CognitiveState"],
      "wiring": {
        "inputs_from": ["latest messages (add_message)", "current task"],
        "outputs_to": ["CognitiveSystem.build_context"]
      },
      "touch_interaction": {
        "gesture": "double-tap",
        "canvas_action": "Double-tapping the working node clears its stale slots and repopulates them with the current turn's state.",
        "visual": "The working node blinks, old slots dim out, and fresh message/task/code slots fill in with a clean-slate shimmer."
      },
      "mini_scenario": "At a new turn the user double-taps working; last turn's active_code clears and the file now under edit populates it.",
      "pitfall": "Stale active code in WorkingContext misleads reasoning; failing to refresh makes the agent reason about a file it already moved past."
    }
  ]
}