samagotchi 0.189.0 → 0.190.1
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 +51 -1
- data/docs/architecture.md +4 -1
- data/docs/cli.md +17 -6
- data/docs/configuration.md +1 -0
- data/docs/context.md +1 -1
- data/docs/guardrails.md +25 -9
- data/docs/internals/attached-context.md +4 -1
- data/docs/internals/engine.md +18 -2
- data/docs/pages.md +3 -2
- data/docs/scheduling.md +106 -6
- data/docs/sessions.md +3 -3
- data/docs/sub-agent.md +1 -1
- data/lib/samagotchi/bundles/guardrails/guardrails/rules.yml +15 -0
- data/lib/samagotchi/bundles/guardrails/manifest.yml +2 -2
- data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
- data/lib/samagotchi/bundles/known-names/plugin.rb +17 -1
- data/lib/samagotchi/bundles/schedules/manifest.yml +4 -4
- data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +13 -3
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/config.rb +2 -0
- data/lib/samagotchi/context_fetch.rb +9 -3
- data/lib/samagotchi/engine/models.rb +6 -4
- data/lib/samagotchi/engine/turn_end.rb +20 -2
- data/lib/samagotchi/engine.rb +5 -1
- data/lib/samagotchi/failure_line.rb +43 -0
- data/lib/samagotchi/guardrail_wiring.rb +3 -1
- data/lib/samagotchi/guardrails/chi_dirs.rb +20 -5
- data/lib/samagotchi/guardrails/parent_approvals.rb +1 -1
- data/lib/samagotchi/guardrails/protected_paths.rb +8 -4
- data/lib/samagotchi/guardrails/shell_paths.rb +16 -2
- data/lib/samagotchi/guardrails/targets.rb +1 -1
- data/lib/samagotchi/guardrails/verdict.rb +5 -3
- data/lib/samagotchi/image_store.rb +4 -0
- data/lib/samagotchi/last_turn.rb +2 -1
- data/lib/samagotchi/llm/utf8_scrub.rb +26 -0
- data/lib/samagotchi/llm_context_view.rb +6 -0
- data/lib/samagotchi/model_profile.rb +4 -1
- data/lib/samagotchi/pages/runtime.rb +4 -3
- data/lib/samagotchi/reply_wait.rb +1 -1
- data/lib/samagotchi/schedule_command.rb +99 -5
- data/lib/samagotchi/schedule_display.rb +21 -5
- data/lib/samagotchi/schedules/condition_part.rb +312 -0
- data/lib/samagotchi/schedules/edit_part.rb +27 -3
- data/lib/samagotchi/schedules/notices_part.rb +8 -2
- data/lib/samagotchi/schedules/protection_part.rb +10 -0
- data/lib/samagotchi/schedules/prune_part.rb +90 -0
- data/lib/samagotchi/schedules.rb +68 -82
- data/lib/samagotchi/session_access/local.rb +6 -4
- data/lib/samagotchi/session_chain/links.rb +68 -9
- data/lib/samagotchi/session_continue.rb +7 -2
- data/lib/samagotchi/session_inbox_delivery.rb +2 -1
- data/lib/samagotchi/system_one/self_protect.rb +4 -2
- data/lib/samagotchi/tools/cancel_schedule.rb +27 -2
- data/lib/samagotchi/tools/execute.rb +5 -1
- data/lib/samagotchi/tools/list_schedules.rb +1 -0
- data/lib/samagotchi/tools/task_runtime.rb +6 -0
- data/lib/samagotchi/turn_endings.rb +14 -2
- data/lib/samagotchi/turn_flow.rb +2 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/public/stream_handlers.js +8 -0
- data/lib/samagotchi/web/session_hub.rb +16 -5
- data/lib/samagotchi/worker.rb +6 -4
- metadata +4 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: known-names
|
|
3
|
-
version: 0.2.
|
|
3
|
+
version: 0.2.2
|
|
4
4
|
requires_chi: ">= 0.150.0"
|
|
5
5
|
scope: system
|
|
6
6
|
description: Catches a near-miss of a protected name (home folder, login, git name, repo name, names from config) in a tool call and rejects it with the right spelling; mode ask / correct optional
|
|
@@ -9,6 +9,6 @@ files:
|
|
|
9
9
|
known_names.md: sha256:d5348b95b6c655017b541910b36e384220da280818160201f6ec9a50e1e4287c
|
|
10
10
|
plugin:
|
|
11
11
|
file: plugin.rb
|
|
12
|
-
sha256: sha256:
|
|
12
|
+
sha256: sha256:ba70badd722f108d6e4dbd577cb436b6662e2ae2281c2832f08c220cba4f99fb
|
|
13
13
|
# config.yml bundles: known-names: (another key warns; an older chi ignores this)
|
|
14
14
|
settings: [names, ignore, mode, derive, min_length, max_distance]
|
|
@@ -9,7 +9,9 @@ require "samagotchi/git_env"
|
|
|
9
9
|
# a rejection that names the right one breaks the loop.
|
|
10
10
|
#
|
|
11
11
|
# A token with a shell glob (*?[]{}) is not checked. In "~name" the name
|
|
12
|
-
# after "~" is checked.
|
|
12
|
+
# after "~" is checked. For a name with a digit, a token that differs from
|
|
13
|
+
# it only in digits (ISSUE-8932 for ISSUE-8926, run-3 for run-2) is not a
|
|
14
|
+
# near miss: numbered siblings, not a misspelling.
|
|
13
15
|
#
|
|
14
16
|
# Settings (config.yml, `bundles: known-names:`), all optional:
|
|
15
17
|
# names: [jonathandoe] names to protect besides the derived ones
|
|
@@ -112,6 +114,7 @@ class KnownNames
|
|
|
112
114
|
|
|
113
115
|
max = @max_distance || (name.length < 10 ? 1 : 2)
|
|
114
116
|
bare = name_down.gsub(EDGE_SEPARATORS, "")
|
|
117
|
+
next if digits_only?(down, bare)
|
|
115
118
|
next if (candidate.length - bare.length).abs > max
|
|
116
119
|
|
|
117
120
|
d = self.class.distance(down, bare)
|
|
@@ -134,6 +137,19 @@ class KnownNames
|
|
|
134
137
|
[upper, upper[0].downcase + upper[1..]].include?(candidate)
|
|
135
138
|
end
|
|
136
139
|
|
|
140
|
+
# "ISSUE-8932" for "ISSUE-8926", "run-3" for "run-2": numbered siblings
|
|
141
|
+
# (tickets, worktrees, versions) aren't misspellings, so a token whose
|
|
142
|
+
# non-digit characters are the name's, in order, passes whatever its
|
|
143
|
+
# digits. Only for a name with a digit: for "johndoe", "johndoe2" (an
|
|
144
|
+
# added digit) is still a near miss. A digit in place of a letter
|
|
145
|
+
# ("j0hndoe") never matches here: its letters differ. Both come in
|
|
146
|
+
# downcased, without edge separators.
|
|
147
|
+
def digits_only?(down, name_down)
|
|
148
|
+
return false unless name_down.match?(/\d/)
|
|
149
|
+
|
|
150
|
+
down.delete("0-9") == name_down.delete("0-9")
|
|
151
|
+
end
|
|
152
|
+
|
|
137
153
|
# [where, text] pairs to scan: the command, the cwd a command names,
|
|
138
154
|
# each path, and a write/edit call's own path (in case normalisation
|
|
139
155
|
# dropped it). Not file contents.
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: schedules
|
|
3
|
-
version: 0.1.
|
|
3
|
+
version: 0.1.1
|
|
4
4
|
scope: system
|
|
5
5
|
description: "A Schedules page in chi web (#/p/schedules, chi page run schedules): every durable schedule, why one is off and how to turn it on, and the recent runs; read-only"
|
|
6
6
|
trust_level: reviewed
|
|
7
|
-
#
|
|
8
|
-
requires_chi: ">= 0.
|
|
7
|
+
# runtime.schedules' condition (the If column) is new in the next chi (release:bump sets the floor)
|
|
8
|
+
requires_chi: ">= 0.190.0"
|
|
9
9
|
scripts:
|
|
10
|
-
schedules.rb: sha256:
|
|
10
|
+
schedules.rb: sha256:ac1bf79cb3df54511c0263cd7dde7e671f853a66c932606c8e2a235835afb44d
|
|
11
11
|
pages: [schedules.rb]
|
|
@@ -93,6 +93,16 @@ def turn_on(schedule)
|
|
|
93
93
|
"to run it again: `chi schedule edit #{schedule.id} --date YYYY-MM-DDTHH:MM`, then `chi schedule enable #{schedule.id}`"
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
+
# The If column: the owner's `--if CMD` (chi runs it before each firing;
|
|
97
|
+
# a "not needed" moves the schedule to its next time), cut; nil for none.
|
|
98
|
+
def if_words(schedule)
|
|
99
|
+
condition = schedule.condition
|
|
100
|
+
return nil if condition.nil?
|
|
101
|
+
|
|
102
|
+
cmd = condition.is_a?(Hash) ? condition["cmd"] : nil
|
|
103
|
+
cmd.is_a?(String) ? cut(cmd, NAME_CHARS) : "(can't be read)"
|
|
104
|
+
end
|
|
105
|
+
|
|
96
106
|
def unreadable_lines(ids)
|
|
97
107
|
lines = ids.compact.map do |id|
|
|
98
108
|
# An id too long to show can't be given to cancel either.
|
|
@@ -132,13 +142,13 @@ page "Schedules" do |ui|
|
|
|
132
142
|
{ "name" => schedule.name, "state" => state_words(schedule), "when" => schedule.recurrence_words,
|
|
133
143
|
"next" => clock(schedule.next_due), "last_run" => clock(schedule.last_fired), "last_result" => schedule.last_result,
|
|
134
144
|
"skips" => "#{schedule.skips}/#{schedule.max_skips}", "failures" => "#{schedule.failures}/#{schedule.max_failures}",
|
|
135
|
-
"mode" => schedule.mode, "id" => schedule.id.to_s[0, 8] }
|
|
145
|
+
"if" => if_words(schedule), "mode" => schedule.mode, "id" => schedule.id.to_s[0, 8] }
|
|
136
146
|
end
|
|
137
147
|
more = ordered.size - rows.size
|
|
138
148
|
ui.text "Schedules#{"\n\nThe first #{SCHEDULE_ROWS}; #{more} more in `chi schedule list --all`." if more.positive?}"
|
|
139
149
|
ui.table rows, columns: { "name" => "Name", "state" => "State", "when" => "When", "next" => "Next", "last_run" => "Last run",
|
|
140
|
-
"last_result" => "Last result", "skips" => "Skips", "failures" => "Failures", "
|
|
141
|
-
"id" => "ID" },
|
|
150
|
+
"last_result" => "Last result", "skips" => "Skips", "failures" => "Failures", "if" => "If",
|
|
151
|
+
"mode" => "Mode", "id" => "ID" },
|
|
142
152
|
key: %w[state next]
|
|
143
153
|
|
|
144
154
|
runs = list.entries.flat_map { |schedule| schedule.history.map { |run| [schedule, run] } }
|
data/lib/samagotchi/config.rb
CHANGED
|
@@ -180,6 +180,8 @@ module Samagotchi
|
|
|
180
180
|
# What one chain and one machine may hold, so a routine that re-schedules itself cannot multiply its entries.
|
|
181
181
|
Entry.new(key: "schedule.max_per_session", yaml_path: %w[schedule max_per_session], type: :integer, default: 10, expose: %i[env config], lifecycle: :next_cli_run),
|
|
182
182
|
Entry.new(key: "schedule.max_total", yaml_path: %w[schedule max_total], type: :integer, default: 50, expose: %i[env config], lifecycle: :next_cli_run),
|
|
183
|
+
# How long a schedule's condition (--if CMD) may run before it is killed and counted as a failure (1..300).
|
|
184
|
+
Entry.new(key: "schedule.condition_timeout_seconds", yaml_path: %w[schedule condition_timeout_seconds], type: :integer, default: 30, expose: %i[env config], lifecycle: :next_cli_run),
|
|
183
185
|
|
|
184
186
|
# Images sent to a model (ImageStore): the long side they are downscaled
|
|
185
187
|
# to, the most bytes one may take (bigger → re-encoded as jpeg), and how
|
|
@@ -35,8 +35,13 @@ module Samagotchi
|
|
|
35
35
|
# replaced while it ran: nothing recorded for it).
|
|
36
36
|
Outcome = Data.define(:status, :snapshot, :error)
|
|
37
37
|
# What the command did: +output+ its stdout, +error+ nil or why it
|
|
38
|
-
# failed, +cancelled+ the caller stopped it
|
|
39
|
-
|
|
38
|
+
# failed, +cancelled+ the caller stopped it, +status+ its
|
|
39
|
+
# Process::Status when it ended on its own within the cap (nil when it
|
|
40
|
+
# was stopped: a timeout, the cap, a cancel; or never ran), so a caller
|
|
41
|
+
# telling exit codes apart never reads a TERM trap's exit 0 as a pass.
|
|
42
|
+
Run = Data.define(:output, :error, :stderr, :cancelled, :status) do
|
|
43
|
+
def initialize(output:, error:, stderr:, cancelled:, status: nil) = super
|
|
44
|
+
end
|
|
40
45
|
|
|
41
46
|
module_function
|
|
42
47
|
|
|
@@ -137,9 +142,10 @@ module Samagotchi
|
|
|
137
142
|
|
|
138
143
|
# Checked again once the reader is done: a command that exits before
|
|
139
144
|
# wait's next tick never gets an over from it.
|
|
145
|
+
ended = error.nil? && !out_reader.over?
|
|
140
146
|
error = OVER_CAP if out_reader.over?
|
|
141
147
|
error ||= exit_error(status, stderr_text)
|
|
142
|
-
Run.new(output: out_reader.text, error: error, stderr: stderr_text, cancelled: false)
|
|
148
|
+
Run.new(output: out_reader.text, error: error, stderr: stderr_text, cancelled: false, status: ended ? status : nil)
|
|
143
149
|
rescue SystemCallError => e
|
|
144
150
|
Run.new(output: nil, error: "couldn't run it: #{e.message}", stderr: "", cancelled: false)
|
|
145
151
|
ensure
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "../config"
|
|
4
4
|
require_relative "../effective_model"
|
|
5
|
+
require_relative "../failure_line"
|
|
5
6
|
require_relative "../llm/backend"
|
|
6
7
|
require_relative "../llm/errors"
|
|
7
8
|
require_relative "../log"
|
|
@@ -91,12 +92,13 @@ module Samagotchi
|
|
|
91
92
|
end
|
|
92
93
|
private :record_prompt_parts
|
|
93
94
|
|
|
94
|
-
# A failed turn's one line (the error's
|
|
95
|
-
#
|
|
96
|
-
# "host main" alone
|
|
95
|
+
# A failed turn's one line (FailureLine.for: the error's
|
|
96
|
+
# #summary, else "chi error: …"). A host's failure under an alias says
|
|
97
|
+
# what the alias became (HostRegistry#alias_note): "host main" alone
|
|
98
|
+
# doesn't tell --model splash's user it was splash.
|
|
97
99
|
# @return [String]
|
|
98
100
|
def failure_summary(error)
|
|
99
|
-
summary =
|
|
101
|
+
summary = FailureLine.for(error)
|
|
100
102
|
return summary unless error.is_a?(LLM::ProviderError) && error.host
|
|
101
103
|
|
|
102
104
|
note = @host_registry.alias_note(effective_model_name)
|
|
@@ -16,8 +16,8 @@ module Samagotchi
|
|
|
16
16
|
# @steer_cut, @hooks, @event_log, @client.
|
|
17
17
|
module TurnEndPart
|
|
18
18
|
# How the turn ended, on the session (Session#last_turn): the caller's
|
|
19
|
-
# save puts it in the file the session hub watches
|
|
20
|
-
#
|
|
19
|
+
# save puts it in the file the session hub watches (#end_turn, and
|
|
20
|
+
# #fail_turn_not_begun where no #end_turn ran).
|
|
21
21
|
# @param limit [Integer, nil] the turn ran out of iterations at this
|
|
22
22
|
# limit: "exhausted" => true, "limit" => N, so a wait with nobody to
|
|
23
23
|
# answer the continue offer says so (ReplyWait, ParentReport "limit")
|
|
@@ -32,6 +32,24 @@ module Samagotchi
|
|
|
32
32
|
**stop_facts(event))
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
# A turn that failed before the Engine's began (the Worker's steps
|
|
36
|
+
# before #run_turn raised, or #begin_turn did): idle again, its
|
|
37
|
+
# last_turn and its :turn_failed as an Engine turn's failure has
|
|
38
|
+
# them (TurnEndings#failed_event, the same line), with no
|
|
39
|
+
# :turn_started before it and nothing kept. Observers only (no turn
|
|
40
|
+
# sink); the caller logs it.
|
|
41
|
+
# @param origin [TurnOrigin] the turn's
|
|
42
|
+
# @param turn_id [String, nil] the id the turn would have run under
|
|
43
|
+
def fail_turn_not_begun(session, error, origin:, turn_id: nil)
|
|
44
|
+
synchronize_events do
|
|
45
|
+
event = failed_event(error, 0)
|
|
46
|
+
event[:turn_id] = turn_id if turn_id
|
|
47
|
+
session.status = Session::STATUS_IDLE
|
|
48
|
+
record_last_turn(session, "failed", 0, origin, event)
|
|
49
|
+
emit_event(nil, origin.empty? ? event : event.merge(origin: origin.to_h))
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
35
53
|
# Why a turn stopped (TurnEndings#stop_facts).
|
|
36
54
|
def stop_facts(event) = @turn_endings.stop_facts(event)
|
|
37
55
|
private :stop_facts
|
data/lib/samagotchi/engine.rb
CHANGED
|
@@ -954,7 +954,8 @@ module Samagotchi
|
|
|
954
954
|
# only re-raised: the turn already ended; so is any error there. Any
|
|
955
955
|
# other error (e.g. an LLM::ProviderError) emits :turn_failed and
|
|
956
956
|
# re-raises; a provider error adds error_kind:, retryable:, host: and a
|
|
957
|
-
# one-line summary
|
|
957
|
+
# one-line summary:, chi's own (FailureLine.chi_error?) error_kind:
|
|
958
|
+
# :chi_error and its summary: (logged with its backtrace, chi_error).
|
|
958
959
|
def run_turn(session, prompt, on_event: nil, max_iterations: nil, cancel_controller: nil, max_tool_output_chars: nil, pending_input: nil, continue: false,
|
|
959
960
|
origin: TurnOrigin::NONE, images: [], id: nil, resumed: false, resume_from: nil, verify: nil, revises: nil,
|
|
960
961
|
input_waiting: nil, opener_source: nil, reply_for: nil)
|
|
@@ -997,6 +998,9 @@ module Samagotchi
|
|
|
997
998
|
raise
|
|
998
999
|
end
|
|
999
1000
|
|
|
1001
|
+
# chi's own failure: where it raised goes to the log (its line
|
|
1002
|
+
# says "details in samagotchi.log").
|
|
1003
|
+
Log.exception(:turn, "chi_error", e) if TurnEndings.chi_error?(e)
|
|
1000
1004
|
# Keep what the turn got to (the prompt plus the loop's completed
|
|
1001
1005
|
# tool iterations) like a cancel does, and save it: a worker exits
|
|
1002
1006
|
# after a failed turn.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "llm/errors"
|
|
4
|
+
require_relative "llm/utf8_scrub"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
# Marks an error whose message tells the user what to change (an image
|
|
8
|
+
# the turn can't use, no model configured): a turn it fails shows that
|
|
9
|
+
# message as it is, never as a chi error (FailureLine.chi_error?).
|
|
10
|
+
module UserError
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# The one line a failed turn shows (every UI, through the end event's
|
|
14
|
+
# summary: TurnEndings#failed_event) and its note keeps (TurnNote.failed:
|
|
15
|
+
# the reload's line, the model's; TurnFlow#prompt_turn_failed too), and
|
|
16
|
+
# whether chi itself failed. Light on purpose: the TUI's and chi web's
|
|
17
|
+
# thin loads reach it through TurnFlow.
|
|
18
|
+
module FailureLine
|
|
19
|
+
# A failed turn's error_kind when chi itself failed (.chi_error?).
|
|
20
|
+
CHI_ERROR = :chi_error
|
|
21
|
+
# The longest error message a chi error's line quotes.
|
|
22
|
+
MESSAGE_MAX = 300
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# Whether +error+ is chi's own failure: neither the provider's
|
|
27
|
+
# (LLM::ProviderError, its own kind and summary) nor one that tells the
|
|
28
|
+
# user what to change (UserError).
|
|
29
|
+
def chi_error?(error) = !error.is_a?(LLM::ProviderError) && !error.is_a?(UserError)
|
|
30
|
+
|
|
31
|
+
# A provider error's summary, a UserError's message, else "chi error:
|
|
32
|
+
# <message> (<Class>); details in samagotchi.log". Its first line, read
|
|
33
|
+
# as UTF-8: the message may hold the bytes that failed.
|
|
34
|
+
# @return [String]
|
|
35
|
+
def for(error)
|
|
36
|
+
return error.summary if error.respond_to?(:summary)
|
|
37
|
+
return error.message unless chi_error?(error)
|
|
38
|
+
|
|
39
|
+
message = LLM::Utf8Scrub.call(error.message.to_s).lines.first.to_s.strip[0, MESSAGE_MAX]
|
|
40
|
+
"chi error: #{message} (#{error.class.name}); details in samagotchi.log"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -273,6 +273,7 @@ module Samagotchi
|
|
|
273
273
|
require_relative "chi_home"
|
|
274
274
|
require_relative "chi_shim"
|
|
275
275
|
require_relative "paths"
|
|
276
|
+
require_relative "guardrails/chi_dirs"
|
|
276
277
|
Guardrails::ProtectedPaths.new(
|
|
277
278
|
store_dir: File.dirname(@approvals.path),
|
|
278
279
|
bundles_dir: InstalledBundles.dir,
|
|
@@ -283,7 +284,8 @@ module Samagotchi
|
|
|
283
284
|
runs_dir: File.join(File.dirname(@approvals.path, 2), "system_one"),
|
|
284
285
|
home_path: ChiHome.path,
|
|
285
286
|
shim_path: ChiShim.path,
|
|
286
|
-
plugins_dir: File.join(Paths.state_dir, "plugins")
|
|
287
|
+
plugins_dir: File.join(Paths.state_dir, "plugins"),
|
|
288
|
+
schedules_path: Guardrails::ChiDirs.schedules_file
|
|
287
289
|
)
|
|
288
290
|
end
|
|
289
291
|
end
|
|
@@ -13,10 +13,12 @@ module Samagotchi
|
|
|
13
13
|
# (read each call). Each surface keeps its set:
|
|
14
14
|
# .protected the config dir, the hooks dir, the approval store's
|
|
15
15
|
# dir, the installed bundles' dir, attached context,
|
|
16
|
-
# the peer mailbox, System 1's saved runs
|
|
16
|
+
# the peer mailbox, System 1's saved runs, and one file,
|
|
17
|
+
# schedules.json: an ask only the user may answer
|
|
17
18
|
# (ParentApprovals.chi_files?)
|
|
18
19
|
# .touches .protected and the session repo's git hooks dir: a
|
|
19
|
-
# rule's touches: chi_dirs (Targets#touches_chi?)
|
|
20
|
+
# rule's touches: chi_dirs (Targets#touches_chi?), with
|
|
21
|
+
# .touches_exact (the state dir itself, as a word)
|
|
20
22
|
# .self_protect .protected and the whole state dir, resolved: what
|
|
21
23
|
# System 1 never answers for (SelfProtect)
|
|
22
24
|
# .review the state dir, the config dir and a hooks dir
|
|
@@ -29,16 +31,23 @@ module Samagotchi
|
|
|
29
31
|
|
|
30
32
|
# chi's config dir, the hooks dir, the approval store's dir, the
|
|
31
33
|
# installed bundles' dir, attached context, the peer mailbox (a message
|
|
32
|
-
# there speaks as a peer)
|
|
33
|
-
# (labels there may train a model: no agent writes them)
|
|
34
|
+
# there speaks as a peer), System 1's saved runs
|
|
35
|
+
# (labels there may train a model: no agent writes them), and
|
|
36
|
+
# schedules.json, a file (#schedules_file: a schedule's prompt and its
|
|
37
|
+
# condition run unattended). The readers match a file root as a dir
|
|
38
|
+
# one: the path itself or under it (PathResolve.under?).
|
|
34
39
|
def protected
|
|
35
40
|
hooks = begin Hooks::Loader.hooks_dir rescue nil end
|
|
36
41
|
bundles = begin MemoryPaths.bundles_dir rescue nil end
|
|
37
42
|
[ConfigFile.config_dir, hooks, File.join(Paths.state_dir, "guardrails"), bundles, File.join(Paths.state_dir, "context"),
|
|
38
|
-
File.join(Paths.state_dir, "mailbox"), File.join(Paths.state_dir, "system_one")]
|
|
43
|
+
File.join(Paths.state_dir, "mailbox"), File.join(Paths.state_dir, "system_one"), schedules_file]
|
|
39
44
|
.compact.map { |dir| dir.to_s.chomp("/") }.reject(&:empty?).uniq
|
|
40
45
|
end
|
|
41
46
|
|
|
47
|
+
# The schedules file, where Schedules#path has it for the default
|
|
48
|
+
# sessions dir: the state root above <state dir>/sessions.
|
|
49
|
+
def schedules_file = File.join(Paths.state_dir, "schedules.json")
|
|
50
|
+
|
|
42
51
|
# .protected (as ParentApprovals.chi_dirs, the name its callers and
|
|
43
52
|
# specs know) and the git hooks dir of +base+'s repo (+context+).
|
|
44
53
|
def touches(context, base)
|
|
@@ -46,6 +55,12 @@ module Samagotchi
|
|
|
46
55
|
ParentApprovals.chi_dirs + [context.hooks_dir(base)].compact
|
|
47
56
|
end
|
|
48
57
|
|
|
58
|
+
# The dirs a shell word touches chi by naming exactly (not what is
|
|
59
|
+
# under them): the state dir, where a copy that names only the dir
|
|
60
|
+
# (`cp /tmp/schedules.json <state>/`, `mv`, `rsync`) lands a file root
|
|
61
|
+
# of .protected. Targets#touches_chi? reads it beside #touches.
|
|
62
|
+
def touches_exact = [Paths.state_dir]
|
|
63
|
+
|
|
49
64
|
# .protected (as ParentApprovals.chi_dirs) and the whole state dir,
|
|
50
65
|
# resolved.
|
|
51
66
|
def self_protect
|
|
@@ -37,7 +37,7 @@ module Samagotchi
|
|
|
37
37
|
# what only looks like one). samagotchi/context and samagotchi/mailbox
|
|
38
38
|
# stop before a word character or a dot, so chi's own
|
|
39
39
|
# lib/samagotchi/context_*.rb isn't one.
|
|
40
|
-
CHI_TEXT = %r{\.config/samagotchi|samagotchi/config\.yml|samagotchi/hooks|samagotchi/guardrails|samagotchi/context(?![\w.])|samagotchi/mailbox(?![\w.])|memories/\.bundles}
|
|
40
|
+
CHI_TEXT = %r{\.config/samagotchi|samagotchi/config\.yml|samagotchi/hooks|samagotchi/guardrails|samagotchi/context(?![\w.])|samagotchi/mailbox(?![\w.])|samagotchi/schedules\.json(?![\w.])|memories/\.bundles}
|
|
41
41
|
# The guardrails bundle's ask before a memory removal: its paths are in
|
|
42
42
|
# the config dir, yet a parent may answer it.
|
|
43
43
|
MEMORY_REMOVE_RULE = "memory-remove"
|
|
@@ -15,8 +15,10 @@ module Samagotchi
|
|
|
15
15
|
# hooks, rules, manifests), attached context (a source's
|
|
16
16
|
# command runs later, outside the gate; chi context writes
|
|
17
17
|
# it), the peer mailbox (a message there speaks as a peer
|
|
18
|
-
# agent; chi peer writes it)
|
|
19
|
-
# label there may train a model)
|
|
18
|
+
# agent; chi peer writes it), System 1's saved runs (a
|
|
19
|
+
# label there may train a model) and schedules.json (a
|
|
20
|
+
# schedule's prompt and condition run unattended; chi schedule
|
|
21
|
+
# writes it); nothing legitimate writes there through a tool
|
|
20
22
|
# that says it writes.
|
|
21
23
|
# ask (once / session): config.yml, home.json (the chi home: the
|
|
22
24
|
# Ruby every chi launch boots), the chi shim (ChiShim: the
|
|
@@ -53,8 +55,9 @@ module Samagotchi
|
|
|
53
55
|
# @param home_path [String, nil] home.json (ChiHome.path)
|
|
54
56
|
# @param shim_path [String, nil] the chi shim (ChiShim.path)
|
|
55
57
|
# @param plugins_dir [String, nil] <state dir>/plugins (the bundles' data, their secrets/ in it)
|
|
58
|
+
# @param schedules_path [String, nil] <state dir>/schedules.json (ChiDirs.schedules_file)
|
|
56
59
|
def initialize(store_dir:, bundles_dir:, config_path:, hooks_dir:, context_dir: nil, runs_dir: nil, home_path: nil, shim_path: nil,
|
|
57
|
-
plugins_dir: nil, mailbox_dir: nil)
|
|
60
|
+
plugins_dir: nil, mailbox_dir: nil, schedules_path: nil)
|
|
58
61
|
@plugins_dir = plugins_dir
|
|
59
62
|
# The same, spelled out: this state dir's own plugins/<x>/secrets.
|
|
60
63
|
@secrets_text = plugins_dir && Regexp.union(SECRETS_TEXT, *([plugins_dir.to_s.chomp("/")] | PathResolve.reals(plugins_dir.to_s.chomp("/"))).map do |dir|
|
|
@@ -65,7 +68,8 @@ module Samagotchi
|
|
|
65
68
|
["installed-bundles", bundles_dir, "installed bundles (hooks, rules, manifests) are changed by chi bundle install"],
|
|
66
69
|
["chi-context", context_dir, "attached context sources run commands; change them with chi context"],
|
|
67
70
|
["chi-mailbox", mailbox_dir, "a message there speaks as a peer agent; chi peer writes the peer mailbox"],
|
|
68
|
-
["system-one-runs", runs_dir, "System 1's saved runs are chi's own record"]
|
|
71
|
+
["system-one-runs", runs_dir, "System 1's saved runs are chi's own record"],
|
|
72
|
+
["chi-schedules", schedules_path, "schedules run commands and prompts unattended; change them with chi schedule"]
|
|
69
73
|
]
|
|
70
74
|
@ask = [
|
|
71
75
|
["chi-config", config_path, "changes chi's config.yml"],
|
|
@@ -39,9 +39,23 @@ module Samagotchi
|
|
|
39
39
|
# @param text [Regexp] the fallback for a word that can't be resolved
|
|
40
40
|
# @param cwd [String, nil] where the command starts
|
|
41
41
|
# @param tmp_roots [Array<String>] where a .git/hooks part doesn't count
|
|
42
|
-
|
|
42
|
+
# @param exact [Array<String>] dirs that count only as themselves, not
|
|
43
|
+
# what is under them (chi's state dir: `cp x <state>/` writes a file
|
|
44
|
+
# root there, schedules.json, by naming only the dir)
|
|
45
|
+
def touches?(command, dirs:, text:, cwd:, home: Dir.home, env: ENV, tmp_roots: [], exact: [])
|
|
43
46
|
roots = dirs.compact.map { |dir| PathResolve.real(dir.to_s.chomp("/")) }.reject(&:empty?).uniq
|
|
44
|
-
|
|
47
|
+
exact = exact.compact.map { |dir| PathResolve.real(dir.to_s.chomp("/")) }.reject(&:empty?).uniq
|
|
48
|
+
any_path?(command, text: text, cwd: cwd, home: home, env: env) do |path|
|
|
49
|
+
names?(path, roots, tmp_roots) || exactly?(path, exact)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def exactly?(path, exact)
|
|
54
|
+
return false if exact.empty?
|
|
55
|
+
|
|
56
|
+
PathResolve.reals(path.to_s.chomp("/")).any? do |real|
|
|
57
|
+
exact.any? { |dir| PathResolve.under?(real, dir) && PathResolve.under?(dir, real) }
|
|
58
|
+
end
|
|
45
59
|
end
|
|
46
60
|
|
|
47
61
|
# Whether a word of +command+ names a path the block holds for (each
|
|
@@ -510,7 +510,7 @@ module Samagotchi
|
|
|
510
510
|
require_relative "parent_approvals"
|
|
511
511
|
dirs = @chi_dirs ? @chi_dirs.call : ParentApprovals.chi_dirs
|
|
512
512
|
shell_envs.any? do |home, env|
|
|
513
|
-
ShellPaths.touches?(@command, dirs: dirs, text: ParentApprovals::CHI_SHELL_TEXT, cwd: @cwd, home: home,
|
|
513
|
+
ShellPaths.touches?(@command, dirs: dirs, exact: ChiDirs.touches_exact, text: ParentApprovals::CHI_SHELL_TEXT, cwd: @cwd, home: home,
|
|
514
514
|
env: env, tmp_roots: Outside.tmp_roots)
|
|
515
515
|
end
|
|
516
516
|
end
|
|
@@ -20,13 +20,15 @@ module Samagotchi
|
|
|
20
20
|
# parent model would be writing a model's prompt); and the bundle's
|
|
21
21
|
# asks before answering chi's questions around chi answer (a parent
|
|
22
22
|
# allowing one would answer a grandchild's question as the user); and
|
|
23
|
-
# a shell command that names a bundle's secrets (ProtectedPaths)
|
|
23
|
+
# a shell command that names a bundle's secrets (ProtectedPaths); and
|
|
24
|
+
# a schedule's condition set from the shell (chi-schedule-if: a shell
|
|
25
|
+
# command chi's tick runs later, ungated).
|
|
24
26
|
# A parent agent may never allow them (ParentApprovals.protected?,
|
|
25
27
|
# ParentPolicy's user_only); System 1 may, only if the user lists the
|
|
26
28
|
# rule by name in guardrails.system_one.rules, and never for the
|
|
27
|
-
# chi-answer/chi-broadcast ones (SystemOne::SelfProtect::NEVER_AUTO).
|
|
29
|
+
# chi-answer/chi-broadcast/chi-schedule-if ones (SystemOne::SelfProtect::NEVER_AUTO).
|
|
28
30
|
PROTECTED_RULES = %w[chi-config chi-home chi-shim chi-hooks shell-touches-chi chi-context-cmd chi-broadcast prompt-memory-write
|
|
29
|
-
model-overlay-write chi-answer-piped chi-answer-http bundle-secrets-shell].freeze
|
|
31
|
+
model-overlay-write chi-answer-piped chi-answer-http bundle-secrets-shell chi-schedule-if].freeze
|
|
30
32
|
SCOPES = %w[once session repo rule].freeze
|
|
31
33
|
# Who answered an ask when it was a parent agent, not the user.
|
|
32
34
|
PARENT_AGENT = "parent agent"
|
|
@@ -7,6 +7,7 @@ require "securerandom"
|
|
|
7
7
|
require "tmpdir"
|
|
8
8
|
require_relative "atomic_file"
|
|
9
9
|
require_relative "config"
|
|
10
|
+
require_relative "failure_line"
|
|
10
11
|
|
|
11
12
|
module Samagotchi
|
|
12
13
|
# Reads an image's format and size from its first bytes, in pure Ruby.
|
|
@@ -235,7 +236,10 @@ module Samagotchi
|
|
|
235
236
|
# Messages hold small refs to those files; the base64 is built only when
|
|
236
237
|
# a request is sent.
|
|
237
238
|
module ImageStore
|
|
239
|
+
# What the user sent can't be used (UserError: its message as it is).
|
|
238
240
|
class Error < StandardError
|
|
241
|
+
include UserError
|
|
242
|
+
|
|
239
243
|
# A ref to a file this session doesn't hold as an image: one wording
|
|
240
244
|
# for a turn's refs (#check_refs) and a web upload's (#ref_for).
|
|
241
245
|
def self.unknown_image(file) = new("unknown image #{file.to_s[0, 80]}")
|
data/lib/samagotchi/last_turn.rb
CHANGED
|
@@ -54,7 +54,8 @@ module Samagotchi
|
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
# Why a turn stopped, for a parent agent's wait (ReplyWait::Result takes
|
|
57
|
-
# them by these names): a failed turn's
|
|
57
|
+
# them by these names): a failed turn's error kind (a provider's, or
|
|
58
|
+
# chi_error: FailureLine), whether a
|
|
58
59
|
# retry may help and whether its work stayed (kept_steps: a parent must
|
|
59
60
|
# not send the task again); a canceled one's reason and the hook that
|
|
60
61
|
# stopped it.
|
|
@@ -29,6 +29,32 @@ module Samagotchi
|
|
|
29
29
|
obj
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
|
+
|
|
33
|
+
# +messages+ (a conversation's entries) as a request builds on them:
|
|
34
|
+
# each entry holding a String #call would change (BINARY with high
|
|
35
|
+
# bytes, invalid UTF-8) replaced by its scrubbed copy, before any
|
|
36
|
+
# UTF-8 regex reads it (Steer.wire_text's forged-frame quoting raises
|
|
37
|
+
# on a BINARY string). The same Array when no entry needs it, and the
|
|
38
|
+
# saved entries are never changed: same bytes in, same text out, so a
|
|
39
|
+
# request's prefix (and its prompt cache) is what it was.
|
|
40
|
+
# @param messages [Array<Hash>]
|
|
41
|
+
# @return [Array<Hash>]
|
|
42
|
+
def messages(messages)
|
|
43
|
+
return messages unless messages.any? { |entry| raw?(entry) }
|
|
44
|
+
|
|
45
|
+
messages.map { |entry| raw?(entry) ? call(entry) : entry }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Whether #call would change a String in +obj+ (at any depth).
|
|
49
|
+
def raw?(obj)
|
|
50
|
+
case obj
|
|
51
|
+
when String
|
|
52
|
+
obj.encoding == Encoding::BINARY ? !obj.ascii_only? : obj.encoding == Encoding::UTF_8 && !obj.valid_encoding?
|
|
53
|
+
when Array then obj.any? { |element| raw?(element) }
|
|
54
|
+
when Hash then obj.any? { |key, value| raw?(key) || raw?(value) }
|
|
55
|
+
else false
|
|
56
|
+
end
|
|
57
|
+
end
|
|
32
58
|
end
|
|
33
59
|
end
|
|
34
60
|
end
|
|
@@ -6,6 +6,7 @@ require_relative "tool_ids"
|
|
|
6
6
|
require_relative "llm_context_edit"
|
|
7
7
|
require_relative "llm_context_stale"
|
|
8
8
|
require_relative "llm_context_strategy"
|
|
9
|
+
require_relative "llm/utf8_scrub"
|
|
9
10
|
|
|
10
11
|
module Samagotchi
|
|
11
12
|
# What the model is sent of the stored conversation: the one step
|
|
@@ -91,9 +92,14 @@ module Samagotchi
|
|
|
91
92
|
|
|
92
93
|
def strategy = none? ? NONE : layers
|
|
93
94
|
|
|
95
|
+
# Every loop's request starts here (ChatLoop#wire_messages, KernelLoop's
|
|
96
|
+
# prompt and warm-up), so raw bytes in the history (a BINARY or invalid
|
|
97
|
+
# UTF-8 String) are read as UTF-8 here, once, before any regex pass
|
|
98
|
+
# (LLM::Utf8Scrub.messages: the stored entries stay as they are).
|
|
94
99
|
# @param conversation [Array<Hash>] the stored entries, in order
|
|
95
100
|
# @return [Array<Hash>] the entries to format
|
|
96
101
|
def messages(conversation)
|
|
102
|
+
conversation = LLM::Utf8Scrub.messages(conversation)
|
|
97
103
|
return conversation if none? && conversation.none? { |entry| LLMContextEdit.drop_on?(entry) }
|
|
98
104
|
|
|
99
105
|
group = Group.new
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "log"
|
|
4
4
|
require_relative "model_markup"
|
|
5
|
+
require_relative "failure_line"
|
|
5
6
|
module Samagotchi
|
|
6
7
|
# Encapsulates all model-specific token formats and parsing behavior.
|
|
7
8
|
# Each profile maps to a model family; ModelProfile.resolve picks one
|
|
@@ -17,8 +18,10 @@ module Samagotchi
|
|
|
17
18
|
# (SessionManager.spawn_options, Tools::Builtins.parent_env).
|
|
18
19
|
MODEL_FROM_CLI_ENV = "SAMAGOTCHI_DEFAULT_MODEL_FROM_CLI"
|
|
19
20
|
# No model anywhere (--model, default.model in config.yml, the env): a
|
|
20
|
-
# first run before any config. An ArgumentError, as before
|
|
21
|
+
# first run before any config. An ArgumentError, as before; a failed
|
|
22
|
+
# turn shows its message as it is (UserError).
|
|
21
23
|
class MissingModel < ArgumentError
|
|
24
|
+
include UserError
|
|
22
25
|
end
|
|
23
26
|
|
|
24
27
|
# A host-qualified model whose host isn't configured (a MissingModel,
|
|
@@ -28,14 +28,15 @@ module Samagotchi
|
|
|
28
28
|
# firing's token, its pending turn, extra keys) are chi's bookkeeping.
|
|
29
29
|
SCHEDULE_MEMBERS = %i[id name title mode prompt recurrence_words next_due last_fired last_result enabled state
|
|
30
30
|
off_reason skips max_skips failures max_failures skip_window anchor head created_at
|
|
31
|
-
history].freeze
|
|
31
|
+
history condition].freeze
|
|
32
32
|
|
|
33
33
|
# One schedule as `chi schedule list --format json` says it: +name+ is
|
|
34
34
|
# its title or the start of its prompt in quotes, +state+ "on",
|
|
35
35
|
# "firing" or "off", +off_reason+ why it is off (ScheduleDisplay.off_reason:
|
|
36
36
|
# "owner", "skips", "failures", "done", "missed", "off"; nil when it
|
|
37
|
-
# isn't), +history+ its Runs, oldest first
|
|
38
|
-
#
|
|
37
|
+
# isn't), +history+ its Runs, oldest first, +condition+ the owner's
|
|
38
|
+
# `--if CMD` ({"cmd", "by", "set_at"}; nil for none). Times are ISO
|
|
39
|
+
# 8601 UTC strings, nil when there is none.
|
|
39
40
|
Schedule = Data.define(*SCHEDULE_MEMBERS) do
|
|
40
41
|
# @param json [Hash{String => Object}] ScheduleDisplay.json's; a
|
|
41
42
|
# missing key reads as nil (history as none), an unknown one is
|
|
@@ -53,7 +53,7 @@ module Samagotchi
|
|
|
53
53
|
# @!attribute limit [Integer, nil] the iteration limit an "exhausted"
|
|
54
54
|
# turn ran out at
|
|
55
55
|
# @!attribute error_kind [String, nil] a "failed" turn's provider error
|
|
56
|
-
# kind (credits, server, auth, …), from last_turn
|
|
56
|
+
# kind (credits, server, auth, …) or chi_error, from last_turn
|
|
57
57
|
# @!attribute retryable [Boolean, nil] whether that error may pass on a
|
|
58
58
|
# retry
|
|
59
59
|
# @!attribute cancel_reason [String, nil] a "canceled" turn's reason
|