Files
deepseek-harness/packages/context/time-context/src/invariant.ts
T
_KermanandTianyi Cui fb79a944f5 refactor(llm): separate durable producer sources from request inputs
Give durable user context its producer-owned source kind instead of a generic plugin wrapper. The V3-to-V4 conversion preserves the frozen rename and collision rules, all own JSON metadata, event coordinates, and historical request-only kinds. Native catalog and JSONL admission retain source validation so unknown attribution can survive without its producer while malformed owned sources are refused.

Use an explicit user-only RequestUserInput for auxiliary LLM prompts that do not own durable message identity. Auto Review and compaction remove their request-only source registrations while retaining prompt content and freezing; provider serializers and projections keep the same wire data, assistant replay isolation, image ordering, and tool correlation. Session writes, Agent delivery, and recorded title requests continue to require identified durable messages.

Bind the saved compatibility policy only to user sources and qualify tmux location attribution without weakening fixed system, model, or tool semantics. Rebuild the current inventory and unpublished V4 acknowledgement on the actual tool-role parent, preserving its rationale and all accepted historical records. The catalog format version records tooling metadata; Session remains V4. Developer-role policy and activation belong to their own feature.

The latest-master Messages adapter, serialization, request-input typing, EpochHeader reservation, and attribution checks pass 103 tests across five files. The retained V3 Messages input-history snapshot passes through the source profile; Host/Client typechecks and all 41 documentation gates pass. The generated comparison records four changed roots and 447 unchanged type fingerprints. Latest-master ARIA changes are retained, with producer source labels applied independently; new Messages fixtures retain their producer names as declared source kinds and use the fixed system-message helper; recorded Session generations are unchanged by this regrouping.

Co-authored-by: Tianyi Cui <[email protected]>
2026-09-18 19:07:12 +08:00

195 lines
8.1 KiB
TypeScript

