samagotchi 0.193.0 → 0.194.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.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -1
  3. data/docs/bundle-authoring.md +1 -1
  4. data/docs/bundles.md +37 -12
  5. data/docs/cli.md +26 -10
  6. data/docs/configuration.md +71 -5
  7. data/docs/desktop.md +3 -0
  8. data/docs/guardrails.md +11 -1
  9. data/docs/hooks.md +1 -0
  10. data/docs/internals/engine.md +46 -3
  11. data/docs/internals/panes.md +66 -0
  12. data/docs/internals/peers.md +101 -5
  13. data/docs/internals/prompt-caching.md +4 -0
  14. data/docs/internals/session-lifecycle.md +9 -2
  15. data/docs/internals/web-frontend.md +16 -4
  16. data/docs/peers.md +79 -5
  17. data/docs/plugins.md +42 -4
  18. data/docs/sessions.md +15 -6
  19. data/docs/sub-agent.md +6 -4
  20. data/docs/system-one.md +4 -0
  21. data/docs/testing.md +3 -0
  22. data/lib/samagotchi/bridge/card_store.rb +1 -1
  23. data/lib/samagotchi/broadcast/triage_model.rb +17 -0
  24. data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
  25. data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
  26. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +28 -7
  27. data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
  28. data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
  29. data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
  30. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +23 -2
  31. data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
  32. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  33. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  34. data/lib/samagotchi/chi_invocation.rb +7 -0
  35. data/lib/samagotchi/client.rb +18 -0
  36. data/lib/samagotchi/codex_rollout.rb +93 -0
  37. data/lib/samagotchi/config.rb +15 -6
  38. data/lib/samagotchi/effective_model.rb +28 -2
  39. data/lib/samagotchi/engine/cards.rb +9 -4
  40. data/lib/samagotchi/engine/hooks.rb +8 -0
  41. data/lib/samagotchi/engine/models.rb +180 -7
  42. data/lib/samagotchi/engine/side_requests.rb +72 -13
  43. data/lib/samagotchi/engine/toolkit.rb +1 -1
  44. data/lib/samagotchi/engine/turn_helpers.rb +1 -1
  45. data/lib/samagotchi/engine.rb +11 -8
  46. data/lib/samagotchi/engine_snapshots.rb +16 -3
  47. data/lib/samagotchi/events.rb +1 -0
  48. data/lib/samagotchi/hooks.rb +3 -1
  49. data/lib/samagotchi/host_registry.rb +146 -16
  50. data/lib/samagotchi/idle_client.rb +13 -2
  51. data/lib/samagotchi/idle_recap.rb +33 -3
  52. data/lib/samagotchi/idle_target.rb +27 -7
  53. data/lib/samagotchi/llm/adapter_common.rb +4 -2
  54. data/lib/samagotchi/llm/anthropic_messages.rb +3 -2
  55. data/lib/samagotchi/llm/errors.rb +35 -4
  56. data/lib/samagotchi/llm/model_listing.rb +19 -0
  57. data/lib/samagotchi/llm/openai_chat.rb +3 -2
  58. data/lib/samagotchi/model_binding.rb +46 -4
  59. data/lib/samagotchi/model_catalog.rb +68 -8
  60. data/lib/samagotchi/model_profile.rb +35 -0
  61. data/lib/samagotchi/models_command.rb +8 -4
  62. data/lib/samagotchi/panes/kitty.rb +122 -0
  63. data/lib/samagotchi/panes.rb +185 -0
  64. data/lib/samagotchi/peer_announce.rb +245 -0
  65. data/lib/samagotchi/peer_command.rb +97 -11
  66. data/lib/samagotchi/peer_continue.rb +10 -0
  67. data/lib/samagotchi/peer_mailbox.rb +30 -7
  68. data/lib/samagotchi/peer_pair_here.rb +149 -0
  69. data/lib/samagotchi/peer_presence.rb +434 -0
  70. data/lib/samagotchi/peer_say.rb +17 -10
  71. data/lib/samagotchi/peer_wait_expiry.rb +47 -0
  72. data/lib/samagotchi/plugin/api.rb +12 -1
  73. data/lib/samagotchi/plugin/card.rb +68 -11
  74. data/lib/samagotchi/plugin/context.rb +20 -1
  75. data/lib/samagotchi/plugin/panes.rb +54 -0
  76. data/lib/samagotchi/plugin/peer.rb +52 -10
  77. data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
  78. data/lib/samagotchi/plugin/peer_start.rb +301 -0
  79. data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
  80. data/lib/samagotchi/plugin/peers.rb +76 -3
  81. data/lib/samagotchi/recap_setup.rb +40 -3
  82. data/lib/samagotchi/self_report.rb +25 -6
  83. data/lib/samagotchi/send_command.rb +6 -6
  84. data/lib/samagotchi/served_lookup.rb +225 -0
  85. data/lib/samagotchi/served_ref.rb +41 -0
  86. data/lib/samagotchi/session_access/contract.rb +6 -0
  87. data/lib/samagotchi/session_access/local.rb +54 -6
  88. data/lib/samagotchi/session_access/remote.rb +4 -4
  89. data/lib/samagotchi/session_access/remote_peers.rb +7 -4
  90. data/lib/samagotchi/session_commands.rb +40 -8
  91. data/lib/samagotchi/session_continue.rb +6 -2
  92. data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
  93. data/lib/samagotchi/session_manager/delivery.rb +5 -0
  94. data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
  95. data/lib/samagotchi/session_manager/removal.rb +3 -0
  96. data/lib/samagotchi/session_model_settings.rb +7 -3
  97. data/lib/samagotchi/session_stores.rb +9 -0
  98. data/lib/samagotchi/sessions_command.rb +16 -3
  99. data/lib/samagotchi/system_one/http_backend.rb +111 -10
  100. data/lib/samagotchi/system_prompt.rb +11 -4
  101. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
  102. data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
  103. data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
  104. data/lib/samagotchi/terminal_ui.rb +1 -1
  105. data/lib/samagotchi/testing/bundle.rb +6 -2
  106. data/lib/samagotchi/turn_endings.rb +6 -2
  107. data/lib/samagotchi/user_typed.rb +29 -0
  108. data/lib/samagotchi/version.rb +1 -1
  109. data/lib/samagotchi/web/app.rb +8 -20
  110. data/lib/samagotchi/web/participant_review.rb +1 -1
  111. data/lib/samagotchi/web/participant_view.rb +1 -1
  112. data/lib/samagotchi/web/peer_routes.rb +33 -13
  113. data/lib/samagotchi/web/public/app.js +1 -1
  114. data/lib/samagotchi/web/public/card.js +26 -10
  115. data/lib/samagotchi/web/public/context_chips.js +24 -2
  116. data/lib/samagotchi/web/public/copy.js +8 -2
  117. data/lib/samagotchi/web/public/data.js +2 -1
  118. data/lib/samagotchi/web/public/format.js +9 -1
  119. data/lib/samagotchi/web/public/index.html +18 -0
  120. data/lib/samagotchi/web/public/info_bar_model.js +1 -1
  121. data/lib/samagotchi/web/public/model_pick.js +18 -5
  122. data/lib/samagotchi/web/public/model_picker.js +1 -1
  123. data/lib/samagotchi/web/public/peer_chip.js +15 -5
  124. data/lib/samagotchi/web/public/peer_data.js +4 -2
  125. data/lib/samagotchi/web/public/peer_wait.js +36 -0
  126. data/lib/samagotchi/web/public/served_ref.js +9 -0
  127. data/lib/samagotchi/web/public/session_fields.js +1 -0
  128. data/lib/samagotchi/web/public/session_state.js +4 -0
  129. data/lib/samagotchi/web/public/stage_missing.js +23 -6
  130. data/lib/samagotchi/web/public/stream_handlers.js +16 -0
  131. data/lib/samagotchi/web/public/version_notices.js +7 -1
  132. data/lib/samagotchi/web/session_control.rb +19 -10
  133. data/lib/samagotchi/web/session_show.rb +20 -0
  134. data/lib/samagotchi/web/update_ops.rb +2 -2
  135. data/lib/samagotchi/worker/command_queue.rb +3 -0
  136. data/lib/samagotchi/worker/mail.rb +14 -1
  137. data/lib/samagotchi/worker.rb +1 -0
  138. metadata +18 -1
