feat(7728): typed triggers on a rule, and a rule says its piece once per session
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Plugin hooks (push) Successful in 22s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Python tests (push) Failing after 1m48s
CI & Build / integration (push) Successful in 2m8s
CI & Build / Build & push image (push) Skipped

Triggers (migration 0130, table rule_triggers): a rule can name the act it
applies to, as a tool, a path glob, a command head or a keyword, checked
exactly against the act rather than by resemblance. With no moment a
trigger delivers its rule on any matching act, even one that reaches no
moment; scoped to a moment, it makes the rule's mount there conditional
on the act matching. A triggered line ranks above a mounted one in the
delivery order and names the trigger that fired. Writable through every
MCP and REST rule door, carried by backup (version 29), and edited in the
rule editor under "Acts that trigger it". The plugin's catch-all hook is
told which tools a trigger can reach.

Lifecycle: a rule's life in a session is one full line, one reminder,
then silence. A repeat whose latest delivery in the session was already a
reminder is left out of both rule arms. A rule that holds stays armed
until it is opened, an opened rule keeps its line, and a full delivery
(after a compaction or once the ledger ages it out) re-arms it. Read from
the delivery records, so it needs no new plugin ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 18:24:39 -05:00
co-authored by Claude Opus 5.5
parent 7df478550f
commit c9e64450ba
21 changed files with 1093 additions and 41 deletions
+15
View File
@@ -62,6 +62,17 @@ export type RuleKind = "rule" | "preference";
export type RuleEnforcement = "enforce" | "hold" | "inform";
/** How a rule ranks among rules of the same enforcement at a crowded moment. */
export type RuleSalience = "high" | "normal" | "low";
/** What a trigger is matched against on the act: the tool called, a path it
* touches, the head of a command it runs, or a word in what it writes. */
export type RuleTriggerKind = "tool" | "path" | "command" | "keyword";
/** A typed predicate on the act, checked exactly. With no `moment` it
* delivers the rule on any matching act; with one, it makes the rule's
* mount on that moment conditional on the act matching. */
export interface RuleTrigger {
kind: RuleTriggerKind;
pattern: string;
moment?: string;
}
/** A new record opened from somewhere that already knows what it is for —
* "Adjust this" on a reply shape opens a preference mounted on that shape's
@@ -105,6 +116,8 @@ export interface Rule {
/** The moments this rule arrives at whenever they happen,
* in catalog order. Present only when the rule is mounted. */
moments?: string[];
/** Present only when the rule has triggers. */
triggers?: RuleTrigger[];
relations?: RuleRelation[];
/** The lessons that point at this rule — the concrete
* situations judged instances of it, plus any suggested and awaiting a
@@ -241,6 +254,8 @@ export interface RuleWrite {
system_ids: number[];
/** Replaces the set; [] unmounts the rule from every moment. */
moments: string[];
/** Replaces the set; [] clears every trigger. */
triggers: RuleTrigger[];
arose_from_id: number | null;
verify_with: string;
expires_when: string;
@@ -9,6 +9,7 @@ import { apiErrorMessage } from "@/api/client";
import {
listRulebooks, listTopics,
type Rule, type RuleEnforcement, type RuleKind, type RulePreset, type RuleSalience,
type RuleTrigger, type RuleTriggerKind,
} from "@/api/rulebooks";
const props = defineProps<{
@@ -50,6 +51,44 @@ const momentCatalog = computed(() => momentsStore.data?.moments ?? []);
const procedureMoments = computed(() =>
ruleMoments.value.filter((m) => m.startsWith(SKILL_PREFIX)),
);
// The acts this rule is triggered by, checked exactly on the server.
const triggers = ref<RuleTrigger[]>([]);
const triggerKind = ref<RuleTriggerKind>("path");
const triggerPattern = ref("");
const triggerMoment = ref("");
const triggerError = ref("");
const TRIGGER_KINDS: { value: RuleTriggerKind; label: string; placeholder: string }[] = [
{ value: "path", label: "Path", placeholder: "alembic/versions/*.py — a file the act touches" },
{ value: "command", label: "Command", placeholder: "git push — the head of a command it runs" },
{ value: "tool", label: "Tool", placeholder: "update_task — the tool it calls" },
{ value: "keyword", label: "Keyword", placeholder: "migration — a word in what it writes" },
];
const triggerPlaceholder = computed(
() => TRIGGER_KINDS.find((k) => k.value === triggerKind.value)?.placeholder ?? "",
);
function triggerLabel(t: RuleTrigger) {
return TRIGGER_KINDS.find((k) => k.value === t.kind)?.label ?? t.kind;
}
function addTrigger() {
const pattern = triggerPattern.value.trim().replace(/\s+/g, " ");
if (!pattern) return;
const moment = triggerMoment.value || undefined;
const same = (t: RuleTrigger) =>
t.kind === triggerKind.value && t.pattern === pattern && (t.moment ?? "") === (moment ?? "");
if (triggers.value.some(same)) {
triggerError.value = "This rule already has that trigger.";
return;
}
triggerError.value = "";
triggers.value.push({ kind: triggerKind.value, pattern, ...(moment ? { moment } : {}) });
triggerPattern.value = "";
}
function removeTrigger(at: number) {
triggers.value.splice(at, 1);
}
const why = ref("");
const howToApply = ref("");
const verifyWith = ref("");
@@ -138,6 +177,7 @@ async function load() {
salience.value = r.salience ?? "normal";
systemIds.value = (r.systems ?? []).map((sys) => sys.id);
ruleMoments.value = [...(r.moments ?? [])];
triggers.value = (r.triggers ?? []).map((t) => ({ ...t }));
why.value = r.why || "";
howToApply.value = r.how_to_apply || "";
verifyWith.value = r.verify_with || "";
@@ -152,6 +192,7 @@ async function load() {
salience.value = "normal";
systemIds.value = [];
ruleMoments.value = [...(props.preset?.moments ?? [])];
triggers.value = [];
why.value = "";
howToApply.value = "";
verifyWith.value = "";
@@ -159,6 +200,9 @@ async function load() {
}
procedureDraft.value = "";
procedureError.value = "";
triggerPattern.value = "";
triggerMoment.value = "";
triggerError.value = "";
await Promise.all([canon.fetchCatalog(), momentsStore.load(), loadHomes()]);
}
@@ -204,6 +248,8 @@ async function save() {
// Always sent, for system_ids' reason: the server reads the list as the
// whole set, so unticking the last moment actually unmounts the rule.
moments: ruleMoments.value,
// Always sent, for the same reason: the list is the whole set.
triggers: triggers.value,
why: why.value,
how_to_apply: howToApply.value,
// Always sent, including empty. The REST door maps "" to NULL, so
@@ -391,6 +437,55 @@ watch(() => props.ruleId, load);
<p v-if="procedureError" class="trigger-warning">{{ procedureError }}</p>
</fieldset>
<fieldset class="moments triggers">
<legend>Acts that trigger it</legend>
<p class="field-note intro">
Matched exactly against what the work does, not by resemblance: a
path it touches, a command it runs, a tool it calls, a word in what it
writes. With no moment, the rule arrives on any act that matches.
With a moment, it arrives at that moment only when the act matches,
so a rule about migrations can sit where files change without arriving
at every edit.
</p>
<ul v-if="triggers.length" class="trigger-list">
<li v-for="(t, i) in triggers" :key="`${t.kind}:${t.pattern}:${t.moment ?? ''}`" class="rule-chip">
<span class="trigger-kind">{{ triggerLabel(t) }}</span>
<code>{{ t.pattern }}</code>
<span v-if="t.moment" class="trigger-at">at <code>{{ t.moment }}</code></span>
<button
type="button"
class="chip-remove"
:aria-label="`Remove the ${triggerLabel(t).toLowerCase()} trigger ${t.pattern}`"
@click="removeTrigger(i)"
>×</button>
</li>
</ul>
<p v-else class="field-note">No triggers: this rule arrives by its moments and by resemblance.</p>
<div class="trigger-add">
<select v-model="triggerKind" class="fs-input" aria-label="What the trigger matches">
<option v-for="k in TRIGGER_KINDS" :key="k.value" :value="k.value">{{ k.label }}</option>
</select>
<input
v-model="triggerPattern"
class="fs-input"
aria-label="The pattern the act must match"
:placeholder="triggerPlaceholder"
@keydown.enter.prevent="addTrigger"
/>
<select v-model="triggerMoment" class="fs-input" aria-label="Only at this moment">
<option value="">Any act</option>
<option v-for="m in momentCatalog" :key="m.name" :value="m.name">Only at {{ m.name }}</option>
</select>
<button
type="button"
class="btn-secondary btn-sm"
:disabled="!triggerPattern.trim()"
@click="addTrigger"
>Add</button>
</div>
<p v-if="triggerError" class="trigger-warning">{{ triggerError }}</p>
</fieldset>
<fieldset v-if="canon.catalog.length" class="areas">
<legend>Areas this rule is about</legend>
<label v-for="entry in canon.catalog" :key="entry.id" class="area-opt">
@@ -569,6 +664,13 @@ legend { padding: 0 0.35rem; font-size: 0.8rem; color: var(--fs-text-tertiary);
.procedure-add { display: flex; gap: var(--fs-space-2); align-items: center; margin-top: var(--fs-space-2); }
.procedure-add input { margin-top: 0; }
.procedure-add .btn-sm { flex: none; }
.trigger-list { list-style: none; padding: 0; margin: 0 0 var(--fs-space-2); display: flex; flex-wrap: wrap; gap: var(--fs-space-2); }
.trigger-list .rule-chip { display: inline-flex; align-items: center; gap: var(--fs-space-1); }
.trigger-kind, .trigger-at { color: var(--fs-text-secondary); font-size: var(--fs-size-tiny); }
.trigger-add { display: flex; flex-wrap: wrap; gap: var(--fs-space-2); align-items: center; margin-top: var(--fs-space-2); }
.trigger-add input { flex: 1 1 14rem; margin-top: 0; }
.trigger-add select { flex: none; width: auto; margin-top: 0; }
.trigger-add .btn-sm { flex: none; }
/* --fs-warning-fg, not --fs-warning: the token set draws the distinction
between the warning HUE and warning text, and this is text. */
.trigger-warning {