samagotchi 0.173.0 → 0.175.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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +48 -1
  3. data/docs/architecture.md +2 -2
  4. data/docs/cli.md +9 -2
  5. data/docs/internals/background-tasks.md +0 -3
  6. data/docs/internals/bridge.md +8 -1
  7. data/docs/internals/participants.md +3 -1
  8. data/docs/internals/remote-sessions.md +8 -0
  9. data/docs/internals/web-frontend.md +45 -5
  10. data/docs/plugins.md +1 -3
  11. data/docs/scheduling.md +9 -3
  12. data/docs/sessions.md +3 -1
  13. data/docs/testing.md +31 -3
  14. data/lib/samagotchi/answer_text.rb +4 -2
  15. data/lib/samagotchi/bridge.rb +1 -1
  16. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  17. data/lib/samagotchi/child_move.rb +11 -4
  18. data/lib/samagotchi/continued_from.rb +51 -0
  19. data/lib/samagotchi/engine.rb +1 -1
  20. data/lib/samagotchi/llm/stray_tool_call.rb +7 -4
  21. data/lib/samagotchi/llm_context_stale.rb +2 -1
  22. data/lib/samagotchi/message_parts.rb +5 -2
  23. data/lib/samagotchi/model_markup.rb +22 -0
  24. data/lib/samagotchi/model_profile.rb +8 -7
  25. data/lib/samagotchi/note_move.rb +11 -4
  26. data/lib/samagotchi/prompt_literal_guard.rb +7 -6
  27. data/lib/samagotchi/schedules.rb +13 -16
  28. data/lib/samagotchi/session_access/contract.rb +1 -0
  29. data/lib/samagotchi/session_access/local.rb +9 -1
  30. data/lib/samagotchi/session_access/remote.rb +26 -3
  31. data/lib/samagotchi/session_chain/delete.rb +405 -0
  32. data/lib/samagotchi/session_chain/delete_preview.rb +96 -0
  33. data/lib/samagotchi/session_chain/links.rb +38 -0
  34. data/lib/samagotchi/session_command_catalog.rb +2 -2
  35. data/lib/samagotchi/session_commands.rb +3 -13
  36. data/lib/samagotchi/session_continue.rb +51 -13
  37. data/lib/samagotchi/session_delete_command.rb +82 -17
  38. data/lib/samagotchi/session_manager/removal.rb +137 -31
  39. data/lib/samagotchi/session_stores.rb +2 -0
  40. data/lib/samagotchi/task_transcript.rb +10 -5
  41. data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +2 -2
  42. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  43. data/lib/samagotchi/terminal_ui/exit_lines.rb +16 -0
  44. data/lib/samagotchi/terminal_ui/local_link.rb +7 -1
  45. data/lib/samagotchi/terminal_ui.rb +3 -3
  46. data/lib/samagotchi/testing/boundary.rb +0 -5
  47. data/lib/samagotchi/testing/bundle.rb +1 -1
  48. data/lib/samagotchi/tool_call_parser.rb +6 -5
  49. data/lib/samagotchi/tools/task_runtime.rb +0 -5
  50. data/lib/samagotchi/turn_note.rb +2 -1
  51. data/lib/samagotchi/version.rb +1 -1
  52. data/lib/samagotchi/web/app.rb +7 -1
  53. data/lib/samagotchi/web/participant_show.rb +1 -0
  54. data/lib/samagotchi/web/participant_view.rb +4 -2
  55. data/lib/samagotchi/web/public/app.js +27 -5
  56. data/lib/samagotchi/web/public/chains.js +3 -3
  57. data/lib/samagotchi/web/public/data.js +12 -0
  58. data/lib/samagotchi/web/public/delete_sheet.js +344 -0
  59. data/lib/samagotchi/web/public/format.js +1 -1
  60. data/lib/samagotchi/web/public/index.html +27 -0
  61. data/lib/samagotchi/web/public/info_bar.js +3 -2
  62. data/lib/samagotchi/web/public/lists_view.js +2 -2
  63. data/lib/samagotchi/web/public/more_sheet.js +42 -5
  64. data/lib/samagotchi/web/public/reload_check.js +0 -3
  65. data/lib/samagotchi/web/public/session_actions.js +50 -6
  66. data/lib/samagotchi/web/public/session_view.js +9 -1
  67. data/lib/samagotchi/web/public/stage_asking.js +1 -7
  68. data/lib/samagotchi/web/public/stage_missing.js +150 -0
  69. data/lib/samagotchi/web/public/stage_view.js +8 -3
  70. data/lib/samagotchi/web/server.rb +13 -13
  71. data/lib/samagotchi/web/session_control.rb +58 -5
  72. data/lib/samagotchi/web/session_show.rb +42 -4
  73. metadata +7 -2
  74. data/lib/samagotchi/web/public/cards_offscreen.js +0 -69
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../model_markup"
4
+
3
5
  module Samagotchi
