@retaw106/mmc-oc 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/dist/cli.js +1 -1
  2. package/dist/config/live-run-config.d.ts.map +1 -1
  3. package/dist/config-init.js +1 -1
  4. package/dist/{context-95zkksn3.js → context-cx3y8abt.js} +2602 -1594
  5. package/dist/features/magic-context/memory/types.d.ts +1 -0
  6. package/dist/features/magic-context/memory/types.d.ts.map +1 -1
  7. package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
  8. package/dist/features/magic-context/session-project-storage.d.ts +8 -2
  9. package/dist/features/magic-context/session-project-storage.d.ts.map +1 -1
  10. package/dist/features/magic-context/storage-meta-persisted.d.ts +12 -0
  11. package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
  12. package/dist/features/magic-context/storage-meta-session.d.ts +6 -0
  13. package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
  14. package/dist/features/magic-context/storage-tags.d.ts +2 -2
  15. package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
  16. package/dist/features/magic-context/transform-decision-log.d.ts +3 -1
  17. package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
  18. package/dist/hooks/magic-context/compartment-runner-types.d.ts +3 -5
  19. package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
  20. package/dist/hooks/magic-context/compartment-runner.d.ts +2 -1
  21. package/dist/hooks/magic-context/compartment-runner.d.ts.map +1 -1
  22. package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
  23. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +2 -0
  24. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
  25. package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
  26. package/dist/hooks/magic-context/historian-drain-gate.d.ts +11 -0
  27. package/dist/hooks/magic-context/historian-drain-gate.d.ts.map +1 -0
  28. package/dist/hooks/magic-context/lkg-replay-fit.d.ts +118 -0
  29. package/dist/hooks/magic-context/lkg-replay-fit.d.ts.map +1 -0
  30. package/dist/hooks/magic-context/lkg-replay.d.ts +2 -0
  31. package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
  33. package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
  34. package/dist/hooks/magic-context/transform-compartment-phase.d.ts +3 -28
  35. package/dist/hooks/magic-context/transform-compartment-phase.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  37. package/dist/{index-fg34g6a5.js → index-1j34a56j.js} +49 -8
  38. package/dist/{index-5pg0acf7.js → index-amycb7b1.js} +1 -1
  39. package/dist/{index-1mjhzqq7.js → index-dr0gepzw.js} +86 -23
  40. package/dist/{index-xt7y10x6.js → index-n0vnzz6h.js} +1 -1
  41. package/dist/{index-977gxfz0.js → index-ramz5kkm.js} +1 -1
  42. package/dist/{index-bdt9my9y.js → index-wtb9hzk6.js} +130 -50
  43. package/dist/index.js +1 -1
  44. package/dist/{message-time-backfill-zfernxbw.js → message-time-backfill-phrwp2fz.js} +1 -1
  45. package/dist/{read-session-chunk-hp464g13.js → read-session-chunk-82rkqa8j.js} +3 -3
  46. package/dist/{recomp-orchestrator-3qxeqwb9.js → recomp-orchestrator-vc5dkzy2.js} +6 -6
  47. package/dist/{send-session-notification-y29y9fsr.js → send-session-notification-rnc4nrsr.js} +3 -3
  48. package/dist/shared/harness.d.ts +1 -1
  49. package/dist/shared/harness.d.ts.map +1 -1
  50. package/dist/shared/historian-tool-expansions.d.ts +1 -1
  51. package/dist/shared/historian-tool-expansions.d.ts.map +1 -1
  52. package/dist/shared/historian-tool-template.d.ts +2 -2
  53. package/dist/shared/historian-tool-template.d.ts.map +1 -1
  54. package/dist/shared/model-resolution.d.ts +1 -1
  55. package/dist/shared/model-resolution.d.ts.map +1 -1
  56. package/dist/shared/sqlite.d.ts +14 -1
  57. package/dist/shared/sqlite.d.ts.map +1 -1
  58. package/dist/tools/ctx-expand/constants.d.ts +1 -1
  59. package/dist/tools/ctx-expand/constants.d.ts.map +1 -1
  60. package/dist/tools/ctx-expand/render.d.ts +3 -2
  61. package/dist/tools/ctx-expand/render.d.ts.map +1 -1
  62. package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
  63. package/dist/v2/fold/host-media.d.ts +4 -0
  64. package/dist/v2/fold/host-media.d.ts.map +1 -1
  65. package/dist/v2/fold/memory-cache.d.ts +70 -0
  66. package/dist/v2/fold/memory-cache.d.ts.map +1 -0
  67. package/dist/v2/fold/native-codec.d.ts +10 -0
  68. package/dist/v2/fold/native-codec.d.ts.map +1 -0
  69. package/dist/v2/fold/native-replay.d.ts +58 -0
  70. package/dist/v2/fold/native-replay.d.ts.map +1 -0
  71. package/dist/v2/fold/policy.d.ts +24 -0
  72. package/dist/v2/fold/policy.d.ts.map +1 -0
  73. package/dist/v2/hidden-child-record.d.ts +6 -0
  74. package/dist/v2/hidden-child-record.d.ts.map +1 -1
  75. package/dist/v2/hidden-completion.d.ts.map +1 -1
  76. package/dist/v2/hooks/context.d.ts.map +1 -1
  77. package/dist/v2/hooks/types.d.ts +6 -0
  78. package/dist/v2/hooks/types.d.ts.map +1 -1
  79. package/dist/v2/server.js +1819 -574
  80. package/dist/v2/store-reader.d.ts +16 -0
  81. package/dist/v2/store-reader.d.ts.map +1 -1
  82. package/dist/web/api.d.ts.map +1 -1
  83. package/dist/web/context-store.d.ts +2 -0
  84. package/dist/web/context-store.d.ts.map +1 -1
  85. package/dist/web/fixtures.d.ts +41 -0
  86. package/dist/web/fixtures.d.ts.map +1 -1
  87. package/dist/web/host-opencode.d.ts +9 -0
  88. package/dist/web/host-opencode.d.ts.map +1 -1
  89. package/dist/web/main.js +873 -64
  90. package/dist/web/memories.d.ts +18 -2
  91. package/dist/web/memories.d.ts.map +1 -1
  92. package/dist/web/ui/panel.css +3 -0
  93. package/dist/web/ui/panel.js +151 -80
  94. package/dist/web/views.d.ts +71 -9
  95. package/dist/web/views.d.ts.map +1 -1
  96. package/dist/{wrapup-orchestrator-wgb2nd19.js → wrapup-orchestrator-x9xamts2.js} +6 -6
  97. package/package.json +1 -1
  98. package/src/config/live-run-config.ts +28 -0
  99. package/src/shared/harness.ts +1 -1
  100. package/src/shared/historian-tool-defaults.json +6 -6
  101. package/src/shared/historian-tool-expansions.ts +65 -8
  102. package/src/shared/historian-tool-preview-defaults.json +13 -0
  103. package/src/shared/historian-tool-template.ts +48 -14
  104. package/src/shared/model-resolution.ts +1 -1
  105. package/src/shared/prompt-surface-a1-golden.md +2 -2
  106. package/src/shared/sqlite-acquisition-routing.test.ts +18 -0
  107. package/src/shared/sqlite.ts +100 -15
  108. package/src/shared/write-transaction-attribution.test.ts +2 -2
