fix(schedule): validate exact time-context marker shapes

This commit is contained in:
pku-xht
2026-08-07 20:55:58 +08:00
committed by Tianyi Cui
parent 081b2819e3
commit de3e0a5d1c
4 changed files with 177 additions and 16 deletions

View File

@@ -67,11 +67,19 @@ function validateReading(
event: SessionEvent<'user/message'>,
fail: InvariantFailure,
): void {
const [block] = event.data.content
if (event.data.content.length !== 1 || block?.type !== 'text') {
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(block.text)
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])
@@ -88,17 +96,19 @@ function validateReading(
fail('time-context source must retain package ownership')
}
const sections: unknown = 'sections' in source ? source.sections : undefined
const section: unknown = Array.isArray(sections) ? sections[0] : 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 !== 4
|| source.form !== 'snapshot'
|| !Array.isArray(sections)
|| sections.length !== 1
|| typeof section !== 'object'
|| section === null
|| !('name' in section)
|| section === undefined
|| Object.keys(section).length !== 2
|| section.name !== SOURCE_NAME
|| !('text' in section)
|| section.text !== block.text) {
|| typeof section.text !== 'string'
|| section.text !== blockText) {
fail('time-context source must carry only the exact snapshot text, not request authority')
}
const renderedAuthority = `Session time zone: ${match[4]}.\nClient time zone for this request: ${match[5]}.`

View File

@@ -155,6 +155,90 @@ describe('time-context invariants', () => {
}).toThrow(/must carry only the exact snapshot text/)
})
it('rejects snapshot provenance whose section differs from the model-visible text', async () => {
const ctx = await setup()
const base = event(reading())
const mismatched: SessionEvent<'user/message'> = {
...base,
data: {
...base.data,
source: {
kind: 'plugin',
plugin: 'time-context',
form: 'snapshot',
sections: [{ name: 'time-context', text: 'different' }],
},
},
}
expect(() => {
ctx.emit('session/event', preparing(1, 1), mismatched)
}).toThrow(/must carry only the exact snapshot text/)
})
it('rejects snapshot provenance whose sections are only array-like', async () => {
const ctx = await setup()
const base = event(reading())
const arrayLike: SessionEvent<'user/message'> = {
...base,
data: {
...base.data,
source: {
kind: 'plugin',
plugin: 'time-context',
form: 'snapshot',
sections: { 0: { name: 'time-context', text: reading() }, length: 1 },
} as never,
},
}
expect(() => {
ctx.emit('session/event', preparing(1, 1), arrayLike)
}).toThrow(/must carry only the exact snapshot text/)
})
it.each([
[
'matched non-string text',
{ type: 'text', text: 7 },
[{ name: 'time-context', text: 7 }],
/must contain exactly one text block/,
],
[
'an extra text-block field',
{ type: 'text', text: reading(), extra: true },
[{ name: 'time-context', text: reading() }],
/must contain exactly one text block/,
],
[
'non-string section text',
{ type: 'text', text: reading() },
[{ name: 'time-context', text: 7 }],
/must carry only the exact snapshot text/,
],
[
'an extra section field',
{ type: 'text', text: reading() },
[{ name: 'time-context', text: reading(), extra: true }],
/must carry only the exact snapshot text/,
],
] as const)(
'rejects snapshot provenance with %s',
async (_name, block, sections, diagnostic) => {
const ctx = await setup()
const base = event(reading())
const malformed: SessionEvent<'user/message'> = {
...base,
data: {
...base.data,
content: [block as never],
source: { kind: 'plugin', plugin: 'time-context', form: 'snapshot', sections } as never,
},
}
expect(() => {
ctx.emit('session/event', preparing(1, 1), malformed)
}).toThrow(diagnostic)
},
)
it('rejects package-owned provenance without snapshot sections', async () => {
const ctx = await setup()
const base = event(reading())