samagotchi 0.194.0 → 0.195.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 +40 -1
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +192 -6
- data/docs/cli.md +1 -1
- data/docs/configuration.md +25 -4
- data/docs/internals/attached-context.md +2 -0
- data/docs/internals/bridge.md +4 -0
- data/docs/internals/engine.md +13 -1
- data/docs/internals/knowledge.md +205 -0
- data/docs/internals/pages.md +17 -9
- data/docs/internals/panes.md +51 -13
- data/docs/internals/peers.md +2 -1
- data/docs/memory.md +4 -2
- data/docs/pages.md +72 -1
- data/docs/peers.md +24 -13
- data/docs/plugins.md +59 -3
- data/docs/releasing.md +1 -1
- data/docs/scheduling.md +4 -0
- data/docs/sessions.md +8 -1
- data/docs/testing.md +3 -1
- 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/dev/manifest.yml +3 -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 +160 -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 +25 -0
- data/lib/samagotchi/bundles/knowledge/plugin.rb +788 -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/guardrails/review_paths.rb +13 -3
- 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/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/plugin/api.rb +7 -1
- data/lib/samagotchi/plugin/attached_context.rb +46 -1
- data/lib/samagotchi/plugin/context.rb +24 -7
- data/lib/samagotchi/plugin/panes.rb +52 -9
- data/lib/samagotchi/plugin/peer_start.rb +60 -35
- data/lib/samagotchi/plugin/peers.rb +1 -1
- data/lib/samagotchi/plugin/project.rb +63 -0
- data/lib/samagotchi/plugin/sessions.rb +36 -1
- data/lib/samagotchi/plugin/tool_spec.rb +16 -7
- data/lib/samagotchi/session.rb +22 -9
- data/lib/samagotchi/session_stats.rb +436 -0
- data/lib/samagotchi/session_window.rb +30 -0
- 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/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 +43 -18
- data/lib/samagotchi/tools/tool_index.rb +6 -2
- data/lib/samagotchi/turn_excerpts.rb +298 -0
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +9 -0
- data/lib/samagotchi/version.rb +1 -1
- metadata +22 -1
data/docs/plugins.md
CHANGED
|
@@ -302,11 +302,33 @@ run by `call_tool` as the tool itself ([the chi group](#chitool_sourcecall-acts_
|
|
|
302
302
|
each key optional, says what a call acts on, for [guardrails](#guardrails).
|
|
303
303
|
- `refs`: `->(args) { [{ kind:, target:, label:, actions: [] }, …] }`, what the call's row traces to in the web
|
|
304
304
|
(display only, the model never sees them). See [Refs on a tool's row](#refs-on-a-tools-row).
|
|
305
|
+
- `declare`: `:always` or `:kit`, which toolkits declare it (below); omitted, a kit's list decides, as for a built-in.
|
|
305
306
|
|
|
306
307
|
The block returns the result text. If it starts with `Error:`, it counts as
|
|
307
308
|
a failure. If it raises, the model gets `Error: <message>`. To return images
|
|
308
309
|
too, see [Returning images](#returning-images).
|
|
309
310
|
|
|
311
|
+
#### `declare:`: which kits declare it
|
|
312
|
+
|
|
313
|
+
- **`declare: :always`**: every toolkit declares it (`lean`, `child`, a config kit on `lean` that no `add:` of
|
|
314
|
+
yours reaches) unless the toolkit or the session removes it (`remove:`, `--no-tool`, `/toolkit remove`). For a
|
|
315
|
+
tool every session should have, such as the knowledge bundle's `knowledge_search`; it costs its declaration's
|
|
316
|
+
tokens in every request, so keep its description short.
|
|
317
|
+
- **`declare: :kit`**: only a toolkit that adds it declares it (a kit's `add:`, `--tool`, `/toolkit add`), never
|
|
318
|
+
`full` by itself: for tools a kind of session needs (the knowledge bundle's catalog tools, which the coordinator
|
|
319
|
+
kit adds). A kit that doesn't add it doesn't "leave it out": no prompt names it (not the not-loaded line, not
|
|
320
|
+
`find_tools`' description) and `full` shows no chip for it, so registering one leaves the system prompt and the
|
|
321
|
+
declared tools of every session that doesn't add it as they were. Where the tool index is up (an MCP server, a kit
|
|
322
|
+
leaving built-ins out) `find_tools` finds it and `call_tool` reaches it as `chi/<name>` (a kit's chip lists it as
|
|
323
|
+
reachable; on a host that loads tools it is among the deferred ones, after the cached part), but it never brings
|
|
324
|
+
`find_tools` and `call_tool` into a session that had none. Called by its own name where it isn't declared, it gets
|
|
325
|
+
the unknown-tool answer.
|
|
326
|
+
- Anything else is a load error (`tool x: declare must be :kit or :always`). A changed `declare:` in
|
|
327
|
+
`chi.replace_tools` is a change (the prompts are built again). The toolkit report (`chi toolkit report`) grades an
|
|
328
|
+
`:always` tool covered on every kit but one removing it, and a `:kit` one only by a kit's adds.
|
|
329
|
+
- `declare:` needs a chi newer than 0.194.0: an older chi refuses the keyword, a load error, so a bundle passing it
|
|
330
|
+
sets `requires_chi` past that (`chi bundle check` warns on a lower one).
|
|
331
|
+
|
|
310
332
|
#### `args`
|
|
311
333
|
|
|
312
334
|
`args` is a frozen Hash with **string keys**: the arguments the model gave,
|
|
@@ -926,6 +948,7 @@ full label, `plugin.rb (bundle my-bundle)`.
|
|
|
926
948
|
| `ctx.session_id` | the session's id (nil before there is one) |
|
|
927
949
|
| `ctx.cwd` | the session's working directory |
|
|
928
950
|
| `ctx.repo_root` | the git checkout holding `cwd`, or nil |
|
|
951
|
+
| `ctx.project` | the project `cwd` belongs to, as chi's memories find it, frozen: `kind` (`:git`, `:marker` for a `chi project init` folder, `:none`), `root` (a repository's **main checkout**, for every linked worktree too; the marked folder; nil for none), `key` (the memories key, `<name>_<hash>` or the marker's id; nil for none), `memories_dir` (where `memory_read`/`memory_write`'s project scope is for this folder: a folder in no project has its own), `system_memories_dir`, `chi_dirs` (chi's state dir, config dir and an outside hooks dir), `secret_root?(path)` (a folder a participant's turn never reads: chi's dirs, `~/.ssh` and the other home secrets, as given and as it resolves) and `secret_name?(relative_path)` (a file inside an allowed folder named like a secret: `.env*`, `credentials*`, keys, a `.git` config). Use it rather than chi's memory paths. One known gap: a session saved with a project root before its marked folder moved keeps that root in `Session#project_root`, while `ctx.project` reads the folder now. Needs a chi newer than 0.194.0 (guard it with `ctx.respond_to?(:project)`) |
|
|
929
952
|
| `ctx.scratch?` | whether the session is a `chi scratch` one (deleted when it ends) |
|
|
930
953
|
| `ctx.delegate?` | whether the session is a delegate child: a task another session handed over |
|
|
931
954
|
| `ctx.frontend` | what runs the session, read now: `:one_shot` (`-p --non-interactive`: one turn, then exit) or `:worker` (a session worker: the web, an attached TUI). There is no `:repl` any more: a bundle that checks for it never matches. Only a worker's answers reach the web while it runs; a one-shot's show there once the session is opened. Until the host sets it (while the plugins load, inside `Engine.new`) it reads `:one_shot`, so read it in hooks and init tasks, not in `register` |
|
|
@@ -1520,6 +1543,8 @@ ctx.sessions.send(id, "then the docs", delivery: :queue) # after its running tu
|
|
|
1520
1543
|
ctx.sessions.read(id) # => {id:, title:, status:, parent_id:, running:, messages:} (title: its /title, else its preview)
|
|
1521
1544
|
ctx.sessions.children # => [{id:, short_id:, state:, branch:, last_reply:, reported:, ...}]
|
|
1522
1545
|
ctx.sessions.stop(id) # one of this session's own children
|
|
1546
|
+
ctx.sessions.list(changed_since: nil) # => [{id:, title:, updated_at:, archived:}] this project's saved sessions
|
|
1547
|
+
ctx.sessions.present_ids # => ["3f2a9c1e-…", …] every saved session's id
|
|
1523
1548
|
```
|
|
1524
1549
|
|
|
1525
1550
|
- `fork(messages:, title: nil, prompt: nil)` starts a child session in its
|
|
@@ -1572,6 +1597,17 @@ ctx.sessions.stop(id) # one of this session's own children
|
|
|
1572
1597
|
- `stop(id)` stops one of this session's own children, as
|
|
1573
1598
|
`chi sessions stop` does, and waits up to 2 s for its worker to let go. It
|
|
1574
1599
|
returns the child's id. Any other session raises.
|
|
1600
|
+
- `list(changed_since: nil)` lists this project's saved sessions
|
|
1601
|
+
(`ctx.project.root`'s; a session in no project lists the ones in none),
|
|
1602
|
+
newest updated first, archived ones included, never a scratch or test_run
|
|
1603
|
+
one: the listing `SessionStats.summaries` gives (reflect's counts read it
|
|
1604
|
+
too). Each file is parsed, so call it from your own thread, not every
|
|
1605
|
+
turn; `changed_since:` (a Time) parses only the files written after it.
|
|
1606
|
+
Read a row's messages with `read(id)`. `present_ids` is every saved
|
|
1607
|
+
session's id from the file names, nothing parsed (~2 ms): what an index of
|
|
1608
|
+
sessions keeps rows for (the knowledge bundle drops a deleted session's
|
|
1609
|
+
with it). Both need a chi newer than 0.194.0 (`chi bundle check` warns
|
|
1610
|
+
below that).
|
|
1575
1611
|
- Each raises `Samagotchi::Plugin::Sessions::Error` with the reason.
|
|
1576
1612
|
|
|
1577
1613
|
## Attached context: `ctx.context`
|
|
@@ -1580,6 +1616,7 @@ ctx.sessions.stop(id) # one of this session's own children
|
|
|
1580
1616
|
ctx.context.attach(url: "https://github.com/acme/app/pull/42", why: "branch feat/x has open PR #42")
|
|
1581
1617
|
ctx.context.attach(name: "ci", cmd: "bin/ci-status", why: "this branch's CI", every_seconds: 120)
|
|
1582
1618
|
ctx.context.list # => [{name:, scope:, why:, hint:, fetched_at:, error:, provider:, summary:}]
|
|
1619
|
+
ctx.context.snapshots # => [{name:, scope:, session_id:, hint:, revision:, fetched_at:, text:}]
|
|
1583
1620
|
ctx.context.decline(url: "https://github.com/acme/app/pull/42") # => "pr-42"
|
|
1584
1621
|
ctx.context.propose(url: "https://github.com/acme/app/pull/42", title: "#42 Make the thing", mentions: ["#42", "feat/x"])
|
|
1585
1622
|
ctx.context.candidates # => [#<data Candidate name="pr-42", url="https://…", title="#42 Make the thing", …>]
|
|
@@ -1641,6 +1678,13 @@ gets a note when its text changes.
|
|
|
1641
1678
|
the session has an id or its file is saved: a command before the first turn). `hint` is the
|
|
1642
1679
|
one the last text gave (else the source's), `summary` that text's summary
|
|
1643
1680
|
(nil before any text; chi 0.63.0).
|
|
1681
|
+
- `snapshots` (read-only) gives the latest texts of this project's sources
|
|
1682
|
+
and of the sources this project's sessions keep as their own (a session in
|
|
1683
|
+
no project: the sessions in none), project ones first, then by session id
|
|
1684
|
+
and name; `session_id` is nil for a project's. A source with no text yet
|
|
1685
|
+
is left out, and it never runs a command. It reads every snapshot file, so
|
|
1686
|
+
call it from your own thread (the knowledge bundle's context source does).
|
|
1687
|
+
Needs a chi newer than 0.194.0 (`chi bundle check` warns below that).
|
|
1644
1688
|
- Two attaches of one source at once (a double click) both return its
|
|
1645
1689
|
name.
|
|
1646
1690
|
- `attach`, `decline` and `propose` raise
|
|
@@ -1667,6 +1711,7 @@ ctx.peers.claimed?("claude") # another pairing holds the wait right now (a p
|
|
|
1667
1711
|
ctx.panes.find(pid: 1234) # the kitty window a pid runs in (title, cwd), or nil
|
|
1668
1712
|
ctx.panes.show(pane) # brings it to the front: only when ctx.user_typed? (else raises)
|
|
1669
1713
|
ctx.panes.open(command: argv, cwd: dir, title: "…") # a background kitty tab: a PaneRef; only when ctx.user_typed?
|
|
1714
|
+
ctx.panes.open(command: argv, cwd: dir, title: "…", in: :pane) { |placed| … } # next to this session's chi terminal, else a tab
|
|
1670
1715
|
ctx.panes.alive?(ref) # its window is still there
|
|
1671
1716
|
ctx.panes.paste(ref, text, enter: true) # one bracketed paste, then Enter: only when ctx.user_typed? (false: gone)
|
|
1672
1717
|
ctx.peers.stop_waiting("claude", mark) # ends the wait #wait returned, only while it is that one
|
|
@@ -1685,7 +1730,15 @@ and the session's wait (`wait`, `claim`). `ctx.panes`
|
|
|
1685
1730
|
`ctx.user_typed?` (else `Plugin::Panes::NotTyped`), which is also what gates a
|
|
1686
1731
|
write to another agent's settings. `open` raises
|
|
1687
1732
|
`Samagotchi::Panes::Unavailable` when no kitty answers ("kitty isn't running or
|
|
1688
|
-
`kitty.listen_on` isn't set"), its one refusal.
|
|
1733
|
+
`kitty.listen_on` isn't set"), its one refusal. With `in: :pane` (the next chi:
|
|
1734
|
+
on 0.194.0 the keyword is an `ArgumentError`, and so is any value but the
|
|
1735
|
+
symbols `:pane` and `:tab`, a string too) it opens a window next to this
|
|
1736
|
+
session's attached chi terminal in kitty, in that terminal's tab, and a
|
|
1737
|
+
background tab when there is none (a start from chi web with no TUI attached,
|
|
1738
|
+
a TUI outside kitty), that tab is in kitty's stack layout, or kitty refuses
|
|
1739
|
+
the launch there; the block, when given, gets a `Plugin::Panes::Placed`:
|
|
1740
|
+
`kind` `:pane` or `:tab`, and `why` a tab when a pane was asked for, in words
|
|
1741
|
+
for your reply. The return is the same `PaneRef` either way. Keep a `PaneRef` in your own
|
|
1689
1742
|
session state (`ctx.session_dir`), never in the mailbox; `ctx.panes` takes a ref
|
|
1690
1743
|
only on a socket `kitty.listen_on` names, and a caller checks what runs in the
|
|
1691
1744
|
window before it pastes. The seam is an accident guard: no tool call, `chi send`,
|
|
@@ -1796,7 +1849,8 @@ pages: [schedules.rb] # scripts: keys named NAME.rb: the page NAME
|
|
|
1796
1849
|
- The owner's own `NAME.rb` in the pages folder replaces a bundle's page, and a bundle's page can't be removed but
|
|
1797
1850
|
by uninstalling the bundle ([Pages from a bundle](pages.md#pages-from-a-bundle)).
|
|
1798
1851
|
- The script is a page script like any other: `page`, `ui` and `runtime` ([pages.md](pages.md)). The shipped
|
|
1799
|
-
schedules bundle is the example (`lib/samagotchi/bundles/schedules/`)
|
|
1852
|
+
schedules bundle is the example (`lib/samagotchi/bundles/schedules/`); the reflect bundle
|
|
1853
|
+
(`lib/samagotchi/bundles/reflect/`) is a second, with `settings:` its page reads through `runtime.settings`.
|
|
1800
1854
|
|
|
1801
1855
|
## Stable for bundles
|
|
1802
1856
|
|
|
@@ -1865,7 +1919,9 @@ that calls it, or for a real external bundle that does. No method on the chance
|
|
|
1865
1919
|
- `pages:` and the page script DSL a bundle's page (or anyone's) is written in: `page`, `ui.text` / `ui.table` /
|
|
1866
1920
|
`ui.walk`, and `runtime` with what it returns (`runtime.schedules`: its members, `rows`, `warning`,
|
|
1867
1921
|
`unreadable_ids`, `Pages::Runtime::Unreadable`) ([pages.md](pages.md)). A page runs in the page runner, not as a
|
|
1868
|
-
plugin, so the scan below doesn't see it either.
|
|
1922
|
+
plugin, so the scan below doesn't see it either. `runtime.sessions(project:)`, `runtime.turn_excerpts`,
|
|
1923
|
+
`runtime.launch_dir` and `runtime.settings` are **experimental**: their members may still change
|
|
1924
|
+
([pages.md](pages.md#reading-chis-data-runtime)).
|
|
1869
1925
|
|
|
1870
1926
|
`Samagotchi::Testing::Boundary::ALLOWED` is the checked copy of items 1-6,
|
|
1871
1927
|
and `Boundary::EXCEPTIONS` the per-bundle list of what each shipped bundle
|
data/docs/releasing.md
CHANGED
|
@@ -13,7 +13,7 @@ can run the whole release; the user approves the notes before the tag and the
|
|
|
13
13
|
and `rake release:check` enforce it). The system bundle upgrades itself when
|
|
14
14
|
chi starts.
|
|
15
15
|
- **Every other shipped bundle** (btw, check-in, guardrails, known-names,
|
|
16
|
-
loop-guard, mcp, skills, slow-commands, agents-md, cost-cap, work-cap, source-links, github-pr, coordinator, claude-peer, codex-peer, ruby, model-notes, decision-card, inline-widgets, web-search, schedules, and the profiles core and dev) has its own semver in its
|
|
16
|
+
loop-guard, mcp, skills, slow-commands, agents-md, cost-cap, work-cap, source-links, github-pr, coordinator, claude-peer, codex-peer, ruby, model-notes, decision-card, inline-widgets, knowledge, web-search, schedules, reflect, and the profiles core and dev) has its own semver in its
|
|
17
17
|
`manifest.yml` and, when it needs a newer chi, a `requires_chi:` line. They never upgrade by themselves: users run
|
|
18
18
|
`chi update` (all of them, with the gem) or `chi bundle upgrade NAME`. So:
|
|
19
19
|
- a change to a bundle's files bumps that bundle's `version:`
|
data/docs/scheduling.md
CHANGED
|
@@ -93,6 +93,10 @@ the fold. If a local moment does not exist (a spring-forward gap), it fires at t
|
|
|
93
93
|
next valid minute. Schedules use the machine's local clock; there are no named
|
|
94
94
|
time zones.
|
|
95
95
|
|
|
96
|
+
A daily chain, for example a project's nightly reflect ([bundles.md](bundles.md#a-nightly-reflect-session)):
|
|
97
|
+
`chi schedule add --session <ID> --every 1d --title "reflect <project>" --keep-links 14 --max-skips 288 -m "Reflect
|
|
98
|
+
on this project's last day: follow skill_reflect."`, run at the hour you want it to fire.
|
|
99
|
+
|
|
96
100
|
`catch_up` says what an overdue entry does when the tick arrives after its moment:
|
|
97
101
|
|
|
98
102
|
- `once` (default): fire once now, then reschedule normally.
|
data/docs/sessions.md
CHANGED
|
@@ -87,6 +87,13 @@ order: `recap`, else the asking bundle's name). It has no records: the block its
|
|
|
87
87
|
by the turn's end during one) and loaded; it is never in `tokens` or `model_segments`. `chi sessions stats --format
|
|
88
88
|
json` has it in `metrics.side`; a file without it (older, or no side requests) has none.
|
|
89
89
|
|
|
90
|
+
Two read-only readers outside the worker read the session file, `analytics.json` and `cards.json` too: `SessionStats`
|
|
91
|
+
(a page's `runtime.sessions`: counts) and `TurnExcerpts` (`runtime.turn_excerpts`: a turn's failing calls), both
|
|
92
|
+
treating every field as optional ([pages.md](pages.md#reading-chis-data-runtime)). A shape change to those files must
|
|
93
|
+
keep them working (`spec/session_stats_spec.rb`, `spec/turn_excerpts_spec.rb`). `SessionStats.summaries` (archived
|
|
94
|
+
included, no test_run or scratch session, by project) is the one listing both its counts and the knowledge bundle's
|
|
95
|
+
sessions source (`ctx.sessions.list`) read; `Session.list(changed_since:)` skips an unchanged file before parsing it.
|
|
96
|
+
|
|
90
97
|
Each turn record in `analytics.json` names the model the turn ran on as `model_ref` (the asked `host:model`, from its first generation; `model` stays the name the server says it served); a turn stopped before it asked a model has none. The first turn after a `/model` switch (or a hook's) to another model also records what its first generation prefilled on the new model: `switch_prefill_tokens` (the prompt less the cached reads; none under 128 tokens, or for a generation with estimated counts) and `switch_prefill_cost_estimate` (from the new model's configured price, no completion; none without a price). A session saved before this has no `model_ref` on its records.
|
|
91
98
|
|
|
92
99
|
A session whose worker waits for an answer (a question, a hook's question, a guardrail approval, or the step-limit question of a turn that ran out of steps, which waits between turns) reads `waiting` in the status column, and json has `waiting: "question"|"approval"|"hook"|"continue"` and `waiting_id`, the question's id for `chi answer --question` (both null otherwise). A delegate's approval its parent relayed to its own card (see *Delegating*) reads `waiting (in parent ab12)`, json `relayed_to: "ab12…"` (the parent's short id, else null). Only while a worker runs it: a question a dead worker left in the file shows the saved status. Answer it with `chi answer`, the web or `chi --attach ID`; `chi sessions stop ID` frees the worker instead.
|
|
@@ -172,7 +179,7 @@ chi sessions clean --dry-run --days 7 # test sessions older than 7 da
|
|
|
172
179
|
| `working_set_pins` | The working-set items the user pinned (`/working-set pin`); kept by a respawn, a continue and a fork. | the Engine's command |
|
|
173
180
|
| `review` | An older chi's review session's subjects and source (`SessionReview`), kept as written, never read: see [Older review sessions](#older-review-sessions). | an older chi |
|
|
174
181
|
| `first_turn_id` | The id the creating prompt's turn gets, so a wait can name it (`chi send --new --wait`, `--wait --turn T ID`); `null` for a session created idle. | `SessionManager.spawn_session` |
|
|
175
|
-
| `first_turn_client_id` | Who sent the creating prompt
|
|
182
|
+
| `first_turn_client_id` | Who sent the creating prompt: any sender that is no human's (`delegate:<parent8>` for a delegate's task, `system:schedule:<id>` for a chain schedule's link, `agent:<name>`) and an interactive one (a web tab's `web:…`, a TUI's `tui:…`); none for `chi send --new`, an idle create, and a file from before interactive ids were kept: that first turn's origin, so its line is no command of the owner's (docs/guardrails.md, Who may run a session command) and `last_turn` names the sender. Written only when set; not carried by a fork, a continue or a delegate. An older chi keeps it but runs the first prompt as a person's. | `SessionManager.spawn_session` (its `client_id:`) |
|
|
176
183
|
| `verify_fresh` | A Verify fresh child's requester, rules and claim (`VerifyFreshOrigin`). | `VerifyFresh.start`, once |
|
|
177
184
|
|
|
178
185
|
## Context notes
|
data/docs/testing.md
CHANGED
|
@@ -56,7 +56,9 @@ chose spawn with `GitEnv::UNSET`, in any run.
|
|
|
56
56
|
`chi web --tunnel tailscale` spec writes a fake one and sets the variable.
|
|
57
57
|
No spec reaches a running kitty either (Panes opens tabs and pastes into windows): the spec config has no
|
|
58
58
|
`kitty.listen_on` and `spec_helper.rb` drops `SAMAGOTCHI_KITTY_LISTEN_ON`; a pane spec stubs `Config.get("kitty.*")`
|
|
59
|
-
with `spec/fixtures/desktop/fake_kitty` (`ls`, `launch`, `focus-window`, `send-text`, `send-key`).
|
|
59
|
+
with `spec/fixtures/desktop/fake_kitty` (`ls`, `launch`, `focus-window`, `send-text`, `send-key`). It drops
|
|
60
|
+
`KITTY_WINDOW_ID` too, so an attached TUI a spec runs never marks the developer's kitty window (the `chi_session`
|
|
61
|
+
user var, [internals/panes.md](internals/panes.md)).
|
|
60
62
|
|
|
61
63
|
Goldens (`*_golden_spec.rb`, the prompt snapshots) hold no machine state, so they pass on any runner: no absolute
|
|
62
64
|
paths (replace them with placeholders), no PATH tools (the suite pins `rg` as found, `spec_helper.rb`; an example
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: claude-peer
|
|
3
|
-
version: 0.
|
|
3
|
+
version: 0.10.0
|
|
4
4
|
scope: system
|
|
5
5
|
description: "Pair this session with a Claude Code session you work with: the claude_send tool and /claude send it a part of the work (a plan step, a diff, a finding) to validate or as context, and Claude's answer comes back as a message with its text (chi peer say); /claude install-skill installs Claude's chi-peer skill, which pairs"
|
|
6
6
|
trust_level: reviewed
|
|
@@ -10,15 +10,18 @@ files:
|
|
|
10
10
|
description: "Send a part of the work to the paired Claude Code session (claude_send): only when the user asks, or to reply to its answer in a peer turn; validate, context or question; Claude's answer comes as a message with its text"
|
|
11
11
|
plugin:
|
|
12
12
|
file: plugin.rb
|
|
13
|
-
sha256: sha256:
|
|
13
|
+
sha256: sha256:a20b84513f78a4c46d1d396e9d8b72fd719a4ed66e48cdf628cb8f8bea492fb6
|
|
14
14
|
scripts:
|
|
15
15
|
pair-remote: sha256:d7681f87a194e540b629564f84cad9f2d0a763197502c74614276ef88a8920cc
|
|
16
16
|
pair: sha256:626b1c4acba6b1ab8a9d9615c709d69da6953d89d32469f370c2eaad145a8bb8
|
|
17
17
|
chi-peer-SKILL.md: sha256:e3d5bd01d221a3206e203098bf7e1e1fbbbf72b2124bae744eace1816d8eca0c
|
|
18
|
-
# config.yml bundles: <name>: start_command: the shell line /<peer> start runs in its kitty
|
|
19
|
-
|
|
18
|
+
# config.yml bundles: <name>: start_command: the shell line /<peer> start runs in its kitty window; open_in: pane
|
|
19
|
+
# (the default: a pane next to this session's chi terminal in kitty, else a background tab) or tab (always a
|
|
20
|
+
# background tab). Another key warns.
|
|
21
|
+
settings: {start_command: string, open_in: string}
|
|
20
22
|
# The plugin needs Samagotchi::Plugin::Peer on the mailbox: chi.peer and
|
|
21
23
|
# ctx.peers, and the skill's chi peer pair|say, are the next chi's (an older
|
|
22
24
|
# chi has no chi.peer: the plugin fails to load there).
|
|
23
|
-
requires_chi: ">= 0.
|
|
25
|
+
requires_chi: ">= 0.195.0"
|
|
24
26
|
# The pair script's no-id form (chi peer pair --here), --peer-session, /<peer> pair and the start hook are the next chi's.
|
|
27
|
+
# open_in (a pane next to the session's chi terminal) is the next chi's.
|
|
@@ -75,8 +75,8 @@ class Plugin < Samagotchi::Plugin::Peer
|
|
|
75
75
|
SKILL_CARD = "claude-skill"
|
|
76
76
|
USAGE = "usage: /claude — whether a Claude Code session is paired (else the ones here); /claude pair — pair the one " \
|
|
77
77
|
"here, or wait for one and list them; /claude pair <id8> — pair that one; /claude show <id8> — bring its kitty " \
|
|
78
|
-
"window to the front; /claude start — open Claude Code in a kitty
|
|
79
|
-
"that
|
|
78
|
+
"window to the front; /claude start — open Claude Code in a kitty window, paired when it starts; /claude show — " \
|
|
79
|
+
"bring that window to the front; /claude unpair — end the pairing or the wait; /claude validate <text> | context <text> — " \
|
|
80
80
|
"send the text to it; /claude last — send the last answer as context; /claude install-skill — install Claude's " \
|
|
81
81
|
"chi-peer skill, which pairs; /claude install-hook [--yes] — Claude Code's start hook, which lists a Claude here"
|
|
82
82
|
# The shipped bundle keeps scripts/ beside plugin.rb; an installed one
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: codex-peer
|
|
3
|
-
version: 0.
|
|
3
|
+
version: 0.9.0
|
|
4
4
|
scope: system
|
|
5
5
|
description: "Pair this session with an OpenAI Codex CLI session you work with: the codex_send tool and /codex send it a part of the work (a plan step, a diff, a finding) to validate or as context, with codex queue, and Codex's answer comes back as a message with its text (chi peer say); /codex install-skill installs Codex's chi-peer skill, which pairs"
|
|
6
6
|
trust_level: reviewed
|
|
@@ -10,14 +10,17 @@ files:
|
|
|
10
10
|
description: "Send a part of the work to the paired Codex CLI session (codex_send): only when the user asks, or to reply to its answer in a peer turn; validate, context or question; Codex's answer comes as a message with its text"
|
|
11
11
|
plugin:
|
|
12
12
|
file: plugin.rb
|
|
13
|
-
sha256: sha256:
|
|
13
|
+
sha256: sha256:f1c328b6afd3b13b4c52dc777b79f4cfcbc3852c0e76b5434ac54326b6524a34
|
|
14
14
|
scripts:
|
|
15
15
|
pair: sha256:17d804d2f27e5034b26fe83e675a41720b95fddb7209fc7828253f40445f55f8
|
|
16
16
|
chi-peer-SKILL.md: sha256:0a793a97673c2831a3f974771bd20b16f4b28f1b9c3da710de7a17dcaae23be4
|
|
17
|
-
# config.yml bundles: <name>: start_command: the shell line /<peer> start runs in its kitty
|
|
18
|
-
|
|
17
|
+
# config.yml bundles: <name>: start_command: the shell line /<peer> start runs in its kitty window; open_in: pane
|
|
18
|
+
# (the default: a pane next to this session's chi terminal in kitty, else a background tab) or tab (always a
|
|
19
|
+
# background tab). Another key warns.
|
|
20
|
+
settings: {start_command: string, open_in: string}
|
|
19
21
|
# The plugin needs Samagotchi::Plugin::Peer on the mailbox: chi.peer and
|
|
20
22
|
# ctx.peers, and the skill's chi peer pair|say, are the next chi's (an older
|
|
21
23
|
# chi has no chi.peer: the plugin fails to load there).
|
|
22
|
-
requires_chi: ">= 0.
|
|
24
|
+
requires_chi: ">= 0.195.0"
|
|
23
25
|
# The pair script's no-id form (chi peer pair --here), --peer-session, /<peer> pair and the start hook are the next chi's.
|
|
26
|
+
# open_in (a pane next to the session's chi terminal) is the next chi's.
|
|
@@ -85,7 +85,7 @@ class Plugin < Samagotchi::Plugin::Peer
|
|
|
85
85
|
STATUS_CARD = "codex"
|
|
86
86
|
SKILL_CARD = "codex-skill"
|
|
87
87
|
USAGE = "usage: /codex — whether a Codex session is paired; /codex pair — wait for one (say \"pair with chi\" in " \
|
|
88
|
-
"Codex, or, with the start hook, anything in a new Codex); /codex start — open Codex in a kitty
|
|
88
|
+
"Codex, or, with the start hook, anything in a new Codex); /codex start — open Codex in a kitty window, its first " \
|
|
89
89
|
"prompt \"pair with chi\" (one Codex turn); /codex unpair — end the pairing or the wait; /codex validate " \
|
|
90
90
|
"<text> | context <text> — send the text to it; /codex last — send the last answer as context; /codex " \
|
|
91
91
|
"install-skill — install Codex's chi-peer skill, which pairs; /codex install-hook [--yes] — Codex's start " \
|
|
@@ -152,12 +152,12 @@ class Plugin < Samagotchi::Plugin::Peer
|
|
|
152
152
|
def hook_written_line
|
|
153
153
|
"Added to `#{hook_file}`. Codex asks you to review it the next time it starts: choose Review hooks (or `/hooks`) " \
|
|
154
154
|
"and trust it; until then Codex skips it. A Codex started with `/codex start` stops on that screen in its " \
|
|
155
|
-
"
|
|
155
|
+
"kitty window the first time: Show it."
|
|
156
156
|
end
|
|
157
157
|
|
|
158
158
|
def hook_stale_tail = ", then trust it in Codex (it is a changed hook)"
|
|
159
159
|
|
|
160
|
-
def start_review_hint = "if Codex shows Hooks need review, Show its
|
|
160
|
+
def start_review_hint = "if Codex shows Hooks need review, Show its window and answer it"
|
|
161
161
|
|
|
162
162
|
# The paired Codex's thread id: the pairing's address.
|
|
163
163
|
def check_hint!(hint)
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: coordinator
|
|
3
|
-
version: 0.
|
|
3
|
+
version: 0.11.0
|
|
4
4
|
scope: system
|
|
5
|
-
description: "Coordinate parallel work: the coordinator skill (split the work, one worktree and child session per task, check each report, merges under a guardrail approval (the git-merge rule) or a question, a handoff memory to resume from), /coordinate <goal>, /coordinate resume, /coordinate end and /children; the coordinator toolkit (lean + delegate, delegate_result, list_sessions, objectives) and the coding toolkit for build children (child + be concise)"
|
|
5
|
+
description: "Coordinate parallel work: the coordinator skill (split the work, one worktree and child session per task, check each report, merges under a guardrail approval (the git-merge rule) or a question, a handoff memory to resume from), /coordinate <goal>, /coordinate resume, /coordinate end and /children; the coordinator toolkit (lean + delegate, delegate_result, list_sessions, objectives, and the knowledge catalog tools) and the coding toolkit for build children (child + be concise)"
|
|
6
6
|
trust_level: reviewed
|
|
7
7
|
files:
|
|
8
8
|
skill_coordinator.md:
|
|
9
|
-
sha256: sha256:
|
|
9
|
+
sha256: sha256:b7783de28c307c3fbe594cb527dbdc379261b844f5408b2bfdb570bd0f14e12e
|
|
10
10
|
description: "Coordinate parallel work: split it, delegate each task wait:false in its own worktree (cwd:), check each report, merge under the git-merge approval or ask the user; resume from a handoff_* memory"
|
|
11
11
|
plugin:
|
|
12
12
|
file: plugin.rb
|
|
13
|
-
sha256: sha256:
|
|
13
|
+
sha256: sha256:b0047788e6f3c706943892e7b97e0b643afddda2ec1d134be60044fa5521ca12
|
|
14
14
|
# the coordinator toolkit needs chi.toolkit and ctx.use_toolkit (0.66.0); the
|
|
15
15
|
# coding toolkit (chi.toolkit prompt:) and the skill's delegate toolkit: need
|
|
16
16
|
# the next chi.
|
|
@@ -19,3 +19,6 @@ requires_chi: ">= 0.107.0"
|
|
|
19
19
|
# live older worker deny every call. A chi without `chi guardrails rule` gives a usage error, and the skill then asks.
|
|
20
20
|
# Nor for the objectives tool in the kit and the skill: on an older chi the kit's add names nothing (it declares only what
|
|
21
21
|
# is registered), and a call of it comes back as an unknown tool, no harm.
|
|
22
|
+
# Nor for ctx.project (/coordinate resume reads the project's handoffs through it): on an older chi /coordinate resume
|
|
23
|
+
# says to run chi update. Nor for the knowledge tools in the kit: without the knowledge bundle (or on an older chi) the
|
|
24
|
+
# kit's add names nothing registered.
|
|
@@ -35,8 +35,9 @@
|
|
|
35
35
|
# itself (the core child-boundary guardrail), in every mode: nothing to set up.
|
|
36
36
|
#
|
|
37
37
|
# The coordinator toolkit is lean plus the tools that run children
|
|
38
|
-
# (delegate, delegate_result, list_sessions)
|
|
39
|
-
# moves)
|
|
38
|
+
# (delegate, delegate_result, list_sessions), objectives (the skill's
|
|
39
|
+
# moves) and the knowledge bundle's catalog tools (knowledge_add,
|
|
40
|
+
# knowledge_list, knowledge_remove: the skill's Knowledge sources). /coordinate puts the session
|
|
40
41
|
# on it before it asks the model (ctx.use_toolkit), which chi allows only
|
|
41
42
|
# before the session's first model reply and outside a turn: otherwise it
|
|
42
43
|
# says to run /toolkit coordinator, unless the session declares delegate
|
|
@@ -66,7 +67,11 @@ class Plugin
|
|
|
66
67
|
def initialize(_settings = {}); end
|
|
67
68
|
|
|
68
69
|
TOOLKIT = "coordinator"
|
|
69
|
-
|
|
70
|
+
# The knowledge bundle's catalog tools (chi.tool(declare: :kit)): this kit
|
|
71
|
+
# adds them; knowledge_search is declared everywhere. An add: naming a tool
|
|
72
|
+
# no bundle registered declares nothing (Toolkit::Resolved#declares? sees
|
|
73
|
+
# registered entries only), so without the bundle the kit is as it was.
|
|
74
|
+
TOOLKIT_TOOLS = %w[delegate delegate_result list_sessions objectives knowledge_add knowledge_list knowledge_remove].freeze
|
|
70
75
|
CODING_TOOLKIT = "coding"
|
|
71
76
|
CODING_PROMPT = "- Be concise in your responses\n- Show file paths clearly when working with files"
|
|
72
77
|
|
|
@@ -154,6 +159,9 @@ class Plugin
|
|
|
154
159
|
|
|
155
160
|
# /coordinate resume [name]
|
|
156
161
|
def resume(name, ctx)
|
|
162
|
+
# An older chi (this bundle upgraded alone) has no ctx.project: say so.
|
|
163
|
+
return "/coordinate resume needs a newer chi (ctx.project): run chi update" unless ctx.respond_to?(:project)
|
|
164
|
+
|
|
157
165
|
open = open_handoffs(ctx)
|
|
158
166
|
if name.empty?
|
|
159
167
|
return "no open coordinator handoff (handoff_* memory) in this project" if open.empty?
|
|
@@ -193,8 +201,10 @@ class Plugin
|
|
|
193
201
|
# The project's handoff_* memories whose index line isn't DONE, in index
|
|
194
202
|
# order: [{name:, description:}]. A line without its file is left out.
|
|
195
203
|
def open_handoffs(ctx)
|
|
196
|
-
dir =
|
|
197
|
-
|
|
204
|
+
dir = ctx.project.memories_dir
|
|
205
|
+
return [] unless dir
|
|
206
|
+
|
|
207
|
+
index = File.join(dir, "index.md")
|
|
198
208
|
return [] unless File.file?(index)
|
|
199
209
|
|
|
200
210
|
File.read(index, encoding: "UTF-8").each_line.filter_map do |line|
|
|
@@ -145,6 +145,14 @@ changed. Do it without asking; chi asks the user to approve a command source, so
|
|
|
145
145
|
`chi context rm <name>` when the epic is done (step 8).
|
|
146
146
|
- Don't poll a source with register_reminder, a schedule or by reading it again each turn.
|
|
147
147
|
|
|
148
|
+
## Knowledge sources
|
|
149
|
+
Keep the epic's sources findable with knowledge_search (the knowledge bundle; skip this if you don't have the
|
|
150
|
+
tool). Its catalog already has the project's docs and memories. When you find a source the epic keeps coming back
|
|
151
|
+
to (a docs folder, a legacy investigation, another checkout), add it once with knowledge_add (kind folder, a
|
|
152
|
+
max_age: 1h for a live folder, 1d for a repo, never for a closed one, and a `why`), and write its name into the
|
|
153
|
+
handoff. A daily review passes its named catalog (catalog: "daily-review") on every knowledge call. Search before
|
|
154
|
+
asking the user something the sources may hold; a hit is a pointer: read it before you rely on it.
|
|
155
|
+
|
|
148
156
|
## End of day
|
|
149
157
|
When the user ends the day (`/coordinate end`, "that's all for today"), before you answer:
|
|
150
158
|
1. Check where things stand: for each branch in your handoff `git log --oneline <default>..<branch>` and
|
|
@@ -192,3 +200,5 @@ Don't start, merge, stop or clean up anything here: a child still running stays
|
|
|
192
200
|
- 2026-11-14 Sprint review removed with /coordinate review: chi has no read-only review sessions (0.9.0)
|
|
193
201
|
- 2026-11-28 Watching what can change the scope: attach an outside source as context (a PR URL, or a script
|
|
194
202
|
printing stable text, a summary in your own words, a wake rule), never poll it; "Watching:" in the handoff (0.10.0)
|
|
203
|
+
- 2026-11-30 Knowledge sources: keep the epic's catalog with knowledge_add, a daily review's named catalog,
|
|
204
|
+
search before asking (0.11.0)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: dev
|
|
3
|
-
version: 0.7.
|
|
3
|
+
version: 0.7.13
|
|
4
4
|
scope: system
|
|
5
5
|
description: "Developer tools"
|
|
6
6
|
trust_level: reviewed
|
|
7
|
-
requires_chi: ">= 0.
|
|
7
|
+
requires_chi: ">= 0.195.0"
|
|
8
8
|
includes:
|
|
9
9
|
- known-names
|
|
10
10
|
- mcp
|
|
@@ -19,3 +19,4 @@ includes:
|
|
|
19
19
|
- model-notes
|
|
20
20
|
- decision-card
|
|
21
21
|
- inline-widgets
|
|
22
|
+
- knowledge
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The knowledge bundle's adapters (manifest.yml plugin: parts:): where a
|
|
4
|
+
# source's items come from. Each lists Items (#items: key, sig, title,
|
|
5
|
+
# pointer, path, updated_at; a refresh re-reads only the ones whose sig
|
|
6
|
+
# changed) and reads one's text (#text, nil to skip it).
|
|
7
|
+
# FolderAdapter a folder: inside a git work tree its tracked files
|
|
8
|
+
# only (git ls-files), else a walk; never a symlink, a
|
|
9
|
+
# file whose real path leaves the root, or one named like
|
|
10
|
+
# a secret (ctx.project.secret_name?)
|
|
11
|
+
# MemoriesAdapter chi's memories: the project's and the system ones
|
|
12
|
+
# (ctx.project's dirs), top-level .md files only
|
|
13
|
+
require "find"
|
|
14
|
+
require "open3"
|
|
15
|
+
|
|
16
|
+
class Plugin
|
|
17
|
+
Item = Data.define(:key, :sig, :title, :ptr, :url, :path, :updated_at)
|
|
18
|
+
|
|
19
|
+
class FolderAdapter
|
|
20
|
+
# The text files a folder source reads by default (include:).
|
|
21
|
+
DEFAULT_INCLUDE = ["**/*.{md,txt,rst,adoc,org,rb,py,js,ts,go,rs,java,kt,swift,c,h,yml,yaml,json,toml,sql,sh,html,css}",
|
|
22
|
+
"**/README", "**/README.*", "**/Makefile"].freeze
|
|
23
|
+
# The project's own docs (the project-docs default source).
|
|
24
|
+
DOCS_INCLUDE = ["**/*.{md,txt,rst,adoc}", "README*", "AGENTS.md", "CLAUDE.md", "CHANGELOG*"].freeze
|
|
25
|
+
# A walk (no git) skips these folders, and any dot folder.
|
|
26
|
+
SKIP_DIRS = %w[node_modules vendor tmp].freeze
|
|
27
|
+
MAX_FILE_BYTES = 1024 * 1024
|
|
28
|
+
MAX_FILES = 20_000
|
|
29
|
+
GLOB = File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
|
|
30
|
+
|
|
31
|
+
# Why +root+ (absolute) can't be a folder source, nil when it can: /,
|
|
32
|
+
# the home folder or above it, chi's own dirs or memories (chi data
|
|
33
|
+
# goes through kind chi), or a secret root (~/.ssh).
|
|
34
|
+
# @param project [#chi_dirs, #memories_dir, #system_memories_dir, #secret_root?]
|
|
35
|
+
def self.refusal(root, project:, home: Dir.home)
|
|
36
|
+
return "#{root} is not a folder" unless File.directory?(root)
|
|
37
|
+
|
|
38
|
+
real = File.realpath(root)
|
|
39
|
+
home_real = begin File.realpath(home.to_s) rescue home.to_s end
|
|
40
|
+
return "#{root} is /: add a folder inside it" if real == "/"
|
|
41
|
+
return "#{root} holds the home folder: add a folder inside it" if home_real == real || home_real.start_with?("#{real}/")
|
|
42
|
+
|
|
43
|
+
chi = [*project.chi_dirs, project.memories_dir, project.system_memories_dir].compact
|
|
44
|
+
chi = chi.flat_map { |dir| [dir, (File.realpath(dir) if File.exist?(dir))].compact }
|
|
45
|
+
if chi.any? { |dir| real == dir || real.start_with?("#{dir}/") || dir.start_with?("#{real}/") }
|
|
46
|
+
return "#{root} is chi's own data (add memories with kind chi)"
|
|
47
|
+
end
|
|
48
|
+
return "#{root} holds secrets" if project.secret_root?(root) || project.secret_root?(real)
|
|
49
|
+
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The git calls in a folder the user added, which may be a copied
|
|
54
|
+
# repository nobody vetted: its own core.fsmonitor would run a command
|
|
55
|
+
# on ls-files (nothing else in its config runs one for these two).
|
|
56
|
+
GIT = ["git", "-c", "core.fsmonitor=false"].freeze
|
|
57
|
+
|
|
58
|
+
# Whether +root+ is inside a git work tree (a bare repository's dir is not).
|
|
59
|
+
def self.work_tree?(root)
|
|
60
|
+
out, status = Open3.capture2e(Samagotchi::GitEnv::UNSET, *GIT, "-C", root, "rev-parse", "--is-inside-work-tree")
|
|
61
|
+
status.success? && out.strip == "true"
|
|
62
|
+
rescue SystemCallError
|
|
63
|
+
false
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The counts #items left out: by a secret name, a symlink (or a path
|
|
67
|
+
# leaving the root), over MAX_FILE_BYTES, past +max_files+.
|
|
68
|
+
attr_reader :skipped
|
|
69
|
+
|
|
70
|
+
# @param root [String] the folder (absolute)
|
|
71
|
+
# @param include [Array<String>] globs on the path relative to +root+
|
|
72
|
+
# @param exclude [Array<String>]
|
|
73
|
+
# @param tracked_only [Boolean] a folder outside git lists nothing (project-docs)
|
|
74
|
+
def initialize(root, project:, include: DEFAULT_INCLUDE, exclude: [], max_files: MAX_FILES, tracked_only: false)
|
|
75
|
+
@root = root
|
|
76
|
+
@real = File.realpath(root)
|
|
77
|
+
@project = project
|
|
78
|
+
@include = include.empty? ? DEFAULT_INCLUDE : include
|
|
79
|
+
@exclude = exclude
|
|
80
|
+
@max_files = max_files
|
|
81
|
+
@tracked_only = tracked_only
|
|
82
|
+
@skipped = { secret: 0, symlink: 0, large: 0, capped: 0 }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @return [Array<Item>]
|
|
86
|
+
def items
|
|
87
|
+
paths = if FolderAdapter.work_tree?(@root)
|
|
88
|
+
tracked
|
|
89
|
+
else
|
|
90
|
+
(@tracked_only ? [] : walked)
|
|
91
|
+
end
|
|
92
|
+
paths = paths.select { |rel| wanted?(rel) }
|
|
93
|
+
if paths.size > @max_files
|
|
94
|
+
@skipped[:capped] = paths.size - @max_files
|
|
95
|
+
paths = paths.first(@max_files)
|
|
96
|
+
end
|
|
97
|
+
paths.filter_map { |rel| item(rel) }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The file's text (scrubbed UTF-8), nil for a binary one or one that
|
|
101
|
+
# changed into a symlink since it was listed.
|
|
102
|
+
def text(item)
|
|
103
|
+
return nil if File.symlink?(item.path)
|
|
104
|
+
|
|
105
|
+
Chunker.clean(File.binread(item.path, MAX_FILE_BYTES))
|
|
106
|
+
rescue SystemCallError
|
|
107
|
+
nil
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
def tracked
|
|
113
|
+
out, status = Open3.capture2(Samagotchi::GitEnv::UNSET, *GIT, "-C", @root, "ls-files", "-z")
|
|
114
|
+
status.success? ? out.split("\0").reject(&:empty?) : []
|
|
115
|
+
rescue SystemCallError
|
|
116
|
+
[]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def walked
|
|
120
|
+
found = []
|
|
121
|
+
Find.find(@root) do |path|
|
|
122
|
+
rel = path.delete_prefix("#{@root}/")
|
|
123
|
+
next if path == @root
|
|
124
|
+
|
|
125
|
+
if File.directory?(path) && !File.symlink?(path)
|
|
126
|
+
name = File.basename(path)
|
|
127
|
+
Find.prune if name.start_with?(".") || SKIP_DIRS.include?(name)
|
|
128
|
+
next
|
|
129
|
+
end
|
|
130
|
+
found << rel
|
|
131
|
+
break if found.size > @max_files * 2
|
|
132
|
+
end
|
|
133
|
+
found.sort
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def wanted?(rel)
|
|
137
|
+
@include.any? { |glob| File.fnmatch?(glob, rel, GLOB) } && @exclude.none? { |glob| File.fnmatch?(glob, rel, GLOB) }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def item(rel)
|
|
141
|
+
path = File.join(@root, rel)
|
|
142
|
+
stat = File.lstat(path)
|
|
143
|
+
return skip(:symlink) if stat.symlink?
|
|
144
|
+
return nil unless stat.file?
|
|
145
|
+
return skip(:symlink) unless File.realpath(path).start_with?("#{@real}/")
|
|
146
|
+
return skip(:secret) if @project.secret_name?(rel)
|
|
147
|
+
return skip(:large) if stat.size > MAX_FILE_BYTES
|
|
148
|
+
|
|
149
|
+
Item.new(key: rel, sig: "#{stat.mtime.to_i}:#{stat.size}", title: rel, ptr: path, url: nil, path: path,
|
|
150
|
+
updated_at: stat.mtime.to_i)
|
|
151
|
+
rescue SystemCallError
|
|
152
|
+
nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def skip(why)
|
|
156
|
+
@skipped[why] += 1
|
|
157
|
+
nil
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
class MemoriesAdapter
|
|
162
|
+
# @param dirs [Hash{String => String}] scope => memories dir
|
|
163
|
+
# (ctx.project's: "project" and "system")
|
|
164
|
+
def initialize(dirs)
|
|
165
|
+
@dirs = dirs.compact
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def skipped = {}
|
|
169
|
+
|
|
170
|
+
# @return [Array<Item>] one per memory file (an overlay too), not index.md
|
|
171
|
+
def items
|
|
172
|
+
@dirs.flat_map do |scope, dir|
|
|
173
|
+
next [] unless File.directory?(dir)
|
|
174
|
+
|
|
175
|
+
Dir.children(dir).sort.filter_map do |file|
|
|
176
|
+
next unless file.end_with?(".md") && file != "index.md" && !file.start_with?(".")
|
|
177
|
+
|
|
178
|
+
path = File.join(dir, file)
|
|
179
|
+
stat = File.lstat(path)
|
|
180
|
+
next unless stat.file?
|
|
181
|
+
|
|
182
|
+
name = memory_name(dir, file.delete_suffix(".md"))
|
|
183
|
+
Item.new(key: "#{scope}:#{file}", sig: "#{stat.mtime.to_i}:#{stat.size}", title: "memory #{name} (#{scope})",
|
|
184
|
+
ptr: "memory_read #{name} #{scope}", url: nil, path: path, updated_at: stat.mtime.to_i)
|
|
185
|
+
rescue SystemCallError
|
|
186
|
+
nil
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def text(item)
|
|
192
|
+
Chunker.clean(File.binread(item.path, FolderAdapter::MAX_FILE_BYTES))
|
|
193
|
+
rescue SystemCallError
|
|
194
|
+
nil
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
private
|
|
198
|
+
|
|
199
|
+
# An overlay (<name>.<model-key>.md) is read with its memory's name.
|
|
200
|
+
def memory_name(dir, base)
|
|
201
|
+
stem = base.sub(/\.[^.]+\z/, "")
|
|
202
|
+
stem != base && File.file?(File.join(dir, "#{stem}.md")) ? stem : base
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|