4
6
  module LLM
5
7
  # A reply that is nothing but a tool call written as text. The host's
@@ -25,16 +27,17 @@ module Samagotchi
25
27
  # predicate is ChatLoop's.
26
28
  module StrayToolCall
27
29
  # A stray call can only open with the block itself.
28
- OPENS = ["<tool_call>", "<function=", "<parameter="].freeze
30
+ OPENS = [ModelMarkup::TOOL_CALL_OPEN, ModelMarkup::FUNCTION_OPEN, ModelMarkup::PARAMETER_OPEN].freeze
29
31
  # ... and end with the close of that same block: nothing else in the
30
32
  # reply. A `<parameter=` call often closes with `</function>` (the
31
33
  # flavour that mixes the two tags does it mid-block too), and a block
32
34
  # opened by `<function=` or `<parameter=` can also end with the
33
35
  # wrapper's own `</tool_call>` instead of its tag.
34
36
  CLOSES = {
35
- "<tool_call>" => ["</tool_call>"],
36
- "<function=" => ["</function>", "</tool_call>"],
37
- "<parameter=" => ["</parameter>", "</function>", "</tool_call>"]
37
+ ModelMarkup::TOOL_CALL_OPEN => [ModelMarkup::TOOL_CALL_CLOSE],
38
+ ModelMarkup::FUNCTION_OPEN => [ModelMarkup::FUNCTION_CLOSE, ModelMarkup::TOOL_CALL_CLOSE],
39
+ ModelMarkup::PARAMETER_OPEN => [ModelMarkup::PARAMETER_CLOSE, ModelMarkup::FUNCTION_CLOSE,
40
+ ModelMarkup::TOOL_CALL_CLOSE]
38
41
  }.freeze
39
42
  # How much of the reply the warning keeps (KernelLoop's own cap).
40
43
  TAIL_CHARS = 400
@@ -5,6 +5,7 @@ require_relative "tool_response"
5
5
  require_relative "tool_ids"
6
6
  require_relative "llm_context_edit"
7
7
  require_relative "model_profile"
8
+ require_relative "model_markup"
8
9
  require_relative "tool_call_parser"
9
10
  require_relative "tools/builtin_calls"
10
11
  require_relative "tools/tool_path"
@@ -264,7 +265,7 @@ module Samagotchi
264
265
  {}
265
266
  end
266
267
 
267
- QWEN_CALL = "<tool_call>"
268
+ QWEN_CALL = ModelMarkup::TOOL_CALL_OPEN
268
269
  GEMMA_CALL = "<|tool_call>"
269
270
 
270
271
  def native_parser(content)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "model_profile"
4
+ require_relative "model_markup"
4
5
  require_relative "tool_call_parser"
5
6
  require_relative "tool_activity"
6
7
  require_relative "tool_row_fields"
@@ -106,7 +107,7 @@ module Samagotchi
106
107
 
107
108
  def thinking_of(content)
108
109
  blocks = content.scan(QWEN_THINK).flatten
109
- if blocks.empty? && !content.include?("<think>") && (open = content[QWEN_OPEN_THINK, 1])
110
+ if blocks.empty? && !content.include?(ModelMarkup::THINK_OPEN) && (open = content[QWEN_OPEN_THINK, 1])
110
111
  blocks = [open]
