@rarog/server 1.0.1 → 1.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/node_modules/@rarog/shared/package.json +1 -1
- package/node_modules/@rarog/shared/src/decode.js +49 -3
- package/node_modules/@rarog/shared/src/protocol/files.js +33 -0
- package/node_modules/@rarog/shared/src/protocol/inbox.js +38 -0
- package/node_modules/@rarog/shared/src/protocol.js +16 -17
- package/node_modules/@rarog/shared/src/rest.js +106 -12
- package/node_modules/@rarog/shared/src/sentences/rest.js +13 -0
- package/package.json +3 -3
- package/server/src/bootstrap/consent.js +88 -2
- package/server/src/bootstrap/http.js +3 -2
- package/server/src/bootstrap/inbox.js +29 -4
- package/server/src/bootstrap/sse.js +2 -1
- package/server/src/cli/doctor-no-hook.js +162 -0
- package/server/src/cli/doctor.js +2 -0
- package/server/src/cli/update-channel.js +100 -0
- package/server/src/cli/update-registry.js +160 -62
- package/server/src/cli/update.js +13 -3
- package/server/src/companion/turns-settle.js +4 -1
- package/server/src/companion/turns.js +36 -5
- package/server/src/config/files.js +32 -0
- package/server/src/config/flags.js +10 -1
- package/server/src/config-env.js +24 -0
- package/server/src/config.js +6 -2
- package/server/src/demo/files.js +49 -0
- package/server/src/demo/inbox.js +73 -0
- package/server/src/demo/review-routes.js +6 -1
- package/server/src/device-effects.js +3 -1
- package/server/src/drivers/herdr/consent-no-hook-io.js +89 -0
- package/server/src/drivers/herdr/consent-no-hook.js +316 -0
- package/server/src/drivers/herdr/refusals.js +226 -0
- package/server/src/drivers/herdr/returned-prompt.js +172 -0
- package/server/src/drivers/herdr-driver.js +116 -287
- package/server/src/files/file-index.js +246 -0
- package/server/src/files/ignored.js +119 -0
- package/server/src/files/listing.js +76 -0
- package/server/src/files/path-rule.js +145 -0
- package/server/src/files/refusals.js +36 -0
- package/server/src/files/send-file.js +115 -0
- package/server/src/files/session-download.js +51 -0
- package/server/src/files/session-files.js +38 -0
- package/server/src/files/session-gate.js +48 -0
- package/server/src/git-status.js +3 -2
- package/server/src/herdr/audit.js +1 -1
- package/server/src/herdr/dialogs/hook/contract.js +16 -9
- package/server/src/herdr/dialogs/index.js +6 -1
- package/server/src/herdr/dialogs/layer1/hook-list/hook-list.js +132 -59
- package/server/src/herdr/dialogs/layer1/hook-list/session-facts.js +135 -0
- package/server/src/herdr/dialogs/layer1/version-gate.js +13 -5
- package/server/src/herdr/dialogs/no-hook.js +63 -0
- package/server/src/herdr/dialogs/screen/block.js +10 -0
- package/server/src/herdr/dialogs/screen/parse.js +10 -1
- package/server/src/herdr/jsonl-pending.js +25 -1
- package/server/src/inbox/card.js +18 -9
- package/server/src/inbox/dialogs.js +40 -12
- package/server/src/inbox/heads.js +55 -0
- package/server/src/inbox/inbox.js +24 -2
- package/server/src/inbox/index.js +3 -1
- package/server/src/index.js +22 -18
- package/server/src/install/repair.js +8 -0
- package/server/src/log-timestamps.js +9 -4
- package/server/src/router.js +7 -1
- package/server/src/routes/devices.js +2 -2
- package/server/src/routes/inbox.js +91 -0
- package/server/src/routes/sessions.js +135 -14
- package/server/src/routes/system.js +5 -4
- package/server/src/safe-read.js +86 -1
- package/server/src/sessions/companion-scope.js +6 -2
- package/server/src/sessions/demo-modules.js +16 -0
- package/server/src/sessions/diff-measure.js +30 -0
- package/server/src/sessions/manager.js +64 -152
- package/server/src/sessions/ring-buffer.js +18 -3
- package/server/src/sessions/seen-marks.js +9 -6
- package/server/src/sessions/session-scope.js +30 -0
- package/server/src/sessions/session.js +20 -1
- package/server/src/sessions/waiting-line.js +72 -0
- package/server/src/sse.js +40 -6
- package/server/src/version.js +30 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { HERDR_PANE_ROWS_WAIT_MS } from '../../config.js';
|
|
2
|
+
import { refused } from '../types.js';
|
|
3
|
+
import { STALE_DECISION_MESSAGE, TERMINAL_DIALOG_MESSAGE, admittedHook, blockVisible, dialogShapeOf, routeDialog, routeHookOf, routeScreenOf, } from '../../herdr/dialogs/index.js';
|
|
4
|
+
import { diagnosisFields, originFields } from './audit-fields.js';
|
|
5
|
+
import { layer2HoldsKeys } from './layer2-card.js';
|
|
6
|
+
/**
|
|
7
|
+
* Odmowa wysyłki cyfry: powiedz DLACZEGO i ODBUDUJ kartę.
|
|
8
|
+
*
|
|
9
|
+
* Karta gaśnie i ocena buduje ją od nowa z tego, co stoi TERAZ — decyzja nie wyszła, ale
|
|
10
|
+
* człowiek ma na czym pracować. To JEDYNA ścieżka odbudowy po odmowie
|
|
11
|
+
* decyzji: pending w managerze przeżywa `refused` (kontrakt `DecisionOutcome`), więc
|
|
12
|
+
* odbudowa nie musi już nadrabiać wpisu zjedzonego przez first-wins.
|
|
13
|
+
*/
|
|
14
|
+
export function refuseDecision(port, message, reason, audit, shape) {
|
|
15
|
+
auditRefusal(port, audit, port.activeReq()?.toolName, 'refuse', shape);
|
|
16
|
+
port.system(message);
|
|
17
|
+
port.clearActive(reason);
|
|
18
|
+
port.scheduleEvaluate();
|
|
19
|
+
return refused(audit);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Herdr odrzucił samo wywołanie — nic nie poszło do panelu.
|
|
23
|
+
*
|
|
24
|
+
* Karty NIE gasimy i oceny nie ponawiamy: dialog stoi dalej, a `#send` zdążył już
|
|
25
|
+
* powiedzieć człowiekowi, co się stało (blokada / panel zniknął / tryb tylko do odczytu).
|
|
26
|
+
* Wpis w dzienniku dokłada tę odmowę do licznika (`summarizeRefusals` w `herdr/audit.ts`) — bez niego
|
|
27
|
+
* najczęstsza awaria kanału nie miałaby w statystyce ani jednej linii.
|
|
28
|
+
*/
|
|
29
|
+
export function refuseSend(port) {
|
|
30
|
+
auditRefusal(port, 'send-failed', port.activeReq()?.toolName);
|
|
31
|
+
return refused('send-failed');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* `allow` over a layer 2 card (`layer2HoldsKeys`, `docs/dialog-card.md`): `terminal-only` with the sentence of the
|
|
35
|
+
* terminal dialogs, the card stays; a choice goes only through the layer 2 step. `null` = another card or none.
|
|
36
|
+
*/
|
|
37
|
+
export function layer2KeyRefusal(port) {
|
|
38
|
+
if (!layer2HoldsKeys(port.activeReq()))
|
|
39
|
+
return null;
|
|
40
|
+
auditRefusal(port, 'terminal-only', port.activeReq()?.toolName);
|
|
41
|
+
port.system(TERMINAL_DIALOG_MESSAGE);
|
|
42
|
+
return refused('terminal-only');
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Odpowiedź z NOTATKĄ nie wyszła — nic nie poszło ani do panelu, ani do pliku.
|
|
46
|
+
*
|
|
47
|
+
* Karty NIE gasimy, jak przy `refuseSend`: dialog stoi dalej, a człowiek ma na niej szkic
|
|
48
|
+
* z notatką (odmowa 422 zostawia kartę w stanie wyboru). Powód `send-failed` z zamkniętej
|
|
49
|
+
* listy znaczy dokładnie to: „nic nie doszło" — dokładniejsze zdanie idzie do transkryptu.
|
|
50
|
+
* `toolName` podaje wołający, gdy odpowiedź przyszła na inną kartę niż ta, która stoi.
|
|
51
|
+
*/
|
|
52
|
+
export function refuseNote(port, message, toolName = port.activeReq()?.toolName) {
|
|
53
|
+
auditRefusal(port, 'send-failed', toolName);
|
|
54
|
+
port.system(message);
|
|
55
|
+
return refused('send-failed');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Wpis „decyzja NIE wyszła" — jedyna metoda dziennika bez ani jednego bajtu do panelu.
|
|
59
|
+
*
|
|
60
|
+
* Powód jest z zamkniętej listy, bo te wpisy mają się SUMOWAĆ (`summarizeRefusals` w `herdr/audit.ts`),
|
|
61
|
+
* a nie opisywać: zdanie dla człowieka i tak idzie do transkryptu osobno. Nazwa narzędzia
|
|
62
|
+
* jedzie z kartą — bez niej „18 odmów" nie mówi, czego dotyczyły.
|
|
63
|
+
*/
|
|
64
|
+
export function auditRefusal(port, reason, toolName, method = 'refuse', shape, degrade) {
|
|
65
|
+
const at = port.deps.now();
|
|
66
|
+
// The class from the CLI registry when it says `waiting`: the counter must tell WHICH dialogs
|
|
67
|
+
// Rarog cannot read, and the router verdict (`routeFields`) uses it as input.
|
|
68
|
+
const cliDialog = port.cliWaiting();
|
|
69
|
+
const hook = readHookQuietly(port);
|
|
70
|
+
/*
|
|
71
|
+
* KSZTAŁT przypadku. Karta bez decyzji niesie go z oceny (`decideCard` liczy go z tego
|
|
72
|
+
* samego odczytu ekranu, z którego powstała karta, razem z podpowodem sprzed spadku na ścieżkę
|
|
73
|
+
* ekranową). Odmowa wysyłki — ze stojącej karty i z ostatniego odczytu ekranu, tego samego, który
|
|
74
|
+
* trafia do zrzutu odmowy.
|
|
75
|
+
*/
|
|
76
|
+
const caseShape = {
|
|
77
|
+
...(degrade === undefined
|
|
78
|
+
? {
|
|
79
|
+
...originFields(port.activeReq()?.reqId ?? null, port.activeReq()?.origin),
|
|
80
|
+
...diagnosisFields({ mismatchReason: null, ...dialogShapeOf(port.screenText()) }),
|
|
81
|
+
}
|
|
82
|
+
: { ...originFields(degrade.reqId, degrade.origin), ...diagnosisFields(degrade.diagnosis) }),
|
|
83
|
+
...routeFields(port, hook),
|
|
84
|
+
};
|
|
85
|
+
const entry = {
|
|
86
|
+
method,
|
|
87
|
+
reason,
|
|
88
|
+
...(toolName === undefined ? {} : { toolName }),
|
|
89
|
+
...(shape === undefined ? {} : { shape }),
|
|
90
|
+
...(cliDialog === null ? {} : { cliDialog }),
|
|
91
|
+
...caseShape,
|
|
92
|
+
};
|
|
93
|
+
const dump = refusalDump(port, reason, toolName, caseShape, cliDialog, hook, at);
|
|
94
|
+
const write = (rows) => {
|
|
95
|
+
const withRows = rows === null ? {} : { paneRows: rows };
|
|
96
|
+
port.audit({ ...entry, ...withRows }, at);
|
|
97
|
+
if (dump !== null)
|
|
98
|
+
writeDump(port, { ...dump, shape: { ...caseShape, ...withRows } });
|
|
99
|
+
};
|
|
100
|
+
/*
|
|
101
|
+
* Wiersze panelu z herdr WYŁĄCZNIE przy karcie bez decyzji: jeden `pane.list`, po nim
|
|
102
|
+
* wpis i zrzut. Karta nie czeka — wpis dochodzi do pliku z czasem oceny. Budżet czekania liczy
|
|
103
|
+
* DRIVER (`HERDR_PANE_ROWS_WAIT_MS`, łącznie z kolejką slotów gniazda): po nim wpis i zrzut idą
|
|
104
|
+
* bez wierszy, dokładnie raz, a spóźniony wynik przepada. Sufit gniazda zostaje zwykły — krótszy
|
|
105
|
+
* oznaczałby herdr jako niedostępny z powodu diagnostyki. Błąd odczytu
|
|
106
|
+
* albo brak metody = wpis bez wierszy, nigdy liczba zgadnięta z ekranu.
|
|
107
|
+
*/
|
|
108
|
+
const client = port.deps.client;
|
|
109
|
+
if (method !== 'degrade' || client.readPaneRows === undefined) {
|
|
110
|
+
write(null);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
let timer = null;
|
|
114
|
+
let written = false;
|
|
115
|
+
const once = (rows) => {
|
|
116
|
+
if (written)
|
|
117
|
+
return;
|
|
118
|
+
written = true;
|
|
119
|
+
if (timer !== null)
|
|
120
|
+
port.scheduler.clearTimeout(timer);
|
|
121
|
+
write(rows);
|
|
122
|
+
};
|
|
123
|
+
timer = port.scheduler.setTimeout(() => { once(null); }, port.deps.paneRowsWaitMs ?? HERDR_PANE_ROWS_WAIT_MS);
|
|
124
|
+
void client.readPaneRows(port.paneId()).then((rows) => { once(rows); }, () => { once(null); });
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Zrzut diagnostyczny do `<dataDir>/refusals/`
|
|
128
|
+
* („Refusal diagnostics” w `docs/dialogs.md`) — jedno wywołanie na jedną odmowę.
|
|
129
|
+
*
|
|
130
|
+
* Stoi w `auditRefusal`, bo to JEDYNY lejek odmów w driverze: każdy powód, który wchodzi
|
|
131
|
+
* do statystyki, zostawia też ekran, z którego powstał. Ekran bierzemy z cache'u ostatniego
|
|
132
|
+
* odczytu (`#screenText`) — świeżego, bo ocena dialogu czyta go tuż przed decyzją.
|
|
133
|
+
*
|
|
134
|
+
* Treść zrzutu składamy OD RAZU (ekran, zrzut hooka i wpis jsonl z chwili odmowy), a zapisujemy
|
|
135
|
+
* po odczycie wierszy panelu (`writeDump`) — w tym czasie kolejna ocena mogłaby podmienić cache.
|
|
136
|
+
*
|
|
137
|
+
* Ze zrzutu hooka jadą wyłącznie METADANE (zdarzenie, czas, narzędzie, a w kształcie przypadku
|
|
138
|
+
* identyfikatory tury i subagenta). `toolInput` NIE —
|
|
139
|
+
* on siedzi w `pending/` i nie ma powodu leżeć na dysku drugi raz.
|
|
140
|
+
*/
|
|
141
|
+
function refusalDump(port, reason, toolName, shape, cliDialog, hook, at) {
|
|
142
|
+
if (port.deps.dumpRefusal === undefined)
|
|
143
|
+
return null;
|
|
144
|
+
const pending = port.pending();
|
|
145
|
+
return {
|
|
146
|
+
reason,
|
|
147
|
+
toolName,
|
|
148
|
+
...(port.deps.sessionId === undefined ? {} : { sessionId: port.deps.sessionId }),
|
|
149
|
+
screen: port.screenText(),
|
|
150
|
+
hook: hook === null
|
|
151
|
+
? null
|
|
152
|
+
: {
|
|
153
|
+
toolName: hook.toolName,
|
|
154
|
+
at: hook.at,
|
|
155
|
+
...(hook.hookEventName === undefined ? {} : { hookEventName: hook.hookEventName }),
|
|
156
|
+
},
|
|
157
|
+
jsonl: pending === null ? null : { id: pending.id, toolName: pending.toolName },
|
|
158
|
+
shape,
|
|
159
|
+
...(cliDialog === null ? {} : { cliDialog }),
|
|
160
|
+
at,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function writeDump(port, dump) {
|
|
164
|
+
try {
|
|
165
|
+
port.deps.dumpRefusal?.(dump);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// Diagnostyka nie ma prawa wywrócić obsługi odmowy — cisza, jak przy dzienniku.
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Zrzut hooka tej sesji do dziennika; błąd czytnika znaczy „brak zrzutu”, nigdy wyjątek w obsłudze. Zrzut `Bash`
|
|
173
|
+
* starszy niż 10 minut liczy się tylko z dowodem z ostatniego odczytu ekranu (`admittedHook`),
|
|
174
|
+
* jak w ocenie: inaczej rozdzielacz widziałby zrzut, którego karta nie wzięła.
|
|
175
|
+
*/
|
|
176
|
+
function readHookQuietly(port) {
|
|
177
|
+
try {
|
|
178
|
+
const raw = port.deps.readPendingHook?.(port.agentSessionId()) ?? null;
|
|
179
|
+
return admittedHook(raw, port.screenText(), (text, expected) => port.resolveOptions(text, expected));
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Pola dziennika z rozdzielacza warstw i pełnego bloku (`approve`, `degrade`, `refuse`): werdykt
|
|
187
|
+
* `routeDialog` z klasy rejestru CLI, zrzutu hooka i ostatniego odczytu ekranu (`#screenText`, ten sam,
|
|
188
|
+
* który trafia do zrzutu odmowy) oraz `blockVisible` z tego odczytu. Wyłącznie dziennik — karta, przyciski
|
|
189
|
+
* i klawisze od tych pól nie zależą. Wyjątek parsera daje wpis bez tych pól, nigdy wstrzymaną wysyłkę.
|
|
190
|
+
*/
|
|
191
|
+
function routeFields(port, hook) {
|
|
192
|
+
try {
|
|
193
|
+
const text = port.screenText();
|
|
194
|
+
const { route, reason } = routeDialog({
|
|
195
|
+
cliDialog: port.cliWaiting(), hook: routeHookOf(hook), screen: routeScreenOf(text),
|
|
196
|
+
});
|
|
197
|
+
return { ...(text === null ? {} : { blockVisible: blockVisible(text) }), route, routeReason: reason };
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return {};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/** Kontekst rejestru i rozdzielacza do wpisu `approve`: klasa CLI (gdy `waiting`) i {@link routeFields}. */
|
|
204
|
+
export function approveContext(port) {
|
|
205
|
+
const cliDialog = port.cliWaiting();
|
|
206
|
+
return { ...(cliDialog === null ? {} : { cliDialog }), ...routeFields(port, readHookQuietly(port)) };
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Odpowiedź w inny dialog niż ten, który AKTUALNIE stoi — mówimy o tym GŁOŚNO.
|
|
210
|
+
*
|
|
211
|
+
* `active === null` to normalny wyścig (dialog właśnie rozstrzygnięto — decyzja spóźniona
|
|
212
|
+
* o ułamek sekundy, cisza jest poprawna). Ale odpowiedź adresująca INNY dialog znaczy, że
|
|
213
|
+
* telefon patrzył na nieświeżą kartę — a jej zjedzenie po cichu wyglądałoby jak „tapnąłem
|
|
214
|
+
* i nic".
|
|
215
|
+
*/
|
|
216
|
+
export function reportStaleDecision(port) {
|
|
217
|
+
auditRefusal(port, 'stale-card', port.activeReq()?.toolName);
|
|
218
|
+
port.system(STALE_DECISION_MESSAGE);
|
|
219
|
+
/*
|
|
220
|
+
* `refused`, nie `stale`: Rarog POKAZUJE dziś inną kartę niż ta, w którą tapnięto, więc
|
|
221
|
+
* karta, o której mówi żądanie, ma zniknąć razem ze swoim pendingiem — ale ta ŻYWA ma
|
|
222
|
+
* zostać. `stale` wygasiłoby cały pending sesji (410 + `expired`), czyli zdjęłoby także
|
|
223
|
+
* kartę, która mówi prawdę o ekranie.
|
|
224
|
+
*/
|
|
225
|
+
return refused('stale-card');
|
|
226
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { composerFieldView, squashWhitespace } from './commands.js';
|
|
2
|
+
import { PROMPT_FIELD_POLL_MS } from './prompt-paste.js';
|
|
3
|
+
import { screenHolds, showsTaken } from './take-back-field.js';
|
|
4
|
+
/**
|
|
5
|
+
* How long the field is read after a delivered `escape`. On CLI 2.1.329 Esc puts the interrupted prompt back into the
|
|
6
|
+
* field only while the turn has shown no text and no tool, the field is empty and the CLI queue is empty; herdr then
|
|
7
|
+
* reports `done` about 0.3 to 0.4 s after the key. Any other interrupt leaves the field as it was.
|
|
8
|
+
*/
|
|
9
|
+
export const RETURNED_PROMPT_WAIT_MS = 1_200;
|
|
10
|
+
/**
|
|
11
|
+
* How long the `escape` waits for the field read before it. A read that does not answer in time proves nothing and the
|
|
12
|
+
* key goes at once: the socket's own request timeout is longer than the wait of the interrupt route.
|
|
13
|
+
*/
|
|
14
|
+
export const FIELD_READ_BEFORE_ESCAPE_MS = 250;
|
|
15
|
+
export class ReturnedPrompt {
|
|
16
|
+
#deps;
|
|
17
|
+
#scheduler;
|
|
18
|
+
#key;
|
|
19
|
+
#port;
|
|
20
|
+
/** The prompt of the newest turn: the text Rarog sent to an idle pane, or the newest human prompt of the file. */
|
|
21
|
+
#turnPrompt = null;
|
|
22
|
+
/**
|
|
23
|
+
* Whether Rarog sent a prompt since the history read of the conversation file began, at construction or at a move to
|
|
24
|
+
* another conversation. That read can end after the send, and its newest prompt is then older than the sent one.
|
|
25
|
+
*/
|
|
26
|
+
#sentSinceHistory = false;
|
|
27
|
+
/** The answer of the last delivered `escape`. */
|
|
28
|
+
#answer = Promise.resolve(null);
|
|
29
|
+
/** The end condition of the read behind {@link #answer}; it also holds back the answer once settled. */
|
|
30
|
+
#stale = () => false;
|
|
31
|
+
constructor(config, port) {
|
|
32
|
+
this.#deps = config.deps;
|
|
33
|
+
this.#scheduler = config.scheduler;
|
|
34
|
+
this.#key = config.key;
|
|
35
|
+
this.#port = port;
|
|
36
|
+
}
|
|
37
|
+
/** The prompt that started the turn, as the driver sent it to an idle pane. */
|
|
38
|
+
noteSent(text) {
|
|
39
|
+
this.#note(text);
|
|
40
|
+
this.#sentSinceHistory = true;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The history of the conversation file, as {@link noteFile}, unless Rarog sent a prompt since the read began: no
|
|
44
|
+
* history entry is then newer than the sent prompt.
|
|
45
|
+
*/
|
|
46
|
+
noteHistory(entries) {
|
|
47
|
+
if (!this.#sentSinceHistory)
|
|
48
|
+
this.noteFile(entries);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Human prompts of the conversation file, in file order: the newest one is the turn's prompt. A harness notice, a
|
|
52
|
+
* command output and an entry with an image are no prompt the field could show as text.
|
|
53
|
+
*/
|
|
54
|
+
noteFile(entries) {
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
if (entry.role !== 'user' || entry.notice !== undefined || entry.commandOutput !== undefined)
|
|
57
|
+
continue;
|
|
58
|
+
const texts = entry.blocks.map((block) => (block.type === 'text' && !('ref' in block.text) ? block.text.text : null));
|
|
59
|
+
if (texts.length > 0 && texts.every((text) => text !== null))
|
|
60
|
+
this.#note(texts.join('\n'));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
#note(text) {
|
|
64
|
+
if (squashWhitespace(text) !== '')
|
|
65
|
+
this.#turnPrompt = text;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The pane moved to another conversation: its field holds none of the old conversation's prompts, and the answer of
|
|
69
|
+
* an `escape` in the old one answers no repeat in the new one. The new file's history read begins now.
|
|
70
|
+
*/
|
|
71
|
+
conversationChanged() {
|
|
72
|
+
this.#turnPrompt = null;
|
|
73
|
+
this.#sentSinceHistory = false;
|
|
74
|
+
this.#answer = Promise.resolve(null);
|
|
75
|
+
this.#stale = () => false;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The turn's prompt for the `escape` about to go, or `null` when no hand-back can be proven. `possible` is the
|
|
79
|
+
* driver's gate: a `claude` pane, no prompt in Rarog's queue and no unsettled Enter (the queue would run next), no
|
|
80
|
+
* field held by the take-back (it shows taken text), no dialog and no permission card (a tool has shown). Then one
|
|
81
|
+
* field read within {@link FIELD_READ_BEFORE_ESCAPE_MS}: Esc leaves a draft in place (CLI 2.1.329), so a field equal
|
|
82
|
+
* to the prompt after the key proves the hand-back only when the field was empty before it. An unreadable or late
|
|
83
|
+
* field proves nothing. The driver asks before its pause gate, so that gate stays the last check before the key.
|
|
84
|
+
*/
|
|
85
|
+
async beforeEscape(possible) {
|
|
86
|
+
if (!possible || this.#turnPrompt === null)
|
|
87
|
+
return null;
|
|
88
|
+
let timer = null;
|
|
89
|
+
const late = new Promise((resolve) => {
|
|
90
|
+
timer = this.#scheduler.setTimeout(() => { resolve(undefined); }, this.#deps.fieldReadBeforeEscapeMs ?? FIELD_READ_BEFORE_ESCAPE_MS);
|
|
91
|
+
});
|
|
92
|
+
const screen = await Promise.race([this.#screen(), late]);
|
|
93
|
+
if (timer !== null)
|
|
94
|
+
this.#scheduler.clearTimeout(timer);
|
|
95
|
+
const view = screen?.view ?? null;
|
|
96
|
+
if (screen === undefined || view === null || screenHolds(screen) || view.cutAtTop || view.history)
|
|
97
|
+
return null;
|
|
98
|
+
return squashWhitespace(view.text) === '' ? this.#turnPrompt : null;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The answer for the `escape` that just went: `expected` is the prompt of {@link beforeEscape}, or `null` when the
|
|
102
|
+
* key did not go. `stale` ends the read and holds back its answer: the driver is gone, a newer send or interrupt
|
|
103
|
+
* came, or the conversation changed.
|
|
104
|
+
*/
|
|
105
|
+
start(expected, stale) {
|
|
106
|
+
this.#stale = stale;
|
|
107
|
+
this.#answer = expected === null ? Promise.resolve(null) : this.#bounded(expected, stale);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The prompt the field showed after the last delivered `escape`, else `null`; settles within the read bound. A repeat
|
|
111
|
+
* of the request asks again without a new key, so `stale` is checked again when the answer is handed out.
|
|
112
|
+
*/
|
|
113
|
+
answer() {
|
|
114
|
+
const stale = this.#stale;
|
|
115
|
+
return this.#answer.then((found) => (found === null || stale() ? null : found));
|
|
116
|
+
}
|
|
117
|
+
/** The read raced against its bound, so a field read that never returns still answers in time. */
|
|
118
|
+
#bounded(expected, stale) {
|
|
119
|
+
let over = false;
|
|
120
|
+
let timer = null;
|
|
121
|
+
const bound = new Promise((resolve) => {
|
|
122
|
+
timer = this.#scheduler.setTimeout(() => {
|
|
123
|
+
over = true;
|
|
124
|
+
resolve(null);
|
|
125
|
+
}, this.#deps.returnedPromptWaitMs ?? RETURNED_PROMPT_WAIT_MS);
|
|
126
|
+
});
|
|
127
|
+
const read = this.#read({ expected: squashWhitespace(expected), collapsed: null }, () => over || stale());
|
|
128
|
+
return Promise.race([read, bound]).then((found) => {
|
|
129
|
+
over = true;
|
|
130
|
+
if (timer !== null)
|
|
131
|
+
this.#scheduler.clearTimeout(timer);
|
|
132
|
+
if (!found)
|
|
133
|
+
return null;
|
|
134
|
+
this.#deps.logger?.(`interrupt: pane ${this.#key} handed the interrupted prompt back to its field`);
|
|
135
|
+
return expected;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* `true` once the whole field, no dialog or form over it, equals the prompt. An empty field or the start of the
|
|
140
|
+
* prompt is read again; a whole field with other text is a draft, which Esc leaves in place (CLI 2.1.329). Each read
|
|
141
|
+
* waits one poll first: right at the key the field still shows what it held before Esc.
|
|
142
|
+
*/
|
|
143
|
+
async #read(own, done) {
|
|
144
|
+
for (;;) {
|
|
145
|
+
await new Promise((resolve) => {
|
|
146
|
+
this.#scheduler.setTimeout(() => { resolve(); }, this.#deps.promptFieldPollMs ?? PROMPT_FIELD_POLL_MS);
|
|
147
|
+
});
|
|
148
|
+
if (done())
|
|
149
|
+
return false;
|
|
150
|
+
const screen = await this.#screen();
|
|
151
|
+
if (done())
|
|
152
|
+
return false;
|
|
153
|
+
const view = screen?.view ?? null;
|
|
154
|
+
if (screen !== undefined && view !== null && !screenHolds(screen) && !view.cutAtTop && !view.history) {
|
|
155
|
+
const field = squashWhitespace(view.text);
|
|
156
|
+
if (showsTaken(own, field, false, false))
|
|
157
|
+
return true;
|
|
158
|
+
if (field !== '' && !showsTaken(own, field, false, true))
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async #screen() {
|
|
164
|
+
try {
|
|
165
|
+
const { text } = await this.#deps.client.readScreen(this.#port.paneId(), 'visible', 'ansi');
|
|
166
|
+
return { text, view: composerFieldView(text) };
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|