samagotchi 0.155.0 → 0.156.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 (124) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -1
  3. data/bin/chi +1 -1
  4. data/docs/architecture.md +10 -6
  5. data/docs/internals/anthropic.md +1 -1
  6. data/docs/internals/attached-context.md +3 -2
  7. data/docs/internals/background-tasks.md +1 -1
  8. data/docs/internals/engine.md +625 -935
  9. data/docs/internals/llm-context-forget.md +8 -3
  10. data/docs/internals/objectives.md +2 -2
  11. data/docs/internals/prompt-caching.md +4 -1
  12. data/docs/internals/remote-sessions.md +3 -1
  13. data/docs/internals/working-set.md +1 -1
  14. data/docs/sessions.md +2 -2
  15. data/docs/system-one.md +3 -2
  16. data/lib/samagotchi/bootstrap/config_writer.rb +1 -1
  17. data/lib/samagotchi/broadcast_command.rb +3 -3
  18. data/lib/samagotchi/bundle_command/try/sandbox.rb +1 -1
  19. data/lib/samagotchi/bundles/system/config_modification_protocol.md +2 -2
  20. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  21. data/lib/samagotchi/child_move.rb +7 -51
  22. data/lib/samagotchi/children_status.rb +1 -1
  23. data/lib/samagotchi/cli/parent_wait.rb +1 -1
  24. data/lib/samagotchi/config.rb +17 -112
  25. data/lib/samagotchi/context_command.rb +1 -2
  26. data/lib/samagotchi/context_window.rb +3 -3
  27. data/lib/samagotchi/desktop/macos.rb +1 -1
  28. data/lib/samagotchi/engine/context_judge.rb +19 -11
  29. data/lib/samagotchi/engine/messages.rb +1 -1
  30. data/lib/samagotchi/engine/models.rb +5 -2
  31. data/lib/samagotchi/engine/objectives.rb +29 -11
  32. data/lib/samagotchi/engine/side_requests.rb +21 -11
  33. data/lib/samagotchi/engine/toolkit.rb +10 -9
  34. data/lib/samagotchi/engine/turn_end.rb +1 -1
  35. data/lib/samagotchi/engine/turn_helpers.rb +7 -7
  36. data/lib/samagotchi/engine/verify.rb +1 -1
  37. data/lib/samagotchi/engine.rb +14 -9
  38. data/lib/samagotchi/guardrails/targets.rb +24 -9
  39. data/lib/samagotchi/host_config.rb +72 -1
  40. data/lib/samagotchi/host_model.rb +2 -6
  41. data/lib/samagotchi/host_registry.rb +1 -1
  42. data/lib/samagotchi/kernel_loop.rb +46 -121
  43. data/lib/samagotchi/llm/anthropic_wire.rb +2 -2
  44. data/lib/samagotchi/llm/chat_loop.rb +34 -18
  45. data/lib/samagotchi/llm/native_backend.rb +1 -1
  46. data/lib/samagotchi/llm_context_apply.rb +54 -1
  47. data/lib/samagotchi/llm_context_notice.rb +1 -1
  48. data/lib/samagotchi/llm_context_strategy.rb +7 -9
  49. data/lib/samagotchi/llm_context_view.rb +9 -0
  50. data/lib/samagotchi/model_price.rb +4 -10
  51. data/lib/samagotchi/model_profile.rb +6 -6
  52. data/lib/samagotchi/model_settings.rb +37 -2
  53. data/lib/samagotchi/note_command.rb +1 -2
  54. data/lib/samagotchi/objectives_audit.rb +4 -3
  55. data/lib/samagotchi/participants.rb +2 -2
  56. data/lib/samagotchi/plugin/context.rb +1 -1
  57. data/lib/samagotchi/plugin/sessions.rb +2 -5
  58. data/lib/samagotchi/plugin/tool_spec.rb +5 -3
  59. data/lib/samagotchi/project_env.rb +1 -1
  60. data/lib/samagotchi/reasoning_history.rb +3 -3
  61. data/lib/samagotchi/reminder_carry.rb +1 -1
  62. data/lib/samagotchi/reprefill_meter.rb +45 -0
  63. data/lib/samagotchi/review_command.rb +1 -1
  64. data/lib/samagotchi/sampling_settings.rb +3 -3
  65. data/lib/samagotchi/self_report.rb +7 -7
  66. data/lib/samagotchi/session.rb +50 -7
  67. data/lib/samagotchi/session_access/local.rb +7 -5
  68. data/lib/samagotchi/session_access/remote.rb +17 -3
  69. data/lib/samagotchi/session_archive_command.rb +2 -2
  70. data/lib/samagotchi/session_chain.rb +7 -6
  71. data/lib/samagotchi/session_continue.rb +6 -5
  72. data/lib/samagotchi/session_delete_command.rb +1 -1
  73. data/lib/samagotchi/session_manager/delivery.rb +254 -0
  74. data/lib/samagotchi/session_manager/emptiness.rb +138 -0
  75. data/lib/samagotchi/session_manager/lifecycle.rb +274 -0
  76. data/lib/samagotchi/session_manager/listing.rb +189 -0
  77. data/lib/samagotchi/session_manager/removal.rb +190 -0
  78. data/lib/samagotchi/session_manager/spawn.rb +242 -0
  79. data/lib/samagotchi/session_manager/worker_command.rb +157 -0
  80. data/lib/samagotchi/session_manager.rb +26 -1540
  81. data/lib/samagotchi/session_metrics.rb +1 -1
  82. data/lib/samagotchi/sessions_command.rb +1 -1
  83. data/lib/samagotchi/share_command.rb +1 -1
  84. data/lib/samagotchi/starting_marker.rb +65 -0
  85. data/lib/samagotchi/steer_cut_after.rb +3 -3
  86. data/lib/samagotchi/system_one/recorder.rb +49 -0
  87. data/lib/samagotchi/terminal_ui/exit_lines.rb +1 -1
  88. data/lib/samagotchi/terminal_ui.rb +2 -6
  89. data/lib/samagotchi/testing/sessions.rb +1 -1
  90. data/lib/samagotchi/thinking.rb +3 -3
  91. data/lib/samagotchi/token_usage.rb +1 -1
  92. data/lib/samagotchi/tool_activity.rb +10 -4
  93. data/lib/samagotchi/tool_declarations.rb +2 -2
  94. data/lib/samagotchi/tool_output_cut.rb +17 -0
  95. data/lib/samagotchi/tool_runner.rb +7 -4
  96. data/lib/samagotchi/toolkit_command.rb +1 -1
  97. data/lib/samagotchi/tools/delegate.rb +10 -5
  98. data/lib/samagotchi/tools/delegate_result.rb +4 -4
  99. data/lib/samagotchi/tools/index_hint.rb +14 -0
  100. data/lib/samagotchi/tools/registry.rb +28 -11
  101. data/lib/samagotchi/tools/send_note.rb +4 -6
  102. data/lib/samagotchi/tools/tool_index.rb +52 -51
  103. data/lib/samagotchi/tools/tool_surface.rb +22 -8
  104. data/lib/samagotchi/turn_context.rb +2 -1
  105. data/lib/samagotchi/turn_stages/context_judge.rb +38 -36
  106. data/lib/samagotchi/turn_stages/objectives.rb +56 -52
  107. data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +13 -17
  108. data/lib/samagotchi/turn_stages/verify_anchors.rb +9 -10
  109. data/lib/samagotchi/turn_stages/warmup.rb +14 -15
  110. data/lib/samagotchi/version.rb +1 -1
  111. data/lib/samagotchi/vision_support.rb +3 -3
  112. data/lib/samagotchi/web/app.rb +15 -8
  113. data/lib/samagotchi/web/participant_access.rb +2 -2
  114. data/lib/samagotchi/web/session_access_routes.rb +3 -3
  115. data/lib/samagotchi/web/session_control.rb +3 -3
  116. data/lib/samagotchi/web/session_create.rb +3 -3
  117. data/lib/samagotchi/web/session_hub.rb +1 -1
  118. data/lib/samagotchi/web/session_show.rb +3 -3
  119. data/lib/samagotchi/web/session_summary.rb +3 -4
  120. data/lib/samagotchi/worker.rb +55 -27
  121. data/lib/samagotchi/worker_boot.rb +184 -0
  122. data/lib/samagotchi/worker_signals.rb +1 -1
  123. data/lib/samagotchi/worker_task_wakes.rb +2 -2
  124. metadata +13 -1
