| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- /**
- * Model-facing workspace instruction rendering within an explicit byte budget.
- *
- * @module @deepseek-ai/dsh-workspace-context/render
- */
- import { dirname } from 'node:path'
- import type { InstructionFile, LoadedInstructionFile } from './files.ts'
- const SYSTEM_REMINDER_OPEN = '<system-reminder>'
- const SYSTEM_REMINDER_CLOSE = '</system-reminder>'
- const WORKSPACE_CONTEXT_INTRO = '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.'
- const COMPACT_WORKSPACE_CONTEXT_INTRO = 'Workspace instructions were omitted or truncated to fit the configured byte budget.'
- /** Byte-accounting record for one truncated instruction file. */
- export interface TruncatedInstruction {
- displayPath: string
- originalBytes: number
- includedBytes: number
- }
- /** Model-facing text plus omitted and truncated source records. */
- export interface RenderedWorkspaceContext {
- text: string
- omitted: InstructionFile[]
- truncated: TruncatedInstruction[]
- }
- /** Structured dynamic state persisted outside model-visible prompt prose. */
- export interface WorkspaceInstructionChange {
- action: 'set' | 'replace' | 'remove'
- scope: string
- path: string
- previousPath?: string
- digest?: string
- }
- /** One state transition paired with the content used to render it. */
- export interface ChangeRenderItem {
- change: WorkspaceInstructionChange
- file: LoadedInstructionFile
- }
- interface RenderStyle {
- intro: string
- section(file: LoadedInstructionFile): string
- }
- function byteLength(value: string): number {
- return Buffer.byteLength(value, 'utf8')
- }
- function truncateUtf8(value: string, maxBytes: number): string {
- let truncated = Buffer.from(value, 'utf8').subarray(0, Math.max(0, maxBytes)).toString('utf8')
- while (byteLength(truncated) > maxBytes) {
- truncated = truncated.slice(0, -1)
- }
- return truncated
- }
- function escapeInstructionContent(content: string): string {
- // TODO(instruction-frame-paths): apply the same delimiter neutralization to
- // every interpolated path, scope, and previous path; repository-controlled
- // names can otherwise close the plugin-owned system-reminder frame.
- return content.replaceAll(SYSTEM_REMINDER_CLOSE, '<\\/system-reminder>')
- }
- function sectionText(file: LoadedInstructionFile): string {
- return `Instructions from: ${file.displayPath}\n\n${escapeInstructionContent(file.content)}`
- }
- /**
- * Derive the logical instruction scope from a model-facing path.
- * @param displayPath - project-relative or user-global instruction path.
- * @returns `user-global`, `.`, or the containing project-relative directory.
- */
- export function scopeForDisplayPath(displayPath: string): string {
- if (displayPath === '~/.dsh/AGENTS.md' || displayPath === '$DSH_HOME/AGENTS.md') return 'user-global'
- return dirname(displayPath)
- }
- function additionalSectionText(file: LoadedInstructionFile): string {
- const scope = scopeForDisplayPath(file.displayPath)
- return [
- `Additional instructions from: ${file.displayPath}`,
- '',
- `These instructions apply to work under \`${scope}\`. Use them as guidance when relevant; more specific instructions take precedence. They do not override system, developer, or direct user instructions.`,
- '',
- escapeInstructionContent(file.content),
- ].join('\n')
- }
- const BASELINE_RENDER_STYLE: RenderStyle = { intro: WORKSPACE_CONTEXT_INTRO, section: sectionText }
- function changedSectionText(item: ChangeRenderItem): string {
- const { change, file } = item
- if (change.action === 'set') return additionalSectionText(file)
- if (change.action === 'remove') {
- return `Instructions removed: ${change.path}\n\nThe previously loaded instructions from this file no longer apply.`
- }
- const description = change.previousPath === undefined
- ? 'This file changed after it was loaded. Use the following content instead of the previously loaded instructions from this file.'
- : `The instructions previously loaded from \`${change.previousPath}\` no longer apply. Use the following content for \`${change.scope}\` instead.`
- return [
- `Updated instructions from: ${change.path}`,
- '',
- description,
- '',
- escapeInstructionContent(file.content),
- ].join('\n')
- }
- /**
- * Render one reconciliation batch and retain only transitions that fit.
- * @param items - ordered state transitions and current file contents.
- * @param maxBytes - maximum UTF-8 bytes allowed in the rendered batch.
- * @returns bounded prompt text and the transitions actually represented by it.
- */
- export function renderInstructionChanges(
- items: ChangeRenderItem[],
- maxBytes: number,
- ): { text: string; changes: WorkspaceInstructionChange[] } {
- const byAbsolutePath = new Map(items.map(item => [item.file.absolutePath, item]))
- const style: RenderStyle = {
- intro: '',
- section(file) {
- const item = byAbsolutePath.get(file.absolutePath)
- /* v8 ignore next -- the renderer receives exactly the files used to construct this map. */
- return item === undefined ? '' : changedSectionText({ ...item, file })
- },
- }
- const rendered = renderInstructionContext(items.map(item => item.file), maxBytes, style)
- const omitted = new Set(rendered.omitted.map(file => file.absolutePath))
- return {
- text: rendered.text,
- // TODO(rendered-change-proof): retain a transition only when its semantic
- // notice survived rendering; a tiny compact budget can currently return
- // unrelated notice text while still committing the full state transition.
- changes: items.filter(item => !omitted.has(item.file.absolutePath)).map(item => item.change),
- }
- }
- function markerText(maxBytes: number, omitted: InstructionFile[], truncated: TruncatedInstruction[]): string {
- if (omitted.length === 0 && truncated.length === 0) return ''
- const parts: string[] = []
- if (omitted.length > 0) {
- parts.push(`omitted ${omitted.map(file => file.displayPath).join(', ')}`)
- }
- if (truncated.length > 0) {
- parts.push(`truncated ${truncated.map(item => `${item.displayPath} from ${item.originalBytes} to ${item.includedBytes} bytes`).join(', ')}`)
- }
- return `Workspace instruction budget ${maxBytes} bytes: ${parts.join('; ')}`
- }
- function buildInstructionText(
- files: LoadedInstructionFile[],
- maxBytes: number,
- omitted: InstructionFile[],
- truncated: TruncatedInstruction[],
- style: RenderStyle,
- ): string {
- const marker = markerText(maxBytes, omitted, truncated)
- const body = [marker, style.intro, ...files.map(file => style.section(file))].filter(block => block.length > 0)
- return [SYSTEM_REMINDER_OPEN, body.join('\n\n'), SYSTEM_REMINDER_CLOSE].join('\n')
- }
- function withTruncatedContent(file: LoadedInstructionFile, includedBytes: number): LoadedInstructionFile {
- return { ...file, content: truncateUtf8(file.content, includedBytes) }
- }
- function truncateToFit(
- file: LoadedInstructionFile,
- includedFiles: LoadedInstructionFile[],
- maxBytes: number,
- omitted: InstructionFile[],
- style: RenderStyle,
- ): LoadedInstructionFile {
- const originalBytes = byteLength(file.content)
- let low = 0
- let high = originalBytes
- let best = withTruncatedContent(file, 0)
- while (low <= high) {
- const mid = Math.floor((low + high) / 2)
- const candidate = withTruncatedContent(file, mid)
- const truncated = [{ displayPath: file.displayPath, originalBytes, includedBytes: byteLength(candidate.content) }]
- const text = buildInstructionText([...includedFiles, candidate], maxBytes, omitted, truncated, style)
- if (byteLength(text) <= maxBytes) {
- best = candidate
- low = mid + 1
- } else {
- high = mid - 1
- }
- }
- return best
- }
- function renderInstructionContext(
- files: LoadedInstructionFile[],
- maxBytes: number,
- style: RenderStyle,
- ): RenderedWorkspaceContext {
- if (maxBytes <= 0 || !Number.isFinite(maxBytes)) return { text: '', omitted: files, truncated: [] }
- const fullText = buildInstructionText(files, maxBytes, [], [], style)
- if (byteLength(fullText) <= maxBytes) return { text: fullText, omitted: [], truncated: [] }
- for (let start = 1; start < files.length; start += 1) {
- const included = files.slice(start)
- const omitted = files.slice(0, start).map(file => ({ absolutePath: file.absolutePath, displayPath: file.displayPath }))
- const suffixText = buildInstructionText(included, maxBytes, omitted, [], style)
- if (byteLength(suffixText) <= maxBytes) return { text: suffixText, omitted, truncated: [] }
- }
- const mostSpecific = files.at(-1)
- /* v8 ignore next -- callers only reach this after a non-empty fullText was built. */
- if (mostSpecific === undefined) return { text: '', omitted: [], truncated: [] }
- const omitted = files.slice(0, -1).map(file => ({ absolutePath: file.absolutePath, displayPath: file.displayPath }))
- for (const candidateStyle of [style, { ...style, intro: COMPACT_WORKSPACE_CONTEXT_INTRO }]) {
- const truncatedFile = truncateToFit(mostSpecific, [], maxBytes, omitted, candidateStyle)
- const truncated = [{
- displayPath: mostSpecific.displayPath,
- originalBytes: byteLength(mostSpecific.content),
- includedBytes: byteLength(truncatedFile.content),
- }]
- const text = buildInstructionText([truncatedFile], maxBytes, omitted, truncated, candidateStyle)
- if (byteLength(text) <= maxBytes) return { text, omitted, truncated }
- }
- const truncated = [{
- displayPath: mostSpecific.displayPath,
- originalBytes: byteLength(mostSpecific.content),
- includedBytes: 0,
- }]
- const compactNotice = markerText(maxBytes, omitted, truncated)
- const compactWithHeading = [compactNotice, style.section(withTruncatedContent(mostSpecific, 0))].join('\n\n')
- if (byteLength(compactWithHeading) <= maxBytes) return { text: compactWithHeading, omitted, truncated }
- const text = byteLength(compactNotice) <= maxBytes ? compactNotice : truncateUtf8(compactNotice, maxBytes)
- return { text, omitted, truncated }
- }
- /**
- * Render the baseline instruction chain with deterministic precedence budgeting.
- * @param files - loaded files ordered from broadest to most specific.
- * @param options - required rendering byte budget.
- * @returns bounded baseline prompt text and budget diagnostics.
- */
- export function renderWorkspaceContext(
- files: LoadedInstructionFile[],
- options: { maxBytes: number },
- ): RenderedWorkspaceContext {
- return renderInstructionContext(files, options.maxBytes, BASELINE_RENDER_STYLE)
- }
|