samagotchi 0.195.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 +41 -1
- data/docs/architecture.md +2 -1
- data/docs/bundle-authoring.md +23 -23
- data/docs/bundles.md +11 -3
- data/docs/cli.md +4 -0
- data/docs/context.md +13 -3
- data/docs/guardrails.md +6 -4
- data/docs/hooks.md +3 -1
- data/docs/internals/block-renderers.md +4 -1
- data/docs/internals/engine.md +2 -1
- data/docs/internals/knowledge.md +15 -5
- data/docs/internals/participants.md +15 -3
- data/docs/internals/peers.md +12 -8
- data/docs/internals/session-lifecycle.md +26 -12
- data/docs/internals/web-frontend.md +64 -27
- data/docs/pages.md +1 -1
- data/docs/peers.md +1 -1
- data/docs/plugins.md +73 -3
- data/docs/sessions.md +1 -1
- data/docs/system-one.md +3 -1
- data/lib/samagotchi/bundle_manifest.rb +30 -5
- 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 +2 -2
- data/lib/samagotchi/bundles/github-pr/manifest.yml +4 -2
- data/lib/samagotchi/bundles/knowledge/chi_adapters.rb +43 -25
- data/lib/samagotchi/bundles/knowledge/manifest.yml +6 -4
- data/lib/samagotchi/bundles/knowledge/plugin.rb +37 -18
- 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/targets.rb +24 -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/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 +5 -1
- data/lib/samagotchi/plugin/chips.rb +125 -0
- data/lib/samagotchi/plugin/context.rb +19 -1
- data/lib/samagotchi/plugin/peer_pairing.rb +2 -2
- data/lib/samagotchi/plugin/peer_start.rb +1 -1
- data/lib/samagotchi/plugin/peers.rb +5 -2
- data/lib/samagotchi/plugin/tool_spec.rb +19 -9
- data/lib/samagotchi/schedules.rb +8 -2
- 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 +5 -4
- data/lib/samagotchi/session_stores.rb +3 -0
- data/lib/samagotchi/speaker.rb +228 -0
- data/lib/samagotchi/steer.rb +36 -132
- 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/tools/registry.rb +8 -3
- data/lib/samagotchi/turn_excerpts.rb +8 -4
- 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 +12 -1
data/lib/samagotchi/schedules.rb
CHANGED
|
@@ -247,15 +247,21 @@ module Samagotchi
|
|
|
247
247
|
end
|
|
248
248
|
|
|
249
249
|
# @param id_or_name [String] an id, a unique prefix of one, or a title
|
|
250
|
-
# @return [Entry, nil]
|
|
250
|
+
# @return [Entry, nil] nil too for the exact id of an entry that doesn't
|
|
251
|
+
# read: an exact id wins over another schedule's prefix or title, so
|
|
252
|
+
# chi schedule cancel ID reaches that entry (#cancel_unreadable) and
|
|
253
|
+
# show, edit, enable and disable say "no schedule" rather than take a
|
|
254
|
+
# different one
|
|
251
255
|
# @raise [Usage] the prefix or the title names more than one schedule
|
|
252
256
|
def lookup(id_or_name)
|
|
253
257
|
given = id_or_name.to_s.strip
|
|
254
258
|
return nil if given.empty?
|
|
255
259
|
|
|
256
|
-
|
|
260
|
+
records = read.entries
|
|
261
|
+
list = records.grep(Entry)
|
|
257
262
|
exact = list.find { |entry| entry.id == given }
|
|
258
263
|
return exact if exact
|
|
264
|
+
return nil if records.any? { |record| !record.is_a?(Entry) && record.raw.is_a?(Hash) && record.raw["id"] == given }
|
|
259
265
|
|
|
260
266
|
matches = ambiguous(list.select { |entry| entry.id.start_with?(given) }, given)
|
|
261
267
|
return matches.first unless matches.empty?
|
|
@@ -13,6 +13,7 @@ require_relative "../note_delivery"
|
|
|
13
13
|
require_relative "../image_store"
|
|
14
14
|
require_relative "../context_candidates"
|
|
15
15
|
require_relative "../worker_inbound"
|
|
16
|
+
require_relative "../context_change"
|
|
16
17
|
require_relative "../context_sources"
|
|
17
18
|
require_relative "../peer_exchanges"
|
|
18
19
|
require_relative "../peer_mailbox"
|
|
@@ -23,7 +24,7 @@ require_relative "../peer_say"
|
|
|
23
24
|
require_relative "../archive_store"
|
|
24
25
|
require_relative "../session_chain"
|
|
25
26
|
require_relative "../session_chain/successor"
|
|
26
|
-
require_relative "../
|
|
27
|
+
require_relative "../session_chain/sharing"
|
|
27
28
|
require_relative "../send_command/busy"
|
|
28
29
|
require_relative "../chi_invocation"
|
|
29
30
|
require_relative "../worker_sidecar"
|
|
@@ -368,7 +369,7 @@ module Samagotchi
|
|
|
368
369
|
|
|
369
370
|
# Attaches +source+ to the target's own sources.
|
|
370
371
|
# @return [ContextAdded]
|
|
371
|
-
# A session's own: under its transition lock (
|
|
372
|
+
# A session's own: under its transition lock (ContextChange.under).
|
|
372
373
|
# @raise [ContextSources::Held] a transition holds the session's lock
|
|
373
374
|
def context_add(target, source)
|
|
374
375
|
location = context_location(target)
|
|
@@ -381,9 +382,10 @@ module Samagotchi
|
|
|
381
382
|
# id): a source of its own, or none of the name. A project's source
|
|
382
383
|
# takes it (the next link sees it too). The session's own source is
|
|
383
384
|
# checked again and written under a shared hold of its transition lock
|
|
384
|
-
# (
|
|
385
|
-
# can't start in between;
|
|
386
|
-
#
|
|
385
|
+
# (SessionChain::Sharing.under), so a continue (which moves its
|
|
386
|
+
# sources) can't start in between; a transition holding the lock now
|
|
387
|
+
# refuses it, naming who continues the session (:continued: a
|
|
388
|
+
# continue's new link in flight, else a saved successor) or :busy.
|
|
387
389
|
# @param fetched [ContextSources::Fetched]
|
|
388
390
|
# @return [ContextPush]
|
|
389
391
|
def context_push(target, name, fetched)
|
|
@@ -398,11 +400,11 @@ module Samagotchi
|
|
|
398
400
|
# Its file is gone: no hold (it would make the folder again).
|
|
399
401
|
return context_push_to(target, name, fetched, attached) unless Session.exist?(sid, **dir)
|
|
400
402
|
|
|
401
|
-
|
|
402
|
-
case (moved = moved_on(sid,
|
|
403
|
-
when
|
|
404
|
-
when
|
|
405
|
-
|
|
403
|
+
SessionChain::Sharing.under(sid, state_dir: state_dir!) do |sharing|
|
|
404
|
+
case (moved = moved_on(sid, sharing))
|
|
405
|
+
when SessionChain::Sharing::Held then next ContextPush.new(status: :busy)
|
|
406
|
+
when nil then nil
|
|
407
|
+
else next ContextPush.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
406
408
|
end
|
|
407
409
|
|
|
408
410
|
context_push_to(target, name, fetched, attached)
|
|
@@ -698,11 +700,11 @@ module Samagotchi
|
|
|
698
700
|
end
|
|
699
701
|
return PeerPaired.new(status: :old_worker) if old_peer_worker?(id)
|
|
700
702
|
|
|
701
|
-
|
|
702
|
-
case (moved = moved_on(id,
|
|
703
|
-
when
|
|
704
|
-
when
|
|
705
|
-
|
|
703
|
+
SessionChain::Sharing.under(id, state_dir: state_dir!) do |sharing|
|
|
704
|
+
case (moved = moved_on(id, sharing))
|
|
705
|
+
when SessionChain::Sharing::Held then next PeerPaired.new(status: :busy)
|
|
706
|
+
when nil then nil
|
|
707
|
+
else next PeerPaired.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
706
708
|
end
|
|
707
709
|
|
|
708
710
|
link = PeerMailbox.pair(id, peer, address: address, from: from, peer_session: peer_session, state_dir: state_dir!)
|
|
@@ -735,11 +737,11 @@ module Samagotchi
|
|
|
735
737
|
message = nil
|
|
736
738
|
start_failed = false
|
|
737
739
|
delivered = begin
|
|
738
|
-
|
|
739
|
-
case (moved = moved_on(id,
|
|
740
|
-
when
|
|
741
|
-
when
|
|
742
|
-
|
|
740
|
+
SessionChain::Sharing.under(id, state_dir: state_dir!) do |sharing|
|
|
741
|
+
case (moved = moved_on(id, sharing))
|
|
742
|
+
when SessionChain::Sharing::Held then return PeerSay.new(status: :busy)
|
|
743
|
+
when nil then nil
|
|
744
|
+
else return PeerSay.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
743
745
|
end
|
|
744
746
|
return unpaired unless PeerMailbox.link(id, peer, state_dir: state_dir!)
|
|
745
747
|
|
|
@@ -922,24 +924,23 @@ module Samagotchi
|
|
|
922
924
|
SessionChain::Successor.of(id, state_dir: state_dir!)
|
|
923
925
|
end
|
|
924
926
|
|
|
925
|
-
# Inside
|
|
926
|
-
# taken): why an arrival can't be written
|
|
927
|
-
#
|
|
928
|
-
#
|
|
929
|
-
#
|
|
930
|
-
#
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
end
|
|
927
|
+
# Inside SessionChain::Sharing.under(+id+) (+sharing+ its value, nil
|
|
928
|
+
# when the shared hold was taken): why an arrival can't be written
|
|
929
|
+
# now. Held: the lock is held and nobody is named (another change, a
|
|
930
|
+
# continue before its intent or after its undo, a delete with no saved
|
|
931
|
+
# successor). A Sharing::Moved (a continue's new link in flight, else
|
|
932
|
+
# a saved successor, under any holder's kind) or, the hold taken, a
|
|
933
|
+
# Successor (#continued_as: archived, with its anchor's head): both
|
|
934
|
+
# answer #id and #in_flight?. nil: it goes in.
|
|
935
|
+
# @return [SessionChain::Sharing::Moved, SessionChain::Sharing::Held, SessionChain::Successor, nil]
|
|
936
|
+
def moved_on(id, sharing) = sharing || continued_as(id)
|
|
936
937
|
|
|
937
938
|
# The block, for a session target under its transition lock
|
|
938
|
-
# (
|
|
939
|
+
# (ContextChange.under); a project's as is.
|
|
939
940
|
def own_change(target, &)
|
|
940
941
|
return yield if target.project?
|
|
941
942
|
|
|
942
|
-
|
|
943
|
+
ContextChange.under(target.session_id, state_dir: state_dir!, &)
|
|
943
944
|
end
|
|
944
945
|
|
|
945
946
|
def state_dir! = @state_dir || Session.default_state_dir
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../session"
|
|
4
|
+
require_relative "../session_transition"
|
|
5
|
+
require_relative "successor"
|
|
6
|
+
|
|
7
|
+
module Samagotchi
|
|
8
|
+
module SessionChain
|
|
9
|
+
# The one answer an arrival gets when it takes a session's transition
|
|
10
|
+
# lock shared (SessionTransition.shared) and another holds it
|
|
11
|
+
# exclusively: moved (Moved: who continues the session, and the kind of
|
|
12
|
+
# the hold) or held (Held: nobody to name). Each caller decides what it
|
|
13
|
+
# does with it: the inbox names nobody under a delete, a stop or an
|
|
14
|
+
# archive, a context change says Busy's words there, a peer say, pair or
|
|
15
|
+
# push names the saved successor whatever the kind.
|
|
16
|
+
#
|
|
17
|
+
# The rule (docs/internals/session-lifecycle.md, The transition lock),
|
|
18
|
+
# on SessionTransition::Busy.held's read of the holder:
|
|
19
|
+
# 1. a continue in flight that names its new link (Busy#successor_id:
|
|
20
|
+
# never under a delete, a stop or an archive, whatever a crashed
|
|
21
|
+
# continue left) → Moved(kind: :continue, in flight);
|
|
22
|
+
# 2. a saved successor (Successor.of without the anchor), under every
|
|
23
|
+
# kind → Moved(kind: the holder's, saved);
|
|
24
|
+
# 3. else Held(kind: the holder's).
|
|
25
|
+
# The saved lookup lists the sessions (Links): one Session.list per
|
|
26
|
+
# refused arrival with no continue in flight, only while a transition
|
|
27
|
+
# holds the lock.
|
|
28
|
+
module Sharing
|
|
29
|
+
# +busy+ is the holder's Busy (Busy.held), +successor+ a Successor.
|
|
30
|
+
Moved = Data.define(:kind, :successor, :busy) do
|
|
31
|
+
def id = successor.id
|
|
32
|
+
def in_flight? = successor.in_flight?
|
|
33
|
+
|
|
34
|
+
# The refusal naming the new link ("is being continued as Y", "was
|
|
35
|
+
# continued as S"): callers add what to do next as a tail.
|
|
36
|
+
# @return [Continued]
|
|
37
|
+
def refusal = Continued.for(busy.session_id, successor)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# +busy+ is the holder's Busy (Busy.held): its words name the holder.
|
|
41
|
+
Held = Data.define(:kind, :busy) do
|
|
42
|
+
# @return [SessionTransition::Busy]
|
|
43
|
+
def refusal = busy
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
module_function
|
|
47
|
+
|
|
48
|
+
# The block under a shared hold of +id+'s transition lock, its
|
|
49
|
+
# argument nil when the hold was taken (or this thread holds it
|
|
50
|
+
# exclusively), else a Moved or a Held by the rule above. An id that
|
|
51
|
+
# is no session id fails closed, as SessionTransition.shared's false:
|
|
52
|
+
# Held, nobody named, Busy's plain words (callers check the id first).
|
|
53
|
+
# Nothing is held while a Moved or a Held is yielded.
|
|
54
|
+
# @yieldparam sharing [Moved, Held, nil]
|
|
55
|
+
# @return the block's value
|
|
56
|
+
def under(id, state_dir:)
|
|
57
|
+
return yield(Held.new(kind: nil, busy: SessionTransition::Busy.new(id))) unless Session.valid_id?(id)
|
|
58
|
+
|
|
59
|
+
SessionTransition.shared(id, state_dir: state_dir) do |free|
|
|
60
|
+
next yield(nil) if free
|
|
61
|
+
|
|
62
|
+
yield answer(id, state_dir: state_dir)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# What a held lock of +id+ answers (the rule).
|
|
67
|
+
# @return [Moved, Held]
|
|
68
|
+
def answer(id, state_dir:)
|
|
69
|
+
busy = SessionTransition::Busy.held(id, state_dir: state_dir)
|
|
70
|
+
if busy.successor_id
|
|
71
|
+
return Moved.new(kind: :continue, successor: Successor.new(id: busy.successor_id, via: :in_flight), busy: busy)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
successor = Successor.of(id, state_dir: state_dir, anchor: false)
|
|
75
|
+
successor ? Moved.new(kind: busy.kind, successor: successor, busy: busy) : Held.new(kind: busy.kind, busy: busy)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -13,8 +13,10 @@ module Samagotchi
|
|
|
13
13
|
# or just saved: ContinueIntent.in_flight).
|
|
14
14
|
#
|
|
15
15
|
# Who asks what: SessionAccess::Local#continued_as (peer say/pair, a
|
|
16
|
-
# context push: .of,
|
|
17
|
-
#
|
|
16
|
+
# context push, the hold taken: .of), SessionChain::Sharing (a shared
|
|
17
|
+
# hold that met a transition: Busy.held's in-flight read, then .of
|
|
18
|
+
# without the anchor), SessionContinue's check (.of, no in-flight: it
|
|
19
|
+
# holds the lock itself),
|
|
18
20
|
# follow_guard (.of without the anchor while it holds the lock, as it
|
|
19
21
|
# always asked; .in_flight when it can't), SessionInbox::Head#continuing
|
|
20
22
|
# and Schedules#continuing_reason (.in_flight alone: their walk already
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "monitor"
|
|
6
|
+
require "time"
|
|
7
|
+
require_relative "atomic_file"
|
|
8
|
+
require_relative "session_paths"
|
|
9
|
+
|
|
10
|
+
module Samagotchi
|
|
11
|
+
# The chips bundles draw in a session's bar (ctx.chips, docs/plugins.md,
|
|
12
|
+
# Chips): chips.json in the session's own folder (beside cards.json), so
|
|
13
|
+
# chi web serves them with no worker running. Written whole through
|
|
14
|
+
# AtomicFile: {"<bundle>" => {"<name>" => {"label", "state", "dot", "title",
|
|
15
|
+
# "at", "body", "actions", "set_at"}}}. Checked chips only: Plugin::Chips
|
|
16
|
+
# builds them. A continue or a fork copies nothing (the bundle sets its
|
|
17
|
+
# chips again); a delete removes the folder. The file doesn't make a
|
|
18
|
+
# session count as used (SessionManager's emptiness).
|
|
19
|
+
class SessionChips
|
|
20
|
+
FILE = "chips.json"
|
|
21
|
+
# A chip's key a write doesn't compare: the same content again writes
|
|
22
|
+
# nothing and announces nothing.
|
|
23
|
+
STAMP = "set_at"
|
|
24
|
+
# Every write in the process, every session's: they are rare (a bundle's
|
|
25
|
+
# change), and one lock keeps a read-change-write whole.
|
|
26
|
+
LOCK = Monitor.new
|
|
27
|
+
|
|
28
|
+
# @return [String] the session's chips.json
|
|
29
|
+
def self.path(session_id, state_dir:) = File.join(SessionPaths.session_dir(session_id, state_dir: state_dir), FILE)
|
|
30
|
+
|
|
31
|
+
# A session's chips, every bundle's: {} for none, or a file that doesn't
|
|
32
|
+
# read (or isn't a mapping of mappings).
|
|
33
|
+
# @return [Hash{String => Hash{String => Hash}}]
|
|
34
|
+
def self.read(session_id, state_dir:)
|
|
35
|
+
from_file(path(session_id, state_dir: state_dir))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.from_file(path)
|
|
39
|
+
data = JSON.parse(File.read(path))
|
|
40
|
+
return {} unless data.is_a?(Hash)
|
|
41
|
+
|
|
42
|
+
data.select { |bundle, chips| bundle.is_a?(String) && chips.is_a?(Hash) && chips.values.all?(Hash) }
|
|
43
|
+
rescue JSON::ParserError, SystemCallError
|
|
44
|
+
{}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Whether two chip lists of a bundle show the same (their stamps aside).
|
|
48
|
+
def self.same?(one, other)
|
|
49
|
+
strip = ->(chips) { chips.transform_values { |chip| chip.except(STAMP) } }
|
|
50
|
+
strip.call(one) == strip.call(other)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# +fresh+ with each chip that didn't change keeping its stamp from
|
|
54
|
+
# +old+, the others stamped +now+.
|
|
55
|
+
def self.stamped(old, fresh, now)
|
|
56
|
+
fresh.to_h do |name, chip|
|
|
57
|
+
was = old[name]
|
|
58
|
+
stamp = was && was.except(STAMP) == chip.except(STAMP) ? was[STAMP] : now
|
|
59
|
+
[name, chip.merge(STAMP => stamp)]
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# @param session_id [String]
|
|
64
|
+
# @param state_dir [String] the sessions dir
|
|
65
|
+
# @param on_change [#call, nil] called after a write that changed
|
|
66
|
+
# something (the Engine's :chips_updated), outside the lock
|
|
67
|
+
def initialize(session_id, state_dir:, on_change: nil)
|
|
68
|
+
@path = self.class.path(session_id, state_dir: state_dir)
|
|
69
|
+
@on_change = on_change
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @return [Hash{String => Hash{String => Hash}}] every bundle's chips now
|
|
73
|
+
def all = self.class.from_file(@path)
|
|
74
|
+
|
|
75
|
+
# Read +bundle+'s chips, change them, write the file when they changed.
|
|
76
|
+
# @yieldparam mine [Hash{String => Hash}] the bundle's chips now (a copy)
|
|
77
|
+
# @yieldreturn [Hash{String => Hash}] what they are after
|
|
78
|
+
# @return [Boolean] whether anything was written (and announced)
|
|
79
|
+
def update(bundle)
|
|
80
|
+
changed = LOCK.synchronize do
|
|
81
|
+
chips = all
|
|
82
|
+
old = chips[bundle] || {}
|
|
83
|
+
fresh = yield(old.transform_values(&:dup))
|
|
84
|
+
next false if self.class.same?(old, fresh)
|
|
85
|
+
|
|
86
|
+
fresh = self.class.stamped(old, fresh, Time.now.utc.iso8601)
|
|
87
|
+
fresh.empty? ? chips.delete(bundle) : chips[bundle] = fresh
|
|
88
|
+
write(chips)
|
|
89
|
+
true
|
|
90
|
+
end
|
|
91
|
+
@on_change&.call if changed
|
|
92
|
+
changed
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
private
|
|
96
|
+
|
|
97
|
+
def write(chips)
|
|
98
|
+
if chips.empty?
|
|
99
|
+
FileUtils.rm_f(@path)
|
|
100
|
+
else
|
|
101
|
+
FileUtils.mkdir_p(File.dirname(@path))
|
|
102
|
+
AtomicFile.write(@path, "#{JSON.generate(chips)}\n")
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The bundle test kit's store (Testing::Recorder#chips): the same
|
|
107
|
+
# update, in memory; #writes counts the changes.
|
|
108
|
+
class Memory
|
|
109
|
+
attr_reader :writes
|
|
110
|
+
|
|
111
|
+
def initialize
|
|
112
|
+
@chips = {}
|
|
113
|
+
@writes = 0
|
|
114
|
+
@mutex = Monitor.new
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def all = @mutex.synchronize { @chips.transform_values(&:dup) }
|
|
118
|
+
|
|
119
|
+
def update(bundle)
|
|
120
|
+
@mutex.synchronize do
|
|
121
|
+
old = @chips[bundle] || {}
|
|
122
|
+
fresh = yield(old.transform_values(&:dup))
|
|
123
|
+
next false if SessionChips.same?(old, fresh)
|
|
124
|
+
|
|
125
|
+
fresh = SessionChips.stamped(old, fresh, Time.now.utc.iso8601)
|
|
126
|
+
fresh.empty? ? @chips.delete(bundle) : @chips[bundle] = fresh
|
|
127
|
+
@writes += 1
|
|
128
|
+
true
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -7,6 +7,7 @@ require_relative "client_id"
|
|
|
7
7
|
require_relative "worker_presence"
|
|
8
8
|
require_relative "continue_intent"
|
|
9
9
|
require_relative "session_chain/links"
|
|
10
|
+
require_relative "session_chain/sharing"
|
|
10
11
|
require_relative "session_chain/successor"
|
|
11
12
|
require_relative "session_transition"
|
|
12
13
|
|
|
@@ -286,9 +287,9 @@ module Samagotchi
|
|
|
286
287
|
|
|
287
288
|
# The block under +policy+'s hold of +id+, or, when a transition
|
|
288
289
|
# holds the lock now (not waited for), +refused+ called with who
|
|
289
|
-
# continues +id+ (
|
|
290
|
-
#
|
|
291
|
-
# words). A :none row,
|
|
290
|
+
# continues +id+ (SessionChain::Sharing's Moved: the holding
|
|
291
|
+
# continue's new link, in flight, else a saved successor, its
|
|
292
|
+
# SessionChain::Successor) or nil (busy: Busy's words). A :none row,
|
|
292
293
|
# or an id that is no session id (the caller's own refusal follows),
|
|
293
294
|
# is the block alone.
|
|
294
295
|
# @param refused [#call] (successor) the refusal's value
|
|
@@ -296,17 +297,16 @@ module Samagotchi
|
|
|
296
297
|
def under(policy, id, state_dir, refused:)
|
|
297
298
|
return yield unless policy.shared? && Session.valid_id?(id)
|
|
298
299
|
|
|
299
|
-
|
|
300
|
-
next yield if
|
|
300
|
+
SessionChain::Sharing.under(id, state_dir: state_dir) do |sharing|
|
|
301
|
+
next yield if sharing.nil?
|
|
301
302
|
|
|
302
303
|
# A delete, a stop or an archive holding it names nobody (Busy's
|
|
303
304
|
# "being deleted": a link being deleted or pruned is not
|
|
304
|
-
# continued anywhere new)
|
|
305
|
-
#
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
refused.call(
|
|
309
|
-
SessionChain::Successor.of(id, state_dir: state_dir, anchor: false))
|
|
305
|
+
# continued anywhere new), on purpose, even with a saved
|
|
306
|
+
# successor; a holder that names no kind (an older chi's
|
|
307
|
+
# continue) still names it.
|
|
308
|
+
moved = sharing.is_a?(SessionChain::Sharing::Moved) && !SessionTransition.other_than_continue?(sharing.kind)
|
|
309
|
+
refused.call(moved ? sharing.successor : nil)
|
|
310
310
|
end
|
|
311
311
|
end
|
|
312
312
|
end
|
|
@@ -5,6 +5,7 @@ require_relative "../session_inbox"
|
|
|
5
5
|
require_relative "../worker_link"
|
|
6
6
|
require_relative "../starting_marker"
|
|
7
7
|
require_relative "../session_transition"
|
|
8
|
+
require_relative "../session_chips"
|
|
8
9
|
|
|
9
10
|
module Samagotchi
|
|
10
11
|
class SessionManager
|
|
@@ -17,8 +18,10 @@ module Samagotchi
|
|
|
17
18
|
# The starting marker is one too: a start left one behind; a fresh one
|
|
18
19
|
# keeps the session from the deleters itself. So is the transition lock
|
|
19
20
|
# (a send's shared hold leaves it: SessionInbox::Hold).
|
|
21
|
+
# A bundle's chips (SessionChips) are its own, not the user's: a bundle
|
|
22
|
+
# that sets one at session start leaves an untouched session discardable.
|
|
20
23
|
EMPTY_SKELETON_FILES = ["pid", "owner.lock", WorkerLink::FILE, AnalyticsFile::NAME, StartingMarker::FILE,
|
|
21
|
-
SessionTransition::LOCK].freeze
|
|
24
|
+
SessionTransition::LOCK, SessionChips::FILE].freeze
|
|
22
25
|
EMPTY_DIRS = [SessionInbox::INPUT_DIR, SessionInbox::NOTES_DIR, "images"].freeze
|
|
23
26
|
EMPTY_SKELETON_DIRS = (EMPTY_DIRS + [SessionInbox::OUTPUT_DIR]).freeze
|
|
24
27
|
|
|
@@ -33,7 +33,7 @@ module Samagotchi
|
|
|
33
33
|
ANNOUNCEABLE_EVENTS = %i[turn_enqueued turn_dequeued input_merged prompt_restored continue_offered continue_resolved
|
|
34
34
|
command_queued command_ran context_added card hook_notice guardrail_warning
|
|
35
35
|
plugin_init_started plugin_init_finished working_set_updated participants_changed
|
|
36
|
-
verdict_recorded objectives_updated].freeze
|
|
36
|
+
verdict_recorded objectives_updated chips_updated].freeze
|
|
37
37
|
|
|
38
38
|
# Handle returned by #subscribe. Holds the observer reference and carries
|
|
39
39
|
# #unsubscribe so a caller can deregister without keeping the registry.
|
|
@@ -14,6 +14,7 @@ require_relative "session_window"
|
|
|
14
14
|
require_relative "model_ref"
|
|
15
15
|
require_relative "message_time"
|
|
16
16
|
require_relative "steer"
|
|
17
|
+
require_relative "speaker"
|
|
17
18
|
require_relative "paths"
|
|
18
19
|
|
|
19
20
|
module Samagotchi
|
|
@@ -308,9 +309,9 @@ module Samagotchi
|
|
|
308
309
|
FAILURE_RULES.find { |_, pattern| pattern.match?(line) }&.first || "other"
|
|
309
310
|
end
|
|
310
311
|
|
|
311
|
-
# The
|
|
312
|
-
# and the plugins' steers by source, whose time is in the window.
|
|
313
|
-
# kind, source, author, turn_start and the time are looked at.
|
|
312
|
+
# The owner's mid-turn lines (input Speaker.of reads as :owner, no turn
|
|
313
|
+
# start) and the plugins' steers by source, whose time is in the window.
|
|
314
|
+
# Only role, kind, source, author, turn_start and the time are looked at.
|
|
314
315
|
# @return [Array(Integer, Hash, Integer)] lines, steers, unreadable files
|
|
315
316
|
def message_counts(id, since)
|
|
316
317
|
data = JSON.parse(File.read(Session.session_file(id)))
|
|
@@ -323,7 +324,7 @@ module Samagotchi
|
|
|
323
324
|
|
|
324
325
|
case message["kind"]
|
|
325
326
|
when Steer::INPUT_KIND
|
|
326
|
-
lines += 1 if
|
|
327
|
+
lines += 1 if Speaker.of(message) == :owner && !message["turn_start"]
|
|
327
328
|
when Steer::KIND then steers[message["source"].to_s] += 1
|
|
328
329
|
end
|
|
329
330
|
end
|
|
@@ -50,6 +50,9 @@ module Samagotchi
|
|
|
50
50
|
continue: "the bridge endpoint belongs to the old worker", corpus_dir: SIDE_CAR),
|
|
51
51
|
Store.new(file: "cards.json", owner: "Bridge::CardStore", removal: :session_files, cross_version: true,
|
|
52
52
|
continue: "cards describe turns in the previous link", corpus_dir: SIDE_CAR),
|
|
53
|
+
# The bundles' own chips (ctx.chips): read by chi web with no worker running.
|
|
54
|
+
Store.new(file: "chips.json", owner: "SessionChips", removal: :session_files, cross_version: true,
|
|
55
|
+
continue: "a continue copies nothing; the bundle sets its chips again", corpus_dir: SIDE_CAR),
|
|
53
56
|
Store.new(file: "pending_card.json", owner: "Bridge::PendingCard", removal: :session_files, cross_version: true,
|
|
54
57
|
continue: "an open card belongs to its running turn", corpus_dir: SIDE_CAR),
|
|
55
58
|
Store.new(file: "turn_drawings.jsonl", owner: "Bridge::TurnDrawings", removal: :session_files, cross_version: true,
|