111
112
  end
112
113
  blocks += content.scan(GEMMA_THOUGHT).flatten
@@ -115,7 +116,9 @@ module Samagotchi
115
116
 
116
117
  def markup_tools(content, responses, registry, cwd = nil, marks = {})
117
118
  calls = []
118
- calls.concat(ToolCallParser::Qwen.new(ModelProfile.qwen36).parse(content)) if content.include?("<tool_call>")
119
+ if content.include?(ModelMarkup::TOOL_CALL_OPEN)
120
+ calls.concat(ToolCallParser::Qwen.new(ModelProfile.qwen36).parse(content))
121
+ end
119
122
  calls.concat(ToolCallParser::Gemma.new(ModelProfile.gemma4).parse(content)) if content.include?("<|tool_call>")
120
123
  return [] if calls.empty?
121
124
 
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Samagotchi
4
+ # The plain Qwen-style markup tokens, apart from the model-specific token
5
+ # formats. ModelProfile, ToolCallParser, StrayToolCall, PromptLiteralGuard,
6
+ # TurnNote, MessageParts, AnswerText, LLMContextStale and ParticipantView
7
+ # read them here without loading a tool class.
8
+ #
9
+ # No requires: it is a leaf every client can load.
10
+ module ModelMarkup
11
+ TOOL_CALL_OPEN = "<tool_call>"
12
+ TOOL_CALL_CLOSE = "</tool_call>"
13
+ FUNCTION_OPEN = "<function="
14
+ FUNCTION_CLOSE = "</function>"
15
+ PARAMETER_OPEN = "<parameter="
16
+ PARAMETER_CLOSE = "</parameter>"
17
+ TOOL_RESPONSE_OPEN = "<tool_response>"
18
+ TOOL_RESPONSE_CLOSE = "</tool_response>"
19
+ THINK_OPEN = "<think>"
20
+ THINK_CLOSE = "</think>"
21
+ end
22
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "log"
4
+ require_relative "model_markup"
4
5
  module Samagotchi
5
6
  # Encapsulates all model-specific token formats and parsing behavior.
6
7
  # Each profile maps to a model family; ModelProfile.resolve picks one
@@ -108,13 +109,13 @@ module Samagotchi
108
109
  name: "qwen36",
109
110
  turn_start: "",
110
111
  turn_end: "",
111
- tool_call_open: "<tool_call>",
112
- tool_call_close: "</tool_call>",
113
- tool_response_open: "<tool_response>",
114
- tool_response_close: "</tool_response>",
112
+ tool_call_open: ModelMarkup::TOOL_CALL_OPEN,
113
+ tool_call_close: ModelMarkup::TOOL_CALL_CLOSE,
114
+ tool_response_open: ModelMarkup::TOOL_RESPONSE_OPEN,
115
+ tool_response_close: ModelMarkup::TOOL_RESPONSE_CLOSE,
115
116
  string_delim: nil,
116
- thought_open: "<think>",
117
- thought_close: "</think>",
117
+ thought_open: ModelMarkup::THINK_OPEN,
118
+ thought_close: ModelMarkup::THINK_CLOSE,
118
119
  system_prefix: "<|im_start|>system\n",
119
120
  user_prefix: "<|im_start|>user\n",
120
121
  assistant_prefix: "<|im_start|>assistant\n",
@@ -377,7 +378,7 @@ module Samagotchi
377
378
  # <eos> or <turn|>, depending on who packed it). llama.cpp itself detects
378
379
  # Gemma 4 by '<|tool_call>call:'.
379
380
  FINGERPRINTS = {
380
- "qwen36" => ["<|im_start|>", "<function="],
381
+ "qwen36" => ["<|im_start|>", ModelMarkup::FUNCTION_OPEN],
381
382
  "gemma4" => ["<|turn>", "<|tool_call>"]
382
383
  }.freeze
383
384
 
@@ -57,15 +57,22 @@ module Samagotchi
57
57
  # meanwhile, never one the continue wrote itself (CONTINUE_SOURCES).
