samagotchi 0.193.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 +116 -1
- data/docs/bundle-authoring.md +2 -2
- data/docs/bundles.md +223 -12
- data/docs/cli.md +27 -11
- data/docs/configuration.md +93 -6
- data/docs/desktop.md +3 -0
- data/docs/guardrails.md +11 -1
- data/docs/hooks.md +1 -0
- data/docs/internals/attached-context.md +2 -0
- data/docs/internals/bridge.md +4 -0
- data/docs/internals/engine.md +59 -4
- data/docs/internals/knowledge.md +205 -0
- data/docs/internals/pages.md +17 -9
- data/docs/internals/panes.md +104 -0
- data/docs/internals/peers.md +102 -5
- data/docs/internals/prompt-caching.md +4 -0
- data/docs/internals/session-lifecycle.md +9 -2
- data/docs/internals/web-frontend.md +16 -4
- data/docs/memory.md +4 -2
- data/docs/pages.md +72 -1
- data/docs/peers.md +90 -5
- data/docs/plugins.md +100 -6
- data/docs/releasing.md +1 -1
- data/docs/scheduling.md +4 -0
- data/docs/sessions.md +23 -7
- data/docs/sub-agent.md +6 -4
- data/docs/system-one.md +4 -0
- data/docs/testing.md +5 -0
- data/lib/samagotchi/bridge/card_store.rb +1 -1
- data/lib/samagotchi/broadcast/triage_model.rb +17 -0
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +11 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +28 -7
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +11 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +23 -2
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
- 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/chi_invocation.rb +7 -0
- data/lib/samagotchi/client.rb +18 -0
- data/lib/samagotchi/codex_rollout.rb +93 -0
- data/lib/samagotchi/config.rb +15 -6
- data/lib/samagotchi/effective_model.rb +28 -2
- data/lib/samagotchi/engine/cards.rb +9 -4
- data/lib/samagotchi/engine/hooks.rb +8 -0
- data/lib/samagotchi/engine/models.rb +180 -7
- data/lib/samagotchi/engine/side_requests.rb +72 -13
- data/lib/samagotchi/engine/toolkit.rb +1 -1
- data/lib/samagotchi/engine/turn_helpers.rb +1 -1
- data/lib/samagotchi/engine.rb +11 -8
- data/lib/samagotchi/engine_snapshots.rb +16 -3
- data/lib/samagotchi/events.rb +1 -0
- 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/hooks.rb +3 -1
- data/lib/samagotchi/host_registry.rb +146 -16
- data/lib/samagotchi/idle_client.rb +13 -2
- data/lib/samagotchi/idle_recap.rb +33 -3
- data/lib/samagotchi/idle_target.rb +27 -7
- data/lib/samagotchi/llm/adapter_common.rb +4 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +3 -2
- data/lib/samagotchi/llm/errors.rb +35 -4
- data/lib/samagotchi/llm/model_listing.rb +19 -0
- data/lib/samagotchi/llm/openai_chat.rb +3 -2
- data/lib/samagotchi/model_binding.rb +46 -4
- data/lib/samagotchi/model_catalog.rb +68 -8
- data/lib/samagotchi/model_profile.rb +35 -0
- data/lib/samagotchi/models_command.rb +8 -4
- 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 +137 -0
- data/lib/samagotchi/panes.rb +254 -0
- data/lib/samagotchi/peer_announce.rb +245 -0
- data/lib/samagotchi/peer_command.rb +97 -11
- data/lib/samagotchi/peer_continue.rb +10 -0
- data/lib/samagotchi/peer_mailbox.rb +30 -7
- data/lib/samagotchi/peer_pair_here.rb +149 -0
- data/lib/samagotchi/peer_presence.rb +434 -0
- data/lib/samagotchi/peer_say.rb +17 -10
- data/lib/samagotchi/peer_wait_expiry.rb +47 -0
- data/lib/samagotchi/plugin/api.rb +18 -1
- data/lib/samagotchi/plugin/attached_context.rb +46 -1
- data/lib/samagotchi/plugin/card.rb +68 -11
- data/lib/samagotchi/plugin/context.rb +42 -6
- data/lib/samagotchi/plugin/panes.rb +97 -0
- data/lib/samagotchi/plugin/peer.rb +52 -10
- data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
- data/lib/samagotchi/plugin/peer_start.rb +326 -0
- data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
- data/lib/samagotchi/plugin/peers.rb +76 -3
- 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/recap_setup.rb +40 -3
- data/lib/samagotchi/self_report.rb +25 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/served_lookup.rb +225 -0
- data/lib/samagotchi/served_ref.rb +41 -0
- data/lib/samagotchi/session.rb +22 -9
- data/lib/samagotchi/session_access/contract.rb +6 -0
- data/lib/samagotchi/session_access/local.rb +54 -6
- data/lib/samagotchi/session_access/remote.rb +4 -4
- data/lib/samagotchi/session_access/remote_peers.rb +7 -4
- data/lib/samagotchi/session_commands.rb +40 -8
- data/lib/samagotchi/session_continue.rb +6 -2
- data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
- data/lib/samagotchi/session_manager/delivery.rb +5 -0
- data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
- data/lib/samagotchi/session_manager/removal.rb +3 -0
- data/lib/samagotchi/session_model_settings.rb +7 -3
- data/lib/samagotchi/session_stats.rb +436 -0
- data/lib/samagotchi/session_stores.rb +9 -0
- data/lib/samagotchi/session_window.rb +30 -0
- data/lib/samagotchi/sessions_command.rb +16 -3
- data/lib/samagotchi/system_one/approval_choice.rb +22 -0
- data/lib/samagotchi/system_one/approval_label.rb +4 -6
- data/lib/samagotchi/system_one/http_backend.rb +111 -10
- data/lib/samagotchi/system_prompt.rb +14 -5
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +30 -0
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
- data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
- data/lib/samagotchi/terminal_ui.rb +1 -1
- data/lib/samagotchi/testing/boundary.rb +17 -4
- data/lib/samagotchi/testing/bundle.rb +6 -2
- 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_endings.rb +6 -2
- data/lib/samagotchi/turn_excerpts.rb +298 -0
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +9 -0
- data/lib/samagotchi/user_typed.rb +29 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +8 -20
- data/lib/samagotchi/web/participant_review.rb +1 -1
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +33 -13
- data/lib/samagotchi/web/public/app.js +1 -1
- data/lib/samagotchi/web/public/card.js +26 -10
- data/lib/samagotchi/web/public/context_chips.js +24 -2
- data/lib/samagotchi/web/public/copy.js +8 -2
- data/lib/samagotchi/web/public/data.js +2 -1
- data/lib/samagotchi/web/public/format.js +9 -1
- data/lib/samagotchi/web/public/index.html +18 -0
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/model_pick.js +18 -5
- data/lib/samagotchi/web/public/model_picker.js +1 -1
- data/lib/samagotchi/web/public/peer_chip.js +15 -5
- data/lib/samagotchi/web/public/peer_data.js +4 -2
- data/lib/samagotchi/web/public/peer_wait.js +36 -0
- data/lib/samagotchi/web/public/served_ref.js +9 -0
- data/lib/samagotchi/web/public/session_fields.js +1 -0
- data/lib/samagotchi/web/public/session_state.js +4 -0
- data/lib/samagotchi/web/public/stage_missing.js +23 -6
- data/lib/samagotchi/web/public/stream_handlers.js +16 -0
- data/lib/samagotchi/web/public/version_notices.js +7 -1
- data/lib/samagotchi/web/session_control.rb +19 -10
- data/lib/samagotchi/web/session_show.rb +20 -0
- data/lib/samagotchi/web/update_ops.rb +2 -2
- data/lib/samagotchi/worker/command_queue.rb +3 -0
- data/lib/samagotchi/worker/mail.rb +14 -1
- data/lib/samagotchi/worker.rb +1 -0
- metadata +39 -1
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
# pair: pair this Claude Code session with a chi session (chi's claude-peer
|
|
3
3
|
# bundle). Run from Claude Code, which sets CLAUDE_CODE_MESSAGING_SOCKET:
|
|
4
4
|
#
|
|
5
|
-
# sh <bundle dir>/scripts/pair <chi session id, prefix or chi web URL>
|
|
5
|
+
# sh <bundle dir>/scripts/pair [<chi session id, prefix or chi web URL>]
|
|
6
6
|
#
|
|
7
7
|
# It pairs (`chi peer pair claude <id> --address <socket>`): the address is
|
|
8
8
|
# this session's inbox socket, where chi's claude_send tool writes; Claude's
|
|
9
|
-
# answers go to the session's mailbox (`chi peer say claude <id>`)
|
|
10
|
-
#
|
|
9
|
+
# answers go to the session's mailbox (`chi peer say claude <id>`), and the
|
|
10
|
+
# pairing names this Claude's session id (--peer-session
|
|
11
|
+
# CLAUDE_CODE_SESSION_ID), so a resume's start hook refreshes it. With no
|
|
12
|
+
# id (`chi peer pair claude --here`): the chi session that waits for a
|
|
13
|
+
# Claude in this folder's project, else its one live session; else chi
|
|
14
|
+
# lists them and exits 1. Again after a Claude restart, it only refreshes
|
|
15
|
+
# the socket. CHI names the chi
|
|
11
16
|
# command (default: chi on the PATH). Through a remote (CHI_REMOTE, or
|
|
12
17
|
# --remote NAME in CHI) chi there can't reach this machine's socket: it pairs
|
|
13
18
|
# for answers only (no address), and says so. With --inbox-socket PATH the
|
|
@@ -17,7 +22,7 @@
|
|
|
17
22
|
set -eu
|
|
18
23
|
|
|
19
24
|
usage() {
|
|
20
|
-
echo "usage: pair [--inbox-socket PATH] <chi session id or URL (http://127.0.0.1:4567/#/s/<id>)>" >&2
|
|
25
|
+
echo "usage: pair [--inbox-socket PATH] [<chi session id or URL (http://127.0.0.1:4567/#/s/<id>)>]" >&2
|
|
21
26
|
exit 2
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -44,7 +49,8 @@ while [ $# -gt 0 ]; do
|
|
|
44
49
|
;;
|
|
45
50
|
esac
|
|
46
51
|
done
|
|
47
|
-
|
|
52
|
+
# --inbox-socket names the remote end of a forward: only with an id.
|
|
53
|
+
[ -n "$target" ] || [ -z "$inbox" ] || usage
|
|
48
54
|
|
|
49
55
|
# A relative path would be chi's cwd, not the forward's remote end: the
|
|
50
56
|
# plugin's check refuses it, so refuse it here with the reason.
|
|
@@ -62,6 +68,8 @@ if [ -z "$sock" ]; then
|
|
|
62
68
|
fi
|
|
63
69
|
|
|
64
70
|
chi=${CHI:-chi}
|
|
71
|
+
# This Claude's own session id: the pairing names it (a resume's refresh).
|
|
72
|
+
psess=${CLAUDE_CODE_SESSION_ID:-}
|
|
65
73
|
|
|
66
74
|
# The remote, as SessionAccess::Dispatch names it: a --remote NAME (or
|
|
67
75
|
# --remote=NAME) word in CHI, the last one, beats CHI_REMOTE (trimmed; blank:
|
|
@@ -80,6 +88,21 @@ for word in $chi; do
|
|
|
80
88
|
esac
|
|
81
89
|
done
|
|
82
90
|
|
|
91
|
+
# No id: chi finds the session here (--here), on this machine only.
|
|
92
|
+
if [ -z "$target" ]; then
|
|
93
|
+
if [ -n "$via" ]; then
|
|
94
|
+
echo "pair: chi is on $remote: pair with an id over a remote (its chi web's URL, or the id from chi sessions list there)" >&2
|
|
95
|
+
exit 2
|
|
96
|
+
fi
|
|
97
|
+
if [ -n "$psess" ]; then
|
|
98
|
+
out=$($chi peer pair claude --here --address "$sock" --from "Claude Code in $PWD" --peer-session "$psess") || exit 1
|
|
99
|
+
else
|
|
100
|
+
out=$($chi peer pair claude --here --address "$sock" --from "Claude Code in $PWD") || exit 1
|
|
101
|
+
fi
|
|
102
|
+
echo "paired with chi ${out%% *} (socket $sock)"
|
|
103
|
+
exit 0
|
|
104
|
+
fi
|
|
105
|
+
|
|
83
106
|
case "$target" in
|
|
84
107
|
http://* | https://*)
|
|
85
108
|
id=$(printf '%s\n' "$target" | sed -n 's|.*/s/\([0-9A-Za-z-][0-9A-Za-z-]*\).*|\1|p')
|
|
@@ -91,7 +114,7 @@ esac
|
|
|
91
114
|
# Without a socket chi can reach, chi can't send here: a pairing with no
|
|
92
115
|
# address (Claude's answers only).
|
|
93
116
|
if [ -n "$via" ] && [ -z "$inbox" ]; then
|
|
94
|
-
$chi peer pair claude "$id" --from "Claude Code in $PWD (on another machine)" >/dev/null
|
|
117
|
+
$chi peer pair claude "$id" --from "Claude Code in $PWD (on another machine)" ${psess:+--peer-session "$psess"} >/dev/null
|
|
95
118
|
ask=$chi
|
|
96
119
|
[ "$via" = chi ] || ask="CHI_REMOTE=$remote $chi"
|
|
97
120
|
printf '%s\n' "pair: chi is on $remote, so chi can't send to this Claude session (claude_send needs its socket on chi's machine): paired with chi $id for answers only. Answer chi with $ask peer say claude $id; chi's replies reach you only when the user relays them (or asks you to run $ask send --wait $id)"
|
|
@@ -100,9 +123,9 @@ if [ -n "$via" ] && [ -z "$inbox" ]; then
|
|
|
100
123
|
fi
|
|
101
124
|
|
|
102
125
|
if [ -z "$inbox" ]; then
|
|
103
|
-
$chi peer pair claude "$id" --address "$sock" --from "Claude Code in $PWD" >/dev/null
|
|
126
|
+
$chi peer pair claude "$id" --address "$sock" --from "Claude Code in $PWD" ${psess:+--peer-session "$psess"} >/dev/null
|
|
104
127
|
echo "paired with chi $id (socket $sock)"
|
|
105
128
|
else
|
|
106
|
-
$chi peer pair claude "$id" --address "$inbox" --from "Claude Code in $PWD, over ssh (its socket $sock)" >/dev/null
|
|
129
|
+
$chi peer pair claude "$id" --address "$inbox" --from "Claude Code in $PWD, over ssh (its socket $sock)" ${psess:+--peer-session "$psess"} >/dev/null
|
|
107
130
|
echo "paired with chi $id (socket $inbox on chi's machine, forwarded to this Claude's $sock)"
|
|
108
131
|
fi
|
|
@@ -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,11 +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
|
-
pair: sha256:
|
|
16
|
-
chi-peer-SKILL.md: sha256:
|
|
15
|
+
pair: sha256:17d804d2f27e5034b26fe83e675a41720b95fddb7209fc7828253f40445f55f8
|
|
16
|
+
chi-peer-SKILL.md: sha256:0a793a97673c2831a3f974771bd20b16f4b28f1b9c3da710de7a17dcaae23be4
|
|
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}
|
|
17
21
|
# The plugin needs Samagotchi::Plugin::Peer on the mailbox: chi.peer and
|
|
18
22
|
# ctx.peers, and the skill's chi peer pair|say, are the next chi's (an older
|
|
19
23
|
# chi has no chi.peer: the plugin fails to load there).
|
|
20
|
-
requires_chi: ">= 0.
|
|
24
|
+
requires_chi: ">= 0.195.0"
|
|
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.
|
|
@@ -42,8 +42,17 @@ require "samagotchi/plugin/peer"
|
|
|
42
42
|
# scripts/pair path, the payload folder and chi's mailbox folder written
|
|
43
43
|
# in. The card shows the writable_roots line Codex's config.toml needs for
|
|
44
44
|
# pairing and replies (its default sandbox can't write chi's state); chi
|
|
45
|
-
# never edits Codex's config. Installing makes the mailbox folder, so
|
|
46
|
-
# root exists when Codex next starts.
|
|
45
|
+
# never edits Codex's config.toml. Installing makes the mailbox folder, so
|
|
46
|
+
# the root exists when Codex next starts. Its "Add the start hook…"
|
|
47
|
+
# (`/codex install-hook`: the merged hooks.json as a diff; its card's Add,
|
|
48
|
+
# `--yes`, only on the user's own tap) appends the SessionStart hook that
|
|
49
|
+
# runs `chi peer announce codex` to $CODEX_HOME/hooks.json, never its
|
|
50
|
+
# trust: Codex reviews a new or changed hook itself at its next start
|
|
51
|
+
# (chi never adds a trust flag), and skips it until then. With it, a new
|
|
52
|
+
# interactive Codex in the project pairs with a waiting session (/codex
|
|
53
|
+
# pair, /codex start) at its first message; a `codex exec` or a resume
|
|
54
|
+
# never does, and a subagent should not (untested; CodexRollout fails
|
|
55
|
+
# closed).
|
|
47
56
|
#
|
|
48
57
|
# In the web, selecting text offers "Confirm with Codex" (chi.annotation):
|
|
49
58
|
# it fills the composer with
|
|
@@ -75,9 +84,12 @@ class Plugin < Samagotchi::Plugin::Peer
|
|
|
75
84
|
THREAD_ID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
|
|
76
85
|
STATUS_CARD = "codex"
|
|
77
86
|
SKILL_CARD = "codex-skill"
|
|
78
|
-
USAGE = "usage: /codex — whether a Codex session is paired; /codex
|
|
79
|
-
"
|
|
80
|
-
"chi
|
|
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 window, its first " \
|
|
89
|
+
"prompt \"pair with chi\" (one Codex turn); /codex unpair — end the pairing or the wait; /codex validate " \
|
|
90
|
+
"<text> | context <text> — send the text to it; /codex last — send the last answer as context; /codex " \
|
|
91
|
+
"install-skill — install Codex's chi-peer skill, which pairs; /codex install-hook [--yes] — Codex's start " \
|
|
92
|
+
"hook, which pairs a new Codex with a waiting session at its first message"
|
|
81
93
|
# The shipped bundle keeps scripts/ beside plugin.rb; an installed one
|
|
82
94
|
# has plugin/plugin.rb and scripts/ beside plugin/.
|
|
83
95
|
SCRIPTS_DIRS = [File.join(__dir__, "scripts"), File.expand_path("../scripts", __dir__)].freeze
|
|
@@ -102,8 +114,51 @@ class Plugin < Samagotchi::Plugin::Peer
|
|
|
102
114
|
"functions or diff range to look at. A long text goes to a file Codex reads" }
|
|
103
115
|
}.freeze
|
|
104
116
|
|
|
117
|
+
# /codex start's first prompt: Codex has no thread until a message, and
|
|
118
|
+
# this one runs the skill's no-id pair, which takes the wait.
|
|
119
|
+
def start_prompt = "pair with chi"
|
|
120
|
+
|
|
121
|
+
# Its start hook (Codex's SessionStart: chi peer announce codex), added to
|
|
122
|
+
# hooks.json from the skill card's "Add the start hook".
|
|
123
|
+
def start_hook? = true
|
|
124
|
+
|
|
125
|
+
# /codex start's first prompt pairs with or without the hook.
|
|
126
|
+
def start_needs_hook? = false
|
|
127
|
+
|
|
105
128
|
private
|
|
106
129
|
|
|
130
|
+
# Codex's user hooks file (Codex reads $CODEX_HOME/hooks.json; inline
|
|
131
|
+
# [hooks] in config.toml and a project's .codex/hooks.json aren't read
|
|
132
|
+
# here).
|
|
133
|
+
def hook_file = File.join(peer_dir, "hooks.json")
|
|
134
|
+
|
|
135
|
+
# No sh test: Codex gives the hook no env to tell a headless run by (it
|
|
136
|
+
# runs under Codex's app-server daemon), so chi boots and the announce's
|
|
137
|
+
# gate (startup, the rollout) says no. `2>/dev/null || true`: a failing
|
|
138
|
+
# hook shows the user an error, and under CHI_REMOTE (the daemon's env)
|
|
139
|
+
# bin/chi exits 2 before chi peer runs.
|
|
140
|
+
def start_hook_line = "#{hook_chi} peer announce #{PEER} 2>/dev/null || true"
|
|
141
|
+
|
|
142
|
+
def hook_paragraph
|
|
143
|
+
"With the start hook, a new Codex in this project pairs with a session that waits (`/codex pair`, `/codex " \
|
|
144
|
+
"start`) at its first message, whatever it says: no \"pair with chi\" turn. A `codex exec` or a resume never " \
|
|
145
|
+
"takes the wait, and an open Codex still pairs with \"pair with chi\". It adds one `#{EVENT}` " \
|
|
146
|
+
"hook to `#{hook_file}` (the one file it writes; never config.toml), every other hook kept in its place:" \
|
|
147
|
+
"\n\n```sh\n#{start_hook_line}\n```\n\nCodex reviews it before it runs: at its next start choose Review " \
|
|
148
|
+
"hooks (or `/hooks`) and trust it; until then Codex skips it. To remove it, delete that #{EVENT} entry from " \
|
|
149
|
+
"the file."
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def hook_written_line
|
|
153
|
+
"Added to `#{hook_file}`. Codex asks you to review it the next time it starts: choose Review hooks (or `/hooks`) " \
|
|
154
|
+
"and trust it; until then Codex skips it. A Codex started with `/codex start` stops on that screen in its " \
|
|
155
|
+
"kitty window the first time: Show it."
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def hook_stale_tail = ", then trust it in Codex (it is a changed hook)"
|
|
159
|
+
|
|
160
|
+
def start_review_hint = "if Codex shows Hooks need review, Show its window and answer it"
|
|
161
|
+
|
|
107
162
|
# The paired Codex's thread id: the pairing's address.
|
|
108
163
|
def check_hint!(hint)
|
|
109
164
|
raise SendError, "the pairing's address isn't a Codex thread id (#{hint}): pair again from Codex" unless hint.match?(THREAD_ID)
|
|
@@ -122,7 +177,8 @@ class Plugin < Samagotchi::Plugin::Peer
|
|
|
122
177
|
def setup_paragraph(ctx)
|
|
123
178
|
"Codex's default sandbox can't write chi's state, and pairing and replies do: add to " \
|
|
124
179
|
"`#{File.join(peer_dir, "config.toml")}` (into its `[sandbox_workspace_write]` section if it has one), " \
|
|
125
|
-
"then restart Codex:\n\n```toml\n#{roots_line(ctx)}\n```\n\nchi
|
|
180
|
+
"then restart Codex:\n\n```toml\n#{roots_line(ctx)}\n```\n\nchi never changes Codex's settings without " \
|
|
181
|
+
"your tap; Codex reviews the start hook itself."
|
|
126
182
|
end
|
|
127
183
|
|
|
128
184
|
def installed_line(short) = "Restart Codex if it is running, then say \"pair with chi #{short}\"."
|
|
@@ -59,8 +59,29 @@ nothing. Don't edit the config yourself; ask the user.
|
|
|
59
59
|
|
|
60
60
|
## Pair (once per chi session, again in a new Codex session)
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
If your start context says you are paired with chi (`paired with chi <id8>
|
|
63
|
+
(…)`: chi's start hook paired this session at its first message), you are:
|
|
64
|
+
answer that chi session's id, and don't pair again unless the user asks
|
|
65
|
+
(the bare pair below then says paired, unless another chi session waits for
|
|
66
|
+
a Codex: then it pairs that one too).
|
|
67
|
+
|
|
68
|
+
When the user says "pair with chi" with no id, run it bare:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
sh "__CHI_PEER_PAIR__"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
It pairs with the chi session that waits for a Codex in this project (the
|
|
75
|
+
user typed `/codex pair` there), else with the one chi session of this
|
|
76
|
+
project that runs now. When it lists several instead (exit 1), show the list
|
|
77
|
+
to the user and ask which one; then pair with that id. When that chi session is
|
|
78
|
+
paired with another Codex already, it refuses (exit 1) and replaces nothing:
|
|
79
|
+
tell the user to run `/codex unpair` there first (pairing with its id replaces
|
|
80
|
+
that pairing; do that only when the user says so).
|
|
81
|
+
|
|
82
|
+
With an id: the chi session id is the `<id>` in
|
|
83
|
+
`http://127.0.0.1:4567/#/s/<id>` (a unique prefix works; the URL works too).
|
|
84
|
+
Run:
|
|
64
85
|
|
|
65
86
|
```sh
|
|
66
87
|
sh "__CHI_PEER_PAIR__" <chi session id or URL>
|
|
@@ -2,10 +2,16 @@
|
|
|
2
2
|
# pair: pair this Codex CLI session with a chi session (chi's codex-peer
|
|
3
3
|
# bundle). Run from Codex, which sets CODEX_THREAD_ID:
|
|
4
4
|
#
|
|
5
|
-
# sh <bundle dir>/scripts/pair <chi session id, prefix or chi web URL>
|
|
5
|
+
# sh <bundle dir>/scripts/pair [<chi session id, prefix or chi web URL>]
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
# this
|
|
7
|
+
# With no id (`chi peer pair codex --here`): the chi session that waits for
|
|
8
|
+
# a Codex in this folder's project (/codex pair), else its one live
|
|
9
|
+
# session; else chi lists them and exits 1. With no session waiting, a
|
|
10
|
+
# live one codex-peer's start hook paired with this thread wins: it says
|
|
11
|
+
# paired.
|
|
12
|
+
#
|
|
13
|
+
# It pairs (`chi peer pair codex <id> --address <thread> --peer-session
|
|
14
|
+
# <thread>`): the address and the peer session are this session's thread id (chi's codex_send tool runs
|
|
9
15
|
# `codex queue --thread <id>` with it); Codex's answers go to the session's
|
|
10
16
|
# mailbox (`chi peer say codex <id>`). Again later, it only refreshes the
|
|
11
17
|
# thread id. CHI names the chi command (default: chi on the PATH). Through a
|
|
@@ -21,13 +27,15 @@
|
|
|
21
27
|
set -eu
|
|
22
28
|
|
|
23
29
|
usage() {
|
|
24
|
-
echo "usage: pair <chi session id or URL (http://127.0.0.1:4567/#/s/<id>)>" >&2
|
|
30
|
+
echo "usage: pair [<chi session id or URL (http://127.0.0.1:4567/#/s/<id>)>]" >&2
|
|
25
31
|
exit 2
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
[ $# -
|
|
29
|
-
|
|
34
|
+
[ $# -le 1 ] || usage
|
|
35
|
+
id=
|
|
36
|
+
case "${1:-}" in
|
|
30
37
|
-h | --help) usage ;;
|
|
38
|
+
"") ;;
|
|
31
39
|
http://* | https://*)
|
|
32
40
|
id=$(printf '%s\n' "$1" | sed -n 's|.*/s/\([0-9A-Za-z-][0-9A-Za-z-]*\).*|\1|p')
|
|
33
41
|
[ -n "$id" ] || { echo "pair: no session id in $1 (a chi web URL ends in #/s/<id>)" >&2; exit 2; }
|
|
@@ -100,6 +108,19 @@ pair() {
|
|
|
100
108
|
fi
|
|
101
109
|
}
|
|
102
110
|
|
|
111
|
+
# No id: chi finds the session here (--here), on this machine only.
|
|
112
|
+
if [ -z "$id" ]; then
|
|
113
|
+
if [ -n "$via" ]; then
|
|
114
|
+
echo "pair: chi is on $remote: pair with an id over a remote (its chi web's URL, or the id from chi sessions list there)" >&2
|
|
115
|
+
exit 2
|
|
116
|
+
fi
|
|
117
|
+
if ! out=$($chi peer pair codex --here --address "$thread" --peer-session "$thread" --from "Codex in $PWD" 2>&1); then
|
|
118
|
+
fail "$out"
|
|
119
|
+
fi
|
|
120
|
+
echo "paired with chi ${out%% *} (thread $thread)"
|
|
121
|
+
exit 0
|
|
122
|
+
fi
|
|
123
|
+
|
|
103
124
|
if [ -n "$via" ]; then
|
|
104
125
|
pair --from "Codex in $PWD (on another machine)"
|
|
105
126
|
ask=$chi
|
|
@@ -108,5 +129,5 @@ if [ -n "$via" ]; then
|
|
|
108
129
|
exit 0
|
|
109
130
|
fi
|
|
110
131
|
|
|
111
|
-
pair --address "$thread" --from "Codex in $PWD"
|
|
132
|
+
pair --address "$thread" --peer-session "$thread" --from "Codex in $PWD"
|
|
112
133
|
echo "paired with chi $id (thread $thread)"
|
|
@@ -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
|