@@ -37,6 +37,7 @@ require_relative "model_profile"
37
37
  require_relative "child_reports"
38
38
  require_relative "verify_fresh"
39
39
  require_relative "child_move"
40
+ require_relative "starting_marker"
40
41
  require_relative "context_absorber"
41
42
  require_relative "context_candidates"
42
43
  require_relative "context_poller"
@@ -47,7 +48,7 @@ require_relative "participant_name"
47
48
 
48
49
  module Samagotchi
49
50
  # The loop of a background session worker, once it owns the session (see
50
- # SessionManager.run_session_loop, which takes the OwnerLock around #run).
51
+ # WorkerBoot.run_session_loop, which takes the OwnerLock around #run).
51
52
  #
52
53
  # It runs the session's Engine and Bridge, takes queued prompts from the
53
54
  # input dir one turn at a time, and returns when nobody has used it for the
@@ -57,8 +58,8 @@ module Samagotchi
57
58
  # exits with 1.
58
59
  #
59
60
  # The file IPC stays behind SessionInbox's functions (find_new_input_files,
60
- # claim_input_file, ...) and the Bridge behind SessionManager.start_bridge,
61
- # which specs stub as seams.
61
+ # claim_input_file, ...) and the Bridge behind #start_bridge, which specs
62
+ # stub as seams.
62
63
  #
