feat(moments): mounted rules arrive when their moment happens, through every door (milestone 458 step 4a, #4922)
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / TypeScript typecheck (push) Successful in 54s
CI & Build / integration (push) Successful in 57s
CI & Build / Python tests (push) Failing after 1m19s
CI & Build / Build & push image (push) Skipped

A rule mounted on a moment now reaches the session when an act reaches
that moment, with no semantic match involved:

- run_moment_arm on the pipeline: a lookup, not a ranked search. Each
  line names the moment and the act that reached it ("at work.deliver,
  reached by `git push`"), so a misfire is visible where it lands and
  can be unmapped in-session. A repeat is cited, not quoted; fresh
  rules are recorded surfaced under source moment_rule with the moment
  in detail. No retrieval_logs row, as for the other lookups, so no
  latency is persisted for this arm.
- rule_scope: a rule's home clause, moved out of semantic_search_rules
  so the moment lookup scopes by the same one.
- rulebooks.rules_on_moments / mounted_moments.
- The plugin door: a catch-all PreToolUse hook (scribe_moment.sh). It
  keeps /moment-tools' answer on disk for five minutes, so a call to a
  tool that cannot reach a mounted rule sends nothing, and an install
  that has mounted nothing sends one request per window. It shares the
  rules ledger with the other arms and fails open silently.
- The MCP door: Scribe's own tools named by the shipped mappings carry
  moment_rules in their response, so a client without the plugin gets
  them too. The hook skips those tools. A guard pins the attach on
  every one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-05 12:21:45 -04:00
co-authored by Claude Opus 5.5
parent cc26054437
commit 78653130d6
24 changed files with 1049 additions and 39 deletions
+2 -1
View File
@@ -19,6 +19,7 @@ from scribe.services import systems as systems_svc
from scribe.services import trash as trash_svc
from scribe.mcp.tools import systems as systems_tools
from scribe.services.note_usage import attach_usage, record_pulled
from scribe.services.moment_delivery import attach_moment_rules
# The payload shape lives in the service (`lesson_to_dict`), shared with the
@@ -245,7 +246,7 @@ async def create_lesson(
await _offer_candidates(uid, data, what, when_to_apply, project_id)
elif no_rule.strip():
await _name_convergence(uid, data, note.id)
return data
return await attach_moment_rules(uid, "create_lesson", {"project_id": project_id}, data)
async def _name_convergence(uid: int, data: dict, lesson_id: int) -> None:
+8 -2
View File
@@ -19,6 +19,7 @@ from scribe.services import task_logs as task_logs_svc
from scribe.services import rulebooks as rulebooks_svc
from scribe.services import trash as trash_svc
from scribe.services.record_refs import refuse_guessed_ids
from scribe.services.moment_delivery import attach_moment_rules
async def list_milestones(project_id: int) -> dict:
@@ -132,7 +133,9 @@ async def create_milestone(
body=body or None,
status=status,
)
return milestone.to_dict()
return await attach_moment_rules(
uid, "create_milestone", {"project_id": project_id}, milestone.to_dict(),
)
async def update_milestone(
@@ -172,7 +175,10 @@ async def update_milestone(
milestone = await milestones_svc.update_milestone(uid, milestone_id, **fields)
if milestone is None:
raise ValueError(f"milestone {milestone_id} not found")
return milestone.to_dict()
return await attach_moment_rules(
uid, "update_milestone", {"status": status, "project_id": project_id},
milestone.to_dict(),
)
async def delete_milestone(milestone_id: int) -> dict:
+2 -1
View File
@@ -23,6 +23,7 @@ from scribe.services import systems as systems_svc
from scribe.services import trash as trash_svc
from scribe.services.note_usage import record_pulled
from scribe.services.record_refs import refuse_guessed_ids
from scribe.services.moment_delivery import attach_moment_rules
async def list_notes(
@@ -236,7 +237,7 @@ async def create_note(
await systems_tools.attach_systems(uid, uid, data, note.id, project_id or None)
await supersession_svc.attach_relations(uid, note.id, data, hint=True)
data.update(dedup_svc.note_overlap_response(overlaps, "note"))
return data
return await attach_moment_rules(uid, "create_note", {"project_id": project_id}, data)
async def update_note(
+2 -1
View File
@@ -14,6 +14,7 @@ from scribe.services import notes as notes_svc
from scribe.services import systems as systems_svc
from scribe.services import trash as trash_svc
from scribe.services.note_usage import record_pulled
from scribe.services.moment_delivery import attach_moment_rules
async def list_processes(
@@ -106,7 +107,7 @@ async def create_process(
)
if system_ids:
await systems_svc.set_record_systems(uid, note.id, system_ids)
return note.to_dict()
return await attach_moment_rules(uid, "create_process", {}, note.to_dict())
async def get_process(name_or_id: str, project_id: int = 0) -> dict:
+4 -3
View File
@@ -22,6 +22,7 @@ from scribe.services import trash as trash_svc
from scribe.services.rule_usage import (
record_rule_outcome, record_rule_pulled,
)
from scribe.services.moment_delivery import attach_moment_rules
# ── Rulebook CRUD ───────────────────────────────────────────────────────
@@ -536,7 +537,7 @@ async def create_rule(
)
data = await rulebooks_svc.rule_detail(uid, rule, system_ids, moments)
data.update(dedup_svc.overlap_response(overlaps, "rule"))
return data
return await attach_moment_rules(uid, "create_rule", {}, data)
async def create_project_rule(
@@ -645,7 +646,7 @@ async def create_project_rule(
)
data = await rulebooks_svc.rule_detail(uid, rule, system_ids, moments)
data.update(dedup_svc.overlap_response(overlaps, "rule"))
return data
return await attach_moment_rules(uid, "create_project_rule", {"project_id": project_id}, data)
async def update_rule(
@@ -878,7 +879,7 @@ async def create_preference(
)
data = await rulebooks_svc.rule_detail(uid, rule, system_ids, moments)
data.update(dedup_svc.overlap_response(overlaps, "preference"))
return data
return await attach_moment_rules(uid, "create_preference", {}, data)
async def update_preference(
+2 -1
View File
@@ -17,6 +17,7 @@ from scribe.services import dedup as dedup_svc
from scribe.services import snippets as snippets_svc
from scribe.services.note_usage import attach_usage, record_pulled
from scribe.services import systems as systems_svc
from scribe.services.moment_delivery import attach_moment_rules
async def list_snippets(
@@ -223,7 +224,7 @@ async def create_snippet(
advice = snippets_svc.trigger_advice(when_to_use)
if advice:
data["trigger_advice"] = advice
return data
return await attach_moment_rules(uid, "create_snippet", {"project_id": project_id}, data)
async def get_snippet(snippet_id: int, project_id: int = 0) -> dict:
+5 -1
View File
@@ -46,6 +46,7 @@ from scribe.services import trash as trash_svc
from scribe.services.note_usage import record_pulled
from scribe.services.record_refs import refuse_guessed_ids
from scribe.services.text import elide
from scribe.services.moment_delivery import attach_moment_rules
# A work log entry is prose, often long — the discipline asks for what was
@@ -466,7 +467,9 @@ async def update_task(
if prefs:
data["reply_preferences"] = prefs
data["report_back"] = REPORT_BACK_CUE + " " + REPLY_PREFERENCES_CUE
return data
return await attach_moment_rules(
uid, "update_task", {"status": status, "project_id": project_id}, data,
)
async def add_task_log(task_id: int, content: str) -> dict:
@@ -734,6 +737,7 @@ async def start_planning(
)
if isinstance(result, dict):
result.update(dedup_svc.batch_overlap_response(overlaps))
await attach_moment_rules(uid, "start_planning", {"project_id": project_id}, result)
return result