feat: сохранять настройки и сессии супервизора в Pi 1.4

This commit is contained in:
Suslov-MD
2026-10-25 15:53:47 +03:00
parent ced355b0ee
commit a73e09d5a1
21 changed files with 1134 additions and 142 deletions
+68
View File
@@ -1,3 +1,4 @@
import type { SavedObserver } from './checkpoint.ts';
import { randomUUID } from 'node:crypto';
import type { AssistantMessage, Message, UserMessage } from '@earendil-works/pi-ai';
import { eventKey, isRecord, parseReport } from './evidence.ts';
@@ -60,6 +61,7 @@ function nextBatch(
/** In-memory observer. Transport and UI are injected; neither can edit the worker. */
export class SupervisorObserver {
readonly onCommit: NonNullable<ObserverOptions['onCommit']>;
readonly request: ObserverOptions['request'];
readonly onChange: NonNullable<ObserverOptions['onChange']>;
readonly inputLimit: number;
@@ -87,6 +89,7 @@ export class SupervisorObserver {
constructor({
request,
onChange = () => {},
onCommit = () => {},
contextWindow,
maxOutputTokens = 4096,
timeoutMs = 240_000,
@@ -99,6 +102,7 @@ export class SupervisorObserver {
) {
throw new Error('invalid-config');
}
this.onCommit = onCommit;
this.request = request;
this.onChange = onChange;
this.inputLimit = contextWindow - maxOutputTokens - 2048;
@@ -191,6 +195,7 @@ export class SupervisorObserver {
this.knownIds.add(context.id);
}
this.contextTokens += bytes(this.messages) + 128;
this.commit();
this.emit();
}
@@ -214,6 +219,9 @@ export class SupervisorObserver {
}
retry(snapshot: Snapshot): void {
if (this.paused === 'storage-failed') {
this.commit();
}
this.paused = '';
this.offer(snapshot);
}
@@ -257,6 +265,7 @@ export class SupervisorObserver {
this.usage = readUsage(response.usage);
const total = this.usage ? Object.values(this.usage).reduce((sum, count) => sum + count, 0) : 0;
this.contextTokens = total > 0 ? total + 128 : estimate + bytes(response) + 128;
this.commit();
this.emit();
}
@@ -314,6 +323,65 @@ export class SupervisorObserver {
}
}
checkpoint(): SavedObserver {
if (!this.snapshot) {
throw new Error('invalid-checkpoint');
}
return structuredClone({
checkpoint: {
version: 1,
sessionId: this.sessionId,
sourceId: this.snapshot.sourceId,
keys: this.keys.slice(0, this.accepted),
skipped: this.skipped,
reviews: this.reviews,
contextTokens: this.contextTokens,
knownIds: [...this.knownIds],
},
messages: this.messages,
});
}
private commit(): void {
try {
this.onCommit(this.checkpoint());
} catch {
this.paused = 'storage-failed';
throw new Error('storage-failed');
}
}
resume(saved: SavedObserver, snapshot: Snapshot): void {
const state = saved.checkpoint;
const keys = snapshot.events.map(eventKey);
if (
state.sourceId !== snapshot.sourceId ||
state.keys.some((key, index) => keys[index] !== key)
) {
throw new Error('source-changed');
}
const knownIds = new Set(state.knownIds);
const last = saved.messages.findLast((message) => message.role === 'assistant');
const report = last ? parseReport(last, knownIds) : [];
this.cancel();
this.resetState();
this.active = true;
this.sessionId = state.sessionId;
this.snapshot = structuredClone(snapshot);
this.keys = keys;
this.accepted = state.keys.length;
this.skipped = state.skipped;
this.reviews = state.reviews;
this.contextTokens = state.contextTokens;
this.knownIds = knownIds;
this.messages = structuredClone(saved.messages);
this.report = report;
this.usage = last ? readUsage(last.usage) : null;
this.commit();
this.pump();
this.emit();
}
async idle(): Promise<void> {
await this.task;
}