samagotchi 0.162.0 → 0.164.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 +42 -1
- data/docs/architecture.md +2 -2
- data/docs/bundles.md +4 -2
- data/docs/cli.md +7 -2
- data/docs/configuration.md +1 -1
- data/docs/internals/block-renderers.md +3 -1
- data/docs/internals/bridge.md +37 -24
- data/docs/internals/inline-widgets.md +5 -2
- data/docs/internals/remote-sessions.md +7 -2
- data/docs/internals/web-frontend.md +1 -1
- data/docs/memory.md +8 -2
- data/docs/sessions.md +3 -2
- data/docs/sub-agent.md +11 -0
- data/docs/testing.md +8 -2
- data/lib/samagotchi/block_renderers.rb +14 -4
- data/lib/samagotchi/bridge/turn_drawings/synth.rb +7 -3
- data/lib/samagotchi/bridge/turn_drawings.rb +45 -30
- data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
- data/lib/samagotchi/bundles/inline-widgets/plugin.rb +8 -4
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/cli/parent_wait.rb +5 -1
- data/lib/samagotchi/continue_intent.rb +48 -0
- data/lib/samagotchi/message_parts.rb +13 -0
- data/lib/samagotchi/note_command.rb +3 -6
- data/lib/samagotchi/parent_report.rb +3 -2
- data/lib/samagotchi/project_command.rb +74 -6
- data/lib/samagotchi/project_subfolders.rb +188 -0
- data/lib/samagotchi/send_command/parent_gate.rb +8 -5
- data/lib/samagotchi/send_command.rb +139 -34
- data/lib/samagotchi/session_access/local.rb +6 -0
- data/lib/samagotchi/session_access/remote.rb +25 -0
- data/lib/samagotchi/session_continue.rb +5 -3
- data/lib/samagotchi/session_inbox_delivery.rb +54 -7
- data/lib/samagotchi/session_manager/delivery.rb +3 -1
- data/lib/samagotchi/terminal_ui/join_replay.rb +2 -2
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/message_display.rb +45 -36
- data/lib/samagotchi/web/public/app.js +8 -7
- data/lib/samagotchi/web/public/apple-touch-icon.png +0 -0
- data/lib/samagotchi/web/public/data.js +4 -6
- data/lib/samagotchi/web/public/favicon.svg +1 -1
- data/lib/samagotchi/web/public/icons/chi-180.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-192.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-512.png +0 -0
- data/lib/samagotchi/web/public/inline_widgets.js +4 -1
- data/lib/samagotchi/web/public/reload_check.js +7 -4
- data/lib/samagotchi/web/public/time_gaps.js +1 -2
- data/lib/samagotchi/web/public/timing.js +49 -224
- data/lib/samagotchi/web/public/turn_block.js +40 -122
- data/lib/samagotchi/web/public/turn_html.js +3 -2
- data/lib/samagotchi/web/public/widget_walk.js +14 -4
- data/lib/samagotchi/web/reload_check_route.rb +6 -3
- data/lib/samagotchi/web/session_access_routes.rb +8 -1
- data/lib/samagotchi/web/session_show.rb +8 -24
- metadata +3 -1
|
@@ -38,6 +38,9 @@ module Samagotchi
|
|
|
38
38
|
# Languages core draws itself: a bundle's renderer for one would be a
|
|
39
39
|
# second drawer for the same block.
|
|
40
40
|
RESERVED = [InlineWidgets::LANG].freeze
|
|
41
|
+
# The [bundle, problem] pairs .renderers has logged in this process.
|
|
42
|
+
WARNED = Set.new
|
|
43
|
+
WARNED_LOCK = Mutex.new
|
|
41
44
|
|
|
42
45
|
module_function
|
|
43
46
|
|
|
@@ -97,25 +100,32 @@ module Samagotchi
|
|
|
97
100
|
# owner's browser, so it is checked as a plugin's load and a provider's
|
|
98
101
|
# script are; a --dev install's links are not.
|
|
99
102
|
# @return [String, nil] nil when no such renderer loads such a file
|
|
100
|
-
# @raise [Invalid] the file is gone or changed
|
|
103
|
+
# @raise [Invalid] the file is gone, has no recorded sha, or changed
|
|
101
104
|
def script_path(bundle, file)
|
|
102
105
|
return nil unless for_installed.any? { |renderer| renderer.bundle == bundle && renderer.files.include?(file) }
|
|
103
106
|
|
|
104
107
|
provenance = MemoryBundle::Provenance.new(name: bundle)
|
|
105
108
|
path = File.join(provenance.scripts_dir, file)
|
|
106
109
|
recorded = provenance.record&.scripts&.fetch(file, nil)
|
|
107
|
-
|
|
110
|
+
raise Invalid, "bundle #{bundle}'s scripts/#{file} isn't among its installed scripts (reinstall the bundle)" if recorded.to_s.empty?
|
|
111
|
+
raise Invalid, "bundle #{bundle}'s scripts/#{file} is missing (reinstall the bundle)" unless File.file?(path)
|
|
112
|
+
return path if MemoryBundle::Provenance.sha_matches?(path, recorded)
|
|
108
113
|
|
|
109
114
|
raise Invalid, "bundle #{bundle}'s scripts/#{file} differs from the installed one (reinstall the bundle)"
|
|
110
115
|
end
|
|
111
116
|
|
|
112
|
-
# Its parsed renderers; a list that doesn't parse is
|
|
117
|
+
# Its parsed renderers; a kept list that doesn't parse is the record's
|
|
118
|
+
# mirror, else none. Logged once per bundle and problem: this runs on each
|
|
119
|
+
# list request and each renderer file fetch.
|
|
113
120
|
def renderers(bundle, manifest)
|
|
114
|
-
if (problem = manifest.problems["block_renderers"])
|
|
121
|
+
if (problem = manifest.problems["block_renderers"]) && WARNED_LOCK.synchronize { WARNED.add?([bundle, problem]) }
|
|
115
122
|
Log.warn(:web, "block_renderers_unreadable", bundle: bundle, msg: problem)
|
|
116
123
|
end
|
|
117
124
|
manifest.block_renderers
|
|
118
125
|
end
|
|
119
126
|
private_class_method :renderers
|
|
127
|
+
|
|
128
|
+
# Forget what was logged (specs).
|
|
129
|
+
def reset_warnings! = WARNED_LOCK.synchronize { WARNED.clear }
|
|
120
130
|
end
|
|
121
131
|
end
|
|
@@ -56,12 +56,16 @@ module Samagotchi
|
|
|
56
56
|
tool_action: "a plugin tool's own action words (its label when saved, else 'calling tool')",
|
|
57
57
|
wake_start: "a wake turn's start time when no message of its own is stamped: its note's, written before it began",
|
|
58
58
|
failure_detail: "a failed turn's error message and class when its record keeps a summary (SessionMetrics#failure_fields)",
|
|
59
|
-
repeated_turn_id: "a range whose turn id an earlier one has (…~N)
|
|
60
|
-
"
|
|
59
|
+
repeated_turn_id: "a range whose turn id an earlier one has (…~N), or a made range renamed because a kept line " \
|
|
60
|
+
"has its id (a failed turn's kept steps, then its re-run): no record (no timing line), no " \
|
|
61
|
+
"CardStore entries (the runs' records share the id: whose they are can't be told)",
|
|
61
62
|
continued_steps: "a Continue's steps right after its turn read as that turn's (no message tells them apart): " \
|
|
62
63
|
"the turn's record and kind, the Continue's calls untimed, its own CardStore entries left out",
|
|
63
64
|
unopened_turn: "a turn with no opening (a reminder's, a Continue's) has no id in the messages: a made-up one, " \
|
|
64
|
-
"and no record to pair by"
|
|
65
|
+
"and no record to pair by",
|
|
66
|
+
fork_before_first_turn: "a /btw keep child saved without its system head: the lines it carried keep the " \
|
|
67
|
+
"parent's indices and hold only once its first turn puts the head back; until " \
|
|
68
|
+
"then its turns are made, without CardStore rows (the child's store is empty)"
|
|
65
69
|
}.freeze
|
|
66
70
|
|
|
67
71
|
# A Synth line that couldn't be made (its minimal line stands in).
|
|
@@ -243,17 +243,23 @@ module Samagotchi
|
|
|
243
243
|
messages = Array(messages)
|
|
244
244
|
real = Degrade.call(:web, "turn_drawings_unreadable", fallback: []) { valid(lines, messages) }
|
|
245
245
|
ranges = TurnRanges.of(messages)
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
turn_records = list_of(timing, :turn_records)
|
|
249
|
-
records = TurnRanges.records(ranges, turn_records)
|
|
246
|
+
records = TurnRanges.records(ranges, list_of(timing, :turn_records))
|
|
247
|
+
taken = real.to_set(&:turn_id)
|
|
250
248
|
tools = list_of(timing, :tool_records).group_by { |t| field(t, :turn_id) }
|
|
251
249
|
own = Array(entries).select { |e| e.is_a?(Hash) && field(e, :of_turn) }.group_by { |e| field(e, :of_turn) }
|
|
252
|
-
made =
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
250
|
+
made = uncovered(ranges, real).filter_map do |i, part|
|
|
251
|
+
next if skip_turn_id && ranges[i].turn_id == skip_turn_id
|
|
252
|
+
|
|
253
|
+
# A made range whose id a kept line has (a failed turn's kept
|
|
254
|
+
# steps, then its re-run under the same turn id: .valid keeps the
|
|
255
|
+
# newest line for an id) is renamed "<id>~<from>": never two
|
|
256
|
+
# lines with one id (the page would draw them as one turn), and
|
|
257
|
+
# not its turn's own (no end turn_id, no record).
|
|
258
|
+
range = taken.include?(part.id) ? part.with(id: "#{part.id}~#{part.from}") : part
|
|
259
|
+
# A piece with its turn's opening is that turn's (by its own id,
|
|
260
|
+
# else by place for a turn saved before turn ids); one without
|
|
261
|
+
# (a Continue's steps after a lined turn) has no record.
|
|
262
|
+
record = records[i] if range.opening && (range.turn_id.nil? || range.own_id?)
|
|
257
263
|
# The turn's tool records by its record's id, else its own (a
|
|
258
264
|
# record lost, its tool records saved: the TUI's join does so too).
|
|
259
265
|
tool_id = record ? field(record, :id) : (range.turn_id if range.own_id?)
|
|
@@ -263,34 +269,43 @@ module Samagotchi
|
|
|
263
269
|
(real + made).sort_by(&:from)
|
|
264
270
|
end
|
|
265
271
|
|
|
266
|
-
# +ranges+
|
|
267
|
-
#
|
|
268
|
-
#
|
|
269
|
-
#
|
|
270
|
-
#
|
|
271
|
-
|
|
272
|
-
taken = lines.to_set(&:turn_id)
|
|
273
|
-
return ranges if taken.empty?
|
|
274
|
-
|
|
275
|
-
made = open.to_set
|
|
276
|
-
ranges.each_with_index.map do |range, i|
|
|
277
|
-
made.include?(i) && taken.include?(range.id) ? range.with(id: "#{range.id}~#{range.from}") : range
|
|
278
|
-
end
|
|
279
|
-
end
|
|
280
|
-
private :reserve_ids
|
|
281
|
-
|
|
282
|
-
# The indexes of +ranges+ no line of +lines+ (sorted by from, apart)
|
|
283
|
-
# overlaps: one walk over both.
|
|
272
|
+
# The parts of +ranges+ no line of +lines+ (sorted by from, apart)
|
|
273
|
+
# overlaps, as [range index, range]: a range none overlaps whole,
|
|
274
|
+
# and a range one covers in part (a Continue's steps read as the
|
|
275
|
+
# turn before's, TurnRanges, with only one of the two lined) split
|
|
276
|
+
# at the lines' bounds, so no turn item is left without a line. One
|
|
277
|
+
# walk over both.
|
|
284
278
|
def uncovered(ranges, lines)
|
|
285
279
|
at = 0
|
|
286
|
-
ranges.
|
|
287
|
-
range = ranges[i]
|
|
280
|
+
ranges.each_with_index.flat_map do |range, i|
|
|
288
281
|
at += 1 while at < lines.size && lines[at].to >= 0 && lines[at].to <= range.from
|
|
289
|
-
|
|
282
|
+
from = range.from
|
|
283
|
+
gaps = []
|
|
284
|
+
(at...lines.size).each do |k|
|
|
285
|
+
break unless lines[k].from < range.to
|
|
286
|
+
|
|
287
|
+
gaps << [from, lines[k].from] if lines[k].from > from
|
|
288
|
+
from = [from, lines[k].to].max
|
|
289
|
+
end
|
|
290
|
+
gaps << [from, range.to] if from < range.to
|
|
291
|
+
gaps.map { |a, b| [i, piece(range, a, b)] }
|
|
290
292
|
end
|
|
291
293
|
end
|
|
292
294
|
private :uncovered
|
|
293
295
|
|
|
296
|
+
# +range+'s messages [+from+, +to+): the range itself when whole; a
|
|
297
|
+
# part with its opening keeps its id; one without is made up
|
|
298
|
+
# ("<id>~<from>", no turn id: no record, no CardStore entries).
|
|
299
|
+
def piece(range, from, to)
|
|
300
|
+
return range if from == range.from && to == range.to
|
|
301
|
+
|
|
302
|
+
opening = range.opening && (range.opening < range.from ? from == range.from : (from...to).cover?(range.opening))
|
|
303
|
+
return range.with(from: from, to: to) if opening
|
|
304
|
+
|
|
305
|
+
TurnRanges::Range.new(from: from, to: to, id: "#{range.id}~#{from}", turn_id: nil, opening: nil)
|
|
306
|
+
end
|
|
307
|
+
private :piece
|
|
308
|
+
|
|
294
309
|
# A fork's drawings (Plugin::Sessions#fork, before its child's worker
|
|
295
310
|
# spawns: no other writer): the lines of +from_dir+'s file that hold
|
|
296
311
|
# against the child's seed +messages+ (same indices, same ends; a range
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: inline-widgets
|
|
3
|
-
version: 0.1.
|
|
3
|
+
version: 0.1.1
|
|
4
4
|
scope: system
|
|
5
5
|
description: "Teaches the model the chi-ui blocks chi web draws: picks (rows of options that fill your reply) and walk (a scenario stepped through)"
|
|
6
6
|
trust_level: reviewed
|
|
7
7
|
plugin:
|
|
8
8
|
file: plugin.rb
|
|
9
|
-
sha256: sha256:
|
|
9
|
+
sha256: sha256:e54a0096ba3b20ccf6bbca2798cb53afd86986a2cb7bee1c02e5520f2ddb5033
|
|
10
10
|
# chi.prompt_section is 0.132.0's; the web draws chi-ui blocks only from the next release
|
|
11
11
|
requires_chi: ">= 0.155.0"
|
|
@@ -9,8 +9,11 @@
|
|
|
9
9
|
#
|
|
10
10
|
# The text is eval 0013's round-2 hint, split in two. It is static, so the
|
|
11
11
|
# prompt cache keeps: same text at every build. A delegate child's answer is
|
|
12
|
-
# read by its parent, not drawn for a user,
|
|
13
|
-
# prints to a terminal
|
|
12
|
+
# read by its parent, not drawn for a user, a one-shot (-p --non-interactive)
|
|
13
|
+
# prints to a terminal, and a review session (chi review) is read by its
|
|
14
|
+
# participants, whose page draws no widgets: none of them gets a section. A
|
|
15
|
+
# working session the owner pairs in keeps them: the owner's page draws them,
|
|
16
|
+
# and a participant invited later mustn't change the prompt (the cache).
|
|
14
17
|
class Plugin
|
|
15
18
|
PICKS = <<~TEXT
|
|
16
19
|
The web UI draws fenced ```chi-ui code blocks (YAML inside) as interactive widgets; the terminal shows their source. Use one whenever it helps the user more than prose: a widget replaces the long explanation, it doesn't sit beside one. Never use one for what markdown shows well (tables, lists, key-value). At most one widget per answer; keep the prose around it to a few sentences and don't repeat the widget's content.
|
|
@@ -71,6 +74,7 @@ class Plugin
|
|
|
71
74
|
|
|
72
75
|
private
|
|
73
76
|
|
|
74
|
-
# Whether the session's answers reach a web page that draws them.
|
|
75
|
-
|
|
77
|
+
# Whether the session's answers reach a web page that draws them. Decided by
|
|
78
|
+
# the session's kind, so the same at every build.
|
|
79
|
+
def drawn?(ctx) = ctx.frontend != :one_shot && !ctx.delegate? && !ctx.review?
|
|
76
80
|
end
|
|
@@ -38,7 +38,7 @@ module Samagotchi
|
|
|
38
38
|
end
|
|
39
39
|
if @json && !@reported && status == 1
|
|
40
40
|
detail = @stderr.last.to_s.delete_prefix("#{command_name}: ")
|
|
41
|
-
@stdout.puts(ParentReport.error_line(detail, session_id: @session_id))
|
|
41
|
+
@stdout.puts(ParentReport.error_line(detail, session_id: @session_id, extra: error_extra))
|
|
42
42
|
@stdout.flush
|
|
43
43
|
end
|
|
44
44
|
status
|
|
@@ -89,6 +89,10 @@ module Samagotchi
|
|
|
89
89
|
Exit::FAILED
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
+
# More keys for the error object, after session_id: {} (chi send's
|
|
93
|
+
# names the id asked).
|
|
94
|
+
def error_extra = {}
|
|
95
|
+
|
|
92
96
|
# More keys for the JSON object: answered_here: false when chi
|
|
93
97
|
# answer's answer didn't go in (the question was no longer open).
|
|
94
98
|
def report_extra
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "session"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
# The files a continue (SessionContinue.run) keeps in the previous link's
|
|
8
|
+
# folder while it runs: LOCK, held for the whole continue, and FILE, its
|
|
9
|
+
# intent (the successor's id), written before the link is archived and
|
|
10
|
+
# removed once the successor is saved or the continue is undone. A send
|
|
11
|
+
# reads them (SessionInbox::Head) to tell a continue in flight from a link
|
|
12
|
+
# nobody continues.
|
|
13
|
+
module ContinueIntent
|
|
14
|
+
# One continue of a session at a time.
|
|
15
|
+
LOCK = "continue.lock"
|
|
16
|
+
# The intent: { successor_id:, previously_archived:, archived_ids: }.
|
|
17
|
+
FILE = "continue.json"
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# The successor's id of a continue of +id+ in flight: its intent written
|
|
22
|
+
# and its lock held. nil when there is none, or the intent is stale (no
|
|
23
|
+
# continue holds the lock: a crash left it, and
|
|
24
|
+
# SessionContinue.repair_interrupted undoes it) or unreadable.
|
|
25
|
+
# @return [String, nil]
|
|
26
|
+
def in_flight(id, state_dir:)
|
|
27
|
+
return nil unless Session.valid_id?(id)
|
|
28
|
+
|
|
29
|
+
dir = Session.session_dir(id, state_dir: state_dir)
|
|
30
|
+
path = File.join(dir, FILE)
|
|
31
|
+
return nil unless File.file?(path) && locked?(File.join(dir, LOCK))
|
|
32
|
+
|
|
33
|
+
data = JSON.parse(File.read(path))
|
|
34
|
+
successor = data["successor_id"] if data.is_a?(Hash)
|
|
35
|
+
Session.valid_id?(successor) ? successor : nil
|
|
36
|
+
rescue JSON::ParserError, SystemCallError
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Whether a continue holds the lock at +path+: a shared try that fails.
|
|
41
|
+
# The try is let go at once (the file is closed).
|
|
42
|
+
def locked?(path)
|
|
43
|
+
File.open(path, File::RDONLY) { |lock| !lock.flock(File::LOCK_SH | File::LOCK_NB) }
|
|
44
|
+
rescue Errno::ENOENT
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -73,6 +73,8 @@ module Samagotchi
|
|
|
73
73
|
# @return [Hash, nil] { thinking:, tools: [{ tool:, params:, title:,
|
|
74
74
|
# output:, output_truncated:, images:, tool_id:, edit: }] } with only what it found; nil for nothing
|
|
75
75
|
def for_message(message, responses, registry: Tools::Builtins.default, cwd: nil, marks: {})
|
|
76
|
+
return nil unless something?(message)
|
|
77
|
+
|
|
76
78
|
content = field(message, :content).to_s
|
|
77
79
|
calls = field(message, :tool_calls)
|
|
78
80
|
tools = if calls.is_a?(Array) && !calls.empty?
|
|
@@ -89,6 +91,17 @@ module Samagotchi
|
|
|
89
91
|
nil
|
|
90
92
|
end
|
|
91
93
|
|
|
94
|
+
# Whether +message+ can have parts (.for_message gives none without):
|
|
95
|
+
# a native call, content (its markup calls and thinking are read from
|
|
96
|
+
# it) or thinking. Cheap, nothing parsed: the owner show keeps a
|
|
97
|
+
# text-less step by it (Web::MessageDisplay), the participant review by
|
|
98
|
+
# its parts, so a new kind of part goes here for both.
|
|
99
|
+
def something?(message)
|
|
100
|
+
calls = field(message, :tool_calls)
|
|
101
|
+
(calls.is_a?(Array) && !calls.empty?) || !field(message, :content).to_s.strip.empty? ||
|
|
102
|
+
!field(message, :thinking).to_s.strip.empty?
|
|
103
|
+
end
|
|
104
|
+
|
|
92
105
|
def thinking_of(content)
|
|
93
106
|
blocks = content.scan(QWEN_THINK).flatten
|
|
94
107
|
if blocks.empty? && !content.include?("<think>") && (open = content[QWEN_OPEN_THINK, 1])
|
|
@@ -121,12 +121,9 @@ module Samagotchi
|
|
|
121
121
|
result.delivered?
|
|
122
122
|
end
|
|
123
123
|
|
|
124
|
-
# The id, or for a session that was continued the link that took it
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
"#{id[0, 8]} → #{result.id[0, 8]} (continued)"
|
|
129
|
-
end
|
|
124
|
+
# The id, or for a session that was continued the link that took it
|
|
125
|
+
# (chi send's wording too).
|
|
126
|
+
def where(id, result) = SessionInbox::Resolved.label(id, result.id)
|
|
130
127
|
|
|
131
128
|
def outcome(result)
|
|
132
129
|
case result.status
|
|
@@ -173,8 +173,9 @@ module Samagotchi
|
|
|
173
173
|
# A failure before any wait (no such session, a busy one, a message
|
|
174
174
|
# that didn't go in) as the JSON object.
|
|
175
175
|
# @param session_id [String, nil]
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
# @param extra [Hash] more keys after session_id (chi send's asked)
|
|
177
|
+
def error_line(detail, session_id: nil, extra: {})
|
|
178
|
+
JSON.generate({ status: "error", session_id: session_id, **extra, detail: detail.to_s })
|
|
178
179
|
end
|
|
179
180
|
|
|
180
181
|
# A pending question (Session#pending_question, a PendingQuestion) as a
|
|
@@ -7,6 +7,7 @@ require_relative "guardrails/path_resolve"
|
|
|
7
7
|
require_relative "memory_paths"
|
|
8
8
|
require_relative "paths"
|
|
9
9
|
require_relative "project_init"
|
|
10
|
+
require_relative "project_subfolders"
|
|
10
11
|
|
|
11
12
|
module Samagotchi
|
|
12
13
|
# `chi project`: which project this folder is in (MemoryPaths.lookup: a
|
|
@@ -19,7 +20,7 @@ module Samagotchi
|
|
|
19
20
|
include CLI::Command
|
|
20
21
|
|
|
21
22
|
USAGE = <<~TEXT
|
|
22
|
-
Usage: chi project [init [--new-id] [DIR]]
|
|
23
|
+
Usage: chi project [init [--new-id | --merge-subfolders] [DIR]]
|
|
23
24
|
(none) this folder's project: how it was found (git, marker or none), its root, its
|
|
24
25
|
top, its id and its project memories folder
|
|
25
26
|
init [DIR] make DIR (default: this folder; created when missing) a chi project: writes
|
|
@@ -29,12 +30,20 @@ module Samagotchi
|
|
|
29
30
|
marked folder with no id: adopts one, moving its folders to it
|
|
30
31
|
--new-id a marked folder's own new id (a copied folder going its own way): its
|
|
31
32
|
memories and project context copied to it
|
|
33
|
+
--merge-subfolders
|
|
34
|
+
move the memories sessions saved in DIR's subfolders before init (each
|
|
35
|
+
keyed by its subfolder's path) into the project's; one the project has by
|
|
36
|
+
that name stays and is named. Marks DIR first when it isn't yet. Not while
|
|
37
|
+
a session in DIR runs
|
|
32
38
|
TEXT
|
|
33
39
|
|
|
34
40
|
SUBCOMMANDS = %w[init].freeze
|
|
35
41
|
FLAGS = {
|
|
36
42
|
nil => CLI::Flags.new(help: HELP_WORDS, args: false),
|
|
37
|
-
"init" => CLI::Flags.new(help: HELP_WORDS)
|
|
43
|
+
"init" => CLI::Flags.new(help: HELP_WORDS) do |f|
|
|
44
|
+
f.switch "--new-id"
|
|
45
|
+
f.switch "--merge-subfolders"
|
|
46
|
+
end
|
|
38
47
|
}.freeze
|
|
39
48
|
|
|
40
49
|
# @param cwd [String] the folder asked about (Dir.pwd)
|
|
@@ -68,8 +77,15 @@ module Samagotchi
|
|
|
68
77
|
return run_show unless @sub
|
|
69
78
|
return usage_error("init takes one folder") if parsed.args.size > 1
|
|
70
79
|
|
|
80
|
+
if parsed.options[:new_id] && parsed.options[:merge_subfolders]
|
|
81
|
+
return usage_error("--merge-subfolders and --new-id don't go together")
|
|
82
|
+
end
|
|
83
|
+
|
|
71
84
|
dir = File.expand_path(parsed.args.first || @cwd, @cwd)
|
|
72
|
-
parsed.options[:new_id]
|
|
85
|
+
return run_new_id(dir) if parsed.options[:new_id]
|
|
86
|
+
return run_merge(dir) if parsed.options[:merge_subfolders]
|
|
87
|
+
|
|
88
|
+
run_init(dir)
|
|
73
89
|
end
|
|
74
90
|
|
|
75
91
|
private
|
|
@@ -89,6 +105,8 @@ module Samagotchi
|
|
|
89
105
|
row("id", found.id) if found.id
|
|
90
106
|
row("memories", memories_line(found))
|
|
91
107
|
notes(found).each { |note| row("note", note) }
|
|
108
|
+
subfolders_note = found.marker? && subfolders_message(found.root)
|
|
109
|
+
row("note", subfolders_note) if subfolders_note
|
|
92
110
|
CLI::Exit::OK
|
|
93
111
|
end
|
|
94
112
|
|
|
@@ -124,7 +142,9 @@ module Samagotchi
|
|
|
124
142
|
!@home.to_s.empty? && Guardrails::PathResolve.real(found.top) == Guardrails::PathResolve.real(@home)
|
|
125
143
|
end
|
|
126
144
|
|
|
127
|
-
|
|
145
|
+
# @param merging [Boolean] --merge-subfolders follows: no subfolders line
|
|
146
|
+
def run_init(dir, merging: false)
|
|
147
|
+
@merging = merging
|
|
128
148
|
outcome = ProjectInit.adopt(dir, home: @home, env: @env, state_dir: @state_dir, sessions: adopt_rows(dir))
|
|
129
149
|
case outcome.kind
|
|
130
150
|
when :git
|
|
@@ -137,12 +157,14 @@ module Samagotchi
|
|
|
137
157
|
CLI::Exit::FAILED
|
|
138
158
|
when :already_marked
|
|
139
159
|
@stdout.puts(outcome.message)
|
|
160
|
+
subfolders_line(outcome.root)
|
|
140
161
|
CLI::Exit::OK
|
|
141
162
|
when :created
|
|
142
163
|
report_init(outcome)
|
|
143
164
|
CLI::Exit::OK
|
|
144
165
|
when :adopted
|
|
145
166
|
@stdout.puts(outcome.message)
|
|
167
|
+
subfolders_line(outcome.root)
|
|
146
168
|
CLI::Exit::OK
|
|
147
169
|
end
|
|
148
170
|
rescue SystemCallError => e
|
|
@@ -162,13 +184,58 @@ module Samagotchi
|
|
|
162
184
|
CLI::Exit::OK
|
|
163
185
|
end
|
|
164
186
|
|
|
187
|
+
# `chi project init --merge-subfolders`: refused while a session in
|
|
188
|
+
# DIR (or a subfolder: a pre-init one stored its own cwd) runs, checked
|
|
189
|
+
# before anything is written; then init as usual (marks or adopts an
|
|
190
|
+
# id), then ProjectSubfolders.merge into the project's memories folder.
|
|
191
|
+
def run_merge(dir)
|
|
192
|
+
root = Guardrails::PathResolve.real(dir)
|
|
193
|
+
running = ProjectSubfolders.running(root, sessions: session_rows, home: @home)
|
|
194
|
+
unless running.empty?
|
|
195
|
+
error_line("#{command_name}: #{ProjectSubfolders.running_message(root, running)}")
|
|
196
|
+
return CLI::Exit::FAILED
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
code = run_init(dir, merging: true)
|
|
200
|
+
return code unless code == CLI::Exit::OK
|
|
201
|
+
|
|
202
|
+
found = lookup(dir)
|
|
203
|
+
return CLI::Exit::OK unless found.marker? && found.id
|
|
204
|
+
|
|
205
|
+
target = File.join(MemoryPaths.projects_dir(env: @env), found.id)
|
|
206
|
+
subfolders = ProjectSubfolders.find(found.root, sessions: session_rows, env: @env, home: @home)
|
|
207
|
+
ProjectSubfolders.merge(subfolders, target: target, env: @env).each { |line| @stdout.puts(line) }
|
|
208
|
+
CLI::Exit::OK
|
|
209
|
+
rescue SystemCallError => e
|
|
210
|
+
error_line("#{command_name}: couldn't move the subfolders' memories: #{e.message}")
|
|
211
|
+
CLI::Exit::FAILED
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# This machine's sessions, listed once per run (a walk of every session file).
|
|
215
|
+
def session_rows = @session_rows ||= @sessions.call
|
|
216
|
+
|
|
217
|
+
# ProjectSubfolders' text for +root+, or nil (none, or no listing).
|
|
218
|
+
def subfolders_message(root)
|
|
219
|
+
ProjectSubfolders.message(ProjectSubfolders.find(root, sessions: session_rows, env: @env, home: @home))
|
|
220
|
+
rescue StandardError => e
|
|
221
|
+
error_line("#{command_name}: couldn't list this machine's sessions (#{e.message})")
|
|
222
|
+
nil
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def subfolders_line(root)
|
|
226
|
+
return if @merging
|
|
227
|
+
|
|
228
|
+
text = subfolders_message(root)
|
|
229
|
+
@stdout.puts(text) if text
|
|
230
|
+
end
|
|
231
|
+
|
|
165
232
|
# The rows an adoption checks for running sessions: only for a marked
|
|
166
233
|
# folder (a listing is a walk of every session file).
|
|
167
234
|
def adopt_rows(dir)
|
|
168
235
|
found = lookup(dir)
|
|
169
236
|
return [] if found.git? || found.id
|
|
170
237
|
|
|
171
|
-
|
|
238
|
+
session_rows
|
|
172
239
|
end
|
|
173
240
|
|
|
174
241
|
def report_init(outcome)
|
|
@@ -178,6 +245,7 @@ module Samagotchi
|
|
|
178
245
|
row("id", outcome.id) if outcome.id
|
|
179
246
|
row("memories", memories_line(lookup(root)))
|
|
180
247
|
@stdout.puts(outcome.message) if outcome.message
|
|
248
|
+
subfolders_line(root)
|
|
181
249
|
sessions_note(root)
|
|
182
250
|
end
|
|
183
251
|
|
|
@@ -187,7 +255,7 @@ module Samagotchi
|
|
|
187
255
|
# restarts; a running chi web groups the older ones after its restart
|
|
188
256
|
# (SessionHub looked their project up once).
|
|
189
257
|
def sessions_note(root, restart: nil)
|
|
190
|
-
rows =
|
|
258
|
+
rows = session_rows.select { |r| !r.cwd.to_s.empty? && lookup(r.cwd.to_s).root == root }
|
|
191
259
|
return if rows.empty?
|
|
192
260
|
|
|
193
261
|
live = rows.select(&:live)
|