# Plan: Recursive Cognitive Kernel

## Objective
Implement a recursive `cognitive-cycle` that allows the agent to chain multiple actions (tool calls) and observe results before finalising a response, similar to SOTA agents like Claude Code.

## Key Files & Context
- `projects/opencortex/docs/README.org`: The primary source of truth for the kernel logic.
- `projects/opencortex/src/core.lisp`: Tangled kernel implementation.
- `projects/opencortex/src/symbolic.lisp`: Contains the `decide` logic.

## Implementation Steps

### 1. State & Interruption
- Define `*interrupt-flag*` and `*interrupt-lock*` in the kernel state.
- Update the `perceive` function to detect the `:interrupt` sensor stimulus and set the flag.

### 2. Recursive `cognitive-cycle`
- Modify `cognitive-cycle` to accept an optional `depth` parameter (default 0).
- Add a check at the beginning of the loop for `depth > 10` or `*interrupt-flag*`.
- Refactor the loop to capture the return value of `dispatch-action`.
- If the action was a "Tool Call" (any target except `:emacs` or `:system-message`) and produced a result, recursively call `cognitive-cycle` with a new `:EVENT` containing that result.

### 3. Actuator Harmonization
- Ensure `execute-system-action` (for `:eval`) returns meaningful results that can be fed back into the loop.
- Update `dispatch-action` to return the result of the actuator call.

### 4. Literate Tangle & Load
- Apply these changes to the `README.org` document.
- Tangle to update `.lisp` files.
- Hot-load the updated code into the live image via Swank.

## Verification & Testing
- **Multi-turn Test**: In the chat, ask the agent to "Calculate the sum of 5+5 and then multiply it by 2 using eval".
- **Introspection Check**: Verify the `*opencortex-reasoning*` buffer shows the two distinct `eval` steps.
- **Interrupt Test**: Trigger a long-running or recursive task and press `C-c C-k` in Emacs to verify the loop halts.