data/docs/system-one.md CHANGED
@@ -37,6 +37,10 @@ same `Client` and `Recording`: the API is in [plugins.md](plugins.md#judgments-c
37
37
  Only bundles' judgments have settings of their own (`JudgmentSettings`). Core's take no host, model or mode here, whatever
38
38
  `system_one.judgments` says: `Record` and `Similar` are model-blind (`Record#initialize` filters by rule, and by repository for a rule in `guardrails.system_one.repo_rules`, never by model), so a model swapped
39
39
  for approvals alone would inherit a clean record and could auto-allow. Before any such swap, `Record` and `Similar` have to go by model.
40
+ `system_one.model: served` changes the judge whenever the host loads another model, under the same model-blind record (the owner's
41
+ choice: the speed of a hand edit, no new kind of risk); each saved run's `model` is the id that answered (after a not-served retry, the
42
+ retried one), and the change logs `system_one_model_changed`. A run whose listing failed or was refused (nothing was asked)
43
+ records `served`, the configured value, with the failure.
40
44
 
41
45
  Core's uses keep their own threads (`ToolkitSuggest::Pending`, `ContextJudge`, the approval path) and contention; only the ask-and-save
42
46
  seam is common. A bundle's `judge_later` runs on the Engine's `SideJudgments` under its full name as the kind, so `SideJudgments#join`
