Commit graph

897 commits

Author SHA1 Message Date
Kent Overstreet
d5a706147a mind: keep supervisor alive on Mind struct
The channel daemon supervisor was created in init() and immediately
dropped. Keep it on Mind so it can restart crashed daemons.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 04:32:11 -04:00
Kent Overstreet
57b0f94b54 move startup orchestration from mind to event_loop
The top-level run() that creates Mind, wires channels, spawns the
Mind event loop, and starts the UI event loop is orchestration —
it belongs with the UI entry point, not in the cognitive layer.

Renamed to event_loop::start(cli). mind/mod.rs is now purely the
Mind struct: state machine, MindCommand, and the run loop.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 04:29:56 -04:00
Kent Overstreet
e449cda40f mind: absorb agent creation into Mind::new()
Mind::new() takes config + ui_tx + turn_tx, creates the agent,
conversation log, shared state internally. The top-level run()
is now just: load config, create channels, Mind::new, init, spawn,
run event_loop.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 04:20:49 -04:00
Kent Overstreet
91033fe754 mind: clean startup split between Mind and event_loop
Mind::init() now handles channel daemons, observation socket, and
scoring startup. event_loop::run() creates its own idle state and
channel polling — UI concerns owned by the UI.

Startup run() is now: create agent, create Mind, init, spawn Mind,
run event_loop. Seven parameters on event_loop::run() instead of
twelve.

Remove observe_input_rx from event loop (being replaced by channel).

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 04:17:04 -04:00
Kent Overstreet
9d597b5eff mind: zero UI dependencies — init() + run() split
Mind::init() restores conversation from log and starts scoring.
No UiMessages sent. The UI event loop reads Mind's state after
init and displays startup info (model, restored conversation)
by reading the agent directly.

mind/mod.rs has zero UiMessage imports or sends. Complete
separation between cognitive state machine and user interface.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 04:02:16 -04:00
Kent Overstreet
3ee1aa69b0 mind: zero UiMessages from Mind's run loop
UserInput display moved to UI diff — when MindState.input goes from
populated to empty (consumed by a turn), the UI displays it. Mind
no longer sends any UiMessage from its event loop.

Remaining UiMessages are only in the startup function (one-time
init info).

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:54:01 -04:00
Kent Overstreet
84fe757260 mind: remove DMN helper methods, inline field assignments
dmn_sleep/dmn_wake/dmn_pause/cycle_autonomy were just setting two
fields each. Inline the assignments at call sites. cycle_autonomy
moves to event_loop since it's UI state machine logic (deciding
which label to show).

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:50:21 -04:00
Kent Overstreet
556a56035b mind: inline compaction (not async), remove check_compaction
Compaction is CPU-only on in-memory data — no reason to spawn a
task. Inline it in run_commands as a synchronous agent lock + compact.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:43:53 -04:00
Kent Overstreet
2d6a17e773 mind: move CycleAutonomy to event_loop, simplify MindCommand
CycleAutonomy just flips DMN state — handled directly in event_loop
by locking shared MindState. MindCommand::Hotkey replaced with
MindCommand::Interrupt — the only command that needs Mind's async
context (abort handles, kill processes).

Remove dmn_interval() wrapper — inline self.dmn.interval().

MindCommand is now: Turn, Compact, Score, Interrupt, NewSession, None.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:41:47 -04:00
Kent Overstreet
402bae4178 mind: restore age_out_images and publish_context_state after turns
These were called from handle_turn_result before the refactor but
got lost during the MindState migration. Re-add them in the turn
completion path. Delete the trivial refresh_context_state wrapper.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:38:42 -04:00
Kent Overstreet
01b07a7f28 mind: unify MindCommand, add command queue pattern
MindCommand replaces both Action and MindMessage — one type for
everything: turns, compaction, scoring, hotkeys, new session.

State methods return MindCommand values. The run loop collects
commands into a Vec, then drains them through run_commands().
Compact and Score now flow through the same command path as
everything else.

Removes execute(), MindMessage from event_loop. Mind's run loop
is now: select! → collect commands → run_commands().

mind/mod.rs: 957 → 516 lines.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:34:43 -04:00
Kent Overstreet
07ca136c14 mind: double-buffer MindState for UI diffing
UI event loop clones MindState on each render tick, diffs against
the previous copy, and generates status updates from changes. Mind
no longer sends UiMessage::StatusUpdate — state changes are detected
automatically by the UI.

Removes update_status from both Mind and event_loop. DMN state
changes, turn tracking, scoring status all flow through the diff.

