@qvac/core 0.6.4 → 1.0.0-rc.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/dist-lib/lib/api/host-run-log.d.ts +43 -0
- package/dist-lib/lib/api/host-run-log.js +97 -0
- package/dist-lib/lib/api/index.d.ts +3 -0
- package/dist-lib/lib/api/index.js +9 -0
- package/dist-lib/lib/api/instrument.js +33 -2
- package/dist-lib/lib/assistant/harness.d.ts +4 -1
- package/dist-lib/lib/assistant/history.d.ts +7 -2
- package/dist-lib/lib/assistant/history.js +34 -3
- package/dist-lib/lib/assistant/host-run-log.d.ts +3 -0
- package/dist-lib/lib/assistant/host-run-log.js +48 -0
- package/dist-lib/lib/assistant.d.ts +2 -0
- package/dist-lib/lib/assistant.js +817 -412
- package/dist-lib/lib/core/async-iterable.js +1 -1
- package/dist-lib/lib/core/chat-activity.d.ts +1 -1
- package/dist-lib/lib/core/chat-activity.js +16 -1
- package/dist-lib/lib/core/chat-search.js +11 -4
- package/dist-lib/lib/core/chunk.d.ts +3 -3
- package/dist-lib/lib/core/chunk.js +6 -2
- package/dist-lib/lib/core/harness-loan.js +4 -0
- package/dist-lib/lib/core/host-run-log-store.d.ts +114 -0
- package/dist-lib/lib/core/host-run-log-store.js +976 -0
- package/dist-lib/lib/core/prompt-history.js +3 -1
- package/dist-lib/lib/core/transcript-visibility.d.ts +19 -0
- package/dist-lib/lib/core/transcript-visibility.js +47 -0
- package/dist-lib/lib/core.d.ts +2 -0
- package/dist-lib/lib/core.js +36 -0
- package/dist-lib/lib/custom-model-pusher.js +9 -5
- package/dist-lib/lib/mesh-scoped-engine.js +5 -0
- package/dist-lib/lib/run-transcript.d.ts +10 -3
- package/dist-lib/lib/run-transcript.js +23 -5
- package/dist-lib/lib/task.js +6 -1
- package/dist-lib/schema/content.d.ts +1 -0
- package/dist-lib/schema/content.js +15 -3
- package/dist-lib/schema/engine.js +90 -3
- package/dist-lib/schema/indexes.js +10 -1
- package/dist-lib/schema/qvac.js +10 -2
- package/dist-lib/spec/bind.js +15 -0
- package/dist-lib/spec/calls.js +5 -0
- package/dist-lib/spec/capabilities.d.ts +10 -0
- package/dist-lib/spec/hrpc/hrpc.json +164 -99
- package/dist-lib/spec/hrpc/index.js +260 -200
- package/dist-lib/spec/hrpc/messages.js +1202 -898
- package/dist-lib/spec/hyperschema/index.js +1202 -898
- package/dist-lib/spec/hyperschema/schema.json +265 -0
- package/dist-lib/spec/hyperschema/types.d.ts +65 -3
- package/dist-lib/spec/mesh/hyperdb/messages.js +385 -241
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +138 -12
- package/dist-lib/spec/mesh/hyperschema/index.js +138 -12
- package/dist-lib/spec/mesh/hyperschema/schema.json +79 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +19 -3
- package/package.json +3 -2
|
@@ -5,10 +5,12 @@ import fs from 'fs';
|
|
|
5
5
|
import crypto from 'hypercore-crypto';
|
|
6
6
|
import os from 'os';
|
|
7
7
|
import path from 'path';
|
|
8
|
+
import { requestFromBoot } from '@qvac/harness/run-log';
|
|
8
9
|
import { isMobilePlatform, mediaModelNameFor, modelsForAgent, runnableModelName } from '@qvac/harness/lib/models/model-catalog.ts';
|
|
9
10
|
import { connectHarness } from '@qvac/harness';
|
|
10
11
|
import * as z from 'zod';
|
|
11
12
|
import { loanFailureNotice } from './core/harness-loan.js';
|
|
13
|
+
import { createHostRunLogAdapter } from './assistant/host-run-log.js';
|
|
12
14
|
import { SKILL_STATE } from '@qvac/harness/lib/tools/skills/skill-state.ts';
|
|
13
15
|
import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
|
|
14
16
|
import { RAG_CITATION_LINE } from '@qvac/harness/lib/prompt.ts';
|
|
@@ -1395,9 +1397,44 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1395
1397
|
});
|
|
1396
1398
|
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1397
1399
|
}
|
|
1400
|
+
async _recordRunBoundaryStatus(engine, { request, runId, agent, deviceId, state, reason }) {
|
|
1401
|
+
try {
|
|
1402
|
+
const { chunks } = await engine.listChunks({
|
|
1403
|
+
chatId: request.chatId,
|
|
1404
|
+
type: 'run-status'
|
|
1405
|
+
});
|
|
1406
|
+
if ((chunks ?? []).some((chunk) => chunk.runId === runId &&
|
|
1407
|
+
(TERMINAL_STATES.has(chunk.runStatus?.state ?? '') ||
|
|
1408
|
+
chunk.runStatus?.state === 'recovery-required'))) {
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
catch (error) {
|
|
1413
|
+
this._log?.warn('runs', 'could not inspect prior run status before setup failure', {
|
|
1414
|
+
runId,
|
|
1415
|
+
error
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
const transcript = new RunTranscript(engine, {
|
|
1419
|
+
chatId: request.chatId,
|
|
1420
|
+
runId,
|
|
1421
|
+
deviceId,
|
|
1422
|
+
model: agent.modelName,
|
|
1423
|
+
log: this._log
|
|
1424
|
+
});
|
|
1425
|
+
try {
|
|
1426
|
+
await transcript.status({ state, reason });
|
|
1427
|
+
}
|
|
1428
|
+
catch (error) {
|
|
1429
|
+
this._seen.delete(runId);
|
|
1430
|
+
this._log?.error('runs', 'could not record run setup failure', { runId, error });
|
|
1431
|
+
}
|
|
1432
|
+
finally {
|
|
1433
|
+
transcript.close();
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1398
1436
|
async _execute(request, agent, { controller, meshId, runId }) {
|
|
1399
1437
|
const engine = meshScoped(this._engine, meshId);
|
|
1400
|
-
const { chatId } = request;
|
|
1401
1438
|
const deviceId = this._requireDeviceId();
|
|
1402
1439
|
if (this.suspended) {
|
|
1403
1440
|
this._seen.delete(runId);
|
|
@@ -1426,8 +1463,115 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1426
1463
|
runId
|
|
1427
1464
|
});
|
|
1428
1465
|
}
|
|
1429
|
-
|
|
1430
|
-
|
|
1466
|
+
const runLog = createHostRunLogAdapter(engine);
|
|
1467
|
+
const pendingLease = runLog.begin(runId);
|
|
1468
|
+
let hostLease;
|
|
1469
|
+
try {
|
|
1470
|
+
hostLease = await raceAbort(pendingLease, controller.signal);
|
|
1471
|
+
}
|
|
1472
|
+
catch (error) {
|
|
1473
|
+
if (controller.signal.aborted) {
|
|
1474
|
+
pendingLease
|
|
1475
|
+
.then((lateLease) => runLog.release(runId, lateLease.generation))
|
|
1476
|
+
.catch((releaseError) => this._log?.warn('runs', 'late run-log begin cleanup failed', {
|
|
1477
|
+
runId,
|
|
1478
|
+
error: releaseError
|
|
1479
|
+
}));
|
|
1480
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1481
|
+
if (outcome.kind === 'replay') {
|
|
1482
|
+
this._seen.delete(runId);
|
|
1483
|
+
}
|
|
1484
|
+
if (outcome.kind === 'failed') {
|
|
1485
|
+
await this._recordRunBoundaryStatus(engine, {
|
|
1486
|
+
request,
|
|
1487
|
+
runId,
|
|
1488
|
+
agent,
|
|
1489
|
+
deviceId,
|
|
1490
|
+
state: 'failed',
|
|
1491
|
+
reason: outcome.reason
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
await this._recordRunBoundaryStatus(engine, {
|
|
1497
|
+
request,
|
|
1498
|
+
runId,
|
|
1499
|
+
agent,
|
|
1500
|
+
deviceId,
|
|
1501
|
+
state: 'failed',
|
|
1502
|
+
reason: runFailureNotice(error)
|
|
1503
|
+
});
|
|
1504
|
+
return;
|
|
1505
|
+
}
|
|
1506
|
+
try {
|
|
1507
|
+
await this._executePrimary({
|
|
1508
|
+
request,
|
|
1509
|
+
agent,
|
|
1510
|
+
controller,
|
|
1511
|
+
meshId,
|
|
1512
|
+
runId,
|
|
1513
|
+
engine,
|
|
1514
|
+
runLog,
|
|
1515
|
+
hostLease
|
|
1516
|
+
});
|
|
1517
|
+
}
|
|
1518
|
+
catch (error) {
|
|
1519
|
+
if (controller.signal.aborted) {
|
|
1520
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1521
|
+
if (outcome.kind === 'replay') {
|
|
1522
|
+
this._seen.delete(runId);
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
if (outcome.kind === 'handoff') {
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
await this._recordRunBoundaryStatus(engine, {
|
|
1529
|
+
request,
|
|
1530
|
+
runId,
|
|
1531
|
+
agent,
|
|
1532
|
+
deviceId,
|
|
1533
|
+
state: 'failed',
|
|
1534
|
+
reason: outcome.reason
|
|
1535
|
+
});
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
await this._recordRunBoundaryStatus(engine, {
|
|
1539
|
+
request,
|
|
1540
|
+
runId,
|
|
1541
|
+
agent,
|
|
1542
|
+
deviceId,
|
|
1543
|
+
state: hostLease.boot?.metadata ? 'recovery-required' : 'failed',
|
|
1544
|
+
reason: runFailureNotice(error)
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
finally {
|
|
1548
|
+
await runLog
|
|
1549
|
+
.release(runId, hostLease.generation)
|
|
1550
|
+
.catch((error) => this._log?.warn('runs', 'run-log lease release failed', { runId, error }));
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
async _executePrimary({ request, agent, controller, meshId, runId, engine, runLog, hostLease }) {
|
|
1554
|
+
const { chatId } = request;
|
|
1555
|
+
const deviceId = this._requireDeviceId();
|
|
1556
|
+
const { boot } = hostLease;
|
|
1557
|
+
const resumed = Boolean(boot?.metadata);
|
|
1558
|
+
const savedRequest = resumed && boot ? asRecord(requestFromBoot(boot)) : null;
|
|
1559
|
+
if (resumed) {
|
|
1560
|
+
const { chunks } = await engine.listChunks({ chatId, type: 'run-request' });
|
|
1561
|
+
const persistedRequest = (chunks ?? []).find((chunk) => chunk.id === request.id);
|
|
1562
|
+
if (!persistedRequest ||
|
|
1563
|
+
persistedRequest.chatId !== chatId ||
|
|
1564
|
+
persistedRequest.runId !== runId ||
|
|
1565
|
+
persistedRequest.seq !== request.seq ||
|
|
1566
|
+
persistedRequest.parentId !== request.parentId ||
|
|
1567
|
+
persistedRequest.runRequest?.agentId !== agent.id) {
|
|
1568
|
+
throw new Error('The original run request is no longer present in its chat history');
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
if (!resumed) {
|
|
1572
|
+
await this._reconcileCompaction(engine, chatId);
|
|
1573
|
+
}
|
|
1574
|
+
if (request.parentId && !resumed) {
|
|
1431
1575
|
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1432
1576
|
}
|
|
1433
1577
|
const lender = this._lenderFor(agent);
|
|
@@ -1461,88 +1605,26 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1461
1605
|
model,
|
|
1462
1606
|
log: this._log
|
|
1463
1607
|
});
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
const all = await listHistoryChunks(engine, chatId);
|
|
1474
|
-
const branches = historyBranches(all, runId);
|
|
1475
|
-
const currentRepair = branches.repairByRunId.get(runId);
|
|
1476
|
-
const repairParent = currentRepair
|
|
1477
|
-
? all.find((chunk) => chunk.id === currentRepair.parentId)
|
|
1478
|
-
: undefined;
|
|
1479
|
-
const acceptedRepairs = acceptedRepairCount(branches, runId);
|
|
1480
|
-
const history = all.filter((chunk) => !!chunk.compaction ||
|
|
1481
|
-
(chunk.runId !== runId &&
|
|
1482
|
-
inputAvailableToRun(chunk, request.seq) &&
|
|
1483
|
-
(!repairParent || chunk.seq <= repairParent.seq || chunk.runId === repairParent.runId) &&
|
|
1484
|
-
isPromptChunk(chunk, branches)));
|
|
1485
|
-
const { chunks: priorAsks } = await engine.listChunks({
|
|
1486
|
-
chatId,
|
|
1487
|
-
type: 'approval-request'
|
|
1488
|
-
});
|
|
1489
|
-
const { chunks: priorResults } = await engine.listChunks({ chatId, type: 'tool-result' });
|
|
1490
|
-
let ran = (priorResults ?? []).some((chunk) => chunk.runId === runId &&
|
|
1491
|
-
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1492
|
-
!isDeniedResult(chunk.toolResult?.result));
|
|
1493
|
-
const named = couldNameSenders(history);
|
|
1494
|
-
const senders = named ? await this._senderNames(engine, history) : NO_SENDERS;
|
|
1495
|
-
if (named || this._chatAttribution.get(chatId)) {
|
|
1496
|
-
await this._reconcileAttribution(chatId, senders);
|
|
1497
|
-
}
|
|
1498
|
-
const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
|
|
1499
|
-
if (currentRepair) {
|
|
1500
|
-
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1501
|
-
}
|
|
1502
|
-
const diagramRepair = currentRepair !== undefined;
|
|
1503
|
-
const runAgent = diagramRepair
|
|
1504
|
-
? {
|
|
1505
|
-
...configuredAgent,
|
|
1506
|
-
toolsScope: 'selected',
|
|
1507
|
-
enabledTools: [],
|
|
1508
|
-
skillsScope: 'selected',
|
|
1509
|
-
enabledSkills: []
|
|
1510
|
-
}
|
|
1511
|
-
: configuredAgent;
|
|
1512
|
-
const seesImages = this._harness.supportsImagesFor
|
|
1513
|
-
? this._harness.supportsImagesFor(model)
|
|
1514
|
-
: (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
1515
|
-
let unreadableImages = new Map();
|
|
1516
|
-
let renderedPdfIds = new Set();
|
|
1517
|
-
if (!seesImages) {
|
|
1518
|
-
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1519
|
-
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
1520
|
-
this._log?.warn('assistant', `no projection pairing for ${model} — dropped ${dropped} image(s)`);
|
|
1521
|
-
}
|
|
1522
|
-
for (const turn of messages) {
|
|
1523
|
-
if (turn.images?.length) {
|
|
1524
|
-
const names = turn.images.map((image) => image.fileName).join(', ');
|
|
1525
|
-
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
1526
|
-
}
|
|
1527
|
-
delete turn.images;
|
|
1528
|
-
delete turn.pdfs;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
else {
|
|
1532
|
-
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
1533
|
-
}
|
|
1534
|
-
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1535
|
-
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1536
|
-
let readinessError = null;
|
|
1537
|
-
try {
|
|
1538
|
-
if (!lender) {
|
|
1539
|
-
await this._downloads?.waitUntilCached(model, controller.signal);
|
|
1608
|
+
let preflightSettledWatch;
|
|
1609
|
+
let preflightOwnershipWatch;
|
|
1610
|
+
let preflightModelWatch;
|
|
1611
|
+
let preflightApprovals;
|
|
1612
|
+
let preflightCleanupDone = false;
|
|
1613
|
+
let mainOwnsCleanup = false;
|
|
1614
|
+
const cleanupPreflight = async () => {
|
|
1615
|
+
if (preflightCleanupDone) {
|
|
1616
|
+
return;
|
|
1540
1617
|
}
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1618
|
+
preflightCleanupDone = true;
|
|
1619
|
+
transcript.close();
|
|
1620
|
+
preflightModelWatch?.stop();
|
|
1621
|
+
preflightOwnershipWatch?.stop();
|
|
1622
|
+
preflightSettledWatch?.stop();
|
|
1623
|
+
await preflightApprovals
|
|
1624
|
+
?.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) })
|
|
1625
|
+
.catch((cleanupError) => this._log?.warn('approvals', 'setup cleanup failed', { runId, cleanupError }));
|
|
1626
|
+
};
|
|
1627
|
+
const abortPreflight = async () => {
|
|
1546
1628
|
try {
|
|
1547
1629
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1548
1630
|
if (outcome.kind === 'replay') {
|
|
@@ -1553,126 +1635,220 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1553
1635
|
}
|
|
1554
1636
|
}
|
|
1555
1637
|
finally {
|
|
1556
|
-
|
|
1557
|
-
ownershipWatch.stop();
|
|
1558
|
-
settledWatch.stop();
|
|
1559
|
-
}
|
|
1560
|
-
return;
|
|
1561
|
-
}
|
|
1562
|
-
if (readinessError) {
|
|
1563
|
-
try {
|
|
1564
|
-
await transcript.status({ state: 'failed', reason: runFailureNotice(readinessError) });
|
|
1565
|
-
}
|
|
1566
|
-
finally {
|
|
1567
|
-
transcript.close();
|
|
1568
|
-
ownershipWatch.stop();
|
|
1569
|
-
settledWatch.stop();
|
|
1638
|
+
await cleanupPreflight();
|
|
1570
1639
|
}
|
|
1571
|
-
return;
|
|
1572
|
-
}
|
|
1573
|
-
const inflight = this._runs.get(runId);
|
|
1574
|
-
if (inflight) {
|
|
1575
|
-
this._runs.set(runId, { ...inflight, executing: true });
|
|
1576
|
-
}
|
|
1577
|
-
this._yieldDownloads();
|
|
1578
|
-
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
1579
|
-
const executing = await transcript.status(busyChatId
|
|
1580
|
-
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1581
|
-
: { state: 'executing' });
|
|
1582
|
-
const runStatusId = executing.id;
|
|
1583
|
-
const approvals = this._createApprove(engine, {
|
|
1584
|
-
chatId,
|
|
1585
|
-
runId,
|
|
1586
|
-
deviceId,
|
|
1587
|
-
agentId: agent.id,
|
|
1588
|
-
hasRun: () => ran
|
|
1589
|
-
});
|
|
1590
|
-
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
1591
|
-
let metrics;
|
|
1592
|
-
const sourceTagFilter = createRagSourceTagFilter();
|
|
1593
|
-
const retrievedRecords = [];
|
|
1594
|
-
let answerText = '';
|
|
1595
|
-
const flushTranscript = async () => {
|
|
1596
|
-
const tail = sourceTagFilter.flush();
|
|
1597
|
-
if (tail) {
|
|
1598
|
-
answerText += tail;
|
|
1599
|
-
await transcript.text('content', tail);
|
|
1600
|
-
}
|
|
1601
|
-
await transcript.flush();
|
|
1602
1640
|
};
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1641
|
+
try {
|
|
1642
|
+
const savedModel = savedRequest?.model ?? savedRequest?.modelId;
|
|
1643
|
+
if (resumed && savedModel !== model) {
|
|
1644
|
+
await transcript.status({
|
|
1645
|
+
state: 'recovery-required',
|
|
1646
|
+
reason: 'The original model is no longer the model selected for this agent.'
|
|
1647
|
+
});
|
|
1609
1648
|
return;
|
|
1610
1649
|
}
|
|
1611
|
-
if (
|
|
1612
|
-
|
|
1613
|
-
}
|
|
1614
|
-
await transcript.text(type, text);
|
|
1615
|
-
};
|
|
1616
|
-
const failRun = async (message) => {
|
|
1617
|
-
const folded = isContextOverflowFailure(message) &&
|
|
1618
|
-
(await this._runCompaction({
|
|
1650
|
+
if (!resumed) {
|
|
1651
|
+
await this._compactIfNearFull({
|
|
1619
1652
|
chatId,
|
|
1620
1653
|
transcript,
|
|
1621
|
-
trigger: '
|
|
1654
|
+
trigger: 'before-run',
|
|
1622
1655
|
signal: controller.signal,
|
|
1623
1656
|
meshId
|
|
1624
|
-
})
|
|
1625
|
-
runFailure = runFailureNotice(message);
|
|
1626
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1627
|
-
if (folded) {
|
|
1628
|
-
await engine
|
|
1629
|
-
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1630
|
-
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1631
|
-
}
|
|
1632
|
-
titleOutcome = 'ended';
|
|
1633
|
-
};
|
|
1634
|
-
const endAbortedRun = async () => {
|
|
1635
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1636
|
-
if (outcome.kind === 'replay') {
|
|
1637
|
-
runVerdict = 'replay';
|
|
1638
|
-
this._seen.delete(runId);
|
|
1639
|
-
return;
|
|
1657
|
+
});
|
|
1640
1658
|
}
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1659
|
+
const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
|
|
1660
|
+
const fsGrantsPromise = this._loadFsGrants(agent.id);
|
|
1661
|
+
void toolConfigsPromise.catch(noop);
|
|
1662
|
+
void fsGrantsPromise.catch(noop);
|
|
1663
|
+
const all = resumed ? [] : await listHistoryChunks(engine, chatId);
|
|
1664
|
+
const branches = historyBranches(all, runId);
|
|
1665
|
+
const currentRepair = branches.repairByRunId.get(runId);
|
|
1666
|
+
const repairParent = currentRepair
|
|
1667
|
+
? all.find((chunk) => chunk.id === currentRepair.parentId)
|
|
1668
|
+
: undefined;
|
|
1669
|
+
const acceptedRepairs = acceptedRepairCount(branches, runId);
|
|
1670
|
+
const history = all.filter((chunk) => !!chunk.compaction ||
|
|
1671
|
+
(chunk.runId !== runId &&
|
|
1672
|
+
inputAvailableToRun(chunk, request.seq) &&
|
|
1673
|
+
(!repairParent ||
|
|
1674
|
+
chunk.seq <= repairParent.seq ||
|
|
1675
|
+
chunk.runId === repairParent.runId) &&
|
|
1676
|
+
isPromptChunk(chunk, branches)));
|
|
1677
|
+
const { chunks: priorAsks } = await engine.listChunks({
|
|
1678
|
+
chatId,
|
|
1679
|
+
type: 'approval-request'
|
|
1680
|
+
});
|
|
1681
|
+
const { chunks: priorResults } = await engine.listChunks({ chatId, type: 'tool-result' });
|
|
1682
|
+
let ran = (priorResults ?? []).some((chunk) => chunk.runId === runId &&
|
|
1683
|
+
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1684
|
+
!isDeniedResult(chunk.toolResult?.result));
|
|
1685
|
+
const named = couldNameSenders(history);
|
|
1686
|
+
const senders = named ? await this._senderNames(engine, history) : NO_SENDERS;
|
|
1687
|
+
if (!resumed && (named || this._chatAttribution.get(chatId))) {
|
|
1688
|
+
await this._reconcileAttribution(chatId, senders);
|
|
1689
|
+
}
|
|
1690
|
+
const messages = resumed
|
|
1691
|
+
? savedMessagesOf(savedRequest)
|
|
1692
|
+
: historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
|
|
1693
|
+
if (currentRepair) {
|
|
1694
|
+
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1695
|
+
}
|
|
1696
|
+
let diagramRepair = currentRepair !== undefined;
|
|
1697
|
+
let runAgent = diagramRepair
|
|
1698
|
+
? {
|
|
1699
|
+
...configuredAgent,
|
|
1700
|
+
toolsScope: 'selected',
|
|
1701
|
+
enabledTools: [],
|
|
1702
|
+
skillsScope: 'selected',
|
|
1703
|
+
enabledSkills: []
|
|
1704
|
+
}
|
|
1705
|
+
: configuredAgent;
|
|
1706
|
+
if (resumed && savedRequest && isRecord(savedRequest.agent)) {
|
|
1707
|
+
const savedAgent = savedRequest.agent;
|
|
1708
|
+
const repairAgent = {
|
|
1709
|
+
...configuredAgent,
|
|
1710
|
+
toolsScope: 'selected',
|
|
1711
|
+
enabledTools: [],
|
|
1712
|
+
skillsScope: 'selected',
|
|
1713
|
+
enabledSkills: []
|
|
1714
|
+
};
|
|
1715
|
+
if (!sameSerializedValue(savedAgent, configuredAgent) &&
|
|
1716
|
+
sameSerializedValue(savedAgent, repairAgent)) {
|
|
1717
|
+
runAgent = repairAgent;
|
|
1718
|
+
diagramRepair = true;
|
|
1719
|
+
}
|
|
1644
1720
|
}
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
if (
|
|
1651
|
-
|
|
1721
|
+
const seesImages = this._harness.supportsImagesFor
|
|
1722
|
+
? this._harness.supportsImagesFor(model)
|
|
1723
|
+
: (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
1724
|
+
let unreadableImages = new Map();
|
|
1725
|
+
let renderedPdfIds = new Set();
|
|
1726
|
+
if (resumed) {
|
|
1727
|
+
unreadableImages = new Map();
|
|
1728
|
+
}
|
|
1729
|
+
else if (!seesImages) {
|
|
1730
|
+
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1731
|
+
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
1732
|
+
this._log?.warn('assistant', `no projection pairing for ${model} — dropped ${dropped} image(s)`);
|
|
1733
|
+
}
|
|
1734
|
+
stripUnseenImages(messages);
|
|
1652
1735
|
}
|
|
1653
|
-
else
|
|
1654
|
-
|
|
1736
|
+
else {
|
|
1737
|
+
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
1655
1738
|
}
|
|
1656
|
-
|
|
1657
|
-
|
|
1739
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1740
|
+
preflightSettledWatch = settledWatch;
|
|
1741
|
+
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1742
|
+
preflightOwnershipWatch = ownershipWatch;
|
|
1743
|
+
let readinessError = null;
|
|
1744
|
+
try {
|
|
1745
|
+
if (!lender && !resumed) {
|
|
1746
|
+
await this._downloads?.waitUntilCached(model, controller.signal);
|
|
1747
|
+
}
|
|
1658
1748
|
}
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
try {
|
|
1662
|
-
const attachTurnIds = attachTurnDocumentIds(history);
|
|
1663
|
-
if (attachTurnIds.length > 0) {
|
|
1664
|
-
backfillDocumentOnlyTurn(messages);
|
|
1749
|
+
catch (err) {
|
|
1750
|
+
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
1665
1751
|
}
|
|
1666
|
-
const knowledge = !diagramRepair &&
|
|
1667
|
-
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1668
|
-
? await this._searchKnowledge(engine, messages, {
|
|
1669
|
-
chatId,
|
|
1670
|
-
attachmentIds: attachTurnIds,
|
|
1671
|
-
signal: controller.signal,
|
|
1672
|
-
preparing: modelWatch.documents
|
|
1673
|
-
})
|
|
1674
|
-
: null;
|
|
1675
1752
|
if (controller.signal.aborted) {
|
|
1753
|
+
await abortPreflight();
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
if (readinessError) {
|
|
1757
|
+
try {
|
|
1758
|
+
await transcript.status({ state: 'failed', reason: runFailureNotice(readinessError) });
|
|
1759
|
+
}
|
|
1760
|
+
finally {
|
|
1761
|
+
await cleanupPreflight();
|
|
1762
|
+
}
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
const inflight = this._runs.get(runId);
|
|
1766
|
+
if (inflight) {
|
|
1767
|
+
this._runs.set(runId, { ...inflight, executing: true });
|
|
1768
|
+
}
|
|
1769
|
+
this._yieldDownloads();
|
|
1770
|
+
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
1771
|
+
let runStatusId;
|
|
1772
|
+
let resumeAccepted = !resumed;
|
|
1773
|
+
if (!resumed) {
|
|
1774
|
+
try {
|
|
1775
|
+
const executing = await transcript.status(busyChatId
|
|
1776
|
+
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1777
|
+
: { state: 'executing' });
|
|
1778
|
+
runStatusId = executing.id;
|
|
1779
|
+
}
|
|
1780
|
+
catch (error) {
|
|
1781
|
+
await cleanupPreflight();
|
|
1782
|
+
throw error;
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
const approvals = this._createApprove(engine, {
|
|
1786
|
+
chatId,
|
|
1787
|
+
runId,
|
|
1788
|
+
deviceId,
|
|
1789
|
+
agentId: agent.id,
|
|
1790
|
+
hasRun: () => ran
|
|
1791
|
+
});
|
|
1792
|
+
preflightApprovals = approvals;
|
|
1793
|
+
const modelWatch = this._watchModelProgress((runStatus) => runStatusId ? transcript.progress(runStatusId, runStatus) : Promise.resolve(), busyChatId !== null);
|
|
1794
|
+
preflightModelWatch = modelWatch;
|
|
1795
|
+
let metrics;
|
|
1796
|
+
let sourceTagFilter = createRagSourceTagFilter();
|
|
1797
|
+
const retrievedRecords = [];
|
|
1798
|
+
const answerTextByRound = new Map();
|
|
1799
|
+
let activeRound = 0;
|
|
1800
|
+
const appendAnswer = (round, text) => {
|
|
1801
|
+
answerTextByRound.set(round, (answerTextByRound.get(round) ?? '') + text);
|
|
1802
|
+
};
|
|
1803
|
+
const flushTranscript = async () => {
|
|
1804
|
+
const tail = sourceTagFilter.flush();
|
|
1805
|
+
if (tail) {
|
|
1806
|
+
appendAnswer(activeRound, tail);
|
|
1807
|
+
await transcript.text('content', tail, activeRound);
|
|
1808
|
+
}
|
|
1809
|
+
await transcript.flush();
|
|
1810
|
+
};
|
|
1811
|
+
let titleOutcome = null;
|
|
1812
|
+
let runFailure = null;
|
|
1813
|
+
let runVerdict = 'completed';
|
|
1814
|
+
const resetRound = async (round, replacedTypes) => {
|
|
1815
|
+
if (replacedTypes === undefined || replacedTypes.includes('content')) {
|
|
1816
|
+
sourceTagFilter = createRagSourceTagFilter();
|
|
1817
|
+
sourceTagFilter.add(retrievedRecords.map((record) => record.title));
|
|
1818
|
+
answerTextByRound.delete(round);
|
|
1819
|
+
}
|
|
1820
|
+
activeRound = round;
|
|
1821
|
+
await transcript.roundReset(round, replacedTypes);
|
|
1822
|
+
};
|
|
1823
|
+
const writeText = async (type, raw, round) => {
|
|
1824
|
+
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
1825
|
+
if (!text) {
|
|
1826
|
+
return;
|
|
1827
|
+
}
|
|
1828
|
+
if (type === 'content') {
|
|
1829
|
+
appendAnswer(round, text);
|
|
1830
|
+
}
|
|
1831
|
+
await transcript.text(type, text, round);
|
|
1832
|
+
};
|
|
1833
|
+
const failRun = async (message) => {
|
|
1834
|
+
const folded = isContextOverflowFailure(message) &&
|
|
1835
|
+
(await this._runCompaction({
|
|
1836
|
+
chatId,
|
|
1837
|
+
transcript,
|
|
1838
|
+
trigger: 'overflow',
|
|
1839
|
+
signal: controller.signal,
|
|
1840
|
+
meshId
|
|
1841
|
+
}));
|
|
1842
|
+
runFailure = runFailureNotice(message);
|
|
1843
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1844
|
+
if (folded) {
|
|
1845
|
+
await engine
|
|
1846
|
+
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1847
|
+
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1848
|
+
}
|
|
1849
|
+
titleOutcome = 'ended';
|
|
1850
|
+
};
|
|
1851
|
+
const endAbortedRun = async () => {
|
|
1676
1852
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1677
1853
|
if (outcome.kind === 'replay') {
|
|
1678
1854
|
runVerdict = 'replay';
|
|
@@ -1685,240 +1861,370 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1685
1861
|
}
|
|
1686
1862
|
runFailure = outcome.reason;
|
|
1687
1863
|
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1688
|
-
|
|
1689
|
-
}
|
|
1690
|
-
const
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
if (hasAudio) {
|
|
1699
|
-
modelWatch.transcribe(false);
|
|
1700
|
-
}
|
|
1701
|
-
backfillImageOnlyTurns(messages);
|
|
1702
|
-
if (seesImages) {
|
|
1703
|
-
renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
|
|
1704
|
-
}
|
|
1705
|
-
annotateDocumentAttachments(messages);
|
|
1706
|
-
const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
|
|
1707
|
-
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
1708
|
-
const reason = documentUnreadReason({
|
|
1709
|
-
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
1710
|
-
failedFiles: knowledge?.search?.failedFiles,
|
|
1711
|
-
attachedCount: unreadIds.length
|
|
1712
|
-
});
|
|
1713
|
-
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
1714
|
-
if (lastUser) {
|
|
1715
|
-
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1864
|
+
titleOutcome = 'ended';
|
|
1865
|
+
};
|
|
1866
|
+
const requireRecovery = async (reason) => {
|
|
1867
|
+
runVerdict = 'recovery-required';
|
|
1868
|
+
await transcript.status({ state: 'recovery-required', reason });
|
|
1869
|
+
titleOutcome = 'ended';
|
|
1870
|
+
};
|
|
1871
|
+
const recordToolResult = async (event) => {
|
|
1872
|
+
if (isDeniedResult(event.result)) {
|
|
1873
|
+
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
1716
1874
|
}
|
|
1717
|
-
if (
|
|
1718
|
-
|
|
1875
|
+
else if (event.name !== SKILL_TOOL) {
|
|
1876
|
+
ran = true;
|
|
1719
1877
|
}
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
const
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1878
|
+
if (WEB_TOOLS.includes(String(event.name))) {
|
|
1879
|
+
await this._citeWebSources(transcript, runId, event.result);
|
|
1880
|
+
}
|
|
1881
|
+
await transcript.chunk(toolResultChunk(event));
|
|
1882
|
+
};
|
|
1883
|
+
mainOwnsCleanup = true;
|
|
1884
|
+
const runCompletion = async () => {
|
|
1885
|
+
try {
|
|
1886
|
+
const attachTurnIds = resumed ? [] : attachTurnDocumentIds(history);
|
|
1887
|
+
if (attachTurnIds.length > 0) {
|
|
1888
|
+
backfillDocumentOnlyTurn(messages);
|
|
1889
|
+
}
|
|
1890
|
+
const knowledge = !resumed &&
|
|
1891
|
+
!diagramRepair &&
|
|
1892
|
+
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1893
|
+
? await this._searchKnowledge(engine, messages, {
|
|
1894
|
+
chatId,
|
|
1895
|
+
attachmentIds: attachTurnIds,
|
|
1896
|
+
signal: controller.signal,
|
|
1897
|
+
preparing: modelWatch.documents
|
|
1898
|
+
})
|
|
1899
|
+
: null;
|
|
1900
|
+
if (controller.signal.aborted) {
|
|
1901
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1902
|
+
if (outcome.kind === 'replay') {
|
|
1903
|
+
runVerdict = 'replay';
|
|
1904
|
+
this._seen.delete(runId);
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
if (outcome.kind === 'handoff') {
|
|
1908
|
+
runVerdict = 'handoff';
|
|
1909
|
+
return;
|
|
1910
|
+
}
|
|
1911
|
+
runFailure = outcome.reason;
|
|
1912
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1913
|
+
return;
|
|
1914
|
+
}
|
|
1915
|
+
const injected = knowledge
|
|
1916
|
+
? await this._injectKnowledge(knowledge, {
|
|
1917
|
+
transcript,
|
|
1918
|
+
sourceTagFilter,
|
|
1919
|
+
retrievedRecords
|
|
1920
|
+
})
|
|
1921
|
+
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1922
|
+
if (!resumed) {
|
|
1923
|
+
const hasAudio = messages.some((turn) => turn.audioAttachments?.length);
|
|
1924
|
+
if (hasAudio) {
|
|
1925
|
+
modelWatch.transcribe(true);
|
|
1926
|
+
}
|
|
1927
|
+
await this._hydrateAudio(messages, controller.signal);
|
|
1928
|
+
if (hasAudio) {
|
|
1929
|
+
modelWatch.transcribe(false);
|
|
1930
|
+
}
|
|
1931
|
+
backfillImageOnlyTurns(messages);
|
|
1932
|
+
if (seesImages) {
|
|
1933
|
+
renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
|
|
1776
1934
|
}
|
|
1777
|
-
|
|
1778
|
-
|
|
1935
|
+
annotateDocumentAttachments(messages);
|
|
1936
|
+
}
|
|
1937
|
+
const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
|
|
1938
|
+
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
1939
|
+
const reason = documentUnreadReason({
|
|
1940
|
+
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
1941
|
+
failedFiles: knowledge?.search?.failedFiles,
|
|
1942
|
+
attachedCount: unreadIds.length
|
|
1943
|
+
});
|
|
1944
|
+
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
1945
|
+
if (lastUser) {
|
|
1946
|
+
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1947
|
+
}
|
|
1948
|
+
if (!injected.sourcesWritten) {
|
|
1949
|
+
await this._writeUnreadNotice(transcript, reason);
|
|
1779
1950
|
}
|
|
1780
1951
|
}
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1952
|
+
if (!resumed) {
|
|
1953
|
+
annotateUnreadableImages(unreadableImages);
|
|
1954
|
+
}
|
|
1955
|
+
const toolConfigs = await toolConfigsPromise;
|
|
1956
|
+
const { roots, write } = await fsGrantsPromise;
|
|
1957
|
+
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
1958
|
+
key,
|
|
1959
|
+
error: errorMessage(err)
|
|
1960
|
+
}));
|
|
1961
|
+
if (await this._isHandled(engine, request)) {
|
|
1962
|
+
return;
|
|
1963
|
+
}
|
|
1964
|
+
let cacheKey;
|
|
1965
|
+
if (resumed) {
|
|
1966
|
+
cacheKey =
|
|
1967
|
+
typeof savedRequest?.cacheKey === 'string' ? savedRequest.cacheKey : undefined;
|
|
1968
|
+
}
|
|
1969
|
+
else if (!diagramRepair) {
|
|
1970
|
+
cacheKey = chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
1971
|
+
}
|
|
1972
|
+
const unloading = lender ? undefined : this._unloading.get(model);
|
|
1973
|
+
if (unloading) {
|
|
1974
|
+
await raceAbort(unloading.catch(noop), controller.signal);
|
|
1975
|
+
}
|
|
1976
|
+
this._log?.info('runs', 'completion requested', {
|
|
1977
|
+
runId,
|
|
1978
|
+
model,
|
|
1979
|
+
cacheKey,
|
|
1980
|
+
turns: messages.length
|
|
1981
|
+
});
|
|
1982
|
+
const harness = this._harnessFor(runAgent);
|
|
1983
|
+
const completionContext = {
|
|
1984
|
+
runId,
|
|
1985
|
+
runLog,
|
|
1986
|
+
resume: resumed,
|
|
1987
|
+
model,
|
|
1988
|
+
agent: runAgent,
|
|
1989
|
+
messages: resumed ? messages : nameSenders(messages),
|
|
1990
|
+
cacheKey,
|
|
1991
|
+
thinkingDefault: thinkingDefault(config),
|
|
1992
|
+
toolConfigs,
|
|
1993
|
+
grantedDirs,
|
|
1994
|
+
grantedDrives,
|
|
1995
|
+
driveOp,
|
|
1996
|
+
grantedWriteDirs: write,
|
|
1997
|
+
approve: approvals.approve,
|
|
1998
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1999
|
+
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2000
|
+
ragSearch: async (input) => {
|
|
2001
|
+
const found = await raceAbort(engine.ragSearch({
|
|
2002
|
+
chatId,
|
|
2003
|
+
query: input.query,
|
|
2004
|
+
mode: 'tool',
|
|
2005
|
+
limit: input.limit
|
|
2006
|
+
}), controller.signal);
|
|
2007
|
+
const hits = found.hits ?? [];
|
|
2008
|
+
const records = sourcesFromHits(hits, RAG_SOURCE_BOUNDS);
|
|
2009
|
+
if (records.length > 0) {
|
|
2010
|
+
try {
|
|
2011
|
+
sourceTagFilter.add(records.map((record) => record.title));
|
|
2012
|
+
retrievedRecords.push(...records.map((record, index) => ({
|
|
2013
|
+
...record,
|
|
2014
|
+
excerpt: hits[index]?.content ?? record.excerpt
|
|
2015
|
+
})));
|
|
2016
|
+
await transcript.chunk({
|
|
2017
|
+
type: 'sources',
|
|
2018
|
+
sources: { sources: records }
|
|
2019
|
+
});
|
|
2020
|
+
}
|
|
2021
|
+
catch (err) {
|
|
2022
|
+
this._log?.warn('assistant', 'rag tool sources chunk skipped', { runId, err });
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
return {
|
|
2026
|
+
enabled: found.enabled === true,
|
|
2027
|
+
indexEmpty: found.indexEmpty === true,
|
|
2028
|
+
hasKnowledge: found.hasKnowledge === true,
|
|
2029
|
+
pendingReferenced: found.pendingReferenced === true,
|
|
2030
|
+
errorMessage: found.errorMessage ?? undefined,
|
|
2031
|
+
hits
|
|
2032
|
+
};
|
|
2033
|
+
},
|
|
2034
|
+
progress: (update) => modelWatch.image(update),
|
|
2035
|
+
generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
|
|
2036
|
+
captureInferenceCalls: await this._capturingInferenceCalls(),
|
|
2037
|
+
signal: controller.signal
|
|
1788
2038
|
};
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
2039
|
+
const events = harness.completion(completionContext);
|
|
2040
|
+
let harnessEvents = 0;
|
|
2041
|
+
for await (const event of events) {
|
|
2042
|
+
harnessEvents++;
|
|
2043
|
+
modelWatch.clear();
|
|
2044
|
+
if (event.type === 'run-started' && resumed) {
|
|
2045
|
+
await requireRecovery('The worker started a fresh run instead of resuming the saved run.');
|
|
2046
|
+
return;
|
|
2047
|
+
}
|
|
2048
|
+
if (event.type === 'run-started') {
|
|
2049
|
+
continue;
|
|
2050
|
+
}
|
|
2051
|
+
if (event.type === 'run-resumed' && !resumeAccepted) {
|
|
2052
|
+
const executing = await transcript.status({ state: 'executing' }, { resumed: true });
|
|
2053
|
+
runStatusId = executing.id;
|
|
2054
|
+
resumeAccepted = true;
|
|
2055
|
+
}
|
|
2056
|
+
if (event.type === 'run-resumed') {
|
|
2057
|
+
continue;
|
|
2058
|
+
}
|
|
2059
|
+
if (resumed && !resumeAccepted && event.type === 'error') {
|
|
2060
|
+
await requireRecovery(resumeSetupFailureNotice(event));
|
|
2061
|
+
return;
|
|
2062
|
+
}
|
|
2063
|
+
if (resumed && !resumeAccepted && event.type !== 'aborted' && event.type !== 'paused') {
|
|
2064
|
+
await requireRecovery('The worker did not confirm the saved run before producing new events.');
|
|
2065
|
+
return;
|
|
2066
|
+
}
|
|
2067
|
+
if (event.type === 'round-reset') {
|
|
2068
|
+
const replacedTypes = Array.isArray(event.replacedTypes)
|
|
2069
|
+
? event.replacedTypes.filter(isReplacedType)
|
|
2070
|
+
: undefined;
|
|
2071
|
+
const round = typeof event.round === 'number' ? event.round : 0;
|
|
2072
|
+
await resetRound(round, replacedTypes);
|
|
2073
|
+
continue;
|
|
2074
|
+
}
|
|
2075
|
+
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
2076
|
+
const round = typeof event.round === 'number' ? event.round : activeRound;
|
|
2077
|
+
await writeText(event.type, event.text, round);
|
|
2078
|
+
continue;
|
|
2079
|
+
}
|
|
2080
|
+
if (event.type === 'metrics') {
|
|
2081
|
+
const { type, ...rest } = event;
|
|
2082
|
+
metrics = rest;
|
|
2083
|
+
this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
|
|
2084
|
+
continue;
|
|
2085
|
+
}
|
|
2086
|
+
if (event.type === 'inference-sent') {
|
|
2087
|
+
void this._inferenceCalls
|
|
2088
|
+
.recordSent(chatId, {
|
|
2089
|
+
runId,
|
|
2090
|
+
round: Number(event.round ?? 0),
|
|
2091
|
+
messages: event.messages,
|
|
2092
|
+
tokens: event.tokens,
|
|
2093
|
+
outcome: event.outcome,
|
|
2094
|
+
reason: event.reason
|
|
2095
|
+
})
|
|
2096
|
+
.catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
|
|
2097
|
+
continue;
|
|
2098
|
+
}
|
|
2099
|
+
if (event.type === 'inference-call') {
|
|
2100
|
+
void this._inferenceCalls
|
|
2101
|
+
.record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
|
|
2102
|
+
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
2103
|
+
continue;
|
|
2104
|
+
}
|
|
2105
|
+
await flushTranscript();
|
|
2106
|
+
if (event.type === 'error') {
|
|
2107
|
+
await failRun(event.message);
|
|
2108
|
+
return;
|
|
2109
|
+
}
|
|
2110
|
+
if (event.type === 'aborted') {
|
|
2111
|
+
await endAbortedRun();
|
|
2112
|
+
return;
|
|
2113
|
+
}
|
|
2114
|
+
if (event.type === 'paused' && controller.signal.reason === SUSPENDED) {
|
|
2115
|
+
this._seen.delete(runId);
|
|
2116
|
+
}
|
|
2117
|
+
if (event.type === 'paused' || event.type === 'recovery-required') {
|
|
2118
|
+
runVerdict = event.type;
|
|
2119
|
+
const operationName = asRecord(event.operation).name;
|
|
2120
|
+
const reason = event.type === 'paused'
|
|
2121
|
+
? 'The run paused after saving a checkpoint.'
|
|
2122
|
+
: `Recovery decision required${typeof operationName === 'string' ? ` for ${operationName}` : ''}.`;
|
|
2123
|
+
await transcript.status({ state: event.type, reason });
|
|
2124
|
+
titleOutcome = 'ended';
|
|
2125
|
+
return;
|
|
2126
|
+
}
|
|
2127
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
2128
|
+
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
2129
|
+
await transcript.chunk(toolCallChunk(event));
|
|
2130
|
+
}
|
|
2131
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
2132
|
+
await recordToolResult(event);
|
|
2133
|
+
}
|
|
2134
|
+
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
2135
|
+
void engine
|
|
2136
|
+
.recordToolActivity({
|
|
2137
|
+
agentId: runAgent.id,
|
|
2138
|
+
toolName: event.name,
|
|
2139
|
+
decision: event.decision,
|
|
2140
|
+
effect: event.effect,
|
|
2141
|
+
arguments: event.args,
|
|
2142
|
+
resource: event.resource ?? undefined,
|
|
2143
|
+
chatId,
|
|
2144
|
+
runId,
|
|
2145
|
+
at: Date.now()
|
|
2146
|
+
})
|
|
2147
|
+
.catch((err) => this._log?.warn('runs', 'tool activity record failed', { runId, err }));
|
|
2148
|
+
}
|
|
2149
|
+
else {
|
|
2150
|
+
this._log?.warn('assistant', 'unknown harness event dropped', { type: event.type });
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
if (resumed && !resumeAccepted) {
|
|
2154
|
+
await requireRecovery('The worker ended without confirming the saved run was resumed.');
|
|
2155
|
+
return;
|
|
2156
|
+
}
|
|
2157
|
+
this._log?.info('runs', 'harness stream ended', { runId, events: harnessEvents });
|
|
2158
|
+
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
2159
|
+
this._log?.info('runs', 'final flush done', { runId });
|
|
2160
|
+
}
|
|
2161
|
+
else {
|
|
2162
|
+
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
2163
|
+
runId,
|
|
2164
|
+
pending: transcript.pending()
|
|
2165
|
+
});
|
|
2166
|
+
}
|
|
2167
|
+
const answerText = [...answerTextByRound.entries()]
|
|
2168
|
+
.sort(([left], [right]) => left - right)
|
|
2169
|
+
.map(([, text]) => text)
|
|
2170
|
+
.join('');
|
|
2171
|
+
const citedTitles = injected.ragInjected
|
|
2172
|
+
? citedByPhraseOverlap(answerText, retrievedRecords)
|
|
2173
|
+
: null;
|
|
2174
|
+
await transcript.status({
|
|
2175
|
+
state: 'completed',
|
|
2176
|
+
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
2177
|
+
});
|
|
2178
|
+
titleOutcome = 'completed';
|
|
1808
2179
|
}
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
.
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
2180
|
+
catch (err) {
|
|
2181
|
+
const outcome = controller.signal.aborted
|
|
2182
|
+
? this._abortOutcome(runId, controller.signal.reason)
|
|
2183
|
+
: null;
|
|
2184
|
+
if (outcome?.kind === 'replay') {
|
|
2185
|
+
runVerdict = 'replay';
|
|
2186
|
+
this._seen.delete(runId);
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
if (outcome?.kind === 'handoff') {
|
|
2190
|
+
runVerdict = 'handoff';
|
|
2191
|
+
return;
|
|
2192
|
+
}
|
|
2193
|
+
await flushTranscript();
|
|
2194
|
+
runFailure =
|
|
2195
|
+
(this._lenderFor(runAgent) ? loanFailureNotice(err) : null) ?? runFailureNotice(err);
|
|
2196
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2197
|
+
titleOutcome = 'ended';
|
|
1821
2198
|
}
|
|
1822
|
-
|
|
2199
|
+
finally {
|
|
1823
2200
|
void this._inferenceCalls
|
|
1824
|
-
.
|
|
1825
|
-
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
1826
|
-
continue;
|
|
1827
|
-
}
|
|
1828
|
-
await flushTranscript();
|
|
1829
|
-
if (event.type === 'error') {
|
|
1830
|
-
await failRun(event.message);
|
|
1831
|
-
return;
|
|
1832
|
-
}
|
|
1833
|
-
if (event.type === 'aborted') {
|
|
1834
|
-
await endAbortedRun();
|
|
1835
|
-
return;
|
|
1836
|
-
}
|
|
1837
|
-
if (isHarnessEvent(event, 'tool-call')) {
|
|
1838
|
-
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
1839
|
-
await transcript.chunk(toolCallChunk(event));
|
|
1840
|
-
}
|
|
1841
|
-
else if (isHarnessEvent(event, 'tool-result')) {
|
|
1842
|
-
await recordToolResult(event);
|
|
1843
|
-
}
|
|
1844
|
-
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
1845
|
-
void engine
|
|
1846
|
-
.recordToolActivity({
|
|
1847
|
-
agentId: runAgent.id,
|
|
1848
|
-
toolName: event.name,
|
|
1849
|
-
decision: event.decision,
|
|
1850
|
-
effect: event.effect,
|
|
1851
|
-
arguments: event.args,
|
|
1852
|
-
resource: event.resource ?? undefined,
|
|
1853
|
-
chatId,
|
|
2201
|
+
.recordOutcome(chatId, {
|
|
1854
2202
|
runId,
|
|
1855
|
-
|
|
2203
|
+
outcome: runFailure ? 'failed' : runVerdict,
|
|
2204
|
+
reason: runFailure
|
|
1856
2205
|
})
|
|
1857
|
-
.catch((err) => this._log?.warn('
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
2206
|
+
.catch((err) => this._log?.warn('assistant', 'run outcome not recorded', { err }));
|
|
2207
|
+
transcript.close();
|
|
2208
|
+
modelWatch.stop();
|
|
2209
|
+
ownershipWatch.stop();
|
|
2210
|
+
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
2211
|
+
settledWatch.stop();
|
|
2212
|
+
if (titleOutcome && !diagramRepair) {
|
|
2213
|
+
void this._autoTitle(engine, {
|
|
2214
|
+
chatId,
|
|
2215
|
+
messages,
|
|
2216
|
+
model,
|
|
2217
|
+
agent: runAgent,
|
|
2218
|
+
probe: titleOutcome === 'completed'
|
|
2219
|
+
});
|
|
2220
|
+
}
|
|
1861
2221
|
}
|
|
1862
|
-
}
|
|
1863
|
-
|
|
1864
|
-
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
1865
|
-
this._log?.info('runs', 'final flush done', { runId });
|
|
1866
|
-
}
|
|
1867
|
-
else {
|
|
1868
|
-
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
1869
|
-
runId,
|
|
1870
|
-
pending: transcript.pending()
|
|
1871
|
-
});
|
|
1872
|
-
}
|
|
1873
|
-
const citedTitles = injected.ragInjected
|
|
1874
|
-
? citedByPhraseOverlap(answerText, retrievedRecords)
|
|
1875
|
-
: null;
|
|
1876
|
-
await transcript.status({
|
|
1877
|
-
state: 'completed',
|
|
1878
|
-
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
1879
|
-
});
|
|
1880
|
-
titleOutcome = 'completed';
|
|
1881
|
-
}
|
|
1882
|
-
catch (err) {
|
|
1883
|
-
const outcome = controller.signal.aborted
|
|
1884
|
-
? this._abortOutcome(runId, controller.signal.reason)
|
|
1885
|
-
: null;
|
|
1886
|
-
if (outcome?.kind === 'replay') {
|
|
1887
|
-
runVerdict = 'replay';
|
|
1888
|
-
this._seen.delete(runId);
|
|
1889
|
-
return;
|
|
1890
|
-
}
|
|
1891
|
-
if (outcome?.kind === 'handoff') {
|
|
1892
|
-
runVerdict = 'handoff';
|
|
1893
|
-
return;
|
|
1894
|
-
}
|
|
1895
|
-
await flushTranscript();
|
|
1896
|
-
runFailure =
|
|
1897
|
-
(this._lenderFor(runAgent) ? loanFailureNotice(err) : null) ?? runFailureNotice(err);
|
|
1898
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1899
|
-
titleOutcome = 'ended';
|
|
2222
|
+
};
|
|
2223
|
+
await runCompletion();
|
|
1900
2224
|
}
|
|
1901
2225
|
finally {
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
runId,
|
|
1905
|
-
outcome: runFailure ? 'failed' : runVerdict,
|
|
1906
|
-
reason: runFailure
|
|
1907
|
-
})
|
|
1908
|
-
.catch((err) => this._log?.warn('assistant', 'run outcome not recorded', { err }));
|
|
1909
|
-
transcript.close();
|
|
1910
|
-
modelWatch.stop();
|
|
1911
|
-
ownershipWatch.stop();
|
|
1912
|
-
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
1913
|
-
settledWatch.stop();
|
|
1914
|
-
if (titleOutcome && !diagramRepair) {
|
|
1915
|
-
void this._autoTitle(engine, {
|
|
1916
|
-
chatId,
|
|
1917
|
-
messages,
|
|
1918
|
-
model,
|
|
1919
|
-
agent: runAgent,
|
|
1920
|
-
probe: titleOutcome === 'completed'
|
|
1921
|
-
});
|
|
2226
|
+
if (!mainOwnsCleanup) {
|
|
2227
|
+
await cleanupPreflight();
|
|
1922
2228
|
}
|
|
1923
2229
|
}
|
|
1924
2230
|
}
|
|
@@ -2736,6 +3042,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2736
3042
|
if (!ask || ask.name !== call.name) {
|
|
2737
3043
|
return false;
|
|
2738
3044
|
}
|
|
3045
|
+
const currentIdentity = callIdentity(call);
|
|
3046
|
+
const currentHasIdentity = typeof currentIdentity.callId === 'string' && typeof currentIdentity.round === 'number';
|
|
3047
|
+
const previousHasIdentity = typeof chunk.callId === 'string' && typeof chunk.round === 'number';
|
|
3048
|
+
if (currentHasIdentity !== previousHasIdentity ||
|
|
3049
|
+
(currentHasIdentity &&
|
|
3050
|
+
(chunk.callId !== currentIdentity.callId || chunk.round !== currentIdentity.round))) {
|
|
3051
|
+
return false;
|
|
3052
|
+
}
|
|
2739
3053
|
return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
|
|
2740
3054
|
(ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
|
|
2741
3055
|
JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
|
|
@@ -2781,7 +3095,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2781
3095
|
});
|
|
2782
3096
|
return park(prior.id, call.name);
|
|
2783
3097
|
}
|
|
2784
|
-
|
|
3098
|
+
const identity = callIdentity(call);
|
|
3099
|
+
const sameEngineCall = typeof identity.callId === 'string' &&
|
|
3100
|
+
typeof identity.round === 'number' &&
|
|
3101
|
+
prior.callId === identity.callId &&
|
|
3102
|
+
prior.round === identity.round;
|
|
3103
|
+
if (sameEngineCall || !hasRun()) {
|
|
2785
3104
|
this._log?.info('approvals', `answer ${status} (from before the app closed)`, {
|
|
2786
3105
|
runId,
|
|
2787
3106
|
chatId,
|
|
@@ -2790,7 +3109,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2790
3109
|
});
|
|
2791
3110
|
return status === 'approved';
|
|
2792
3111
|
}
|
|
2793
|
-
this._log?.info('approvals', 'asking again:
|
|
3112
|
+
this._log?.info('approvals', 'asking again: legacy history cannot prove this call identity', {
|
|
2794
3113
|
runId,
|
|
2795
3114
|
chatId,
|
|
2796
3115
|
priorChunkId: prior.id,
|
|
@@ -2804,10 +3123,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2804
3123
|
resource: call.resource ?? undefined,
|
|
2805
3124
|
agentId
|
|
2806
3125
|
};
|
|
3126
|
+
const identity = callIdentity(call);
|
|
2807
3127
|
const chunk = await engine.appendChunk({
|
|
2808
3128
|
chatId,
|
|
2809
3129
|
runId,
|
|
2810
3130
|
type: 'approval-request',
|
|
3131
|
+
callId: identity.callId,
|
|
3132
|
+
round: identity.round,
|
|
2811
3133
|
approvalRequest,
|
|
2812
3134
|
originDeviceId: deviceId,
|
|
2813
3135
|
effect: call.effect === 'read' ? 'read' : 'write',
|
|
@@ -3047,6 +3369,8 @@ function toolCallChunk(event) {
|
|
|
3047
3369
|
return {
|
|
3048
3370
|
type: 'tool-call',
|
|
3049
3371
|
toolCall: { name: event.name, arguments: event.args },
|
|
3372
|
+
round: typeof event.round === 'number' ? event.round : undefined,
|
|
3373
|
+
callId: typeof event.callId === 'string' ? event.callId : undefined,
|
|
3050
3374
|
effect: event.effect === 'read' ? 'read' : 'write',
|
|
3051
3375
|
display: event.display,
|
|
3052
3376
|
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined
|
|
@@ -3055,7 +3379,9 @@ function toolCallChunk(event) {
|
|
|
3055
3379
|
function toolResultChunk(event) {
|
|
3056
3380
|
return {
|
|
3057
3381
|
type: 'tool-result',
|
|
3058
|
-
toolResult: { name: event.name, result: withoutDataChannel(event.result) }
|
|
3382
|
+
toolResult: { name: event.name, result: withoutDataChannel(event.result) },
|
|
3383
|
+
round: typeof event.round === 'number' ? event.round : undefined,
|
|
3384
|
+
callId: typeof event.callId === 'string' ? event.callId : undefined
|
|
3059
3385
|
};
|
|
3060
3386
|
}
|
|
3061
3387
|
function runMetricsSummary(metrics) {
|
|
@@ -3074,6 +3400,85 @@ function runMetricsSummary(metrics) {
|
|
|
3074
3400
|
function asRecord(value) {
|
|
3075
3401
|
return isRecord(value) ? { ...value } : {};
|
|
3076
3402
|
}
|
|
3403
|
+
function sameSerializedValue(left, right) {
|
|
3404
|
+
return (JSON.stringify(normalizeSerializedValue(left)) ===
|
|
3405
|
+
JSON.stringify(normalizeSerializedValue(right)));
|
|
3406
|
+
}
|
|
3407
|
+
function isReplacedType(type) {
|
|
3408
|
+
return type === 'content' || type === 'thinking';
|
|
3409
|
+
}
|
|
3410
|
+
function resumeSetupFailureNotice(event) {
|
|
3411
|
+
if (event.name === 'RunIncompatible' &&
|
|
3412
|
+
(event.kind === 'machine' || event.kind === 'log' || event.kind === 'xstate') &&
|
|
3413
|
+
typeof event.found === 'string' &&
|
|
3414
|
+
event.found.length <= 40 &&
|
|
3415
|
+
/^\d{1,10}(?:\.\d{1,10}){0,3}(?:-[A-Za-z0-9.-]{1,16})?$/.test(event.found) &&
|
|
3416
|
+
typeof event.expected === 'string' &&
|
|
3417
|
+
event.expected.length <= 40 &&
|
|
3418
|
+
/^\d{1,10}(?:\.\d{1,10}){0,3}(?:-[A-Za-z0-9.-]{1,16})?$/.test(event.expected)) {
|
|
3419
|
+
return `The saved ${event.kind} version ${event.found} is incompatible; this worker expects ${event.expected}.`;
|
|
3420
|
+
}
|
|
3421
|
+
if (event.code === 'PENDING_ARGS_REDACTED') {
|
|
3422
|
+
return 'Pending tool arguments were redacted, so this run needs an explicit recovery decision.';
|
|
3423
|
+
}
|
|
3424
|
+
if (event.name === 'RunIncompatible') {
|
|
3425
|
+
return 'This worker cannot resume the saved run because its run-log version is incompatible.';
|
|
3426
|
+
}
|
|
3427
|
+
if (event.message === 'Resume requires the original durable request digest key') {
|
|
3428
|
+
return 'The saved request digest key is unavailable, so this run needs recovery.';
|
|
3429
|
+
}
|
|
3430
|
+
if (event.message === 'Resume requires the original tool configuration') {
|
|
3431
|
+
return 'The saved tool configuration no longer matches. Restore it to resume the run.';
|
|
3432
|
+
}
|
|
3433
|
+
if (event.message ===
|
|
3434
|
+
'Resume cannot replace the original model, conversation, or execution configuration') {
|
|
3435
|
+
return 'The original model, conversation, or execution configuration changed; restore it to resume.';
|
|
3436
|
+
}
|
|
3437
|
+
return 'The saved run identity or current execution policy no longer matches. Restore the original model, agent, and tool configuration to resume.';
|
|
3438
|
+
}
|
|
3439
|
+
function savedMessagesOf(saved) {
|
|
3440
|
+
const messages = saved?.messages;
|
|
3441
|
+
return Array.isArray(messages) ? messages : [];
|
|
3442
|
+
}
|
|
3443
|
+
function stripUnseenImages(messages) {
|
|
3444
|
+
for (const turn of messages) {
|
|
3445
|
+
if (turn.images?.length) {
|
|
3446
|
+
const names = turn.images.map((image) => image.fileName).join(', ');
|
|
3447
|
+
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
3448
|
+
}
|
|
3449
|
+
delete turn.images;
|
|
3450
|
+
delete turn.pdfs;
|
|
3451
|
+
}
|
|
3452
|
+
}
|
|
3453
|
+
function callIdentity(call) {
|
|
3454
|
+
const { callId, round } = asRecord(call);
|
|
3455
|
+
return {
|
|
3456
|
+
callId: typeof callId === 'string' ? callId : undefined,
|
|
3457
|
+
round: typeof round === 'number' ? round : undefined
|
|
3458
|
+
};
|
|
3459
|
+
}
|
|
3460
|
+
function normalizeSerializedValue(value) {
|
|
3461
|
+
if (value instanceof Uint8Array) {
|
|
3462
|
+
return { type: 'Buffer', data: Array.from(value) };
|
|
3463
|
+
}
|
|
3464
|
+
if (Array.isArray(value)) {
|
|
3465
|
+
return value.map(normalizeSerializedValue);
|
|
3466
|
+
}
|
|
3467
|
+
if (!isRecord(value)) {
|
|
3468
|
+
return value;
|
|
3469
|
+
}
|
|
3470
|
+
const record = value;
|
|
3471
|
+
if (record.type === 'Buffer' && Array.isArray(record.data)) {
|
|
3472
|
+
return { type: 'Buffer', data: record.data.map(normalizeSerializedValue) };
|
|
3473
|
+
}
|
|
3474
|
+
const normalized = {};
|
|
3475
|
+
for (const key of Object.keys(record).sort()) {
|
|
3476
|
+
if (record[key] !== undefined) {
|
|
3477
|
+
normalized[key] = normalizeSerializedValue(record[key]);
|
|
3478
|
+
}
|
|
3479
|
+
}
|
|
3480
|
+
return normalized;
|
|
3481
|
+
}
|
|
3077
3482
|
function isStreamDestroyed(err) {
|
|
3078
3483
|
return isRecord(err) && err.code === 'STREAM_DESTROYED';
|
|
3079
3484
|
}
|