data/docs/testing.md CHANGED
@@ -54,6 +54,9 @@ adapters load, so a run from `git rebase --exec`, `bisect run` or a hook can't r
54
54
  chose spawn with `GitEnv::UNSET`, in any run.
55
55
  `SAMAGOTCHI_TAILSCALE_CLI` points at a file that does not exist (`spec_helper.rb`), so no spec reaches a real `tailscale`; a
56
56
  `chi web --tunnel tailscale` spec writes a fake one and sets the variable.
57
+ No spec reaches a running kitty either (Panes opens tabs and pastes into windows): the spec config has no
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`).
57
60
 
58
61
  Goldens (`*_golden_spec.rb`, the prompt snapshots) hold no machine state, so they pass on any runner: no absolute
59
62
  paths (replace them with placeholders), no PATH tools (the suite pins `rg` as found, `spec_helper.rb`; an example
@@ -345,7 +345,7 @@ module Samagotchi
345
345
  def anchor = @running_turn || @turn_log.last
346
346
 
347
347
  def add_card(event)
348
- card = event.slice(:type, :id, :source, :title, :body, :level, :actions, :command)
348
+ card = event.slice(:type, :id, :source, :title, :body, :level, :actions, :rows, :command)
349
349
  index = @entries.index { |entry| entry[:type] == :card && entry[:id] == card[:id] }
350
350
  if index
351
351
  # Replaced where it was: the first one's place stays, and whose it is
@@ -31,6 +31,9 @@ module Samagotchi
31
31
  def resolve(host_registry: nil, get: ->(key) { Config.get(key) })
32
32
  registry = host_registry || HostRegistry.current
33
33
  SETTINGS.each do |model, host_ref, base_url|
34
+ refusal = get.call(base_url).to_s.strip.empty? && list_served(registry, get.call(model), get.call(host_ref))
35
+ return Choice.new(target: nil, setting: model, problem: "#{model}: #{refusal}") if refusal
36
+
34
37
  target = IdleTarget.resolve(model: get.call(model), host_ref: get.call(host_ref), base_url: get.call(base_url),
35
38
  host_registry: registry)
36
39
  return Choice.new(target: target, setting: model, problem: nil) if target
@@ -38,11 +41,25 @@ module Samagotchi
38
41
  return Choice.new(target: nil, setting: model, problem: problem(e, model, host_ref))
39
42
  end
40
43
  model = ModelProfile.required_model_name(get.call("default.model"))
44
+ refusal = list_served(registry, model, nil)
45
+ return Choice.new(target: nil, setting: "default.model", problem: "default.model: #{refusal}") if refusal
46
+
41
47
  Choice.new(target: IdleTarget.of_model(registry, model), setting: "default.model", problem: nil)
42
48
  rescue StandardError => e
43
49
  Choice.new(target: nil, setting: "default.model", problem: e.message.to_s.lines.first.to_s.strip[0, 200])
44
50
  end
45
51
 
52
+ # A served +model+ (main:served, a bare served on +host_ref+'s host)
53
+ # listed now: this CLI process knows nothing of what the host serves.
54
+ # One bounded listing, no retry. @return [String, nil] the refusal
55
+ # when the host lists none or several models
56
+ def list_served(registry, model, host_ref)
57
+ return nil if model.to_s.strip.empty?
58
+
59
+ registry.refresh_served!(model.to_s.strip, max_age: 0, host: host_ref.to_s.strip.empty? ? nil : host_ref.to_s.strip)&.refusal
60
+ end
61
+ private_class_method :list_served
62
+
46
63
  def problem(error, model_key, host_ref_key)
47
64
  case error.reason
48
65
  when :host_ref_unknown then "#{host_ref_key} '#{error.host_ref}' is not in hosts:"
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: claude-peer
3
- version: 0.8.0
3
+ version: 0.9.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,12 +10,15 @@ 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:a7fa566ff03f6e63d2889196539acbc2fa0c3615aa3a75c1282c91d062bc5e6f
13
+ sha256: sha256:14fe3e36e84dd25172fd3fb0dba54a4aa94d4b8cdb4101573431927e84551657
14
14
  scripts:
15
15
  pair-remote: sha256:d7681f87a194e540b629564f84cad9f2d0a763197502c74614276ef88a8920cc
16
- pair: sha256:98cddde54801f49b123cd0269f9ddc834b2566fde4ad241c07a50f3b794fedd0
17
- chi-peer-SKILL.md: sha256:c5e33144978b950651d71ce872b5a6916d7314954b7b01bc79936fd1948f304d
16
+ pair: sha256:626b1c4acba6b1ab8a9d9615c709d69da6953d89d32469f370c2eaad145a8bb8
17
+ chi-peer-SKILL.md: sha256:e3d5bd01d221a3206e203098bf7e1e1fbbbf72b2124bae744eace1816d8eca0c
18
+ # config.yml bundles: <name>: start_command: the shell line /<peer> start runs in its kitty tab (another key warns)
19
+ settings: [start_command]
18
20
  # The plugin needs Samagotchi::Plugin::Peer on the mailbox: chi.peer and
19
21
  # ctx.peers, and the skill's chi peer pair|say, are the next chi's (an older
20
22
  # chi has no chi.peer: the plugin fails to load there).
21
- requires_chi: ">= 0.188.0"
23
+ requires_chi: ">= 0.194.0"
24
+ # The pair script's no-id form (chi peer pair --here), --peer-session, /<peer> pair and the start hook are the next chi's.
@@ -38,7 +38,11 @@ require "samagotchi/plugin/peer"
38
38
  # keep what was completed); `/claude install-skill` shows a card with
39
39
  # Claude's chi-peer skill's target and an Install action (`--yes`), the card
40
40
  # being the user's consent: the copy happens in-process, where no guardrail
41
- # sees it. The skill is scripts/chi-peer-SKILL.md beside this file, with the
41
+ # sees it; and "Add the start hook…" (`/claude install-hook`: the merged
42
+ # settings as a diff), whose card's Add (`--yes`, only on the user's own
43
+ # tap) merges the SessionStart hook that runs `chi
44
+ # peer announce claude` into Claude Code's settings.json. `/claude pair`
45
+ # pairs with a Claude Code session here, no id copied (Plugin::PeerPairing). The skill is scripts/chi-peer-SKILL.md beside this file, with the
42
46
  # installed scripts/pair path and the payload folder written in.
