Context policy: validate, reduce, and audit
Support provides replaceable policy implementations while Core keeps the final input contract and budget checks.
The current ContextPolicy discriminant values are validate, reduce, and audit. validate only validates and assembles; reduce selects registered steps after a watermark triggers; audit checks whether App modifications stay within the declared mutable range.
triggerChars/triggerTokens control when to start, targetInputChars/targetInputTokens control the desired watermark, and the final hard budget is checked separately by request preparation and Core. Characters and tokens cannot be guessed from each other; without a counter, do not pretend to have token usage.
Reduction steps contain a real fragmentId, kind, and targetChars. trim specifies history grouping and the minimum number of retained groups, and compress specifies handlerId. Protected questions, system instructions, authorization, and evidence must not be rewritten arbitrarily. Reports record actual changes and usage and do not treat successful compression as a semantic quality guarantee.
Calling processContext directly accepts a ContextPlan, ContextPolicy, and execution options; usually prefer putting it in an ordinary Workflow node so process events and cancellation chains are naturally part of the Loop.
Progressive examples for the same business case
Explicitly use a validation-only policy
Beginner · Node.js
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'validate' };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mode);import { Support } from '@jangts/agent-core';
const policy = { mode: 'validate' };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mode);Configure reduction order from real business fragments
Configuration · Node.js
These numbers are explicit choices for the example business, not SDK recommendations for capacities that suit all models.
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'reduce', triggerChars: 1800, targetInputChars: 1500,
selection: { mode: 'sequence' },
steps: [{ id: 'recent-history', kind: 'trim', fragmentId: 'history', field: 'messages',
grouping: { kind: 'user-turn' }, keepGroups: 2, targetChars: 1000 }],
};
Support.validateContextPolicy(policy, [], []);
console.log(policy.steps);import { Support } from '@jangts/agent-core';
const policy = { mode: 'reduce', triggerChars: 1800, targetInputChars: 1500,
selection: { mode: 'sequence' },
steps: [{ id: 'recent-history', kind: 'trim', fragmentId: 'history', field: 'messages',
grouping: { kind: 'user-turn' }, keepGroups: 2, targetChars: 1000 }],
};
Support.validateContextPolicy(policy, [], []);
console.log(policy.steps);Limit the mutable range for the App's own processing
Extension · Node.js
Auditing also requires the ContextPlan to carry the original fragments; listing IDs alone does not modify or publish material by itself.
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);import { Support } from '@jangts/agent-core';
const policy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);