Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(graph): explicit grammar needs a relation token, per-relation move pairs, frontmatter timestamp dates; tests for conservative cues (GBRA-49 P1)

garrytan73d7fd94153f
diff --git a/src/core/link-temporal-evidence.ts b/src/core/link-temporal-evidence.ts
index 8fb32b38af..02bf412222 100644
--- a/src/core/link-temporal-evidence.ts
+++ b/src/core/link-temporal-evidence.ts
@@ -24,6 +24,7 @@ import {
relationSemantics, normalizePartialDate, type AssertionTense, type DatePrecision,
type TransitionKind, type TransitionProducer,
} from './link-validity.ts';
+import { KNOWN_LINK_TYPES } from './search/relational-intent.ts';
export interface OwnedRow {
from_slug: string;
@@ -155,7 +156,13 @@ const DATED_LINE_RE = /^\s*(?:[-*]\s*)?\*\*(\d{4}-\d{2}-\d{2})\*\*\s*[|\-–—]
const DATED_HEADING_RE = /^\s*###\s+(\d{4}-\d{2}-\d{2})\s*[-–—]+\s*(.+?)\s*$/;
const EXPLICIT_RE = /\b(Started|Ended)\s+([a-z][a-z0-9_]*)\s+(?:\[\[([^\]|\n]+)(?:\|[^\]\n]*)?\]\]|\[[^\]\n]*\]\(([^)\s]+)\))/g;
-const EXPLICIT_TEST = new RegExp(EXPLICIT_RE.source);
+/**
+ * "Started at [Acme]" is prose, not the grammar: the token after Started/Ended
+ * must name a relation (a known link type, a temporal type, or a snake_case
+ * identifier; non-temporal ones are reported).
+ */
+const isRelationToken = (token: string) => token.includes('_') || KNOWN_LINK_TYPES.has(token) || relationSemantics(token) !== 'reference';
+const hasExplicit = (text: string) => [...text.matchAll(EXPLICIT_RE)].some(m => isRelationToken(m[2]));
interface DatedLine { date: string; text: string; dream: boolean }
@@ -201,6 +208,7 @@ export function deriveTemporalEvidence(page: PageForEvidence, rows: readonly Own
for (const line of lines) {
for (const m of line.text.matchAll(EXPLICIT_RE)) {
const linkType = m[2];
+ if (!isRelationToken(linkType)) continue;
const target = normalizeLinkTarget(m[3] ?? m[4] ?? '');
const semantics = relationSemantics(linkType);
if (!target) { unmatched.push({ line: line.text, reason: 'no_target' }); continue; }
@@ -219,17 +227,17 @@ export function deriveTemporalEvidence(page: PageForEvidence, rows: readonly Own
// qualified ("Acme's round", "Acme alumni"). "Left/moved from [A] to/for
// [B]" also starts B.
for (const line of lines) {
- if (EXPLICIT_TEST.test(line.text)) continue;
+ if (hasExplicit(line.text)) continue;
const refs = referencesIn(line.text);
const eventLine = EVENT_CONTEXT.test(line.text.replace(/\[[^\]\n]*\]\([^)\s]*\)|\[\[[^\]\n]*\]\]/g, ' '));
let prevEnd = 0;
- let prevEndedOnLine = false;
+ let prevEnded = new Set<string>();
for (const ref of refs) {
const window = windowBefore(line.text, ref.index, prevEnd);
- const between = window.replace(/^[^)\]]*(?:\)|\]\])/, '');
+ const between = window.replace(/^.*?(?:\]\([^)]*\)|\]\])/, '');
const qualified = QUALIFIED_AFTER.test(line.text.slice(ref.end));
prevEnd = ref.index + 2;
- let endedHere = false;
+ const endedHere = new Set<string>();
for (const r of temporalRows) {
if (!refersTo(ref.target, other(r)) || !r.link_type) continue;
const cues = cuesFor(r.link_type);
@@ -241,14 +249,14 @@ export function deriveTemporalEvidence(page: PageForEvidence, rows: readonly Own
kind = /\b(?:left|stepped\s+(?:down|off|away)\s+from|resigned\s+from)\s*$/i.test(window) ? 'end'
: /\b(?:(?:re-?)?joined|was\s+(?:added|named|appointed)\s+to)\s*$/i.test(window) ? 'start' : null;
} else kind = cues.end.test(window) ? 'end'
- : cues.start.test(window) || (prevEndedOnLine && /^\s*(?:to|for)\s*$/i.test(between)) ? 'start' : null;
+ : cues.start.test(window) || (prevEnded.has(r.link_type) && /^\s*(?:to|for)\s*$/i.test(between)) ? 'start' : null;
} else if (EVENT_START[r.link_type]?.test(window)) kind = 'start';
if (!kind) continue;
- if (kind === 'end') endedHere = true;
+ if (kind === 'end') endedHere.add(r.link_type);
push({ from_slug: r.from_slug, to_slug: r.to_slug, link_type: r.link_type, kind, occurred_on: line.date,
date_precision: 'day', producer: line.dream ? 'dream' : 'timeline', line_hash: lineHash(line.text) });
}
- prevEndedOnLine = endedHere;
+ prevEnded = endedHere;
}
}
@@ -297,7 +305,9 @@ function stripDatedLines(text: string): string {
function stringish(v: unknown): string | null {
if (v instanceof Date) return v.toISOString().slice(0, 10);
if (typeof v === 'number') return String(v);
- return typeof v === 'string' ? v : null;
+ if (typeof v !== 'string') return null;
+ // A YAML date (`since: 2021-04-09`) round-trips through page storage as a UTC-midnight timestamp.
+ return /^\d{4}-\d{2}-\d{2}T00:00:00(?:\.0+)?Z$/.test(v) ? v.slice(0, 10) : v;
}
function nameMatches(name: string, slug: string): boolean {
diff --git a/test/link-temporal-evidence.test.ts b/test/link-temporal-evidence.test.ts
index ea7b3a86d5..2d91b33d95 100644
--- a/test/link-temporal-evidence.test.ts
+++ b/test/link-temporal-evidence.test.ts
@@ -103,3 +103,67 @@ test('normalizeLinkTarget', () => {
expect(normalizeLinkTarget('../companies/acme-example.md#team')).toBe('companies/acme-example');
expect(normalizeLinkTarget('./people/a')).toBe('people/a');
});
+
+describe('conservative cues (only a cue that governs the relationship moves it)', () => {
+ const acme = row('companies/acme-example', 'works_at');
+ const widget = row('companies/widget-co', 'works_at');
+ const line = (text: string) => `- **2025-03-01** | note — ${text}`;
+
+ test('investing, meeting and event lines never start or end employment', () => {
+ for (const text of [
+ "Joined [Acme](companies/acme-example)'s Series B as an angel investor",
+ 'Back at [Acme](companies/acme-example) for an alumni reunion',
+ 'Joined the [Acme](companies/acme-example) alumni dinner',
+ 'Left [Acme](companies/acme-example) early after the panel',
+ 'Returned to [Acme](companies/acme-example) to give a talk',
+ ]) expect(derive('', line(text), [acme]).transitions).toEqual([]);
+ });
+
+ test('a qualified reference is about something of the organization, not the organization', () => {
+ expect(derive('', line("Left [Acme](companies/acme-example)'s London office"), [acme]).transitions).toEqual([]);
+ expect(derive('', line('Joined [Acme](companies/acme-example) alumni on a trip'), [acme]).transitions).toEqual([]);
+ });
+
+ test("an advisory board possessive starts and ends advises", () => {
+ const adv = row('companies/acme-example', 'advises');
+ expect(brief(derive('', line("Joined [Acme](companies/acme-example)'s advisory board"), [adv]).transitions)).toEqual(['acme-example:advises:start:2025-03-01:timeline']);
+ expect(brief(derive('', line("Stepped down from [Acme](companies/acme-example)'s advisory board"), [adv]).transitions)).toEqual(['acme-example:advises:end:2025-03-01:timeline']);
+ });
+
+ test('moved from / left A to / for B ends A and starts B, for the relation that ended only', () => {
+ for (const text of [
+ 'Moved from [Acme](companies/acme-example) to [Widget](companies/widget-co) as CTO',
+ 'Left [Acme](companies/acme-example) for [Widget](companies/widget-co) (CTO)',
+ 'Switched from [Acme](companies/acme-example) to [Widget](companies/widget-co)',
+ ]) expect(brief(derive('', line(text), [acme, widget]).transitions)).toEqual(['acme-example:works_at:end:2025-03-01:timeline', 'widget-co:works_at:start:2025-03-01:timeline']);
+ const advWidget = row('companies/widget-co', 'advises');
+ expect(brief(derive('', line('Moved from [Acme](companies/acme-example) to [Widget](companies/widget-co)'), [acme, advWidget]).transitions)).toEqual(['acme-example:works_at:end:2025-03-01:timeline']);
+ });
+
+ test('wider join and leave verbs', () => {
+ for (const text of ['Moved on from [Acme](companies/acme-example)', 'Stepped away from [Acme](companies/acme-example)', 'Wrapped up her time at [Acme](companies/acme-example)', 'Last day at [Acme](companies/acme-example)'])
+ expect(brief(derive('', line(text), [acme]).transitions)).toEqual(['acme-example:works_at:end:2025-03-01:timeline']);
+ for (const text of ['Was hired by [Acme](companies/acme-example) as CTO', 'Started at [Acme](companies/acme-example) as CTO', 'Accepted an offer from [Acme](companies/acme-example)', 'Rejoined [Acme](companies/acme-example)'])
+ expect(brief(derive('', line(text), [acme]).transitions)).toEqual(['acme-example:works_at:start:2025-03-01:timeline']);
+ });
+
+ test('"Started at [X]" is prose, not the explicit grammar; a snake_case token still is', () => {
+ expect(derive('', line('Started at [Acme](companies/acme-example) as CTO'), [acme]).unmatched).toEqual([]);
+ expect(derive('', line('Ended reports_to [[people/bob-example]]'), []).unmatched.map(u => u.reason)).toEqual(['not_temporal']);
+ });
+
+ test('present perfect and promotions read as present; plain past tense reads as past', () => {
+ const tense = (compiled: string) => derive(compiled, '', [acme]).tense.get(rowKey(acme));
+ expect(tense('Alice has worked at [Acme](companies/acme-example) for years.')).toBe('present');
+ expect(tense('Alice was promoted to CTO at [Acme](companies/acme-example) last year.')).toBe('present');
+ expect(tense("Alice has spent six years at [Acme](companies/acme-example).")).toBe('present');
+ expect(tense('Alice worked at [Acme](companies/acme-example).')).toBe('past');
+ expect(tense('Alice was CTO at [Acme](companies/acme-example).')).toBe('past');
+ });
+
+ test('frontmatter dates stored as UTC-midnight timestamps', () => {
+ const r = row('companies/acme-example', 'works_at', { link_source: 'frontmatter', origin_field: 'company' });
+ const ev = derive('', '', [r], { company: [{ name: 'companies/acme-example', since: '2019-07-14T00:00:00.000Z' }, { name: 'companies/acme-example', since: '2023-09-16T00:00:00.000Z' }] });
+ expect(ev.transitions.map(t => `${t.kind}:${t.occurred_on}`)).toEqual(['start:2019-07-14', 'start:2023-09-16']);
+ });
+});