58
58
  # @return [Array<String>] the file names given back
59
59
  def give_back(from:, to:, moved:, state_dir:)
60
- files = unclaimed(notes_dir(from, state_dir)).select do |file|
60
+ rename(returnable(from: from, moved: moved, state_dir: state_dir), notes_dir(to, state_dir)).tap do |back|
61
+ Log.info(:worker, "notes_given_back", from: from[0, 8], to: to[0, 8], count: back.size) unless back.empty?
62
+ end
63
+ end
64
+
65
+ # The note files #give_back takes from +from+: those in +moved+ and
66
+ # every other unclaimed one but an objectives list and the continue's
67
+ # own (CONTINUE_SOURCES). A chain delete's plan counts them.
68
+ # @return [Array<String>] paths, oldest first
69
+ def returnable(from:, moved:, state_dir:)
70
+ unclaimed(notes_dir(from, state_dir)).select do |file|
61
71
  next true if moved.include?(File.basename(file))
62
72
 
63
73
  data = read(file)
64
74
  data && !objectives?(data) && !CONTINUE_SOURCES.include?(data["source"])
65
75
  end
66
- rename(files, notes_dir(to, state_dir)).tap do |back|
67
- Log.info(:worker, "notes_given_back", from: from[0, 8], to: to[0, 8], count: back.size) unless back.empty?
68
- end
69
76
  end
70
77
 
71
78
  # How far back along the chain a worker adopts (Adoption): the links
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "model_profile"
4
+ require_relative "model_markup"
4
5
 
5
6
  module Samagotchi
6
7
  # Escapes literal control-token text when arbitrary source/output is embedded
@@ -25,12 +26,12 @@ module Samagotchi
25
26
  QWEN_TOKEN_PAIRS = [
26
27
  ["[[SAMAGOTCHI_LITERAL_IM_START]]", "<|im_start|>"],
27
28
  ["[[SAMAGOTCHI_LITERAL_IM_END]]", "<|im_end|>"],
28
- ["[[SAMAGOTCHI_LITERAL_TOOL_CALL_OPEN]]", "<tool_call>"],
29
- ["[[SAMAGOTCHI_LITERAL_TOOL_CALL_CLOSE]]", "</tool_call>"],
30
- ["[[SAMAGOTCHI_LITERAL_TOOL_RESPONSE_OPEN]]", "<tool_response>"],
31
- ["[[SAMAGOTCHI_LITERAL_TOOL_RESPONSE_CLOSE]]", "</tool_response>"],
32
- ["[[SAMAGOTCHI_LITERAL_THINK_OPEN]]", "<think>"],
33
- ["[[SAMAGOTCHI_LITERAL_THINK_CLOSE]]", "</think>"],
29
+ ["[[SAMAGOTCHI_LITERAL_TOOL_CALL_OPEN]]", ModelMarkup::TOOL_CALL_OPEN],
30
+ ["[[SAMAGOTCHI_LITERAL_TOOL_CALL_CLOSE]]", ModelMarkup::TOOL_CALL_CLOSE],
31
+ ["[[SAMAGOTCHI_LITERAL_TOOL_RESPONSE_OPEN]]", ModelMarkup::TOOL_RESPONSE_OPEN],
32
+ ["[[SAMAGOTCHI_LITERAL_TOOL_RESPONSE_CLOSE]]", ModelMarkup::TOOL_RESPONSE_CLOSE],
33
+ ["[[SAMAGOTCHI_LITERAL_THINK_OPEN]]", ModelMarkup::THINK_OPEN],
34
+ ["[[SAMAGOTCHI_LITERAL_THINK_CLOSE]]", ModelMarkup::THINK_CLOSE],
34
35
  ["[[SAMAGOTCHI_LITERAL_VISION_START]]", "<|vision_start|>"],
35
36
  ["[[SAMAGOTCHI_LITERAL_VISION_END]]", "<|vision_end|>"]
36
37
  ].freeze
@@ -209,6 +209,14 @@ module Samagotchi
209
209
  entries.find { |entry| target_of(entry, links).first == session_id }