63
64
  # The loop sleeps on a Waker, which the Bridge wakes when it queues a turn
64
65
  # or a command, and the reminder callback when it queues one. A fallback
@@ -132,11 +133,11 @@ module Samagotchi
132
133
  # @param idle_exit_minutes [Numeric, nil] nil: session.idle_exit_minutes
133
134
  # @param poll_interval [Numeric, nil] seconds between fallback ticks
134
135
  # @param signals [WorkerSignals, WorkerSignals::None] the process's
135
- # trapped signals (SessionManager.run_session_loop); none by default
136
+ # trapped signals (WorkerBoot.run_session_loop); none by default
136
137
  # @param signal_grace [Array(Numeric, Numeric, Numeric)] the grace, the
137
138
  # short grace and the hard exit's delay (specs shorten them)
138
139
  # @param owner_lock [OwnerLock, nil] the session's, which this process
139
- # holds (SessionManager.run_session_loop): marked leaving as the Bridge
140
+ # holds (WorkerBoot.run_session_loop): marked leaving as the Bridge
140
141
  # stops, so an attach waits for the recap on leave (OwnerLock.await_release)
141
142
  def initialize(session_id:, state_dir:, session_dir:, idle_exit_minutes: nil, poll_interval: nil,
142
143
  signals: WorkerSignals::NONE, signal_grace: [SIGNAL_GRACE, SIGNAL_SHORT_GRACE, SIGNAL_HARD_EXIT],
@@ -185,16 +186,16 @@ module Samagotchi
185
186
  end
186
187
 
187
188
  # Whether the session was empty as the worker left it, so the caller
188
- # deletes it once the lock is free (SessionManager.run_session_loop
189
+ # deletes it once the lock is free (WorkerBoot.run_session_loop
189
190
  # checks again then).
190
191
  def discard? = @discard
191
192
 
192
193
  # Whether this worker runs a `chi scratch` session: deleted as it leaves
193
- # (SessionManager.run_session_loop).
194
+ # (WorkerBoot.run_session_loop).
194
195
  def scratch? = !!@session&.scratch
195
196
 
196
197
  # Whether delegate reports wait that an idle parent runs a turn for
197
- # (#wake_state): SessionManager.run_session_loop starts a new worker
198
+ # (#wake_state): WorkerBoot.run_session_loop starts a new worker
198
199
  # for them after this one leaves. Reports the budget or a failure held
199
200
  # back wait for the next human input instead.
200
201
  def wake_due? = wake_state == :due
@@ -208,7 +209,7 @@ module Samagotchi
208
209
  # (chi stop), :signal when a signal stopped it (WorkerSignals),
209
210
  # :crashed when the loop raised (the session is marked errored),
210
211
  # :scratch_left when a scratch session's terminal is gone (the caller
211
- # deletes it); SessionManager.run_session_loop turns it into the exit
212
+ # deletes it); WorkerBoot.run_session_loop turns it into the exit
212
213
  def run
213
214
  # The thread a signal's expired grace raises in (the main thread in a
214
215
  # worker process).
@@ -261,7 +262,7 @@ module Samagotchi
261
262
  # This worker holds the session now (a continue's starting marker held
262
263
  # off any other until it did), and a continue that died moving
263
264
  # delegates here left its intent: finish it.
264
- ChildMove.clear_starting(@session_id, state_dir: @state_dir)
265
+ StartingMarker.clear(@session_id, state_dir: @state_dir)
265
266
  ChildMove.adopt(@session_id, state_dir: @state_dir)
266
267
  @session = Session.load(@session_id, state_dir: @state_dir)
267
268
  # A newer chi saved it: this worker keeps the fields it doesn't know
@@ -362,25 +363,25 @@ module Samagotchi
362
363
  # The Bridge (UIs and clients reach the worker from here on), then the
363
364
  # idle-exit policy.
364
365
  def serve
365
- @bridge = SessionManager.start_bridge(engine: @engine, state_dir: @state_dir, session_id: @session_id,
366
- on_input: -> { @waker.wake },
367
- on_command: method(:on_command),
368
- on_exit_request: method(:exit_request),
369
- # Decided again as it leaves (a note may still come in).
370
- exit_discards: method(:empty_session?),
371
- # What a stop-all /exit stopped, for its 200.
372
- exit_stopped: method(:stopped_holds),
373
- # chi web's Stop asks about these before TERMed-ing
374
- # the worker (its own holds routes).
375
- on_holds: -> { @idle_exit&.stoppable_holds },
376
- on_holds_stop: -> { @hold_stop&.stop_all })
366
+ @bridge = start_bridge(engine: @engine, state_dir: @state_dir, session_id: @session_id,
367
+ on_input: -> { @waker.wake },
368
+ on_command: method(:on_command),
369
+ on_exit_request: method(:exit_request),
370
+ # Decided again as it leaves (a note may still come in).
371
+ exit_discards: method(:empty_session?),
372
+ # What a stop-all /exit stopped, for its 200.
373
+ exit_stopped: method(:stopped_holds),
374
+ # chi web's Stop asks about these before TERMed-ing
375
+ # the worker (its own holds routes).
376
+ on_holds: -> { @idle_exit&.stoppable_holds },
377
+ on_holds_stop: -> { @hold_stop&.stop_all })
377
378
  # What failed to load and what plugins showed as they loaded, now that
378
379
  # a UI can be there (a late one gets them in the snapshot).
379
380
  @engine.announce_load_events!
380
381
  announce_session_warnings
381
382
  @idle_exit = WorkerIdleExit.new(
382
383
  engine: @engine, bridge: @bridge,
383
- timeout_minutes: @idle_exit_minutes || SessionManager.config_idle_exit_minutes,
384
+ timeout_minutes: @idle_exit_minutes || config_idle_exit_minutes,
384
385
  input_pending: method(:input_pending?),
385
386
  awaiting_continue: -> { @turn_flow.awaiting_continue? },
386
387
  running_tasks: -> { Tools::TaskRuntime.running_created_in(@engine.messages_checkpoint) },
@@ -398,6 +399,33 @@ module Samagotchi
398
399
  state_dir: @state_dir)
399
400
  end
400
401
 
402
+ # Start the in-process Bridge transport for this worker. The bridge is
403
+ # the single live client transport, so every worker starts it. Bridge
404
+ # creation happens *before* the loop so the capture observer is in place
405
+ # for the whole session; the caller stops the returned instance on exit
406
+ # (see #run's ensure).
407
+ #
408
+ # @return [Samagotchi::Bridge, nil] nil when the transport failed to start
409
+ # (the worker degrades: turns still flow through the input-dir loop, but
410
+ # there is no live SSE or in-process cancel/answer).
411
+ # @param on_input [#call, nil] called after the Bridge queues a turn
412
+ def start_bridge(engine:, state_dir:, session_id:, on_input: nil, on_command: nil, on_exit_request: nil,
413
+ exit_discards: nil, exit_stopped: nil, on_holds: nil, on_holds_stop: nil)
414
+ require_relative "bridge"
415
+ Samagotchi::Bridge.new(
416
+ engine: engine, state_dir: state_dir, session_id: session_id, input_format: SessionInbox::INPUT_FORMAT,
417
+ on_input: on_input, on_command: on_command, on_exit_request: on_exit_request, exit_discards: exit_discards,
418
+ exit_stopped: exit_stopped, on_holds: on_holds, on_holds_stop: on_holds_stop
419
+ ).start
420
+ rescue StandardError => e
421
+ Log.error(:bridge, "start_failed", echo: "Bridge: failed to start for session #{session_id}: #{e.class}: #{e.message}", sid: session_id, error: e.class.name)
422
+ nil
423
+ end
424
+
425
+ def config_idle_exit_minutes
426
+ Degrade.call(:config, "idle_exit_minutes_unreadable") { Samagotchi::Config.get("session.idle_exit_minutes") }
427
+ end
428
+
401
429
  # The delegate children of this session that are running a turn or have
402
430
  # a live worker. ChildrenStatus.of scans the session files; this runs on
403
431
  # an exit request, not every pass.
@@ -1634,7 +1662,7 @@ module Samagotchi
1634
1662
 
1635
1663
  # #leave_idle for a scratch worker its terminal left: checked again with
1636
1664
  # the event log held, then the intake closes. The session is deleted
1637
- # once the lock is free (SessionManager.run_session_loop: :scratch_left).
1665
+ # once the lock is free (WorkerBoot.run_session_loop: :scratch_left).
1638
1666
  # @return [Boolean] false when a client came back since
1639
1667
  def leave_scratch
1640
1668
  stop_bridge_after do
@@ -1842,8 +1870,8 @@ module Samagotchi
1842
1870
  # A restart hands the reminders to the next worker (ReminderCarry), while
1843
1871
  # the event log is held: no turn can register or fire one from here. A
1844
1872
  # write that fails keeps this worker up with them rather than losing them.
1845
- # A session stopped meanwhile drops them (run_session_loop then starts no
1846
- # successor): nothing is written.
1873
+ # A session stopped meanwhile drops them (WorkerBoot.run_session_loop
1874
+ # then starts no successor): nothing is written.
1847
1875
  # @return [Boolean] false when the write failed
1848
1876
  def carry_reminders
1849
1877
  if stopped_on_disk?
@@ -1881,7 +1909,7 @@ module Samagotchi
1881
1909
  # @return [Symbol] +result+, with #discard? decided and, for a session
1882
1910
  # kept, its recap written: the Bridge is closed and the idle jobs are
1883
1911
  # stopped by now, and nothing waits on this (the TUI has detached). A
1884
- # `chi send` meanwhile is picked up after the exit (run_session_loop);
1912
+ # `chi send` meanwhile is picked up after the exit (WorkerBoot.run_session_loop);
1885
1913
  # a `chi --attach` finds no Bridge until then.
1886
1914
  # A restart keeps even an empty session (a new worker takes it over at
1887
1915
  # once) and leaves the recap to the conversation's next real pause.
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "session_manager"
4
+
5
+ module Samagotchi
6
+ # WorkerBoot: the worker process, from run_session_loop (what the boot
7
+ # string SessionManager#worker_command writes runs, through
8
+ # SessionManager.run_session_loop) to what it does as it leaves (CROSS-VERSION
9
+ # CONTRACT: spec/worker_contract_spec.rb). It takes the session's OwnerLock,
10
+ # leaves the spawner's process tree, installs the signals and the project
11
+ # env, and runs the Worker (worker.rb loads only then, after the lock). Calls
12
+ # SessionManager's resume_session, discardable? and delete_session; the env
13
+ # keys it reads are SessionManager's, which the spawner sets.
14
+ module WorkerBoot
15
+ class << self
16
+ # Run the session loop inside the forked process: the worker's main,
17
+ # which the boot string reaches through SessionManager.run_session_loop.
18
+ #
19
+ # Every worker starts its per-session Bridge (the single live client
20
+ # transport) on a port bound to 127.0.0.1 before the loop and stops it on
21
+ # exit. If the bridge fails to start the worker degrades: turns still flow
22
+ # through the input-dir loop, but there is no live SSE or in-process
23
+ # cancel/answer.
24
+ #
25
+ # The worker first takes the session's OwnerLock; when another owner holds
26
+ # it (a racing resume spawned two workers, or the TUI has the session) it
27
+ # exits quietly.
28
+ #
29
+ # A worker nobody uses returns once session.idle_exit_minutes have passed
30
+ # (see WorkerIdleExit), and one a client asked to exit (Bridge POST /exit)
31
+ # returns as soon as nothing keeps it. The next send wakes a new one.
32
+ # @param idle_exit_minutes [Numeric, nil] nil: session.idle_exit_minutes
33
+ # @param poll_interval [Numeric, nil] seconds between the loop's fallback
34
+ # ticks (nil: Worker::FALLBACK_TICK_SECONDS); queued turns wake it at once
35
+ # A stopped session's worker exits 0 (a signalled one too) and a
36
+ # crashed one 1, there.
37
+ # @return [Symbol] :idle_exit, :exit_requested, :restart or :scratch_left
38
+ def run_session_loop(session_id, state_dir: nil, owner_wait: OwnerLock::DEFAULT_WAIT,
39
+ idle_exit_minutes: nil, poll_interval: nil)
40
+ # A worker inherits chi's locale (LC_ALL=C too): read files as UTF-8.
41
+ Utf8Default.apply!
42
+ sd = state_dir || Session.default_state_dir
43
+ # The spawner passed the file and level through ENV; a worker's stderr
44
+ # is /dev/null, so warnings only reach the file.
45
+ Log.configure(stderr: false)
46
+ Log.session_id = session_id
47
+ session_dir = Session.session_dir(session_id, state_dir: sd)
48
+ # Read (and taken out of ENV, so nothing the worker runs inherits it)
49
+ # before anything else: set by spawn_options for a spawned worker.
50
+ spawned = ENV.delete(SessionManager::WORKER_PROCESS_ENV) == "1"
51
+ # Out of the spawner's process tree before the lock (it records this
52
+ # process's pid) and before any thread starts.
53
+ detach_from_spawner(session_dir) if ENV.delete(SessionManager::WORKER_DETACH_ENV) == "1"
54
+ # Kept in a module ivar so the lock's File lives as long as the worker.
55
+ @owner_lock = OwnerLock.acquire(session_dir, kind: "worker", wait: owner_wait)
56
+ exit(0) unless @owner_lock
57
+ # Set by the boot string when the newest installed chi didn't load.
58
+ fallback = ENV.delete(SessionManager::BOOT_FALLBACK_ENV)
59
+ Log.info(:worker, "start", cwd: Dir.pwd, version: VERSION)
60
+ Log.warn(:worker, "boot_fallback", version: VERSION, error: fallback) if fallback
61
+ # Tools follow the project (its `mise env`, computed once per worker;
62
+ # this process's ENV is left as it is); never in a loop run in-process.
63
+ apply_project_env(session_id, state_dir: sd) if spawned
64
+ # A spawned worker stops its turn on TERM/HUP/INT and saves it; a
65
+ # loop run in-process (specs) traps nothing.
66
+ signals = spawned ? WorkerSignals.new.install! : WorkerSignals::NONE
67
+ # Only now, after the detach and the lock: the Engine and all it loads.
68
+ require_relative "worker"
69
+ worker = Worker.new(session_id: session_id, state_dir: sd, session_dir: session_dir,
70
+ idle_exit_minutes: idle_exit_minutes, poll_interval: poll_interval, signals: signals,
71
+ owner_lock: @owner_lock)
72
+ result = begin
73
+ worker.run
74
+ rescue StandardError, ScriptError => e
75
+ # The worker's stderr is /dev/null: the log is the only trace.
76
+ Log.exception(:worker, "crashed", e)
77
+ raise
78
+ ensure
79
+ @owner_lock.release
80
+ # A signal from here on ends the process as Ruby's default does.
81
+ signals.restore!
82
+ end
83
+ Log.info(:worker, "stop", reason: result)
84
+ # A scratch session goes with its worker however it leaves (its
85
+ # terminal deletes it too, whichever comes first); nothing resumes it.
86
+ if worker.scratch?
87
+ delete_left_scratch(session_id, state_dir: sd)
88
+ exit(result == :crashed ? 1 : 0) if %i[stopped signal crashed].include?(result)
89
+ return result
90
+ end
91
+ # A stopped session is left as it is: no resume for input that came
92
+ # in, no discard. One whose worker a signal stopped isn't discarded
93
+ # either; input that came in (queued behind the turn it stopped)
94
+ # wakes a new worker, as after an idle exit.
95
+ exit(0) if result == :stopped
96
+ if result == :signal
97
+ SessionManager.resume_session(session_id, state_dir: sd) unless SessionInbox.find_new_input_files(session_dir).empty?
98
+ exit(0)
99
+ end
100
+ exit(1) if result == :crashed
101
+ # Only after the release: a writer that still saw this worker as the
102
+ # owner may have queued input since the last check. Either it finds no
103
+ # owner after its write and wakes one, or this finds its input.
104
+ # A restart starts its successor here, on the newest chi installed
105
+ # (worker_command), once this one's lock is free.
106
+ # Delegate reports that rang while it left wake one too.
107
+ if result == :restart && Session.stopped_marker?(session_id, state_dir: sd)
108
+ # A stop landed as it left: no successor (resume_session would clear
109
+ # the stop), and the reminders go with it.
110
+ ReminderCarry.remove(session_dir)
111
+ Log.info(:worker, "restart_dropped", why: :stopped)
112
+ elsif result == :restart ||
113
+ (%i[idle_exit exit_requested].include?(result) &&
114
+ (!SessionInbox.find_new_input_files(session_dir).empty? || worker.wake_due?))
115
+ SessionManager.resume_session(session_id, state_dir: sd)
116
+ elsif worker.discard?
117
+ discard_left_session(session_id, state_dir: sd, default_model: worker.default_model)
118
+ end
119
+ result
120
+ end
121
+
122
+ # A spawned worker leaves its spawner's process tree: a killer that
123
+ # walks the tree by parent pid (a terminal closing, a tool's teardown
124
+ # of a `chi send --wait` it ran in the background) no longer reaches it.
125
+ # This process (the spawner's child, which its Process.detach waits on)
126
+ # forks the worker, which starts a session of its own (setsid: no
127
+ # controlling terminal, no SIGHUP from one) and returns to run the loop;
128
+ # this one waits until the worker holds the session's OwnerLock, or has
129
+ # exited, or WORKER_START_GRACE passed, and exits: the spawner's waiter
130
+ # stays alive exactly while the worker starts (#still_starting?). The
131
+ # worker is reparented to init (launchd), which reaps it.
132
+ private def detach_from_spawner(session_dir)
133
+ worker = Process.fork
134
+ unless worker
135
+ Process.setsid
136
+ return
137
+ end
138
+
139
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + SessionManager::WORKER_START_GRACE
140
+ until Process.waitpid(worker, Process::WNOHANG) || OwnerLock.owner(session_dir)&.pid == worker ||
141
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
142
+ sleep(OwnerLock::RETRY_INTERVAL)
143
+ end
144
+ Process.exit!(0)
145
+ end
146
+
147
+ # Delete a scratch session its worker left (its terminal gone:
148
+ # Worker#leave_scratch, or any other way out), now the lock is free.
149
+ # Its terminal may have deleted it first.
150
+ private def delete_left_scratch(session_id, state_dir:)
151
+ SessionManager.delete_session(session_id, state_dir: state_dir)
152
+ Log.info(:worker, "scratch_deleted", sid: session_id)
153
+ rescue SessionManager::DeleteRefused, ArgumentError, SystemCallError => e
154
+ Log.info(:worker, "scratch_kept", sid: session_id, error: e.class.name, msg: e.message)
155
+ end
156
+
157
+ # Delete a session its worker left empty. Checked again now the lock is
158
+ # free: a note or input may have come in since the worker looked, and a
159
+ # worker woken meanwhile owns it (delete_session refuses). A `chi send`
160
+ # landing between this check and the delete fails with "no session"
161
+ # (a window of an empty session's last moments, left as is).
162
+ private def discard_left_session(session_id, state_dir:, default_model:)
163
+ return unless SessionManager.discardable?(session_id, state_dir: state_dir, default_model: default_model)
164
+
165
+ SessionManager.delete_session(session_id, state_dir: state_dir, purge_runs: false)
166
+ Log.info(:worker, "discarded_empty", sid: session_id)
167
+ rescue SessionManager::DeleteRefused, SessionManager::OwnedByTUI, ArgumentError, SystemCallError => e
168
+ Log.info(:worker, "kept_session", sid: session_id, error: e.class.name, msg: e.message)
169
+ end
170
+
171
+ # The tools' ProjectEnv for the session's folder (its working_directory;
172
+ # spawn_options chdirs there unless it is gone), installed for this
173
+ # process's tool spawns. Never fatal: a worker without it still runs.
174
+ private def apply_project_env(session_id, state_dir:)
175
+ session = Session.summary_from_file(Session.session_file(session_id, state_dir: state_dir))
176
+ dir = session&.working_directory.to_s
177
+ overlay = ProjectEnv.install!(ProjectEnv.compute(dir))
178
+ Log.info(:worker, "project_env_applied", dir: dir, ruby: overlay.ruby) if overlay.applied
179
+ rescue StandardError => e
180
+ Log.warn(:worker, "project_env", reason: "error", error: e.class.name, msg: e.message)
181
+ end
182
+ end
183
+ end
184
+ end
@@ -3,7 +3,7 @@
3
3
  module Samagotchi
4
4
  # TERM, HUP and INT in a worker process: the running turn stops as a Stop
5
5
  # does (reason :signal), is saved, and the worker leaves (Worker#run
6
- # returns :signal). SessionManager.run_session_loop installs the traps in
6
+ # returns :signal). WorkerBoot.run_session_loop installs the traps in
7
7
  # a spawned worker only (WORKER_PROCESS_ENV), never in a process that runs
8
8
  # the loop in-process (the specs, which fire #fire directly).
9
9
  #
@@ -3,10 +3,10 @@
3
3
  require "securerandom"
4
4
 
5
5
  require_relative "child_reports"
6
- require_relative "kernel_loop"
7
6
  require_relative "log"
8
7
  require_relative "task_finish_note"
9
8
  require_relative "task_watch"
9
+ require_relative "tool_output_cut"
10
10
  require_relative "tools/task_runtime"
11
11
  require_relative "worker_inbound"
12
12
  require_relative "worker_wakes"
@@ -130,7 +130,7 @@ module Samagotchi
130
130
 
131
131
  def note(entry, record, wake:, turn_id: nil)
132
132
  TaskFinishNote.note(record, limit_sec: entry.limit_sec, early_sec: entry.early_sec, wake: wake, turn_id: turn_id,
133
- cap: KernelLoop.resolve_output_char_cap(nil))
133
+ cap: ToolOutputCut.max_chars(nil))
134
134
  end
