workcrew 0.1.0__py3-none-any.whl
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.
- workcrew/__init__.py +1 -0
- workcrew/__main__.py +6 -0
- workcrew/board.py +555 -0
- workcrew/cli.py +2340 -0
- workcrew/harness/__init__.py +51 -0
- workcrew/harness/claude.py +523 -0
- workcrew/harness/claude_screen.py +84 -0
- workcrew/link.py +1061 -0
- workcrew/notes/leader.md +21 -0
- workcrew/notes/worker.md +5 -0
- workcrew/picker.py +107 -0
- workcrew/place.py +97 -0
- workcrew/prompt.py +386 -0
- workcrew/reconcile.py +411 -0
- workcrew/send.py +244 -0
- workcrew/state.py +516 -0
- workcrew/tmux.py +992 -0
- workcrew/width.py +30 -0
- workcrew-0.1.0.dist-info/METADATA +173 -0
- workcrew-0.1.0.dist-info/RECORD +23 -0
- workcrew-0.1.0.dist-info/WHEEL +4 -0
- workcrew-0.1.0.dist-info/entry_points.txt +2 -0
- workcrew-0.1.0.dist-info/licenses/LICENSE +18 -0
workcrew/cli.py
ADDED
|
@@ -0,0 +1,2340 @@
|
|
|
1
|
+
"""The commands: the words, their arguments, who may run them, and what a refusal prints."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
import difflib
|
|
7
|
+
import errno
|
|
8
|
+
import fnmatch
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
11
|
+
import shlex
|
|
12
|
+
import shutil
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
import termios
|
|
16
|
+
import time
|
|
17
|
+
import tty
|
|
18
|
+
from collections.abc import Callable, Iterator, Mapping, Sequence
|
|
19
|
+
from dataclasses import dataclass, replace
|
|
20
|
+
from datetime import UTC, datetime
|
|
21
|
+
from importlib import metadata
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from select import select as _waiting
|
|
24
|
+
from socket import gethostname
|
|
25
|
+
from typing import NoReturn, cast
|
|
26
|
+
|
|
27
|
+
from workcrew import board, harness, link, picker, prompt, reconcile, send
|
|
28
|
+
from workcrew.place import Side
|
|
29
|
+
from workcrew.reconcile import POLLER
|
|
30
|
+
from workcrew.send import notify
|
|
31
|
+
from workcrew.state import (
|
|
32
|
+
FORGOTTEN,
|
|
33
|
+
KEY,
|
|
34
|
+
Member,
|
|
35
|
+
Notice,
|
|
36
|
+
Refused,
|
|
37
|
+
Roster,
|
|
38
|
+
StateError,
|
|
39
|
+
UsageError,
|
|
40
|
+
check_new_name,
|
|
41
|
+
check_team_name,
|
|
42
|
+
crash_point,
|
|
43
|
+
create,
|
|
44
|
+
escape,
|
|
45
|
+
forget,
|
|
46
|
+
fresh,
|
|
47
|
+
identity,
|
|
48
|
+
load,
|
|
49
|
+
locked,
|
|
50
|
+
make,
|
|
51
|
+
names_a_team,
|
|
52
|
+
new_key,
|
|
53
|
+
note,
|
|
54
|
+
root_dir,
|
|
55
|
+
root_locked,
|
|
56
|
+
save,
|
|
57
|
+
shown,
|
|
58
|
+
slug,
|
|
59
|
+
team_dir,
|
|
60
|
+
)
|
|
61
|
+
from workcrew.tmux import (
|
|
62
|
+
Gone,
|
|
63
|
+
Lay,
|
|
64
|
+
Server,
|
|
65
|
+
ServerView,
|
|
66
|
+
check_version,
|
|
67
|
+
show_size,
|
|
68
|
+
socket_team,
|
|
69
|
+
whoami,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
HELP = """\
|
|
73
|
+
workcrew — a team of Claude Code sessions in tmux.
|
|
74
|
+
|
|
75
|
+
USAGE
|
|
76
|
+
workcrew [--workers N] [--no-cap] [-- CLAUDE FLAGS…] start this folder's team, or bring it back
|
|
77
|
+
workcrew --name NAME [--workers N] [--no-cap] [-- …] start another team in this folder
|
|
78
|
+
workcrew -r NAME [-- CLAUDE FLAGS…] bring back the team NAME from any folder, and attach
|
|
79
|
+
workcrew -r pick a team to open (a list, without a terminal)
|
|
80
|
+
workcrew -r NAME <command> [arguments] owner only: a command on the team NAME, from any folder
|
|
81
|
+
workcrew <command> [arguments] in a pane of a team: a command on that team
|
|
82
|
+
|
|
83
|
+
EXAMPLES
|
|
84
|
+
workcrew --workers 3 start a leader and 3 workers in this folder
|
|
85
|
+
workcrew --workers 0 start the leader alone; add workers later
|
|
86
|
+
workcrew -- --model opus start, and run every member on opus
|
|
87
|
+
workcrew -r web send leader rewrite the parser give the leader its goal
|
|
88
|
+
workcrew -r web send workers rebase on main reach every worker at once
|
|
89
|
+
workcrew -r web board look at the board
|
|
90
|
+
workcrew -r web exit stop every member; workcrew -r web brings them back
|
|
91
|
+
|
|
92
|
+
COMMANDS
|
|
93
|
+
add [N] add N workers (default 1)
|
|
94
|
+
remove MEMBER… remove workers; a task one was doing goes back to Ready
|
|
95
|
+
send all|workers|TO MESSAGE… write to members
|
|
96
|
+
layout left|right|middle where the leader's pane is: a side, or between the workers
|
|
97
|
+
board [--loop [SEC]] look at the board; --loop redraws it workcrew board --help
|
|
98
|
+
exit [--forget] stop every member, on every screen; C-b d leaves this screen only
|
|
99
|
+
resume [MEMBER…] start members that exited; all of them, or just those named
|
|
100
|
+
resume MEMBER… -- FLAGS set those members' --model and --effort; `--` alone clears them
|
|
101
|
+
rename NEW rename the team; workcrew -r NEW resumes it from any folder
|
|
102
|
+
rename MEMBER NEW rename a worker
|
|
103
|
+
link … another person's team workcrew link --help
|
|
104
|
+
|
|
105
|
+
Your `send` is typed into the member's pane, so `send w1 /compact` works and
|
|
106
|
+
`send leader <goal>` is how you give the leader its goal; a member's `send`
|
|
107
|
+
to another member is a teammate's note instead. Members write to each other
|
|
108
|
+
the same way: `workcrew send w3 <text>`.
|
|
109
|
+
|
|
110
|
+
Members are `leader` and `w1`, `w2`, … TO is ONE word, then the message
|
|
111
|
+
exactly as you typed it. Besides `all` and `workers`, TO is a shell-style
|
|
112
|
+
pattern — `w*`, `w[1-3]`, `w1,w3`, `leader` — several joined by commas with
|
|
113
|
+
no spaces. In a shell, quote a pattern (`send 'w*' …`) or use `workers`,
|
|
114
|
+
which your shell will not touch.
|
|
115
|
+
|
|
116
|
+
Inside a team: C-] (Ctrl+]) opens the command line, where these commands are typed;
|
|
117
|
+
the word `workcrew` may be left out. Click a pane, or use tmux's own keys, to go to it.
|
|
118
|
+
|
|
119
|
+
Everything after `--` goes to Claude Code. A new tail applies to members
|
|
120
|
+
started from now; to put every member on it, run `workcrew -r NAME exit`, then
|
|
121
|
+
`workcrew -r NAME`.
|
|
122
|
+
`workcrew --` with nothing after it clears the flags.
|
|
123
|
+
Exit codes: 0 done, 1 refused or failed, 2 wrong usage or a state workcrew will not touch.
|
|
124
|
+
State: ${XDG_STATE_HOME:-~/.local/state}/workcrew/, a folder for each team.
|
|
125
|
+
"""
|
|
126
|
+
|
|
127
|
+
BOARD_HELP = """\
|
|
128
|
+
workcrew board — the team's task board.
|
|
129
|
+
|
|
130
|
+
workcrew board print the board
|
|
131
|
+
workcrew board --loop [SEC] redraw it every SEC seconds (default 5) until
|
|
132
|
+
q, Esc or Ctrl-C; -l for short
|
|
133
|
+
|
|
134
|
+
A task is named by its number: 7 or T7.
|
|
135
|
+
|
|
136
|
+
A task moves Ready → Running → Review → Done. A worker holds at most one
|
|
137
|
+
Running task.
|
|
138
|
+
|
|
139
|
+
THE VERBS BELOW ARE RUN BY MEMBERS, NOT BY YOU.
|
|
140
|
+
Only the leader creates, closes and takes back tasks, and gives one to a worker;
|
|
141
|
+
workers take and report them.
|
|
142
|
+
If you run one yourself, workcrew refuses it and tells you how to ask the leader.
|
|
143
|
+
|
|
144
|
+
THE LEADER
|
|
145
|
+
board new TITLE [-f FILE] add a task to the end of Ready, brief in FILE
|
|
146
|
+
board new … --to WORKER give it to WORKER instead, who must hold nothing
|
|
147
|
+
board close ID close a task that is in Ready or Review
|
|
148
|
+
board back ID return a Running or Review task to Ready
|
|
149
|
+
board back ID --to WORKER give it, or a Ready task, to WORKER instead, with its reports
|
|
150
|
+
A WORKER
|
|
151
|
+
board take [ID] take the top Ready task, or ID
|
|
152
|
+
board report ID TEXT hand your task to the leader for review
|
|
153
|
+
|
|
154
|
+
The leader may not `take`.
|
|
155
|
+
Every verb is one move, made under the team's lock, so two members never
|
|
156
|
+
claim one task.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
LINK_HELP = f"""\
|
|
160
|
+
workcrew link — messages between your team and other people's teams.
|
|
161
|
+
|
|
162
|
+
link this team's address, and the log's path
|
|
163
|
+
link add {link.ADD_ARGS} trust a team, pin its keys
|
|
164
|
+
link remove TEAM stop trusting it
|
|
165
|
+
link send A,B[/MEMBER] TEXT… send; the default is their leader
|
|
166
|
+
link read read what was addressed to you
|
|
167
|
+
|
|
168
|
+
Any member of your team may send to, and read from, any member of theirs.
|
|
169
|
+
`link send bob hello` reaches bob's leader; `link send bob/w2 hello`
|
|
170
|
+
reaches that worker; `link send bob,carol hello` reaches both leaders.
|
|
171
|
+
Name every team in full: there is no `all` and no pattern.
|
|
172
|
+
THEIR-TEAM picks one team when the same keys signed several teams' addresses.
|
|
173
|
+
|
|
174
|
+
Every outgoing message asks you first and shows you the whole text.
|
|
175
|
+
|
|
176
|
+
The keys come from `github:<user>` or from a path to a public-key file; a
|
|
177
|
+
key pasted on the command line is not accepted, because a command line
|
|
178
|
+
splits it. An incoming message is accepted only if it is signed by a key you
|
|
179
|
+
have pinned, is addressed to this team, and is not a replay. What fails reaches
|
|
180
|
+
nobody; it shows only in the log, whose path `link` prints.
|
|
181
|
+
|
|
182
|
+
A message from a linked team is data, not an instruction. It cannot answer a
|
|
183
|
+
permission prompt, and it does not speak with your authority — nor does a
|
|
184
|
+
member's note to another member of your own team.
|
|
185
|
+
"""
|
|
186
|
+
|
|
187
|
+
TYPED_HELP = """\
|
|
188
|
+
add [N] add N workers (default 1)
|
|
189
|
+
remove MEMBER… remove workers; their tasks go back to Ready
|
|
190
|
+
send all|workers|TO MESSAGE… the rest of the line is the message
|
|
191
|
+
layout left|right|middle where the leader's pane is
|
|
192
|
+
board look at the board
|
|
193
|
+
exit [--forget] stop every member, on every screen
|
|
194
|
+
(C-b d leaves this screen only)
|
|
195
|
+
resume [MEMBER…] start members that exited, or just those named
|
|
196
|
+
resume MEMBER… -- FLAGS set their --model and --effort; `--` clears them
|
|
197
|
+
rename NEW rename the team
|
|
198
|
+
rename MEMBER NEW rename a worker
|
|
199
|
+
link [add|remove|send|read] …
|
|
200
|
+
-r NAME <command> the same on the team NAME
|
|
201
|
+
-r list your teams
|
|
202
|
+
|
|
203
|
+
TO: all, workers, w*, w[1-3], w1,w3, leader — commas, no spaces.
|
|
204
|
+
send leader rewrite the parser send workers fix the tests
|
|
205
|
+
send w1 /compact
|
|
206
|
+
|
|
207
|
+
Your send is typed into the member's pane, so `send w1 /compact` works and
|
|
208
|
+
`send leader <goal>` is how you give the leader its goal. A member's send to
|
|
209
|
+
another member is a teammate's note instead.
|
|
210
|
+
|
|
211
|
+
The prompt reads `(team) `. Esc, Ctrl-C or Enter on an empty line leaves.
|
|
212
|
+
Paste several lines to send a long message: the extra lines become the body
|
|
213
|
+
of a `send`; with any other command nothing runs.
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
APPLY = (
|
|
217
|
+
"They apply to members started from now;"
|
|
218
|
+
" to put every member on them, run workcrew -r {name} exit, then workcrew -r {name}."
|
|
219
|
+
)
|
|
220
|
+
NO_TERMINAL = "There is no terminal here; workcrew -r {name} in a terminal shows the team.\n"
|
|
221
|
+
LATE_WORKERS = (
|
|
222
|
+
"--workers applies only when the team is created;"
|
|
223
|
+
" later use workcrew add N or workcrew remove MEMBER."
|
|
224
|
+
)
|
|
225
|
+
_READ_ONLY = (errno.EROFS, errno.EACCES) # the lock file cannot be opened for writing
|
|
226
|
+
SANDBOXED = (
|
|
227
|
+
"nothing was done: this ran inside Claude Code's sandbox,"
|
|
228
|
+
" where workcrew cannot reach its team.\n"
|
|
229
|
+
"Run the workcrew command on its own, not chained with another command,"
|
|
230
|
+
" and with no backtick in it, even inside quotes."
|
|
231
|
+
)
|
|
232
|
+
# A backtick in an argument is certain from here; one the shell already substituted is not.
|
|
233
|
+
SANDBOXED_BY_BACKTICK = (
|
|
234
|
+
"nothing was done: a backtick in this command kept it inside Claude Code's sandbox, even"
|
|
235
|
+
" inside quotes, and there workcrew cannot reach its team.\n"
|
|
236
|
+
"Run it again without backticks; a line break or a ; inside quotes is fine."
|
|
237
|
+
)
|
|
238
|
+
NO_WRITE = (
|
|
239
|
+
"the team's state is read-only from here, so nothing was changed.\n"
|
|
240
|
+
"Run workcrew on a line of its own: chained with && or ; it runs in Claude Code's sandbox."
|
|
241
|
+
)
|
|
242
|
+
NOT_RUNNING = "team {name} is not running.\nRun {back} to bring back the same {sessions}."
|
|
243
|
+
NOT_TOLD = "The leader was not told: {why}."
|
|
244
|
+
REMOVED = (
|
|
245
|
+
"you are no longer on this team, so nothing was changed.\n"
|
|
246
|
+
"Ask your user what to do; your transcript is intact."
|
|
247
|
+
)
|
|
248
|
+
CALLER_GONE = (
|
|
249
|
+
"the session this command runs under ended while it waited for the lock,"
|
|
250
|
+
" so nothing was changed."
|
|
251
|
+
)
|
|
252
|
+
INSIDE = "you are already inside this team."
|
|
253
|
+
NAMED_INSIDE = (
|
|
254
|
+
"-r {name} alone starts or attaches a team, which is done from a shell outside any team.\n"
|
|
255
|
+
"Run workcrew -r {name} there, or give a command after the name: workcrew -r {name} board."
|
|
256
|
+
)
|
|
257
|
+
NOT_YOUR_PANE = "_exec starts the session of the pane it runs in, and this is not that pane."
|
|
258
|
+
|
|
259
|
+
LIST = "Run workcrew --help for the list."
|
|
260
|
+
BOARD_VERBS = ("new", "close", "back", "take", "report")
|
|
261
|
+
LINK_VERBS = ("add", "remove", "send", "read")
|
|
262
|
+
# Each link verb's own arguments: the fewest it means anything with, the most it can use, and
|
|
263
|
+
# the one line a reader sees when the count is wrong. None: the message is the rest of the line.
|
|
264
|
+
LINK_ARITY: dict[str, tuple[int, int | None, str]] = {
|
|
265
|
+
"add": (4, 4, link.USAGE),
|
|
266
|
+
"remove": (1, 1, "link remove takes one team, e.g. workcrew link remove bob."),
|
|
267
|
+
"send": (
|
|
268
|
+
2,
|
|
269
|
+
None,
|
|
270
|
+
"link send needs a team and a message, e.g. workcrew link send bob the parser is ready.",
|
|
271
|
+
),
|
|
272
|
+
"read": (0, 0, "link read takes nothing: it reads what is addressed to you."),
|
|
273
|
+
}
|
|
274
|
+
SIDES = ("left", "right", "middle")
|
|
275
|
+
LOOP = ("-l", "--loop")
|
|
276
|
+
# What a number a reader typed may look like, and nothing else: `str.isdigit` is true of the
|
|
277
|
+
# digits of every script and of numbers too long for int(). Nine digits is the shape a task's
|
|
278
|
+
# id already has. A count of none is a team of the leader alone, so only --workers takes a 0.
|
|
279
|
+
COUNT = re.compile("[0-9]{1,9}")
|
|
280
|
+
AT_LEAST_ONE = re.compile("[1-9][0-9]{0,8}")
|
|
281
|
+
WORKERS = "--workers takes a number of workers, e.g. --workers 3."
|
|
282
|
+
OUR_OPTIONS = ("--no-cap", "--workers", "--name") # the options of ours a tail may hold
|
|
283
|
+
NO_MEMBER = (
|
|
284
|
+
"resume's tail starts the members it names, and this line names none.\n"
|
|
285
|
+
"The flags every member starts with go with the bare workcrew command."
|
|
286
|
+
)
|
|
287
|
+
LEADER_FLAGS = (
|
|
288
|
+
"the leader's own flags are its owner's to set: a leader that restarts itself ends the turn"
|
|
289
|
+
" that ran the command.\nAsk your user to run it."
|
|
290
|
+
)
|
|
291
|
+
# Measured on 2.1.332: this beats --effort, so a member would never run at the effort it names.
|
|
292
|
+
EFFORT_ENV = "CLAUDE_CODE_EFFORT_LEVEL"
|
|
293
|
+
EFFORT_IN_ENV = (
|
|
294
|
+
f"the team's environment sets {EFFORT_ENV}, which Claude Code reads before --effort, so no"
|
|
295
|
+
" member would run at the effort you give here.\n"
|
|
296
|
+
f"Unset {EFFORT_ENV} where you start teams, then workcrew exit and bring the team back."
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
NO_TEAM_YET = "No team on this host yet. To start one, run workcrew in your project's folder.\n"
|
|
300
|
+
# Words a reader reaches for that the tool does not have, and the one line each gets.
|
|
301
|
+
HINTS = {
|
|
302
|
+
"-n": "there is no {w}. To name a new team, in a shell outside any team: workcrew --name"
|
|
303
|
+
" web. To rename a team: workcrew rename web.",
|
|
304
|
+
"-c": "there is no {w} — a team comes back with its sessions from a shell outside any team:"
|
|
305
|
+
" workcrew -r NAME. workcrew -r shows your teams.",
|
|
306
|
+
"--continue": "there is no {w} — a team comes back with its sessions from a shell outside"
|
|
307
|
+
" any team: workcrew -r NAME. workcrew -r shows your teams.",
|
|
308
|
+
"focus": "there is no {w} — click the pane, or use tmux's own keys.",
|
|
309
|
+
"jump": "there is no {w} — click the pane, or use tmux's own keys.",
|
|
310
|
+
"/resume": "there is no {w} — to start members that exited: workcrew resume.",
|
|
311
|
+
"restart": "there is no {w} — to start members that exited: workcrew resume.",
|
|
312
|
+
"status": "there is no {w} — the board shows every member and what it holds:"
|
|
313
|
+
" run workcrew board.",
|
|
314
|
+
"reset": "there is no {w} — to stop the team and forget it, run workcrew exit --forget.",
|
|
315
|
+
}
|
|
316
|
+
# name: the word, "" for the bare command. The owner alone may run the first group.
|
|
317
|
+
OWNER_ONLY = ("", "-r", "rename", "exit", "link add", "link remove")
|
|
318
|
+
LEADER_OR_OWNER = ("add", "layout", "remove", "resume", "rename MEMBER")
|
|
319
|
+
# Every command a member may run at all. The panes' own commands are here because a member's
|
|
320
|
+
# pane runs them; the owner's command line, the hooks' reconcile and the status line are not,
|
|
321
|
+
# since a member running them would act as the owner.
|
|
322
|
+
MEMBERS_MAY = frozenset(
|
|
323
|
+
{
|
|
324
|
+
"_exec",
|
|
325
|
+
"_hello",
|
|
326
|
+
"_idle",
|
|
327
|
+
"_linkwatch",
|
|
328
|
+
"add",
|
|
329
|
+
"board",
|
|
330
|
+
"layout",
|
|
331
|
+
"link",
|
|
332
|
+
"remove",
|
|
333
|
+
"rename",
|
|
334
|
+
"resume",
|
|
335
|
+
"send",
|
|
336
|
+
"--help",
|
|
337
|
+
"--version",
|
|
338
|
+
}
|
|
339
|
+
)
|
|
340
|
+
COMMANDS = (
|
|
341
|
+
"add",
|
|
342
|
+
"remove",
|
|
343
|
+
"send",
|
|
344
|
+
"layout",
|
|
345
|
+
"board",
|
|
346
|
+
"rename",
|
|
347
|
+
"resume",
|
|
348
|
+
"exit",
|
|
349
|
+
"link",
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
@dataclass(frozen=True)
|
|
354
|
+
class Result:
|
|
355
|
+
out: str
|
|
356
|
+
err: str
|
|
357
|
+
code: int
|
|
358
|
+
attach: Server | None = None # the team to become a client of, once the result is printed
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
@dataclass(frozen=True)
|
|
362
|
+
class Caller:
|
|
363
|
+
"""Who is running this command, found once before anything is dispatched."""
|
|
364
|
+
|
|
365
|
+
key: str | None # the team whose server the command runs inside, if any
|
|
366
|
+
id: str # a member id, or "owner"
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
@dataclass(frozen=True)
|
|
370
|
+
class Ctx:
|
|
371
|
+
"""What a command that acts on a team is given. The roster is not among them: a command
|
|
372
|
+
reads it under the lock, through `_held`, and nothing read before the lock is used after."""
|
|
373
|
+
|
|
374
|
+
call: Call
|
|
375
|
+
d: Path
|
|
376
|
+
server: Server
|
|
377
|
+
caller: str
|
|
378
|
+
env: Mapping[str, str]
|
|
379
|
+
typed: bool
|
|
380
|
+
here: str | None # the team whose pane the caller is in, if any; not always the one acted on
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
@dataclass(frozen=True)
|
|
384
|
+
class Held:
|
|
385
|
+
"""What `_held` read under the lock."""
|
|
386
|
+
|
|
387
|
+
r: Roster
|
|
388
|
+
view: ServerView | None # None when the team's server is not running
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
@dataclass(frozen=True)
|
|
392
|
+
class Call:
|
|
393
|
+
name: str # the command word; "" is the bare workcrew
|
|
394
|
+
args: tuple[str, ...] = ()
|
|
395
|
+
flags: tuple[str, ...] | None = None # the tail after "--"; None: no "--" was given
|
|
396
|
+
workers: int | None = None
|
|
397
|
+
cap: bool = True
|
|
398
|
+
# The name or key after `-r`. None: no -r was given, so the caller's own team. An empty
|
|
399
|
+
# string is never "no name": `-r ''` once let a member's `link send` past its -r check.
|
|
400
|
+
team: str | None = None
|
|
401
|
+
new: str | None = None # the name `--name` gives a team being made
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def parse(argv: Sequence[str]) -> Call:
|
|
405
|
+
"""One command line to a call. Raises UsageError (exit 2) or Refused (exit 1)."""
|
|
406
|
+
named = _named_line(argv)
|
|
407
|
+
if named is not None:
|
|
408
|
+
return named
|
|
409
|
+
head = list(argv)
|
|
410
|
+
workers, cap, seen, made = None, True, "", None
|
|
411
|
+
rest: list[str] = []
|
|
412
|
+
i = 0
|
|
413
|
+
while i < len(head):
|
|
414
|
+
tok = head[i]
|
|
415
|
+
if tok == "--" and (not rest or rest[0] == "-r"):
|
|
416
|
+
# The tail is Claude Code's, so nothing in it is taken as ours here. A word in it
|
|
417
|
+
# that is one of ours is refused below instead, even where Claude Code would have
|
|
418
|
+
# read it as another flag's value.
|
|
419
|
+
rest.extend(head[i:])
|
|
420
|
+
break
|
|
421
|
+
if tok == "--no-cap":
|
|
422
|
+
cap, seen = False, tok
|
|
423
|
+
elif tok == "--workers":
|
|
424
|
+
seen, i = tok, i + 1
|
|
425
|
+
workers = _number(head[i] if i < len(head) else "", COUNT, WORKERS)
|
|
426
|
+
elif tok == "--name":
|
|
427
|
+
seen, i = tok, i + 1
|
|
428
|
+
made = head[i] if i < len(head) else ""
|
|
429
|
+
if not made:
|
|
430
|
+
raise UsageError("--name needs a team's name: workcrew --name NAME.")
|
|
431
|
+
elif not rest or rest[0] == "-r": # past the command word every token is its argument
|
|
432
|
+
rest.append(tok)
|
|
433
|
+
else:
|
|
434
|
+
rest.extend(head[i:])
|
|
435
|
+
break
|
|
436
|
+
i += 1
|
|
437
|
+
flags: tuple[str, ...] | None = None
|
|
438
|
+
# Only the bare command and `-r` take a tail of Claude Code flags. After any other command
|
|
439
|
+
# word `--` is one of its arguments, so a message or a title may hold one. The options
|
|
440
|
+
# above are read wherever they fall, so a misplaced one is named rather than sent as text.
|
|
441
|
+
if (not rest or rest[0] in ("--", "-r", "resume")) and "--" in rest:
|
|
442
|
+
cut = rest.index("--")
|
|
443
|
+
rest, flags = rest[:cut], tuple(rest[cut + 1 :])
|
|
444
|
+
name, args = _command(rest)
|
|
445
|
+
_check_options(name, args, workers, seen, made, flags)
|
|
446
|
+
return Call(name, args, flags, workers, cap, new=made)
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def _check_options(
|
|
450
|
+
name: str,
|
|
451
|
+
args: tuple[str, ...],
|
|
452
|
+
workers: int | None,
|
|
453
|
+
seen: str,
|
|
454
|
+
made: str | None,
|
|
455
|
+
flags: tuple[str, ...] | None,
|
|
456
|
+
) -> None:
|
|
457
|
+
"""The tool's own options, wherever in the line they fell."""
|
|
458
|
+
if workers is not None and name != "":
|
|
459
|
+
raise UsageError(LATE_WORKERS)
|
|
460
|
+
# One starts a team here, the other names one that exists. `seen` holds the last option
|
|
461
|
+
# read, so `--name` is asked for by itself.
|
|
462
|
+
if made is not None and name == "-r":
|
|
463
|
+
raise UsageError("'--name' goes with the bare workcrew command, not with '-r'.")
|
|
464
|
+
if seen and name not in ("", "-r"):
|
|
465
|
+
raise UsageError(f"{seen!r} goes with the bare workcrew command, not with {name!r}.")
|
|
466
|
+
# A member's tail is the two values it is started with, so no option of ours can fall in it:
|
|
467
|
+
# the allowlist below names every word it takes.
|
|
468
|
+
if name == "resume" and flags is not None:
|
|
469
|
+
if not args:
|
|
470
|
+
raise UsageError(NO_MEMBER)
|
|
471
|
+
return
|
|
472
|
+
# Claude Code reads `--name=value` as the flag `--name`, which is how its gate reads one
|
|
473
|
+
# too, so ours written that way is still ours. The answer names the option rather than the
|
|
474
|
+
# word as typed: the option is what goes before the `--`, and `--no-cap` takes no value.
|
|
475
|
+
ours = next((n for f in flags or () if (n := f.split("=", 1)[0]) in OUR_OPTIONS), None)
|
|
476
|
+
if ours == "--workers" and name == "-r":
|
|
477
|
+
# Putting it before the -- is what the refusal below would say, and `-r` names a team
|
|
478
|
+
# that is already made, so following it would only be refused again.
|
|
479
|
+
raise UsageError(LATE_WORKERS)
|
|
480
|
+
if ours is not None:
|
|
481
|
+
raise UsageError(
|
|
482
|
+
f"{ours!r} is workcrew's own option; put it before the -- that starts the tail."
|
|
483
|
+
)
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
def _named_line(argv: Sequence[str]) -> Call | None:
|
|
487
|
+
"""`-r NAME COMMAND…`, or None for any other line. Only a command word ends the name: before
|
|
488
|
+
an option the line is the -r that brings a team back, and an option there once started a
|
|
489
|
+
new team in the current folder. A word that begins with `-` is an option, not a name."""
|
|
490
|
+
if not names_a_team(argv):
|
|
491
|
+
return None
|
|
492
|
+
if not argv[1]: # a script's unset variable, which would otherwise mean no name at all
|
|
493
|
+
raise UsageError("-r needs a team's name: workcrew -r NAME COMMAND.")
|
|
494
|
+
if argv[2:3] == ["-r"]:
|
|
495
|
+
raise UsageError("-r is given once: workcrew -r NAME COMMAND.")
|
|
496
|
+
if argv[2:3] and argv[2] in COMMANDS:
|
|
497
|
+
return replace(parse(argv[2:]), team=argv[1])
|
|
498
|
+
return None
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def select(word: str, r: Roster) -> list[Member]:
|
|
502
|
+
"""The members one recipient word picks, in roster order."""
|
|
503
|
+
listing = ", ".join(m.name for m in r.members)
|
|
504
|
+
parts = word.split(",")
|
|
505
|
+
if "" in parts:
|
|
506
|
+
raise Refused(
|
|
507
|
+
f"{word!r} has an empty pattern."
|
|
508
|
+
" Join patterns with commas and no spaces, like leader,w3."
|
|
509
|
+
)
|
|
510
|
+
chosen: set[str] = set()
|
|
511
|
+
for part in parts:
|
|
512
|
+
hit = [m for m in r.members if _matches(part, m)]
|
|
513
|
+
if not hit and not r.workers() and (part == "workers" or _glob(part)):
|
|
514
|
+
raise Refused(
|
|
515
|
+
f"the team has no workers, so {part!r} matches nobody.\n"
|
|
516
|
+
"Add some with workcrew add 2, or send to leader."
|
|
517
|
+
)
|
|
518
|
+
if not hit:
|
|
519
|
+
raise Refused(
|
|
520
|
+
f"no member matches {part!r}. Members: {listing}.\nRun workcrew board to see them."
|
|
521
|
+
)
|
|
522
|
+
chosen |= {m.id for m in hit}
|
|
523
|
+
return [m for m in r.members if m.id in chosen]
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def members(words: Sequence[str], r: Roster, command: str, owner: bool = False) -> list[Member]:
|
|
527
|
+
"""The members `remove` or `resume` names: those two take names, not patterns."""
|
|
528
|
+
out = []
|
|
529
|
+
for word in words:
|
|
530
|
+
found = r.by_name(word)
|
|
531
|
+
if found is None and command == "resume" and owner and not _glob(word):
|
|
532
|
+
raise Refused(
|
|
533
|
+
f"no member {word}; to resume team {word}, in a shell outside any team:"
|
|
534
|
+
f" workcrew -r {word}."
|
|
535
|
+
)
|
|
536
|
+
if found is None:
|
|
537
|
+
tail = (
|
|
538
|
+
f"\nPatterns pick who receives a send; {command} takes names."
|
|
539
|
+
if _glob(word)
|
|
540
|
+
else ""
|
|
541
|
+
)
|
|
542
|
+
listing = ", ".join(m.name for m in r.members)
|
|
543
|
+
raise Refused(f"no member {word!r}. Members: {listing}.{tail}")
|
|
544
|
+
if found not in out: # `remove w1 w1` named one member, and removing it twice raises
|
|
545
|
+
out.append(found)
|
|
546
|
+
return out
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def rename(args: Sequence[str], r: Roster) -> tuple[Member | None, str]:
|
|
550
|
+
"""The worker to rename and its new name; None: the team's own name."""
|
|
551
|
+
if len(args) == 1:
|
|
552
|
+
found = r.by_name(args[0])
|
|
553
|
+
if found is not None and found.id == "leader":
|
|
554
|
+
raise Refused("the leader is not renamed.")
|
|
555
|
+
if found is not None:
|
|
556
|
+
raise UsageError(
|
|
557
|
+
f"{found.name} is a member; to rename it: workcrew rename {found.name} NEW"
|
|
558
|
+
)
|
|
559
|
+
return None, args[0]
|
|
560
|
+
who = members(args[:1], r, "rename")[0]
|
|
561
|
+
if who.id == "leader":
|
|
562
|
+
raise Refused("the leader is not renamed.")
|
|
563
|
+
return who, args[1]
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
def check_caller(call: Call, caller: str, r: Roster) -> None:
|
|
567
|
+
"""Mechanism 8: the owner's commands, the leader's and everyone's. me is None: the owner."""
|
|
568
|
+
me = None
|
|
569
|
+
if caller != "owner":
|
|
570
|
+
me = r.by_id(caller)
|
|
571
|
+
if me is None:
|
|
572
|
+
raise Refused(REMOVED)
|
|
573
|
+
if call.name == "board" and call.args[:1] and call.args[0] in BOARD_VERBS:
|
|
574
|
+
if me is None:
|
|
575
|
+
raise Refused("Tasks are the leader's to create: tell the leader what you want.")
|
|
576
|
+
return
|
|
577
|
+
typed = f"{call.name} {call.args[0]}" if call.name == "link" and call.args else call.name
|
|
578
|
+
shown = repr(typed) if typed else "the bare workcrew"
|
|
579
|
+
group = _group(call)
|
|
580
|
+
if group in OWNER_ONLY and me is not None:
|
|
581
|
+
raise Refused(f"{shown} is the owner's, and you are {me.name}.\nAsk your user to run it.")
|
|
582
|
+
if group in LEADER_OR_OWNER and me is not None and me.id != "leader":
|
|
583
|
+
raise Refused(
|
|
584
|
+
f"{shown} is the leader's or the owner's, and you are {me.name}.\n"
|
|
585
|
+
"Tell the leader what you need: workcrew send leader <what you need>."
|
|
586
|
+
)
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def _group(call: Call) -> str:
|
|
590
|
+
"""Which group a command belongs to. The two renames share a word, so the form decides:
|
|
591
|
+
`rename NEW` names the team and is the owner's, `rename MEMBER NEW` names a worker and the
|
|
592
|
+
leader may do it too."""
|
|
593
|
+
if call.name == "link" and call.args:
|
|
594
|
+
return f"link {call.args[0]}"
|
|
595
|
+
if call.name == "rename" and len(call.args) == 2:
|
|
596
|
+
return "rename MEMBER"
|
|
597
|
+
return call.name
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def parse_line(line: str) -> list[str]:
|
|
601
|
+
"""One typed line to argv. A message keeps its bytes; every other argument is one word."""
|
|
602
|
+
line = prompt.command(line)
|
|
603
|
+
words = line.split()
|
|
604
|
+
if not words:
|
|
605
|
+
return []
|
|
606
|
+
if words[0] in ("?", "help"):
|
|
607
|
+
return ["--help"]
|
|
608
|
+
# After `-r NAME` the command's own rules hold, as they do at the start of the line.
|
|
609
|
+
head = words[:2] if names_a_team(words) and len(words) > 2 else []
|
|
610
|
+
rest, words = (line.split(maxsplit=2)[2], words[2:]) if head else (line, words)
|
|
611
|
+
if words[0] == "send" and len(words) > 2:
|
|
612
|
+
return [*head, *rest.split(maxsplit=2)]
|
|
613
|
+
if words[0] == "link" and words[1:2] == ["send"] and len(words) > 3:
|
|
614
|
+
return [*head, *rest.split(maxsplit=3)]
|
|
615
|
+
return [*head, *words]
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
# A line printed for the reader to type is the line that works where the reader is: outside the
|
|
619
|
+
# team it acts on, a command names the team. A text that gives another person a command must not
|
|
620
|
+
# spell it `workcrew COMMAND`, or it would name this team.
|
|
621
|
+
_RUN = re.compile(r"(?<!-r )\bworkcrew (?=(?:" + "|".join(COMMANDS) + r")\b)")
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
def _naming(text: str, team: str | None) -> str:
|
|
625
|
+
"""Each `workcrew COMMAND` in `text` as `workcrew -r TEAM COMMAND`; None: as it is."""
|
|
626
|
+
if team is None:
|
|
627
|
+
return text
|
|
628
|
+
return _RUN.sub(lambda _: f"workcrew -r {shlex.quote(team)} ", text)
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
def _cmd(team: str | None, line: str) -> str:
|
|
632
|
+
return f"workcrew {line}" if team is None else f"workcrew -r {shlex.quote(team)} {line}"
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
def _named_by(c: Ctx, r: Roster) -> str | None:
|
|
636
|
+
"""The name a result's lines give the team: its name when the line named it, else none."""
|
|
637
|
+
return r.name if c.call.team is not None else None
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def _back_line(c: Ctx, r: Roster, ends: bool = False) -> str:
|
|
641
|
+
"""The line that brings team `r` back, for where the reader is: a shell outside any team
|
|
642
|
+
attaches it; inside a team `-r NAME` alone is refused, so there it is `resume`. `ends`: the
|
|
643
|
+
caller's own team is ending, which takes the reader out of it."""
|
|
644
|
+
if c.here is None or (ends and c.here == r.key):
|
|
645
|
+
return f"workcrew -r {shlex.quote(r.name)}"
|
|
646
|
+
return _cmd(_named_by(c, r), "resume")
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
def _outsider(argv: Sequence[str], who: Caller | None) -> str | None:
|
|
650
|
+
"""The team a refusal's lines name: the one the line named, or NAME for an owner in no team
|
|
651
|
+
who named none; None for a caller in a pane of the team, and for a member, whose `-r` is
|
|
652
|
+
refused."""
|
|
653
|
+
if who is None or who.id != "owner":
|
|
654
|
+
return None
|
|
655
|
+
# The shared test, and one condition of its own: an empty name names no team to print.
|
|
656
|
+
if names_a_team(argv) and argv[1]:
|
|
657
|
+
return argv[1]
|
|
658
|
+
return "NAME" if who.key is None else None
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
class _Worded(Refused):
|
|
662
|
+
"""A refusal whose lines are already worded for where the reader is, and which holds the
|
|
663
|
+
reader's own words: no line in it is named again."""
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
def refusal(exc: Refused | UsageError | StateError, typed: bool, team: str | None = None) -> Result:
|
|
667
|
+
"""The lines a refusal prints: the command line's, or the owner's typed line's."""
|
|
668
|
+
text = str(exc) if isinstance(exc, _Worded) else _naming(str(exc), team)
|
|
669
|
+
if typed:
|
|
670
|
+
return Result("", " ".join(text.splitlines()) + "\n", _code(exc))
|
|
671
|
+
first, _, rest = text.partition("\n")
|
|
672
|
+
return Result("", f"workcrew: {first}\n" + (f"{rest}\n" if rest else ""), _code(exc))
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
def _code(exc: Refused | UsageError | StateError) -> int:
|
|
676
|
+
return 1 if isinstance(exc, Refused) else 2
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
def _glob(word: str) -> bool:
|
|
680
|
+
return any(c in word for c in "*?[,")
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
def _matches(part: str, m: Member) -> bool:
|
|
684
|
+
if part == "all":
|
|
685
|
+
return True
|
|
686
|
+
if part == "workers":
|
|
687
|
+
return m.id != "leader"
|
|
688
|
+
return fnmatch.fnmatchcase(m.name, part)
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
def _number(word: str, shape: re.Pattern[str], line: str) -> int:
|
|
692
|
+
if shape.fullmatch(word) is None:
|
|
693
|
+
raise UsageError(line)
|
|
694
|
+
return int(word)
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
def _command(head: Sequence[str]) -> tuple[str, tuple[str, ...]]:
|
|
698
|
+
if not head:
|
|
699
|
+
return "", ()
|
|
700
|
+
word, args = head[0], tuple(head[1:])
|
|
701
|
+
if word in ("--help", "-h", "help", "?"):
|
|
702
|
+
return "--help", ()
|
|
703
|
+
if word in ("--version", "-v"):
|
|
704
|
+
return "--version", ()
|
|
705
|
+
if word == "-r":
|
|
706
|
+
if len(args) > 1:
|
|
707
|
+
raise UsageError("-r takes one team name, or nothing.")
|
|
708
|
+
return "-r", args
|
|
709
|
+
if word in HINTS:
|
|
710
|
+
raise UsageError(HINTS[word].format(w=repr(word)))
|
|
711
|
+
if word not in COMMANDS:
|
|
712
|
+
near = difflib.get_close_matches(word, COMMANDS, 1)
|
|
713
|
+
guess = f" Did you mean {near[0]!r}?" if near else ""
|
|
714
|
+
raise UsageError(f"{word!r} is not a command.{guess} {LIST}")
|
|
715
|
+
return _arguments(word, args)
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
def _usage(word: str) -> str:
|
|
719
|
+
"""A command's own lines of the help, or board's and link's own help, which lists the
|
|
720
|
+
verbs."""
|
|
721
|
+
if word in ("board", "link"):
|
|
722
|
+
return BOARD_HELP if word == "board" else LINK_HELP
|
|
723
|
+
block = HELP.split("\nCOMMANDS\n", 1)[1].split("\n\n", 1)[0]
|
|
724
|
+
mine = [line for line in block.splitlines() if line.split()[0] == word]
|
|
725
|
+
return "\n".join([*mine, LIST]) + "\n"
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
def _arguments(word: str, args: tuple[str, ...]) -> tuple[str, tuple[str, ...]]:
|
|
729
|
+
# Help asked right after the command word or one of its verbs, where no title, message or
|
|
730
|
+
# operand has begun: a leader asking `board new --help` once made a task of it.
|
|
731
|
+
verbs = {"board": BOARD_VERBS, "link": LINK_VERBS}.get(word, ())
|
|
732
|
+
at = 1 if args[:1] and args[0] in verbs else 0
|
|
733
|
+
if args[at : at + 1] in (("--help",), ("-h",)):
|
|
734
|
+
return word, ("--help",)
|
|
735
|
+
if word in RULES:
|
|
736
|
+
ok, line = RULES[word]
|
|
737
|
+
if not ok(args):
|
|
738
|
+
raise UsageError(line)
|
|
739
|
+
return word, RESHAPE[word](args) if word in RESHAPE else args
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
def _board(args: tuple[str, ...]) -> tuple[str, ...]:
|
|
743
|
+
if not args or args[0] in BOARD_VERBS:
|
|
744
|
+
return args
|
|
745
|
+
if args[0] == "assign":
|
|
746
|
+
raise UsageError(board.NO_ASSIGN)
|
|
747
|
+
if args[0] in LOOP and len(args) <= 2:
|
|
748
|
+
sec = args[1] if len(args) > 1 else "5"
|
|
749
|
+
_number(
|
|
750
|
+
sec, AT_LEAST_ONE, "--loop takes a number of seconds, e.g. workcrew board --loop 5."
|
|
751
|
+
)
|
|
752
|
+
return ("--loop", sec)
|
|
753
|
+
raise UsageError(
|
|
754
|
+
f"board takes nothing, --loop [SEC], or one of {', '.join(BOARD_VERBS)}."
|
|
755
|
+
" Run workcrew board --help for the verbs."
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
def _rename(args: tuple[str, ...]) -> tuple[str, ...]:
|
|
760
|
+
if len(args) not in (1, 2):
|
|
761
|
+
raise UsageError(
|
|
762
|
+
"rename takes the team's new name, or a worker and its new name:"
|
|
763
|
+
" workcrew rename w2 parser."
|
|
764
|
+
)
|
|
765
|
+
return args
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
def _link(args: tuple[str, ...]) -> tuple[str, ...]:
|
|
769
|
+
if not args:
|
|
770
|
+
return args
|
|
771
|
+
if args[0] not in LINK_ARITY:
|
|
772
|
+
raise UsageError(
|
|
773
|
+
f"link takes {', '.join(LINK_VERBS[:-1])} or {LINK_VERBS[-1]}, not {args[0]!r}."
|
|
774
|
+
)
|
|
775
|
+
least, most, line = LINK_ARITY[args[0]]
|
|
776
|
+
after = len(args) - 1
|
|
777
|
+
if after < least or (most is not None and after > most):
|
|
778
|
+
raise UsageError(line)
|
|
779
|
+
if args[0] == "send":
|
|
780
|
+
return (args[0], args[1], " ".join(args[2:]))
|
|
781
|
+
return args
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
def _send(args: tuple[str, ...]) -> tuple[str, ...]:
|
|
785
|
+
if not " ".join(args[1:]).strip():
|
|
786
|
+
raise UsageError(
|
|
787
|
+
"send needs a message after the recipients.\n"
|
|
788
|
+
"Write it on the same line: workcrew send workers fix the tests"
|
|
789
|
+
)
|
|
790
|
+
return (args[0], " ".join(args[1:]))
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
# the commands whose arguments are only checked, and the ones reshaped into (verb, …)
|
|
794
|
+
RULES: dict[str, tuple[Callable[[tuple[str, ...]], bool], str]] = {
|
|
795
|
+
"add": (
|
|
796
|
+
lambda a: len(a) <= 1 and (not a or AT_LEAST_ONE.fullmatch(a[0]) is not None),
|
|
797
|
+
"add takes a number of workers, e.g. workcrew add 2.",
|
|
798
|
+
),
|
|
799
|
+
"remove": (
|
|
800
|
+
bool,
|
|
801
|
+
"remove takes the names of the workers to remove, e.g. workcrew remove w3.",
|
|
802
|
+
),
|
|
803
|
+
"layout": (lambda a: len(a) == 1 and a[0] in SIDES, "workcrew layout left|right|middle"),
|
|
804
|
+
"exit": (lambda a: a in ((), ("--forget",)), "exit takes nothing, or --forget."),
|
|
805
|
+
}
|
|
806
|
+
RESHAPE: dict[str, Callable[[tuple[str, ...]], tuple[str, ...]]] = {
|
|
807
|
+
"send": _send,
|
|
808
|
+
"board": _board,
|
|
809
|
+
"rename": _rename,
|
|
810
|
+
"link": _link,
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
def run(argv: Sequence[str], env: Mapping[str, str], typed: bool = False) -> Result:
|
|
815
|
+
"""One command line: what it prints, and what it exits with."""
|
|
816
|
+
r = _answer(argv, env, typed)
|
|
817
|
+
# Every result leaves here, for a shell or the command line's popup, and holds text a peer,
|
|
818
|
+
# a member or the file system wrote: a key's type, a folder's name. In the popup tmux passed
|
|
819
|
+
# a title or clipboard sequence on to the owner's terminal (3.2a, 3.8).
|
|
820
|
+
return replace(r, out=escape(r.out), err=escape(r.err))
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
def _answer(argv: Sequence[str], env: Mapping[str, str], typed: bool) -> Result:
|
|
824
|
+
who: Caller | None = None
|
|
825
|
+
try:
|
|
826
|
+
# Who is calling is settled here, once, for every command. No path below may reach a
|
|
827
|
+
# change without it: `-r` once did, and let a worker rewrite the team's flags.
|
|
828
|
+
try:
|
|
829
|
+
who = Caller(*whoami(env))
|
|
830
|
+
except Refused:
|
|
831
|
+
# In Claude Code's sandbox the team's server cannot be reached, which looks like a
|
|
832
|
+
# pane that is gone; the state tells the two apart. The team is the one `$TMUX`
|
|
833
|
+
# names, since only a caller with it is refused here; a folder that is no team's,
|
|
834
|
+
# by its mode or its roster, is not looked into.
|
|
835
|
+
d = team_dir(cast(str, socket_team(env)), env)
|
|
836
|
+
with contextlib.suppress(StateError):
|
|
837
|
+
if load(d) is not None:
|
|
838
|
+
_outside_the_sandbox(d, argv)
|
|
839
|
+
raise
|
|
840
|
+
if argv and argv[0] in HIDDEN:
|
|
841
|
+
_may(who, argv[0], env)
|
|
842
|
+
if who.key is None:
|
|
843
|
+
raise _outside(argv, env)
|
|
844
|
+
return HIDDEN[argv[0]](list(argv[1:]), env, who)
|
|
845
|
+
call = parse(argv)
|
|
846
|
+
if call.team is not None and who.id != "owner":
|
|
847
|
+
# A member's `-r OWN link send` was a spelling the members' ask rule for
|
|
848
|
+
# `link send` does not match.
|
|
849
|
+
raise Refused(
|
|
850
|
+
"-r names a team, and only the owner names one; your commands act on your own team."
|
|
851
|
+
)
|
|
852
|
+
if call.args[:1] != ("--help",): # usage changes nothing, whoever asks for it
|
|
853
|
+
_may(who, call.name, env)
|
|
854
|
+
r = _act(call, argv, env, typed, who)
|
|
855
|
+
return replace(r, out=_ends(r.out), err=_ends(r.err))
|
|
856
|
+
except (Refused, UsageError, StateError) as exc:
|
|
857
|
+
return refusal(exc, typed, _outsider(argv, who))
|
|
858
|
+
except OSError as exc:
|
|
859
|
+
if exc.errno not in _READ_ONLY:
|
|
860
|
+
raise
|
|
861
|
+
return refusal(StateError(NO_WRITE), typed)
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
def typed_run(line: str, env: Mapping[str, str], cwd: str) -> str:
|
|
865
|
+
"""One line from the owner's command line, which has no pane of its own: an `exit` of its
|
|
866
|
+
own team is detached, because it ends the server the popup running it is on."""
|
|
867
|
+
argv = parse_line(line)
|
|
868
|
+
if _own_exit(argv, env):
|
|
869
|
+
subprocess.Popen(
|
|
870
|
+
[env["WORKCREW_BIN"], *argv],
|
|
871
|
+
cwd=cwd,
|
|
872
|
+
env=dict(env),
|
|
873
|
+
start_new_session=True,
|
|
874
|
+
# No terminal of the command line's, on any of the three: this child outlives the
|
|
875
|
+
# process that leads that terminal's session, and a BSD kernel revokes a terminal
|
|
876
|
+
# for every process still holding it when its leader ends.
|
|
877
|
+
stdin=subprocess.DEVNULL,
|
|
878
|
+
stdout=subprocess.DEVNULL,
|
|
879
|
+
stderr=subprocess.DEVNULL,
|
|
880
|
+
)
|
|
881
|
+
return ""
|
|
882
|
+
answer = run(argv, env, typed=True)
|
|
883
|
+
return answer.out or answer.err
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
def _own_exit(argv: Sequence[str], env: Mapping[str, str]) -> bool:
|
|
887
|
+
"""An `exit` of the command line's own team, however the line names it. One that asks for
|
|
888
|
+
help stops nothing, and its usage is for the command line to show."""
|
|
889
|
+
try:
|
|
890
|
+
call = parse(argv)
|
|
891
|
+
return (
|
|
892
|
+
call.name == "exit"
|
|
893
|
+
and call.args[:1] != ("--help",)
|
|
894
|
+
and (call.team is None or _named(call.team, env).key == socket_team(env))
|
|
895
|
+
)
|
|
896
|
+
except (Refused, UsageError): # it runs where it was typed, and says so
|
|
897
|
+
return False
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
def _folder(env: Mapping[str, str], path: Path) -> str:
|
|
901
|
+
"""A folder as the owner knows it. A shell's PWD keeps the name they typed, while the
|
|
902
|
+
tool resolves every link to make the team's key; on macOS /tmp and /var are links, so the
|
|
903
|
+
two differ for most of the paths a team is started in."""
|
|
904
|
+
told = env.get("PWD", "")
|
|
905
|
+
# Only for the folder the owner is standing in: a PWD left over from another directory
|
|
906
|
+
# would put their spelling on a folder it does not name.
|
|
907
|
+
if told and os.path.realpath(told) == os.path.realpath(path) == str(Path.cwd()):
|
|
908
|
+
return shown(Path(told))
|
|
909
|
+
return shown(path)
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
def _may(who: Caller, name: str, env: Mapping[str, str]) -> None:
|
|
913
|
+
"""A member runs only what this list allows. A command missing from it is the owner's,
|
|
914
|
+
whatever else the rules say: the list is what keeps a new command from arriving open."""
|
|
915
|
+
if who.id == "owner" or name in MEMBERS_MAY:
|
|
916
|
+
return
|
|
917
|
+
shown = repr(name) if name else "the bare workcrew"
|
|
918
|
+
raise Refused(
|
|
919
|
+
f"{shown} is the owner's, and you are {_known_as(who, env)}.\nAsk your user to run it."
|
|
920
|
+
)
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
def _known_as(who: Caller, env: Mapping[str, str]) -> str:
|
|
924
|
+
"""The caller's name for a refusal, falling back to its id when the roster is unreadable."""
|
|
925
|
+
if who.key is not None:
|
|
926
|
+
with contextlib.suppress(StateError):
|
|
927
|
+
r = load(team_dir(who.key, env))
|
|
928
|
+
m = r.by_id(who.id) if r is not None else None
|
|
929
|
+
if m is not None:
|
|
930
|
+
return m.name
|
|
931
|
+
return who.id
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
def _act(
|
|
935
|
+
call: Call, argv: Sequence[str], env: Mapping[str, str], typed: bool, who: Caller
|
|
936
|
+
) -> Result:
|
|
937
|
+
if call.name == "--help":
|
|
938
|
+
return Result(TYPED_HELP if typed else HELP, "", 0)
|
|
939
|
+
if call.name == "--version":
|
|
940
|
+
# The version alone: it is what a script compares, and the reader just typed the name.
|
|
941
|
+
return Result(f"{metadata.version('workcrew')}\n", "", 0)
|
|
942
|
+
if call.args[:1] == ("--help",):
|
|
943
|
+
return Result(_usage(call.name), "", 0)
|
|
944
|
+
if call.name in ALONE:
|
|
945
|
+
return ALONE[call.name](call, env, who)
|
|
946
|
+
here = who.key
|
|
947
|
+
if call.team is not None:
|
|
948
|
+
who = replace(who, key=_named(call.team, env).key)
|
|
949
|
+
elif who.key is None:
|
|
950
|
+
raise _outside(argv, env)
|
|
951
|
+
d, r = _open(env, who, argv)
|
|
952
|
+
check_caller(call, who.id, r)
|
|
953
|
+
return ON_A_TEAM[call.name](Ctx(call, d, Server(r.key), who.id, env, typed, here))
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
def _start(call: Call, env: Mapping[str, str], who: Caller) -> Result:
|
|
957
|
+
"""The bare command: start the team in this folder, or bring it back, then attach to it."""
|
|
958
|
+
check_version()
|
|
959
|
+
if who.key is not None:
|
|
960
|
+
raise Refused(INSIDE)
|
|
961
|
+
cwd = Path.cwd()
|
|
962
|
+
_gate(call)
|
|
963
|
+
# The lock the look and the making share, so two first starts in one folder make one team.
|
|
964
|
+
# It is dropped here, before the team's own lock below: a first start in another folder
|
|
965
|
+
# must not wait on whatever this team is doing.
|
|
966
|
+
with root_locked(env):
|
|
967
|
+
here = _here(env, cwd)
|
|
968
|
+
if call.new is not None or not here:
|
|
969
|
+
d, first = _make_team(call, env, cwd, call.new or _default_name(cwd)), "Started"
|
|
970
|
+
elif len(here) == 1:
|
|
971
|
+
d, first = team_dir(here[0][0], env), ""
|
|
972
|
+
else:
|
|
973
|
+
raise Refused(_several(here, env, cwd))
|
|
974
|
+
if not first:
|
|
975
|
+
# Outside the root lock: this asks the team's server, and a frozen one would hold every
|
|
976
|
+
# first start on the host. A team being made has no server to ask.
|
|
977
|
+
_boots(Server(d.name))
|
|
978
|
+
with locked(d):
|
|
979
|
+
r = load(d)
|
|
980
|
+
if r is None:
|
|
981
|
+
raise Refused(FORGOTTEN)
|
|
982
|
+
# Before `_flags` saves anything: a command changes nothing it then refuses.
|
|
983
|
+
if not first and call.workers is not None:
|
|
984
|
+
raise UsageError(_naming(LATE_WORKERS, r.name))
|
|
985
|
+
flags = "" if first else _flags(d, r, call)
|
|
986
|
+
# Only a start names the team: its name may differ from the folder's (`tboard`).
|
|
987
|
+
result, notices = _run_team(call, env, d, r, first, flags, r.name if first else None)
|
|
988
|
+
notify(r, notices)
|
|
989
|
+
return result
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
def _dirs(env: Mapping[str, str]) -> list[Path]:
|
|
993
|
+
"""What could hold a team. The root also holds the lock the bare command takes."""
|
|
994
|
+
root = root_dir(env)
|
|
995
|
+
return [d for d in sorted(root.iterdir()) if d.is_dir()] if root.is_dir() else []
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
def _here(env: Mapping[str, str], cwd: Path) -> list[tuple[str, str]]:
|
|
999
|
+
"""(key, name) of the teams whose roster names this folder. A directory that answers
|
|
1000
|
+
nothing is passed over: bringing a team back needs no other team's name."""
|
|
1001
|
+
out = []
|
|
1002
|
+
for d in _dirs(env):
|
|
1003
|
+
with contextlib.suppress(StateError):
|
|
1004
|
+
got = identity(d)
|
|
1005
|
+
if got is not None and (got[1], got[2]) == (str(cwd), gethostname()):
|
|
1006
|
+
out.append((d.name, got[0]))
|
|
1007
|
+
return out
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
def _known(env: Mapping[str, str]) -> list[tuple[str, str, str]]:
|
|
1011
|
+
"""(key, name, folder) of every team on this host, read strictly: a name is checked against
|
|
1012
|
+
them all, so a directory that answers neither name nor folder stops the check."""
|
|
1013
|
+
here = gethostname()
|
|
1014
|
+
out = []
|
|
1015
|
+
for d in _dirs(env):
|
|
1016
|
+
got = identity(d)
|
|
1017
|
+
if got is not None and got[2] == here:
|
|
1018
|
+
out.append((d.name, got[0], got[1]))
|
|
1019
|
+
return out
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
def _taken(new: str, env: Mapping[str, str], mine: str = "") -> tuple[str, str, str] | None:
|
|
1023
|
+
"""The team that holds a name, read the one way: a name is unique on the host, so every
|
|
1024
|
+
team's is read, a roster from a newer workcrew included. Another team's key is taken too:
|
|
1025
|
+
`-r` reads names and keys as one, and a name that is a key would reach two teams."""
|
|
1026
|
+
return next((t for t in _known(env) if new in (t[1], t[0]) and t[0] != mine), None)
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
def _make_team(call: Call, env: Mapping[str, str], cwd: Path, name: str) -> Path:
|
|
1030
|
+
_team_name(name)
|
|
1031
|
+
taken = _taken(name, env)
|
|
1032
|
+
if taken is not None:
|
|
1033
|
+
raise Refused(
|
|
1034
|
+
f"{name} is the name of the team in {shown(Path(taken[2]))};"
|
|
1035
|
+
" choose another: workcrew --name NAME."
|
|
1036
|
+
)
|
|
1037
|
+
key = new_key(name, cwd)
|
|
1038
|
+
# Before the directory: a start this refuses must leave no team behind.
|
|
1039
|
+
_boots(Server(key))
|
|
1040
|
+
d = team_dir(key, env)
|
|
1041
|
+
make(d)
|
|
1042
|
+
create(d, cwd, gethostname(), name, list(call.flags or []), call.cap, _workers(call))
|
|
1043
|
+
return d
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
def _default_name(cwd: Path) -> str:
|
|
1047
|
+
"""The name a team takes from its folder when none is given. `slug` prefixes a reserved
|
|
1048
|
+
word; a command word is prefixed here, where the command words are, so a folder called
|
|
1049
|
+
`board` still starts a team."""
|
|
1050
|
+
s = slug(cwd.name)
|
|
1051
|
+
return f"t{s[:14]}" if s in COMMANDS else s
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
def _team_name(new: str) -> str:
|
|
1055
|
+
"""Every rule a team's name keeps. The command word is refused here, and not beside the
|
|
1056
|
+
other rules in state.py, because the command words are here; both the team being made and
|
|
1057
|
+
`rename` come through this one function."""
|
|
1058
|
+
check_team_name(new)
|
|
1059
|
+
if new in COMMANDS:
|
|
1060
|
+
raise Refused(f"'{new}' is a command, so it cannot name a team.")
|
|
1061
|
+
return new
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
def _several(here: Sequence[tuple[str, str]], env: Mapping[str, str], cwd: Path) -> str:
|
|
1065
|
+
"""The bare command was asked to start or attach and did neither, so it says how to do
|
|
1066
|
+
each. Every name works from anywhere, so the lines need no folder."""
|
|
1067
|
+
lines = "".join(f"\n workcrew -r {name}" for _, name in here)
|
|
1068
|
+
return (
|
|
1069
|
+
f"{len(here)} teams use {_folder(env, cwd)}, so none was started."
|
|
1070
|
+
f" Open one:{lines}\nOr start another in this folder: workcrew --name NAME"
|
|
1071
|
+
)
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
def _gate(call: Call) -> harness.Harness:
|
|
1075
|
+
h = harness.get("claude")
|
|
1076
|
+
if call.flags is not None:
|
|
1077
|
+
# A member's tail is the two values it is started with; the team's is everything Claude
|
|
1078
|
+
# Code takes. The harness judges both, here, before any lock and before anything is saved.
|
|
1079
|
+
if call.name == "resume":
|
|
1080
|
+
h.member_flags(call.flags)
|
|
1081
|
+
else:
|
|
1082
|
+
h.gate(call.flags)
|
|
1083
|
+
h.check()
|
|
1084
|
+
return h
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
def _unstop(d: Path, r: Roster, asked: Sequence[Member]) -> None:
|
|
1088
|
+
"""The members the owner now wants running, saved before any of them starts."""
|
|
1089
|
+
if any(m.stopping for m in asked):
|
|
1090
|
+
for m in asked:
|
|
1091
|
+
m.stopping = False
|
|
1092
|
+
save(d, r)
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
def _run_team(
|
|
1096
|
+
call: Call,
|
|
1097
|
+
env: Mapping[str, str],
|
|
1098
|
+
d: Path,
|
|
1099
|
+
r: Roster,
|
|
1100
|
+
first: str,
|
|
1101
|
+
flags: str,
|
|
1102
|
+
team: str | None = None,
|
|
1103
|
+
) -> tuple[Result, list[Notice]]:
|
|
1104
|
+
"""Start what is not running and hand the caller to tmux. The lock is already held, so the
|
|
1105
|
+
notices are given back to be sent once it is not."""
|
|
1106
|
+
cwd = Path(r.cwd)
|
|
1107
|
+
server = Server(r.key)
|
|
1108
|
+
# Its own read: the bare start and `-r` hold the lock with no team context yet.
|
|
1109
|
+
view = reconcile.up(server)
|
|
1110
|
+
was = reconcile.activity(view, r) if view is not None else {}
|
|
1111
|
+
want = [m.id for m in r.members if was.get(m.id) not in ("busy", "idle", "wait")]
|
|
1112
|
+
_unstop(d, r, r.members)
|
|
1113
|
+
# tmux makes a client's terminal of its stdin alone and fails, after this is printed, on
|
|
1114
|
+
# one that is not (server-client.c); inside tmux the client opens a window there instead.
|
|
1115
|
+
attach = bool(env.get("TMUX")) or os.isatty(0)
|
|
1116
|
+
if not first:
|
|
1117
|
+
first = "Resumed" if want else "Attached to" if attach else ""
|
|
1118
|
+
held = reconcile.start(d, r, server, want, _tool("owner", server), boots=True)
|
|
1119
|
+
reconcile.restart_poller(d, r, server)
|
|
1120
|
+
# A team that was down comes back as the owner arranged it, when its members are the same;
|
|
1121
|
+
# one that runs keeps the owner's arrangement.
|
|
1122
|
+
notices = reconcile.reconcile(d, r, server, "restore" if view is None else "join")
|
|
1123
|
+
back = "".join(
|
|
1124
|
+
f"{m} had exited; it is back.\n" for m in want if was.get(m) == "exited" and m not in held
|
|
1125
|
+
)
|
|
1126
|
+
wait = _waiting_for_trust(r, held, True, r.name) + "\n" if held else ""
|
|
1127
|
+
where = _folder(env, cwd)
|
|
1128
|
+
opened = _opened(first, r, where, team) if first else ""
|
|
1129
|
+
text = opened + back + wait + _caps(r) + flags
|
|
1130
|
+
if not attach:
|
|
1131
|
+
return Result(text + NO_TERMINAL.format(name=r.name), "", 0), notices
|
|
1132
|
+
return Result(text, "", 0, server), notices
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
def _workers(call: Call) -> int:
|
|
1136
|
+
return 3 if call.workers is None else call.workers
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
def _opened(first: str, r: Roster, folder: str, team: str | None) -> str:
|
|
1140
|
+
"""The one line that says which of Started, Resumed and Attached happened."""
|
|
1141
|
+
crew = f"{_crew(r, team)} in {folder}"
|
|
1142
|
+
if first == "Started" and not r.workers():
|
|
1143
|
+
return f"Started {crew}. Add workers with {_cmd(r.name, 'add N')}.\n"
|
|
1144
|
+
if first == "Resumed":
|
|
1145
|
+
return f"Resumed {crew} (same session ids).\n"
|
|
1146
|
+
return f"{first} {crew}.\n"
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
def _crew(r: Roster, team: str | None = None) -> str:
|
|
1150
|
+
n = len(r.workers())
|
|
1151
|
+
if n == 0:
|
|
1152
|
+
return "the leader alone" if team is None else f"the leader of {team}, alone,"
|
|
1153
|
+
of = "" if team is None else f" of {team}"
|
|
1154
|
+
return f"leader + {n} worker{'s' if n > 1 else ''}{of}"
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
def _caps(r: Roster) -> str:
|
|
1158
|
+
if r.cap and not reconcile.cap_ok():
|
|
1159
|
+
return "Memory caps are off here: no systemd user manager.\n"
|
|
1160
|
+
return ""
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
def _flags(d: Path, r: Roster, call: Call) -> str:
|
|
1164
|
+
"""A new tail becomes the team's flags; it reaches members started from now."""
|
|
1165
|
+
if call.flags is None or list(call.flags) == r.flags:
|
|
1166
|
+
return ""
|
|
1167
|
+
was = " ".join(r.flags)
|
|
1168
|
+
r.flags = list(call.flags)
|
|
1169
|
+
r.flags_at = _stamp()
|
|
1170
|
+
save(d, r)
|
|
1171
|
+
apply = APPLY.format(name=r.name)
|
|
1172
|
+
if not r.flags:
|
|
1173
|
+
return f"Team flags are now none. {apply}\n"
|
|
1174
|
+
return f"Team flags: was {f'`{was}`' if was else 'none'}, now `{' '.join(r.flags)}`.\n{apply}\n"
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
def _stamp() -> str:
|
|
1178
|
+
"""To the millisecond, as Claude Code stamps a request: cut back to the second, flags set
|
|
1179
|
+
here would lose to a request made earlier in the second they were set in."""
|
|
1180
|
+
return datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
def _board_cmd(c: Ctx) -> Result:
|
|
1184
|
+
"""The view, the redraw, or one of the members' verbs."""
|
|
1185
|
+
args = c.call.args
|
|
1186
|
+
if not args or args[0] == "--loop":
|
|
1187
|
+
return _loop(c, int(args[1])) if args else Result(_drawn(c, c.typed), "", 0)
|
|
1188
|
+
verb, rest = args[0], list(args[1:])
|
|
1189
|
+
to = _to(rest) if verb in ("new", "back") else None
|
|
1190
|
+
brief = _brief(rest) if verb == "new" else ""
|
|
1191
|
+
if verb == "new":
|
|
1192
|
+
rest = [" ".join(rest)] if rest else []
|
|
1193
|
+
elif verb == "report" and len(rest) > 1:
|
|
1194
|
+
rest = [rest[0], " ".join(rest[1:])]
|
|
1195
|
+
with _held(c) as h:
|
|
1196
|
+
was = reconcile.activity(h.view, h.r) if h.view is not None else {}
|
|
1197
|
+
out, notices = board.act(
|
|
1198
|
+
c.d, verb, rest, c.caller, [w.id for w in h.r.workers()], was, h.r.names(), brief, to
|
|
1199
|
+
)
|
|
1200
|
+
notices += reconcile.reconcile(c.d, h.r, c.server)
|
|
1201
|
+
notify(h.r, notices)
|
|
1202
|
+
return Result(out, "", 0)
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
def _ends(text: str) -> str:
|
|
1206
|
+
"""What a command prints ends in a newline, so a shell's next line starts on its own."""
|
|
1207
|
+
return text if not text or text.endswith("\n") else text + "\n"
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
def _to(rest: list[str]) -> str | None:
|
|
1211
|
+
"""`board new TITLE --to WORKER`, `board back ID --to WORKER`: read as `-f` is, a word of
|
|
1212
|
+
its own wherever it falls, so a title keeps `--to` only inside a quoted word."""
|
|
1213
|
+
if "--to" not in rest:
|
|
1214
|
+
return None
|
|
1215
|
+
if rest.count("--to") > 1:
|
|
1216
|
+
raise UsageError("--to is given once: workcrew board new TITLE --to WORKER.")
|
|
1217
|
+
at = rest.index("--to")
|
|
1218
|
+
if at + 1 >= len(rest):
|
|
1219
|
+
raise UsageError("--to takes a worker, e.g. workcrew board new TITLE --to w2.")
|
|
1220
|
+
worker = rest[at + 1]
|
|
1221
|
+
del rest[at : at + 2]
|
|
1222
|
+
return worker
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
def _brief(rest: list[str]) -> str:
|
|
1226
|
+
"""`board new TITLE -f FILE`: the file holds the brief, and the title stays one line."""
|
|
1227
|
+
if "-f" not in rest:
|
|
1228
|
+
return ""
|
|
1229
|
+
at = rest.index("-f")
|
|
1230
|
+
if at + 1 >= len(rest):
|
|
1231
|
+
raise UsageError(
|
|
1232
|
+
"-f takes a file holding the brief, e.g. workcrew board new TITLE -f b.md."
|
|
1233
|
+
)
|
|
1234
|
+
path = Path(rest[at + 1])
|
|
1235
|
+
del rest[at : at + 2]
|
|
1236
|
+
try:
|
|
1237
|
+
return path.read_text()
|
|
1238
|
+
except OSError as e:
|
|
1239
|
+
raise Refused(f"{shown(path)} cannot be read: {e.strerror}.") from e
|
|
1240
|
+
except UnicodeDecodeError:
|
|
1241
|
+
raise Refused(f"{shown(path)} is not UTF-8 text, so it cannot be a brief.") from None
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
def _drawn(c: Ctx, popup: bool) -> str:
|
|
1245
|
+
with _held(c) as h:
|
|
1246
|
+
# With the server down nothing says which members had started, so every one shows EXITED.
|
|
1247
|
+
was: Mapping[str, board.Activity] = (
|
|
1248
|
+
reconcile.activity(h.view, h.r)
|
|
1249
|
+
if h.view is not None
|
|
1250
|
+
else dict.fromkeys((m.id for m in h.r.members), "exited")
|
|
1251
|
+
)
|
|
1252
|
+
return board.view(
|
|
1253
|
+
c.d,
|
|
1254
|
+
[(m.id, was[m.id]) for m in h.r.members],
|
|
1255
|
+
h.r.names(),
|
|
1256
|
+
_size(popup, c.env),
|
|
1257
|
+
)
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
def _size(popup: bool, env: Mapping[str, str]) -> tuple[int, int] | None:
|
|
1261
|
+
"""The terminal the board will be read in. A line typed at C-] shows its result in a popup
|
|
1262
|
+
of its own on the caller's team, whichever team it names, but a redraw stays in the
|
|
1263
|
+
three-row command line it was typed at."""
|
|
1264
|
+
own = socket_team(env) if popup else None
|
|
1265
|
+
if own is not None:
|
|
1266
|
+
return show_size(Server(own).view())
|
|
1267
|
+
if not sys.stdout.isatty():
|
|
1268
|
+
return None
|
|
1269
|
+
size = os.get_terminal_size()
|
|
1270
|
+
return size.columns, size.lines
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
def _loop(c: Ctx, sec: int) -> Result:
|
|
1274
|
+
"""`board --loop [SEC]`: redraw in place until the owner stops it."""
|
|
1275
|
+
with contextlib.suppress(KeyboardInterrupt):
|
|
1276
|
+
while True:
|
|
1277
|
+
sys.stdout.write("\x1b[H\x1b[2J" + _drawn(c, False))
|
|
1278
|
+
sys.stdout.flush()
|
|
1279
|
+
if _stopped(sec):
|
|
1280
|
+
break
|
|
1281
|
+
# The board's last line carries no newline, so without this the shell's next prompt
|
|
1282
|
+
# would start on it.
|
|
1283
|
+
return Result("\n", "", 0)
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
def _stopped(sec: int) -> bool:
|
|
1287
|
+
"""Esc or q pressed while the next redraw is waited for. cbreak leaves Ctrl-C a signal."""
|
|
1288
|
+
if not sys.stdin.isatty():
|
|
1289
|
+
time.sleep(sec)
|
|
1290
|
+
return False
|
|
1291
|
+
was = termios.tcgetattr(sys.stdin)
|
|
1292
|
+
try:
|
|
1293
|
+
# TCSAFLUSH, the default, would throw away a key pressed before the redraw.
|
|
1294
|
+
tty.setcbreak(sys.stdin.fileno(), termios.TCSANOW)
|
|
1295
|
+
ready, _, _ = _waiting([sys.stdin], [], [], sec)
|
|
1296
|
+
return bool(ready) and os.read(sys.stdin.fileno(), 1) in (b"\x1b", b"q")
|
|
1297
|
+
finally:
|
|
1298
|
+
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, was)
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
def _line(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
1302
|
+
"""The team's own status line, which tmux runs every few seconds."""
|
|
1303
|
+
c = _hidden("_line", env, who)
|
|
1304
|
+
d = c.d
|
|
1305
|
+
with _held(c) as h:
|
|
1306
|
+
r, view = h.r, h.view
|
|
1307
|
+
if view is None:
|
|
1308
|
+
return Result("", "", 0)
|
|
1309
|
+
was = reconcile.activity(view, r)
|
|
1310
|
+
gone = bool(link.relays(d)) and not any(
|
|
1311
|
+
p.member == POLLER and not p.dead for p in view.panes
|
|
1312
|
+
)
|
|
1313
|
+
counts = board.counts(d)
|
|
1314
|
+
idle = sum(
|
|
1315
|
+
1 for w in r.workers() if was.get(w.id) == "idle" and board.held(d, w.id) is None
|
|
1316
|
+
)
|
|
1317
|
+
waiting = [m.name for m in r.members if was.get(m.id) == "wait"]
|
|
1318
|
+
return Result(
|
|
1319
|
+
board.status_line(
|
|
1320
|
+
was.get("leader") == "exited",
|
|
1321
|
+
waiting,
|
|
1322
|
+
counts,
|
|
1323
|
+
idle,
|
|
1324
|
+
sum(1 for m in r.members if was.get(m.id) == "exited"),
|
|
1325
|
+
link_stopped=gone,
|
|
1326
|
+
width=int(argv[0]),
|
|
1327
|
+
),
|
|
1328
|
+
"",
|
|
1329
|
+
0,
|
|
1330
|
+
)
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
def _link_cmd(c: Ctx) -> Result:
|
|
1334
|
+
"""Federation. `link` takes the locks it needs itself, across the network, so the team
|
|
1335
|
+
lock is held here only for `remove`, which touches nothing but the links file."""
|
|
1336
|
+
args = c.call.args
|
|
1337
|
+
if args and args[0] == "remove":
|
|
1338
|
+
with _held(c) as h:
|
|
1339
|
+
out = link.remove(c.d, args[1])
|
|
1340
|
+
notices = reconcile.reconcile(c.d, h.r, c.server) # the last link ends the poller
|
|
1341
|
+
notify(h.r, notices)
|
|
1342
|
+
return Result(_ends(out), "", 0)
|
|
1343
|
+
with _held(c) as h:
|
|
1344
|
+
r = h.r
|
|
1345
|
+
if not args:
|
|
1346
|
+
return Result(_ends(link.address(c.d, r)), "", 0)
|
|
1347
|
+
if args[0] == "add":
|
|
1348
|
+
out = link.add(c.d, r, args[1], args[2], args[3], args[4])
|
|
1349
|
+
try:
|
|
1350
|
+
with _held(c) as h:
|
|
1351
|
+
# An owner's command: it restarts a dead poller, which would hide the new link.
|
|
1352
|
+
reconcile.restart_poller(c.d, h.r, c.server)
|
|
1353
|
+
notices = reconcile.reconcile(c.d, h.r, c.server)
|
|
1354
|
+
except Refused:
|
|
1355
|
+
return Result(_ends(out), "", 0) # the link is on record; only the tidying is lost
|
|
1356
|
+
notify(h.r, notices)
|
|
1357
|
+
return Result(_ends(out), "", 0)
|
|
1358
|
+
if args[0] == "send":
|
|
1359
|
+
return Result(_ends(link.send(c.d, r, c.caller, args[1].split(","), args[2])), "", 0)
|
|
1360
|
+
return Result(_ends(link.read(c.d, c.caller)), "", 0)
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
def _linkwatch(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
1364
|
+
"""The poller's pane: one pass over every relay, every two seconds, until it is closed."""
|
|
1365
|
+
c = _hidden("_linkwatch", env, who)
|
|
1366
|
+
while True:
|
|
1367
|
+
with _held(c) as h:
|
|
1368
|
+
r = h.r
|
|
1369
|
+
# The relays are reached without the lock: a pass can take as long as the network does.
|
|
1370
|
+
notify(r, link.poll(c.d, r))
|
|
1371
|
+
time.sleep(2)
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
def _send_cmd(c: Ctx) -> Result:
|
|
1375
|
+
"""The owner's text is typed into the panes; a member's goes through the inbox."""
|
|
1376
|
+
to, text = c.call.args
|
|
1377
|
+
with _held(c) as h:
|
|
1378
|
+
targets = select(to, h.r)
|
|
1379
|
+
names, roster, named = h.r.names(), h.r, _named_by(c, h.r)
|
|
1380
|
+
panes = {p.member: p.id for p in h.view.panes if not p.dead} if h.view is not None else {}
|
|
1381
|
+
if c.caller == "owner":
|
|
1382
|
+
out, code = send.typed_text(
|
|
1383
|
+
send.type_to(c.server, [(m, panes.get(m.id)) for m in targets], text),
|
|
1384
|
+
_cmd(named, "resume"),
|
|
1385
|
+
_back_line(c, roster, ends=True),
|
|
1386
|
+
)
|
|
1387
|
+
else:
|
|
1388
|
+
out, code = send.inbox_to(targets, names[c.caller], text, panes)
|
|
1389
|
+
_settle(c)
|
|
1390
|
+
return Result(_ends(out), "", code)
|
|
1391
|
+
|
|
1392
|
+
|
|
1393
|
+
def _layout(c: Ctx) -> Result:
|
|
1394
|
+
"""Store the side and lay the window out; the line says where the leader went."""
|
|
1395
|
+
side = c.call.args[0]
|
|
1396
|
+
with _held(c) as h:
|
|
1397
|
+
h.r.side = cast("Side", side)
|
|
1398
|
+
if h.view is None: # the next start lays out the side asked for, not what was kept
|
|
1399
|
+
h.r.arrangement = None
|
|
1400
|
+
save(c.d, h.r)
|
|
1401
|
+
notices = reconcile.reconcile(c.d, h.r, c.server, "grid")
|
|
1402
|
+
notify(h.r, notices)
|
|
1403
|
+
return Result(_laid_out(h.r, c.server, _named_by(c, h.r)), "", 0)
|
|
1404
|
+
|
|
1405
|
+
|
|
1406
|
+
def _laid_out(r: Roster, server: Server, team: str | None) -> str:
|
|
1407
|
+
# Its own read, after the lock: reconcile has just moved the panes this line describes.
|
|
1408
|
+
view = reconcile.up(server)
|
|
1409
|
+
sides = {"left": "on the left", "right": "on the right", "middle": "in the middle"}
|
|
1410
|
+
if view is None:
|
|
1411
|
+
return f"Saved: the leader goes {sides[r.side]} when the team next starts.\n"
|
|
1412
|
+
if not any(p.member == "leader" for p in view.panes):
|
|
1413
|
+
# Nothing was laid out around a leader, so the line cannot say where one sits.
|
|
1414
|
+
back = _cmd(team, "resume leader")
|
|
1415
|
+
return f"Saved: the leader goes {sides[r.side]}; `{back}` brings it back.\n"
|
|
1416
|
+
used, cells = reconcile.laid_out(view, r)
|
|
1417
|
+
beside = sum(1 for cell in cells if cell.member not in (None, "leader"))
|
|
1418
|
+
narrow = ""
|
|
1419
|
+
if r.side == "middle" and used != "middle":
|
|
1420
|
+
narrow = (
|
|
1421
|
+
"The leader goes in the middle with two or more panes beside it;"
|
|
1422
|
+
f" with {beside} it is on the right.\n"
|
|
1423
|
+
)
|
|
1424
|
+
many = "" if beside == 1 else "s"
|
|
1425
|
+
return f"Leader {sides[used]} ({beside} worker{many} beside it).\n{narrow}"
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
def _rename_cmd(c: Ctx) -> Result:
|
|
1429
|
+
"""The team's own name, or a worker's. Nothing else moves: not the state, not a session."""
|
|
1430
|
+
with _held(c) as h:
|
|
1431
|
+
who, new = rename(c.call.args, h.r)
|
|
1432
|
+
check_new_name(new, h.r, who)
|
|
1433
|
+
if who is None:
|
|
1434
|
+
_team_name(new)
|
|
1435
|
+
if who is None:
|
|
1436
|
+
text, notices = _rename_team(c, h, new)
|
|
1437
|
+
else:
|
|
1438
|
+
text, notices = _rename_worker(c, h, who, new)
|
|
1439
|
+
notices += reconcile.reconcile(c.d, h.r, c.server)
|
|
1440
|
+
notify(h.r, notices)
|
|
1441
|
+
return Result(text, "", 0)
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
def _rename_team(c: Ctx, h: Held, new: str) -> tuple[str, list[Notice]]:
|
|
1445
|
+
r = h.r
|
|
1446
|
+
taken = _taken(new, c.env, r.key)
|
|
1447
|
+
if taken is not None:
|
|
1448
|
+
raise Refused(f"{new} is the name of the team in {shown(Path(taken[2]))}; choose another.")
|
|
1449
|
+
old, r.name = r.name, new
|
|
1450
|
+
save(c.d, r)
|
|
1451
|
+
# `-r NEW` alone is refused inside any team.
|
|
1452
|
+
where = f" workcrew -r {new} resumes it from any folder." if c.here is None else ""
|
|
1453
|
+
return f"Renamed {old} to {new}.{where}\n", []
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
def _rename_worker(c: Ctx, h: Held, who: Member, new: str) -> tuple[str, list[Notice]]:
|
|
1457
|
+
old, who.name = who.name, new
|
|
1458
|
+
save(c.d, h.r)
|
|
1459
|
+
if h.view is not None:
|
|
1460
|
+
pane = next((p.id for p in h.view.panes if p.member == who.id), None)
|
|
1461
|
+
if pane is not None:
|
|
1462
|
+
# The name is saved. A pane that closed since the view was read took its border
|
|
1463
|
+
# title with it, so there is nothing left to put the new name on.
|
|
1464
|
+
with contextlib.suppress(Gone):
|
|
1465
|
+
c.server.set_name(pane, new)
|
|
1466
|
+
# The leader is told only when it was not the leader that renamed the worker.
|
|
1467
|
+
told = [] if c.caller == "leader" else [Notice("leader", f"{old} is now {new}")]
|
|
1468
|
+
return f"Renamed {old} to {new}.\n", [*told, Notice(who.id, f"You are now {new}")]
|
|
1469
|
+
|
|
1470
|
+
|
|
1471
|
+
@contextlib.contextmanager
|
|
1472
|
+
def _held(c: Ctx) -> Iterator[Held]:
|
|
1473
|
+
"""The only way in. A command can wait on the lock long enough for its own pane to be
|
|
1474
|
+
killed, for the team to be forgotten and another to take its folder, for the roster to
|
|
1475
|
+
change and for the server to stop, so all four are read here and nothing a command read
|
|
1476
|
+
before the lock is used after it. The caller is checked against that roster too, because
|
|
1477
|
+
`check_caller` ruled on the one from before the wait."""
|
|
1478
|
+
with locked(c.d):
|
|
1479
|
+
# Asked again, and answered with: a caller with no pane of its own is the member whose
|
|
1480
|
+
# harness it runs under, and that harness can end while the command waits.
|
|
1481
|
+
if whoami(c.env)[1] != c.caller:
|
|
1482
|
+
raise Refused(CALLER_GONE)
|
|
1483
|
+
r = fresh(c.d)
|
|
1484
|
+
# Only a member can be lost from the roster. The owner is on no roster, and neither is
|
|
1485
|
+
# the poller, whose pane carries the tool's own tag.
|
|
1486
|
+
if c.caller not in ("owner", POLLER) and r.by_id(c.caller) is None:
|
|
1487
|
+
raise Refused(REMOVED)
|
|
1488
|
+
yield Held(r, reconcile.up(c.server))
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
def _settle(c: Ctx) -> None:
|
|
1492
|
+
"""Reconcile and notify after a command that changed no state. What the command did is
|
|
1493
|
+
already done, so the entry's refusals take none of it back: a caller the roster has lost
|
|
1494
|
+
since, or a team forgotten since, leaves only this tidying, which is then left undone."""
|
|
1495
|
+
try:
|
|
1496
|
+
with _held(c) as h:
|
|
1497
|
+
notices = reconcile.reconcile(c.d, h.r, c.server)
|
|
1498
|
+
except Refused:
|
|
1499
|
+
return
|
|
1500
|
+
notify(h.r, notices)
|
|
1501
|
+
|
|
1502
|
+
|
|
1503
|
+
def _exit(c: Ctx) -> Result:
|
|
1504
|
+
"""Stop every member; the state and the transcripts stay, and so do the owner's panes."""
|
|
1505
|
+
call, d, server = c.call, c.d, c.server
|
|
1506
|
+
forget_it = call.args == ("--forget",)
|
|
1507
|
+
with _held(c) as h:
|
|
1508
|
+
# Placed before anything stops, so a cache with no place refuses with nothing done. The
|
|
1509
|
+
# state folder's name is the key; a roster.json repaired by hand may say otherwise.
|
|
1510
|
+
cache = _cache(c.env, d.name) if forget_it else None
|
|
1511
|
+
if h.view is None and not forget_it:
|
|
1512
|
+
raise Refused(
|
|
1513
|
+
NOT_RUNNING.format(name=h.r.name, back=_back_line(c, h.r), sessions=_sessions(h.r))
|
|
1514
|
+
)
|
|
1515
|
+
stopped, left = 0, 0
|
|
1516
|
+
if h.view is not None:
|
|
1517
|
+
stopped = sum(1 for p in h.view.panes if p.member and not p.dead)
|
|
1518
|
+
# A stop killed half way left the window half closed, and the arrangement before it.
|
|
1519
|
+
again = any(m.stopping for m in h.r.members)
|
|
1520
|
+
# Saved before the first mark or signal: however the stop ends, no pass starts them.
|
|
1521
|
+
for m in h.r.members:
|
|
1522
|
+
m.stopping = True
|
|
1523
|
+
with contextlib.suppress(Gone):
|
|
1524
|
+
# The next start puts the panes back as they are now, and lays out for whoever
|
|
1525
|
+
# attaches then, not for a grid this start never showed anyone.
|
|
1526
|
+
if not again:
|
|
1527
|
+
h.r.arrangement = server.arrangement() or h.r.arrangement
|
|
1528
|
+
server.take_fresh()
|
|
1529
|
+
save(d, h.r)
|
|
1530
|
+
# Before the first signal, while the server still has its screens: with the last
|
|
1531
|
+
# member it ends, and each screen showed only tmux's `[exited]`. A screen that also
|
|
1532
|
+
# shows the owner's own panes stays on them, and so does the server.
|
|
1533
|
+
if h.view.clients and all(p.member is not None for p in h.view.panes):
|
|
1534
|
+
# A screen that left since the view is no reason to stop less.
|
|
1535
|
+
with contextlib.suppress(Gone, StateError):
|
|
1536
|
+
server.let_go(back=not forget_it)
|
|
1537
|
+
left = reconcile.stop(server, h.view)
|
|
1538
|
+
gone, kept = None, ""
|
|
1539
|
+
if cache is not None:
|
|
1540
|
+
# Before the state is set aside: once it is, no key names this folder, and a crash
|
|
1541
|
+
# in between would leave it for good. A crash here leaves the team still known,
|
|
1542
|
+
# and the same command finishes it.
|
|
1543
|
+
kept = _clear(cache, d)
|
|
1544
|
+
gone = forget(d)
|
|
1545
|
+
head, one = _exited(h.r, stopped)
|
|
1546
|
+
if gone is not None:
|
|
1547
|
+
head = f"{head} and forgot this team." if stopped else "Forgot this team."
|
|
1548
|
+
return Result(
|
|
1549
|
+
f"{head} Its state is kept at {shown(gone)}; delete that when you are sure.\n{kept}",
|
|
1550
|
+
"",
|
|
1551
|
+
0,
|
|
1552
|
+
)
|
|
1553
|
+
panes = ""
|
|
1554
|
+
if left:
|
|
1555
|
+
many = left > 1
|
|
1556
|
+
panes = (
|
|
1557
|
+
f"{left} pane{'s' if many else ''} you opened {'are' if many else 'is'} still open.\n"
|
|
1558
|
+
)
|
|
1559
|
+
back = _back_line(c, h.r, ends=not left)
|
|
1560
|
+
return Result(f"{head}. {back} brings the same {one} back.\n{panes}", "", 0)
|
|
1561
|
+
|
|
1562
|
+
|
|
1563
|
+
def _sessions(r: Roster) -> str:
|
|
1564
|
+
n = len(r.members)
|
|
1565
|
+
if n == 1:
|
|
1566
|
+
return "session; its transcript is intact"
|
|
1567
|
+
return f"{n} sessions; their transcripts are intact"
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
def _exited(r: Roster, stopped: int) -> tuple[str, str]:
|
|
1571
|
+
"""A team that is the leader alone speaks of one session, not of N members."""
|
|
1572
|
+
if not r.workers():
|
|
1573
|
+
return "Exited the leader", "session"
|
|
1574
|
+
return f"Exited {stopped} member{'' if stopped == 1 else 's'}", "sessions"
|
|
1575
|
+
|
|
1576
|
+
|
|
1577
|
+
def _resume(c: Ctx) -> Result:
|
|
1578
|
+
"""Start members that exited: those named, or the whole team when it is down."""
|
|
1579
|
+
call, d, server = c.call, c.d, c.server
|
|
1580
|
+
_gate(call)
|
|
1581
|
+
with _held(c) as held:
|
|
1582
|
+
r, view = held.r, held.view
|
|
1583
|
+
reconcile.need_folder(d, r)
|
|
1584
|
+
_boots(server)
|
|
1585
|
+
# With the server down nothing is running, and nothing says who had started.
|
|
1586
|
+
was: Mapping[str, board.Activity] = (
|
|
1587
|
+
reconcile.activity(view, r)
|
|
1588
|
+
if view is not None
|
|
1589
|
+
else dict.fromkeys((m.id for m in r.members), "exited")
|
|
1590
|
+
)
|
|
1591
|
+
asked = (
|
|
1592
|
+
members(call.args, r, "resume", c.caller == "owner") if call.args else list(r.members)
|
|
1593
|
+
)
|
|
1594
|
+
want = [m.id for m in asked if was.get(m.id) in (None, "exited", "not-started")]
|
|
1595
|
+
tail: str | None = None
|
|
1596
|
+
if call.flags is not None:
|
|
1597
|
+
tail, stopped = _member_tail(c, r, asked, was, call.flags)
|
|
1598
|
+
want += stopped # their panes are dead now, so the start below brings them back
|
|
1599
|
+
_unstop(d, r, asked)
|
|
1600
|
+
waiting = reconcile.start(
|
|
1601
|
+
d, r, server, want, _tool(c.caller, server), boots=c.caller == "owner"
|
|
1602
|
+
)
|
|
1603
|
+
want = [m for m in want if m not in waiting]
|
|
1604
|
+
reconcile.restart_poller(d, r, server)
|
|
1605
|
+
# A team that was down comes back as the owner arranged it, as the bare start brings it.
|
|
1606
|
+
notices = reconcile.reconcile(d, r, server, "restore" if view is None else "join")
|
|
1607
|
+
if tail is not None:
|
|
1608
|
+
text = tail
|
|
1609
|
+
elif not call.args and view is None and not waiting:
|
|
1610
|
+
text = _resumed(r, _folder(c.env, Path(r.cwd)), c.here is None)
|
|
1611
|
+
else:
|
|
1612
|
+
text = _back(
|
|
1613
|
+
[r.names()[m] for m in want if was.get(m) == "exited"],
|
|
1614
|
+
[m.name for m in asked if m.id not in want + waiting] if call.args else [],
|
|
1615
|
+
[r.names()[m] for m in want if was.get(m) == "not-started"],
|
|
1616
|
+
_waiting_for_trust(
|
|
1617
|
+
r,
|
|
1618
|
+
waiting,
|
|
1619
|
+
was.get("leader") in board.LIVE or "leader" in want,
|
|
1620
|
+
_named_by(c, r),
|
|
1621
|
+
),
|
|
1622
|
+
)
|
|
1623
|
+
notify(r, notices)
|
|
1624
|
+
return Result(text, "", 0)
|
|
1625
|
+
|
|
1626
|
+
|
|
1627
|
+
def _resumed(r: Roster, folder: str, outside: bool) -> str:
|
|
1628
|
+
attach = f" workcrew -r {shlex.quote(r.name)} attaches it." if outside else ""
|
|
1629
|
+
return f"Resumed {_crew(r)} in {folder} (same session ids).{attach}\n"
|
|
1630
|
+
|
|
1631
|
+
|
|
1632
|
+
def _member_tail(
|
|
1633
|
+
c: Ctx,
|
|
1634
|
+
r: Roster,
|
|
1635
|
+
asked: Sequence[Member],
|
|
1636
|
+
was: Mapping[str, board.Activity],
|
|
1637
|
+
flags: Sequence[str],
|
|
1638
|
+
) -> tuple[str, list[str]]:
|
|
1639
|
+
"""The tail of `resume MEMBER… -- --model V --effort V`: the flags those members start with
|
|
1640
|
+
from now. They are saved before anything stops or starts, so a crash leaves them for the next
|
|
1641
|
+
start, and stamped again once the stop has returned. Gives back the lines to print and the
|
|
1642
|
+
members it stopped, which the start that follows brings back on them."""
|
|
1643
|
+
if c.caller == "leader" and any(m.id == "leader" for m in asked):
|
|
1644
|
+
raise Refused(LEADER_FLAGS)
|
|
1645
|
+
if "--effort" in flags and c.server.has_env(EFFORT_ENV):
|
|
1646
|
+
raise Refused(EFFORT_IN_ENV)
|
|
1647
|
+
changed = [m for m in asked if m.flags != list(flags)]
|
|
1648
|
+
live = [m for m in changed if was.get(m.id) in board.LIVE]
|
|
1649
|
+
text = "".join(_set_line(m.name, flags, m in changed, m in live) for m in asked)
|
|
1650
|
+
for m in changed:
|
|
1651
|
+
m.flags = list(flags)
|
|
1652
|
+
m.flags_at = _stamp()
|
|
1653
|
+
save(c.d, r)
|
|
1654
|
+
crash_point("resume.flags")
|
|
1655
|
+
if live:
|
|
1656
|
+
# Saved as stopping before the first signal, as `exit` does: a death the tool asked for
|
|
1657
|
+
# is told to nobody, and no pass starts them before this command does.
|
|
1658
|
+
for m in live:
|
|
1659
|
+
m.stopping = True
|
|
1660
|
+
save(c.d, r)
|
|
1661
|
+
reconcile.stop_members(c.server, [m.id for m in live])
|
|
1662
|
+
# Stamped after the stop and not before it: a session writes until its pane dies, and
|
|
1663
|
+
# its own model and effort are passed back to the next start where they are newer than
|
|
1664
|
+
# the flags, so the stamp above lost to the session this command was ending. The stop
|
|
1665
|
+
# gives up after its wait, and a pane that outlives it keeps the race for its next start.
|
|
1666
|
+
for m in live:
|
|
1667
|
+
m.flags_at = _stamp()
|
|
1668
|
+
save(c.d, r)
|
|
1669
|
+
return text, [m.id for m in live]
|
|
1670
|
+
|
|
1671
|
+
|
|
1672
|
+
def _set_line(name: str, flags: Sequence[str], changed: bool, restarted: bool) -> str:
|
|
1673
|
+
"""What the tool did with one member's tail, never what the model will do with it: a model
|
|
1674
|
+
may ignore an effort, and that is Claude Code's business."""
|
|
1675
|
+
starts = _starts_with(flags)
|
|
1676
|
+
if not changed:
|
|
1677
|
+
return f"{name} already starts with {starts}; nothing changed.\n"
|
|
1678
|
+
return f"{name} now starts with {starts}{'; it was restarted' if restarted else ''}.\n"
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
def _starts_with(flags: Sequence[str]) -> str:
|
|
1682
|
+
"""A tail in the words of the line that reports it; an empty one leaves the team's flags."""
|
|
1683
|
+
pairs = [f"{flags[i].removeprefix('--')} {flags[i + 1]}" for i in range(0, len(flags), 2)]
|
|
1684
|
+
return " and ".join(pairs) if pairs else "the team's flags"
|
|
1685
|
+
|
|
1686
|
+
|
|
1687
|
+
def _back(back: Sequence[str], running: Sequence[str], fresh: Sequence[str], held: str) -> str:
|
|
1688
|
+
"""Only a member that had exited comes back; one that never started simply starts."""
|
|
1689
|
+
lines = []
|
|
1690
|
+
if back:
|
|
1691
|
+
lines.append(f"{_and(back)} {'are' if len(back) > 1 else 'is'} back.")
|
|
1692
|
+
for name in running:
|
|
1693
|
+
lines.append(f"{name} was already running{'' if back or fresh else '; nothing to do'}.")
|
|
1694
|
+
if fresh:
|
|
1695
|
+
lines.append(f"{_and(fresh)} {'start' if len(fresh) > 1 else 'starts'} now.")
|
|
1696
|
+
if held:
|
|
1697
|
+
lines.append(held)
|
|
1698
|
+
if not lines:
|
|
1699
|
+
return "Nothing to bring back: every member is running.\n"
|
|
1700
|
+
return " ".join(lines) + "\n"
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
def _waiting_for_trust(r: Roster, held: Sequence[str], leader_up: bool, team: str | None) -> str:
|
|
1704
|
+
"""What becomes of members held back by an untrusted folder: one that never ran starts on
|
|
1705
|
+
the leader's hello; one that has run waits for the owner's next command. Only a running
|
|
1706
|
+
leader shows the trust question; a dead leader's pane shows the one it last answered."""
|
|
1707
|
+
if not held:
|
|
1708
|
+
return "" # nothing is waiting on the trust question, so it is in nobody's way
|
|
1709
|
+
names = r.names()
|
|
1710
|
+
fresh = [names[m] for m in held if (x := r.by_id(m)) is not None and x.inbox is None]
|
|
1711
|
+
ran = [names[m] for m in held if names[m] not in fresh]
|
|
1712
|
+
lines = []
|
|
1713
|
+
if not leader_up:
|
|
1714
|
+
lines.append(
|
|
1715
|
+
"Claude Code does not trust this folder yet, and only the leader asks; it is not"
|
|
1716
|
+
f" running. Run {_cmd(team, 'resume leader')} and answer the trust question in its"
|
|
1717
|
+
" pane."
|
|
1718
|
+
)
|
|
1719
|
+
if fresh:
|
|
1720
|
+
verb = "start" if len(fresh) > 1 else "starts"
|
|
1721
|
+
lines.append(
|
|
1722
|
+
f"{_and(fresh)} then {verb} on {'their' if len(fresh) > 1 else 'its'} own."
|
|
1723
|
+
if not leader_up
|
|
1724
|
+
else f"{_and(fresh)} {verb} once you answer the trust question in the leader's pane."
|
|
1725
|
+
)
|
|
1726
|
+
if ran:
|
|
1727
|
+
lines.append(
|
|
1728
|
+
f"Then run {_cmd(team, 'resume ' + ' '.join(ran))}."
|
|
1729
|
+
if not leader_up
|
|
1730
|
+
else f"{_and(ran)} {'wait' if len(ran) > 1 else 'waits'}: Claude Code does not trust"
|
|
1731
|
+
" this folder yet, so only the leader starts. Answer the trust question in the"
|
|
1732
|
+
f" leader's pane, then run {_cmd(team, 'resume ' + ' '.join(ran))}."
|
|
1733
|
+
)
|
|
1734
|
+
return " ".join(lines)
|
|
1735
|
+
|
|
1736
|
+
|
|
1737
|
+
def _and(names: Sequence[str]) -> str:
|
|
1738
|
+
return names[0] if len(names) == 1 else ", ".join(names[:-1]) + f" and {names[-1]}"
|
|
1739
|
+
|
|
1740
|
+
|
|
1741
|
+
def _add(c: Ctx) -> Result:
|
|
1742
|
+
"""New workers get the next ids; a stopped team only records them."""
|
|
1743
|
+
call, d, server = c.call, c.d, c.server
|
|
1744
|
+
want = int(call.args[0]) if call.args else 1
|
|
1745
|
+
with _held(c) as h:
|
|
1746
|
+
r, live = h.r, h.view is not None
|
|
1747
|
+
if live:
|
|
1748
|
+
reconcile.need_folder(d, r)
|
|
1749
|
+
new = []
|
|
1750
|
+
for _ in range(want):
|
|
1751
|
+
mid = f"w{r.next_worker}"
|
|
1752
|
+
r.next_worker += 1
|
|
1753
|
+
r.members.append(
|
|
1754
|
+
Member(
|
|
1755
|
+
mid, mid, r.members[0].harness, harness.get(r.members[0].harness).new_id(), None
|
|
1756
|
+
)
|
|
1757
|
+
)
|
|
1758
|
+
new.append(mid)
|
|
1759
|
+
save(d, r)
|
|
1760
|
+
if live and c.caller == "owner":
|
|
1761
|
+
# Not reconcile's to do: it knows only the path the server holds, and an owner's
|
|
1762
|
+
# command by a new path is how an upgrade reaches the team.
|
|
1763
|
+
server.setup(str(d / "tmux.conf"), _tool(c.caller, server))
|
|
1764
|
+
# The leader's note names only the workers it started with. One that added them read
|
|
1765
|
+
# the result, and a stopped team's leader finds them in its note when it starts.
|
|
1766
|
+
hears = live and c.caller != "leader"
|
|
1767
|
+
notices = []
|
|
1768
|
+
if hears:
|
|
1769
|
+
team = ", ".join(m.name for m in r.members)
|
|
1770
|
+
notices.append(Notice("leader", f"Your owner added {_and(new)}. Team: {team}."))
|
|
1771
|
+
notices += reconcile.reconcile(d, r, server)
|
|
1772
|
+
missed = notify(r, notices)
|
|
1773
|
+
who = ", ".join(new)
|
|
1774
|
+
if not live:
|
|
1775
|
+
starts = "They start" if len(new) > 1 else "It starts"
|
|
1776
|
+
return Result(f"Added {who}. {starts} when you run {_back_line(c, r)}.\n", "", 0)
|
|
1777
|
+
told = f" {_leader_hears(r, h.view, missed)}" if hears else ""
|
|
1778
|
+
return Result(f"Added {who}.{told}\n", "", 0)
|
|
1779
|
+
|
|
1780
|
+
|
|
1781
|
+
def _remove(c: Ctx) -> Result:
|
|
1782
|
+
"""A removed worker leaves the roster; reconcile closes its pane, and its task goes back."""
|
|
1783
|
+
call, d, server = c.call, c.d, c.server
|
|
1784
|
+
notices: list[Notice] = []
|
|
1785
|
+
with _held(c) as h:
|
|
1786
|
+
r, view = h.r, h.view
|
|
1787
|
+
gone = members(call.args, r, "remove")
|
|
1788
|
+
if any(m.id == "leader" for m in gone):
|
|
1789
|
+
raise Refused("the leader is not removed.")
|
|
1790
|
+
# The leader is told only when it was not the leader that removed the worker.
|
|
1791
|
+
hears = c.caller != "leader"
|
|
1792
|
+
# Every task goes back before anyone leaves the roster, and the roster is saved once.
|
|
1793
|
+
# A crash anywhere in here then leaves a team whose every task has a member holding
|
|
1794
|
+
# it, and the same command again finishes the work.
|
|
1795
|
+
left = [w.id for w in r.workers() if w not in gone]
|
|
1796
|
+
activity = reconcile.activity(view, r) if view is not None else {}
|
|
1797
|
+
freed = []
|
|
1798
|
+
ready: dict[str, Notice] = {}
|
|
1799
|
+
for m in gone:
|
|
1800
|
+
ids, more = board.release(d, m.id, left, activity, r.names())
|
|
1801
|
+
# Each release counts Ready again: a worker hears only the count the command leaves.
|
|
1802
|
+
ready.update((n.to, n) for n in more)
|
|
1803
|
+
freed.append((m, ids))
|
|
1804
|
+
notices += ready.values()
|
|
1805
|
+
for m in gone:
|
|
1806
|
+
r.members.remove(m)
|
|
1807
|
+
save(d, r)
|
|
1808
|
+
if hears:
|
|
1809
|
+
notices += [Notice("leader", _left(m.name, ids, r)) for m, ids in freed]
|
|
1810
|
+
notices += reconcile.reconcile(d, r, server)
|
|
1811
|
+
missed = notify(r, notices)
|
|
1812
|
+
told = _leader_hears(r, view, missed) if hears else ""
|
|
1813
|
+
lines = [_removed(m, ids, told, r.cwd) for m, ids in freed]
|
|
1814
|
+
if len(gone) > 1:
|
|
1815
|
+
lines.append(f"Team now: {', '.join(m.name for m in r.members)}.\n")
|
|
1816
|
+
return Result("".join(lines), "", 0)
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
def _removed(m: Member, ids: Sequence[int], told: str, cwd: str) -> str:
|
|
1820
|
+
"""`remove` is the one command a hand alone can undo, so it says how. A member that never
|
|
1821
|
+
said hello never ran, and has no session to reopen."""
|
|
1822
|
+
held = (
|
|
1823
|
+
f"T{ids[0]} is back in Ready. The working tree may hold its unfinished edits."
|
|
1824
|
+
if ids
|
|
1825
|
+
else "It held no task."
|
|
1826
|
+
)
|
|
1827
|
+
line = " ".join(part for part in (f"Removed {m.name}.", held, told) if part) + "\n"
|
|
1828
|
+
if m.inbox is None:
|
|
1829
|
+
return line
|
|
1830
|
+
where = shlex.quote(cwd)
|
|
1831
|
+
return f"{line}To reopen its session: cd {where} && {m.harness} --resume {m.session_id}\n"
|
|
1832
|
+
|
|
1833
|
+
|
|
1834
|
+
def _left(name: str, ids: Sequence[int], r: Roster) -> str:
|
|
1835
|
+
team = ", ".join(m.name for m in r.members)
|
|
1836
|
+
if ids:
|
|
1837
|
+
return (
|
|
1838
|
+
f"{name} left; its task T{ids[0]} is back in Ready, possibly half-done. Team: {team}."
|
|
1839
|
+
)
|
|
1840
|
+
return f"{name} left. Team: {team}."
|
|
1841
|
+
|
|
1842
|
+
|
|
1843
|
+
def _leader_hears(r: Roster, view: ServerView | None, missed: Sequence[Notice]) -> str:
|
|
1844
|
+
"""Whether the notice reached the leader, as its delivery says. A notice is sent once and
|
|
1845
|
+
never resent, so the owner is told which is true and can say it themselves."""
|
|
1846
|
+
if not any(n.to == "leader" for n in missed):
|
|
1847
|
+
return "The leader was told."
|
|
1848
|
+
leader = r.by_id("leader")
|
|
1849
|
+
if view is None or not any(p.member == "leader" and not p.dead for p in view.panes):
|
|
1850
|
+
return NOT_TOLD.format(why="it is not running")
|
|
1851
|
+
if leader is None or leader.inbox is None:
|
|
1852
|
+
return NOT_TOLD.format(why="it is still starting")
|
|
1853
|
+
return NOT_TOLD.format(why="it did not answer")
|
|
1854
|
+
|
|
1855
|
+
|
|
1856
|
+
def _exec(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
1857
|
+
"""A member pane's own command: become that member's harness."""
|
|
1858
|
+
key, mid = argv[0], argv[1]
|
|
1859
|
+
# Only as the pane the tool itself started: without this a worker could run the leader's
|
|
1860
|
+
# session, with the leader's note and flags, in its own pane, and a path in place of a key
|
|
1861
|
+
# would build that session from any state directory (`team_dir` joins the two).
|
|
1862
|
+
if (key, mid) != (who.key, who.id):
|
|
1863
|
+
raise Refused(NOT_YOUR_PANE)
|
|
1864
|
+
d = team_dir(key, env)
|
|
1865
|
+
r = load(d)
|
|
1866
|
+
m = r.by_id(mid) if r is not None else None
|
|
1867
|
+
if r is None or m is None:
|
|
1868
|
+
raise StateError(f"{shown(d)} has no member {mid}.")
|
|
1869
|
+
h = harness.get(m.harness)
|
|
1870
|
+
cmd = h.argv(r, m, note(d, mid), d / "settings.json")
|
|
1871
|
+
# Made here, from the environment tmux gave the pane, and never passed by the command that
|
|
1872
|
+
# started it: that command's shell (a member's sandbox, another tmux) is not the member's.
|
|
1873
|
+
env = h.env(env, _tmpdir(env, key, mid))
|
|
1874
|
+
os.execvpe(cmd[0], cmd, env) # noqa: S606 the harness's argv
|
|
1875
|
+
|
|
1876
|
+
|
|
1877
|
+
def _boots(server: Server) -> None:
|
|
1878
|
+
"""A command about to boot the team's server: the server keeps this command's PATH, and its
|
|
1879
|
+
members call `workcrew` by name on it, so the first one there must be this tool."""
|
|
1880
|
+
if server.alive():
|
|
1881
|
+
return
|
|
1882
|
+
tool = os.path.realpath(sys.argv[0])
|
|
1883
|
+
found = shutil.which("workcrew")
|
|
1884
|
+
if found is None:
|
|
1885
|
+
raise Refused(
|
|
1886
|
+
"members find `workcrew` on your PATH, and there is none there; this is"
|
|
1887
|
+
f" {shown(Path(tool))}.\n"
|
|
1888
|
+
"Install it (uv tool install workcrew), or start the team with `uv run workcrew`."
|
|
1889
|
+
)
|
|
1890
|
+
if os.path.realpath(found) != tool:
|
|
1891
|
+
raise Refused(
|
|
1892
|
+
f"members find `workcrew` on your PATH, and the first there is {shown(Path(found))},"
|
|
1893
|
+
f" not this one, {shown(Path(tool))}.\n"
|
|
1894
|
+
"Make this one the first `workcrew` on your PATH, or start the team with"
|
|
1895
|
+
" `uv run workcrew`."
|
|
1896
|
+
)
|
|
1897
|
+
|
|
1898
|
+
|
|
1899
|
+
def _tool(caller: str, server: Server) -> str:
|
|
1900
|
+
"""The tool's path a command sets. Only the owner's command sets its own, so an upgrade
|
|
1901
|
+
reaches the team; a member's command keeps the one the server holds, whatever path the
|
|
1902
|
+
member ran it by."""
|
|
1903
|
+
return str(Path(sys.argv[0]).absolute()) if caller == "owner" else server.bin()
|
|
1904
|
+
|
|
1905
|
+
|
|
1906
|
+
def _cache(env: Mapping[str, str], key: str) -> Path:
|
|
1907
|
+
"""The folder that holds a team's members' TMPDIRs."""
|
|
1908
|
+
base = env.get("XDG_CACHE_HOME", "")
|
|
1909
|
+
root = Path(base) if Path(base).is_absolute() else Path(env.get("HOME", "")) / ".cache"
|
|
1910
|
+
if not root.is_absolute():
|
|
1911
|
+
raise StateError(
|
|
1912
|
+
"HOME is not an absolute path, so workcrew cannot find the members' TMPDIRs.\n"
|
|
1913
|
+
"Set HOME, or XDG_CACHE_HOME, to an absolute path."
|
|
1914
|
+
)
|
|
1915
|
+
return root / "workcrew" / key
|
|
1916
|
+
|
|
1917
|
+
|
|
1918
|
+
def _clear(cache: Path, d: Path) -> str:
|
|
1919
|
+
"""Removes the members' TMPDIRs, and says what it left. Forgetting the state is the command
|
|
1920
|
+
and this is clean-up after it, so nothing that goes wrong here stops the forgetting."""
|
|
1921
|
+
real, held = os.path.realpath(cache), os.path.realpath(d)
|
|
1922
|
+
if held == real or held.startswith(real + os.sep):
|
|
1923
|
+
return "Its members' TMPDIRs were not removed: their folder holds the team's state.\n"
|
|
1924
|
+
if not os.path.lexists(cache):
|
|
1925
|
+
return ""
|
|
1926
|
+
try:
|
|
1927
|
+
shutil.rmtree(cache)
|
|
1928
|
+
except OSError as exc: # a link, a read-only folder inside, a process still writing there
|
|
1929
|
+
# The system's reason when it gives one; an exception's own text is not for the owner.
|
|
1930
|
+
why = f": {exc.strerror}" if exc.strerror else ""
|
|
1931
|
+
return f"Its members' TMPDIRs are left at {shown(cache)}{why}.\n"
|
|
1932
|
+
return ""
|
|
1933
|
+
|
|
1934
|
+
|
|
1935
|
+
def _tmpdir(env: Mapping[str, str], key: str, mid: str) -> Path:
|
|
1936
|
+
"""Each member's own TMPDIR, so no two members share scratch files."""
|
|
1937
|
+
d = _cache(env, key) / mid
|
|
1938
|
+
d.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
1939
|
+
return d
|
|
1940
|
+
|
|
1941
|
+
|
|
1942
|
+
def _hello(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
1943
|
+
"""A member's session has started: record how to reach it, and tell who must know."""
|
|
1944
|
+
c = _hidden("_hello", env, who)
|
|
1945
|
+
d, server, caller = c.d, c.server, c.caller
|
|
1946
|
+
said = harness.get("claude").hello(sys.stdin.buffer.read(), env)
|
|
1947
|
+
notices: list[Notice] = []
|
|
1948
|
+
with _held(c) as held:
|
|
1949
|
+
r = held.r
|
|
1950
|
+
me = r.by_id(caller)
|
|
1951
|
+
if me is None:
|
|
1952
|
+
raise Refused(REMOVED)
|
|
1953
|
+
# Each Claude Code process has its own inbox, and /clear keeps it: a new one is a new
|
|
1954
|
+
# process, so a registered member that reports one has started again.
|
|
1955
|
+
back = me.inbox is not None and said.inbox != me.inbox
|
|
1956
|
+
me.session_id = said.session_id # a cleared session is a new one
|
|
1957
|
+
me.inbox = said.inbox
|
|
1958
|
+
save(d, r)
|
|
1959
|
+
view = server.view() # its own read: the members just started are in no earlier one
|
|
1960
|
+
task = board.held(d, me.id) if me.id != "leader" and back else None
|
|
1961
|
+
if task is not None:
|
|
1962
|
+
# Nothing else tells the leader a worker is back, so it goes on waiting on its task.
|
|
1963
|
+
# A worker with none keeps nobody waiting: Ready reaches it unasked.
|
|
1964
|
+
notices.append(Notice("leader", f"{me.name} is back; it holds T{task.id}."))
|
|
1965
|
+
notices += _bring_up(d, r, me, back, view)
|
|
1966
|
+
notices += reconcile.reconcile(d, r, server)
|
|
1967
|
+
notify(r, notices)
|
|
1968
|
+
return Result("", "", 0)
|
|
1969
|
+
|
|
1970
|
+
|
|
1971
|
+
def _idle(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
1972
|
+
"""A member's turn ends. A worker with work to do is sent back to it: to its Running task once
|
|
1973
|
+
per task, or to Ready once per turn; every other stop marks the pane idle. A worker waiting for
|
|
1974
|
+
its own subagent is let go: Claude Code wakes it when that ends. One holding a task with other
|
|
1975
|
+
background work running is let go too, and the leader is told once per task: a dev server
|
|
1976
|
+
never ends."""
|
|
1977
|
+
c = _hidden("_idle", env, who)
|
|
1978
|
+
h = harness.get("claude")
|
|
1979
|
+
stdin = sys.stdin.buffer.read()
|
|
1980
|
+
failed = list(argv) == ["failed"] # the StopFailure hook's: the turn ended on an error
|
|
1981
|
+
reason = ""
|
|
1982
|
+
told: list[Notice] = []
|
|
1983
|
+
with _held(c) as held:
|
|
1984
|
+
bg = h.background(stdin)
|
|
1985
|
+
worker = c.caller not in ("owner", "leader") and bg != "wakes"
|
|
1986
|
+
if worker and not failed:
|
|
1987
|
+
t = board.held(c.d, c.caller)
|
|
1988
|
+
if t is not None and bg == "runs":
|
|
1989
|
+
if board.wait_once(c.d, t):
|
|
1990
|
+
told = [Notice("leader", _running(held.r.names()[c.caller], t))]
|
|
1991
|
+
elif t is not None and board.refuse_once(c.d, t):
|
|
1992
|
+
reason = (
|
|
1993
|
+
f'You hold T{t.id} "{t.title}" and it is Running: work on it, or report it'
|
|
1994
|
+
" blocked."
|
|
1995
|
+
)
|
|
1996
|
+
elif t is not None and board.tell_once(c.d, t):
|
|
1997
|
+
# Sent back once already. Let go this time, it sits idle still holding the task,
|
|
1998
|
+
# and only the leader can ask it what it needs or put the task back.
|
|
1999
|
+
told = [Notice("leader", _stalled(held.r.names()[c.caller], t))]
|
|
2000
|
+
elif t is None and not h.stopping(stdin) and (n := board.counts(c.d)["ready"]):
|
|
2001
|
+
reason = board.ready_text(n)
|
|
2002
|
+
ready = sorted(t.id for t in board.tasks(c.d) if t.state == "ready")
|
|
2003
|
+
stuck = worker and (failed or h.stopping(stdin)) and board.held(c.d, c.caller) is None
|
|
2004
|
+
if stuck and ready:
|
|
2005
|
+
told.append(Notice("leader", _untaken(held.r.names()[c.caller], ready, failed)))
|
|
2006
|
+
# The caller's own pane, from the view the lock read: a member's command can drop
|
|
2007
|
+
# TMUX_PANE, and the caller is then settled by what it descends from.
|
|
2008
|
+
panes = held.view.panes if held.view is not None else ()
|
|
2009
|
+
pane = next((p.id for p in panes if p.member == c.caller), None)
|
|
2010
|
+
if not reason and pane is not None:
|
|
2011
|
+
# Under the lock: whoever creates a task next waits for it and then reads the mark,
|
|
2012
|
+
# so a task made the moment after the count above cannot find this worker busy and
|
|
2013
|
+
# leave it unasked while it has already stopped.
|
|
2014
|
+
with contextlib.suppress(Gone):
|
|
2015
|
+
c.server.set_state(pane, "idle")
|
|
2016
|
+
notify(held.r, told)
|
|
2017
|
+
if reason:
|
|
2018
|
+
return Result(h.refuse(reason) + "\n", "", 0)
|
|
2019
|
+
return Result("", "", 0)
|
|
2020
|
+
|
|
2021
|
+
|
|
2022
|
+
def _running(name: str, t: board.Task) -> str:
|
|
2023
|
+
"""What the leader is told when a worker stops holding its task with its own background work,
|
|
2024
|
+
such as a server, still running."""
|
|
2025
|
+
return (
|
|
2026
|
+
f'{name} stopped, still holding T{t.id} "{t.title}" (Running), while its own background'
|
|
2027
|
+
" work runs; it is woken if that work ends."
|
|
2028
|
+
)
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
def _untaken(name: str, ready: Sequence[int], failed: bool) -> str:
|
|
2032
|
+
"""What the leader is told when a worker that holds nothing ends a turn it is not sent back
|
|
2033
|
+
from while Ready has tasks. Not another worker: two that do not take pass Ready back and
|
|
2034
|
+
forth without end (161 notices in 20 s with two fake workers)."""
|
|
2035
|
+
# Not "without taking": a worker sent back to Ready may take and report one, then stop again.
|
|
2036
|
+
how = "'s turn failed" if failed else " stopped again after being sent back once,"
|
|
2037
|
+
n, top = len(ready), ready[0]
|
|
2038
|
+
return (
|
|
2039
|
+
f"{name}{how} while it held no task, and Ready has {n} task{'s' * (n != 1)} (T{top})."
|
|
2040
|
+
f" To give T{top} to one worker, run `workcrew board back {top} --to WORKER`."
|
|
2041
|
+
)
|
|
2042
|
+
|
|
2043
|
+
|
|
2044
|
+
def _stalled(name: str, t: board.Task) -> str:
|
|
2045
|
+
"""What the leader is told when a worker stops a second time still holding its task."""
|
|
2046
|
+
return (
|
|
2047
|
+
f'{name} stopped while holding T{t.id} "{t.title}" (Running). If it asked you something,'
|
|
2048
|
+
" answer it; if not, ask what it needs."
|
|
2049
|
+
f" `workcrew board back {t.id}` returns the task to Ready."
|
|
2050
|
+
)
|
|
2051
|
+
|
|
2052
|
+
|
|
2053
|
+
def _bring_up(d: Path, r: Roster, me: Member, back: bool, view: ServerView) -> list[Notice]:
|
|
2054
|
+
"""What a member is told at the start of a session; only one started again is back. The
|
|
2055
|
+
poller tells a member of a linked message once, so one that arrived while this member was
|
|
2056
|
+
away was never said: the count goes last, after whatever it must do."""
|
|
2057
|
+
said = _at_hand(d, r, me, back, view)
|
|
2058
|
+
task = board.held(d, me.id)
|
|
2059
|
+
# A task's text is sent once, when it is given, to the inbox on record then: a session not yet
|
|
2060
|
+
# up, or since restarted, never got it. A task with no brief and no report is its title.
|
|
2061
|
+
text = board.task_text(d, task.id, me.id) if task is not None else ""
|
|
2062
|
+
told = (
|
|
2063
|
+
[Notice(me.id, text, "leader")] if task is not None and text.strip() != task.title else []
|
|
2064
|
+
)
|
|
2065
|
+
if n := link.unread(d, me.id):
|
|
2066
|
+
count = f"{n} unread link message{'s' * (n != 1)}. {link.READ_HINT}"
|
|
2067
|
+
if told:
|
|
2068
|
+
told.append(Notice(me.id, count))
|
|
2069
|
+
else:
|
|
2070
|
+
said = f"{said} {count}".lstrip()
|
|
2071
|
+
return ([Notice(me.id, said)] if said else []) + told
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
def _at_hand(d: Path, r: Roster, me: Member, back: bool, view: ServerView) -> str:
|
|
2075
|
+
if me.id == "leader":
|
|
2076
|
+
return _leader_sees(d, r, view, back)
|
|
2077
|
+
task = board.held(d, me.id)
|
|
2078
|
+
if task is not None:
|
|
2079
|
+
head = "You are back, and you still hold" if back else "You hold"
|
|
2080
|
+
return f'{head} T{task.id} "{task.title}". Carry on with it.'
|
|
2081
|
+
n = board.counts(d)["ready"]
|
|
2082
|
+
take = board.ready_text(n) if n else ""
|
|
2083
|
+
# With nothing to take, a notice would start a turn that only acknowledges it.
|
|
2084
|
+
return f"You are back. {take}" if back and take else take
|
|
2085
|
+
|
|
2086
|
+
|
|
2087
|
+
def _leader_sees(d: Path, r: Roster, view: ServerView, back: bool) -> str:
|
|
2088
|
+
c = board.counts(d)
|
|
2089
|
+
line = f"Ready {c['ready']}, Running {c['running']}, Review {c['review']}."
|
|
2090
|
+
was = reconcile.activity(view, r)
|
|
2091
|
+
# A member with no pane here has still run if it ever said hello: the started marks belong
|
|
2092
|
+
# to the server, so a team brought back on a new one shows as never started. One that has
|
|
2093
|
+
# not said hello is starting, not down.
|
|
2094
|
+
down = [
|
|
2095
|
+
m
|
|
2096
|
+
for m in r.members
|
|
2097
|
+
if (a := was.get(m.id)) == "exited" or (a == "not-started" and m.inbox is not None)
|
|
2098
|
+
]
|
|
2099
|
+
if not (c["ready"] or c["running"] or c["review"] or down):
|
|
2100
|
+
return "" # a notice starts a turn, and here the leader would only acknowledge it
|
|
2101
|
+
if back and not down:
|
|
2102
|
+
team = ", ".join(m.name for m in r.members)
|
|
2103
|
+
line = f"The team is back with the same sessions: {team}. {line}"
|
|
2104
|
+
for m in down:
|
|
2105
|
+
task = board.held(d, m.id)
|
|
2106
|
+
line += f" {m.name} is EXITED; it holds {f'T{task.id}' if task else 'no task'}."
|
|
2107
|
+
return line
|
|
2108
|
+
|
|
2109
|
+
|
|
2110
|
+
def _reconcile(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
2111
|
+
"""A tmux hook: bring the panes back to the roster and tell what changed."""
|
|
2112
|
+
c = _hidden("_reconcile", env, who)
|
|
2113
|
+
size = _attached(argv)
|
|
2114
|
+
with _held(c) as h:
|
|
2115
|
+
# The first attach after a grid laid out or an arrangement restored with no client does
|
|
2116
|
+
# it again at the client's size; any later attach leaves the owner's arrangement alone.
|
|
2117
|
+
how: Lay = (c.server.take_fresh() if size is not None else None) or "join"
|
|
2118
|
+
# A member added since this process started is not a stray: reconcile would kill its
|
|
2119
|
+
# pane.
|
|
2120
|
+
notices = reconcile.reconcile(c.d, h.r, c.server, how, size)
|
|
2121
|
+
notify(h.r, notices)
|
|
2122
|
+
return Result("", "", 0)
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
def _attached(argv: Sequence[str]) -> tuple[int, int] | None:
|
|
2126
|
+
"""`attached W H` from the attach hook: the client's size, less the status line's one row."""
|
|
2127
|
+
if argv[:1] == ["attached"] and len(argv) == 3 and argv[1].isdigit() and argv[2].isdigit():
|
|
2128
|
+
return int(argv[1]), int(argv[2]) - 1
|
|
2129
|
+
return None
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
def _prompt(argv: Sequence[str], env: Mapping[str, str], who: Caller) -> Result:
|
|
2133
|
+
# The popup inherits the active pane's TMUX_PANE; without it the owner's line is the owner's,
|
|
2134
|
+
# and so is every command it runs.
|
|
2135
|
+
here = {k: v for k, v in env.items() if k != "TMUX_PANE"}
|
|
2136
|
+
d, r = _open(here, Caller(who.key, "owner"), argv)
|
|
2137
|
+
server = Server(r.key)
|
|
2138
|
+
code = prompt.main(
|
|
2139
|
+
d / "history",
|
|
2140
|
+
lambda line: typed_run(line, here, r.cwd),
|
|
2141
|
+
server.show,
|
|
2142
|
+
server.esc_guard,
|
|
2143
|
+
)
|
|
2144
|
+
return Result("", "", code)
|
|
2145
|
+
|
|
2146
|
+
|
|
2147
|
+
def _hidden(name: str, env: Mapping[str, str], who: Caller) -> Ctx:
|
|
2148
|
+
"""A hook's or a pane's own command, as a command on a team, so that it takes the lock by
|
|
2149
|
+
the one entry. Only the team's key is taken from the roster read here, and a team's key
|
|
2150
|
+
never changes."""
|
|
2151
|
+
d, r = _open(env, who, [name])
|
|
2152
|
+
return Ctx(Call(name), d, Server(r.key), who.id, env, False, who.key)
|
|
2153
|
+
|
|
2154
|
+
|
|
2155
|
+
def _open(env: Mapping[str, str], who: Caller, argv: Sequence[str]) -> tuple[Path, Roster]:
|
|
2156
|
+
"""The team this command is for, and its roster."""
|
|
2157
|
+
# Every caller refuses one in no team before it gets here, in the reader's own words.
|
|
2158
|
+
d = team_dir(cast(str, who.key), env)
|
|
2159
|
+
r = load(d)
|
|
2160
|
+
if r is None:
|
|
2161
|
+
raise Refused(FORGOTTEN)
|
|
2162
|
+
if who.id != "owner":
|
|
2163
|
+
_outside_the_sandbox(d, argv)
|
|
2164
|
+
return d, r
|
|
2165
|
+
|
|
2166
|
+
|
|
2167
|
+
def _outside_the_sandbox(d: Path, argv: Sequence[str]) -> None:
|
|
2168
|
+
"""A member's `workcrew` chained with another command runs inside Claude Code's sandbox,
|
|
2169
|
+
where the team's state cannot be written. Refuse before anything is delivered or changed;
|
|
2170
|
+
create nothing."""
|
|
2171
|
+
try:
|
|
2172
|
+
os.close(os.open(d / "lock", os.O_RDWR))
|
|
2173
|
+
except OSError as e:
|
|
2174
|
+
if e.errno in _READ_ONLY:
|
|
2175
|
+
raise Refused(
|
|
2176
|
+
SANDBOXED_BY_BACKTICK if any("`" in a for a in argv) else SANDBOXED
|
|
2177
|
+
) from e
|
|
2178
|
+
|
|
2179
|
+
|
|
2180
|
+
def _teams(env: Mapping[str, str]) -> list[Roster]:
|
|
2181
|
+
"""Every team on this host, by the state each one left behind. A state home shared
|
|
2182
|
+
between machines holds other hosts' teams, whose folders and sockets are not here."""
|
|
2183
|
+
out = []
|
|
2184
|
+
here = gethostname()
|
|
2185
|
+
root = team_dir("x", env).parent
|
|
2186
|
+
for d in sorted(root.iterdir()) if root.is_dir() else []:
|
|
2187
|
+
# One unreadable team does not hide the others; the command that opens it says why.
|
|
2188
|
+
with contextlib.suppress(StateError):
|
|
2189
|
+
r = load(d)
|
|
2190
|
+
# A forgotten team's folder keeps its roster under another name.
|
|
2191
|
+
if r is not None and r.host == here and d.name == r.key:
|
|
2192
|
+
out.append(r)
|
|
2193
|
+
return out
|
|
2194
|
+
|
|
2195
|
+
|
|
2196
|
+
def _outside(argv: Sequence[str], env: Mapping[str, str]) -> Refused:
|
|
2197
|
+
"""A command on a team, from a shell in none: a folder may hold several teams, so it names
|
|
2198
|
+
none, and the lines name this folder's. Each line is the whole command as typed: with the
|
|
2199
|
+
word alone, `add 2` became a line that adds one."""
|
|
2200
|
+
word, here = argv[0], Path.cwd()
|
|
2201
|
+
names = [r.name for r in _teams(env) if r.cwd == str(here)]
|
|
2202
|
+
if not names:
|
|
2203
|
+
return Refused(
|
|
2204
|
+
f"{word} acts on one team, and this shell is in none;"
|
|
2205
|
+
f" {_folder(env, here)} holds no team.\nRun workcrew -r to see your teams."
|
|
2206
|
+
)
|
|
2207
|
+
lines = "".join(f"\n workcrew -r {name} {shlex.join(argv)}" for name in names)
|
|
2208
|
+
return _Worded(f"{word} acts on one team, and this shell is in none. Name it:{lines}")
|
|
2209
|
+
|
|
2210
|
+
|
|
2211
|
+
def _listing(teams: Sequence[Roster]) -> str:
|
|
2212
|
+
return "".join(f"{r.name} {shown(Path(r.cwd))} {r.key}\n" for r in teams)
|
|
2213
|
+
|
|
2214
|
+
|
|
2215
|
+
def _named(want: str, env: Mapping[str, str]) -> Roster:
|
|
2216
|
+
"""The one team on this host that a name or a key names."""
|
|
2217
|
+
teams = _teams(env)
|
|
2218
|
+
hit = [r for r in teams if want in (r.name, r.key)]
|
|
2219
|
+
if not hit:
|
|
2220
|
+
_unreadable(want, env)
|
|
2221
|
+
raise Refused(f"no team named {want} on this host.\n{_listing(teams).rstrip()}")
|
|
2222
|
+
if len(hit) > 1:
|
|
2223
|
+
where = ", ".join(f"{r.key} ({shown(Path(r.cwd))})" for r in hit)
|
|
2224
|
+
raise Refused(f"{want!r} matches {len(hit)} teams: {where}. Pass one of these keys.")
|
|
2225
|
+
return hit[0]
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
def _unreadable(want: str | None, env: Mapping[str, str]) -> None:
|
|
2229
|
+
"""The team named, or with no name any team, which the list passed over because it cannot be
|
|
2230
|
+
read: a roster from a newer workcrew, or one that does not parse. Its own refusal says why;
|
|
2231
|
+
"no team named", or "no team yet", would tell the owner that a team which is there, intact,
|
|
2232
|
+
is gone."""
|
|
2233
|
+
root = team_dir("x", env).parent
|
|
2234
|
+
# The root also holds its own lock file, which is no team. With no name, only a folder named
|
|
2235
|
+
# as a team's key, and holding a roster, can be one: a forgotten team's, a stray one or one
|
|
2236
|
+
# made by hand is not, as `-r NAME` answers for a folder with no roster.
|
|
2237
|
+
for d in [d for d in sorted(root.iterdir()) if d.is_dir()] if root.is_dir() else []:
|
|
2238
|
+
if want is None and not (re.fullmatch(KEY, d.name) and (d / "roster.json").exists()):
|
|
2239
|
+
continue
|
|
2240
|
+
try:
|
|
2241
|
+
got = identity(d)
|
|
2242
|
+
except StateError:
|
|
2243
|
+
# No name to read. The folder's name is the key, made from the name's slug.
|
|
2244
|
+
if want is None or want == d.name or d.name.rpartition("-")[0] == slug(want):
|
|
2245
|
+
load(d)
|
|
2246
|
+
continue
|
|
2247
|
+
if got is not None and got[2] == gethostname() and want in (None, got[0], d.name):
|
|
2248
|
+
load(d)
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
def _choose(teams: Sequence[Roster], env: Mapping[str, str]) -> str | None:
|
|
2252
|
+
"""The key of the team picked on the terminal: a name can move to another team while the
|
|
2253
|
+
picker is open, a key never does. This folder's teams come first, so the picker opens on the
|
|
2254
|
+
first of them."""
|
|
2255
|
+
here = {key for key, _ in _here(env, Path.cwd())}
|
|
2256
|
+
ordered = sorted(teams, key=lambda r: r.key not in here)
|
|
2257
|
+
rows = [(escape(r.name), escape(_folder(env, Path(r.cwd))), _crew(r)) for r in ordered]
|
|
2258
|
+
i = picker.pick(rows)
|
|
2259
|
+
return None if i is None else ordered[i].key
|
|
2260
|
+
|
|
2261
|
+
|
|
2262
|
+
def _elsewhere(call: Call, env: Mapping[str, str], who: Caller) -> Result:
|
|
2263
|
+
"""`-r`: the teams on this host, or the one named, from any folder."""
|
|
2264
|
+
if not call.args:
|
|
2265
|
+
teams = _teams(env)
|
|
2266
|
+
if not teams:
|
|
2267
|
+
_unreadable(None, env)
|
|
2268
|
+
terminal = os.isatty(0) and os.isatty(1)
|
|
2269
|
+
if not teams and terminal:
|
|
2270
|
+
# Someone who typed it from `claude -r` habit, before their first team: an empty
|
|
2271
|
+
# answer reads as if the command did nothing.
|
|
2272
|
+
return Result(NO_TEAM_YET, "", 0)
|
|
2273
|
+
# A choice brings a team back and attaches it, which needs a terminal and a caller in
|
|
2274
|
+
# no team: anywhere else the list is printed, and a script reads it as it always has.
|
|
2275
|
+
if who.key is not None or not teams or not terminal:
|
|
2276
|
+
return Result(_listing(teams), "", 0)
|
|
2277
|
+
chosen = _choose(teams, env)
|
|
2278
|
+
if chosen is None:
|
|
2279
|
+
return Result("", "", 1) # nothing was brought back
|
|
2280
|
+
call = replace(call, args=(chosen,))
|
|
2281
|
+
if who.key is not None:
|
|
2282
|
+
# It starts or attaches a team: at `C-]` the attach is dropped, and from a shell inside
|
|
2283
|
+
# a team one forgotten word after the name would start that team's sessions.
|
|
2284
|
+
raise Refused(NAMED_INSIDE.format(name=call.args[0]))
|
|
2285
|
+
r = _named(call.args[0], env)
|
|
2286
|
+
d = team_dir(r.key, env)
|
|
2287
|
+
reconcile.need_folder(d, r)
|
|
2288
|
+
check_version()
|
|
2289
|
+
_gate(call)
|
|
2290
|
+
_boots(Server(r.key))
|
|
2291
|
+
with locked(d):
|
|
2292
|
+
# The listing above was read without the lock: everything this command changes or
|
|
2293
|
+
# prints comes from the roster as it is now.
|
|
2294
|
+
now = fresh(d)
|
|
2295
|
+
flags = _flags(d, now, call)
|
|
2296
|
+
result, notices = _run_team(call, env, d, now, "", flags, now.name)
|
|
2297
|
+
notify(now, notices)
|
|
2298
|
+
return result
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
# the commands that open no team, and the ones that act on the team they are run in
|
|
2302
|
+
ALONE: dict[str, Callable[[Call, Mapping[str, str], Caller], Result]] = {
|
|
2303
|
+
"": _start,
|
|
2304
|
+
"-r": _elsewhere,
|
|
2305
|
+
}
|
|
2306
|
+
ON_A_TEAM: dict[str, Callable[[Ctx], Result]] = {
|
|
2307
|
+
"add": _add,
|
|
2308
|
+
"board": _board_cmd,
|
|
2309
|
+
"exit": _exit,
|
|
2310
|
+
"layout": _layout,
|
|
2311
|
+
"link": _link_cmd,
|
|
2312
|
+
"remove": _remove,
|
|
2313
|
+
"rename": _rename_cmd,
|
|
2314
|
+
"resume": _resume,
|
|
2315
|
+
"send": _send_cmd,
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
def main() -> NoReturn:
|
|
2320
|
+
result = run(sys.argv[1:], os.environ)
|
|
2321
|
+
sys.stdout.write(result.out)
|
|
2322
|
+
sys.stderr.write(result.err)
|
|
2323
|
+
sys.stdout.flush()
|
|
2324
|
+
if result.attach is not None:
|
|
2325
|
+
# attach replaces this process, so it comes after the result is printed. A server that
|
|
2326
|
+
# ended meanwhile leaves nothing to attach to, and the result stands.
|
|
2327
|
+
with contextlib.suppress(Gone):
|
|
2328
|
+
result.attach.attach()
|
|
2329
|
+
sys.exit(result.code)
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
HIDDEN: dict[str, Callable[[Sequence[str], Mapping[str, str], Caller], Result]] = {
|
|
2333
|
+
"_exec": _exec,
|
|
2334
|
+
"_line": _line,
|
|
2335
|
+
"_linkwatch": _linkwatch,
|
|
2336
|
+
"_hello": _hello,
|
|
2337
|
+
"_idle": _idle,
|
|
2338
|
+
"_prompt": _prompt,
|
|
2339
|
+
"_reconcile": _reconcile,
|
|
2340
|
+
}
|