samagotchi 0.194.0 → 0.196.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +80 -1
- data/docs/architecture.md +2 -1
- data/docs/bundle-authoring.md +23 -23
- data/docs/bundles.md +201 -7
- data/docs/cli.md +5 -1
- data/docs/configuration.md +25 -4
- data/docs/context.md +13 -3
- data/docs/guardrails.md +6 -4
- data/docs/hooks.md +3 -1
- data/docs/internals/attached-context.md +2 -0
- data/docs/internals/block-renderers.md +4 -1
- data/docs/internals/bridge.md +4 -0
- data/docs/internals/engine.md +15 -2
- data/docs/internals/knowledge.md +215 -0
- data/docs/internals/pages.md +17 -9
- data/docs/internals/panes.md +51 -13
- data/docs/internals/participants.md +15 -3
- data/docs/internals/peers.md +14 -9
- data/docs/internals/session-lifecycle.md +26 -12
- data/docs/internals/web-frontend.md +64 -27
- data/docs/memory.md +4 -2
- data/docs/pages.md +72 -1
- data/docs/peers.md +25 -14
- data/docs/plugins.md +132 -6
- data/docs/releasing.md +1 -1
- data/docs/scheduling.md +4 -0
- data/docs/sessions.md +9 -2
- data/docs/system-one.md +3 -1
- data/docs/testing.md +3 -1
- data/lib/samagotchi/bundle_manifest.rb +30 -5
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +2 -2
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +3 -3
- data/lib/samagotchi/bundles/coordinator/manifest.yml +7 -4
- data/lib/samagotchi/bundles/coordinator/plugin.rb +15 -5
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +10 -0
- data/lib/samagotchi/bundles/core/manifest.yml +2 -2
- data/lib/samagotchi/bundles/cost-cap/manifest.yml +4 -4
- data/lib/samagotchi/bundles/cost-cap/plugin.rb +54 -3
- data/lib/samagotchi/bundles/dev/manifest.yml +3 -2
- data/lib/samagotchi/bundles/github-pr/manifest.yml +4 -2
- data/lib/samagotchi/bundles/knowledge/adapters.rb +205 -0
- data/lib/samagotchi/bundles/knowledge/catalog.rb +151 -0
- data/lib/samagotchi/bundles/knowledge/chi_adapters.rb +178 -0
- data/lib/samagotchi/bundles/knowledge/chunker.rb +55 -0
- data/lib/samagotchi/bundles/knowledge/github.rb +246 -0
- data/lib/samagotchi/bundles/knowledge/manifest.yml +27 -0
- data/lib/samagotchi/bundles/knowledge/plugin.rb +807 -0
- data/lib/samagotchi/bundles/knowledge/query.rb +30 -0
- data/lib/samagotchi/bundles/knowledge/query_log.rb +55 -0
- data/lib/samagotchi/bundles/knowledge/refresh.rb +133 -0
- data/lib/samagotchi/bundles/knowledge/store.rb +264 -0
- data/lib/samagotchi/bundles/reflect/manifest.yml +17 -0
- data/lib/samagotchi/bundles/reflect/scripts/reflect.rb +439 -0
- data/lib/samagotchi/bundles/reflect/skill_reflect.md +100 -0
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/context_change.rb +44 -0
- data/lib/samagotchi/context_note.rb +1 -1
- data/lib/samagotchi/context_providers.rb +14 -0
- data/lib/samagotchi/context_sources.rb +3 -25
- data/lib/samagotchi/engine/plugins.rb +21 -0
- data/lib/samagotchi/events.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +12 -4
- data/lib/samagotchi/guardrails/review_paths.rb +13 -3
- data/lib/samagotchi/guardrails/targets.rb +24 -2
- data/lib/samagotchi/guardrails_command.rb +3 -9
- data/lib/samagotchi/hook_label.rb +19 -0
- data/lib/samagotchi/hook_routes.rb +3 -2
- data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
- data/lib/samagotchi/memory_bundle/builder.rb +2 -1
- data/lib/samagotchi/memory_bundle/installed_copy.rb +2 -1
- data/lib/samagotchi/page_command.rb +90 -6
- data/lib/samagotchi/pages/launch.rb +16 -8
- data/lib/samagotchi/pages/runner.rb +7 -2
- data/lib/samagotchi/pages/runtime.rb +62 -2
- data/lib/samagotchi/pages.rb +4 -0
- data/lib/samagotchi/panes/kitty.rb +20 -5
- data/lib/samagotchi/panes.rb +81 -12
- data/lib/samagotchi/participants.rb +17 -6
- data/lib/samagotchi/peer_exchanges.rb +1 -2
- data/lib/samagotchi/peer_note.rb +38 -5
- data/lib/samagotchi/peer_presence.rb +20 -7
- data/lib/samagotchi/plugin/api.rb +11 -1
- data/lib/samagotchi/plugin/attached_context.rb +46 -1
- data/lib/samagotchi/plugin/chips.rb +125 -0
- data/lib/samagotchi/plugin/context.rb +43 -8
- data/lib/samagotchi/plugin/panes.rb +52 -9
- data/lib/samagotchi/plugin/peer_pairing.rb +2 -2
- data/lib/samagotchi/plugin/peer_start.rb +61 -36
- data/lib/samagotchi/plugin/peers.rb +6 -3
- data/lib/samagotchi/plugin/project.rb +63 -0
- data/lib/samagotchi/plugin/sessions.rb +36 -1
- data/lib/samagotchi/plugin/tool_spec.rb +28 -9
- data/lib/samagotchi/schedules.rb +8 -2
- data/lib/samagotchi/session.rb +22 -9
- data/lib/samagotchi/session_access/local.rb +34 -33
- data/lib/samagotchi/session_chain/sharing.rb +79 -0
- data/lib/samagotchi/session_chain/successor.rb +4 -2
- data/lib/samagotchi/session_chips.rb +133 -0
- data/lib/samagotchi/session_inbox_delivery.rb +11 -11
- data/lib/samagotchi/session_manager/emptiness.rb +4 -1
- data/lib/samagotchi/session_observer.rb +1 -1
- data/lib/samagotchi/session_stats.rb +437 -0
- data/lib/samagotchi/session_stores.rb +3 -0
- data/lib/samagotchi/session_window.rb +30 -0
- data/lib/samagotchi/speaker.rb +228 -0
- data/lib/samagotchi/steer.rb +36 -132
- data/lib/samagotchi/system_one/approval_choice.rb +22 -0
- data/lib/samagotchi/system_one/approval_label.rb +4 -6
- data/lib/samagotchi/system_prompt.rb +3 -1
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +30 -0
- data/lib/samagotchi/testing/boundary.rb +17 -4
- data/lib/samagotchi/testing/bundle.rb +5 -0
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +10 -0
- data/lib/samagotchi/testing/sessions.rb +11 -3
- data/lib/samagotchi/toolkit.rb +11 -4
- data/lib/samagotchi/toolkit_report_command.rb +14 -18
- data/lib/samagotchi/toolkit_suggest.rb +9 -2
- data/lib/samagotchi/tools/registry.rb +48 -18
- data/lib/samagotchi/tools/tool_index.rb +6 -2
- data/lib/samagotchi/turn_excerpts.rb +302 -0
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +9 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +21 -10
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +5 -2
- data/lib/samagotchi/web/plugin_chip_rows.rb +39 -0
- data/lib/samagotchi/web/public/agent_rows.js +1 -0
- data/lib/samagotchi/web/public/app.js +17 -25
- data/lib/samagotchi/web/public/chains.js +0 -26
- data/lib/samagotchi/web/public/command_list.js +2 -3
- data/lib/samagotchi/web/public/composer.js +2 -3
- data/lib/samagotchi/web/public/composer_mount.js +3 -6
- data/lib/samagotchi/web/public/context_chips.js +240 -93
- data/lib/samagotchi/web/public/context_groups.js +84 -0
- data/lib/samagotchi/web/public/draft_follow.js +33 -0
- data/lib/samagotchi/web/public/drafts.js +26 -22
- data/lib/samagotchi/web/public/index.html +32 -0
- data/lib/samagotchi/web/public/peer_chip_kind.js +49 -0
- data/lib/samagotchi/web/public/peer_note.js +2 -2
- data/lib/samagotchi/web/public/peer_row.js +10 -5
- data/lib/samagotchi/web/public/peer_wait.js +4 -2
- data/lib/samagotchi/web/public/plugin_chip.js +55 -0
- data/lib/samagotchi/web/public/plugin_chip_kind.js +48 -0
- data/lib/samagotchi/web/public/send_turn.js +8 -21
- data/lib/samagotchi/web/public/session_actions.js +10 -23
- data/lib/samagotchi/web/public/session_view.js +7 -3
- data/lib/samagotchi/web/public/stream_handlers.js +2 -0
- metadata +33 -1
data/docs/guardrails.md
CHANGED
|
@@ -572,8 +572,10 @@ message:
|
|
|
572
572
|
the owner's other sessions, reminders and schedules and the session's
|
|
573
573
|
attached context: `list_sessions`, `list_reminders`, `list_schedules`,
|
|
574
574
|
`context_read`. Every built-in tool has its place in
|
|
575
|
-
`PairBoundary::TOOL_CLASSES` (a spec fails on one that hasn't);
|
|
576
|
-
|
|
575
|
+
`PairBoundary::TOOL_CLASSES` (a spec fails on one that hasn't);
|
|
576
|
+
- a plugin tool its bundle declared `participant: :deny` (`PairBoundary::BUNDLE_OWNER`,
|
|
577
|
+
[plugins](plugins.md#participant-the-owners-only)), by its name or through `call_tool`, before any
|
|
578
|
+
check or ask; any other plugin's or an MCP server's tool goes on to the path and command checks;
|
|
577
579
|
- `ask_user_question`, with the advice to ask in the reply: nobody on the
|
|
578
580
|
participant's side answers a card.
|
|
579
581
|
|
|
@@ -615,8 +617,8 @@ share`, `/bin/SH -c`, `NOHUP`: a disk that folds case, macOS's default, runs
|
|
|
615
617
|
them as git, chi, sh, nohup; `ShellLex.program_name`), and so is chi or git
|
|
616
618
|
config in a script's text.
|
|
617
619
|
|
|
618
|
-
It asks the owner, fresh and once, before a
|
|
619
|
-
`targets:` name no command or path (`PairBoundary::UNTARGETED`: a browser
|
|
620
|
+
It asks the owner, fresh and once, before a call of a plugin tool not declared
|
|
621
|
+
`participant: :deny`, or of an MCP tool, whose `targets:` name no command or path (`PairBoundary::UNTARGETED`: a browser
|
|
620
622
|
MCP's navigate, say), since nothing here can tell what it reaches.
|
|
621
623
|
|
|
622
624
|
And it **asks the owner** before a `write` or `edit` that may land outside the
|
data/docs/hooks.md
CHANGED
|
@@ -176,7 +176,9 @@ A steer is saved in the session as `{role: "user", kind: "steer", source:
|
|
|
176
176
|
"<bundle>", content: "…"}`, its text raw. The model reads it as a user turn
|
|
177
177
|
led by one line naming the sender: `[Steer from the <bundle> plugin,
|
|
178
178
|
mid-task. Follow it; if it asks for nothing, carry on with the task.]`.
|
|
179
|
-
Its `source` is the hook's bundle (a config or turn hook's label otherwise)
|
|
179
|
+
Its `source` is the hook's bundle (a config or turn hook's label otherwise):
|
|
180
|
+
`HookLabel.source` reads it from a bundle hook's label (`<file> (bundle
|
|
181
|
+
<name>)`), as `SessionStats` does for hook notices by bundle.
|
|
180
182
|
Lines typed into a running turn are saved as `{role: "user", kind: "input",
|
|
181
183
|
content: "…"}`: part of that turn, not a turn of their own. They carry a
|
|
182
184
|
`source` when they are not the user's own: `chi_send` (`chi send -m`),
|
|
@@ -70,6 +70,8 @@ comment is the file map and [../context.md](../context.md#storage) lists it for
|
|
|
70
70
|
`SessionAccess::Remote#context_push`'s body (an older chi web drops it: the answer arrives unlinked). `ContextProviders` resolves a URL to a command source from an installed bundle's
|
|
71
71
|
manifest `context_providers:` (read from its provenance, so the CLI and chi web resolve without loading plugins);
|
|
72
72
|
`{url}` is filled at attach, `{bundle_dir}` and `{ruby}` when the command runs.
|
|
73
|
+
- **Read by plugins.** `ctx.context.snapshots` (`Plugin::AttachedContext#snapshots`, the knowledge bundle's context
|
|
74
|
+
source) reads the project location's and this project's sessions' own snapshots' texts, read-only (never a fetch).
|
|
73
75
|
- **Protected.** The root is a `ProtectedPaths` entry: the file tools cannot write it, because a source's command
|
|
74
76
|
runs later outside the guardrails gate with the worker's environment ([../context.md](../context.md#safety)).
|
|
75
77
|
|
|
@@ -221,7 +221,10 @@ with an **Ask in chat** button (it fills the composer with "The X block in your
|
|
|
221
221
|
send it again, corrected."; never sends), as does an older answer's failed block and an answer's third. A live worker
|
|
222
222
|
without the feature (`workerPredates`, with the store's `bridgeUp`): "· restart this session to fix"; a worker that
|
|
223
223
|
isn't up is asked, and chi web's 503 `not_live` gives the plain line. An answer is read again at most once per block
|
|
224
|
-
for the page's life, so a copy still failing after the redraw says a fix didn't help.
|
|
224
|
+
for the page's life, so a copy still failing after the redraw says a fix didn't help. No word within `GIVE_UP_MS`
|
|
225
|
+
(60 s) of a 202 and the page gives up on the blocks it asked for; a redraw it can't show (the answer's re-read fails or
|
|
226
|
+
finds no bubble to write) settles them the same way: the plain line with Ask in chat
|
|
227
|
+
([web-frontend.md](web-frontend.md#a-failed-block-asked-for-again)). The terminal and a participant's page show nothing new.
|
|
225
228
|
|
|
226
229
|
**The turn's drawing.** A patched `content` changes the digest the turn's drawing line ends on (`TurnDrawings#holds?`),
|
|
227
230
|
so the Bridge's `TurnDrawings` (an observer of `:block_fix` with `landed: "answer"`) writes that turn's line again with
|
data/docs/internals/bridge.md
CHANGED
|
@@ -256,6 +256,10 @@ the "asking again" and steer-cut rows, ✂ rows and questions (with how they wer
|
|
|
256
256
|
turn no longer runs), while a between-turns card keeps its buttons.
|
|
257
257
|
- **Version 1.** A file from before the log (top-level `turns`, each entry its own, maybe `running`) is no longer read, so its
|
|
258
258
|
earlier cards do not show. `SessionManager.cards_empty?` still reads both versions to decide whether a session is empty.
|
|
259
|
+
- **Other readers.** Besides the web and `chi guardrails replay`, `SessionStats` (a page's `runtime.sessions`) reads
|
|
260
|
+
`cards.json` raw, read-only: version 2 only, its approvals and hook notices by turn id, and the caps by
|
|
261
|
+
`CAPACITY`, `NOTICE_TYPES` and `LLM_CONTEXT_TYPES` (the rule `cap_class` keeps private). A change to the entries'
|
|
262
|
+
shape or to those constants must keep it working (`spec/session_stats_spec.rb`).
|
|
259
263
|
- **An older chi on a version 2 file.** Its `seed` wants an integer `turns`, finds none, and starts with no entries. A chi web not
|
|
260
264
|
yet restarted only reads: it lists no cards for that session until restarted, and the file stays whole. An older **writer** does
|
|
261
265
|
damage: at its first saved event, `:turn_started`, it writes `{"version":1,"turns":0,"running":true,"entries":[]}` over the file
|
data/docs/internals/engine.md
CHANGED
|
@@ -339,6 +339,18 @@ only input (`entries`, `reachable_names`, `left_out_names`, `removed_names`, `st
|
|
|
339
339
|
per build before `generate` knows `loads_tools`, `SystemPrompt::Built#surface`; `#warn_stranded`; the
|
|
340
340
|
suggestion's lists; the index's `#declare` / `#refresh!`). Off the turn thread, from `ToolkitBinding#published`
|
|
341
341
|
only (`#explained` for `/toolkit` and the snapshot, `ctx.load_tools`).
|
|
342
|
+
- **A plugin tool's `declare:`** (`chi.tool(declare: :kit | :always)`, `Registry::Entry#declare`) has three seats.
|
|
343
|
+
(1) `Registry#declared?`'s nil branch (full) leaves a `:kit` tool out, and `Toolkit::Resolved#declares?` reads it
|
|
344
|
+
(`:always` unless removed; `:kit` only when added); `Resolved#everything?` needs no adds, so `full` + an add is a
|
|
345
|
+
`Resolved` (`ToolkitBinding` maps only an everything-kit to nil). (2) `Registry#reachable?` (what `call_tool`
|
|
346
|
+
reaches: `reachable_names`, so `index_only` and `DeferredTools`; `stranded_names`) takes a `:kit` tool on full
|
|
347
|
+
too, while `#counts_for_index?` (`index_reaches?`) never counts one, so a `:kit` tool never brings the index's
|
|
348
|
+
tools in; `#left_out?` names a `:kit` tool only when removed, and neither `SystemPrompt#not_loaded_line` nor
|
|
349
|
+
`find_tools`' description (`ToolIndex#registry_groups(described: true)`) counts one, so no kit's prompt or
|
|
350
|
+
declared tools change (a kit's chip lists it as reachable; a host that loads tools defers it). (3) The
|
|
351
|
+
toolkit suggestion logs `always_tools` and `kit_tools` apart from `plugin_tools`, and `ToolkitReportCommand#covers?`
|
|
352
|
+
grades a `:kit` one by a kit's adds only. `full`'s request bytes with a `:kit` tool registered are pinned by
|
|
353
|
+
`spec/engine_toolkit_spec.rb`.
|
|
342
354
|
|
|
343
355
|
## State management
|
|
344
356
|
|
|
@@ -586,6 +598,7 @@ Turn events (the main ones; `events.rb` keeps the shared sets):
|
|
|
586
598
|
| `:plugin_init_wait` | a turn waits for init tasks that provide tools (`PluginTasks`) | `{tasks: [{bundle, id, label}]}` |
|
|
587
599
|
| `:used_memories_updated` | a memory was read, and after the loop | `{used_memory_names, read_names?}` |
|
|
588
600
|
| `:working_set_updated` | after `:generation_started` and once in `end_turn`, only when the set changed (`Engine#emit_working_set`) | [working-set.md](working-set.md) |
|
|
601
|
+
| `:chips_updated` | announced (any thread, observers only) after a bundle's `ctx.chips` change was written (`Engine#announce_chips`, the Host's `chips` store `SessionChips`; the write is outside the event lock, the same chip again announces nothing); the web reads `GET …/context` again; participants never get it | `{session_id, at}`, no chips: [plugins.md](../plugins.md#chips-ctxchips) |
|
|
589
602
|
| `:hook_notice` | a hook's or plugin's notify | `{hook, text, level}` (`between_turns: true` outside a turn) |
|
|
590
603
|
| `:guardrail_warning` | a guardrails line | `{message}` |
|
|
591
604
|
| `:turn_completed` | normal end | `{turn_id, result, turn_summary, display_pending}`, plus `verify`, `verdict`, `revises` when there are; the SSE frame omits `result` |
|
|
@@ -605,7 +618,7 @@ form (`#empty?`, `#wire` nil), so events and hooks carry no origin for it. The b
|
|
|
605
618
|
when someone other than the owner wrote it (stamped by chi web from the request's `Web::Principal`). **An owner's turn
|
|
606
619
|
has no `author` anywhere**: not in its input file, its origin or the session.
|
|
607
620
|
|
|
608
|
-
Where a participant's author (`"p:<id>"`; `
|
|
621
|
+
Where a participant's author (`"p:<id>"`; `Speaker.participant_author?`, `Steer`'s delegates to it) goes (the model is in
|
|
609
622
|
[participants.md](participants.md)):
|
|
610
623
|
|
|
611
624
|
- **The opening message** gets `author` and `author_name` (`Engine#author_fields`; the name from
|
|
@@ -669,7 +682,7 @@ edit must keep:
|
|
|
669
682
|
`Tools::Builtins.registry`, `ToolkitBinding`, `Tools::ToolIndex`, `PeerRegistry`, then the plugins; each plugin's
|
|
670
683
|
`ctx` is a `Plugin::Context` over `plugin_host`). After the plugins (or none), `@tool_index.declare(@tools)`
|
|
671
684
|
registers `find_tools` + `call_tool` (source "index", not core), declared per toolkit by `Registry#declared?`
|
|
672
|
-
(`Toolkit::Resolved#declares?` treats built-ins and plugin tools alike).
|
|
685
|
+
(`Toolkit::Resolved#declares?` treats built-ins and plugin tools alike, but for a plugin tool's `declare:`).
|
|
673
686
|
- `wire_kernel` gives the kernel one `KernelLoop::Services` (Engine-lifetime: hooks, tools, reminder store, mutes,
|
|
674
687
|
question handler, peers, gate, warm-up, generation guard); the client is no service (a model switch swaps it,
|
|
675
688
|
`sync_kernel_client!`).
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# The knowledge bundle: store, catalogs, refresh, adapters
|
|
2
|
+
|
|
3
|
+
The user side is [bundles.md](../bundles.md#the-knowledge-bundle). The code is `lib/samagotchi/bundles/knowledge/`:
|
|
4
|
+
`plugin.rb` (the tools, `/knowledge`, the hooks) and its parts, loaded first in this order: `store.rb` (`Store`),
|
|
5
|
+
`query.rb` (`Query`), `chunker.rb` (`Chunker`), `adapters.rb` (`FolderAdapter`, `MemoriesAdapter`),
|
|
6
|
+
`chi_adapters.rb` (`SessionsAdapter`, `ContextAdapter`), `github.rb` (`GithubAdapter`), `catalog.rb` (`Catalog`),
|
|
7
|
+
`refresh.rb` (`Refresh`), `query_log.rb` (`QueryLog`). It is written only against the plugin API: the core seams it
|
|
8
|
+
needs are `chi.tool(declare:)` ([plugins.md](../plugins.md#declare-which-kits-declare-it); the three seats are in
|
|
9
|
+
[engine.md](engine.md#toolsurface)), `ctx.project`, `ctx.sessions.list` / `present_ids` (`SessionStats.summaries`,
|
|
10
|
+
the listing reflect's counts read too) and `ctx.context.snapshots`.
|
|
11
|
+
|
|
12
|
+
## The store: the sqlite3 CLI
|
|
13
|
+
|
|
14
|
+
One SQLite file per catalog, `index.db`, through the system `sqlite3` CLI (no gem: nothing to build on a user's
|
|
15
|
+
machine or a pod). Every run is
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
Open3.capture3(SQLITE, "-init", "/dev/null", "-safe", "-bail", "-nofollow", ["-json",] db, stdin_data: sql)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- **`-init /dev/null`**: with piped stdin the CLI still reads `$XDG_CONFIG_HOME/sqlite3/sqliterc` (and
|
|
22
|
+
`~/.sqliterc`); a user's `.headers on` / `.mode box` would break the `-json` parse.
|
|
23
|
+
- **`-safe`** refuses `.shell`, `.system`, `.output`, `.once`, `.read`, `.import`, `.open`, `ATTACH`, `writefile()`,
|
|
24
|
+
`readfile()`, `edit()`, `.save`, `.backup`, `.cd`. So one catalog per call: no cross-file query.
|
|
25
|
+
- **Values are hex.** The SQL text is constant per operation and goes on stdin, never argv. Every value goes in as
|
|
26
|
+
`cast(x'<hex>' as text)`: a batch is one hex-literal JSON array read with `json_each`, and the FTS5 MATCH string
|
|
27
|
+
(which the bundle builds, `Query`) is hex too. A title like `x'; drop table items; --\n.shell echo PWNED` is stored
|
|
28
|
+
as text. Numbers (`since`, `limit`) are `Integer()`s.
|
|
29
|
+
- **`-nofollow`** refuses a db reached through a symlinked path and can't create one. So the catalog folder is
|
|
30
|
+
`File.realpath`ed (and `ctx.data_dir` once), and the db is made in Ruby first with `CREAT | EXCL | NOFOLLOW`,
|
|
31
|
+
mode 0600 (`-wal` / `-shm` inherit it); folders are 0700. It is made as `index.db.new`, given its schema (WAL,
|
|
32
|
+
checkpointed, its `-wal`/`-shm` removed) and renamed to `index.db`: a search finds no db or a whole one, never an
|
|
33
|
+
empty file (`no such table`) or one switching to WAL.
|
|
34
|
+
- **Searches read-write under `pragma query_only=1`**, not `-readonly`: Apple's `-readonly` fails on a WAL db whose
|
|
35
|
+
`-wal`/`-shm` are gone (another binary, a restore, deleted them). A search never waits on a write transaction
|
|
36
|
+
(WAL). `-json` prints nothing (not `[]`) for no rows.
|
|
37
|
+
- **Every run starts `.timeout 2000`** (`Store::BUSY_MS`; the dot-command, since `pragma busy_timeout` prints a row
|
|
38
|
+
into `-json`). Each run is its own process, so each is often the db's sole connection, and a sole connection takes
|
|
39
|
+
an exclusive lock when it opens (WAL recovery) and closes (the checkpoint) the db. With no busy timeout a search
|
|
40
|
+
meeting that lock answered `database is locked` (CI saw it; a stress loop of searches beside a writer, ~1 in 4).
|
|
41
|
+
The lock lasts a few ms; the timeout waits it out (and a writer, for another's transaction).
|
|
42
|
+
- **The load probe** (`Plugin#probe`, once per worker at plugin load, a few ms) runs the real shapes in `:memory:`:
|
|
43
|
+
an FTS5 table, a hex `json_each` insert, a `snippet()`/`bm25()` select with a hex MATCH. A CLI that fails it gets no
|
|
44
|
+
tools (no dead `:always` tool in every session), one `Log.warn`, and `/knowledge` says why. `needs: [sqlite3]` is
|
|
45
|
+
advisory.
|
|
46
|
+
|
|
47
|
+
```sql
|
|
48
|
+
create table items(id integer primary key, source text, key text, title text, ptr text, url text, path text,
|
|
49
|
+
updated_at integer, sig text, unique(source, key));
|
|
50
|
+
create table chunk_map(chunk integer primary key, item integer, line integer);
|
|
51
|
+
create virtual table chunks using fts5(title, body, tokenize='porter unicode61'); -- no prefix= (+33 % size)
|
|
52
|
+
create table meta(k text primary key, v text); -- the schema version
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
A schema version other than `Store::SCHEMA_VERSION` drops the file and builds it again (re-creatable data). Writes
|
|
56
|
+
are `begin immediate` transactions of at most 1.5 MB of JSON each (the 2 s busy timeout), under the catalog's
|
|
57
|
+
`index.lock`.
|
|
58
|
+
|
|
59
|
+
**The query** (`Query`): words with no letter or digit are dropped (`"login" AND "--"` finds nothing; an empty MATCH
|
|
60
|
+
is a syntax error: an empty query is refused), each kept word quoted, the last also a prefix (`"sqli"*`), joined by
|
|
61
|
+
AND; under 3 hits an OR pass fills up, ranked after. Ranking is `bm25(chunks, 5.0, 1.0)` (title, body), ties by the
|
|
62
|
+
newer item; one hit per item.
|
|
63
|
+
|
|
64
|
+
**The chunker** (`Chunker`): at most 1,500 chars on line boundaries, a markdown heading starts a chunk, its title is
|
|
65
|
+
the file's name and the nearest heading; text is scrubbed to valid UTF-8; a NUL in the first 8 KB is binary, skipped.
|
|
66
|
+
|
|
67
|
+
## Catalogs
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
$XDG_STATE_HOME/samagotchi/plugins/knowledge/ (ctx.data_dir, realpath'd once; dirs 0700, files 0600)
|
|
71
|
+
projects/<project key>/ catalog.json index.db (+ -wal, -shm) queries.jsonl index.lock sources/<name>.lock
|
|
72
|
+
clones/<owner>__<repo>__<digest>/ (a github source's shallow clone)
|
|
73
|
+
system/ the same, for sessions in no project
|
|
74
|
+
catalogs/<name>/ the same, for named catalogs
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- `Catalog.locate`: `catalog: NAME` (`[a-z0-9][a-z0-9-]{0,31}`, not `system` or `projects`) → `catalogs/NAME`; else
|
|
78
|
+
`ctx.project.key` → `projects/<key>` (a key that isn't a safe path segment becomes `p_<sha256 prefix>`); else
|
|
79
|
+
`system/`.
|
|
80
|
+
- `catalog.json` holds the sources (name, kind, location, max_age, why, globs, refreshed_at, last_error, items,
|
|
81
|
+
chunks, skipped counts; a github source's `clone`, a sessions or context source's `project_root`, `listed_at`),
|
|
82
|
+
`removed_defaults`, `seeded` and notes. It is read without a lock and written
|
|
83
|
+
(`AtomicFile.write`, 0600) only under `index.lock`, which every index write takes too.
|
|
84
|
+
- **Defaults**, seeded once at a project or system catalog's first use: `project-docs` (kind folder, the project
|
|
85
|
+
root, `FolderAdapter::DOCS_INCLUDE`, tracked only; only when the root is in a git work tree, else a note) and
|
|
86
|
+
`chi-memories` (the project's and the system memories dirs; the system catalog the system dir only). A named
|
|
87
|
+
catalog has none. A removed default stays removed.
|
|
88
|
+
|
|
89
|
+
## Refresh: at search time only
|
|
90
|
+
|
|
91
|
+
- **When:** only when `knowledge_search` (or `knowledge_add`, for its source) runs. The stale check reads
|
|
92
|
+
`catalog.json` only (`now - refreshed_at > max_age`): a fresh source costs no walk and no `stat`.
|
|
93
|
+
- **Budget:** the selected stale sources refresh on a thread the search waits for at most `refresh_budget_ms` (1500;
|
|
94
|
+
`knowledge_add` 10 s for its own source), sessions sources last (a first listing reads every session file). A
|
|
95
|
+
`github` source refreshes on a thread of its own the search never waits for (`Refresh.new(background:)`), unless
|
|
96
|
+
it is its own `knowledge_add`. A named catalog's sessions or context source refreshes only from a session in the
|
|
97
|
+
project recorded at its add (`project_root`): `ctx.sessions` / `ctx.context` read the calling session's project. Past it the answer is read from the index as it is, the age line says
|
|
98
|
+
`refreshing now`, and the thread finishes. It is handed `ctx.cancel_controller` (a cancelled turn stops it between
|
|
99
|
+
groups of 200 items) and is **not joined at worker exit**: a killed `sqlite3` child is safe under WAL, and the next
|
|
100
|
+
refresh redoes that batch.
|
|
101
|
+
- **Locks:** `sources/<name>.lock`, `flock` without waiting, per refresh: one held elsewhere (another worker) is
|
|
102
|
+
skipped (`refreshing elsewhere`). `index.lock` exclusive around each write transaction and `catalog.json` write.
|
|
103
|
+
Readers take none.
|
|
104
|
+
- **Diff:** an adapter lists items with a `sig` (a file's `mtime:size`, a blob sha, a session's `updated_at`, a
|
|
105
|
+
snapshot's revision); only items whose sig changed are read and re-chunked, the gone ones deleted by `chunk_map`.
|
|
106
|
+
An adapter with `#gone(old_keys)` lists only what changed (the sessions one, since the source's `listed_at`, the
|
|
107
|
+
start of its last good refresh, less 2 s; dropped when the source is added again, and ignored when the index holds
|
|
108
|
+
no rows for it, so a rebuilt or deleted index lists in full) and says itself which old keys are gone. An adapter with `#chunks(item)`
|
|
109
|
+
makes its own chunks (a session's: `line` = the message index).
|
|
110
|
+
- **Failures** stay on the source (`last_error`, its time) and show in the age line; old rows stay searchable.
|
|
111
|
+
- A participant's search refreshes only the sources it selected (never a chi one).
|
|
112
|
+
|
|
113
|
+
## Adapters
|
|
114
|
+
|
|
115
|
+
`items → [Item(key, sig, title, ptr, url, path, updated_at)]`, `text(item) → String | nil`, `skipped → counts`.
|
|
116
|
+
|
|
117
|
+
- **`FolderAdapter`**: inside a git work tree its tracked files only (`git -C DIR ls-files -z`, env
|
|
118
|
+
`GitEnv::UNSET`), else a `Find` walk skipping dot folders, `node_modules`, `vendor`, `tmp`. Each file through
|
|
119
|
+
`File.lstat`: a symlink is never followed, a file whose realpath leaves the root is skipped, so is one
|
|
120
|
+
`ctx.project.secret_name?` names (on its path relative to the root: the denylist's name half, `ReviewPaths
|
|
121
|
+
.secret_name?`), one over 1 MiB, past `max_files` (20,000). `knowledge_list` shows the counts, never names. Root
|
|
122
|
+
refusals (`FolderAdapter.refusal`): `/`, the home folder or above, anything under (or holding) `ctx.project.chi_dirs`
|
|
123
|
+
or the memories dirs (chi data goes through kind `chi`, so the participant rule holds), and a root
|
|
124
|
+
`ctx.project.secret_root?` matches (`~/.ssh`). The guardrail votes first through the tool's `targets:` (a folder's
|
|
125
|
+
`{ paths: [location] }`, a read); the adapter re-checks.
|
|
126
|
+
- **`MemoriesAdapter`**: the top-level `.md` files of `ctx.project.memories_dir` and `system_memories_dir` (not
|
|
127
|
+
`index.md`, not `.trash` or other folders), an overlay (`name.<model-key>.md`) under its memory's name. Pointer
|
|
128
|
+
`memory_read NAME scope`.
|
|
129
|
+
- **`GithubAdapter`** (`github.rb`): `GithubAdapter.parse` is the location's only door (D1): `OWNER/REPO[@REF][:SUBPATH]`
|
|
130
|
+
or `https://github.com/OWNER/REPO[/tree/REF[/SUBPATH]]`; `OWNER` `[A-Za-z0-9][A-Za-z0-9-]{0,38}`, `REPO`
|
|
131
|
+
`[A-Za-z0-9._-]{1,100}` and not `.`/`..`, no part starting with `-`, `REF` with no `..`, whitespace or control
|
|
132
|
+
characters and `git check-ref-format --allow-onelevel REF` (argv) passing, `SUBPATH` relative with no `..` segment.
|
|
133
|
+
`REF` is a branch or a tag: it goes to `clone --branch`, which takes no commit sha. The URL form's `/tree/REF/PATH`
|
|
134
|
+
can't tell a branch with a `/` from its path (`feature/x` reads as branch `feature`, path `x`): that one is
|
|
135
|
+
`OWNER/REPO@feature/x:docs`.
|
|
136
|
+
The clone is `clones/<owner>__<repo>__<digest of the location>/` (two refs of one repo, a case-insensitive disk):
|
|
137
|
+
`gh repo clone --no-upstream OWNER/REPO DIR.partial -- --depth 1 --single-branch --no-tags --config <SAFE>…
|
|
138
|
+
[--branch REF]` (gh's parser takes no `--` before its positionals: the validated name and the bundle's folder are
|
|
139
|
+
its only ones), renamed into place. `SAFE` (`GithubAdapter::SAFE`, a `--config` each on the clone, a `-c` each on
|
|
140
|
+
every other git spawn): `core.hooksPath=/dev/null`, `core.fsmonitor=false`, `protocol.allow=never`,
|
|
141
|
+
`protocol.https.allow=always`, `protocol.ssh.allow=always`, `submodule.recurse=false` (no `file://`, `ext::` or
|
|
142
|
+
other transport a repo's config could name; the spec's git wrapper alone adds `protocol.file.allow=always` for
|
|
143
|
+
its `file://` origin). A pull: `git <SAFE> -C DIR -c credential.helper= -c 'credential.helper=!gh auth
|
|
144
|
+
git-credential' fetch --depth 1 --no-tags origin -- REF|HEAD`, then `reset --hard FETCH_HEAD`; a failing one
|
|
145
|
+
clones again. Every spawn: no shell,
|
|
146
|
+
env `GitEnv::UNSET` + `GIT_TERMINAL_PROMPT=0` + `GIT_LFS_SKIP_SMUDGE=1`, its own process group killed after 120 s.
|
|
147
|
+
The listing is `git ls-tree -r -z --full-tree HEAD` (blob shas are the sigs): symlinks (120000) and submodules
|
|
148
|
+
(160000) skipped, then the folder rules on the clone-relative path. Pointer `OWNER/REPO:path:line`, URL
|
|
149
|
+
`https://github.com/OWNER/REPO/blob/<commit>/<path>#L<line>` (a participant's `read` of the clone, under chi's
|
|
150
|
+
state dir, is denied; the URL isn't). `updated_at` is the HEAD commit's time. `knowledge_remove` deletes the clone.
|
|
151
|
+
- **`SessionsAdapter`** (`chi_adapters.rb`): `ctx.sessions.list(changed_since:)` (`SessionStats.summaries` of the
|
|
152
|
+
session's project, or of no project: no test_run or scratch session, archived ones kept), each changed session read
|
|
153
|
+
with `ctx.sessions.read` (the saved file, or a live worker's snapshot), each message read by `ctx.speaker`: the
|
|
154
|
+
owner's (`:owner`) with no kind or `input`, the model's (`:model`) with no kind; never `tool_response`, a steer, a
|
|
155
|
+
system note or `thinking`, a sender's or a plugin's line, nor the user lines chi writes (`:chi`: a kind-less `!cmd`
|
|
156
|
+
line or `/continue no`'s reason line). **A participant's turn goes whole** (H1, audit r24): from an opening (a
|
|
157
|
+
message with a `turn_id`, or `turn_start`) that `ctx.speaker` reads as `:participant` to the next opening, their
|
|
158
|
+
prompt, the model's answers and any line merged into it (a live snapshot's merged input has no kind and no
|
|
159
|
+
`turn_id`, so it never ends one: the rule drops more, never less); a participant's line merged into the owner's
|
|
160
|
+
turn goes alone. An item's sig is `"<RULE>:<updated_at>"` (`SessionsAdapter::RULE`, now `"2"`). **The old-rule
|
|
161
|
+
sweep:** every search that selects a sessions source (`Plugin#sweep`, whatever the source's age, project or the
|
|
162
|
+
search's budget) deletes its rows whose sig lacks the prefix (indexed before the rule, a participant's text with
|
|
163
|
+
them; `#sweep_old_rule`, under the catalog lock) and clears its `listed_at`; `Plugin#adapter_for` lists in full
|
|
164
|
+
while any such row is left, so only those sessions are re-chunked, and a listing cut short stays full until one
|
|
165
|
+
completes. A stale worker on the older bundle writes unprefixed sigs again, swept at the next search. Every message's text and the
|
|
166
|
+
session's title go through `Redact.text(text, Redact::Viewer.new(cwd: nil))` before they are chunked, as
|
|
167
|
+
`TurnExcerpts` and `ObjectivesRecord` do: a key by shape (`sk-…`, `ghp_…`, …) or by name (`token=…`) is stored
|
|
168
|
+
as `[redacted]`, and home as `~`. Chunks of
|
|
169
|
+
≤ 1,500 chars, `line` = the index of the message a chunk starts at. Pointer `s:<id>`, rendered `knowledge_search
|
|
170
|
+
open: s:<id>#<line>`. `#gone` = the old keys not in `ctx.sessions.present_ids`. A session that can't be read
|
|
171
|
+
(`ctx.sessions.read` raising) keeps the source's `listed_at` at its `updated_at` (`Refresh`: the min of the
|
|
172
|
+
listing's start and each unread item's), so the next listing reads it again. A named catalog's source added again
|
|
173
|
+
from another project (its `project_root` changes) drops its rows first: they are the old project's, which no id
|
|
174
|
+
sweep removes (`present_ids` is every saved session, not one project's). **The id sweep:** every search that
|
|
175
|
+
selects a sessions source deletes the rows of ids no longer in `present_ids` (~2 ms) before the query, and the
|
|
176
|
+
searching session's own rows are excluded in the query (`Store#search(skip:)`: a project catalog is shared, so
|
|
177
|
+
another session indexes yours). Measured here: listing 407 session files 0.2 s; `Session.load` of this project's 171
|
|
178
|
+
sessions 0.3 s (15,556 user/model messages, 1.4 M chars; `ctx.sessions.read` also asks a running worker for its
|
|
179
|
+
snapshot, not measured): a source's first, full listing costs about 0.5 s before the chunking and the index
|
|
180
|
+
writes, which is why sessions refresh last and past the budget finish in the background.
|
|
181
|
+
- **`ContextAdapter`**: `ctx.context.snapshots` (this project's sources and its sessions' own; read-only, never a
|
|
182
|
+
command), one item per source with a text, key `project/<name>` or `session:<id>/<name>`, sig its revision. A hit
|
|
183
|
+
reads `context_read NAME` when this session has that source attached at that scope (`ctx.context.list`), else
|
|
184
|
+
`knowledge_search open: c:<key>#<line>`. Not redacted: it is a fetched outside text, like a folder's or a
|
|
185
|
+
repository's file (secret-named files are skipped, contents kept), and a redaction that joins a private key's
|
|
186
|
+
lines would move the `c:<key>#<line>` pointers off `context_read`'s lines.
|
|
187
|
+
- **`open:`** reads `Store#chunks_of(key, sources:)` (the chunks of that item in the catalog's chi sessions or
|
|
188
|
+
context sources). A long message's pieces share its line, so a pointer past a line's first chunk is
|
|
189
|
+
`#<line>.<part>` (`Store#search` returns each row's `part`); `open:` shows the chunk at the pointer (else the last
|
|
190
|
+
one before it) and the rest of its line's while they fit 4 KB, with the pointers of the chunks before and after. From the index only; an `s:` pointer at a session no longer in `ctx.sessions.present_ids` is refused before its
|
|
191
|
+
rows are read (a session deleted since the last search's sweep). It writes no query-log line (it is logged as a
|
|
192
|
+
follow-up of the search before it).
|
|
193
|
+
|
|
194
|
+
## Participants
|
|
195
|
+
|
|
196
|
+
`ctx.turn_author` set: `knowledge_search` drops the `chi` sources before the query (a `sources:` of only chi ones is
|
|
197
|
+
refused; the age line says they were left out) and refuses `catalog:` (a named catalog spans projects; the participant
|
|
198
|
+
was invited to one session). `open:` refuses (every pointer it reads is chi data). `knowledge_add` /
|
|
199
|
+
`knowledge_list` / `knowledge_remove` are `participant: :deny` (the pair boundary refuses them, no owner ask; the
|
|
200
|
+
handlers have no participant check of their own). A participant's `/` line
|
|
201
|
+
is a 403 already, so `/knowledge` is the owner's.
|
|
202
|
+
|
|
203
|
+
## The query log and the embeddings gate
|
|
204
|
+
|
|
205
|
+
`queries.jsonl` beside the index, 0600, one line per search: `at, session_id, toolkit, model, catalog, query, words,
|
|
206
|
+
sources, since, hits, top (pointers), stale (names), ms, author (owner|participant)`. An `after_tool_call` hook
|
|
207
|
+
appends the **next 3 tool calls of the same turn** after a search, raw: `{after: <the search's at>, tool, arg}`
|
|
208
|
+
(`arg`, at most 200 chars: a plugin tool's `path` / `name` / `url` / `open` / `query` argument; a built-in's
|
|
209
|
+
target, the flattened call's (`Tools::BuiltinCalls.build`) `path`, else its main argument in `content` (`read`'s
|
|
210
|
+
path, `execute`'s command; a `write` logs its file, never its body), never the tool's name; a `knowledge_search
|
|
211
|
+
open:` is a follow-up, a new search is not); a `before_turn` ends the count. At 5 MB the
|
|
212
|
+
file moves to `queries.1.jsonl` (one kept). Nothing is classified in the bundle: an offline script reads it when the
|
|
213
|
+
Build 3 gate (embeddings) is checked: after ≥ 4 weeks and ≥ 100 owner searches, embeddings are planned only if ≥ 10 %
|
|
214
|
+
of searches are re-searched where the second query hit what the first missed, and a hand check finds most of those
|
|
215
|
+
are meaning misses.
|
data/docs/internals/pages.md
CHANGED
|
@@ -10,9 +10,11 @@ The user side is [pages.md](../pages.md). This is how a run happens and what it
|
|
|
10
10
|
| `pages/builder.rb` | `Builder` (the script's `ui`: `text`, `table`, `walk`) and `TableBlock` (columns, formats, key columns, cells, the total) |
|
|
11
11
|
| `pages/store.rb` | `Store`: read, list, `record_run`, `record_failure`, `locked`, `remove`, `file_id`, `remove_temporary_outputs` |
|
|
12
12
|
| `pages/runner.rb` | `Runner.run(argv)`: the runner process's whole job |
|
|
13
|
-
| `pages/runtime.rb` | `Runtime`, a script's `runtime
|
|
13
|
+
| `pages/runtime.rb` | `Runtime`, a script's `runtime` (`Runtime.new(page:)`, the run's page from `Runner::Collector.new(name)`): `#schedules` (`ScheduleList`, `Schedule`, `Run`, `Unreadable`), `#sessions` (`SessionStats.read`), `#turn_excerpts` (`TurnExcerpts.read`), `#launch_dir` (`Pages::LAUNCH_DIR_KEY`), `#settings` (the bundle's `settings:` keys of `ConfigFile.bundle_settings`) |
|
|
14
|
+
| `session_stats.rb` (core) | `SessionStats.window` (the one listing seat: window, filters, project pick, cap) and `.read` (its counts: `Listing`, `Project`, `SessionCounts`, `TurnCounts`, `FAILURE_KINDS`) |
|
|
15
|
+
| `turn_excerpts.rb` (core) | `TurnExcerpts.read`: given turns' failing calls (`Result`, `Excerpt`, `Call`), redacted and cut, the one text reader |
|
|
14
16
|
| `pages/bundled.rb` | `Bundled`: the pages installed bundles ship (a manifest's `pages:`), `Page#refusal` |
|
|
15
|
-
| `pages/launch.rb` | `Launch.run(name, timeout:, cancelled:)`: the one launch path |
|
|
17
|
+
| `pages/launch.rb` | `Launch.run(name, timeout:, cancelled:, launch_dir:)`: the one launch path; sets `CHI_PAGE_LAUNCH_DIR` (`Pages::LAUNCH_DIR_KEY`, defined in `pages.rb` so the runner never loads this file) |
|
|
16
18
|
| `page_command.rb` | `chi page run | list | rm` |
|
|
17
19
|
|
|
18
20
|
`pages.rb` requires json, digest, time, `inline_widgets`, `paths` and `atomic_file`, and autoloads its parts. Nothing
|
|
@@ -20,9 +22,13 @@ it reaches at load requires the engine, the config, the web, the plugins, `Child
|
|
|
20
22
|
loads it alone. `launch.rb` is the only file that needs `ChildEnv` and `ProcessGroup`, and nothing in `pages.rb`
|
|
21
23
|
requires it. Two parts load more, and only when used: `Runtime#schedules` requires `schedules.rb` (which reaches the
|
|
22
24
|
config, `SessionManager` and `ProcessGroup`; about 70 ms) when a script calls it, and `Bundled` requires
|
|
23
|
-
`installed_bundles.rb` (the config) when a name has no script of the user's. `
|
|
24
|
-
|
|
25
|
-
`
|
|
25
|
+
`installed_bundles.rb` (the config) when a name has no script of the user's. `Runtime#sessions` requires
|
|
26
|
+
`session_stats.rb` (the session file, analytics, `CardStore`'s constants, `SessionWindow` and so the config: 75
|
|
27
|
+
files, about 20 ms on the Studio) and `Runtime#turn_excerpts` `turn_excerpts.rb` (`SessionPaths`, `AnalyticsFile`,
|
|
28
|
+
`Redact`: about 1 ms). `#launch_dir` loads nothing, and `#settings` loads the config only for a bundle's page (whose
|
|
29
|
+
run has loaded it already through `Bundled`). `spec/pages_runtime_spec.rb` pins it: a real runner process running a
|
|
30
|
+
user's page that never calls `runtime` has loaded no `runtime.rb`, `schedules.rb`, `config.rb`, `session_stats.rb`,
|
|
31
|
+
`turn_excerpts.rb` or `launch.rb`, and one calling `runtime.settings` or `runtime.launch_dir` no `config.rb`.
|
|
26
32
|
|
|
27
33
|
**The resolver.** `Pages.script_path(name)` is the script a name runs: the user's `NAME.rb` when it exists, else the
|
|
28
34
|
page of that name an installed bundle ships (`Bundled.find`), else the user's path (no such page). Every read and run
|
|
@@ -62,8 +68,9 @@ script 152 against 73 preloaded libraries and a stale `PWD`). It costs about 70
|
|
|
62
68
|
`ChildEnv.unbundled` (an installed gem's Bundler changes undone),
|
|
63
69
|
`ChildEnv.unset` (the spawner's guardrails settings and session markers), `Launch.cleared_keys` unset
|
|
64
70
|
(`CHI_REMOTE`, every `SAMAGOTCHI_WORKER_*` key and the vendor agent markers `AgentCaller::VENDOR_MARKERS`:
|
|
65
|
-
`CLAUDECODE`, `CODEX_THREAD_ID`, `AI_AGENT`, read from those constants
|
|
66
|
-
|
|
71
|
+
`CLAUDECODE`, `CODEX_THREAD_ID`, `AI_AGENT`, read from those constants, and `CHI_PAGE_LAUNCH_DIR`), then
|
|
72
|
+
`CHI_PAGE_LAUNCH_DIR` set to the caller's `launch_dir:` (`chi page run`'s `Dir.pwd`; a Refresh passes none, so it
|
|
73
|
+
stays unset), `Paths.absolute_xdg`, and `PWD` set to the pages folder (spawn's `chdir:` doesn't change it). So a run never goes remote, never looks like a worker's child,
|
|
67
74
|
and a script's `chi send` is the owner's even from a chi web started in an agent's pane. "This chi's lib" is
|
|
68
75
|
`File.expand_path("../..", __dir__)` from `pages/launch.rb`: a checkout and an installed gem alike.
|
|
69
76
|
3. stdout and stderr are each read by a `ProcessGroup::PipeReader` (64 KB, the tail kept).
|
|
@@ -216,7 +223,8 @@ owner's `~/.claude/plans/evals/0016-pages-skill/`.
|
|
|
216
223
|
|
|
217
224
|
## Later builds
|
|
218
225
|
|
|
219
|
-
- `runtime` has
|
|
220
|
-
|
|
226
|
+
- `runtime` has `schedules` (its first page: the schedules bundle), and, experimental, `sessions`, `turn_excerpts`,
|
|
227
|
+
`launch_dir` and `settings` (their first page: the reflect bundle). A method comes with its first caller.
|
|
228
|
+
`launch_dir` is not a page parameter: it is only where the run was typed.
|
|
221
229
|
- Not planned yet: picks on a page, parameters, a live install (the hub watches the pages folder only, so a bundle's
|
|
222
230
|
page shows on the next list read).
|
data/docs/internals/panes.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# Panes: the windows agents run in
|
|
2
2
|
|
|
3
3
|
A pane is where an agent's own UI runs and where the human can walk in: today a kitty window. It carries no peer
|
|
4
|
-
message (that is the mailbox, [peers.md](peers.md)). `list`, `find` and `show` find and raise a window;
|
|
5
|
-
`alive?` and `paste` open a background tab
|
|
6
|
-
`Plugin::PeerStart`).
|
|
4
|
+
message (that is the mailbox, [peers.md](peers.md)). `list`, `find`, `find_var` and `show` find and raise a window;
|
|
5
|
+
`open`, `alive?` and `paste` open a window (a background tab, or one next to the session's chi terminal) and type
|
|
6
|
+
into it (claude-peer's and codex-peer's `/<peer> start`, `Plugin::PeerStart`).
|
|
7
7
|
|
|
8
8
|
`Samagotchi::Panes` (`lib/samagotchi/panes.rb`), the pure parts in `Panes::Kitty` (`panes/kitty.rb`), ported from the
|
|
9
9
|
desktop helper's `Kitty.swift` as they are:
|
|
@@ -18,7 +18,21 @@ desktop helper's `Kitty.swift` as they are:
|
|
|
18
18
|
- **Finding a window:** the one whose `foreground_processes[].pid` contains the pid (membership: never
|
|
19
19
|
`windows[].pid`, which is the login wrapper or a launcher's sh). Its title is `windows[].title` (never the tab's:
|
|
20
20
|
a tab holds several windows), cleaned as the helper's `AgentText.cleanTitle` (the status glyph dropped).
|
|
21
|
-
- **
|
|
21
|
+
- **What `ls` gives a `Pane`:** besides the key, title, cwd and pids, the window's `user_vars` (`vars`) and its tab's
|
|
22
|
+
`layout` (`tab_layout`: `fat`, `splits`, `stack`…; "" when `ls` has none) and `is_focused` (`tab_focused`: the
|
|
23
|
+
active tab of the focused OS window). kitty 0.49.2's `ls` has all of them (`Window.as_dict`, `TabManager.list_tabs`).
|
|
24
|
+
- **The session's terminal (the mark):** the attached TUI (`TerminalUI::AttachLauncher#mark_window`) sets the user var
|
|
25
|
+
`chi_session` (`Panes::SESSION_VAR`) on its own window to `<session id>:<its pid>` (`Panes.session_mark`) with the
|
|
26
|
+
OSC 1337 `SetUserVar` escape (`Kitty.user_var`: the value base64, as kitty requires), written before the live
|
|
27
|
+
region opens and unset (the no-`=` form; an empty value would leave `""`) after it closes, in `run`'s outer
|
|
28
|
+
`ensure`, so every way out but kill -9 clears it. Only with `KITTY_WINDOW_ID` set and stdout a tty (a piped
|
|
29
|
+
`chi -p … | tee` never gets it), never for a `--remote` attach. No socket is needed (an escape, not
|
|
30
|
+
`kitty @ set-user-vars`). Inside tmux the escape is dropped unless tmux passes it through: no mark, so a start
|
|
31
|
+
opens a tab. `spec_helper` drops `KITTY_WINDOW_ID`, so a suite run in kitty marks nothing.
|
|
32
|
+
- **Finding the mark:** `find_var(name, id)` is the window whose var `name` matches `\A<id>:(\d+)\z` (the id
|
|
33
|
+
escaped) while that pid is among its foreground pids: a TUI killed hard leaves its mark on a window that may run a
|
|
34
|
+
shell now, and that one never matches. Several (two attached terminals of one session): the focused tab's first,
|
|
35
|
+
else the first in `ls` order. on its socket: it brings a background tab forward, and a background OS
|
|
22
36
|
window (checked with `kitty @ ls`'s `is_focused` while the screen was unlocked).
|
|
23
37
|
- **Open:** `open(command:, cwd:, title:)` runs `launch --type=tab --keep-focus --cwd DIR --tab-title TITLE ARGV…`
|
|
24
38
|
on the kitty whose OS window is focused (`ls`'s `is_focused`), else the live socket file with the newest mtime (the
|
|
@@ -27,6 +41,15 @@ desktop helper's `Kitty.swift` as they are:
|
|
|
27
41
|
15 s limit (`LAUNCH_SECONDS`; `ls` 2 s, the rest 5 s): one killed early may still open a tab no ref names. No socket answering
|
|
28
42
|
raises `Panes::Unavailable`, the one message "kitty isn't running or `kitty.listen_on` isn't set"; a launch that
|
|
29
43
|
fails or prints no id raises `Panes::Error`.
|
|
44
|
+
- **Open next to a window:** `open(…, next_to: pane)` runs, on **that pane's own socket** (checked as any ref is),
|
|
45
|
+
`launch --type=window --match window_id:N --next-to id:N --location=LOC --keep-focus --cwd DIR ARGV…`. The
|
|
46
|
+
`--match window_id:N` picks the pane's tab: over remote control kitty ignores `--next-to` unless the matched window
|
|
47
|
+
is in the target tab, the active one by default, so without it the window would land in whatever tab the user is
|
|
48
|
+
in (`window_id:`, not `id:`, which for `launch` first looks for a tab id). `LOC` is `split` in the `splits` layout,
|
|
49
|
+
else `neighbor` (the slot after it: in `fat`, the default layout, a strip under chi). No `--title` (it would freeze
|
|
50
|
+
the title the command sets, which Show's lists read). A non-zero exit (the window gone since the `ls`, a kitty that
|
|
51
|
+
refuses `--next-to`, anything: stderr isn't read) raises `Panes::SplitFailed`; a timeout raises `Panes::Error`,
|
|
52
|
+
never `SplitFailed`, since a slow launch may still open its window (`run` tells `:timeout` from `:failed`).
|
|
30
53
|
- **Alive and paste:** `alive?(ref)` is `ls --match id:N` on the ref's socket. `paste(ref, text, enter:)` is the
|
|
31
54
|
helper's `KittyRunner#send`: `alive?` first (send-text reports nothing for a gone window), then
|
|
32
55
|
`send-text --match id:N --bracketed-paste=disable --stdin` with the text wrapped in `ESC[200~ … ESC[201~` (its own
|
|
@@ -38,8 +61,8 @@ desktop helper's `Kitty.swift` as they are:
|
|
|
38
61
|
- **No kitty** (unset, not running, the pid in no window, the binary missing): nothing found, no error; a row then
|
|
39
62
|
has no Show.
|
|
40
63
|
|
|
41
|
-
`ctx.panes` (`Plugin::Panes`, [plugins.md](../plugins.md)) is the plugin's view
|
|
42
|
-
`show`, `open` and `paste`, which raise `Plugin::Panes::NotTyped` unless the command was typed or tapped by a person
|
|
64
|
+
`ctx.panes` (`Plugin::Panes`, [plugins.md](../plugins.md)) is the plugin's view, built with its session's id:
|
|
65
|
+
`find(pid:)`, `alive?(ref)`, and `show`, `open` and `paste`, which raise `Plugin::Panes::NotTyped` unless the command was typed or tapped by a person
|
|
43
66
|
(`ctx.user_typed?`, `UserTyped`): they act as the user on their screen, so no tool call, `chi send`, agent, participant, schedule, delegate or plugin path reaches them. The seam trusts the request's client id, so it is an accident guard as the guardrails are ([guardrails.md](../guardrails.md#protected-paths)), not a sandbox: a model's shell holding the Bridge's secret could still post as a UI, as it can a `!` line. Nothing derived from `.here` is used to open or
|
|
44
67
|
paste: `/<peer> start` keeps its `PaneRef` in the bundle's own session state (`state.json` under `ctx.session_dir`, a
|
|
45
68
|
`SessionStores` row removed with the session), never in the mailbox. That file is not protected (chi's own tools can
|
|
@@ -48,19 +71,34 @@ one of `kitty.listen_on`'s own (`Kitty.sockets`), else they answer as for a clos
|
|
|
48
71
|
session's `/<peer> show` also needs the paired peer's pid in that window. A continue doesn't carry that state (the
|
|
49
72
|
pairing it does).
|
|
50
73
|
|
|
74
|
+
`Plugin::Panes#open(command:, cwd:, title:, in: :tab)` (another `in:`, a string too, is an `ArgumentError`): `:tab` is the background tab; `:pane` is `find_var` of this
|
|
75
|
+
session's mark, then a window next to it, else a tab: no mark, the mark's tab in the `stack` layout (the window
|
|
76
|
+
would open hidden behind chi, `--keep-focus`), or a `SplitFailed` (one tab launch then; a timeout gets none and
|
|
77
|
+
raises as any open error). A block gets a `Plugin::Panes::Placed(kind, why)`: `:pane`, or `:tab` with the reason
|
|
78
|
+
(`NO_TERMINAL`, `STACKED`, `SPLIT_FAILED`) when a pane was asked for. Either way it returns the `PaneRef`, so
|
|
79
|
+
`alive?`, `show`, `paste` and the saved record work the same for a pane and a tab (all per window). A start tapped
|
|
80
|
+
in chi web or on the phone opens next to the session's attached TUI too when there is one: the rule names the
|
|
81
|
+
session's terminal, not where the tap came from. One extra `ls` per start (`find_var`; the tab path has its own).
|
|
82
|
+
|
|
51
83
|
`/<peer> start` (`Plugin::PeerStart`): the checks (typed, no live pairing, claude-peer's start hook current), then
|
|
52
84
|
the wait (`ctx.peers.wait`), then `open` with `[$SHELL, -l, -c, "exec <start_command>"]` (the account's shell when
|
|
53
|
-
`$SHELL` is unset)
|
|
85
|
+
`$SHELL` is unset) and `in:` from the bundle setting `open_in` (`pane`, the default, or `tab`; another value means
|
|
86
|
+
`pane`, and the start line names it: no setting checks values, only keys and types). The start line says what
|
|
87
|
+
opened ("opened a pane next to this chi", or "opened a background tab "<title>" (why)"); every later line says
|
|
88
|
+
"kitty window" for both, so the saved record needs no kind. An open wait (`/<peer> pair`'s) is kept, not rewritten; a pane error takes back only the wait
|
|
54
89
|
this start wrote (`ctx.peers.stop_waiting(name, mark)`, `PeerPresence.drop_wait`: never another wait nor an announce's
|
|
55
|
-
claimed file), so a refusal leaves no mark of its own. A second start while the wait is open and its
|
|
56
|
-
double tap) is refused, "already starting in kitty
|
|
57
|
-
|
|
90
|
+
claimed file), so a refusal leaves no mark of its own. A second start while the wait is open and its window `alive?` (a
|
|
91
|
+
double tap) is refused, "already starting claude · chi <id8> in kitty …" (chi's label, the saved `title`: a
|
|
92
|
+
pane carries no title of chi's); two at the same moment take turns on a flock
|
|
93
|
+
(`start.lock` in `ctx.session_dir`, held across the checks, the wait and the open: commands run in parallel). The window
|
|
58
94
|
is checked once more 1.5 s after the launch (`PeerStart.settle_seconds`): gone already (its command not found), the
|
|
59
95
|
start's wait is taken back and the reply names `start_command`. The
|
|
60
96
|
status card's started row (scene 5) shows while the ref is `alive?` and the paired Claude's pid (its `PeerHere`) runs
|
|
61
|
-
in that window, and "its kitty
|
|
97
|
+
in that window, and "its kitty window is closed" once the ref is gone and so is the pairing's socket.
|
|
62
98
|
|
|
63
99
|
Specs: `spec/panes_spec.rb` (on `spec/fixtures/desktop/fake_kitty`, which answers `ls`, `launch`, `focus-window`,
|
|
64
|
-
`send-text` and `send-key`, and logs the argv, stdin and `KITTY_*` env it got; its canned windows carry pids
|
|
65
|
-
|
|
100
|
+
`send-text` and `send-key`, and logs the argv, stdin and `KITTY_*` env it got; its canned windows carry pids and
|
|
101
|
+
`user_vars`, its tabs `layout` and `is_focused`, its OS windows `is_focused`; `launch --match window_id:N` adds the
|
|
102
|
+
window to N's tab or exits 1, and `$FAKE_KITTY_SPLIT_FAILS` / `$FAKE_KITTY_LAUNCH_SLEEP` play a kitty that refuses
|
|
103
|
+
`--next-to` and a slow launch), `spec/terminal_ui/attach_launcher_spec.rb` (the mark) and `spec/samagotchi/bundles/claude_peer_start_spec.rb`. No spec reaches a running kitty
|
|
66
104
|
([testing.md](../testing.md)).
|
|
@@ -72,6 +72,14 @@ participant** and **one role**, until it expires or is revoked.
|
|
|
72
72
|
`Participants.invite`, `SessionAccess` Local/Remote `#invite`, the invite route, the web participants panel, the
|
|
73
73
|
TUI's participant commands, `SessionAccess::Contract` and `SessionAccess::ParticipantView`'s fields. The owner's
|
|
74
74
|
routes (`public_h`) don't show it yet.
|
|
75
|
+
- **An invite during a transition** (`Participants.invite`): checked and issued under a shared hold of the session's
|
|
76
|
+
transition lock (`SessionChain::Sharing.under`, [session-lifecycle.md](session-lifecycle.md#the-transition-lock-sessiontransition)):
|
|
77
|
+
a continue archives the link and ends its grants, so a grant issued in between would outlive the archive. Held by
|
|
78
|
+
another, the invite is refused `Grants::Invalid` (`invalid_participant`: `chi share` exits 1, chi web answers 400;
|
|
79
|
+
text only, so an older chi's `--remote` prints it as before), naming the session that continues it under any holder
|
|
80
|
+
(`Participants.held_refusal`: "session ab12cd34 is being continued as <new id>: invite there in a moment" while a
|
|
81
|
+
continue names its new link, "… was continued as <id>: invite there" for a saved successor), else with Busy's words
|
|
82
|
+
naming the holder ("session ab12cd34 is being deleted; try again in a moment").
|
|
75
83
|
- Rotating the owner token (`chi web --new-token`) never touches grants; `chi share revoke --all` ends them all.
|
|
76
84
|
- **Bound grants** (`identity`, an email in lower case; nil for any, and absent from the store then): a bound grant
|
|
77
85
|
is honoured only on a request through the proxy (`web.public_url`) whose identity header carries that email; on the
|
|
@@ -221,7 +229,8 @@ A participant's turn acts with the session's tools under the owner's guardrails,
|
|
|
221
229
|
once only, and `Guardrails::PairBoundary` denies secrets, chi itself and chi web on loopback, background and
|
|
222
230
|
scheduled jobs, the environment and credential printers, git config writes (reads only) and git's hooks,
|
|
223
231
|
owner-voiced tools, the owner's task outputs, the owner's other sessions, reminders and schedules and the
|
|
224
|
-
attached context (`PairBoundary::TOOL_CLASSES`), and the model's own
|
|
232
|
+
attached context (`PairBoundary::TOOL_CLASSES`), a bundle tool declared `participant: :deny`, and the model's own
|
|
233
|
+
question, and asks before a command that makes
|
|
225
234
|
a link and before a write that may land outside the project, failing closed (a global install or a user config write
|
|
226
235
|
that names no path, `Guardrails::ImplicitWriters`, and a script read from stdin count; a session at or above home has
|
|
227
236
|
none: every write asks) ([guardrails](../guardrails.md#a-participants-pair-boundary)).
|
|
@@ -384,7 +393,9 @@ command that waits for it, and `send --wait -m` skips the owner's busy check (it
|
|
|
384
393
|
doesn't refuse it; that check's `wait/prepare` without a turn would be 403 `not_permitted`) and waits on the turn it
|
|
385
394
|
queued, as `--turn T` does.
|
|
386
395
|
|
|
387
|
-
**The tool calls.** `Guardrails::PairBoundary` sits in the participant boundary's seat
|
|
396
|
+
**The tool calls.** `Guardrails::PairBoundary` sits in the participant boundary's seat; a bundle tool declared
|
|
397
|
+
`chi.tool …, participant: :deny` is refused there before any check or ask (`Targets#participant`, read from the
|
|
398
|
+
registered entry; `call_tool(chi/<name>)` is unwrapped to the tool's own name first). The owner's checks and rules
|
|
388
399
|
decide the rest, as for the owner's turns ([guardrails](../guardrails.md#a-participants-pair-boundary)). It judges a path every way it resolves
|
|
389
400
|
(`PathResolve.reals`: a `..` after a symlinked folder climbs out of its target, as the read opens it;
|
|
390
401
|
[guardrails](../guardrails.md#how-paths-resolve)). Approvals and questions are owner-only: the
|
|
@@ -499,7 +510,8 @@ fields: a field added to an event later reaches nobody until it is named there.
|
|
|
499
510
|
`context_window_tokens`, never `status`, the model-facing line; `generation_completed`: its `speed` only;
|
|
500
511
|
`working_set_updated`: `.working_set`, below); and bare markers (the type alone) for the answers to a question or
|
|
501
512
|
offer, and `snapshot` / `reset` (the page re-reads the session at a `reset`, as the owner's). Every other type,
|
|
502
|
-
known (`DROPPED`) or new, is dropped
|
|
513
|
+
known (`DROPPED`) or new, is dropped (`chips_updated` among them: a bundle's chips are the owner's session bar, and
|
|
514
|
+
`GET …/context` is owner-only). `spec/web/participant_view_spec.rb` scans every `type: :name` chi emits and
|
|
503
515
|
every handler of the web's stream, and fails until a new one is named in `EVENTS` or `DROPPED`.
|
|
504
516
|
- **The frames** (`Web::ParticipantStreamFilter`). The Bridge's chunks are cut into frames (`\r?\n\r?\n`, a partial
|
|
505
517
|
one kept for the next chunk, one over 1 MiB dropped to its end); each frame's JSON goes through `.event` and is
|