43
47
  #
44
48
  # In the web, selecting text offers "Confirm with Claude" (chi.annotation):
@@ -69,9 +73,12 @@ class Plugin < Samagotchi::Plugin::Peer
69
73
  CHECKED_ON = "Claude Code 2.1.296"
70
74
  STATUS_CARD = "claude"
71
75
  SKILL_CARD = "claude-skill"
72
- USAGE = "usage: /claude — whether a Claude Code session is paired; /claude validate <text> | context <text> — send " \
73
- "the text to it; /claude last — send the last answer as context; /claude install-skill — install Claude's " \
74
- "chi-peer skill, which pairs"
76
+ USAGE = "usage: /claude — whether a Claude Code session is paired (else the ones here); /claude pair — pair the one " \
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 tab, paired when it starts; /claude show — bring " \
79
+ "that tab to the front; /claude unpair — end the pairing or the wait; /claude validate <text> | context <text> — " \
80
+ "send the text to it; /claude last — send the last answer as context; /claude install-skill — install Claude's " \
81
+ "chi-peer skill, which pairs; /claude install-hook [--yes] — Claude Code's start hook, which lists a Claude here"
75
82
  # The shipped bundle keeps scripts/ beside plugin.rb; an installed one
76
83
  # has plugin/plugin.rb and scripts/ beside plugin/.
77
84
  SCRIPTS_DIRS = [File.join(__dir__, "scripts"), File.expand_path("../scripts", __dir__)].freeze
@@ -95,6 +102,18 @@ class Plugin < Samagotchi::Plugin::Peer
95
102
  "functions or diff range to look at. A long text goes to a file Claude reads" }
96
103
  }.freeze
97
104
 