135
135
 
136
136
  def add_note(entry, record)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samagotchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.155.0
4
+ version: 0.156.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -622,6 +622,7 @@ files:
622
622
  - lib/samagotchi/remotes.rb
623
623
  - lib/samagotchi/reply_wait.rb
624
624
  - lib/samagotchi/reply_wait/poll.rb
625
+ - lib/samagotchi/reprefill_meter.rb
625
626
  - lib/samagotchi/review_apply.rb
626
627
  - lib/samagotchi/review_command.rb
627
628
  - lib/samagotchi/review_start.rb
@@ -652,6 +653,13 @@ files:
652
653
  - lib/samagotchi/session_delete_command.rb
653
654
  - lib/samagotchi/session_inbox.rb
654
655
  - lib/samagotchi/session_manager.rb
656
+ - lib/samagotchi/session_manager/delivery.rb
657
+ - lib/samagotchi/session_manager/emptiness.rb
658
+ - lib/samagotchi/session_manager/lifecycle.rb
659
+ - lib/samagotchi/session_manager/listing.rb
660
+ - lib/samagotchi/session_manager/removal.rb
661
+ - lib/samagotchi/session_manager/spawn.rb
662
+ - lib/samagotchi/session_manager/worker_command.rb
655
663
  - lib/samagotchi/session_metrics.rb
