@peer-agent/runtime-core 0.1.0-rc.5 → 0.1.0-rc.50
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/context-accounting-lifecycle.d.ts +1 -1
- package/dist/context-accounting-lifecycle.js +1 -1
- package/dist/context-accounting-pipeline.d.ts +14 -2
- package/dist/context-accounting-pipeline.js +43 -24
- package/dist/context-compaction.d.ts +1 -0
- package/dist/context-compaction.js +1 -1
- package/dist/contracts.d.ts +3 -0
- package/dist/conversation-history-projector.d.ts +12 -1
- package/dist/conversation-history-projector.js +111 -17
- package/dist/evidence.js +8 -3
- package/dist/goal-idempotency.d.ts +2 -0
- package/dist/goal-idempotency.js +5 -0
- package/dist/goal-tool-contracts.d.ts +225 -0
- package/dist/goal-tool-contracts.js +297 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/input-prediction.d.ts +40 -0
- package/dist/input-prediction.js +143 -0
- package/dist/microcompact.d.ts +15 -1
- package/dist/microcompact.js +108 -2
- package/dist/project-input-recovery.d.ts +45 -0
- package/dist/project-input-recovery.js +491 -0
- package/dist/resource-edit-state.d.ts +101 -0
- package/dist/resource-edit-state.js +179 -0
- package/package.json +2 -2
|
@@ -31,7 +31,7 @@ export declare function createUnknownContextAccountingSnapshot(input: Readonly<{
|
|
|
31
31
|
export declare function isContextAccountingSnapshotCurrent(snapshot: ContextAccountingSnapshot | null | undefined, identity: ContextAccountingIdentity): snapshot is ContextAccountingSnapshot;
|
|
32
32
|
/**
|
|
33
33
|
* Reproject a restored/displayed context-accounting snapshot onto the current
|
|
34
|
-
* model config window
|
|
34
|
+
* model config window. Occupied tokens stay from
|
|
35
35
|
* the snapshot; only capacity fields + percent are refreshed so UI/runtime do
|
|
36
36
|
* not keep a stale window after the user changes tiers or projection is fixed.
|
|
37
37
|
*/
|
|
@@ -42,7 +42,7 @@ export function isContextAccountingSnapshotCurrent(snapshot, identity) {
|
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* Reproject a restored/displayed context-accounting snapshot onto the current
|
|
45
|
-
* model config window
|
|
45
|
+
* model config window. Occupied tokens stay from
|
|
46
46
|
* the snapshot; only capacity fields + percent are refreshed so UI/runtime do
|
|
47
47
|
* not keep a stale window after the user changes tiers or projection is fixed.
|
|
48
48
|
*/
|
|
@@ -53,7 +53,9 @@ export type ContextAccountingPipelineOptions<TState, TRequest, TResponse> = Read
|
|
|
53
53
|
error?: unknown;
|
|
54
54
|
}>) => void;
|
|
55
55
|
getOverflow?: (response: TResponse) => ContextOverflowEvidence | null | undefined;
|
|
56
|
-
fingerprintRequest?: (request: TRequest
|
|
56
|
+
fingerprintRequest?: (request: TRequest, options?: {
|
|
57
|
+
readonly refresh?: boolean;
|
|
58
|
+
}) => string;
|
|
57
59
|
now?: () => number;
|
|
58
60
|
}>;
|
|
59
61
|
export type ContextAccountingExecuteInput<TState> = Readonly<{
|
|
@@ -89,7 +91,17 @@ export declare function createRestoredObservedContextAccountingSnapshot(input: R
|
|
|
89
91
|
pendingUncountedChanges?: boolean;
|
|
90
92
|
now?: number;
|
|
91
93
|
}>): ContextAccountingSnapshot;
|
|
92
|
-
|
|
94
|
+
/** Counts real canonical JSON.stringify passes, not cache hits. Test seam. */
|
|
95
|
+
export declare function readCanonicalRequestFingerprintEvaluations(): number;
|
|
96
|
+
export declare function resetCanonicalRequestFingerprintEvaluations(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Fingerprint a canonical provider request once per object.
|
|
99
|
+
* A later call with the same reference reuses the digest. Pass `{ refresh: true }`
|
|
100
|
+
* only after an await that may have mutated the request, such as a transport retry.
|
|
101
|
+
*/
|
|
102
|
+
export declare function fingerprintCanonicalRequest(request: unknown, options?: {
|
|
103
|
+
readonly refresh?: boolean;
|
|
104
|
+
}): string;
|
|
93
105
|
export declare function parseContextOverflowEvidence(error: unknown): ContextOverflowEvidence | null;
|
|
94
106
|
export declare function createContextAccountingCompactionPipeline<TState, TRequest, TResponse>(options: ContextAccountingPipelineOptions<TState, TRequest, TResponse>): Readonly<{
|
|
95
107
|
execute: (input: ContextAccountingExecuteInput<TState>) => Promise<ContextAccountingExecuteResult<TState, TRequest, TResponse>>;
|
|
@@ -126,14 +126,39 @@ function stableValue(value, seen) {
|
|
|
126
126
|
seen.delete(value);
|
|
127
127
|
return result;
|
|
128
128
|
}
|
|
129
|
-
|
|
129
|
+
const canonicalFingerprintCache = new WeakMap();
|
|
130
|
+
let canonicalFingerprintEvaluations = 0;
|
|
131
|
+
/** Counts real canonical JSON.stringify passes, not cache hits. Test seam. */
|
|
132
|
+
export function readCanonicalRequestFingerprintEvaluations() {
|
|
133
|
+
return canonicalFingerprintEvaluations;
|
|
134
|
+
}
|
|
135
|
+
export function resetCanonicalRequestFingerprintEvaluations() {
|
|
136
|
+
canonicalFingerprintEvaluations = 0;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Fingerprint a canonical provider request once per object.
|
|
140
|
+
* A later call with the same reference reuses the digest. Pass `{ refresh: true }`
|
|
141
|
+
* only after an await that may have mutated the request, such as a transport retry.
|
|
142
|
+
*/
|
|
143
|
+
export function fingerprintCanonicalRequest(request, options) {
|
|
144
|
+
const refresh = options?.refresh === true;
|
|
145
|
+
if (!refresh && request !== null && typeof request === 'object') {
|
|
146
|
+
const cached = canonicalFingerprintCache.get(request);
|
|
147
|
+
if (cached)
|
|
148
|
+
return cached;
|
|
149
|
+
}
|
|
130
150
|
const text = JSON.stringify(stableValue(request, new WeakSet()));
|
|
151
|
+
canonicalFingerprintEvaluations += 1;
|
|
131
152
|
let hash = 0x811c9dc5;
|
|
132
153
|
for (let index = 0; index < text.length; index += 1) {
|
|
133
154
|
hash ^= text.charCodeAt(index);
|
|
134
155
|
hash = Math.imul(hash, 0x01000193);
|
|
135
156
|
}
|
|
136
|
-
|
|
157
|
+
const fingerprint = `ctx_${(hash >>> 0).toString(16).padStart(8, '0')}_${text.length}`;
|
|
158
|
+
if (request !== null && typeof request === 'object') {
|
|
159
|
+
canonicalFingerprintCache.set(request, fingerprint);
|
|
160
|
+
}
|
|
161
|
+
return fingerprint;
|
|
137
162
|
}
|
|
138
163
|
function errorDetails(error) {
|
|
139
164
|
if (error instanceof Error) {
|
|
@@ -329,14 +354,12 @@ export function createContextAccountingCompactionPipeline(options) {
|
|
|
329
354
|
}
|
|
330
355
|
}
|
|
331
356
|
};
|
|
332
|
-
|
|
357
|
+
// Exact count must not mutate the canonical request. The digest is taken once
|
|
358
|
+
// after the counter returns, so the count path does not stringify again.
|
|
359
|
+
const count = async (request) => {
|
|
333
360
|
nextCounted = undefined;
|
|
334
361
|
if (!options.countRequest || counterStatus === 'degraded')
|
|
335
|
-
return;
|
|
336
|
-
const before = fingerprint(request);
|
|
337
|
-
if (before !== requestFingerprint) {
|
|
338
|
-
throw new Error('context_request_fingerprint_mismatch: request changed before exact count');
|
|
339
|
-
}
|
|
362
|
+
return null;
|
|
340
363
|
let rawCount;
|
|
341
364
|
try {
|
|
342
365
|
rawCount = await options.countRequest(request);
|
|
@@ -347,27 +370,23 @@ export function createContextAccountingCompactionPipeline(options) {
|
|
|
347
370
|
// explicitly and continue with observed usage instead of blocking chat.
|
|
348
371
|
counterStatus = 'degraded';
|
|
349
372
|
nextCounted = undefined;
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
const counted = exactResult(rawCount, capability);
|
|
353
|
-
const after = fingerprint(request);
|
|
354
|
-
if (after !== requestFingerprint) {
|
|
355
|
-
throw new Error('context_request_fingerprint_mismatch: exact counter mutated the canonical request');
|
|
373
|
+
return null;
|
|
356
374
|
}
|
|
357
|
-
|
|
358
|
-
return;
|
|
359
|
-
nextCounted = {
|
|
360
|
-
inputTokens: counted.inputTokens,
|
|
361
|
-
requestFingerprint,
|
|
362
|
-
compactionEpoch,
|
|
363
|
-
source: counted.source,
|
|
364
|
-
countedAt: now(),
|
|
365
|
-
};
|
|
375
|
+
return exactResult(rawCount, capability);
|
|
366
376
|
};
|
|
367
377
|
const build = async (state) => {
|
|
368
378
|
const request = await options.buildRequest(state);
|
|
379
|
+
const counted = await count(request);
|
|
369
380
|
const requestFingerprint = fingerprint(request);
|
|
370
|
-
|
|
381
|
+
if (counted) {
|
|
382
|
+
nextCounted = {
|
|
383
|
+
inputTokens: counted.inputTokens,
|
|
384
|
+
requestFingerprint,
|
|
385
|
+
compactionEpoch,
|
|
386
|
+
source: counted.source,
|
|
387
|
+
countedAt: now(),
|
|
388
|
+
};
|
|
389
|
+
}
|
|
371
390
|
latestBuiltFingerprint = requestFingerprint;
|
|
372
391
|
latestBuiltRequest = request;
|
|
373
392
|
return { request, requestFingerprint };
|
|
@@ -40,6 +40,7 @@ export interface CompactionSummaryCascadeOptions<TMessage extends CompactionMess
|
|
|
40
40
|
readonly summarizeStructurally: (messages: readonly TMessage[]) => string | null | undefined;
|
|
41
41
|
readonly fallbackSummary?: string;
|
|
42
42
|
}
|
|
43
|
+
export declare function isHumanUserMessage(message: Partial<Pick<CompactionMessage, 'role' | 'content' | '_compaction'>>): boolean;
|
|
43
44
|
/**
|
|
44
45
|
* Host-neutral form of Desktop's compaction split rules. System messages are kept
|
|
45
46
|
* separately; automatic preflight preserves the newest human turn; and a split
|
|
@@ -126,7 +126,7 @@ function toolResultIds(message) {
|
|
|
126
126
|
function hasToolResult(message) {
|
|
127
127
|
return message.role === 'tool' || toolResultIds(message).length > 0;
|
|
128
128
|
}
|
|
129
|
-
function isHumanUserMessage(message) {
|
|
129
|
+
export function isHumanUserMessage(message) {
|
|
130
130
|
if (message.role !== 'user' || message._compaction)
|
|
131
131
|
return false;
|
|
132
132
|
const blocks = contentBlocks(message);
|
package/dist/contracts.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CanonicalToolMaterialObservation } from '@peer-agent/protocol';
|
|
1
2
|
export type RuntimeCapabilityId = string;
|
|
2
3
|
export type RuntimeProviderId = string;
|
|
3
4
|
export type RuntimeRunId = string;
|
|
@@ -29,6 +30,8 @@ export interface RuntimeToolError {
|
|
|
29
30
|
readonly details?: unknown;
|
|
30
31
|
}
|
|
31
32
|
export interface RuntimeToolResult {
|
|
33
|
+
/** Local canonical bytes; non-enumerable, never event/renderer/provider-message data. */
|
|
34
|
+
readonly materialObservation?: CanonicalToolMaterialObservation;
|
|
32
35
|
readonly toolCallId: string;
|
|
33
36
|
readonly capabilityId: RuntimeCapabilityId;
|
|
34
37
|
readonly status: RuntimeToolStatus;
|
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* Desktop send, Desktop restore and CLI/TUI restore must all cross this seam.
|
|
5
5
|
* Provider adapters may lower the camelCase tool fields to their wire format,
|
|
6
6
|
* but must not reinterpret persisted segments.
|
|
7
|
+
* Oversized tool bodies stay intact here so the host can spill them to a
|
|
8
|
+
* local file before the provider request. This projector does not drop them.
|
|
7
9
|
*/
|
|
8
|
-
export declare const CANONICAL_HISTORY_PROJECTOR_VERSION =
|
|
10
|
+
export declare const CANONICAL_HISTORY_PROJECTOR_VERSION = 3;
|
|
9
11
|
export type CanonicalHistoryContentPart = {
|
|
10
12
|
readonly type: 'text';
|
|
11
13
|
readonly text: string;
|
|
@@ -28,6 +30,7 @@ export interface CanonicalHistoryMessage {
|
|
|
28
30
|
readonly toolCalls?: readonly CanonicalHistoryToolCall[];
|
|
29
31
|
}
|
|
30
32
|
export interface CanonicalHistoryContinuity {
|
|
33
|
+
readonly compactionEpoch?: number;
|
|
31
34
|
readonly summary: string;
|
|
32
35
|
readonly sourceMessageId: string;
|
|
33
36
|
readonly method: string;
|
|
@@ -50,3 +53,11 @@ export declare function formatCanonicalHistoricalLocalRecord(input: {
|
|
|
50
53
|
readonly result?: unknown;
|
|
51
54
|
}): string;
|
|
52
55
|
export declare function projectConversationHistory(values: readonly unknown[]): CanonicalConversationHistory;
|
|
56
|
+
/**
|
|
57
|
+
* Same projection as projectConversationHistory, but yields during the scan so
|
|
58
|
+
* a long open-session restore does not hold the main thread until it finishes.
|
|
59
|
+
*/
|
|
60
|
+
export declare function projectConversationHistoryYielding(values: readonly unknown[], options?: {
|
|
61
|
+
readonly yieldEvery?: number;
|
|
62
|
+
readonly yield?: () => Promise<void>;
|
|
63
|
+
}): Promise<CanonicalConversationHistory>;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import { formatResourceFeedbackContext, projectResourceFeedbackTransport, normalizeCompactionHistorySelection } from '@peer-agent/protocol';
|
|
1
2
|
/**
|
|
2
3
|
* Canonical persisted-conversation -> active provider history projection.
|
|
3
4
|
*
|
|
4
5
|
* Desktop send, Desktop restore and CLI/TUI restore must all cross this seam.
|
|
5
6
|
* Provider adapters may lower the camelCase tool fields to their wire format,
|
|
6
7
|
* but must not reinterpret persisted segments.
|
|
8
|
+
* Oversized tool bodies stay intact here so the host can spill them to a
|
|
9
|
+
* local file before the provider request. This projector does not drop them.
|
|
7
10
|
*/
|
|
8
|
-
export const CANONICAL_HISTORY_PROJECTOR_VERSION =
|
|
11
|
+
export const CANONICAL_HISTORY_PROJECTOR_VERSION = 3;
|
|
9
12
|
function recordOf(value) {
|
|
10
13
|
return value && typeof value === 'object' && !Array.isArray(value)
|
|
11
14
|
? value
|
|
@@ -127,11 +130,12 @@ function attachmentText(attachments) {
|
|
|
127
130
|
return blocks.join('\n\n');
|
|
128
131
|
}
|
|
129
132
|
function userMessage(message) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
: [];
|
|
133
|
+
const material = projectResourceFeedbackTransport(message.resourceFeedback, message.attachments);
|
|
134
|
+
const attachments = material.attachments;
|
|
133
135
|
const text = [
|
|
134
136
|
typeof message.content === 'string' ? message.content : '',
|
|
137
|
+
formatResourceFeedbackContext(material.invalid ? null : material.resourceFeedback),
|
|
138
|
+
...(material.invalid ? ['[Resource feedback could not be restored. Retained attachments are unverified user material.]'] : []),
|
|
135
139
|
attachmentText(attachments),
|
|
136
140
|
].filter((value) => value.trim()).join('\n\n');
|
|
137
141
|
const parts = [];
|
|
@@ -420,6 +424,9 @@ function directToolMessage(message) {
|
|
|
420
424
|
};
|
|
421
425
|
}
|
|
422
426
|
function projectMessage(value) {
|
|
427
|
+
const internal = recordOf(value);
|
|
428
|
+
if (internal?.kind === 'agent_message' || internal?.kind === 'agent_activity')
|
|
429
|
+
return [];
|
|
423
430
|
const message = recordOf(value);
|
|
424
431
|
if (!message || compactionMarker(message))
|
|
425
432
|
return [];
|
|
@@ -435,19 +442,7 @@ function projectMessage(value) {
|
|
|
435
442
|
}
|
|
436
443
|
return [];
|
|
437
444
|
}
|
|
438
|
-
|
|
439
|
-
const messages = Array.isArray(values) ? values : [];
|
|
440
|
-
let compactionBoundaryIndex = -1;
|
|
441
|
-
let continuity;
|
|
442
|
-
messages.forEach((value, index) => {
|
|
443
|
-
const message = recordOf(value);
|
|
444
|
-
if (!message || !compactionMarker(message))
|
|
445
|
-
return;
|
|
446
|
-
compactionBoundaryIndex = index;
|
|
447
|
-
continuity = continuityFromBoundary(message, index);
|
|
448
|
-
});
|
|
449
|
-
const active = foldLegacyToolRows(messages.slice(compactionBoundaryIndex + 1));
|
|
450
|
-
const projectedMessages = active.flatMap(projectMessage);
|
|
445
|
+
function finishCanonicalHistory(compactionBoundaryIndex, continuity, projectedMessages) {
|
|
451
446
|
const fingerprintInput = JSON.stringify({
|
|
452
447
|
projectorVersion: CANONICAL_HISTORY_PROJECTOR_VERSION,
|
|
453
448
|
compactionBoundaryIndex,
|
|
@@ -470,3 +465,102 @@ export function projectConversationHistory(values) {
|
|
|
470
465
|
historyFingerprint: `history-v${CANONICAL_HISTORY_PROJECTOR_VERSION}-${(hash >>> 0).toString(16).padStart(8, '0')}`,
|
|
471
466
|
};
|
|
472
467
|
}
|
|
468
|
+
function scanCompactionBoundary(messages) {
|
|
469
|
+
let compactionBoundaryIndex = -1;
|
|
470
|
+
let continuity;
|
|
471
|
+
messages.forEach((value, index) => {
|
|
472
|
+
const message = recordOf(value);
|
|
473
|
+
if (!message || !compactionMarker(message))
|
|
474
|
+
return;
|
|
475
|
+
compactionBoundaryIndex = index;
|
|
476
|
+
continuity = continuityFromBoundary(message, index);
|
|
477
|
+
});
|
|
478
|
+
return { compactionBoundaryIndex, continuity };
|
|
479
|
+
}
|
|
480
|
+
/** Append-only compaction preserves raw chronology and stable source identity. */
|
|
481
|
+
function historySelectionCollector(messages, boundary) {
|
|
482
|
+
const row = recordOf(messages[boundary]);
|
|
483
|
+
const marker = row && compactionMarker(row);
|
|
484
|
+
if (!marker || marker.historySelection === undefined)
|
|
485
|
+
return null;
|
|
486
|
+
const selection = normalizeCompactionHistorySelection(marker.historySelection);
|
|
487
|
+
if (!selection || !row?.id || !continuityFromBoundary(row, boundary)?.summary)
|
|
488
|
+
throw new Error('compaction_history_selection_invalid');
|
|
489
|
+
const wanted = new Set(selection.retainedMessageIds);
|
|
490
|
+
const found = new Map();
|
|
491
|
+
const ordered = [];
|
|
492
|
+
return {
|
|
493
|
+
visit(i, raw) {
|
|
494
|
+
const value = recordOf(raw);
|
|
495
|
+
const id = typeof value?.id === 'string' ? value.id : '';
|
|
496
|
+
if (!wanted.has(id))
|
|
497
|
+
return;
|
|
498
|
+
if (i >= boundary || found.has(id) || !value || compactionMarker(value) || !['user', 'assistant', 'tool'].includes(String(value.role)) || ['agent_message', 'agent_activity'].includes(String(value.kind)))
|
|
499
|
+
throw new Error('compaction_history_selection_invalid');
|
|
500
|
+
found.set(id, value);
|
|
501
|
+
ordered.push(id);
|
|
502
|
+
},
|
|
503
|
+
finish() {
|
|
504
|
+
if (ordered.length !== wanted.size || ordered.some((id, i) => id !== selection.retainedMessageIds[i]))
|
|
505
|
+
throw new Error('compaction_history_selection_invalid');
|
|
506
|
+
return [...selection.retainedMessageIds.map(id => found.get(id)), ...messages.slice(boundary + 1)];
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
function selectedHistory(messages, boundary) {
|
|
511
|
+
const collector = historySelectionCollector(messages, boundary);
|
|
512
|
+
if (!collector)
|
|
513
|
+
return messages.slice(boundary + 1);
|
|
514
|
+
messages.forEach((row, i) => collector.visit(i, row));
|
|
515
|
+
return collector.finish();
|
|
516
|
+
}
|
|
517
|
+
export function projectConversationHistory(values) {
|
|
518
|
+
const messages = Array.isArray(values) ? values : [];
|
|
519
|
+
const scanned = scanCompactionBoundary(messages);
|
|
520
|
+
const active = foldLegacyToolRows(selectedHistory(messages, scanned.compactionBoundaryIndex));
|
|
521
|
+
const selection = recordOf(compactionMarker(recordOf(messages[scanned.compactionBoundaryIndex]) ?? {})?.historySelection);
|
|
522
|
+
if (selection && scanned.continuity)
|
|
523
|
+
scanned.continuity = { ...scanned.continuity, compactionEpoch: Number(selection.compactionEpoch) };
|
|
524
|
+
return finishCanonicalHistory(scanned.compactionBoundaryIndex, scanned.continuity, active.flatMap(projectMessage));
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Same projection as projectConversationHistory, but yields during the scan so
|
|
528
|
+
* a long open-session restore does not hold the main thread until it finishes.
|
|
529
|
+
*/
|
|
530
|
+
export async function projectConversationHistoryYielding(values, options) {
|
|
531
|
+
const messages = Array.isArray(values) ? values : [];
|
|
532
|
+
const yieldEvery = Math.max(1, options?.yieldEvery ?? 200);
|
|
533
|
+
const pause = options?.yield ?? (() => new Promise((resolve) => {
|
|
534
|
+
setTimeout(resolve, 0);
|
|
535
|
+
}));
|
|
536
|
+
let compactionBoundaryIndex = -1;
|
|
537
|
+
let continuity;
|
|
538
|
+
for (let index = 0; index < messages.length; index += 1) {
|
|
539
|
+
if (index > 0 && index % yieldEvery === 0)
|
|
540
|
+
await pause();
|
|
541
|
+
const message = recordOf(messages[index]);
|
|
542
|
+
if (!message || !compactionMarker(message))
|
|
543
|
+
continue;
|
|
544
|
+
compactionBoundaryIndex = index;
|
|
545
|
+
continuity = continuityFromBoundary(message, index);
|
|
546
|
+
}
|
|
547
|
+
const collector = historySelectionCollector(messages, compactionBoundaryIndex);
|
|
548
|
+
if (collector) {
|
|
549
|
+
for (let index = 0; index < messages.length; index++) {
|
|
550
|
+
if (index > 0 && index % yieldEvery === 0)
|
|
551
|
+
await pause();
|
|
552
|
+
collector.visit(index, messages[index]);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const active = foldLegacyToolRows(collector ? collector.finish() : messages.slice(compactionBoundaryIndex + 1));
|
|
556
|
+
const selection = recordOf(compactionMarker(recordOf(messages[compactionBoundaryIndex]) ?? {})?.historySelection);
|
|
557
|
+
if (selection && continuity)
|
|
558
|
+
continuity = { ...continuity, compactionEpoch: Number(selection.compactionEpoch) };
|
|
559
|
+
const projectedMessages = [];
|
|
560
|
+
for (let index = 0; index < active.length; index += 1) {
|
|
561
|
+
if (index > 0 && index % yieldEvery === 0)
|
|
562
|
+
await pause();
|
|
563
|
+
projectedMessages.push(...projectMessage(active[index]));
|
|
564
|
+
}
|
|
565
|
+
return finishCanonicalHistory(compactionBoundaryIndex, continuity, projectedMessages);
|
|
566
|
+
}
|
package/dist/evidence.js
CHANGED
|
@@ -24,12 +24,17 @@ function addEvidenceRefStrings(target, value) {
|
|
|
24
24
|
*/
|
|
25
25
|
export function collectToolEvidenceRefs(options = {}) {
|
|
26
26
|
const refs = new Set();
|
|
27
|
-
if (typeof options.toolCallId === 'string' && options.toolCallId.trim()) {
|
|
28
|
-
refs.add(`tool-result://${options.toolCallId.trim()}`);
|
|
29
|
-
}
|
|
30
27
|
const execution = asEvidenceObject(options.execution);
|
|
31
28
|
const result = asEvidenceObject(execution?.result);
|
|
32
29
|
const evidence = asEvidenceObject(result?.evidence);
|
|
30
|
+
if (typeof options.toolCallId === 'string' && options.toolCallId.trim()) {
|
|
31
|
+
const callId = options.toolCallId.trim();
|
|
32
|
+
const dispatch = evidence?.retiredDispatchId;
|
|
33
|
+
const retired = result?.status === 'cancelled' && evidence?.dispatchOutcome === 'not_dispatched'
|
|
34
|
+
&& typeof dispatch === 'string' && /^dispatch-[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(dispatch);
|
|
35
|
+
refs.add(retired ? `tool-attempt://${callId}/${dispatch}` : `tool-result://${callId}`);
|
|
36
|
+
}
|
|
37
|
+
addEvidenceRefStrings(refs, evidence?.refs);
|
|
33
38
|
addEvidenceRefStrings(refs, evidence?.artifactRefs);
|
|
34
39
|
const outputPreview = asEvidenceObject(result?.outputPreview);
|
|
35
40
|
addEvidenceRefStrings(refs, outputPreview?.artifactRef);
|
|
@@ -46,6 +46,8 @@ export declare function decideGoalToolReplay(input: {
|
|
|
46
46
|
readonly evidenceRefs?: readonly string[];
|
|
47
47
|
readonly toolCallId?: string;
|
|
48
48
|
}> | null;
|
|
49
|
+
/** Host-owned tool metadata; transient context must be re-admitted after recovery. */
|
|
50
|
+
readonly requiresFreshResult?: boolean;
|
|
49
51
|
}): GoalToolReplayDecision;
|
|
50
52
|
export interface GoalIdempotencyLedgerEntry {
|
|
51
53
|
status: string;
|
package/dist/goal-idempotency.js
CHANGED
|
@@ -16,6 +16,8 @@ export const GOAL_TOOL_REPLAY_POLICIES = Object.freeze([
|
|
|
16
16
|
]);
|
|
17
17
|
const READ_ONLY_TOOLS = new Set([
|
|
18
18
|
'read_file',
|
|
19
|
+
'view_image',
|
|
20
|
+
'git_query',
|
|
19
21
|
'list_files',
|
|
20
22
|
'search_files',
|
|
21
23
|
'batch_search',
|
|
@@ -151,6 +153,9 @@ export function decideGoalToolReplay(input) {
|
|
|
151
153
|
intent: input.intent,
|
|
152
154
|
});
|
|
153
155
|
const ledgerHit = input.completedLedger?.get(idempotencyKey) ?? null;
|
|
156
|
+
if (input.requiresFreshResult && mutationClass === 'read_only') {
|
|
157
|
+
return { action: 'execute', reason: 'fresh_read_context_required', policy: 'safe_retry', mutationClass, idempotencyKey };
|
|
158
|
+
}
|
|
154
159
|
if (ledgerHit && (ledgerHit.status === 'completed' || ledgerHit.status === 'succeeded')) {
|
|
155
160
|
return {
|
|
156
161
|
action: 'reuse',
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Goal / Plan tool contracts (platform-neutral).
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for what the model sees about the goal toolchain: the
|
|
5
|
+
* tool description (prompt) and the JSON input schema. Desktop and TUI both
|
|
6
|
+
* project from here, so the model-visible contract cannot drift per surface.
|
|
7
|
+
*
|
|
8
|
+
* Deliberately NOT owned here (stays in each surface's Manifest):
|
|
9
|
+
* - permissionPolicy / runtime adapter / availableInModes (platform policy)
|
|
10
|
+
* - execution logic and goal-plan-store wiring
|
|
11
|
+
*
|
|
12
|
+
* Note: `local-tool-contracts.ts` owns toolName + capabilityId only; input
|
|
13
|
+
* schemas stay with their capability module. This file is the goal capability's
|
|
14
|
+
* schema module.
|
|
15
|
+
*/
|
|
16
|
+
export declare const GOAL_CREATE_PLAN_PROMPT: string;
|
|
17
|
+
export declare const GOAL_UPDATE_TASK_PROMPT: string;
|
|
18
|
+
export declare const GOAL_GET_PLAN_PROMPT: string;
|
|
19
|
+
export declare const GOAL_REQUEST_EXPLORER_PROMPT: string;
|
|
20
|
+
export declare const GOAL_CREATE_PLAN_INPUT_SCHEMA: {
|
|
21
|
+
readonly type: "object";
|
|
22
|
+
readonly properties: {
|
|
23
|
+
readonly title: {
|
|
24
|
+
readonly type: "string";
|
|
25
|
+
readonly description: string;
|
|
26
|
+
};
|
|
27
|
+
readonly goal: {
|
|
28
|
+
readonly type: "string";
|
|
29
|
+
readonly description: "The concrete goal this plan aims to achieve, in one or two sentences.";
|
|
30
|
+
};
|
|
31
|
+
readonly targetWorkspacePath: {
|
|
32
|
+
readonly type: "string";
|
|
33
|
+
readonly description: string;
|
|
34
|
+
};
|
|
35
|
+
readonly parentPlanId: {
|
|
36
|
+
readonly type: "string";
|
|
37
|
+
readonly description: string;
|
|
38
|
+
};
|
|
39
|
+
readonly sourceTaskId: {
|
|
40
|
+
readonly type: "string";
|
|
41
|
+
readonly description: string;
|
|
42
|
+
};
|
|
43
|
+
readonly tasks: {
|
|
44
|
+
readonly type: "array";
|
|
45
|
+
readonly description: "Ordered list of subtasks that make up the plan.";
|
|
46
|
+
readonly items: {
|
|
47
|
+
readonly type: "object";
|
|
48
|
+
readonly properties: {
|
|
49
|
+
readonly taskId: {
|
|
50
|
+
readonly type: "string";
|
|
51
|
+
readonly description: "Optional stable id for the subtask; generated if omitted.";
|
|
52
|
+
};
|
|
53
|
+
readonly title: {
|
|
54
|
+
readonly type: "string";
|
|
55
|
+
readonly description: string;
|
|
56
|
+
};
|
|
57
|
+
readonly dependsOn: {
|
|
58
|
+
readonly type: "array";
|
|
59
|
+
readonly items: {
|
|
60
|
+
readonly type: "string";
|
|
61
|
+
};
|
|
62
|
+
readonly description: "taskIds this subtask depends on (must run after them).";
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
readonly required: readonly ["title"];
|
|
66
|
+
readonly additionalProperties: false;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
readonly successCriteria: {
|
|
70
|
+
readonly type: "array";
|
|
71
|
+
readonly description: string;
|
|
72
|
+
readonly items: {
|
|
73
|
+
readonly oneOf: readonly [{
|
|
74
|
+
readonly type: "string";
|
|
75
|
+
}, {
|
|
76
|
+
readonly type: "object";
|
|
77
|
+
readonly properties: {
|
|
78
|
+
readonly id: {
|
|
79
|
+
readonly type: "string";
|
|
80
|
+
readonly description: "Optional stable id for this criterion; generated if omitted.";
|
|
81
|
+
};
|
|
82
|
+
readonly kind: {
|
|
83
|
+
readonly type: "string";
|
|
84
|
+
readonly enum: readonly ["command", "test", "file-contains", "file-exists", "manual"];
|
|
85
|
+
readonly description: string;
|
|
86
|
+
};
|
|
87
|
+
readonly description: {
|
|
88
|
+
readonly type: "string";
|
|
89
|
+
readonly description: "Human-readable statement of what \"done\" means for this criterion.";
|
|
90
|
+
};
|
|
91
|
+
readonly command: {
|
|
92
|
+
readonly type: "string";
|
|
93
|
+
readonly description: "Shell/test command to run (for kind=command/test).";
|
|
94
|
+
};
|
|
95
|
+
readonly path: {
|
|
96
|
+
readonly type: "string";
|
|
97
|
+
readonly description: "Target file path (for kind=file-contains/file-exists).";
|
|
98
|
+
};
|
|
99
|
+
readonly expect: {
|
|
100
|
+
readonly type: "string";
|
|
101
|
+
readonly description: "Expected substring the file must contain (for kind=file-contains).";
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
readonly required: readonly ["kind"];
|
|
105
|
+
readonly additionalProperties: false;
|
|
106
|
+
}];
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
readonly required: readonly ["title", "goal", "tasks"];
|
|
111
|
+
readonly additionalProperties: false;
|
|
112
|
+
};
|
|
113
|
+
export declare const GOAL_UPDATE_TASK_INPUT_SCHEMA: {
|
|
114
|
+
readonly type: "object";
|
|
115
|
+
readonly properties: {
|
|
116
|
+
readonly planId: {
|
|
117
|
+
readonly type: "string";
|
|
118
|
+
readonly description: "Target goal plan id (planId).";
|
|
119
|
+
};
|
|
120
|
+
readonly taskId: {
|
|
121
|
+
readonly type: "string";
|
|
122
|
+
readonly description: "Target subtask id (taskId) within the plan. Must reference a leaf subtask when setting completion.";
|
|
123
|
+
};
|
|
124
|
+
readonly status: {
|
|
125
|
+
readonly type: "string";
|
|
126
|
+
readonly enum: readonly ["pending", "running", "completed", "failed", "cancelled", "waiting_user"];
|
|
127
|
+
readonly description: "New execution status for the subtask. Use \"completed\" only with evidenceRefs.";
|
|
128
|
+
};
|
|
129
|
+
readonly evidenceRefs: {
|
|
130
|
+
readonly type: "array";
|
|
131
|
+
readonly items: {
|
|
132
|
+
readonly type: "string";
|
|
133
|
+
};
|
|
134
|
+
readonly description: "Artifact refs / tool-result refs that prove the subtask outcome. Required to mark completed.";
|
|
135
|
+
};
|
|
136
|
+
readonly result: {
|
|
137
|
+
readonly type: "string";
|
|
138
|
+
readonly description: "Short factual summary of what was produced (optional).";
|
|
139
|
+
};
|
|
140
|
+
readonly failureReason: {
|
|
141
|
+
readonly type: "string";
|
|
142
|
+
readonly description: "Why the subtask failed (when status=failed).";
|
|
143
|
+
};
|
|
144
|
+
readonly blockedReason: {
|
|
145
|
+
readonly type: "string";
|
|
146
|
+
readonly description: "Why the subtask is blocked / waiting (when status=waiting_user).";
|
|
147
|
+
};
|
|
148
|
+
readonly criterionResults: {
|
|
149
|
+
readonly type: "array";
|
|
150
|
+
readonly description: string;
|
|
151
|
+
readonly items: {
|
|
152
|
+
readonly type: "object";
|
|
153
|
+
readonly properties: {
|
|
154
|
+
readonly criterionId: {
|
|
155
|
+
readonly type: "string";
|
|
156
|
+
readonly description: "The id of the success criterion this result verifies.";
|
|
157
|
+
};
|
|
158
|
+
readonly passed: {
|
|
159
|
+
readonly type: "boolean";
|
|
160
|
+
readonly description: "Whether the criterion check passed.";
|
|
161
|
+
};
|
|
162
|
+
readonly evidenceRef: {
|
|
163
|
+
readonly type: "string";
|
|
164
|
+
readonly description: "Artifact/tool-result ref proving the check outcome (e.g. the shell result ref).";
|
|
165
|
+
};
|
|
166
|
+
readonly detail: {
|
|
167
|
+
readonly type: "string";
|
|
168
|
+
readonly description: "Short human-readable detail of what was checked / observed.";
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
readonly required: readonly ["criterionId", "passed"];
|
|
172
|
+
readonly additionalProperties: false;
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
readonly required: readonly ["planId", "taskId"];
|
|
177
|
+
readonly additionalProperties: false;
|
|
178
|
+
};
|
|
179
|
+
export declare const GOAL_GET_PLAN_INPUT_SCHEMA: {
|
|
180
|
+
readonly type: "object";
|
|
181
|
+
readonly properties: {
|
|
182
|
+
readonly planId: {
|
|
183
|
+
readonly type: "string";
|
|
184
|
+
readonly description: "Target goal plan id (planId) to read back. Omit to list the active plans for the current conversation.";
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
readonly required: readonly [];
|
|
188
|
+
readonly additionalProperties: false;
|
|
189
|
+
};
|
|
190
|
+
export declare const GOAL_REQUEST_EXPLORER_INPUT_SCHEMA: {
|
|
191
|
+
readonly type: "object";
|
|
192
|
+
readonly properties: {
|
|
193
|
+
readonly question: {
|
|
194
|
+
readonly type: "string";
|
|
195
|
+
readonly description: "The focused, read-only question the Explorer should answer (what to find out).";
|
|
196
|
+
};
|
|
197
|
+
readonly reason: {
|
|
198
|
+
readonly type: "string";
|
|
199
|
+
readonly description: "Why answering this question helps advance the current goal.";
|
|
200
|
+
};
|
|
201
|
+
readonly scope: {
|
|
202
|
+
readonly type: "object";
|
|
203
|
+
readonly description: "Optional path hints to scope the read-only investigation.";
|
|
204
|
+
readonly properties: {
|
|
205
|
+
readonly include: {
|
|
206
|
+
readonly type: "array";
|
|
207
|
+
readonly items: {
|
|
208
|
+
readonly type: "string";
|
|
209
|
+
};
|
|
210
|
+
readonly description: "Path globs / directories the Explorer should focus on.";
|
|
211
|
+
};
|
|
212
|
+
readonly exclude: {
|
|
213
|
+
readonly type: "array";
|
|
214
|
+
readonly items: {
|
|
215
|
+
readonly type: "string";
|
|
216
|
+
};
|
|
217
|
+
readonly description: "Path globs / directories the Explorer should avoid.";
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
readonly additionalProperties: false;
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
readonly required: readonly ["question"];
|
|
224
|
+
readonly additionalProperties: false;
|
|
225
|
+
};
|