diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ea2d24f3..d5ac2dcb 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,8 @@ this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +- Distinguish a historical replay from a lost lesson in step-tail teaching (#998). \`registerStepTailProvider\` accepts an opt-in \`dedupe: 'visible'\` and each \`provide\` call now receives \`context.visible\` (\`{ any, current }\`) read from the agent's derived model-visible history before the batch commits. With the option, a contribution already visible at the same id and revision is skipped entirely, so a session restored from its seed adds 0 new messages instead of re-teaching what the log already shows, while a compaction \`replace\` that removes the visible message re-arms the provider; a provider without the option may still append on every batch. The ops-ptc batching lesson opts in: after a compaction removed it, the next accepted batch delivers exactly one short re-anchor (\`COMPACTED_TEACHING_REMINDER\`) instead of the whole lesson, an older visible revision re-teaches the new wording in full, a batch withdrawn before admission still leaves the lesson to the next accepted one, and the lesson text and revision are untouched. \`test/resident-switch.test.mjs\` fixture sessions gained a \`deriveMessages\` backed by their committed list, because its committed-lesson assertion must now pass on real visibility evidence. +- 区分 step-tail 教学的「历史重放」与「压缩丢失」(#998)。\`registerStepTailProvider\` 新增可选 \`dedupe: 'visible'\`,每次 \`provide\` 调用都会收到 \`context.visible\`(\`{ any, current }\`),取自该批次提交之前的 agent 派生可见历史。开启该选项后,同 id 同 revision 且已可见的贡献会被整体跳过——按 seed 恢复的会话新增 0 条消息,而不是把日志里已有的课再教一遍;压缩 \`replace\` 移除可见消息后 provider 重新武装;未开启选项的 provider 仍可每批追加。ops-ptc 批量教学已启用该选项:压缩移除课程后,下一个被接受的批次只投递一条短重锚(\`COMPACTED_TEACHING_REMINDER\`)而非整课全文;可见的是旧 revision 时按新措辞整课重教;提交前被撤回的批次不消耗教学机会;教学正文与 revision 未动。\`test/resident-switch.test.mjs\` 的假 session 补上了由 committed 列表支撑的 \`deriveMessages\`,因为其「已提交课程不重复」断言现在必须走真实可见性证据。 - Publish the explicit full PTC disclosure tier (#987): with `disclosurePolicy: 'full'` the `ptc` and `ops-ptc` rows' first round now carries the whole permitted `allowedTools` pool plus `ops_result` (the D1 sign-off item 1 decision, taken conservatively per #979 §3 so refs spilled this step stay consumable — and forced by the unified rule, since on-demand can always load `result`) with no `ops_load_sdk`. The first program can call the permitted pool directly; read-only sessions still exclude every write API, an empty cap stays at resident + `result`, `residentTools:false` drops the resident surface but not the tier, duplicated names register once, `global.dynamicTools:false` keeps its veto (the row publishes its legacy surface letter for letter) and a row that also turns its loader off keeps the tier. `PtcConfig.disclosurePolicy` stays optional because custom integrations keep the legacy on-demand default until #988; the builtin plan carries the parsed value. On-demand (default) behaviour is letter-identical; the settings-page entry is #989. - 发布显式 PTC 全量披露档位(#987):`disclosurePolicy: 'full'` 的 `ptc` 与 `ops-ptc` 行首轮现在携带整个获准 `allowedTools` 池外加 `ops_result`(D1 签字项 1 的保守决定,依 #979 §3 让本步落盘的 ref 仍可消费——统一口径也逼出同一结论,因为按需档永远可加载 `result`),且无 `ops_load_sdk`。第一个程序即可直接调用获准池;只读会话仍排除全部写 API,空上限停在 常驻 + `result`,`residentTools:false` 丢常驻面但不丢档位,重复名只注册一次,`global.dynamicTools:false` 保持否决(该行逐字发布旧面),同时关掉 loader 的行保留档位。`PtcConfig.disclosurePolicy` 保持可选,因为自定义 integration 在 #988 之前维持旧 on-demand 缺省;内置计划携带解析值。按需档(缺省)行为逐字不变;设置页入口归 #989。 - Parse an explicit PTC disclosure policy without changing any legacy behaviour: the `ptc` and `ops-ptc` rows accept a YAML-only `disclosurePolicy` key, `'on-demand'` (default, the legacy progressive path) or `'full'`. Illegal values are errors naming the key and the received value or type, and non-PTC rows reject the key with a readable diagnostic. The choice survives read → save-unrelated-setting → read through the settings serializer, and no upgrade, settings save or `global.dynamicTools` flip ever writes or erases it. The D1 sign-off item on precedence is resolved conservatively: while `global.dynamicTools` is off the full tier is not applied — the row keeps its legacy resident/alwaysLoaded surface — and the saved choice re-applies when the switch returns on; the row-level `dynamicTools` keeps its loader-only meaning because the full tier publishes no loader. Publishing the full tier remains the D3 slice; no publication behaviour or settings UI changes (#986). diff --git a/src/adapter/dsh/types.ts b/src/adapter/dsh/types.ts index c4f27944..de0c838a 100644 --- a/src/adapter/dsh/types.ts +++ b/src/adapter/dsh/types.ts @@ -9,7 +9,7 @@ export type { Context, Fiber } from '@deepseek-ai/cordis' export type { EntryOptions } from '@deepseek-ai/cordis-plugin-loader' export type { Agent, PreStepDecision } from '@deepseek-ai/dsh-agent' export type { FileSystem, FsTarget, FsInfo, FsObservation } from '@deepseek-ai/dsh-fs' -export type { GenerateOptions, StreamChunk, UserMessage } from '@deepseek-ai/dsh-llm' +export type { GenerateOptions, StreamChunk, Message, UserMessage } from '@deepseek-ai/dsh-llm' export type { Session } from '@deepseek-ai/dsh-session' export type { SettingsPathOpView } from '@deepseek-ai/dsh-settings/types' export type { AssembleContext, PromptAssembly, PromptPresentation, SystemPrompt } from '@deepseek-ai/dsh-system-prompt' diff --git a/src/prompt-injection/index.ts b/src/prompt-injection/index.ts index 79215ae4..51cdf77d 100644 --- a/src/prompt-injection/index.ts +++ b/src/prompt-injection/index.ts @@ -1,8 +1,8 @@ -import type { Agent, Context, PreStepDecision, UserMessage } from '../adapter/dsh/types.js' +import type { Agent, Context, Message, PreStepDecision, UserMessage } from '../adapter/dsh/types.js' import { loadUserMessage } from '../adapter/dsh/host.js' import { PromptProviderError } from './types.js' -import type { PromptContribution, PromptDisposer, StepTailContext, StepTailProviderOptions, SystemSectionOptions } from './types.js' -export type { PromptContribution, PromptDisposer, StepTailContext, StepTailProviderOptions, SystemSectionOptions } from './types.js' +import type { PromptContribution, PromptDisposer, StepTailContext, StepTailProviderOptions, SystemSectionOptions, VisibleContribution } from './types.js' +export type { PromptContribution, PromptDisposer, StepTailContext, StepTailProviderOptions, SystemSectionOptions, VisibleContribution } from './types.js' export { PromptProviderError } from './types.js' interface TailEntry { options: StepTailProviderOptions; active: boolean; lifetime: AbortController } @@ -20,6 +20,21 @@ function stateFor(agent: Agent): AgentState { function validate(options: PromptContribution): void { if (!options.id.trim() || !options.revision.trim()) throw new TypeError('Prompt id and revision must be nonempty strings.') if (!Number.isFinite(options.priority ?? 0)) throw new TypeError('Prompt priority must be finite.') + if ((options as StepTailProviderOptions).dedupe !== undefined && (options as StepTailProviderOptions).dedupe !== 'visible') { + throw new TypeError('Step-tail dedupe must be "visible".') + } +} +/** Read one contribution's visibility from the derived model-visible history. */ +function readVisibility(history: readonly Message[], id: string, revision: string): VisibleContribution { + let any = false, current = false + for (const message of history) { + if (message.role !== 'user') continue + const source = message.source + if (source?.kind !== 'dsh-ops:step-tail' || source.contributor !== id) continue + any = true + if (source.revision === revision) { current = true; break } + } + return { any, current } } function lifetime(owner: Context, agent: Agent, cleanup: PromptDisposer): PromptDisposer { let active = true @@ -71,17 +86,26 @@ function ordered(entries: Iterable): TailEntry[] { return [...entries].sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0) || (a.options.id < b.options.id ? -1 : a.options.id > b.options.id ? 1 : 0)) } -async function appendTails(state: AgentState, payload: Omit, decision: PreStepDecision): Promise { +async function appendTails(state: AgentState, payload: Omit, decision: PreStepDecision): Promise { if (decision.kind !== 'enter' || !decision.messages.length || payload.signal.aborted || !state.tails.size) return decision const entries = ordered(state.tails.values()) - const context: StepTailContext = Object.freeze({ ...payload, messages: freeze(structuredClone(decision.messages)) }) + // The committed history is the visibility evidence for every provider: read it + // once per batch, before this batch itself commits, so a contribution replayed + // from a restored session is distinguishable from a new addition and a + // compaction that removed one is distinguishable from a fresh session. + const history = payload.agent.session.deriveMessages() + const context: Omit = Object.freeze({ ...payload, messages: freeze(structuredClone(decision.messages)) }) const pending: { entry: TailEntry; text: string }[] = [] for (const entry of entries) { if (payload.signal.aborted) return decision if (!entry.active) continue + const visible = readVisibility(history, entry.options.id, entry.options.revision) + // A same-revision message already visible is a historical replay, not a new + // addition: the provider is skipped entirely instead of re-teaching. + if (entry.options.dedupe === 'visible' && visible.current) continue try { const signal = AbortSignal.any([payload.signal, entry.lifetime.signal]) - const text = await cancellable(Promise.resolve().then(() => entry.active && !signal.aborted ? entry.options.provide(Object.freeze({ ...context, signal })) : undefined), signal) + const text = await cancellable(Promise.resolve().then(() => entry.active && !signal.aborted ? entry.options.provide(Object.freeze({ ...context, signal, visible })) : undefined), signal) if (payload.signal.aborted) return decision if (text !== undefined && typeof text !== 'string') throw new TypeError('Step-tail providers must return text or undefined.') if (text && entry.active) pending.push({ entry, text }) diff --git a/src/prompt-injection/types.ts b/src/prompt-injection/types.ts index cbf81f75..685d3344 100644 --- a/src/prompt-injection/types.ts +++ b/src/prompt-injection/types.ts @@ -12,6 +12,13 @@ export interface SystemSectionOptions extends PromptContribution { readonly text: string | ((context: AssembleContext) => string) } export type ReadonlyValue = T extends object ? { readonly [K in keyof T]: ReadonlyValue } : T +/** What the model-visible history currently shows for one step-tail contribution. */ +export interface VisibleContribution { + /** A step-tail message of this contributor is visible, whatever its revision. */ + readonly any: boolean + /** A visible message of this contributor carries exactly this revision. */ + readonly current: boolean +} export interface StepTailContext { readonly agent: Agent readonly turn: number @@ -20,8 +27,19 @@ export interface StepTailContext { readonly signal: AbortSignal /** Detached, deeply frozen nonempty accepted batch, without this API's contributions. */ readonly messages: readonly ReadonlyValue[] + /** Visibility of this contribution in the agent's derived history, read before this batch commits. */ + readonly visible: VisibleContribution } export interface StepTailProviderOptions extends PromptContribution { + /** + * Opt-in restore dedupe: while a step-tail message with this id and revision + * is visible in the agent's derived history, the provider is not evaluated and + * nothing is appended. A session restored from that history therefore adds 0 + * messages, while a compaction replace that removes the visible message + * re-arms the provider, whose next provide then sees visible.any === false. + * Without the option every accepted batch may append, as before. + */ + readonly dedupe?: 'visible' /** * Called only for accepted nonempty user-message batches, not every model request. * The final active check before this hook returns its batch to host pre-step is diff --git a/src/publication/ptc.ts b/src/publication/ptc.ts index 0a82a513..7a561494 100644 --- a/src/publication/ptc.ts +++ b/src/publication/ptc.ts @@ -28,6 +28,12 @@ import { publicationRegistration, publicationReady } from './reentry.js' import { retired as retiredLegacyRow } from '../settings/legacy.js' export { resolvePtcConfig } from '../config.js' export const name = 'dsh-ops-ptc' +/** + * Short re-anchor delivered once when a compaction removed the full lesson from + * the visible history: the compaction summary is assumed to keep the gist, so + * one line re-points the operating rule instead of re-sending the whole lesson. + */ +export const COMPACTED_TEACHING_REMINDER = 'The batch-call guidance delivered earlier is no longer visible in this session history. Keep grouping independent tool calls in one run_code program under Promise.all, open every program with a first-line // plan:, and route file work through the ops_* APIs instead of the shell or Node IO.' interface Entry { agent: Agent @@ -39,7 +45,7 @@ interface Entry { pending: Set definitions: Map disposers: (() => void)[] - /** Whether the teacher trajectory reached session history, not merely the pre-step. */ + /** Whether the lesson reached this agent's durable history, not merely the pre-step. After a compaction removes it from view, this latch is what earns the short re-anchor instead of the full text. */ taught: boolean files: Map void; dispatch: ToolDefinition['execute']; granted?: { entitled: boolean; selected: boolean; config: object } }> } @@ -197,13 +203,24 @@ export function mountPtc(root: Context, rawConfig: unknown, gate: PtcGate = { al // The provider must not latch here. A batch can still be withdrawn before // admission (cancellation, rejection), and latching on the return value // would then skip the lesson for the whole session without a trace. + // The framework skips the provider while the same-revision lesson is + // visible, so a session restored from that history adds 0 new messages + // instead of re-teaching what the model can already see. entry.disposers.push(registerStepTailProvider(entry.ctx, agent, { id: BATCHING_GUIDANCE_ID, revision: BATCHING_GUIDANCE_REVISION, - provide() { - if (entry.taught) return undefined - const text = loadBatchingGuidance() - return text.length > 0 ? text : undefined + dedupe: 'visible', + provide(context) { + // Reaching here means the model cannot currently see the lesson: + // an older revision visible is a wording change, so the new text + // teaches anew; nothing visible with the latch set means a + // compaction removed it after it was taught, so one short + // re-anchor; a fresh session gets the full lesson. + if (context.visible.any || !entry.taught) { + const text = loadBatchingGuidance() + return text.length > 0 ? text : undefined + } + return COMPACTED_TEACHING_REMINDER }, })) // The program-shape trigger observes the ROOT run_code call: only there is diff --git a/test/prompt-injection.test.mjs b/test/prompt-injection.test.mjs index 71d1a75d..7577eea2 100644 --- a/test/prompt-injection.test.mjs +++ b/test/prompt-injection.test.mjs @@ -1,7 +1,11 @@ -import { assert, test, report } from './lib/harness.mjs' +import { assert, test, report, waitFor } from './lib/harness.mjs' import { renderPrompt } from '@deepseek-ai/dsh-system-prompt' import { Session } from '@deepseek-ai/dsh-session' +import { createUserMessage } from '@deepseek-ai/dsh-llm' +import Subprocess from '@deepseek-ai/dsh-subprocess-local' import { registerSystemSection, registerStepTailProvider, PromptProviderError } from '../lib/prompt-injection/index.js' +import { mountPtc } from '../lib/publication/ptc.js' +import { loadBatchingGuidance } from '../lib/ptc/batching-guidance.js' import { promptHost, tailMessages } from './lib/prompt-injection-host.mjs' const identity = 'You are an AI agent powered by DeepSeek Harness.\n\n' @@ -211,4 +215,184 @@ await test('agent teardown releases registrations owned by a longer-lived plugin assert.deepEqual(h.errors, []) } finally { await h.close() } }) + +// ---- resume dedupe and compaction re-teaching (#998) ---- + +/** + * Boot the real host loop plus the shipped ops-ptc teaching wiring with no + * published tools, so only the lesson machinery runs. The worker binary stays + * inert (nothing in the empty pool ever dispatches), so the node executable + * stands in for it and this suite needs no native build. + */ +async function teachingHost() { + const h = await promptHost() + await h.ctx.plugin(Subprocess) + h.ctx.provide('sandboxPolicy', { resolve: () => ({ mode: 'danger-full-access', workspaceRoot: process.cwd() }) }) + await h.ctx.plugin({ name: 'ptc-teaching-fixture', apply: inner => { + mountPtc(inner, { binaryPath: process.execPath, required: true, allowedTools: [], alwaysLoaded: [] }, + { allows: () => true, resident: false, dynamic: false, batching: true }) + } }) + return h +} + +/** One synthetic pre-step, exactly the payload the resident-switch suite drives: evaluated, never committed. */ +const probeTeaching = async agent => { + const accepted = createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: 'Probe.' }] }) + const decision = await agent.ctx.waterfall('agent/pre-step', { + agent, messages: [accepted], turn: 0, step: 0, signal: new AbortController().signal, + modelSelection: { provider: 'fixture', model: 'capture' }, + }, async () => ({ kind: 'enter', messages: [accepted] })) + return tailMessages(decision) +} + +/** Committed durable user messages of one agent's session log. */ +const committedOf = (h, agent) => h.events + .filter(({ session, event }) => session === agent.session && event.type === 'user/message') + .map(({ event }) => event.data) + +/** Shadow every visible node except the system prompt with one compaction summary. */ +const compactHistory = (agent, text) => { + const nodes = [...agent.session.surface.nodes] + agent.session.append('user/message', + createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text }] }), + { surfaceOp: { op: 'replace', startSeq: nodes[1], endSeq: nodes[nodes.length - 1] }, sourceEventSeqs: nodes.slice(1) }) +} + +await test('dedupe providers are skipped while their same-revision message is visible and re-armed when a compaction removes it', async () => { + const h = await promptHost() + try { + const a = await h.agent() + const seen = [] + let calls = 0 + const stop = registerStepTailProvider(h.owner(), a, { id: 'lesson', revision: '1', dedupe: 'visible', + provide: context => { ++calls; seen.push(context.visible); return 'FULL' } }) + await h.run(a) + assert.equal(calls, 1) + assert.deepEqual(seen[0], { any: false, current: false }, 'nothing is visible before the first commit') + const id = tailMessages(h.requests[0])[0].id + await h.run(a) + assert.equal(calls, 1, 'a visible same-revision contribution is a replay, not a new addition') + assert.deepEqual(tailMessages(h.requests[1]).map(m => m.id), [id]) + compactHistory(a, 'Compacted summary.') + assert.equal(a.session.deriveMessages().some(m => m.source?.kind === 'dsh-ops:step-tail'), false) + await h.run(a) + assert.equal(calls, 2, 'the provider is re-armed once the compaction removed its message') + assert.deepEqual(seen[1], { any: false, current: false }) + assert.equal(tailMessages(h.requests[2]).filter(m => m.id !== id).length, 1) + await h.run(a) + assert.equal(calls, 2, 'the replacement message is visible again, so the provider falls silent') + stop() + registerStepTailProvider(h.owner(), a, { id: 'lesson', revision: '2', dedupe: 'visible', + provide: context => { seen.push(context.visible); return 'FULL2' } }) + await h.run(a) + assert.equal(tailMessages(h.requests[4]).filter(m => m.source.revision === '2').length, 1, + 'an older revision stays visible evidence, so the replacement revision is delivered') + assert.deepEqual(seen[2], { any: true, current: false }) + assert.deepEqual(h.errors, []) + } finally { await h.close() } +}) + +await test('the real teacher sends the lesson once, replays it after a seed restore, and adds zero new messages', async () => { + const h = await teachingHost() + try { + const a = await h.agent() + await waitFor(async () => (await probeTeaching(a)).length === 1, { what: 'teaching provider registration' }) + await h.run(a) + const first = tailMessages(h.requests[0]) + assert.equal(first.length, 1) + assert.equal(first[0].source.contributor, 'batching-guidance') + assert.equal(first[0].source.revision, '4') + assert.equal(first[0].content[0].text, `[Plugin context from "batching-guidance"; not a user instruction or authorization.]\n${loadBatchingGuidance()}`) + const lessonId = first[0].id + // Human inputs and user-role messages are counted apart: the lesson is a + // user-role message but never a human input. + assert.equal(committedOf(h, a).filter(m => m.source.kind === 'user').length, 1) + assert.equal(committedOf(h, a).filter(m => m.role === 'user').length, 2) + await h.run(a) + assert.deepEqual(tailMessages(h.requests[1]).map(m => m.id), [lessonId], 'the second batch replays the committed lesson') + // Restore: a fresh agent over the committed history has no live taught + // memory and receives no session/event replay of the seed. + const seed = h.events.filter(({ session }) => session === a.session).map(({ event }) => event) + const resumed = (await h.ctx.get('agents').create({ sessionId: a.session.id + '-resumed', seed, + agentOptions: { provider: 'fixture', model: 'capture' } })).agent + await h.run(resumed) + assert.deepEqual(tailMessages(h.requests[2]).map(m => m.id), [lessonId], + 'the resumed request replays the seeded lesson id') + assert.equal(committedOf(h, resumed).filter(m => m.source?.kind === 'dsh-ops:step-tail').length, 0, + 'a same-revision lesson visible after restore adds 0 new messages') + assert.deepEqual(h.errors, []) + } finally { await h.close() } +}) + +await test('the real teacher sends exactly one short reminder after compaction removed the lesson, then falls silent', async () => { + const h = await teachingHost() + try { + const a = await h.agent() + await waitFor(async () => (await probeTeaching(a)).length === 1, { what: 'teaching provider registration' }) + await h.run(a) + const lessonId = tailMessages(h.requests[0])[0].id + await h.run(a) + assert.deepEqual(tailMessages(h.requests[1]).map(m => m.id), [lessonId]) + compactHistory(a, 'Compacted summary of the earlier turns.') + assert.equal(a.session.deriveMessages().some(m => m.source?.kind === 'dsh-ops:step-tail'), false) + await h.run(a) + const added = tailMessages(h.requests[2]).filter(m => m.id !== lessonId) + assert.equal(added.length, 1, 'the compacted lesson earns exactly one new message') + const { COMPACTED_TEACHING_REMINDER } = await import('../lib/publication/ptc.js') + assert.equal(added[0].content[0].text, `[Plugin context from "batching-guidance"; not a user instruction or authorization.]\n${COMPACTED_TEACHING_REMINDER}`) + assert.ok(COMPACTED_TEACHING_REMINDER.length < loadBatchingGuidance().length / 8, + 'the reminder is a short re-anchor, not the full lesson again') + await h.run(a) + assert.deepEqual(tailMessages(h.requests[3]).map(m => m.id), [added[0].id], + 'the reminder is visible now, so no second one is appended') + assert.deepEqual(h.errors, []) + } finally { await h.close() } +}) + +await test('a batch withdrawn before admission does not consume the real teaching opportunity', async () => { + const h = await teachingHost() + try { + const b = await h.agent() + await waitFor(async () => (await probeTeaching(b)).length === 1, { what: 'teaching provider registration' }) + let rejectOnce = true + const off = b.ctx.on('agent/pre-step', async (_payload, next) => { + if (!rejectOnce) return next() + rejectOnce = false + await next() + return { kind: 'reject' } + }) + b.followup(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: 'withdrawn' }] })) + await b.whenIdle() + assert.equal(committedOf(h, b).filter(m => m.source?.kind === 'dsh-ops:step-tail').length, 0, + 'the withdrawn batch committed nothing') + off() + await h.run(b) + assert.equal(committedOf(h, b).filter(m => m.source?.kind === 'dsh-ops:step-tail').length, 1, + 'the next accepted batch still receives the full lesson') + assert.match(committedOf(h, b).find(m => m.source?.kind === 'dsh-ops:step-tail').content[0].text, /Batch tool calls/) + assert.deepEqual(h.errors, []) + } finally { await h.close() } +}) + +await test('tool continuation steps replay the committed lesson without appending a new one', async () => { + const h = await teachingHost() + try { + const a = await h.agent() + await waitFor(async () => (await probeTeaching(a)).length === 1, { what: 'teaching provider registration' }) + h.ctx.get('tools').register({ name: 'fixture_tool', description: 'Fixture', parameters: { type: 'object', properties: {} }, + output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] }, execute: () => 'done' }) + const tool = async function* () { + yield { type: 'tool-call-delta', index: 0, id: 'fixture-call', name: 'fixture_tool', argumentsDelta: '{}' } + yield { type: 'finish', reason: { kind: 'stop' } } + } + await h.run(a, [tool, undefined]) + const lessonId = tailMessages(h.requests[0])[0].id + assert.deepEqual(tailMessages(h.requests[1]).map(m => m.id), [lessonId], + 'the continuation replays the committed lesson id') + assert.equal(committedOf(h, a).filter(m => m.source?.kind === 'dsh-ops:step-tail').length, 1, + 'one tool call, one lesson: no second full text was appended') + assert.deepEqual(h.errors, []) + } finally { await h.close() } +}) + report('prompt-injection') diff --git a/test/resident-switch.test.mjs b/test/resident-switch.test.mjs index 68945394..870fd7d7 100644 --- a/test/resident-switch.test.mjs +++ b/test/resident-switch.test.mjs @@ -74,7 +74,9 @@ async function setup(cwd, mode, switches, global = {}, override = {}) { presets.set(scopeOf(preset), mode) for (const name of ['read', 'glob', 'grep']) preset.tools.register({ name, description: name, parameters: { type: 'object', properties: {} }, output: { schema: { type: 'object' }, render: () => [] }, execute: async () => ({}) }) - const agent = { id, session: { id, mode: 'danger-full-access', header: { cwd }, append() {}, read() { return [] } } } + // The derived history the teaching dedupe reads; batching tests commit into it. + const committed = [] + const agent = { id, session: { id, mode: 'danger-full-access', header: { cwd }, append() {}, read() { return [] }, committed, deriveMessages: () => committed } } agent.ctx = await scoped(preset, agent, scopeOf(preset)) if (mode === 'ptc' || mode === 'ops-ptc') await agent.ctx.plugin({ inject: ['tools', 'systemPrompt'], apply: inner => { selectExecutor(inner, 'ptc') } }) agents.push(agent) @@ -198,6 +200,7 @@ await test('ops-ptc batchingGuidance teaches until the lesson is committed to hi // admission never becomes history, so the lesson must be offered again. assert.equal(stepTailMessages(await drivePreStep(agent, 2)).length, 1, 'an uncommitted lesson is offered again') // Once the host reports the durable message, the lesson is spent for the session. + agent.session.committed.push(first[0]) await ctx.serial('session/event', agent.session, { type: 'user/message', data: { source: first[0].source } }) assert.equal(stepTailMessages(await drivePreStep(agent, 3)).length, 0, 'a committed lesson is never repeated') } finally { await shutdown(ctx) }