105
+ # Claude lists itself here (its start hook, chi peer announce): /claude
106
+ # pair finds the sessions open in this project.
107
+ def announces? = true
108
+
109
+ # Its inbox socket missing: that Claude ended (a resume pairs again with a
110
+ # new one). No address (answers only) isn't gone.
111
+ def link_gone?(link) = link.socket_gone?
112
+
113
+ # Its start hook (Claude Code's SessionStart: chi peer announce claude),
114
+ # added to settings.json from the skill card's "Add the start hook".
115
+ def start_hook? = true
116
+
98
117
  # The one JSON line Claude Code's inbox socket takes.
99
118
  def line(content) = "#{JSON.generate(type: "user", message: { role: "user", content: content })}\n"
100
119
 
@@ -118,7 +137,7 @@ class Plugin < Samagotchi::Plugin::Peer
118
137
  def setup_paragraph(ctx)
119
138
  rule = "Read(#{tilde(ctx.data_dir)}/**)"
120
139
  "So Claude isn't asked before reading a long part's file, add `#{rule}` to Claude Code's permissions " \
121
- "(`permissions.allow` in its settings.json); chi doesn't change Claude's settings."
140
+ "(`permissions.allow` in its settings.json); chi never changes Claude's settings without your tap."
122
141
  end
123
142
 
124
143
  # Claude Code's config folder: CLAUDE_CONFIG_DIR, else ~/.claude.
@@ -19,19 +19,40 @@ headless review run.
19
19
  <!-- Installed by chi's claude-peer bundle (/claude install-skill); a newer
20
20
  bundle installs a newer copy. -->
21
21
 
22
- ## Pair (once per chi session, again after this session restarts)
22
+ ## Pair (once per chi session)
23
23
 
24
- The chi session id is the `<id>` in `http://127.0.0.1:4567/#/s/<id>` (a unique
25
- prefix works; the URL works too). Run:
24
+ When the user says "pair with chi" with no id, run it bare:
25
+
26
+ ```sh
27
+ sh "__CHI_PEER_PAIR__"
28
+ ```
29
+
30
+ It pairs with the chi session that waits for a Claude in this project (the
31
+ user typed `/claude pair` there), else with the one chi session of this
32
+ project that runs now. When it lists several instead (exit 1), show the list
33
+ to the user and ask which one; then pair with that id. When that chi session is
34
+ paired with another Claude that is still open, it refuses (exit 1) and replaces
35
+ nothing: tell the user to run `/claude unpair` there first (pairing with its id
36
+ replaces that pairing; do that only when the user says so). One paired with a
37
+ Claude that has ended is replaced, and the pair says so.
38
+
39
+ With an id: the chi session id is the `<id>` in
40
+ `http://127.0.0.1:4567/#/s/<id>` (a unique prefix works; the URL works too):
26
41
 
27
42
  ```sh
28
43
  sh "__CHI_PEER_PAIR__" <chi session id or URL>
29
44
  ```
30
45
 
31
- It pairs this session with the chi session (`chi peer pair claude <id>`, with
32
- this session's inbox socket, `$CLAUDE_CODE_MESSAGING_SOCKET`, as the address
33
- chi sends to). Running it again only refreshes the socket. The socket changes
34
- when this Claude session restarts: pair again then.
46
+ It pairs this session with the chi session (`chi peer pair claude`, with this
47
+ session's inbox socket, `$CLAUDE_CODE_MESSAGING_SOCKET`, as the address chi
48
+ sends to, and this session's id). Running it again only refreshes the socket.
49
+
50
+ The socket changes when this Claude session restarts (`claude --resume`).
51
+ With chi's start hook installed (`/claude install-skill` in chi, "Add the
52
+ start hook") the pairing follows by itself: the start context says `chi <id8>:
53
+ pairing refreshed`. Without it, pair again after a restart. A start context
54
+ line `paired with chi <id8> (<title>)` means the hook paired this session with
55
+ a chi session that waited: there is nothing to run.
35
56
 
36
57
  Tell the user it's paired, and that in chi they can say "send this to Claude
37
58
  for validation" or "give Claude this as context", or type
@@ -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>`). Again
10
- # after a Claude restart, it only refreshes the socket. CHI names the chi
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
- [ -n "$target" ] || usage
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.7.0
3
+ version: 0.8.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,14 @@ 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:2a1998dac902d5eb5987ef0d90121688a08acc74d640292fe0893ac95e69ca37
13
+ sha256: sha256:c656ff80192292bf9db1321dfd7aa8711687ea4e6c8a83c9251357f86a6076bb
14
14
  scripts:
15
- pair: sha256:ce80efa7ce0b8c1cd2ae430ea9a9a7a44119905f598e985dcefb325df4f2ed84
16
- chi-peer-SKILL.md: sha256:b24ec6ee3b5b3de6a69fa4b347dbc3593bba03a7f3602f9120c00d2b2e9b1024
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 tab (another key warns)
18
+ settings: [start_command]
17
19
  # The plugin needs Samagotchi::Plugin::Peer on the mailbox: chi.peer and
18
20
  # ctx.peers, and the skill's chi peer pair|say, are the next chi's (an older
19
21
  # chi has no chi.peer: the plugin fails to load there).
20
- requires_chi: ">= 0.188.0"
22
+ requires_chi: ">= 0.194.0"
23
+ # The pair script's no-id form (chi peer pair --here), --peer-session, /<peer> pair and the start hook are 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 the
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 validate <text> | context <text> — send the " \
79
- "text to it; /codex last — send the last answer as context; /codex install-skill — install Codex's " \
80
- "chi-peer skill, which pairs"
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 tab, 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
+ "background tab 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 tab 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 doesn't change Codex's settings."
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
- The chi session id is the `<id>` in `http://127.0.0.1:4567/#/s/<id>` (a unique
63
- prefix works; the URL works too). Run:
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
- # It pairs (`chi peer pair codex <id> --address <thread>`): the address is
8
- # this session's thread id (chi's codex_send tool runs
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
- [ $# -eq 1 ] || usage
29
- case "$1" in
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,10 +1,10 @@
1
1
  ---
2
2
  name: dev
3
- version: 0.7.11
3
+ version: 0.7.12
4
4
  scope: system
5
5
  description: "Developer tools"
6
6
  trust_level: reviewed
7
- requires_chi: ">= 0.188.0"
7
+ requires_chi: ">= 0.194.0"
8
8
  includes:
9
9
  - known-names
10
10
  - mcp
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: samagotchi-system
3
- version: 0.193.0
3
+ version: 0.194.0
4
4
  scope: system
5
5
  description: Default system memories — identity, self map, config modification protocol, memory guide and the delegated-session rules
6
6
  files:
@@ -22,6 +22,13 @@ module Samagotchi
22
22
  # chi --resume ID (chi --remote NAME --resume ID).
23
23
  def resume(id) = command("--resume #{id}")
24
24
 
25
+ # How a stopped session starts again, after the caller's lead-in
26
+ # ("start it yourself: …", "ask your user to start it: …"): its resume,
27
+ # or a message from a terminal or the web. Only its owner's does: an
28
+ # agent's, a peer's or a schedule's never un-stops it
29
+ # (SessionInbox::WakePolicy).
30
+ def start_stopped(id) = "`#{resume(id)}`, or a message from a terminal or #{remote? ? web : "the web"}"
31
+
25
32
  # What shows the session ("… shows it"): its attach.
26
33
  def shows(id) = attach(id)
27
34
 
@@ -344,6 +344,24 @@ module Samagotchi
344
344
  raise "#{LABEL} model listing failed (#{@host}:#{@port}): #{e.message}"
345
345
  end
346
346
 
347
+ # #list_models as one bounded GET: the /props probe's short timeouts, no
348
+ # retry (ServedLookup's turn-start listing: a host that is down fails
349
+ # fast, and the turn's own request says so).
350
+ def list_models_probe
351
+ uri = URI("#{@scheme}://#{@host}:#{@port}#{MODELS_PATH}")
352
+ response = @http.fetch(uri, Net::HTTP::Get.new(uri), retries: false, log_fields: { purpose: "models" },
353
+ open_timeout: CONTEXT_WINDOW_PROBE_OPEN_TIMEOUT,
354
+ read_timeout: CONTEXT_WINDOW_PROBE_READ_TIMEOUT,
355
+ cancel_controller: Client.probe_cancel)
356
+ clear_props_failures!
357
+ parsed = JSON.parse(response.body.to_s)
358
+ parsed.fetch("data", parsed)
359
+ rescue LLM::ProviderError, LLM::RequestCancelled
360
+ raise
361
+ rescue StandardError => e
362
+ raise "#{LABEL} model listing failed (#{@host}:#{@port}): #{e.message}"
363
+ end
364
+
347
365
  # What the running server says about itself (/props), as a ServerProps.
348
366
  # One GET with short
349
367
  # timeouts and no retry: it runs before generation and must never hold up