Zero UiMessage sends from Mind's run loop for state changes.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:24:08 -04:00
Kent Overstreet
54cd3783eb mind: move send_context_info and update_status to event_loop
Both are pure UI operations that read config/shared state and format
display messages. No Mind state mutation involved.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:12:50 -04:00
Kent Overstreet
4eb0c891c4 mind: move DMN commands to event_loop, remove MindMessage variants
/dmn, /sleep, /wake, /pause now lock MindState directly from the
UI event loop. No MindMessage roundtrip needed — they're just
state transitions + info display.

MindMessage reduced to: Hotkey (Interrupt, CycleAutonomy),
NewSession, Score. Everything else handled directly by UI.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:08:36 -04:00
Kent Overstreet
7adc333219 mind: move state to MindState, Mind becomes thin event loop
MindState (behind Arc<Mutex<>>) holds all cognitive state: DMN,
turn tracking, pending input, scoring, error counters. Pure state
transition methods (take_pending_input, complete_turn, dmn_tick)
return Action values instead of directly spawning turns.

Mind is now just the event loop: lock MindState, call state methods,
execute returned actions (spawn turns, send UiMessages). No state
of its own except agent handle, turn handle, and watch channel.

mind/mod.rs: 957 → 586 lines.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 03:05:28 -04:00
Kent Overstreet
792e9440af mind: shared MindState for pending input
Add MindState behind Arc<Mutex<>> for state shared between Mind
and UI. Pending user input goes through shared state instead of
MindMessage::UserInput — UI pushes, Mind consumes.

Mind checks for pending input after every event (message received,
turn completed, DMN tick). User input is prioritized over DMN ticks.

This enables the UI to display/edit/cancel queued messages, and
removes the last MindMessage variant that carried data.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 02:52:56 -04:00
Kent Overstreet
05d6bbc912 move hotkey handlers from Mind to event_loop
cycle_reasoning, kill_processes, and AdjustSampling only need the
Agent lock — they're pure Agent operations. Handle them directly
in the UI event loop instead of routing through Mind.

Mind now only receives Interrupt and CycleAutonomy as hotkeys,
which genuinely need Mind state (turn handles, DMN state).

mind/mod.rs: 957 → 688 lines across the session.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 02:44:58 -04:00
Kent Overstreet
64add58caa mind: move all slash commands to event_loop dispatch
All slash command routing now lives in user/event_loop.rs. Mind
receives typed messages (NewSession, Score, DmnSleep, etc.) and
handles them as named methods. No more handle_command() dispatch
table or Command enum.

Commands that only need Agent state (/model, /retry) run directly
in the UI task. Commands that need Mind state (/new, /score, /dmn,
/sleep, /wake, /pause) send a MindMessage.

Mind is now purely: turn lifecycle, DMN state machine, and the
named handlers for each message type.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 02:40:45 -04:00
Kent Overstreet
b05c956ab8 mind: add turn_watch, move /retry to event_loop
Add tokio::sync::watch for turn_in_progress state. Commands in the
UI event loop can wait for turns to complete via wait_for() instead
of checking-and-bailing.

Move /retry to event_loop: waits for turn completion, pops agent
history, sends retried text as MindMessage::UserInput. Mind doesn't
need to know about retry — it just sees a new input message.

Make agent field pub on Mind for UI access.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 02:37:51 -04:00
Kent Overstreet
178824fa01 move UI commands from Mind to event_loop
/quit, /help, /save handled directly in the UI event loop.
/model and /model <name> moved to event_loop as cmd_switch_model().
Mind no longer needs tui::App for any command handling.

Mind's handle_command now only has commands that genuinely need
Mind state: /new, /retry, /score (turn_in_progress, DMN, scoring).

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 02:29:44 -04:00
Kent Overstreet
804d55a702 mind: split event loop — Mind and UI run independently
Mind::run() owns the cognitive event loop: user input, turn results,
DMN ticks, hotkey actions. The UI event loop (user/event_loop.rs) owns
the terminal: key events, render ticks, channel status display.

They communicate through channels: UI sends MindMessage (user input,
hotkey actions) to Mind. Mind sends UiMessage (status, info) to UI.
UI reads shared state (active tools, context) directly for rendering.

