@myagentroam/agent 0.9.141 → 0.9.143
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/agent-core/host/contracts.d.ts +64 -0
- package/dist/{host → agent-core/host}/local-agent-host.d.ts +6 -5
- package/dist/{host → agent-core/host}/local-agent-host.js +4 -4
- package/dist/{model → agent-core/model}/anthropic-messages.js +24 -13
- package/dist/{model → agent-core/model}/contracts.d.ts +6 -16
- package/dist/{model → agent-core/model}/openai-responses.js +16 -9
- package/dist/agent-core/model/recovery.d.ts +4 -0
- package/dist/agent-core/model/recovery.js +71 -0
- package/dist/{prompts → agent-core/prompts}/core.d.ts +3 -13
- package/dist/{prompts → agent-core/prompts}/index.d.ts +1 -1
- package/dist/{prompts → agent-core/prompts}/modes.d.ts +1 -1
- package/dist/{prompts → agent-core/prompts}/resources.d.ts +1 -1
- package/dist/{runtime/compact.d.ts → agent-core/runtime/compaction/history.d.ts} +2 -23
- package/dist/{runtime/compact.js → agent-core/runtime/compaction/history.js} +2 -92
- package/dist/agent-core/runtime/compaction/operation.d.ts +22 -0
- package/dist/agent-core/runtime/compaction/operation.js +91 -0
- package/dist/agent-core/runtime/context/contracts.d.ts +12 -0
- package/dist/{runtime/environment-context.d.ts → agent-core/runtime/context/environment.d.ts} +2 -2
- package/dist/{runtime/environment-context.js → agent-core/runtime/context/environment.js} +1 -1
- package/dist/{runtime/context-gc-runtime.d.ts → agent-core/runtime/context/gc-runtime.d.ts} +4 -4
- package/dist/{runtime/context-gc-runtime.js → agent-core/runtime/context/gc-runtime.js} +3 -3
- package/dist/{runtime/context-gc-tokens.d.ts → agent-core/runtime/context/gc-tokens.d.ts} +2 -2
- package/dist/{runtime/context-gc-tokens.js → agent-core/runtime/context/gc-tokens.js} +7 -5
- package/dist/{runtime/context-gc.d.ts → agent-core/runtime/context/gc.d.ts} +3 -3
- package/dist/{runtime/context-gc.js → agent-core/runtime/context/gc.js} +152 -19
- package/dist/{runtime → agent-core/runtime/context}/instructions.d.ts +1 -1
- package/dist/{runtime → agent-core/runtime/context}/instructions.js +1 -1
- package/dist/{runtime → agent-core/runtime/context}/normalize.d.ts +1 -1
- package/dist/{runtime/context-projection.d.ts → agent-core/runtime/context/projection.d.ts} +3 -3
- package/dist/{runtime/context-projection.js → agent-core/runtime/context/projection.js} +35 -4
- package/dist/agent-core/runtime/context/request-budget.d.ts +6 -0
- package/dist/agent-core/runtime/context/request-budget.js +22 -0
- package/dist/agent-core/runtime/context/restore.d.ts +15 -0
- package/dist/agent-core/runtime/context/restore.js +85 -0
- package/dist/agent-core/runtime/context/usage.d.ts +18 -0
- package/dist/agent-core/runtime/context/usage.js +138 -0
- package/dist/agent-core/runtime/context/world-state.d.ts +35 -0
- package/dist/agent-core/runtime/context/world-state.js +133 -0
- package/dist/agent-core/runtime/contracts.d.ts +37 -0
- package/dist/agent-core/runtime/event-projection.d.ts +5 -0
- package/dist/agent-core/runtime/event-projection.js +17 -0
- package/dist/agent-core/runtime/events.d.ts +143 -0
- package/dist/agent-core/runtime/execution.d.ts +50 -0
- package/dist/agent-core/runtime/execution.js +397 -0
- package/dist/{runtime → agent-core/runtime}/rollout-budget.d.ts +1 -1
- package/dist/agent-core/sdk/agent.js +209 -0
- package/dist/agent-core/sdk/model-runtime.d.ts +13 -0
- package/dist/agent-core/sdk/model-runtime.js +63 -0
- package/dist/agent-core/sdk/session-title.d.ts +3 -0
- package/dist/agent-core/sdk/session-title.js +42 -0
- package/dist/agent-core/sdk/session.d.ts +62 -0
- package/dist/agent-core/sdk/session.js +1147 -0
- package/dist/agent-core/sdk/subagent-backend.d.ts +3 -0
- package/dist/agent-core/sdk/subagent-backend.js +46 -0
- package/dist/agent-core/sdk/types.d.ts +56 -0
- package/dist/{session → agent-core/session}/catalog.d.ts +2 -1
- package/dist/{session → agent-core/session}/context-gc-checkpoint.d.ts +3 -0
- package/dist/{session → agent-core/session}/context-gc-checkpoint.js +122 -23
- package/dist/agent-core/session/context-gc-observations.d.ts +38 -0
- package/dist/agent-core/session/context-gc-observations.js +150 -0
- package/dist/{session → agent-core/session}/context-gc-replacements.d.ts +1 -1
- package/dist/{session → agent-core/session}/context-gc-replacements.js +6 -2
- package/dist/{session → agent-core/session}/jsonl-store.d.ts +2 -2
- package/dist/{session → agent-core/session}/jsonl-store.js +1 -1
- package/dist/agent-core/subagent/contracts.d.ts +91 -0
- package/dist/agent-core/subagent/contracts.js +1 -0
- package/dist/agent-core/subagent/scheduler.d.ts +117 -0
- package/dist/{subagent → agent-core/subagent}/session-controller.d.ts +3 -2
- package/dist/agent-core/tools/agent/agent-cancel.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-cancel.js +1 -1
- package/dist/agent-core/tools/agent/agent-message.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-message.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/agent-start.d.ts +3 -3
- package/dist/{tools → agent-core/tools/agent}/agent-start.js +3 -3
- package/dist/agent-core/tools/agent/agent-wait.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-wait.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/question.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/question.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/skill.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/skill.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-read.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-write.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-write.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo.d.ts +2 -9
- package/dist/{tools → agent-core/tools/agent}/todo.js +1 -1
- package/dist/{tools → agent-core/tools}/code-mode/cells.d.ts +4 -1
- package/dist/{tools → agent-core/tools}/code-mode/cells.js +60 -6
- package/dist/{tools → agent-core/tools}/code-mode/dispatch.d.ts +2 -4
- package/dist/{tools → agent-core/tools}/code-mode/dispatch.js +10 -6
- package/dist/{tools → agent-core/tools}/code-mode/engine.d.ts +13 -1
- package/dist/agent-core/tools/code-mode/engine.js +1 -0
- package/dist/{tools/code-mode.d.ts → agent-core/tools/code-mode/index.d.ts} +7 -7
- package/dist/{tools/code-mode.js → agent-core/tools/code-mode/index.js} +190 -28
- package/dist/agent-core/tools/code-mode/observations.d.ts +103 -0
- package/dist/agent-core/tools/code-mode/observations.js +195 -0
- package/dist/{tools → agent-core/tools}/code-mode/quickjs-engine.js +91 -17
- package/dist/{tools → agent-core/tools}/code-mode/state.d.ts +2 -13
- package/dist/agent-core/tools/code-mode/worker.d.ts +1 -0
- package/dist/{tools → agent-core/tools}/code-mode/worker.js +8 -1
- package/dist/agent-core/tools/contracts.d.ts +172 -0
- package/dist/agent-core/tools/contracts.js +1 -0
- package/dist/agent-core/tools/execution.d.ts +36 -0
- package/dist/agent-core/tools/execution.js +111 -0
- package/dist/agent-core/tools/local/apply-patch.d.ts +19 -0
- package/dist/{tools → agent-core/tools/local}/apply-patch.js +8 -2
- package/dist/{tools → agent-core/tools/local}/exec.d.ts +5 -17
- package/dist/{tools → agent-core/tools/local}/exec.js +4 -4
- package/dist/{tools → agent-core/tools/local}/read.d.ts +7 -15
- package/dist/{tools → agent-core/tools/local}/read.js +185 -2
- package/dist/{tools/local-registry.d.ts → agent-core/tools/local/registry.d.ts} +5 -5
- package/dist/agent-core/tools/local/registry.js +101 -0
- package/dist/{tools → agent-core/tools/local}/view-image.d.ts +4 -9
- package/dist/{tools → agent-core/tools/local}/view-image.js +2 -2
- package/dist/agent-core/tools/local/web-fetch.d.ts +4 -0
- package/dist/agent-core/tools/local/web-fetch.js +85 -0
- package/dist/agent-core/tools/local/web-search.d.ts +7 -0
- package/dist/agent-core/tools/local/web-search.js +34 -0
- package/dist/{tools/local-workspace-tools.js → agent-core/tools/local/workspace-tools.js} +1 -1
- package/dist/{sdk/mcp-catalog.d.ts → agent-core/tools/mcp/catalog.d.ts} +1 -2
- package/dist/{sdk/mcp-catalog.js → agent-core/tools/mcp/catalog.js} +1 -1
- package/dist/agent-core/tools/mcp/history.d.ts +4 -0
- package/dist/agent-core/tools/mcp/history.js +33 -0
- package/dist/{tools → agent-core/tools/mcp}/tool-search.d.ts +3 -3
- package/dist/{tools → agent-core/tools/mcp}/tool-search.js +4 -4
- package/dist/agent-core/tools/output-projection.d.ts +14 -0
- package/dist/agent-core/tools/output-projection.js +223 -0
- package/dist/{tools → agent-core/tools}/registry.d.ts +1 -1
- package/dist/agent-core/tools/registry.js +47 -0
- package/dist/{tools → agent-core/tools}/runtime-context.d.ts +8 -7
- package/dist/agent-core/tools/runtime-context.js +1 -0
- package/dist/{tools → agent-core/tools}/runtime.d.ts +1 -1
- package/dist/cli/codex-auth-file.d.ts +1 -1
- package/dist/cli/codex-auth-file.js +2 -2
- package/dist/cli/main.js +9 -9
- package/dist/index.d.ts +17 -15
- package/dist/index.js +8 -8
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.d.ts +1 -1
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.js +1 -1
- package/package.json +3 -3
- package/dist/host/contracts.d.ts +0 -21
- package/dist/sdk/agent.js +0 -2548
- package/dist/sdk/types.d.ts +0 -349
- package/dist/subagent/scheduler.d.ts +0 -206
- package/dist/tools/agent-cancel.d.ts +0 -7
- package/dist/tools/agent-message.d.ts +0 -7
- package/dist/tools/agent-wait.d.ts +0 -7
- package/dist/tools/apply-patch.d.ts +0 -32
- package/dist/tools/local-registry.js +0 -373
- package/dist/tools/registry.js +0 -47
- package/dist/tools/web-fetch.d.ts +0 -2
- package/dist/tools/web-fetch.js +0 -12
- package/dist/tools/web-search.d.ts +0 -2
- package/dist/tools/web-search.js +0 -15
- /package/dist/{error.d.ts → agent-core/error.d.ts} +0 -0
- /package/dist/{error.js → agent-core/error.js} +0 -0
- /package/dist/{host → agent-core/host}/contracts.js +0 -0
- /package/dist/{host → agent-core/host}/local-process-executor.d.ts +0 -0
- /package/dist/{host → agent-core/host}/local-process-executor.js +0 -0
- /package/dist/{host → agent-core/host}/network.d.ts +0 -0
- /package/dist/{host → agent-core/host}/network.js +0 -0
- /package/dist/{model → agent-core/model}/anthropic-messages.d.ts +0 -0
- /package/dist/{model → agent-core/model}/anthropic-thinking.d.ts +0 -0
- /package/dist/{model → agent-core/model}/anthropic-thinking.js +0 -0
- /package/dist/{model → agent-core/model}/catalog-file.d.ts +0 -0
- /package/dist/{model → agent-core/model}/catalog-file.js +0 -0
- /package/dist/{model → agent-core/model}/configuration.d.ts +0 -0
- /package/dist/{model → agent-core/model}/configuration.js +0 -0
- /package/dist/{model → agent-core/model}/contracts.js +0 -0
- /package/dist/{model → agent-core/model}/http.d.ts +0 -0
- /package/dist/{model → agent-core/model}/http.js +0 -0
- /package/dist/{model → agent-core/model}/openai-responses.d.ts +0 -0
- /package/dist/{model → agent-core/model}/runtime-credential.d.ts +0 -0
- /package/dist/{model → agent-core/model}/runtime-credential.js +0 -0
- /package/dist/{model → agent-core/model}/sse.d.ts +0 -0
- /package/dist/{model → agent-core/model}/sse.js +0 -0
- /package/dist/{model → agent-core/model}/transport-test.d.ts +0 -0
- /package/dist/{model → agent-core/model}/transport-test.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/compact.d.ts +0 -0
- /package/dist/{prompts → agent-core/prompts}/compact.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/core.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/execution-budget.d.ts +0 -0
- /package/dist/{prompts → agent-core/prompts}/execution-budget.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/index.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/modes.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/output.d.ts +0 -0
- /package/dist/{prompts → agent-core/prompts}/output.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/resources.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/subagent.d.ts +0 -0
- /package/dist/{prompts → agent-core/prompts}/subagent.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/title.d.ts +0 -0
- /package/dist/{prompts → agent-core/prompts}/title.js +0 -0
- /package/dist/{prompts → agent-core/prompts}/workflow.d.ts +0 -0
- /package/dist/{prompts → agent-core/prompts}/workflow.js +0 -0
- /package/dist/{runtime → agent-core/runtime}/async-queue.d.ts +0 -0
- /package/dist/{runtime → agent-core/runtime}/async-queue.js +0 -0
- /package/dist/{sdk/types.js → agent-core/runtime/context/contracts.js} +0 -0
- /package/dist/{runtime → agent-core/runtime/context}/normalize.js +0 -0
- /package/dist/{session/types.js → agent-core/runtime/contracts.js} +0 -0
- /package/dist/{tools/code-mode/engine.js → agent-core/runtime/events.js} +0 -0
- /package/dist/{runtime → agent-core/runtime}/execution-policy.d.ts +0 -0
- /package/dist/{runtime → agent-core/runtime}/execution-policy.js +0 -0
- /package/dist/{runtime → agent-core/runtime}/plan-output.d.ts +0 -0
- /package/dist/{runtime → agent-core/runtime}/plan-output.js +0 -0
- /package/dist/{runtime → agent-core/runtime}/rollout-budget.js +0 -0
- /package/dist/{runtime → agent-core/runtime}/token-budget.d.ts +0 -0
- /package/dist/{runtime → agent-core/runtime}/token-budget.js +0 -0
- /package/dist/{sdk → agent-core/sdk}/agent.d.ts +0 -0
- /package/dist/{tools/code-mode/worker.d.ts → agent-core/sdk/types.js} +0 -0
- /package/dist/{session → agent-core/session}/catalog.js +0 -0
- /package/dist/{session → agent-core/session}/context-gc-subagent.d.ts +0 -0
- /package/dist/{session → agent-core/session}/context-gc-subagent.js +0 -0
- /package/dist/{session → agent-core/session}/transaction-recovery.d.ts +0 -0
- /package/dist/{session → agent-core/session}/transaction-recovery.js +0 -0
- /package/dist/{session → agent-core/session}/types.d.ts +0 -0
- /package/dist/{tools/runtime-context.js → agent-core/session/types.js} +0 -0
- /package/dist/{subagent → agent-core/subagent}/scheduler.js +0 -0
- /package/dist/{subagent → agent-core/subagent}/session-controller.js +0 -0
- /package/dist/{tools → agent-core/tools/agent}/todo-read.js +0 -0
- /package/dist/{tools → agent-core/tools}/callers.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/callers.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/quickjs-engine.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/state.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/text-output.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/text-output.js +0 -0
- /package/dist/{tools → agent-core/tools}/execution-limits.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/execution-limits.js +0 -0
- /package/dist/{tools → agent-core/tools/local}/read-context.d.ts +0 -0
- /package/dist/{tools → agent-core/tools/local}/read-context.js +0 -0
- /package/dist/{tools/local-workspace-tools.d.ts → agent-core/tools/local/workspace-tools.d.ts} +0 -0
- /package/dist/{tools → agent-core/tools}/runtime.js +0 -0
- /package/dist/{tools → agent-core/tools}/scheduler.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/scheduler.js +0 -0
- /package/dist/{tools → agent-core/tools}/shared/path-resolver.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/shared/path-resolver.js +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ClientToolDefinition, ModelUsage } from '
|
|
2
|
-
import { type ContextGcPlatform } from '
|
|
3
|
-
import { JsonlSessionStore, type SessionRecord } from '
|
|
4
|
-
import { type ContextProjection } from './
|
|
1
|
+
import type { ClientToolDefinition, ModelUsage } from '../../model/contracts.js';
|
|
2
|
+
import { type ContextGcPlatform } from '../../session/context-gc-checkpoint.js';
|
|
3
|
+
import { JsonlSessionStore, type SessionRecord } from '../../session/jsonl-store.js';
|
|
4
|
+
import { type ContextProjection } from './projection.js';
|
|
5
5
|
export interface ContextGcRuntimeInput {
|
|
6
6
|
readonly store: JsonlSessionStore;
|
|
7
7
|
readonly sessionId: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
-
import { ContextGcCheckpointCache, contextGcMinimumUsageGrowth, contextGcReplacementKey, contextGcSourceProjectionHash } from '
|
|
3
|
-
import { CONTEXT_GC_POLICY, contextGcEligibleUsage, inspectContextGc } from './
|
|
4
|
-
import { buildContextProjection } from './
|
|
2
|
+
import { ContextGcCheckpointCache, contextGcMinimumUsageGrowth, contextGcReplacementKey, contextGcSourceProjectionHash } from '../../session/context-gc-checkpoint.js';
|
|
3
|
+
import { CONTEXT_GC_POLICY, contextGcEligibleUsage, inspectContextGc } from './gc.js';
|
|
4
|
+
import { buildContextProjection } from './projection.js';
|
|
5
5
|
/** Only scheduling metadata survives a call; records and validation caches do not. */
|
|
6
6
|
export class ContextGcRuntime {
|
|
7
7
|
#scope = '';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ClientToolDefinition, ModelMessage } from '
|
|
2
|
-
export { MODEL_TOKEN_ESTIMATOR as CONTEXT_GC_TOKEN_ESTIMATOR } from '
|
|
1
|
+
import type { ClientToolDefinition, ModelMessage } from '../../model/contracts.js';
|
|
2
|
+
export { MODEL_TOKEN_ESTIMATOR as CONTEXT_GC_TOKEN_ESTIMATOR } from '../token-budget.js';
|
|
3
3
|
interface TokenContext {
|
|
4
4
|
readonly modelId: string;
|
|
5
5
|
readonly modelProtocol?: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { openAiInput } from '
|
|
2
|
-
import { countModelTokens } from '
|
|
3
|
-
export { MODEL_TOKEN_ESTIMATOR as CONTEXT_GC_TOKEN_ESTIMATOR } from '
|
|
1
|
+
import { openAiInput } from '../../model/openai-responses.js';
|
|
2
|
+
import { countModelTokens } from '../token-budget.js';
|
|
3
|
+
export { MODEL_TOKEN_ESTIMATOR as CONTEXT_GC_TOKEN_ESTIMATOR } from '../token-budget.js';
|
|
4
4
|
/** Uses Responses lowering, including native alias and cross-model filtering. */
|
|
5
5
|
export function contextGcTokenEntries(input, messages) {
|
|
6
6
|
return serializedEntries(input, messages).map((entry) => ({
|
|
@@ -14,8 +14,10 @@ function serializedEntries(input, messages) {
|
|
|
14
14
|
(input.messages ?? messages).some((message) => message.provider === 'OPENAI_RESPONSES'));
|
|
15
15
|
const values = responses
|
|
16
16
|
? openAiInput(messages, undefined, input.modelId, true, true, input.responsesConfigurationUpdates !== false)
|
|
17
|
-
: messages
|
|
18
|
-
(message.
|
|
17
|
+
: messages
|
|
18
|
+
.filter((message) => message.role !== 'provider' ||
|
|
19
|
+
(message.provider === 'ANTHROPIC_MESSAGES' && message.providerModelId === input.modelId))
|
|
20
|
+
.map(({ observations: _observations, observationId: _observationId, ...message }) => message);
|
|
19
21
|
return values.map((value, index) => {
|
|
20
22
|
const item = value;
|
|
21
23
|
const callId = item.call_id ?? item.callId;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ClientToolDefinition, ModelMessage, ModelUsage } from '
|
|
2
|
-
import { ContextGcCheckpointCache, type ContextGcCheckpointDraft, type ContextGcEconomicsPolicy, type ContextGcMetrics, type ContextGcPlatform, type ContextGcReplacement } from '
|
|
3
|
-
import type { SessionRecord } from '
|
|
1
|
+
import type { ClientToolDefinition, ModelMessage, ModelUsage } from '../../model/contracts.js';
|
|
2
|
+
import { ContextGcCheckpointCache, type ContextGcCheckpointDraft, type ContextGcEconomicsPolicy, type ContextGcMetrics, type ContextGcPlatform, type ContextGcReplacement } from '../../session/context-gc-checkpoint.js';
|
|
3
|
+
import type { SessionRecord } from '../../session/jsonl-store.js';
|
|
4
4
|
export declare const CONTEXT_GC_POLICY: ContextGcEconomicsPolicy;
|
|
5
5
|
interface ContextGcEconomicsInput {
|
|
6
6
|
readonly contextUsageTokens: number;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { countModelTokens } from '../token-budget.js';
|
|
2
|
+
import { renderObservations } from '../../tools/code-mode/observations.js';
|
|
3
|
+
import { codeCallEvidence, codeObservationEvidence, retainedCodeInputs } from '../../session/context-gc-observations.js';
|
|
4
|
+
import { ContextGcCheckpointCache, contextGcBaseFromRecords, contextGcLogicalPathKey, contextGcMinimumUsageGrowth, contextGcRecordSourceHash, contextGcTargetKey, normalizeContextGcReplacements } from '../../session/context-gc-checkpoint.js';
|
|
5
|
+
import { codeCellCompletionSequence, contextGcApplyPatchInputReplacement, contextGcCodeModeInputReplacement, contextGcCodeModeOutputReplacement, contextGcExecInputReplacement, contextGcExecOutputReplacement, contextGcPatchResultFromCompletedPayload, contextGcStaleReadReplacement, contextGcWebOutputReplacement, normalizeContextGcCodeModeInput, normalizeContextGcExecResult } from '../../session/context-gc-replacements.js';
|
|
6
|
+
import { contextGcSubagentNeedsRetainedPriorFinal, contextGcSubagentOutputReplacement, contextGcSubagentPriorFinalIsRetained, contextGcSubagentSnapshotIsPreciselyCovered, contextGcSubagentSnapshotIsSuperseded, contextGcSubagentSnapshotTurnId, normalizeContextGcSubagentSnapshot } from '../../session/context-gc-subagent.js';
|
|
7
|
+
import { parseReadContextOutput } from '../../tools/local/read-context.js';
|
|
8
|
+
import { CONTEXT_GC_TOKEN_ESTIMATOR, contextGcCacheImpact, contextGcMessageTokens, contextGcPrefixTokens, contextGcReclaimedTokens } from './gc-tokens.js';
|
|
9
|
+
import { applyContextGcReplacementsToMessages, providerToolCallId, providerToolName } from './projection.js';
|
|
7
10
|
export const CONTEXT_GC_POLICY = {
|
|
8
11
|
version: 1,
|
|
9
12
|
immediatePaybackRollouts: 3,
|
|
@@ -258,6 +261,37 @@ function collectCodeModeCandidates(input, groups, existingTargets, usageRatio) {
|
|
|
258
261
|
: LARGE_TERMINAL_OUTPUT_TOKENS;
|
|
259
262
|
if (visibleTokens < minimumVisibleTokens)
|
|
260
263
|
continue;
|
|
264
|
+
for (const observation of group.result.message.observations ?? []) {
|
|
265
|
+
if (observation.type !== 'value' ||
|
|
266
|
+
countModelTokens(observation.value) < minimumVisibleTokens)
|
|
267
|
+
continue;
|
|
268
|
+
const target = targetFor(group.result.record, {
|
|
269
|
+
...group.result.message,
|
|
270
|
+
observationId: observation.id
|
|
271
|
+
});
|
|
272
|
+
if (!target || existingTargets.has(contextGcTargetKey(target)))
|
|
273
|
+
continue;
|
|
274
|
+
const evidenceSequence = group.result.record.sequence;
|
|
275
|
+
const candidate = {
|
|
276
|
+
replacements: [
|
|
277
|
+
{
|
|
278
|
+
kind: 'CODE_MODE_OUTPUT',
|
|
279
|
+
callId: group.callId,
|
|
280
|
+
target,
|
|
281
|
+
replacementContent: contextGcCodeModeOutputReplacement(observation.value, observation.truncated === true),
|
|
282
|
+
evidenceSequence
|
|
283
|
+
}
|
|
284
|
+
],
|
|
285
|
+
observedRollouts: successfulRolloutsAfter(input.records, Math.max(evidenceSequence, completionSequence), input.modelId, input.contextWindowTokens),
|
|
286
|
+
evidenceSequence
|
|
287
|
+
};
|
|
288
|
+
if (candidate.observedRollouts >= CONTEXT_GC_POLICY.largeExecProtectionRollouts)
|
|
289
|
+
eligible.push(candidate);
|
|
290
|
+
else {
|
|
291
|
+
protectedTokens += candidateTokens(input, candidate);
|
|
292
|
+
retryAfterRollouts = Math.min(retryAfterRollouts, CONTEXT_GC_POLICY.largeExecProtectionRollouts - candidate.observedRollouts);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
261
295
|
const callTarget = targetFor(group.call.record, group.call.message);
|
|
262
296
|
const resultTarget = targetFor(group.result.record, group.result.message);
|
|
263
297
|
if (!callTarget || !resultTarget)
|
|
@@ -270,13 +304,14 @@ function collectCodeModeCandidates(input, groups, existingTargets, usageRatio) {
|
|
|
270
304
|
toolName: group.toolName,
|
|
271
305
|
callId: group.callId,
|
|
272
306
|
target: callTarget,
|
|
273
|
-
replacementArguments: contextGcCodeModeInputReplacement(code),
|
|
307
|
+
replacementArguments: contextGcCodeModeInputReplacement(code, group.result.message.observations ? retainedCodeInputs(input.records, group.callId) : []),
|
|
274
308
|
providerAliasTargets: group.aliases
|
|
275
309
|
.map(({ record, message }) => targetFor(record, message))
|
|
276
310
|
.filter((target) => target !== undefined),
|
|
277
311
|
evidenceSequence
|
|
278
312
|
});
|
|
279
|
-
if (!
|
|
313
|
+
if (!group.result.message.observations &&
|
|
314
|
+
!existingTargets.has(contextGcTargetKey(resultTarget)))
|
|
280
315
|
replacements.push({
|
|
281
316
|
kind: 'CODE_MODE_OUTPUT',
|
|
282
317
|
callId: group.callId,
|
|
@@ -503,7 +538,7 @@ function collectStageACandidates(input, groups, existingReplacements, existingTa
|
|
|
503
538
|
const reads = [...groups.values()].filter((group) => group.toolName === 'read' &&
|
|
504
539
|
group.call?.message.role === 'tool_call' &&
|
|
505
540
|
group.result?.message.role === 'tool');
|
|
506
|
-
const claimedReadItems = new Set([...existingReadReplacements.values()].flatMap((replacement) => replacement.items.map((item) => `${replacement.target.recordId}:${item.index}`)));
|
|
541
|
+
const claimedReadItems = new Set([...existingReadReplacements.values()].flatMap((replacement) => replacement.items.map((item) => `${replacement.target.recordId}:${replacement.target.observationId ?? ''}:${item.index}`)));
|
|
507
542
|
const patches = [...groups.values()]
|
|
508
543
|
.flatMap((patch) => {
|
|
509
544
|
if (patch.toolName !== 'apply_patch' ||
|
|
@@ -534,7 +569,7 @@ function collectStageACandidates(input, groups, existingReplacements, existingTa
|
|
|
534
569
|
evidenceSequence
|
|
535
570
|
});
|
|
536
571
|
for (const read of reads) {
|
|
537
|
-
if (read.result.record.sequence >=
|
|
572
|
+
if ((read.completed?.sequence ?? read.result.record.sequence) >= patch.completed.sequence)
|
|
538
573
|
continue;
|
|
539
574
|
const parsed = parseReadContextOutput(read.result.message.content);
|
|
540
575
|
if (!parsed)
|
|
@@ -542,7 +577,7 @@ function collectStageACandidates(input, groups, existingReplacements, existingTa
|
|
|
542
577
|
const selected = parsed.items.flatMap((item) => {
|
|
543
578
|
if (item.pathTruncated)
|
|
544
579
|
return [];
|
|
545
|
-
const itemKey = `${read.result.record.recordId}:${item.index}`;
|
|
580
|
+
const itemKey = `${read.result.record.recordId}:${read.result.message.observationId ?? ''}:${item.index}`;
|
|
546
581
|
if (claimedReadItems.has(itemKey))
|
|
547
582
|
return [];
|
|
548
583
|
const pathKey = contextGcLogicalPathKey(item.path, input.workspace, input.platform);
|
|
@@ -562,7 +597,8 @@ function collectStageACandidates(input, groups, existingReplacements, existingTa
|
|
|
562
597
|
supersededByCallId: patch.callId,
|
|
563
598
|
operation: matched.operation
|
|
564
599
|
}),
|
|
565
|
-
evidenceSequence
|
|
600
|
+
evidenceSequence,
|
|
601
|
+
...(read.result.message.observationId ? { supersededByCallId: patch.callId } : {})
|
|
566
602
|
}
|
|
567
603
|
];
|
|
568
604
|
});
|
|
@@ -616,8 +652,10 @@ function collectSubagentCandidates(input, groups, existingTargets) {
|
|
|
616
652
|
const turnId = snapshot && contextGcSubagentSnapshotTurnId(snapshot);
|
|
617
653
|
return snapshot && turnId ? [{ group, snapshot, turnId }] : [];
|
|
618
654
|
})
|
|
619
|
-
.sort((left, right) => left.group.
|
|
655
|
+
.sort((left, right) => left.group.completed.sequence - right.group.completed.sequence);
|
|
620
656
|
for (const [index, source] of invocations.entries()) {
|
|
657
|
+
if (source.group.evidenceOnly)
|
|
658
|
+
continue;
|
|
621
659
|
if (source.group.toolName !== 'agent_wait' &&
|
|
622
660
|
source.group.toolName !== 'agent_message' &&
|
|
623
661
|
source.group.toolName !== 'agent_cancel')
|
|
@@ -759,7 +797,9 @@ function collectExecCandidates(input, groups, existingTargets, usageRatio) {
|
|
|
759
797
|
if (!terminal || (processId && input.activeProcessIds.has(processId)))
|
|
760
798
|
continue;
|
|
761
799
|
const final = lifecycle.at(-1);
|
|
762
|
-
const visibleTokens = contextGcMessageTokens(input, lifecycle
|
|
800
|
+
const visibleTokens = contextGcMessageTokens(input, lifecycle
|
|
801
|
+
.filter((invocation) => !invocation.group.evidenceOnly)
|
|
802
|
+
.flatMap((invocation) => [
|
|
763
803
|
invocation.group.call.message,
|
|
764
804
|
invocation.group.result.message,
|
|
765
805
|
...invocation.group.aliases.map((alias) => alias.message)
|
|
@@ -773,13 +813,15 @@ function collectExecCandidates(input, groups, existingTargets, usageRatio) {
|
|
|
773
813
|
const observedRollouts = successfulRolloutsAfter(input.records, evidenceSequence, input.modelId, input.contextWindowTokens);
|
|
774
814
|
const replacements = [];
|
|
775
815
|
for (const invocation of lifecycle) {
|
|
816
|
+
if (invocation.group.evidenceOnly)
|
|
817
|
+
continue;
|
|
776
818
|
const callTarget = targetFor(invocation.group.call.record, invocation.group.call.message);
|
|
777
819
|
const resultTarget = targetFor(invocation.group.result.record, invocation.group.result.message);
|
|
778
|
-
if (!
|
|
820
|
+
if (!resultTarget) {
|
|
779
821
|
replacements.splice(0);
|
|
780
822
|
break;
|
|
781
823
|
}
|
|
782
|
-
if (!existingTargets.has(contextGcTargetKey(callTarget)))
|
|
824
|
+
if (callTarget && !existingTargets.has(contextGcTargetKey(callTarget)))
|
|
783
825
|
replacements.push({
|
|
784
826
|
kind: 'TOOL_CALL_INPUT',
|
|
785
827
|
toolName: invocation.group.toolName,
|
|
@@ -794,6 +836,7 @@ function collectExecCandidates(input, groups, existingTargets, usageRatio) {
|
|
|
794
836
|
if (!existingTargets.has(contextGcTargetKey(resultTarget)))
|
|
795
837
|
replacements.push({
|
|
796
838
|
kind: 'EXEC_OUTPUT',
|
|
839
|
+
...(resultTarget.observationId ? { terminalCallId: final.group.callId } : {}),
|
|
797
840
|
callId: invocation.group.callId,
|
|
798
841
|
target: resultTarget,
|
|
799
842
|
replacementContent: contextGcExecOutputReplacement(invocation.result),
|
|
@@ -918,6 +961,89 @@ function buildToolGroups(records, messages, sources) {
|
|
|
918
961
|
else if (type === 'tool.failed')
|
|
919
962
|
current.failed = record;
|
|
920
963
|
}
|
|
964
|
+
for (const [index, message] of messages.entries()) {
|
|
965
|
+
if (!message.observations || message.role !== 'tool')
|
|
966
|
+
continue;
|
|
967
|
+
const outer = message.callId ? groups.get(message.callId) : undefined;
|
|
968
|
+
if (!outer?.completed ||
|
|
969
|
+
outer.failed ||
|
|
970
|
+
codeCellCompletionSequence(records, outer.completed) === undefined)
|
|
971
|
+
continue;
|
|
972
|
+
const record = sources[index];
|
|
973
|
+
for (const entry of message.calls ?? []) {
|
|
974
|
+
if (![
|
|
975
|
+
'apply_patch',
|
|
976
|
+
'exec',
|
|
977
|
+
'exec_wait',
|
|
978
|
+
'agent_start',
|
|
979
|
+
'agent_wait',
|
|
980
|
+
'agent_message',
|
|
981
|
+
'agent_cancel'
|
|
982
|
+
].includes(entry.toolName) ||
|
|
983
|
+
message.observations.some((value) => value.type === 'tool_result' && value.source.callId === entry.callId))
|
|
984
|
+
continue;
|
|
985
|
+
const evidence = codeCallEvidence(records, record, entry);
|
|
986
|
+
if (!evidence)
|
|
987
|
+
continue;
|
|
988
|
+
groups.set(`execution:${evidence.callId}`, {
|
|
989
|
+
evidenceOnly: true,
|
|
990
|
+
callId: evidence.callId,
|
|
991
|
+
toolName: entry.toolName,
|
|
992
|
+
aliases: [],
|
|
993
|
+
completed: evidence.completed,
|
|
994
|
+
call: {
|
|
995
|
+
record: evidence.call,
|
|
996
|
+
message: {
|
|
997
|
+
role: 'tool_call',
|
|
998
|
+
callId: evidence.callId,
|
|
999
|
+
name: entry.toolName,
|
|
1000
|
+
content: '',
|
|
1001
|
+
arguments: entry.input
|
|
1002
|
+
}
|
|
1003
|
+
},
|
|
1004
|
+
result: {
|
|
1005
|
+
record,
|
|
1006
|
+
message: {
|
|
1007
|
+
role: 'tool',
|
|
1008
|
+
callId: message.callId,
|
|
1009
|
+
content: evidence.result.payload.content
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
for (const observation of message.observations) {
|
|
1015
|
+
if (observation.type !== 'tool_result')
|
|
1016
|
+
continue;
|
|
1017
|
+
const evidence = codeObservationEvidence(records, record, observation);
|
|
1018
|
+
if (!evidence)
|
|
1019
|
+
continue;
|
|
1020
|
+
groups.set(`${evidence.callId}:${observation.id}`, {
|
|
1021
|
+
callId: evidence.callId,
|
|
1022
|
+
toolName: observation.source.toolName,
|
|
1023
|
+
aliases: [],
|
|
1024
|
+
completed: evidence.completed,
|
|
1025
|
+
call: {
|
|
1026
|
+
record: evidence.call,
|
|
1027
|
+
message: {
|
|
1028
|
+
role: 'tool_call',
|
|
1029
|
+
callId: evidence.callId,
|
|
1030
|
+
name: observation.source.toolName,
|
|
1031
|
+
content: '',
|
|
1032
|
+
arguments: observation.source.input
|
|
1033
|
+
}
|
|
1034
|
+
},
|
|
1035
|
+
result: {
|
|
1036
|
+
record,
|
|
1037
|
+
message: {
|
|
1038
|
+
role: 'tool',
|
|
1039
|
+
callId: message.callId,
|
|
1040
|
+
content: observation.result.content,
|
|
1041
|
+
observationId: observation.id
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
921
1047
|
return groups;
|
|
922
1048
|
}
|
|
923
1049
|
function successfulRolloutsAfter(records, evidenceSequence, modelId, contextWindowTokens) {
|
|
@@ -1111,9 +1237,15 @@ function candidateTokens(input, candidate) {
|
|
|
1111
1237
|
? replacement.providerAliasTargets.map(contextGcTargetKey)
|
|
1112
1238
|
: [])
|
|
1113
1239
|
]));
|
|
1114
|
-
return contextGcMessageTokens(input, input.messages.
|
|
1240
|
+
return contextGcMessageTokens(input, input.messages.flatMap((message, index) => {
|
|
1115
1241
|
const target = targetFor(input.messageSources[index], message);
|
|
1116
|
-
|
|
1242
|
+
if (target && targets.has(contextGcTargetKey(target)))
|
|
1243
|
+
return [message];
|
|
1244
|
+
const selected = target &&
|
|
1245
|
+
message.observations?.filter((value) => targets.has(contextGcTargetKey({ ...target, observationId: value.id })));
|
|
1246
|
+
return selected?.length
|
|
1247
|
+
? [{ ...message, observations: selected, content: renderObservations(selected) }]
|
|
1248
|
+
: [];
|
|
1117
1249
|
}));
|
|
1118
1250
|
}
|
|
1119
1251
|
function targetFor(record, message) {
|
|
@@ -1128,7 +1260,8 @@ function targetFor(record, message) {
|
|
|
1128
1260
|
role: message.role,
|
|
1129
1261
|
...(callId === undefined ? {} : { callId }),
|
|
1130
1262
|
...(toolName === undefined ? {} : { toolName }),
|
|
1131
|
-
sourceHash: contextGcRecordSourceHash(record)
|
|
1263
|
+
sourceHash: contextGcRecordSourceHash(record),
|
|
1264
|
+
...(message.observationId ? { observationId: message.observationId } : {})
|
|
1132
1265
|
};
|
|
1133
1266
|
}
|
|
1134
1267
|
function modelContextUsage(value) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { open, readdir, realpath, stat } from 'node:fs/promises';
|
|
2
2
|
import { dirname, isAbsolute, join, parse, posix, relative, resolve } from 'node:path';
|
|
3
|
-
import { MarAgentError } from '
|
|
3
|
+
import { MarAgentError } from '../../error.js';
|
|
4
4
|
const MAX_FILE_BYTES = 64 * 1024;
|
|
5
5
|
const MAX_PROJECT_BYTES = 32 * 1024;
|
|
6
6
|
const INSTRUCTION_FILENAMES = ['AGENTS.override.md', 'AGENTS.md'];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ModelMessage } from '
|
|
2
|
-
import { type ContextGcCheckpointCache, type ContextGcCheckpointPayload, type ContextGcReplacement } from '
|
|
3
|
-
import type { SessionRecord } from '
|
|
1
|
+
import type { ModelMessage } from '../../model/contracts.js';
|
|
2
|
+
import { type ContextGcCheckpointCache, type ContextGcCheckpointPayload, type ContextGcReplacement } from '../../session/context-gc-checkpoint.js';
|
|
3
|
+
import type { SessionRecord } from '../../session/jsonl-store.js';
|
|
4
4
|
export interface ContextProjectionEntry {
|
|
5
5
|
readonly record: SessionRecord;
|
|
6
6
|
readonly message: ModelMessage;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { anthropicThinkingBlock } from '
|
|
2
|
-
import {
|
|
3
|
-
import { TOOL_EXECUTION_LIMITS } from '
|
|
4
|
-
import { replaceReadContextItems } from '
|
|
1
|
+
import { anthropicThinkingBlock } from '../../model/anthropic-thinking.js';
|
|
2
|
+
import { contextGcRecordSourceHash, contextGcTargetKey, latestValidContextGcCheckpoint } from '../../session/context-gc-checkpoint.js';
|
|
3
|
+
import { TOOL_EXECUTION_LIMITS } from '../../tools/execution-limits.js';
|
|
4
|
+
import { replaceReadContextItems } from '../../tools/local/read-context.js';
|
|
5
|
+
import { parseCodeCalls, parseObservations, renderObservationResult } from '../../tools/code-mode/observations.js';
|
|
5
6
|
const MAX_MODEL_CONTEXT_BYTES = 1024 * 1024;
|
|
6
7
|
export function buildContextProjection(records, workspace, checkpointCache) {
|
|
7
8
|
let activeModelId;
|
|
@@ -50,6 +51,32 @@ export function applyContextGcReplacementsToMessages(messages, messageSources, r
|
|
|
50
51
|
continue;
|
|
51
52
|
const replacement = target ? direct.get(contextGcTargetKey(target)) : undefined;
|
|
52
53
|
let next = cloneMessage(message);
|
|
54
|
+
if (next.observations && target) {
|
|
55
|
+
const observations = next.observations.map((value) => {
|
|
56
|
+
const itemTarget = { ...target, observationId: value.id };
|
|
57
|
+
const item = direct.get(contextGcTargetKey(itemTarget));
|
|
58
|
+
if (!item || !targetMatchesReplacement(itemTarget, item.target))
|
|
59
|
+
return value;
|
|
60
|
+
if (item.kind === 'READ_OUTPUT_ITEMS' && value.type === 'tool_result')
|
|
61
|
+
return {
|
|
62
|
+
...value,
|
|
63
|
+
result: {
|
|
64
|
+
...value.result,
|
|
65
|
+
content: replaceReadContextItems(value.result.content, item.items)
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
if ('replacementContent' in item)
|
|
69
|
+
return value.type === 'tool_result'
|
|
70
|
+
? { ...value, result: { ...value.result, content: item.replacementContent } }
|
|
71
|
+
: { ...value, value: item.replacementContent };
|
|
72
|
+
return value;
|
|
73
|
+
});
|
|
74
|
+
next = {
|
|
75
|
+
...next,
|
|
76
|
+
observations,
|
|
77
|
+
content: renderObservationResult(observations, next.content)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
53
80
|
if (replacement && target && targetMatchesReplacement(target, replacement.target)) {
|
|
54
81
|
if (replacement.kind === 'READ_OUTPUT_ITEMS' &&
|
|
55
82
|
next.role === 'tool' &&
|
|
@@ -122,6 +149,10 @@ export function restoreModelContext(payload, providerModelId) {
|
|
|
122
149
|
role: 'tool',
|
|
123
150
|
content: payload.content,
|
|
124
151
|
callId: payload.callId,
|
|
152
|
+
...(parseCodeCalls(payload.calls) ? { calls: parseCodeCalls(payload.calls) } : {}),
|
|
153
|
+
...(parseObservations(payload.observations)
|
|
154
|
+
? { observations: parseObservations(payload.observations) }
|
|
155
|
+
: {}),
|
|
125
156
|
...(Array.isArray(payload.toolSearchTools)
|
|
126
157
|
? {
|
|
127
158
|
toolSearchTools: payload.toolSearchTools.filter((tool) => isRecord(tool) &&
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ClientToolDefinition, ModelMessage } from '../../model/contracts.js';
|
|
2
|
+
export declare function estimatedRequestTokens(system: string, messages: readonly ModelMessage[], tools: readonly ClientToolDefinition[], images?: readonly {
|
|
3
|
+
mimeType: string;
|
|
4
|
+
dataBase64: string;
|
|
5
|
+
}[]): number;
|
|
6
|
+
export declare function stableJson(value: unknown): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AGENT_EXECUTION_POLICY } from '../execution-policy.js';
|
|
2
|
+
import { countModelTokens } from '../token-budget.js';
|
|
3
|
+
export function estimatedRequestTokens(system, messages, tools, images) {
|
|
4
|
+
const transientImageCount = messages.reduce((total, message) => total + (message.images?.length ?? 0), images?.length ?? 0);
|
|
5
|
+
const textMessages = messages.map((message) => {
|
|
6
|
+
const textMessage = { ...message };
|
|
7
|
+
delete textMessage.images;
|
|
8
|
+
return textMessage;
|
|
9
|
+
});
|
|
10
|
+
return (countModelTokens(`${system}${stableJson(textMessages)}${stableJson(tools)}`) +
|
|
11
|
+
transientImageCount * AGENT_EXECUTION_POLICY.estimatedTokensPerImage);
|
|
12
|
+
}
|
|
13
|
+
export function stableJson(value) {
|
|
14
|
+
if (Array.isArray(value))
|
|
15
|
+
return `[${value.map(stableJson).join(',')}]`;
|
|
16
|
+
if (value !== null && typeof value === 'object')
|
|
17
|
+
return `{${Object.entries(value)
|
|
18
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
19
|
+
.map(([key, entry]) => `${JSON.stringify(key)}:${stableJson(entry)}`)
|
|
20
|
+
.join(',')}}`;
|
|
21
|
+
return JSON.stringify(value) ?? 'undefined';
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ModelMessage } from '../../model/contracts.js';
|
|
2
|
+
import { type JsonlSessionStore } from '../../session/jsonl-store.js';
|
|
3
|
+
import type { EnvironmentContextState } from './contracts.js';
|
|
4
|
+
import { type ContextProjection } from './projection.js';
|
|
5
|
+
export declare function restoreMessages(store: JsonlSessionStore, sessionId: string, records: readonly import('../../session/jsonl-store.js').SessionRecord[], options: {
|
|
6
|
+
includeImages: boolean;
|
|
7
|
+
currentTurnId?: string;
|
|
8
|
+
projection?: ContextProjection;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
environmentState: EnvironmentContextState | undefined;
|
|
11
|
+
userMessages: string[];
|
|
12
|
+
messages: ModelMessage[];
|
|
13
|
+
ignoredImages: number;
|
|
14
|
+
currentTurnUserIndex: number | undefined;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { MarAgentError } from '../../error.js';
|
|
2
|
+
import { compactSummaryFromCheckpoint, replacementHistoryFromCheckpoint } from '../../session/jsonl-store.js';
|
|
3
|
+
import { compactedHistoryMessages, continuationMessage } from '../compaction/history.js';
|
|
4
|
+
import { environmentContextUpdate, restoreEnvironmentState } from './environment.js';
|
|
5
|
+
import { buildContextProjection } from './projection.js';
|
|
6
|
+
export async function restoreMessages(store, sessionId, records, options) {
|
|
7
|
+
const messages = [];
|
|
8
|
+
const modelProjection = new Map((options.projection ??
|
|
9
|
+
buildContextProjection(records, (await store.getSession(sessionId)).workspace)).entries.map((entry) => [entry.record.recordId, entry.message]));
|
|
10
|
+
let userMessages = [];
|
|
11
|
+
let ignoredImages = 0;
|
|
12
|
+
let currentTurnUserIndex;
|
|
13
|
+
let environmentState;
|
|
14
|
+
for (const record of records) {
|
|
15
|
+
const payload = record.payload;
|
|
16
|
+
const compactSummary = compactSummaryFromCheckpoint(record);
|
|
17
|
+
if (record.type === 'turn.user' && typeof payload.prompt === 'string') {
|
|
18
|
+
userMessages.push(payload.prompt);
|
|
19
|
+
let images = [];
|
|
20
|
+
if (options.includeImages)
|
|
21
|
+
try {
|
|
22
|
+
images = await store.readInputImages(sessionId, payload.images);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (!(error instanceof MarAgentError) || error.code !== 'MAR_AGENT_SESSION_CORRUPT')
|
|
26
|
+
throw error;
|
|
27
|
+
ignoredImages += Array.isArray(payload.images) ? payload.images.length : 1;
|
|
28
|
+
}
|
|
29
|
+
if (record.turnId === options.currentTurnId && currentTurnUserIndex === undefined)
|
|
30
|
+
currentTurnUserIndex = messages.length;
|
|
31
|
+
messages.push({
|
|
32
|
+
role: 'user',
|
|
33
|
+
content: payload.prompt,
|
|
34
|
+
...(images.length ? { images } : {})
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else if ((record.type === 'message.completed' || record.type === 'plan.completed') &&
|
|
38
|
+
typeof payload.text === 'string')
|
|
39
|
+
messages.push({ role: 'assistant', content: payload.text });
|
|
40
|
+
else if (compactSummary !== undefined) {
|
|
41
|
+
environmentState = restoreEnvironmentState({ version: 1, state: payload.environment });
|
|
42
|
+
if (environmentState)
|
|
43
|
+
messages.push(environmentContextUpdate(environmentState));
|
|
44
|
+
const history = replacementHistoryFromCheckpoint(record);
|
|
45
|
+
userMessages =
|
|
46
|
+
history
|
|
47
|
+
?.filter((message) => message.kind === 'user_input')
|
|
48
|
+
.map((message) => message.content) ?? [];
|
|
49
|
+
messages.push(...(history
|
|
50
|
+
? compactedHistoryMessages(history)
|
|
51
|
+
: [{ role: 'user', content: continuationMessage(compactSummary) }]));
|
|
52
|
+
}
|
|
53
|
+
else if (record.type === 'context.environment') {
|
|
54
|
+
const current = restoreEnvironmentState(payload);
|
|
55
|
+
if (current) {
|
|
56
|
+
const message = environmentContextUpdate(current, environmentState);
|
|
57
|
+
if (message)
|
|
58
|
+
messages.push(message);
|
|
59
|
+
environmentState = current;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (record.type === 'model.context') {
|
|
63
|
+
const message = modelProjection.get(record.recordId);
|
|
64
|
+
if (message)
|
|
65
|
+
messages.push(message);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const completedToolCalls = new Set(messages.filter((message) => message.role === 'tool').map((message) => message.callId));
|
|
69
|
+
return {
|
|
70
|
+
environmentState,
|
|
71
|
+
userMessages,
|
|
72
|
+
messages: messages.filter((message) => {
|
|
73
|
+
if (message.role === 'tool_call')
|
|
74
|
+
return completedToolCalls.has(message.callId);
|
|
75
|
+
if (message.role !== 'provider' || message.provider !== 'OPENAI_RESPONSES')
|
|
76
|
+
return true;
|
|
77
|
+
const item = message.item;
|
|
78
|
+
if (item.type !== 'function_call' && item.type !== 'custom_tool_call')
|
|
79
|
+
return true;
|
|
80
|
+
return typeof item.call_id !== 'string' || completedToolCalls.has(item.call_id);
|
|
81
|
+
}),
|
|
82
|
+
ignoredImages,
|
|
83
|
+
currentTurnUserIndex
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type MarAgentReasoningEffort } from '../../model/configuration.js';
|
|
2
|
+
import type { ModelMessage } from '../../model/contracts.js';
|
|
3
|
+
export interface AuthoritativeModelUsage {
|
|
4
|
+
readonly contextInputTokens: number;
|
|
5
|
+
readonly outputTokens: number | undefined;
|
|
6
|
+
readonly historyMessages?: readonly ModelMessage[];
|
|
7
|
+
readonly appendedMessages?: readonly ModelMessage[];
|
|
8
|
+
}
|
|
9
|
+
export declare function latestModelUsage(records: readonly import('../../session/jsonl-store.js').SessionRecord[], selectedModelId: string): AuthoritativeModelUsage | undefined;
|
|
10
|
+
export declare function reasoningEffortBaseline(records: readonly import('../../session/jsonl-store.js').SessionRecord[], modelId: string): MarAgentReasoningEffort | undefined;
|
|
11
|
+
export declare function lastConfigurationEffort(messages: readonly ModelMessage[], modelId: string): MarAgentReasoningEffort | undefined;
|
|
12
|
+
export declare function shouldUseNativeCompaction(input: {
|
|
13
|
+
readonly estimatedRequestTokens: number;
|
|
14
|
+
readonly contextWindowTokens: number;
|
|
15
|
+
readonly latestUsage: AuthoritativeModelUsage | undefined;
|
|
16
|
+
readonly historyMessages: readonly ModelMessage[];
|
|
17
|
+
readonly appendedMessages: readonly ModelMessage[];
|
|
18
|
+
}): boolean;
|