samagotchi 0.194.0 → 0.196.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +80 -1
- data/docs/architecture.md +2 -1
- data/docs/bundle-authoring.md +23 -23
- data/docs/bundles.md +201 -7
- data/docs/cli.md +5 -1
- data/docs/configuration.md +25 -4
- data/docs/context.md +13 -3
- data/docs/guardrails.md +6 -4
- data/docs/hooks.md +3 -1
- data/docs/internals/attached-context.md +2 -0
- data/docs/internals/block-renderers.md +4 -1
- data/docs/internals/bridge.md +4 -0
- data/docs/internals/engine.md +15 -2
- data/docs/internals/knowledge.md +215 -0
- data/docs/internals/pages.md +17 -9
- data/docs/internals/panes.md +51 -13
- data/docs/internals/participants.md +15 -3
- data/docs/internals/peers.md +14 -9
- data/docs/internals/session-lifecycle.md +26 -12
- data/docs/internals/web-frontend.md +64 -27
- data/docs/memory.md +4 -2
- data/docs/pages.md +72 -1
- data/docs/peers.md +25 -14
- data/docs/plugins.md +132 -6
- data/docs/releasing.md +1 -1
- data/docs/scheduling.md +4 -0
- data/docs/sessions.md +9 -2
- data/docs/system-one.md +3 -1
- data/docs/testing.md +3 -1
- data/lib/samagotchi/bundle_manifest.rb +30 -5
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +2 -2
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +3 -3
- data/lib/samagotchi/bundles/coordinator/manifest.yml +7 -4
- data/lib/samagotchi/bundles/coordinator/plugin.rb +15 -5
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +10 -0
- data/lib/samagotchi/bundles/core/manifest.yml +2 -2
- data/lib/samagotchi/bundles/cost-cap/manifest.yml +4 -4
- data/lib/samagotchi/bundles/cost-cap/plugin.rb +54 -3
- data/lib/samagotchi/bundles/dev/manifest.yml +3 -2
- data/lib/samagotchi/bundles/github-pr/manifest.yml +4 -2
- data/lib/samagotchi/bundles/knowledge/adapters.rb +205 -0
- data/lib/samagotchi/bundles/knowledge/catalog.rb +151 -0
- data/lib/samagotchi/bundles/knowledge/chi_adapters.rb +178 -0
- data/lib/samagotchi/bundles/knowledge/chunker.rb +55 -0
- data/lib/samagotchi/bundles/knowledge/github.rb +246 -0
- data/lib/samagotchi/bundles/knowledge/manifest.yml +27 -0
- data/lib/samagotchi/bundles/knowledge/plugin.rb +807 -0
- data/lib/samagotchi/bundles/knowledge/query.rb +30 -0
- data/lib/samagotchi/bundles/knowledge/query_log.rb +55 -0
- data/lib/samagotchi/bundles/knowledge/refresh.rb +133 -0
- data/lib/samagotchi/bundles/knowledge/store.rb +264 -0
- data/lib/samagotchi/bundles/reflect/manifest.yml +17 -0
- data/lib/samagotchi/bundles/reflect/scripts/reflect.rb +439 -0
- data/lib/samagotchi/bundles/reflect/skill_reflect.md +100 -0
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/context_change.rb +44 -0
- data/lib/samagotchi/context_note.rb +1 -1
- data/lib/samagotchi/context_providers.rb +14 -0
- data/lib/samagotchi/context_sources.rb +3 -25
- data/lib/samagotchi/engine/plugins.rb +21 -0
- data/lib/samagotchi/events.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +12 -4
- data/lib/samagotchi/guardrails/review_paths.rb +13 -3
- data/lib/samagotchi/guardrails/targets.rb +24 -2
- data/lib/samagotchi/guardrails_command.rb +3 -9
- data/lib/samagotchi/hook_label.rb +19 -0
- data/lib/samagotchi/hook_routes.rb +3 -2
- data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
- data/lib/samagotchi/memory_bundle/builder.rb +2 -1
- data/lib/samagotchi/memory_bundle/installed_copy.rb +2 -1
- data/lib/samagotchi/page_command.rb +90 -6
- data/lib/samagotchi/pages/launch.rb +16 -8
- data/lib/samagotchi/pages/runner.rb +7 -2
- data/lib/samagotchi/pages/runtime.rb +62 -2
- data/lib/samagotchi/pages.rb +4 -0
- data/lib/samagotchi/panes/kitty.rb +20 -5
- data/lib/samagotchi/panes.rb +81 -12
- data/lib/samagotchi/participants.rb +17 -6
- data/lib/samagotchi/peer_exchanges.rb +1 -2
- data/lib/samagotchi/peer_note.rb +38 -5
- data/lib/samagotchi/peer_presence.rb +20 -7
- data/lib/samagotchi/plugin/api.rb +11 -1
- data/lib/samagotchi/plugin/attached_context.rb +46 -1
- data/lib/samagotchi/plugin/chips.rb +125 -0
- data/lib/samagotchi/plugin/context.rb +43 -8
- data/lib/samagotchi/plugin/panes.rb +52 -9
- data/lib/samagotchi/plugin/peer_pairing.rb +2 -2
- data/lib/samagotchi/plugin/peer_start.rb +61 -36
- data/lib/samagotchi/plugin/peers.rb +6 -3
- data/lib/samagotchi/plugin/project.rb +63 -0
- data/lib/samagotchi/plugin/sessions.rb +36 -1
- data/lib/samagotchi/plugin/tool_spec.rb +28 -9
- data/lib/samagotchi/schedules.rb +8 -2
- data/lib/samagotchi/session.rb +22 -9
- data/lib/samagotchi/session_access/local.rb +34 -33
- data/lib/samagotchi/session_chain/sharing.rb +79 -0
- data/lib/samagotchi/session_chain/successor.rb +4 -2
- data/lib/samagotchi/session_chips.rb +133 -0
- data/lib/samagotchi/session_inbox_delivery.rb +11 -11
- data/lib/samagotchi/session_manager/emptiness.rb +4 -1
- data/lib/samagotchi/session_observer.rb +1 -1
- data/lib/samagotchi/session_stats.rb +437 -0
- data/lib/samagotchi/session_stores.rb +3 -0
- data/lib/samagotchi/session_window.rb +30 -0
- data/lib/samagotchi/speaker.rb +228 -0
- data/lib/samagotchi/steer.rb +36 -132
- data/lib/samagotchi/system_one/approval_choice.rb +22 -0
- data/lib/samagotchi/system_one/approval_label.rb +4 -6
- data/lib/samagotchi/system_prompt.rb +3 -1
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +30 -0
- data/lib/samagotchi/testing/boundary.rb +17 -4
- data/lib/samagotchi/testing/bundle.rb +5 -0
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +10 -0
- data/lib/samagotchi/testing/sessions.rb +11 -3
- data/lib/samagotchi/toolkit.rb +11 -4
- data/lib/samagotchi/toolkit_report_command.rb +14 -18
- data/lib/samagotchi/toolkit_suggest.rb +9 -2
- data/lib/samagotchi/tools/registry.rb +48 -18
- data/lib/samagotchi/tools/tool_index.rb +6 -2
- data/lib/samagotchi/turn_excerpts.rb +302 -0
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +9 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +21 -10
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +5 -2
- data/lib/samagotchi/web/plugin_chip_rows.rb +39 -0
- data/lib/samagotchi/web/public/agent_rows.js +1 -0
- data/lib/samagotchi/web/public/app.js +17 -25
- data/lib/samagotchi/web/public/chains.js +0 -26
- data/lib/samagotchi/web/public/command_list.js +2 -3
- data/lib/samagotchi/web/public/composer.js +2 -3
- data/lib/samagotchi/web/public/composer_mount.js +3 -6
- data/lib/samagotchi/web/public/context_chips.js +240 -93
- data/lib/samagotchi/web/public/context_groups.js +84 -0
- data/lib/samagotchi/web/public/draft_follow.js +33 -0
- data/lib/samagotchi/web/public/drafts.js +26 -22
- data/lib/samagotchi/web/public/index.html +32 -0
- data/lib/samagotchi/web/public/peer_chip_kind.js +49 -0
- data/lib/samagotchi/web/public/peer_note.js +2 -2
- data/lib/samagotchi/web/public/peer_row.js +10 -5
- data/lib/samagotchi/web/public/peer_wait.js +4 -2
- data/lib/samagotchi/web/public/plugin_chip.js +55 -0
- data/lib/samagotchi/web/public/plugin_chip_kind.js +48 -0
- data/lib/samagotchi/web/public/send_turn.js +8 -21
- data/lib/samagotchi/web/public/session_actions.js +10 -23
- data/lib/samagotchi/web/public/session_view.js +7 -3
- data/lib/samagotchi/web/public/stream_handlers.js +2 -0
- metadata +33 -1
|
@@ -11,9 +11,16 @@ module Samagotchi
|
|
|
11
11
|
# only when called, so a page that never calls it stays as light as the
|
|
12
12
|
# runner (spec/pages_runtime_spec.rb loads one to check).
|
|
13
13
|
#
|
|
14
|
-
# runtime.schedules
|
|
14
|
+
# runtime.schedules every durable schedule (ScheduleList)
|
|
15
|
+
# runtime.sessions(project:) a project's sessions over the last 14
|
|
16
|
+
# days, as counts (SessionStats::Listing)
|
|
17
|
+
# runtime.turn_excerpts(turns) the failing calls of given turns
|
|
18
|
+
# (TurnExcerpts::Result)
|
|
19
|
+
# runtime.launch_dir the folder `chi page run` was typed in
|
|
20
|
+
# runtime.settings a bundle page's own config.yml settings
|
|
15
21
|
#
|
|
16
|
-
# A method comes with its first page (docs/internals/pages.md).
|
|
22
|
+
# A method comes with its first page (docs/internals/pages.md). All but
|
|
23
|
+
# schedules are experimental.
|
|
17
24
|
class Runtime
|
|
18
25
|
# What a runtime read can't give: the run fails with its message.
|
|
19
26
|
class Unreadable < Error; end
|
|
@@ -68,6 +75,11 @@ module Samagotchi
|
|
|
68
75
|
def rows = entries.map(&:row)
|
|
69
76
|
end
|
|
70
77
|
|
|
78
|
+
# @param page [String, nil] the page this run is of (Runner's), for #settings
|
|
79
|
+
def initialize(page: nil)
|
|
80
|
+
@page = page
|
|
81
|
+
end
|
|
82
|
+
|
|
71
83
|
# @return [ScheduleList]
|
|
72
84
|
# @raise [Unreadable] schedules.json isn't readable JSON (Schedules::Corrupt's words)
|
|
73
85
|
def schedules
|
|
@@ -79,6 +91,54 @@ module Samagotchi
|
|
|
79
91
|
rescue Schedules::Corrupt => e
|
|
80
92
|
raise Unreadable, e.message
|
|
81
93
|
end
|
|
94
|
+
|
|
95
|
+
# A project's sessions over the last 14 days (capped by the
|
|
96
|
+
# retention), as counts: SessionStats.read's value as it is. Read-only,
|
|
97
|
+
# local files only.
|
|
98
|
+
# @param project [String, nil] a folder (its project is read); nil, or
|
|
99
|
+
# a folder in no project: the newest session you started's project
|
|
100
|
+
# @return [SessionStats::Listing]
|
|
101
|
+
def sessions(project: nil)
|
|
102
|
+
require_relative "../session_stats"
|
|
103
|
+
SessionStats.read(project: project)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The failing tool calls of the given turns (worst first), redacted
|
|
107
|
+
# and cut, about 4k tokens in all: TurnExcerpts.read's value as it is.
|
|
108
|
+
# Text from your sessions.
|
|
109
|
+
# @param turns [Array<Array(String, String)>] [session_id, turn_id] pairs
|
|
110
|
+
# @param include_text [Boolean] true adds each turn's prompt and answer
|
|
111
|
+
# @return [TurnExcerpts::Result]
|
|
112
|
+
def turn_excerpts(turns, include_text: false)
|
|
113
|
+
require_relative "../turn_excerpts"
|
|
114
|
+
TurnExcerpts.read(turns, include_text: include_text == true)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# The folder `chi page run` was typed in (absolute), nil when chi
|
|
118
|
+
# web's Refresh ran the page.
|
|
119
|
+
# @return [String, nil]
|
|
120
|
+
def launch_dir
|
|
121
|
+
dir = ENV.fetch(LAUNCH_DIR_KEY, nil)
|
|
122
|
+
dir.nil? || dir.empty? ? nil : dir
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# A bundle page's own settings: config.yml `bundles: <bundle>:`, only
|
|
126
|
+
# the keys its manifest lists in settings:, as written (the page checks
|
|
127
|
+
# their types). {} for a user's page (no config is loaded then), for a
|
|
128
|
+
# bundle with no settings: and when config.yml has none.
|
|
129
|
+
# @return [Hash{String => Object}] frozen
|
|
130
|
+
def settings
|
|
131
|
+
bundled = @page && Pages.bundled(@page)
|
|
132
|
+
return {}.freeze unless bundled
|
|
133
|
+
|
|
134
|
+
require_relative "../config"
|
|
135
|
+
require_relative "../installed_bundles"
|
|
136
|
+
keys = InstalledBundles.standing(bundled.bundle)&.record&.manifest&.settings
|
|
137
|
+
return {}.freeze if keys.nil? || keys.empty?
|
|
138
|
+
|
|
139
|
+
values = ConfigFile.bundle_settings[bundled.bundle] || {}
|
|
140
|
+
values.transform_keys(&:to_s).slice(*keys).freeze
|
|
141
|
+
end
|
|
82
142
|
end
|
|
83
143
|
end
|
|
84
144
|
end
|
data/lib/samagotchi/pages.rb
CHANGED
|
@@ -38,6 +38,10 @@ module Samagotchi
|
|
|
38
38
|
MAX_OUTPUT_BYTES = 256 * 1024
|
|
39
39
|
TIMEOUT_SECONDS = 30
|
|
40
40
|
FORMATS = %i[money count].freeze
|
|
41
|
+
# The env key a run gets the folder `chi page run` was typed in by
|
|
42
|
+
# (Launch sets it, Runtime#launch_dir reads it; unset for chi web's
|
|
43
|
+
# Refresh). Here, not in Launch: the runner never loads launch.rb.
|
|
44
|
+
LAUNCH_DIR_KEY = "CHI_PAGE_LAUNCH_DIR"
|
|
41
45
|
|
|
42
46
|
# Anything that keeps a page from running or from being stored. Each
|
|
43
47
|
# subclass owns its message.
|
|
@@ -53,8 +53,10 @@ module Samagotchi
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
# A `kitty @ ls` on +socket+ as panes: every window, its key
|
|
56
|
-
# (`<socket>#<window id>`), cleaned title, cwd
|
|
57
|
-
# foreground processes
|
|
56
|
+
# (`<socket>#<window id>`), cleaned title, cwd, the pids of its
|
|
57
|
+
# foreground processes, its user vars, and its tab's layout and
|
|
58
|
+
# focus (the tab's is_focused: the active tab of the focused OS
|
|
59
|
+
# window).
|
|
58
60
|
# @return [Array<Pane>]
|
|
59
61
|
def panes(ls, socket)
|
|
60
62
|
data = JSON.parse(ls.to_s)
|
|
@@ -62,19 +64,32 @@ module Samagotchi
|
|
|
62
64
|
|
|
63
65
|
data.flat_map do |os_window|
|
|
64
66
|
Array(os_window.is_a?(Hash) ? os_window["tabs"] : nil).flat_map do |tab|
|
|
65
|
-
|
|
67
|
+
next [] unless tab.is_a?(Hash)
|
|
68
|
+
|
|
69
|
+
Array(tab["windows"]).filter_map { |window| pane(window, socket, tab) }
|
|
66
70
|
end
|
|
67
71
|
end
|
|
68
72
|
rescue JSON::ParserError
|
|
69
73
|
[]
|
|
70
74
|
end
|
|
71
75
|
|
|
72
|
-
def pane(window, socket)
|
|
76
|
+
def pane(window, socket, tab = {})
|
|
73
77
|
return nil unless window.is_a?(Hash) && window["id"].is_a?(Integer)
|
|
74
78
|
|
|
75
79
|
pids = Array(window["foreground_processes"]).filter_map { |process| process["pid"] if process.is_a?(Hash) && process["pid"].is_a?(Integer) }
|
|
80
|
+
vars = window["user_vars"].is_a?(Hash) ? window["user_vars"].to_h { |name, value| [name.to_s, value.to_s] } : {}
|
|
76
81
|
Pane.new(driver: "kitty", key: "#{socket}##{window["id"]}", title: clean_title(window["title"].to_s),
|
|
77
|
-
cwd: window["cwd"].to_s, pids: pids
|
|
82
|
+
cwd: window["cwd"].to_s, pids: pids, vars: vars, tab_layout: tab["layout"].to_s,
|
|
83
|
+
tab_focused: tab["is_focused"] == true)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The OSC 1337 escape that sets user var +name+ on the window the
|
|
87
|
+
# escape is written to (its value base64, as kitty requires), or,
|
|
88
|
+
# with +value+ nil, unsets it (the no-`=` form; an empty value would
|
|
89
|
+
# leave the var set to "").
|
|
90
|
+
def user_var(name, value)
|
|
91
|
+
record = value.nil? ? name.to_s : "#{name}=#{[value.to_s].pack("m0")}"
|
|
92
|
+
"\e]1337;SetUserVar=#{record}\a"
|
|
78
93
|
end
|
|
79
94
|
|
|
80
95
|
# A title without the status glyph agents put in front ("✳ Claude Code").
|
data/lib/samagotchi/panes.rb
CHANGED
|
@@ -7,8 +7,9 @@ require_relative "panes/kitty"
|
|
|
7
7
|
module Samagotchi
|
|
8
8
|
# Panes: where an agent's own UI runs (a kitty window) and where the human
|
|
9
9
|
# can walk in. A pane carries no peer message. #list, #find a pid's
|
|
10
|
-
# window, #
|
|
11
|
-
#
|
|
10
|
+
# window, #find_var the window a session's attached TUI marked, #show it
|
|
11
|
+
# (focus-window), #open a background tab or a window next to another
|
|
12
|
+
# running a command, #alive? and #paste into it: show, open and paste act as the
|
|
12
13
|
# user on their screen, so ctx.panes runs them only under the user-typed
|
|
13
14
|
# seam (Plugin::Panes, UserTyped). One
|
|
14
15
|
# driver, kitty, through `kitty @ --to unix:<socket>`, with kitty's own
|
|
@@ -21,8 +22,11 @@ module Samagotchi
|
|
|
21
22
|
# Unavailable. See docs/internals/panes.md.
|
|
22
23
|
class Panes
|
|
23
24
|
# A window: its +driver+ ("kitty"), +key+ (`<socket>#<window id>`),
|
|
24
|
-
# +title+ (cleaned), +cwd
|
|
25
|
-
|
|
25
|
+
# +title+ (cleaned), +cwd+, the +pids+ of its foreground processes, its
|
|
26
|
+
# user +vars+ (name => value), its tab's +tab_layout+ ("fat",
|
|
27
|
+
# "splits", "stack"…; "" when ls names none) and +tab_focused+ (its
|
|
28
|
+
# tab is the focused OS window's active one).
|
|
29
|
+
Pane = Data.define(:driver, :key, :title, :cwd, :pids, :vars, :tab_layout, :tab_focused)
|
|
26
30
|
|
|
27
31
|
# A window #open made: what a caller keeps to show, check or paste
|
|
28
32
|
# into it later (claude-peer keeps it in its own session state, never in
|
|
@@ -46,6 +50,21 @@ module Samagotchi
|
|
|
46
50
|
def initialize(msg = "kitty isn't running or `kitty.listen_on` isn't set") = super
|
|
47
51
|
end
|
|
48
52
|
|
|
53
|
+
# A launch next to a window that kitty refused (exited non-zero: the
|
|
54
|
+
# window gone since the ls, a kitty without --next-to, anything): a
|
|
55
|
+
# caller may open a tab instead. Never raised for a timeout (a slow
|
|
56
|
+
# launch may still open its window).
|
|
57
|
+
class SplitFailed < Error
|
|
58
|
+
def initialize(msg = "kitty didn't open the window next to it") = super
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The user var an attached TUI (TerminalUI::AttachLauncher) sets on its
|
|
62
|
+
# own kitty window: "<session id>:<the TUI's pid>" (.session_mark).
|
|
63
|
+
SESSION_VAR = "chi_session"
|
|
64
|
+
|
|
65
|
+
# SESSION_VAR's value for +session_id+'s TUI running as +pid+.
|
|
66
|
+
def self.session_mark(session_id, pid) = "#{session_id}:#{pid}"
|
|
67
|
+
|
|
49
68
|
LS_SECONDS = 2
|
|
50
69
|
SHOW_SECONDS = 5
|
|
51
70
|
# A launch that takes long may still open its tab: it isn't killed at
|
|
@@ -82,6 +101,22 @@ module Samagotchi
|
|
|
82
101
|
list.find { |pane| pane.pids.include?(pid) }
|
|
83
102
|
end
|
|
84
103
|
|
|
104
|
+
# The window whose user var +name+ is "<+id+>:<pid>" (.session_mark)
|
|
105
|
+
# while that pid is still among its foreground processes (a TUI killed
|
|
106
|
+
# hard leaves its mark on a window that may run a shell now: never
|
|
107
|
+
# that one); the focused tab's first, else the first; nil for none.
|
|
108
|
+
# @return [Pane, nil]
|
|
109
|
+
def find_var(name, id)
|
|
110
|
+
return nil if id.to_s.empty?
|
|
111
|
+
|
|
112
|
+
pattern = /\A#{Regexp.escape(id.to_s)}:(\d+)\z/
|
|
113
|
+
marked = list.select do |pane|
|
|
114
|
+
found = pattern.match(pane.vars[name.to_s].to_s)
|
|
115
|
+
found && pane.pids.include?(Integer(found[1], 10))
|
|
116
|
+
end
|
|
117
|
+
marked.find(&:tab_focused) || marked.first
|
|
118
|
+
end
|
|
119
|
+
|
|
85
120
|
# Brings +pane+'s window to the front (its tab, its OS window).
|
|
86
121
|
# @return [Boolean] whether kitty did (false: the window is gone)
|
|
87
122
|
def show(pane)
|
|
@@ -95,13 +130,18 @@ module Samagotchi
|
|
|
95
130
|
# Opens a background tab (launch --type=tab --keep-focus: a tab only,
|
|
96
131
|
# an os-window's --keep-focus doesn't keep it) running +command+ (argv)
|
|
97
132
|
# in +cwd+, its tab titled +title+, in the kitty whose OS window is
|
|
98
|
-
# focused, else the one started last (Kitty.target).
|
|
133
|
+
# focused, else the one started last (Kitty.target). With +next_to+ (a
|
|
134
|
+
# Pane), a window in that pane's tab instead, on its own socket
|
|
135
|
+
# (#open_next_to); +title+ is then unused (no --title: it would freeze
|
|
136
|
+
# the title the command sets).
|
|
99
137
|
# @return [PaneRef]
|
|
100
138
|
# @raise [Unavailable] no kitty answers
|
|
139
|
+
# @raise [SplitFailed] kitty refused the window next to +next_to+
|
|
101
140
|
# @raise [Error] kitty didn't open it
|
|
102
|
-
def open(command:, cwd:, title:)
|
|
141
|
+
def open(command:, cwd:, title:, next_to: nil)
|
|
103
142
|
argv = Array(command).map(&:to_s)
|
|
104
143
|
raise ArgumentError, "no command to open" if argv.empty? || argv.first.empty?
|
|
144
|
+
return open_next_to(next_to, argv, cwd) if next_to
|
|
105
145
|
|
|
106
146
|
lists = Kitty.sockets(@listen_on, @env).filter_map do |socket|
|
|
107
147
|
out, ok = kitty(socket, ["ls"], LS_SECONDS)
|
|
@@ -145,6 +185,27 @@ module Samagotchi
|
|
|
145
185
|
|
|
146
186
|
private
|
|
147
187
|
|
|
188
|
+
# launch --type=window in +pane+'s tab (--match window_id: picks that
|
|
189
|
+
# tab: over remote control kitty ignores --next-to outside the target
|
|
190
|
+
# tab, the active one by default), next to it (--next-to id:), in a
|
|
191
|
+
# split when the tab's layout is splits, else the layout's slot after
|
|
192
|
+
# it (neighbor), keeping the focus. On +pane+'s own socket, checked as
|
|
193
|
+
# any ref is (#known).
|
|
194
|
+
def open_next_to(pane, argv, cwd)
|
|
195
|
+
socket, id = known(pane)
|
|
196
|
+
raise SplitFailed unless socket
|
|
197
|
+
|
|
198
|
+
location = pane.tab_layout == "splits" ? "split" : "neighbor"
|
|
199
|
+
out, status = run(socket, ["launch", "--type=window", "--match", "window_id:#{id}", "--next-to", "id:#{id}",
|
|
200
|
+
"--location=#{location}", "--keep-focus", "--cwd", cwd.to_s, *argv], LAUNCH_SECONDS)
|
|
201
|
+
raise SplitFailed if status == :failed
|
|
202
|
+
|
|
203
|
+
window = Integer(out.to_s.strip, 10, exception: false)
|
|
204
|
+
raise Error, "kitty didn't open the window" unless status == :ok && window
|
|
205
|
+
|
|
206
|
+
PaneRef.new(driver: "kitty", key: "#{socket}##{window}")
|
|
207
|
+
end
|
|
208
|
+
|
|
148
209
|
# [socket, window id] of +ref+ (a Pane or a PaneRef) when its socket is
|
|
149
210
|
# one of listen_on's own (Kitty.sockets), else nil: a ref read back from
|
|
150
211
|
# a file (a bundle's state.json, which chi's own tools can write) never
|
|
@@ -159,21 +220,29 @@ module Samagotchi
|
|
|
159
220
|
# [stdout, success] of `kitty @ --to unix:<socket> <args>`, killed past
|
|
160
221
|
# +seconds+; +stdin+ written to it first.
|
|
161
222
|
def kitty(socket, args, seconds, stdin: nil)
|
|
162
|
-
|
|
223
|
+
out, status = run(socket, args, seconds, stdin: stdin)
|
|
224
|
+
[out, status == :ok]
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# [stdout, status] of #kitty's run: status :ok, :failed (no binary, a
|
|
228
|
+
# non-zero exit, an IO error) or :timeout (killed past +seconds+).
|
|
229
|
+
def run(socket, args, seconds, stdin: nil)
|
|
230
|
+
return ["", :failed] if @binary.empty? || !File.executable?(@binary)
|
|
163
231
|
|
|
164
232
|
Open3.popen3(@env, @binary, "@", "--to", "unix:#{socket}", *args, unsetenv_others: true, pgroup: true) do |input, stdout, stderr, wait|
|
|
165
233
|
input.write(stdin) if stdin
|
|
166
234
|
input.close
|
|
167
|
-
|
|
168
|
-
Thread.new {
|
|
235
|
+
# A kill past +seconds+ closes the pipes under the readers: quietly.
|
|
236
|
+
reader = Thread.new { stdout.read }.tap { |thread| thread.report_on_exception = false }
|
|
237
|
+
Thread.new { stderr.read }.report_on_exception = false
|
|
169
238
|
unless wait.join(seconds)
|
|
170
239
|
kill(wait.pid)
|
|
171
|
-
return ["",
|
|
240
|
+
return ["", :timeout]
|
|
172
241
|
end
|
|
173
|
-
[reader.value.to_s, wait.value.success?]
|
|
242
|
+
[reader.value.to_s, wait.value.success? ? :ok : :failed]
|
|
174
243
|
end
|
|
175
244
|
rescue SystemCallError, IOError
|
|
176
|
-
["",
|
|
245
|
+
["", :failed]
|
|
177
246
|
end
|
|
178
247
|
|
|
179
248
|
def kill(pid)
|
|
@@ -7,7 +7,7 @@ require_relative "atomic_file"
|
|
|
7
7
|
require_relative "participant_messages"
|
|
8
8
|
require_relative "archive_store"
|
|
9
9
|
require_relative "session"
|
|
10
|
-
require_relative "
|
|
10
|
+
require_relative "session_chain/sharing"
|
|
11
11
|
require_relative "grants"
|
|
12
12
|
require_relative "participant_turn_mode"
|
|
13
13
|
|
|
@@ -124,11 +124,13 @@ module Samagotchi
|
|
|
124
124
|
identity = Grants.clean_identity(identity) unless identity.nil?
|
|
125
125
|
raise Grants::Unbindable if !identity.nil? && public_url&.identity_header.nil?
|
|
126
126
|
|
|
127
|
-
# Checked and issued under the session's transition lock shared
|
|
128
|
-
# continue archives the link and ends its
|
|
129
|
-
# one issued between the two would outlive
|
|
130
|
-
|
|
131
|
-
|
|
127
|
+
# Checked and issued under the session's transition lock shared
|
|
128
|
+
# (SessionChain::Sharing): a continue archives the link and ends its
|
|
129
|
+
# grants (end_sessions), so one issued between the two would outlive
|
|
130
|
+
# the archive. Refused while a transition holds it: naming who
|
|
131
|
+
# continues the session (#held_refusal), else the holder's Busy words.
|
|
132
|
+
issued = SessionChain::Sharing.under(session_id, state_dir: state_dir) do |sharing|
|
|
133
|
+
raise Grants::Invalid, held_refusal(sharing) if sharing
|
|
132
134
|
if ArchiveStore.archived?(Session.session_dir(session_id, state_dir: state_dir))
|
|
133
135
|
raise Grants::Invalid, "session #{session_id[0, 8]} is archived: unarchive it to invite anyone"
|
|
134
136
|
end
|
|
@@ -143,6 +145,15 @@ module Samagotchi
|
|
|
143
145
|
issued
|
|
144
146
|
end
|
|
145
147
|
|
|
148
|
+
# An invite's words for a held lock (SessionChain::Sharing's value):
|
|
149
|
+
# the session that continues it, under any holder (a continue's new
|
|
150
|
+
# link in flight: "in a moment", it may not be saved yet), else Busy's.
|
|
151
|
+
def held_refusal(sharing)
|
|
152
|
+
return sharing.busy.message if sharing.is_a?(SessionChain::Sharing::Held)
|
|
153
|
+
|
|
154
|
+
"#{sharing.refusal.message}: invite there#{" in a moment" if sharing.in_flight?}"
|
|
155
|
+
end
|
|
156
|
+
|
|
146
157
|
# Sets participant +id+'s cap on +session_id+ (chi share cap): the
|
|
147
158
|
# grant, the sidecar (the worker's check) and the audit line.
|
|
148
159
|
# @return [Grants::Grant, nil] nil when +id+ isn't an active grant there
|
|
@@ -75,9 +75,8 @@ module Samagotchi
|
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
def answer_row(entry)
|
|
78
|
-
verdict = entry["summary"].to_s.scan(PeerNote::VERDICT).last&.first
|
|
79
78
|
{ summary: entry["summary"], at: entry["at"], serial: entry["serial"],
|
|
80
|
-
verdict:
|
|
79
|
+
verdict: PeerNote.verdict_of(entry["summary"]) }.compact
|
|
81
80
|
end
|
|
82
81
|
end
|
|
83
82
|
end
|
data/lib/samagotchi/peer_note.rb
CHANGED
|
@@ -91,6 +91,9 @@ module Samagotchi
|
|
|
91
91
|
VERDICT = /\bVERDICT:?\s*([A-Z]+)(?![A-Za-z|])/
|
|
92
92
|
# The verdicts a note's peer hash names (.payload); another word is none.
|
|
93
93
|
VERDICTS = %w[OK CHANGES UNSURE].freeze
|
|
94
|
+
# JavaScript's \s (.said: peer_row.js summaryWithoutVerdict's own white
|
|
95
|
+
# space, so the two agree on any summary).
|
|
96
|
+
JS_SPACE = "\t\n\v\f\r \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
|
|
94
97
|
|
|
95
98
|
# The mailbox's note (a peer's message, PeerMailbox): its opening line,
|
|
96
99
|
# approved by the owner; its source; how its lines point at a message
|
|
@@ -180,7 +183,35 @@ module Samagotchi
|
|
|
180
183
|
|
|
181
184
|
# Whether a peer's answer agrees: the last verdict in its summary is
|
|
182
185
|
# OK. Its wake (when it asked) closes the round (AGREED_LINE).
|
|
183
|
-
def agrees?(summary) = summary
|
|
186
|
+
def agrees?(summary) = verdict_of(summary) == "OK"
|
|
187
|
+
|
|
188
|
+
# A peer's verdict: the last VERDICT mark in its +summary+, nil unless
|
|
189
|
+
# it is one of VERDICTS. The one parser of .payload, .agrees? and
|
|
190
|
+
# PeerExchanges' answer rows.
|
|
191
|
+
def verdict_of(summary)
|
|
192
|
+
verdict = summary.to_s.scan(VERDICT).last&.first
|
|
193
|
+
verdict if VERDICTS.include?(verdict)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# The +summary+ the peer row shows: without its last VERDICT mark when
|
|
197
|
+
# that mark is +verdict+ (.verdict_of's), the punctuation it leaves
|
|
198
|
+
# tidied, white space squashed. The port of peer_row.js
|
|
199
|
+
# summaryWithoutVerdict (kept for notes saved before .payload's "said");
|
|
200
|
+
# spec/fixtures/peer_said_cases.json holds the cases both run.
|
|
201
|
+
def said(summary, verdict)
|
|
202
|
+
text = summary.to_s
|
|
203
|
+
last = text.to_enum(:scan, VERDICT).map { ::Regexp.last_match }.last
|
|
204
|
+
return squash(text) unless last && verdict && last[1] == verdict
|
|
205
|
+
|
|
206
|
+
before = last.pre_match.sub(/[#{JS_SPACE}(\[]+\z/o, "")
|
|
207
|
+
after = last.post_match.sub(/\A[#{JS_SPACE})\]]*[,;.:—–-]?[#{JS_SPACE}]*/o, "")
|
|
208
|
+
joined = squash("#{before} #{after}").gsub(/\([#{JS_SPACE}]*\)|\[[#{JS_SPACE}]*\]/o, "")
|
|
209
|
+
squash(joined.sub(/[#{JS_SPACE},;:—–-]+\z/o, ""))
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# +text+'s white space runs as one space, none at its ends (JavaScript's
|
|
213
|
+
# replace(/\s+/g, " ").trim()).
|
|
214
|
+
def squash(text) = text.gsub(/[#{JS_SPACE}]+/o, " ").gsub(/\A +| +\z/, "")
|
|
184
215
|
|
|
185
216
|
# A peer's +text+ for its note: { text: } up to +cap+ (+held+: a note
|
|
186
217
|
# that doesn't wake, up to HELD_CHARS when that is less), { long:
|
|
@@ -196,12 +227,14 @@ module Samagotchi
|
|
|
196
227
|
# A peer's note's payload, whose answer it is (the UIs draw it; the
|
|
197
228
|
# model's content doesn't carry it): the peer's +peer+ and +label+, +re+
|
|
198
229
|
# (the send it names), +summary+, +serial+, +verdict+ (the summary's
|
|
199
|
-
# last, one of VERDICTS), +
|
|
200
|
-
#
|
|
230
|
+
# last, one of VERDICTS: .verdict_of), +said+ (the summary without that
|
|
231
|
+
# verdict, .said: the web's peer row; only with a verdict), +agreed+
|
|
232
|
+
# (.agrees?) and +inline+ (true: the note holds the text, .body); string
|
|
233
|
+
# keys, nils dropped.
|
|
201
234
|
def payload(peer:, label:, re:, summary:, serial:, inline: false)
|
|
202
|
-
verdict = summary
|
|
235
|
+
verdict = verdict_of(summary)
|
|
203
236
|
{ "peer" => peer, "label" => label, "re" => re, "summary" => summary, "serial" => serial,
|
|
204
|
-
"verdict" => (verdict if
|
|
237
|
+
"verdict" => verdict, "said" => (said(summary, verdict) if verdict), "agreed" => verdict == "OK",
|
|
205
238
|
"inline" => (true if inline) }.compact
|
|
206
239
|
end
|
|
207
240
|
end
|
|
@@ -29,6 +29,8 @@ module Samagotchi
|
|
|
29
29
|
PAIRED = "paired"
|
|
30
30
|
# How long a mark waits (owner decision 3: a constant).
|
|
31
31
|
WAIT_SECONDS = 600
|
|
32
|
+
# The longest product name a wait mark keeps (PeerWaiting's +app+).
|
|
33
|
+
APP_CHARS = 60
|
|
32
34
|
# A peer session's id as a file name: Claude Code's uuid, a Codex thread.
|
|
33
35
|
PEER_SESSION_RE = /\A[A-Za-z0-9][A-Za-z0-9-]{0,63}\z/
|
|
34
36
|
FILE_MAX_BYTES = 8192
|
|
@@ -55,17 +57,26 @@ module Samagotchi
|
|
|
55
57
|
end
|
|
56
58
|
end
|
|
57
59
|
|
|
58
|
-
# A chi +session+ that waits for a peer of its +project+ since +at
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
# A chi +session+ that waits for a peer of its +project+ since +at+;
|
|
61
|
+
# +app+: the peer's product name as its bundle words it ("Claude Code":
|
|
62
|
+
# chi web's waiting chip, which loads no plugins), nil from an older
|
|
63
|
+
# worker's mark.
|
|
64
|
+
PeerWaiting = Data.define(:session, :project, :at, :app) do
|
|
65
|
+
def initialize(session:, project:, at:, app: nil) = super
|
|
66
|
+
|
|
67
|
+
def to_h = { "session" => session, "project" => project, "at" => at, "app" => app }.compact
|
|
61
68
|
|
|
62
69
|
def self.from_h(data)
|
|
63
70
|
return nil unless data.is_a?(Hash) && SessionPaths.valid_id?(data["session"].to_s)
|
|
64
71
|
return nil unless [data["project"], data["at"]].all? { |value| value.nil? || value.is_a?(String) }
|
|
65
72
|
|
|
66
|
-
new(session: data["session"], project: data["project"], at: data["at"])
|
|
73
|
+
new(session: data["session"], project: data["project"], at: data["at"], app: app_of(data["app"]))
|
|
67
74
|
end
|
|
68
75
|
|
|
76
|
+
# +value+ as an +app+: a String of 1 to APP_CHARS characters, else
|
|
77
|
+
# none (any peer can write the folder).
|
|
78
|
+
def self.app_of(value) = (value if value.is_a?(String) && !value.empty? && value.length <= APP_CHARS)
|
|
79
|
+
|
|
69
80
|
# When the wait ends; nil for an unparsable +at+.
|
|
70
81
|
def until_at
|
|
71
82
|
Time.iso8601(at.to_s) + WAIT_SECONDS
|
|
@@ -161,12 +172,14 @@ module Samagotchi
|
|
|
161
172
|
false
|
|
162
173
|
end
|
|
163
174
|
|
|
164
|
-
# Session +session+ waits for +peer+ (written whole; again: a new wait)
|
|
175
|
+
# Session +session+ waits for +peer+ (written whole; again: a new wait),
|
|
176
|
+
# +app+ the peer's product name (PeerWaiting).
|
|
165
177
|
# @return [PeerWaiting]
|
|
166
|
-
def wait!(peer, session:, project:, state_dir: nil, now: Time.now)
|
|
178
|
+
def wait!(peer, session:, project:, app: nil, state_dir: nil, now: Time.now)
|
|
167
179
|
SessionPaths.check_id!(session)
|
|
168
180
|
dir = writable_dir(peer, WAITING, state_dir: state_dir)
|
|
169
|
-
mark = PeerWaiting.new(session: session, project: project, at: now.utc.iso8601
|
|
181
|
+
mark = PeerWaiting.new(session: session, project: project, at: now.utc.iso8601,
|
|
182
|
+
app: PeerWaiting.app_of(app))
|
|
170
183
|
AtomicFile.write(File.join(dir, "#{session}.json"), "#{JSON.generate(mark.to_h)}\n")
|
|
171
184
|
mark
|
|
172
185
|
end
|
|
@@ -74,7 +74,17 @@ module Samagotchi
|
|
|
74
74
|
"ctx.peers.release" => "0.194.0",
|
|
75
75
|
"ctx.peers.stop_waiting" => "0.194.0",
|
|
76
76
|
"ctx.panes" => "0.194.0",
|
|
77
|
-
"ctx.user_typed" => "0.194.0"
|
|
77
|
+
"ctx.user_typed" => "0.194.0",
|
|
78
|
+
"chi.tool(declare:)" => "0.195.0",
|
|
79
|
+
"ctx.project" => "0.195.0",
|
|
80
|
+
"ctx.sessions.list" => "0.195.0",
|
|
81
|
+
"ctx.sessions.present_ids" => "0.195.0",
|
|
82
|
+
"ctx.context.snapshots" => "0.195.0",
|
|
83
|
+
"ctx.panes.open(in:)" => "0.195.0",
|
|
84
|
+
"ctx.peers.wait(app:)" => "0.196.0",
|
|
85
|
+
"ctx.speaker" => "0.196.0",
|
|
86
|
+
"chi.tool(participant:)" => "0.196.0",
|
|
87
|
+
"ctx.chips" => "0.196.0"
|
|
78
88
|
}.freeze
|
|
79
89
|
|
|
80
90
|
# chi.on's on_error: values. :log (the default) logs a raise and goes
|
|
@@ -6,6 +6,8 @@ require_relative "../context_candidates_log"
|
|
|
6
6
|
require_relative "../context_sources"
|
|
7
7
|
require_relative "../context_providers"
|
|
8
8
|
require_relative "../session"
|
|
9
|
+
require_relative "../session_paths"
|
|
10
|
+
require_relative "project"
|
|
9
11
|
|
|
10
12
|
module Samagotchi
|
|
11
13
|
module Plugin
|
|
@@ -19,9 +21,12 @@ module Samagotchi
|
|
|
19
21
|
class Error < StandardError; end
|
|
20
22
|
|
|
21
23
|
# @param host [Host] session_id and state_dir
|
|
22
|
-
|
|
24
|
+
# @param project [#call, nil] → ctx.project (Project), whose sources
|
|
25
|
+
# #snapshots reads; nil: the host's folder's
|
|
26
|
+
def initialize(host, bundle:, project: nil)
|
|
23
27
|
@host = host
|
|
24
28
|
@bundle = bundle
|
|
29
|
+
@project = project
|
|
25
30
|
end
|
|
26
31
|
|
|
27
32
|
# Attach a source to this session. +url+: through an installed
|
|
@@ -161,10 +166,50 @@ module Samagotchi
|
|
|
161
166
|
end
|
|
162
167
|
end
|
|
163
168
|
|
|
169
|
+
# The latest texts of this project's sources and of the sources this
|
|
170
|
+
# project's sessions keep as their own (a session in no project: the
|
|
171
|
+
# sessions in none), project ones first, then by session id and name.
|
|
172
|
+
# Read-only: it never runs a source's command, and a source with no
|
|
173
|
+
# text yet is left out. Each snapshot file is read (up to
|
|
174
|
+
# ContextSources::TEXT_MAX_BYTES of text each), so call it from a
|
|
175
|
+
# background part, not every turn.
|
|
176
|
+
# @return [Array<Hash>] frozen {name:, scope:, session_id:, hint:,
|
|
177
|
+
# revision:, fetched_at:, text:} (session_id nil for a project's)
|
|
178
|
+
def snapshots
|
|
179
|
+
project = @project ? @project.call : Project.for(@host.cwd.call || Dir.pwd)
|
|
180
|
+
rows = []
|
|
181
|
+
rows.concat(texts(ContextSources.project_location(project.key, state_dir: state_dir), nil)) if project.key
|
|
182
|
+
dir = File.join(ContextSources.root(state_dir: state_dir), "sessions")
|
|
183
|
+
cache = {}
|
|
184
|
+
(Dir.exist?(dir) ? Dir.children(dir).sort : []).each do |id|
|
|
185
|
+
next unless SessionPaths.valid_id?(id) && project_session?(id, project.root, cache)
|
|
186
|
+
|
|
187
|
+
rows.concat(texts(ContextSources.session_location(id, state_dir: state_dir), id))
|
|
188
|
+
end
|
|
189
|
+
rows
|
|
190
|
+
end
|
|
191
|
+
|
|
164
192
|
private
|
|
165
193
|
|
|
166
194
|
def state_dir = @host.state_dir&.call || Session.default_state_dir
|
|
167
195
|
|
|
196
|
+
# Whether saved session +id+ is of +root+ (nil: of no project), not a
|
|
197
|
+
# test_run or scratch one.
|
|
198
|
+
def project_session?(id, root, cache)
|
|
199
|
+
summary = Session.summary_from_file(SessionPaths.session_file(id, state_dir: state_dir))
|
|
200
|
+
!summary.nil? && !summary.test_run && !summary.scratch && summary.project_root(cache: cache) == root
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def texts(location, session_id)
|
|
204
|
+
location.sources.sort_by(&:name).filter_map do |source|
|
|
205
|
+
snapshot = location.snapshot(source.name)
|
|
206
|
+
next unless snapshot.text?
|
|
207
|
+
|
|
208
|
+
{ name: source.name, scope: location.scope, session_id: session_id, hint: snapshot.hint || source.hint,
|
|
209
|
+
revision: snapshot.revision, fetched_at: snapshot.fetched_at, text: snapshot.text }.freeze
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
168
213
|
# Two attaches at once (a double click): the one that lost the race
|
|
169
214
|
# finds the source there and returns its name too.
|
|
170
215
|
def add(own, source)
|