210
210
  end
211
211
 
212
+ # The enabled schedule a delete of +session_id+ would break: one whose
213
+ # target (#protecting), anchor or head it is. A head deleted would make
214
+ # the schedule fall back to its anchor (an archived link) and fail.
215
+ # @return [Entry, nil]
216
+ def protecting_link(session_id)
217
+ entries.find { |entry| entry.anchor == session_id || entry.head == session_id } || protecting(session_id)
218
+ end
219
+
212
220
  # @param id_or_name [String] an id, a unique prefix of one, or a title
213
221
  # @return [Entry, nil]
214
222
  # @raise [Usage] the prefix or the title names more than one schedule
@@ -303,17 +311,7 @@ module Samagotchi
303
311
  # chain and each link back to its root. A schedule belongs to a chain when
304
312
  # its anchor or its head is one of them.
305
313
  # @return [Array<String>]
306
- def chain_of(session_id)
307
- links = links_now
308
- head = links.latest(session_id)
309
- ids = [head]
310
- id = head
311
- while (previous = links.continues(id)) && !ids.include?(previous)
312
- ids << previous
313
- id = previous
314
- end
315
- ids
316
- end
314
+ def chain_of(session_id) = links_now.chain(session_id).reverse
317
315
 
318
316
  # The schedules of this session's chain — the ones the tools may touch
319
317
  # without `all: true`.
@@ -1230,11 +1228,12 @@ module Samagotchi
1230
1228
  next if link == entry.anchor
1231
1229
  break unless ArchiveStore.archived?(Session.session_dir(link, state_dir: @state_dir))
1232
1230
 
1233
- descendants = SessionManager.descendant_ids(link, @state_dir)
1231
+ tree = SessionManager.delete_tree_ids(link, @state_dir)
1232
+ descendants = tree[0...-1]
1234
1233
  # The whole subtree must be ready: a live worker anywhere in it, or a
1235
1234
  # link's descendant that is not archived, stops the walk here rather
1236
1235
  # than half-deleting a tree.
1237
- owned = [link, *descendants].select { |sid| SessionManager.session_owner(sid, state_dir: @state_dir) }
1236
+ owned = tree.select { |sid| SessionManager.session_owner(sid, state_dir: @state_dir) }
1238
1237
  unless owned.empty?
1239
1238
  Log.warn(:worker, "prune_waiting", sid: link, id: entry.id, owned: owned.map { |sid| sid[0, 8] })
1240
1239
  break
@@ -1250,9 +1249,7 @@ module Samagotchi
1250
1249
  # refused partway, nothing above it has gone, so the next firing
1251
1250
  # retries the same set instead of leaving children under a link that
1252
1251
  # is already deleted.
1253
- (descendants.reverse + [link]).each do |sid|
1254
- SessionManager.delete_session(sid, state_dir: @state_dir)
1255
- end
1252
+ SessionManager.delete_ids(tree, state_dir: @state_dir)
1256
1253
  Log.info(:worker, "pruned_link", sid: link, id: entry.id, links: descendants.size + 1)
1257
1254
  end
1258
1255
  rescue SessionManager::DeleteRefused => e
@@ -116,6 +116,7 @@ module Samagotchi
116
116
  "sessions unarchive" => SUPPORTED,
117
117
  "sessions delete" => SUPPORTED,
118
118
  "sessions delete --force" => SUPPORTED,
119
+ "sessions delete --chain" => SUPPORTED,
119
120
  "sessions prune" => LOCAL_ONLY,
120
121
  "sessions clean" => LOCAL_ONLY,
121
122
  "sessions stats" => LOCAL_ONLY,
@@ -293,7 +293,15 @@ module Samagotchi
293
293
 
294
294
  # @param force [Boolean] stop a live worker first (else refused)
295
295
  # @return [Hash] SessionManager.delete_session's
