samagotchi 0.182.0 → 0.184.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 +107 -1
- data/bin/chi +3 -1
- data/docs/architecture.md +22 -11
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +103 -4
- data/docs/cli.md +28 -6
- data/docs/configuration.md +4 -2
- data/docs/guardrails.md +16 -1
- data/docs/internals/block-renderers.md +58 -3
- data/docs/internals/bridge.md +14 -6
- data/docs/internals/bundles.md +26 -5
- data/docs/internals/engine.md +20 -9
- data/docs/internals/participants.md +1 -1
- data/docs/internals/prompt-caching.md +8 -1
- data/docs/internals/remote-sessions.md +40 -5
- data/docs/internals/web-frontend.md +96 -31
- data/docs/internals/web-push.md +11 -0
- data/docs/internals/working-set.md +239 -7
- data/docs/mcp-server.md +84 -0
- data/docs/memory.md +2 -1
- data/docs/plugins.md +309 -22
- data/docs/scheduling.md +14 -0
- data/docs/sessions.md +4 -4
- data/docs/sub-agent.md +9 -0
- data/docs/testing.md +3 -1
- data/docs/thin-clients.md +165 -0
- data/lib/samagotchi/bridge/pending_card.rb +4 -3
- data/lib/samagotchi/bridge/sse_writer.rb +16 -15
- data/lib/samagotchi/bridge.rb +59 -68
- data/lib/samagotchi/bridge_client.rb +12 -4
- data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
- data/lib/samagotchi/bundle_command/check.rb +55 -20
- data/lib/samagotchi/bundle_command.rb +50 -21
- data/lib/samagotchi/bundle_manifest.rb +105 -4
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/client.rb +260 -0
- data/lib/samagotchi/bundles/mcp/content.rb +107 -0
- data/lib/samagotchi/bundles/mcp/http_client.rb +432 -0
- data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
- data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
- data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
- data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
- data/lib/samagotchi/bundles/system/manifest.yml +3 -3
- data/lib/samagotchi/bundles/system/self_map.md +7 -2
- data/lib/samagotchi/cli/parent_wait.rb +5 -3
- data/lib/samagotchi/command_intake.rb +7 -0
- data/lib/samagotchi/commands/registry.rb +8 -3
- data/lib/samagotchi/continue_offer.rb +24 -15
- data/lib/samagotchi/engine/block_fix.rb +4 -4
- data/lib/samagotchi/engine/cards.rb +14 -5
- data/lib/samagotchi/engine/events.rb +10 -15
- data/lib/samagotchi/engine/models.rb +1 -1
- data/lib/samagotchi/engine/plugins.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -1
- data/lib/samagotchi/engine/session.rb +14 -6
- data/lib/samagotchi/engine/side_requests.rb +1 -1
- data/lib/samagotchi/engine/sign_in.rb +142 -0
- data/lib/samagotchi/engine/turn_end.rb +3 -3
- data/lib/samagotchi/engine/turn_helpers.rb +4 -3
- data/lib/samagotchi/engine.rb +43 -27
- data/lib/samagotchi/engine_snapshots.rb +1 -1
- data/lib/samagotchi/guardrail_wiring.rb +3 -1
- data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
- data/lib/samagotchi/guardrails/verdict.rb +3 -2
- data/lib/samagotchi/hold_stop.rb +4 -4
- data/lib/samagotchi/idle_recap.rb +19 -11
- data/lib/samagotchi/idle_reminders.rb +7 -5
- data/lib/samagotchi/idle_scheduler.rb +7 -10
- data/lib/samagotchi/installed_bundles/dir.rb +37 -0
- data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
- data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
- data/lib/samagotchi/installed_bundles/record.rb +3 -2
- data/lib/samagotchi/installed_bundles/standing.rb +3 -1
- data/lib/samagotchi/llm/http.rb +96 -5
- data/lib/samagotchi/mcp_command.rb +110 -0
- data/lib/samagotchi/mcp_server/http.rb +112 -0
- data/lib/samagotchi/mcp_server/protocol.rb +263 -0
- data/lib/samagotchi/mcp_server/stdio.rb +93 -0
- data/lib/samagotchi/mcp_server/text.rb +155 -0
- data/lib/samagotchi/mcp_server/tools.rb +246 -0
- data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
- data/lib/samagotchi/memory_bundle/builder.rb +20 -2
- data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
- data/lib/samagotchi/memory_bundle/installer.rb +16 -3
- data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
- data/lib/samagotchi/memory_bundle/shas.rb +6 -4
- data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
- data/lib/samagotchi/memory_bundle/status.rb +15 -6
- data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
- data/lib/samagotchi/model_notes.rb +34 -12
- data/lib/samagotchi/plugin/api.rb +6 -1
- data/lib/samagotchi/plugin/card.rb +36 -11
- data/lib/samagotchi/plugin/context.rb +63 -6
- data/lib/samagotchi/plugin/http.rb +40 -15
- data/lib/samagotchi/plugin/http_errors.rb +15 -0
- data/lib/samagotchi/plugin/http_stream.rb +86 -0
- data/lib/samagotchi/plugin/http_transport.rb +58 -16
- data/lib/samagotchi/plugin/loader.rb +18 -11
- data/lib/samagotchi/plugin/secrets.rb +266 -0
- data/lib/samagotchi/plugin/sign_in.rb +392 -0
- data/lib/samagotchi/plugin/sse_reader.rb +103 -0
- data/lib/samagotchi/prompt_files.rb +11 -4
- data/lib/samagotchi/prompt_layout.rb +88 -0
- data/lib/samagotchi/prompt_part.rb +82 -20
- data/lib/samagotchi/queued_command.rb +28 -4
- data/lib/samagotchi/recap_setup.rb +13 -4
- data/lib/samagotchi/ref.rb +3 -2
- data/lib/samagotchi/relay_verifier.rb +8 -7
- data/lib/samagotchi/relay_watcher.rb +4 -4
- data/lib/samagotchi/reply_wait.rb +6 -3
- data/lib/samagotchi/run_options.rb +2 -1
- data/lib/samagotchi/schedule_command.rb +18 -4
- data/lib/samagotchi/schedules.rb +40 -8
- data/lib/samagotchi/send_command.rb +54 -41
- data/lib/samagotchi/session_access/ask.rb +285 -0
- data/lib/samagotchi/session_access/contract.rb +5 -0
- data/lib/samagotchi/session_access/exchange/token.rb +101 -0
- data/lib/samagotchi/session_access/exchange.rb +176 -0
- data/lib/samagotchi/session_access/local.rb +34 -6
- data/lib/samagotchi/session_access/remote.rb +4 -1
- data/lib/samagotchi/session_command_catalog.rb +10 -0
- data/lib/samagotchi/session_commands.rb +17 -0
- data/lib/samagotchi/session_config.rb +4 -4
- data/lib/samagotchi/session_inbox.rb +15 -0
- data/lib/samagotchi/session_manager/delivery.rb +8 -4
- data/lib/samagotchi/session_observer.rb +27 -0
- data/lib/samagotchi/steer_cut.rb +4 -3
- data/lib/samagotchi/system_prompt.rb +196 -161
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
- data/lib/samagotchi/testing/boundary.rb +4 -0
- data/lib/samagotchi/testing/bundle.rb +48 -2
- data/lib/samagotchi/testing/fake_http.rb +60 -15
- data/lib/samagotchi/testing/host.rb +4 -1
- data/lib/samagotchi/testing/recorder.rb +45 -5
- data/lib/samagotchi/tools/memory.rb +11 -3
- data/lib/samagotchi/tools/tool_index.rb +14 -3
- data/lib/samagotchi/turn_stages/warmup.rb +2 -2
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +9 -8
- data/lib/samagotchi/web/ask_routes.rb +362 -0
- data/lib/samagotchi/web/mcp_route.rb +91 -0
- data/lib/samagotchi/web/public/app.js +2 -3
- data/lib/samagotchi/web/public/block_frame.js +72 -11
- data/lib/samagotchi/web/public/block_host.js +44 -8
- data/lib/samagotchi/web/public/block_regions.js +126 -0
- data/lib/samagotchi/web/public/card.js +22 -9
- data/lib/samagotchi/web/public/card_actions.js +21 -0
- data/lib/samagotchi/web/public/card_kinds.js +1 -1
- data/lib/samagotchi/web/public/card_registry.js +3 -2
- data/lib/samagotchi/web/public/data.js +15 -12
- data/lib/samagotchi/web/public/delete_sheet.js +231 -169
- data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
- data/lib/samagotchi/web/public/format.js +3 -0
- data/lib/samagotchi/web/public/index.html +52 -7
- data/lib/samagotchi/web/public/more_sheet.js +6 -4
- data/lib/samagotchi/web/public/picture_regions.js +243 -0
- data/lib/samagotchi/web/public/picture_viewer.js +113 -12
- data/lib/samagotchi/web/public/refs.js +5 -3
- data/lib/samagotchi/web/public/region_hit.js +144 -0
- data/lib/samagotchi/web/public/removal_sheet_model.js +274 -0
- data/lib/samagotchi/web/public/running_tasks.js +3 -17
- data/lib/samagotchi/web/public/session_actions.js +96 -100
- data/lib/samagotchi/web/public/working_set_view.js +167 -21
- data/lib/samagotchi/web/public/ws_part_peek.js +277 -0
- data/lib/samagotchi/web/public/ws_peek.js +3 -1
- data/lib/samagotchi/web/public/ws_preview.css +30 -0
- data/lib/samagotchi/web/public/ws_reader.js +7 -3
- data/lib/samagotchi/web/public/ws_sections.js +37 -7
- data/lib/samagotchi/web/push/notifier.rb +33 -6
- data/lib/samagotchi/web/server.rb +1 -1
- data/lib/samagotchi/web/session_access_routes.rb +14 -11
- data/lib/samagotchi/web/session_control.rb +31 -9
- data/lib/samagotchi/web/session_create.rb +2 -2
- data/lib/samagotchi/web/session_hub.rb +26 -4
- data/lib/samagotchi/web/working_set_routes.rb +93 -5
- data/lib/samagotchi/worker/child_side.rb +3 -2
- data/lib/samagotchi/worker/command_queue.rb +13 -13
- data/lib/samagotchi/worker/leave.rb +3 -3
- data/lib/samagotchi/worker/scratch.rb +6 -6
- data/lib/samagotchi/worker/signal_grace.rb +6 -5
- data/lib/samagotchi/worker/startup.rb +3 -3
- data/lib/samagotchi/worker/wake_turns.rb +4 -4
- data/lib/samagotchi/worker.rb +32 -21
- data/lib/samagotchi/worker_exit_claim.rb +1 -1
- data/lib/samagotchi/worker_idle_exit.rb +18 -9
- data/lib/samagotchi/worker_inbound.rb +12 -7
- data/lib/samagotchi/worker_link.rb +2 -2
- data/lib/samagotchi/worker_participant_gate.rb +9 -7
- data/lib/samagotchi/worker_task_wakes.rb +7 -5
- data/lib/samagotchi/working_set.rb +162 -90
- data/lib/samagotchi/working_set_locator.rb +221 -0
- data/lib/samagotchi/working_set_own_changes.rb +221 -0
- data/lib/samagotchi/working_set_part.rb +281 -0
- metadata +36 -1
|
@@ -40,9 +40,11 @@ module Samagotchi
|
|
|
40
40
|
# Loads installed bundles' plugins (manifest plugin: {file:, sha256:})
|
|
41
41
|
# into an Engine's registries (docs/plugins.md).
|
|
42
42
|
#
|
|
43
|
-
# For each bundle, by name:
|
|
44
|
-
#
|
|
45
|
-
#
|
|
43
|
+
# For each bundle, by name: every file of the plugin (its parts, then
|
|
44
|
+
# the plugin file) must match the sha256 recorded at install
|
|
45
|
+
# (InstalledBundles::Dir#plugin_refusal) and chi must meet the bundle's
|
|
46
|
+
# requires_chi. The parts, in their manifest order, then the file are
|
|
47
|
+
# module_eval'd into one fresh module in the bundle's namespace (#namespace_for), the
|
|
46
48
|
# class named like the file (plugin.rb → Plugin) is built with the
|
|
47
49
|
# bundle's settings, and its #register gets a Plugin::Api. What it
|
|
48
50
|
# registered takes effect only when #register returns: a plugin that
|
|
@@ -85,7 +87,7 @@ module Samagotchi
|
|
|
85
87
|
end
|
|
86
88
|
|
|
87
89
|
# Whether it may load is the verdict's (InstalledBundles::Standing, then
|
|
88
|
-
# Dir#
|
|
90
|
+
# Dir#plugin_refusal); what a refusal does is the loader's: a
|
|
89
91
|
# failure, required for a guardrail plugin.
|
|
90
92
|
# @param standing [InstalledBundles::Standing] the bundle's (built from
|
|
91
93
|
# +bundle+ when not given)
|
|
@@ -100,10 +102,10 @@ module Samagotchi
|
|
|
100
102
|
# (its sha, requires_chi, a raise) denies every tool call.
|
|
101
103
|
required = standing.unreadable? ? unreadable_required?(bundle_name) : bundle.manifest.plugin_required
|
|
102
104
|
into = required ? guardrail_failures || failures : failures
|
|
103
|
-
refusal = standing.refusal || installed.
|
|
105
|
+
refusal = standing.refusal || installed.plugin_refusal(bundle)
|
|
104
106
|
return failed(bundle_name, basename, refusal.text, into, required: required) if refusal
|
|
105
107
|
|
|
106
|
-
api = register_file(bundle_name, file, registries, settings: settings)
|
|
108
|
+
api = register_file(bundle_name, file, registries, settings: settings, parts: installed.plugin_part_paths(bundle))
|
|
107
109
|
warn_unrequired(bundle_name, basename) if api.fail_closed? && !required
|
|
108
110
|
Log.info(TAG, "plugin_loaded", bundle: bundle_name, file: basename, **api.counts)
|
|
109
111
|
true
|
|
@@ -127,15 +129,17 @@ module Samagotchi
|
|
|
127
129
|
# makes them; the test kit loads a bundle's source this way).
|
|
128
130
|
# @param namespace [Module, nil] the module the file is evaluated in
|
|
129
131
|
# (default: a fresh one, #fresh_namespace)
|
|
132
|
+
# @param parts [Array<String>] the plugin's parts' paths, evaluated in
|
|
133
|
+
# this order before +file+, into the same module
|
|
130
134
|
# @param api_class [Class] what #register gets (Api; the test kit's
|
|
131
135
|
# recording subclass)
|
|
132
136
|
# @yieldparam plugin [Object] the built plugin, before #register
|
|
133
137
|
# @return [Api] the committed Api (#counts)
|
|
134
138
|
# @raise [Exception] whatever the file, #register or #commit! raised,
|
|
135
139
|
# after stopping the services it started; nothing is registered
|
|
136
|
-
def register_file(bundle_name, file, registries, settings: {}, namespace: nil, api_class: Api)
|
|
140
|
+
def register_file(bundle_name, file, registries, settings: {}, namespace: nil, api_class: Api, parts: [])
|
|
137
141
|
api = nil
|
|
138
|
-
plugin = instantiate(bundle_name, file, settings, namespace: namespace)
|
|
142
|
+
plugin = instantiate(bundle_name, file, settings, namespace: namespace, parts: parts)
|
|
139
143
|
yield plugin if block_given?
|
|
140
144
|
plugin_label = label(bundle_name, File.basename(file))
|
|
141
145
|
api = api_class.new(bundle: bundle_name, label: plugin_label, registries: registries,
|
|
@@ -181,14 +185,17 @@ module Samagotchi
|
|
|
181
185
|
end
|
|
182
186
|
end
|
|
183
187
|
|
|
184
|
-
# module_eval the file into a fresh
|
|
188
|
+
# module_eval each of +parts+, in order, then the file into a fresh
|
|
189
|
+
# module in the bundle's namespace
|
|
185
190
|
# (Samagotchi::Bundles::<bundle>::PluginLoad<n>: each load gets new
|
|
186
|
-
# classes, not the last load's reopened
|
|
191
|
+
# classes, not the last load's reopened; each eval names its own file,
|
|
192
|
+
# so a backtrace or a SyntaxError names the part) and build its class
|
|
187
193
|
# (plugin.rb → Plugin; my_plugin.rb → MyPlugin) as config hooks are
|
|
188
194
|
# built (Hooks.build_plugin): an initialize that takes an argument
|
|
189
195
|
# gets the settings.
|
|
190
|
-
def instantiate(bundle_name, file, settings, namespace: nil)
|
|
196
|
+
def instantiate(bundle_name, file, settings, namespace: nil, parts: [])
|
|
191
197
|
namespace ||= fresh_namespace(bundle_name)
|
|
198
|
+
parts.each { |part| namespace.module_eval(File.read(part), part, 1) }
|
|
192
199
|
namespace.module_eval(File.read(file), file, 1)
|
|
193
200
|
klass = Hooks.class_for(file, namespace)
|
|
194
201
|
plugin = Hooks.build_plugin(klass, settings)
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "monitor"
|
|
5
|
+
require_relative "../atomic_file"
|
|
6
|
+
require_relative "../paths"
|
|
7
|
+
|
|
8
|
+
module Samagotchi
|
|
9
|
+
module Plugin
|
|
10
|
+
# ctx.secrets (docs/plugins.md, Secrets): what a bundle keeps that must
|
|
11
|
+
# not sit in a plain data file, tokens. Per bundle: bundle mcp's "linear"
|
|
12
|
+
# and another bundle's "linear" are different secrets. A value is never
|
|
13
|
+
# logged, never in an exception's message, never in #inspect.
|
|
14
|
+
#
|
|
15
|
+
# Core never caches: #get reads the store each time; a bundle keeps the
|
|
16
|
+
# token in memory and reads again on a 401.
|
|
17
|
+
#
|
|
18
|
+
# The store is FileStore everywhere: one 0600 file per secret in a 0700
|
|
19
|
+
# folder under the state dir. The login Keychain was tried and left out
|
|
20
|
+
# (plan 3's B0, 2026-11-21): `security` takes a value without it showing
|
|
21
|
+
# in an argument list only through `security -i` on stdin, whose line is
|
|
22
|
+
# cut at about 4 KB and ends at a newline, and its prompt keeps 128
|
|
23
|
+
# characters.
|
|
24
|
+
class Secrets
|
|
25
|
+
# The store can't be reached (disk, permissions). The message names
|
|
26
|
+
# the store and the secret's name only.
|
|
27
|
+
class Unavailable < StandardError; end
|
|
28
|
+
|
|
29
|
+
# Another holder (another worker refreshing the same token) kept the
|
|
30
|
+
# secret's lock past the deadline.
|
|
31
|
+
class Busy < StandardError; end
|
|
32
|
+
|
|
33
|
+
# A name never starts with a dot (the lock folder is .locks) nor ends
|
|
34
|
+
# in .tmp (AtomicFile's temp files beside the values).
|
|
35
|
+
NAME = /\A(?!.*\.tmp\z)[a-z0-9][a-z0-9._-]{0,63}\z/
|
|
36
|
+
# The secrets this thread holds the lock of now (#locked).
|
|
37
|
+
HELD = :samagotchi_secrets_held
|
|
38
|
+
MAX_BYTES = 16 * 1024
|
|
39
|
+
LOCK_TIMEOUT = 30
|
|
40
|
+
|
|
41
|
+
# @param store [FileStore, MemoryStore] the bundle's own
|
|
42
|
+
def initialize(store)
|
|
43
|
+
@store = store
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def inspect = "#<#{self.class.name} backend=#{backend}>"
|
|
47
|
+
|
|
48
|
+
def pretty_print(printer) = printer.text(inspect)
|
|
49
|
+
|
|
50
|
+
# @return [Symbol] :file (or :memory in the bundle test kit), for a
|
|
51
|
+
# bundle's status line
|
|
52
|
+
def backend = @store.backend
|
|
53
|
+
|
|
54
|
+
# @return [String, nil] the value, nil when there is none
|
|
55
|
+
# @raise [ArgumentError] a bad name; [Unavailable]
|
|
56
|
+
def get(name)
|
|
57
|
+
@store.read(check_name(name))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @return [true]
|
|
61
|
+
# @raise [ArgumentError] a bad name or value; [Unavailable]; [Busy]
|
|
62
|
+
def set(name, value, timeout: LOCK_TIMEOUT)
|
|
63
|
+
name = check_name(name)
|
|
64
|
+
value = check_value(value)
|
|
65
|
+
locked(name, timeout) { @store.write(name, value) }
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [Boolean] whether there was one
|
|
70
|
+
def delete(name, timeout: LOCK_TIMEOUT)
|
|
71
|
+
name = check_name(name)
|
|
72
|
+
locked(name, timeout) { @store.remove(name) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Read, change, write under the secret's lock (two workers refreshing
|
|
76
|
+
# the same token don't each spend the refresh token: the second one's
|
|
77
|
+
# block gets the first one's new value). The block gets the value now
|
|
78
|
+
# (nil for none) and returns the new one; nil deletes it.
|
|
79
|
+
# @return [String, nil] what the secret holds after
|
|
80
|
+
# @raise [ArgumentError] a bad name, or a block's bad value (nothing
|
|
81
|
+
# is written then); [Unavailable]; [Busy] past +timeout+ seconds
|
|
82
|
+
def update(name, timeout: LOCK_TIMEOUT)
|
|
83
|
+
raise ArgumentError, "ctx.secrets.update needs a block" unless block_given?
|
|
84
|
+
|
|
85
|
+
name = check_name(name)
|
|
86
|
+
locked(name, timeout) do
|
|
87
|
+
value = yield @store.read(name)
|
|
88
|
+
if value.nil?
|
|
89
|
+
@store.remove(name)
|
|
90
|
+
next nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
@store.write(name, check_value(value))
|
|
94
|
+
value
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
# The store's lock on +name+; refused at once when this thread holds
|
|
101
|
+
# it already (a set, delete or update inside the secret's own update
|
|
102
|
+
# block would wait for itself: a file lock taken again by the same
|
|
103
|
+
# process conflicts, and the wait would end in Busy).
|
|
104
|
+
def locked(name, timeout, &)
|
|
105
|
+
key = @store.lock_key(name)
|
|
106
|
+
held = (Thread.current[HELD] ||= [])
|
|
107
|
+
if held.include?(key)
|
|
108
|
+
raise ArgumentError, "secret #{name} is held by this thread already (inside its own update block): " \
|
|
109
|
+
"return the new value from the block instead"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
held << key
|
|
113
|
+
begin
|
|
114
|
+
@store.locked(name, timeout: timeout, &)
|
|
115
|
+
ensure
|
|
116
|
+
held.delete(key)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def check_name(name)
|
|
121
|
+
name = name.to_s if name.is_a?(Symbol)
|
|
122
|
+
unless name.is_a?(String) && name.match?(NAME)
|
|
123
|
+
raise ArgumentError, "a secret's name is a String like \"linear\" (a-z 0-9 . _ -, 1..64, a letter or digit first, " \
|
|
124
|
+
"not ending .tmp)"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
name
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Never the value in the message.
|
|
131
|
+
def check_value(value)
|
|
132
|
+
raise ArgumentError, "a secret's value is a String (encode JSON yourself)" unless value.is_a?(String)
|
|
133
|
+
raise ArgumentError, "a secret's value is at most #{MAX_BYTES} bytes" if value.bytesize > MAX_BYTES
|
|
134
|
+
|
|
135
|
+
value
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# One file per secret, <state dir>/plugins/<bundle>/secrets/<name>,
|
|
139
|
+
# 0600 in a 0700 folder, written whole (AtomicFile). Its lock is
|
|
140
|
+
# .locks/<name> under that folder (no secret's name starts with a
|
|
141
|
+
# dot), an exclusive flock held across processes.
|
|
142
|
+
class FileStore
|
|
143
|
+
POLL = 0.02
|
|
144
|
+
LOCKS = ".locks"
|
|
145
|
+
|
|
146
|
+
# @param dir [String] the bundle's secrets folder
|
|
147
|
+
# @param clock [#call] monotonic seconds (specs pass their own)
|
|
148
|
+
# @param wait [#call] waits +seconds+ between lock tries
|
|
149
|
+
def initialize(dir, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }, wait: ->(seconds) { sleep(seconds) })
|
|
150
|
+
@dir = dir
|
|
151
|
+
@clock = clock
|
|
152
|
+
@wait = wait
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# @param bundle [String]
|
|
156
|
+
def self.for_bundle(bundle, env: ENV)
|
|
157
|
+
new(File.join(Paths.state_dir(env: env), "plugins", bundle.to_s, "secrets"))
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def backend = :file
|
|
161
|
+
|
|
162
|
+
def inspect = "#<#{self.class.name}>"
|
|
163
|
+
|
|
164
|
+
# What one thread's held locks are told apart by: the lock file.
|
|
165
|
+
def lock_key(name) = File.expand_path(lock_path(name))
|
|
166
|
+
|
|
167
|
+
def read(name)
|
|
168
|
+
File.binread(path(name)).force_encoding(Encoding::UTF_8)
|
|
169
|
+
rescue Errno::ENOENT
|
|
170
|
+
nil
|
|
171
|
+
rescue SystemCallError, IOError
|
|
172
|
+
raise Unavailable, "the secrets folder can't be read (secret #{name})"
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def write(name, value)
|
|
176
|
+
folder
|
|
177
|
+
AtomicFile.write(path(name), value, perm: 0o600)
|
|
178
|
+
rescue SystemCallError, IOError
|
|
179
|
+
raise Unavailable, "the secrets folder can't be written (secret #{name})"
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def remove(name)
|
|
183
|
+
File.delete(path(name))
|
|
184
|
+
true
|
|
185
|
+
rescue Errno::ENOENT
|
|
186
|
+
false
|
|
187
|
+
rescue SystemCallError
|
|
188
|
+
raise Unavailable, "the secrets folder can't be written (secret #{name})"
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# @raise [Busy] the lock still held after +timeout+ seconds
|
|
192
|
+
def locked(name, timeout:)
|
|
193
|
+
lock = open_lock(name)
|
|
194
|
+
begin
|
|
195
|
+
take(lock, name, timeout)
|
|
196
|
+
yield
|
|
197
|
+
ensure
|
|
198
|
+
lock.close
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
private
|
|
203
|
+
|
|
204
|
+
def path(name) = File.join(@dir, name)
|
|
205
|
+
|
|
206
|
+
def lock_path(name) = File.join(@dir, LOCKS, name)
|
|
207
|
+
|
|
208
|
+
def folder
|
|
209
|
+
FileUtils.mkdir_p(@dir, mode: 0o700)
|
|
210
|
+
File.chmod(0o700, @dir)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def open_lock(name)
|
|
214
|
+
folder
|
|
215
|
+
locks = File.join(@dir, LOCKS)
|
|
216
|
+
FileUtils.mkdir_p(locks, mode: 0o700)
|
|
217
|
+
File.chmod(0o700, locks)
|
|
218
|
+
File.open(lock_path(name), File::RDWR | File::CREAT, 0o600)
|
|
219
|
+
rescue SystemCallError, IOError
|
|
220
|
+
raise Unavailable, "the secrets folder can't be locked (secret #{name})"
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def take(lock, name, timeout)
|
|
224
|
+
deadline = @clock.call + timeout
|
|
225
|
+
until lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
226
|
+
raise Busy, "secret #{name} is held by another process (waited #{timeout} s)" if @clock.call >= deadline
|
|
227
|
+
|
|
228
|
+
@wait.call(POLL)
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# The bundle test kit's store: a Hash, never a disk or a Keychain.
|
|
234
|
+
# bundle.secrets reads it, bundle.set(secrets: {...}) seeds it.
|
|
235
|
+
class MemoryStore
|
|
236
|
+
def initialize(values = {})
|
|
237
|
+
@values = values
|
|
238
|
+
@mutex = Monitor.new
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def backend = :memory
|
|
242
|
+
|
|
243
|
+
def inspect = "#<#{self.class.name}>"
|
|
244
|
+
|
|
245
|
+
def to_h = @mutex.synchronize { @values.dup }
|
|
246
|
+
|
|
247
|
+
# @param values [Hash] name => value, what the store then holds
|
|
248
|
+
def replace(values)
|
|
249
|
+
@mutex.synchronize { @values = values.to_h { |name, value| [name.to_s, value.to_s.dup.freeze] } }
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def read(name) = @mutex.synchronize { @values[name]&.dup }
|
|
253
|
+
|
|
254
|
+
def write(name, value)
|
|
255
|
+
@mutex.synchronize { @values[name] = value.dup.freeze }
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def remove(name) = @mutex.synchronize { !@values.delete(name).nil? }
|
|
259
|
+
|
|
260
|
+
def lock_key(name) = [object_id, name]
|
|
261
|
+
|
|
262
|
+
def locked(_name, timeout:, &) = @mutex.synchronize(&)
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|