[engine] DaySentence: The folded petal waits until opened, one a day, never lost

This commit is contained in:
2026-11-02 08:55:05 +01:00
parent ad41c58730
commit 636c0bcbc0
3 changed files with 389 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
/**
* The rose's folded petal: the day's sentence (.agents/features/day-sentence.md).
*
* Pure: the log, the library, the clock and the place are parameters. Which sentence
* waits is a function of what was opened, never of the days that passed, so a
* petal not opened is never lost (CLAUDE.md §5.5) and a gap is never shown.
*/
import type { HopeSentence, SentenceOpened } from '@rosea/schema';
import { seeded, type RoseKeys } from '../rose/rose.js';
import { nextInvitation, type Place } from '../sun/sun.js';
import { daysBetween, localDay, type LocalDay } from '../time/localDay.js';
/** The petal's tuning. The author's to change (day-sentence.md P3, P4). */
export const PETAL = {
/** The last petal opened at most this many local days ago: she also remembers an earlier one. */
nearDays: 3,
/** The good moment: this long before the sun module's invitation… */
beforeMinutes: 60,
/** …until this long after it. */
afterMinutes: 60
} as const;
const MINUTE = 60_000;
/** What the petal needs to know of a library sentence. */
export interface PetalSentence {
readonly id: HopeSentence['id'];
readonly use: HopeSentence['use'];
readonly source: { readonly verifiedAt: HopeSentence['source']['verifiedAt'] };
}
export type PetalNow =
/** No sunset sentence in the library: no petal, and nothing said about one. */
| { readonly kind: 'none' }
| { readonly kind: 'folded'; readonly id: string }
/** Opened today. `remembered` is an earlier one she recalls, on a nearby day (P3). */
| { readonly kind: 'open'; readonly id: string; readonly remembered: string | null };
const byKeys = (keys: RoseKeys): ReadonlySet<string> => new Set(typeof keys === 'string' ? [keys] : keys);
/**
* The one order the petals come in (P1): sunset sentences by the day they were
* verified, then by a hash of the id, so the first days are not all one book and a
* sentence verified later always comes after the ones already there.
*/
export function petalOrder(sentences: readonly PetalSentence[]): string[] {
const seen = new Set<string>();
const sunset: { id: string; day: LocalDay; mix: number }[] = [];
for (const s of sentences) {
if (!s.use.includes('sunset') || seen.has(s.id)) continue;
seen.add(s.id);
sunset.push({ id: s.id, day: s.source.verifiedAt, mix: seeded(s.id, s.source.verifiedAt, 'petal') });
}
sunset.sort((a, b) => (a.day < b.day ? -1 : a.day > b.day ? 1 : a.mix - b.mix || (a.id < b.id ? -1 : 1)));
return sunset.map((s) => s.id);
}
/** The first opening of each local day, in the order they happened (P2). Later ones that day changed nothing. */
function dayOpenings(opened: readonly SentenceOpened[], keys: RoseKeys): SentenceOpened[] {
const mine = byKeys(keys);
const sorted = opened.filter((e) => mine.has(e.from)).sort((a, b) => a.ts - b.ts);
const firstOfDay = new Map<LocalDay, SentenceOpened>();
for (const e of sorted) if (!firstOfDay.has(e.localDay)) firstOfDay.set(e.localDay, e);
return [...firstOfDay.values()];
}
/** The rose's petal at `now`, for the rose named by `keys`. */
export function petalAt(
sentences: readonly PetalSentence[],
opened: readonly SentenceOpened[],
keys: RoseKeys,
now: number,
timeZone: string
): PetalNow {
const today = localDay(now, timeZone);
const days = dayOpenings(opened, keys);
const order = petalOrder(sentences);
const todays = days.find((e) => e.localDay === today);
if (todays !== undefined) {
const before = days.filter((e) => e.localDay < today);
const previous = before.at(-1);
let remembered: string | null = null;
if (previous !== undefined && daysBetween(previous.localDay, today) <= PETAL.nearDays) {
const known = new Set(order);
const earlier = [...new Set(before.map((e) => e.messageId))].filter((id) => id !== todays.messageId && known.has(id));
if (earlier.length > 0) remembered = earlier[Math.floor(seeded(todays.from, today, 'remember') * earlier.length)] ?? null;
}
return { kind: 'open', id: todays.messageId, remembered };
}
if (order.length === 0) return { kind: 'none' };
const lastOpened = new Map<string, number>();
for (const e of days) lastOpened.set(e.messageId, e.ts);
const never = order.find((id) => !lastOpened.has(id));
if (never !== undefined) return { kind: 'folded', id: never };
// Every one has been opened: the one opened longest ago comes round again.
let oldest = order[0]!;
for (const id of order) if (lastOpened.get(id)! < lastOpened.get(oldest)!) oldest = id;
return { kind: 'folded', id: oldest };
}
/**
* The best moment to open the petal (P4): around the sun module's invitation, the
* sunset, or in polar day the lowest light, in polar night the brightest twilight.
* It changes only what she says.
*/
export function goodMoment(now: number, place: Place): boolean {
const { at } = nextInvitation(now - PETAL.afterMinutes * MINUTE, place);
return now >= at - PETAL.beforeMinutes * MINUTE;
}
/** The event the log keeps when the petal is opened. The day comes from the one module that computes days. */
export function sentenceOpened(p: {
readonly now: number;
readonly timeZone: string;
readonly from: string;
readonly messageId: string;
readonly clientVersion: string;
}): SentenceOpened {
return {
kind: 'sentence-opened', ts: Math.round(p.now), localDay: localDay(p.now, p.timeZone),
from: p.from, messageId: p.messageId, clientVersion: p.clientVersion
};
}
/** What makes two openings the same: the same moment, rose and sentence. */
export const openedIdentity = (e: SentenceOpened): string => `${e.ts}|${e.from}|${e.messageId}`;
/** The openings in `incoming` that `existing` does not hold yet (bringing a rose back twice adds nothing twice). */
export function newOpened(existing: readonly SentenceOpened[], incoming: readonly SentenceOpened[]): SentenceOpened[] {
const seen = new Set(existing.map(openedIdentity));
const out: SentenceOpened[] = [];
for (const e of incoming) {
const id = openedIdentity(e);
if (!seen.has(id)) {
seen.add(id);
out.push(e);
}
}
return out;
}