296
- def delete(id, force:) = SessionManager.delete_session(id, stop: force, **dir)
296
+ # A user's delete: SessionManager.delete_session's revive_previous: and
297
+ # refuse_continuing: (D2/D5), on unless told otherwise.
298
+ def delete(id, force:, revive_previous: true, refuse_continuing: true)
299
+ SessionManager.delete_session(id, stop: force, revive_previous: revive_previous,
300
+ refuse_continuing: refuse_continuing, **dir)
301
+ end
302
+
303
+ # Every link of the chain (SessionManager.delete_chain).
304
+ def delete_chain(id, force:) = SessionManager.delete_chain(id, stop: force, **dir)
297
305
 
298
306
  # --- context (a session's sources, or a project's: local only)
299
307
  #
@@ -316,6 +316,8 @@ module Samagotchi
316
316
 
317
317
  # Web::App::BOUND_FEATURE: POST …/participants takes an identity.
318
318
  BOUND_FEATURE = "bound_grants"
319
+ # Web::App::CHAIN_DELETE_FEATURE: DELETE …/chain.
320
+ CHAIN_DELETE_FEATURE = "chain_delete"
319
321
 
320
322
  # The header that chi web names the signed-in user in (GET
321
323
  # /api/info's proxy), nil when it has no such proxy, or is a chi
@@ -493,13 +495,33 @@ module Samagotchi
493
495
  end
494
496
 
495
497
  # @return [Hash] {id:, stopped:}
498
+ # A chi web before chain_delete answers without unarchived,
499
+ # moved_back, deleted and left_running (no revive, no undo, no D7
500
+ # there): each reads as [].
496
501
  def delete(id, force:)
497
502
  reply = request(:delete, session_path(id), query: { force: (true if force) })
498
503
  raise_refusal(reply) unless reply.status == 200
499
504
 
500
- { id: reply.json["session_id"] || id, stopped: reply.json["stopped"] == true }
505
+ { id: reply.json["session_id"] || id, stopped: reply.json["stopped"] == true,
506
+ unarchived: ids_of(reply.json["unarchived"]), moved_back: ids_of(reply.json["moved_back"]),
507
+ deleted: ids_of(reply.json["deleted"]), left_running: ids_of(reply.json["left_running"]) }
501
508
  end
502
509
 
510
+ # DELETE …/chain there (SessionManager.delete_chain's result).
511
+ # @raise [RemoteTooOld] a chi web without it (an older one would answer 404)
512
+ def delete_chain(id, force:)
513
+ require_feature!(CHAIN_DELETE_FEATURE)
514
+ reply = request(:delete, "#{session_path(id)}/chain", query: { force: (true if force) })
515
+ raise_refusal(reply) unless reply.status == 200
516
+
517
+ json = reply.json
518
+ { id: json["session_id"] || id, deleted: ids_of(json["deleted"]), left: ids_of(json["left"]),
519
+ left_running: ids_of(json["left_running"]), unarchived: ids_of(json["unarchived"]),
520
+ stopped: ids_of(json["stopped"]), cut_off: json["cut_off"] == true, refusal: json["refusal"] }
521
+ end
522
+
523
+ private def ids_of(value) = Array(value).select { |id| Session.valid_id?(id) }
524
+
503
525
  # --- context (a session's own sources: a project's are local only)
504
526
 
505
527
  # @return [ContextAdded]
@@ -761,8 +783,9 @@ module Samagotchi
761
783
  raise SessionManager::ScratchSession, sid if json["scratch"] == true
762
784
 
763
785
  raise SessionManager::OwnedByTUI, sid
764
- when "worker_running", "still_stopping", "scheduled"
765
- raise SessionManager::DeleteRefused.new(sid, reason, message: message)
786
+ when "worker_running", "still_stopping", "scheduled", "continuing", "delegate_unmovable", "chain_scheduled"
787
+ raise SessionManager::DeleteRefused.new(sid, reason || error.to_sym, message: message, ids: ids_of(json["ids"]),
788
+ schedule_id: json["schedule_id"])
766
789
  when "busy", "scratch"
767
790
  raise SessionManager::ArchiveRefused.new(sid, reason, busy_id: json["busy_id"]) if reason
768
791
  when "continued", "open_children", "folder_gone"