dsh-paseo-host 0.1.0 → 0.1.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/lib/host.js +88 -1
- package/lib/scan.js +6 -1
- package/lib/sync.js +44 -4
- package/package.json +1 -1
package/lib/host.js
CHANGED
|
@@ -38,6 +38,8 @@ const METHODS = {
|
|
|
38
38
|
workspaces: "workspace/list",
|
|
39
39
|
answer: "session/answer",
|
|
40
40
|
approval: "session/approval",
|
|
41
|
+
rewind: "session/rewind",
|
|
42
|
+
currentSeq: "session/current-seq",
|
|
41
43
|
};
|
|
42
44
|
|
|
43
45
|
export const HOST_DEFAULTS = {
|
|
@@ -120,6 +122,26 @@ function errorOf(error) {
|
|
|
120
122
|
return { code, message };
|
|
121
123
|
}
|
|
122
124
|
|
|
125
|
+
/** One page big enough to cover a normal session's log for a rewind boundary. */
|
|
126
|
+
const REWIND_EVENT_LIMIT = 5000;
|
|
127
|
+
|
|
128
|
+
/** Inclusive boundary before the turn that owns `seq`, mirroring the stdio bridge. */
|
|
129
|
+
function rewindBoundaryOf(events, seq) {
|
|
130
|
+
const at = events.findIndex(event => event?.seq === seq);
|
|
131
|
+
if (at < 0) throw new Error(`rewind seq ${seq} is not in the session log`);
|
|
132
|
+
let boundary = seq;
|
|
133
|
+
for (let i = at; i >= 0; i -= 1) {
|
|
134
|
+
const event = events[i];
|
|
135
|
+
if (event === undefined) break;
|
|
136
|
+
if (event.type === "turn/start") {
|
|
137
|
+
boundary = event.seq - 1;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
if (event.type === "turn/end") break;
|
|
141
|
+
}
|
|
142
|
+
return boundary;
|
|
143
|
+
}
|
|
144
|
+
|
|
123
145
|
|
|
124
146
|
/** Paseo route ids and Host catalog provider ids do not always match (opencode-go vs opencode). */
|
|
125
147
|
function providerCandidates(provider) {
|
|
@@ -397,6 +419,8 @@ class Connection {
|
|
|
397
419
|
steering: true,
|
|
398
420
|
images: false,
|
|
399
421
|
commands: true,
|
|
422
|
+
// Rewinding forks the durable prefix through the session controller.
|
|
423
|
+
rewind: true,
|
|
400
424
|
},
|
|
401
425
|
};
|
|
402
426
|
}
|
|
@@ -464,6 +488,10 @@ class Connection {
|
|
|
464
488
|
this.settle(requestId, "approval", behavior);
|
|
465
489
|
return {};
|
|
466
490
|
}
|
|
491
|
+
case METHODS.rewind:
|
|
492
|
+
return await this.#rewind(controller, params);
|
|
493
|
+
case METHODS.currentSeq:
|
|
494
|
+
return await this.#currentSeq(controller, params);
|
|
467
495
|
default:
|
|
468
496
|
throw Object.assign(new Error(`unknown method: ${method}`), { code: "UNSUPPORTED_CAPABILITY" });
|
|
469
497
|
}
|
|
@@ -615,7 +643,17 @@ class Connection {
|
|
|
615
643
|
mode: params.delivery === "steer" ? "steer" : "queue",
|
|
616
644
|
content,
|
|
617
645
|
}, new AbortController().signal);
|
|
618
|
-
|
|
646
|
+
// The durable boundary lets Paseo attach a rewind token to this message.
|
|
647
|
+
// A missing seq only costs that token, never the prompt.
|
|
648
|
+
let seq;
|
|
649
|
+
try {
|
|
650
|
+
const events = await this.#readEvents(controller, sessionId);
|
|
651
|
+
const last = events.length > 0 ? events[events.length - 1] : undefined;
|
|
652
|
+
if (typeof last?.seq === "number") seq = last.seq;
|
|
653
|
+
} catch {
|
|
654
|
+
/* rewind stays unavailable for this message */
|
|
655
|
+
}
|
|
656
|
+
return { disposition: "started", ...(seq === undefined ? {} : { seq }) };
|
|
619
657
|
}
|
|
620
658
|
|
|
621
659
|
async #cancel(controller, params) {
|
|
@@ -625,6 +663,55 @@ class Connection {
|
|
|
625
663
|
return {};
|
|
626
664
|
}
|
|
627
665
|
|
|
666
|
+
/** Read the stored log for one session, oldest first. */
|
|
667
|
+
async #readEvents(controller, sessionId) {
|
|
668
|
+
const page = await controller.events({
|
|
669
|
+
address: { kind: "session", sessionId },
|
|
670
|
+
maxMessages: REWIND_EVENT_LIMIT,
|
|
671
|
+
}, new AbortController().signal);
|
|
672
|
+
return (Array.isArray(page?.records) ? page.records : [])
|
|
673
|
+
.filter(record => record !== null && typeof record === "object" && record.type === "event" && record.event)
|
|
674
|
+
.map(record => record.event);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
async #currentSeq(controller, params) {
|
|
678
|
+
const sessionId = typeof params.sessionId === "string" ? params.sessionId : this.#sessionId;
|
|
679
|
+
if (sessionId === undefined) throw new Error("session/current-seq requires an open session");
|
|
680
|
+
const events = await this.#readEvents(controller, sessionId);
|
|
681
|
+
const last = events.length > 0 ? events[events.length - 1] : undefined;
|
|
682
|
+
return { seq: typeof last?.seq === "number" ? last.seq : 0 };
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* Fork the durable prefix before the turn that owns `seq`.
|
|
687
|
+
*
|
|
688
|
+
* DSH never deletes events, so rewinding is a fork: the controller copies
|
|
689
|
+
* the prefix, hands back a new session id, and Paseo reopens that id. A turn
|
|
690
|
+
* still running is cancelled first — forking an open turn would snapshot it.
|
|
691
|
+
*/
|
|
692
|
+
async #rewind(controller, params) {
|
|
693
|
+
const sessionId = typeof params.sessionId === "string" ? params.sessionId : this.#sessionId;
|
|
694
|
+
if (sessionId === undefined) throw new Error("session/rewind requires an open session");
|
|
695
|
+
const seq = params.seq;
|
|
696
|
+
if (!Number.isSafeInteger(seq) || seq < 0) throw new Error("session/rewind requires a valid event seq");
|
|
697
|
+
|
|
698
|
+
try {
|
|
699
|
+
await controller.cancel({ sessionId });
|
|
700
|
+
} catch {
|
|
701
|
+
/* idle session */
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const events = await this.#readEvents(controller, sessionId);
|
|
705
|
+
const boundary = rewindBoundaryOf(events, seq);
|
|
706
|
+
if (boundary < 0) throw new Error("cannot rewind past the very first message");
|
|
707
|
+
const forked = await controller.fork({ sessionId, atSeq: boundary });
|
|
708
|
+
const newId = typeof forked?.sessionId === "string" ? forked.sessionId : undefined;
|
|
709
|
+
if (newId === undefined || newId === "") throw new Error("session/rewind returned no session id");
|
|
710
|
+
this.#sessionId = newId;
|
|
711
|
+
trace(`rewind ${sessionId} at ${seq} -> ${newId} (boundary ${boundary})`);
|
|
712
|
+
return { sessionId: newId };
|
|
713
|
+
}
|
|
714
|
+
|
|
628
715
|
#closeSession() {
|
|
629
716
|
this.#followAbort?.abort();
|
|
630
717
|
this.#followAbort = undefined;
|
package/lib/scan.js
CHANGED
|
@@ -198,7 +198,7 @@ let agentsCache = { root: '', at: 0, value: null };
|
|
|
198
198
|
* cheap but happens in every paseo session process, so a short TTL keeps N
|
|
199
199
|
* concurrent sessions from each re-reading the tree every tick.
|
|
200
200
|
*/
|
|
201
|
-
export function readPaseoAgentsCached(root, ttlMs =
|
|
201
|
+
export function readPaseoAgentsCached(root, ttlMs = 5000) {
|
|
202
202
|
const now = Date.now();
|
|
203
203
|
if (agentsCache.value !== null && agentsCache.root === root && now - agentsCache.at < ttlMs) {
|
|
204
204
|
return agentsCache.value;
|
|
@@ -207,3 +207,8 @@ export function readPaseoAgentsCached(root, ttlMs = 30000) {
|
|
|
207
207
|
agentsCache = { root, at: now, value };
|
|
208
208
|
return value;
|
|
209
209
|
}
|
|
210
|
+
|
|
211
|
+
/** Force the next read to hit disk; a filesystem event proves the cache is stale. */
|
|
212
|
+
export function invalidatePaseoAgentsCache() {
|
|
213
|
+
agentsCache = { root: '', at: 0, value: null };
|
|
214
|
+
}
|
package/lib/sync.js
CHANGED
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
*
|
|
21
21
|
* @module dsh-paseo-host
|
|
22
22
|
*/
|
|
23
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
23
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, watch, writeFileSync } from 'node:fs';
|
|
24
24
|
import { dirname, join } from 'node:path';
|
|
25
25
|
import { buildPlan, planIsEmpty } from './plan.js';
|
|
26
26
|
import {
|
|
27
|
-
dshHome, isDirectory, isUsefulTitle, paseoHome, readDshWorkspaceState, readPaseoAgentsCached, readPaseoWorkspaces,
|
|
27
|
+
dshHome, invalidatePaseoAgentsCache, isDirectory, isUsefulTitle, paseoHome, readDshWorkspaceState, readPaseoAgentsCached, readPaseoWorkspaces,
|
|
28
28
|
} from './scan.js';
|
|
29
29
|
|
|
30
30
|
const LOG_MAX_BYTES = 512 * 1024;
|
|
@@ -34,7 +34,7 @@ export const SYNC_DEFAULTS = {
|
|
|
34
34
|
/** Paseo 数据根;空串 = ~/.paseo。 */
|
|
35
35
|
paseoRoot: '',
|
|
36
36
|
/** 轮询间隔;新建会话后最迟这么久进入工作区。 */
|
|
37
|
-
intervalMs:
|
|
37
|
+
intervalMs: 120000,
|
|
38
38
|
/** 首次扫描延迟,给 workspace registry / sessionPersistence 留启动时间。 */
|
|
39
39
|
initialDelayMs: 4000,
|
|
40
40
|
/** 是否补工作区归属(desktop profile 用)。 */
|
|
@@ -465,8 +465,15 @@ export function startLiveStatusRelay(ctx, options) {
|
|
|
465
465
|
export function startSyncLoop(ctx, options, memo) {
|
|
466
466
|
let stopped = false;
|
|
467
467
|
let running = false;
|
|
468
|
+
let pendingRun = false;
|
|
468
469
|
const run = () => {
|
|
469
|
-
if (stopped
|
|
470
|
+
if (stopped) return;
|
|
471
|
+
// An event that lands mid-pass must not be dropped: the fallback interval
|
|
472
|
+
// is minutes away, so queue one more pass for right after this one.
|
|
473
|
+
if (running) {
|
|
474
|
+
pendingRun = true;
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
470
477
|
running = true;
|
|
471
478
|
void tick(ctx, options, memo)
|
|
472
479
|
.catch(error => {
|
|
@@ -475,8 +482,38 @@ export function startSyncLoop(ctx, options, memo) {
|
|
|
475
482
|
})
|
|
476
483
|
.finally(() => {
|
|
477
484
|
running = false;
|
|
485
|
+
if (pendingRun && !stopped) {
|
|
486
|
+
pendingRun = false;
|
|
487
|
+
run();
|
|
488
|
+
}
|
|
478
489
|
});
|
|
479
490
|
};
|
|
491
|
+
// Event-driven: an archive or unarchive rewrites the agent record, and a new
|
|
492
|
+
// agent appears as a new JSON file. Watching the tree turns the old 30s poll
|
|
493
|
+
// into a sub-second reaction; the interval below stays as a fallback for
|
|
494
|
+
// missed events (watch limits, plugin reload).
|
|
495
|
+
const paseoRoot = paseoHome(options.paseoRoot);
|
|
496
|
+
let watcher;
|
|
497
|
+
let watchTimer;
|
|
498
|
+
try {
|
|
499
|
+
watcher = watch(join(paseoRoot, 'agents'), { recursive: true }, (_event, filename) => {
|
|
500
|
+
if (filename === null || !String(filename).endsWith('.json')) return;
|
|
501
|
+
if (watchTimer !== undefined) clearTimeout(watchTimer);
|
|
502
|
+
watchTimer = setTimeout(() => {
|
|
503
|
+
watchTimer = undefined;
|
|
504
|
+
invalidatePaseoAgentsCache();
|
|
505
|
+
run();
|
|
506
|
+
}, 1500);
|
|
507
|
+
watchTimer.unref?.();
|
|
508
|
+
});
|
|
509
|
+
watcher.on('error', () => {
|
|
510
|
+
watcher?.close();
|
|
511
|
+
watcher = undefined;
|
|
512
|
+
});
|
|
513
|
+
} catch {
|
|
514
|
+
watcher = undefined; // the poll fallback still covers it
|
|
515
|
+
}
|
|
516
|
+
|
|
480
517
|
// A sub-5s loop would re-scan every session file on each tick; treat anything
|
|
481
518
|
// below that as a misconfiguration and clamp up.
|
|
482
519
|
const interval = Math.max(5000, Number(options.intervalMs) || SYNC_DEFAULTS.intervalMs);
|
|
@@ -496,5 +533,8 @@ export function startSyncLoop(ctx, options, memo) {
|
|
|
496
533
|
stopped = true;
|
|
497
534
|
clearTimeout(first);
|
|
498
535
|
clearInterval(timer);
|
|
536
|
+
if (watchTimer !== undefined) clearTimeout(watchTimer);
|
|
537
|
+
watcher?.close();
|
|
538
|
+
watcher = undefined;
|
|
499
539
|
};
|
|
500
540
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-paseo-host",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH-side companion for paseo-dsh: a loopback host socket over the session controller plus workspace, archive, title and live-status sync.",
|
|
6
6
|
"license": "MIT",
|