@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
|
@@ -2,6 +2,7 @@ import { MAX_FILES_PER_MESSAGE, MAX_IMAGES_PER_MESSAGE, MAX_MESSAGE_BODY_BYTES,
|
|
|
2
2
|
import { COMPANION_BUSY_ERROR, COMPANION_CLI_ERROR, COMPANION_DISABLED_ERROR, COMPANION_LIMIT_ERROR, COMPANION_SESSIONS_DISABLED_ERROR, COMPANION_TIMEOUT_ERROR, companionTurnsCountSchema, answersBodySchema, companionAskBodySchema, messageBodySchema, queueTakeBackBodySchema, restartBodySchema, seenBodySchema, } from '@rarog/shared/rest';
|
|
3
3
|
import { say } from '@rarog/shared/sentences';
|
|
4
4
|
import { redact } from '../auth/index.js';
|
|
5
|
+
import { sendDownload } from '../files/send-file.js';
|
|
5
6
|
import { conversationToken } from '../companion/conversation.js';
|
|
6
7
|
import { readSettledTurns } from '../companion/turns-settle.js';
|
|
7
8
|
import { clientLabel, diffErrorStatus, managerErrorStatus, methodNotAllowed, parseBody, readJsonBody, readValidatedBody, recheckDevice, sendError, sendFailure, sendJson, } from '../router.js';
|
|
@@ -236,6 +237,113 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
236
237
|
}
|
|
237
238
|
return true;
|
|
238
239
|
}
|
|
240
|
+
/*
|
|
241
|
+
* GET /api/sessions/:id/files?dir=<rel> → one directory of the session (`files/session-files.ts`): the gate of the
|
|
242
|
+
* file routes, the listing, git's `ignored` labels. With `RAROG_FILES=0` the route does not exist: 404 as an
|
|
243
|
+
* unknown route for every method. An unknown session is 404 `session-not-found`, as on the diff routes, so the
|
|
244
|
+
* app does not read it as a directory gone (`not-found`). The device is checked again after the read.
|
|
245
|
+
*/
|
|
246
|
+
if (action === 'files' && segments.length === 4) {
|
|
247
|
+
if (!config.filesEnabled) {
|
|
248
|
+
sendError(res, 404, 'not-found', say('rest.endpoint-not-found'));
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
if (method !== 'GET')
|
|
252
|
+
return methodNotAllowed(res, 'GET');
|
|
253
|
+
let listing;
|
|
254
|
+
try {
|
|
255
|
+
listing = await manager.listFiles(sessionId, url.searchParams.get('dir') ?? '', config.filesMaxBytes);
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
const status = diffErrorStatus(err);
|
|
259
|
+
if (status === 500)
|
|
260
|
+
log.error(`manager action failed: ${redact(err, secrets)}`);
|
|
261
|
+
if (!res.headersSent)
|
|
262
|
+
sendFailure(res, status, err, 'diff');
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
if (!recheckDevice(req, res, 'while listing the files of'))
|
|
266
|
+
return true;
|
|
267
|
+
if (listing.ok)
|
|
268
|
+
sendJson(res, 200, listing.value);
|
|
269
|
+
else
|
|
270
|
+
sendError(res, listing.refusal.status, listing.refusal.code, listing.refusal.sentence);
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
/*
|
|
274
|
+
* GET /api/sessions/:id/files/index → the paths of the session's files (`files/file-index.ts`): the gate of the
|
|
275
|
+
* file routes, then a walk of the session directory by the rule of the listing, narrowed by git. Off with
|
|
276
|
+
* `RAROG_FILES=0` like the listing. Requests during a walk of the same directory get its answer; the device is
|
|
277
|
+
* checked again after it, per request.
|
|
278
|
+
*/
|
|
279
|
+
if (action === 'files' && segments.length === 5 && segments[4] === 'index') {
|
|
280
|
+
if (!config.filesEnabled) {
|
|
281
|
+
sendError(res, 404, 'not-found', say('rest.endpoint-not-found'));
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
if (method !== 'GET')
|
|
285
|
+
return methodNotAllowed(res, 'GET');
|
|
286
|
+
let index;
|
|
287
|
+
try {
|
|
288
|
+
index = await ctx.fileIndex.index(sessionId, config.filesMaxBytes, manager.filesPort());
|
|
289
|
+
}
|
|
290
|
+
catch (err) {
|
|
291
|
+
const status = diffErrorStatus(err);
|
|
292
|
+
if (status === 500)
|
|
293
|
+
log.error(`manager action failed: ${redact(err, secrets)}`);
|
|
294
|
+
if (!res.headersSent)
|
|
295
|
+
sendFailure(res, status, err, 'diff');
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
if (!recheckDevice(req, res, 'while indexing the files of'))
|
|
299
|
+
return true;
|
|
300
|
+
if (index.ok)
|
|
301
|
+
sendJson(res, 200, index.value);
|
|
302
|
+
else
|
|
303
|
+
sendError(res, index.refusal.status, index.refusal.code, index.refusal.sentence);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
/*
|
|
307
|
+
* GET /api/sessions/:id/files/raw?path=<rel> → the bytes of one file of the session: the gate and the path rule
|
|
308
|
+
* of the listing and the limit before any byte (`files/session-download.ts`, 413 with `size` and `maxBytes`), then
|
|
309
|
+
* exactly `Content-Length` bytes from one descriptor (`files/send-file.ts`). Off with `RAROG_FILES=0` like the
|
|
310
|
+
* listing. The device is checked again after the open and the download is registered under it with no `await` in
|
|
311
|
+
* between, so a device unpaired from then on cuts the response; every exit after the open closes the file.
|
|
312
|
+
*/
|
|
313
|
+
if (action === 'files' && segments.length === 5 && segments[4] === 'raw') {
|
|
314
|
+
if (!config.filesEnabled) {
|
|
315
|
+
sendError(res, 404, 'not-found', say('rest.endpoint-not-found'));
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
if (method !== 'GET')
|
|
319
|
+
return methodNotAllowed(res, 'GET');
|
|
320
|
+
let answer;
|
|
321
|
+
try {
|
|
322
|
+
answer = await manager.openFile(sessionId, url.searchParams.get('path') ?? '', config.filesMaxBytes);
|
|
323
|
+
}
|
|
324
|
+
catch (err) {
|
|
325
|
+
const status = diffErrorStatus(err);
|
|
326
|
+
if (status === 500)
|
|
327
|
+
log.error(`manager action failed: ${redact(err, secrets)}`);
|
|
328
|
+
if (!res.headersSent)
|
|
329
|
+
sendFailure(res, status, err, 'diff');
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
if (!recheckDevice(req, res, 'while opening a file of')) {
|
|
333
|
+
if (answer.ok)
|
|
334
|
+
await answer.value.close().catch(() => { });
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
if (!answer.ok) {
|
|
338
|
+
const { refusal } = answer;
|
|
339
|
+
sendError(res, refusal.status, refusal.code, refusal.sentence, 'extra' in refusal ? refusal.extra : undefined);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
await sendDownload(res, answer.value, {
|
|
343
|
+
owner, downloads: ctx.fileDownloads, log: (line) => { log.info(`${line} (session ${sessionId})`); },
|
|
344
|
+
});
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
239
347
|
/*
|
|
240
348
|
* GET /api/sessions/:id/commands — slash-komendy, które Rarog przepuści w tej sesji: ta sama migawka
|
|
241
349
|
* katalogu co strażnik wysyłki, z jej `revision`. Odczyt, więc GET i trasa
|
|
@@ -250,14 +358,15 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
250
358
|
}, 'message');
|
|
251
359
|
}
|
|
252
360
|
/*
|
|
253
|
-
* GET /api/sessions/:id/companion/turns?count=1..3 → {messages, conversation, stateSince, complete}
|
|
361
|
+
* GET /api/sessions/:id/companion/turns?count=1..3 → {messages, conversation, stateSince, readMark, complete}
|
|
254
362
|
* (`docs/companion.md`, "Turns route"): the last turns of the engine conversation the session writes now, for the
|
|
255
363
|
* voice agent across sessions. Demo mode has no route (404 as unknown). The gate, in order: the voice agent (503
|
|
256
364
|
* `companion-disabled`), the mode across sessions (503 `companion-sessions-disabled`), `count` (400), a Claude Code
|
|
257
|
-
* session in scope (404). The conversation
|
|
258
|
-
* it (`companion/turns-settle.ts`): another conversation by then is read
|
|
259
|
-
* response closes without a body; a session gone or out of scope by then gets
|
|
260
|
-
* checked again before the answer, so a device unpaired during the read gets 401 and
|
|
365
|
+
* session in scope (404). The conversation, `stateSince` and the read mark of the session's memory are taken
|
|
366
|
+
* together before each read and again after it (`companion/turns-settle.ts`): another conversation by then is read
|
|
367
|
+
* again, at most three reads, then the response closes without a body; a session gone or out of scope by then gets
|
|
368
|
+
* the gate's 404. The device is checked again before the answer, so a device unpaired during the read gets 401 and
|
|
369
|
+
* no turns.
|
|
261
370
|
*/
|
|
262
371
|
if (action === 'companion' && segments.length === 5 && segments[4] === 'turns') {
|
|
263
372
|
if (config.demo) {
|
|
@@ -299,7 +408,9 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
299
408
|
return { refused: () => { sendError(res, 404, 'not-found', say('rest.companion-claude-only')); } };
|
|
300
409
|
}
|
|
301
410
|
const stateSince = manager.get(sessionId)?.stateSince ?? '';
|
|
302
|
-
return {
|
|
411
|
+
return {
|
|
412
|
+
target: { cwd: target.cwd, agentSessionId: target.agentSessionId, stateSince, readMark: target.readMark },
|
|
413
|
+
};
|
|
303
414
|
};
|
|
304
415
|
await readSettledTurns(ctx.companionTurns, take, count.data, (outcome) => {
|
|
305
416
|
if (!recheckDevice(req, res, 'while reading voice agent turns for'))
|
|
@@ -440,6 +551,11 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
440
551
|
* dalej wracają jako 400/409 z `runManagerAction` — mimo `async` tutaj.
|
|
441
552
|
*/
|
|
442
553
|
const delivered = await waitForDelivery(manager.interrupt(sessionId), interruptWaitMs);
|
|
554
|
+
/*
|
|
555
|
+
* Only a key that went can have handed the prompt back to Claude Code's field, so a `no` or `pending` answer
|
|
556
|
+
* waits for no field read. The read after a delivered key is bounded in the driver (`returned-prompt.ts`).
|
|
557
|
+
*/
|
|
558
|
+
const returnedPrompt = delivered === 'yes' ? await manager.returnedPrompt(sessionId) : null;
|
|
443
559
|
/*
|
|
444
560
|
* Odpowiedź niesie ŚWIEŻE `SessionSummary`, żeby widok po Esc nie czekał długo na stan.
|
|
445
561
|
*
|
|
@@ -452,9 +568,12 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
452
568
|
* `activity` jest ulotna (`seq === null`), więc replay po reconnekcie jej nie powtórzy.
|
|
453
569
|
*
|
|
454
570
|
* `ok: true` zostaje dla zgodności ze starszym klientem, który czyta sam status;
|
|
455
|
-
* `delivered` jest polem ADDYTYWNYM i taki klient je zignoruje.
|
|
571
|
+
* `delivered` jest polem ADDYTYWNYM i taki klient je zignoruje. `returnedPrompt` is additive as well and stands
|
|
572
|
+
* only when the field proved the hand-back.
|
|
456
573
|
*/
|
|
457
|
-
sendJson(res, 202, {
|
|
574
|
+
sendJson(res, 202, {
|
|
575
|
+
ok: true, session: manager.get(sessionId), delivered, ...(returnedPrompt === null ? {} : { returnedPrompt }),
|
|
576
|
+
});
|
|
458
577
|
});
|
|
459
578
|
}
|
|
460
579
|
/*
|
|
@@ -524,12 +643,14 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
524
643
|
* obsłużonej z telefonu. Dalej 200 i dalej natychmiastowe: fokus leci w TLE, a jego
|
|
525
644
|
* niepowodzenie nie jest błędem tego wywołania (znacznik lokalny stoi tak czy inaczej).
|
|
526
645
|
*
|
|
527
|
-
* The voice agent's pick-up (`docs/companion.md`, "Picking up a result"): the body `{by,
|
|
646
|
+
* The voice agent's pick-up (`docs/companion.md`, "Picking up a result"): the body `{by, conversation, readMark}`
|
|
528
647
|
* (all three or none, else the 400 of the schema) meets the gate of the mode's other routes (503), then the
|
|
529
648
|
* catalogue's scope (the 404 of the turns route), and the manager refuses with a 409 anything but the result the
|
|
530
|
-
* agent read (`markSeen` in `seen-marks.ts`: `waiting-input` with no decision card, the
|
|
531
|
-
*
|
|
532
|
-
*
|
|
649
|
+
* agent read (`markSeen` in `seen-marks.ts`: `waiting-input` with no decision card, the conversation of the token,
|
|
650
|
+
* the turns read's `readMark`: no newer message in the session's memory and the same `stateSince`), all with no
|
|
651
|
+
* `await` after the body.
|
|
652
|
+
* The token becomes a check of a conversation id as for a send. A body without `by` is a person's receipt with any
|
|
653
|
+
* flags.
|
|
533
654
|
*/
|
|
534
655
|
if (action === 'seen' && segments.length === 4) {
|
|
535
656
|
const body = await readValidatedBody(req, res, config, seenBodySchema);
|
|
@@ -537,10 +658,10 @@ export async function routeSessions(ctx, req, res, rq) {
|
|
|
537
658
|
return true;
|
|
538
659
|
const token = deps.conversationToken ?? conversationToken;
|
|
539
660
|
const read = body.conversation;
|
|
540
|
-
const pickUp = body.by === undefined || body.
|
|
661
|
+
const pickUp = body.by === undefined || body.readMark === undefined || read === undefined
|
|
541
662
|
? undefined
|
|
542
663
|
: {
|
|
543
|
-
by: body.by,
|
|
664
|
+
by: body.by, readMark: body.readMark,
|
|
544
665
|
conversation: (agentSessionId) => token(agentSessionId) === read,
|
|
545
666
|
};
|
|
546
667
|
if (pickUp !== undefined && (!config.companionEnabled || deps.companion === undefined)) {
|
|
@@ -8,7 +8,7 @@ import { companionCatalog } from '../companion/catalog.js';
|
|
|
8
8
|
import { conversationToken } from '../companion/conversation.js';
|
|
9
9
|
import { HEALTH_ALIVE, fullHealthAllowed, healthPayload, readHookHealth } from '../health.js';
|
|
10
10
|
import { clientLabel, methodNotAllowed, readValidatedBody, recheckDevice, sendError, sendJson, } from '../router.js';
|
|
11
|
-
import {
|
|
11
|
+
import { serverCapabilities } from '../version.js';
|
|
12
12
|
/**
|
|
13
13
|
* Ciało `POST /api/voice/token`: puste i takie ma zostać.
|
|
14
14
|
*
|
|
@@ -98,9 +98,9 @@ export async function routeSystem(ctx, req, res, rq) {
|
|
|
98
98
|
apnsEnabled: apns !== undefined,
|
|
99
99
|
/*
|
|
100
100
|
* Możliwości po nazwie (`version.ts`) — apka sprawdza, czy serwer umie to, czego ona
|
|
101
|
-
* potrzebuje, zamiast porównywać numery wersji.
|
|
101
|
+
* potrzebuje, zamiast porównywać numery wersji. `inbox` only while the server keeps an Inbox.
|
|
102
102
|
*/
|
|
103
|
-
capabilities:
|
|
103
|
+
capabilities: serverCapabilities({ inbox: deps.inbox !== undefined, files: config.filesEnabled }),
|
|
104
104
|
/*
|
|
105
105
|
* System maszyny serwera (`serverSystem` w `version.ts`): apka bierze z niego słowo maszyny, „Mac” albo
|
|
106
106
|
* „VPS”. Pole jest zawsze; klient czyta brak (serwer sprzed pola) jako „nie wiem”.
|
|
@@ -267,7 +267,8 @@ export async function routeSystem(ctx, req, res, rq) {
|
|
|
267
267
|
if (method !== 'GET')
|
|
268
268
|
return methodNotAllowed(res, 'GET');
|
|
269
269
|
// `Accept-Encoding` decyduje o kompresji strumienia (`RAROG_SSE_GZIP`).
|
|
270
|
-
|
|
270
|
+
// `inbox=1`: an app that shows the Inbox asks for its heads (`docs/implementation-spec.md`, "Inbox").
|
|
271
|
+
hub.openGlobal(res, owner, req.headers['accept-encoding'], rq.url.searchParams.get('inbox') === '1');
|
|
271
272
|
return true;
|
|
272
273
|
}
|
|
273
274
|
return false;
|
package/server/src/safe-read.js
CHANGED
|
@@ -18,9 +18,16 @@
|
|
|
18
18
|
* wołający ma już własną obsługę „nie da się przeczytać”, a `ensureGitExclude` odróżnia po `ENOENT` brak pliku
|
|
19
19
|
* od pliku, którego nie wolno ruszyć. Pilnuje tego strażnik `server/test/pliki-zwykle-guard.test.ts`: odczyt
|
|
20
20
|
* z `node:fs` poza tym modułem stoi na jego liście wyjątków z powodem.
|
|
21
|
+
*
|
|
22
|
+
* A file sent on in pieces opens with {@link openRegularFile} and is read with {@link exactReader}: exactly the bytes
|
|
23
|
+
* of its `fstat`, from that one descriptor.
|
|
24
|
+
*
|
|
25
|
+
* Besides regular files, one directory read lives here, because on Linux it opens the directory:
|
|
26
|
+
* {@link readDirectoryByIdentity} lists a directory through its `dev` and `ino` rather than its path. On Linux that
|
|
27
|
+
* binds the read to the checked directory; on macOS it narrows the window of a swap but does not close it.
|
|
21
28
|
*/
|
|
22
29
|
import { closeSync, constants, fstatSync, openSync, readSync } from 'node:fs';
|
|
23
|
-
import { open } from 'node:fs/promises';
|
|
30
|
+
import { open, readdir } from 'node:fs/promises';
|
|
24
31
|
/** Pierwszy odczyt bierze rozmiar z `fstat` (+1, żeby od razu zobaczyć koniec); plik rosnący dostaje więcej. */
|
|
25
32
|
const GROW_FACTOR = 2;
|
|
26
33
|
function openFlags(followSymlink) {
|
|
@@ -92,6 +99,84 @@ export async function readRegularFile(path, opts) {
|
|
|
92
99
|
await handle.close();
|
|
93
100
|
}
|
|
94
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* The open of {@link readRegularFile} without the read, for a caller that sends the file on in pieces: the same
|
|
104
|
+
* flags and the `fstat` on the descriptor; not a regular file → the handle is closed and the result is `null`. The
|
|
105
|
+
* caller closes the handle. Throws on an error of the open, like the readers.
|
|
106
|
+
*/
|
|
107
|
+
export async function openRegularFile(path, opts) {
|
|
108
|
+
const handle = await open(path, openFlags(opts.followSymlink));
|
|
109
|
+
try {
|
|
110
|
+
const stats = await handle.stat({ bigint: true });
|
|
111
|
+
if (stats.isFile())
|
|
112
|
+
return { handle, stats };
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
await handle.close();
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
await handle.close();
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
/** Bytes per piece of {@link exactReader} by default. */
|
|
122
|
+
export const EXACT_PIECE_BYTES = 64 * 1024;
|
|
123
|
+
/**
|
|
124
|
+
* Reads the first `size` bytes of a file open on `handle` (the `fstat` size of {@link openRegularFile}), one piece
|
|
125
|
+
* of at most `pieceBytes` per call, each at its own offset: the position of the descriptor does not matter, and bytes
|
|
126
|
+
* appended past `size` are never read. `short`: the file ended before `size` bytes, so it was cut since its `fstat`
|
|
127
|
+
* and what was read is not the file. The caller calls it one step at a time, owns the handle and closes it; an error
|
|
128
|
+
* of the read rejects.
|
|
129
|
+
*/
|
|
130
|
+
export function exactReader(handle, size, pieceBytes = EXACT_PIECE_BYTES) {
|
|
131
|
+
let position = 0;
|
|
132
|
+
return async () => {
|
|
133
|
+
if (position >= size)
|
|
134
|
+
return { kind: 'done' };
|
|
135
|
+
const buffer = Buffer.allocUnsafe(Math.min(pieceBytes, size - position));
|
|
136
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, position);
|
|
137
|
+
if (bytesRead === 0)
|
|
138
|
+
return { kind: 'short', read: position };
|
|
139
|
+
position += bytesRead;
|
|
140
|
+
return { kind: 'piece', bytes: bytesRead === buffer.length ? buffer : buffer.subarray(0, bytesRead) };
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const DIRECTORY_FLAGS = constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
|
144
|
+
const entriesOf = (dir) => readdir(dir, { withFileTypes: true, encoding: 'buffer' });
|
|
145
|
+
export const DIRECTORY_IO = {
|
|
146
|
+
platform: process.platform,
|
|
147
|
+
openDirectory: (dir) => open(dir, DIRECTORY_FLAGS),
|
|
148
|
+
readOpen: (fd) => entriesOf(`/proc/self/fd/${String(fd)}`),
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Entries of the directory `dir` whose `stat` gave `identity`, names as bytes, read through that identity instead of
|
|
152
|
+
* the path:
|
|
153
|
+
* - macOS: `/.vol/<dev>/<ino>`. The kernel turns the inode back into its current path and looks that path up within
|
|
154
|
+
* the one call (a parent without search permission refuses it), so a swap made before the call does not redirect
|
|
155
|
+
* it. A swap that lands inside the call still can: Node has no descriptor-based directory read on macOS, so this
|
|
156
|
+
* narrows the race and the caller's checks after the read narrow it further, but neither closes it.
|
|
157
|
+
* - Linux: the directory opened with `O_DIRECTORY | O_NOFOLLOW`, its `fstat` must carry `identity`, and the entries
|
|
158
|
+
* come through `/proc/self/fd/<fd>`, that is from the open descriptor. The descriptor is closed on every path.
|
|
159
|
+
*
|
|
160
|
+
* `null`: another platform, or another directory stands under `dir` now. Throws on an error of the open or the read,
|
|
161
|
+
* as `readdir` does (`EACCES` for a directory the server may not read, `ENOENT` for one removed).
|
|
162
|
+
*/
|
|
163
|
+
export async function readDirectoryByIdentity(dir, identity, io = {}) {
|
|
164
|
+
const { platform, openDirectory, readOpen } = { ...DIRECTORY_IO, ...io };
|
|
165
|
+
if (platform === 'darwin')
|
|
166
|
+
return entriesOf(`/.vol/${String(identity.dev)}/${String(identity.ino)}`);
|
|
167
|
+
if (platform !== 'linux')
|
|
168
|
+
return null;
|
|
169
|
+
const handle = await openDirectory(dir);
|
|
170
|
+
try {
|
|
171
|
+
const info = await handle.stat({ bigint: true });
|
|
172
|
+
if (!info.isDirectory() || info.dev !== identity.dev || info.ino !== identity.ino)
|
|
173
|
+
return null;
|
|
174
|
+
return await readOpen(handle.fd);
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
await handle.close();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
95
180
|
/**
|
|
96
181
|
* Dopisuje `text` na koniec pliku zwykłego, tworząc go, gdy go nie ma (prawa `0644` przed umaskiem).
|
|
97
182
|
*
|
|
@@ -42,11 +42,15 @@ export function companionTargetOf(session, config) {
|
|
|
42
42
|
/**
|
|
43
43
|
* {@link companionTargetOf} with the newest message of the session's transcript in memory, for the voice agent's
|
|
44
44
|
* session catalog, only when that message belongs to the engine conversation the session writes now
|
|
45
|
-
* (`Session.lastMessageOfConversation`)
|
|
45
|
+
* (`Session.lastMessageOfConversation`), and the read mark of that memory (`Session.readMark`), which the turns route
|
|
46
|
+
* takes with the conversation before it reads the file. It reads the session only: no file, no mark of a card or a
|
|
47
|
+
* message as shown.
|
|
46
48
|
*/
|
|
47
49
|
export function companionEntryOf(session, config) {
|
|
48
50
|
const target = companionTargetOf(session, config);
|
|
49
|
-
return target === null
|
|
51
|
+
return target === null
|
|
52
|
+
? null
|
|
53
|
+
: { ...target, lastMessage: session.lastMessageOfConversation(), readMark: session.readMark };
|
|
50
54
|
}
|
|
51
55
|
/**
|
|
52
56
|
* Whether the session is in the engine conversation a client read: the conversation the session writes (`init` of
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Dynamiczny import scenariuszy demo — wołany WYŁĄCZNIE przy `config.demo`; treść z podanego zestawu. */
|
|
2
|
+
export async function loadDemoModules(content) {
|
|
3
|
+
const seeds = await import('../demo/seeds.js');
|
|
4
|
+
const files = await import('../demo/files.js');
|
|
5
|
+
return {
|
|
6
|
+
demoSeeds: (now) => seeds.demoSeeds(content, now),
|
|
7
|
+
demoBlocks: seeds.demoBlocks,
|
|
8
|
+
demoProjects: () => seeds.demoProjects(content),
|
|
9
|
+
demoResumable: (cwd, now) => seeds.demoResumable(content, cwd, now),
|
|
10
|
+
demoDiff: () => seeds.demoDiff(content),
|
|
11
|
+
demoDiffFiles: seeds.demoDiffFiles,
|
|
12
|
+
demoDiffFile: (path) => seeds.demoDiffFile(content, path),
|
|
13
|
+
demoFileEntries: files.demoFileEntries,
|
|
14
|
+
demoFileBytes: files.demoFileBytes,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The log line of a diff read: `git: diff list|file <ms> ms`, with the contract code of a refusal. One function over
|
|
3
|
+
* the manager's clock and logger; it reads nothing and holds no state. The manager re-exports the phrases, so
|
|
4
|
+
* importers keep their path. This module does not import `manager.ts`.
|
|
5
|
+
*/
|
|
6
|
+
import { DiffError } from '../git-diff.js';
|
|
7
|
+
/** Fraza linii kosztu listy plików gita czytanej od nowa: `git: diff list <ms> ms`. */
|
|
8
|
+
export const GIT_DIFF_LIST_LOG = 'git: diff list';
|
|
9
|
+
/** Fraza linii kosztu patcha jednego pliku: `git: diff file <ms> ms`. */
|
|
10
|
+
export const GIT_DIFF_FILE_LOG = 'git: diff file';
|
|
11
|
+
/**
|
|
12
|
+
* Linia `git: diff list|file <ms>` w logu: koszt list gita czytanych od nowa przy każdym
|
|
13
|
+
* żądaniu, czyli liczba, po której zapadnie decyzja o pamięci podręcznej. Niesie czas i — przy
|
|
14
|
+
* odmowie — kod z kontraktu; NIGDY ścieżki ani nazwy pliku.
|
|
15
|
+
*/
|
|
16
|
+
export async function measureDiff(phrase, clock, read) {
|
|
17
|
+
const startedAt = clock.now();
|
|
18
|
+
const line = (suffix) => {
|
|
19
|
+
clock.log(`${phrase} ${String(clock.now() - startedAt)} ms${suffix}`);
|
|
20
|
+
};
|
|
21
|
+
try {
|
|
22
|
+
const result = await read();
|
|
23
|
+
line('');
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
line(` (${error instanceof DiffError ? error.code : 'error'})`);
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|