T-119: M1.3e — @stock-bot/research: DuckDB SQL planner + injected-executor MarketDataStore adapter for curated daily Parquet (bars 1d, events, shortsAsOf, fx 1d) with mandatory Vault / known_at predicates, tests and local benchmark (no new dependencies)

This commit is contained in:
Studio 2026-11-29 07:56:10 -05:00
parent 9a213f126b
commit 0a08a9962e
2 changed files with 113 additions and 3 deletions

View file

@ -1757,10 +1757,10 @@ export function createDuckDbMarketDataStore(options: DuckDbMarketDataStoreOption
'pinAdjFactorsArtifact'
);
}
const release = rawPin.release as () => Promise<void> | void;
const pin = rawPin as unknown as PinnedAdjFactorsArtifact;
try {
const pinnedPath = validateAdjFactorsArtifactPath(
rawPin.pinnedPath as string,
pin.pinnedPath,
'pinnedPath'
);
const canonicalPinned = canonicalizePosixPath(pinnedPath);
@ -1776,7 +1776,7 @@ export function createDuckDbMarketDataStore(options: DuckDbMarketDataStoreOption
}
rawRows = await executeAdjustedBarsQuery(q, pinnedPath);
} finally {
await release();
await pin.release();
}
} else if (immutableAdjFactorsPath !== undefined) {
await ensureBudgetInitialized();

View file

@ -1041,6 +1041,116 @@ describe('adj_factors snapshot verification & Vault future-split leakage prevent
expect(openFds.size).toBe(0);
});
it('preserves the `this` receiver when calling pinned artifact `release()` after successful and failed scans', async () => {
class HandlePinnedArtifact implements PinnedAdjFactorsArtifact {
readonly pinnedPath: string;
released = false;
releaseCount = 0;
readonly fileHandle: { closed: boolean; close: () => Promise<void> };
constructor(pinnedPath: string) {
this.pinnedPath = pinnedPath;
this.fileHandle = {
closed: false,
close: () => {
this.fileHandle.closed = true;
return Promise.resolve();
},
};
}
async release(): Promise<void> {
if (!(this instanceof HandlePinnedArtifact)) {
throw new TypeError('release() was invoked without its PinnedAdjFactorsArtifact receiver');
}
await this.fileHandle.close();
this.released = true;
this.releaseCount += 1;
}
}
const createdPins: HandlePinnedArtifact[] = [];
let nextFd = 100;
const pinAdjFactorsArtifact: AdjFactorsArtifactPinner = () => {
const pin = new HandlePinnedArtifact(`/proc/self/fd/${nextFd}`);
nextFd += 1;
createdPins.push(pin);
return pin;
};
let shouldFailScan = false;
const fake = createRecordingExecutor({
onQuery: (sql) => {
if (sql.includes('parquet_kv_metadata')) {
return makeFencedKvRows({
asOfMicros: null,
vaultCutoffMicros: CUTOFF_MICROS,
rowsWritten: 1,
});
}
if (shouldFailScan) {
throw new Error('simulated DuckDB bars scan failure');
}
return [
{
listing_id: 42,
ts: Date.UTC(2024, 5, 7, 20, 0, 0),
open: 50.0,
high: 51.0,
low: 49.5,
close: 50.0,
volume: 2000,
vwap: 50.25,
known_at: Date.UTC(2024, 5, 7, 20, 0, 0),
},
];
},
});
const store = createDuckDbMarketDataStore({
executor: fake.executor,
curatedRoot: CURATED_ROOT,
vaultCutoffTs: CUTOFF_MS,
pinAdjFactorsArtifact,
env: {},
});
// 1. Successful split-adjusted scan: returns rows and invokes pin.release() with `this` intact.
const batches = await collectBars(
store.bars({
listingIds: [42],
timeframe: '1d',
from: '2024-06-01',
to: '2024-06-15',
adjustment: 'split',
})
);
expect(batches).toHaveLength(1);
expect(batches[0]!.rows[0]!.close).toBe(50.0);
expect(createdPins).toHaveLength(1);
expect(createdPins[0]!.released).toBe(true);
expect(createdPins[0]!.fileHandle.closed).toBe(true);
expect(createdPins[0]!.releaseCount).toBe(1);
// 2. Failed scan (executor error): propagates error and still invokes pin.release() with `this` intact.
shouldFailScan = true;
await expect(
collectBars(
store.bars({
listingIds: [42],
timeframe: '1d',
from: '2024-06-01',
to: '2024-06-15',
adjustment: 'split',
})
)
).rejects.toThrow('simulated DuckDB bars scan failure');
expect(createdPins).toHaveLength(2);
expect(createdPins[1]!.released).toBe(true);
expect(createdPins[1]!.fileHandle.closed).toBe(true);
expect(createdPins[1]!.releaseCount).toBe(1);
});
it('fails closed on unpinned mutable adj_factors paths and buggy no-op pinners', async () => {
// 1. Default mutable path with neither pinAdjFactorsArtifact nor immutableAdjFactorsPath
const unpinnedFake = createRecordingExecutor();