tradeblocks-mcp 3.4.0 → 3.5.0

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.
@@ -918,6 +918,35 @@ async function exactFileAddress(filePath) {
918
918
  await opened.handle.close();
919
919
  }
920
920
  }
921
+ async function snapshotOpenFile(source, snapshotPath) {
922
+ const snapshot = await fs2.open(snapshotPath, "wx", 256);
923
+ const hash = createHash2("sha256");
924
+ let bytes = 0;
925
+ const buffer = Buffer.allocUnsafe(1024 * 1024);
926
+ try {
927
+ while (true) {
928
+ const read = await source.read(buffer, 0, buffer.byteLength, bytes);
929
+ if (read.bytesRead === 0) break;
930
+ hash.update(buffer.subarray(0, read.bytesRead));
931
+ let written = 0;
932
+ while (written < read.bytesRead) {
933
+ const result = await snapshot.write(
934
+ buffer,
935
+ written,
936
+ read.bytesRead - written,
937
+ bytes + written
938
+ );
939
+ if (result.bytesWritten === 0) throw new Error("Unable to write inspection snapshot");
940
+ written += result.bytesWritten;
941
+ }
942
+ bytes += read.bytesRead;
943
+ }
944
+ await snapshot.sync();
945
+ } finally {
946
+ await snapshot.close();
947
+ }
948
+ return { address: `sha256:${hash.digest("hex")}`, bytes };
949
+ }
921
950
  function sameCommitContent(previous, input) {
922
951
  return previous.schemaRevision === input.schemaRevision && previous.relativePath === input.relativePath && addressCanonicalJson(previous.coverage) === addressCanonicalJson(input.coverage) && addressCanonicalJson(previous.quality) === addressCanonicalJson(input.quality) && previous.file.address === input.file.address && previous.file.bytes === input.file.bytes && previous.file.rows === input.file.rows;
923
952
  }
