@farmslot/agent-runtime 0.41.0 → 0.43.0
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/CHANGELOG.md +11 -0
- package/dist/native/account-profiles.d.ts +1 -0
- package/dist/native/account-profiles.d.ts.map +1 -1
- package/dist/native/account-profiles.js +17 -1
- package/dist/native/account-profiles.js.map +1 -1
- package/dist/native/acp.d.ts.map +1 -1
- package/dist/native/acp.js +14 -0
- package/dist/native/acp.js.map +1 -1
- package/dist/native/claude.d.ts.map +1 -1
- package/dist/native/claude.js +52 -1
- package/dist/native/claude.js.map +1 -1
- package/dist/native/client.d.ts +11 -2
- package/dist/native/client.d.ts.map +1 -1
- package/dist/native/client.js +63 -2
- package/dist/native/client.js.map +1 -1
- package/dist/native/codex.d.ts.map +1 -1
- package/dist/native/codex.js +33 -5
- package/dist/native/codex.js.map +1 -1
- package/dist/native/conversation-ipc.d.ts +12 -0
- package/dist/native/conversation-ipc.d.ts.map +1 -0
- package/dist/native/conversation-ipc.js +126 -0
- package/dist/native/conversation-ipc.js.map +1 -0
- package/dist/native/conversation.d.ts +52 -0
- package/dist/native/conversation.d.ts.map +1 -0
- package/dist/native/conversation.js +313 -0
- package/dist/native/conversation.js.map +1 -0
- package/dist/native/host.js +51 -1
- package/dist/native/host.js.map +1 -1
- package/dist/native/ipc.d.ts +26 -1
- package/dist/native/ipc.d.ts.map +1 -1
- package/dist/native/ipc.js +54 -1
- package/dist/native/ipc.js.map +1 -1
- package/dist/native/manager.d.ts +13 -2
- package/dist/native/manager.d.ts.map +1 -1
- package/dist/native/manager.js +230 -6
- package/dist/native/manager.js.map +1 -1
- package/dist/native/msp-stream.d.ts +14 -0
- package/dist/native/msp-stream.d.ts.map +1 -0
- package/dist/native/msp-stream.js +65 -0
- package/dist/native/msp-stream.js.map +1 -0
- package/dist/native/observation.d.ts +9 -4
- package/dist/native/observation.d.ts.map +1 -1
- package/dist/native/observation.js +116 -52
- package/dist/native/observation.js.map +1 -1
- package/dist/native/pi.d.ts.map +1 -1
- package/dist/native/pi.js +6 -0
- package/dist/native/pi.js.map +1 -1
- package/dist/native/process-tree.d.ts +4 -4
- package/dist/native/process-tree.d.ts.map +1 -1
- package/dist/native/process-tree.js +82 -20
- package/dist/native/process-tree.js.map +1 -1
- package/dist/native/process.d.ts.map +1 -1
- package/dist/native/process.js +7 -9
- package/dist/native/process.js.map +1 -1
- package/dist/native/service.d.ts +1 -1
- package/dist/native/service.d.ts.map +1 -1
- package/dist/native/service.js +13 -1
- package/dist/native/service.js.map +1 -1
- package/dist/native/storage.d.ts +1 -0
- package/dist/native/storage.d.ts.map +1 -1
- package/dist/native/storage.js +16 -3
- package/dist/native/storage.js.map +1 -1
- package/dist/native/supervisor.js +13 -5
- package/dist/native/supervisor.js.map +1 -1
- package/dist/native/types.d.ts +5 -1
- package/dist/native/types.d.ts.map +1 -1
- package/dist/native/worker-history.d.ts +2 -2
- package/dist/native/worker-history.d.ts.map +1 -1
- package/dist/native/worker-history.js +30 -19
- package/dist/native/worker-history.js.map +1 -1
- package/dist/native/worker-service.d.ts +1 -1
- package/dist/native/worker-service.d.ts.map +1 -1
- package/dist/native/worker-service.js +2 -1
- package/dist/native/worker-service.js.map +1 -1
- package/dist/native/workspace.d.ts.map +1 -1
- package/dist/native/workspace.js +15 -1
- package/dist/native/workspace.js.map +1 -1
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +10 -0
- package/dist/version.js.map +1 -0
- package/package.json +9 -3
package/dist/native/manager.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type NativeCommandReceipt, type NativeSessionCreateParams, type NativeSessionEnsureParams, type NativeSessionEvent, type NativeSessionInfo, type NativeSessionRequester, type NativeSessionResponse, type NativeSessionSendResult } from '@farmslot/protocol';
|
|
1
|
+
import { type NativeApprovalMode, type NativeCommandObservation, type NativeCommandReceipt, type NativeSessionCreateParams, type NativeSessionEnsureParams, type NativeSessionEvent, type NativeSessionInfo, type NativeSessionRequester, type NativeSessionResponse, type NativeSessionSendResult } from '@farmslot/protocol';
|
|
2
|
+
import { NativeConversationJournal } from './conversation.js';
|
|
2
3
|
import { type NativeWorkerCancelResult, type NativeWorkerLaunch, type NativeWorkerResumeParams } from './worker-launch.js';
|
|
3
4
|
export declare function resolveNativeExecutable(binary: string, cwd: string, environment?: NodeJS.ProcessEnv): Promise<{
|
|
4
5
|
executable: string;
|
|
@@ -9,6 +10,7 @@ export declare class NativeSessionManager {
|
|
|
9
10
|
private readonly root;
|
|
10
11
|
private readonly executionNodeId;
|
|
11
12
|
private sessions;
|
|
13
|
+
private readonly conversationGeneration;
|
|
12
14
|
private readonly processObservations;
|
|
13
15
|
/** Responses being delivered, by request ID. A resolution outside delivery has no known decision. */
|
|
14
16
|
private readonly delivering;
|
|
@@ -34,7 +36,12 @@ export declare class NativeSessionManager {
|
|
|
34
36
|
cancelWorker(owner: string, id: string, leaseId: string, generation?: string, sourceLeaseId?: string, resumeCommandId?: string): Promise<NativeWorkerCancelResult>;
|
|
35
37
|
transferWorker(owner: string, id: string, generation: string, leaseId: string, launch: NativeWorkerLaunch): NativeSessionInfo;
|
|
36
38
|
private append;
|
|
39
|
+
private sessionRecord;
|
|
37
40
|
private owned;
|
|
41
|
+
inspect(owner: string, id: string): NativeSessionInfo;
|
|
42
|
+
commandStatus(owner: string, id: string, commandId: string): NativeCommandObservation;
|
|
43
|
+
/** Command acceptance observes receipt metadata, never a transcript read or a retry. */
|
|
44
|
+
waitCommandStatus(owner: string, id: string, commandId: string, signal: AbortSignal): Promise<NativeCommandObservation>;
|
|
38
45
|
private reconcileMissingProcess;
|
|
39
46
|
private snapshot;
|
|
40
47
|
list(owner: string): NativeSessionInfo[];
|
|
@@ -57,7 +64,7 @@ export declare class NativeSessionManager {
|
|
|
57
64
|
}[];
|
|
58
65
|
pendingRequests: NativeSessionEvent[];
|
|
59
66
|
};
|
|
60
|
-
readWorker(owner: string, id: string, leaseId: string, after?: number, limit?: number): {
|
|
67
|
+
readWorker(owner: string, id: string, leaseId: string, after?: number, limit?: number, generation?: string): {
|
|
61
68
|
session: NativeSessionInfo;
|
|
62
69
|
events: NativeSessionEvent[];
|
|
63
70
|
cursor: number;
|
|
@@ -66,6 +73,10 @@ export declare class NativeSessionManager {
|
|
|
66
73
|
pendingRequests: NativeSessionEvent[];
|
|
67
74
|
scope?: import("@farmslot/protocol").NativeWorkerHistoryScope;
|
|
68
75
|
};
|
|
76
|
+
setApprovalMode(owner: string, id: string, mode: NativeApprovalMode): Promise<NativeSessionInfo>;
|
|
77
|
+
private updateConversation;
|
|
78
|
+
/** Observation is passive: it neither creates nor resumes the runner. */
|
|
79
|
+
conversation(owner: string, id: string, leaseId?: string, generation?: string): NativeConversationJournal;
|
|
69
80
|
private assertAccountCurrent;
|
|
70
81
|
send(owner: string, id: string, commandId: string, text: string): Promise<NativeSessionSendResult>;
|
|
71
82
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../src/native/manager.ts"],"names":[],"mappings":"AAgBA,OAAO,EACL,KAAK,oBAAoB,EAEzB,KAAK,yBAAyB,EAC9B,KAAK,yBAAyB,EAC9B,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../src/native/manager.ts"],"names":[],"mappings":"AAgBA,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,oBAAoB,EAEzB,KAAK,yBAAyB,EAC9B,KAAK,yBAAyB,EAC9B,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EAEtB,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAG7B,MAAM,oBAAoB,CAAC;AAQ5B,OAAO,EAAE,yBAAyB,EAAmB,MAAM,mBAAmB,CAAC;AAoB/E,OAAO,EACL,KAAK,wBAAwB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,EAAE,MAAM,GAAG,oBAAoB;IA+BjF,OAAO,CAAC,mBAAmB;IAgDrB,OAAO,CACX,KAAK,EAAE,MAAM,EACb,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,IAAI,CAAC;IA0BV,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMnD,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAiDrD;;;OAGG;IACH,OAAO,CAAC,aAAa;IAarB,OAAO,CAAC,mBAAmB;IAI3B,gGAAgG;YAClF,iBAAiB;IAe/B;;;OAGG;YACW,gBAAgB;IA6B9B,gFAAgF;IAChF,OAAO,CAAC,eAAe;IAiCvB,4FAA4F;YAC9E,eAAe;CAY9B"}
|
package/dist/native/manager.js
CHANGED
|
@@ -8,6 +8,7 @@ import { promisify } from 'node:util';
|
|
|
8
8
|
import { nativeStructuredResult, } from '@farmslot/protocol';
|
|
9
9
|
import { assertNativeProfileCurrent, nativeProfileEnvironment, requireNativeProfile, } from './account-profiles.js';
|
|
10
10
|
import { NATIVE_CHECKPOINT_WAIT_MS, NativeCheckpoints, withinMs } from './checkpoint.js';
|
|
11
|
+
import { NativeConversationJournal, splitNativeText } from './conversation.js';
|
|
11
12
|
import { effectiveModelQuestions } from './model-questions.js';
|
|
12
13
|
import { NativeRequestRefused } from './process.js';
|
|
13
14
|
import { journalProcesses } from './process-tree.js';
|
|
@@ -146,6 +147,7 @@ export class NativeSessionManager {
|
|
|
146
147
|
this.root = root;
|
|
147
148
|
this.executionNodeId = executionNodeId;
|
|
148
149
|
this.sessions = new Map();
|
|
150
|
+
this.conversationGeneration = randomUUID();
|
|
149
151
|
this.processObservations = new Map();
|
|
150
152
|
/** Responses being delivered, by request ID. A resolution outside delivery has no known decision. */
|
|
151
153
|
this.delivering = new Map();
|
|
@@ -164,6 +166,7 @@ export class NativeSessionManager {
|
|
|
164
166
|
const commands = new Map();
|
|
165
167
|
const events = [];
|
|
166
168
|
const workerHistory = new NativeWorkerHistory();
|
|
169
|
+
const conversations = new Map();
|
|
167
170
|
for (const entry of entries) {
|
|
168
171
|
workerHistory.observe(entry);
|
|
169
172
|
info = entry.info ?? info;
|
|
@@ -173,6 +176,15 @@ export class NativeSessionManager {
|
|
|
173
176
|
commands.set(command.commandId, command);
|
|
174
177
|
if (entry.event)
|
|
175
178
|
events.push(entry.event);
|
|
179
|
+
if (info)
|
|
180
|
+
this.updateConversation({
|
|
181
|
+
info,
|
|
182
|
+
events,
|
|
183
|
+
commands,
|
|
184
|
+
pendingRequests: new Map(pending.map((event) => [event.request.id, event])),
|
|
185
|
+
workerHistory,
|
|
186
|
+
conversations,
|
|
187
|
+
}, entry.event, Boolean(entry.info || entry.commands || entry.pending));
|
|
176
188
|
}
|
|
177
189
|
if (!info || !context)
|
|
178
190
|
throw new Error('Native journal has no durable session identity');
|
|
@@ -180,6 +192,7 @@ export class NativeSessionManager {
|
|
|
180
192
|
throw new Error('Native journal belongs to another execution node');
|
|
181
193
|
const record = {
|
|
182
194
|
workerHistory,
|
|
195
|
+
conversations,
|
|
183
196
|
info,
|
|
184
197
|
context,
|
|
185
198
|
processes,
|
|
@@ -210,6 +223,7 @@ export class NativeSessionManager {
|
|
|
210
223
|
});
|
|
211
224
|
}
|
|
212
225
|
}
|
|
226
|
+
durableWrite(join(this.root, 'metadata.json'), { revision: randomUUID() });
|
|
213
227
|
}
|
|
214
228
|
persist(record, event) {
|
|
215
229
|
const previous = this.persisted.get(record) ?? { commands: new Map() };
|
|
@@ -254,6 +268,9 @@ export class NativeSessionManager {
|
|
|
254
268
|
previous.commands.set(command.commandId, JSON.stringify(command));
|
|
255
269
|
this.persisted.set(record, { info, context, pending, processes, commands: previous.commands });
|
|
256
270
|
record.workerHistory.observe(entry);
|
|
271
|
+
if (entry.info || entry.commands || entry.pending)
|
|
272
|
+
durableWrite(join(this.root, 'metadata.json'), { revision: randomUUID() });
|
|
273
|
+
this.updateConversation(record, event, Boolean(entry.info || entry.commands || entry.pending));
|
|
257
274
|
}
|
|
258
275
|
create(owner, params, workerLaunch, requester) {
|
|
259
276
|
return this.start(owner, params, undefined, workerLaunch, undefined, undefined, requester);
|
|
@@ -279,6 +296,8 @@ export class NativeSessionManager {
|
|
|
279
296
|
}
|
|
280
297
|
async start(ownerPrincipalId, params, reservedId, workerLaunch, assertResumeCurrent, relocation, requester) {
|
|
281
298
|
assertResumeCurrent?.();
|
|
299
|
+
if (workerLaunch && params.approvalMode !== undefined)
|
|
300
|
+
throw new Error('Workers retain their launch approval policy');
|
|
282
301
|
const profile = params.profileId
|
|
283
302
|
? requireNativeProfile(params.profileId, params.runner)
|
|
284
303
|
: undefined;
|
|
@@ -452,6 +471,9 @@ export class NativeSessionManager {
|
|
|
452
471
|
...resolved,
|
|
453
472
|
model: params.model ?? previous?.info.model,
|
|
454
473
|
mode,
|
|
474
|
+
...(!workerLaunch
|
|
475
|
+
? { approvalMode: params.approvalMode ?? previous?.info.approvalMode ?? 'ask' }
|
|
476
|
+
: {}),
|
|
455
477
|
accountMode: 'native',
|
|
456
478
|
resultSchema: params.resultSchema ?? previous?.info.resultSchema,
|
|
457
479
|
...(effort ? { effort } : {}),
|
|
@@ -474,6 +496,7 @@ export class NativeSessionManager {
|
|
|
474
496
|
};
|
|
475
497
|
const record = {
|
|
476
498
|
workerHistory: previous?.workerHistory ?? new NativeWorkerHistory(),
|
|
499
|
+
conversations: previous?.conversations ?? new Map(),
|
|
477
500
|
info,
|
|
478
501
|
events: previous?.events ?? [],
|
|
479
502
|
commands: previous?.commands ?? new Map(),
|
|
@@ -517,6 +540,7 @@ export class NativeSessionManager {
|
|
|
517
540
|
mode,
|
|
518
541
|
model: info.model,
|
|
519
542
|
effort,
|
|
543
|
+
approvalMode: info.approvalMode,
|
|
520
544
|
onSpawn: (pid, identity) => {
|
|
521
545
|
info.processPid = pid;
|
|
522
546
|
info.processIdentity = identity;
|
|
@@ -576,9 +600,23 @@ export class NativeSessionManager {
|
|
|
576
600
|
throw new Error('Reserved sessionId belongs to another owner');
|
|
577
601
|
if (record.info.workerLeaseId !== workerLaunch?.leaseId)
|
|
578
602
|
throw new Error('Reserved sessionId belongs to another worker task lease');
|
|
579
|
-
for (const field of [
|
|
580
|
-
|
|
581
|
-
|
|
603
|
+
for (const field of [
|
|
604
|
+
'runner',
|
|
605
|
+
'cwd',
|
|
606
|
+
'model',
|
|
607
|
+
'mode',
|
|
608
|
+
'profileId',
|
|
609
|
+
'resultSchema',
|
|
610
|
+
'approvalMode',
|
|
611
|
+
]) {
|
|
612
|
+
const requested = field === 'mode'
|
|
613
|
+
? (params.mode ?? 'default')
|
|
614
|
+
: field === 'approvalMode' && !workerLaunch
|
|
615
|
+
? (params.approvalMode ?? 'ask')
|
|
616
|
+
: params[field];
|
|
617
|
+
if ((field === 'approvalMode' && !workerLaunch
|
|
618
|
+
? (record.info[field] ?? 'ask')
|
|
619
|
+
: record.info[field]) !== requested)
|
|
582
620
|
throw new Error(`Reserved sessionId launch configuration differs: ${field}`);
|
|
583
621
|
}
|
|
584
622
|
if (!workerLaunch && params.effort !== undefined && record.info.effort !== params.effort)
|
|
@@ -836,6 +874,13 @@ export class NativeSessionManager {
|
|
|
836
874
|
return this.snapshot(record);
|
|
837
875
|
}
|
|
838
876
|
append(record, event) {
|
|
877
|
+
if (event.type === 'text.delta' &&
|
|
878
|
+
event.text &&
|
|
879
|
+
Buffer.byteLength(JSON.stringify(event.text)) > 60 * 1024) {
|
|
880
|
+
for (const text of splitNativeText(event.text))
|
|
881
|
+
this.append(record, { ...event, text });
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
839
884
|
event = compactEventDiagnostics(event);
|
|
840
885
|
const structuredResult = event.type === 'turn.completed' &&
|
|
841
886
|
event.status === 'completed' &&
|
|
@@ -942,15 +987,69 @@ export class NativeSessionManager {
|
|
|
942
987
|
this.scheduleQueuedInput(record);
|
|
943
988
|
}
|
|
944
989
|
}
|
|
945
|
-
|
|
990
|
+
sessionRecord(owner, id) {
|
|
946
991
|
const record = this.sessions.get(id);
|
|
947
992
|
if (!record || record.info.ownerPrincipalId !== owner)
|
|
948
993
|
throw Object.assign(new Error('Native session not found for this principal'), {
|
|
949
994
|
code: 'NOT_FOUND',
|
|
950
995
|
});
|
|
996
|
+
return record;
|
|
997
|
+
}
|
|
998
|
+
owned(owner, id, observations) {
|
|
999
|
+
const record = this.sessionRecord(owner, id);
|
|
951
1000
|
this.reconcileMissingProcess(record, observations);
|
|
952
1001
|
return record;
|
|
953
1002
|
}
|
|
1003
|
+
inspect(owner, id) {
|
|
1004
|
+
return this.snapshot(this.sessionRecord(owner, id));
|
|
1005
|
+
}
|
|
1006
|
+
commandStatus(owner, id, commandId) {
|
|
1007
|
+
const record = this.sessionRecord(owner, id);
|
|
1008
|
+
const command = record.commands.get(commandId);
|
|
1009
|
+
if (!command)
|
|
1010
|
+
return { session: this.snapshot(record) };
|
|
1011
|
+
const { text: _text, leaseId: _lease, ...receipt } = command;
|
|
1012
|
+
return { session: this.snapshot(record), receipt: structuredClone(receipt) };
|
|
1013
|
+
}
|
|
1014
|
+
/** Command acceptance observes receipt metadata, never a transcript read or a retry. */
|
|
1015
|
+
waitCommandStatus(owner, id, commandId, signal) {
|
|
1016
|
+
const journal = this.conversation(owner, id);
|
|
1017
|
+
return new Promise((resolve, reject) => {
|
|
1018
|
+
let stopped = false;
|
|
1019
|
+
let timer;
|
|
1020
|
+
let unlisten = () => { };
|
|
1021
|
+
const cleanup = () => {
|
|
1022
|
+
stopped = true;
|
|
1023
|
+
unlisten();
|
|
1024
|
+
if (timer)
|
|
1025
|
+
clearTimeout(timer);
|
|
1026
|
+
signal.removeEventListener('abort', abort);
|
|
1027
|
+
};
|
|
1028
|
+
const abort = () => {
|
|
1029
|
+
cleanup();
|
|
1030
|
+
reject(new Error('Native command observation connection closed'));
|
|
1031
|
+
};
|
|
1032
|
+
const sample = (deadline = false) => {
|
|
1033
|
+
if (stopped)
|
|
1034
|
+
return;
|
|
1035
|
+
const observed = this.commandStatus(owner, id, commandId);
|
|
1036
|
+
if (deadline ||
|
|
1037
|
+
observed.receipt?.accepted ||
|
|
1038
|
+
['failed', 'unknown'].includes(observed.receipt?.state ?? '') ||
|
|
1039
|
+
['closed', 'failed'].includes(observed.session.state)) {
|
|
1040
|
+
cleanup();
|
|
1041
|
+
resolve(observed);
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
unlisten = journal.listen(() => sample());
|
|
1045
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
1046
|
+
timer = setTimeout(() => sample(true), 30_000);
|
|
1047
|
+
if (signal.aborted)
|
|
1048
|
+
abort();
|
|
1049
|
+
else
|
|
1050
|
+
sample();
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
954
1053
|
reconcileMissingProcess(record, observations) {
|
|
955
1054
|
const pid = record.info.processPid;
|
|
956
1055
|
// A live adapter owns descendant cleanup. A recovered journal has no
|
|
@@ -995,6 +1094,7 @@ export class NativeSessionManager {
|
|
|
995
1094
|
...record.info.capabilities,
|
|
996
1095
|
// A host property, not the runner's: this host removes any session's queued input.
|
|
997
1096
|
cancelQueued: true,
|
|
1097
|
+
...(record.info.workerManaged ? { approvalModes: undefined } : {}),
|
|
998
1098
|
...(unavailable ? { resume: false, resumeUnavailableReason: unavailable } : {}),
|
|
999
1099
|
},
|
|
1000
1100
|
};
|
|
@@ -1033,14 +1133,138 @@ export class NativeSessionManager {
|
|
|
1033
1133
|
pendingRequests: [...record.pendingRequests.values()],
|
|
1034
1134
|
};
|
|
1035
1135
|
}
|
|
1036
|
-
readWorker(owner, id, leaseId, after, limit) {
|
|
1136
|
+
readWorker(owner, id, leaseId, after, limit, generation) {
|
|
1037
1137
|
const record = this.owned(owner, id, this.processObservations);
|
|
1038
1138
|
if (record.workerLeaseError)
|
|
1039
1139
|
throw record.workerLeaseError;
|
|
1040
|
-
const result = record.workerHistory.read(leaseId, record.events, [...record.pendingRequests.values()], after, limit);
|
|
1140
|
+
const result = record.workerHistory.read(leaseId, record.events, [...record.pendingRequests.values()], after, limit, generation);
|
|
1041
1141
|
return { ...result, session: this.snapshot({ info: result.session }) };
|
|
1042
1142
|
}
|
|
1143
|
+
async setApprovalMode(owner, id, mode) {
|
|
1144
|
+
if (mode !== 'ask' && mode !== 'bypass')
|
|
1145
|
+
throw new Error('Unknown native approval mode');
|
|
1146
|
+
const record = this.owned(owner, id);
|
|
1147
|
+
this.assertUnmanagedSession(owner, id);
|
|
1148
|
+
if (!record.adapter?.setApprovalMode ||
|
|
1149
|
+
record.info.state !== 'idle' ||
|
|
1150
|
+
record.sending ||
|
|
1151
|
+
record.configuring ||
|
|
1152
|
+
record.pendingRequests.size ||
|
|
1153
|
+
[...record.commands.values()].some((command) => command.generation === record.info.generation &&
|
|
1154
|
+
['pending', 'unknown', 'accepted'].includes(command.state)))
|
|
1155
|
+
throw new Error('Wait for an idle session with no pending input before changing approvals');
|
|
1156
|
+
record.configuring = true;
|
|
1157
|
+
record.info.approvalModePending = mode;
|
|
1158
|
+
try {
|
|
1159
|
+
// Write intent before the native setting. A lost acknowledgement must never send under
|
|
1160
|
+
// a policy different from the one displayed; an explicit idempotent setter can confirm it.
|
|
1161
|
+
this.persist(record);
|
|
1162
|
+
await record.adapter.setApprovalMode(mode);
|
|
1163
|
+
record.info.approvalMode = mode;
|
|
1164
|
+
delete record.info.approvalModePending;
|
|
1165
|
+
this.persist(record);
|
|
1166
|
+
return this.snapshot(record);
|
|
1167
|
+
}
|
|
1168
|
+
finally {
|
|
1169
|
+
record.configuring = false;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
updateConversation(record, event, metadataChanged = true) {
|
|
1173
|
+
if (!event && !metadataChanged)
|
|
1174
|
+
return;
|
|
1175
|
+
const lease = record.info.workerLeaseId ?? '';
|
|
1176
|
+
const key = lease ? JSON.stringify([lease, record.info.generation]) : '';
|
|
1177
|
+
if (!record.conversations.has(key))
|
|
1178
|
+
record.conversations.set(key, new NativeConversationJournal(this.conversationGeneration));
|
|
1179
|
+
for (const [identity, journal] of record.conversations) {
|
|
1180
|
+
const previous = journal.metadata;
|
|
1181
|
+
if (identity !== key) {
|
|
1182
|
+
if (!journal.listenerCount) {
|
|
1183
|
+
record.conversations.delete(identity);
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
// A resumed process can keep its lease. Freeze its predecessor before publishing
|
|
1187
|
+
// successor events, just as a released task lease freezes its journal window.
|
|
1188
|
+
if (previous && !journal.released)
|
|
1189
|
+
journal.commit({
|
|
1190
|
+
...previous,
|
|
1191
|
+
events: [],
|
|
1192
|
+
pendingRequests: [],
|
|
1193
|
+
...(previous.scope ? { scope: { ...previous.scope, released: true } } : {}),
|
|
1194
|
+
});
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
if (event && !metadataChanged && previous) {
|
|
1198
|
+
journal.commit({
|
|
1199
|
+
...previous,
|
|
1200
|
+
events: [event],
|
|
1201
|
+
cursor: event.sequence,
|
|
1202
|
+
...(previous.scope ? { scope: { ...previous.scope, endAt: event.sequence } } : {}),
|
|
1203
|
+
});
|
|
1204
|
+
continue;
|
|
1205
|
+
}
|
|
1206
|
+
const page = lease
|
|
1207
|
+
? record.workerHistory.read(lease, record.events, [...record.pendingRequests.values()], journal.sequence || undefined, 1)
|
|
1208
|
+
: {
|
|
1209
|
+
session: this.snapshot(record),
|
|
1210
|
+
events: [],
|
|
1211
|
+
cursor: record.events.length,
|
|
1212
|
+
hasMore: false,
|
|
1213
|
+
commands: [...record.commands.values()]
|
|
1214
|
+
.slice(-100)
|
|
1215
|
+
.map(({ text: _text, leaseId: _lease, ...receipt }) => receipt),
|
|
1216
|
+
pendingRequests: [...record.pendingRequests.values()],
|
|
1217
|
+
};
|
|
1218
|
+
page.session = this.snapshot({ info: page.session });
|
|
1219
|
+
page.events =
|
|
1220
|
+
event &&
|
|
1221
|
+
event.sequence > (page.scope?.startAfter ?? 0) &&
|
|
1222
|
+
event.sequence <= (page.scope?.endAt ?? page.cursor)
|
|
1223
|
+
? [event]
|
|
1224
|
+
: [];
|
|
1225
|
+
page.cursor = page.scope?.endAt ?? record.events.length;
|
|
1226
|
+
page.hasMore = false;
|
|
1227
|
+
journal.commit(page);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
/** Observation is passive: it neither creates nor resumes the runner. */
|
|
1231
|
+
conversation(owner, id, leaseId, generation) {
|
|
1232
|
+
const record = this.sessionRecord(owner, id);
|
|
1233
|
+
if (Boolean(record.info.workerManaged) !== Boolean(leaseId) ||
|
|
1234
|
+
Boolean(leaseId) !== Boolean(generation))
|
|
1235
|
+
throw new Error('Worker conversations require their pinned history lease and generation');
|
|
1236
|
+
if (record.workerLeaseError)
|
|
1237
|
+
throw record.workerLeaseError;
|
|
1238
|
+
const key = leaseId ? JSON.stringify([leaseId, generation]) : '';
|
|
1239
|
+
let journal = record.conversations.get(key);
|
|
1240
|
+
if (!journal && leaseId && generation) {
|
|
1241
|
+
const metadata = record.workerHistory.read(leaseId, record.events, [], undefined, 1, generation);
|
|
1242
|
+
const end = metadata.scope.endAt;
|
|
1243
|
+
let start = end, bytes = 0;
|
|
1244
|
+
while (start > metadata.scope.startAfter && end - start < 128) {
|
|
1245
|
+
const size = Buffer.byteLength(JSON.stringify(record.events[start - 1]));
|
|
1246
|
+
if (bytes + size > 512 * 1024)
|
|
1247
|
+
break;
|
|
1248
|
+
bytes += size;
|
|
1249
|
+
start--;
|
|
1250
|
+
}
|
|
1251
|
+
const page = record.workerHistory.read(leaseId, record.events, [...record.pendingRequests.values()], start, 128, generation);
|
|
1252
|
+
journal = new NativeConversationJournal(this.conversationGeneration);
|
|
1253
|
+
journal.reset({
|
|
1254
|
+
...page,
|
|
1255
|
+
session: this.snapshot({ info: page.session }),
|
|
1256
|
+
historyBefore: start,
|
|
1257
|
+
truncated: start > metadata.scope.startAfter,
|
|
1258
|
+
});
|
|
1259
|
+
record.conversations.set(key, journal);
|
|
1260
|
+
}
|
|
1261
|
+
if (!journal)
|
|
1262
|
+
throw new Error('Native conversation history generation is unknown');
|
|
1263
|
+
return journal;
|
|
1264
|
+
}
|
|
1043
1265
|
assertAccountCurrent(record) {
|
|
1266
|
+
if (record.info.approvalModePending)
|
|
1267
|
+
throw new Error('Approval mode is unconfirmed; set it again before sending input');
|
|
1044
1268
|
if (!record.info.profileId)
|
|
1045
1269
|
return;
|
|
1046
1270
|
assertNativeProfileCurrent({
|