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
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
require_relative "card"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
module Plugin
|
|
8
|
+
# ctx.chips (docs/plugins.md, Chips): a bundle's own chips in the web's
|
|
9
|
+
# session bar, after the peer chips and the sources. A chip is a label
|
|
10
|
+
# (with an age when it has a time), one of core's three looks, an
|
|
11
|
+
# optional dot, and a small card a tap opens: a markdown body and up to
|
|
12
|
+
# MAX_ACTIONS buttons, as a card's. Stored with the session
|
|
13
|
+
# (SessionChips), so chi web shows them with no worker running; a change
|
|
14
|
+
# announces :chips_updated and an open tab reads them again. The same
|
|
15
|
+
# chip again writes nothing. A bundle sees and changes only its own.
|
|
16
|
+
class Chips
|
|
17
|
+
# A source name's rules (ContextSources::NAME_RE): the strip's key is
|
|
18
|
+
# chip:<bundle>/<name>.
|
|
19
|
+
NAME = /\A[a-z0-9][a-z0-9-]{0,39}\z/
|
|
20
|
+
STATES = %i[ok warn error].freeze
|
|
21
|
+
MAX_LABEL = 40
|
|
22
|
+
MAX_TITLE = 200
|
|
23
|
+
MAX_BODY = 4_000
|
|
24
|
+
MAX_ACTIONS = 4
|
|
25
|
+
MAX_CHIPS = 3
|
|
26
|
+
|
|
27
|
+
# @param bundle [String]
|
|
28
|
+
# @param store [#call] → the session's store (SessionChips; the test
|
|
29
|
+
# kit's SessionChips::Memory), nil with no session yet
|
|
30
|
+
def initialize(bundle, store)
|
|
31
|
+
@bundle = bundle.to_s
|
|
32
|
+
@store = store
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def inspect = "#<#{self.class.name} bundle=#{@bundle}>"
|
|
36
|
+
|
|
37
|
+
# Set chip +name+ (a new one, or a change to it).
|
|
38
|
+
# @param label [String] one line, at most MAX_LABEL characters (cut)
|
|
39
|
+
# @param state [Symbol] :ok, :warn or :error
|
|
40
|
+
# @param dot [Boolean] a dot on the chip
|
|
41
|
+
# @param title [String, nil] its tooltip
|
|
42
|
+
# @param at [Time, String, nil] a time the web shows as an age that
|
|
43
|
+
# ticks (" · 3m")
|
|
44
|
+
# @param body [String] the card's text, markdown as a card's
|
|
45
|
+
# @param actions [Array<Hash>] a card's actions (Card.build_action),
|
|
46
|
+
# at most MAX_ACTIONS
|
|
47
|
+
# @return [Boolean] whether it changed anything (false: the same chip
|
|
48
|
+
# again, or no session yet)
|
|
49
|
+
# @raise [ArgumentError] a bad name, label, state, time or action; more
|
|
50
|
+
# than MAX_ACTIONS actions; a chip past MAX_CHIPS for the bundle
|
|
51
|
+
def set(name, label:, state: :ok, dot: false, title: nil, at: nil, body: "", actions: [])
|
|
52
|
+
name = check_name(name)
|
|
53
|
+
chip = self.class.build(label: label, state: state, dot: dot, title: title, at: at, body: body, actions: actions)
|
|
54
|
+
store = @store.call or return false
|
|
55
|
+
|
|
56
|
+
store.update(@bundle) do |mine|
|
|
57
|
+
if !mine.key?(name) && mine.size >= MAX_CHIPS
|
|
58
|
+
raise ArgumentError, "a bundle has at most #{MAX_CHIPS} chips (#{@bundle}: #{mine.keys.join(", ")})"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
mine.merge(name => chip)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @return [Boolean] whether there was one
|
|
66
|
+
def remove(name)
|
|
67
|
+
name = check_name(name)
|
|
68
|
+
store = @store.call or return false
|
|
69
|
+
|
|
70
|
+
store.update(@bundle) { |mine| mine.except(name) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @return [Hash{String => Hash}] this bundle's chips now, by name
|
|
74
|
+
# (symbol keys: label, state, dot, title, at, body, actions, set_at)
|
|
75
|
+
def list
|
|
76
|
+
store = @store.call or return {}
|
|
77
|
+
|
|
78
|
+
(store.all[@bundle] || {}).transform_values { |chip| chip.transform_keys(&:to_sym) }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A chip as stored (string keys, without its stamp).
|
|
82
|
+
# @raise [ArgumentError] as #set
|
|
83
|
+
def self.build(label:, state:, dot:, title:, at:, body:, actions:)
|
|
84
|
+
label = Card.one_line(label, MAX_LABEL)
|
|
85
|
+
raise ArgumentError, "a chip needs a label" if label.empty?
|
|
86
|
+
|
|
87
|
+
state = state.to_s.to_sym
|
|
88
|
+
raise ArgumentError, "a chip's state is one of #{STATES.join(", ")}" unless STATES.include?(state)
|
|
89
|
+
raise ArgumentError, "a chip's dot: is true or false" unless [true, false].include?(dot)
|
|
90
|
+
|
|
91
|
+
body = body.to_s
|
|
92
|
+
raise ArgumentError, "a chip's body is at most #{MAX_BODY} characters" if body.length > MAX_BODY
|
|
93
|
+
|
|
94
|
+
actions = Array(actions)
|
|
95
|
+
raise ArgumentError, "a chip has at most #{MAX_ACTIONS} actions" if actions.size > MAX_ACTIONS
|
|
96
|
+
|
|
97
|
+
title = title.nil? ? nil : Card.one_line(title, MAX_TITLE)
|
|
98
|
+
{ "label" => label, "state" => state.to_s, "dot" => dot, "title" => title.to_s.empty? ? nil : title,
|
|
99
|
+
"at" => time(at), "body" => body,
|
|
100
|
+
"actions" => actions.map { |action| Card.build_action(action).transform_keys(&:to_s) } }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @return [String, nil] +at+ as UTC ISO 8601
|
|
104
|
+
def self.time(at)
|
|
105
|
+
case at
|
|
106
|
+
when nil then nil
|
|
107
|
+
when Time then at.utc.iso8601
|
|
108
|
+
when String then Time.iso8601(at).utc.iso8601
|
|
109
|
+
else raise ArgumentError, "a chip's at: is a Time or an ISO 8601 string"
|
|
110
|
+
end
|
|
111
|
+
rescue ArgumentError
|
|
112
|
+
raise ArgumentError, "a chip's at: is a Time or an ISO 8601 string, not #{at.to_s[0, 40].inspect}"
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def check_name(name)
|
|
118
|
+
name = name.to_s
|
|
119
|
+
raise ArgumentError, "a chip's name is lowercase letters, digits and -, at most 40: #{name[0, 40].inspect}" unless NAME.match?(name)
|
|
120
|
+
|
|
121
|
+
name
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -11,6 +11,7 @@ require_relative "../idle_client"
|
|
|
11
11
|
require_relative "../tool_ids"
|
|
12
12
|
require_relative "../cache_confirm"
|
|
13
13
|
require_relative "../user_typed"
|
|
14
|
+
require_relative "../speaker"
|
|
14
15
|
require_relative "service"
|
|
15
16
|
require_relative "side_question"
|
|
16
17
|
|
|
@@ -40,7 +41,11 @@ module Samagotchi
|
|
|
40
41
|
autoload :HttpCancelled, File.expand_path("http_errors", __dir__)
|
|
41
42
|
autoload :Judgments, File.expand_path("judgments", __dir__)
|
|
42
43
|
autoload :Secrets, File.expand_path("secrets", __dir__)
|
|
44
|
+
autoload :Chips, File.expand_path("chips", __dir__)
|
|
43
45
|
autoload :SignIn, File.expand_path("sign_in", __dir__)
|
|
46
|
+
# ctx.project's value: it reads the guardrails' denylist, which a
|
|
47
|
+
# plugin that never asks (and the bundle test kit) doesn't load.
|
|
48
|
+
autoload :Project, File.expand_path("project", __dir__)
|
|
44
49
|
|
|
45
50
|
# The Engine's side of a Context: callables, so the Engine itself is
|
|
46
51
|
# never handed out. +messages+ returns the conversation, +notify+ takes
|
|
@@ -89,12 +94,15 @@ module Samagotchi
|
|
|
89
94
|
# (Engine#start_sign_in; the test kit's opens no port).
|
|
90
95
|
# +turn_interactive+ returns whether the running turn came from a screen
|
|
91
96
|
# that can answer a question (ClientId.interactive?: a web tab, a TUI).
|
|
97
|
+
# +chips+ returns the session's chip store ctx.chips writes through
|
|
98
|
+
# (SessionChips, announcing :chips_updated; the test kit's in memory),
|
|
99
|
+
# nil with no session yet.
|
|
92
100
|
Host = Struct.new(:session_id, :cwd, :messages, :messages_partial, :notify, :ask_user, :cancelled, :card,
|
|
93
101
|
:ask_model, :model_name, :model_key, :state_dir, :scratch, :setup, :prompt_files,
|
|
94
102
|
:steer, :stop_turn, :stop_generation, :frontend, :toolkit, :declared_tools, :use_toolkit, :load_tools,
|
|
95
103
|
:cancel_controller, :cost, :usage, :turn_author, :cache_confirm, :model_names,
|
|
96
104
|
:candidate_proposed, :judgments, :turn_running, :current_cancel, :secrets_store,
|
|
97
|
-
:sign_in, :turn_interactive, keyword_init: true)
|
|
105
|
+
:sign_in, :turn_interactive, :chips, keyword_init: true)
|
|
98
106
|
|
|
99
107
|
# ctx.ask_model failed: the model couldn't be reached, timed out, or
|
|
100
108
|
# sent nothing usable. The message says why, for the user.
|
|
@@ -341,6 +349,19 @@ module Samagotchi
|
|
|
341
349
|
# @return [String, nil] the git checkout holding #cwd
|
|
342
350
|
def repo_root = @git.root(cwd)
|
|
343
351
|
|
|
352
|
+
# The project #cwd belongs to, as chi's memories find it: its kind,
|
|
353
|
+
# root, memories key and dirs, chi's own dirs and the secret checks a
|
|
354
|
+
# reader of files outside the session's folder needs (Plugin::Project).
|
|
355
|
+
# Looked up again when #cwd changes.
|
|
356
|
+
# @return [Project]
|
|
357
|
+
def project
|
|
358
|
+
dir = cwd
|
|
359
|
+
cached = @project
|
|
360
|
+
return cached.last if cached&.first == dir
|
|
361
|
+
|
|
362
|
+
Project.for(dir, env: @env).tap { |found| @project = [dir, found] }
|
|
363
|
+
end
|
|
364
|
+
|
|
344
365
|
# $XDG_STATE_HOME/samagotchi/plugins/<bundle>/, created on first use.
|
|
345
366
|
# @return [String]
|
|
346
367
|
def data_dir
|
|
@@ -367,6 +388,12 @@ module Samagotchi
|
|
|
367
388
|
Array(@host.messages.call).map { |message| message.dup.freeze }.freeze
|
|
368
389
|
end
|
|
369
390
|
|
|
391
|
+
# Who wrote +message+ (an entry of #messages or ctx.sessions.read):
|
|
392
|
+
# :owner, :participant, :chi, :sender, :plugin, :model or :tool; nil
|
|
393
|
+
# for a non-Hash (Speaker.of). The message's keys stay chi's own.
|
|
394
|
+
# @return [Symbol, nil]
|
|
395
|
+
def speaker(message) = Speaker.of(message)
|
|
396
|
+
|
|
370
397
|
# The newest answer's text as the user saw it (AnswerText.visible): the
|
|
371
398
|
# newest bubble of #messages, its thoughts (a cut-off one too) and call
|
|
372
399
|
# markup out. Mid-turn a worker's can be the running turn's interim text.
|
|
@@ -544,10 +571,10 @@ module Samagotchi
|
|
|
544
571
|
end
|
|
545
572
|
|
|
546
573
|
# Other sessions: fork one from a conversation, send one a message,
|
|
547
|
-
# read one (Sessions).
|
|
574
|
+
# read one, list this project's (Sessions).
|
|
548
575
|
# @return [Sessions]
|
|
549
576
|
def sessions
|
|
550
|
-
@sessions ||= Sessions.new(@host)
|
|
577
|
+
@sessions ||= Sessions.new(@host, project: -> { project })
|
|
551
578
|
end
|
|
552
579
|
|
|
553
580
|
# The bundle's secrets: tokens kept apart from its data files
|
|
@@ -557,6 +584,13 @@ module Samagotchi
|
|
|
557
584
|
@secrets ||= Secrets.new(@host.secrets_store.call(@bundle))
|
|
558
585
|
end
|
|
559
586
|
|
|
587
|
+
# The bundle's own chips in the web's session bar (Chips,
|
|
588
|
+
# docs/plugins.md): set, remove, list.
|
|
589
|
+
# @return [Chips]
|
|
590
|
+
def chips
|
|
591
|
+
@chips ||= Chips.new(@bundle, -> { @host.chips&.call })
|
|
592
|
+
end
|
|
593
|
+
|
|
560
594
|
# Send the session's owner to an address and get back the address the
|
|
561
595
|
# browser was sent to (SignIn, docs/plugins.md, Sign-in): a card with
|
|
562
596
|
# +button+, a loopback listener, /callback for a browser elsewhere.
|
|
@@ -594,18 +628,19 @@ module Samagotchi
|
|
|
594
628
|
cancel_lookup: -> { http_cancel_controller })
|
|
595
629
|
end
|
|
596
630
|
|
|
597
|
-
# This session's attached context: attach a URL or a command, list
|
|
598
|
-
# (AttachedContext).
|
|
631
|
+
# This session's attached context: attach a URL or a command, list,
|
|
632
|
+
# read this project's latest texts (AttachedContext).
|
|
599
633
|
# @return [AttachedContext]
|
|
600
634
|
def context
|
|
601
|
-
@context ||= AttachedContext.new(@host, bundle: @bundle)
|
|
635
|
+
@context ||= AttachedContext.new(@host, bundle: @bundle, project: -> { project })
|
|
602
636
|
end
|
|
603
637
|
|
|
604
638
|
# The panes where agents' own UIs run (Samagotchi::Panes: kitty
|
|
605
|
-
# windows), read now from the config
|
|
639
|
+
# windows), read now from the config; #open(in: :pane) opens next to
|
|
640
|
+
# this session's attached chi terminal. #show moves the user's screen,
|
|
606
641
|
# so it runs only in a command they typed (#user_typed?).
|
|
607
642
|
# @return [Panes]
|
|
608
|
-
def panes = Panes.new
|
|
643
|
+
def panes = Panes.new(session_id: session_id)
|
|
609
644
|
|
|
610
645
|
# This session's peers: a pairing and a peer's messages (Peers).
|
|
611
646
|
# @return [Peers]
|
|
@@ -8,14 +8,31 @@ module Samagotchi
|
|
|
8
8
|
# ctx.panes (docs/plugins.md, Panes): the kitty windows agents run in
|
|
9
9
|
# (Samagotchi::Panes, its config read at each call). #find a pid's
|
|
10
10
|
# window and its title; #alive? whether a window #open made is still
|
|
11
|
-
# there. #show brings one to the front, #open opens a
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# call or chi
|
|
11
|
+
# there. #show brings one to the front, #open opens a pane next to
|
|
12
|
+
# this session's attached chi terminal or a background tab, and #paste
|
|
13
|
+
# types into one: they act as the user on their screen, so only in a
|
|
14
|
+
# command a person typed at a UI (UserTyped: never a tool call or chi
|
|
15
|
+
# send).
|
|
15
16
|
class Panes
|
|
16
17
|
# Show, open or paste from anything but a command the user typed.
|
|
17
18
|
class NotTyped < StandardError; end
|
|
18
19
|
|
|
20
|
+
# What #open opened: +kind+ :pane (a window next to this session's
|
|
21
|
+
# chi terminal) or :tab (a background tab), and +why+ a tab when a
|
|
22
|
+
# pane was asked for (nil otherwise), in words for the user.
|
|
23
|
+
Placed = Data.define(:kind, :why)
|
|
24
|
+
|
|
25
|
+
# The words of Placed#why.
|
|
26
|
+
NO_TERMINAL = "no chi terminal of this session in kitty"
|
|
27
|
+
STACKED = "this chi's kitty tab is in the stack layout, which would hide it"
|
|
28
|
+
SPLIT_FAILED = "kitty couldn't open it next to this chi"
|
|
29
|
+
|
|
30
|
+
# @param session_id [String, nil] the session whose attached terminal
|
|
31
|
+
# a pane opens next to (Samagotchi::Panes::SESSION_VAR)
|
|
32
|
+
def initialize(session_id: nil)
|
|
33
|
+
@session_id = session_id
|
|
34
|
+
end
|
|
35
|
+
|
|
19
36
|
# @return [Samagotchi::Panes::Pane, nil] the window +pid+ runs in
|
|
20
37
|
def find(pid:) = Samagotchi::Panes.from_config.find(pid: pid)
|
|
21
38
|
|
|
@@ -27,15 +44,27 @@ module Samagotchi
|
|
|
27
44
|
Samagotchi::Panes.from_config.show(pane)
|
|
28
45
|
end
|
|
29
46
|
|
|
30
|
-
# A
|
|
31
|
-
# +title
|
|
47
|
+
# A kitty window running +command+ (argv) in +cwd+: with +in+ :tab
|
|
48
|
+
# (the default) a background tab titled +title+; with :pane a window
|
|
49
|
+
# next to this session's attached chi TUI (the window it marked,
|
|
50
|
+
# Samagotchi::Panes#find_var), in its tab, else a background tab: no
|
|
51
|
+
# such window, its tab in the stack layout, or kitty refusing the
|
|
52
|
+
# launch next to it (never after a launch that timed out: it may
|
|
53
|
+
# still open). Yields a Placed, when given a block, for the words.
|
|
32
54
|
# @return [Samagotchi::Panes::PaneRef]
|
|
55
|
+
# @raise [ArgumentError] +in+ isn't :pane or :tab
|
|
33
56
|
# @raise [NotTyped] not in a command the user typed
|
|
34
57
|
# @raise [Samagotchi::Panes::Error] no kitty answers (Unavailable), or it didn't open it
|
|
35
|
-
def open(command:, cwd:, title:)
|
|
36
|
-
|
|
58
|
+
def open(command:, cwd:, title:, in: :tab)
|
|
59
|
+
where = binding.local_variable_get(:in)
|
|
60
|
+
raise ArgumentError, "in: is :pane or :tab, not #{where.inspect}" unless %i[pane tab].include?(where)
|
|
61
|
+
raise NotTyped, "it opens a kitty window on your screen, so it runs only when you tap it or type it" unless UserTyped.typed?
|
|
37
62
|
|
|
38
|
-
Samagotchi::Panes.from_config
|
|
63
|
+
panes = Samagotchi::Panes.from_config
|
|
64
|
+
ref, placed = where == :pane ? beside_chi(panes, command, cwd, title) : nil
|
|
65
|
+
ref ||= panes.open(command: command, cwd: cwd, title: title)
|
|
66
|
+
yield(placed || Placed.new(kind: :tab, why: nil)) if block_given?
|
|
67
|
+
ref
|
|
39
68
|
end
|
|
40
69
|
|
|
41
70
|
# Whether +ref+'s window is still there.
|
|
@@ -49,6 +78,20 @@ module Samagotchi
|
|
|
49
78
|
|
|
50
79
|
Samagotchi::Panes.from_config.paste(ref, text, enter: enter)
|
|
51
80
|
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
# [ref, Placed] of a pane next to the session's chi TUI, or [nil,
|
|
85
|
+
# Placed] (a tab, and why) when it can't be.
|
|
86
|
+
def beside_chi(panes, command, cwd, title)
|
|
87
|
+
chi = panes.find_var(Samagotchi::Panes::SESSION_VAR, @session_id) if @session_id
|
|
88
|
+
return [nil, Placed.new(kind: :tab, why: NO_TERMINAL)] unless chi
|
|
89
|
+
return [nil, Placed.new(kind: :tab, why: STACKED)] if chi.tab_layout == "stack"
|
|
90
|
+
|
|
91
|
+
[panes.open(command: command, cwd: cwd, title: title, next_to: chi), Placed.new(kind: :pane, why: nil)]
|
|
92
|
+
rescue Samagotchi::Panes::SplitFailed
|
|
93
|
+
[nil, Placed.new(kind: :tab, why: SPLIT_FAILED)]
|
|
94
|
+
end
|
|
52
95
|
end
|
|
53
96
|
end
|
|
54
97
|
end
|
|
@@ -55,7 +55,7 @@ module Samagotchi
|
|
|
55
55
|
def pair_or_wait(ctx)
|
|
56
56
|
peer = self.class
|
|
57
57
|
unless announces?
|
|
58
|
-
ctx.peers.wait(peer::PEER)
|
|
58
|
+
ctx.peers.wait(peer::PEER, app: peer::APP)
|
|
59
59
|
return "waiting for a #{peer::APP} session (#{wait_minutes} min): say \"pair with chi\" in #{peer::NAME} " \
|
|
60
60
|
"(#{peer::NAME} doesn't list itself here)"
|
|
61
61
|
end
|
|
@@ -63,7 +63,7 @@ module Samagotchi
|
|
|
63
63
|
here = ctx.peers.here(peer::PEER)
|
|
64
64
|
return pair_with(ctx, here.first) if here.size == 1
|
|
65
65
|
|
|
66
|
-
ctx.peers.wait(peer::PEER)
|
|
66
|
+
ctx.peers.wait(peer::PEER, app: peer::APP)
|
|
67
67
|
return "waiting for a #{peer::APP} session: start one in this project (#{wait_minutes} min)" if here.empty?
|
|
68
68
|
|
|
69
69
|
here_card(ctx, here)
|
|
@@ -13,29 +13,37 @@ module Samagotchi
|
|
|
13
13
|
module Plugin
|
|
14
14
|
# `/<peer> start` (docs/peers.md, Start one in kitty; the peer-handshake
|
|
15
15
|
# mock-up's scenes 3 and 5): chi writes this session's wait, then opens a
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
16
|
+
# kitty window in the session's cwd running the bundle setting
|
|
17
|
+
# `start_command` (default the peer's own command), through the user's
|
|
18
|
+
# login shell: with the bundle setting `open_in` pane (the default) a
|
|
19
|
+
# pane next to this session's attached chi terminal, else (none, or
|
|
20
|
+
# `open_in: tab`) a background tab titled `<peer> · chi <id8>`
|
|
21
|
+
# (Plugin::Panes#open). claude-peer's Claude pairs
|
|
19
22
|
# on its own (its start hook takes the wait); codex-peer's Codex gets a
|
|
20
23
|
# first prompt, "pair with chi" (#start_prompt), which runs its skill's
|
|
21
24
|
# no-id pair and so spends one Codex turn (with codex-peer's start hook,
|
|
22
25
|
# 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
|
|
24
|
-
# their screen: ctx.panes.open raises otherwise); a second start while
|
|
25
|
-
# the wait is open and its
|
|
26
|
-
#
|
|
26
|
+
# paired; with or without the hook, #start_needs_hook?). Only in a command the user typed or tapped (it opens a window
|
|
27
|
+
# on their screen: ctx.panes.open raises otherwise); a second start while
|
|
28
|
+
# the wait is open and its window is there is refused (a double tap). The
|
|
29
|
+
# window's PaneRef is kept in the bundle's own session state (state.json,
|
|
27
30
|
# ctx.session_dir: removed with the session), never in the mailbox, and
|
|
28
31
|
# checked on each use: Panes takes a ref only on a socket kitty.listen_on
|
|
29
32
|
# names, and a paired session's Show only the window its peer runs in.
|
|
30
|
-
# `/<peer> show` alone brings that
|
|
31
|
-
# says "started by chi" with Show while it is open, and "its kitty
|
|
32
|
-
# is closed" with Start one in kitty after.
|
|
33
|
+
# `/<peer> show` alone brings that window to the front; the status card
|
|
34
|
+
# says "started by chi" with Show while it is open, and "its kitty
|
|
35
|
+
# window is closed" with Start one in kitty after. Later lines say
|
|
36
|
+
# "kitty window" for a pane and a tab alike; only the start line says
|
|
37
|
+
# which opened.
|
|
33
38
|
module PeerStart
|
|
34
39
|
STATE_KEY = "pane"
|
|
35
40
|
LOCK_FILE = "start.lock"
|
|
36
|
-
# How long after the launch the
|
|
37
|
-
# command that isn't found closes its
|
|
41
|
+
# How long after the launch the window is checked once more: a start
|
|
42
|
+
# command that isn't found closes its window at once.
|
|
38
43
|
SETTLE_SECONDS = 1.5
|
|
44
|
+
# The bundle setting open_in's values (another means pane, and the
|
|
45
|
+
# start line names it).
|
|
46
|
+
OPEN_IN = { "pane" => :pane, "tab" => :tab }.freeze
|
|
39
47
|
|
|
40
48
|
class << self
|
|
41
49
|
# SETTLE_SECONDS, unless set (specs: 0).
|
|
@@ -58,7 +66,7 @@ module Samagotchi
|
|
|
58
66
|
# /<peer> start
|
|
59
67
|
def start_here(ctx)
|
|
60
68
|
peer = self.class
|
|
61
|
-
return "#{peer::COMMAND} start: it opens a kitty
|
|
69
|
+
return "#{peer::COMMAND} start: it opens a kitty window on your screen, so it runs only when you tap it or type it" unless ctx.user_typed?
|
|
62
70
|
return pairing_now if ctx.peers.claimed?(peer::PEER)
|
|
63
71
|
|
|
64
72
|
link = ctx.peers.link(peer::PEER)
|
|
@@ -66,7 +74,7 @@ module Samagotchi
|
|
|
66
74
|
|
|
67
75
|
refused = start_hook_refusal and return refused
|
|
68
76
|
# Two starts at once (commands run in parallel: a double tap) take
|
|
69
|
-
# turns, so the second sees the first's wait and
|
|
77
|
+
# turns, so the second sees the first's wait and window.
|
|
70
78
|
start_lock(ctx) do
|
|
71
79
|
starting = already_starting(ctx) and next starting
|
|
72
80
|
gone = end_gone_pairing(ctx)
|
|
@@ -98,26 +106,28 @@ module Samagotchi
|
|
|
98
106
|
end
|
|
99
107
|
end
|
|
100
108
|
|
|
101
|
-
# A start while this session waits and the
|
|
102
|
-
# double tap): the line that says so, else nil.
|
|
109
|
+
# A start while this session waits and the window it opened is there
|
|
110
|
+
# (a double tap): the line that says so, else nil.
|
|
103
111
|
def already_starting(ctx)
|
|
104
112
|
peer = self.class
|
|
105
113
|
mark = ctx.peers.waiting(peer::PEER)
|
|
106
114
|
started = started_pane(ctx)
|
|
107
115
|
return nil unless mark && !mark.stale? && started && ctx.panes.alive?(started[:ref])
|
|
108
116
|
|
|
109
|
-
"#{peer::COMMAND} start: already starting
|
|
117
|
+
"#{peer::COMMAND} start: already starting #{started[:title]} in kitty (#{peer::COMMAND} show brings it to the front)"
|
|
110
118
|
end
|
|
111
119
|
|
|
112
|
-
# The wait (an open one kept: /<peer> pair's), then the
|
|
113
|
-
# didn't open takes back only the wait this start wrote.
|
|
120
|
+
# The wait (an open one kept: /<peer> pair's), then the window; a
|
|
121
|
+
# window that didn't open takes back only the wait this start wrote.
|
|
114
122
|
def open_started(ctx)
|
|
115
123
|
peer = self.class
|
|
116
124
|
open = ctx.peers.waiting(peer::PEER)
|
|
117
|
-
written = ctx.peers.wait(peer::PEER) unless open && !open.stale?
|
|
125
|
+
written = ctx.peers.wait(peer::PEER, app: peer::APP) unless open && !open.stale?
|
|
118
126
|
title = started_title(ctx)
|
|
127
|
+
where, unknown = open_in(ctx)
|
|
128
|
+
placed = nil
|
|
119
129
|
begin
|
|
120
|
-
ref = ctx.panes.open(command: start_argv(ctx), cwd: ctx.cwd, title: title)
|
|
130
|
+
ref = ctx.panes.open(command: start_argv(ctx), cwd: ctx.cwd, title: title, in: where) { |got| placed = got }
|
|
121
131
|
rescue Samagotchi::Panes::Error, Plugin::Panes::NotTyped => e
|
|
122
132
|
ctx.peers.stop_waiting(peer::PEER, written) if written
|
|
123
133
|
return "#{peer::COMMAND} start: #{e.message}"
|
|
@@ -125,10 +135,19 @@ module Samagotchi
|
|
|
125
135
|
return closed_at_once(ctx, written) if closed_at_once?(ctx, ref)
|
|
126
136
|
|
|
127
137
|
save_started(ctx, ref, title)
|
|
128
|
-
opened_line(ctx, title)
|
|
138
|
+
opened_line(ctx, title, placed, unknown)
|
|
129
139
|
end
|
|
130
140
|
|
|
131
|
-
#
|
|
141
|
+
# [:pane or :tab, the setting's value when it is neither (nil
|
|
142
|
+
# otherwise)] of the bundle setting open_in (unset: pane).
|
|
143
|
+
def open_in(ctx)
|
|
144
|
+
value = ctx.settings["open_in"].to_s.strip
|
|
145
|
+
return [:pane, nil] if value.empty?
|
|
146
|
+
|
|
147
|
+
OPEN_IN.key?(value) ? [OPEN_IN[value], nil] : [:pane, value]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The window gone SETTLE_SECONDS after it opened (its command not found
|
|
132
151
|
# in the login shell's PATH, or failing at once).
|
|
133
152
|
def closed_at_once?(ctx, ref)
|
|
134
153
|
delay = PeerStart.settle_seconds
|
|
@@ -140,13 +159,18 @@ module Samagotchi
|
|
|
140
159
|
peer = self.class
|
|
141
160
|
ctx.peers.stop_waiting(peer::PEER, written) if written
|
|
142
161
|
shell = File.basename(login_shell)
|
|
143
|
-
"#{peer::COMMAND} start: the
|
|
162
|
+
"#{peer::COMMAND} start: the window closed at once: check bundles.#{peer::BUNDLE}.start_command (it runs as " \
|
|
144
163
|
"`#{shell} -l -c \"exec #{start_command(ctx)}\"`, so it needs that login shell's PATH)"
|
|
145
164
|
end
|
|
146
165
|
|
|
147
|
-
|
|
166
|
+
# What opened (Plugin::Panes::Placed: a pane, or a tab and why) and
|
|
167
|
+
# what it runs; an open_in value that isn't pane or tab named first.
|
|
168
|
+
def opened_line(ctx, title, placed, unknown)
|
|
148
169
|
peer = self.class
|
|
149
|
-
|
|
170
|
+
where = placed&.kind == :pane ? "a pane next to this chi" : "a background tab \"#{title}\""
|
|
171
|
+
where = "#{where} (#{placed.why})" if placed&.why
|
|
172
|
+
line = "opened #{where} running `#{start_command(ctx)}`"
|
|
173
|
+
line = "open_in \"#{unknown}\" isn't pane or tab: #{line}" if unknown
|
|
150
174
|
line = if start_prompt
|
|
151
175
|
"#{line} with the first prompt \"#{start_prompt}\" (one #{peer::NAME} turn): it pairs when #{peer::NAME} " \
|
|
152
176
|
"runs it (waiting #{wait_minutes} min)"
|
|
@@ -183,7 +207,7 @@ module Samagotchi
|
|
|
183
207
|
line.empty? ? self.class::PEER : line
|
|
184
208
|
end
|
|
185
209
|
|
|
186
|
-
# The
|
|
210
|
+
# The window's argv: the user's login shell (kitty's own env may lack the
|
|
187
211
|
# PATH a terminal has) exec'ing the start command, the first prompt as
|
|
188
212
|
# its last word.
|
|
189
213
|
def start_argv(ctx)
|
|
@@ -205,26 +229,26 @@ module Samagotchi
|
|
|
205
229
|
nil
|
|
206
230
|
end
|
|
207
231
|
|
|
208
|
-
# /<peer> show alone: the
|
|
232
|
+
# /<peer> show alone: the window chi started, to the front.
|
|
209
233
|
def show_started(ctx)
|
|
210
234
|
peer = self.class
|
|
211
235
|
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
236
|
|
|
213
237
|
started = started_pane(ctx)
|
|
214
|
-
return "#{peer::COMMAND} show: chi started no #{peer::NAME}
|
|
238
|
+
return "#{peer::COMMAND} show: chi started no #{peer::NAME} window here (#{peer::COMMAND} show <id8> shows one #{peer::COMMAND} pair lists)" unless started
|
|
215
239
|
|
|
216
240
|
link = ctx.peers.link(peer::PEER)
|
|
217
241
|
# Paired: only the window the paired peer runs in (a ref read back
|
|
218
242
|
# from state.json is checked, never trusted).
|
|
219
243
|
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
|
|
244
|
+
return "#{peer::COMMAND} show: the paired #{peer::NAME} doesn't run in the window chi started"
|
|
221
245
|
end
|
|
222
|
-
return "#{peer::COMMAND} show: its kitty
|
|
246
|
+
return "#{peer::COMMAND} show: its kitty window is closed (#{peer::COMMAND} start opens another)" unless ctx.panes.show(started[:ref])
|
|
223
247
|
|
|
224
248
|
nil
|
|
225
249
|
end
|
|
226
250
|
|
|
227
|
-
# The status card's row for a pairing with the
|
|
251
|
+
# The status card's row for a pairing with the window chi started
|
|
228
252
|
# (scene 5): [rows, actions], or nil when chi started none, or the
|
|
229
253
|
# pairing isn't with it.
|
|
230
254
|
def started_rows(ctx, link)
|
|
@@ -235,11 +259,11 @@ module Samagotchi
|
|
|
235
259
|
if ctx.panes.alive?(started[:ref])
|
|
236
260
|
return nil unless same_window?(ctx, link, started[:ref])
|
|
237
261
|
|
|
238
|
-
[[{ title: "Paired · started by chi", sub: "
|
|
262
|
+
[[{ title: "Paired · started by chi", sub: "#{started[:title]} · last heard #{last_heard(ctx, link)}" }],
|
|
239
263
|
[{ label: "Show", command: "#{peer::COMMAND} show", row: 0, primary: true },
|
|
240
264
|
{ label: "Unpair", command: "#{peer::COMMAND} unpair", row: 0 }]]
|
|
241
265
|
elsif link_gone?(link)
|
|
242
|
-
[[{ title: "Its kitty
|
|
266
|
+
[[{ title: "Its kitty window is closed",
|
|
243
267
|
sub: "That #{peer::NAME} ended. Start another, or wait for one you start yourself." }],
|
|
244
268
|
[{ label: "Start one in kitty", command: "#{peer::COMMAND} start", row: 0, primary: true },
|
|
245
269
|
{ label: "Unpair", command: "#{peer::COMMAND} unpair", row: 0 }]]
|
|
@@ -248,7 +272,7 @@ module Samagotchi
|
|
|
248
272
|
nil
|
|
249
273
|
end
|
|
250
274
|
|
|
251
|
-
# Paired since chi started the
|
|
275
|
+
# Paired since chi started the window (a pairing older than it is another).
|
|
252
276
|
def started_pairing?(link, started)
|
|
253
277
|
Time.iso8601(link.paired_at.to_s) >= Time.iso8601(started[:at].to_s) - 5
|
|
254
278
|
rescue ArgumentError
|
|
@@ -269,7 +293,8 @@ module Samagotchi
|
|
|
269
293
|
started_ago(ctx.peers.last_message(self.class::PEER)&.at || link.paired_at)
|
|
270
294
|
end
|
|
271
295
|
|
|
272
|
-
# {ref:, title:, at:} of the
|
|
296
|
+
# {ref:, title:, at:} of the window chi started (a pane or a tab), or
|
|
297
|
+
# nil.
|
|
273
298
|
def started_pane(ctx)
|
|
274
299
|
data = read_state(ctx)[STATE_KEY]
|
|
275
300
|
ref = Samagotchi::Panes::PaneRef.from_h(data) or return nil
|
|
@@ -85,12 +85,15 @@ module Samagotchi
|
|
|
85
85
|
PeerPresence.here(name.to_s, project: key, state_dir: state_dir)
|
|
86
86
|
end
|
|
87
87
|
|
|
88
|
-
# This session starts waiting for +name+ (10 minutes; again: anew)
|
|
88
|
+
# This session starts waiting for +name+ (10 minutes; again: anew);
|
|
89
|
+
# +app+: the peer's product name ("Claude Code"), chi web's waiting chip's.
|
|
89
90
|
# @return [PeerPresence::PeerWaiting]
|
|
90
|
-
def wait(name
|
|
91
|
+
def wait(name, app: nil)
|
|
92
|
+
PeerPresence.wait!(name.to_s, session: session_id!, project: project, app: app, state_dir: state_dir)
|
|
93
|
+
end
|
|
91
94
|
|
|
92
95
|
# Ends this session's wait for +name+ when it is still +mark+ (the
|
|
93
|
-
# one #wait returned: a start whose
|
|
96
|
+
# one #wait returned: a start whose window didn't open), leaving any other
|
|
94
97
|
# wait, a claim and any pairing alone (PeerPresence.drop_wait).
|
|
95
98
|
# @return [Boolean] whether it was removed
|
|
96
99
|
def stop_waiting(name, mark)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../memory_paths"
|
|
4
|
+
require_relative "../guardrails/chi_dirs"
|
|
5
|
+
require_relative "../guardrails/review_paths"
|
|
6
|
+
|
|
7
|
+
module Samagotchi
|
|
8
|
+
module Plugin
|
|
9
|
+
# ctx.project (docs/plugins.md): the project a session's folder belongs
|
|
10
|
+
# to, as chi's memories find it (MemoryPaths.lookup, the same lookup
|
|
11
|
+
# Session#project_root's fallback makes), frozen. Every linked worktree
|
|
12
|
+
# of a repository is its main checkout's project.
|
|
13
|
+
#
|
|
14
|
+
# +kind+ :git, :marker (a `chi project init` folder) or :none
|
|
15
|
+
# +root+ the project root (a git repository's main checkout, the
|
|
16
|
+
# marked folder), nil for :none
|
|
17
|
+
# +key+ the memories key (`<name>_<hash>` or the marker's id),
|
|
18
|
+
# nil for :none or a key in conflict
|
|
19
|
+
# +memories_dir+ the project scope's memories folder, the one memory_read
|
|
20
|
+
# and memory_write use here: for :none too (a folder in no
|
|
21
|
+
# project keeps its own, keyed by its path); nil for a key
|
|
22
|
+
# in conflict
|
|
23
|
+
# +system_memories_dir+ the system scope's
|
|
24
|
+
# +chi_dirs+ chi's state dir, config dir (memories, installed bundles)
|
|
25
|
+
# and an outside hooks dir (Guardrails::ChiDirs.review)
|
|
26
|
+
Project = Data.define(:kind, :root, :key, :memories_dir, :system_memories_dir, :chi_dirs) do
|
|
27
|
+
# The project +cwd+ belongs to. A key MemoryPaths refuses (another
|
|
28
|
+
# project's folder holds the id) leaves +key+ and +memories_dir+ nil:
|
|
29
|
+
# the memory tools refuse that folder too.
|
|
30
|
+
# @param env [Hash] where the config dir (the memories) is read from
|
|
31
|
+
# @return [Project]
|
|
32
|
+
def self.for(cwd, env: ENV)
|
|
33
|
+
found = MemoryPaths.lookup(cwd.to_s)
|
|
34
|
+
key = found.project? ? conflict_free { MemoryPaths.key_for(root: found.root, id: found.id, env: env) } : nil
|
|
35
|
+
memories = conflict_free { MemoryPaths.scope_dir("project", env: env, cwd: cwd.to_s) }
|
|
36
|
+
new(kind: found.kind, root: found.project? ? found.root.dup.freeze : nil, key: key&.dup&.freeze,
|
|
37
|
+
memories_dir: memories&.dup&.freeze, system_memories_dir: MemoryPaths.system_dir(env: env).freeze,
|
|
38
|
+
chi_dirs: Guardrails::ChiDirs.review.map { |dir| dir.to_s.dup.freeze }.freeze)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.conflict_free
|
|
42
|
+
yield
|
|
43
|
+
rescue MemoryPaths::ProjectIdConflict
|
|
44
|
+
nil
|
|
45
|
+
end
|
|
46
|
+
private_class_method :conflict_free
|
|
47
|
+
|
|
48
|
+
# Whether an absolute +path+ (a folder to read, as given and as it
|
|
49
|
+
# resolves) is one a participant's turn never reads: chi's own dirs,
|
|
50
|
+
# the home secret dirs and files (~/.ssh, ~/.netrc…), or a secret's
|
|
51
|
+
# name (Guardrails::ReviewPaths#secret_refusal).
|
|
52
|
+
def secret_root?(path)
|
|
53
|
+
!Guardrails::ReviewPaths.new(root: "", denied_dirs: chi_dirs).secret_refusal([path.to_s]).nil?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Whether a file's +relative_path+ (inside a folder already allowed) is
|
|
57
|
+
# named like a secret: .env*, credentials, keys, .git configs…
|
|
58
|
+
# (Guardrails::ReviewPaths.secret_name?: the name half only, not chi's
|
|
59
|
+
# dirs, so a folder under the state dir isn't secret file by file).
|
|
60
|
+
def secret_name?(relative_path) = Guardrails::ReviewPaths.secret_name?(relative_path.to_s)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|