@@ -1482,28 +1511,67 @@ var FilePartitionCommitStore = class {
1482
1511
  * identity, coverage, row-count, and exact-byte inspection.
1483
1512
  */
1484
1513
  async [INTERNAL_HISTORICAL_PARTITION_ADOPTION](conn, identity) {
1485
- const captured = JSON.parse(canonicalJsonBytes(identity).toString("utf8"));
1486
- validateIdentity(captured);
1514
+ const captured = captureIdentity(identity);
1487
1515
  const definition = canonicalPartitionDataset(captured.dataset);
1488
1516
  const relativePath = canonicalRelativePath(captured);
1489
1517
  const targetPath = path3.resolve(this.targetPath(relativePath));
1490
- const before = await exactFileAddress(targetPath);
1491
- const inspected = await inspectCanonicalParquet(conn, targetPath, captured);
1492
- const after = await exactFileAddress(targetPath);
1493
- if (before.address !== after.address || before.bytes !== after.bytes) {
1494
- throw new PartitionFileIntegrityError(
1495
- targetPath,
1496
- "existing Parquet changed during semantic inspection"
1497
- );
1498
- }
1499
- return this.adoptExistingFileCommit({
1500
- dataset: captured.dataset,
1501
- partition: captured.partition,
1502
- schemaRevision: definition.schemaRevision,
1503
- relativePath,
1504
- coverage: inspected.coverage,
1505
- quality: { inputRows: inspected.rows, writtenRows: inspected.rows, droppedRows: 0 },
1506
- file: { ...after, rows: inspected.rows }
1518
+ return this.withPartitionLock(captured, async () => {
1519
+ const realMarketRoot = await this.validateMarketRoot();
1520
+ const validatedTargetPath = await this.validatedTargetPath(relativePath, realMarketRoot);
1521
+ if (validatedTargetPath !== targetPath) {
1522
+ throw new PartitionFileIntegrityError(
1523
+ validatedTargetPath,
1524
+ "validated target differs from the registered path"
1525
+ );
1526
+ }
1527
+ const opened = await openRegularFileNoFollow(targetPath);
1528
+ const snapshotRoot = path3.join(this.provenanceRootDir, "inspection-snapshots");
1529
+ await this.ensureDurableDirectory(snapshotRoot);
1530
+ const snapshotDir = await fs2.mkdtemp(path3.join(snapshotRoot, "adopt-"));
1531
+ const snapshotPath = path3.join(snapshotDir, "partition.parquet");
1532
+ try {
1533
+ const snapshotFingerprint = await snapshotOpenFile(opened.handle, snapshotPath);
1534
+ const snapshotObserved = await exactFileAddress(snapshotPath);
1535
+ if (snapshotObserved.address !== snapshotFingerprint.address || snapshotObserved.bytes !== snapshotFingerprint.bytes) {
1536
+ throw new PartitionFileIntegrityError(
1537
+ snapshotPath,
1538
+ "inspection snapshot bytes disagree with the pinned canonical inode"
1539
+ );
1540
+ }
1541
+ await partitionCommitTestFaults.get(this)?.("historical-after-snapshot");
1542
+ const inspected = await inspectCanonicalParquet(conn, snapshotPath, captured);
1543
+ const snapshotAfter = await exactFileAddress(snapshotPath);
1544
+ if (snapshotAfter.address !== snapshotFingerprint.address || snapshotAfter.bytes !== snapshotFingerprint.bytes) {
1545
+ throw new PartitionFileIntegrityError(
1546
+ snapshotPath,
1547
+ "inspection snapshot changed during semantic inspection"
1548
+ );
1549
+ }
1550
+ const canonicalAfter = await exactOpenFileAddress(opened.handle);
1551
+ const afterStat = await opened.handle.stat();
1552
+ if (!afterStat.isFile() || afterStat.nlink !== 1 || !sameInode(opened.stat, afterStat) || canonicalAfter.address !== snapshotFingerprint.address || canonicalAfter.bytes !== snapshotFingerprint.bytes) {
1553
+ throw new PartitionFileIntegrityError(
1554
+ targetPath,
1555
+ "canonical inode changed during historical semantic inspection"
1556
+ );
1557
+ }
1558
+ await requireNamedRegularInode(targetPath, opened.stat);
1559
+ return this.adoptExistingFileCommit(
1560
+ {
1561
+ dataset: captured.dataset,
1562
+ partition: captured.partition,
1563
+ schemaRevision: definition.schemaRevision,
1564
+ relativePath,
1565
+ coverage: inspected.coverage,
1566
+ quality: { inputRows: inspected.rows, writtenRows: inspected.rows, droppedRows: 0 },
1567
+ file: { ...snapshotFingerprint, rows: inspected.rows }
1568
+ },
1569
+ true
1570
+ );
1571
+ } finally {
1572
+ await opened.handle.close();
1573
+ await fs2.rm(snapshotDir, { recursive: true });
1574
+ }
1507
1575
  });
1508
1576
  }
1509
1577
  /**
@@ -1512,11 +1580,11 @@ var FilePartitionCommitStore = class {
1512
1580
  * existing regular inode under the partition lock, then appends authority
1513
1581
  * only when the caller-supplied rows/coverage/fingerprint agree exactly.
1514
1582
  */
1515
- async adoptExistingFileCommit(input) {
1583
+ async adoptExistingFileCommit(input, lockHeld = false) {
1516
1584
  const captured = captureInput(input);
1517
1585
  const targetPath = path3.resolve(this.targetPath(captured.relativePath));
1518
1586
  const realMarketRoot = await this.validateMarketRoot();
1519
- return this.withPartitionLock(captured, async () => {
1587
+ const adopt = async () => {
1520
1588
  const validatedTargetPath = await this.validatedTargetPath(
1521
1589
  captured.relativePath,
1522
1590
  realMarketRoot
@@ -1609,7 +1677,8 @@ var FilePartitionCommitStore = class {
1609
1677
  } finally {
1610
1678
  await handle.close();
1611
1679
  }
1612
- });
1680
+ };
1681
+ return lockHeld ? adopt() : this.withPartitionLock(captured, adopt);
1613
1682
  }
1614
1683
  async publishFileCommit(input) {
1615
1684
  const captured = captureInput(input);
@@ -3538,7 +3607,7 @@ async function openReadWriteConnection(dbPath, threads, memoryLimit) {
3538
3607
  await createMarketParquetViews(connection, dataRoot);
3539
3608
  await ensureMarketDataTables(connection);
3540
3609
  try {
3541
- const blocksDir = (await import("./sync-4Y4BSU6S.js")).getBlocksDir(dataRoot);
3610
+ const blocksDir = (await import("./sync-AWI764HE.js")).getBlocksDir(dataRoot);
3542
3611
  await migrateMetadataToJson(connection, dataRoot, blocksDir);
3543
3612
  } catch (err) {
3544
3613
  console.warn(
@@ -5338,4 +5407,4 @@ export {
5338
5407
  loadReportingLog,
5339
5408
  importCsv
5340
5409
  };
5341
- //# sourceMappingURL=chunk-TMCRS75V.js.map
5410
+ //# sourceMappingURL=chunk-5HBZ3TZM.js.map