@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
package/server/src/inbox/card.js
CHANGED
|
@@ -57,12 +57,15 @@ export function composeCard(session, shown, requestOf = () => undefined) {
|
|
|
57
57
|
if (kind === null || shown === undefined)
|
|
58
58
|
return null;
|
|
59
59
|
const at = kind === 'result' ? shown.at : session.stateSince;
|
|
60
|
-
const
|
|
60
|
+
const decided = kind === 'decision' ? dialogOf(session, requestOf) : undefined;
|
|
61
|
+
const dialog = decided?.dialog;
|
|
61
62
|
const key = keyOf(shown.turnId, kind, dialog, at);
|
|
63
|
+
// The revision changes the version only: a removal of the card holds while its buttons change.
|
|
64
|
+
const revision = decided?.revision === undefined ? [] : [decided.revision];
|
|
62
65
|
return {
|
|
63
66
|
sessionId: session.id,
|
|
64
67
|
turnId: shown.turnId,
|
|
65
|
-
version: digest([shown.version, kind, key.dialog ?? '', key.dialogId ?? '', key.since ?? '']),
|
|
68
|
+
version: digest([shown.version, kind, key.dialog ?? '', key.dialogId ?? '', key.since ?? '', ...revision]),
|
|
66
69
|
kind,
|
|
67
70
|
state: session.state,
|
|
68
71
|
at,
|
|
@@ -98,21 +101,27 @@ function dialogIdOf(dialog) {
|
|
|
98
101
|
return digest([dialog.kind, 'request', dialog.requestId]);
|
|
99
102
|
return digest([dialog.kind, 'summary', JSON.stringify(dialog.waitingFor ?? null)]);
|
|
100
103
|
}
|
|
104
|
+
/** The dialog of a decision card, and the revision of its request's state and buttons when it has one. */
|
|
101
105
|
function dialogOf(session, requestOf) {
|
|
102
106
|
const waiting = session.waitingFor;
|
|
103
107
|
const kind = waiting?.kind ?? (session.state === 'waiting-question' ? 'question' : undefined);
|
|
104
108
|
if (kind === undefined) {
|
|
105
109
|
return {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
110
|
+
dialog: {
|
|
111
|
+
kind: 'panel',
|
|
112
|
+
...(session.herdrDialog === undefined ? {} : { herdrDialog: session.herdrDialog }),
|
|
113
|
+
...(session.herdrDialogReason === undefined ? {} : { herdrDialogReason: session.herdrDialogReason }),
|
|
114
|
+
},
|
|
109
115
|
};
|
|
110
116
|
}
|
|
111
|
-
const
|
|
117
|
+
const request = requestOf(kind, waiting);
|
|
112
118
|
return {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
dialog: {
|
|
120
|
+
kind,
|
|
121
|
+
...(waiting === undefined ? {} : { waitingFor: waiting }),
|
|
122
|
+
...(request === undefined ? {} : { requestId: request.id }),
|
|
123
|
+
},
|
|
124
|
+
...(request?.revision === undefined ? {} : { revision: request.revision }),
|
|
116
125
|
};
|
|
117
126
|
}
|
|
118
127
|
/** Opaque hash of the parts; the JSON array keeps the parts apart. */
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* The dialog requests each session waits on, folded from the session manager's events (`permission-pending`,
|
|
3
3
|
* `permission-settled`, `permission-resolved`, `question-pending`, `question-resolved`): which request a decision card
|
|
4
4
|
* stands for. The manager keeps its queue of requests to itself and its transcript read marks consent cards as shown
|
|
5
|
-
* on a device, so the Inbox folds the events every device gets. It keeps request ids, states and a digest of
|
|
6
|
-
* session summary's `waitingFor` would say of each request, never their text.
|
|
5
|
+
* on a device, so the Inbox folds the events every device gets. It keeps request ids, states, buttons and a digest of
|
|
6
|
+
* what the session summary's `waitingFor` would say of each request, never their text.
|
|
7
7
|
*/
|
|
8
8
|
import { createHash } from 'node:crypto';
|
|
9
9
|
import { EXIT_PLAN_MODE_TOOL, WAITING_TEXT_MAX } from '@rarog/shared';
|
|
@@ -17,18 +17,31 @@ export class PendingDialogs {
|
|
|
17
17
|
const { request } = event;
|
|
18
18
|
const hook = request.channel !== undefined;
|
|
19
19
|
this.#of(sessionId).permissions.set(request.permId, {
|
|
20
|
-
hook, open: !hook || request.state === 'pending',
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
hook, open: !hook || request.state === 'pending',
|
|
21
|
+
said: {
|
|
22
|
+
tool: request.toolName,
|
|
23
|
+
subject: request.subject ?? (request.toolName === EXIT_PLAN_MODE_TOOL ? 'plan' : 'tool'),
|
|
24
|
+
text: digestOf(permissionLine(request)),
|
|
25
|
+
},
|
|
26
|
+
canAllow: request.canAllow !== false, state: request.state, unavailable: request.unavailable,
|
|
23
27
|
});
|
|
24
28
|
return;
|
|
25
29
|
}
|
|
26
30
|
case 'permission-settled': {
|
|
27
|
-
const
|
|
28
|
-
|
|
31
|
+
const permissions = this.#of(sessionId).permissions;
|
|
32
|
+
const entry = permissions.get(event.permId);
|
|
33
|
+
if (entry === undefined) {
|
|
34
|
+
// Only a consent card changes in place; one the fold never saw pending still holds its place.
|
|
35
|
+
permissions.set(event.permId, {
|
|
36
|
+
hook: true, open: event.state === 'pending', said: undefined, canAllow: event.canAllow, state: event.state,
|
|
37
|
+
unavailable: event.unavailable,
|
|
38
|
+
});
|
|
29
39
|
return;
|
|
40
|
+
}
|
|
30
41
|
entry.open = !entry.hook || event.state === 'pending';
|
|
31
42
|
entry.canAllow = event.canAllow;
|
|
43
|
+
entry.state = event.state;
|
|
44
|
+
entry.unavailable = event.unavailable;
|
|
32
45
|
return;
|
|
33
46
|
}
|
|
34
47
|
case 'permission-resolved':
|
|
@@ -53,7 +66,8 @@ export class PendingDialogs {
|
|
|
53
66
|
* the screen or the transcript, else the first consent card that waits for a person; the first question. It is
|
|
54
67
|
* given only when `waitingFor` says of it what the summary says (tool, subject, text line, question count): the
|
|
55
68
|
* fold lacks a consent card the server recovered at start, before anyone listened, and would otherwise give the id
|
|
56
|
-
* of the card behind it.
|
|
69
|
+
* of the card behind it. A consent card known only from a change cannot be compared, so it gives no id either.
|
|
70
|
+
* `undefined` = the fold has none, or another card than the one `waitingFor` describes.
|
|
57
71
|
*/
|
|
58
72
|
current(sessionId, kind, waitingFor) {
|
|
59
73
|
const requests = this.#sessions.get(sessionId);
|
|
@@ -67,11 +81,25 @@ export class PendingDialogs {
|
|
|
67
81
|
}
|
|
68
82
|
const permissions = [...requests.permissions];
|
|
69
83
|
const [id, permission] = permissions.find(([, p]) => !p.hook) ?? permissions.find(([, p]) => p.hook && p.open) ?? [];
|
|
70
|
-
|
|
84
|
+
const said = permission?.said;
|
|
85
|
+
if (said === undefined || waitingFor.count !== undefined)
|
|
71
86
|
return undefined;
|
|
72
|
-
const
|
|
73
|
-
return
|
|
74
|
-
|
|
87
|
+
const line = permission?.canAllow === true ? said.text : digestOf('');
|
|
88
|
+
return said.tool === waitingFor.tool && said.subject === waitingFor.subject && line === text ? id : undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The {@link current} request with the revision of its state and buttons: for a consent card an opaque digest of
|
|
92
|
+
* `state`, `canAllow` and `unavailable`, which `permission-settled` changes in place; none for a card from the
|
|
93
|
+
* screen or the transcript and for a question, which do not change in place.
|
|
94
|
+
*/
|
|
95
|
+
request(sessionId, kind, waitingFor) {
|
|
96
|
+
const id = this.current(sessionId, kind, waitingFor);
|
|
97
|
+
if (id === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
const entry = kind === 'permission' ? this.#sessions.get(sessionId)?.permissions.get(id) : undefined;
|
|
100
|
+
if (entry === undefined || !entry.hook)
|
|
101
|
+
return { id };
|
|
102
|
+
return { id, revision: digestOf(JSON.stringify([entry.state ?? '', entry.canAllow, entry.unavailable ?? ''])) };
|
|
75
103
|
}
|
|
76
104
|
forget(sessionId) {
|
|
77
105
|
this.#sessions.delete(sessionId);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** The head of a card: what the stream says of it, without the text and the dialog details. */
|
|
2
|
+
export function headOf(card) {
|
|
3
|
+
return {
|
|
4
|
+
sessionId: card.sessionId, turnId: card.turnId, version: card.version, kind: card.kind, state: card.state,
|
|
5
|
+
at: card.at, ...(card.dialog === undefined ? {} : { dialog: card.dialog.kind }),
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
/** Newest first, as the Inbox shows the cards; the session id keeps the order stable for equal times. */
|
|
9
|
+
export function byNewest(a, b) {
|
|
10
|
+
if (a.at !== b.at)
|
|
11
|
+
return a.at < b.at ? 1 : -1;
|
|
12
|
+
return a.sessionId < b.sessionId ? -1 : a.sessionId > b.sessionId ? 1 : 0;
|
|
13
|
+
}
|
|
14
|
+
export class InboxHeads {
|
|
15
|
+
#source;
|
|
16
|
+
/** The head last published for each session, with its JSON for the comparison. */
|
|
17
|
+
#published = new Map();
|
|
18
|
+
#listeners = new Set();
|
|
19
|
+
/** Follows the tracker's changes from now on; the heads start empty and fill with its reads. */
|
|
20
|
+
constructor(inbox) {
|
|
21
|
+
this.#source = inbox;
|
|
22
|
+
inbox.onChange((sessionId) => { this.note(sessionId); });
|
|
23
|
+
}
|
|
24
|
+
/** The published heads, newest first: the `inbox-heads` of a stream that opens now. */
|
|
25
|
+
heads() {
|
|
26
|
+
return [...this.#published.values()].map((entry) => entry.head).sort(byNewest);
|
|
27
|
+
}
|
|
28
|
+
/** Listener of the events; returns the function that detaches it. */
|
|
29
|
+
onEvent(listener) {
|
|
30
|
+
this.#listeners.add(listener);
|
|
31
|
+
return () => { this.#listeners.delete(listener); };
|
|
32
|
+
}
|
|
33
|
+
/** Compares the session's visible card with the head published for it and publishes the difference. */
|
|
34
|
+
note(sessionId) {
|
|
35
|
+
const card = this.#source.card(sessionId);
|
|
36
|
+
const previous = this.#published.get(sessionId);
|
|
37
|
+
if (card === null || this.#source.removed(sessionId)) {
|
|
38
|
+
if (previous === undefined)
|
|
39
|
+
return;
|
|
40
|
+
this.#published.delete(sessionId);
|
|
41
|
+
this.#emit({ type: 'inbox-removed', sessionId, turnId: previous.head.turnId });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const head = headOf(card);
|
|
45
|
+
const json = JSON.stringify(head);
|
|
46
|
+
if (previous?.json === json)
|
|
47
|
+
return;
|
|
48
|
+
this.#published.set(sessionId, { head, json });
|
|
49
|
+
this.#emit({ type: 'inbox-head', head });
|
|
50
|
+
}
|
|
51
|
+
#emit(event) {
|
|
52
|
+
for (const listener of this.#listeners)
|
|
53
|
+
listener(event);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -44,6 +44,7 @@ export class Inbox {
|
|
|
44
44
|
#running = new Map();
|
|
45
45
|
/** The read of each session asked for while its previous read ran; it starts once that one ends. */
|
|
46
46
|
#queued = new Map();
|
|
47
|
+
#listeners = new Set();
|
|
47
48
|
#disposed = false;
|
|
48
49
|
constructor(config, port) {
|
|
49
50
|
this.#port = port;
|
|
@@ -119,6 +120,14 @@ export class Inbox {
|
|
|
119
120
|
this.#schedule(session.id);
|
|
120
121
|
}
|
|
121
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Listener of changes: called with a session after every card read and every change of its card or removals,
|
|
125
|
+
* also when nothing visible changed (the listener compares). Returns the function that detaches it.
|
|
126
|
+
*/
|
|
127
|
+
onChange(listener) {
|
|
128
|
+
this.#listeners.add(listener);
|
|
129
|
+
return () => { this.#listeners.delete(listener); };
|
|
130
|
+
}
|
|
122
131
|
/** The card of a session, also a removed one; `null` = none. */
|
|
123
132
|
card(sessionId) {
|
|
124
133
|
return this.#cards.get(sessionId) ?? null;
|
|
@@ -163,6 +172,7 @@ export class Inbox {
|
|
|
163
172
|
sessionId, ...key, ...(receipt && session.seen !== true ? { seen: true } : {}),
|
|
164
173
|
};
|
|
165
174
|
const stored = await this.#store.add(entry);
|
|
175
|
+
this.#notify(sessionId);
|
|
166
176
|
if (stored === 'failed')
|
|
167
177
|
return { kind: 'write-failed' };
|
|
168
178
|
if (stored === 'present')
|
|
@@ -195,6 +205,7 @@ export class Inbox {
|
|
|
195
205
|
if (card.version !== ref.version || found.moved)
|
|
196
206
|
return STALE;
|
|
197
207
|
const removed = await this.#store.delete(key);
|
|
208
|
+
this.#notify(sessionId);
|
|
198
209
|
if (removed === 'failed')
|
|
199
210
|
return { kind: 'write-failed' };
|
|
200
211
|
if (removed === 'absent')
|
|
@@ -270,6 +281,7 @@ export class Inbox {
|
|
|
270
281
|
this.#transitions.delete(id);
|
|
271
282
|
this.#looks.delete(id);
|
|
272
283
|
this.#states.delete(id);
|
|
284
|
+
this.#notify(id);
|
|
273
285
|
}
|
|
274
286
|
}
|
|
275
287
|
#schedule(sessionId) {
|
|
@@ -309,7 +321,10 @@ export class Inbox {
|
|
|
309
321
|
this.#log(`inbox: card read failed for session ${sessionId} (${err instanceof Error ? err.message : String(err)})`);
|
|
310
322
|
return false;
|
|
311
323
|
})
|
|
312
|
-
.finally(() => {
|
|
324
|
+
.finally(() => {
|
|
325
|
+
this.#running.delete(sessionId);
|
|
326
|
+
this.#notify(sessionId);
|
|
327
|
+
});
|
|
313
328
|
this.#running.set(sessionId, read);
|
|
314
329
|
return read;
|
|
315
330
|
}
|
|
@@ -381,7 +396,7 @@ export class Inbox {
|
|
|
381
396
|
turns = { conversation, shown, ended: session.state === 'waiting-input' ? shown : turns.ended };
|
|
382
397
|
}
|
|
383
398
|
this.#turns.set(sessionId, turns);
|
|
384
|
-
const requestOf = (dialog, waitingFor) => this.#dialogs.
|
|
399
|
+
const requestOf = (dialog, waitingFor) => this.#dialogs.request(sessionId, dialog, waitingFor);
|
|
385
400
|
const card = composeCard(session, working ? turns.ended : turns.shown, requestOf);
|
|
386
401
|
if (card === null) {
|
|
387
402
|
this.#cards.delete(sessionId);
|
|
@@ -406,6 +421,13 @@ export class Inbox {
|
|
|
406
421
|
#forget(sessionId) {
|
|
407
422
|
this.#cards.delete(sessionId);
|
|
408
423
|
this.#turns.delete(sessionId);
|
|
424
|
+
this.#notify(sessionId);
|
|
425
|
+
}
|
|
426
|
+
#notify(sessionId) {
|
|
427
|
+
if (this.#disposed)
|
|
428
|
+
return;
|
|
429
|
+
for (const listener of this.#listeners)
|
|
430
|
+
this.#safely(sessionId, 'change listener', () => { listener(sessionId); });
|
|
409
431
|
}
|
|
410
432
|
#safely(sessionId, what, step) {
|
|
411
433
|
try {
|
|
@@ -10,10 +10,12 @@
|
|
|
10
10
|
* a decision card's dialog;
|
|
11
11
|
* - `segments.ts`: the read of the conversation file a session writes now, through `readInputSegments`;
|
|
12
12
|
* - `removals.ts`: `inbox.json`, the removals with their atomic write chain;
|
|
13
|
-
* - `inbox.ts`: the tracker that reads cards again after the session manager's events, and remove and restore
|
|
13
|
+
* - `inbox.ts`: the tracker that reads cards again after the session manager's events, and remove and restore;
|
|
14
|
+
* - `heads.ts`: the heads of the visible cards and the stream events of their changes.
|
|
14
15
|
*/
|
|
15
16
|
export { cardKindOf, composeCard, removalKeyOf, shownTurnFor } from './card.js';
|
|
16
17
|
export { PendingDialogs } from './dialogs.js';
|
|
18
|
+
export { InboxHeads, byNewest, headOf } from './heads.js';
|
|
17
19
|
export { INBOX_DEBOUNCE_MS, INBOX_SLOW_READ_MS, Inbox } from './inbox.js';
|
|
18
20
|
export { INBOX_CLOSED_LIMIT, INBOX_FILENAME, INBOX_FORMAT } from './removals.js';
|
|
19
21
|
export { INBOX_FIRST_WINDOW, createSegmentReader } from './segments.js';
|
package/server/src/index.js
CHANGED
|
@@ -26,12 +26,13 @@ import { consoleLogger } from './router.js';
|
|
|
26
26
|
import { readHookHealth } from './health.js';
|
|
27
27
|
import { consentAdminControl, consentCardAwaitedAt, consentCardLive, consentManagerPort, consentTranscriptOf, createConsentChannel, observeConsent, startConsentCycle, startConsentRegistration, watchConsentMode, } from './bootstrap/consent.js';
|
|
28
28
|
import { createAliveSessionsSignal } from './bootstrap/alive-sessions.js';
|
|
29
|
+
import { consentNoHookSources, consentSessionEntry } from './bootstrap/consent.js';
|
|
29
30
|
import { createDeviceAuth, startDeviceSweep } from './bootstrap/devices.js';
|
|
30
31
|
import { healthProbes } from './bootstrap/health.js';
|
|
31
32
|
import { prepareHookFiles } from './bootstrap/hook-files.js';
|
|
32
33
|
import { loadDemoHerdr } from './bootstrap/herdr.js';
|
|
33
34
|
import { buildRouter, configureHttpServer } from './bootstrap/http.js';
|
|
34
|
-
import {
|
|
35
|
+
import { assembleInbox } from './bootstrap/inbox.js';
|
|
35
36
|
import { startServerIroh } from './bootstrap/iroh.js';
|
|
36
37
|
import { loadSdk, managerConfig } from './bootstrap/manager.js';
|
|
37
38
|
import { createPush } from './bootstrap/push.js';
|
|
@@ -43,6 +44,7 @@ import { consentSubagentsOpen } from './herdr/dialogs/hook/contract.js';
|
|
|
43
44
|
import { CONSENT_EVIDENCE_SCAN_MAX_BYTES } from './herdr/dialogs/layer1/contract.js';
|
|
44
45
|
import { nodeConsentEvidenceIo } from './herdr/consent-evidence.js';
|
|
45
46
|
import { createSessionHookList } from './herdr/dialogs/layer1/hook-list/hook-list.js';
|
|
47
|
+
import { FileDownloads } from './files/send-file.js';
|
|
46
48
|
import { consentStep } from './herdr/consent-step.js';
|
|
47
49
|
import { createSubagentObservation } from './herdr/consent-subagent.js';
|
|
48
50
|
import { hookCommand } from './herdr/hook-commands.js';
|
|
@@ -178,22 +180,17 @@ async function loadHerdr(config, log, shadow, demoContent, consentMode, consentL
|
|
|
178
180
|
topLoss: createConsentTopLoss(),
|
|
179
181
|
sessionHooks: createSessionHookList({
|
|
180
182
|
home: homedir(), accepted: [hookCommand(homedir())],
|
|
181
|
-
entry:
|
|
182
|
-
const read = await cliVersionReader({ registryOnly: true });
|
|
183
|
-
if (read.registry === null || (read.unreadablePids ?? 0) > 0)
|
|
184
|
-
return null;
|
|
185
|
-
if (read.unlistedSessions?.has(engineSessionId) === true)
|
|
186
|
-
return null;
|
|
187
|
-
const entries = read.registry.get(engineSessionId);
|
|
188
|
-
const entry = entries?.length === 1 ? entries[0] : undefined;
|
|
189
|
-
return entry === undefined ? null
|
|
190
|
-
: { pid: entry.pid, ...(entry.startedAt === undefined ? {} : { startedAt: entry.startedAt }) };
|
|
191
|
-
},
|
|
183
|
+
entry: consentSessionEntry(cliVersionReader),
|
|
192
184
|
onVerdict: (engineSessionId, verdict) => {
|
|
193
185
|
log.info(`consent: session hooks ${engineSessionId} ${verdict.ok ? 'ok' : `no (${verdict.why})`}`);
|
|
194
186
|
},
|
|
195
187
|
}),
|
|
196
188
|
};
|
|
189
|
+
// Consent without a hook (`drivers/herdr/consent-no-hook.ts`): traces, session facts, `ps` and `statusLine`.
|
|
190
|
+
const consentNoHook = cliVersionReader === null ? undefined : consentNoHookSources({
|
|
191
|
+
link: consentLink, dataDir: config.dataDir, home: homedir(), roots: config.herdrRoots,
|
|
192
|
+
entry: consentSessionEntry(cliVersionReader), proofGate: consentProofGate,
|
|
193
|
+
});
|
|
197
194
|
const supervisor = herdr.createHerdrSupervisor({
|
|
198
195
|
socketPath: config.herdrSocket,
|
|
199
196
|
roots: config.herdrRoots,
|
|
@@ -316,6 +313,7 @@ async function loadHerdr(config, log, shadow, demoContent, consentMode, consentL
|
|
|
316
313
|
consentCardLive: (engineSessionId) => consentCardLive(consentLink, engineSessionId),
|
|
317
314
|
// The reason of a consent card without the approve button: what the desk release of its call key kept.
|
|
318
315
|
consentDeskRelease: (engineSessionId, key) => consentLink.cycle?.deskRelease(engineSessionId, key) ?? null,
|
|
316
|
+
...(consentNoHook === undefined ? {} : { consentNoHook }),
|
|
319
317
|
// Karta zgody z ekranu albo jsonl czeka na kartę kanału, póki rejestracja hooka sesji nie ma karty (z sufitem).
|
|
320
318
|
consentCardAwaited: (engineSessionId) => consentCardAwaitedAt(consentLink, engineSessionId),
|
|
321
319
|
// The measured "Yes" digit waits for no registration without a card, a subagent's too, of any age.
|
|
@@ -608,11 +606,14 @@ async function main() {
|
|
|
608
606
|
manager.onEvent((scope) => { if (scope === 'global')
|
|
609
607
|
apns.liveActivity.poke(); });
|
|
610
608
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
609
|
+
/*
|
|
610
|
+
* Inbox cards from the manager's events, their removals in `inbox.json` and the heads for the stream
|
|
611
|
+
* (`bootstrap/inbox.ts`), the demo over its own sessions; `null` with `RAROG_INBOX=0`.
|
|
612
|
+
*/
|
|
613
|
+
const inboxParts = await assembleInbox({
|
|
614
|
+
config, manager, ready: manager.ready, log: (line) => { log.info(line); },
|
|
615
615
|
});
|
|
616
|
+
const inbox = inboxParts?.inbox ?? null;
|
|
616
617
|
// Nadzorca podpięty, seedy demo założone — dopiero wtedy wpuszczamy ruch HTTP.
|
|
617
618
|
await manager.ready;
|
|
618
619
|
apns?.liveActivity.poke();
|
|
@@ -622,6 +623,7 @@ async function main() {
|
|
|
622
623
|
: undefined;
|
|
623
624
|
const hub = createSseHub({
|
|
624
625
|
config, manager, serverStartId, log, demoNotes,
|
|
626
|
+
...(inboxParts === null ? {} : { inbox: inboxParts.heads }),
|
|
625
627
|
// The PR status reads GitHub only while a client is connected.
|
|
626
628
|
...(prStatus === null
|
|
627
629
|
? {}
|
|
@@ -638,9 +640,11 @@ async function main() {
|
|
|
638
640
|
*/
|
|
639
641
|
const term = await loadTerm(config, log, { devices, limiter: loginLimiter });
|
|
640
642
|
const companion = assembleCompanion({ config, manager, log: (line) => { log.info(line); } }); // off in demo
|
|
643
|
+
// Session file downloads in flight by device: the router registers them, an unpair stops them.
|
|
644
|
+
const fileDownloads = new FileDownloads();
|
|
641
645
|
// Skutki odpięcia urządzenia, pierwszy przebieg wygasania i przebieg co minutę (`bootstrap/devices.ts`).
|
|
642
646
|
const deviceSweep = startDeviceSweep(devices, {
|
|
643
|
-
pairing, hub, term: term ?? undefined, companion, apns, log: (line) => { log.info(line); },
|
|
647
|
+
pairing, hub, term: term ?? undefined, companion, files: fileDownloads, apns, log: (line) => { log.info(line); },
|
|
644
648
|
});
|
|
645
649
|
// Sondy `/health` z `bootstrap/health.ts`; pole kanału zgód dokłada `main()`.
|
|
646
650
|
const health = {
|
|
@@ -662,7 +666,7 @@ async function main() {
|
|
|
662
666
|
const router = buildRouter({
|
|
663
667
|
config, manager, hub, serverStartId, startedAtMs, log, health, system, macStats, demoUsage, demoVoice, demoNotes,
|
|
664
668
|
readUpload: herdr?.readUpload, term, companion, notifier, presence, apns, loginLimiter, irohPair, devices, pairing,
|
|
665
|
-
reviewConstants, demoHost,
|
|
669
|
+
reviewConstants, demoHost, inbox, fileDownloads,
|
|
666
670
|
});
|
|
667
671
|
// Sterowanie testów UI tylko w zwykłym demo: w trybie recenzji słuchacz `request` to sam router.
|
|
668
672
|
const demoControl = config.demo && config.demoReview === null && herdr?.scenarioPanels !== undefined
|
|
@@ -127,6 +127,14 @@ export function npmUpdatePackArgs(current, latest, dir) {
|
|
|
127
127
|
return ['pack', `${RAROG_PACKAGE_NAME}@>${current} <=${latest}`, '--ignore-scripts', '--json', '--pack-destination',
|
|
128
128
|
dir];
|
|
129
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Arguments of `npm-cli.js` with which `rarog update --beta` downloads exactly `version`, the target it chose from the
|
|
132
|
+
* dist-tags, into `dir`, with its name and version as JSON. npm's release age gate holds an exact version back as it
|
|
133
|
+
* holds a range: not old enough is `ETARGET`.
|
|
134
|
+
*/
|
|
135
|
+
export function npmUpdateExactPackArgs(version, dir) {
|
|
136
|
+
return ['pack', `${RAROG_PACKAGE_NAME}@${version}`, '--ignore-scripts', '--json', '--pack-destination', dir];
|
|
137
|
+
}
|
|
130
138
|
/**
|
|
131
139
|
* Arguments of `npm-cli.js` with which `rarog update` installs exactly the tarball `file` it checked: global,
|
|
132
140
|
* without install scripts (the package needs none, and a release must not run code at install).
|
|
@@ -9,12 +9,17 @@
|
|
|
9
9
|
* w testach i wyjście komend `rarog …` dla człowieka go nie dostają.
|
|
10
10
|
*
|
|
11
11
|
* Granica linii to `\n` w bajtach zapisu: zapis wielowierszowy (stos błędu) dostaje prefiks na każdej linii, a linia
|
|
12
|
-
* zapisana kilkoma `write` jeden, z czasem pierwszego fragmentu.
|
|
12
|
+
* zapisana kilkoma `write` jeden, z czasem pierwszego fragmentu. A line that starts after a newline inside the same
|
|
13
|
+
* write continues one log call (a stack trace, a value with a newline), so its prefix is the time followed by
|
|
14
|
+
* {@link LOG_CONTINUATION_MARK}: after {@link LOG_TIME_PREFIX} it starts with `| `, and a value that carries a whole
|
|
15
|
+
* log line cannot pass for a line the server wrote. Czego nie obejmuje: bajtów, które omijają
|
|
13
16
|
* `process.stdout.write` / `process.stderr.write` (wydruk niezłapanego wyjątku z samego Node, proces potomny
|
|
14
17
|
* z odziedziczonym deskryptorem).
|
|
15
18
|
*/
|
|
16
19
|
/** Prefiks czasu na początku linii logu serwera. Czytelnicy logu przyjmują linię z nim i bez niego (stare pliki). */
|
|
17
20
|
export const LOG_TIME_PREFIX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z /u;
|
|
21
|
+
/** Follows the time on a line that continues the log call of the line above, in the same write. */
|
|
22
|
+
export const LOG_CONTINUATION_MARK = '| ';
|
|
18
23
|
const NEWLINE = 0x0a;
|
|
19
24
|
/**
|
|
20
25
|
* Owija `stream.write`: przed każdą linią wstawia czas z `now()` (raz na zapis, tylko gdy linia się w nim zaczyna).
|
|
@@ -38,12 +43,12 @@ export function timestampWrites(stream, now = () => new Date()) {
|
|
|
38
43
|
if (bytes.length === 0)
|
|
39
44
|
return original(chunk, ...rest);
|
|
40
45
|
const parts = [];
|
|
41
|
-
let
|
|
46
|
+
let time = null;
|
|
42
47
|
let from = 0;
|
|
43
48
|
while (from < bytes.length) {
|
|
44
49
|
if (atLineStart) {
|
|
45
|
-
|
|
46
|
-
parts.push(
|
|
50
|
+
time ??= now().toISOString();
|
|
51
|
+
parts.push(Buffer.from(from === 0 ? `${time} ` : `${time} ${LOG_CONTINUATION_MARK}`, 'utf8'));
|
|
47
52
|
}
|
|
48
53
|
const newline = bytes.indexOf(NEWLINE, from);
|
|
49
54
|
const end = newline === -1 ? bytes.length : newline + 1;
|
package/server/src/router.js
CHANGED
|
@@ -13,7 +13,10 @@ import { createUsageProvider } from './usage.js';
|
|
|
13
13
|
import { serverSystem } from './version.js';
|
|
14
14
|
import { COMPANION_SPEECH_KEY, createVoiceMinter } from './voice/soniox.js';
|
|
15
15
|
import { createCompanionTokenMinter } from './companion/openai-token.js';
|
|
16
|
+
import { SessionFileIndex } from './files/file-index.js';
|
|
17
|
+
import { FileDownloads } from './files/send-file.js';
|
|
16
18
|
import { handleDemoPairing, routeDevices } from './routes/devices.js';
|
|
19
|
+
import { routeInbox } from './routes/inbox.js';
|
|
17
20
|
import { routeLimits } from './routes/limits.js';
|
|
18
21
|
import { routeNotes } from './routes/notes.js';
|
|
19
22
|
import { routeProjects } from './routes/projects.js';
|
|
@@ -391,6 +394,8 @@ export function createRouter(deps) {
|
|
|
391
394
|
?? createUsageProvider({ logger: (line) => { log.info(line); } });
|
|
392
395
|
const loginLimiter = deps.loginLimiter ?? createLoginLimiter();
|
|
393
396
|
const interruptWaitMs = deps.interruptWaitMs ?? INTERRUPT_WAIT_MS;
|
|
397
|
+
const fileDownloads = deps.fileDownloads ?? new FileDownloads();
|
|
398
|
+
const fileIndex = deps.fileIndex ?? new SessionFileIndex();
|
|
394
399
|
const { config, manager, hub, serverStartId, startedAtMs, presence, apns } = deps;
|
|
395
400
|
const notes = deps.notes ?? fileNotes(config.dataDir);
|
|
396
401
|
const system = deps.system ?? serverSystem();
|
|
@@ -453,10 +458,11 @@ export function createRouter(deps) {
|
|
|
453
458
|
const ctx = {
|
|
454
459
|
deps, config, manager, hub, serverStartId, startedAtMs, presence, apns, notes, system, voice, companionToken,
|
|
455
460
|
companionAvailable, companionSessionsAvailable, companionTurns, companionSpeechToken, usageProvider,
|
|
456
|
-
healthProbes, secrets, log, interruptWaitMs, apnsTestCooldown, loginLimiter,
|
|
461
|
+
healthProbes, secrets, log, interruptWaitMs, apnsTestCooldown, loginLimiter, fileDownloads, fileIndex,
|
|
457
462
|
};
|
|
458
463
|
const ROUTE_GROUPS = [
|
|
459
464
|
routeSystem, routeTerminal, routeNotes, routeDevices, routeProjects, routeLimits, routePush, routeSessions,
|
|
465
|
+
routeInbox,
|
|
460
466
|
];
|
|
461
467
|
/**
|
|
462
468
|
* Odmowa bramki: brak nagłówka `Authorization` (401 `unauthorized`), nieznany klucz urządzenia
|
|
@@ -6,7 +6,7 @@ import { loginPeer, pairingLink, publicUrlOf, redact } from '../auth/index.js';
|
|
|
6
6
|
import { isIrohSocket, markAuthenticated, noteIrohRefusal } from '../iroh/socket.js';
|
|
7
7
|
import { methodNotAllowed, parseBody, readJsonBody, readValidatedBody, sendError, sendJson, sendNoContent, } from '../router.js';
|
|
8
8
|
import { sanitizeBuild, sanitizeLabel } from '../text.js';
|
|
9
|
-
import {
|
|
9
|
+
import { serverCapabilities } from '../version.js';
|
|
10
10
|
/** Pusty obiekt — ciało `POST /api/pairings` (kod wydaje serwer, klient niczego nie podaje). */
|
|
11
11
|
const emptyBodySchema = z.strictObject({});
|
|
12
12
|
/**
|
|
@@ -99,7 +99,7 @@ async function handlePair(ctx, req, res, identity) {
|
|
|
99
99
|
id: identity.store.serverId,
|
|
100
100
|
name: identity.serverName,
|
|
101
101
|
version: config.version,
|
|
102
|
-
capabilities:
|
|
102
|
+
capabilities: serverCapabilities({ inbox: deps.inbox !== undefined, files: config.filesEnabled }),
|
|
103
103
|
// Serwer iroh — ten sam co w `/api/version`; serwer bez nasłuchu iroh pisze `null`.
|
|
104
104
|
iroh: deps.iroh ?? null,
|
|
105
105
|
},
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { INBOX_CARD_CHANGED_ERROR, INBOX_DISABLED_ERROR, inboxActionBodySchema, } from '@rarog/shared/rest';
|
|
2
|
+
import { say } from '@rarog/shared/sentences';
|
|
3
|
+
import { methodNotAllowed, readValidatedBody, sendError, sendJson } from '../router.js';
|
|
4
|
+
import { decodeSegment } from './context.js';
|
|
5
|
+
/**
|
|
6
|
+
* The card on the wire, as the tracker composed it: a `decision` card carries the dialog the session summary
|
|
7
|
+
* describes and the id of its card in the session stream, not that pending card. Reading it (`ManagerApi.transcript`,
|
|
8
|
+
* also with `peek`) marks every consent card of the session as shown, also those this answer would not carry; the next
|
|
9
|
+
* change of such a card then goes as `permission-settled`, which a device that never got the card drops, instead of
|
|
10
|
+
* `permission-pending`.
|
|
11
|
+
*/
|
|
12
|
+
function wireOf(card) {
|
|
13
|
+
const { dialog, ...head } = card;
|
|
14
|
+
return dialog === undefined ? head : { ...head, dialog };
|
|
15
|
+
}
|
|
16
|
+
function disabled(res) {
|
|
17
|
+
sendError(res, 503, INBOX_DISABLED_ERROR, say('rest.inbox-disabled'));
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
function unknownSession(res) {
|
|
21
|
+
sendError(res, 404, 'not-found', say('rest.session-not-found'));
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
async function act(ctx, req, res, inbox, sessionId, action) {
|
|
25
|
+
const body = await readValidatedBody(req, res, ctx.config, inboxActionBodySchema);
|
|
26
|
+
if (body === null)
|
|
27
|
+
return;
|
|
28
|
+
if (ctx.manager.get(sessionId) === undefined) {
|
|
29
|
+
unknownSession(res);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const result = action === 'remove' ? await inbox.remove(sessionId, body) : await inbox.restore(sessionId, body);
|
|
33
|
+
if (result.kind === 'stale') {
|
|
34
|
+
sendError(res, 409, INBOX_CARD_CHANGED_ERROR, say('rest.inbox-card-changed'));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (result.kind === 'write-failed') {
|
|
38
|
+
ctx.log.error(`inbox: ${action} of the card of session ${sessionId} not saved`);
|
|
39
|
+
sendError(res, 500, 'internal', say('rest.inbox-save-failed'));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const response = {
|
|
43
|
+
ok: true, changed: result.changed, ...(result.receipt === undefined ? {} : { receipt: result.receipt }),
|
|
44
|
+
};
|
|
45
|
+
sendJson(res, 200, response);
|
|
46
|
+
}
|
|
47
|
+
export async function routeInbox(ctx, req, res, rq) {
|
|
48
|
+
const { method, pathname, segments } = rq;
|
|
49
|
+
const inbox = ctx.deps.inbox;
|
|
50
|
+
if (pathname === '/api/inbox') {
|
|
51
|
+
if (method !== 'GET')
|
|
52
|
+
return methodNotAllowed(res, 'GET');
|
|
53
|
+
if (inbox === undefined)
|
|
54
|
+
return disabled(res);
|
|
55
|
+
sendJson(res, 200, { cards: inbox.visibleCards().map(wireOf) });
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
// /api/inbox/:sessionId[/remove|/restore]
|
|
59
|
+
if (segments[0] === 'api' && segments[1] === 'inbox' && segments.length >= 3) {
|
|
60
|
+
const sessionId = decodeSegment(segments[2] ?? '');
|
|
61
|
+
const action = segments[3];
|
|
62
|
+
if (segments.length === 3) {
|
|
63
|
+
if (method !== 'GET')
|
|
64
|
+
return methodNotAllowed(res, 'GET');
|
|
65
|
+
if (inbox === undefined)
|
|
66
|
+
return disabled(res);
|
|
67
|
+
if (ctx.manager.get(sessionId) === undefined)
|
|
68
|
+
return unknownSession(res);
|
|
69
|
+
const card = inbox.card(sessionId);
|
|
70
|
+
sendJson(res, 200, card === null || inbox.removed(sessionId) ? {} : { card: wireOf(card) });
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (action === 'remove' && segments.length === 4) {
|
|
74
|
+
if (method !== 'POST')
|
|
75
|
+
return methodNotAllowed(res, 'POST');
|
|
76
|
+
if (inbox === undefined)
|
|
77
|
+
return disabled(res);
|
|
78
|
+
await act(ctx, req, res, inbox, sessionId, 'remove');
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
if (action === 'restore' && segments.length === 4) {
|
|
82
|
+
if (method !== 'POST')
|
|
83
|
+
return methodNotAllowed(res, 'POST');
|
|
84
|
+
if (inbox === undefined)
|
|
85
|
+
return disabled(res);
|
|
86
|
+
await act(ctx, req, res, inbox, sessionId, 'restore');
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|