@@ -245,6 +245,7 @@ function installTransactionRouting(db: BetterSqlite3.Database, readonly: boolean
245
245
  configurable: true,
246
246
  writable: true,
247
247
  value: (sql: string) => {
248
+ if (/^\s*(?:COMMIT|END)\b/i.test(sql)) assertTransformWrite();
248
249
  if (/^\s*BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)(?:\s+TRANSACTION)?\s*;?\s*$/i.test(sql)) {
249
250
  // A transaction that ended without passing through exec (for
250
251
  // example an automatic rollback) must not be timed as this one.
@@ -556,6 +557,33 @@ interface StatementOwner {
556
557
  const statementOwners = new WeakMap<Database, StatementOwner>();
557
558
  const FIRST_PRUNE_AT = 256;
558
559
 
560
+ /**
561
+ * Bun's default close (`sqlite3_close_v2`) only releases the native connection
562
+ * once every statement created by prepare() is finalized. The close override
563
+ * in ownPreparedStatement can finalize only statements that are still
564
+ * reachable. Once the garbage collector
565
+ * has marked a one-shot statement dead, its weak reference is already cleared,
566
+ * but the statement is finalized only when its memory is later swept. Until
567
+ * then the closed connection keeps the database, WAL and shared-memory files
568
+ * open, for an unbounded time.
569
+ *
570
+ * `close(true)` finalizes every outstanding statement, reachable or not, and
571
+ * releases the connection immediately. When the caller did not ask for errors
572
+ * and that close fails, fall back to the caller's own close so a teardown path
573
+ * never starts throwing; the fallback can defer the release as before.
574
+ */
575
+ function closeReleasingStatements(
576
+ close: (...args: unknown[]) => unknown,
577
+ args: unknown[],
578
+ ): unknown {
579
+ if (args[0] === true) return close(true);
580
+ try {
581
+ return close(true);
582
+ } catch {
583
+ return close(...args);
584
+ }
585
+ }
586
+
559
587
  function ownPreparedStatement(db: Database, statement: Statement): Statement {
560
588
  const finalizable = statement as FinalizableStatement;
561
589
  // Node finalizes its statements on close and exposes no finalize() method.
@@ -579,7 +607,7 @@ function ownPreparedStatement(db: Database, statement: Statement): Statement {
579
607
  }
580
608
  }
581
609
  references.clear();
582
- const result = close(...args);
610
+ const result = closeReleasingStatements(close, args);
583
611
  if (failure) throw failure;
584
612
  return result;
585
613
  },
@@ -617,7 +645,12 @@ export function prepareCachedStatement(db: Database, sql: string): Statement {
617
645
 
618
646
  const privilegeDepth = new WeakMap<Database, number>();
619
647
  const transformPassScope = new AsyncLocalStorage<
620
- { active: boolean; remainingWaitMs: number } | undefined
648
+ | {
649
+ active: boolean;
650
+ remainingWaitMs: number;
651
+ guard?: { assert(): void; remainingMs(): number };
652
+ }
653
+ | undefined
621
654
  >();
622
655
  const admissionScope = new AsyncLocalStorage<boolean>();
623
656
  const backgroundWriterScope = new AsyncLocalStorage<boolean>();
@@ -655,6 +688,17 @@ export function withoutSqliteTransformPass<T>(operation: () => T): T {
655
688
  return transformPassScope.run(undefined, operation);
656
689
  }
657
690
 
691
+ /** Attach the adapter's existing generation/cancellation/deadline guard to writes. */
692
+ export function guardSqliteTransformPass(guard: { assert(): void; remainingMs(): number }): void {
693
+ const lease = transformPassScope.getStore();
694
+ if (lease) lease.guard = guard;
695
+ }
696
+
697
+ /** Check the attached generation/cancellation/deadline guard before SQL or cache publication. */
698
+ export function assertTransformWrite(): void {
699
+ if (!backgroundWriterScope.getStore()) transformPassScope.getStore()?.guard?.assert();
700
+ }
701
+
658
702
  /** Bun names SQLite codes; node:sqlite exposes the numeric (possibly extended) errcode. */
659
703
  export function isTransientSqliteError(error: unknown): boolean {
660
704
  if (!error || typeof error !== "object") return false;
@@ -698,6 +742,7 @@ interface WriterAcquisition {
698
742
  startedAt: number;
699
743
  attempts: number;
700
744
  claimed: boolean;
745
+ remainingMs?: () => number;
701
746
  }
702
747
 
703
748
  /** A write transaction whose BEGIN went through `acquireShort` and has not ended yet. */
@@ -724,6 +769,7 @@ function foregroundWaitLease() {
724
769
  * native API cannot separate lock waiting from statement execution. Exhaustion
725
770
  * switches to zero-wait acquisition, not a refusal when the writer is free. */
726
771
  function withAutocommitTimeout<T>(db: Database, operation: () => T): T {
772
+ assertTransformWrite();
727
773
  const lease = foregroundWaitLease();
728
774
  if (isInTransaction(db) || (!lease && !backgroundWriterScope.getStore())) return operation();
729
775
  const previous = pragmaNumber(db, "busy_timeout");
@@ -749,6 +795,7 @@ function withAutocommitTimeout<T>(db: Database, operation: () => T): T {
749
795
 
750
796
  /** The connection is never handed back to another caller with a shortened timeout. */
751
797
  function acquireShort(db: Database, acquire: () => unknown, site: string): void {
798
+ assertTransformWrite();
752
799
  const started = performance.now();
753
800
  const previous = pragmaNumber(db, "busy_timeout");
754
801
  const outer = writerAcquisitionScope.getStore();
@@ -763,7 +810,12 @@ function acquireShort(db: Database, acquire: () => unknown, site: string): void
763
810
  // stays short. An ordinary BEGIN inside a transform pass cannot yield;
764
811
  // give that single attempt enough time for a brief writer to finish.
765
812
  const lease = foregroundWaitLease();
766
- const timeout = lease ? Math.floor(lease.remainingWaitMs) : SHORT_BUSY_TIMEOUT_MS;
813
+ const timeout = Math.floor(
814
+ Math.min(
815
+ lease ? lease.remainingWaitMs : SHORT_BUSY_TIMEOUT_MS,
816
+ owner?.remainingMs?.() ?? lease?.guard?.remainingMs() ?? Infinity,
817
+ ),
818
+ );
767
819
  db.exec(`PRAGMA busy_timeout=${timeout}`);
768
820
  acquire();
769
821
  acquiredAt = performance.now();
@@ -871,11 +923,28 @@ export async function beginSqliteWriterAsync(db: Database, site: string): Promis
871
923
  }
872
924
 
873
925
  /** Retry admission before any turn work, yielding between short lock attempts. */
874
- export async function withAsyncPrivilegedWriter<T>(db: Database, operation: () => T): Promise<T> {
926
+ export async function withAsyncPrivilegedWriter<T>(
927
+ db: Database,
928
+ operation: () => T,
929
+ options: {
930
+ signal?: AbortSignal;
931
+ beforeRetry?: (error: SqliteAcquisitionBusyError) => void;
932
+ jitter?: boolean;
933
+ budgetMs?: number;
934
+ assertCurrentPass?: () => void;
935
+ } = {},
936
+ ): Promise<T> {
875
937
  const started = performance.now();
938
+ const budgetMs = Math.max(
939
+ 0,
940
+ Math.min(FOREGROUND_ACQUISITION_BUDGET_MS, options.budgetMs ?? Infinity),
941
+ );
876
942
  let attempts = 0;
877
943
  for (;;) {
944
+ options.signal?.throwIfAborted();
945
+ options.assertCurrentPass?.();
878
946
  attempts++;
947
+ let entered = false;
879
948
  try {
880
949
  const acquisition: WriterAcquisition = {
881
950
  site: "privileged_writer",
@@ -883,35 +952,51 @@ export async function withAsyncPrivilegedWriter<T>(db: Database, operation: () =
883
952
  startedAt: started,
884
953
  attempts,
885
954
  claimed: false,
955
+ remainingMs: () => Math.max(0, budgetMs - (performance.now() - started)),
886
956
  };
887
957
  // The acquisition and hold of this transaction are reported when it
888
958
  // commits or rolls back (see endWriterTransaction).
889
959
  return admissionScope.run(true, () =>
890
960
  withSqliteTransformPass(() =>
891
961
  writerAcquisitionScope.run(acquisition, () =>
892
- withPrivilegedWriter(db, operation),
962
+ withPrivilegedWriter(db, () => {
963
+ entered = true;
964
+ return operation();
965
+ }),
893
966
  ),
894
967
  ),
895
968
  );
896
969
  } catch (error) {
897
970
  // A callback failure can occur after writes; only retry a failed BEGIN.
898
- if (!(error instanceof SqliteAcquisitionBusyError)) throw error;
971
+ if (entered || !(error instanceof SqliteAcquisitionBusyError)) throw error;
972
+ // Adapters may serve a validated saved request instead of waiting.
973
+ // This runs only after a failed BEGIN, never after callback writes.
974
+ options.beforeRetry?.(error);
899
975
  const elapsed = performance.now() - started;
900
- if (elapsed >= FOREGROUND_ACQUISITION_BUDGET_MS) {
976
+ if (elapsed >= budgetMs) {
901
977
  reportSqliteDiagnostic(
902
978
  `sqlite writer site=privileged_writer lane=foreground acquire_ms=${Math.round(elapsed)} attempts=${attempts} outcome=busy`,
903
979
  );
904
980
  throw error;
905
981
  }
906
- await new Promise<void>((resolve) =>
907
- setTimeout(
908
- resolve,
909
- Math.min(
910
- attempts === 1 ? 500 : 1000,
911
- FOREGROUND_ACQUISITION_BUDGET_MS - elapsed,
912
- ),
913
- ),
982
+ const backoff = attempts === 1 ? 500 : 1000;
983
+ const delay = Math.min(
984
+ options.jitter ? backoff * (0.8 + Math.random() * 0.4) : backoff,
985
+ budgetMs - elapsed,
914
986
  );
987
+ await new Promise<void>((resolve, reject) => {
988
+ const abort = () => {
989
+ clearTimeout(timer);
990
+ options.signal?.removeEventListener("abort", abort);
991
+ reject(options.signal?.reason ?? new Error("Writer wait aborted"));
992
+ };
993
+ const timer = setTimeout(() => {
994
+ options.signal?.removeEventListener("abort", abort);
995
+ resolve();
996
+ }, delay);
997
+ options.signal?.addEventListener("abort", abort, { once: true });
998
+ if (options.signal?.aborted) abort();
999
+ });
915
1000
  }
916
1001
  }
917
1002
  }
@@ -70,7 +70,7 @@ const fencedSources: Array<{ path: string; sites: string[]; reporterCalls?: stri
70
70
  },
71
71
  {
72
72
  path: "packages/pi-plugin/src/context-handler.ts",
73
- sites: ["pi_compaction_queue"],
73
+ sites: ["pi_fallback_adoption"],
74
74
  },
75
75
  ];
76
76
 
@@ -116,7 +116,7 @@ const newlyCoveredSites = [
116
116
  "pi_materialize_cache",
117
117
  "pi_soft_refresh_cache",
118
118
  "pi_historian_publish",
119
- "pi_compaction_queue",
119
+ "pi_fallback_adoption",
120
120
  "user_memory_candidate_insert",
121
121
  ] as const;
122
122