dsh-hashline-edit 0.5.5 → 0.5.6

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.
package/lib/index.js CHANGED
@@ -2913,7 +2913,7 @@ import { join as join6, dirname as dirname5 } from "node:path";
2913
2913
 
2914
2914
  // src/store-kernel/constants.ts
2915
2915
  var HASH_STORE_BUSY_TIMEOUT = 250;
2916
- var HASH_STORE_VERSION = 11;
2916
+ var HASH_STORE_VERSION = 12;
2917
2917
  var BUSY_MAX_ATTEMPTS = 8;
2918
2918
  var BUSY_BACKOFF_BASE_MS = 50;
2919
2919
  var BUSY_BACKOFF_CAP_MS = 400;
@@ -3225,7 +3225,12 @@ function ensureSnapshotTables(db) {
3225
3225
  if (hasLeases) {
3226
3226
  db.exec(`DELETE FROM served_leases WHERE ${FOREIGN_GENERATION_LEASE_SQL}`);
3227
3227
  }
3228
- db.exec(`DELETE FROM file_snapshots WHERE ${FOREIGN_GENERATION_SNAPSHOT_SQL}`);
3228
+ db.exec(
3229
+ `DELETE FROM file_snapshots WHERE ${FOREIGN_GENERATION_SNAPSHOT_SQL}`
3230
+ );
3231
+ if (hasTable("file_undo") && readTableColumns(db, "file_undo").has("anchor_generation")) {
3232
+ db.exec(`DELETE FROM file_undo WHERE anchor_generation NOT IN (0, ${ANCHOR_GENERATION})`);
3233
+ }
3229
3234
  db.exec(`DELETE FROM line_lineage WHERE ${ORPHAN_LINEAGE_SQL}`);
3230
3235
  if (hasLeases) {
3231
3236
  db.exec(`DELETE FROM served_leases WHERE ${DANGLING_LEASE_SQL}`);
@@ -3270,6 +3275,33 @@ function ensureFileUndoSchema(db) {
3270
3275
  db.exec(CUT_INTENT_DDL);
3271
3276
  addColumnIfMissing(db, "cut_intent", "direction");
3272
3277
  }
3278
+ function reclaimStoreSpace(db) {
3279
+ try {
3280
+ db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
3281
+ } catch {
3282
+ }
3283
+ try {
3284
+ const total = Number(db.prepare("PRAGMA page_count").get().page_count ?? 0);
3285
+ const free = Number(db.prepare("PRAGMA freelist_count").get().freelist_count ?? 0);
3286
+ if (total > 0 && free > 2048 && free / total > 0.2) {
3287
+ try {
3288
+ db.exec("PRAGMA auto_vacuum = INCREMENTAL");
3289
+ } catch {
3290
+ }
3291
+ db.exec("VACUUM");
3292
+ try {
3293
+ db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
3294
+ } catch {
3295
+ }
3296
+ } else {
3297
+ try {
3298
+ db.exec("PRAGMA incremental_vacuum");
3299
+ } catch {
3300
+ }
3301
+ }
3302
+ } catch {
3303
+ }
3304
+ }
3273
3305
  function buildStore(db) {
3274
3306
  const stampDue = !storeStampMatches(db);
3275
3307
  db.exec("PRAGMA journal_mode = WAL");
@@ -3318,6 +3350,7 @@ function buildStore(db) {
3318
3350
  addColumnIfMissing(db, "served", "snapshotId");
3319
3351
  addColumnIfMissing(db, "undo", "result_content");
3320
3352
  if (!stampDue) return false;
3353
+ reclaimStoreSpace(db);
3321
3354
  db.prepare(
3322
3355
  "INSERT INTO meta (key, value) VALUES ('version', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value"
3323
3356
  ).run(String(HASH_STORE_VERSION));
@@ -5395,6 +5428,10 @@ function vacuumSnapshots(db, options = {}) {
5395
5428
  }
5396
5429
  }
5397
5430
  if (evict.length > 0) deleteVacuumSnapshots(db, evict);
5431
+ try {
5432
+ db.exec("PRAGMA incremental_vacuum");
5433
+ } catch {
5434
+ }
5398
5435
  const deferredBytes = Math.max(0, total - VACUUM_GLOBAL_BUDGET_BYTES);
5399
5436
  return {
5400
5437
  evicted: evict.length,
@@ -6,7 +6,7 @@
6
6
  * WHY: beside the DDL, so the retry model and the stamp cannot be read from two modules that drift.
7
7
  */
8
8
  export declare const HASH_STORE_BUSY_TIMEOUT = 250;
9
- export declare const HASH_STORE_VERSION = 11;
9
+ export declare const HASH_STORE_VERSION = 12;
10
10
  declare const BUSY_MAX_ATTEMPTS = 8;
11
11
  declare const BUSY_BACKOFF_BASE_MS = 50;
12
12
  declare const BUSY_BACKOFF_CAP_MS = 400;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-hashline-edit",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "description": "Hash-anchored read/apply_patch/edit tools for DeepSeek Harness (dsh). Every line gets a unique 3- or 4-char hash (A-Za-z0-9, configurable) that stays stable across edits; apply_patch and the anchored range edit tool are the file-mutation entry points, each committing its whole batch as one transaction.",
5
5
  "keywords": [
6
6
  "anchors",
package/src/hash-store.ts CHANGED
@@ -544,7 +544,12 @@ export function ensureSnapshotTables(db: DatabaseSync): void {
544
544
  if (hasLeases) {
545
545
  db.exec(`DELETE FROM served_leases WHERE ${FOREIGN_GENERATION_LEASE_SQL}`);
546
546
  }
547
- db.exec(`DELETE FROM file_snapshots WHERE ${FOREIGN_GENERATION_SNAPSHOT_SQL}`);
547
+ db.exec(
548
+ `DELETE FROM file_snapshots WHERE ${FOREIGN_GENERATION_SNAPSHOT_SQL}`,
549
+ );
550
+ if (hasTable("file_undo") && readTableColumns(db, "file_undo").has("anchor_generation")) {
551
+ db.exec(`DELETE FROM file_undo WHERE anchor_generation NOT IN (0, ${ANCHOR_GENERATION})`);
552
+ }
548
553
  db.exec(`DELETE FROM line_lineage WHERE ${ORPHAN_LINEAGE_SQL}`);
549
554
  // WHY: one provenance rule as of this sweep — a lease that names no surviving
550
555
  // WHY: snapshot for its own path is dangling (same-generation pre-column rows read
@@ -651,6 +656,45 @@ export function ensureFileUndoSchema(db: DatabaseSync): void {
651
656
  addColumnIfMissing(db, "cut_intent", "direction");
652
657
  }
653
658
 
659
+ /**
660
+ * WHY: deleted rows leave free pages SQLite never returns to the OS without a VACUUM, so a large
661
+ * generation/schema migration (or months of TTL pruning) can leave the file tens of times its live
662
+ * size — the 1.16GB-for-100MB case that made every open of a big store crawl. After the slow-path
663
+ * sweep we checkpoint the WAL and, when free pages dominate, switch to incremental auto-vacuum and
664
+ * VACUUM once; later TTL deletes are then reclaimed cheaply by `PRAGMA incremental_vacuum`.
665
+ */
666
+ function reclaimStoreSpace(db: DatabaseSync): void {
667
+ try {
668
+ db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
669
+ } catch {
670
+ // best-effort: a held reader only defers the truncate
671
+ }
672
+ try {
673
+ const total = Number((db.prepare("PRAGMA page_count").get() as { page_count?: number }).page_count ?? 0);
674
+ const free = Number((db.prepare("PRAGMA freelist_count").get() as { freelist_count?: number }).freelist_count ?? 0);
675
+ if (total > 0 && free > 2048 && free / total > 0.2) {
676
+ try {
677
+ db.exec("PRAGMA auto_vacuum = INCREMENTAL");
678
+ } catch {
679
+ // pre-existing auto_vacuum cannot always be changed; the standalone VACUUM below still reclaims
680
+ }
681
+ db.exec("VACUUM");
682
+ try {
683
+ db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
684
+ } catch {
685
+ // best-effort
686
+ }
687
+ } else {
688
+ try {
689
+ db.exec("PRAGMA incremental_vacuum");
690
+ } catch {
691
+ // best-effort
692
+ }
693
+ }
694
+ } catch {
695
+ // best-effort: space reclamation must never fail an otherwise-good open
696
+ }
697
+ }
654
698
  function buildStore(db: DatabaseSync): boolean {
655
699
  // WHY: the open's stamp-due decision is made ONCE here and handed back through `openDb`: it gates the
656
700
  // WHY: sweep below and the `version` upsert at the end, and it is what `openStore` owes a stamp write
@@ -772,6 +816,7 @@ function buildStore(db: DatabaseSync): boolean {
772
816
  // WHY: its value is a build constant, so it belongs to the build. The halves that certify the WHOLE
773
817
  // WHY: open are owed to `openStore` instead: written here they would certify hook work that has not run.
774
818
  if (!stampDue) return false;
819
+ reclaimStoreSpace(db);
775
820
  db.prepare(
776
821
  "INSERT INTO meta (key, value) VALUES ('version', ?) " +
777
822
  "ON CONFLICT(key) DO UPDATE SET value = excluded.value",
@@ -185,6 +185,12 @@ export function vacuumSnapshots(db: DatabaseSync, options: VacuumOptions = {}):
185
185
  }
186
186
 
187
187
  if (evict.length > 0) deleteVacuumSnapshots(db, evict);
188
+ try {
189
+ // WHY: with incremental auto-vacuum (set on first upgrade reclaim) this returns freed TTL pages cheaply.
190
+ db.exec("PRAGMA incremental_vacuum");
191
+ } catch {
192
+ // best-effort: a legacy auto_vacuum=0 store reclaims on the next slow open instead.
193
+ }
188
194
  const deferredBytes = Math.max(0, total - VACUUM_GLOBAL_BUDGET_BYTES);
189
195
  return {
190
196
  evicted: evict.length,
@@ -29,7 +29,7 @@ export const HASH_STORE_BUSY_TIMEOUT = 250;
29
29
  // WHY: needs to stay covered. Without it every materialization re-scanned each path's lease set.
30
30
  // WHY: v11 adds `file_snapshots.stat_key` plus `idx_snapshots_path_stat` — the stat identity a read
31
31
  // WHY: materialization saw, so an unchanged file's next read adopts on `stat(2)` alone (ADR-0043).
32
- export const HASH_STORE_VERSION = 11;
32
+ export const HASH_STORE_VERSION = 12;
33
33
 
34
34
  const BUSY_MAX_ATTEMPTS = 8;
35
35
  const BUSY_BACKOFF_BASE_MS = 50;