samagotchi 0.192.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +128 -1
- data/docs/architecture.md +3 -3
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +82 -40
- data/docs/cli.md +47 -17
- data/docs/configuration.md +75 -8
- data/docs/context.md +8 -5
- data/docs/desktop.md +3 -0
- data/docs/guardrails.md +20 -5
- data/docs/hooks.md +1 -0
- data/docs/internals/bridge.md +3 -1
- data/docs/internals/bundles.md +2 -2
- data/docs/internals/engine.md +46 -3
- data/docs/internals/pages.md +4 -4
- data/docs/internals/panes.md +66 -0
- data/docs/internals/participants.md +5 -3
- data/docs/internals/peers.md +176 -27
- data/docs/internals/prompt-caching.md +6 -1
- data/docs/internals/remote-sessions.md +6 -0
- data/docs/internals/session-lifecycle.md +12 -4
- data/docs/internals/web-frontend.md +106 -22
- data/docs/internals/working-set.md +5 -1
- data/docs/peers.md +150 -27
- data/docs/plugins.md +48 -7
- data/docs/releasing.md +30 -2
- data/docs/scheduling.md +3 -2
- data/docs/sessions.md +16 -7
- data/docs/sub-agent.md +6 -4
- data/docs/system-one.md +4 -0
- data/docs/testing.md +38 -3
- data/docs/thin-clients.md +27 -11
- 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 +8 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +61 -24
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +57 -19
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
- data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
- 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/compat.rb +8 -1
- data/lib/samagotchi/config.rb +19 -6
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +2 -2
- 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 +197 -7
- data/lib/samagotchi/engine/plugins.rb +2 -0
- 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/pair_boundary.rb +49 -18
- 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/jsonl_store.rb +13 -2
- data/lib/samagotchi/llm/adapter_common.rb +4 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +14 -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 +8 -2
- data/lib/samagotchi/llm/prompt_cache.rb +5 -0
- 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/pages/launch.rb +1 -1
- data/lib/samagotchi/panes/kitty.rb +122 -0
- data/lib/samagotchi/panes.rb +185 -0
- data/lib/samagotchi/peer_announce.rb +245 -0
- data/lib/samagotchi/peer_cold.rb +92 -0
- data/lib/samagotchi/peer_command.rb +121 -20
- data/lib/samagotchi/peer_continue.rb +42 -16
- data/lib/samagotchi/peer_exchanges.rb +7 -0
- data/lib/samagotchi/peer_mailbox.rb +50 -10
- data/lib/samagotchi/peer_pair_here.rb +149 -0
- data/lib/samagotchi/peer_presence.rb +434 -0
- data/lib/samagotchi/peer_receipts.rb +33 -6
- data/lib/samagotchi/peer_say.rb +46 -21
- data/lib/samagotchi/peer_wait_expiry.rb +47 -0
- data/lib/samagotchi/plugin/api.rb +13 -1
- data/lib/samagotchi/plugin/card.rb +68 -11
- data/lib/samagotchi/plugin/context.rb +33 -2
- data/lib/samagotchi/plugin/panes.rb +54 -0
- data/lib/samagotchi/plugin/peer.rb +170 -63
- data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
- data/lib/samagotchi/plugin/peer_start.rb +301 -0
- data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
- data/lib/samagotchi/plugin/peers.rb +89 -9
- 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_access/contract.rb +6 -0
- data/lib/samagotchi/session_access/local.rb +149 -40
- data/lib/samagotchi/session_access/remote.rb +4 -4
- data/lib/samagotchi/session_access/remote_peers.rb +8 -5
- data/lib/samagotchi/session_chain/links.rb +8 -3
- data/lib/samagotchi/session_commands.rb +40 -8
- data/lib/samagotchi/session_continue.rb +11 -6
- 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_manager/spawn.rb +6 -1
- data/lib/samagotchi/session_model_settings.rb +7 -3
- data/lib/samagotchi/session_stores.rb +25 -11
- data/lib/samagotchi/sessions_command.rb +16 -3
- data/lib/samagotchi/system_one/http_backend.rb +111 -10
- data/lib/samagotchi/system_one/runs.rb +5 -2
- data/lib/samagotchi/system_prompt.rb +11 -4
- 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 +0 -1
- data/lib/samagotchi/testing/bundle.rb +6 -2
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +3 -0
- data/lib/samagotchi/turn_endings.rb +6 -2
- 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 +40 -15
- data/lib/samagotchi/web/public/agent_rows.js +206 -0
- data/lib/samagotchi/web/public/agent_runs.js +595 -0
- data/lib/samagotchi/web/public/answer_fold.js +41 -44
- data/lib/samagotchi/web/public/app.js +4 -4
- data/lib/samagotchi/web/public/block_fix.js +1 -1
- data/lib/samagotchi/web/public/card.js +26 -10
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- 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/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +56 -2
- 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/page_view.js +1 -1
- data/lib/samagotchi/web/public/pages.css +1 -7
- 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_mailbox_api.js +2 -1
- data/lib/samagotchi/web/public/peer_note.js +4 -2
- data/lib/samagotchi/web/public/peer_row.js +37 -0
- data/lib/samagotchi/web/public/peer_wait.js +36 -0
- data/lib/samagotchi/web/public/quiet_notes.js +12 -1
- data/lib/samagotchi/web/public/revise_stack.js +5 -2
- data/lib/samagotchi/web/public/send_turn.js +11 -3
- 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/turn_block.js +4 -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 +24 -1
- data/lib/samagotchi/worker.rb +8 -5
- data/lib/samagotchi/worker_inbound/mail.rb +34 -6
- data/lib/samagotchi/worker_inbound.rb +11 -3
- data/lib/samagotchi/worker_link.rb +13 -1
- data/lib/samagotchi/worker_wakes.rb +33 -5
- metadata +22 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "etc"
|
|
4
|
+
require "json"
|
|
5
|
+
require "shellwords"
|
|
6
|
+
require "time"
|
|
7
|
+
require_relative "../atomic_file"
|
|
8
|
+
require_relative "../panes"
|
|
9
|
+
require_relative "../plugin_session_state"
|
|
10
|
+
require_relative "panes"
|
|
11
|
+
|
|
12
|
+
module Samagotchi
|
|
13
|
+
module Plugin
|
|
14
|
+
# `/<peer> start` (docs/peers.md, Start one in kitty; the peer-handshake
|
|
15
|
+
# mock-up's scenes 3 and 5): chi writes this session's wait, then opens a
|
|
16
|
+
# background kitty tab titled `<peer> · chi <id8>` in the session's cwd
|
|
17
|
+
# running the bundle setting `start_command` (default the peer's own
|
|
18
|
+
# command), through the user's login shell. claude-peer's Claude pairs
|
|
19
|
+
# on its own (its start hook takes the wait); codex-peer's Codex gets a
|
|
20
|
+
# first prompt, "pair with chi" (#start_prompt), which runs its skill's
|
|
21
|
+
# no-id pair and so spends one Codex turn (with codex-peer's start hook,
|
|
22
|
+
# the hook pairs at that first message and the skill's pair finds it
|
|
23
|
+
# paired; with or without the hook, #start_needs_hook?). Only in a command the user typed or tapped (it opens a tab on
|
|
24
|
+
# their screen: ctx.panes.open raises otherwise); a second start while
|
|
25
|
+
# the wait is open and its tab is there is refused (a double tap). The
|
|
26
|
+
# tab's PaneRef is kept in the bundle's own session state (state.json,
|
|
27
|
+
# ctx.session_dir: removed with the session), never in the mailbox, and
|
|
28
|
+
# checked on each use: Panes takes a ref only on a socket kitty.listen_on
|
|
29
|
+
# names, and a paired session's Show only the window its peer runs in.
|
|
30
|
+
# `/<peer> show` alone brings that tab to the front; the status card
|
|
31
|
+
# says "started by chi" with Show while it is open, and "its kitty tab
|
|
32
|
+
# is closed" with Start one in kitty after.
|
|
33
|
+
module PeerStart
|
|
34
|
+
STATE_KEY = "pane"
|
|
35
|
+
LOCK_FILE = "start.lock"
|
|
36
|
+
# How long after the launch the tab is checked once more: a start
|
|
37
|
+
# command that isn't found closes its tab at once.
|
|
38
|
+
SETTLE_SECONDS = 1.5
|
|
39
|
+
|
|
40
|
+
class << self
|
|
41
|
+
# SETTLE_SECONDS, unless set (specs: 0).
|
|
42
|
+
attr_writer :settle_seconds
|
|
43
|
+
|
|
44
|
+
def settle_seconds = @settle_seconds || SETTLE_SECONDS
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The first prompt the started peer gets (codex-peer: "pair with
|
|
48
|
+
# chi"); nil: none (claude-peer's hook pairs).
|
|
49
|
+
def start_prompt = nil
|
|
50
|
+
|
|
51
|
+
# Whether a started peer needs the start hook to pair (claude-peer: a
|
|
52
|
+
# Claude can't pair on its own without it); codex-peer's first prompt
|
|
53
|
+
# pairs with or without one.
|
|
54
|
+
def start_needs_hook? = true
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
# /<peer> start
|
|
59
|
+
def start_here(ctx)
|
|
60
|
+
peer = self.class
|
|
61
|
+
return "#{peer::COMMAND} start: it opens a kitty tab on your screen, so it runs only when you tap it or type it" unless ctx.user_typed?
|
|
62
|
+
return pairing_now if ctx.peers.claimed?(peer::PEER)
|
|
63
|
+
|
|
64
|
+
link = ctx.peers.link(peer::PEER)
|
|
65
|
+
return paired_already(ctx) if link && !link_gone?(link)
|
|
66
|
+
|
|
67
|
+
refused = start_hook_refusal and return refused
|
|
68
|
+
# Two starts at once (commands run in parallel: a double tap) take
|
|
69
|
+
# turns, so the second sees the first's wait and tab.
|
|
70
|
+
start_lock(ctx) do
|
|
71
|
+
starting = already_starting(ctx) and next starting
|
|
72
|
+
gone = end_gone_pairing(ctx)
|
|
73
|
+
joined(gone, open_started(ctx))
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Runs the block holding this session's start lock (a flock on
|
|
78
|
+
# LOCK_FILE in ctx.session_dir); without a session dir, unlocked.
|
|
79
|
+
def start_lock(ctx)
|
|
80
|
+
dir = ctx.session_dir(create: true)
|
|
81
|
+
return yield unless dir
|
|
82
|
+
|
|
83
|
+
File.open(File.join(dir, LOCK_FILE), File::RDWR | File::CREAT, 0o600) do |file|
|
|
84
|
+
file.flock(File::LOCK_EX)
|
|
85
|
+
yield
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Without the start hook a Claude started in kitty can't pair on its
|
|
90
|
+
# own (claude-peer: settings.json has no current announce line).
|
|
91
|
+
def start_hook_refusal
|
|
92
|
+
return nil unless start_hook? && start_needs_hook?
|
|
93
|
+
|
|
94
|
+
peer = self.class
|
|
95
|
+
case hook_state
|
|
96
|
+
when :none then "#{peer::COMMAND} start: install the start hook first (#{peer::COMMAND} install-skill)"
|
|
97
|
+
when :stale then "#{peer::COMMAND} start: #{hook_note}"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A start while this session waits and the tab it opened is there (a
|
|
102
|
+
# double tap): the line that says so, else nil.
|
|
103
|
+
def already_starting(ctx)
|
|
104
|
+
peer = self.class
|
|
105
|
+
mark = ctx.peers.waiting(peer::PEER)
|
|
106
|
+
started = started_pane(ctx)
|
|
107
|
+
return nil unless mark && !mark.stale? && started && ctx.panes.alive?(started[:ref])
|
|
108
|
+
|
|
109
|
+
"#{peer::COMMAND} start: already starting in kitty tab \"#{started[:title]}\" (#{peer::COMMAND} show brings it to the front)"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The wait (an open one kept: /<peer> pair's), then the tab; a tab that
|
|
113
|
+
# didn't open takes back only the wait this start wrote.
|
|
114
|
+
def open_started(ctx)
|
|
115
|
+
peer = self.class
|
|
116
|
+
open = ctx.peers.waiting(peer::PEER)
|
|
117
|
+
written = ctx.peers.wait(peer::PEER) unless open && !open.stale?
|
|
118
|
+
title = started_title(ctx)
|
|
119
|
+
begin
|
|
120
|
+
ref = ctx.panes.open(command: start_argv(ctx), cwd: ctx.cwd, title: title)
|
|
121
|
+
rescue Samagotchi::Panes::Error, Plugin::Panes::NotTyped => e
|
|
122
|
+
ctx.peers.stop_waiting(peer::PEER, written) if written
|
|
123
|
+
return "#{peer::COMMAND} start: #{e.message}"
|
|
124
|
+
end
|
|
125
|
+
return closed_at_once(ctx, written) if closed_at_once?(ctx, ref)
|
|
126
|
+
|
|
127
|
+
save_started(ctx, ref, title)
|
|
128
|
+
opened_line(ctx, title)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The tab gone SETTLE_SECONDS after it opened (its command not found
|
|
132
|
+
# in the login shell's PATH, or failing at once).
|
|
133
|
+
def closed_at_once?(ctx, ref)
|
|
134
|
+
delay = PeerStart.settle_seconds
|
|
135
|
+
sleep(delay) if delay.positive?
|
|
136
|
+
!ctx.panes.alive?(ref)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def closed_at_once(ctx, written)
|
|
140
|
+
peer = self.class
|
|
141
|
+
ctx.peers.stop_waiting(peer::PEER, written) if written
|
|
142
|
+
shell = File.basename(login_shell)
|
|
143
|
+
"#{peer::COMMAND} start: the tab closed at once: check bundles.#{peer::BUNDLE}.start_command (it runs as " \
|
|
144
|
+
"`#{shell} -l -c \"exec #{start_command(ctx)}\"`, so it needs that login shell's PATH)"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def opened_line(ctx, title)
|
|
148
|
+
peer = self.class
|
|
149
|
+
line = "opened a kitty tab \"#{title}\" running `#{start_command(ctx)}`"
|
|
150
|
+
line = if start_prompt
|
|
151
|
+
"#{line} with the first prompt \"#{start_prompt}\" (one #{peer::NAME} turn): it pairs when #{peer::NAME} " \
|
|
152
|
+
"runs it (waiting #{wait_minutes} min)"
|
|
153
|
+
else
|
|
154
|
+
"#{line}: it pairs when it starts (waiting #{wait_minutes} min)"
|
|
155
|
+
end
|
|
156
|
+
hint = start_hook_hint
|
|
157
|
+
hint ? "#{line}; #{hint}" : line
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# What the opened line adds while the start hook is there (codex-peer:
|
|
161
|
+
# Codex may stop on its hook review first); nil: nothing.
|
|
162
|
+
def start_review_hint = nil
|
|
163
|
+
|
|
164
|
+
def start_hook_hint
|
|
165
|
+
hint = start_review_hint or return nil
|
|
166
|
+
start_hook? && hook_state == :current ? hint : nil
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# The status card's Start one in kitty (codex-peer's: it says the turn
|
|
170
|
+
# it spends).
|
|
171
|
+
def start_action
|
|
172
|
+
peer = self.class
|
|
173
|
+
label = start_prompt ? "Start one in kitty (one #{peer::NAME} turn)" : "Start one in kitty"
|
|
174
|
+
{ label: label, command: "#{peer::COMMAND} start" }
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def started_title(ctx) = "#{self.class::PEER} · chi #{ctx.session_id.to_s[0, 8]}"
|
|
178
|
+
|
|
179
|
+
# The bundle setting start_command (a shell line: the owner may add
|
|
180
|
+
# flags), else the peer's own command.
|
|
181
|
+
def start_command(ctx)
|
|
182
|
+
line = ctx.settings["start_command"].to_s.strip
|
|
183
|
+
line.empty? ? self.class::PEER : line
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# The tab's argv: the user's login shell (kitty's own env may lack the
|
|
187
|
+
# PATH a terminal has) exec'ing the start command, the first prompt as
|
|
188
|
+
# its last word.
|
|
189
|
+
def start_argv(ctx)
|
|
190
|
+
line = start_command(ctx)
|
|
191
|
+
line = "#{line} #{Shellwords.escape(start_prompt)}" if start_prompt
|
|
192
|
+
[login_shell, "-l", "-c", "exec #{line}"]
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# $SHELL, else the account's own (a worker under launchd may have no
|
|
196
|
+
# $SHELL), else /bin/sh.
|
|
197
|
+
def login_shell
|
|
198
|
+
[ENV.fetch("SHELL", ""), account_shell].map { |shell| shell.to_s.strip }
|
|
199
|
+
.find { |shell| shell.start_with?("/") && File.executable?(shell) } || "/bin/sh"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def account_shell
|
|
203
|
+
Etc.getpwuid(Process.uid)&.shell
|
|
204
|
+
rescue ArgumentError, SystemCallError
|
|
205
|
+
nil
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# /<peer> show alone: the tab chi started, to the front.
|
|
209
|
+
def show_started(ctx)
|
|
210
|
+
peer = self.class
|
|
211
|
+
return "#{peer::COMMAND} show: Show brings a window to the front, so it runs only when you tap it or type it" unless ctx.user_typed?
|
|
212
|
+
|
|
213
|
+
started = started_pane(ctx)
|
|
214
|
+
return "#{peer::COMMAND} show: chi started no #{peer::NAME} tab here (#{peer::COMMAND} show <id8> shows one #{peer::COMMAND} pair lists)" unless started
|
|
215
|
+
|
|
216
|
+
link = ctx.peers.link(peer::PEER)
|
|
217
|
+
# Paired: only the window the paired peer runs in (a ref read back
|
|
218
|
+
# from state.json is checked, never trusted).
|
|
219
|
+
if link && ctx.panes.alive?(started[:ref]) && !same_window?(ctx, link, started[:ref])
|
|
220
|
+
return "#{peer::COMMAND} show: the paired #{peer::NAME} doesn't run in the tab chi started"
|
|
221
|
+
end
|
|
222
|
+
return "#{peer::COMMAND} show: its kitty tab is closed (#{peer::COMMAND} start opens another)" unless ctx.panes.show(started[:ref])
|
|
223
|
+
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# The status card's row for a pairing with the tab chi started
|
|
228
|
+
# (scene 5): [rows, actions], or nil when chi started none, or the
|
|
229
|
+
# pairing isn't with it.
|
|
230
|
+
def started_rows(ctx, link)
|
|
231
|
+
peer = self.class
|
|
232
|
+
started = started_pane(ctx) or return nil
|
|
233
|
+
return nil unless started_pairing?(link, started)
|
|
234
|
+
|
|
235
|
+
if ctx.panes.alive?(started[:ref])
|
|
236
|
+
return nil unless same_window?(ctx, link, started[:ref])
|
|
237
|
+
|
|
238
|
+
[[{ title: "Paired · started by chi", sub: "kitty tab \"#{started[:title]}\" · last heard #{last_heard(ctx, link)}" }],
|
|
239
|
+
[{ label: "Show", command: "#{peer::COMMAND} show", row: 0, primary: true },
|
|
240
|
+
{ label: "Unpair", command: "#{peer::COMMAND} unpair", row: 0 }]]
|
|
241
|
+
elsif link_gone?(link)
|
|
242
|
+
[[{ title: "Its kitty tab is closed",
|
|
243
|
+
sub: "That #{peer::NAME} ended. Start another, or wait for one you start yourself." }],
|
|
244
|
+
[{ label: "Start one in kitty", command: "#{peer::COMMAND} start", row: 0, primary: true },
|
|
245
|
+
{ label: "Unpair", command: "#{peer::COMMAND} unpair", row: 0 }]]
|
|
246
|
+
end
|
|
247
|
+
rescue StandardError # degrade-ok: kitty not answering: the card without its started row
|
|
248
|
+
nil
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Paired since chi started the tab (a pairing older than it is another).
|
|
252
|
+
def started_pairing?(link, started)
|
|
253
|
+
Time.iso8601(link.paired_at.to_s) >= Time.iso8601(started[:at].to_s) - 5
|
|
254
|
+
rescue ArgumentError
|
|
255
|
+
false
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# The paired peer runs in that window: its pid (the start hook's
|
|
259
|
+
# announce) in it; a peer that lists nothing (codex-peer) can't be told
|
|
260
|
+
# apart, so the window being open is enough.
|
|
261
|
+
def same_window?(ctx, link, ref)
|
|
262
|
+
entry = ctx.peers.here(self.class::PEER).find { |here| here.session == link.peer_session }
|
|
263
|
+
return !announces? unless entry
|
|
264
|
+
|
|
265
|
+
ctx.panes.find(pid: entry.pid)&.key == ref.key
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def last_heard(ctx, link)
|
|
269
|
+
started_ago(ctx.peers.last_message(self.class::PEER)&.at || link.paired_at)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# {ref:, title:, at:} of the tab chi started, or nil.
|
|
273
|
+
def started_pane(ctx)
|
|
274
|
+
data = read_state(ctx)[STATE_KEY]
|
|
275
|
+
ref = Samagotchi::Panes::PaneRef.from_h(data) or return nil
|
|
276
|
+
{ ref: ref, title: data["title"].to_s, at: data["at"].to_s }
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def save_started(ctx, ref, title)
|
|
280
|
+
path = state_path(ctx, create: true) or return
|
|
281
|
+
state = read_state(ctx).merge(STATE_KEY => ref.to_h.merge("title" => title, "at" => Time.now.utc.iso8601))
|
|
282
|
+
AtomicFile.write(path, "#{JSON.generate(state)}\n")
|
|
283
|
+
rescue SystemCallError => e
|
|
284
|
+
ctx.log.warn("peer_start_not_saved", error: e.message)
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def read_state(ctx)
|
|
288
|
+
path = state_path(ctx, create: false)
|
|
289
|
+
data = path && File.file?(path) ? JSON.parse(File.read(path)) : {}
|
|
290
|
+
data.is_a?(Hash) ? data : {}
|
|
291
|
+
rescue JSON::ParserError, SystemCallError
|
|
292
|
+
{}
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def state_path(ctx, create:)
|
|
296
|
+
dir = ctx.session_dir(create: create) or return nil
|
|
297
|
+
File.join(dir, PluginSessionState::STATE_FILE)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
end
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "open3"
|
|
6
|
+
require "shellwords"
|
|
7
|
+
require "tempfile"
|
|
8
|
+
require_relative "../atomic_file"
|
|
9
|
+
require_relative "../chi_shim"
|
|
10
|
+
|
|
11
|
+
module Samagotchi
|
|
12
|
+
module Plugin
|
|
13
|
+
# The peer's start hook (claude-peer: Claude Code's SessionStart runs
|
|
14
|
+
# `chi peer announce claude`, so a Claude lists itself here, takes a
|
|
15
|
+
# wait and follows a resume; codex-peer: Codex's runs `chi peer
|
|
16
|
+
# announce codex`, so a new interactive Codex takes a wait at its first
|
|
17
|
+
# message): `/<peer> install-hook` shows the exact line, the merged file
|
|
18
|
+
# as a diff, the one file it writes and how to remove it; `--yes` (that
|
|
19
|
+
# card's "Add the start hook") merges it into the peer's user file
|
|
20
|
+
# (#hook_file: Claude's settings.json, Codex's hooks.json), keeping
|
|
21
|
+
# every other key and every other hook in its place, and only in a
|
|
22
|
+
# command the user typed or tapped (ctx.user_typed?: it writes another
|
|
23
|
+
# agent's settings). A bundle opts in with #start_hook? and gives its
|
|
24
|
+
# file, line and texts. chi never writes Codex's trust (config.toml):
|
|
25
|
+
# Codex reviews a new or changed hook itself, and chi never bypasses
|
|
26
|
+
# that.
|
|
27
|
+
module PeerStartHook
|
|
28
|
+
HOOK_CARD_SUFFIX = "-hook"
|
|
29
|
+
EVENT = "SessionStart"
|
|
30
|
+
|
|
31
|
+
# Whether the bundle installs a start hook (claude-peer).
|
|
32
|
+
def start_hook? = false
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# The hook's one line (claude-peer's; a bundle gives its own). The sh test keeps a headless run (a nested
|
|
37
|
+
# `claude -p`) from booting chi; subagents fire no SessionStart. `2>/dev/null
|
|
38
|
+
# || true`: a failing hook shows the user "hook failed" with its stderr,
|
|
39
|
+
# and under CHI_REMOTE bin/chi exits 2 before chi peer runs.
|
|
40
|
+
def start_hook_line
|
|
41
|
+
"[ \"$CLAUDE_CODE_SESSION_ATTENDED\" = 1 ] && #{hook_chi} peer announce #{self.class::PEER} 2>/dev/null || true"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The chi a hook line runs: the chi home's shim (ChiShim), shell-quoted.
|
|
45
|
+
def hook_chi = Shellwords.escape(ChiShim.path)
|
|
46
|
+
|
|
47
|
+
# The peer's user file the hook goes in (claude-peer: settings.json
|
|
48
|
+
# under #peer_dir; codex-peer: hooks.json).
|
|
49
|
+
def hook_file = File.join(peer_dir, "settings.json")
|
|
50
|
+
|
|
51
|
+
# /<peer> install-hook [--yes]
|
|
52
|
+
def install_hook(ctx, yes:)
|
|
53
|
+
peer = self.class
|
|
54
|
+
return peer::USAGE unless start_hook?
|
|
55
|
+
|
|
56
|
+
current, merged, problem = merged_settings
|
|
57
|
+
return "#{peer::COMMAND} install-hook: #{problem}" if problem
|
|
58
|
+
return hook_card(ctx, current, merged) unless yes
|
|
59
|
+
unless ctx.user_typed?
|
|
60
|
+
return "#{peer::COMMAND} install-hook: it writes #{peer::APP}'s settings, so it runs only when you tap Add " \
|
|
61
|
+
"the start hook or type it"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
write_hook(ctx, merged)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# [current text or nil, the merged JSON text, a problem or nil].
|
|
68
|
+
def merged_settings
|
|
69
|
+
file = hook_file
|
|
70
|
+
current = File.file?(file) ? File.read(file) : nil
|
|
71
|
+
data = current.nil? || current.strip.empty? ? {} : JSON.parse(current)
|
|
72
|
+
return [current, nil, "#{file} isn't a JSON object: add the line by hand"] unless data.is_a?(Hash)
|
|
73
|
+
unless hooks_shape?(data)
|
|
74
|
+
return [current, nil, "#{file}'s hooks aren't an object with a #{EVENT} list, so chi won't rewrite them: add " \
|
|
75
|
+
"the line by hand: `#{start_hook_line}`"]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
[current, "#{JSON.pretty_generate(with_hook(data))}\n", nil]
|
|
79
|
+
rescue JSON::ParserError
|
|
80
|
+
[current, nil, "#{file} isn't JSON: fix it, or add the line by hand"]
|
|
81
|
+
rescue SystemCallError => e
|
|
82
|
+
[nil, nil, "couldn't read #{file}: #{e.message}"]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# No `hooks`, or an object whose #{EVENT}, if any, is a list: what
|
|
86
|
+
# #with_hook merges into without dropping anything.
|
|
87
|
+
def hooks_shape?(data)
|
|
88
|
+
return true unless data.key?("hooks")
|
|
89
|
+
|
|
90
|
+
hooks = data["hooks"]
|
|
91
|
+
hooks.is_a?(Hash) && (!hooks.key?(EVENT) || hooks[EVENT].is_a?(Array))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# +data+ with this bundle's start hook: an announce entry already there
|
|
95
|
+
# (an older chi's path) replaced in place, else one group appended
|
|
96
|
+
# (never reordered: Codex keys a hook's trust by its group's index).
|
|
97
|
+
def with_hook(data)
|
|
98
|
+
hooks = data["hooks"].is_a?(Hash) ? data["hooks"] : {}
|
|
99
|
+
groups = Array(hooks[EVENT]).map(&:dup)
|
|
100
|
+
found = false
|
|
101
|
+
groups = groups.map do |group|
|
|
102
|
+
next group unless group.is_a?(Hash) && group["hooks"].is_a?(Array)
|
|
103
|
+
|
|
104
|
+
group.merge("hooks" => group["hooks"].map do |hook|
|
|
105
|
+
next hook unless ours?(hook)
|
|
106
|
+
|
|
107
|
+
found = true
|
|
108
|
+
hook.merge("command" => start_hook_line)
|
|
109
|
+
end)
|
|
110
|
+
end
|
|
111
|
+
groups << { "hooks" => [{ "type" => "command", "command" => start_hook_line }] } unless found
|
|
112
|
+
data.merge("hooks" => hooks.merge(EVENT => groups))
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def ours?(hook) = hook.is_a?(Hash) && hook["command"].to_s.include?("peer announce #{self.class::PEER}")
|
|
116
|
+
|
|
117
|
+
# The announce lines in the settings now ([] for none or unreadable).
|
|
118
|
+
def installed_hook_lines
|
|
119
|
+
data = JSON.parse(File.read(hook_file))
|
|
120
|
+
Array(data.dig("hooks", EVENT)).flat_map { |group| group.is_a?(Hash) ? Array(group["hooks"]) : [] }
|
|
121
|
+
.select { |hook| ours?(hook) }.map { |hook| hook["command"].to_s }
|
|
122
|
+
rescue JSON::ParserError, SystemCallError, TypeError
|
|
123
|
+
[]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# :none, :current, or :stale (an announce line that isn't this chi's:
|
|
127
|
+
# a moved chi home).
|
|
128
|
+
def hook_state
|
|
129
|
+
lines = installed_hook_lines
|
|
130
|
+
return :none if lines.empty?
|
|
131
|
+
|
|
132
|
+
lines.include?(start_hook_line) ? :current : :stale
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# A status card's line about the hook, or nil.
|
|
136
|
+
def hook_note
|
|
137
|
+
return nil unless start_hook? && hook_state == :stale
|
|
138
|
+
|
|
139
|
+
"the start hook points at a chi that is gone: add it again (#{self.class::COMMAND} install-skill, Add the start hook)" \
|
|
140
|
+
"#{hook_stale_tail}"
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# What the stale note adds (codex-peer: Codex asks again).
|
|
144
|
+
def hook_stale_tail = ""
|
|
145
|
+
|
|
146
|
+
def hook_card(ctx, current, merged)
|
|
147
|
+
peer = self.class
|
|
148
|
+
body = [hook_paragraph, "```diff\n#{hook_diff(current, merged)}\n```"]
|
|
149
|
+
actions = hook_state == :current ? [] : [hook_action]
|
|
150
|
+
ctx.card(id: "#{peer::STATUS_CARD}#{HOOK_CARD_SUFFIX}", title: "#{peer::NAME}'s start hook", body: body.join("\n\n"),
|
|
151
|
+
actions: actions)
|
|
152
|
+
nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# What the hook does, its exact line, the one file and how to remove it
|
|
156
|
+
# (claude-peer's; a bundle gives its own).
|
|
157
|
+
def hook_paragraph
|
|
158
|
+
peer = self.class
|
|
159
|
+
"With the start hook, a #{peer::APP} session lists itself here when it starts, so `#{peer::COMMAND} pair` " \
|
|
160
|
+
"finds it, a wait pairs on its own, and a resume keeps its pairing. It adds one `#{EVENT}` hook to " \
|
|
161
|
+
"`#{hook_file}` (the one file it writes), every other key kept:\n\n```sh\n#{start_hook_line}\n```\n\n" \
|
|
162
|
+
"To remove it, delete that #{EVENT} entry from the file."
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# The card's text once the hook is written (claude-peer's).
|
|
166
|
+
def hook_written_line
|
|
167
|
+
"Added to `#{hook_file}`. A #{self.class::APP} session started from now on lists itself here; one open now " \
|
|
168
|
+
"needs a restart (`claude --resume`), or say \"pair with chi\" there."
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def hook_action = { label: "Add the start hook", command: "#{self.class::COMMAND} install-hook --yes" }
|
|
172
|
+
|
|
173
|
+
def write_hook(ctx, merged)
|
|
174
|
+
target = hook_target
|
|
175
|
+
FileUtils.mkdir_p(File.dirname(target))
|
|
176
|
+
AtomicFile.write(target, merged, perm: (File.stat(target).mode & 0o777 if File.exist?(target)))
|
|
177
|
+
ctx.card(id: "#{self.class::STATUS_CARD}#{HOOK_CARD_SUFFIX}", title: "#{self.class::NAME}'s start hook",
|
|
178
|
+
body: hook_written_line)
|
|
179
|
+
nil
|
|
180
|
+
rescue SystemCallError => e
|
|
181
|
+
"#{self.class::COMMAND} install-hook: couldn't write #{hook_file}: #{e.message}"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# The file the hook lives in: a symlinked one (a dotfiles repo's) is
|
|
185
|
+
# written at its real path, so the link stays a link.
|
|
186
|
+
# @raise [SystemCallError] a link to nowhere
|
|
187
|
+
def hook_target = File.symlink?(hook_file) ? File.realpath(hook_file) : hook_file
|
|
188
|
+
|
|
189
|
+
# `diff -u` of the file now (none: empty) and the merged one.
|
|
190
|
+
def hook_diff(current, merged)
|
|
191
|
+
Tempfile.create(["chi-settings-now", ".json"]) do |now|
|
|
192
|
+
now.write(current.to_s)
|
|
193
|
+
now.flush
|
|
194
|
+
Tempfile.create(["chi-settings", ".json"]) do |after|
|
|
195
|
+
after.write(merged)
|
|
196
|
+
after.flush
|
|
197
|
+
out, = Open3.capture2("diff", "-u", "--label", "#{File.basename(hook_file)} now", "--label", "with the start hook",
|
|
198
|
+
now.path, after.path)
|
|
199
|
+
out.chomp
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
rescue SystemCallError
|
|
203
|
+
merged
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../memory_paths"
|
|
3
4
|
require_relative "../peer_mailbox"
|
|
5
|
+
require_relative "../peer_presence"
|
|
4
6
|
require_relative "../peer_receipts"
|
|
5
7
|
require_relative "../session"
|
|
6
8
|
|
|
@@ -8,9 +10,12 @@ module Samagotchi
|
|
|
8
10
|
module Plugin
|
|
9
11
|
# ctx.peers (docs/plugins.md, Peers): what a peer bundle reads of its
|
|
10
12
|
# peer's side of the session (PeerMailbox): the pairing (where its
|
|
11
|
-
# sends go) and the peer's messages
|
|
12
|
-
#
|
|
13
|
-
#
|
|
13
|
+
# sends go) and the peer's messages; and pairing from chi's side: the
|
|
14
|
+
# peer sessions here (PeerPresence, chi peer announce), this session's
|
|
15
|
+
# wait for one, and #pair / #unpair over SessionAccess::Local (the one
|
|
16
|
+
# writer, with its worker-age and continue rules). A peer says through
|
|
17
|
+
# `chi peer` (its skill); the worker takes the messages
|
|
18
|
+
# (WorkerInbound#absorb_mail). Plugin::Peer is its caller.
|
|
14
19
|
class Peers
|
|
15
20
|
# @param host [Host] session_id and state_dir
|
|
16
21
|
# @param continues [#call] the id of the session this one continues, or nil
|
|
@@ -23,18 +28,25 @@ module Samagotchi
|
|
|
23
28
|
# @return [PeerMailbox::PeerLink, nil] nil when not paired (or no session yet)
|
|
24
29
|
def link(name) = read_link(@host.session_id.call, name)
|
|
25
30
|
|
|
26
|
-
# The pairing a continue
|
|
27
|
-
#
|
|
28
|
-
# #tell_moved) and pairs again.
|
|
31
|
+
# The previous link's pairing, in a continue's new link: the one the
|
|
32
|
+
# continue copied here (PeerContinue), as the previous link keeps it.
|
|
29
33
|
# @return [PeerMailbox::PeerLink, nil] nil when this session continues none, or it wasn't paired
|
|
30
34
|
def left_behind(name) = read_link(@continues.call, name)
|
|
31
35
|
|
|
32
|
-
# +name+'s message +serial+ (its text, summary, wake, re, at).
|
|
36
|
+
# +name+'s message +serial+ (its text, summary, wake, re, at). +box+
|
|
37
|
+
# names the mailbox: this session's box for +name+ (the note's), or the
|
|
38
|
+
# first 8 of the session this one continues (a continue's note points
|
|
39
|
+
# there for a message it cut: PeerContinue::CUT), so only the link one
|
|
40
|
+
# hop back in this session's own chain; anything else reads nothing.
|
|
33
41
|
# @return [PeerMailbox::PeerMessage, nil] nil when it isn't there (trimmed) or isn't good
|
|
34
42
|
def message(name, serial, box: nil)
|
|
35
43
|
id = @host.session_id.call or return nil
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
previous = @continues.call
|
|
45
|
+
if box && previous && box == previous.to_s[0, 8]
|
|
46
|
+
id = previous
|
|
47
|
+
elsif box && PeerReceipts.read(id, state_dir: state_dir).peer(name.to_s).box != box
|
|
48
|
+
return nil
|
|
49
|
+
end
|
|
38
50
|
|
|
39
51
|
PeerMailbox.message(id, name.to_s, serial, state_dir: state_dir)
|
|
40
52
|
rescue PeerMailbox::Invalid, SessionPaths::InvalidId
|
|
@@ -54,10 +66,78 @@ module Samagotchi
|
|
|
54
66
|
nil
|
|
55
67
|
end
|
|
56
68
|
|
|
69
|
+
# Pairs +name+ with this session (SessionAccess::Local#peer_pair: its
|
|
70
|
+
# refusals; a pairing that paired ends this session's wait).
|
|
71
|
+
# @return [PeerPaired]
|
|
72
|
+
def pair(name, address:, from:, peer_session: nil)
|
|
73
|
+
access.peer_pair(session_id!, name.to_s, address: address, from: from, peer_session: peer_session)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Ends the pairing and this session's wait (SessionAccess::Local#peer_unpair).
|
|
77
|
+
# @return [PeerUnpaired]
|
|
78
|
+
def unpair(name) = access.peer_unpair(session_id!, name.to_s)
|
|
79
|
+
|
|
80
|
+
# The live +name+ sessions of this session's project (its cwd's
|
|
81
|
+
# MemoryPaths.project_key), oldest first; none without a project.
|
|
82
|
+
# @return [Array<PeerPresence::PeerHere>]
|
|
83
|
+
def here(name)
|
|
84
|
+
key = project or return []
|
|
85
|
+
PeerPresence.here(name.to_s, project: key, state_dir: state_dir)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# This session starts waiting for +name+ (10 minutes; again: anew).
|
|
89
|
+
# @return [PeerPresence::PeerWaiting]
|
|
90
|
+
def wait(name) = PeerPresence.wait!(name.to_s, session: session_id!, project: project, state_dir: state_dir)
|
|
91
|
+
|
|
92
|
+
# Ends this session's wait for +name+ when it is still +mark+ (the
|
|
93
|
+
# one #wait returned: a start whose tab didn't open), leaving any other
|
|
94
|
+
# wait, a claim and any pairing alone (PeerPresence.drop_wait).
|
|
95
|
+
# @return [Boolean] whether it was removed
|
|
96
|
+
def stop_waiting(name, mark)
|
|
97
|
+
id = @host.session_id.call or return false
|
|
98
|
+
PeerPresence.drop_wait(name.to_s, id, mark, state_dir: state_dir)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# This session's wait for +name+, stale too (PeerWaiting#stale?), or nil.
|
|
102
|
+
def waiting(name)
|
|
103
|
+
id = @host.session_id.call or return nil
|
|
104
|
+
PeerPresence.waiting(name.to_s, id, state_dir: state_dir)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Takes this session's fresh wait for +name+ before a pairing against
|
|
108
|
+
# it (PeerPresence.claim); nil when none is there to take.
|
|
109
|
+
def claim(name)
|
|
110
|
+
id = @host.session_id.call or return nil
|
|
111
|
+
PeerPresence.claim(name.to_s, id, state_dir: state_dir)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Whether another pairing holds this session's wait for +name+ now
|
|
115
|
+
# (PeerPresence.claimed?): a pair then writes nothing.
|
|
116
|
+
def claimed?(name)
|
|
117
|
+
id = @host.session_id.call or return false
|
|
118
|
+
PeerPresence.claimed?(name.to_s, id, state_dir: state_dir)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# A claim whose pairing didn't pair, given back.
|
|
122
|
+
def release(claim) = PeerPresence.release(claim)
|
|
123
|
+
|
|
57
124
|
private
|
|
58
125
|
|
|
59
126
|
def state_dir = @host.state_dir&.call || Session.default_state_dir
|
|
60
127
|
|
|
128
|
+
def access
|
|
129
|
+
require_relative "../session_access/local"
|
|
130
|
+
SessionAccess::Local.new(state_dir: state_dir)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def session_id! = @host.session_id.call || raise(ArgumentError, "this session has no id yet")
|
|
134
|
+
|
|
135
|
+
def project
|
|
136
|
+
MemoryPaths.project_key(@host.cwd&.call || Dir.pwd)
|
|
137
|
+
rescue StandardError # degrade-ok: a folder whose project can't be told (a ProjectIdConflict) lists no peer
|
|
138
|
+
nil
|
|
139
|
+
end
|
|
140
|
+
|
|
61
141
|
def read_link(id, name)
|
|
62
142
|
return nil unless id
|
|
63
143
|
|