SPB Git forge
7commits 1branches 0releases
229.0 KBsize
maindefault branch
12 days agolast push
TypeScript 91.8% HTML 3.2% JavaScript 3% SQL 1.4% CSS 0.7%
1.2 KB · 33 lines typescript
Raw Blame History
1/**2 * Navigation safety (§41): keep recent page fingerprints / urls / actions and detect A→B→A→B patterns.3 */4export class LoopDetector {5  private fingerprints: string[] = [];6  private actions: string[] = [];7  constructor(private readonly window = 12) {}89  record(pageFingerprint: string, actionKey: string): void {10    this.fingerprints.push(pageFingerprint);11    this.actions.push(actionKey);12    if (this.fingerprints.length > this.window) this.fingerprints.shift();13    if (this.actions.length > this.window) this.actions.shift();14  }1516  /** True when the last four page states alternate (A B A B) or the same page repeats 4×. */17  detect(): { loop: boolean; pattern?: string } {18    const f = this.fingerprints;19    if (f.length >= 4) {20      const [a, b, c, d] = f.slice(-4);21      if (a === c && b === d && a !== b) return { loop: true, pattern: "ABAB" };22      if (a === b && b === c && c === d) return { loop: true, pattern: "AAAA" };23    }24    const acts = this.actions.slice(-6);25    if (acts.length === 6 && new Set(acts).size === 1 && !acts[0]!.startsWith("SCROLL_DOWN")) return { loop: true, pattern: "same_action×6" };26    return { loop: false };27  }2829  recentActionTypes(): string[] {30    return this.actions.map((a) => a.split(":")[0]!);31  }32}33