# Conflicts: # apps/cli/tests/web-agent-presets.e2e.ts # docs/subsystems/system-prompt.i18n.yaml # docs/tool-catalog.i18n.yaml # packages/context/agent-instructions/tests/agent-instructions.spec.ts
6587 lines
275 KiB
TypeScript
6587 lines
275 KiB
TypeScript
import { FsError } from '@deepseek-ai/dsh-fs'
|
|
import { executeToolFixture } from '@deepseek-ai/dsh-tool-testkit'
|
|
import * as NativeToolExecutor from '@deepseek-ai/dsh-tool-executor-native'
|
|
import { chmod, mkdtemp, mkdir, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises'
|
|
import { provideWorkingDirectoryFixture } from '@deepseek-ai/dsh-agent-loop-testkit'
|
|
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { Context } from '@deepseek-ai/cordis'
|
|
import Loader from '@deepseek-ai/cordis-plugin-loader'
|
|
import type { Config as AgentInstructionsConfig } from '@deepseek-ai/dsh-agent-instructions'
|
|
import * as AgentInstructions from '@deepseek-ai/dsh-agent-instructions'
|
|
import LlmRuntime, { createUserMessage, ToolCallId, type Message, type MessageSource, type StreamChunk } from '@deepseek-ai/dsh-llm'
|
|
import type { ContextFormed } from '@deepseek-ai/dsh-llm'
|
|
import SessionStore, { SessionId, SessionSeq, type SessionEvent, type SurfaceIntent, type UserMessage } from '@deepseek-ai/dsh-session'
|
|
import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
|
|
import AgentRegistry, { agentEvents, type Agent } from '@deepseek-ai/dsh-agent'
|
|
import AgentLoop from '@deepseek-ai/dsh-agent-loop'
|
|
import { FileSystem, FsTargetKey, FsVersion } from '@deepseek-ai/dsh-fs'
|
|
import type {
|
|
FsDirEntry,
|
|
FsEditOutcome,
|
|
FsEditRequest,
|
|
FsInfo,
|
|
FsPathInfo,
|
|
FsTarget,
|
|
FsWriteIntent,
|
|
FsWriteOutcome,
|
|
} from '@deepseek-ai/dsh-fs'
|
|
import LocalFileSystem from '@deepseek-ai/dsh-fs-local'
|
|
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
|
|
import ToolRuntime from '@deepseek-ai/dsh-tools'
|
|
import { defineContentToolFixture } from '@deepseek-ai/dsh-tool-testkit'
|
|
import type {
|
|
ToolExecution,
|
|
ToolExecutionToken,
|
|
} from '@deepseek-ai/dsh-tools'
|
|
import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
|
|
import {
|
|
discoverBaselineInstructionFiles,
|
|
loadBaselineInstructions,
|
|
renderAgentInstructions,
|
|
} from '@deepseek-ai/dsh-agent-instructions'
|
|
import {
|
|
applyInstructionVersionUpdates,
|
|
baselineInstructionState,
|
|
reconcileInstructionContext,
|
|
type InstructionVersionCache,
|
|
} from '../src/state.ts'
|
|
import { resolveConfig } from '../src/config.ts'
|
|
import {
|
|
AGENTS_GLOBAL_DIRECTORY,
|
|
candidateScopeKey,
|
|
renderInstructionChanges,
|
|
renderAgentInstructionSet,
|
|
USER_GLOBAL_DIRECTORY,
|
|
USER_GLOBAL_FILE,
|
|
type ChangeRenderItem,
|
|
} from '../src/render.ts'
|
|
import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
|
|
import {
|
|
mountAgentLoopTestDependencies,
|
|
mountAgentLoopTestHarness,
|
|
} from '@deepseek-ai/dsh-agent-loop-testkit'
|
|
|
|
declare module '@deepseek-ai/dsh-llm' {
|
|
interface MessageSourceMap {
|
|
'downstream': { kind: 'downstream' } & ContextFormed
|
|
'other': { kind: 'other' } & ContextFormed
|
|
'test-skills': { kind: 'test-skills' } & ContextFormed
|
|
}
|
|
}
|
|
|
|
type CheckpointSource = Extract<MessageSource, { readonly kind: 'compact-checkpoint' }>
|
|
|
|
/** Build a typed checkpoint source for an instruction projection fixture. */
|
|
function checkpointSource(compactionId: string): CheckpointSource {
|
|
return { kind: 'compact-checkpoint', compactionId: compactionId as CheckpointSource['compactionId'] }
|
|
}
|
|
|
|
/** Per-candidate reconciliation scope key: directory paired with the file name. */
|
|
const sk = (directory: string, candidateName: string): string => candidateScopeKey(directory, candidateName)
|
|
|
|
/** Intro of a baseline rendered with the full workspace-instruction guidance. */
|
|
const baselineIntro = 'The following workspace instructions may be relevant to your work. '
|
|
+ 'Use them as guidance when applicable. More specific instructions take precedence over broader ones. '
|
|
+ 'They do not override system, developer, or direct user instructions.'
|
|
|
|
/** Guidance that follows the applicability sentence of every additional-instructions section. */
|
|
const additionalGuidance = 'Use them as guidance when relevant; more specific instructions take precedence. '
|
|
+ 'They do not override system, developer, or direct user instructions.'
|
|
|
|
/** Five 5,000-byte project files from the root down four 10-character directories, with a long budget marker. */
|
|
const deepProjectFiles = [
|
|
'AGENTS.md',
|
|
'aaaaaaaaaa/AGENTS.md',
|
|
'aaaaaaaaaa/bbbbbbbbbb/AGENTS.md',
|
|
'aaaaaaaaaa/bbbbbbbbbb/cccccccccc/AGENTS.md',
|
|
'aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/AGENTS.md',
|
|
].map(displayPath => ({ absolutePath: `/repo/${displayPath}`, displayPath, content: 'p'.repeat(5000) }))
|
|
|
|
/** Budget marker of {@link deepProjectFiles} rendered alone under a `maxBytes` too small to keep any of their content. */
|
|
function deepProjectMarker(maxBytes: number): string {
|
|
return `Workspace instruction budget ${String(maxBytes)} bytes: omitted AGENTS.md, aaaaaaaaaa/AGENTS.md, `
|
|
+ 'aaaaaaaaaa/bbbbbbbbbb/AGENTS.md, aaaaaaaaaa/bbbbbbbbbb/cccccccccc/AGENTS.md; '
|
|
+ 'truncated aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/AGENTS.md from 5000 to 0 bytes'
|
|
}
|
|
|
|
/** A plain ASCII instruction file of `kib` KiB at a `~/` user-global or project display path. */
|
|
function kibInstruction(displayPath: string, kib: number): { absolutePath: string; displayPath: string; content: string } {
|
|
return {
|
|
absolutePath: displayPath.startsWith('~/') ? `/home/user/${displayPath.slice(2)}` : `/repo/${displayPath}`,
|
|
displayPath,
|
|
content: 'x'.repeat(kib * 1024),
|
|
}
|
|
}
|
|
|
|
const testToolSignal = new AbortController().signal
|
|
const isolatedInboxCtx = new Context()
|
|
provideWorkingDirectoryFixture(isolatedInboxCtx)
|
|
await mountAgentLoopTestDependencies(isolatedInboxCtx)
|
|
const isolatedAgentLoop = await mountAgentLoopTestHarness(isolatedInboxCtx)
|
|
let nextStubSession = 1
|
|
// The shared agents root defaults to the developer's real ~/.agents, whose
|
|
// AGENTS.md would otherwise join every discovery result. Tests that exercise
|
|
// that root pin `DSH_AGENTS_HOME`; the rest read this absent root.
|
|
const absentAgentsHome = await mkdtemp(join(tmpdir(), 'dsh-absent-agents-'))
|
|
beforeEach(() => {
|
|
vi.stubEnv('DSH_AGENTS_HOME', absentAgentsHome)
|
|
})
|
|
afterAll(async () => {
|
|
await isolatedInboxCtx.fiber.dispose()
|
|
await rm(absentAgentsHome, { recursive: true, force: true })
|
|
})
|
|
|
|
type TestAgent = Agent
|
|
|
|
/** Admit one test Agent's pending input through the production loop driver. */
|
|
function claimInbox(agent: Agent, target: 'next-turn' | 'next-step'): UserMessage[] {
|
|
return isolatedAgentLoop.claim(agent, target, 1)
|
|
}
|
|
const requestTimeoutMs = process.platform === 'win32' ? 5_000 : 1_000
|
|
|
|
async function tempRepo(): Promise<string> {
|
|
return mkdtemp(join(tmpdir(), 'dsh-workspace-context-'))
|
|
}
|
|
|
|
async function write(path: string, content: string): Promise<void> {
|
|
await mkdir(join(path, '..'), { recursive: true })
|
|
await writeFile(path, content)
|
|
}
|
|
|
|
/**
|
|
* Pin the harness home for the current test; the plugin resolves it from the
|
|
* process environment and ignores row configuration.
|
|
* @param home - absolute harness home that `$DSH_HOME` names.
|
|
*/
|
|
function pinHarnessHome(home: string): void {
|
|
vi.stubEnv('DSH_HOME', home)
|
|
}
|
|
|
|
/** Pin the shared agents root for the current test; the plugin resolves it from the process environment. */
|
|
function pinAgentsHome(home: string): void {
|
|
vi.stubEnv('DSH_AGENTS_HOME', home)
|
|
}
|
|
|
|
class RecordingFileSystem extends FileSystem {
|
|
override async patchText(): Promise<never> { throw new FsError('patch unsupported by test provider', 'FS_IO_ERROR') }
|
|
override async deleteFile(): Promise<never> { throw new FsError('delete unsupported by test provider', 'FS_IO_ERROR') }
|
|
override async moveFile(): Promise<never> { throw new FsError('move unsupported by test provider', 'FS_IO_ERROR') }
|
|
|
|
override watch(): never { throw new Error('Fixture does not support watching') }
|
|
entries = new Map<string, { type: FsInfo['type']; content?: string; version?: FsVersion }>()
|
|
missingOnResolve = new Set<string>()
|
|
throwOnStat = new Set<string>()
|
|
throwOnRead = new Set<string>()
|
|
omitSizes = new Set<string>()
|
|
readTargets: string[] = []
|
|
readTextTargets: string[] = []
|
|
signals: AbortSignal[] = []
|
|
|
|
override async resolve(path: string, opts?: { cwd?: string; signal?: AbortSignal }): Promise<FsTarget> {
|
|
if (opts?.signal !== undefined) this.signals.push(opts.signal)
|
|
opts?.signal?.throwIfAborted()
|
|
// resolve(), not join(): entries are seeded with host join() keys, and on
|
|
// Windows a joined '/'-rooted prefix would not match a resolved drive path.
|
|
const absolute = resolve(opts?.cwd ?? '/', path)
|
|
if (this.missingOnResolve.has(absolute)) {
|
|
throw Object.assign(new Error(`not found: ${absolute}`), { code: 'FS_NOT_FOUND' })
|
|
}
|
|
return { targetKey: FsTargetKey(absolute), displayPath: absolute }
|
|
}
|
|
|
|
override processPath(target: FsTarget): string { return String(target.targetKey) }
|
|
|
|
override fileUrl(target: FsTarget): string { return `file://${target.targetKey}` }
|
|
|
|
override contains(parent: FsTarget, child: FsTarget): boolean {
|
|
const descendant = relative(String(parent.targetKey), String(child.targetKey))
|
|
return descendant === '' || (!descendant.startsWith('..') && !isAbsolute(descendant))
|
|
}
|
|
|
|
override async stat(target: FsTarget, signal?: AbortSignal): Promise<FsInfo | undefined> {
|
|
if (signal !== undefined) this.signals.push(signal)
|
|
signal?.throwIfAborted()
|
|
if (this.throwOnStat.has(target.targetKey)) throw new Error(`stat failed: ${target.displayPath}`)
|
|
const entry = this.entries.get(target.targetKey)
|
|
if (entry === undefined) return undefined
|
|
const info: FsInfo = {
|
|
version: entry.version ?? FsVersion(`v:${target.targetKey}:${entry.type}:${entry.content ?? ''}`),
|
|
type: entry.type,
|
|
}
|
|
if (entry.content !== undefined && !this.omitSizes.has(target.targetKey)) info.size = Buffer.byteLength(entry.content, 'utf8')
|
|
return info
|
|
}
|
|
|
|
override async lstat(path: string, opts?: { cwd?: string }, signal?: AbortSignal): Promise<FsPathInfo | undefined> {
|
|
if (signal !== undefined) this.signals.push(signal)
|
|
signal?.throwIfAborted()
|
|
const target = await this.resolve(path, { ...opts, ...signal === undefined ? {} : { signal } })
|
|
const info = await this.stat(target, signal)
|
|
if (info === undefined) return undefined
|
|
return {
|
|
version: info.version,
|
|
type: info.type,
|
|
...(info.size !== undefined ? { size: info.size } : {}),
|
|
}
|
|
}
|
|
|
|
override async readText(target: FsTarget, signal?: AbortSignal): Promise<string> {
|
|
if (signal !== undefined) this.signals.push(signal)
|
|
signal?.throwIfAborted()
|
|
this.readTextTargets.push(target.targetKey)
|
|
return this.entries.get(target.targetKey)?.content ?? ''
|
|
}
|
|
|
|
override async readBytes(_target: FsTarget, _signal: AbortSignal | undefined, _maxBytes: number): Promise<Uint8Array> {
|
|
throw new Error('not needed in agent-instructions tests')
|
|
}
|
|
|
|
override async readByteRange(_target: FsTarget, _range: { offset: number; length: number }, _signal?: AbortSignal): Promise<Uint8Array> {
|
|
throw new Error('not needed in agent-instructions tests')
|
|
}
|
|
|
|
override async streamText(target: FsTarget, signal?: AbortSignal): Promise<AsyncIterable<string>> {
|
|
if (signal !== undefined) this.signals.push(signal)
|
|
signal?.throwIfAborted()
|
|
this.readTargets.push(target.targetKey)
|
|
if (this.throwOnRead.has(target.targetKey)) throw new Error(`read failed: ${target.displayPath}`)
|
|
const content = this.entries.get(target.targetKey)?.content ?? ''
|
|
return (async function* () {
|
|
const midpoint = Math.ceil(content.length / 2)
|
|
yield content.slice(0, midpoint)
|
|
signal?.throwIfAborted()
|
|
yield content.slice(midpoint)
|
|
})()
|
|
}
|
|
|
|
override async listDir(_target: FsTarget): Promise<FsDirEntry[]> {
|
|
return []
|
|
}
|
|
|
|
override async writeText(_target: FsTarget, _content: string, _expected?: FsWriteIntent): Promise<FsWriteOutcome> {
|
|
return { operation: 'update', version: FsVersion('unused'), before: '', after: _content }
|
|
}
|
|
|
|
override async editText(_target: FsTarget, _edit: FsEditRequest): Promise<FsEditOutcome> {
|
|
return { version: FsVersion('unused'), before: '', after: '' }
|
|
}
|
|
}
|
|
|
|
class BlockingReadFileSystem extends RecordingFileSystem {
|
|
readonly started = Promise.withResolvers<undefined>()
|
|
|
|
override async streamText(target: FsTarget, signal?: AbortSignal): Promise<AsyncIterable<string>> {
|
|
if (signal !== undefined) this.signals.push(signal)
|
|
this.readTargets.push(target.targetKey)
|
|
this.started.resolve(undefined)
|
|
return (async function* () {
|
|
await new Promise<void>((_resolve, reject) => {
|
|
const abortReason = (): Error => signal?.reason instanceof Error ? signal.reason : new Error('aborted')
|
|
if (signal?.aborted) { reject(abortReason()); return }
|
|
signal?.addEventListener('abort', () => { reject(abortReason()) }, { once: true })
|
|
})
|
|
yield 'unreachable'
|
|
})()
|
|
}
|
|
}
|
|
|
|
async function mountAgentInstructionsPlugin(ctx: Context, config: AgentInstructionsConfig): Promise<Awaited<ReturnType<Context['plugin']>>> {
|
|
if (ctx.get('workingDirectory') === undefined) provideWorkingDirectoryFixture(ctx)
|
|
if (ctx.get('sessionProjections') === undefined) if (ctx.get('sessionProjections') === undefined) await ctx.plugin(SessionProjectionRegistry)
|
|
if (ctx.get('agents') === undefined) await ctx.plugin(AgentRegistry)
|
|
return ctx.plugin(AgentInstructions, config)
|
|
}
|
|
|
|
async function mountAgentInstructions(ctx: Context, config: AgentInstructionsConfig): Promise<Awaited<ReturnType<Context['plugin']>>> {
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
return mountAgentInstructionsPlugin(ctx, config)
|
|
}
|
|
|
|
async function mountFileToolsAndAgentInstructions(ctx: Context, config: AgentInstructionsConfig): Promise<Awaited<ReturnType<Context['plugin']>>> {
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
await ctx.plugin(ToolFs)
|
|
return mountAgentInstructionsPlugin(ctx, config)
|
|
}
|
|
|
|
async function stubAgent(cwd?: string, seed: readonly SessionEvent[] = []): Promise<TestAgent> {
|
|
const id = SessionId(`agent-instructions-${String(nextStubSession++)}`)
|
|
const agent = await isolatedAgentLoop.create(
|
|
id,
|
|
{},
|
|
cwd === undefined ? {} : { cwd },
|
|
)
|
|
const append = agent.session.append.bind(agent.session) as unknown as (
|
|
type: SessionEvent['type'],
|
|
data: SessionEvent['data'],
|
|
opts?: Partial<SurfaceIntent>,
|
|
) => SessionEvent
|
|
for (const event of seed) {
|
|
if ('surfaceOp' in event || 'sourceEventSeqs' in event) {
|
|
append(event.type, event.data, {
|
|
...event.surfaceOp === undefined ? {} : { surfaceOp: event.surfaceOp },
|
|
...event.sourceEventSeqs === undefined ? {} : { sourceEventSeqs: event.sourceEventSeqs },
|
|
})
|
|
} else {
|
|
append(event.type, event.data)
|
|
}
|
|
}
|
|
if (seed.at(-1)?.type !== 'session/end-seed') agent.session.append('session/end-seed', {})
|
|
return agent
|
|
}
|
|
|
|
function stubToolExecution(
|
|
input: Omit<ToolExecution, 'token' | 'rootCallId'> & {
|
|
token?: ToolExecutionToken
|
|
rootCallId?: ToolExecution['rootCallId']
|
|
},
|
|
): ToolExecution {
|
|
return {
|
|
token: input.token ?? Symbol('workspace-context-test-execution') as ToolExecutionToken,
|
|
...input,
|
|
rootCallId: input.rootCallId ?? input.callId,
|
|
}
|
|
}
|
|
|
|
function blocksText(blocks: readonly { type: string; text?: string }[] | undefined): string {
|
|
return blocks?.map(block => block.type === 'text' ? block.text ?? '' : '').join('\n') ?? ''
|
|
}
|
|
|
|
async function agentInstructionsOf(agent: Agent): Promise<UserMessage> {
|
|
return vi.waitFor(() => {
|
|
const context = agent.inbox.nextStep.find(message =>
|
|
message.source.kind === 'agent-instructions')
|
|
expect(context).toBeDefined()
|
|
return context!
|
|
}, { timeout: 10_000 })
|
|
}
|
|
|
|
async function syncAgentInstructions(ctx: Context, agent: Agent): Promise<void> {
|
|
await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step', { modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: testToolSignal },
|
|
async () => ({ kind: 'enter' as const, messages: [] }),
|
|
)
|
|
}
|
|
|
|
async function syncedAgentInstructions(ctx: Context, agent: Agent): Promise<UserMessage> {
|
|
await syncAgentInstructions(ctx, agent)
|
|
return agentInstructionsOf(agent)
|
|
}
|
|
|
|
function baselineEvents(agent: Agent): SessionEvent[] {
|
|
return agent.session.snapshotEvents().filter(event =>
|
|
event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions'
|
|
&& event.data.source.baseline === true)
|
|
}
|
|
|
|
async function appendAdditionalContexts(ctx: Context, agent: TestAgent): Promise<SessionSeq | undefined> {
|
|
await syncedAgentInstructions(ctx, agent)
|
|
let lastSeq: SessionSeq | undefined
|
|
for (const claimed of claimInbox(agent, 'next-step')) {
|
|
if (claimed.source.kind !== 'agent-instructions') continue
|
|
const event = agent.session.append('user/message', claimed, { surfaceOp: 'append' })
|
|
ctx.emit('session/event', agent.session, event)
|
|
lastSeq = event.seq
|
|
}
|
|
return lastSeq
|
|
}
|
|
|
|
const composedPrefixes = new WeakMap<object, Message[]>()
|
|
|
|
async function composeBaselinePrefix(ctx: Context, agent: TestAgent): Promise<Message[]> {
|
|
const signal = new AbortController().signal
|
|
await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: [] }),
|
|
)
|
|
const claimed = claimInbox(agent, 'next-step')
|
|
const decision = await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: claimed, turn: 1, step: 2, signal },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: claimed }),
|
|
)
|
|
const entered = decision.kind === 'enter' ? decision.messages : []
|
|
for (const message of entered) {
|
|
const event = agent.session.append('user/message', message, { surfaceOp: 'append' })
|
|
ctx.emit('session/event', agent.session, event)
|
|
}
|
|
const prefix = agent.session.deriveMessages()
|
|
composedPrefixes.set(agent, prefix)
|
|
return prefix
|
|
}
|
|
|
|
function derivedText(agent: Agent): string {
|
|
return blocksText(composedPrefixes.get(agent)?.[0]?.content)
|
|
}
|
|
|
|
function expectNoDerivedMessages(agent: Agent): void {
|
|
expect(agent.session.deriveMessages()).toEqual([])
|
|
expect(composedPrefixes.get(agent) ?? []).toEqual([])
|
|
}
|
|
|
|
describe('workspace context instruction discovery', () => {
|
|
it('treats ENOTDIR while probing a host candidate as confirmed absence', async () => {
|
|
const root = await tempRepo()
|
|
const homeFile = join(root, 'not-a-directory')
|
|
try {
|
|
await writeFile(homeFile, 'file')
|
|
|
|
pinHarnessHome(homeFile)
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads user-global first, then every root-to-cwd candidate in precedence order', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'packages/app')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), 'global rules')
|
|
await write(join(root, 'AGENTS.md'), 'root agents')
|
|
await write(join(root, 'CLAUDE.md'), 'root claude')
|
|
await write(join(root, 'packages/CLAUDE.md'), 'package claude')
|
|
await write(join(cwd, 'AGENTS.md'), 'app agents')
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({ cwd })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual([
|
|
'$DSH_HOME/AGENTS.md',
|
|
'AGENTS.md',
|
|
'CLAUDE.md',
|
|
join('packages', 'CLAUDE.md'),
|
|
join('packages', 'app', 'AGENTS.md'),
|
|
])
|
|
expect(files.map(file => file.absolutePath)).toContain(join(root, 'CLAUDE.md'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads a same-directory local overlay in addition to the base file by default', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root base')
|
|
await write(join(root, 'AGENTS.local.md'), 'root local')
|
|
await write(join(cwd, 'CLAUDE.md'), 'pkg base')
|
|
await write(join(cwd, 'CLAUDE.local.md'), 'pkg local')
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({ cwd })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual([
|
|
'AGENTS.md',
|
|
'AGENTS.local.md',
|
|
join('pkg', 'CLAUDE.md'),
|
|
join('pkg', 'CLAUDE.local.md'),
|
|
])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads no local overlay when localInstructionFileCandidates is empty', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'base rule')
|
|
await write(join(root, 'AGENTS.local.md'), 'local rule')
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({
|
|
cwd: root,
|
|
localInstructionFileCandidates: [],
|
|
})
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['AGENTS.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('treats a .git file as a project root marker and does not search above it', async () => {
|
|
const outer = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const root = join(outer, 'worktree')
|
|
const cwd = join(root, 'src')
|
|
await write(join(outer, 'AGENTS.md'), 'outer must not load')
|
|
await write(join(root, '.git'), 'gitdir: ../.git/worktrees/worktree')
|
|
await write(join(root, 'AGENTS.md'), 'root')
|
|
await mkdir(cwd, { recursive: true })
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({ cwd })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['AGENTS.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(outer, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('re-reads content after a same-version, same-size rewrite', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await mkdir(cwd, { recursive: true })
|
|
|
|
pinHarnessHome(home)
|
|
expect(await loadBaselineInstructions({ cwd, maxBytes: 65536 })).toBeUndefined()
|
|
|
|
const leaf = join(cwd, 'AGENTS.md')
|
|
await write(leaf, 'first')
|
|
pinHarnessHome(home)
|
|
const first = await loadBaselineInstructions({ cwd, maxBytes: 65536 })
|
|
expect(first?.text).toContain('first')
|
|
pinHarnessHome(home)
|
|
const again = await loadBaselineInstructions({ cwd, maxBytes: 65536 })
|
|
expect(again?.text).toContain('first')
|
|
|
|
const before = await stat(leaf)
|
|
await writeFile(leaf, 'other')
|
|
await utimes(leaf, before.atime, before.mtime)
|
|
pinHarnessHome(home)
|
|
const second = await loadBaselineInstructions({ cwd, maxBytes: 65536 })
|
|
expect(second?.text).toContain('other')
|
|
expect(second?.text).not.toContain('first')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// POSIX-only fixture: chmod 0 cannot make a file unreadable to its owner on Windows.
|
|
it.skipIf(process.platform === 'win32')('skips a file that becomes unreadable after discovery without failing the request', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await mkdir(cwd, { recursive: true })
|
|
const leaf = join(cwd, 'AGENTS.md')
|
|
await write(leaf, 'secret-ish rule')
|
|
await chmod(leaf, 0)
|
|
|
|
pinHarnessHome(home)
|
|
const loaded = await loadBaselineInstructions({ cwd, maxBytes: 65536 })
|
|
|
|
expect(loaded).toBeUndefined()
|
|
await chmod(leaf, 0o600)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('follows a symlinked instruction file to its target content', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const outside = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(outside, 'shared.md'), 'shared instruction body')
|
|
await symlink(join(outside, 'shared.md'), join(root, 'AGENTS.md'))
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
pinHarnessHome(home)
|
|
const loaded = await loadBaselineInstructions({ cwd: root, maxBytes: 65536 })
|
|
|
|
expect(files.map(file => file.displayPath)).toContain('AGENTS.md')
|
|
expect(loaded?.text).toContain('shared instruction body')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(outside, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('follows a symlinked instruction file through ctx.fs to its target content', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const outside = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(outside, 'shared.md'), 'shared provider instruction body')
|
|
await symlink(join(outside, 'shared.md'), join(root, 'AGENTS.md'))
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('shared provider instruction body')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(outside, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('disables baseline loading when the byte budget is zero', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
|
|
pinHarnessHome(home)
|
|
await expect(loadBaselineInstructions({ cwd: root, maxBytes: 0 })).resolves.toBeUndefined()
|
|
pinHarnessHome(home)
|
|
await expect(loadBaselineInstructions({ cwd: root, maxBytes: 65536, maxSourceBytes: 0 })).resolves.toBeUndefined()
|
|
pinHarnessHome(home)
|
|
await expect(loadBaselineInstructions({
|
|
cwd: root, maxBytes: 65536, maxSourceBytes: Infinity,
|
|
})).resolves.toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('honors configured instruction candidates that exclude CLAUDE.md', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'CLAUDE.md'), 'claude only')
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({
|
|
cwd: root,
|
|
instructionFileCandidates: ['AGENTS.md'],
|
|
})
|
|
|
|
expect(files).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads every configured instruction candidate in configured order without hard-coding AGENTS.md priority', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'native rule')
|
|
await write(join(root, 'CLAUDE.local.md'), 'local claude rule')
|
|
await write(join(root, 'CLAUDE.md'), 'claude rule')
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({
|
|
cwd: root,
|
|
instructionFileCandidates: ['CLAUDE.local.md', 'AGENTS.md', 'CLAUDE.md'],
|
|
})
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['CLAUDE.local.md', 'AGENTS.md', 'CLAUDE.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('ignores configured instruction candidates that are not same-directory file names', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'native rule')
|
|
await write(join(root, '.claude/CLAUDE.md'), 'nested claude rule')
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({
|
|
cwd: root,
|
|
instructionFileCandidates: ['', '.', '..', '.claude/CLAUDE.md', 'nested\\CLAUDE.md', 'AGENTS.md'],
|
|
})
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['AGENTS.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('uses the default harness home and cwd itself as root when no project marker exists', async () => {
|
|
const root = await tempRepo()
|
|
const emptyHome = await tempRepo()
|
|
// Isolate the default-home fallback: blank DSH_HOME is treated as unset, and
|
|
// the home dirs point at an empty dir so the default ~/.dsh holds no global
|
|
// scope. Windows homedir() reads USERPROFILE (not HOME), so both must be
|
|
// stubbed or a real ~/.dsh/AGENTS.md would otherwise leak in.
|
|
vi.stubEnv('DSH_HOME', '')
|
|
vi.stubEnv('HOME', emptyHome)
|
|
if (process.platform === 'win32') vi.stubEnv('USERPROFILE', emptyHome)
|
|
try {
|
|
const cwd = join(root, 'child')
|
|
await mkdir(cwd, { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'parent without marker')
|
|
await write(join(cwd, 'AGENTS.md'), 'cwd without marker')
|
|
|
|
const files = await discoverBaselineInstructionFiles({ cwd })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['AGENTS.md'])
|
|
expect(files.map(file => file.absolutePath)).toEqual([join(cwd, 'AGENTS.md')])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(emptyHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('uses an already-resolved home its owner passes instead of the environment', async () => {
|
|
const root = await tempRepo()
|
|
const ownerHome = await tempRepo()
|
|
const envHome = await tempRepo()
|
|
try {
|
|
await write(join(ownerHome, 'AGENTS.md'), 'owner-resolved rule')
|
|
await write(join(envHome, 'AGENTS.md'), 'environment rule')
|
|
pinHarnessHome(envHome)
|
|
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root, dshHome: ownerHome })
|
|
const loaded = await loadBaselineInstructions({ cwd: root, dshHome: ownerHome, maxBytes: 65536 })
|
|
|
|
expect(files).toEqual([{ absolutePath: join(ownerHome, 'AGENTS.md'), displayPath: '$DSH_HOME/AGENTS.md' }])
|
|
expect(loaded?.text).toContain('owner-resolved rule')
|
|
expect(loaded?.text).not.toContain('environment rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(ownerHome, { recursive: true, force: true })
|
|
await rm(envHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('honors DSH_HOME from the process environment', async () => {
|
|
const root = await tempRepo()
|
|
const envHome = await tempRepo()
|
|
try {
|
|
await write(join(envHome, 'AGENTS.md'), 'env global rule')
|
|
vi.stubEnv('DSH_HOME', envHome)
|
|
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files).toEqual([{ absolutePath: join(envHome, 'AGENTS.md'), displayPath: '$DSH_HOME/AGENTS.md' }])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(envHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('honors DSH_AGENTS_HOME from the process environment', async () => {
|
|
const root = await tempRepo()
|
|
const emptyHome = await tempRepo()
|
|
const envAgents = await tempRepo()
|
|
try {
|
|
await write(join(envAgents, 'AGENTS.md'), 'env shared rule')
|
|
vi.stubEnv('DSH_AGENTS_HOME', envAgents)
|
|
|
|
pinHarnessHome(emptyHome)
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files).toEqual([{ absolutePath: join(envAgents, 'AGENTS.md'), displayPath: '$DSH_AGENTS_HOME/AGENTS.md' }])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(emptyHome, { recursive: true, force: true })
|
|
await rm(envAgents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads both user-global roots with the harness home first', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), 'harness home rule')
|
|
await write(join(agents, 'AGENTS.md'), 'shared agents rule')
|
|
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
const loaded = await loadBaselineInstructions({ cwd: root, maxBytes: 65536 })
|
|
const text = loaded?.text ?? ''
|
|
|
|
expect(text).toContain('Instructions from: $DSH_HOME/AGENTS.md\n\nharness home rule')
|
|
expect(text).toContain('Instructions from: $DSH_AGENTS_HOME/AGENTS.md\n\nshared agents rule')
|
|
expect(text.indexOf('harness home rule')).toBeLessThan(text.indexOf('shared agents rule'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('labels the default agents root as ~/.agents when HOME points at the configured default', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await write(join(home, '.agents/AGENTS.md'), 'shared default rule')
|
|
|
|
// A set environment variable would override the homedir default and
|
|
// relabel the root, so both homes must fall back to the stubbed OS home.
|
|
vi.stubEnv('DSH_HOME', '')
|
|
vi.stubEnv('DSH_AGENTS_HOME', '')
|
|
vi.resetModules()
|
|
vi.doMock('node:os', () => ({ homedir: () => home }))
|
|
const isolated = await import('@deepseek-ai/dsh-agent-instructions')
|
|
const files = await isolated.discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['~/.agents/AGENTS.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
vi.doUnmock('node:os')
|
|
vi.resetModules()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('labels the default DSH home as ~/.dsh when HOME points at the configured default', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await write(join(home, '.dsh/AGENTS.md'), 'global default rule')
|
|
|
|
// A set DSH_HOME would override the homedir default and relabel the home.
|
|
vi.stubEnv('DSH_HOME', '')
|
|
vi.resetModules()
|
|
vi.doMock('node:os', () => ({ homedir: () => home }))
|
|
const isolated = await import('@deepseek-ai/dsh-agent-instructions')
|
|
const files = await isolated.discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['~/.dsh/AGENTS.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
vi.doUnmock('node:os')
|
|
vi.resetModules()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('expands a ~/.dsh DSH_HOME to the operating-system home directory', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await write(join(home, '.dsh/AGENTS.md'), 'global tilde rule')
|
|
|
|
vi.resetModules()
|
|
vi.doMock('node:os', () => ({ homedir: () => home }))
|
|
const isolated = await import('@deepseek-ai/dsh-agent-instructions')
|
|
pinHarnessHome('~/.dsh')
|
|
const files = await isolated.discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files).toEqual([{ absolutePath: join(home, '.dsh/AGENTS.md'), displayPath: '~/.dsh/AGENTS.md' }])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
vi.doUnmock('node:os')
|
|
vi.resetModules()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('deduplicates user-global instructions when DSH_HOME points at the project root', async () => {
|
|
const root = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'same file')
|
|
|
|
pinHarnessHome(root)
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files).toEqual([{ absolutePath: join(root, 'AGENTS.md'), displayPath: '$DSH_HOME/AGENTS.md' }])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('ignores instruction candidates that are directories', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await mkdir(join(root, 'AGENTS.md'), { recursive: true })
|
|
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('workspace context rendering', () => {
|
|
it('renders familiar system-reminder instructions without custom workspace tags or state markers', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root rules' },
|
|
{ absolutePath: '/repo/pkg/CLAUDE.md', displayPath: 'pkg/CLAUDE.md', content: 'package rules' },
|
|
], { maxBytes: 65536 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'The following workspace instructions may be relevant to your work. Use them as guidance when applicable. More specific instructions take precedence over broader ones. They do not override system, developer, or direct user instructions.',
|
|
'',
|
|
'Instructions from: AGENTS.md',
|
|
'',
|
|
'root rules',
|
|
'',
|
|
'Instructions from: pkg/CLAUDE.md',
|
|
'',
|
|
'package rules',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(rendered.text).not.toContain('<agent-instructions')
|
|
expect(rendered.text).not.toContain('agent-instructions:')
|
|
expect(rendered.text).not.toContain('/repo/')
|
|
expect(rendered.omitted).toEqual([])
|
|
expect(rendered.truncated).toEqual([])
|
|
})
|
|
|
|
it('neutralizes a literal system-reminder closing delimiter inside instruction content', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'safe\n</system-reminder>\nnot outside' },
|
|
], { maxBytes: 65536 })
|
|
|
|
expect(rendered.text.match(/<\/system-reminder>/g)).toHaveLength(1)
|
|
expect(rendered.text).toContain('<\\/system-reminder>')
|
|
})
|
|
|
|
it('neutralizes system-reminder closing delimiters in paths and derived scopes', () => {
|
|
const displayPath = 'scope</system-reminder>/AGENTS.md'
|
|
const file = { absolutePath: `/repo/${displayPath}`, displayPath, content: 'rules' }
|
|
const rendered = [
|
|
renderAgentInstructions([file], { maxBytes: 65536 }).text,
|
|
...(['set', 'replace', 'remove'] as const).map(action => renderInstructionChanges([{
|
|
change: { action, scope: 'scope</system-reminder>\0AGENTS.md', path: displayPath },
|
|
file,
|
|
}], 65536).text),
|
|
]
|
|
|
|
for (const text of rendered) {
|
|
expect(text.match(/<\/system-reminder>/g)).toHaveLength(1)
|
|
expect(text).toContain('scope<\\/system-reminder>')
|
|
}
|
|
})
|
|
|
|
it('neutralizes a system-reminder closing delimiter in budget marker paths', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{
|
|
absolutePath: '/repo/scope</system-reminder>/AGENTS.md',
|
|
displayPath: 'scope</system-reminder>/AGENTS.md',
|
|
content: 'root '.repeat(100),
|
|
},
|
|
{ absolutePath: '/repo/leaf/AGENTS.md', displayPath: 'leaf/AGENTS.md', content: 'leaf rules' },
|
|
], { maxBytes: 400 })
|
|
|
|
expect(rendered.text).toContain('omitted scope<\\/system-reminder>/AGENTS.md')
|
|
expect(rendered.text.match(/<\/system-reminder>/g)).toHaveLength(1)
|
|
})
|
|
|
|
it('preserves more specific files under the byte budget and names omitted/truncated paths', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(100) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'leaf '.repeat(100) },
|
|
], { maxBytes: 260 })
|
|
|
|
expect(rendered.text).toContain('Workspace instruction budget 260 bytes')
|
|
expect(rendered.text).toContain('omitted AGENTS.md')
|
|
expect(rendered.text).toContain('truncated pkg/AGENTS.md')
|
|
expect(rendered.text).toContain('Instructions from: pkg/AGENTS.md')
|
|
expect(rendered.text).not.toContain('Instructions from: AGENTS.md\n\nroot')
|
|
expect(rendered.omitted.map(item => item.displayPath)).toEqual(['AGENTS.md'])
|
|
expect(rendered.truncated.map(item => item.displayPath)).toEqual(['pkg/AGENTS.md'])
|
|
})
|
|
|
|
it('keeps the rendered block within the byte budget when files are both omitted and truncated', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(100) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'leaf '.repeat(100) },
|
|
], { maxBytes: 260 })
|
|
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(260)
|
|
expect(rendered.text).not.toContain(':;')
|
|
expect(rendered.omitted.map(item => item.displayPath)).toEqual(['AGENTS.md'])
|
|
expect(rendered.truncated.map(item => item.displayPath)).toEqual(['pkg/AGENTS.md'])
|
|
})
|
|
|
|
it('drops a parent file while keeping a specific child file intact when the child fits', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(200) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'leaf rule' },
|
|
], { maxBytes: 700 })
|
|
|
|
expect(rendered.text).toContain('omitted AGENTS.md')
|
|
expect(rendered.text).toContain('Instructions from: pkg/AGENTS.md\n\nleaf rule')
|
|
expect(rendered.text).not.toContain('root root')
|
|
expect(rendered.omitted.map(item => item.displayPath)).toEqual(['AGENTS.md'])
|
|
expect(rendered.truncated).toEqual([])
|
|
})
|
|
|
|
it('keeps the longest most-specific suffix that fits under the byte budget', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(200) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'package rule' },
|
|
{ absolutePath: '/repo/pkg/app/AGENTS.md', displayPath: 'pkg/app/AGENTS.md', content: 'app rule' },
|
|
], { maxBytes: 760 })
|
|
|
|
expect(rendered.text).toContain('omitted AGENTS.md')
|
|
expect(rendered.text).toContain('Instructions from: pkg/AGENTS.md\n\npackage rule')
|
|
expect(rendered.text).toContain('Instructions from: pkg/app/AGENTS.md\n\napp rule')
|
|
expect(rendered.text).not.toContain('root root')
|
|
expect(rendered.omitted.map(item => item.displayPath)).toEqual(['AGENTS.md'])
|
|
expect(rendered.truncated).toEqual([])
|
|
})
|
|
|
|
it('truncates a single oversized file to the largest content slice that fits', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'x'.repeat(1000) },
|
|
], { maxBytes: 700 })
|
|
|
|
expect(rendered.text).toContain('truncated AGENTS.md')
|
|
expect(rendered.text).toContain('Instructions from: AGENTS.md')
|
|
expect(rendered.truncated).toHaveLength(1)
|
|
expect(rendered.truncated[0]?.originalBytes).toBe(1000)
|
|
expect(rendered.truncated[0]!.includedBytes).toBeGreaterThan(0)
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(700)
|
|
})
|
|
|
|
it('omits all text when the render budget is disabled', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root rules' },
|
|
], { maxBytes: 0 })
|
|
|
|
expect(rendered).toEqual({
|
|
text: '',
|
|
omitted: [{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root rules' }],
|
|
truncated: [],
|
|
})
|
|
})
|
|
|
|
it('falls back to a compact truncation notice when even the empty heading cannot fit', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'x'.repeat(1000) },
|
|
], { maxBytes: 100 })
|
|
|
|
expect(rendered.text).toBe('Workspace instruction budget 100 bytes: truncated pkg/AGENTS.md from 1000 to 0 bytes')
|
|
expect(rendered.truncated).toEqual([{ displayPath: 'pkg/AGENTS.md', originalBytes: 1000, includedBytes: 0 }])
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(100)
|
|
})
|
|
|
|
it('keeps the empty instruction heading when it fits beside the compact notice', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'x'.repeat(1000) },
|
|
], { maxBytes: 120 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'Workspace instruction budget 120 bytes: truncated pkg/AGENTS.md from 1000 to 0 bytes',
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBe(120)
|
|
})
|
|
|
|
it('represents a genuinely empty instruction when its compact heading fits', () => {
|
|
const file = { absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: '' }
|
|
const rendered = renderAgentInstructionSet([file], { maxBytes: 117 })
|
|
|
|
expect(rendered.rendered.text).toContain('truncated pkg/AGENTS.md from 0 to 0 bytes')
|
|
expect(rendered.rendered.text).toContain('Instructions from: pkg/AGENTS.md')
|
|
expect(rendered.included).toEqual([file])
|
|
})
|
|
|
|
it('represents a genuinely empty instruction through the framed compact-intro path', () => {
|
|
const file = {
|
|
absolutePath: '/repo/pkg/AGENTS.md',
|
|
displayPath: 'pkg/AGENTS.md',
|
|
content: '',
|
|
}
|
|
|
|
const rendered = renderAgentInstructionSet([file], { maxBytes: 300 })
|
|
|
|
expect(rendered.rendered.text).toContain('<system-reminder>')
|
|
expect(rendered.rendered.text).toContain('Workspace instructions were omitted or truncated')
|
|
expect(rendered.rendered.text).toContain('Instructions from: pkg/AGENTS.md')
|
|
expect(rendered.rendered.truncated).toEqual([
|
|
{ displayPath: 'pkg/AGENTS.md', originalBytes: 0, includedBytes: 0 },
|
|
])
|
|
expect(rendered.included).toEqual([file])
|
|
})
|
|
|
|
it('truncates the compact notice itself when the render budget is smaller than the notice', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'x'.repeat(1000) },
|
|
], { maxBytes: 20 })
|
|
|
|
expect(rendered.text).toBe('Workspace instructio')
|
|
expect(rendered.truncated).toEqual([{ displayPath: 'pkg/AGENTS.md', originalBytes: 1000, includedBytes: 0 }])
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBe(20)
|
|
})
|
|
|
|
it('does not commit a change when only the generic compact notice survives', () => {
|
|
const change = {
|
|
action: 'set' as const,
|
|
scope: sk('pkg', 'AGENTS.md'),
|
|
path: 'pkg/AGENTS.md',
|
|
digest: 'digest',
|
|
}
|
|
const rendered = renderInstructionChanges([{
|
|
change,
|
|
file: { absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'x'.repeat(1000) },
|
|
}], 20)
|
|
|
|
expect(rendered.text).toBe('Workspace instructio')
|
|
expect(rendered.changes).toEqual([])
|
|
})
|
|
|
|
it('commits a change when its file-specific semantic section survives truncation', () => {
|
|
const change = {
|
|
action: 'replace' as const,
|
|
scope: sk('pkg', 'AGENTS.md'),
|
|
path: 'pkg/AGENTS.md',
|
|
digest: 'digest',
|
|
}
|
|
const rendered = renderInstructionChanges([{
|
|
change,
|
|
file: { absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'x'.repeat(1000) },
|
|
}], 400)
|
|
|
|
expect(rendered.text).toContain('Updated instructions from: pkg/AGENTS.md')
|
|
expect(rendered.changes).toEqual([change])
|
|
})
|
|
|
|
// Each prose-derived budget is the smallest current value that retains the named heading plus a zero-byte marker.
|
|
it.each([
|
|
{ action: 'set' as const, maxBytes: 327, heading: 'Additional instructions from:' },
|
|
{ action: 'replace' as const, maxBytes: 256, heading: 'Updated instructions from:' },
|
|
])('does not commit a $action change when its heading survives with zero content bytes', ({ action, maxBytes, heading }) => {
|
|
const change = {
|
|
action,
|
|
scope: sk('pkg', 'AGENTS.md'),
|
|
path: 'pkg/AGENTS.md',
|
|
digest: 'digest',
|
|
}
|
|
const rendered = renderInstructionChanges([{
|
|
change,
|
|
file: { absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'x'.repeat(1000) },
|
|
}], maxBytes)
|
|
|
|
expect(rendered.text).toContain(heading)
|
|
expect(rendered.text).toContain('from 1000 to 0 bytes')
|
|
expect(rendered.changes).toEqual([])
|
|
})
|
|
|
|
it('does not commit a multibyte change when the budget cuts its first code point', () => {
|
|
const change = {
|
|
action: 'set' as const,
|
|
scope: sk('pkg', 'AGENTS.md'),
|
|
path: 'pkg/AGENTS.md',
|
|
digest: 'digest',
|
|
}
|
|
const rendered = renderInstructionChanges([{
|
|
change,
|
|
file: { absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: '😀'.repeat(100) },
|
|
}], 366)
|
|
|
|
expect(rendered.text).not.toContain('�')
|
|
expect(rendered.text).not.toContain('😀')
|
|
expect(rendered.changes).toEqual([])
|
|
})
|
|
|
|
it('keeps compact truncation notices within budget when a multibyte display path is cut', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/repo/路径/AGENTS.md', displayPath: '路径/AGENTS.md', content: 'x'.repeat(1000) },
|
|
], { maxBytes: 51 })
|
|
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(51)
|
|
})
|
|
|
|
it('renders a user-global file and project files that fit one budget as one unmarked chain', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'Prefer pnpm.' },
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root rules' },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'package rules' },
|
|
], { maxBytes: 65536 })
|
|
|
|
expect(rendered).toEqual({
|
|
text: [
|
|
'<system-reminder>',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'Prefer pnpm.',
|
|
'',
|
|
'Instructions from: AGENTS.md',
|
|
'',
|
|
'root rules',
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'package rules',
|
|
'</system-reminder>',
|
|
].join('\n'),
|
|
omitted: [],
|
|
truncated: [],
|
|
})
|
|
})
|
|
|
|
it.each([
|
|
{
|
|
layout: '20/30/20 KiB',
|
|
files: [kibInstruction('~/.dsh/AGENTS.md', 20), kibInstruction('AGENTS.md', 30), kibInstruction('pkg/AGENTS.md', 20)],
|
|
},
|
|
{ layout: '40/30 KiB', files: [kibInstruction('~/.dsh/AGENTS.md', 40), kibInstruction('AGENTS.md', 30)] },
|
|
{ layout: '63.5/8 KiB', files: [kibInstruction('~/.dsh/AGENTS.md', 63.5), kibInstruction('AGENTS.md', 8)] },
|
|
])('keeps every file of the $layout layout when each chain fits its own budget', ({ files }) => {
|
|
const { rendered, included } = renderAgentInstructionSet(files, { maxBytes: 65536 })
|
|
|
|
expect(rendered.omitted).toEqual([])
|
|
expect(rendered.truncated).toEqual([])
|
|
expect(included).toEqual(files)
|
|
expect(rendered.text).toBe(renderAgentInstructions(files, { maxBytes: 1_048_576 }).text)
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeGreaterThan(65536)
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 65536)
|
|
})
|
|
|
|
it('omits the harness-home file before the shared-root file when only the user-global chain exceeds its budget', () => {
|
|
const files = [
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'home '.repeat(100) },
|
|
{ absolutePath: '/home/user/.agents/AGENTS.md', displayPath: '~/.agents/AGENTS.md', content: 'shared rule' },
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(40) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'package rule' },
|
|
]
|
|
|
|
const { rendered, included } = renderAgentInstructionSet(files, { maxBytes: 600 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 600 bytes: omitted ~/.dsh/AGENTS.md',
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.agents/AGENTS.md',
|
|
'',
|
|
'shared rule',
|
|
'',
|
|
'Instructions from: AGENTS.md',
|
|
'',
|
|
'root '.repeat(40),
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'package rule',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(rendered.omitted).toEqual([{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md' }])
|
|
expect(rendered.truncated).toEqual([])
|
|
expect(included).toEqual(files.slice(1))
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeGreaterThan(600)
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 600)
|
|
})
|
|
|
|
it('truncates an oversized user-global file within its own budget while the project chain renders whole', () => {
|
|
const userGlobal = kibInstruction('~/.dsh/AGENTS.md', 70)
|
|
const files = [
|
|
userGlobal,
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root rule' },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'package rule' },
|
|
]
|
|
const truncated = [{ displayPath: '~/.dsh/AGENTS.md', originalBytes: 71680, includedBytes: 65129 }]
|
|
|
|
const { rendered, included } = renderAgentInstructionSet(files, { maxBytes: 65536 })
|
|
|
|
expect(rendered).toEqual({
|
|
text: [
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 65536 bytes: truncated ~/.dsh/AGENTS.md from 71680 to 65129 bytes',
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
userGlobal.content.slice(0, 65129),
|
|
'',
|
|
'Instructions from: AGENTS.md',
|
|
'',
|
|
'root rule',
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'package rule',
|
|
'</system-reminder>',
|
|
].join('\n'),
|
|
omitted: [],
|
|
truncated,
|
|
})
|
|
expect(included).toEqual(files)
|
|
expect(renderAgentInstructions([userGlobal], { maxBytes: 65536 }).truncated).toEqual(truncated)
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 65536)
|
|
})
|
|
|
|
it('records the same omission and truncation for an over-budget project chain as rendering it alone', () => {
|
|
const project = [
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(200) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'leaf '.repeat(100) },
|
|
]
|
|
const projectAlone = renderAgentInstructions(project, { maxBytes: 600 })
|
|
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'Prefer pnpm.' },
|
|
...project,
|
|
], { maxBytes: 600 })
|
|
|
|
expect(projectAlone.omitted.map(file => file.displayPath)).toEqual(['AGENTS.md'])
|
|
expect(rendered.omitted).toEqual(projectAlone.omitted)
|
|
expect(rendered.truncated).toEqual(projectAlone.truncated)
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 600 bytes: omitted AGENTS.md; truncated pkg/AGENTS.md from 500 to 186 bytes',
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'Prefer pnpm.',
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'leaf '.repeat(100).slice(0, 186),
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 600)
|
|
})
|
|
|
|
it('keeps a user-global file that fills its budget exactly while the project chain fits only its notice and empty heading', () => {
|
|
const userGlobal = { absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'Use pnpm.' }
|
|
const project = [
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'r'.repeat(1000) },
|
|
{ absolutePath: '/repo/packages/app/AGENTS.md', displayPath: 'packages/app/AGENTS.md', content: 'a'.repeat(1000) },
|
|
{
|
|
absolutePath: '/repo/packages/app/src/feature/AGENTS.md',
|
|
displayPath: 'packages/app/src/feature/AGENTS.md',
|
|
content: 'f'.repeat(1000),
|
|
},
|
|
]
|
|
expect(Buffer.byteLength(renderAgentInstructions([userGlobal], { maxBytes: 320 }).text, 'utf8')).toBe(320)
|
|
expect(renderAgentInstructions(project, { maxBytes: 320 }).text).toMatch(/^Workspace instruction budget 320 bytes: /)
|
|
|
|
const { rendered, included } = renderAgentInstructionSet([userGlobal, ...project], { maxBytes: 320 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 320 bytes: omitted AGENTS.md, packages/app/AGENTS.md; '
|
|
+ 'truncated packages/app/src/feature/AGENTS.md from 1000 to 0 bytes',
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'Use pnpm.',
|
|
'',
|
|
'Instructions from: packages/app/src/feature/AGENTS.md',
|
|
'',
|
|
'',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(rendered.text.split(baselineIntro)).toHaveLength(2)
|
|
expect(included).toEqual([userGlobal])
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 320)
|
|
})
|
|
|
|
it('keeps a user-global file that fills its budget exactly while the project chain fits only its bare notice', () => {
|
|
const userGlobal = { absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(5) }
|
|
expect(Buffer.byteLength(renderAgentInstructions([userGlobal], { maxBytes: 316 }).text, 'utf8')).toBe(316)
|
|
expect(renderAgentInstructions(deepProjectFiles, { maxBytes: 316 }).text).toBe(deepProjectMarker(316))
|
|
|
|
const { rendered, included } = renderAgentInstructionSet([userGlobal, ...deepProjectFiles], { maxBytes: 316 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
deepProjectMarker(316),
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'g'.repeat(5),
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(included).toEqual([userGlobal])
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 316)
|
|
})
|
|
|
|
it('merges a frameless project chain at exactly twice the budget and falls back to one budget one byte above', () => {
|
|
const userGlobal = (bytes: number): { absolutePath: string; displayPath: string; content: string } => (
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(bytes) }
|
|
)
|
|
const deepestHeading = 'Instructions from: aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/AGENTS.md'
|
|
expect(renderAgentInstructions(deepProjectFiles, { maxBytes: 318 }).text).toBe(`${deepProjectMarker(318)}\n\n${deepestHeading}\n\n`)
|
|
expect(renderAgentInstructions([userGlobal(6)], { maxBytes: 318 })).toMatchObject({ omitted: [], truncated: [] })
|
|
|
|
const atBound = renderAgentInstructionSet([userGlobal(5), ...deepProjectFiles], { maxBytes: 318 })
|
|
const overBound = renderAgentInstructionSet([userGlobal(6), ...deepProjectFiles], { maxBytes: 318 })
|
|
|
|
expect(atBound.rendered.text).toBe([
|
|
'<system-reminder>',
|
|
deepProjectMarker(318),
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'g'.repeat(5),
|
|
'',
|
|
deepestHeading,
|
|
'',
|
|
'',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(atBound.rendered.text, 'utf8')).toBe(2 * 318)
|
|
expect(atBound.included).toEqual([userGlobal(5)])
|
|
expect(overBound.rendered.text).toBe('Workspace instruction budget 318 bytes: omitted ~/.dsh/AGENTS.md, AGENTS.md, aaaaaaaaaa/AGENTS.md, '
|
|
+ 'aaaaaaaaaa/bbbbbbbbbb/AGENTS.md, aaaaaaaaaa/bbbbbbbbbb/cccccccccc/AGENTS.md; '
|
|
+ 'truncated aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/AGENTS.md from 5000 to 0 bytes')
|
|
expect(overBound.rendered.omitted[0]).toEqual({ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md' })
|
|
expect(overBound.included).toEqual([])
|
|
})
|
|
|
|
it('keeps both chains whole when each fills its budget exactly and truncates both one byte lower', () => {
|
|
const files = [
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(289) },
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'p'.repeat(296) },
|
|
]
|
|
for (const file of files) {
|
|
expect(Buffer.byteLength(renderAgentInstructions([file], { maxBytes: 600 }).text, 'utf8')).toBe(600)
|
|
}
|
|
|
|
const exact = renderAgentInstructionSet(files, { maxBytes: 600 })
|
|
const below = renderAgentInstructionSet(files, { maxBytes: 599 })
|
|
|
|
expect(exact.rendered).toEqual({ text: renderAgentInstructions(files, { maxBytes: 928 }).text, omitted: [], truncated: [] })
|
|
expect(Buffer.byteLength(exact.rendered.text, 'utf8')).toBe(928)
|
|
expect(exact.included).toEqual(files)
|
|
expect(below.rendered.text.split('\n')[1]).toBe(
|
|
'Workspace instruction budget 599 bytes: truncated ~/.dsh/AGENTS.md from 289 to 198 bytes, AGENTS.md from 296 to 212 bytes',
|
|
)
|
|
expect(below.rendered.omitted).toEqual([])
|
|
expect(below.included).toEqual(files)
|
|
expect(Buffer.byteLength(below.rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 599)
|
|
})
|
|
|
|
it('frames both chains under the compact intro when neither chain fits the full intro', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(1000) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'p'.repeat(1000) },
|
|
], { maxBytes: 300 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 300 bytes: truncated ~/.dsh/AGENTS.md from 1000 to 51 bytes, pkg/AGENTS.md from 1000 to 57 bytes',
|
|
'',
|
|
'Workspace instructions were omitted or truncated to fit the configured byte budget.',
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'g'.repeat(51),
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'p'.repeat(57),
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 300)
|
|
})
|
|
|
|
it('frames the budget notices of both chains without an intro when neither chain fits a frame of its own', () => {
|
|
const { rendered, included } = renderAgentInstructionSet([
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(1000) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'p'.repeat(1000) },
|
|
], { maxBytes: 150 })
|
|
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 150 bytes: truncated ~/.dsh/AGENTS.md from 1000 to 0 bytes, pkg/AGENTS.md from 1000 to 0 bytes',
|
|
'',
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'',
|
|
'',
|
|
'Instructions from: pkg/AGENTS.md',
|
|
'',
|
|
'',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(included).toEqual([])
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 150)
|
|
})
|
|
|
|
it('falls back to the single-budget notice when the merged chain notices would exceed twice the budget', () => {
|
|
const rendered = renderAgentInstructions([
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(1000) },
|
|
{ absolutePath: '/repo/pkg/AGENTS.md', displayPath: 'pkg/AGENTS.md', content: 'p'.repeat(1000) },
|
|
], { maxBytes: 50 })
|
|
|
|
expect(rendered).toEqual({
|
|
text: 'Workspace instruction budget 50 bytes: omitted ~/.',
|
|
omitted: [{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md' }],
|
|
truncated: [{ displayPath: 'pkg/AGENTS.md', originalBytes: 1000, includedBytes: 0 }],
|
|
})
|
|
})
|
|
|
|
it('keeps every two-chain baseline and change batch within twice the budget at every limit', () => {
|
|
const files = [
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'home '.repeat(200) },
|
|
{ absolutePath: '/home/user/.agents/AGENTS.md', displayPath: '~/.agents/AGENTS.md', content: 'shared '.repeat(30) },
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root '.repeat(200) },
|
|
{ absolutePath: '/repo/packages/app/AGENTS.md', displayPath: 'packages/app/AGENTS.md', content: 'app '.repeat(100) },
|
|
]
|
|
const actions = ['set', 'replace', 'set', 'remove'] as const
|
|
const items = files.map((file, index) => ({
|
|
change: { action: actions[index]!, scope: file.displayPath, path: file.displayPath, digest: 'digest' },
|
|
file,
|
|
}))
|
|
|
|
for (let maxBytes = 1; maxBytes <= 3000; maxBytes += 1) {
|
|
expect(Buffer.byteLength(renderAgentInstructions(files, { maxBytes }).text, 'utf8')).toBeLessThanOrEqual(2 * maxBytes)
|
|
expect(Buffer.byteLength(renderInstructionChanges(items, maxBytes).text, 'utf8')).toBeLessThanOrEqual(2 * maxBytes)
|
|
}
|
|
})
|
|
|
|
it('truncates multibyte content in both chains on code point boundaries near the limit', () => {
|
|
const files = [
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: '规'.repeat(400) },
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: '😀'.repeat(300) },
|
|
]
|
|
|
|
const rendered = renderAgentInstructions(files, { maxBytes: 700 })
|
|
|
|
expect(rendered.truncated).toEqual([
|
|
{ displayPath: '~/.dsh/AGENTS.md', originalBytes: 1200, includedBytes: 297 },
|
|
{ displayPath: 'AGENTS.md', originalBytes: 1200, includedBytes: 312 },
|
|
])
|
|
expect(rendered.text).toContain([
|
|
'Instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
'规'.repeat(99),
|
|
'',
|
|
'Instructions from: AGENTS.md',
|
|
'',
|
|
'😀'.repeat(78),
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
for (let maxBytes = 640; maxBytes <= 720; maxBytes += 1) {
|
|
const text = renderAgentInstructions(files, { maxBytes }).text
|
|
expect(text).not.toContain('�')
|
|
expect(Buffer.byteLength(text, 'utf8')).toBeLessThanOrEqual(2 * maxBytes)
|
|
}
|
|
})
|
|
|
|
it.each([0, -1, Number.NaN, Number.POSITIVE_INFINITY])('renders nothing from either chain when the budget %s disables loading', (maxBytes) => {
|
|
const files = [
|
|
{ absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'global rule' },
|
|
{ absolutePath: '/repo/AGENTS.md', displayPath: 'AGENTS.md', content: 'root rule' },
|
|
]
|
|
const items = files.map(file => ({
|
|
change: { action: 'set' as const, scope: file.displayPath, path: file.displayPath, digest: 'digest' },
|
|
file,
|
|
}))
|
|
|
|
expect(renderAgentInstructions(files, { maxBytes })).toEqual({ text: '', omitted: files, truncated: [] })
|
|
expect(renderInstructionChanges(items, maxBytes)).toEqual({ text: '', changes: [] })
|
|
})
|
|
|
|
it('retains a user-global change beside project changes when each chain fits its own budget', () => {
|
|
const items = [
|
|
{
|
|
change: { action: 'set' as const, scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '~/.dsh/AGENTS.md', digest: 'global' },
|
|
file: { absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'Prefer pnpm.' },
|
|
},
|
|
{
|
|
change: { action: 'set' as const, scope: sk('a', 'AGENTS.md'), path: 'a/AGENTS.md', digest: 'a' },
|
|
file: { absolutePath: '/repo/a/AGENTS.md', displayPath: 'a/AGENTS.md', content: 'Rule a.' },
|
|
},
|
|
{
|
|
change: { action: 'set' as const, scope: sk('b', 'AGENTS.md'), path: 'b/AGENTS.md', digest: 'b' },
|
|
file: { absolutePath: '/repo/b/AGENTS.md', displayPath: 'b/AGENTS.md', content: 'Rule b.' },
|
|
},
|
|
]
|
|
const rendered = renderInstructionChanges(items, 560)
|
|
|
|
expect(rendered.changes).toEqual(items.map(item => item.change))
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Additional instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
`These user-global instructions apply to all work. ${additionalGuidance}`,
|
|
'',
|
|
'Prefer pnpm.',
|
|
'',
|
|
'Additional instructions from: a/AGENTS.md',
|
|
'',
|
|
`These instructions apply to work under \`a\`. ${additionalGuidance}`,
|
|
'',
|
|
'Rule a.',
|
|
'',
|
|
'Additional instructions from: b/AGENTS.md',
|
|
'',
|
|
`These instructions apply to work under \`b\`. ${additionalGuidance}`,
|
|
'',
|
|
'Rule b.',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeGreaterThan(560)
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 560)
|
|
})
|
|
|
|
it('drops a user-global change that cannot fit its own budget while retaining the project changes', () => {
|
|
const items = [
|
|
{
|
|
change: { action: 'set' as const, scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '~/.dsh/AGENTS.md', digest: 'global' },
|
|
file: { absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(1000) },
|
|
},
|
|
{
|
|
change: { action: 'replace' as const, scope: sk('a', 'AGENTS.md'), path: 'a/AGENTS.md', digest: 'a' },
|
|
file: { absolutePath: '/repo/a/AGENTS.md', displayPath: 'a/AGENTS.md', content: 'Rule a.' },
|
|
},
|
|
{
|
|
change: { action: 'remove' as const, scope: sk('b', 'AGENTS.md'), path: 'b/AGENTS.md' },
|
|
file: { absolutePath: 'removed:b/AGENTS.md', displayPath: 'b/AGENTS.md', content: '' },
|
|
},
|
|
]
|
|
|
|
const rendered = renderInstructionChanges(items, 330)
|
|
|
|
expect(rendered.changes).toEqual([items[1]!.change, items[2]!.change])
|
|
expect(rendered.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 330 bytes: truncated ~/.dsh/AGENTS.md from 1000 to 0 bytes',
|
|
'',
|
|
'Updated instructions from: a/AGENTS.md',
|
|
'',
|
|
'This file changed after it was loaded. Use the following content instead of the previously loaded instructions from this file.',
|
|
'',
|
|
'Rule a.',
|
|
'',
|
|
'Instructions removed: b/AGENTS.md',
|
|
'',
|
|
'The previously loaded instructions from this file no longer apply.',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(rendered.text, 'utf8')).toBeLessThanOrEqual(2 * 330)
|
|
})
|
|
|
|
it('keeps the loaded order of a change batch whose project item precedes its user-global item', () => {
|
|
const items = (bytes: number): ChangeRenderItem[] => [
|
|
{
|
|
change: { action: 'set', scope: sk('a', 'AGENTS.md'), path: 'a/AGENTS.md', digest: 'a' },
|
|
file: { absolutePath: '/repo/a/AGENTS.md', displayPath: 'a/AGENTS.md', content: 'a'.repeat(bytes) },
|
|
},
|
|
{
|
|
change: { action: 'set', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '~/.dsh/AGENTS.md', digest: 'global' },
|
|
file: { absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(bytes) },
|
|
},
|
|
]
|
|
const batchText = (marker: string[], projectBytes: number, userGlobalBytes: number): string => [
|
|
'<system-reminder>',
|
|
...marker,
|
|
'Additional instructions from: a/AGENTS.md',
|
|
'',
|
|
`These instructions apply to work under \`a\`. ${additionalGuidance}`,
|
|
'',
|
|
'a'.repeat(projectBytes),
|
|
'',
|
|
'Additional instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
`These user-global instructions apply to all work. ${additionalGuidance}`,
|
|
'',
|
|
'g'.repeat(userGlobalBytes),
|
|
'</system-reminder>',
|
|
].join('\n')
|
|
const small = items(10)
|
|
const large = items(1000)
|
|
|
|
const fits = renderInstructionChanges(small, 65536)
|
|
const split = renderInstructionChanges(small, 400)
|
|
const truncated = renderInstructionChanges(large, 400)
|
|
|
|
expect(fits).toEqual({ text: batchText([], 10, 10), changes: small.map(item => item.change) })
|
|
expect(split).toEqual(fits)
|
|
expect(Buffer.byteLength(split.text, 'utf8')).toBeGreaterThan(400)
|
|
expect(truncated).toEqual({
|
|
text: batchText(['Workspace instruction budget 400 bytes: truncated a/AGENTS.md from 1000 to 41 bytes, ~/.dsh/AGENTS.md from 1000 to 25 bytes', ''], 41, 25),
|
|
changes: large.map(item => item.change),
|
|
})
|
|
})
|
|
|
|
it('retains a user-global change beside a frameless project batch at exactly twice the budget and drops it one byte above', () => {
|
|
const items = (globalBytes: number): ChangeRenderItem[] => [
|
|
{
|
|
change: { action: 'set', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '~/.dsh/AGENTS.md', digest: 'global' },
|
|
file: { absolutePath: '/home/user/.dsh/AGENTS.md', displayPath: '~/.dsh/AGENTS.md', content: 'g'.repeat(globalBytes) },
|
|
},
|
|
{
|
|
change: { action: 'set', scope: sk('a', 'AGENTS.md'), path: 'a/AGENTS.md', digest: 'a' },
|
|
file: { absolutePath: '/repo/a/AGENTS.md', displayPath: 'a/AGENTS.md', content: 'p'.repeat(5000) },
|
|
},
|
|
]
|
|
const atBoundItems = items(34)
|
|
expect(renderInstructionChanges(items(35).slice(0, 1), 321).changes).toHaveLength(1)
|
|
|
|
const atBound = renderInstructionChanges(atBoundItems, 321)
|
|
const overBound = renderInstructionChanges(items(35), 321)
|
|
|
|
expect(atBound.changes).toEqual([atBoundItems[0]!.change])
|
|
expect(atBound.text).toBe([
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 321 bytes: truncated a/AGENTS.md from 5000 to 0 bytes',
|
|
'',
|
|
'Additional instructions from: ~/.dsh/AGENTS.md',
|
|
'',
|
|
`These user-global instructions apply to all work. ${additionalGuidance}`,
|
|
'',
|
|
'g'.repeat(34),
|
|
'',
|
|
'Additional instructions from: a/AGENTS.md',
|
|
'',
|
|
`These instructions apply to work under \`a\`. ${additionalGuidance}`,
|
|
'',
|
|
'',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
expect(Buffer.byteLength(atBound.text, 'utf8')).toBe(2 * 321)
|
|
expect(overBound).toEqual({
|
|
text: 'Workspace instruction budget 321 bytes: omitted ~/.dsh/AGENTS.md; truncated a/AGENTS.md from 5000 to 0 bytes',
|
|
changes: [],
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('workspace context request injection', () => {
|
|
it('ignores a stale row home in both baseline loading and refresh', async () => {
|
|
const root = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
const processHome = join(root, 'process-home')
|
|
const rowHome = join(root, 'row-home')
|
|
await mkdir(join(root, '.git'))
|
|
await write(join(processHome, 'AGENTS.md'), 'process home instruction')
|
|
await write(join(rowHome, 'AGENTS.md'), 'stale row instruction')
|
|
pinHarnessHome(processHome)
|
|
// Schemastery retains unknown row keys, so the actual plugin must ignore them.
|
|
const rowConfig = { maxBytes: 65536, dshHome: rowHome, agentsHome: rowHome }
|
|
await mountAgentInstructions(ctx, rowConfig)
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('process home instruction')
|
|
expect(derivedText(agent)).not.toContain('stale row instruction')
|
|
|
|
await write(join(processHome, 'AGENTS.md'), 'updated process home instruction')
|
|
await write(join(rowHome, 'AGENTS.md'), 'updated stale row instruction')
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const pending = await agentInstructionsOf(agent)
|
|
expect(pending.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [{ action: 'replace', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
expect(blocksText(pending.content)).toContain('updated process home instruction')
|
|
expect(blocksText(pending.content)).not.toContain('stale row instruction')
|
|
} finally {
|
|
await ctx.fiber.dispose()
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('requires an explicit maxBytes configuration', async () => {
|
|
const ctx = new Context()
|
|
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await expect(mountAgentInstructionsPlugin(ctx, {} as AgentInstructionsConfig)).rejects.toThrow(/maxBytes/)
|
|
})
|
|
|
|
it('mounts without requiring a filesystem provider', async () => {
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
} finally {
|
|
await ctx.fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('requires agents without making the optional filesystem a static dependency', () => {
|
|
expect(AgentInstructions.inject).toEqual(['agents', 'workingDirectory'])
|
|
})
|
|
|
|
it('does not inject baseline context when no filesystem provider is present', async () => {
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent('/virtual/repo')
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
await ctx.fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('contributes baseline instructions through durable injected history', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(baselineEvents(agent)).toHaveLength(1)
|
|
expect(baselineEvents(agent)[0]).toMatchObject({
|
|
type: 'user/message',
|
|
data: {
|
|
role: 'user',
|
|
source: {
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
baseline: true,
|
|
changes: [{ action: 'set', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
},
|
|
},
|
|
})
|
|
const baseline = baselineEvents(agent)[0]
|
|
expect(baseline?.type === 'user/message' && Array.isArray(baseline.data.content)).toBe(true)
|
|
expect(composedPrefixes.get(agent)).toHaveLength(1)
|
|
expect(derivedText(agent)).toContain('<system-reminder>')
|
|
expect(derivedText(agent)).toContain('Instructions from: AGENTS.md')
|
|
expect(derivedText(agent)).toContain('repo rule')
|
|
expect(derivedText(agent)).not.toContain('<context source=')
|
|
expect(derivedText(agent)).not.toContain('<agent-instructions')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('queues and later commits one durable baseline contribution', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const first = await composeBaselinePrefix(ctx, agent)
|
|
const second = await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(second).toEqual(first)
|
|
expect(agent.session.snapshotEvents().filter(event => event.type === 'user/message' && event.data.source.kind !== 'user')).toHaveLength(1)
|
|
expect(derivedText(agent)).toContain('repo rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('retains one visible baseline across repeated session resumes', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
const firstResume = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, firstResume)
|
|
const secondResume = await stubAgent(root, firstResume.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, secondResume)
|
|
|
|
expect(baselineEvents(firstResume)).toHaveLength(1)
|
|
expect(baselineEvents(secondResume)).toHaveLength(1)
|
|
expect(secondResume.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('preserves a visible baseline when its source is unavailable during resume', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'repo rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
fs.throwOnStat.add(join(root, 'AGENTS.md'))
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, resumed)
|
|
|
|
expect(baselineEvents(resumed)).toHaveLength(1)
|
|
expect(resumed.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not promote an unchanged budget-omitted baseline file during resume', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root '.repeat(200))
|
|
await write(join(cwd, 'AGENTS.md'), 'package rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 700 })
|
|
const original = await stubAgent(cwd)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
const firstResume = await stubAgent(cwd, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, firstResume)
|
|
const secondResume = await stubAgent(cwd, firstResume.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, secondResume)
|
|
|
|
expect(baselineEvents(secondResume)).toHaveLength(1)
|
|
expect(secondResume.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')).toHaveLength(1)
|
|
expect(blocksText(secondResume.session.deriveMessages()[0]?.content)).toContain('omitted AGENTS.md')
|
|
expect(blocksText(secondResume.session.deriveMessages()[0]?.content)).not.toContain('root root')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps the shared-root duplicate of a user-global file truncated to 0 bytes excluded across steps and resumes', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'project instruction')
|
|
await write(join(home, 'AGENTS.md'), 'global instruction '.repeat(100))
|
|
await write(join(agents, 'AGENTS.md'), `\n${'global instruction '.repeat(100)}\n`)
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
// A user-global file is budgeted alone, so it loses all content only at a
|
|
// budget this small: here its chain keeps only the heading, while the
|
|
// project chain keeps part of its file under the compact intro.
|
|
await mountAgentInstructions(ctx, { maxBytes: 244 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
const contexts = original.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')
|
|
expect(contexts).toHaveLength(1)
|
|
expect(derivedText(original)).toContain('truncated $DSH_HOME/AGENTS.md from 1900 to 0 bytes')
|
|
expect(derivedText(original)).toContain('Instructions from: AGENTS.md\n\nproject')
|
|
expect(derivedText(original)).not.toContain('global instruction')
|
|
expect(derivedText(original)).not.toContain('$DSH_AGENTS_HOME')
|
|
await syncAgentInstructions(ctx, original)
|
|
await syncAgentInstructions(ctx, original)
|
|
expect(original.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
|
|
const firstResume = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, firstResume)
|
|
const secondResume = await stubAgent(root, firstResume.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, secondResume)
|
|
expect(secondResume.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
for (const path of [root, home, agents]) await rm(path, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('adds the user-global file that a resumed single-budget baseline omitted as one additional transition', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'project rule '.repeat(20))
|
|
await write(join(home, 'AGENTS.md'), 'global rule '.repeat(20))
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 700 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, original)
|
|
expect(derivedText(original)).toContain('Instructions from: $DSH_HOME/AGENTS.md')
|
|
const userGlobalScope = sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE)
|
|
// The fixture models a log recorded under one shared budget: the same
|
|
// identity, a baseline with only the project file, and its omission notice.
|
|
const recorded = original.session.snapshotEvents().map((event) => {
|
|
if (event.type !== 'user/message' || event.data.source.kind !== 'agent-instructions') return event
|
|
const text = [
|
|
'<system-reminder>',
|
|
'Workspace instruction budget 700 bytes: omitted $DSH_HOME/AGENTS.md',
|
|
'',
|
|
baselineIntro,
|
|
'',
|
|
'Instructions from: AGENTS.md',
|
|
'',
|
|
'project rule '.repeat(20),
|
|
'</system-reminder>',
|
|
].join('\n')
|
|
const changes = event.data.source.changes.filter(change => change.scope !== userGlobalScope)
|
|
return { ...event, data: { ...event.data, content: [{ type: 'text' as const, text }], source: { ...event.data.source, changes } } }
|
|
})
|
|
|
|
const resumed = await stubAgent(root, recorded)
|
|
await composeBaselinePrefix(ctx, resumed)
|
|
|
|
expect(baselineEvents(resumed)).toHaveLength(1)
|
|
const transitions = resumed.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions'
|
|
&& event.data.source.baseline !== true)
|
|
expect(transitions).toHaveLength(1)
|
|
const transition = transitions[0]
|
|
expect(transition?.type === 'user/message' ? transition.data.source : undefined).toMatchObject({
|
|
changes: [{ action: 'set', scope: userGlobalScope, path: '$DSH_HOME/AGENTS.md' }],
|
|
})
|
|
expect(transition?.type === 'user/message' ? blocksText(transition.data.content) : '').toContain([
|
|
'Additional instructions from: $DSH_HOME/AGENTS.md',
|
|
'',
|
|
`These user-global instructions apply to all work. ${additionalGuidance}`,
|
|
'',
|
|
'global rule '.repeat(20),
|
|
].join('\n'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
for (const path of [root, home]) await rm(path, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps a duplicate excluded when only a different digest fits the same group budget', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'broad instruction '.repeat(100))
|
|
await write(join(root, 'CLAUDE.md'), 'project instruction')
|
|
await write(join(root, 'AGENTS.local.md'), 'broad instruction '.repeat(100))
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 700 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
const contexts = original.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')
|
|
expect(contexts).toHaveLength(1)
|
|
expect(contexts[0]?.type === 'user/message' ? contexts[0].data.source : undefined).toMatchObject({
|
|
changes: [{ scope: sk('.', 'CLAUDE.md') }],
|
|
})
|
|
await syncAgentInstructions(ctx, original)
|
|
expect(original.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, resumed)
|
|
expect(resumed.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
for (const path of [root, home]) await rm(path, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('removes a previously visible baseline file that leaves the retained budget set', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await mkdir(cwd, { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root '.repeat(200))
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 700 })
|
|
const original = await stubAgent(cwd)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
await write(join(cwd, 'AGENTS.md'), 'package rule')
|
|
const resumed = await stubAgent(cwd, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(ctx, resumed)
|
|
|
|
expect(baselineEvents(resumed)).toHaveLength(1)
|
|
const update = resumed.session.snapshotEvents().findLast(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions'
|
|
&& event.data.source.baseline !== true)
|
|
expect(update?.type === 'user/message' && update.data.source.kind === 'agent-instructions'
|
|
? update.data.source.changes
|
|
: undefined).toMatchObject([
|
|
{ action: 'remove', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' },
|
|
{ action: 'set', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') },
|
|
])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('recomposes the baseline when candidate precedence changes between resumes', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const originalCtx = new Context()
|
|
provideWorkingDirectoryFixture(originalCtx)
|
|
const resumedCtx = new Context()
|
|
provideWorkingDirectoryFixture(resumedCtx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'agents rule')
|
|
await write(join(root, 'CLAUDE.md'), 'claude rule')
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(originalCtx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(originalCtx, original)
|
|
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(resumedCtx, {
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['CLAUDE.md', 'AGENTS.md'],
|
|
})
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(resumedCtx, resumed)
|
|
|
|
const baselines = baselineEvents(resumed)
|
|
expect(baselines).toHaveLength(2)
|
|
const replacement = baselines.at(-1)
|
|
const replacementText = replacement?.type === 'user/message'
|
|
? blocksText(replacement.data.content)
|
|
: ''
|
|
expect(replacementText).toContain('replaces all earlier workspace instruction baselines')
|
|
expect(replacementText.indexOf('Instructions from: CLAUDE.md'))
|
|
.toBeLessThan(replacementText.indexOf('Instructions from: AGENTS.md'))
|
|
const baselineIdentities = baselines.flatMap(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions'
|
|
&& typeof event.data.source.baselineIdentity === 'string'
|
|
? [event.data.source.baselineIdentity]
|
|
: [])
|
|
expect(new Set(baselineIdentities).size).toBe(2)
|
|
|
|
const repeated = await stubAgent(root, resumed.session.snapshotEvents())
|
|
await composeBaselinePrefix(resumedCtx, repeated)
|
|
expect(baselineEvents(repeated)).toHaveLength(2)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await originalCtx.fiber.dispose()
|
|
await resumedCtx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('tombstones candidates removed across successive baseline configurations', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agentsCtx = new Context()
|
|
provideWorkingDirectoryFixture(agentsCtx)
|
|
const claudeCtx = new Context()
|
|
provideWorkingDirectoryFixture(claudeCtx)
|
|
const restoredCtx = new Context()
|
|
provideWorkingDirectoryFixture(restoredCtx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'agents rule')
|
|
await write(join(root, 'CLAUDE.md'), 'claude rule')
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(agentsCtx, {
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['AGENTS.md'],
|
|
})
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(agentsCtx, original)
|
|
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(claudeCtx, {
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['CLAUDE.md'],
|
|
})
|
|
const claudeResume = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(claudeCtx, claudeResume)
|
|
const claudeBaseline = baselineEvents(claudeResume).at(-1)
|
|
expect(claudeBaseline?.type === 'user/message' && claudeBaseline.data.source.kind === 'agent-instructions'
|
|
? claudeBaseline.data.source.changes
|
|
: undefined).toMatchObject([
|
|
{ action: 'remove', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' },
|
|
{ action: 'set', scope: sk('.', 'CLAUDE.md'), path: 'CLAUDE.md' },
|
|
])
|
|
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(restoredCtx, {
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['AGENTS.md'],
|
|
})
|
|
const restored = await stubAgent(root, claudeResume.session.snapshotEvents())
|
|
await composeBaselinePrefix(restoredCtx, restored)
|
|
const restoredBaseline = baselineEvents(restored).at(-1)
|
|
expect(restoredBaseline?.type === 'user/message' && restoredBaseline.data.source.kind === 'agent-instructions'
|
|
? restoredBaseline.data.source.changes
|
|
: undefined).toMatchObject([
|
|
{ action: 'remove', scope: sk('.', 'CLAUDE.md'), path: 'CLAUDE.md' },
|
|
{ action: 'set', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' },
|
|
])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await agentsCtx.fiber.dispose()
|
|
await claudeCtx.fiber.dispose()
|
|
await restoredCtx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('supersedes an incompatible visible baseline when no current candidate exists', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const originalCtx = new Context()
|
|
provideWorkingDirectoryFixture(originalCtx)
|
|
const resumedCtx = new Context()
|
|
provideWorkingDirectoryFixture(resumedCtx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'agents rule')
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(originalCtx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(originalCtx, original)
|
|
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(resumedCtx, {
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['POLICY.md'],
|
|
})
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await composeBaselinePrefix(resumedCtx, resumed)
|
|
|
|
const baselines = baselineEvents(resumed)
|
|
expect(baselines).toHaveLength(2)
|
|
const replacement = baselines.at(-1)
|
|
expect(replacement?.type === 'user/message' ? blocksText(replacement.data.content) : '')
|
|
.toContain('No workspace instructions are currently active.')
|
|
expect(replacement?.type === 'user/message' && replacement.data.source.kind === 'agent-instructions'
|
|
? replacement.data.source.changes
|
|
: undefined).toMatchObject([
|
|
{ action: 'remove', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' },
|
|
])
|
|
|
|
const repeated = await stubAgent(root, resumed.session.snapshotEvents())
|
|
await composeBaselinePrefix(resumedCtx, repeated)
|
|
expect(baselineEvents(repeated)).toHaveLength(2)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await originalCtx.fiber.dispose()
|
|
await resumedCtx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('reuses an inserted but unadmitted baseline after session recovery and plugin reload', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
const fiber = await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await agentEvents(ctx, original).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: [] }),
|
|
)
|
|
const inserted = original.inbox.nextStep[0]
|
|
expect(inserted?.source).toMatchObject({ kind: 'agent-instructions', baseline: true })
|
|
|
|
await fiber.dispose()
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await agentEvents(ctx, resumed).serial('agent/created', { source: 'resume' })
|
|
const claimed = claimInbox(resumed, 'next-step')
|
|
const decision = await agentEvents(ctx, resumed).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: claimed, turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: claimed }),
|
|
)
|
|
if (decision.kind !== 'enter') throw new Error('recovered baseline was rejected')
|
|
for (const message of decision.messages) {
|
|
const event = resumed.session.append('user/message', message, { surfaceOp: 'append' })
|
|
ctx.emit('session/event', resumed.session, event)
|
|
}
|
|
|
|
expect(decision.messages.map(message => message.id)).toEqual([inserted?.id])
|
|
expect(resumed.inbox.nextStep).toEqual([])
|
|
expect(resumed.session.snapshotEvents().filter(event => event.type === 'agent/inbox/spliced'
|
|
&& event.data.inserted.some(message => message.source.kind === 'agent-instructions'
|
|
&& message.source.baseline === true))).toHaveLength(1)
|
|
expect(baselineEvents(resumed)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('replaces a recovered unadmitted baseline when its source changed offline', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'old repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
const fiber = await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await agentEvents(ctx, original).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: [] }),
|
|
)
|
|
const stale = original.inbox.nextStep[0]
|
|
expect(blocksText(stale?.content)).toContain('old repo rule')
|
|
|
|
await write(join(root, 'AGENTS.md'), 'new repo rule')
|
|
await fiber.dispose()
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await agentEvents(ctx, resumed).serial('agent/created', { source: 'resume' })
|
|
const staleClaim = claimInbox(resumed, 'next-step')
|
|
const staleDecision = await agentEvents(ctx, resumed).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: staleClaim, turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: staleClaim }),
|
|
)
|
|
|
|
if (staleDecision.kind !== 'enter') throw new Error('recovered baseline was rejected')
|
|
expect(staleDecision.messages).toHaveLength(2)
|
|
expect(staleDecision.messages[0]).toBe(staleClaim[0])
|
|
const replacement = staleDecision.messages[1]
|
|
expect(replacement?.id).not.toBe(stale?.id)
|
|
expect(blocksText(replacement?.content)).toContain('new repo rule')
|
|
expect(blocksText(replacement?.content)).not.toContain('old repo rule')
|
|
expect(resumed.inbox.nextStep).toHaveLength(0)
|
|
|
|
for (const message of staleDecision.messages) {
|
|
const event = resumed.session.append('user/message', message, { surfaceOp: 'append' })
|
|
ctx.emit('session/event', resumed.session, event)
|
|
}
|
|
expect(baselineEvents(resumed)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it.each([
|
|
{ label: 'baseline loading is disabled', maxBytes: 0, provideFs: true },
|
|
{ label: 'the filesystem provider is unavailable', maxBytes: 65536, provideFs: false },
|
|
])('does not requeue recovered workspace contexts when $label', async ({ maxBytes, provideFs }) => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const originalCtx = new Context()
|
|
provideWorkingDirectoryFixture(originalCtx)
|
|
const resumedCtx = new Context()
|
|
provideWorkingDirectoryFixture(resumedCtx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(originalCtx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await agentEvents(originalCtx, original).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: [] }),
|
|
)
|
|
const stale = original.inbox.nextStep[0]
|
|
expect(stale?.source).toMatchObject({ kind: 'agent-instructions', baseline: true })
|
|
|
|
await originalCtx.fiber.dispose()
|
|
if (provideFs) await resumedCtx.plugin(LocalFileSystem, { cwd: '/' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(resumedCtx, { maxBytes })
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
await agentEvents(resumedCtx, resumed).serial('agent/created', { source: 'resume' })
|
|
const claimed = claimInbox(resumed, 'next-step')
|
|
const decision = await agentEvents(resumedCtx, resumed).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: claimed, turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: claimed }),
|
|
)
|
|
|
|
expect(claimed.map(message => message.id)).toEqual([stale?.id])
|
|
expect(decision).toEqual({ kind: 'enter', messages: claimed })
|
|
expect(resumed.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await originalCtx.fiber.dispose()
|
|
await resumedCtx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('queues and records a removal for stale visible nested context', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'stale nested instructions' }],
|
|
source: {
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'set', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md'), digest: 'stale' }],
|
|
},
|
|
}), {
|
|
surfaceOp: 'append',
|
|
})
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
const removal = agent.session.snapshotEvents().find(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions'
|
|
&& event.data.source.changes.some(change => change.action === 'remove'))
|
|
expect(removal?.type === 'user/message' ? removal.data.source : undefined).toMatchObject({
|
|
changes: [{ action: 'remove', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('combines startup reconciliation and baseline into one durable inbox context', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'stale nested instructions' }],
|
|
source: {
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'set', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md'), digest: 'stale' }],
|
|
},
|
|
}), {
|
|
surfaceOp: 'append',
|
|
})
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
const workspaceEvents = agent.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')
|
|
expect(workspaceEvents).toHaveLength(2)
|
|
expect(workspaceEvents.some(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions'
|
|
&& event.data.source.changes.some(change => change.action === 'remove'))).toBe(true)
|
|
expect(baselineEvents(agent)).toHaveLength(1)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
expect(agent.session.snapshotEvents().filter(event => event.type === 'user/message'
|
|
&& event.data.source.kind === 'agent-instructions')).toHaveLength(2)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('enters the baseline right after the claimed prompt in the first pre-step without queuing another step', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const prompt = createUserMessage({
|
|
content: [{ type: 'text', text: 'current prompt' }],
|
|
source: { kind: 'user' },
|
|
})
|
|
const downstream = { kind: 'enter' as const, messages: [prompt] }
|
|
|
|
const decision = await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [prompt], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve(downstream),
|
|
)
|
|
|
|
expect(decision).toMatchObject({ kind: 'enter' })
|
|
if (decision.kind !== 'enter') throw new Error('workspace baseline was rejected')
|
|
expect(decision.messages).toHaveLength(2)
|
|
expect(decision.messages[0]).toBe(prompt)
|
|
expect(decision.messages[1]?.source).toMatchObject({ kind: 'agent-instructions', baseline: true })
|
|
expect(blocksText(decision.messages[1]?.content)).toContain('Instructions from: AGENTS.md')
|
|
expect(agent.inbox.nextStep).toHaveLength(0)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('reports a changed user-global instruction through the visible-scope reconcile path', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
await write(join(home, 'AGENTS.md'), 'global rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
await write(join(home, 'AGENTS.md'), 'updated global rule')
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const pending = await agentInstructionsOf(agent)
|
|
expect(pending?.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [{ action: 'replace', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
expect(blocksText(pending?.content)).toContain('updated global rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('reports a changed shared-agents instruction through the visible-scope reconcile path', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(agents, 'AGENTS.md'), 'shared rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
await write(join(agents, 'AGENTS.md'), 'updated shared rule')
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const pending = await agentInstructionsOf(agent)
|
|
expect(pending?.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [{ action: 'replace', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
expect(blocksText(pending?.content)).toContain('updated shared rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('queues a removal when a rendered shared-agents file disappears', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(agents, 'AGENTS.md'), 'shared rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
await rm(join(agents, 'AGENTS.md'))
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const pending = await agentInstructionsOf(agent)
|
|
expect(pending?.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [{ action: 'remove', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
expect(blocksText(pending?.content)).toContain('Instructions removed: $DSH_AGENTS_HOME/AGENTS.md')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('renders a shared-agents delta without exposing the internal scope key', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
await write(join(agents, 'AGENTS.md'), 'shared delta rule')
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const text = blocksText((await agentInstructionsOf(agent))?.content)
|
|
expect(text).toContain('Additional instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
expect(text).toContain('These user-global instructions apply to all work.')
|
|
expect(text).toContain('shared delta rule')
|
|
expect(text).not.toContain(AGENTS_GLOBAL_DIRECTORY)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it.each([
|
|
{ failure: 'metadata', winner: 'edit' },
|
|
{ failure: 'metadata', winner: 'delete' },
|
|
{ failure: 'read', winner: 'edit' },
|
|
{ failure: 'read', winner: 'delete' },
|
|
] as const)(
|
|
'preserves the global group when a hidden duplicate has a $failure failure and its winner is $winner',
|
|
async ({ failure, winner }) => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const harnessPath = join(home, USER_GLOBAL_FILE)
|
|
const sharedPath = join(agents, USER_GLOBAL_FILE)
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(harnessPath, { type: 'file', content: 'shared global rule', version: FsVersion('harness-v1') })
|
|
fs.entries.set(sharedPath, { type: 'file', content: 'shared global rule', version: FsVersion('shared-v1') })
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent).match(/shared global rule/g)).toHaveLength(1)
|
|
expect(derivedText(agent)).not.toContain('$DSH_AGENTS_HOME/AGENTS.md')
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
|
|
if (winner === 'edit') {
|
|
fs.entries.set(harnessPath, { type: 'file', content: 'new harness rule', version: FsVersion('harness-v2') })
|
|
} else {
|
|
fs.entries.delete(harnessPath)
|
|
}
|
|
const failures = failure === 'metadata' ? fs.throwOnStat : fs.throwOnRead
|
|
failures.add(sharedPath)
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
expect(baselineEvents(agent)).toHaveLength(1)
|
|
|
|
failures.delete(sharedPath)
|
|
await syncAgentInstructions(ctx, agent)
|
|
const pending = agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')
|
|
expect(pending).toHaveLength(1)
|
|
expect(pending[0]?.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [
|
|
{
|
|
action: winner === 'edit' ? 'replace' : 'remove',
|
|
scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE),
|
|
path: '$DSH_HOME/AGENTS.md',
|
|
},
|
|
{ action: 'set', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '$DSH_AGENTS_HOME/AGENTS.md' },
|
|
],
|
|
})
|
|
const recoveredText = blocksText(pending[0]?.content)
|
|
expect(recoveredText).toContain(`${winner === 'edit' ? 'Updated instructions from:' : 'Instructions removed:'} $DSH_HOME/AGENTS.md`)
|
|
expect(recoveredText).toContain('Additional instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
expect(recoveredText).toContain('These user-global instructions apply to all work.')
|
|
expect(recoveredText.match(/shared global rule/g)).toHaveLength(1)
|
|
if (winner === 'edit') expect(recoveredText).toContain('new harness rule')
|
|
|
|
for (const message of claimInbox(agent, 'next-step')) {
|
|
const event = agent.session.append('user/message', message, { surfaceOp: 'append' })
|
|
ctx.emit('session/event', agent.session, event)
|
|
}
|
|
await syncAgentInstructions(ctx, agent)
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
expect(baselineEvents(agent)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
},
|
|
)
|
|
|
|
it.each(['shared-stat', 'shared-read', 'home-stat-once'] as const)('keeps a user-global group frozen across a project alias during %s', async (failure) => {
|
|
const root = await tempRepo()
|
|
const agents = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const homePath = join(root, 'AGENTS.md')
|
|
const sharedPath = join(agents, 'AGENTS.md')
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(homePath, { type: 'file', content: 'global rule A', version: FsVersion('home-v1') })
|
|
fs.entries.set(sharedPath, { type: 'file', content: 'global rule A', version: FsVersion('shared-v1') })
|
|
pinHarnessHome(root)
|
|
pinAgentsHome(agents)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
const baseline = baselineEvents(agent)[0]
|
|
expect(baseline?.type === 'user/message' ? baseline.data.source : undefined).toMatchObject({
|
|
changes: [{ action: 'set', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
fs.entries.set(homePath, { type: 'file', content: 'global rule B', version: FsVersion('home-v2') })
|
|
const originalStat = fs.stat.bind(fs)
|
|
let failHomeOnce = failure === 'home-stat-once'
|
|
fs.stat = async (target, signal) => {
|
|
if (failHomeOnce && target.targetKey === homePath) {
|
|
failHomeOnce = false
|
|
throw new Error('One transient failure of the aliased home path')
|
|
}
|
|
return originalStat(target, signal)
|
|
}
|
|
if (failure === 'shared-stat') fs.throwOnStat.add(sharedPath)
|
|
if (failure === 'shared-read') fs.throwOnRead.add(sharedPath)
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
fs.throwOnStat.clear()
|
|
fs.throwOnRead.clear()
|
|
await syncAgentInstructions(ctx, agent)
|
|
const recovered = await agentInstructionsOf(agent)
|
|
expect(recovered.source).toMatchObject({
|
|
changes: [
|
|
{ action: 'replace', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) },
|
|
{ action: 'set', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) },
|
|
],
|
|
})
|
|
expect(blocksText(recovered.content)).toContain('Updated instructions from: $DSH_HOME/AGENTS.md')
|
|
expect(blocksText(recovered.content)).toContain('Additional instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
expect(blocksText(recovered.content)).not.toContain('Additional instructions from: AGENTS.md')
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
for (const path of [root, agents]) await rm(path, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('preserves a cold global group when the unrecorded shared duplicate cannot be probed', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const harnessPath = join(home, USER_GLOBAL_FILE)
|
|
const sharedPath = join(agents, USER_GLOBAL_FILE)
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(harnessPath, { type: 'file', content: 'new harness rule', version: FsVersion('harness-v2') })
|
|
fs.entries.set(sharedPath, { type: 'file', content: 'shared global rule', version: FsVersion('shared-v1') })
|
|
const agent = await stubAgent(root)
|
|
const baseline = baselineInstructionState([{
|
|
absolutePath: harnessPath,
|
|
displayPath: '$DSH_HOME/AGENTS.md',
|
|
content: 'shared global rule',
|
|
version: FsVersion('harness-v1'),
|
|
}])
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'shared global rule' }],
|
|
source: { kind: 'agent-instructions', form: 'instructions', changes: [...baseline.changes.values()] },
|
|
}), { surfaceOp: 'append' })
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
const resolved = resolveConfig({ maxBytes: 65536 })
|
|
const cache: InstructionVersionCache = new WeakMap()
|
|
const options = {
|
|
cwd: root,
|
|
authorityMessages: [],
|
|
scopeMessages: [],
|
|
touchedPaths: [],
|
|
includeBaselineScopes: true,
|
|
projectRoot: root,
|
|
signal: testToolSignal,
|
|
}
|
|
fs.throwOnStat.add(sharedPath)
|
|
|
|
expect(await reconcileInstructionContext(agent, resolved, cache, fs, options)).toBeUndefined()
|
|
expect(cache.get(agent.session)?.size ?? 0).toBe(0)
|
|
|
|
fs.throwOnStat.delete(sharedPath)
|
|
const recovered = await reconcileInstructionContext(agent, resolved, cache, fs, options)
|
|
expect(recovered?.context.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [
|
|
{ action: 'replace', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '$DSH_HOME/AGENTS.md' },
|
|
{ action: 'set', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '$DSH_AGENTS_HOME/AGENTS.md' },
|
|
],
|
|
})
|
|
expect(blocksText(recovered?.context.content)).toContain('These user-global instructions apply to all work.')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('promotes a content-deduplicated shared file when the harness-home file disappears', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), 'shared global rule')
|
|
await write(join(agents, 'AGENTS.md'), 'shared global rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent).match(/shared global rule/g)).toHaveLength(1)
|
|
|
|
await rm(join(home, 'AGENTS.md'))
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const text = blocksText((await agentInstructionsOf(agent))?.content)
|
|
expect(text).toContain('Instructions removed: $DSH_HOME/AGENTS.md')
|
|
expect(text).toContain('Additional instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
expect(text).toContain('shared global rule')
|
|
expect(text).not.toContain('agents-global')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads a changed shared file that the baseline deduplicated', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), 'shared global rule')
|
|
await write(join(agents, 'AGENTS.md'), 'shared global rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
await write(join(agents, 'AGENTS.md'), 'updated shared global rule')
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const pending = await agentInstructionsOf(agent)
|
|
expect(pending?.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [{ action: 'set', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
expect(blocksText(pending?.content)).toContain('updated shared global rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it.each([
|
|
{ formation: 'shared-edited', winner: 'edit' },
|
|
{ formation: 'shared-edited', winner: 'delete' },
|
|
{ formation: 'home-edited', winner: 'edit' },
|
|
{ formation: 'home-edited', winner: 'delete' },
|
|
] as const)(
|
|
'caches a removed duplicate after $formation and promotes it after winner $winner',
|
|
async ({ formation, winner }) => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
const ctx = new Context()
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const harnessPath = join(home, USER_GLOBAL_FILE)
|
|
const sharedPath = join(agents, USER_GLOBAL_FILE)
|
|
const duplicateText = 'global rule B'
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(harnessPath, {
|
|
type: 'file',
|
|
content: formation === 'shared-edited' ? duplicateText : 'harness rule A',
|
|
version: FsVersion('harness-v1'),
|
|
})
|
|
fs.entries.set(sharedPath, {
|
|
type: 'file',
|
|
content: formation === 'shared-edited' ? 'shared rule A' : duplicateText,
|
|
version: FsVersion('shared-v1'),
|
|
})
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
expect(derivedText(agent)).toContain('Instructions from: $DSH_HOME/AGENTS.md')
|
|
expect(derivedText(agent)).toContain('Instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
|
|
if (formation === 'shared-edited') {
|
|
fs.entries.set(sharedPath, { type: 'file', content: duplicateText, version: FsVersion('shared-v2') })
|
|
} else {
|
|
fs.entries.set(harnessPath, { type: 'file', content: duplicateText, version: FsVersion('harness-v2') })
|
|
}
|
|
const removal = await syncedAgentInstructions(ctx, agent)
|
|
if (removal.source.kind !== 'agent-instructions') throw new Error('missing instruction removal source')
|
|
expect(removal.source.changes).toContainEqual({
|
|
action: 'remove',
|
|
scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE),
|
|
path: '$DSH_AGENTS_HOME/AGENTS.md',
|
|
})
|
|
expect(blocksText(removal.content)).toContain('Instructions removed: $DSH_AGENTS_HOME/AGENTS.md')
|
|
await appendAdditionalContexts(ctx, agent)
|
|
|
|
fs.readTargets.length = 0
|
|
await syncAgentInstructions(ctx, agent)
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
expect(fs.readTargets).not.toContain(sharedPath)
|
|
|
|
if (winner === 'edit') {
|
|
fs.entries.set(harnessPath, { type: 'file', content: 'harness rule C', version: FsVersion('harness-v3') })
|
|
} else {
|
|
fs.entries.delete(harnessPath)
|
|
}
|
|
const promotion = await syncedAgentInstructions(ctx, agent)
|
|
expect(promotion.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [
|
|
{
|
|
action: winner === 'edit' ? 'replace' : 'remove',
|
|
scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE),
|
|
path: '$DSH_HOME/AGENTS.md',
|
|
},
|
|
{ action: 'set', scope: sk(AGENTS_GLOBAL_DIRECTORY, USER_GLOBAL_FILE), path: '$DSH_AGENTS_HOME/AGENTS.md' },
|
|
],
|
|
})
|
|
const promotedText = blocksText(promotion.content)
|
|
expect(promotedText).toContain('Additional instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
expect(promotedText).toContain('These user-global instructions apply to all work.')
|
|
expect(promotedText.match(/global rule B/g)).toHaveLength(1)
|
|
expect(fs.readTargets.filter(path => path === sharedPath)).toHaveLength(1)
|
|
await appendAdditionalContexts(ctx, agent)
|
|
|
|
await syncAgentInstructions(ctx, agent)
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')).toEqual([])
|
|
expect(fs.readTargets.filter(path => path === sharedPath)).toHaveLength(1)
|
|
expect(baselineEvents(agent)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
},
|
|
)
|
|
|
|
it('keeps a shared file created later with identical content suppressed without re-reading it', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
const pendingContexts = (agent: Agent): UserMessage[] =>
|
|
agent.inbox.nextStep.filter(message => message.source.kind === 'agent-instructions')
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), 'shared global rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
await write(join(agents, 'AGENTS.md'), 'shared global rule')
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(pendingContexts(agent)).toEqual([])
|
|
|
|
// A second pass proves the duplicate is recognized from cached metadata
|
|
// rather than by reading the shared file again.
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(pendingContexts(agent)).toEqual([])
|
|
expect(derivedText(agent).match(/shared global rule/g)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps a project directory named agents-global out of the shared global scope', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
const cwd = join(root, AGENTS_GLOBAL_DIRECTORY)
|
|
await write(join(cwd, 'AGENTS.md'), 'project rule')
|
|
|
|
const escapedDirectory = `.${sep}${AGENTS_GLOBAL_DIRECTORY}`
|
|
const escapedPath = `${escapedDirectory}${sep}AGENTS.md`
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
const files = await discoverBaselineInstructionFiles({ cwd })
|
|
expect(files.map(file => file.displayPath)).toEqual([escapedPath])
|
|
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(cwd)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
expect(derivedText(agent)).toContain(`Instructions from: ${escapedPath}`)
|
|
|
|
await rm(join(cwd, 'AGENTS.md'))
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
const pending = await agentInstructionsOf(agent)
|
|
expect(pending?.source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
changes: [{ action: 'remove', scope: sk(escapedDirectory, USER_GLOBAL_FILE) }],
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('queues the desired workspace context when the current step is rejected', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const downstream = { kind: 'reject' as const }
|
|
|
|
const decision = await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve(downstream),
|
|
)
|
|
|
|
expect(decision).toBe(downstream)
|
|
expect(agent.inbox.nextStep).toHaveLength(1)
|
|
expect(blocksText(agent.inbox.nextStep[0]?.content)).toContain('repo rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('retains a visible baseline after a plugin remount', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
await write(join(root, 'file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
const fiber = await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
// Hot remount over the live session: the durable baseline remains
|
|
// visible, so the fresh mount does not append a duplicate.
|
|
await fiber.dispose()
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(baselineEvents(agent)).toHaveLength(1)
|
|
|
|
await write(join(root, 'AGENTS.md'), 'updated repo rule')
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-remount'),
|
|
name: 'read',
|
|
arguments: { file_path: 'file.txt' },
|
|
agent,
|
|
})
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'replace', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('restores a compacted baseline on a hot plugin remount', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
pinHarnessHome(home)
|
|
const fiber = await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
const baseline = baselineEvents(agent)[0]
|
|
expect(baseline).toBeDefined()
|
|
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'compacted summary' }],
|
|
source: checkpointSource('instruction-compaction-1'),
|
|
}), {
|
|
surfaceOp: { op: 'replace', startSeq: baseline!.seq, endSeq: baseline!.seq },
|
|
sourceEventSeqs: [baseline!.seq],
|
|
})
|
|
|
|
await fiber.dispose()
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(baselineEvents(agent)).toHaveLength(2)
|
|
expect(blocksText(agent.session.deriveMessages().at(-1)?.content)).toContain('repo rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('folds a compacted baseline into the next entering pre-step before another filesystem touch', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'first post-compaction request rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
const baseline = baselineEvents(agent)[0]
|
|
expect(baseline).toBeDefined()
|
|
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'compacted summary' }],
|
|
source: checkpointSource('instruction-compaction-2'),
|
|
}), {
|
|
surfaceOp: { op: 'replace', startSeq: baseline!.seq, endSeq: baseline!.seq },
|
|
sourceEventSeqs: [baseline!.seq],
|
|
})
|
|
const prompt = createUserMessage({
|
|
content: [{ type: 'text', text: 'continue after compaction' }],
|
|
source: { kind: 'user' },
|
|
})
|
|
|
|
const decision = await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [prompt], turn: 2, step: 1, signal: AbortSignal.timeout(requestTimeoutMs) },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: [prompt] }),
|
|
)
|
|
|
|
if (decision.kind !== 'enter') throw new Error('post-compaction request was rejected')
|
|
expect(decision.messages).toHaveLength(2)
|
|
expect(decision.messages[0]).toBe(prompt)
|
|
expect(decision.messages[1]?.source).toMatchObject({ kind: 'agent-instructions', baseline: true })
|
|
expect(blocksText(decision.messages[1]?.content)).toContain('first post-compaction request rule')
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('appends a replacement transition when a resumed session edited its baseline offline', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'old root rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, original)
|
|
|
|
// The first resumed pre-step retains the compatible visible baseline and
|
|
// appends only the offline file transition needed to reach current state.
|
|
await write(join(root, 'AGENTS.md'), 'new root rule after offline edit')
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
|
|
// Resume announces its lifecycle start before the first step.
|
|
await agentEvents(ctx, resumed).serial('agent/created', { source: 'resume' })
|
|
await composeBaselinePrefix(ctx, resumed)
|
|
|
|
const baselines = baselineEvents(resumed)
|
|
expect(baselines).toHaveLength(1)
|
|
const latest = resumed.session.snapshotEvents().findLast(event =>
|
|
event.type === 'user/message' && event.data.source.kind === 'agent-instructions')
|
|
expect(latest?.type === 'user/message' ? latest.data.source : undefined).toMatchObject({
|
|
changes: [{ action: 'replace', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
})
|
|
expect(latest?.type === 'user/message' && blocksText(latest.data.content))
|
|
.toContain('new root rule after offline edit')
|
|
const original0 = baselines[0]
|
|
expect(original0?.type === 'user/message' && blocksText(original0.data.content))
|
|
.toContain('old root rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('tracks only baseline files that were actually included under the byte budget', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root '.repeat(200))
|
|
await write(join(cwd, 'AGENTS.md'), 'package rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 700 })
|
|
const agent = await stubAgent(cwd)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('omitted AGENTS.md')
|
|
expect(derivedText(agent)).toContain(`Instructions from: ${join('pkg', 'AGENTS.md')}\n\npackage rule`)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps an independent pre-step contribution after the queued workspace context', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
ctx.on('agent/pre-step', async (_payload, next) => {
|
|
const decision = await next()
|
|
if (decision.kind === 'reject') return decision
|
|
return {
|
|
...decision,
|
|
messages: [
|
|
...decision.messages,
|
|
createUserMessage({ content: [{ type: 'text', text: '<system-reminder>Available skills</system-reminder>' }], source: { kind: 'test-skills' } }),
|
|
],
|
|
}
|
|
})
|
|
|
|
const prefix = await composeBaselinePrefix(ctx, await stubAgent(root))
|
|
|
|
expect(prefix).toHaveLength(2)
|
|
expect(blocksText(prefix[0]?.content)).toContain('Instructions from: AGENTS.md')
|
|
expect(blocksText(prefix[1]?.content)).toBe('<system-reminder>Available skills</system-reminder>')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('queues a replacement when a frozen baseline file changes before a later fs tool call', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'old root rule')
|
|
await write(join(root, 'file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
await write(join(root, 'AGENTS.md'), 'new root rule with more detail')
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-baseline-change'), name: 'read', arguments: { file_path: 'file.txt' }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'replace', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('Updated instructions from: AGENTS.md')
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('new root rule with more detail')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('queues a removal when a frozen baseline file is deleted before a later fs tool call', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root rule')
|
|
await write(join(root, 'file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
await rm(join(root, 'AGENTS.md'))
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-baseline-remove'), name: 'read', arguments: { file_path: 'file.txt' }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'remove', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('Instructions removed: AGENTS.md')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('deduplicates one AGENTS.md that is both user-global and the project-root candidate', async () => {
|
|
const root = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'shared root and global rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(root)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent).match(/shared root and global rule/g)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('deduplicates one AGENTS.md that is both the harness-home and shared-agents file', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const agents = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), ' shared global rule\n\n')
|
|
await write(join(agents, 'AGENTS.md'), 'shared global rule')
|
|
const ctx = new Context()
|
|
pinHarnessHome(home)
|
|
pinAgentsHome(agents)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
const text = derivedText(agent)
|
|
expect(text.match(/shared global rule/g)).toHaveLength(1)
|
|
expect(text).toContain('Instructions from: $DSH_HOME/AGENTS.md')
|
|
expect(text).not.toContain('Instructions from: $DSH_AGENTS_HOME/AGENTS.md')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
await rm(agents, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('deduplicates trimmed-identical sibling candidates in one directory and renders the earliest original bytes', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'shared repo rule')
|
|
await write(join(root, 'CLAUDE.md'), ' shared repo rule\n\n')
|
|
await write(join(root, 'file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
const text = derivedText(agent)
|
|
expect(text.match(/shared repo rule/g)).toHaveLength(1)
|
|
expect(text).toContain('Instructions from: AGENTS.md')
|
|
expect(text).not.toContain('Instructions from: CLAUDE.md')
|
|
// The kept candidate's original bytes are rendered, not the whitespace-padded duplicate.
|
|
expect(text).not.toContain(' shared repo rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it.each([10, 120])('does not expose state markers when baseline content is omitted at %i bytes', async (maxBytes) => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'x'.repeat(1000))
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
const contexts = agent.session.snapshotEvents().filter(event =>
|
|
event.type === 'user/message' && event.data.source.kind !== 'user',
|
|
)
|
|
expect(contexts).toHaveLength(1)
|
|
const source = contexts[0]?.type === 'user/message' ? contexts[0].data.source : undefined
|
|
expect(source?.kind === 'agent-instructions' ? source.changes : undefined).toEqual([])
|
|
if (maxBytes === 120) {
|
|
expect(derivedText(agent)).toContain('Instructions from: AGENTS.md')
|
|
expect(derivedText(agent)).toContain('from 1000 to 0 bytes')
|
|
} else {
|
|
expect(derivedText(agent)).not.toContain('Instructions from: AGENTS.md')
|
|
}
|
|
expect(derivedText(agent)).not.toContain('agent-instructions:')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads instruction file content through ctx.fs instead of direct node reads', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'node fs rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'ctx.fs rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('ctx.fs rule')
|
|
expect(derivedText(agent)).not.toContain('node fs rule')
|
|
expect(fs.readTargets).toEqual([join(root, 'AGENTS.md')])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads provider-visible instruction files that do not exist on the host filesystem', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'provider-only rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('provider-only rule')
|
|
expect(fs.readTargets).toEqual([join(root, 'AGENTS.md')])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('continues provider root discovery when a marker is confirmed absent', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const cwd = join(root, 'pkg')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.missingOnResolve.add(join(cwd, '.git'))
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'provider parent rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(cwd)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('provider parent rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps the direct provider API usable without an operation signal', async () => {
|
|
const root = resolve('/virtual/no-signal-repo')
|
|
const home = resolve('/virtual/no-signal-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'optional capability signal' })
|
|
|
|
pinHarnessHome(home)
|
|
const rendered = await loadBaselineInstructions({ cwd: root, maxBytes: 65536 }, fs)
|
|
|
|
expect(rendered?.text).toContain('optional capability signal')
|
|
expect(fs.signals).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('rejects a provider-sized instruction file before reading content', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'far too large' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536, maxSourceBytes: 4 })
|
|
|
|
const prefix = await composeBaselinePrefix(ctx, await stubAgent(root))
|
|
|
|
expect(prefix).toEqual([])
|
|
expect(fs.readTargets).toEqual([])
|
|
expect(fs.readTextTargets).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('bounds streamed instruction content when provider size is unavailable', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const instructionPath = join(root, 'AGENTS.md')
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(instructionPath, { type: 'file', content: 'far too large' })
|
|
fs.omitSizes.add(instructionPath)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536, maxSourceBytes: 4 })
|
|
|
|
const prefix = await composeBaselinePrefix(ctx, await stubAgent(root))
|
|
|
|
expect(prefix).toEqual([])
|
|
expect(fs.readTargets).toEqual([instructionPath, instructionPath])
|
|
expect(fs.readTextTargets).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('aborts an in-flight baseline stream with the prompt signal', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(BlockingReadFileSystem)
|
|
const fs = ctx.fs as BlockingReadFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'blocked' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const controller = new AbortController()
|
|
const reason = new Error('cancel prefix')
|
|
const pending = agentEvents(ctx, await stubAgent(root)).waterfall(
|
|
'agent/pre-step',
|
|
{ modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: controller.signal },
|
|
() => Promise.resolve({ kind: 'enter' as const, messages: [] }),
|
|
)
|
|
|
|
await fs.started.promise
|
|
controller.abort(reason)
|
|
|
|
await expect(pending).rejects.toBe(reason)
|
|
expect(fs.signals).toContain(controller.signal)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads user-global and CLAUDE fallback content through ctx.fs', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(home, 'AGENTS.md'), 'node global rule')
|
|
await write(join(root, 'CLAUDE.md'), 'node claude rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(home, 'AGENTS.md'), { type: 'file', content: 'ctx global rule' })
|
|
fs.entries.set(join(root, 'CLAUDE.md'), { type: 'file', content: 'ctx claude rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('ctx global rule')
|
|
expect(derivedText(agent)).toContain('ctx claude rule')
|
|
expect(derivedText(agent)).not.toContain('node global rule')
|
|
expect(derivedText(agent)).not.toContain('node claude rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips provider-visible instruction candidates when ctx.fs reports a non-file target', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'node fs rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'directory' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads instruction files when ctx.fs omits the metadata size', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'node fs rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('Instructions from: AGENTS.md')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips provider-visible instruction candidates when ctx.fs cannot stat them', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'node fs rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.throwOnStat.add(join(root, 'AGENTS.md'))
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips a candidate whose provider probe fails while still loading its available sibling', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.throwOnStat.add(join(root, 'AGENTS.md'))
|
|
fs.entries.set(join(root, 'CLAUDE.md'), { type: 'file', content: 'claude sibling rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('claude sibling rule')
|
|
expect(fs.readTargets).toContain(join(root, 'CLAUDE.md'))
|
|
expect(fs.readTargets).not.toContain(join(root, 'AGENTS.md'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('surfaces ctx.fs marker lookup failures instead of crossing into an ancestor project', async () => {
|
|
const root = await tempRepo()
|
|
const cwd = join(root, 'pkg')
|
|
const home = await tempRepo()
|
|
try {
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.throwOnStat.add(join(cwd, '.git'))
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'ancestor rule must not load' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(cwd)
|
|
|
|
await expect(composeBaselinePrefix(ctx, agent))
|
|
.rejects.toThrow(`stat failed: ${join(cwd, '.git')}`)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps different session cwd instruction files isolated in one context', async () => {
|
|
const repoA = await tempRepo()
|
|
const repoB = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(repoA, '.git'), { recursive: true })
|
|
await mkdir(join(repoB, '.git'), { recursive: true })
|
|
await write(join(repoA, 'AGENTS.md'), 'repo A only')
|
|
await write(join(repoB, 'AGENTS.md'), 'repo B only')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agentA = await stubAgent(repoA)
|
|
const agentB = await stubAgent(repoB)
|
|
|
|
await composeBaselinePrefix(ctx, agentA)
|
|
await composeBaselinePrefix(ctx, agentB)
|
|
|
|
expect(derivedText(agentA)).toContain('repo A only')
|
|
expect(derivedText(agentA)).not.toContain('repo B only')
|
|
expect(derivedText(agentB)).toContain('repo B only')
|
|
expect(derivedText(agentB)).not.toContain('repo A only')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(repoA, { recursive: true, force: true })
|
|
await rm(repoB, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('uses schema defaults on the plugin path so ancestor discovery still finds .git roots', async () => {
|
|
const root = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'child')
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await mkdir(cwd, { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root schema default rule')
|
|
await write(join(cwd, 'AGENTS.md'), 'child schema default rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(cwd)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('Instructions from: AGENTS.md\n\nroot schema default rule')
|
|
expect(derivedText(agent)).toContain(`Instructions from: ${join('child', 'AGENTS.md')}\n\nchild schema default rule`)
|
|
await ctx.fiber.dispose()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('renders a default local overlay alongside the base file in the baseline prefix', async () => {
|
|
const root = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'base rule')
|
|
await write(join(root, 'AGENTS.local.md'), 'local rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expect(derivedText(agent)).toContain('Instructions from: AGENTS.md\n\nbase rule')
|
|
expect(derivedText(agent)).toContain('Instructions from: AGENTS.local.md\n\nlocal rule')
|
|
await ctx.fiber.dispose()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('cleans up its pre-step listener when the plugin fiber is disposed', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
const fiber = await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
await fiber.dispose()
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not inject anything when maxBytes is zero', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 0 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not inject an empty agent-instructions message when maxBytes is negative', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: -1 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('leaves the request unchanged when no instruction files are present', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await composeBaselinePrefix(ctx, agent)
|
|
|
|
expectNoDerivedMessages(agent)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('labels a custom DSH_HOME as $DSH_HOME instead of pretending it is ~/.dsh', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await write(join(home, 'AGENTS.md'), 'global custom rule')
|
|
pinHarnessHome(home)
|
|
const files = await discoverBaselineInstructionFiles({ cwd: root })
|
|
|
|
expect(files.map(file => file.displayPath)).toEqual(['$DSH_HOME/AGENTS.md'])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not repeat a candidate metadata probe during one discovery and read pass', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'repo rule')
|
|
|
|
const observedStats = new Map<string, number>()
|
|
vi.resetModules()
|
|
vi.doMock('node:fs/promises', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('node:fs/promises')>()
|
|
return {
|
|
...actual,
|
|
stat: async (path: string) => {
|
|
observedStats.set(path, (observedStats.get(path) ?? 0) + 1)
|
|
return actual.stat(path)
|
|
},
|
|
}
|
|
})
|
|
const isolated = await import('@deepseek-ai/dsh-agent-instructions')
|
|
pinHarnessHome(home)
|
|
await isolated.loadBaselineInstructions({ cwd: root, maxBytes: 65536 })
|
|
observedStats.clear()
|
|
pinHarnessHome(home)
|
|
await isolated.loadBaselineInstructions({ cwd: root, maxBytes: 65536 })
|
|
|
|
expect(observedStats.get(join(root, 'AGENTS.md'))).toBe(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
vi.doUnmock('node:fs/promises')
|
|
vi.resetModules()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips an unavailable host candidate but still loads its available sibling', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'CLAUDE.md'), 'claude host sibling rule')
|
|
vi.resetModules()
|
|
vi.doMock('node:fs/promises', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('node:fs/promises')>()
|
|
return {
|
|
...actual,
|
|
stat: async (path: string) => {
|
|
if (path === join(root, 'AGENTS.md')) {
|
|
throw Object.assign(new Error('permission denied'), { code: 'EACCES' })
|
|
}
|
|
return actual.stat(path)
|
|
},
|
|
}
|
|
})
|
|
const isolated = await import('@deepseek-ai/dsh-agent-instructions')
|
|
|
|
pinHarnessHome(home)
|
|
const rendered = await isolated.loadBaselineInstructions({ cwd: root, maxBytes: 65536 })
|
|
|
|
expect(rendered?.text).toContain('claude host sibling rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
vi.doUnmock('node:fs/promises')
|
|
vi.resetModules()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('surfaces host marker metadata failures instead of crossing into an ancestor project', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
const cwd = join(root, 'pkg')
|
|
const markerPath = join(cwd, '.git')
|
|
const failure = Object.assign(new Error(`permission denied: ${markerPath}`), {
|
|
code: 'EACCES',
|
|
path: markerPath,
|
|
})
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'ancestor rule must not load')
|
|
await mkdir(cwd, { recursive: true })
|
|
vi.resetModules()
|
|
vi.doMock('node:fs/promises', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('node:fs/promises')>()
|
|
return {
|
|
...actual,
|
|
stat: async (path: string) => {
|
|
if (path === markerPath) throw failure
|
|
return actual.stat(path)
|
|
},
|
|
}
|
|
})
|
|
const isolated = await import('@deepseek-ai/dsh-agent-instructions')
|
|
|
|
pinHarnessHome(home)
|
|
await expect(isolated.loadBaselineInstructions({ cwd, maxBytes: 65536 }))
|
|
.rejects.toBe(failure)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
vi.doUnmock('node:fs/promises')
|
|
vi.resetModules()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('dynamic nested workspace context injection', () => {
|
|
it('projects a successful file result even when a later sibling aborts the step', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested rule survives an aborted tool batch')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const adapter = new MockAdapter([
|
|
[
|
|
{ type: 'block-start', index: 0, blockType: 'tool-call' },
|
|
{ type: 'block-end', index: 0, block: { type: 'tool-call', id: ToolCallId('read-before-abort'), name: 'read', arguments: '{"file_path":"pkg/deep/file.txt"}' } },
|
|
{ type: 'block-start', index: 1, blockType: 'tool-call' },
|
|
{ type: 'block-end', index: 1, block: { type: 'tool-call', id: ToolCallId('abort-after-read'), name: 'abort_step', arguments: '{}' } },
|
|
{ type: 'finish', reason: { kind: 'tool-calls' } },
|
|
] satisfies StreamChunk[],
|
|
toolCallResponse('read-after-abort', 'read', { file_path: join('pkg', 'deep', 'file.txt') }),
|
|
textResponse('done'),
|
|
])
|
|
await ctx.plugin(LlmRuntime)
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(SessionProjectionRegistry)
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(AgentRegistry)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
await ctx.plugin(AgentLoop, { agents: [] })
|
|
ctx.llm.registerAdapter(['mock'], adapter)
|
|
const agent = await ctx.agentLoop.create(SessionId('workspace-context-abort'), { provider: 'mock', model: 'mock' }, { cwd: root })
|
|
ctx.tools.register(defineContentToolFixture({
|
|
name: 'abort_step',
|
|
description: 'Abort the current test step.',
|
|
parameters: {},
|
|
async execute() {
|
|
agent.cancel({ kind: 'user' })
|
|
return [{ type: 'text', text: 'aborted' }]
|
|
},
|
|
}))
|
|
|
|
agent.followup(createUserMessage({ content: [{ type: 'text', text: 'read and abort' }], source: { kind: 'user' } }))
|
|
await agent.whenIdle()
|
|
expect(agent.session.snapshotEvents().filter(event =>
|
|
event.type === 'user/message' && event.data.source.kind !== 'user',
|
|
)).toHaveLength(0)
|
|
|
|
agent.followup(createUserMessage({ content: [{ type: 'text', text: 'retry the read' }], source: { kind: 'user' } }))
|
|
await agent.whenIdle()
|
|
|
|
const contexts = agent.session.snapshotEvents().filter(event => event.type === 'user/message' && event.data.source.kind !== 'user')
|
|
expect(contexts).toHaveLength(1)
|
|
expect(adapter.requests).toHaveLength(3)
|
|
expect(adapter.requests.at(-1)?.messages.map(blocks => blocksText(blocks.content)).join('\n'))
|
|
.toContain('nested rule survives an aborted tool batch')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('builds persisted digest state without inventing a provider version', () => {
|
|
const state = baselineInstructionState([{
|
|
absolutePath: '/repo/AGENTS.md',
|
|
displayPath: 'AGENTS.md',
|
|
content: 'root rule',
|
|
}])
|
|
|
|
const change = state.changes.get(sk('.', 'AGENTS.md'))
|
|
expect(change).toMatchObject({
|
|
action: 'set',
|
|
path: 'AGENTS.md',
|
|
})
|
|
expect(change?.digest).toMatch(/^[a-f0-9]{40}$/)
|
|
expect(state.versions).toEqual(new Map())
|
|
})
|
|
|
|
it('creates and releases version-cache state only for non-empty updates', async () => {
|
|
const agent = await stubAgent('/repo')
|
|
const cache: InstructionVersionCache = new WeakMap()
|
|
const change = { action: 'set' as const, scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md', digest: 'digest' }
|
|
applyInstructionVersionUpdates(agent.session, [], cache)
|
|
expect(cache.get(agent.session)).toBeUndefined()
|
|
|
|
applyInstructionVersionUpdates(agent.session, [{
|
|
change,
|
|
state: { path: 'AGENTS.md', version: FsVersion('v1'), digest: 'digest', trimmedDigest: 'trimmed' },
|
|
}], cache)
|
|
expect(cache.get(agent.session)?.has(change.scope)).toBe(true)
|
|
applyInstructionVersionUpdates(agent.session, [{ change: { ...change, action: 'remove' } }], cache)
|
|
expect(cache.get(agent.session)).toBeUndefined()
|
|
})
|
|
|
|
it('does not refresh dynamic instructions after the tool signal is aborted', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'nested' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const controller = new AbortController()
|
|
const reason = new Error('cancel dynamic reconciliation')
|
|
controller.abort(reason)
|
|
const exec = stubToolExecution({
|
|
callId: ToolCallId('cancelled-dynamic-read'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'file.txt') },
|
|
agent: await stubAgent(root),
|
|
signal: controller.signal,
|
|
})
|
|
|
|
ctx.emit('tools/result', exec, {
|
|
content: [{ type: 'text', text: 'ok' }],
|
|
isError: false,
|
|
value: null,
|
|
})
|
|
|
|
await Promise.resolve()
|
|
expect(fs.signals).toEqual([])
|
|
expect(exec.agent?.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('attaches newly discovered nested instructions after a successful file read touches a descendant path', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'baseline root rule')
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-nested'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(result.isError).toBe(false)
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({ kind: 'agent-instructions' })
|
|
const queuedSource = ((await syncedAgentInstructions(ctx, agent))).source
|
|
expect(queuedSource).toMatchObject({ kind: 'agent-instructions', form: 'instructions' })
|
|
expect(queuedSource.kind === 'agent-instructions' && queuedSource.changes.some(change =>
|
|
change.action === 'set'
|
|
&& change.scope === sk('pkg', 'AGENTS.md')
|
|
&& change.path === join('pkg', 'AGENTS.md'))).toBe(true)
|
|
const source = ((await syncedAgentInstructions(ctx, agent))).source
|
|
const firstChange = source?.kind === 'agent-instructions'
|
|
? source.changes.find(change => change.scope === sk('pkg', 'AGENTS.md'))
|
|
: undefined
|
|
const changeDigest = typeof firstChange === 'object' && firstChange !== null && !Array.isArray(firstChange)
|
|
? firstChange.digest
|
|
: undefined
|
|
expect(changeDigest).toMatch(/^[a-f0-9]{40}$/)
|
|
const text = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(text).toContain('nested package rule')
|
|
expect(text).not.toContain('<agent-instructions')
|
|
expect(text).toContain('baseline root rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('finishes a committed file-result projection after the tool signal ends', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const controller = new AbortController()
|
|
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: controller.signal,
|
|
callId: ToolCallId('read-before-signal-end'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'file.txt') },
|
|
agent,
|
|
}), { content: [{ type: 'text', text: 'ok' }], isError: false, value: null })
|
|
controller.abort(new Error('tool execution ended'))
|
|
|
|
expect(blocksText((await agentInstructionsOf(agent)).content)).toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads every configured instruction candidate present in a nested scope', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'native package rule')
|
|
await write(join(root, 'pkg/CLAUDE.local.md'), 'local package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, {
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['CLAUDE.local.md', 'AGENTS.md', 'CLAUDE.md'],
|
|
})
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-configured-nested-candidate'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
const text = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'CLAUDE.local.md')}`)
|
|
expect(text).toContain('local package rule')
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(text).toContain('native package rule')
|
|
expect(text.indexOf(join('pkg', 'CLAUDE.local.md'))).toBeLessThan(text.indexOf(join('pkg', 'AGENTS.md')))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('attaches a nested base file and its local overlay together by default', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'baseline root rule')
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested base rule')
|
|
await write(join(root, 'pkg/AGENTS.local.md'), 'nested local rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-nested-overlay'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
const source = ((await syncedAgentInstructions(ctx, agent))).source
|
|
const changes = source?.kind === 'agent-instructions'
|
|
? source.changes
|
|
: []
|
|
expect(changes).toEqual(expect.arrayContaining([
|
|
expect.objectContaining({ action: 'set', path: join('pkg', 'AGENTS.md') }),
|
|
expect.objectContaining({ action: 'set', path: join('pkg', 'AGENTS.local.md') }),
|
|
]))
|
|
const text = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(text).toContain('nested base rule')
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.local.md')}`)
|
|
expect(text).toContain('nested local rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not attach a nested local overlay when the overlay is disabled', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested base rule')
|
|
await write(join(root, 'pkg/AGENTS.local.md'), 'nested local rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, {
|
|
maxBytes: 65536,
|
|
localInstructionFileCandidates: [],
|
|
})
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-nested-overlay-disabled'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
const text = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(text).not.toContain(join('pkg', 'AGENTS.local.md'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not attach nested instructions again for the same session once a path has been loaded', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-nested-1'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
const second = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-nested-2'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(second.additionalContexts).toBeUndefined()
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips instruction content reads while provider version and effective state are unchanged', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const instructionPath = join(root, 'pkg/AGENTS.md')
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(instructionPath, { type: 'file', content: 'nested package rule' })
|
|
fs.omitSizes.add(instructionPath)
|
|
fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' })
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-version-fast-path'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
const second = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-with-version-fast-path'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(second.additionalContexts).toBeUndefined()
|
|
expect(fs.readTargets.filter(path => path === instructionPath)).toHaveLength(1)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('re-reads a changed provider version, then refreshes metadata when SHA-1 is unchanged', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const instructionPath = join(root, 'pkg/AGENTS.md')
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(instructionPath, { type: 'file', content: 'same package rule', version: FsVersion('revision-1') })
|
|
fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' })
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-same-digest-version-change'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
fs.entries.set(instructionPath, { type: 'file', content: 'same package rule', version: FsVersion('revision-2') })
|
|
const afterVersionChange = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-same-digest-version-change'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
const afterRefresh = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-version-cache-refresh'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(afterVersionChange.additionalContexts).toBeUndefined()
|
|
expect(afterRefresh.additionalContexts).toBeUndefined()
|
|
await vi.waitFor(() => {
|
|
expect(fs.readTargets.filter(path => path === instructionPath)).toHaveLength(2)
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('isolates instruction version caches between sessions that touch the same scope', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const instructionPath = join(root, 'pkg/AGENTS.md')
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(instructionPath, { type: 'file', content: 'shared path, separate sessions' })
|
|
fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' })
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
|
|
const firstAgent = await stubAgent(root)
|
|
const secondAgent = await stubAgent(root)
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-from-first-session'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent: firstAgent,
|
|
})
|
|
const second = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-from-second-session'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent: secondAgent,
|
|
})
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(second.additionalContexts).toBeUndefined()
|
|
expect(((await syncedAgentInstructions(ctx, firstAgent))).source.kind).toBe('agent-instructions')
|
|
expect(((await syncedAgentInstructions(ctx, secondAgent))).source.kind).toBe('agent-instructions')
|
|
expect(fs.readTargets.filter(path => path === instructionPath)).toHaveLength(2)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('replaces previously loaded instructions when the same file content changes', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'old package rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-change'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await write(join(root, 'pkg/AGENTS.md'), 'new package rule with more detail')
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-change'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'replace', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toBe([
|
|
'<system-reminder>',
|
|
`Updated instructions from: ${join('pkg', 'AGENTS.md')}`,
|
|
'',
|
|
'This file changed after it was loaded. Use the following content instead of the previously loaded instructions from this file.',
|
|
'',
|
|
'new package rule with more detail',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('reconciles distinct sibling candidates as independent scopes', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'native package rule')
|
|
await write(join(root, 'pkg/CLAUDE.md'), 'sibling package rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-both-siblings'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
const firstText = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(firstText).toContain('native package rule')
|
|
expect(firstText).toContain('sibling package rule')
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await rm(join(root, 'pkg/AGENTS.md'))
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-one-sibling-removed'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
// Removing one candidate only removes its own scope; the sibling scope is untouched.
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'remove', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain(`Instructions removed: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).not.toContain('sibling package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('drops a newly discovered sibling whose content duplicates an earlier candidate in the scope', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested rule')
|
|
await write(join(root, 'pkg/CLAUDE.md'), 'nested rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-nested-dup-siblings'), name: 'read', arguments: { file_path: join('pkg', 'deep', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'set', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
const text = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(text.match(/nested rule/g)).toHaveLength(1)
|
|
expect(text).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(text).not.toContain(join('pkg', 'CLAUDE.md'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it.each(['visible', 'claimed'] as const)(
|
|
'keeps unavailable active candidate groups unchanged with cold and warm caches when authority is $s',
|
|
async (authority) => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'pkg/CLAUDE.md'), { type: 'file', content: 'nested rule' })
|
|
fs.throwOnStat.add(join(root, 'pkg/AGENTS.md'))
|
|
const agent = await stubAgent(root)
|
|
const agentsScope = sk('pkg', 'AGENTS.md')
|
|
const loaded = baselineInstructionState([{
|
|
absolutePath: join(root, 'pkg/AGENTS.md'),
|
|
displayPath: join('pkg', 'AGENTS.md'),
|
|
content: 'nested rule',
|
|
version: FsVersion('loaded-agents'),
|
|
}])
|
|
const previous = loaded.changes.get(agentsScope)
|
|
if (previous === undefined) throw new Error('missing AGENTS.md baseline state')
|
|
const authoritative = createUserMessage({
|
|
content: [{ type: 'text', text: 'nested rule' }],
|
|
source: { kind: 'agent-instructions', form: 'instructions', changes: [previous] },
|
|
})
|
|
if (authority === 'visible') {
|
|
agent.session.append('user/message', authoritative, { surfaceOp: 'append' })
|
|
}
|
|
const authorityMessages = authority === 'claimed' ? [authoritative] : []
|
|
|
|
for (const instructionFileCandidates of [
|
|
['AGENTS.md', 'CLAUDE.md'],
|
|
['CLAUDE.md', 'AGENTS.md'],
|
|
]) {
|
|
pinHarnessHome(home)
|
|
const resolved = resolveConfig({
|
|
maxBytes: 65536,
|
|
instructionFileCandidates,
|
|
localInstructionFileCandidates: [],
|
|
})
|
|
const coldCache: InstructionVersionCache = new WeakMap()
|
|
const warmCache: InstructionVersionCache = new WeakMap()
|
|
warmCache.set(agent.session, new Map(loaded.versions))
|
|
const options = {
|
|
cwd: root,
|
|
authorityMessages,
|
|
scopeMessages: [createUserMessage({
|
|
content: [{ type: 'text', text: 'pending baseline duplicate' }],
|
|
source: {
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'set', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
},
|
|
})],
|
|
touchedPaths: [],
|
|
includeBaselineScopes: false,
|
|
signal: testToolSignal,
|
|
}
|
|
|
|
const cold = await reconcileInstructionContext(agent, resolved, coldCache, fs, options)
|
|
const warm = await reconcileInstructionContext(agent, resolved, warmCache, fs, options)
|
|
|
|
expect(cold).toEqual(warm)
|
|
expect(cold).toBeUndefined()
|
|
}
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
},
|
|
)
|
|
|
|
it('skips visible baseline scopes when baseline scopes are excluded from reconciliation', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'repo rule' })
|
|
const agent = await stubAgent(root)
|
|
const rootScope = sk('.', 'AGENTS.md')
|
|
const loaded = baselineInstructionState([{
|
|
absolutePath: join(root, 'AGENTS.md'),
|
|
displayPath: 'AGENTS.md',
|
|
content: 'repo rule',
|
|
version: FsVersion('loaded-agents'),
|
|
}])
|
|
const previous = loaded.changes.get(rootScope)
|
|
if (previous === undefined) throw new Error('missing AGENTS.md baseline state')
|
|
const authoritative = createUserMessage({
|
|
content: [{ type: 'text', text: 'repo rule' }],
|
|
source: { kind: 'agent-instructions', form: 'instructions', changes: [previous] },
|
|
})
|
|
agent.session.append('user/message', authoritative, { surfaceOp: 'append' })
|
|
pinHarnessHome(home)
|
|
const resolved = resolveConfig({ maxBytes: 65536, localInstructionFileCandidates: [] })
|
|
const cache: InstructionVersionCache = new WeakMap()
|
|
cache.set(agent.session, new Map(loaded.versions))
|
|
const options = {
|
|
cwd: root,
|
|
authorityMessages: [],
|
|
scopeMessages: [],
|
|
touchedPaths: [],
|
|
includeBaselineScopes: false,
|
|
signal: testToolSignal,
|
|
}
|
|
|
|
const result = await reconcileInstructionContext(agent, resolved, cache, fs, options)
|
|
|
|
expect(result).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('ignores an unavailable scope whose visible state is already removed', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.throwOnStat.add(join(root, 'pkg/AGENTS.md'))
|
|
const agent = await stubAgent(root)
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'removed nested instructions' }],
|
|
source: {
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'remove', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
},
|
|
}), { surfaceOp: 'append' })
|
|
pinHarnessHome(home)
|
|
const resolved = resolveConfig({
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['AGENTS.md'],
|
|
localInstructionFileCandidates: [],
|
|
})
|
|
|
|
const result = await reconcileInstructionContext(agent, resolved, new WeakMap(), fs, {
|
|
cwd: root,
|
|
authorityMessages: [],
|
|
scopeMessages: [],
|
|
touchedPaths: [],
|
|
includeBaselineScopes: false,
|
|
signal: testToolSignal,
|
|
})
|
|
|
|
expect(result).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads one transition when user-global and project scopes resolve to the same file', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'shared rule' })
|
|
const agent = await stubAgent(root)
|
|
pinHarnessHome(root)
|
|
const resolved = resolveConfig({
|
|
maxBytes: 65536,
|
|
instructionFileCandidates: ['AGENTS.md'],
|
|
localInstructionFileCandidates: [],
|
|
})
|
|
|
|
const result = await reconcileInstructionContext(agent, resolved, new WeakMap(), fs, {
|
|
cwd: root,
|
|
authorityMessages: [],
|
|
scopeMessages: [],
|
|
touchedPaths: [],
|
|
includeBaselineScopes: true,
|
|
signal: testToolSignal,
|
|
})
|
|
|
|
expect(result?.context.source).toMatchObject({
|
|
changes: [{ action: 'set', scope: sk(USER_GLOBAL_DIRECTORY, USER_GLOBAL_FILE) }],
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('removes a previously rendered sibling once its content becomes a duplicate of an earlier candidate', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'canonical nested rule')
|
|
await write(join(root, 'pkg/CLAUDE.md'), 'initial divergent nested rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-dup-convergence'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
const firstText = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(firstText).toContain('canonical nested rule')
|
|
expect(firstText).toContain('initial divergent nested rule')
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await write(join(root, 'pkg/CLAUDE.md'), 'canonical nested rule')
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-dup-convergence'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
const convergence = await syncedAgentInstructions(ctx, agent)
|
|
expect(convergence.source).toMatchObject({
|
|
changes: [{ action: 'remove', scope: sk('pkg', 'CLAUDE.md'), path: join('pkg', 'CLAUDE.md') }],
|
|
})
|
|
expect(blocksText(convergence.content)).toContain(`Instructions removed: ${join('pkg', 'CLAUDE.md')}`)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('removes an unchanged sibling when an earlier candidate changes to match its content', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'primary nested rule')
|
|
await write(join(root, 'pkg/CLAUDE.md'), 'secondary nested rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-earlier-converges'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
// Only the earlier candidate changes; the sibling stays byte-identical but now duplicates it.
|
|
await write(join(root, 'pkg/AGENTS.md'), 'secondary nested rule')
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-earlier-converges'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [
|
|
{ action: 'replace', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') },
|
|
{ action: 'remove', scope: sk('pkg', 'CLAUDE.md'), path: join('pkg', 'CLAUDE.md') },
|
|
],
|
|
})
|
|
const text = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
expect(text).toContain(`Instructions removed: ${join('pkg', 'CLAUDE.md')}`)
|
|
expect(text).toContain(`Updated instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('removes previously loaded instructions when no candidate remains in the scope', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'package rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-remove'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await rm(join(root, 'pkg/AGENTS.md'))
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-remove'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'remove', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toBe([
|
|
'<system-reminder>',
|
|
`Instructions removed: ${join('pkg', 'AGENTS.md')}`,
|
|
'',
|
|
'The previously loaded instructions from this file no longer apply.',
|
|
'</system-reminder>',
|
|
].join('\n'))
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('removes a previously loaded instruction file once it resolves to a directory through a symlink', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'package rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-symlink-dir'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
const firstText = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
await appendAdditionalContexts(ctx, agent)
|
|
expect(firstText).toContain('package rule')
|
|
|
|
// The candidate now resolves through a symlink to a directory. A non-file
|
|
// target is a confirmed absence (not unavailable), so the loaded scope is
|
|
// removed; an unavailable classification would emit no change at all.
|
|
await rm(join(root, 'pkg/AGENTS.md'))
|
|
await mkdir(join(root, 'pkg/elsewhere'), { recursive: true })
|
|
await symlink(join(root, 'pkg/elsewhere'), join(root, 'pkg/AGENTS.md'))
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-symlink-dir'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'remove', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain(`Instructions removed: ${join('pkg', 'AGENTS.md')}`)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads a candidate again after a logged removal tombstone', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'first package rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-tombstone'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await rm(join(root, 'pkg/AGENTS.md'))
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-to-create-tombstone'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
await write(join(root, 'pkg/AGENTS.md'), 'restored package rule')
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-tombstone'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
changes: [{ action: 'set', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain(`Additional instructions from: ${join('pkg', 'AGENTS.md')}`)
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('restored package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not report removal when a previously loaded scope is temporarily unavailable', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'provider package rule' })
|
|
fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' })
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-provider-failure'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
fs.throwOnStat.add(join(root, 'pkg/AGENTS.md'))
|
|
const duringFailure = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-during-provider-failure'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(duringFailure.additionalContexts).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('derives loaded nested instructions from resumed session history instead of duplicating them', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-resume'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
await appendAdditionalContexts(ctx, agent)
|
|
const resumed = await stubAgent(root, agent.session.snapshotEvents())
|
|
|
|
const afterResume = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-resume'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent: resumed,
|
|
})
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(afterResume.additionalContexts).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('appends an update during resumed prefix composition when visible nested instructions changed offline', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'old nested rule')
|
|
await write(join(root, 'pkg/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-offline-change'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent: original,
|
|
})
|
|
await appendAdditionalContexts(ctx, original)
|
|
await write(join(root, 'pkg/AGENTS.md'), 'new nested rule after resume')
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
|
|
await composeBaselinePrefix(ctx, resumed)
|
|
|
|
const update = resumed.session.snapshotEvents().findLast(event => event.type === 'user/message' && event.data.source.kind !== 'user')
|
|
expect(update?.type === 'user/message' && update.data.source).toMatchObject({
|
|
changes: [{ action: 'replace', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(update?.type === 'user/message' && blocksText(update.data.content)).toContain('new nested rule after resume')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('re-arms a nested instruction after compaction removes its context message from the surface', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-before-compact'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
const contextSeq = (await appendAdditionalContexts(ctx, agent))!
|
|
const visibleBeforeCompact = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-while-visible'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'compacted summary' }],
|
|
source: checkpointSource('instruction-compaction-3'),
|
|
}), {
|
|
surfaceOp: { op: 'replace', startSeq: SessionSeq(contextSeq), endSeq: SessionSeq(contextSeq) },
|
|
sourceEventSeqs: [SessionSeq(contextSeq)],
|
|
})
|
|
|
|
const afterCompact = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-compact'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(visibleBeforeCompact.additionalContexts).toBeUndefined()
|
|
expect(afterCompact.additionalContexts).toBeUndefined()
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('re-arms an unchanged baseline after compaction removes it from the surface', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'root rule')
|
|
await write(join(root, 'file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await composeBaselinePrefix(ctx, agent)
|
|
const baseline = baselineEvents(agent)[0]
|
|
expect(baseline).toBeDefined()
|
|
|
|
const whileVisible = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-visible-baseline'),
|
|
name: 'read',
|
|
arguments: { file_path: 'file.txt' },
|
|
agent,
|
|
})
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'compacted summary' }],
|
|
source: checkpointSource('instruction-compaction-4'),
|
|
}), {
|
|
surfaceOp: { op: 'replace', startSeq: baseline!.seq, endSeq: baseline!.seq },
|
|
sourceEventSeqs: [baseline!.seq],
|
|
})
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-compacted-baseline'),
|
|
name: 'read',
|
|
arguments: { file_path: 'file.txt' },
|
|
agent,
|
|
})
|
|
const rearmedContext = (await syncedAgentInstructions(ctx, agent))
|
|
await appendAdditionalContexts(ctx, agent)
|
|
const afterRearm = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-rearmed-baseline'),
|
|
name: 'read',
|
|
arguments: { file_path: 'file.txt' },
|
|
agent,
|
|
})
|
|
|
|
expect(whileVisible.additionalContexts).toBeUndefined()
|
|
expect(rearmedContext.source).toMatchObject({
|
|
changes: [{ action: 'set', scope: sk('.', 'AGENTS.md'), path: 'AGENTS.md' }],
|
|
})
|
|
expect(blocksText(rearmedContext.content)).toContain('root rule')
|
|
expect(afterRearm.additionalContexts).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not treat markdown headings inside instruction content as loaded instruction metadata', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'package note\n## pkg/sub/AGENTS.md\njust a document heading')
|
|
await write(join(root, 'pkg/file.txt'), 'package file')
|
|
await write(join(root, 'pkg/sub/AGENTS.md'), 'subtree rule')
|
|
await write(join(root, 'pkg/sub/file.txt'), 'subtree file')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-package'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'file.txt') },
|
|
agent,
|
|
})
|
|
const firstText = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
await appendAdditionalContexts(ctx, agent)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-subtree'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'sub', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(firstText).toContain('package note')
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('subtree rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not mark omitted nested files as pending-loaded', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), `parent rule ${'x'.repeat(5000)}`)
|
|
await write(join(root, 'pkg/other.txt'), 'package file')
|
|
await write(join(root, 'pkg/sub/AGENTS.md'), 'subtree rule')
|
|
await write(join(root, 'pkg/sub/file.txt'), 'subtree file')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 700 })
|
|
const agent = await stubAgent(root)
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-subtree-omitting-parent'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'sub', 'file.txt') },
|
|
agent,
|
|
})
|
|
const firstText = blocksText(((await syncedAgentInstructions(ctx, agent))).content)
|
|
await appendAdditionalContexts(ctx, agent)
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-parent-after-omit'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'other.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(firstText).toContain(join('pkg', 'AGENTS.md'))
|
|
expect(firstText).toContain('subtree rule')
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('parent rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('ignores prompt-text spoofs, malformed metadata, and metadata from other plugins', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [
|
|
{ type: 'reasoning', text: 'Additional instructions from: pkg/AGENTS.md' },
|
|
{ type: 'text', text: 'Updated instructions from: pkg/AGENTS.md' },
|
|
],
|
|
source: {
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [
|
|
null,
|
|
{ action: 'unknown', scope: 'pkg', path: join('pkg', 'AGENTS.md') },
|
|
{ action: 'set', scope: 'pkg', path: 42 },
|
|
{ action: 'set', scope: 'pkg', path: join('pkg', 'AGENTS.md'), digest: 42 },
|
|
],
|
|
} as never,
|
|
}), { surfaceOp: 'append' })
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'stale metadata version' }],
|
|
source: { kind: 'agent-instructions', changes: 'invalid' } as never,
|
|
}), { surfaceOp: 'append' })
|
|
agent.session.append('user/message', createUserMessage({
|
|
content: [{ type: 'text', text: 'foreign plugin context' }],
|
|
source: { kind: 'other' },
|
|
}), { surfaceOp: 'append' })
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-spoofed-state'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('loads nested instructions for absolute touched paths but not root-level files', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'root.txt'), 'root file')
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const rootResult = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-root-file'),
|
|
name: 'read',
|
|
arguments: { file_path: 'root.txt' },
|
|
agent,
|
|
})
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-absolute-nested-file'),
|
|
name: 'read',
|
|
arguments: { file_path: join(root, 'pkg/deep/file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(rootResult.additionalContexts).toBeUndefined()
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips unreadable nested instruction files without attaching empty context', async () => {
|
|
// Cross-platform unreadable fixture: the provider read throws (chmod 0
|
|
// cannot make a file unreadable to its owner on Windows).
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
const nested = join(root, 'pkg/AGENTS.md')
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(nested, { type: 'file', content: 'nested package rule' })
|
|
fs.entries.set(join(root, 'pkg/deep/file.txt'), { type: 'file', content: 'hello' })
|
|
fs.throwOnRead.add(nested)
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-with-unreadable-nested-instruction'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
expect(result.isError).toBe(false)
|
|
expect(result.additionalContexts).toBeUndefined()
|
|
await vi.waitFor(() => {
|
|
expect(fs.readTargets).toContain(nested)
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('preserves a downstream canonical value replacement while queuing workspace context separately', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
ctx.on('tools/post-execute', async () => ({
|
|
kind: 'accept' as const,
|
|
value: {
|
|
path: 'pkg/deep/file.txt',
|
|
offset: 1,
|
|
lines: [{ number: 1, text: 'downstream replacement' }],
|
|
totalLines: 1,
|
|
},
|
|
additionalContexts: [createUserMessage({
|
|
content: [{ type: 'text' as const, text: 'downstream context' }],
|
|
source: { kind: 'downstream' as const },
|
|
})],
|
|
}))
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-with-downstream'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(result.isError).toBe(false)
|
|
if (result.isError) throw new Error('expected read replacement success')
|
|
expect(result.value).toEqual({
|
|
path: 'pkg/deep/file.txt',
|
|
offset: 1,
|
|
lines: [{ number: 1, text: 'downstream replacement' }],
|
|
totalLines: 1,
|
|
})
|
|
expect(blocksText(result.content)).toContain('downstream replacement')
|
|
expect(result.additionalContexts).toHaveLength(1)
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({ kind: 'agent-instructions' })
|
|
expect(((await syncedAgentInstructions(ctx, agent))).source).toMatchObject({
|
|
kind: 'agent-instructions',
|
|
form: 'instructions',
|
|
changes: [{ action: 'set', scope: sk('pkg', 'AGENTS.md'), path: join('pkg', 'AGENTS.md') }],
|
|
})
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('nested package rule')
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).not.toContain('downstream context')
|
|
expect(result.additionalContexts?.[0]).toEqual({
|
|
id: expect.any(String) as unknown,
|
|
role: 'user',
|
|
content: [{ type: 'text', text: 'downstream context' }],
|
|
source: { kind: 'downstream' },
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not attach discovered instructions when a downstream listener blocks the tool call', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
ctx.on('tools/post-execute', async () => ({
|
|
kind: 'block' as const,
|
|
feedback: [{ type: 'text' as const, text: 'blocked downstream' }],
|
|
}))
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-blocked-downstream'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
// The pipeline rejected this touch, so no workspace instructions from it
|
|
// should reach the model, and the block feedback must survive unchanged.
|
|
expect(result.isError).toBe(true)
|
|
expect(blocksText(result.content)).toBe('blocked downstream')
|
|
expect(result.additionalContexts).toBeUndefined()
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not project a file touch when an outer post-execute listener blocks the final result', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
await ctx.plugin(ToolFs)
|
|
let shouldBlock = true
|
|
ctx.on('tools/post-execute', async (_exec, _result, next) => {
|
|
const downstream = await next()
|
|
return shouldBlock
|
|
? { kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'outer policy block' }] }
|
|
: downstream
|
|
})
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const blocked = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('outer-block-first'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
|
|
shouldBlock = false
|
|
const accepted = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('outer-block-retry'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(blocked.isError).toBe(true)
|
|
expect(blocked.additionalContexts).toBeUndefined()
|
|
expect(accepted.isError).toBe(false)
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('projects a successful nested file result independently of a blocked composite result', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(LocalFileSystem, { cwd: '/' })
|
|
await ctx.plugin(ToolFs)
|
|
ctx.tools.register(defineContentToolFixture({
|
|
name: 'composite-read',
|
|
description: 'read through a nested dispatch',
|
|
parameters: {},
|
|
async execute(_args, exec) {
|
|
const nested = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId(`${exec.callId}:nested`),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
...exec.agent === undefined ? {} : { agent: exec.agent },
|
|
parent: exec.token,
|
|
...exec.signal === undefined ? {} : { signal: exec.signal },
|
|
})
|
|
for (const context of nested.additionalContexts ?? []) exec.deferContext(context)
|
|
return nested.content
|
|
},
|
|
}))
|
|
ctx.on('tools/post-execute', async (exec, _result, next) => {
|
|
const downstream = await next()
|
|
return exec.name === 'composite-read'
|
|
? { kind: 'block' as const, feedback: [{ type: 'text' as const, text: 'outer composite block' }] }
|
|
: downstream
|
|
})
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const blocked = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('composite-first'), name: 'composite-read', arguments: {}, agent,
|
|
})
|
|
|
|
expect(blocked.isError).toBe(true)
|
|
expect(blocked.additionalContexts).toBeUndefined()
|
|
expect(blocksText(((await syncedAgentInstructions(ctx, agent))).content)).toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('defers a nested file projection until the enclosing step commits', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'nested package rule' })
|
|
const agent = await stubAgent(root)
|
|
const turnStart = agent.session.append('turn/start', { turn: 1 })
|
|
ctx.emit('session/event', agent.session, turnStart)
|
|
const stepStart = agent.session.append('step/start', { turn: 1, step: 1 })
|
|
ctx.emit('session/event', agent.session, stepStart)
|
|
const outerToken = Symbol('outer-code-run') as ToolExecutionToken
|
|
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
token: Symbol('nested-read') as ToolExecutionToken,
|
|
parent: outerToken,
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('nested-read'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'file.txt') },
|
|
agent,
|
|
}), { content: [], isError: false, value: null })
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
token: Symbol('nested-non-file') as ToolExecutionToken,
|
|
parent: outerToken,
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('nested-non-file'),
|
|
name: 'search',
|
|
arguments: {},
|
|
agent,
|
|
}), { content: [], isError: false, value: null })
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
token: Symbol('second-nested-read') as ToolExecutionToken,
|
|
parent: outerToken,
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('second-nested-read'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'second.txt') },
|
|
agent,
|
|
}), { content: [], isError: false, value: null })
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
token: outerToken,
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('outer-code-run'),
|
|
name: 'run_code',
|
|
arguments: {},
|
|
agent,
|
|
}), { content: [], isError: false, value: null })
|
|
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
|
|
const stepEnd = agent.session.append('step/end', { turn: 1, step: 1 })
|
|
ctx.emit('session/event', agent.session, stepEnd)
|
|
expect(blocksText((await syncedAgentInstructions(ctx, agent)).content))
|
|
.toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('seeds closed step state from existing session history', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'nested package rule' })
|
|
const agent = await stubAgent(root)
|
|
agent.session.append('turn/start', { turn: 1 })
|
|
agent.session.append('step/start', { turn: 1, step: 1 })
|
|
agent.session.append('step/end', { turn: 1, step: 1 })
|
|
agent.session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-closed-step'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'file.txt') },
|
|
agent,
|
|
}), { content: [], isError: false, value: null })
|
|
|
|
expect(blocksText((await syncedAgentInstructions(ctx, agent)).content))
|
|
.toContain('nested package rule')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('ignores failed, aborted, agentless, and non-file final results', async () => {
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const agent = await stubAgent('/')
|
|
const plainResult = { callId: ToolCallId('plain'), content: [], isError: false as const, value: null }
|
|
const aborted = new AbortController()
|
|
aborted.abort(new Error('cancelled'))
|
|
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal, callId: ToolCallId('agentless'), name: 'read', arguments: { file_path: 'file.txt' },
|
|
}), plainResult)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal, callId: ToolCallId('failed'), name: 'read', arguments: { file_path: 'failed/file.txt' }, agent,
|
|
}), { content: [], isError: true, error: { message: 'failed' } })
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: aborted.signal, callId: ToolCallId('aborted'), name: 'read', arguments: { file_path: 'aborted/file.txt' }, agent,
|
|
}), plainResult)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('null-arguments'), name: 'read', arguments: null, agent,
|
|
}), plainResult)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('missing-path'), name: 'read', arguments: {}, agent,
|
|
}), plainResult)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('non-string-path'), name: 'read', arguments: { file_path: 1 }, agent,
|
|
}), plainResult)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('blank-path'), name: 'read', arguments: { file_path: ' ' }, agent,
|
|
}), plainResult)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('non-fs'), name: 'composite', arguments: {}, agent,
|
|
}), plainResult)
|
|
|
|
await Promise.resolve()
|
|
expect(fs.signals).toEqual([])
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
await ctx.fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('warns when an asynchronous file-result projection fails', { timeout: 20_000 }, async () => {
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const root = resolve('/')
|
|
const agent = await stubAgent(root)
|
|
const failure = new Error('projection failed')
|
|
const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => undefined)
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'AGENTS.md'), { type: 'file', content: 'workspace rule' })
|
|
vi.spyOn(agent.inbox, 'prepend').mockImplementationOnce(() => { throw failure })
|
|
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('projection-failure'),
|
|
name: 'read',
|
|
arguments: { file_path: 'file.txt' },
|
|
agent,
|
|
}), { content: [], isError: false, value: null })
|
|
|
|
await vi.waitFor(() => {
|
|
expect(warn).toHaveBeenCalledWith('workspace instruction refresh failed: %o', failure)
|
|
}, { timeout: 10_000 })
|
|
} finally {
|
|
await ctx.fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('does not attach nested instructions when the byte budget is disabled', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 0 })
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-with-disabled-budget'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent: await stubAgent(root),
|
|
})
|
|
|
|
expect(result.isError).toBe(false)
|
|
expect(result.additionalContexts).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('retries a nested instruction touch when only a truncated budget notice was rendered', async () => {
|
|
const root = join(await tempRepo(), 'virtual-repo')
|
|
const home = join(await tempRepo(), 'virtual-home')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRuntime)
|
|
await ctx.plugin(NativeToolExecutor)
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
const instructionPath = join(root, 'pkg/AGENTS.md')
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(instructionPath, { type: 'file', content: 'x'.repeat(1000) })
|
|
fs.entries.set(join(root, 'pkg/file.txt'), { type: 'file', content: 'hello' })
|
|
await ctx.plugin(ToolFs)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 20 })
|
|
const agent = await stubAgent(root)
|
|
|
|
const first = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-tiny-budget-1'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
const second = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-tiny-budget-2'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
expect(first.additionalContexts).toBeUndefined()
|
|
expect(second.additionalContexts).toBeUndefined()
|
|
// Nothing was emitted, and the uncommitted version made the second sync
|
|
// probe the instruction file again — the retry.
|
|
expect(agent.inbox.nextStep).toHaveLength(0)
|
|
expect(fs.readTargets.filter(path => path === instructionPath)).toHaveLength(2)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(dirname(root), { recursive: true, force: true })
|
|
await rm(dirname(home), { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not attach nested instructions after a failed file read', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-missing'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'missing.txt') },
|
|
agent: await stubAgent(root),
|
|
})
|
|
|
|
expect(result.isError).toBe(true)
|
|
expect(result.additionalContexts).toBeUndefined()
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('cleans up its tools/result listener when the plugin fiber is disposed', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'nested package rule')
|
|
await write(join(root, 'pkg/deep/file.txt'), 'hello')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
const fiber = await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
await fiber.dispose()
|
|
const agent = await stubAgent(root)
|
|
|
|
const result = await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('read-after-dispose'),
|
|
name: 'read',
|
|
arguments: { file_path: join('pkg', 'deep', 'file.txt') },
|
|
agent,
|
|
})
|
|
|
|
expect(result.isError).toBe(false)
|
|
expect(result.additionalContexts).toBeUndefined()
|
|
await Promise.resolve()
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('workspace context inbox synchronization', () => {
|
|
const acceptedResult = {
|
|
content: [{ type: 'text' as const, text: 'ok' }],
|
|
isError: false as const,
|
|
value: null,
|
|
}
|
|
|
|
it('keeps one reusable desired context when recovery contains an exact duplicate', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'AGENTS.md'), 'duplicate baseline')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await syncAgentInstructions(ctx, agent)
|
|
const desired = agent.inbox.nextStep[0]!
|
|
agent.inbox.append('next-step', createUserMessage({ content: desired.content, source: desired.source }))
|
|
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
expect(agent.inbox.nextStep).toHaveLength(1)
|
|
expect(agent.inbox.nextStep[0]?.id).toBe(desired.id)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('holds back a dynamic change a one-byte positive render budget cannot represent', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'pkg/AGENTS.md'), { type: 'file', content: 'tiny-budget rule' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 1 })
|
|
const agent = await stubAgent(root)
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('tiny-budget-touch'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
}), acceptedResult)
|
|
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
// One byte cannot semantically represent the transition, so nothing is
|
|
// emitted and nothing commits — the uncommitted version retries on the
|
|
// next touch instead of committing state the model never saw.
|
|
expect(agent.inbox.nextStep).toHaveLength(0)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('settles same-scope replacement and deletion against files instead of pending prose', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'pending version one')
|
|
await write(join(root, 'pkg/file.txt'), 'file')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('pending-v1'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(blocksText(agent.inbox.nextStep[0]?.content)).toContain('pending version one')
|
|
const duplicate = createUserMessage({
|
|
content: agent.inbox.nextStep[0]!.content,
|
|
source: agent.inbox.nextStep[0]!.source,
|
|
})
|
|
agent.inbox.append('next-step', duplicate)
|
|
|
|
await write(join(root, 'pkg/AGENTS.md'), 'pending version two with more detail')
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('pending-v2'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep).toHaveLength(1)
|
|
expect(agent.inbox.nextStep[0]?.id).not.toBe(duplicate.id)
|
|
expect(blocksText(agent.inbox.nextStep[0]?.content)).toContain('pending version two with more detail')
|
|
expect(blocksText(agent.inbox.nextStep[0]?.content)).not.toContain('pending version one')
|
|
|
|
await rm(join(root, 'pkg/AGENTS.md'))
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('pending-delete'), name: 'read', arguments: { file_path: join('pkg', 'file.txt') }, agent,
|
|
})
|
|
await syncAgentInstructions(ctx, agent)
|
|
expect(agent.inbox.nextStep).toEqual([])
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps a completed tool projection when a later pre-step aborts', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'a/AGENTS.md'), { type: 'file', content: 'restored A' })
|
|
fs.entries.set(join(root, 'b/AGENTS.md'), { type: 'file', content: 'restored B' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const first = stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('projected-before-abort'), name: 'read', arguments: { file_path: join('a', 'file.txt') }, agent,
|
|
})
|
|
ctx.emit('tools/result', first, acceptedResult)
|
|
const controller = new AbortController()
|
|
controller.abort(new Error('abort pre-step reconciliation'))
|
|
|
|
await expect(agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step', { modelSelection: { provider: 'mock', model: 'mock' }, messages: [], turn: 1, step: 1, signal: controller.signal },
|
|
async () => ({ kind: 'enter' as const, messages: [] }),
|
|
)).rejects.toThrow('abort pre-step reconciliation')
|
|
|
|
ctx.emit('tools/result', stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('projected-after-abort'), name: 'read', arguments: { file_path: join('b', 'file.txt') }, agent,
|
|
}), acceptedResult)
|
|
await syncAgentInstructions(ctx, agent)
|
|
const text = blocksText(agent.inbox.nextStep[0]?.content)
|
|
expect(text).toContain('restored A')
|
|
expect(text).toContain('restored B')
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('serializes concurrent final results and merges both touched scopes into one pending context', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
try {
|
|
await ctx.plugin(RecordingFileSystem)
|
|
const fs = ctx.fs as RecordingFileSystem
|
|
fs.entries.set(join(root, '.git'), { type: 'directory' })
|
|
fs.entries.set(join(root, 'a/AGENTS.md'), { type: 'file', content: 'scope A' })
|
|
fs.entries.set(join(root, 'b/AGENTS.md'), { type: 'file', content: 'scope B' })
|
|
pinHarnessHome(home)
|
|
await mountAgentInstructionsPlugin(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(root)
|
|
const first = stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('concurrent-a'), name: 'read', arguments: { file_path: join('a', 'file.txt') }, agent,
|
|
})
|
|
const second = stubToolExecution({
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('concurrent-b'), name: 'read', arguments: { file_path: join('b', 'file.txt') }, agent,
|
|
})
|
|
|
|
ctx.emit('tools/result', first, acceptedResult)
|
|
ctx.emit('tools/result', second, acceptedResult)
|
|
await syncAgentInstructions(ctx, agent)
|
|
|
|
await vi.waitFor(() => {
|
|
expect(agent.inbox.nextStep).toHaveLength(1)
|
|
const text = blocksText(agent.inbox.nextStep[0]?.content)
|
|
expect(text).toContain('scope A')
|
|
expect(text).toContain('scope B')
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await ctx.fiber.dispose()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('merges a recovered pending context with a fresh touched scope', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'a/AGENTS.md'), 'recovered scope A')
|
|
await write(join(root, 'a/file.txt'), 'a')
|
|
await write(join(root, 'b/AGENTS.md'), 'fresh scope B')
|
|
await write(join(root, 'b/file.txt'), 'b')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const original = await stubAgent(root)
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('recover-pending-a'), name: 'read', arguments: { file_path: join('a', 'file.txt') }, agent: original,
|
|
})
|
|
await syncAgentInstructions(ctx, original)
|
|
const resumed = await stubAgent(root, original.session.snapshotEvents())
|
|
|
|
await executeToolFixture(ctx, {
|
|
signal: testToolSignal,
|
|
callId: ToolCallId('recover-pending-b'), name: 'read', arguments: { file_path: join('b', 'file.txt') }, agent: resumed,
|
|
})
|
|
await syncAgentInstructions(ctx, resumed)
|
|
|
|
await vi.waitFor(() => {
|
|
expect(resumed.inbox.nextStep).toHaveLength(1)
|
|
const text = blocksText(resumed.inbox.nextStep[0]?.content)
|
|
expect(text).toContain('recovered scope A')
|
|
expect(text).toContain('fresh scope B')
|
|
})
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('enters an offline correction immediately after its claimed stale context', async () => {
|
|
const root = await tempRepo()
|
|
const home = await tempRepo()
|
|
try {
|
|
await mkdir(join(root, '.git'), { recursive: true })
|
|
await write(join(root, 'pkg/AGENTS.md'), 'old claimed rule')
|
|
await write(join(root, 'pkg/file.txt'), 'file')
|
|
const ctx = new Context()
|
|
provideWorkingDirectoryFixture(ctx)
|
|
pinHarnessHome(home)
|
|
await mountFileToolsAndAgentInstructions(ctx, { maxBytes: 65536 })
|
|
const agent = await stubAgent(join(root, 'pkg'))
|
|
await syncedAgentInstructions(ctx, agent)
|
|
const claimed = claimInbox(agent, 'next-step')
|
|
await write(join(root, 'pkg/AGENTS.md'), 'new claimed rule with more detail')
|
|
const downstream = { kind: 'enter' as const, messages: claimed }
|
|
|
|
const decision = await agentEvents(ctx, agent).waterfall(
|
|
'agent/pre-step', { modelSelection: { provider: 'mock', model: 'mock' }, messages: claimed, turn: 1, step: 1, signal: testToolSignal },
|
|
async () => downstream,
|
|
)
|
|
|
|
if (decision.kind !== 'enter') throw new Error('offline correction was rejected')
|
|
expect(decision.messages).toHaveLength(2)
|
|
expect(decision.messages[0]).toBe(claimed[0])
|
|
expect(blocksText(decision.messages[0]?.content)).toContain('old claimed rule')
|
|
expect(blocksText(decision.messages[1]?.content)).toContain('new claimed rule with more detail')
|
|
expect(agent.inbox.nextStep).toHaveLength(0)
|
|
} finally {
|
|
vi.unstubAllEnvs()
|
|
await rm(root, { recursive: true, force: true })
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
})
|
|
|
|
describe('workspace context plugin export shape', () => {
|
|
it('has the namespace-plugin export shape (no stray default) so the Loader keeps name/Config/apply', () => {
|
|
expect('default' in AgentInstructions).toBe(false)
|
|
expect(typeof AgentInstructions.apply).toBe('function')
|
|
|
|
const loader = Object.create(Loader.prototype) as Loader
|
|
const unwrapped = loader.unwrapExports(AgentInstructions) as Record<string, unknown>
|
|
expect(unwrapped).toBe(AgentInstructions)
|
|
expect(unwrapped.name).toBe('agent-instructions')
|
|
expect(unwrapped.Config).toBeDefined()
|
|
expect(typeof unwrapped.apply).toBe('function')
|
|
})
|
|
})
|