diff --git a/packages/schema/schemas/sentence-opened.schema.json b/packages/schema/schemas/sentence-opened.schema.json new file mode 100644 index 0000000..64a7917 --- /dev/null +++ b/packages/schema/schemas/sentence-opened.schema.json @@ -0,0 +1,39 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://rosea.invalid/schema/sentence-opened.json", + "title": "SentenceOpened", + "description": "The rose's folded petal was opened (CLAUDE.md §6.2, .agents/features/day-sentence.md). Append-only, never edited. The first one of a local day is that day's petal.", + "type": "object", + "additionalProperties": false, + "required": ["kind", "ts", "localDay", "from", "messageId", "clientVersion"], + "properties": { + "kind": { + "const": "sentence-opened" + }, + "ts": { + "description": "Epoch milliseconds. Stays on the device.", + "type": "integer", + "minimum": 0 + }, + "localDay": { + "description": "The player's local day, from engine/src/time/localDay.ts and nowhere else.", + "type": "string", + "pattern": "^\\d{4}-\\d{2}-\\d{2}$" + }, + "from": { + "description": "The key of the rose whose petal it was.", + "type": "string", + "pattern": "^[A-Za-z0-9_-]{32,128}$" + }, + "messageId": { + "description": "The hope-library id that was in the petal. Never text: the pattern admits no space (CLAUDE.md §5.6 rule 1).", + "type": "string", + "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$", + "maxLength": 64 + }, + "clientVersion": { + "type": "string", + "pattern": "^\\d+\\.\\d+\\.\\d+$" + } + } +} diff --git a/packages/schema/src/validate.ts b/packages/schema/src/validate.ts index 7e498dc..efb35d8 100644 --- a/packages/schema/src/validate.ts +++ b/packages/schema/src/validate.ts @@ -10,8 +10,9 @@ import { Ajv, type ErrorObject, type ValidateFunction } from 'ajv'; import careEvent from '../schemas/care-event.schema.json' with { type: 'json' }; import hopeSentence from '../schemas/hope-sentence.schema.json' with { type: 'json' }; +import sentenceOpened from '../schemas/sentence-opened.schema.json' with { type: 'json' }; import trace from '../schemas/trace.schema.json' with { type: 'json' }; -import type { CareEvent, HopeSentence, Trace } from './generated/index.js'; +import type { CareEvent, HopeSentence, SentenceOpened, Trace } from './generated/index.js'; export type SchemaResult = | { readonly ok: true; readonly value: T } @@ -38,4 +39,5 @@ function describe(error: ErrorObject): string { export const validateHopeSentence = wrap(ajv.compile(hopeSentence)); export const validateCareEvent = wrap(ajv.compile(careEvent)); +export const validateSentenceOpened = wrap(ajv.compile(sentenceOpened)); export const validateTrace = wrap(ajv.compile(trace)); diff --git a/packages/schema/test/schemas.test.ts b/packages/schema/test/schemas.test.ts index 049252a..7dde8d1 100644 --- a/packages/schema/test/schemas.test.ts +++ b/packages/schema/test/schemas.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { validateCareEvent, validateHopeSentence, validateTrace } from '../src/validate.js'; +import { validateCareEvent, validateHopeSentence, validateSentenceOpened, validateTrace } from '../src/validate.js'; const KEY_A = 'A'.repeat(43); const KEY_B = 'b_-'.repeat(15); @@ -103,6 +103,26 @@ describe('care event', () => { }); }); +describe('sentence opened (day-sentence.md P6)', () => { + const opened = { kind: 'sentence-opened', ts: 1761500000000, localDay: '2026-10-24', from: KEY_A, messageId: 'ps-23-4', clientVersion: '0.1.0' }; + + it('accepts an opened petal', () => { + expect(validateSentenceOpened(opened)).toMatchObject({ ok: true }); + }); + + it('rejects text in place of an id, so no words can ride in the log', () => { + expect(validateSentenceOpened({ ...opened, messageId: 'The LORD is my shepherd' }).ok).toBe(false); + expect(validateSentenceOpened({ ...opened, messageId: null }).ok).toBe(false); + }); + + it('rejects a missing day, an extra field, or another kind', () => { + const { localDay: _, ...noDay } = opened; + expect(validateSentenceOpened(noDay).ok).toBe(false); + expect(validateSentenceOpened({ ...opened, atSunset: true }).ok).toBe(false); + expect(validateSentenceOpened({ ...opened, kind: 'care' }).ok).toBe(false); + }); +}); + describe('trace', () => { it('accepts a day-bucketed trace', () => { expect(validateTrace(trace)).toMatchObject({ ok: true });