Skip to content

TTSR Injection Lifecycle

This document covers the current Time Traveling Stream Rules (TTSR) runtime path from rule discovery to stream interruption, retry injection, extension notifications, and session-state handling.

At session creation, createAgentSession() loads all discovered rules and constructs a TtsrManager:

const ttsrSettings = settings.getGroup("ttsr");
const ttsrManager = new TtsrManager(ttsrSettings);
const rulesResult = await loadCapability<Rule>(ruleCapability.id, { cwd });
for (const rule of rulesResult.items) {
if (rule.ttsrTrigger) ttsrManager.addRule(rule);
}

loadCapability("rules") deduplicates by rule.name with first-wins semantics (higher provider priority first). Shadowed duplicates are removed before TTSR registration.

Registration is skipped when:

  • rule.ttsrTrigger is absent
  • a rule with the same rule.name was already registered in this manager
  • the regex fails to compile (new RegExp(rule.ttsrTrigger) throws)

Invalid regex triggers are logged as warnings and ignored; session startup continues.

TtsrSettings.enabled is loaded into the manager but is not currently checked in runtime gating. If rules exist, matching still runs.

TTSR detection runs inside AgentSession.#handleAgentEvent.

On turn_start, the stream buffer is reset:

  • ttsrManager.resetBuffer()

When assistant updates arrive and rules exist:

  • monitor text_delta and toolcall_delta
  • append delta into manager buffer
  • call check(buffer)

check() iterates registered rules and returns all matching rules that pass repeat policy (#canTrigger).

3. Trigger decision and immediate abort path

Section titled “3. Trigger decision and immediate abort path”

When one or more rules match:

  1. markInjected(matches) records rule names in manager injection state.
  2. matched rules are queued in #pendingTtsrInjections.
  3. #ttsrAbortPending = true.
  4. agent.abort() is called immediately.
  5. ttsr_triggered event is emitted asynchronously (fire-and-forget).
  6. retry work is scheduled via setTimeout(..., 50).

Abort is not blocked on extension callbacks.

4. Retry scheduling, context mode, and reminder injection

Section titled “4. Retry scheduling, context mode, and reminder injection”

After the 50ms timeout:

  1. #ttsrAbortPending = false
  2. read ttsrManager.getSettings().contextMode
  3. if contextMode === "discard", drop partial assistant output with agent.popMessage()
  4. build injection content from pending rules using ttsr-interrupt.md template
  5. append a synthetic user message containing one <system-interrupt ...> block per rule
  6. call agent.continue() to retry generation

Template payload is:

<system-interrupt reason="rule_violation" rule="{{name}}" path="{{path}}">
...
{{content}}
</system-interrupt>

Pending injections are cleared after content generation.

  • discard: partial/aborted assistant message is removed before retry.
  • keep: partial assistant output remains in conversation state; reminder is appended after it.

TtsrManager tracks #messageCount and per-rule lastInjectedAt.

A rule can trigger only once after it has an injection record.

A rule can re-trigger only when:

  • messageCount - lastInjectedAt >= repeatGap

messageCount increments on turn_end, so gap is measured in completed turns, not stream chunks.

6. Event emission and extension/hook surfaces

Section titled “6. Event emission and extension/hook surfaces”

AgentSessionEvent includes:

{ type: "ttsr_triggered"; rules: Rule[] }

#emitSessionEvent() routes the event to:

  • extension listeners (ExtensionRunner.emit({ type: "ttsr_triggered", rules }))
  • local session subscribers
  • extension API exposes on("ttsr_triggered", ...)
  • hook API exposes on("ttsr_triggered", ...)
  • custom tools receive onSession({ reason: "ttsr_triggered", rules })

Interactive mode uses session.isTtsrAbortPending to suppress showing the aborted assistant stop reason as a visible failure during TTSR interruption, and renders a TtsrNotificationComponent when the event arrives.

7. Persistence and resume state (current implementation)

Section titled “7. Persistence and resume state (current implementation)”

SessionManager has full schema support for injected-rule persistence:

  • entry type: ttsr_injection
  • append API: appendTtsrInjection(ruleNames)
  • query API: getInjectedTtsrRules()
  • context reconstruction includes SessionContext.injectedTtsrRules

TtsrManager also supports restoration via restoreInjected(ruleNames).

In the current runtime path:

  • AgentSession does not append ttsr_injection entries when TTSR triggers.
  • createAgentSession() does not restore existingSession.injectedTtsrRules back into ttsrManager.

Net effect: injected-rule suppression is enforced in-memory for the live process, but is not currently persisted/restored across session reload/resume by this path.

8. Race boundaries and ordering guarantees

Section titled “8. Race boundaries and ordering guarantees”
  • abort is synchronous from TTSR handler perspective (agent.abort() called immediately)
  • retry is deferred by timer (50ms)
  • extension notification is asynchronous and intentionally not awaited before abort/retry scheduling

check() returns all currently matching eligible rules. They are injected as a batch on the next retry message.

During the timer window, state can change (user interruption, mode actions, additional events). The retry call is best-effort: agent.continue().catch(() => {}) swallows follow-up errors.

  • Invalid ttsr_trigger regex: skipped with warning; other rules continue.
  • Duplicate rule names at capability layer: lower-priority duplicates are shadowed before registration.
  • Duplicate names at manager layer: second registration is ignored.
  • contextMode: "keep": partial violating output can remain in context before reminder retry.
  • Repeat-after-gap depends on turn count increments at turn_end; mid-turn chunks do not advance gap counters.