@genesislcap/ai-assistant 15.35.3 → 15.35.4
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/dist/ai-assistant.api.json +483 -0
- package/dist/ai-assistant.d.ts +192 -0
- package/dist/chat-driver.cjs +221 -4
- package/dist/chat-driver.cjs.map +2 -2
- package/dist/chat-driver.mjs +221 -4
- package/dist/chat-driver.mjs.map +2 -2
- package/dist/custom-elements.json +1616 -1350
- package/dist/dts/components/ai-driver/ai-driver.d.ts +31 -0
- package/dist/dts/components/ai-driver/ai-driver.d.ts.map +1 -1
- package/dist/dts/components/chat-driver/chat-driver.d.ts +93 -0
- package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
- package/dist/dts/components/orchestrating-driver/orchestrating-driver.d.ts +17 -0
- package/dist/dts/components/orchestrating-driver/orchestrating-driver.d.ts.map +1 -1
- package/dist/dts/state/persistence/index.d.ts +1 -0
- package/dist/dts/state/persistence/index.d.ts.map +1 -1
- package/dist/dts/state/persistence/session-persister.d.ts +12 -0
- package/dist/dts/state/persistence/session-persister.d.ts.map +1 -1
- package/dist/dts/state/persistence/session-snapshot.d.ts +14 -0
- package/dist/dts/state/persistence/session-snapshot.d.ts.map +1 -1
- package/dist/dts/utils/condense-history.d.ts +114 -0
- package/dist/dts/utils/condense-history.d.ts.map +1 -1
- package/dist/esm/components/chat-driver/chat-driver.js +140 -17
- package/dist/esm/components/orchestrating-driver/orchestrating-driver.js +17 -0
- package/dist/esm/state/persistence/session-persister.js +63 -15
- package/dist/esm/state/persistence/session-snapshot.js +2 -17
- package/dist/esm/utils/condense-history.js +123 -0
- package/package.json +18 -18
|
@@ -4,7 +4,7 @@ import { NOOP_ACTIVITY_BUS } from '../../channel/ai-activity-bus';
|
|
|
4
4
|
import { resolveChatProvider } from '../../config/validate-providers';
|
|
5
5
|
import { clearSession, getMetaEvents, mergeMetaEvents, recordMetaEvent, recordTurnError, recordTurnRetry, } from '../../state/debug-event-log';
|
|
6
6
|
import { generateSuggestions } from '../../suggestions/generate-suggestions';
|
|
7
|
-
import { applyCondensation, condenseForPersistence, } from '../../utils/condense-history';
|
|
7
|
+
import { applyCondensation, condenseForPersistence, isValidCondenseTrigger, parsePersistedCondensePolicy, rebaseRestoredCondensePolicy, snapshotCondensePolicies, toolCallIdsIn, } from '../../utils/condense-history';
|
|
8
8
|
import { estimatePreambleTokens, estimateRequestTokens, } from '../../utils/context-tokens';
|
|
9
9
|
import { applyHistoryCap, buildCompactionSummaryPrompt, planCompaction, normalizeForProvider, } from '../../utils/history-transform';
|
|
10
10
|
import { logger } from '../../utils/logger';
|
|
@@ -92,8 +92,29 @@ export class ChatDriver extends EventTarget {
|
|
|
92
92
|
* provider call to collapse stale payloads from the model-bound history only.
|
|
93
93
|
* Accumulates across agents on a shared driver (a superseded read collapses no
|
|
94
94
|
* matter which agent made it) and is never cleared — it dies with the driver.
|
|
95
|
+
*
|
|
96
|
+
* "Dies with the driver" used to mean a page reload silently disarmed every policy that had
|
|
97
|
+
* not fired yet: the restored transcript carried the full payload and nothing was left to
|
|
98
|
+
* collapse it, so it rode every later provider call (GENC-1632). The unfired entries are now
|
|
99
|
+
* persisted with the session and re-registered on restore — see
|
|
100
|
+
* {@link ChatDriver.getCondensePoliciesForPersistence} and
|
|
101
|
+
* {@link ChatDriver.restoreCondensePolicies}.
|
|
95
102
|
*/
|
|
96
103
|
this.condensePolicies = new Map();
|
|
104
|
+
/**
|
|
105
|
+
* Restored policies whose owning activation was still live at save time, waiting for that agent
|
|
106
|
+
* to be re-installed so they can be stamped with the activation it resumes in (GENC-1632).
|
|
107
|
+
*
|
|
108
|
+
* They cannot be stamped at restore time. On a fresh page `activeAgentName` is unset, so the
|
|
109
|
+
* first `installAgent` after the reload is a "name change" and advances `currentActivation` —
|
|
110
|
+
* which is exactly the `agentEnd` signal. An entry stamped before that bump reads as belonging
|
|
111
|
+
* to an activation that has ended, and it and every `phaseEnd` entry (through the `agentEnd`
|
|
112
|
+
* backstop) collapse on the first provider call after the reload, mid-flow. For Create's
|
|
113
|
+
* Planning phase that is the uploaded scope document vanishing the moment the user carries on
|
|
114
|
+
* talking about it — a worse failure than the one being fixed. So the stamp is deferred to that
|
|
115
|
+
* `installAgent`, and consumed there once whichever agent it installs.
|
|
116
|
+
*/
|
|
117
|
+
this.condenseAwaitingReactivation = new Set();
|
|
97
118
|
/**
|
|
98
119
|
* Monotonic model-call counter for the driver's whole lifetime — the age clock
|
|
99
120
|
* for `condenseWhen({ on: { kind: 'age' } })`. The per-`sendMessage` `iterations`
|
|
@@ -625,8 +646,10 @@ export class ChatDriver extends EventTarget {
|
|
|
625
646
|
// flow is over, so its `agentEnd`-tagged payloads become collapsible. Guarded
|
|
626
647
|
// on a name change so re-applying the same agent (e.g. a per-turn re-resolve)
|
|
627
648
|
// does not spuriously advance the clock and prematurely drop its payloads.
|
|
628
|
-
if (config.name !== this.activeAgentName)
|
|
649
|
+
if (config.name !== this.activeAgentName) {
|
|
629
650
|
this.currentActivation += 1;
|
|
651
|
+
this.resumeRestoredCondensePolicies(config.name);
|
|
652
|
+
}
|
|
630
653
|
this.systemPrompt = config.systemPrompt;
|
|
631
654
|
if (typeof config.toolDefinitions === 'function') {
|
|
632
655
|
this.toolDefinitionsFactory = config.toolDefinitions;
|
|
@@ -997,6 +1020,113 @@ export class ChatDriver extends EventTarget {
|
|
|
997
1020
|
getHistoryForPersistence() {
|
|
998
1021
|
return condenseForPersistence([...this.history], this.condensePolicies);
|
|
999
1022
|
}
|
|
1023
|
+
/** The four condensation clocks as they read now. */
|
|
1024
|
+
condenseClocks() {
|
|
1025
|
+
return {
|
|
1026
|
+
modelCall: this.modelCallSeq,
|
|
1027
|
+
turn: this.turnSeq,
|
|
1028
|
+
activation: this.currentActivation,
|
|
1029
|
+
phaseEpoch: this.currentPhaseEpoch,
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* The `condenseWhen` policies that have not fired yet, for the calls in `messages`, in the
|
|
1034
|
+
* clock-independent form {@link ChatDriver.restoreCondensePolicies} re-registers. See
|
|
1035
|
+
* {@link AiDriver.getCondensePoliciesForPersistence}.
|
|
1036
|
+
*
|
|
1037
|
+
* The other half of {@link ChatDriver.getHistoryForPersistence} (GENC-1632). That stores the
|
|
1038
|
+
* payloads the model has already stopped seeing as their stubs; this stores the policies for the
|
|
1039
|
+
* ones it has NOT stopped seeing yet, so a reload does not strand them at full size for the rest
|
|
1040
|
+
* of the session. Only entries whose call is in `messages` — the transcript actually being
|
|
1041
|
+
* written — are returned, so a frozen (non-resumable) or compacted save carries no policy for a
|
|
1042
|
+
* call it does not contain.
|
|
1043
|
+
*
|
|
1044
|
+
* @beta
|
|
1045
|
+
*/
|
|
1046
|
+
getCondensePoliciesForPersistence(messages) {
|
|
1047
|
+
return snapshotCondensePolicies(this.condensePolicies, this.condenseClocks(), messages);
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Re-register policies saved by {@link ChatDriver.getCondensePoliciesForPersistence} against
|
|
1051
|
+
* the restored transcript. See {@link AiDriver.restoreCondensePolicies}.
|
|
1052
|
+
*
|
|
1053
|
+
* Call AFTER {@link ChatDriver.loadHistory} has seeded the restored transcript: an entry is
|
|
1054
|
+
* re-registered only if its tool call is in the driver's history, so one whose call the
|
|
1055
|
+
* restorable-boundary trim cut away (an orphaned trailing `tool_use`) is dropped rather than
|
|
1056
|
+
* resurrected against nothing.
|
|
1057
|
+
*
|
|
1058
|
+
* First-wins still holds per call: an entry is skipped if a policy is already registered for its
|
|
1059
|
+
* tool call — a restore never overrides what a live handler declared. `superseded` needs no
|
|
1060
|
+
* special handling — survivors are resolved from HISTORY order on every pass, so a restored entry
|
|
1061
|
+
* and a later call on the same `by` key resolve exactly as two live ones would.
|
|
1062
|
+
*
|
|
1063
|
+
* Clocks are rebased so each trigger is exactly as far from firing as it was at save time (see
|
|
1064
|
+
* `rebaseRestoredCondensePolicy`), with one deferral. An entry owned by the activation that was
|
|
1065
|
+
* live at save time, whose agent is not the one installed right now — the normal case on a
|
|
1066
|
+
* fresh page, where nothing is installed yet — is parked in
|
|
1067
|
+
* `condenseAwaitingReactivation` and stamped by the next `installAgent`: with
|
|
1068
|
+
* that agent's resumed activation if it is the same agent, or left behind (so it reads as
|
|
1069
|
+
* ended, which it has) if a different agent took over.
|
|
1070
|
+
*
|
|
1071
|
+
* A host with no agent to re-activate (a bare driver built from a static config) loses nothing
|
|
1072
|
+
* by this: the entry's owner (`undefined`) matches the installed agent (`undefined`), so it is
|
|
1073
|
+
* stamped live straight away and ends when the driver's single activation does — on
|
|
1074
|
+
* `releaseAgent` / `completeSubAgent`, exactly as a policy registered live would.
|
|
1075
|
+
*
|
|
1076
|
+
* Returns how many entries were actually re-armed, split from how many were merely present in
|
|
1077
|
+
* `entries` (review follow-up, GENC-1632): a malformed entry, one whose call the restorable-
|
|
1078
|
+
* boundary trim removed, or a first-wins duplicate is silently dropped, so the caller's own count
|
|
1079
|
+
* of `entries` can't tell "all of them came back" from "none did". `restored` is every entry that
|
|
1080
|
+
* reached `condensePolicies`; `awaitingReactivation` is the subset of those parked pending the
|
|
1081
|
+
* next `installAgent` rather than stamped live immediately.
|
|
1082
|
+
*
|
|
1083
|
+
* @beta
|
|
1084
|
+
*/
|
|
1085
|
+
restoreCondensePolicies(entries) {
|
|
1086
|
+
if (!Array.isArray(entries) || entries.length === 0)
|
|
1087
|
+
return { restored: 0, awaitingReactivation: 0 };
|
|
1088
|
+
const present = toolCallIdsIn(this.history);
|
|
1089
|
+
const clocks = this.condenseClocks();
|
|
1090
|
+
let restored = 0;
|
|
1091
|
+
let awaitingReactivation = 0;
|
|
1092
|
+
for (const raw of entries) {
|
|
1093
|
+
const entry = parsePersistedCondensePolicy(raw);
|
|
1094
|
+
if (!entry || !present.has(entry.toolCallId))
|
|
1095
|
+
continue;
|
|
1096
|
+
if (this.condensePolicies.has(entry.toolCallId))
|
|
1097
|
+
continue;
|
|
1098
|
+
this.condensePolicies.set(entry.toolCallId, rebaseRestoredCondensePolicy(entry, clocks));
|
|
1099
|
+
restored += 1;
|
|
1100
|
+
if (entry.elapsed.activations === 0 && entry.agent !== this.activeAgentName) {
|
|
1101
|
+
this.condenseAwaitingReactivation.add(entry.toolCallId);
|
|
1102
|
+
awaitingReactivation += 1;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
return { restored, awaitingReactivation };
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Stamp the restored entries parked in `condenseAwaitingReactivation` now that
|
|
1109
|
+
* `installAgent` has advanced the activation clock for `agentName` (GENC-1632).
|
|
1110
|
+
*
|
|
1111
|
+
* Same agent → the flow the entry belonged to is resuming, so it takes the new activation and
|
|
1112
|
+
* stays full until THAT activation ends (and, for `phaseEnd`, until the next `endPhase()`).
|
|
1113
|
+
* Different agent → its owner did not come back; its activation is over, so it keeps the stamp
|
|
1114
|
+
* it was restored with, which is now behind the clock, and collapses on the next provider call
|
|
1115
|
+
* exactly as a live `agentEnd` payload does on a swap.
|
|
1116
|
+
*
|
|
1117
|
+
* One-shot: the set is emptied whatever the outcome, because only the FIRST activation after the
|
|
1118
|
+
* restore can be the resumption. Any later swap is an ordinary activation boundary.
|
|
1119
|
+
*/
|
|
1120
|
+
resumeRestoredCondensePolicies(agentName) {
|
|
1121
|
+
if (this.condenseAwaitingReactivation.size === 0)
|
|
1122
|
+
return;
|
|
1123
|
+
for (const toolCallId of this.condenseAwaitingReactivation) {
|
|
1124
|
+
const entry = this.condensePolicies.get(toolCallId);
|
|
1125
|
+
if (entry && entry.agent === agentName)
|
|
1126
|
+
entry.activation = this.currentActivation;
|
|
1127
|
+
}
|
|
1128
|
+
this.condenseAwaitingReactivation.clear();
|
|
1129
|
+
}
|
|
1000
1130
|
/**
|
|
1001
1131
|
* {@inheritDoc AiDriver.getSuggestions}
|
|
1002
1132
|
*
|
|
@@ -1338,12 +1468,9 @@ export class ChatDriver extends EventTarget {
|
|
|
1338
1468
|
return;
|
|
1339
1469
|
}
|
|
1340
1470
|
const triggers = Array.isArray(policy.on) ? policy.on : [policy.on];
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
? typeof t.turns === 'number' && t.turns >= 1
|
|
1345
|
-
: (t === null || t === void 0 ? void 0 : t.kind) === 'turnEnd' || (t === null || t === void 0 ? void 0 : t.kind) === 'agentEnd' || (t === null || t === void 0 ? void 0 : t.kind) === 'phaseEnd';
|
|
1346
|
-
if (triggers.length === 0 || !triggers.every(validTrigger)) {
|
|
1471
|
+
// Shared with the restore path's validation (GENC-1632), so a policy that could not be
|
|
1472
|
+
// registered live can never be re-registered from a stored session either.
|
|
1473
|
+
if (triggers.length === 0 || !triggers.every(isValidCondenseTrigger)) {
|
|
1347
1474
|
logger.warn(`ChatDriver(${(_b = this.activeAgentName) !== null && _b !== void 0 ? _b : 'unknown'}): condenseWhen called with an invalid trigger — ignoring`);
|
|
1348
1475
|
return;
|
|
1349
1476
|
}
|
|
@@ -1353,15 +1480,11 @@ export class ChatDriver extends EventTarget {
|
|
|
1353
1480
|
logger.error(`ChatDriver(${(_c = this.activeAgentName) !== null && _c !== void 0 ? _c : 'unknown'}): condenseWhen called more than once for the same tool call — keeping the first policy, ignoring this one`);
|
|
1354
1481
|
return;
|
|
1355
1482
|
}
|
|
1356
|
-
// Stamp the
|
|
1357
|
-
// runs during the current model-call's tool batch).
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
turn: this.turnSeq,
|
|
1362
|
-
activation: this.currentActivation,
|
|
1363
|
-
phaseEpoch: this.currentPhaseEpoch,
|
|
1364
|
-
});
|
|
1483
|
+
// Stamp the four clocks NOW (read straight off the driver — the handler
|
|
1484
|
+
// runs during the current model-call's tool batch). `agent` is not a clock; it is
|
|
1485
|
+
// recorded so a persisted entry can tell, after a reload, whether the agent that
|
|
1486
|
+
// re-activates is the one that owned the call (GENC-1632).
|
|
1487
|
+
this.condensePolicies.set(activeToolCallId, Object.assign({ policy, iteration: this.modelCallSeq, turn: this.turnSeq, activation: this.currentActivation, phaseEpoch: this.currentPhaseEpoch }, (this.activeAgentName !== undefined && { agent: this.activeAgentName })));
|
|
1365
1488
|
} });
|
|
1366
1489
|
}
|
|
1367
1490
|
/**
|
|
@@ -312,6 +312,23 @@ export class OrchestratingDriver extends EventTarget {
|
|
|
312
312
|
getHistoryForPersistence() {
|
|
313
313
|
return this.chatDriver.getHistoryForPersistence();
|
|
314
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Delegates to the inner {@link ChatDriver}, which owns the condensation registry and clocks
|
|
317
|
+
* (GENC-1632). The forward matters for the same reason as `getHistoryForPersistence` above: the
|
|
318
|
+
* persister reaches it through `?.`, so a missing forward would compile, pass the ChatDriver
|
|
319
|
+
* tests, and do nothing for every orchestrated host — which is every host with an agents array.
|
|
320
|
+
*/
|
|
321
|
+
getCondensePoliciesForPersistence(messages) {
|
|
322
|
+
return this.chatDriver.getCondensePoliciesForPersistence(messages);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Delegates to the inner {@link ChatDriver} (GENC-1632). The deferred activation stamp it
|
|
326
|
+
* performs is taken by the inner driver's `installAgent`, which this driver's own
|
|
327
|
+
* `installAgent` always reaches, so nothing extra is needed here.
|
|
328
|
+
*/
|
|
329
|
+
restoreCondensePolicies(entries) {
|
|
330
|
+
return this.chatDriver.restoreCondensePolicies(entries);
|
|
331
|
+
}
|
|
315
332
|
/** {@inheritDoc AiDriver.setContextGuard} */
|
|
316
333
|
setContextGuard(policy) {
|
|
317
334
|
this.chatDriver.setContextGuard(policy);
|
|
@@ -263,6 +263,30 @@ export class SessionPersister {
|
|
|
263
263
|
const raw = (_d = (_b = (_a = driver === null || driver === void 0 ? void 0 : driver.getHistoryForPersistence) === null || _a === void 0 ? void 0 : _a.call(driver)) !== null && _b !== void 0 ? _b : (_c = driver === null || driver === void 0 ? void 0 : driver.getRawHistory) === null || _c === void 0 ? void 0 : _c.call(driver)) !== null && _d !== void 0 ? _d : storeMessages;
|
|
264
264
|
return raw.filter((m) => !isSessionNoticeMarker(m));
|
|
265
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* The `condenseWhen` policies still waiting to fire for the calls in `messages` (GENC-1632) —
|
|
268
|
+
* the half of condensation `getHistoryForPersistence` cannot carry, because a payload whose
|
|
269
|
+
* trigger has not fired is stored full and needs its policy to collapse after a reload.
|
|
270
|
+
*
|
|
271
|
+
* `flowDropped` is the frozen (non-resumable) save: its snapshot drops the running flow, so the
|
|
272
|
+
* activation that owned the pending payloads does not resume and they are written as ended.
|
|
273
|
+
*
|
|
274
|
+
* Never throws: a failure here costs a resumed session some context, which is not worth losing
|
|
275
|
+
* the save over.
|
|
276
|
+
*/
|
|
277
|
+
condensePoliciesToPersist(driver, messages, opts = {}) {
|
|
278
|
+
var _a;
|
|
279
|
+
try {
|
|
280
|
+
const pending = (_a = driver === null || driver === void 0 ? void 0 : driver.getCondensePoliciesForPersistence) === null || _a === void 0 ? void 0 : _a.call(driver, messages);
|
|
281
|
+
if (!(pending === null || pending === void 0 ? void 0 : pending.length) || !opts.flowDropped)
|
|
282
|
+
return pending;
|
|
283
|
+
return pending.map((e) => e.elapsed.activations > 0 ? e : Object.assign(Object.assign({}, e), { elapsed: Object.assign(Object.assign({}, e.elapsed), { activations: 1 }) }));
|
|
284
|
+
}
|
|
285
|
+
catch (e) {
|
|
286
|
+
logger.error('Session save: capturing condense policies failed:', e);
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
266
290
|
/**
|
|
267
291
|
* Capture the current session to durable storage (GENC-1351 §5.1). Reads the
|
|
268
292
|
* full transcript from the driver and the restorable scalars from the store, and
|
|
@@ -340,10 +364,11 @@ export class SessionPersister {
|
|
|
340
364
|
// completed exchanges that were already safely stored. Taking the max of the two
|
|
341
365
|
// makes the boundary monotonic: it advances with each new prompt and never rewinds.
|
|
342
366
|
const boundary = Math.max(keepThroughLastUserMessage(messages).length, this._persistedMessages);
|
|
367
|
+
const frozenMessages = messages.slice(0, Math.min(boundary, messages.length));
|
|
343
368
|
const frozen = createPersistedSession({
|
|
344
369
|
sessionKey: key,
|
|
345
370
|
host: window.location.host,
|
|
346
|
-
messages:
|
|
371
|
+
messages: frozenMessages,
|
|
347
372
|
activeAgentName: (_a = s.activeAgent) === null || _a === void 0 ? void 0 : _a.name,
|
|
348
373
|
pinnedAgentName: s.pinnedAgentName,
|
|
349
374
|
flowOwnerAgentName: null,
|
|
@@ -353,6 +378,14 @@ export class SessionPersister {
|
|
|
353
378
|
activeModel: s.activeModel,
|
|
354
379
|
activeProviderName: s.activeProviderName,
|
|
355
380
|
agentSnapshots: {},
|
|
381
|
+
// Pending condense policies are carried here too (GENC-1632), but with the live
|
|
382
|
+
// activation marked as ENDED: this snapshot drops the flow's state, so on return the
|
|
383
|
+
// agent starts afresh rather than resuming, and the activation those payloads belonged to
|
|
384
|
+
// is over. Without the mark, a re-run of the same agent would be taken for a resumption
|
|
385
|
+
// and adopt the dead flow's `agentEnd` / `phaseEnd` payloads for its whole new run.
|
|
386
|
+
condensePolicies: this.condensePoliciesToPersist(driver, frozenMessages, {
|
|
387
|
+
flowDropped: true,
|
|
388
|
+
}),
|
|
356
389
|
});
|
|
357
390
|
// Skip the write when the frozen boundary has not moved — a flow autosaves on every
|
|
358
391
|
// transcript change, and while it runs the last user message (and therefore the
|
|
@@ -420,6 +453,7 @@ export class SessionPersister {
|
|
|
420
453
|
activeModel: s.activeModel,
|
|
421
454
|
activeProviderName: s.activeProviderName,
|
|
422
455
|
agentSnapshots,
|
|
456
|
+
condensePolicies: this.condensePoliciesToPersist(driver, messages),
|
|
423
457
|
});
|
|
424
458
|
// Observability (GENC-1351): record what each autosave captured for stateful
|
|
425
459
|
// agents — `agents: []` here means no machine snapshot was taken (the bug to
|
|
@@ -487,7 +521,7 @@ export class SessionPersister {
|
|
|
487
521
|
*/
|
|
488
522
|
restore() {
|
|
489
523
|
return __awaiter(this, void 0, void 0, function* () {
|
|
490
|
-
var _a, _b, _c, _d;
|
|
524
|
+
var _a, _b, _c, _d, _e;
|
|
491
525
|
const { provider } = this.deps.getPersistence();
|
|
492
526
|
if (!provider)
|
|
493
527
|
return;
|
|
@@ -543,16 +577,6 @@ export class SessionPersister {
|
|
|
543
577
|
// Seed the monotonic freeze floor too (GENC-1511): a flow that goes non-resumable
|
|
544
578
|
// straight after a restore must not freeze BELOW what the restore just brought back.
|
|
545
579
|
this._persistedMessages = trimmed.length;
|
|
546
|
-
// Observability (GENC-1351): record exactly what the restore primed, so a
|
|
547
|
-
// post-reload debug-log export shows whether a stateful agent's snapshot
|
|
548
|
-
// actually came back (meta events reset on reload, so this is the only window
|
|
549
|
-
// onto the saved→loaded handoff).
|
|
550
|
-
this.logMeta('persistence.restored', {
|
|
551
|
-
agents: Object.keys((_a = snapshot.agentSnapshots) !== null && _a !== void 0 ? _a : {}),
|
|
552
|
-
agentSnapshots: snapshot.agentSnapshots,
|
|
553
|
-
pinnedAgentName: snapshot.pinnedAgentName,
|
|
554
|
-
flowOwnerAgentName: snapshot.flowOwnerAgentName,
|
|
555
|
-
});
|
|
556
580
|
// Loudly surface saved agent state that can't be rehydrated because the agent
|
|
557
581
|
// isn't in the current set — otherwise the flow silently fails to resume, a
|
|
558
582
|
// real footgun for hosts with dynamic agent arrays. Non-fatal: the transcript
|
|
@@ -616,7 +640,31 @@ export class SessionPersister {
|
|
|
616
640
|
}
|
|
617
641
|
const messages = [...trimmed, ...trailers];
|
|
618
642
|
ref.actions.aiAssistant.loadSession(toLoadSessionPayload(Object.assign(Object.assign({}, snapshot), { messages })));
|
|
619
|
-
(
|
|
643
|
+
(_a = driver === null || driver === void 0 ? void 0 : driver.loadHistory) === null || _a === void 0 ? void 0 : _a.call(driver, [...messages]);
|
|
644
|
+
// Re-arm the condense policies that had not fired at save time (GENC-1632) — AFTER
|
|
645
|
+
// `loadHistory`, because the driver keeps only entries whose tool call is in the history it
|
|
646
|
+
// now holds, which is how an entry for a call `trimToRestorableBoundary` cut away is dropped
|
|
647
|
+
// rather than resurrected. A snapshot from before the field existed has none, so this is a
|
|
648
|
+
// no-op and the restore is exactly what it was. Timing against the agent's re-activation is
|
|
649
|
+
// the driver's problem, not ours: it defers the activation stamp to the next `installAgent`.
|
|
650
|
+
const condenseRestore = (_b = driver === null || driver === void 0 ? void 0 : driver.restoreCondensePolicies) === null || _b === void 0 ? void 0 : _b.call(driver, snapshot.condensePolicies);
|
|
651
|
+
// Observability (GENC-1351, counts refined for GENC-1632 review): record exactly what the
|
|
652
|
+
// restore primed, so a post-reload debug-log export shows whether a stateful agent's snapshot
|
|
653
|
+
// actually came back (meta events reset on reload, so this is the only window onto the
|
|
654
|
+
// saved→loaded handoff). Logged HERE, after `restoreCondensePolicies` has run, rather than
|
|
655
|
+
// where the snapshot was first read: `stored` alone (the old field) counts what the snapshot
|
|
656
|
+
// OFFERED, not what came back — `restoreCondensePolicies` silently drops a malformed entry, one
|
|
657
|
+
// whose call the restorable-boundary trim removed, and a first-wins duplicate, so a bare
|
|
658
|
+
// `stored: 14` can't tell "all 14 re-armed" from "0 re-armed". `restored`/`awaitingReactivation`
|
|
659
|
+
// are left OFF the object (not zeroed) when there is no driver or it predates this method, so
|
|
660
|
+
// that case can't be misread as "re-armed none" either.
|
|
661
|
+
this.logMeta('persistence.restored', {
|
|
662
|
+
agents: Object.keys((_c = snapshot.agentSnapshots) !== null && _c !== void 0 ? _c : {}),
|
|
663
|
+
condensePolicies: Object.assign({ stored: Array.isArray(snapshot.condensePolicies) ? snapshot.condensePolicies.length : 0 }, condenseRestore),
|
|
664
|
+
agentSnapshots: snapshot.agentSnapshots,
|
|
665
|
+
pinnedAgentName: snapshot.pinnedAgentName,
|
|
666
|
+
flowOwnerAgentName: snapshot.flowOwnerAgentName,
|
|
667
|
+
});
|
|
620
668
|
// Interrupted flow (§5.9): pre-fill the composer with the prompt that started
|
|
621
669
|
// the flow so "send it again" is one keystroke, and record it for observability.
|
|
622
670
|
if (snapshot.interrupted) {
|
|
@@ -639,8 +687,8 @@ export class SessionPersister {
|
|
|
639
687
|
// driver's routing agrees with the restored (picker-locked) store state.
|
|
640
688
|
const orchestrator = asOrchestrator(driver);
|
|
641
689
|
if (orchestrator) {
|
|
642
|
-
orchestrator.setPinnedAgent((
|
|
643
|
-
orchestrator.setFlowOwner((
|
|
690
|
+
orchestrator.setPinnedAgent((_d = snapshot.pinnedAgentName) !== null && _d !== void 0 ? _d : null);
|
|
691
|
+
orchestrator.setFlowOwner((_e = snapshot.flowOwnerAgentName) !== null && _e !== void 0 ? _e : null);
|
|
644
692
|
}
|
|
645
693
|
logger.debug('Session restored', { key, messages: messages.length });
|
|
646
694
|
}
|
|
@@ -13,23 +13,8 @@ export const PERSISTED_SESSION_VERSION = 1;
|
|
|
13
13
|
* @internal
|
|
14
14
|
*/
|
|
15
15
|
export function createPersistedSession(input) {
|
|
16
|
-
var _a;
|
|
17
|
-
return {
|
|
18
|
-
version: PERSISTED_SESSION_VERSION,
|
|
19
|
-
savedAt: new Date().toISOString(),
|
|
20
|
-
host: input.host,
|
|
21
|
-
sessionKey: input.sessionKey,
|
|
22
|
-
messages: input.messages,
|
|
23
|
-
activeAgentName: input.activeAgentName,
|
|
24
|
-
pinnedAgentName: input.pinnedAgentName,
|
|
25
|
-
flowOwnerAgentName: input.flowOwnerAgentName,
|
|
26
|
-
sessionCostUsd: input.sessionCostUsd,
|
|
27
|
-
contextTokens: input.contextTokens,
|
|
28
|
-
contextLimit: input.contextLimit,
|
|
29
|
-
activeModel: input.activeModel,
|
|
30
|
-
activeProviderName: input.activeProviderName,
|
|
31
|
-
agentSnapshots: (_a = input.agentSnapshots) !== null && _a !== void 0 ? _a : {},
|
|
32
|
-
};
|
|
16
|
+
var _a, _b;
|
|
17
|
+
return Object.assign({ version: PERSISTED_SESSION_VERSION, savedAt: new Date().toISOString(), host: input.host, sessionKey: input.sessionKey, messages: input.messages, activeAgentName: input.activeAgentName, pinnedAgentName: input.pinnedAgentName, flowOwnerAgentName: input.flowOwnerAgentName, sessionCostUsd: input.sessionCostUsd, contextTokens: input.contextTokens, contextLimit: input.contextLimit, activeModel: input.activeModel, activeProviderName: input.activeProviderName, agentSnapshots: (_a = input.agentSnapshots) !== null && _a !== void 0 ? _a : {} }, (((_b = input.condensePolicies) === null || _b === void 0 ? void 0 : _b.length) ? { condensePolicies: input.condensePolicies } : {}));
|
|
33
18
|
}
|
|
34
19
|
/**
|
|
35
20
|
* Resolve the notice text + pre-fill prompt for an interrupted (non-resumable) flow
|
|
@@ -22,6 +22,129 @@ import { APPROX_CHARS_PER_TOKEN } from './context-tokens';
|
|
|
22
22
|
export const CONDENSE_MIN_CHARS = 1000;
|
|
23
23
|
/** Key the collapsed args are stored under — a tool-call's args must stay a Record. */
|
|
24
24
|
export const CONDENSED_ARGS_KEY = 'condensed';
|
|
25
|
+
/** Whether `t` is a well-formed trigger — the same rule `condenseWhen` enforces on registration. */
|
|
26
|
+
export function isValidCondenseTrigger(t) {
|
|
27
|
+
if ((t === null || t === void 0 ? void 0 : t.kind) === 'superseded')
|
|
28
|
+
return typeof t.by === 'string' && t.by.length > 0;
|
|
29
|
+
if ((t === null || t === void 0 ? void 0 : t.kind) === 'age')
|
|
30
|
+
return typeof t.turns === 'number' && t.turns >= 1;
|
|
31
|
+
return (t === null || t === void 0 ? void 0 : t.kind) === 'turnEnd' || (t === null || t === void 0 ? void 0 : t.kind) === 'agentEnd' || (t === null || t === void 0 ? void 0 : t.kind) === 'phaseEnd';
|
|
32
|
+
}
|
|
33
|
+
/** Whether `r` is a well-formed replacement mode (`pointer` / `drop` / `{ replaceWith }`). */
|
|
34
|
+
function isValidCondenseResult(r) {
|
|
35
|
+
if (r === 'pointer' || r === 'drop')
|
|
36
|
+
return true;
|
|
37
|
+
return (!!r && typeof r === 'object' && typeof r.replaceWith === 'string');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Every tool-call id carried by an assistant message in `history`. Exported so the save side
|
|
41
|
+
* ({@link snapshotCondensePolicies}) and the restore side (`ChatDriver.restoreCondensePolicies`)
|
|
42
|
+
* compute "is this call still present?" from one definition — GENC-1632 review: the two had drifted
|
|
43
|
+
* into separate copies of the same loop.
|
|
44
|
+
*/
|
|
45
|
+
export function toolCallIdsIn(history) {
|
|
46
|
+
var _a;
|
|
47
|
+
const ids = new Set();
|
|
48
|
+
for (const msg of history) {
|
|
49
|
+
for (const tc of (_a = msg.toolCalls) !== null && _a !== void 0 ? _a : [])
|
|
50
|
+
ids.add(tc.id);
|
|
51
|
+
}
|
|
52
|
+
return ids;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The entries to PERSIST alongside `history` (GENC-1632): every registered policy that has not
|
|
56
|
+
* fired yet and whose tool call is still in the transcript being saved, re-expressed as elapsed
|
|
57
|
+
* clock counts (see {@link PersistedCondensePolicy}).
|
|
58
|
+
*
|
|
59
|
+
* `history` is the transcript actually being written, not the driver's live one, because the two
|
|
60
|
+
* can differ: a non-resumable save freezes at the last user message, and a compaction may have
|
|
61
|
+
* summarised a call away. An entry whose call is not in the saved transcript has nothing to
|
|
62
|
+
* collapse after a reload, so it is not written.
|
|
63
|
+
*
|
|
64
|
+
* Pure, no writes to the registry — safe on a save path that runs on every autosave.
|
|
65
|
+
*/
|
|
66
|
+
export function snapshotCondensePolicies(policies, clocks, history) {
|
|
67
|
+
if (policies.size === 0)
|
|
68
|
+
return [];
|
|
69
|
+
const present = toolCallIdsIn(history);
|
|
70
|
+
const out = [];
|
|
71
|
+
for (const [toolCallId, entry] of policies) {
|
|
72
|
+
if (entry.firedTrigger || !present.has(toolCallId))
|
|
73
|
+
continue;
|
|
74
|
+
out.push(Object.assign(Object.assign({ toolCallId, policy: entry.policy }, (entry.agent !== undefined && { agent: entry.agent })), { elapsed: {
|
|
75
|
+
// `max(0, …)`: every clock is monotonic, so these are never negative on a live driver;
|
|
76
|
+
// the clamp only stops a malformed entry from being written out as "the future".
|
|
77
|
+
modelCalls: Math.max(0, clocks.modelCall - entry.iteration),
|
|
78
|
+
turns: Math.max(0, clocks.turn - entry.turn),
|
|
79
|
+
activations: Math.max(0, clocks.activation - entry.activation),
|
|
80
|
+
phases: Math.max(0, clocks.phaseEpoch - entry.phaseEpoch),
|
|
81
|
+
} }));
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Validate one persisted entry read back from storage. Returns `undefined` for anything that is not
|
|
87
|
+
* a well-formed {@link PersistedCondensePolicy} — the blob came off a disk or a network, and a
|
|
88
|
+
* policy that cannot be evaluated must be dropped rather than reach `applyCondensation`, which
|
|
89
|
+
* trusts its registry. A missing or malformed `elapsed` count reads as `0` (the boundary had not
|
|
90
|
+
* passed), which errs towards keeping a payload visible rather than eliding it.
|
|
91
|
+
*/
|
|
92
|
+
export function parsePersistedCondensePolicy(value) {
|
|
93
|
+
var _a;
|
|
94
|
+
if (!value || typeof value !== 'object')
|
|
95
|
+
return undefined;
|
|
96
|
+
const v = value;
|
|
97
|
+
if (typeof v.toolCallId !== 'string' || !v.toolCallId)
|
|
98
|
+
return undefined;
|
|
99
|
+
const policy = v.policy;
|
|
100
|
+
if (!policy || typeof policy !== 'object')
|
|
101
|
+
return undefined;
|
|
102
|
+
const triggers = Array.isArray(policy.on) ? policy.on : [policy.on];
|
|
103
|
+
if (triggers.length === 0 || !triggers.every(isValidCondenseTrigger))
|
|
104
|
+
return undefined;
|
|
105
|
+
if (policy.args === undefined && policy.response === undefined)
|
|
106
|
+
return undefined;
|
|
107
|
+
if (policy.args !== undefined && !isValidCondenseResult(policy.args))
|
|
108
|
+
return undefined;
|
|
109
|
+
if (policy.response !== undefined && !isValidCondenseResult(policy.response))
|
|
110
|
+
return undefined;
|
|
111
|
+
const count = (n) => typeof n === 'number' && Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;
|
|
112
|
+
const e = ((_a = v.elapsed) !== null && _a !== void 0 ? _a : {});
|
|
113
|
+
return Object.assign(Object.assign({ toolCallId: v.toolCallId, policy }, (typeof v.agent === 'string' && { agent: v.agent })), { elapsed: {
|
|
114
|
+
modelCalls: count(e.modelCalls),
|
|
115
|
+
turns: count(e.turns),
|
|
116
|
+
activations: count(e.activations),
|
|
117
|
+
phases: count(e.phases),
|
|
118
|
+
} });
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Re-register a persisted entry against a (typically fresh) driver's clocks (GENC-1632): each
|
|
122
|
+
* stamp is the current clock minus the elapsed count, so every trigger stands exactly as far from
|
|
123
|
+
* firing as it did when the session was saved.
|
|
124
|
+
*
|
|
125
|
+
* Per clock, what that means after a reload:
|
|
126
|
+
* - `age` — exact. The model-call distance is preserved, so a result seen twice of its three
|
|
127
|
+
* allowed looks gets one more. The rebased `iteration` is usually negative (the call predates
|
|
128
|
+
* this driver's first model call), which the trigger arithmetic is indifferent to; it does
|
|
129
|
+
* surface as a negative `turn` on the eventual `context.condensed` event.
|
|
130
|
+
* - `turnEnd` — a call from the turn that was running at save time collapses on the next
|
|
131
|
+
* `sendMessage`. That is the original intent, not an approximation: a reload ends the turn.
|
|
132
|
+
* - `phaseEnd` — collapses at the next `endPhase()`, or at once if the phase had already ended.
|
|
133
|
+
* - `agentEnd` (and `phaseEnd`'s backstop) — an entry whose activation had already ended collapses
|
|
134
|
+
* at once. One owned by the activation live at save time is stamped with the CURRENT
|
|
135
|
+
* activation, which reads as live; but the driver will very likely advance its activation clock
|
|
136
|
+
* when the agent is re-installed after the reload, so this stamp is provisional and the driver
|
|
137
|
+
* must re-stamp it at that point — see `ChatDriver.installAgent`. Leaving it here would collapse
|
|
138
|
+
* the payload the moment the flow resumes, which is the failure this whole mechanism exists to
|
|
139
|
+
* avoid.
|
|
140
|
+
*
|
|
141
|
+
* The `reported*` latches start clear, so the eventual full→stub transition reports once, like any
|
|
142
|
+
* other entry. The stored transcript cannot hold a stub for an unfired entry, so there is no
|
|
143
|
+
* earlier report to duplicate.
|
|
144
|
+
*/
|
|
145
|
+
export function rebaseRestoredCondensePolicy(entry, clocks) {
|
|
146
|
+
return Object.assign({ policy: entry.policy, iteration: clocks.modelCall - entry.elapsed.modelCalls, turn: clocks.turn - entry.elapsed.turns, activation: clocks.activation - entry.elapsed.activations, phaseEpoch: clocks.phaseEpoch - entry.elapsed.phases }, (entry.agent !== undefined && { agent: entry.agent }));
|
|
147
|
+
}
|
|
25
148
|
/** Short human-readable reason for the stub text, per fired trigger. */
|
|
26
149
|
function triggerReason(trigger) {
|
|
27
150
|
switch (trigger.kind) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genesislcap/ai-assistant",
|
|
3
3
|
"description": "Genesis AI Assistant micro-frontend",
|
|
4
|
-
"version": "15.35.
|
|
4
|
+
"version": "15.35.4",
|
|
5
5
|
"license": "SEE LICENSE IN license.txt",
|
|
6
6
|
"main": "dist/esm/index.js",
|
|
7
7
|
"types": "dist/ai-assistant.d.ts",
|
|
@@ -82,32 +82,32 @@
|
|
|
82
82
|
}
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@genesislcap/foundation-comms": "15.35.
|
|
86
|
-
"@genesislcap/foundation-testing": "15.35.
|
|
87
|
-
"@genesislcap/genx": "15.35.
|
|
88
|
-
"@genesislcap/rollup-builder": "15.35.
|
|
89
|
-
"@genesislcap/ts-builder": "15.35.
|
|
90
|
-
"@genesislcap/uvu-playwright-builder": "15.35.
|
|
91
|
-
"@genesislcap/vite-builder": "15.35.
|
|
92
|
-
"@genesislcap/webpack-builder": "15.35.
|
|
85
|
+
"@genesislcap/foundation-comms": "15.35.4",
|
|
86
|
+
"@genesislcap/foundation-testing": "15.35.4",
|
|
87
|
+
"@genesislcap/genx": "15.35.4",
|
|
88
|
+
"@genesislcap/rollup-builder": "15.35.4",
|
|
89
|
+
"@genesislcap/ts-builder": "15.35.4",
|
|
90
|
+
"@genesislcap/uvu-playwright-builder": "15.35.4",
|
|
91
|
+
"@genesislcap/vite-builder": "15.35.4",
|
|
92
|
+
"@genesislcap/webpack-builder": "15.35.4",
|
|
93
93
|
"@types/dompurify": "^3.0.5",
|
|
94
94
|
"@types/marked": "^5.0.2",
|
|
95
95
|
"esbuild": "0.25.12"
|
|
96
96
|
},
|
|
97
97
|
"dependencies": {
|
|
98
|
-
"@genesislcap/foundation-ai": "15.35.
|
|
99
|
-
"@genesislcap/foundation-logger": "15.35.
|
|
100
|
-
"@genesislcap/foundation-notifications": "15.35.
|
|
101
|
-
"@genesislcap/foundation-redux": "15.35.
|
|
102
|
-
"@genesislcap/foundation-ui": "15.35.
|
|
103
|
-
"@genesislcap/foundation-utils": "15.35.
|
|
104
|
-
"@genesislcap/rapid-design-system": "15.35.
|
|
105
|
-
"@genesislcap/web-core": "15.35.
|
|
98
|
+
"@genesislcap/foundation-ai": "15.35.4",
|
|
99
|
+
"@genesislcap/foundation-logger": "15.35.4",
|
|
100
|
+
"@genesislcap/foundation-notifications": "15.35.4",
|
|
101
|
+
"@genesislcap/foundation-redux": "15.35.4",
|
|
102
|
+
"@genesislcap/foundation-ui": "15.35.4",
|
|
103
|
+
"@genesislcap/foundation-utils": "15.35.4",
|
|
104
|
+
"@genesislcap/rapid-design-system": "15.35.4",
|
|
105
|
+
"@genesislcap/web-core": "15.35.4",
|
|
106
106
|
"dompurify": "^3.3.1",
|
|
107
107
|
"marked": "^17.0.3"
|
|
108
108
|
},
|
|
109
109
|
"peerDependencies": {
|
|
110
|
-
"@genesislcap/foundation-comms": "^15.35.
|
|
110
|
+
"@genesislcap/foundation-comms": "^15.35.4"
|
|
111
111
|
},
|
|
112
112
|
"peerDependenciesMeta": {
|
|
113
113
|
"@genesislcap/foundation-comms": {
|