samagotchi 0.164.0 → 0.165.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 +52 -1
- data/docs/architecture.md +7 -5
- data/docs/cli.md +20 -13
- data/docs/configuration.md +5 -1
- data/docs/guardrails.md +13 -16
- data/docs/internals/block-renderers.md +1 -1
- data/docs/internals/bridge.md +4 -3
- data/docs/internals/bundles.md +4 -2
- data/docs/internals/inline-widgets.md +8 -1
- data/docs/internals/web-frontend.md +36 -9
- data/docs/memory.md +12 -7
- data/docs/plugins.md +6 -4
- data/docs/releasing.md +6 -2
- data/docs/scheduling.md +25 -3
- data/docs/sessions.md +8 -6
- data/docs/testing.md +28 -4
- data/lib/samagotchi/block_renderers.rb +3 -6
- data/lib/samagotchi/bridge/card_store.rb +2 -1
- data/lib/samagotchi/bridge/turn_accumulator.rb +7 -8
- data/lib/samagotchi/bridge.rb +45 -3
- data/lib/samagotchi/bundle_command/installed_check.rb +2 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_home.rb +44 -2
- data/lib/samagotchi/client_id.rb +34 -3
- data/lib/samagotchi/compat.rb +36 -0
- data/lib/samagotchi/context_command.rb +3 -0
- data/lib/samagotchi/context_providers.rb +5 -6
- data/lib/samagotchi/context_sources.rb +2 -2
- data/lib/samagotchi/continue_intent.rb +50 -5
- data/lib/samagotchi/events.rb +4 -4
- data/lib/samagotchi/extension_load.rb +2 -1
- data/lib/samagotchi/guardrails/child_boundary.rb +7 -27
- data/lib/samagotchi/guardrails/rules.rb +11 -9
- data/lib/samagotchi/guardrails/targets.rb +3 -4
- data/lib/samagotchi/inline_widgets.rb +16 -1
- data/lib/samagotchi/memory_bundle/provenance.rb +46 -4
- data/lib/samagotchi/memory_bundle/status.rb +2 -1
- data/lib/samagotchi/memory_command.rb +3 -0
- data/lib/samagotchi/memory_orphans.rb +4 -7
- data/lib/samagotchi/memory_paths.rb +64 -7
- data/lib/samagotchi/memory_usage.rb +1 -1
- data/lib/samagotchi/plugin/sessions.rb +4 -2
- data/lib/samagotchi/project_command.rb +4 -1
- data/lib/samagotchi/project_id_clash.rb +37 -26
- data/lib/samagotchi/project_init.rb +24 -7
- data/lib/samagotchi/reply_wait/poll.rb +2 -1
- data/lib/samagotchi/schedules.rb +124 -106
- data/lib/samagotchi/send_command.rb +71 -18
- data/lib/samagotchi/session.rb +3 -2
- data/lib/samagotchi/session_access/local.rb +19 -2
- data/lib/samagotchi/session_access/remote.rb +15 -4
- data/lib/samagotchi/session_chain/links.rb +109 -0
- data/lib/samagotchi/session_chain.rb +7 -19
- data/lib/samagotchi/session_continue.rb +138 -54
- data/lib/samagotchi/session_inbox_delivery.rb +57 -38
- data/lib/samagotchi/session_manager/delivery.rb +104 -18
- data/lib/samagotchi/session_manager/removal.rb +13 -7
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_retention.rb +20 -10
- data/lib/samagotchi/session_setup.rb +31 -1
- data/lib/samagotchi/state_format.rb +12 -8
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +6 -7
- data/lib/samagotchi/terminal_ui.rb +20 -9
- data/lib/samagotchi/tools/delegate.rb +2 -1
- data/lib/samagotchi/verify_fresh.rb +4 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/public/activity.js +2 -2
- data/lib/samagotchi/web/public/app.js +13 -13
- data/lib/samagotchi/web/public/bar_fold.js +293 -0
- data/lib/samagotchi/web/public/card.js +1 -1
- data/lib/samagotchi/web/public/composer_mode.js +5 -7
- data/lib/samagotchi/web/public/index.html +132 -65
- data/lib/samagotchi/web/public/info_bar.js +28 -16
- data/lib/samagotchi/web/public/info_bar_model.js +70 -1
- data/lib/samagotchi/web/public/more_sheet.js +98 -40
- data/lib/samagotchi/web/public/stage_view.js +3 -1
- data/lib/samagotchi/web/public/stream_handlers.js +1 -1
- data/lib/samagotchi/web/public/turn_events.js +0 -122
- data/lib/samagotchi/web/public/turn_model.js +1 -1
- data/lib/samagotchi/web/public/widget_picks.js +18 -3
- data/lib/samagotchi/web/session_control.rb +15 -2
- data/lib/samagotchi/web/session_create.rb +4 -2
- data/lib/samagotchi/web/update_check.rb +27 -8
- metadata +3 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "fileutils"
|
|
3
4
|
require "json"
|
|
5
|
+
require_relative "atomic_file"
|
|
4
6
|
require_relative "session"
|
|
5
7
|
|
|
6
8
|
module Samagotchi
|
|
@@ -8,16 +10,44 @@ module Samagotchi
|
|
|
8
10
|
# folder while it runs: LOCK, held for the whole continue, and FILE, its
|
|
9
11
|
# intent (the successor's id), written before the link is archived and
|
|
10
12
|
# removed once the successor is saved or the continue is undone. A send
|
|
11
|
-
#
|
|
12
|
-
# nobody continues.
|
|
13
|
+
# (SessionInbox::Head) and a schedule tick (Schedules#continuing_reason)
|
|
14
|
+
# read them to tell a continue in flight from a link nobody continues.
|
|
13
15
|
module ContinueIntent
|
|
14
16
|
# One continue of a session at a time.
|
|
15
17
|
LOCK = "continue.lock"
|
|
16
18
|
# The intent: { successor_id:, previously_archived:, archived_ids: }.
|
|
17
19
|
FILE = "continue.json"
|
|
18
20
|
|
|
21
|
+
# The intent as read: the successor's id (nil when it names no valid
|
|
22
|
+
# one), the ids archived before the continue, and the ones it archived
|
|
23
|
+
# (nil when the file has no list: written before the archive).
|
|
24
|
+
# CROSS-VERSION: another chi's repair reads this file
|
|
25
|
+
# (spec/compat/readers_spec.rb).
|
|
26
|
+
Intent = Data.define(:successor_id, :previously_archived, :archived_ids)
|
|
27
|
+
|
|
19
28
|
module_function
|
|
20
29
|
|
|
30
|
+
# Write the intent at +path+ (the continue's one writer).
|
|
31
|
+
# @param archived_ids [Array<String>, nil] nil: not archived yet (not written)
|
|
32
|
+
def write(path, successor_id:, previously_archived:, archived_ids: nil)
|
|
33
|
+
data = { successor_id: successor_id, previously_archived: previously_archived, archived_ids: archived_ids }.compact
|
|
34
|
+
AtomicFile.write(path, "#{JSON.generate(data)}\n", perm: 0o600)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The intent at +path+; a file that isn't a JSON object reads as one
|
|
38
|
+
# naming nothing.
|
|
39
|
+
# @return [Intent]
|
|
40
|
+
# @raise [JSON::ParserError, SystemCallError] it doesn't parse, or is gone
|
|
41
|
+
def read(path)
|
|
42
|
+
data = JSON.parse(File.read(path))
|
|
43
|
+
data = {} unless data.is_a?(Hash)
|
|
44
|
+
successor = data["successor_id"]
|
|
45
|
+
archived = data["archived_ids"].is_a?(Array) ? data["archived_ids"].select { |id| Session.valid_id?(id) } : nil
|
|
46
|
+
Intent.new(successor_id: Session.valid_id?(successor) ? successor : nil,
|
|
47
|
+
previously_archived: Array(data["previously_archived"]).select { |id| Session.valid_id?(id) },
|
|
48
|
+
archived_ids: archived)
|
|
49
|
+
end
|
|
50
|
+
|
|
21
51
|
# The successor's id of a continue of +id+ in flight: its intent written
|
|
22
52
|
# and its lock held. nil when there is none, or the intent is stale (no
|
|
23
53
|
# continue holds the lock: a crash left it, and
|
|
@@ -30,13 +60,28 @@ module Samagotchi
|
|
|
30
60
|
path = File.join(dir, FILE)
|
|
31
61
|
return nil unless File.file?(path) && locked?(File.join(dir, LOCK))
|
|
32
62
|
|
|
33
|
-
|
|
34
|
-
successor = data["successor_id"] if data.is_a?(Hash)
|
|
35
|
-
Session.valid_id?(successor) ? successor : nil
|
|
63
|
+
read(path).successor_id
|
|
36
64
|
rescue JSON::ParserError, SystemCallError
|
|
37
65
|
nil
|
|
38
66
|
end
|
|
39
67
|
|
|
68
|
+
# The block under a shared hold of +id+'s continue lock, so no continue
|
|
69
|
+
# of it starts until the block returns (its exclusive lock waits); the
|
|
70
|
+
# block's argument is false when a continue holds the lock now (not
|
|
71
|
+
# waited for: nothing is held then). A followed send's check and the
|
|
72
|
+
# resume that would un-archive the link run inside it
|
|
73
|
+
# (SessionManager.deliver_turn's follow:).
|
|
74
|
+
# @yieldparam held [Boolean]
|
|
75
|
+
def hold(id, state_dir:)
|
|
76
|
+
return yield(false) unless Session.valid_id?(id)
|
|
77
|
+
|
|
78
|
+
dir = Session.session_dir(id, state_dir: state_dir)
|
|
79
|
+
FileUtils.mkdir_p(dir)
|
|
80
|
+
File.open(File.join(dir, LOCK), File::RDWR | File::CREAT, 0o600) do |lock|
|
|
81
|
+
yield(lock.flock(File::LOCK_SH | File::LOCK_NB) ? true : false)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
40
85
|
# Whether a continue holds the lock at +path+: a shared try that fails.
|
|
41
86
|
# The try is let go at once (the file is closed).
|
|
42
87
|
def locked?(path)
|
data/lib/samagotchi/events.rb
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
module Samagotchi
|
|
4
4
|
# The sets of event types more than one place reads (the Worker, the
|
|
5
5
|
# Bridge's observers, the TUI, the web), kept here once. The web's own
|
|
6
|
-
# copies (app.js's stream handlers
|
|
7
|
-
#
|
|
6
|
+
# copies (app.js's stream handlers) are checked against these by
|
|
7
|
+
# spec/events_drift_spec.rb.
|
|
8
8
|
module Events
|
|
9
9
|
# A turn is over: it completed, was canceled, or failed.
|
|
10
10
|
TURN_END = %i[turn_completed turn_canceled turn_failed].freeze
|
|
@@ -17,8 +17,8 @@ module Samagotchi
|
|
|
17
17
|
# turn's drawing: a join takes its cards from CardStore).
|
|
18
18
|
PART_KINDS = %w[generation thinking text tool input steer reminder notice card].freeze
|
|
19
19
|
# Types no Engine emits that a UI handles: the Bridge's stream frames
|
|
20
|
-
# and the
|
|
21
|
-
#
|
|
20
|
+
# and the one synthetic replay event (a prompt merged into the turn,
|
|
21
|
+
# TurnAccumulator.replay_events).
|
|
22
22
|
STREAM_FRAMES = %i[snapshot reset stream_closed].freeze
|
|
23
23
|
SYNTHETIC = %i[merged_input].freeze
|
|
24
24
|
|
|
@@ -35,7 +35,8 @@ module Samagotchi
|
|
|
35
35
|
# The hooks: config.yml's (Hooks::Loader; an empty Registry without a
|
|
36
36
|
# hooks config). An installed record that still has class-file hooks (a
|
|
37
37
|
# 0.157 install) adds a not-required failure each, which the Engine
|
|
38
|
-
# announces at the first turn: they don't load (#tell_retired_hooks
|
|
38
|
+
# announces at the first turn: they don't load (#tell_retired_hooks;
|
|
39
|
+
# compat shim retired_class_file_hooks: Compat::SHIMS).
|
|
39
40
|
# @return [Hooks::Registry]
|
|
40
41
|
def hooks
|
|
41
42
|
tell_retired_hooks
|
|
@@ -21,16 +21,14 @@ module Samagotchi
|
|
|
21
21
|
# the call writes (writes: true), the same declaration
|
|
22
22
|
# ProtectedPaths reads — a call is judged by what its targets say it
|
|
23
23
|
# acts on, not by its tool's name;
|
|
24
|
-
# execute, task_create
|
|
24
|
+
# a shell call (execute, task_create, or a plugin tool whose targets:
|
|
25
|
+
# carry a command): mutating git runs in a dir outside root
|
|
25
26
|
# (ShellGitDirs), or a command that isn't read-only starts outside
|
|
26
27
|
# root (its cwd:, else Dir.pwd) or names a path in the repository's
|
|
27
28
|
# other checkouts (the main one and sibling worktrees) or its
|
|
28
29
|
# common git dir; or it runs git that changes what all checkouts
|
|
29
30
|
# share, from anywhere (SharedGit: config, update-ref, worktree add,
|
|
30
|
-
# stash clear, tags, another branch's delete).
|
|
31
|
-
# declared command: is checked for what chi can read for sure only:
|
|
32
|
-
# SharedGit, and whether the call's cwd: is outside root (its text
|
|
33
|
-
# may not be shell; Targets#rule_command).
|
|
31
|
+
# stash clear, tags, another branch's delete).
|
|
34
32
|
# Paths elsewhere in a command (/usr/bin, ~/.gem) don't count, and
|
|
35
33
|
# read-only commands never ask, nor do ones that only write into a tmp
|
|
36
34
|
# dir (> / >>, cp's destination, tee; Targets#read_only_but_tmp_writes?). Paths resolve under chi's env and the
|
|
@@ -41,8 +39,6 @@ module Samagotchi
|
|
|
41
39
|
RULE = "child-boundary"
|
|
42
40
|
SOURCE = "core"
|
|
43
41
|
SCOPES = %w[once session].freeze
|
|
44
|
-
SHELL_TOOLS = %w[execute task_create].freeze
|
|
45
|
-
|
|
46
42
|
# @param root [#call] → String, nil: the child's folder (nil: no check)
|
|
47
43
|
# @param worktrees [#call] → Array<String>: every checkout of root's
|
|
48
44
|
# repository (GitInfo#worktrees); root's own is left out here
|
|
@@ -74,15 +70,15 @@ module Samagotchi
|
|
|
74
70
|
def crosses?(targets, root)
|
|
75
71
|
case targets.tool
|
|
76
72
|
when "write", "edit" then paths_cross?(targets, root)
|
|
77
|
-
when *SHELL_TOOLS then shell_crosses?(targets, root)
|
|
78
73
|
else
|
|
74
|
+
return true if !Targets::CORE_TOOLS.include?(targets.tool) && targets.writes? && paths_cross?(targets, root)
|
|
75
|
+
return shell_crosses?(targets, root) if targets.shell?
|
|
76
|
+
|
|
79
77
|
# chi's other own tools (read, memory_write, …) are unchanged:
|
|
80
78
|
# memory_write carries writes: for ProtectedPaths, but its paths
|
|
81
79
|
# are memory files (Outside exempts them) and its `..` entry names
|
|
82
80
|
# asked for nothing here before.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
plugin_crosses?(targets, root)
|
|
81
|
+
false
|
|
86
82
|
end
|
|
87
83
|
end
|
|
88
84
|
|
|
@@ -90,22 +86,6 @@ module Samagotchi
|
|
|
90
86
|
targets.paths.any? { |path| Outside.outside?(path, root: root) }
|
|
91
87
|
end
|
|
92
88
|
|
|
93
|
-
# A plugin tool, by what its targets: say it acts on (Targets#writes?,
|
|
94
|
-
# #command, #cwd), not by its name: paths its targets say it writes
|
|
95
|
-
# like a write's, and a declared command like a shell's — but for the
|
|
96
|
-
# command only what chi can read for sure: git that changes what every
|
|
97
|
-
# checkout shares (SharedGit), and where the call runs (its cwd:). A
|
|
98
|
-
# plugin's command text may not be shell (Targets#rule_command), so
|
|
99
|
-
# the shell half's git-dir, cd and redirect reads are left out; a
|
|
100
|
-
# plugin that wants more can bring a rule of its own.
|
|
101
|
-
def plugin_crosses?(targets, root)
|
|
102
|
-
return true if targets.writes? && paths_cross?(targets, root)
|
|
103
|
-
return :shared if targets.command && SharedGit.changes?(targets.command, own_branch: @branch.call)
|
|
104
|
-
return true if targets.command && targets.cwd && Outside.outside?(targets.cwd, root: root)
|
|
105
|
-
|
|
106
|
-
false
|
|
107
|
-
end
|
|
108
|
-
|
|
109
89
|
def shell_crosses?(targets, root)
|
|
110
90
|
return true if targets.git_dirs.any? { |dir| dir.is_a?(String) && Outside.outside?(dir, root: root) }
|
|
111
91
|
return :shared if SharedGit.changes?(targets.command, own_branch: @branch.call)
|
|
@@ -9,10 +9,10 @@ module Samagotchi
|
|
|
9
9
|
module Guardrails
|
|
10
10
|
# Declarative rules from config.yml's `guardrails:` section (and, later,
|
|
11
11
|
# installed bundles). All the fields a rule gives must match:
|
|
12
|
-
# tool: a tool name, a list, or "shell" (
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# acts as (Targets#acts_as
|
|
12
|
+
# tool: a tool name, a list, or "shell" (a shell call, including a
|
|
13
|
+
# plugin tool whose targets: declare a command); a name may be
|
|
14
|
+
# a glob ("mcp_*", "mcp_{git,gh}_*"); it matches the call's
|
|
15
|
+
# name or the tool a plugin tool's call acts as (Targets#acts_as)
|
|
16
16
|
# command: a Ruby regex on a shell tool's command, the body of a
|
|
17
17
|
# heredoc its command only reads as data left out
|
|
18
18
|
# (Targets#rule_command, ShellLex.code_text)
|
|
@@ -60,12 +60,13 @@ module Samagotchi
|
|
|
60
60
|
MODES = %w[auto strict].freeze
|
|
61
61
|
GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB
|
|
62
62
|
|
|
63
|
-
Rule = Struct.new(:id, :tools, :command, :path, :git, :touches, :rm, :memory, :models, :modes, :skip_read_only, :verdict, :reason,
|
|
63
|
+
Rule = Struct.new(:id, :tools, :shell, :command, :path, :git, :touches, :rm, :memory, :models, :modes, :skip_read_only, :verdict, :reason,
|
|
64
64
|
:scopes, :source, keyword_init: true) do
|
|
65
65
|
def matches?(targets)
|
|
66
66
|
return false unless targets
|
|
67
67
|
return false unless for_model?(targets.model_name, targets.model_key, -> { targets.small_model? })
|
|
68
|
-
return false if tools && !tool_matches?(targets.tool) && !(targets.acts_as && tool_matches?(targets.acts_as))
|
|
68
|
+
return false if tools && !tool_matches?(targets.tool) && !(targets.acts_as && tool_matches?(targets.acts_as)) &&
|
|
69
|
+
!(shell && targets.shell?)
|
|
69
70
|
return false if command && !(targets.rule_command && command.match?(targets.rule_command))
|
|
70
71
|
return false if path && !path_matches?(targets)
|
|
71
72
|
return false if git && !targets.git_outside_repo?
|
|
@@ -158,7 +159,8 @@ module Samagotchi
|
|
|
158
159
|
raise ParseError, "#{label}: give at least one of #{MATCH_KEYS.join(", ")}"
|
|
159
160
|
end
|
|
160
161
|
|
|
161
|
-
|
|
162
|
+
tools, shell = tools_of(raw["tool"], label)
|
|
163
|
+
Rule.new(id: id, tools: tools, shell: shell, command: regex_of(raw["command"], label),
|
|
162
164
|
path: path_of(raw["path"], label), git: git_of(raw["git"], label),
|
|
163
165
|
touches: touches_of(raw["touches"], label), rm: rm_of(raw["rm"], label), memory: memory_of(raw["memory"], label),
|
|
164
166
|
models: models_of(raw["models"], label),
|
|
@@ -168,12 +170,12 @@ module Samagotchi
|
|
|
168
170
|
end
|
|
169
171
|
|
|
170
172
|
def self.tools_of(value, label)
|
|
171
|
-
return nil if value.nil?
|
|
173
|
+
return [nil, false] if value.nil?
|
|
172
174
|
|
|
173
175
|
names = Array(value).map(&:to_s)
|
|
174
176
|
raise ParseError, "#{label}: tool must be a name or a list of names" if names.empty? || names.any?(&:empty?)
|
|
175
177
|
|
|
176
|
-
names.flat_map { |n| n == "shell" ? Targets::SHELL_TOOLS : [n] }.uniq
|
|
178
|
+
[names.flat_map { |n| n == "shell" ? Targets::SHELL_TOOLS : [n] }.uniq, names.include?("shell")]
|
|
177
179
|
end
|
|
178
180
|
|
|
179
181
|
# Whether a rule's tool name is a glob.
|
|
@@ -357,7 +357,7 @@ module Samagotchi
|
|
|
357
357
|
@repo_root = repo_root
|
|
358
358
|
end
|
|
359
359
|
|
|
360
|
-
def shell? = SHELL_TOOLS.include?(@tool)
|
|
360
|
+
def shell? = SHELL_TOOLS.include?(@tool) || !@command.nil?
|
|
361
361
|
|
|
362
362
|
# The homes and environments a shell call's words may resolve
|
|
363
363
|
# against, as [home, env] pairs: chi's own, and when the call's env:
|
|
@@ -381,9 +381,8 @@ module Samagotchi
|
|
|
381
381
|
@redefines_shell = self.class.redefines_shell?(@env)
|
|
382
382
|
end
|
|
383
383
|
|
|
384
|
-
# What a rule's command: reads:
|
|
385
|
-
# heredoc its command only reads as data left out (ShellLex.code_text)
|
|
386
|
-
# a plugin tool's own command as given (its text may not be shell).
|
|
384
|
+
# What a rule's command: reads: a shell command with the body of each
|
|
385
|
+
# heredoc its command only reads as data left out (ShellLex.code_text).
|
|
387
386
|
# Approvals, the approval question and hooks keep #command.
|
|
388
387
|
def rule_command
|
|
389
388
|
return @rule_command if defined?(@rule_command)
|
|
@@ -46,6 +46,9 @@ module Samagotchi
|
|
|
46
46
|
# The deepest legal block nests 6 (root, steps, a step, cases, a case, a
|
|
47
47
|
# folded option); past this it is refused before the stack can overflow.
|
|
48
48
|
MAX_DEPTH = 8
|
|
49
|
+
# What a row id may not start with: a shell line's ! and a session
|
|
50
|
+
# command's / (CommandIntake), with the spaces between them.
|
|
51
|
+
COMMAND_LEAD = %r{\A[!/\s]+}
|
|
49
52
|
# How much of a bad type value an error repeats.
|
|
50
53
|
MAX_ECHO = 40
|
|
51
54
|
|
|
@@ -212,7 +215,7 @@ module Samagotchi
|
|
|
212
215
|
where = "row #{index + 1}"
|
|
213
216
|
raise Refused, "#{where} isn't a mapping" unless row.is_a?(Hash)
|
|
214
217
|
|
|
215
|
-
id = text(row["id"], "#{where}'s id", required: true)
|
|
218
|
+
id = row_id(text(row["id"], "#{where}'s id", required: true), where)
|
|
216
219
|
where = "row #{id}"
|
|
217
220
|
options = list(row["options"], "#{where}'s options")
|
|
218
221
|
raise Refused, "#{where} has #{options.size} options (#{OPTIONS.min}-#{OPTIONS.max})" unless OPTIONS.cover?(options.size)
|
|
@@ -230,6 +233,18 @@ module Samagotchi
|
|
|
230
233
|
"recommended" => recommended, "why" => text(row["why"], "#{where}'s why", free: true))
|
|
231
234
|
end
|
|
232
235
|
|
|
236
|
+
# A row's id leads the page's filled reply ("<id>: <option>"), so it never
|
|
237
|
+
# starts with ! or / (a shell line, a session command): those are dropped,
|
|
238
|
+
# a mend like a fold, and an id that is nothing else is refused.
|
|
239
|
+
def row_id(id, where)
|
|
240
|
+
safe = id.sub(COMMAND_LEAD, "")
|
|
241
|
+
return id if safe == id
|
|
242
|
+
raise Refused, "#{where}'s id is only ! and /" if safe.empty?
|
|
243
|
+
|
|
244
|
+
@folded = true
|
|
245
|
+
safe
|
|
246
|
+
end
|
|
247
|
+
|
|
233
248
|
def walk(tree)
|
|
234
249
|
title = text(tree["title"], "the title", required: true, free: true)
|
|
235
250
|
actors = list(tree["actors"], "actors").map { |actor| text(actor, "an actor", required: true) }
|
|
@@ -66,6 +66,21 @@ module Samagotchi
|
|
|
66
66
|
]
|
|
67
67
|
)
|
|
68
68
|
|
|
69
|
+
# pending.json's format (#write_pending), in its pending_format key:
|
|
70
|
+
# its version key is the bundle's. A journal with no pending_format is
|
|
71
|
+
# one from before it (the same members); a newer chi's keeps its
|
|
72
|
+
# format and the keys this one doesn't know through a write.
|
|
73
|
+
PENDING_FORMAT_VERSION = 1
|
|
74
|
+
PENDING_FORMAT = StateFormat.new(
|
|
75
|
+
version: PENDING_FORMAT_VERSION,
|
|
76
|
+
members: [
|
|
77
|
+
StateFormat.required(:scope),
|
|
78
|
+
StateFormat.required(:target_dir),
|
|
79
|
+
StateFormat.added(:version, in_version: 1, default: nil),
|
|
80
|
+
StateFormat.added(:files, in_version: 1, default: {})
|
|
81
|
+
]
|
|
82
|
+
)
|
|
83
|
+
|
|
69
84
|
def initialize(name:)
|
|
70
85
|
@name = name
|
|
71
86
|
@bundle_dir = File.join(self.class.bundles_dir, name)
|
|
@@ -98,6 +113,19 @@ module Samagotchi
|
|
|
98
113
|
File.join(@bundle_dir, "scripts")
|
|
99
114
|
end
|
|
100
115
|
|
|
116
|
+
# Why the installed scripts/+file+ isn't as installed, nil when it is:
|
|
117
|
+
# the record has no sha for it (+recorded+), it is gone, or it changed.
|
|
118
|
+
# The one check a provider's command and a block renderer's load make
|
|
119
|
+
# before chi runs or serves the file; a --dev install's links pass.
|
|
120
|
+
def script_failure(file, recorded)
|
|
121
|
+
path = File.join(scripts_dir, file)
|
|
122
|
+
return "bundle #{@name}'s scripts/#{file} isn't among its installed scripts (reinstall the bundle)" if self.class.recorded_sha(recorded).empty?
|
|
123
|
+
return "bundle #{@name}'s scripts/#{file} is missing (reinstall the bundle)" unless File.file?(path)
|
|
124
|
+
return nil if self.class.sha_matches?(path, recorded)
|
|
125
|
+
|
|
126
|
+
"bundle #{@name}'s scripts/#{file} differs from the installed one (reinstall the bundle)"
|
|
127
|
+
end
|
|
128
|
+
|
|
101
129
|
# The intent journal of a run that has not written its record yet
|
|
102
130
|
# (#write_pending): its scope and target dir, plus the files and sha256s
|
|
103
131
|
# of the texts a run meant to put there. #write removes it.
|
|
@@ -112,9 +140,14 @@ module Samagotchi
|
|
|
112
140
|
def write_pending(version:, scope:, target_dir:, files:)
|
|
113
141
|
shas = pending_files(target_dir: target_dir)
|
|
114
142
|
files.each { |key, sha| shas[key] = (shas.fetch(key, []) + [sha]).uniq }
|
|
143
|
+
target = File.expand_path(target_dir)
|
|
144
|
+
earlier = pending_metadata
|
|
145
|
+
kept = earlier && earlier["target_dir"] == target ? earlier.except("version", "scope", "target_dir", "files") : {}
|
|
146
|
+
format = [kept.delete("pending_format").to_i, PENDING_FORMAT_VERSION].max
|
|
115
147
|
FileUtils.mkdir_p(@bundle_dir)
|
|
116
148
|
AtomicFile.write(pending_path, JSON.pretty_generate(
|
|
117
|
-
"version" => version.to_s, "scope" => scope.to_s, "target_dir" =>
|
|
149
|
+
kept.merge("pending_format" => format, "version" => version.to_s, "scope" => scope.to_s, "target_dir" => target,
|
|
150
|
+
"files" => shas)
|
|
118
151
|
))
|
|
119
152
|
end
|
|
120
153
|
|
|
@@ -571,12 +604,21 @@ module Samagotchi
|
|
|
571
604
|
files.is_a?(Hash) ? files.to_h { |key, shas| [key, Array(shas).grep(String)] } : {}
|
|
572
605
|
end
|
|
573
606
|
|
|
607
|
+
# The journal as PENDING_FORMAT reads it, string keys, its format under
|
|
608
|
+
# pending_format; nil when there is none or it doesn't read.
|
|
574
609
|
def pending_metadata
|
|
575
610
|
data = JSON.parse(File.read(pending_path))
|
|
576
|
-
return unless data.is_a?(Hash)
|
|
611
|
+
return unless data.is_a?(Hash)
|
|
612
|
+
|
|
613
|
+
format = data["pending_format"]
|
|
614
|
+
format = nil unless format.is_a?(Integer) && format >= 0
|
|
615
|
+
document = PENDING_FORMAT.read({ "version" => format, "journals" => [data] }, collection: "journals")
|
|
616
|
+
journal = document.entries.first
|
|
617
|
+
return unless journal.readable?
|
|
618
|
+
return unless journal.attributes[:scope].is_a?(String) && journal.attributes[:target_dir].is_a?(String)
|
|
577
619
|
|
|
578
|
-
|
|
579
|
-
rescue JSON::ParserError, EncodingError, SystemCallError
|
|
620
|
+
journal.to_h.merge("pending_format" => document.version)
|
|
621
|
+
rescue JSON::ParserError, EncodingError, SystemCallError, StateFormat::InvalidDocument
|
|
580
622
|
nil
|
|
581
623
|
end
|
|
582
624
|
private :pending_files, :pending_metadata
|
|
@@ -85,7 +85,8 @@ module Samagotchi
|
|
|
85
85
|
|
|
86
86
|
# The class-file hooks the record still has (a 0.157 install's; chi
|
|
87
87
|
# loads none, Manifest::HooksRetired), each "present" or "missing" in
|
|
88
|
-
# hooks/, by name; {} for none
|
|
88
|
+
# hooks/, by name; {} for none (compat shim retired_class_file_hooks:
|
|
89
|
+
# Compat::SHIMS).
|
|
89
90
|
# @return [Hash{String => String}]
|
|
90
91
|
def self.retired_hooks(provenance, bundle)
|
|
91
92
|
bundle.hooks.keys.sort.to_h do |file|
|
|
@@ -96,6 +96,9 @@ module Samagotchi
|
|
|
96
96
|
return usage_error("unknown scope #{scope.inspect}: use --scope project|system") if scope && !SCOPES.include?(scope)
|
|
97
97
|
|
|
98
98
|
send(:"run_#{@sub}", parsed.options, parsed.args)
|
|
99
|
+
rescue MemoryPaths::ProjectIdConflict => e
|
|
100
|
+
error_line("#{command_name}: #{e.message}")
|
|
101
|
+
CLI::Exit::FAILED
|
|
99
102
|
end
|
|
100
103
|
|
|
101
104
|
# The memories of +scope+ (project first, then system, when nil), by
|
|
@@ -24,10 +24,6 @@ module Samagotchi
|
|
|
24
24
|
# @!attribute computed_exists [Boolean] whether +computed+ is a folder (empty of memories)
|
|
25
25
|
Found = Data.define(:computed, :candidates, :computed_exists)
|
|
26
26
|
|
|
27
|
-
# A project keyed by its path is <name>_<8 hex>; a marked project's folder is
|
|
28
|
-
# its id, <name>-<8 hex>.
|
|
29
|
-
PATH_HASH = /_\h{8}\z/
|
|
30
|
-
|
|
31
27
|
module_function
|
|
32
28
|
|
|
33
29
|
# A candidate folder's key, in words: an earlier path's hash, or a chi project
|
|
@@ -37,8 +33,9 @@ module Samagotchi
|
|
|
37
33
|
hash_keyed?(candidate) ? "another path's hash" : "project id #{File.basename(candidate)}"
|
|
38
34
|
end
|
|
39
35
|
|
|
40
|
-
# Internal: is this candidate keyed by its path
|
|
41
|
-
|
|
36
|
+
# Internal: is this candidate keyed by its path (<name>_<8 hex>) rather
|
|
37
|
+
# than by a project id (<name>-<8 hex>)?
|
|
38
|
+
def hash_keyed?(candidate) = MemoryPaths.path_key_shape?(File.basename(candidate))
|
|
42
39
|
private_class_method :hash_keyed?
|
|
43
40
|
|
|
44
41
|
# The orphan candidates for +cwd+'s project, or nil: only inside a
|
|
@@ -66,7 +63,7 @@ module Samagotchi
|
|
|
66
63
|
return nil if candidates.empty?
|
|
67
64
|
|
|
68
65
|
Found.new(computed: computed, candidates: candidates, computed_exists: File.directory?(computed))
|
|
69
|
-
rescue SystemCallError
|
|
66
|
+
rescue MemoryPaths::ProjectIdConflict, SystemCallError
|
|
70
67
|
nil
|
|
71
68
|
end
|
|
72
69
|
|
|
@@ -19,6 +19,17 @@ module Samagotchi
|
|
|
19
19
|
# The file that makes a folder in no git repository a chi project
|
|
20
20
|
# (`chi project init`). The file, not the bare .chi/ dir, is the marker.
|
|
21
21
|
MARKER = File.join(".chi", "project.yml").freeze
|
|
22
|
+
CLAIM = ".root"
|
|
23
|
+
|
|
24
|
+
class ProjectIdConflict < ArgumentError
|
|
25
|
+
attr_reader :folder
|
|
26
|
+
|
|
27
|
+
def initialize(folder)
|
|
28
|
+
@folder = folder
|
|
29
|
+
super("project memory folder #{folder} is already in use; run `chi project init --new-id` " \
|
|
30
|
+
"to give this project its own id")
|
|
31
|
+
end
|
|
32
|
+
end
|
|
22
33
|
|
|
23
34
|
# How a folder's project was found (#lookup): +kind+ is :git, :marker or
|
|
24
35
|
# :none; +root+ is what memories are keyed on (#project_root: git's
|
|
@@ -80,20 +91,59 @@ module Samagotchi
|
|
|
80
91
|
|
|
81
92
|
# One folder per project: a marked project's id (ProjectId), else its
|
|
82
93
|
# root's path key (#key_for_root).
|
|
83
|
-
def project_key(cwd = Dir.pwd)
|
|
84
|
-
found = lookup(cwd)
|
|
85
|
-
key_for(root: found.root, id: found.id)
|
|
94
|
+
def project_key(cwd = Dir.pwd, env: ENV, home: default_home)
|
|
95
|
+
found = lookup(cwd, home: home)
|
|
96
|
+
key_for(root: found.root, id: found.id, env: env, home: home)
|
|
86
97
|
end
|
|
87
98
|
|
|
88
99
|
# The key of a project already resolved: +id+ (a ProjectLookup's, a
|
|
89
100
|
# session's project_id) when there is one, else +root+'s path key. Every
|
|
90
101
|
# caller holding a root keys here, with the id that came with it.
|
|
91
|
-
def key_for(root:, id:
|
|
102
|
+
def key_for(root:, id:, env: ENV, home: default_home)
|
|
103
|
+
return key_for_root(root) unless id
|
|
104
|
+
|
|
105
|
+
conflict = project_id_conflict(root: root, id: id, env: env, home: home)
|
|
106
|
+
raise conflict if conflict
|
|
107
|
+
|
|
108
|
+
id
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# The shape of a #key_for_root name (`<basename>_<8 lowercase hex>`);
|
|
112
|
+
# generated ids (ProjectId.generate) are `<name>-<8 hex>`.
|
|
113
|
+
PATH_KEY_SHAPE = /_[0-9a-f]{8}\z/
|
|
114
|
+
private_constant :PATH_KEY_SHAPE
|
|
115
|
+
|
|
116
|
+
# An existing id folder is safe only when it is claimed by this root, or
|
|
117
|
+
# the caller or its previous claimant no longer has this id. An unclaimed
|
|
118
|
+
# folder is taken only when its name has the path-key shape (another
|
|
119
|
+
# project's path-keyed folder); one of any other name is a legacy id
|
|
120
|
+
# folder (an older `chi project init` or chi wrote no claim):
|
|
121
|
+
# ProjectIdClash.check claims it at session start.
|
|
122
|
+
def project_id_conflict(root:, id:, env: ENV, home: default_home)
|
|
123
|
+
folder = File.join(projects_dir(env: env), id)
|
|
124
|
+
return nil unless File.exist?(folder) || File.symlink?(folder)
|
|
125
|
+
return ProjectIdConflict.new(folder) unless File.directory?(folder)
|
|
126
|
+
|
|
127
|
+
claim_path = File.join(folder, CLAIM)
|
|
128
|
+
other = File.file?(claim_path) ? File.read(claim_path, 4096).to_s.lines.first.to_s.strip : ""
|
|
129
|
+
if other.empty?
|
|
130
|
+
return path_key_shape?(id) ? ProjectIdConflict.new(folder) : nil
|
|
131
|
+
end
|
|
132
|
+
return nil if other == root || !same_project?(root, id, home)
|
|
133
|
+
return ProjectIdConflict.new(folder) if same_project?(other, id, home)
|
|
134
|
+
|
|
135
|
+
nil
|
|
136
|
+
rescue SystemCallError
|
|
137
|
+
ProjectIdConflict.new(folder)
|
|
138
|
+
end
|
|
92
139
|
|
|
93
140
|
# The path key: basename + 8 hex chars of MD5(full root path). Pure: it
|
|
94
|
-
# reads no file
|
|
141
|
+
# reads no file.
|
|
95
142
|
def key_for_root(root) = "#{File.basename(root)}_#{Digest::MD5.hexdigest(root)[0..7]}"
|
|
96
143
|
|
|
144
|
+
# Whether +key+ looks like a #key_for_root name.
|
|
145
|
+
def path_key_shape?(key) = PATH_KEY_SHAPE.match?(key)
|
|
146
|
+
|
|
97
147
|
# The id in the marker at +root+, or nil (none, or not a valid one: one
|
|
98
148
|
# debug line says why).
|
|
99
149
|
def marker_id(root)
|
|
@@ -227,10 +277,17 @@ module Samagotchi
|
|
|
227
277
|
rescue SystemCallError
|
|
228
278
|
gitdir
|
|
229
279
|
end
|
|
230
|
-
|
|
280
|
+
|
|
281
|
+
def same_project?(other, id, home)
|
|
282
|
+
return false if other.empty? || !File.directory?(other)
|
|
283
|
+
|
|
284
|
+
found = lookup(other, home: home)
|
|
285
|
+
found.marker? && found.root == File.realpath(other) && found.id == id
|
|
286
|
+
end
|
|
287
|
+
private_class_method :find_dot_git, :git_root, :gitdir_from_file, :common_dir, :same_project?
|
|
231
288
|
|
|
232
289
|
def project_dir(env: ENV, cwd: Dir.pwd)
|
|
233
|
-
File.join(projects_dir(env: env), project_key(cwd))
|
|
290
|
+
File.join(projects_dir(env: env), project_key(cwd, env: env))
|
|
234
291
|
end
|
|
235
292
|
|
|
236
293
|
# The folder of a memory scope: "system" (or blank) or "project"; nil for
|
|
@@ -351,7 +351,7 @@ module Samagotchi
|
|
|
351
351
|
|
|
352
352
|
def project_dir_of(session)
|
|
353
353
|
root, id = session_project(session)
|
|
354
|
-
root ? File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: root, id: id)) : nil
|
|
354
|
+
root ? File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: root, id: id, env: @env)) : nil
|
|
355
355
|
end
|
|
356
356
|
|
|
357
357
|
# A session of this folder's project (worktrees of one repository share
|
|
@@ -112,7 +112,8 @@ module Samagotchi
|
|
|
112
112
|
end
|
|
113
113
|
|
|
114
114
|
# Send +text+ to session +id+ as a user message (it runs as a turn;
|
|
115
|
-
#
|
|
115
|
+
# an idle session is woken, but one its owner stopped or archived is
|
|
116
|
+
# not: refused). Waits up to 5 s for its worker, so call
|
|
116
117
|
# it from an anytime command or your own thread, never a tool or hook
|
|
117
118
|
# of a running turn. A running turn takes it at its next step; with
|
|
118
119
|
# +delivery: :queue+ it runs as a turn of its own after that one
|
|
@@ -121,7 +122,8 @@ module Samagotchi
|
|
|
121
122
|
# :queue
|
|
122
123
|
# @return [String] the id of the session it went to
|
|
123
124
|
# @raise [Error] no such session, a one-shot owns it, it wasn't taken
|
|
124
|
-
# (a worker older than queueing refuses a queue
|
|
125
|
+
# (a worker older than queueing refuses a queue; a stopped or
|
|
126
|
+
# archived session: session_stopped / session_archived), or delivery is
|
|
125
127
|
# :cut or unknown
|
|
126
128
|
def send(id, text, delivery: nil)
|
|
127
129
|
raise Error, "nothing to send" if text.to_s.strip.empty?
|
|
@@ -86,6 +86,9 @@ module Samagotchi
|
|
|
86
86
|
return run_merge(dir) if parsed.options[:merge_subfolders]
|
|
87
87
|
|
|
88
88
|
run_init(dir)
|
|
89
|
+
rescue MemoryPaths::ProjectIdConflict => e
|
|
90
|
+
error_line("#{command_name}: #{e.message}")
|
|
91
|
+
CLI::Exit::FAILED
|
|
89
92
|
end
|
|
90
93
|
|
|
91
94
|
private
|
|
@@ -114,7 +117,7 @@ module Samagotchi
|
|
|
114
117
|
|
|
115
118
|
# @param found [MemoryPaths::ProjectLookup] the project (its root and id key the folder)
|
|
116
119
|
def memories_line(found)
|
|
117
|
-
dir = File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: found.root, id: found.id))
|
|
120
|
+
dir = File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: found.root, id: found.id, env: @env))
|
|
118
121
|
count = memory_count(dir)
|
|
119
122
|
"#{Paths.tilde(dir)}#{" (#{count} #{count == 1 ? "memory" : "memories"})" if count.positive?}"
|
|
120
123
|
end
|