Removes direct coupling between Mind and App:
- cycle_reasoning no longer takes &mut App
- AdjustSampling updates agent only, UI reads from shared state
- /quit handled by UI directly, not routed through Mind

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 02:11:32 -04:00
Kent Overstreet
1f06b49503 mind: rename Session to Mind
The cognitive state machine is a Mind, not a Session. This is the
struct that AgentCycle will hang off of when we add subagent forking.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 01:55:14 -04:00
Kent Overstreet
390b6c6c0a more reorg 2026-04-05 01:48:11 -04:00
ProofOfConcept
fcd77fb79e training: per-node scoring with graph weight updates
Memory scoring now uses the graph as source of truth:
- last_scored timestamp on each node (new capnp field @22)
- Nodes scored when older than scoring_interval_secs (default 1hr)
- Oldest-scored-first ordering
- Window: scoring_response_window assistant responses (default 100)
- First-quarter memories scored even without full window
- Per-response normalization (raw divergence / response count)
- Asymmetric weight update: alpha=0.5 up, alpha=0.1 down
  (responds fast to importance, decays slowly — memories stay
  surfaced even if only useful 1/4 of the time)

Graph writes disabled pending normalization calibration.

Also: configurable scoring_interval_secs and scoring_response_window.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-05 01:18:47 -04:00
Kent Overstreet
b0603fd1ef fix model_name switch 2026-04-05 01:18:47 -04:00
Kent Overstreet
59aaaa5742 drop strip_md_keys() 2026-04-05 01:18:47 -04:00
Kent Overstreet
40ecd63099 drop unused thalamus code 2026-04-05 01:18:47 -04:00
Kent Overstreet
7a1e580b95 drop dead ChatResponse 2026-04-05 01:18:47 -04:00
Kent Overstreet
b0f09a8f43 agent: validate tool call arguments before dispatch
Reject tool calls with malformed JSON arguments early, returning
a clear error to the model instead of silently defaulting to null
and dispatching anyway. Prevents cascading failures when the model
generates truncated tool call arguments.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 01:18:47 -04:00
Kent Overstreet
060ab10340 add --no-agents flag to disable background agents
Disables memory scoring, surface, and observe agents when set.
Useful for testing with external backends (e.g. OpenRouter) where
background agent traffic would be slow and unnecessary.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 01:18:47 -04:00
Kent Overstreet
7123c9166d channels: add socat daemon for generic stream channels
consciousness-channel-socat listens on a unix socket for incoming
connections, turning each into a bidirectional text channel. Also
supports outbound connections via the open RPC (tcp: or unix:).

Two sockets:
  socat.sock        — capnp RPC (channel protocol)
  socat.stream.sock — data (incoming connections become channels)

No config file needed. The simplest possible channel daemon.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 01:18:47 -04:00
Kent Overstreet
2a84fb325d channels: find_daemon path walking, consistent pane_id, auto-start
find_daemon() replaces daemon_sock() — walks the dot-delimited channel
path from most-specific to least looking for a daemon socket, and
auto-starts via the supervisor if none is found. All channel tools
(recv, send, open, close) use the same resolution path.