/** Package-owned durable clock-context invariants. @module @deepseek-ai/dsh-time-context/invariant */
import type { Context } from '@deepseek-ai/cordis'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import type { InvariantFailure, InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import {
deriveBrowserTimeZoneContext,
renderBrowserTimeZoneContext,
} from './request-zone.ts'
import { createTimestampFormatter, formatTimestamp } from './timestamp.ts'
const PACKAGE_NAME = '@deepseek-ai/dsh-time-context'
const SOURCE_NAME = 'time-context'
const READING = new RegExp(
'^Time sampled while preparing turn (\\d+), step (\\d+): '
+ '(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:Z|[+-]\\d{2}:\\d{2})\\[[^\\]]+\\])\\n'
+ '(Browser time zone for this request: .+)\\n'
+ 'Elapsed since the preceding (model-visible message|step context): '
+ '(?:unavailable|(?:(?:\\d+d )?(?:\\d+h )?(?:\\d+m )?\\d+s))\\.$',
)
/** Cordis companion plugin name. */
export const name = 'time-context-invariant'
/** Service required before the companion can reserve package ownership. */
export const inject = ['invariants']
/** Derive the open step boundary at which a time-context reading may append. */
function preparationPosition(history: readonly SessionEvent[], fail: InvariantFailure): { turn: number; step: number } {
let openTurn: number | undefined
let openStep: number | undefined
let requestStarted = false
for (const event of history) {
switch (event.type) {
case 'turn/start': {
openTurn = event.data.turn
openStep = undefined
requestStarted = false
break
}
case 'step/start': {
openStep = event.data.step
requestStarted = false
break
}
case 'request/header': {
requestStarted = true
break
}
case 'step/end': {
openStep = undefined
requestStarted = false
break
}
case 'turn/end': {
openTurn = undefined
openStep = undefined
requestStarted = false
break
}
default:
break
}
}
if (openTurn === undefined) fail('time-context reading must be appended inside an open turn')
if (openStep === undefined) fail('time-context reading must follow step/start')
if (requestStarted) fail('time-context reading must precede request/header')
return { turn: openTurn, step: openStep }
}
/** Collect the entered user messages belonging to one open turn. */
function requestMessages(history: readonly SessionEvent[], turn: number) {
const start = history.findLastIndex(event => event.type === 'turn/start' && event.data.turn === turn)
return history.slice(start + 1)
.flatMap(event => event.type === 'user/message' ? [event.data] : [])
}
/** Validate one plugin-attributed time reading against its session position and timestamp. */
function validateReading(
history: readonly SessionEvent[],
event: SessionEvent<'user/message'>,
fail: InvariantFailure,
): void {
const blockValue: unknown = event.data.content[0]
const block = typeof blockValue === 'object' && blockValue !== null
? blockValue as Record<string, unknown>
: undefined
const blockText = block?.text
if (event.data.content.length !== 1
|| block === undefined
|| Object.keys(block).length !== 2
|| block.type !== 'text'
|| typeof blockText !== 'string') {
fail('time-context messages must contain exactly one text block')
}
const match = READING.exec(blockText)
if (match === null) fail('time-context message does not match the durable reading format')
const turn = Number(match[1])
const step = Number(match[2])
if (!Number.isSafeInteger(turn) || turn < 1 || !Number.isSafeInteger(step) || step < 1) {
fail('time-context turn and step must be positive safe integers')
}
const expected = preparationPosition(history, fail)
if (turn !== expected.turn || step !== expected.step) {
fail(`time-context reading names turn ${turn}/step ${step}, expected turn ${expected.turn}/step ${expected.step}`)
}
const source = event.data.source
/* v8 ignore next 2 -- replay and dispatch callers select this exact package-owned source before validation. */
if (source.kind !== SOURCE_NAME) {
fail('time-context source must retain package ownership')
}
const sections: unknown = 'sections' in source ? source.sections : undefined
const sectionValue: unknown = Array.isArray(sections) ? sections[0] : undefined
const section = typeof sectionValue === 'object' && sectionValue !== null
? sectionValue as Record<string, unknown>
: undefined
if (Object.keys(source).length !== 3
|| source.form !== 'snapshot'
|| !Array.isArray(sections)
|| sections.length !== 1
|| section === undefined
|| Object.keys(section).length !== 2
|| section.name !== SOURCE_NAME
|| section.text !== blockText) {
fail('time-context source must carry only the exact snapshot text, not request authority')
}
const renderedBrowserContext = match[4]
const browserContext = deriveBrowserTimeZoneContext(requestMessages(history, turn))
const expectedBrowserContext = renderBrowserTimeZoneContext(browserContext)
if (renderedBrowserContext !== expectedBrowserContext) {
fail('time-context browser-zone text does not match current-turn user messages')
}
const baseline = match[5]
if ((step === 1) !== (baseline === 'model-visible message')) {
fail(`time-context step ${step} uses the wrong elapsed-time baseline ${JSON.stringify(baseline)}`)
}
const rendered = match[3]
/* v8 ignore next -- the preceding fixed regexp always supplies capture group three. */
if (rendered === undefined) fail('time-context reading omitted its rendered timestamp')
const renderedTime = Date.parse(rendered.replace(/\[[^\]]+\]$/, ''))
if (!Number.isFinite(renderedTime) || !Number.isSafeInteger(event.time)
|| event.time < renderedTime) {
fail('time-context rendered timestamp must parse and not postdate its durable event')
}
if (browserContext.kind === 'resolved') {
let expectedTimestamp: string
try {
expectedTimestamp = formatTimestamp(
renderedTime,
createTimestampFormatter(browserContext.timeZone),
browserContext.timeZone,
)
} catch (error: unknown) {
fail(`time-context browser zone cannot format its durable timestamp: ${String(error)}`)
}
if (rendered !== expectedTimestamp) {
fail('time-context rendered timestamp does not match the unique browser zone')
}
}
}
/* jscpd:ignore-start -- package companions share replay and dispatch plumbing */
/** Validate all package-owned readings already present in one session. */
function validateSession(session: Session, fail: InvariantFailure): void {
// oxlint-disable-next-line typescript/no-deprecated -- Existing Session history read; migration deferred.
const events = session.snapshotEvents()
for (const [index, event] of events.entries()) {
if (event.type !== 'user/message'
|| event.data.source.kind !== SOURCE_NAME) continue
validateReading(events.slice(0, index), event, fail)
}
}
/** Install validation for loaded and newly appended context readings. */
const install: InvariantInstaller = Object.assign((ctx: Context, fail: InvariantFailure) => {
for (const session of ctx.sessions.list()) validateSession(session, fail)
ctx.on('session/created', (session) => { validateSession(session, fail) }, { global: true })
ctx.on('internal/dispatch', (_mode, eventName, args) => {
if (eventName !== 'session/event') return
const [session, event] = args as [Session, SessionEvent]
if (event.type !== 'user/message'
|| event.data.source.kind !== SOURCE_NAME) return
// oxlint-disable-next-line typescript/no-deprecated -- Existing Session history read; migration deferred.
validateReading(session.snapshotEvents(), event, fail)
}, { global: true })
}, { inject: ['sessions'] })
/* jscpd:ignore-end */
/**
* Register the time-context invariant companion.
* @param ctx - Cordis context carrying the invariant service.
* @returns the installed registration's disposer after setup succeeds.
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))