🧊 feat: Persist Context Fading Tier Across Agent Runs (#15505)

* 🧊 feat: Persist Context Fading Tier Across Agent Runs

Carry the pruner's latched context-fading tier from `@librechat/agents`
through `contextMeta` the way `calibrationRatio` already travels, so a
historical tool result keeps the same truncated bytes from one run to the
next and Anthropic's prefix-based prompt cache survives across turns
(danny-avila/agents#497).

- Add `IAgentFadingTier` to the message and conversation `contextMeta`
  types, schemas and the data-provider zod schema
- Add `isAgentFadingTier`, `resolvePersistableFadingTier` and
  `resolveRunContextMeta` in `packages/api`; a tier is persisted once it
  carries information (masking active or a budget below the window), and
  a latched tier is kept even at a neutral calibration ratio
- Seed `createRun({ fadingTier })` from the parent response's contextMeta
  regardless of encoding, since caps are character-based; the field rides
  the existing forward-compatible `runConfig` variable and older SDK
  versions ignore it, as does `Run.getFadingTier` being absent
- Accept a valid `fading` field in event actor context and reject a
  malformed one, matching the calibration checks
- Capture run context meta through one module-level helper for both
  completion paths instead of two inline copies

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Seed Fading Tier Across HITL Resume, Validate at the DB Layer

Address review findings on fading tier persistence:

- Leave the "is this tier worth persisting" decision to the SDK, whose
  `Run.getFadingTier()` now returns only informative tiers; comparing
  against the client's window misclassified untouched conversations
  whenever a reserve ratio was configured
- Carry `contextMeta` through the HITL pause projection (staged approval,
  job metadata, resume state) and seed the rebuilt client from it, so a
  paused turn resumes with the same tier and calibration instead of
  re-deriving a shallower one and rewriting the prefix
- Share run seeding between `chatCompletion` and `resumeCompletion`, and
  make capture tolerate partial runs and bare resume contexts
- Validate `contextMeta.fading` in `commitAgentEventActorState` with a
  shared `isAgentFadingTier` guard in data-schemas, so a malformed tier is
  rejected at the DB layer instead of silently cold-starting the actor
- Reuse `IAgentEventActorContextMeta` for `IMessage.contextMeta` instead of
  extending an inline duplicate

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 test: Cover Resume Seeding From Paused Context Meta

Assert the resume controller seeds the rebuilt client from the context meta
captured at the pause before rebuilding the run, and move the capture
helper's JSDoc back above its function.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧹 style: Sort Imports in Fading Tier Plumbing

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Persist Fading Tier Through Redis Jobs and Branches

Address Codex and Copilot review findings on fading tier persistence:

- Deserialize `contextMeta` in `RedisJobStore`, whose explicit read mapper
  rebuilt every other pause field but this one, so Redis-backed
  deployments resumed with no tier to seed after every HITL pause
- Copy server-private `contextMeta` onto the assistant message created by
  `POST /api/messages/branch`, so the next turn seeds its pruner the same
  way it would from the parallel source response
- Constrain the `fading` subdocuments in both Mongoose schemas (version
  enum, positive budget, required fields) to match the zod schema
- Throw a distinct "fading tier is invalid" error from both context meta
  validators instead of reporting a calibration failure

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Carry Context Meta Through Stop and Re-Pause

Addresses the second Codex pass on the fading-tier persistence:

- A re-pause whose resumed segment has nothing persistable now clears the
  job's `contextMeta` in the same transition instead of leaving the first
  pause's calibration and tier for the next resume to seed from.
- The client publishes the run's live calibration and fading tier onto the
  job after each pre-invoke context snapshot (deduplicated on value), and
  the Stop path copies `jobData.contextMeta` onto the stopped response, so
  a follow-up from a stopped turn keeps the prompt prefix stable.
- Mid-run captures (HITL pause, Stop) read the graph's live state; `Run`
  refreshes its own getters only after the stream settles, so the pause
  used to persist the seeds it started from.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Fence Context Meta Publication Ahead of Stop

A Stop handled by another replica reads the job before the owner is
signaled, so a fire-and-forget publish could lose the race, and a run
stopped before its first context snapshot never published the tier it
inherited. The client now publishes the inherited seed before the run
streams or resumes, and the context-usage handler awaits each
snapshot's publish so the write lands before the model call it
describes begins. Failures still only log; the pre-run publish is
optional-called so bare resume contexts keep working.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Read Back Context Meta After the Abort Claim

The run owner publishes its context meta ahead of each model call and
awaits the write, so a publish landing between the abort's initial job
read and its terminal claim describes the call whose partial output the
abort snapshot carries. abortJob now reads the same-epoch job back after
the claim and content re-read and takes its context meta, so the stopped
response persists the tier that produced its bytes. The refresh is
best-effort and logs on failure like the content refresh beside it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Share In-Flight Context Meta Writes and Clear Neutral Live State

Two snapshot callbacks in a parallel-agent run can capture the same tier
before the first write settles; the publisher now caches the in-flight
promise per serialized value so the second caller awaits the same
durable write instead of treating an uncommitted value as published.
A live snapshot with nothing left to persist after an earlier publish now
writes a neutral record (ratio 1, no tier), since a running job's fields
cannot be deleted through the metadata writer, so a Stop no longer
persists an earlier non-neutral state onto the response. The pre-run
seed publish is unchanged and is told apart from live snapshots by a
flag from the context-usage sink.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 chore: Drop Unused Rest Parameter in Context Meta Test

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 feat: Persist Per-Agent Fading Tiers as Compact Context Meta

Reconciles the host with @librechat/agents at 9aacad1a, where graph
history stays canonical and each Run derives a provider-only projection
from the latched tiers. LibreChat therefore persists only compact state:
the calibration ratio, the default agent's tier, and now the per-agent
tiers from Run.getFadingTiers(), stored as validated entries so agent IDs
never become MongoDB field names and restored onto a null-prototype
record for RunConfig.fadingTiers. Truncated messages, canonical tool
content, projection provenance and per-message truncation state are
never persisted; the SDK's latched flag is stripped on capture.

The per-agent map travels everywhere the single tier already did:
response contextMeta, event-actor checkpoint state, HITL pause and
resume job metadata in both stores, the Stop path, and branch creation.
A shared Mongoose definition keeps the message and conversation schemas
identical, the zod schema validates entries, and commitAgentEventActorState
rejects malformed maps. Comments no longer describe graph messages as
truncated; only the provider projection is.

Tests cover: tool results and inputs reach the persisted content parts
at full size; contextMeta carries exactly the compact tier and
calibration fields; a persisted snapshot round-trips into the seeded
RunConfig unchanged and prototype-safe; per-agent tiers survive pause,
Redis job round-trips, and event-actor context validation.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Restore Event Actor Types and Publish Seeds Before Abortable Setup

The previous commit's doc rewording in the conversation types dropped the
event-actor interfaces that precede the fading tier, which broke the
declaration bundle in CI; they are restored unchanged.

The inherited context meta is now published onto the job as soon as the
parent's state is loaded and awaited at the top of chatCompletion and
resumeCompletion, ahead of run creation and the other abortable setup
stages, so a Stop during setup still persists the parent's tiers onto the
stopped response. The partial response saved when every subscriber
disconnects now carries the job's context meta like the Stop and pause
paths do. The branch route keeps the source's context meta in the saved
message but strips it from the HTTP response, matching client reads.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Serialize Distinct Context Meta Publications

Overlapping snapshots from parallel agents could carry different tiers;
each started its own write and both job stores keep whichever write
finished last, so an older snapshot could overwrite a newer one. Each
distinct publication is now issued after the previous one settles, in
call order, so the newest snapshot is what a Stop reads back. Equal
values still share one write.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Project Root Message Reads and Keep Inherited Meta on Setup Failure

The root GET /api/messages paths returned raw documents: the
single-message query read called getMessages without a projection and the
cursor page performed an unprojected find. Both now apply
CLIENT_MESSAGE_SELECT, with getMessagesByCursor accepting a select option,
so the persisted fading tiers never reach the client through the
paginated endpoint; the search-hydration read projects the same way.

A turn that loads a parent's context meta but fails before its run exists
used to replace the inherited meta with the capture of a missing run, so
the persisted error response lost the last valid tiers. Both finalizers
now keep the inherited meta when no run was created while a created run's
neutral state may still clear it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 refactor: Move Context Meta Publication Into packages/api

The publication coordinator (ordering, deduplication, in-flight sharing,
failure state) now lives in packages/api as createContextMetaPublisher,
with selectRunContextMetaToPublish deciding what a publication carries;
the legacy client keeps only the wiring to the job metadata writer. A
transiently failing write is retried with a short backoff before the
publication is reported and forgotten, so a snapshot's model call no
longer proceeds on the first rejection while the job holds the previous
record. The SDK swallows handler errors, so retrying is the only way to
strengthen the fence without failing the turn.

A terminal save now supplies contextMeta: null when the finished run has
nothing to carry, and saveMessage unsets a previously stored value in
that case, so a disconnect snapshot's record cannot outlive the run that
completed neutrally.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Widen the saveMessage Method Type for the Null Unset

The exported method interface still typed contextMeta as the message
field, so the spec exercising the null unset failed typechecking; it now
mirrors the implementation signature.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Publish Every Agent's Snapshot, Sanitize Search Hits, Unbound Agent IDs

The context-usage handler now awaits the run's context meta publish for
every ON_CONTEXT_USAGE event, hidden sequential agents included, while
the visible-snapshot bookkeeping and the client emission keep their gate;
a Stop before the next visible snapshot finds the tier a hidden agent's
model call latched. Search results are built from the hydrated hit minus
its server-private contextMeta, since Meilisearch hydration projects every
schema field. The per-agent tier entry guard no longer caps the agent ID
length: ephemeral agent IDs encode endpoint, model and sender without a
bound, and dropping such an entry would let that agent alone re-derive
its tier on the next turn. Also formats the publication spec so the
static checks pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Unset Context Meta Atomically, Keep Publication State After a Failed Write

- `saveMessage` folds the `contextMeta` unset into the same update that
  persists the terminal response, on both the plain and the provenance-merge
  paths, so a failure between two writes can no longer leave a completed row
  carrying a disconnect snapshot's state.
- `createContextMetaPublisher` tracks whether any record has committed
  separately from the retryable latest publication, so `hasPublished` stays
  true after a later publication exhausts its retries and a following neutral
  snapshot still overwrites the earlier record on the job.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Carry Context Meta Through the Resumable Stop Route and Neutral Resumed Completions

- The resumable Stop route copies the refreshed `jobData.contextMeta` onto the
  terminal assistant message it persists, as the abort middleware already
  does, so a stopped resumable response keeps the tiers that produced its
  partial output.
- A resumed run that completes with neutral context state now saves
  `contextMeta: null`, which unsets the paused segment's record instead of
  letting an omitted field keep it on the completed row.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Trust Only Server-Authored Context Meta, Unset It on a Neutral Stop, Read It From the Job on Disconnect

- `POST /api/messages/:conversationId` and conversation imports drop any
  client-supplied `contextMeta`, and the run seeds its calibration and fading
  tiers only from a server-authored parent response, so a forged tier can never
  shape a provider projection.
- Both Stop paths save `contextMeta: jobData.contextMeta ?? null`, so a job that
  re-paused with neutral state unsets what an earlier pause stored on the row
  instead of leaving it behind.
- The disconnect partial save reads the same-epoch job record for the run's
  published context meta, since the client-facing resume snapshot never carries
  server-private state.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧪 test: Mock the Job Store in the Resume Metadata Spec

The disconnect partial save now reads the same-epoch job record through
`GenerationJobManager.getJobStore()`, so the resume metadata spec's manager
mock provides it as the other disconnect spec already does.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 🧊 fix: Keep Context Meta Off the POST Message Response

`POST /api/messages/:conversationId` returned the saved row verbatim, so a
client write against an existing server-authored message echoed the row's
stored context meta. The route now projects the row through the same client
projection the branch route uses, which drops the server-private field.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

* 📦 chore: Bump @librechat/agents to 3.7.16 for Persisted Fading Tiers

3.7.16 is the first release that carries danny-avila/agents#497, so
`Run.getFadingTier()` / `getFadingTiers()` now report the latched tier
this branch persists and `RunConfig.fadingTier` / `fadingTiers` seed the
next run from it. The dependency set is unchanged from 3.7.15; only the
package entry moves.

Adds an SDK-backed round trip to the fading spec: a first pruner latches
an informative tier, `resolveRunContextMeta` reduces it to the persisted
shape, and a second pruner seeded from that shape reproduces the first
run's projected tool-exchange bytes under instruction and calibration
drift, while an unseeded pruner under the same drift relaxes its tier and
rewrites them.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WNbgMeR2aZDyhsqAy4L8gF

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Danny Avila 2026-09-02 20:29:43 -04:00 • committed by GitHub
parent 676cd512b0
commit 8a46ca070c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
49 changed files with 2657 additions and 99 deletions

View file

@ -124,6 +124,125 @@ describe('resumable event generation fencing', () => {
);
});
it('keeps a tool result and its input at full size in the content parts LibreChat persists', async () => {
const { GraphEvents, createContentAggregator } = jest.requireActual('@librechat/agents');
const { getDefaultHandlers } = require('../callbacks');
/** Far beyond any cap the SDK's provider-only projection would apply. */
const output = 'r'.repeat(600_000);
const args = { query: 'q'.repeat(120_000) };
const { contentParts, stepMap, aggregateContent } = createContentAggregator();
const handlers = getDefaultHandlers({
res: { write: jest.fn() },
aggregateContent,
contentParts,
stepMap,
toolEndCallback: jest.fn(),
collectedUsage: [],
streamId: 'conversation-1',
jobCreatedAt: 1234,
});
const step = {
id: 'step-full-size',
index: 0,
stepDetails: {
type: 'tool_calls',
tool_calls: [{ id: 'call-full-size', name: 'fetch', args: JSON.stringify(args) }],
},
};
await handlers[GraphEvents.ON_RUN_STEP].handle(GraphEvents.ON_RUN_STEP, step);
await handlers[GraphEvents.ON_RUN_STEP_COMPLETED].handle(GraphEvents.ON_RUN_STEP_COMPLETED, {
result: {
id: step.id,
index: 0,
tool_call: { id: 'call-full-size', name: 'fetch', args, output },
},
});
expect(contentParts).toHaveLength(1);
expect(contentParts[0].tool_call.output).toBe(output);
expect(contentParts[0].tool_call.args).toEqual(args);
expect(JSON.stringify(contentParts[0])).not.toContain('[truncated:');
});
it('publishes a hidden sequential agent snapshot without recording or forwarding it', async () => {
const { GenerationJobManager } = require('@librechat/api');
const { GraphEvents } = jest.requireActual('@librechat/agents');
const { getDefaultHandlers } = require('../callbacks');
const onSnapshot = jest.fn(async () => undefined);
const contextUsageSink = { latest: null, count: 0, onSnapshot };
const handlers = getDefaultHandlers({
res: { write: jest.fn() },
aggregateContent: jest.fn(),
toolEndCallback: jest.fn(),
collectedUsage: [],
streamId: 'conversation-1',
jobCreatedAt: 1234,
contextUsageSink,
usageEmitSink: [],
});
await handlers[GraphEvents.ON_CONTEXT_USAGE].handle(
GraphEvents.ON_CONTEXT_USAGE,
{ contextBudget: 1000, remainingContextTokens: 400 },
{
hide_sequential_outputs: true,
last_agent_id: 'agent-final',
langgraph_node: 'agent-intermediate',
},
);
expect(onSnapshot).toHaveBeenCalledTimes(1);
expect(contextUsageSink).toMatchObject({ latest: null, count: 0 });
expect(GenerationJobManager.emitChunk).not.toHaveBeenCalled();
});
it('records a context snapshot and notifies the sink before forwarding it', async () => {
const { GenerationJobManager } = require('@librechat/api');
const { GraphEvents } = jest.requireActual('@librechat/agents');
const { getDefaultHandlers } = require('../callbacks');
let releaseSnapshot;
const onSnapshot = jest.fn(
() =>
new Promise((resolve) => {
releaseSnapshot = resolve;
}),
);
const contextUsageSink = { latest: null, count: 0, onSnapshot };
const usageEmitSink = [{ input_tokens: 10 }];
const data = { contextBudget: 1000, remainingContextTokens: 400 };
const handlers = getDefaultHandlers({
res: { write: jest.fn() },
aggregateContent: jest.fn(),
toolEndCallback: jest.fn(),
collectedUsage: [],
streamId: 'conversation-1',
jobCreatedAt: 1234,
contextUsageSink,
usageEmitSink,
});
let settled = false;
const handled = handlers[GraphEvents.ON_CONTEXT_USAGE]
.handle(GraphEvents.ON_CONTEXT_USAGE, data, { hide_sequential_outputs: false })
.then(() => {
settled = true;
});
await Promise.resolve();
expect(contextUsageSink).toMatchObject({ latest: data, count: 1, latestUsageIndex: 1 });
expect(onSnapshot).toHaveBeenCalledTimes(1);
expect(settled).toBe(false);
releaseSnapshot();
await handled;
expect(settled).toBe(true);
expect(GenerationJobManager.emitChunk).toHaveBeenCalledWith(
'conversation-1',
{ event: GraphEvents.ON_CONTEXT_USAGE, data },
{ expectedCreatedAt: 1234 },
);
});
it('resolves MCP identity from a function-shaped root tool call', async () => {
const { GraphEvents } = jest.requireActual('@librechat/agents');
const { getDefaultHandlers } = require('../callbacks');

View file

@ -28,6 +28,7 @@ const mockGenerationJobManager = {
beginProviderExecution: jest.fn(),
markProviderExecutionDrained: jest.fn(),
getResumeState: jest.fn(),
getJobStore: jest.fn(),
updateMetadata: jest.fn(),
claimGeneration: jest.fn(),
releaseGeneration: jest.fn(),
@ -142,8 +143,20 @@ describe('ResumableAgentController tenant context', () => {
* Drives the controller far enough to register the `allSubscribersLeft` handler,
* fires it, and returns the tenant context that was active during `saveMessage`.
*/
const firePartialDisconnect = async (user) => {
const partialContextMeta = {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
};
const firePartialDisconnect = async (
user,
jobRecord = { createdAt: 1000, contextMeta: partialContextMeta },
) => {
let allSubscribersLeftHandler;
mockGenerationJobManager.getJobStore.mockReturnValue({
getJob: jest.fn().mockResolvedValue(jobRecord),
});
mockGenerationJobManager.createJob.mockResolvedValue({
createdAt: 1000,
metadata: {
@ -202,6 +215,30 @@ describe('ResumableAgentController tenant context', () => {
return tenantSeenBySave;
};
it('carries the context meta the run published onto the partial response saved on disconnect', async () => {
await firePartialDisconnect({ id: 'user-123', tenantId: 'tenant-a' });
expect(mockSaveMessage).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
messageId: 'response-message',
unfinished: true,
contextMeta: partialContextMeta,
}),
expect.any(Object),
);
});
it('leaves context meta off the partial response when the job record belongs to another epoch', async () => {
await firePartialDisconnect(
{ id: 'user-123', tenantId: 'tenant-a' },
{ createdAt: 2000, contextMeta: partialContextMeta },
);
const [, savedMessage] = mockSaveMessage.mock.calls[0];
expect(savedMessage).not.toHaveProperty('contextMeta');
});
it('restores the authenticated tenant before saving a partial response on disconnect', async () => {
const tenantSeenBySave = await firePartialDisconnect({ id: 'user-123', tenantId: 'tenant-a' });

View file

@ -24,6 +24,7 @@ const mockGenerationJobManager = {
markProviderExecutionDrained: jest.fn(),
failPausePersistence: jest.fn(),
getResumeState: jest.fn(),
getJobStore: jest.fn(),
updateMetadata: jest.fn(),
persistAgentEventDetachedTerminalEvidence: jest.fn(),
claimGeneration: jest.fn(),
@ -444,6 +445,9 @@ describe('ResumableAgentController resume metadata', () => {
emitter: { on: jest.fn() },
});
mockGenerationJobManager.getResumeState.mockResolvedValue(null);
mockGenerationJobManager.getJobStore.mockReturnValue({
getJob: jest.fn().mockResolvedValue(null),
});
mockGenerationJobManager.getJob.mockResolvedValue(undefined);
mockGenerationJobManager.updateMetadata.mockResolvedValue(undefined);
mockGenerationJobManager.isRedis = false;

View file

@ -281,6 +281,7 @@ function makeClient(overrides = {}) {
pendingApproval: false,
buildResponseMetadata: jest.fn(() => null),
resumeCompletion: jest.fn().mockResolvedValue(undefined),
seedContextMeta: jest.fn(),
...overrides,
};
}
@ -2677,6 +2678,26 @@ describe('ResumeAgentController (POST /agents/chat/resume)', () => {
);
});
it('seeds the rebuilt client from the context meta captured at the pause', async () => {
const contextMeta = {
calibrationRatio: 1.25,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
};
mockGenerationJobManager.getJob.mockResolvedValue(
makeToolApprovalJob({ metadata: { contextMeta } }),
);
await post(approveBody());
await settled;
await flush();
const client = await mockInitializeClient.mock.results[0].value.then((r) => r.client);
expect(client.seedContextMeta).toHaveBeenCalledWith(contextMeta);
expect(client.seedContextMeta.mock.invocationCallOrder[0]).toBeLessThan(
client.resumeCompletion.mock.invocationCallOrder[0],
);
});
it('reuses the persisted MCP identity for edited and overridden turns', async () => {
const persistedMCPRequestBody = {
messageId: RESPONSE_MSG_ID,
@ -3206,6 +3227,34 @@ describe('ResumeAgentController (POST /agents/chat/resume)', () => {
);
});
it('unsets the paused row context meta when the resumed run completes neutrally', async () => {
mockGenerationJobManager.getJob.mockResolvedValue(
makeToolApprovalJob({
metadata: {
contextMeta: {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
},
},
}),
);
mockInitializeClient.mockResolvedValue({
client: makeClient({ contextMeta: undefined }),
userMCPAuthMap: {},
});
await post(approveBody());
await settled;
await flush();
expect(mockSaveMessage).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ contextMeta: null }),
expect.objectContaining({ context: expect.stringContaining('resumed response end') }),
);
});
it('carries manualSkills/alwaysAppliedSkills onto the resumed requestMessage', async () => {
const job = makeToolApprovalJob();
job.metadata.userMessage.manualSkills = ['skill-a'];

View file

@ -806,26 +806,31 @@ function getDefaultHandlers({
* @param {GraphRunnableConfig['configurable']} [metadata] The runnable metadata.
*/
handle: async (event, data, metadata) => {
if (
const visible =
checkIfLastAgent(metadata?.last_agent_id, metadata?.langgraph_node) ||
!metadata?.hide_sequential_outputs
) {
/** Capture the latest visible snapshot (last-wins) and how many usage
* events preceded it BEFORE awaiting the emit. `emitEvent` can yield
* (resumable SSE / Redis publish); with parallel runs active this
* call's own primary usage could land in `usageEmitSink` during that
* yield, pushing `latestUsageIndex` past the very event that proves the
* snapshot completed — the save path would then slice it away and drop
* a valid breakdown. The recorded index lets the save path persist only
* when a PRIMARY usage follows this snapshot (the snapshot's call
* actually invoked the model); a summarization detour emits a snapshot
* whose only following usage is tagged `summarization`, which a plain
* snapshot-count would over-count and wrongly drop. */
if (contextUsageSink) {
contextUsageSink.latest = data;
contextUsageSink.count = (contextUsageSink.count ?? 0) + 1;
contextUsageSink.latestUsageIndex = usageEmitSink?.length ?? 0;
}
!metadata?.hide_sequential_outputs;
/** Capture the latest visible snapshot (last-wins) and how many usage
* events preceded it BEFORE awaiting the emit. `emitEvent` can yield
* (resumable SSE / Redis publish); with parallel runs active this
* call's own primary usage could land in `usageEmitSink` during that
* yield, pushing `latestUsageIndex` past the very event that proves the
* snapshot completed — the save path would then slice it away and drop
* a valid breakdown. The recorded index lets the save path persist only
* when a PRIMARY usage follows this snapshot (the snapshot's call
* actually invoked the model); a summarization detour emits a snapshot
* whose only following usage is tagged `summarization`, which a plain
* snapshot-count would over-count and wrongly drop. */
if (visible && contextUsageSink) {
contextUsageSink.latest = data;
contextUsageSink.count = (contextUsageSink.count ?? 0) + 1;
contextUsageSink.latestUsageIndex = usageEmitSink?.length ?? 0;
}
/** Every agent's snapshot publishes the run's context meta, hidden
* sequential agents included: their model calls latch tiers too, and a
* Stop before the next visible snapshot must find them on the job. Awaited
* so the write lands before the model call it describes begins. */
await contextUsageSink?.onSnapshot?.();
if (visible) {
await emitForJob({ event, data });
}
},

View file

@ -142,6 +142,12 @@ const {
createCompactionSemanticIndexProjection,
restoreCompactionSemanticIndexSnapshot,
MAX_AGENT_CONTEXT_SKILLS,
isAgentFadingTier,
isAgentFadingTierEntries,
resolveRunContextMeta,
resolveRunFadingTiers,
createContextMetaPublisher,
selectRunContextMetaToPublish,
} = require('@librechat/api');
const {
Run,
@ -210,7 +216,7 @@ function normalizeEventActorContextMeta(contextMeta) {
if (contextMeta == null) {
return undefined;
}
const { calibrationRatio, encoding } = contextMeta;
const { calibrationRatio, encoding, fading, fadingTiers } = contextMeta;
if (
!Number.isFinite(calibrationRatio) ||
calibrationRatio < 0.5 ||
@ -222,7 +228,64 @@ function normalizeEventActorContextMeta(contextMeta) {
) {
throw new RangeError('Event actor context calibration is invalid');
}
return { calibrationRatio, ...(encoding == null ? {} : { encoding }) };
if (fading != null && !isAgentFadingTier(fading)) {
throw new RangeError('Event actor context fading tier is invalid');
}
if (fadingTiers != null && !isAgentFadingTierEntries(fadingTiers)) {
throw new RangeError('Event actor context fading tiers are invalid');
}
return {
calibrationRatio,
...(encoding == null ? {} : { encoding }),
...(fading == null ? {} : { fading }),
...(fadingTiers == null ? {} : { fadingTiers }),
};
}
/**
* Seeds for a new run from the previous run's contextMeta: the calibration
* ratio when the tokenizer encoding still matches, and the fading tiers, which
* are character-based and so seed regardless of encoding. The default agent's
* tier and the per-agent map are both passed; the SDK restores each agent from
* its own entry and falls back to the default tier for the first agent.
*/
function resolveRunSeeds(client) {
const prevMeta = client.contextMeta;
if (prevMeta == null) {
return {};
}
const currentEncoding = client.getEncoding();
const encodingMatch = prevMeta.encoding === currentEncoding;
const calibrationRatio =
encodingMatch && prevMeta.calibrationRatio > 0 ? prevMeta.calibrationRatio : undefined;
const fadingTier = isAgentFadingTier(prevMeta.fading) ? prevMeta.fading : undefined;
const fadingTiers = resolveRunFadingTiers(prevMeta.fadingTiers);
logger.debug(
`[AgentClient] contextMeta from parent: ratio=${prevMeta.calibrationRatio}, encoding=${prevMeta.encoding}, current=${currentEncoding}, seeded=${calibrationRatio ?? 'none'}, fading=${fadingTier ? `${fadingTier.budgetTokens}/${fadingTier.masked}` : 'none'}, agents=${fadingTiers ? Object.keys(fadingTiers).length : 0}`,
);
return { calibrationRatio, fadingTier, fadingTiers };
}
/**
* Captures the compact context state of a run for persistence on the response
* message: calibration plus the latched fading tiers, never message content.
* Called from `finally`, so values survive an abort. The tier getters are
* optional so SDK versions without them persist calibration alone, and they
* already return only tiers that carry information; the encoding is only
* resolved when there is something to persist.
*/
function captureRunContextMeta(client) {
const run = client.run;
/** `Run` refreshes its own getters only after `processStream` settles, so a
* capture taken mid-run (a HITL pause, a Stop) reads the live graph state. */
const graph = run?.Graph;
const source = graph ?? run;
return resolveRunContextMeta({
calibrationRatio: source?.getCalibrationRatio?.() ?? 0,
fadingTier: source?.getFadingTier?.(),
fadingTiers: source?.getFadingTiers?.(),
getEncoding: () => client.getEncoding(),
});
}
function getLatestEventActorSummary(contentParts) {
@ -356,6 +419,9 @@ class AgentClient extends BaseClient {
* ON_CONTEXT_USAGE handler; persisted on `metadata.contextUsage`.
* @type {{ latest: import('librechat-data-provider').TContextUsageEvent | null } | undefined} */
this.contextUsageSink = contextUsageSink;
if (this.contextUsageSink != null) {
this.contextUsageSink.onSnapshot = () => this.publishRunContextMeta({ live: true });
}
/** Every emitted `on_token_usage` payload for this response (primary,
* summarization, sequential, and subagent); aggregated into the rollup
* persisted on `metadata.usage`.
@ -1866,6 +1932,56 @@ class AgentClient extends BaseClient {
};
}
/**
* Seeds context meta captured at a pause (or from a parent response) into a
* rebuilt client. Malformed values are dropped rather than trusted.
* @param {unknown} contextMeta
*/
/**
* Publishes the run's compact context state onto the job: the inherited seed
* before the run starts, then the live state after each pre-invoke context
* snapshot. A Stop or disconnect persists the response from job data alone, on
* whichever replica handles it, so callers await the publish ahead of the
* model call it describes. Ordering, deduplication, retries and failure
* handling live in the `packages/api` publisher; this is only the wiring.
* @param {{ live?: boolean }} [options] `live` marks a snapshot from the running
* graph; the pre-run call publishes the inherited seed instead.
* @returns {Promise<void>}
*/
publishRunContextMeta({ live = false } = {}) {
const streamId = this.options?.req?._resumableStreamId;
if (!streamId) {
return Promise.resolve();
}
this.contextMetaPublisher ??= createContextMetaPublisher({
write: (contextMeta) =>
GenerationJobManager.updateMetadata(streamId, { contextMeta }, this.jobCreatedAt),
onFailure: (err) =>
logger.warn(
`[AgentClient] Failed to publish context meta for ${streamId}`,
getSafeErrorMetadata(err),
),
});
const contextMeta = selectRunContextMetaToPublish({
live,
captured: captureRunContextMeta(this),
inherited: this.contextMeta,
hasPublished: this.contextMetaPublisher.hasPublished,
getEncoding: () => this.getEncoding(),
});
return contextMeta == null ? Promise.resolve() : this.contextMetaPublisher.publish(contextMeta);
}
seedContextMeta(contextMeta) {
try {
this.contextMeta = normalizeEventActorContextMeta(contextMeta);
} catch (err) {
logger.warn('[AgentClient] Ignoring malformed context meta', getSafeErrorMetadata(err));
this.contextMeta = undefined;
}
void this.publishRunContextMeta?.();
}
async loadHistory(conversationId, parentMessageId = null) {
if (this.eventActorContinuation === 'warm') {
logger.debug('[AgentClient] Skipping durable history for compatible event actor', {
@ -2425,8 +2541,17 @@ class AgentClient extends BaseClient {
* last is the current user message). Seeds the pruner's calibration EMA for this run. */
const parentResponse =
orderedMessages.length >= 2 ? orderedMessages[orderedMessages.length - 2] : undefined;
if (parentResponse?.contextMeta && !parentResponse.isCreatedByUser) {
/** Only a server-authored response may seed the run: a client-submitted
* row carries no trusted calibration or fading state. */
if (
parentResponse?.contextMeta &&
!parentResponse.isCreatedByUser &&
parentResponse.isUserSubmitted !== true
) {
this.contextMeta = parentResponse.contextMeta;
/** Start the seed publish as soon as the parent's state is known: a Stop
* during the rest of setup must already find it on the job. */
void this.publishRunContextMeta?.();
}
const result = {
@ -3515,6 +3640,7 @@ class AgentClient extends BaseClient {
...(staged.compactionSemanticIndex == null
? {}
: { compactionSemanticIndex: staged.compactionSemanticIndex }),
...(staged.contextMeta == null ? {} : { contextMeta: staged.contextMeta }),
persistencePending: true,
...(eventActorSuspension == null
? {}
@ -3760,6 +3886,10 @@ class AgentClient extends BaseClient {
compactionSemanticIndex: createCompactionSemanticIndexProjection(
this.compactionSemanticIndexSnapshot,
),
// Calibration and fading state at the pause, so the resumed segment seeds
// its rebuilt pruner from the same tiers and its provider projection of
// history keeps the same bytes.
contextMeta: captureRunContextMeta({ run, getEncoding: () => this.getEncoding() }),
};
if (this.eventActorInvocationId != null) {
return;
@ -3770,6 +3900,9 @@ class AgentClient extends BaseClient {
}
async chatCompletion({ payload, userMCPAuthMap, abortController = null }) {
/** The inherited state is on the job before any abortable setup begins; a
* publish already started while loading history is simply awaited. */
await this.publishRunContextMeta?.();
/** @type {Partial<GraphRunnableConfig>} */
let config;
/** @type {ReturnType<createRun>} */
@ -4143,18 +4276,7 @@ class AgentClient extends BaseClient {
memoryPromise = this.runMemory(memoryMessages);
}
/** Seed calibration state from previous run if encoding matches */
const currentEncoding = this.getEncoding();
const prevMeta = this.contextMeta;
const encodingMatch = prevMeta?.encoding === currentEncoding;
const calibrationRatio =
encodingMatch && prevMeta?.calibrationRatio > 0 ? prevMeta.calibrationRatio : undefined;
if (prevMeta) {
logger.debug(
`[AgentClient] contextMeta from parent: ratio=${prevMeta.calibrationRatio}, encoding=${prevMeta.encoding}, current=${currentEncoding}, seeded=${calibrationRatio ?? 'none'}`,
);
}
const { calibrationRatio, fadingTier, fadingTiers } = resolveRunSeeds(this);
const streamId = this.options.req?._resumableStreamId;
// HITL: establish an empty checkpoint barrier for THIS immutable generation
@ -4260,6 +4382,8 @@ class AgentClient extends BaseClient {
: { compactionSemanticIndex: continuationCompactionSemanticIndex }),
initialSessions,
calibrationRatio,
fadingTier,
fadingTiers,
runId: this.responseMessageId,
signal: abortController.signal,
/** The phase wrapper stays outermost: it claims and offsets the
@ -4328,6 +4452,8 @@ class AgentClient extends BaseClient {
if (this.activityLabelsMarkedPromise != null) {
await this.activityLabelsMarkedPromise;
}
/** The inherited tier must be on the job before any Stop can read it. */
await this.publishRunContextMeta?.();
try {
const invocationMessages =
this.eventActorContinuation === 'warm' ? messages.slice(-1) : messages;
@ -4479,17 +4605,10 @@ class AgentClient extends BaseClient {
* the failure; retain that model-visible state for actor reconciliation. */
this.eventActorSummary =
getLatestEventActorSummary(this.contentParts) ?? this.eventActorSummary;
/** Capture calibration state from the run for persistence on the response message.
* Runs in finally so values are captured even on abort. */
const ratio = this.run?.getCalibrationRatio() ?? 0;
if (ratio > 0 && ratio !== 1) {
this.contextMeta = {
calibrationRatio: Math.round(ratio * 1000) / 1000,
encoding: this.getEncoding(),
};
} else {
this.contextMeta = undefined;
}
/** A run that never came to exist has no state of its own: keep the
* inherited meta so the persisted error response still seeds the next
* turn. A created run's neutral state may still clear it. */
this.contextMeta = this.run == null ? this.contextMeta : captureRunContextMeta(this);
this.finalizeSubagentContent();
this.stampMcpServerIdentities();
@ -4591,6 +4710,9 @@ class AgentClient extends BaseClient {
activityPhaseSnapshot,
compactionSemanticIndex,
}) {
/** The seeded state is on the job before the run is rebuilt, so a Stop
* during rebuild still persists it onto the stopped response. */
await this.publishRunContextMeta?.();
/** @type {Partial<GraphRunnableConfig>} */
let config;
/** @type {ReturnType<createRun>} */
@ -4791,6 +4913,7 @@ class AgentClient extends BaseClient {
// rebuilt model binding. Undefined/empty for non-deferred turns is a no-op.
discoveredToolNames,
initialSessions,
...resolveRunSeeds(this),
runId: this.responseMessageId,
signal: abortController.signal,
// The rebuilt graph numbers content indices from 0, but the aggregator was
@ -4849,6 +4972,7 @@ class AgentClient extends BaseClient {
if (this.activityLabelsMarkedPromise != null) {
await this.activityLabelsMarkedPromise;
}
await this.publishRunContextMeta?.();
try {
await run.resume(
resumeValue,
@ -4926,15 +5050,10 @@ class AgentClient extends BaseClient {
} finally {
this.eventActorSummary =
getLatestEventActorSummary(this.contentParts) ?? this.eventActorSummary;
const ratio = this.run?.getCalibrationRatio() ?? 0;
if (ratio > 0 && ratio !== 1) {
this.contextMeta = {
calibrationRatio: Math.round(ratio * 1000) / 1000,
encoding: this.getEncoding(),
};
} else {
this.contextMeta = undefined;
}
/** A run that never came to exist has no state of its own: keep the
* inherited meta so the persisted error response still seeds the next
* turn. A created run's neutral state may still clear it. */
this.contextMeta = this.run == null ? this.contextMeta : captureRunContextMeta(this);
this.finalizeSubagentContent();
this.stampMcpServerIdentities();

View file

@ -921,6 +921,570 @@ describe('AgentClient - interrupt discovery persistence', () => {
});
});
it('makes the run context meta durable when the run pauses', async () => {
const streamId = 'conversation-context-meta-pause';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
});
client.conversationId = streamId;
client.responseMessageId = 'response-context-meta-pause';
client.jobCreatedAt = job.createdAt;
const fading = { v: 1, budgetTokens: 50_000, masked: true };
await client.handleRunInterrupt(
{
getInterrupt: () => ({
interruptId: 'ask-interrupt',
threadId: streamId,
payload: {
type: 'ask_user_question',
question: { question: 'Proceed?' },
},
}),
getDiscoveredTools: () => [],
getRunMessages: () => [],
getCalibrationRatio: () => 1.2,
getFadingTier: () => ({ ...fading, latched: true }),
getFadingTiers: () => ({
'agent-123': { ...fading, latched: true },
'agent-worker': { v: 1, budgetTokens: 8_000, masked: false, latched: true },
}),
},
streamId,
);
const paused = await GenerationJobManager.getJob(streamId);
expect(paused?.status).toBe('requires_action');
/** Only the compact tiers travel: the SDK's provenance flag is stripped. */
expect(paused?.metadata.contextMeta).toEqual({
calibrationRatio: 1.2,
encoding: client.getEncoding(),
fading,
fadingTiers: [
{ agentId: 'agent-123', v: 1, budgetTokens: 50_000, masked: true },
{ agentId: 'agent-worker', v: 1, budgetTokens: 8_000, masked: false },
],
});
});
it('captures the live graph state at a pause instead of the run seeds', async () => {
const streamId = 'conversation-context-meta-live-pause';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
});
client.conversationId = streamId;
client.responseMessageId = 'response-context-meta-live-pause';
client.jobCreatedAt = job.createdAt;
const seeded = { v: 1, budgetTokens: 50_000, masked: false };
const live = { v: 1, budgetTokens: 25_000, masked: true };
await client.handleRunInterrupt(
{
getInterrupt: () => ({
interruptId: 'ask-interrupt',
threadId: streamId,
payload: {
type: 'ask_user_question',
question: { question: 'Proceed?' },
},
}),
getDiscoveredTools: () => [],
getRunMessages: () => [],
getCalibrationRatio: () => 1,
getFadingTier: () => seeded,
Graph: {
getCalibrationRatio: () => 1.4,
getFadingTier: () => live,
},
},
streamId,
);
const paused = await GenerationJobManager.getJob(streamId);
expect(paused?.metadata.contextMeta).toEqual({
calibrationRatio: 1.4,
encoding: client.getEncoding(),
fading: live,
});
});
it('publishes the live context meta onto the job after each context snapshot', async () => {
const streamId = 'conversation-context-meta-publish';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const contextUsageSink = { latest: null, count: 0 };
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
contextUsageSink,
});
const seed = { calibrationRatio: 1.05, encoding: client.getEncoding() };
client.contextMeta = seed;
const updateMetadata = jest.spyOn(GenerationJobManager, 'updateMetadata');
/** Before the run exists the inherited seed is what a Stop must carry. */
await client.publishRunContextMeta();
await expect(GenerationJobManager.getJob(streamId)).resolves.toMatchObject({
metadata: { contextMeta: seed },
});
const tier = { v: 1, budgetTokens: 25_000, masked: true };
let ratio = 1.1;
client.run = {
Graph: {
getCalibrationRatio: () => ratio,
getFadingTier: () => tier,
},
};
expect(typeof contextUsageSink.onSnapshot).toBe('function');
await contextUsageSink.onSnapshot();
await contextUsageSink.onSnapshot();
ratio = 1.3;
await contextUsageSink.onSnapshot();
expect(updateMetadata).toHaveBeenCalledTimes(3);
expect(updateMetadata).toHaveBeenLastCalledWith(
streamId,
{
contextMeta: {
calibrationRatio: 1.3,
encoding: client.getEncoding(),
fading: tier,
},
},
job.createdAt,
);
const running = await GenerationJobManager.getJob(streamId);
expect(running?.metadata.contextMeta).toEqual({
calibrationRatio: 1.3,
encoding: client.getEncoding(),
fading: tier,
});
updateMetadata.mockRestore();
});
it('publishes a neutral record when live state stops carrying anything', async () => {
const streamId = 'conversation-context-meta-neutral';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const contextUsageSink = { latest: null, count: 0 };
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
contextUsageSink,
});
let ratio = 1;
let tier;
client.run = {
Graph: {
getCalibrationRatio: () => ratio,
getFadingTier: () => tier,
},
};
const updateMetadata = jest.spyOn(GenerationJobManager, 'updateMetadata');
/** A fresh conversation's first neutral snapshot has nothing to clear. */
await contextUsageSink.onSnapshot();
expect(updateMetadata).not.toHaveBeenCalled();
await expect(GenerationJobManager.getJob(streamId)).resolves.toMatchObject({
metadata: expect.not.objectContaining({ contextMeta: expect.anything() }),
});
ratio = 1.25;
await contextUsageSink.onSnapshot();
await expect(GenerationJobManager.getJob(streamId)).resolves.toMatchObject({
metadata: { contextMeta: { calibrationRatio: 1.25, encoding: client.getEncoding() } },
});
ratio = 1;
await contextUsageSink.onSnapshot();
await contextUsageSink.onSnapshot();
expect(updateMetadata).toHaveBeenCalledTimes(2);
await expect(GenerationJobManager.getJob(streamId)).resolves.toMatchObject({
metadata: { contextMeta: { calibrationRatio: 1, encoding: client.getEncoding() } },
});
updateMetadata.mockRestore();
});
it('shares one in-flight write between equal concurrent snapshots', async () => {
const streamId = 'conversation-context-meta-inflight';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const contextUsageSink = { latest: null, count: 0 };
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
contextUsageSink,
});
const tier = { v: 1, budgetTokens: 25_000, masked: true };
client.run = {
Graph: {
getCalibrationRatio: () => 1.2,
getFadingTier: () => tier,
},
};
let settled = false;
let releaseWrite;
const updateMetadata = jest
.spyOn(GenerationJobManager, 'updateMetadata')
.mockImplementationOnce(async () => {
await new Promise((resolve) => {
releaseWrite = resolve;
});
settled = true;
});
const first = contextUsageSink.onSnapshot();
const second = contextUsageSink.onSnapshot();
let secondSettled = false;
void second.then(() => {
secondSettled = true;
});
await Promise.resolve();
expect(secondSettled).toBe(false);
expect(updateMetadata).toHaveBeenCalledTimes(1);
releaseWrite();
await Promise.all([first, second]);
expect(settled).toBe(true);
expect(secondSettled).toBe(true);
updateMetadata.mockRestore();
});
it('serializes distinct concurrent snapshots so the newest value wins', async () => {
const streamId = 'conversation-context-meta-ordered';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const contextUsageSink = { latest: null, count: 0 };
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
contextUsageSink,
});
let tier = { v: 1, budgetTokens: 50_000, masked: false };
client.run = {
Graph: {
getCalibrationRatio: () => 1.2,
getFadingTier: () => tier,
},
};
let releaseFirstWrite;
const originalUpdateMetadata = Object.getPrototypeOf(GenerationJobManager).updateMetadata;
const updateMetadata = jest.spyOn(GenerationJobManager, 'updateMetadata');
updateMetadata.mockImplementationOnce(async (...args) => {
await new Promise((resolve) => {
releaseFirstWrite = resolve;
});
return originalUpdateMetadata.apply(GenerationJobManager, args);
});
const older = contextUsageSink.onSnapshot();
tier = { v: 1, budgetTokens: 25_000, masked: true };
const newer = contextUsageSink.onSnapshot();
await Promise.resolve();
/** The newer write waits for the older one instead of racing it. */
expect(updateMetadata).toHaveBeenCalledTimes(1);
releaseFirstWrite();
await Promise.all([older, newer]);
expect(updateMetadata).toHaveBeenCalledTimes(2);
await expect(GenerationJobManager.getJob(streamId)).resolves.toMatchObject({
metadata: {
contextMeta: {
calibrationRatio: 1.2,
encoding: client.getEncoding(),
fading: { v: 1, budgetTokens: 25_000, masked: true },
},
},
});
updateMetadata.mockRestore();
});
it('publishes the inherited context meta before the resumed run continues', async () => {
const streamId = 'conversation-context-meta-resume-seed';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
});
const seed = {
calibrationRatio: 1.15,
encoding: client.getEncoding(),
fading: { v: 1, budgetTokens: 30_000, masked: true },
};
client.seedContextMeta(seed);
let metaWhenResumed;
let metaWhenCreated;
const resume = jest.fn(async () => {
metaWhenResumed = (await GenerationJobManager.getJob(streamId))?.metadata.contextMeta;
});
mockCreateRun.mockImplementationOnce(async () => {
metaWhenCreated = (await GenerationJobManager.getJob(streamId))?.metadata.contextMeta;
return {
Graph: null,
resume,
processStream: jest.fn().mockResolvedValue(),
getCalibrationRatio: jest.fn(() => 0),
getInterrupt: jest.fn(() => undefined),
};
});
client.conversationId = streamId;
client.responseMessageId = 'response-context-meta-resume-seed';
client.recordCollectedUsage = jest.fn().mockResolvedValue();
await client.resumeCompletion({
resumeValue: { decisions: [] },
streamId,
checkpointNamespace: 'resume-seed',
});
expect(resume).toHaveBeenCalledTimes(1);
expect(metaWhenCreated).toEqual(seed);
expect(metaWhenResumed).toEqual(seed);
});
it('publishes the inherited context meta before a fresh run streams', async () => {
const streamId = 'conversation-context-meta-stream-seed';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
});
const seed = {
calibrationRatio: 1.15,
encoding: client.getEncoding(),
fading: { v: 1, budgetTokens: 30_000, masked: true },
};
client.contextMeta = seed;
let metaWhenStreamed;
let metaWhenCreated;
const processStream = jest.fn(async () => {
metaWhenStreamed = (await GenerationJobManager.getJob(streamId))?.metadata.contextMeta;
});
/** Run creation is the first abortable setup stage; the seed must precede it. */
mockCreateRun.mockImplementationOnce(async () => {
metaWhenCreated = (await GenerationJobManager.getJob(streamId))?.metadata.contextMeta;
return {
Graph: null,
processStream,
getCalibrationRatio: jest.fn(() => 0),
getInterrupt: jest.fn(() => undefined),
};
});
client.conversationId = streamId;
client.responseMessageId = 'response-context-meta-stream-seed';
client.parentMessageId = 'parent-context-meta-stream-seed';
client.recordCollectedUsage = jest.fn().mockResolvedValue();
await client.chatCompletion({ payload: [] });
expect(processStream).toHaveBeenCalledTimes(1);
expect(metaWhenCreated).toEqual(seed);
expect(metaWhenStreamed).toEqual(seed);
});
it('keeps the inherited context meta when a fresh run fails before it exists', async () => {
const streamId = 'conversation-context-meta-setup-failure';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
});
const seed = {
calibrationRatio: 1.15,
encoding: client.getEncoding(),
fading: { v: 1, budgetTokens: 30_000, masked: true },
};
client.contextMeta = seed;
mockCreateRun.mockRejectedValueOnce(new Error('run creation failed'));
client.conversationId = streamId;
client.responseMessageId = 'response-context-meta-setup-failure';
client.parentMessageId = 'parent-context-meta-setup-failure';
client.recordCollectedUsage = jest.fn().mockResolvedValue();
await client.chatCompletion({ payload: [] }).catch(() => undefined);
expect(client.run).toBeUndefined();
expect(client.contextMeta).toEqual(seed);
});
it('keeps the inherited context meta when a resumed run fails to rebuild', async () => {
const streamId = 'conversation-context-meta-resume-failure';
const job = await GenerationJobManager.createJob(streamId, 'user-123', streamId);
const client = new AgentClient({
req: {
user: { id: 'user-123' },
body: { endpoint: EModelEndpoint.agents, agent_id: 'agent-123' },
config: { endpoints: { [EModelEndpoint.agents]: {} } },
_resumableStreamId: streamId,
},
res: {},
agent: {
id: 'agent-123',
endpoint: EModelEndpoint.openAI,
provider: EModelEndpoint.openAI,
model_parameters: { model: 'gpt-4' },
},
contentParts: [],
collectedUsage: [],
artifactPromises: [],
jobCreatedAt: job.createdAt,
});
const seed = {
calibrationRatio: 1.15,
encoding: client.getEncoding(),
fading: { v: 1, budgetTokens: 30_000, masked: true },
};
client.seedContextMeta(seed);
mockCreateRun.mockRejectedValueOnce(new Error('rebuild failed'));
client.conversationId = streamId;
client.responseMessageId = 'response-context-meta-resume-failure';
client.recordCollectedUsage = jest.fn().mockResolvedValue();
await client
.resumeCompletion({
resumeValue: { decisions: [] },
streamId,
checkpointNamespace: 'resume-failure',
})
.catch(() => undefined);
expect(client.run).toBeUndefined();
expect(client.contextMeta).toEqual(seed);
});
it('caps an event-bound pause at the inherited binding deadline', async () => {
const now = Date.now();
const streamId = 'conversation-event-bound-pause';
@ -2236,7 +2800,15 @@ describe('AgentClient - startup telemetry', () => {
client.eventActorContinuation = 'warm';
client.eventActorDiscoveredToolNames = ['deferred_tool'];
client.eventActorSummary = { text: 'summary of earlier turns', tokenCount: 40 };
client.contextMeta = { calibrationRatio: 1.25, encoding: client.getEncoding() };
client.contextMeta = {
calibrationRatio: 1.25,
encoding: client.getEncoding(),
fading: { v: 1, budgetTokens: 20_000, masked: true },
fadingTiers: [
{ agentId: 'agent-123', v: 1, budgetTokens: 20_000, masked: true },
{ agentId: 'agent-worker', v: 1, budgetTokens: 8_000, masked: false },
],
};
client.compactionSemanticIndexSnapshot = baseCompactionSemanticIndexSnapshot;
client.recordCollectedUsage = jest.fn().mockResolvedValue();
@ -2255,9 +2827,17 @@ describe('AgentClient - startup telemetry', () => {
indexTokenCountMap: {},
initialSummary: { text: 'summary of earlier turns', tokenCount: 40 },
calibrationRatio: 1.25,
fadingTier: { v: 1, budgetTokens: 20_000, masked: true },
fadingTiers: {
'agent-123': { v: 1, budgetTokens: 20_000, masked: true },
'agent-worker': { v: 1, budgetTokens: 8_000, masked: false },
},
compactionSemanticIndex: evolvedCompactionSemanticIndexSnapshot.entries,
}),
);
/** The seeded map must be prototype-safe: a null-prototype record built from own keys. */
const seededTiers = mockCreateRun.mock.calls.at(-1)[0].fadingTiers;
expect(Object.getPrototypeOf(seededTiers)).toBeNull();
expect(mockFormatAgentMessages.mock.calls[0][4]).toEqual(
expect.objectContaining({
compactionSemanticIndex: expect.objectContaining({
@ -4516,6 +5096,43 @@ describe('AgentClient - titleConvo', () => {
).resolves.toEqual(expect.objectContaining({ prompt: expect.any(Array) }));
});
it.each([
['seeds the run from a server-authored parent response', undefined, true],
['ignores context meta on a client-submitted parent response', true, false],
])('%s', async (_label, isUserSubmitted, expectSeed) => {
const contextMeta = {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
};
const parentResponse = {
messageId: 'assistant-seed',
parentMessageId: null,
sender: 'Assistant',
role: 'assistant',
isCreatedByUser: false,
...(isUserSubmitted != null && { isUserSubmitted }),
text: 'Prior response',
contextMeta,
};
const userMessage = {
messageId: 'user-next',
parentMessageId: 'assistant-seed',
sender: 'User',
role: 'user',
isCreatedByUser: true,
text: 'Next question',
};
client.setModelBoundStoredMessages([parentResponse, userMessage]);
client.contextMeta = undefined;
client.publishRunContextMeta = jest.fn().mockResolvedValue(undefined);
await client.buildMessages([parentResponse, userMessage], 'user-next', {}, {});
expect(client.contextMeta).toEqual(expectSeed ? contextMeta : undefined);
expect(client.publishRunContextMeta).toHaveBeenCalledTimes(expectSeed ? 1 : 0);
});
it('preserves persisted source identity through both agent formatting passes', async () => {
mockReq.config.filters = {
messages: {
@ -7656,3 +8273,88 @@ describe('AgentClient - resumeCompletion content protection', () => {
});
});
});
describe('fading tier context meta', () => {
const fading = { v: 1, budgetTokens: 20_000, masked: true };
const fadingTiers = [
{ agentId: 'agent-123', v: 1, budgetTokens: 20_000, masked: true },
{ agentId: 'agent-worker', v: 1, budgetTokens: 8_000, masked: false },
];
it('carries valid fading tiers through event actor context', async () => {
const client = Object.create(AgentClient.prototype);
client.options = { req: { config: {} } };
const contextMeta = { calibrationRatio: 1.25, encoding: 'o200k_base', fading, fadingTiers };
client.getEventActorContext = jest.fn().mockResolvedValue({
fingerprint: 'fingerprint-1',
skillManifest: [],
discoveredToolNames: [],
contextMeta,
});
await expect(
client.prepareEventActorContext({
contextFingerprint: 'fingerprint-1',
skillManifest: [],
discoveredToolNames: [],
contextMeta,
}),
).resolves.toMatchObject({ contextMeta });
expect(client.contextMeta).toEqual(contextMeta);
});
it('rejects event actor context carrying a malformed fading tier', async () => {
const client = Object.create(AgentClient.prototype);
client.options = { req: { config: {} } };
client.getEventActorContext = jest.fn();
await expect(
client.prepareEventActorContext({
contextFingerprint: 'fingerprint-1',
skillManifest: [],
discoveredToolNames: [],
contextMeta: { calibrationRatio: 1.25, fading: { v: 1, budgetTokens: -5, masked: true } },
}),
).resolves.toBeUndefined();
expect(client.getEventActorContext).not.toHaveBeenCalled();
});
it('rejects event actor context carrying malformed per-agent tiers', async () => {
const client = Object.create(AgentClient.prototype);
client.options = { req: { config: {} } };
client.getEventActorContext = jest.fn();
await expect(
client.prepareEventActorContext({
contextFingerprint: 'fingerprint-1',
skillManifest: [],
discoveredToolNames: [],
contextMeta: {
calibrationRatio: 1.25,
fadingTiers: [{ agentId: '', v: 1, budgetTokens: 20_000, masked: true }],
},
}),
).resolves.toBeUndefined();
expect(client.getEventActorContext).not.toHaveBeenCalled();
});
});
describe('seedContextMeta', () => {
it('seeds valid context meta and drops a malformed fading tier', () => {
const client = Object.create(AgentClient.prototype);
const contextMeta = {
calibrationRatio: 1.25,
encoding: 'claude',
fading: { v: 1, budgetTokens: 20_000, masked: true },
};
client.seedContextMeta(contextMeta);
expect(client.contextMeta).toEqual(contextMeta);
client.seedContextMeta({ calibrationRatio: 1.25, fading: { v: 1, budgetTokens: 0 } });
expect(client.contextMeta).toBeUndefined();
client.seedContextMeta(undefined);
expect(client.contextMeta).toBeUndefined();
});
});

View file

@ -1679,7 +1679,10 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
return;
}
const resumeState = await GenerationJobManager.getResumeState(streamId, jobCreatedAt);
const [resumeState, jobRecord] = await Promise.all([
GenerationJobManager.getResumeState(streamId, jobCreatedAt),
GenerationJobManager.getJobStore().getJob(streamId),
]);
if (!resumeState?.userMessage) {
logger.debug('[ResumableAgentController] No user message to save partial response for');
return;
@ -1687,6 +1690,13 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
partialResponseSaved = true;
const responseConversationId = resumeState.conversationId || conversationId;
/** The run publishes its calibration and fading tiers onto the job; a
* partial response saved on disconnect must carry them like the Stop and
* pause paths do, or a turn continued from it re-derives its provider
* projection of history and loses the cached prefix. The same-epoch job
* record is the source, since the client-facing resume snapshot never
* carries server-private state. */
const contextMeta = jobRecord?.createdAt === jobCreatedAt ? jobRecord.contextMeta : undefined;
try {
const partialMessage = {
@ -1702,6 +1712,7 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
endpoint: endpointOption.endpoint,
iconURL: resumeState.iconURL || endpointIconURL,
model: resumeState.model || responseModel,
...(contextMeta != null && { contextMeta }),
};
if (req.body?.agent_id) {
@ -2722,6 +2733,8 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
reqCtx,
{
...response,
/** A neutral finish unsets what a disconnect snapshot may have stored. */
contextMeta: response.contextMeta ?? null,
user: userId,
unfinished: responseIsUnfinished,
/** Distinguishes "ran out of steps" from a user stop, so the client can

View file

@ -500,11 +500,12 @@ async function finalizeResumedTurn({
if (Object.keys(responseMetadata).length > 0) {
responseMessage.metadata = responseMetadata;
}
// Carry the resumed run's context-window calibration (BaseClient.sendMessage persists
// this on the response). Without it, the NEXT turn can't seed its pruner from this
// run and falls back to uncalibrated token accounting.
if (client?.contextMeta != null) {
responseMessage.contextMeta = client.contextMeta;
// Carry the resumed run's compact context meta (calibration and fading tiers), as
// BaseClient.sendMessage persists it on the response. Without it, the NEXT turn can't
// seed its pruner from this run. A neutral finish unsets what the paused segment
// stored on this row, since an omitted field would otherwise survive the save.
if (client != null) {
responseMessage.contextMeta = client.contextMeta ?? null;
}
// Win terminal ownership BEFORE the outcome-defining response write. Stop
@ -1797,6 +1798,9 @@ const ResumeAgentController = async (req, res, next, initializeClient, addTitle)
client.checkpointNamespace = checkpointNamespace;
client.responseMessageId = job.metadata.responseMessageId;
client.parentMessageId = job.metadata.userMessage?.messageId ?? Constants.NO_PARENT;
// Seed the rebuilt pruner from the tier and calibration captured at the pause, so the
// resumed segment keeps historical tool results byte-identical to the paused one.
client.seedContextMeta?.(job.metadata?.contextMeta);
if (client.contentParts) {
GenerationJobManager.setContentParts(streamId, client.contentParts, job.createdAt);
}

View file

@ -114,6 +114,11 @@ async function abortMessage(req, res) {
error: false,
isCreatedByUser: false,
tokenCount: completionTokens,
/** The run publishes its calibration and fading tiers onto the job as it
* goes; a stopped response must carry them or the next turn re-derives its
* provider projection of history from scratch and loses the cached prefix.
* A job with none unsets what an earlier pause stored on this row. */
...(jobData != null && { contextMeta: jobData.contextMeta ?? null }),
};
/** Persist the usage/cost rollup + context breakdown for the stopped response

View file

@ -403,6 +403,47 @@ describe('abortMiddleware - transactions config', () => {
);
});
it('carries the context meta the run published onto the job into the stopped response', async () => {
const contextMeta = {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
};
GenerationJobManager.abortJob.mockResolvedValue({
success: true,
jobData: { ...buildJobData(), contextMeta },
content: [],
text: 'partial',
collectedUsage: [],
});
const res = buildRes();
await handleAbort()(buildReq(), res);
expect(db.saveMessage).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({ messageId: 'msg-123', contextMeta }),
expect.any(Object),
);
const finalEvent = JSON.parse(res.send.mock.calls[0][0]);
expect(finalEvent.responseMessage.contextMeta).toEqual(contextMeta);
});
it('unsets context meta on a stopped response when the job carries none', async () => {
GenerationJobManager.abortJob.mockResolvedValue({
success: true,
jobData: buildJobData(),
content: [],
text: 'partial',
collectedUsage: [],
});
await handleAbort()(buildReq(), buildRes());
const [, savedMessage] = db.saveMessage.mock.calls[0];
expect(savedMessage.contextMeta).toBeNull();
});
it('resolves the config from req and forwards it on the token-count fallback path', async () => {
GenerationJobManager.abortJob.mockResolvedValue({
success: true,

View file

@ -318,6 +318,47 @@ describe('message route conversation ownership filters', () => {
]);
});
it.each([
{
name: 'carries server-private context meta onto the branch',
contextMeta: {
calibrationRatio: 1.25,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
},
},
{ name: 'leaves context meta absent when the source has none', contextMeta: undefined },
])('$name', async ({ contextMeta }) => {
getMessage.mockResolvedValue({
messageId: 'source-message',
conversationId: 'convo-1',
parentMessageId: 'parent-1',
isCreatedByUser: false,
...(contextMeta == null ? {} : { contextMeta }),
content: [
{ type: 'text', text: 'Different agent content', agentId: 'agent-2' },
{ type: 'text', text: 'Assistant content', agentId: 'agent-1' },
],
});
saveMessage.mockImplementation(async (_ctx, message) => message);
const response = await request(app).post('/api/messages/branch').send({
messageId: 'source-message',
agentId: 'agent-1',
});
expect(response.status).toBe(201);
const savedMessage = saveMessage.mock.calls[0][1];
if (contextMeta == null) {
expect(savedMessage).not.toHaveProperty('contextMeta');
} else {
expect(savedMessage.contextMeta).toEqual(contextMeta);
}
/** Server-private state stays in the database; normal reads strip it, so must this response. */
expect(response.body).not.toHaveProperty('contextMeta');
expect(response.body.messageId).toBe(savedMessage.messageId);
});
it('does not hydrate branch files for an inert file policy', async () => {
getMessage.mockResolvedValue({
messageId: 'source-message',
@ -401,6 +442,12 @@ describe('message route conversation ownership filters', () => {
isTemporary: false,
files: [{ file_id: 'file-1' }],
user: authenticatedUserId,
/** An existing server-authored row keeps its stored state through the update. */
contextMeta: {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
},
};
saveMessage.mockResolvedValue(savedMessage);
@ -420,6 +467,11 @@ describe('message route conversation ownership filters', () => {
userSubmittedMessageFieldPaths: [
{ path: '/forged/model/output', field: 'decision_reason' },
],
contextMeta: {
calibrationRatio: 1,
encoding: 'claude',
fading: { v: 1, budgetTokens: 1, masked: true },
},
});
expect(response.status).toBe(201);
@ -437,6 +489,9 @@ describe('message route conversation ownership filters', () => {
expect(saveMessage.mock.calls[0][1].conversationId).not.toBe(bodyConversationId);
expect(saveMessage.mock.calls[0][1]).not.toHaveProperty('userSubmittedPaths');
expect(saveMessage.mock.calls[0][1]).not.toHaveProperty('userSubmittedMessageFieldPaths');
expect(saveMessage.mock.calls[0][1]).not.toHaveProperty('contextMeta');
expect(response.body.messageId).toBe(savedMessage.messageId);
expect(response.body).not.toHaveProperty('contextMeta');
expect(saveConvo).toHaveBeenCalledWith(
expect.objectContaining({ userId: authenticatedUserId }),
{
@ -513,6 +568,36 @@ describe('message route conversation ownership filters', () => {
},
);
it('strips server-private context meta from hydrated search hits', async () => {
searchMessages.mockResolvedValue({
hits: [
{
messageId: 'hit-1',
conversationId: 'convo-1',
text: 'needle in a haystack',
contextMeta: {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
},
},
],
});
getConvosQueried.mockResolvedValue({
convoMap: { 'convo-1': { title: 'Found', model: 'gpt-4' } },
});
getMessages.mockResolvedValue([
{ messageId: 'hit-1', isCreatedByUser: false, endpoint: 'agents', iconURL: null },
]);
const response = await request(app).get('/api/messages?search=needle');
expect(response.status).toBe(200);
expect(response.body.messages).toHaveLength(1);
expect(response.body.messages[0]).toMatchObject({ messageId: 'hit-1', title: 'Found' });
expect(response.body.messages[0]).not.toHaveProperty('contextMeta');
});
it('returns indistinguishable not-found responses for child and missing query reads', async () => {
getConvoOwnership.mockResolvedValueOnce({
user: authenticatedUserId,
@ -616,6 +701,27 @@ describe('message route conversation ownership filters', () => {
expect.objectContaining({ user: { id: authenticatedUserId } }),
'active-convo',
);
/** The paginated read projects like every other client read, so persisted
* `contextMeta` never reaches the client through this endpoint. */
expect(getMessagesByCursor).toHaveBeenCalledWith(
{ conversationId: 'active-convo', user: authenticatedUserId },
expect.objectContaining({ select: CLIENT_MESSAGE_SELECT }),
);
});
it('projects a single-message query read like every other client read', async () => {
getConvoOwnership.mockResolvedValue({ conversationId: 'convo-1' });
getMessages.mockResolvedValue([{ messageId: 'message-1', conversationId: 'convo-1' }]);
const response = await request(app).get(
'/api/messages?conversationId=convo-1&messageId=message-1',
);
expect(response.status).toBe(200);
expect(getMessages).toHaveBeenCalledWith(
{ conversationId: 'convo-1', messageId: 'message-1', user: authenticatedUserId },
CLIENT_MESSAGE_SELECT,
);
});
it('should start conversation message reads before validation resolves', async () => {

View file

@ -424,6 +424,75 @@ describe('Agent Abort Endpoint', () => {
);
});
it('carries the context meta the run published onto the stopped response', async () => {
const contextMeta = {
calibrationRatio: 1.2,
encoding: 'claude',
fading: { v: 1, budgetTokens: 50_000, masked: true },
fadingTiers: [{ agentId: 'agent-a', v: 1, budgetTokens: 50_000, masked: true }],
};
mockGenerationJobManager.getJob.mockResolvedValue({
metadata: { userId: 'test-user-123' },
});
const abortResult = {
success: true,
jobData: {
userMessage: { messageId: 'user-msg-123' },
responseMessageId: 'response-msg-456',
conversationId: 'test-stream-123',
contextMeta,
},
content: [{ type: 'text', text: 'Partial response...' }],
text: 'Partial response...',
};
mockGenerationJobManager.abortJob.mockImplementation(async (_streamId, options) => {
await options.beforePublish(abortResult);
return abortResult;
});
const response = await request(app)
.post('/api/agents/chat/abort')
.send({ conversationId: 'test-stream-123' });
expect(response.status).toBe(200);
expect(mockSaveMessage).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ messageId: 'response-msg-456', contextMeta }),
expect.objectContaining({
context: 'api/server/routes/agents/index.js - abort endpoint',
}),
);
});
it('unsets context meta on the stopped response when the job has none', async () => {
mockGenerationJobManager.getJob.mockResolvedValue({
metadata: { userId: 'test-user-123' },
});
const abortResult = {
success: true,
jobData: {
userMessage: { messageId: 'user-msg-123' },
responseMessageId: 'response-msg-456',
conversationId: 'test-stream-123',
},
content: [{ type: 'text', text: 'Partial response...' }],
text: 'Partial response...',
};
mockGenerationJobManager.abortJob.mockImplementation(async (_streamId, options) => {
await options.beforePublish(abortResult);
return abortResult;
});
await request(app)
.post('/api/agents/chat/abort')
.send({ conversationId: 'test-stream-123' });
const [, savedResponse] = mockSaveMessage.mock.calls.find(
([, message]) => message.messageId === 'response-msg-456',
);
expect(savedResponse.contextMeta).toBeNull();
});
it('saves the aborted partial as temporary from job metadata, not the request body', async () => {
const jobStreamId = 'test-stream-123';

View file

@ -794,6 +794,11 @@ router.post('/chat/abort', configMiddleware, async (req, res, next) => {
jobData.userSubmittedMessageFieldPaths.length > 0 && {
userSubmittedMessageFieldPaths: jobData.userSubmittedMessageFieldPaths,
}),
/** The run published its compact context meta onto the job ahead
* of each model call; the stopped response must carry it so the
* next turn seeds the same tiers. A job with none unsets what an
* earlier pause stored on this row, since omission would keep it. */
contextMeta: jobData.contextMeta ?? null,
user: userId,
};

View file

@ -101,11 +101,12 @@ router.get('/', async (req, res) => {
let scopedMessageRead;
if (typeof conversationId === 'string') {
const ownershipRead = db.getConvoOwnership(user, conversationId);
/** Client-facing reads never expose server-private fields such as `contextMeta`. */
const messageRead = messageId
? db.getMessages({ conversationId, messageId, user })
? db.getMessages({ conversationId, messageId, user }, CLIENT_MESSAGE_SELECT)
: db.getMessagesByCursor(
{ conversationId, user },
{ sortField, sortOrder, limit: pageSize, cursor },
{ sortField, sortOrder, limit: pageSize, cursor, select: CLIENT_MESSAGE_SELECT },
);
scopedMessageRead = Promise.resolve(messageRead).then(
(value) => ({ ok: true, value }),
@ -161,10 +162,13 @@ router.get('/', async (req, res) => {
}
}
const dbMessages = await db.getMessages({
user,
messageId: { $in: messageIds },
});
const dbMessages = await db.getMessages(
{
user,
messageId: { $in: messageIds },
},
CLIENT_MESSAGE_SELECT,
);
const dbMessageMap = {};
for (const dbMessage of dbMessages) {
@ -175,9 +179,12 @@ router.get('/', async (req, res) => {
for (const message of cleanedMessages) {
const convo = result.convoMap[message.conversationId];
const dbMessage = dbMessageMap[message.messageId];
/** Search hydrates every schema field; server-private state never leaves. */
const publicHit = { ...message };
delete publicHit.contextMeta;
activeMessages.push({
...message,
...publicHit,
title: convo.title,
conversationId: message.conversationId,
model: convo.model,
@ -209,6 +216,18 @@ router.get('/', async (req, res) => {
* @param {string} req.body.agentId - The agentId to filter content by
* @returns {TMessage} The newly created branch message
*/
/**
* Projects a saved row for a client response. The context meta stays in the
* database for the next turn; no client-facing read or write response carries it.
* @param {TMessage} message
* @returns {TMessage}
*/
function toClientMessage(message) {
const clientMessage = { ...message };
delete clientMessage.contextMeta;
return clientMessage;
}
router.post('/branch', configMiddleware, async (req, res) => {
try {
const { messageId, agentId } = req.body;
@ -298,6 +317,9 @@ router.post('/branch', configMiddleware, async (req, res) => {
endpoint: sourceMessage.endpoint,
sender: sourceMessage.sender,
iconURL: sourceMessage.iconURL,
// Server-private context meta (calibration and fading tier) travels with the branch so
// the next turn seeds its pruner the same way it would from the source response.
...(sourceMessage.contextMeta != null && { contextMeta: sourceMessage.contextMeta }),
...(typeof sourceMessage.isUserSubmitted === 'boolean' && {
isUserSubmitted: sourceMessage.isUserSubmitted,
}),
@ -339,7 +361,7 @@ router.post('/branch', configMiddleware, async (req, res) => {
return res.status(500).json({ error: 'Failed to save branch message' });
}
res.status(201).json(savedMessage);
res.status(201).json(toClientMessage(savedMessage));
} catch (error) {
if (isContentFilterError(error)) {
return res.status(error.statusCode).json(error.body);
@ -491,6 +513,9 @@ router.post('/:conversationId', storedMessageMutationMiddleware, async (req, res
delete message.isUserSubmitted;
delete message.userSubmittedPaths;
delete message.userSubmittedMessageFieldPaths;
/** Server-private run state: a client-authored row must never seed a run's
* calibration or fading tiers, so the field only ever comes from the server. */
delete message.contextMeta;
const reqCtx = {
userId: req?.user?.id,
isTemporary: req?.body?.isTemporary,
@ -514,7 +539,7 @@ router.post('/:conversationId', storedMessageMutationMiddleware, async (req, res
context: 'POST /api/messages/:conversationId',
...(savedMessage._id != null ? { appendMessageIds: [savedMessage._id] } : {}),
});
res.status(201).json(savedMessage);
res.status(201).json(toClientMessage(savedMessage));
} catch (error) {
logger.error('Error saving message:', error);
res.status(500).json({ error: 'Internal server error' });

View file

@ -495,7 +495,8 @@ const initializeClient = async ({
/** Latest visible context snapshot + every emitted usage payload for this
* response, captured by the handlers and persisted on the response message's
* metadata so the breakdown and branch/total cost survive a reload.
* @type {{ latest: import('librechat-data-provider').TContextUsageEvent | null, count: number }} */
* `onSnapshot` is installed by the client to publish run state after each snapshot.
* @type {{ latest: import('librechat-data-provider').TContextUsageEvent | null, count: number, onSnapshot?: () => void }} */
const contextUsageSink = { latest: null, count: 0 };
/** @type {Array<import('librechat-data-provider').TTokenUsageEvent>} */
const usageEmitSink = [];

View file

@ -59,8 +59,11 @@ function sanitizeImportedMessage(message) {
const text = castPersistedImportedText(message.text);
const content = normalizeImportedArray(message.content);
const attachments = normalizeImportedArray(message.attachments);
const importable = { ...message };
/** Server-private run state never comes from an import. */
delete importable.contextMeta;
return {
...message,
...importable,
isUserSubmitted: true,
...(text !== message.text && { text }),
...(sanitizeTextMarkers &&

View file

@ -966,6 +966,33 @@ describe('importLibreChatConvo', () => {
]);
});
it('drops server-private context meta from imported messages', async () => {
const message = {
messageId: 'message-1',
parentMessageId: Constants.NO_PARENT,
text: 'Imported response',
isCreatedByUser: false,
contextMeta: {
calibrationRatio: 1,
encoding: 'claude',
fading: { v: 1, budgetTokens: 1, masked: true },
},
};
const jsonData = {
conversationId: 'context-meta-import',
title: 'Context meta import',
recursive: false,
messages: [message],
};
const importBatchBuilder = new ImportBatchBuilder('user-123');
const importer = getImporter(jsonData);
await importer(jsonData, 'user-123', () => importBatchBuilder);
expect(importBatchBuilder.messages[0]).not.toHaveProperty('contextMeta');
expect(importBatchBuilder.messages[0].isUserSubmitted).toBe(true);
});
it('sanitizes singleton content and attachment fields before Mongoose array casting', async () => {
const message = {
messageId: 'message-1',