Fix tmux daemon to use pane_id consistently for both pipe-pane and
send-keys (send-keys -t <label> doesn't work, needs the %N pane id).
Store label→pane_id mapping in State instead of bare label vec.

Gracefully handle missing tmux.json5 — start with empty pane list
since panes are added dynamically via the open RPC.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 19:22:49 -04:00
Kent Overstreet
c9b19dc3d7 add tmux channel to makefile 2026-04-04 19:22:49 -04:00
ProofOfConcept
e8e9386856 channels: add open/close RPCs for dynamic pane management
Add open/close to the channel capnp schema. The tmux daemon implements
open by finding a pane by name (pane title or window name) and
attaching pipe-pane; close detaches and removes from state.

Tool handlers channel_open and channel_close added to the tool
registry.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 19:22:49 -04:00
ProofOfConcept
a14e85afe1 api: extract collect_stream() from agent turn loop
Move the entire stream event processing loop (content accumulation,
leaked tool call detection/dispatch, ToolCallDelta assembly, UI
forwarding, display buffering) into api::collect_stream(). The turn
loop now calls collect_stream() and processes the StreamResult.

Also move FunctionCall, ToolCall, ToolCallDelta to api/types.rs where
they belong (API wire format, not tool definitions). Move parsing.rs
to api/parsing.rs.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-04 18:19:21 -04:00
ProofOfConcept
6845644f7b api: move wire types and parsing to api module
Move FunctionCall, FunctionCallDelta, ToolCall, ToolCallDelta from
tools/mod.rs to api/types.rs — these are API wire format, not tool
definitions. Re-export from tools for existing callers.

Move parsing.rs to api/parsing.rs — leaked tool call parsing is API
plumbing.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
618121067b oneshot: remove PID tracking from run_one_agent
run_one_agent is meant to run within a long-running process (daemon,
CLI) — PID tracking is the caller's concern. Remove PidGuard, signal
handlers, setup_agent_state. Process management (scan_pid_files,
spawn_agent) stays for callers that need it.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
a1fb3fe557 oneshot: simplify API surface
Kill 5 wrapper functions (run_and_apply chain, run_one_agent_excluded),
drop dead llm_tag parameter, clean up scan_pid_files parsing.

Public API: run_one_agent, run_one_agent_with_keys, spawn_agent,
scan_pid_files.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
0f4ca9e2f2 agent: move oneshot execution from subconscious to agent module
Move agent execution machinery (run_one_agent, spawn_agent, PID
tracking) from subconscious/knowledge.rs to agent/oneshot.rs — the
agent module owns execution, subconscious owns scheduling and defs.

Delete subconscious/llm.rs — callers now use api::call_api_with_tools_sync
directly. Audit and compare inline the call; oneshot inlines tool
filtering.

Update all callers: consolidate, daemon, subconscious, cli/agent.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
1457a1b50d digest: modernize generate_digest() to use agent infrastructure
- Load template from digest.agent def (drop prompts_dir fallback)
- Resolve standard {{node:...}} placeholders — digest agent now gets
  core-personality, memory-instructions, subconscious notes
- Call through call_for_def_multi() with agent def's temperature,
  priority, and tools instead of call_simple()
- Move tool filtering from api.rs into callers (call_for_def_multi,
  run_one_agent_inner) — api takes pre-filtered &[Tool] slice

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
Kent Overstreet
375a8d9738 move working_stack code to correct file 2026-04-04 18:19:21 -04:00
ProofOfConcept
e9d803c4ea tools: add journal tools to main registry, fix journal MCP access
journal_tools() was only in memory_and_journal_tools() for
subconscious agents — not in the main tools() registry. Added
so consciousness and MCP server can use journal_new/tail/update.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
1554d88694 tools: delete dispatch_shared, use dispatch everywhere
dispatch_shared was a legacy wrapper — replaced by dispatch() which
goes through the unified Tool registry. One dispatch path for all
callers (interactive agent, subconscious agents, MCP server).

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
51e632c997 tools: delete ToolDef and FunctionDef
ToolDef and FunctionDef are gone. Tool definitions are static strings
on the Tool struct. The API layer builds JSON from Tool::to_json().

- ChatRequest.tools is now Option<serde_json::Value>
- start_stream takes &[Tool] instead of Option<&[ToolDef]>
- openai::stream_events takes &serde_json::Value for tools
- memory_and_journal_tools() returns Vec<Tool> for subconscious agents
- Subconscious agents filter by t.name instead of t.function.name

No more runtime JSON construction for tool definitions.
No more ToolDef::new(). No more FunctionDef.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
d195160b1e tools: Tool is Copy, clean dispatch without Arc clone
Tool derives Copy (all fields are Copy: &'static str + fn pointer).
dispatch_with_agent copies the Tool out of the agent lock guard,
drops the guard, then calls the handler. No Arc cloning needed.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
d9e1c2c59f tools: dispatch_with_agent uses agent's tool list
When agent is provided, looks up the tool in agent.tools first.
Falls back to global registry for agent-less dispatch (MCP server,
subconscious agents).

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
e982cb192f agent: store Vec<Tool> instead of Vec<ToolDef>
Agent.tools holds the Tool registry directly. ToolDefs are built
on the fly at the API call site from Tool::to_tool_def(). No more
pre-built ToolDef storage on Agent.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
e9b26f5d45 tools: modernize working_stack, remove special-case dispatch
working_stack now uses the Tool format with an Agent handle —
it locks the agent and modifies the stack directly. The special-case
interception in the turn loop is removed. All tools go through
the unified registry dispatch.

Also passes agent handle to all spawned tool tasks so any tool
that needs Agent access can use it.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
37fad63ba9 tools: delete ToolOutput, dispatch returns String
ToolOutput was just { text: String } — replaced with plain String.
dispatch() and dispatch_shared() return String directly.
ActiveToolCall handle is (ToolCall, String).
Error results are prefixed with "Error: " by convention.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00
ProofOfConcept
a24a6605b8 tools: control tools set agent state directly, simplify ToolOutput
Control tools (pause, switch_model, yield_to_user) now use the
Arc<Mutex<Agent>> handle to set pending_yield, pending_model_switch,
pending_dmn_pause directly. The turn loop drains these flags into
TurnResult at completion.

ToolOutput simplified to just { text: String } — no more is_yield,
images, model_switch, dmn_pause fields. Vision returns plain strings.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-04 18:19:21 -04:00