Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(links): the bundled packs' sketch verb regexes are NER-only, so markdown links fall through to the tuned matchers (#5882)

garrytanab5ed16723da
diff --git a/docs/architecture/schema-packs.md b/docs/architecture/schema-packs.md
index 6f19f7315e..8fb9724a0f 100644
--- a/docs/architecture/schema-packs.md
+++ b/docs/architecture/schema-packs.md
@@ -169,6 +169,15 @@ that gives `attended` a phrase `regex` decides attendance itself and keeps its
outgoing semantics. Brains extracted before this rule re-derive with
`gbrain extract links --source db --include-frontmatter`.
+A link type's `inference.regex` runs before the in-code link matchers, so a
+pack regex decides the verb for a markdown link when it matches. A rule marked
+`ner_only: true` runs only for NER body mentions (`gbrain extract ner`). The
+bundled `gbrain-base` and `gbrain-base-v2` packs mark their `founded`,
+`works_at`, `invested_in` and `advises` sketch regexes this way, so a bare
+"started" or "joined" near a markdown link no longer labels it `founded` or
+`works_at`; the tuned in-code matchers decide those links. Brains extracted
+before this change re-derive with `gbrain extract links --source db`.
+
## How the agent uses the active pack
Every read + write path consults the active pack at runtime:
diff --git a/src/core/extract-ner.ts b/src/core/extract-ner.ts
index 9a9ad0e7a4..ef19cf7147 100644
--- a/src/core/extract-ner.ts
+++ b/src/core/extract-ner.ts
@@ -82,7 +82,7 @@ export function inferNerLinkType(
): string | null {
if (!targetType) return null;
try {
- return inferLinkTypeFromPack(pack, targetType, context);
+ return inferLinkTypeFromPack(pack, targetType, context, undefined, undefined, { ner: true });
} catch {
return null;
}
diff --git a/src/core/schema-pack/base/gbrain-base-v2.yaml b/src/core/schema-pack/base/gbrain-base-v2.yaml
index 848e5ab782..61dc50330c 100644
--- a/src/core/schema-pack/base/gbrain-base-v2.yaml
+++ b/src/core/schema-pack/base/gbrain-base-v2.yaml
@@ -366,18 +366,22 @@ link_types:
inverse: founded_by
inference:
regex: \b(founded|founder of|co-?founded|started)\b
+ ner_only: true
- name: works_at
inverse: employs
inference:
regex: \b(works? at|employed by|works? for|joined|hired by|ceo of|cto of|cmo of)\b
+ ner_only: true
- name: invested_in
inverse: investor_of
inference:
regex: \b(invested in|backed|seeded|funded|wrote a check)\b
+ ner_only: true
- name: advises
inverse: advised_by
inference:
regex: \b(advises|advisor|advising|advisor to)\b
+ ner_only: true
- name: sourced_from
- name: derived_from
- name: supersedes
diff --git a/src/core/schema-pack/base/gbrain-base.yaml b/src/core/schema-pack/base/gbrain-base.yaml
index b704a73b32..9a14e88949 100644
--- a/src/core/schema-pack/base/gbrain-base.yaml
+++ b/src/core/schema-pack/base/gbrain-base.yaml
@@ -336,18 +336,22 @@ link_types:
- name: founded
inference:
regex: \b(founded|founder of|co-?founded|started)\b
+ ner_only: true
- name: invested_in
inference:
regex: \b(invested in|backed|seeded|funded|wrote a check)\b
+ ner_only: true
- name: advises
inference:
regex: \b(advises|advisor|advising|advisor to)\b
+ ner_only: true
- name: works_at
inference:
regex: \b(works? at|employed by|works? for|joined|hired by|ceo of|cto of|cmo of)\b
+ ner_only: true
- name: yc_partner
diff --git a/src/core/schema-pack/link-inference.ts b/src/core/schema-pack/link-inference.ts
index 8f02ff03e4..3e0038949c 100644
--- a/src/core/schema-pack/link-inference.ts
+++ b/src/core/schema-pack/link-inference.ts
@@ -24,7 +24,9 @@
// pack link_type entry.
// 2. Pack-declared regex matchers (in declaration order from the
// manifest; first match wins). Runs under PageRegexBudget for
-// ReDoS protection.
+// ReDoS protection. A rule marked `ner_only` runs only for NER
+// (`opts.ner`): the bundled packs' sketch regexes label NER body
+// mentions but never override markdown links (#5882).
// 3. Fall-through to the caller's legacy `inferLinkType` for
// gbrain-base's production-quality matching of founded /
// invested_in / advises / works_at + page-role priors.
@@ -58,6 +60,7 @@ export function inferLinkTypeFromPack(
context: string,
budget?: PageRegexBudget,
targetType?: string,
+ opts: { ner?: boolean } = {},
): string | null {
const rules = [
...pack.link_types.filter(lt => lt.inference?.page_type),
@@ -66,6 +69,8 @@ export function inferLinkTypeFromPack(
for (const lt of rules) {
const rule = lt.inference;
if (!rule || (!rule.page_type && !rule.target_type && !rule.regex)) continue;
+ // #5882: an NER-only sketch regex never pre-empts the tuned markdown matchers.
+ if (rule.ner_only && !opts.ner) continue;
if (rule.page_type && rule.page_type !== pageType) continue;
if (rule.target_type && rule.target_type !== targetType) continue;
const pattern = rule.regex;
diff --git a/src/core/schema-pack/manifest-v1.ts b/src/core/schema-pack/manifest-v1.ts
index b08838a30d..7f3dd723ed 100644
--- a/src/core/schema-pack/manifest-v1.ts
+++ b/src/core/schema-pack/manifest-v1.ts
@@ -35,6 +35,8 @@ const LinkInferenceSchema = z.object({
regex: z.string().optional(),
page_type: z.string().optional(),
target_type: z.string().optional(),
+ /** The regex labels body mentions found by NER only; markdown links fall through to the in-code matchers (#5882). */
+ ner_only: z.boolean().optional(),
}).strict();
const LinkTypeSchema = z.object({
diff --git a/test/link-inference-pack.test.ts b/test/link-inference-pack.test.ts
index 1d16a47db8..83dcf5b942 100644
--- a/test/link-inference-pack.test.ts
+++ b/test/link-inference-pack.test.ts
@@ -139,21 +139,36 @@ describe('frontmatterLinkTypeFromPack (T7b)', () => {
// same text routed fine on gbrain-base.
import { readFileSync } from 'node:fs';
import { parseYamlMini } from '../src/core/schema-pack/index.ts';
+import { inferNerLinkType } from '../src/core/extract-ner.ts';
describe('#2117: gbrain-base-v2 NER verb routes', () => {
const p = new URL('../src/core/schema-pack/base/gbrain-base-v2.yaml', import.meta.url);
const v2 = parseSchemaPackManifest(parseYamlMini(readFileSync(p, 'utf-8')), {
path: p.pathname,
});
-
- test('founded/invested_in/advises/works_at resolve via pack regexes', () => {
- expect(inferLinkTypeFromPack(v2, 'person', 'co-founded Acme Corp last year')).toBe('founded');
- expect(inferLinkTypeFromPack(v2, 'person', 'invested in Acme Series A')).toBe('invested_in');
- expect(inferLinkTypeFromPack(v2, 'person', 'advises widget-co on hiring')).toBe('advises');
- expect(inferLinkTypeFromPack(v2, 'person', 'works at widget-co')).toBe('works_at');
+ const p1 = new URL('../src/core/schema-pack/base/gbrain-base.yaml', import.meta.url);
+ const v1 = parseSchemaPackManifest(parseYamlMini(readFileSync(p1, 'utf-8')), { path: p1.pathname });
+
+ test('founded/invested_in/advises/works_at resolve via pack regexes for NER', () => {
+ expect(inferNerLinkType(v2, 'person', 'co-founded Acme Corp last year')).toBe('founded');
+ expect(inferNerLinkType(v2, 'person', 'invested in Acme Series A')).toBe('invested_in');
+ expect(inferNerLinkType(v2, 'person', 'advises widget-co on hiring')).toBe('advises');
+ expect(inferNerLinkType(v2, 'person', 'works at widget-co')).toBe('works_at');
});
test('non-matching text still falls through to null', () => {
- expect(inferLinkTypeFromPack(v2, 'person', 'a sentence with no verb signal')).toBeNull();
+ expect(inferNerLinkType(v2, 'person', 'a sentence with no verb signal')).toBeNull();
+ });
+
+ // #5882: the sketch regexes are NER-only, so markdown link typing falls
+ // through to the tuned in-code matchers instead of labelling a bare
+ // "started" founded or a bare "joined" works_at.
+ test('the sketch regexes never claim a markdown link context', () => {
+ for (const pack of [v2, v1]) {
+ expect(inferLinkTypeFromPack(pack, 'meeting', 'the migration work started on Monday')).not.toBe('founded');
+ expect(inferLinkTypeFromPack(pack, 'note', 'the migration work started on Monday')).toBeNull();
+ expect(inferLinkTypeFromPack(pack, 'note', 'she joined the call late')).toBeNull();
+ expect(inferLinkTypeFromPack(pack, 'note', 'co-founded Acme Corp last year')).toBeNull();
+ }
});
});