samagotchi 0.123.0 → 0.125.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 +48 -1
- data/bin/chi +13 -2
- data/docs/architecture.md +23 -3
- data/docs/cli.md +40 -13
- data/docs/desktop.md +51 -3
- data/docs/internals/participants.md +26 -1
- data/docs/internals/remote-sessions.md +2 -0
- data/docs/sessions.md +2 -2
- data/docs/testing.md +2 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/desktop/macos/App.swift +9 -1
- data/lib/samagotchi/desktop/macos/ChiRunner.swift +129 -17
- data/lib/samagotchi/desktop/macos/Images.swift +4 -3
- data/lib/samagotchi/desktop/macos/Panel.swift +315 -80
- data/lib/samagotchi/desktop/macos/Sessions.swift +272 -0
- data/lib/samagotchi/process_group.rb +10 -1
- data/lib/samagotchi/reminder_carry.rb +92 -0
- data/lib/samagotchi/reminder_store.rb +49 -0
- data/lib/samagotchi/run_options.rb +43 -2
- data/lib/samagotchi/session_manager.rb +39 -4
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/terminal_ui/attached_loop/composer.rb +225 -0
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +138 -0
- data/lib/samagotchi/terminal_ui/attached_loop/questions.rb +225 -0
- data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +154 -0
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +302 -0
- data/lib/samagotchi/terminal_ui/attached_loop.rb +16 -1253
- data/lib/samagotchi/terminal_ui/exit_lines.rb +141 -0
- data/lib/samagotchi/terminal_ui/join_replay.rb +235 -0
- data/lib/samagotchi/update_command.rb +40 -13
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +205 -5
- data/lib/samagotchi/web/participants_admin.rb +15 -4
- data/lib/samagotchi/web/public/annotate_bar.css +8 -4
- data/lib/samagotchi/web/public/annotate_bar.js +34 -6
- data/lib/samagotchi/web/public/app.js +106 -6
- data/lib/samagotchi/web/public/claim_link.js +347 -0
- data/lib/samagotchi/web/public/data.js +17 -0
- data/lib/samagotchi/web/public/index.html +21 -2
- data/lib/samagotchi/web/public/participant.html +1 -0
- data/lib/samagotchi/web/public/participant.js +46 -4
- data/lib/samagotchi/web/public/participants_panel.js +15 -0
- data/lib/samagotchi/web/public/route.js +3 -2
- data/lib/samagotchi/web/public/toasts.js +13 -5
- data/lib/samagotchi/web/public/tokens.css +2 -1
- data/lib/samagotchi/web/public/update_notice.js +6 -6
- data/lib/samagotchi/web/public/update_sheet.js +158 -0
- data/lib/samagotchi/web/public/verify_preset.js +18 -0
- data/lib/samagotchi/web/server.rb +116 -7
- data/lib/samagotchi/web/session_hub.rb +28 -0
- data/lib/samagotchi/web/tunnel/tailscale.rb +8 -2
- data/lib/samagotchi/web/update_check.rb +14 -7
- data/lib/samagotchi/worker.rb +57 -3
- data/lib/samagotchi/worker_idle_exit.rb +7 -6
- metadata +13 -1
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// The panel's chi side without the panel: what a send to chi sessions runs
|
|
2
|
+
// (SendPlan, built from the panel's state, run by PanelController), and the
|
|
3
|
+
// build spec's seams: `ChiHelper --sessions` prints the session rows the
|
|
4
|
+
// panel lists, `ChiHelper --send-plan` the chi calls a send would make.
|
|
5
|
+
import Foundation
|
|
6
|
+
|
|
7
|
+
/// A send to chi sessions as the chi calls it makes: `chi send` (a message,
|
|
8
|
+
/// or `--new` on a new row, this Mac's or a remote's) or `chi note`, one per
|
|
9
|
+
/// machine. Pure: nothing runs here.
|
|
10
|
+
struct SendPlan {
|
|
11
|
+
/// One chi call: its args after the launch file's argv, its stdin, its
|
|
12
|
+
/// name in the panel's lines ("chi send"), the remote it goes to and its
|
|
13
|
+
/// time limit.
|
|
14
|
+
struct Call: Equatable {
|
|
15
|
+
let command: String
|
|
16
|
+
let args: [String]
|
|
17
|
+
let stdin: Data?
|
|
18
|
+
var remote: String?
|
|
19
|
+
var timeout: TimeInterval = 10
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// A session on a machine: nil is this Mac.
|
|
23
|
+
struct Target: Equatable {
|
|
24
|
+
let remote: String?
|
|
25
|
+
let id: String
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let kind: SendKind
|
|
29
|
+
/// UUID-shaped session ids and their machines, in list order; not used
|
|
30
|
+
/// with +new+.
|
|
31
|
+
let targets: [Target]
|
|
32
|
+
let new: Bool
|
|
33
|
+
/// The remote a new session starts on; nil: this Mac.
|
|
34
|
+
let newRemote: String?
|
|
35
|
+
/// Where a new session starts (PanelModel.newSessionDir, or
|
|
36
|
+
/// newSessionDir(remote:) on a remote); nil: nowhere known, no call.
|
|
37
|
+
let newDir: String?
|
|
38
|
+
/// `--model` for `chi send --new` on this Mac, verbatim; nil for the
|
|
39
|
+
/// default. Never on a remote (D4: the list is this Mac's).
|
|
40
|
+
let model: String?
|
|
41
|
+
/// The message line, trimmed; `-m` of `chi send`.
|
|
42
|
+
let prompt: String
|
|
43
|
+
/// A message's stdin: the quoted context; nil when there is none.
|
|
44
|
+
let context: String?
|
|
45
|
+
/// `chi note`'s stdin: the message line, then the context.
|
|
46
|
+
let note: String
|
|
47
|
+
/// `chi note --source`, trimmed; "" for none.
|
|
48
|
+
let source: String
|
|
49
|
+
/// `--image` paths; a note's are dropped before (text only).
|
|
50
|
+
let images: [String]
|
|
51
|
+
|
|
52
|
+
/// The panel's state for this send.
|
|
53
|
+
static func from(_ model: PanelModel, kind: SendKind, sessions: [LiveSession], new: Bool, images: [PanelImage]) -> SendPlan {
|
|
54
|
+
let newRemote = new ? model.newRemote : nil
|
|
55
|
+
let newDir = newRemote.map(model.newSessionDir(remote:)) ?? model.newSessionDir
|
|
56
|
+
return SendPlan(kind: kind, targets: sessions.map { Target(remote: $0.remote, id: $0.id) }, new: new, newRemote: newRemote,
|
|
57
|
+
newDir: newDir, model: newRemote == nil ? model.modelForSend : nil,
|
|
58
|
+
prompt: model.trimmedPrompt, context: model.hasContext ? model.text : nil, note: model.noteText,
|
|
59
|
+
source: model.source.trimmingCharacters(in: .whitespacesAndNewlines), images: images.map(\.url.path))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// This Mac's call first, then one per remote in list order. A session
|
|
63
|
+
/// goes once, to the first machine it is under; a remote whose name chi
|
|
64
|
+
/// wouldn't take is left out (the listing never makes one).
|
|
65
|
+
var calls: [Call] {
|
|
66
|
+
if new {
|
|
67
|
+
guard newDir != nil, newRemote.map(Remotes.valid) ?? true else { return [] }
|
|
68
|
+
return [call(ids: [], remote: newRemote)]
|
|
69
|
+
}
|
|
70
|
+
var machines: [String?] = []
|
|
71
|
+
var ids: [String?: [String]] = [:]
|
|
72
|
+
var seen = Set<String>()
|
|
73
|
+
let ordered = targets.filter { $0.remote == nil } + targets.filter { $0.remote != nil }
|
|
74
|
+
for target in ordered where target.remote.map(Remotes.valid) ?? true && seen.insert(target.id).inserted {
|
|
75
|
+
if ids[target.remote] == nil { machines.append(target.remote) }
|
|
76
|
+
ids[target.remote, default: []].append(target.id)
|
|
77
|
+
}
|
|
78
|
+
return machines.map { call(ids: ids[$0] ?? [], remote: $0) }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Reading, converting and a new worker's start take longer than text;
|
|
82
|
+
/// a remote's text, waking a stopped worker there, too.
|
|
83
|
+
private func call(ids: [String], remote: String?) -> Call {
|
|
84
|
+
var args: [String]
|
|
85
|
+
let command: String
|
|
86
|
+
let stdin: Data?
|
|
87
|
+
switch kind {
|
|
88
|
+
case .message, .paste:
|
|
89
|
+
// stdin is the quoted context; without -m, chi takes it as the message.
|
|
90
|
+
command = "chi send"
|
|
91
|
+
args = new ? ["send", "--new", "--dir", newDir ?? ""] : ["send"]
|
|
92
|
+
// The row's name verbatim; none for the default, nor on a remote.
|
|
93
|
+
if new, remote == nil, let model { args += ["--model", model] }
|
|
94
|
+
if !prompt.isEmpty { args += ["-m", prompt] }
|
|
95
|
+
args += images.flatMap { ["--image", $0] }
|
|
96
|
+
stdin = context.map { Data($0.utf8) }
|
|
97
|
+
case .note:
|
|
98
|
+
command = "chi note"
|
|
99
|
+
args = ["note"]
|
|
100
|
+
if !source.isEmpty { args += ["--source", source] }
|
|
101
|
+
stdin = Data(note.utf8)
|
|
102
|
+
}
|
|
103
|
+
if !new { args += ids }
|
|
104
|
+
if let remote { args = ["--remote", remote] + args }
|
|
105
|
+
let timeout: TimeInterval = !images.isEmpty && kind != .note ? 30 : remote == nil ? 10 : 15
|
|
106
|
+
return Call(command: command, args: args, stdin: stdin, remote: remote, timeout: timeout)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private func printJSON(_ object: Any) {
|
|
111
|
+
let data = try! JSONSerialization.data(withJSONObject: object, options: [.sortedKeys])
|
|
112
|
+
print(String(decoding: data, as: UTF8.self))
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// `ChiHelper --sessions [--launch PATH] [--last KEY]... [--order
|
|
116
|
+
/// local-first|remotes-first] [--click KEY]...`: the panel's session rows in
|
|
117
|
+
/// its order (live, then recent), the new row's folder, a section per remote
|
|
118
|
+
/// (rows not listed before, or its line, and its new row's folder) and what
|
|
119
|
+
/// the panel preselects with KEYs as the remembered choice (none by default:
|
|
120
|
+
/// never the user's) when the answers come in that order (this Mac's first
|
|
121
|
+
/// by default), then after the clicks (a row's key, "new", "new:REMOTE" for
|
|
122
|
+
/// a remote's new row, "model" for ⌘M), as JSON ({"rows": [{"id", "desc",
|
|
123
|
+
/// "cwd", "busy", "recent"}], "new_dir", "remotes": [{"name", "rows" (each
|
|
124
|
+
/// with "remote"), "error", "new_dir" (null: no new row)}], "selected",
|
|
125
|
+
/// "new_selected", "new_remote", "picker_open", "message", "hint"}); a
|
|
126
|
+
/// failed listing of this Mac's is its message on stderr, exit 1.
|
|
127
|
+
func sessionsCommand(_ args: [String]) -> Int32 {
|
|
128
|
+
var args = args
|
|
129
|
+
var launchPath = ChiRunner.launchPath
|
|
130
|
+
var last: [String] = []
|
|
131
|
+
var remotesFirst = false
|
|
132
|
+
var clicks: [String] = []
|
|
133
|
+
let usage = "usage: ChiHelper --sessions [--launch PATH] [--last KEY]... [--order local-first|remotes-first] [--click KEY]...\n"
|
|
134
|
+
while !args.isEmpty {
|
|
135
|
+
let arg = args.removeFirst()
|
|
136
|
+
switch arg {
|
|
137
|
+
case "--launch" where !args.isEmpty: launchPath = args.removeFirst()
|
|
138
|
+
case "--last" where !args.isEmpty: last.append(args.removeFirst())
|
|
139
|
+
case "--click" where !args.isEmpty: clicks.append(args.removeFirst())
|
|
140
|
+
case "--order" where !args.isEmpty:
|
|
141
|
+
let order = args.removeFirst()
|
|
142
|
+
guard ["local-first", "remotes-first"].contains(order) else {
|
|
143
|
+
FileHandle.standardError.write(usage.data(using: .utf8)!)
|
|
144
|
+
return 2
|
|
145
|
+
}
|
|
146
|
+
remotesFirst = order == "remotes-first"
|
|
147
|
+
default:
|
|
148
|
+
FileHandle.standardError.write(usage.data(using: .utf8)!)
|
|
149
|
+
return 2
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let runner = ChiRunner(launchPath: launchPath)
|
|
153
|
+
// As the panel: this Mac's lists beside `remote ls`, then each remote's
|
|
154
|
+
// at once. All are awaited, then fed to the panel's model in the order
|
|
155
|
+
// --order says (the panel takes them as they come).
|
|
156
|
+
let queue = DispatchQueue(label: "sessions-command")
|
|
157
|
+
let group = DispatchGroup()
|
|
158
|
+
var local: Result<[LiveSession], ChiError>?
|
|
159
|
+
var names: [String] = []
|
|
160
|
+
var answers: [String: Result<[LiveSession], ChiError>] = [:]
|
|
161
|
+
group.enter()
|
|
162
|
+
runner.sessions(queue: queue) { local = $0; group.leave() }
|
|
163
|
+
group.enter()
|
|
164
|
+
runner.remotes(queue: queue) { found in
|
|
165
|
+
names = found
|
|
166
|
+
for name in found {
|
|
167
|
+
group.enter()
|
|
168
|
+
runner.sessions(remote: name, queue: queue) { answers[name] = $0; group.leave() }
|
|
169
|
+
}
|
|
170
|
+
group.leave()
|
|
171
|
+
}
|
|
172
|
+
group.wait()
|
|
173
|
+
let model = PanelModel()
|
|
174
|
+
model.lastChoice = last
|
|
175
|
+
model.startRemotes(names)
|
|
176
|
+
let showRemotes = { for name in names { model.showRemote(name, answers[name]!) } }
|
|
177
|
+
if remotesFirst { showRemotes() }
|
|
178
|
+
model.showSessions(local!)
|
|
179
|
+
if !remotesFirst { showRemotes() }
|
|
180
|
+
for key in clicks {
|
|
181
|
+
switch key {
|
|
182
|
+
case "new": model.toggleNew()
|
|
183
|
+
case "model": _ = model.pressModel()
|
|
184
|
+
case let key where key.hasPrefix("new:"): model.toggleNew(remote: String(key.dropFirst(4)))
|
|
185
|
+
default: model.toggle(key)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
switch local! {
|
|
189
|
+
case .failure(let error):
|
|
190
|
+
FileHandle.standardError.write("\(error.description)\n".data(using: .utf8)!)
|
|
191
|
+
return 1
|
|
192
|
+
case .success:
|
|
193
|
+
let row = { (s: LiveSession) -> [String: Any] in
|
|
194
|
+
var row: [String: Any] = ["id": s.id, "desc": s.desc ?? NSNull(), "cwd": s.cwd ?? NSNull(), "busy": s.busy ?? NSNull(),
|
|
195
|
+
"recent": s.recent]
|
|
196
|
+
if let remote = s.remote { row["remote"] = remote }
|
|
197
|
+
return row
|
|
198
|
+
}
|
|
199
|
+
let remotes: [[String: Any]] = model.remoteGroups.map { group in
|
|
200
|
+
["name": group.section.name, "rows": group.rows.map(row), "error": group.section.error ?? NSNull(),
|
|
201
|
+
"new_dir": model.newSessionDir(remote: group.section.name) ?? NSNull()]
|
|
202
|
+
}
|
|
203
|
+
printJSON(["rows": model.sessions.map(row), "new_dir": model.newSessionDir, "remotes": remotes,
|
|
204
|
+
"selected": model.selected.sorted(), "new_selected": model.newSelected, "new_remote": model.newRemote ?? NSNull(),
|
|
205
|
+
"picker_open": model.pickerOpen, "message": model.message, "hint": model.machineHint])
|
|
206
|
+
return 0
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// `ChiHelper --send-plan message|note|paste [--id [REMOTE:]ID]... [--new [REMOTE:]DIR]
|
|
211
|
+
/// [--default MODEL] [--model MODEL] [--prompt P] [--source S] [--image PATH]...`:
|
|
212
|
+
/// the panel's text on stdin; prints the calls SendPlan makes as JSON
|
|
213
|
+
/// ([{"command", "args", "stdin", "timeout"}], stdin null when none). An id
|
|
214
|
+
/// as a row's key: "studio:<id>" on a remote. --new is the new row, DIR the
|
|
215
|
+
/// folder of its only (live) session; with REMOTE: (DIR absolute), that
|
|
216
|
+
/// remote's new row; --default is the configured model, --model the pick.
|
|
217
|
+
func sendPlanCommand(_ args: [String]) -> Int32 {
|
|
218
|
+
let usage = "usage: ChiHelper --send-plan message|note|paste [--id [REMOTE:]ID]... [--new [REMOTE:]DIR] [--default MODEL] "
|
|
219
|
+
+ "[--model MODEL] "
|
|
220
|
+
+ "[--prompt P] [--source S] [--image PATH]...\n"
|
|
221
|
+
var args = args
|
|
222
|
+
let kinds: [String: SendKind] = ["message": .message, "note": .note, "paste": .paste]
|
|
223
|
+
guard let first = args.first, let kind = kinds[first] else {
|
|
224
|
+
FileHandle.standardError.write(usage.data(using: .utf8)!)
|
|
225
|
+
return 2
|
|
226
|
+
}
|
|
227
|
+
args.removeFirst()
|
|
228
|
+
let model = PanelModel()
|
|
229
|
+
var sessions: [LiveSession] = [], newDir: String?, images: [PanelImage] = []
|
|
230
|
+
while !args.isEmpty {
|
|
231
|
+
let arg = args.removeFirst()
|
|
232
|
+
guard !args.isEmpty else { FileHandle.standardError.write(usage.data(using: .utf8)!); return 2 }
|
|
233
|
+
let value = args.removeFirst()
|
|
234
|
+
switch arg {
|
|
235
|
+
case "--id":
|
|
236
|
+
// A UUID has no ":", so the last one splits off the remote.
|
|
237
|
+
let colon = value.lastIndex(of: ":")
|
|
238
|
+
var session = LiveSession(id: colon.map { String(value[value.index(after: $0)...]) } ?? value, desc: nil, cwd: nil,
|
|
239
|
+
busy: nil, owner: nil, updatedAt: nil)
|
|
240
|
+
session.remote = colon.map { String(value[..<$0]) }
|
|
241
|
+
sessions.append(session)
|
|
242
|
+
case "--new" where value.hasPrefix("/"): newDir = value
|
|
243
|
+
case "--new":
|
|
244
|
+
guard let colon = value.firstIndex(of: ":") else { FileHandle.standardError.write(usage.data(using: .utf8)!); return 2 }
|
|
245
|
+
let name = String(value[..<colon])
|
|
246
|
+
var session = LiveSession(id: UUID().uuidString, desc: nil, cwd: String(value[value.index(after: colon)...]), busy: nil,
|
|
247
|
+
owner: nil, updatedAt: nil)
|
|
248
|
+
session.remote = name
|
|
249
|
+
model.startRemotes([name])
|
|
250
|
+
model.showRemote(name, .success([session]))
|
|
251
|
+
model.newRemote = name
|
|
252
|
+
case "--default": model.newModel = value
|
|
253
|
+
case "--model": model.pickedModel = value
|
|
254
|
+
case "--prompt": model.prompt = value
|
|
255
|
+
case "--source": model.source = value
|
|
256
|
+
case "--image": images.append(PanelImage(url: URL(fileURLWithPath: value), temp: false))
|
|
257
|
+
default:
|
|
258
|
+
FileHandle.standardError.write(usage.data(using: .utf8)!)
|
|
259
|
+
return 2
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
model.text = String(decoding: FileHandle.standardInput.readDataToEndOfFile(), as: UTF8.self)
|
|
263
|
+
if let newDir {
|
|
264
|
+
model.sessions = [LiveSession(id: UUID().uuidString, desc: nil, cwd: newDir, busy: nil, owner: nil, updatedAt: nil)]
|
|
265
|
+
}
|
|
266
|
+
let plan = SendPlan.from(model, kind: kind, sessions: sessions, new: newDir != nil || model.newRemote != nil, images: images)
|
|
267
|
+
printJSON(plan.calls.map { call -> [String: Any] in
|
|
268
|
+
["command": call.command, "args": call.args, "timeout": call.timeout,
|
|
269
|
+
"stdin": call.stdin.map { String(decoding: $0, as: UTF8.self) } ?? NSNull()]
|
|
270
|
+
})
|
|
271
|
+
return 0
|
|
272
|
+
}
|
|
@@ -119,7 +119,7 @@ module Samagotchi
|
|
|
119
119
|
# How long #stop waits for +stopped+ after the KILL by default.
|
|
120
120
|
KILL_WAIT_SEC = 1.0
|
|
121
121
|
|
|
122
|
-
# TERMs the group, waits up to +grace+ for +stopped+, then KILLs it and
|
|
122
|
+
# TERMs (and CONTs) the group, waits up to +grace+ for +stopped+, then KILLs it and
|
|
123
123
|
# waits up to +kill_wait+ for +stopped+ again (a KILL is delivered, not
|
|
124
124
|
# done, when kill returns).
|
|
125
125
|
#
|
|
@@ -146,6 +146,15 @@ module Samagotchi
|
|
|
146
146
|
extra = walk ? descendant_groups(pgid) : []
|
|
147
147
|
signal(pgid, "TERM", **signal_options)
|
|
148
148
|
extra.each { |group| signal(group, "TERM") }
|
|
149
|
+
# A stopped process (SIGSTOP, Ctrl-Z) holds the TERM pending, its
|
|
150
|
+
# handler unrun, until it is continued: CONT it after the TERM, so it
|
|
151
|
+
# resumes with the TERM already waiting (a CONT first would let it run
|
|
152
|
+
# unsignalled, to be stopped again) and stops within the grace, not by
|
|
153
|
+
# the KILL. A running process ignores the CONT. Always quiet: a group
|
|
154
|
+
# the TERM already ended may be zombies only by now, which macOS
|
|
155
|
+
# refuses a signal with EPERM.
|
|
156
|
+
signal(pgid, "CONT", **signal_options, quiet: QUIET)
|
|
157
|
+
extra.each { |group| signal(group, "CONT") }
|
|
149
158
|
# +stopped+ is asked until it says true once (it may reap: a second
|
|
150
159
|
# wait on a reaped pid raises ECHILD), then only the extra groups.
|
|
151
160
|
done = false
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require "time"
|
|
6
|
+
require_relative "atomic_file"
|
|
7
|
+
require_relative "log"
|
|
8
|
+
|
|
9
|
+
module Samagotchi
|
|
10
|
+
# A restart's hand-off of the session's reminders, which otherwise live only
|
|
11
|
+
# in the worker's Engine: the leaving worker writes `<id>/reminders.json`
|
|
12
|
+
# (Worker#leave_on_request, as it takes a restart) and its successor takes
|
|
13
|
+
# it as it starts (Worker#start): read and deleted before its Engine is
|
|
14
|
+
# built, restored into the Engine's store after. Only an asked
|
|
15
|
+
# restart writes one: a stop, an idle exit, a crash and a fallback restart
|
|
16
|
+
# (a TERM) drop the reminders as before, and a stop or an archive removes a
|
|
17
|
+
# file left behind (SessionManager.stop_session, .archive_session); a
|
|
18
|
+
# restart a stop landed on starts no successor (.run_session_loop). A file is honoured only by the same
|
|
19
|
+
# session, within WINDOW of its writing (a successor starts in seconds), so
|
|
20
|
+
# one no successor read can't bring reminders back a day later.
|
|
21
|
+
module ReminderCarry
|
|
22
|
+
FILE = "reminders.json"
|
|
23
|
+
# Seconds a hand-off stays good after it was written.
|
|
24
|
+
WINDOW = 120
|
|
25
|
+
|
|
26
|
+
module_function
|
|
27
|
+
|
|
28
|
+
def path(session_dir) = File.join(session_dir, FILE)
|
|
29
|
+
|
|
30
|
+
# Write +store+'s reminders for the successor; no reminders, no file.
|
|
31
|
+
# @return [Integer] how many were written
|
|
32
|
+
# @raise [SystemCallError] when the write fails (the caller stays up)
|
|
33
|
+
def write(session_dir, session_id:, store:, wall: Time.now)
|
|
34
|
+
entries = store.export(wall: wall)
|
|
35
|
+
if entries.empty?
|
|
36
|
+
remove(session_dir)
|
|
37
|
+
return 0
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
body = { "session_id" => session_id, "written_at" => wall.getutc.iso8601(3), "reminders" => entries }
|
|
41
|
+
AtomicFile.write(path(session_dir), "#{JSON.generate(body)}\n", perm: 0o600)
|
|
42
|
+
entries.size
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Read the hand-off and delete it. A file of another session, outside
|
|
46
|
+
# WINDOW or unreadable, or one found as a stopped session starts, is
|
|
47
|
+
# deleted unused (log reminders_carry_dropped; at warn unless stopped).
|
|
48
|
+
# @return [Array<Hash>, nil] the entries for ReminderStore#restore, nil
|
|
49
|
+
# when there is none to restore
|
|
50
|
+
def take(session_dir, session_id:, stopped: false, wall: Time.now)
|
|
51
|
+
file = path(session_dir)
|
|
52
|
+
return nil unless File.exist?(file)
|
|
53
|
+
|
|
54
|
+
data = read(file)
|
|
55
|
+
remove(session_dir)
|
|
56
|
+
why = drop_reason(data, session_id, stopped, wall)
|
|
57
|
+
return data["reminders"] unless why
|
|
58
|
+
|
|
59
|
+
if why == :stopped
|
|
60
|
+
Log.info(:worker, "reminders_carry_dropped", why: why)
|
|
61
|
+
else
|
|
62
|
+
Log.warn(:worker, "reminders_carry_dropped", why: why)
|
|
63
|
+
end
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @return [true]
|
|
68
|
+
def remove(session_dir)
|
|
69
|
+
FileUtils.rm_f(path(session_dir))
|
|
70
|
+
true
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def read(file)
|
|
74
|
+
data = JSON.parse(File.read(file))
|
|
75
|
+
data.is_a?(Hash) ? data : nil
|
|
76
|
+
rescue JSON::ParserError, SystemCallError
|
|
77
|
+
nil
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def drop_reason(data, session_id, stopped, wall)
|
|
81
|
+
return :unreadable unless data
|
|
82
|
+
return :stopped if stopped
|
|
83
|
+
return :other_session unless data["session_id"] == session_id
|
|
84
|
+
|
|
85
|
+
written = Time.iso8601(data["written_at"].to_s)
|
|
86
|
+
:stale if (wall - written).abs > WINDOW
|
|
87
|
+
rescue ArgumentError
|
|
88
|
+
:stale
|
|
89
|
+
end
|
|
90
|
+
private_class_method :read, :drop_reason
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "monitor"
|
|
4
4
|
require "securerandom"
|
|
5
|
+
require "time"
|
|
5
6
|
|
|
6
7
|
module Samagotchi
|
|
7
8
|
# Thread-safe store for periodic reminders registered by the agent.
|
|
@@ -122,10 +123,58 @@ module Samagotchi
|
|
|
122
123
|
end
|
|
123
124
|
end
|
|
124
125
|
|
|
126
|
+
# The reminders as a restart hands them to the next worker (ReminderCarry):
|
|
127
|
+
# each one's id, name, description and interval, and its next fire as
|
|
128
|
+
# wall-clock ISO time, since the monotonic clock doesn't cross processes.
|
|
129
|
+
# @return [Array<Hash{String => Object}>]
|
|
130
|
+
def export(wall: Time.now, mono: Process.clock_gettime(Process::CLOCK_MONOTONIC))
|
|
131
|
+
@mutex.synchronize do
|
|
132
|
+
@reminders.each_value.map do |r|
|
|
133
|
+
{ "id" => r[:id], "name" => r[:name], "description" => r[:description],
|
|
134
|
+
"interval_minutes" => r[:interval_minutes],
|
|
135
|
+
"next_fire_at" => (wall + (r[:next_fire_at] - mono)).utc.iso8601(3) }
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Take back what #export wrote in another process. A reminder keeps its
|
|
141
|
+
# id and schedule: one overdue is due now (once, no catch-up), one further
|
|
142
|
+
# ahead than its interval (a clock that jumped) is due one interval from
|
|
143
|
+
# now. An entry #register would refuse, or with no time, is skipped.
|
|
144
|
+
# @param entries [Array<Hash>] #export's, parsed from JSON
|
|
145
|
+
# @return [Array<String>] the names restored
|
|
146
|
+
def restore(entries, wall: Time.now, mono: Process.clock_gettime(Process::CLOCK_MONOTONIC))
|
|
147
|
+
return [] unless entries.is_a?(Array)
|
|
148
|
+
|
|
149
|
+
restored = entries.filter_map { |entry| restored_entry(entry, wall, mono) }
|
|
150
|
+
@mutex.synchronize { restored.each { |r| @reminders[r[:name]] = r } }
|
|
151
|
+
restored.map { |r| r[:name] }
|
|
152
|
+
end
|
|
153
|
+
|
|
125
154
|
# Clear all reminders.
|
|
126
155
|
# Called when the session ends or the engine is reset.
|
|
127
156
|
def clear_all
|
|
128
157
|
@mutex.synchronize { @reminders.clear }
|
|
129
158
|
end
|
|
159
|
+
|
|
160
|
+
private
|
|
161
|
+
|
|
162
|
+
def restored_entry(entry, wall, mono)
|
|
163
|
+
return nil unless entry.is_a?(Hash)
|
|
164
|
+
|
|
165
|
+
name = entry["name"].to_s.strip
|
|
166
|
+
description = entry["description"].to_s.strip
|
|
167
|
+
interval = Integer(entry["interval_minutes"], exception: false)
|
|
168
|
+
return nil if name.empty? || description.empty? || interval.nil? || !(1..1440).cover?(interval)
|
|
169
|
+
|
|
170
|
+
next_wall = Time.iso8601(entry["next_fire_at"].to_s)
|
|
171
|
+
period = interval * 60
|
|
172
|
+
next_fire = mono + (next_wall - wall).clamp(0, period)
|
|
173
|
+
id = entry["id"].is_a?(String) && !entry["id"].empty? ? entry["id"] : SecureRandom.hex(6)
|
|
174
|
+
{ id: id, name: name, description: description, interval_minutes: interval,
|
|
175
|
+
last_fire_at: next_fire - period, next_fire_at: next_fire }
|
|
176
|
+
rescue ArgumentError
|
|
177
|
+
nil
|
|
178
|
+
end
|
|
130
179
|
end
|
|
131
180
|
end
|
|
@@ -16,6 +16,32 @@ module Samagotchi
|
|
|
16
16
|
# with the config registry's.
|
|
17
17
|
KNOWN = %w[--prompt --resume --attach --shared --no-shared --model --thinking --toolkit --tool --no-tool --set --parent-answers --memory --mute --append-system-prompt --dir --llm-context --llm-context-apply --llm-context-budget --no-interrupt --non-interactive --verbose --no-default-input --port --open --new-token --tunnel --start-dir].freeze
|
|
18
18
|
|
|
19
|
+
# An OptionParser that also records each switch it took, as the
|
|
20
|
+
# normalized [flag] or [flag, value] (a --[no-]x as --x or --no-x; an
|
|
21
|
+
# abbreviation never gets this far): chi web's restart argv.
|
|
22
|
+
class RecordingParser < OptionParser
|
|
23
|
+
def initialize(record, *, &)
|
|
24
|
+
@record = record
|
|
25
|
+
super(*, &)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def define(*args, &block)
|
|
29
|
+
switch = nil
|
|
30
|
+
switch = super do |value|
|
|
31
|
+
@record&.push(RecordingParser.normalized(switch, value))
|
|
32
|
+
block&.call(value)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.normalized(switch, value)
|
|
37
|
+
flag = switch.long.first || switch.short.first
|
|
38
|
+
return [flag.sub("[no-]", value ? "" : "no-")] if flag.include?("[no-]")
|
|
39
|
+
return [flag] if switch.is_a?(OptionParser::Switch::NoArgument)
|
|
40
|
+
|
|
41
|
+
[flag, value.to_s]
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
19
45
|
module_function
|
|
20
46
|
|
|
21
47
|
# Parse +argv+ in place: what the parser leaves is bin/chi's to read
|
|
@@ -25,7 +51,7 @@ module Samagotchi
|
|
|
25
51
|
# (Config.reload!'s cli_overrides)
|
|
26
52
|
def parse(argv)
|
|
27
53
|
# no_interrupt / no_default_input start from config.yml or the env; the flags below turn them on.
|
|
28
|
-
options = { verbose: false, no_interrupt: Samagotchi::Config.get("no_interrupt"), non_interactive: false, no_default_input: Samagotchi::Config.get("no_default_input"), web: false, web_port: nil, web_open: false, web_markdown: nil }
|
|
54
|
+
options = { verbose: false, no_interrupt: Samagotchi::Config.get("no_interrupt"), non_interactive: false, no_default_input: Samagotchi::Config.get("no_default_input"), web: false, web_port: nil, web_open: false, web_markdown: nil, flags: [] }
|
|
29
55
|
cli_overrides = {}
|
|
30
56
|
|
|
31
57
|
# Strict kebab enforcement: --recap_base_url is unknown (OptionParser would otherwise accept _ as -)
|
|
@@ -100,11 +126,13 @@ module Samagotchi
|
|
|
100
126
|
value = argv[index + 1]
|
|
101
127
|
missing_no_tool.call if value.nil? || value.start_with?("-")
|
|
102
128
|
(options[:no_tools] ||= []) << argv.delete_at(index + 1)
|
|
129
|
+
options[:flags] << ["--no-tool", value]
|
|
103
130
|
argv.delete_at(index)
|
|
104
131
|
elsif arg.start_with?("--no-tool=")
|
|
105
132
|
value = arg.delete_prefix("--no-tool=")
|
|
106
133
|
missing_no_tool.call if value.empty? || value.start_with?("-")
|
|
107
134
|
options[:no_tools] = (options[:no_tools] || []) << value
|
|
135
|
+
options[:flags] << ["--no-tool", value]
|
|
108
136
|
argv.delete_at(index)
|
|
109
137
|
else
|
|
110
138
|
reject_abbreviation.call(arg)
|
|
@@ -139,6 +167,19 @@ module Samagotchi
|
|
|
139
167
|
.push("--no-tool").uniq
|
|
140
168
|
end
|
|
141
169
|
|
|
170
|
+
# The flags not repeated by chi web's restart: one-shot actions of the
|
|
171
|
+
# first start (open a browser, replace the LAN token).
|
|
172
|
+
WEB_ONE_SHOT = %w[--open --new-token].freeze
|
|
173
|
+
|
|
174
|
+
# chi web's own command line again, for its in-place restart: every flag
|
|
175
|
+
# the parse took (options[:flags], in its normalized spelling) but the
|
|
176
|
+
# one-shot ones, so the restarted chi web parses to the same options and
|
|
177
|
+
# config overrides.
|
|
178
|
+
# @return [Array<String>]
|
|
179
|
+
def web_restart_argv(options)
|
|
180
|
+
["web", *options.fetch(:flags).reject { |flag, _| WEB_ONE_SHOT.include?(flag) }.flatten(1)]
|
|
181
|
+
end
|
|
182
|
+
|
|
142
183
|
# The parser's switches (OptionParser::Switch), short and long.
|
|
143
184
|
def parser_switches(parser)
|
|
144
185
|
parser.instance_variable_get(:@stack).flat_map { |list| list.list }
|
|
@@ -148,7 +189,7 @@ module Samagotchi
|
|
|
148
189
|
# @param options [Hash] filled in by the parse
|
|
149
190
|
# @param cli_overrides [Hash] the config keys the flags set
|
|
150
191
|
def build_parser(options, cli_overrides)
|
|
151
|
-
|
|
192
|
+
RecordingParser.new(options[:flags]) do |opts|
|
|
152
193
|
opts.banner = <<~BANNER
|
|
153
194
|
Usage: chi [options]
|
|
154
195
|
chi bootstrap [HOST[:PORT]|URL] first setup: find the model server, write config.yml
|
|
@@ -36,6 +36,7 @@ require_relative "model_profile"
|
|
|
36
36
|
require_relative "installed_gem"
|
|
37
37
|
require_relative "installed_versions"
|
|
38
38
|
require_relative "recap_store"
|
|
39
|
+
require_relative "reminder_carry"
|
|
39
40
|
require_relative "archive_store"
|
|
40
41
|
require_relative "continue_offer"
|
|
41
42
|
require_relative "image_store"
|
|
@@ -402,6 +403,9 @@ module Samagotchi
|
|
|
402
403
|
stopped = tree.select { |sid| owners[sid] }
|
|
403
404
|
stopped.each { |sid| stop_session(sid, state_dir: sd, wait: wait) }
|
|
404
405
|
archived, discarded = tree.partition { |sid| ArchiveStore.archive(sid, state_dir: sd) }
|
|
406
|
+
# A restart's reminders hand-off no successor took (the stop above
|
|
407
|
+
# removes a live one's): archived sessions keep no reminders.
|
|
408
|
+
archived.each { |sid| ReminderCarry.remove(Session.session_dir(sid, state_dir: sd)) }
|
|
405
409
|
# D8: an archive ends the review (unarchiving doesn't restore it).
|
|
406
410
|
Participants.end_sessions(archived, by: "archive", state_dir: sd)
|
|
407
411
|
{ id: id, archived: archived, stopped: stopped, discarded: discarded }
|
|
@@ -620,6 +624,17 @@ module Samagotchi
|
|
|
620
624
|
@spawns_mutex.synchronize { @spawns.clear }
|
|
621
625
|
end
|
|
622
626
|
|
|
627
|
+
# Starter processes are children of chi web, unlike the detached workers
|
|
628
|
+
# they launch; the web restart adopts these pids before replacing itself.
|
|
629
|
+
def self.live_spawn_pids
|
|
630
|
+
@spawns_mutex.synchronize do
|
|
631
|
+
@spawns.delete_if { |_, (_, waiter)| !(waiter.respond_to?(:alive?) && waiter.alive?) }
|
|
632
|
+
@spawns.values.filter_map do |_, waiter|
|
|
633
|
+
waiter.pid if waiter.respond_to?(:pid) && waiter.alive?
|
|
634
|
+
end.uniq.sort
|
|
635
|
+
end
|
|
636
|
+
end
|
|
637
|
+
|
|
623
638
|
# Read any new output files from a session directory.
|
|
624
639
|
# Optionally filters to only files newer than `since_time`.
|
|
625
640
|
def self.read_responses(session_id, since_time: nil, state_dir: nil)
|
|
@@ -652,7 +667,8 @@ module Samagotchi
|
|
|
652
667
|
input_queued: "a prompt is queued",
|
|
653
668
|
continue_offered: "the step-limit question waits for an answer",
|
|
654
669
|
question_pending: "a question or approval waits for an answer",
|
|
655
|
-
|
|
670
|
+
# Only a worker from before ReminderCarry still holds a restart for its reminders.
|
|
671
|
+
reminders: "it has reminders, which its worker's older chi can't carry over a restart",
|
|
656
672
|
command_running: "a /btw (or another command) is still running",
|
|
657
673
|
relays_open: "a delegate's approval relayed here is open or just answered",
|
|
658
674
|
background_tasks: "background tasks it started are still running"
|
|
@@ -811,7 +827,7 @@ module Samagotchi
|
|
|
811
827
|
end
|
|
812
828
|
private_class_method :running_created_task?
|
|
813
829
|
|
|
814
|
-
# Stop a session by sending TERM to its process.
|
|
830
|
+
# Stop a session by sending TERM (then CONT, for a stopped one) to its process.
|
|
815
831
|
# With +wait+ (seconds), also wait for the owner to let go of the session,
|
|
816
832
|
# so a resume right after spawns a fresh worker instead of finding the
|
|
817
833
|
# dying one.
|
|
@@ -827,15 +843,29 @@ module Samagotchi
|
|
|
827
843
|
cancel_running_turn(session_id, state_dir: sd)
|
|
828
844
|
# Mark first: a worker that has not taken the lock yet sees it and exits.
|
|
829
845
|
Session.mark_stopped(session_id, state_dir: sd)
|
|
846
|
+
# A stop drops the reminders: a restart's hand-off no successor took
|
|
847
|
+
# yet must not bring them back on the next resume.
|
|
848
|
+
ReminderCarry.remove(Session.session_dir(session_id, state_dir: sd))
|
|
830
849
|
pid = owner&.pid.to_i
|
|
831
850
|
begin
|
|
832
851
|
Process.kill("TERM", pid) if pid && pid > 0
|
|
833
852
|
rescue Errno::ESRCH
|
|
834
853
|
nil # already exited
|
|
835
854
|
end
|
|
855
|
+
continue_stopped(pid)
|
|
836
856
|
wait_for_owner_release(session_id, timeout: wait, state_dir: sd) if wait
|
|
837
857
|
end
|
|
838
858
|
|
|
859
|
+
# A stopped worker (SIGSTOP, Ctrl-Z) runs its TERM handler only once
|
|
860
|
+
# continued: CONT it after the TERM (as ProcessGroup.stop does), so it
|
|
861
|
+
# leaves now rather than outliving the stop's wait. Best effort: one the
|
|
862
|
+
# TERM ended may be gone already.
|
|
863
|
+
private_class_method def self.continue_stopped(pid)
|
|
864
|
+
Process.kill("CONT", pid) if pid && pid > 0
|
|
865
|
+
rescue Errno::ESRCH, Errno::EPERM
|
|
866
|
+
nil
|
|
867
|
+
end
|
|
868
|
+
|
|
839
869
|
STOP_CANCEL_WAIT = 3.0
|
|
840
870
|
# How long a stop from the CLI (chi sessions stop, delete, a continue's
|
|
841
871
|
# orphan) waits for the worker to let go of the session, here or
|
|
@@ -1126,8 +1156,13 @@ module Samagotchi
|
|
|
1126
1156
|
# A restart starts its successor here, on the newest chi installed
|
|
1127
1157
|
# (worker_command), once this one's lock is free.
|
|
1128
1158
|
# Delegate reports that rang while it left wake one too.
|
|
1129
|
-
if result == :restart
|
|
1130
|
-
|
|
1159
|
+
if result == :restart && Session.stopped_marker?(session_id, state_dir: sd)
|
|
1160
|
+
# A stop landed as it left: no successor (resume_session would clear
|
|
1161
|
+
# the stop), and the reminders go with it.
|
|
1162
|
+
ReminderCarry.remove(session_dir)
|
|
1163
|
+
Log.info(:worker, "restart_dropped", why: :stopped)
|
|
1164
|
+
elsif result == :restart ||
|
|
1165
|
+
(%i[idle_exit exit_requested].include?(result) &&
|
|
1131
1166
|
(!SessionInbox.find_new_input_files(session_dir).empty? || worker.wake_due?))
|
|
1132
1167
|
resume_session(session_id, state_dir: sd)
|
|
1133
1168
|
elsif worker.discard?
|
|
@@ -34,7 +34,7 @@ module Samagotchi
|
|
|
34
34
|
[--scope=all]: every project's sessions; by default only this git project's (all outside a repo)
|
|
35
35
|
--archived: archived sessions too (marked [archived]; json: archived: true)
|
|
36
36
|
stop ID... # stop each session's worker (IDs or unique prefixes); chi --resume ID then starts a fresh one
|
|
37
|
-
restart ID... # hand each session to a new worker on the newest chi installed; refused while a turn
|
|
37
|
+
restart ID... # hand each session to a new worker on the newest chi installed; keeps its reminders; refused while a turn or question would be lost
|
|
38
38
|
archive ID... # hide sessions (and their delegates) from every list and keep them for good; unarchive ID... brings them back
|
|
39
39
|
delete [--force] ID... # delete sessions for good (IDs or unique prefixes); --force stops a live worker first
|
|
40
40
|
prune [--dry-run] [--days N] [--keep N] [--keep-status running,...] [--test-only]
|