656
664
  - lib/samagotchi/session_metrics/cost.rb
657
665
  - lib/samagotchi/session_metrics/model_segment.rb
@@ -670,6 +678,7 @@ files:
670
678
  - lib/samagotchi/share_command.rb
671
679
  - lib/samagotchi/side_judgments.rb
672
680
  - lib/samagotchi/side_usage.rb
681
+ - lib/samagotchi/starting_marker.rb
673
682
  - lib/samagotchi/steer.rb
674
683
  - lib/samagotchi/steer_cut.rb
675
684
  - lib/samagotchi/steer_cut_after.rb
@@ -687,6 +696,7 @@ files:
687
696
  - lib/samagotchi/system_one/log.rb
688
697
  - lib/samagotchi/system_one/question.rb
689
698
  - lib/samagotchi/system_one/record.rb
699
+ - lib/samagotchi/system_one/recorder.rb
690
700
  - lib/samagotchi/system_one/recording.rb
691
701
  - lib/samagotchi/system_one/runs.rb
692
702
  - lib/samagotchi/system_one/runs_store.rb
@@ -791,6 +801,7 @@ files:
791
801
  - lib/samagotchi/tools/edit.rb
792
802
  - lib/samagotchi/tools/execute.rb
793
803
  - lib/samagotchi/tools/forget_outputs.rb
804
+ - lib/samagotchi/tools/index_hint.rb
794
805
  - lib/samagotchi/tools/list_reminders.rb
795
806
  - lib/samagotchi/tools/list_sessions.rb
796
807
  - lib/samagotchi/tools/memory.rb
@@ -1070,6 +1081,7 @@ files:
1070
1081
  - lib/samagotchi/web/update_ops.rb
1071
1082
  - lib/samagotchi/web/verify_fresh_routes.rb
1072
1083
  - lib/samagotchi/worker.rb
1084
+ - lib/samagotchi/worker_boot.rb
1073
1085
  - lib/samagotchi/worker_idle_exit.rb
1074
1086
  - lib/samagotchi/worker_inbound.rb
1075
1087
  - lib/samagotchi/worker_link.rb