Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Merge remote-tracking branch 'origin/master' into capy/gbra-49-p1-temporal-edges

garrytan7f0d2593588d
diff --cc src/core/ops/edge-temporal.ts
index 3980e82a7d,0000000000..4706edfafc
mode 100644,000000..100644
--- a/src/core/ops/edge-temporal.ts
+++ b/src/core/ops/edge-temporal.ts
@@@ -1,82 -1,0 +1,82 @@@
+/**
+ * Temporal edge policy for graph read operations (get_links, get_backlinks,
+ * traverse_graph). Graph reads return live relationships by default; `status`
+ * ('live' | 'ended' | 'all'), `as_of` (YYYY-MM-DD) and `during` (YYYY,
+ * YYYY-MM, YYYY-MM-DD or FROM..UNTIL) choose history. `graph.edge_validity off`
+ * turns the policy off (every edge, as before). Hidden former relationships are
+ * reported through the `gbrain.temporal` response meta and a model-visible
+ * `former_relationships_hidden` notice so an empty result is never mistaken
+ * for "no relationship ever existed".
+ */
+import type { OperationContext, ParamDef } from './contract.ts';
+import { opError } from './contract.ts';
+import type { Notice } from '../agent-output.ts';
+import type { Link } from '../types.ts';
+import { edgeMatchesTemporal, edgeValidityEnabled, parseTemporalParams, utcToday, type EdgeTemporalOpts, type TemporalAnnotation } from '../link-validity.ts';
+
+export const TEMPORAL_EDGE_PARAMS: Record<'status' | 'as_of' | 'during', ParamDef> = {
+ status: { type: 'string', enum: ['live', 'ended', 'all'], description: 'Default live (true now).' },
+ as_of: { type: 'string', description: 'YYYY-MM-DD.' },
+ during: { type: 'string', description: 'Period: 2022, 2022-03 or 2021..2023.' },
+};
+
+
+/** Starter-surface (budgeted) forms: live is the default; `all` = history. */
- export const STARTER_STATUS_PARAM: ParamDef = { type: 'string', enum: ['live', 'all'], description: 'Default live.' };
- export const STARTER_AS_OF_PARAM: ParamDef = { type: 'string', description: 'YYYY-MM-DD' };
++export const STARTER_STATUS_PARAM: ParamDef = { type: 'string', enum: ['live', 'all'] };
++export const STARTER_AS_OF_PARAM: ParamDef = { type: 'string' };
+
+/** Validate the temporal params and resolve the effective policy for this call. */
+export async function resolveEdgeTemporal(ctx: OperationContext, p: Record<string, unknown>, opName: string): Promise<EdgeTemporalOpts> {
+ const parsed = parseTemporalParams({ status: p.status, as_of: p.as_of, during: p.during });
+ if (!parsed.ok) {
+ throw opError('invalid_params', `${opName}: ${parsed.message}`,
+ `Pass ${parsed.param} like status: "all", as_of: "2022-06-30" or during: "2022"; omit it for what is true today.`);
+ }
+ const enabled = await edgeValidityEnabled(ctx.engine);
+ return { ...parsed.opts, disabled: !enabled };
+}
+
+/** Keep the rows matching the policy; count what was hidden. Rows must carry a temporal annotation. */
+export function filterTemporalLinks(links: Link[], temporal: EdgeTemporalOpts): { kept: Link[]; hidden: number } {
+ if (temporal.disabled) return { kept: links, hidden: 0 };
+ const kept = links.filter(l => edgeMatchesTemporal(l as Link & TemporalAnnotation, temporal));
+ // A default (live, today) read keeps the established row shape: every kept
+ // row is live, so the annotation adds nothing. History reads (status, as_of,
+ // during) return each row's status, stints, recorded_at and retired_at.
+ const historyRead = (temporal.status ?? 'live') !== 'live' || !!temporal.asOf || !!temporal.during;
+ const shaped = kept.map(l => {
+ const { first_start: _first, ...rest } = l as Link & { first_start?: unknown };
+ if (historyRead) return rest as Link;
+ const { status: _s, stints: _st, recorded_at: _r, retired_at: _rt, ...plain } = rest as Link & TemporalAnnotation;
+ return plain as Link;
+ });
+ return { kept: shaped, hidden: links.length - kept.length };
+}
+
+/**
+ * Publish what the temporal policy did: response meta always (when the policy
+ * is on), and a notice when it hid relationships, naming the exact call that
+ * shows them.
+ */
+export function reportTemporal(ctx: OperationContext, opName: string, p: Record<string, unknown>, temporal: EdgeTemporalOpts, hidden: number | null): void {
+ if (temporal.disabled) return;
+ const effective = { status: temporal.status ?? 'live', as_of: temporal.asOf ?? utcToday(), ...(temporal.during ? { during: temporal.during } : {}), ...(hidden !== null ? { hidden } : {}) };
+ ctx.emitResponseMeta?.('gbrain.temporal', effective);
+ if (!hidden || (temporal.status ?? 'live') === 'all') return;
+ const args: Record<string, unknown> = {};
+ for (const key of ['slug', 'source_id', 'all_sources', 'depth', 'link_type', 'direction']) if (p[key] !== undefined) args[key] = p[key];
+ args.status = 'all';
+ const notice: Notice = {
+ code: 'former_relationships_hidden',
+ kind: 'info',
+ why: `${hidden} relationship${hidden === 1 ? '' : 's'} that ${hidden === 1 ? 'is' : 'are'} not ${temporal.status === 'ended' ? 'ended' : 'true'} as of ${effective.as_of} ${hidden === 1 ? 'was' : 'were'} left out of this ${opName} result.`,
+ fix: {
+ mcp: { tool: opName, arguments: args },
+ consent: [],
+ actor: 'agent',
+ why: 'Shows every relationship with its status and dates (live, ended, ended_unknown_date); use as_of: YYYY-MM-DD for a past date.',
+ requires_exclusive: false,
+ },
+ };
+ ctx.emitNotice?.(notice);
+}
diff --cc test/fixtures/goldens/cli/tools-json.json
index c4950f0dcf,5e17c00079..7d9251684d
--- a/test/fixtures/goldens/cli/tools-json.json
+++ b/test/fixtures/goldens/cli/tools-json.json
@@@ -2,8 -2,8 +2,8 @@@
"golden": {
"exitCode": 0,
"stderr": "",
- "stdoutBytes": 198727,
- "stdoutSha256": "1bd62e59ff08b005f7796f8bb9ad92de5526d41a67a00f95811b3e7a4ec2d8c8",
- "stdoutBytes": 196986,
- "stdoutSha256": "a94ef05467e333a594813f1214c3c3d5d17174ebf09c587a357b40fde49c668e",
++ "stdoutBytes": 198716,
++ "stdoutSha256": "bd7322fce920c952cc0f01e8a84e9cc9c773791f710891b68e024b0043a931be",
"toolNames": [
"remember",
"entity",
@@@ -1642,10 -1610,6 +1647,9 @@@
"description": "Same as source_id __all__.",
"type": "boolean"
},
+ "as_of": {
- "description": "YYYY-MM-DD",
+ "type": "string"
+ },
"slug": {
"description": "Page slug.",
"type": "string"
@@@ -1653,14 -1617,6 +1657,13 @@@
"source_id": {
"description": "One source, or '__all__'.",
"type": "string"
+ },
+ "status": {
- "description": "Default live.",
+ "enum": [
+ "live",
+ "all"
+ ],
+ "type": "string"
}
},
"required": [
@@@ -1708,10 -1662,6 +1711,9 @@@
"description": "Same as source_id __all__.",
"type": "boolean"
},
+ "as_of": {
- "description": "YYYY-MM-DD",
+ "type": "string"
+ },
"depth": {
"description": "Max depth (cap 10).",
"type": "number"
@@@ -1736,14 -1686,6 +1738,13 @@@
"source_id": {
"description": "One source, or '__all__'.",
"type": "string"
+ },
+ "status": {
- "description": "Default live.",
+ "enum": [
+ "live",
+ "all"
+ ],
+ "type": "string"
}
},
"required": [
diff --cc test/mcp-schema-budget.test.ts
index 4503834462,244ba88ded..42052648b0
--- a/test/mcp-schema-budget.test.ts
+++ b/test/mcp-schema-budget.test.ts
@@@ -51,7 -51,7 +51,7 @@@ const PARAM_DESCRIPTION_HARD_CAP = 200
const TOOL_BUDGETS: Record<string, number> = {
add_timeline_entry: 640, cancel_job: 270, cancel_write_request: 350, capture: 1250, context_pack: 760,
delete_skill: 810, delta: 830, edit_page: 1090, entity: 460, find_anomalies: 520, forget: 560, get_agent_job: 270,
- get_backlinks: 550, get_ingest_log: 280, get_page: 810, get_recent_salience: 660, get_skill: 910,
- get_backlinks: 430, get_ingest_log: 280, get_page: 930, get_recent_salience: 660, get_skill: 910,
++ get_backlinks: 550, get_ingest_log: 280, get_page: 930, get_recent_salience: 660, get_skill: 910,
get_skill_asset: 790, get_write_request: 330, join_brain: 560, leave_brain: 540, list_brain_skillpack: 230,
list_link_sources: 220, list_pages: 1090, list_skills: 670, list_write_requests: 450, put_page: 1320,
put_skill: 1420, query: 3250, recall: 1590, remember: 1370, request_tools: 560, resolve_slugs: 410, search: 1760,