@hardkas/query 0.2.1-alpha → 0.2.2-alpha.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 (3) hide show
  1. package/dist/index.d.ts +156 -35
  2. package/dist/index.js +545 -407
  3. package/package.json +4 -3
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { NetworkId, ContentHash, KaspaAddress, ArtifactId, LineageId, TxId } from '@hardkas/core';
1
+ import { NetworkId, ContentHash, KaspaAddress, ArtifactId, LineageId, TxId, ExecutionMode } from '@hardkas/core';
2
2
 
3
3
  /**
4
4
  * @hardkas/query — Core types for the HardKAS query and introspection engine.
@@ -49,24 +49,42 @@ interface QueryResult<T = unknown> {
49
49
  readonly truncated: boolean;
50
50
  readonly deterministic: boolean;
51
51
  readonly queryHash: string;
52
- readonly explain?: readonly ExplainChain[] | undefined;
53
52
  readonly annotations: QueryAnnotations;
53
+ readonly explain?: ExplainBlock | undefined;
54
+ readonly why?: readonly WhyBlock[] | undefined;
54
55
  }
56
+ type QueryStoreStatus = "fresh" | "stale" | "rebuilding" | "unknown";
55
57
  /** Non-deterministic metadata, always isolated from deterministic fields. */
56
58
  interface QueryAnnotations {
57
59
  readonly executedAt: string;
58
60
  readonly executionMs: number;
59
61
  readonly filesScanned?: number | undefined;
60
- }
61
- interface ExplainChain {
62
+ readonly backendUsed?: string | undefined;
63
+ readonly freshness?: QueryStoreStatus | undefined;
64
+ }
65
+ interface ExplainBlock {
66
+ readonly backend: string;
67
+ readonly executionPlan: readonly string[];
68
+ readonly indexesUsed: readonly string[];
69
+ readonly filtersApplied: readonly string[];
70
+ readonly rowsRead: number;
71
+ readonly scannedFiles: number;
72
+ readonly freshness: QueryStoreStatus;
73
+ readonly warnings: readonly string[];
74
+ }
75
+ interface WhyBlock {
62
76
  readonly question: string;
63
- readonly conclusion: string;
64
- readonly steps: readonly ReasoningStep[];
65
- readonly model: string;
66
- readonly confidence: "definitive" | "probable";
67
- readonly references: readonly string[];
77
+ readonly answer: string;
78
+ readonly evidence: readonly EvidenceRef[];
79
+ readonly causalChain: readonly CausalStep[];
80
+ readonly model?: string;
81
+ readonly confidence?: "definitive" | "probable";
82
+ }
83
+ interface EvidenceRef {
84
+ readonly type: "artifactId" | "contentHash" | "txId" | "traceId" | "eventId" | "blockId" | "filePath";
85
+ readonly value: string;
68
86
  }
69
- interface ReasoningStep {
87
+ interface CausalStep {
70
88
  readonly order: number;
71
89
  readonly assertion: string;
72
90
  readonly evidence: string;
@@ -84,7 +102,8 @@ interface ArtifactQueryItem {
84
102
  readonly version: string;
85
103
  readonly networkId: NetworkId;
86
104
  readonly mode: string;
87
- readonly createdAt: string;
105
+ readonly createdAt: string | null;
106
+ readonly payload: any;
88
107
  readonly contentHash?: ContentHash | undefined;
89
108
  readonly from?: {
90
109
  readonly address: KaspaAddress;
@@ -150,7 +169,7 @@ interface LineageNode {
150
169
  readonly filePath: string;
151
170
  readonly networkId: NetworkId;
152
171
  readonly mode: string;
153
- readonly createdAt: string;
172
+ readonly createdAt: string | null;
154
173
  }
155
174
  interface LineageTransition {
156
175
  readonly from: LineageNode;
@@ -239,12 +258,96 @@ interface DagAnomaly {
239
258
  readonly blockId?: string | undefined;
240
259
  }
241
260
 
261
+ interface ArtifactDocument {
262
+ readonly contentHash: string;
263
+ readonly schema: string;
264
+ readonly version: string;
265
+ readonly kind: string;
266
+ readonly mode: ExecutionMode;
267
+ readonly networkId: NetworkId;
268
+ readonly createdAt: string | null;
269
+ readonly txId: string | null;
270
+ readonly artifactId: string;
271
+ readonly path: string;
272
+ readonly payload: any;
273
+ }
274
+ interface EventDocument {
275
+ readonly eventId: string;
276
+ readonly kind: string;
277
+ readonly domain: string;
278
+ readonly timestamp: string | null;
279
+ readonly workflowId: string;
280
+ readonly correlationId: string;
281
+ readonly causationId: string | null;
282
+ readonly txId: string | null;
283
+ readonly artifactId: string | null;
284
+ readonly networkId: string;
285
+ readonly payload: any;
286
+ }
287
+ interface LineageEdgeDocument {
288
+ readonly parentArtifactId: string;
289
+ readonly childArtifactId: string;
290
+ readonly lineageId: string;
291
+ readonly rule: string;
292
+ readonly createdAt: string | null;
293
+ }
294
+ /**
295
+ * Common storage backend interface for QueryEngine.
296
+ * Allows switching between filesystem/memory and SQLite.
297
+ */
298
+ interface QueryBackend {
299
+ /** Check if the backend is ready/connected. */
300
+ isReady(): boolean;
301
+ /** Backend identifier for diagnostics. */
302
+ kind(): "sqlite" | "filesystem";
303
+ /** Find artifacts matching optional basic filters. */
304
+ findArtifacts(filters?: {
305
+ schema?: string;
306
+ mode?: string;
307
+ networkId?: string;
308
+ }): Promise<ArtifactDocument[]>;
309
+ /** Get a specific artifact by content hash or artifact ID. */
310
+ getArtifact(idOrHash: string): Promise<ArtifactDocument | null>;
311
+ /** Get events matching optional filters. */
312
+ getEvents(filters?: {
313
+ kind?: string;
314
+ txId?: string;
315
+ }): Promise<EventDocument[]>;
316
+ /** Get lineage edges. */
317
+ getLineageEdges(filters?: {
318
+ parentHash?: string;
319
+ childHash?: string;
320
+ }): Promise<LineageEdgeDocument[]>;
321
+ /** Get the current freshness/health status of the store. */
322
+ getStoreStatus(): Promise<string>;
323
+ /** Perform index integrity check. */
324
+ doctor(): Promise<any>;
325
+ /** Atomic wipe and rebuild. */
326
+ rebuild(): Promise<void>;
327
+ /** Find all transaction receipts (for replay analysis). */
328
+ findReceipts(filters?: {
329
+ status?: string;
330
+ networkId?: string;
331
+ }): Promise<ArtifactDocument[]>;
332
+ /** Find all transaction traces. */
333
+ findTraces(filters?: {
334
+ txId?: string;
335
+ }): Promise<ArtifactDocument[]>;
336
+ }
337
+
242
338
  interface QueryEngineOptions {
243
339
  /** Root directory for artifact/lineage scanning (typically .hardkas/ or project root). */
244
340
  readonly artifactDir: string;
341
+ /** Primary data backend. If not provided, defaults to auto-discovery (SQLite > Filesystem). */
342
+ readonly backend?: QueryBackend;
245
343
  }
246
344
  declare class QueryEngine {
247
345
  private readonly adapters;
346
+ readonly backend: QueryBackend;
347
+ /**
348
+ * Primary entry point for creating a QueryEngine with auto-discovery.
349
+ */
350
+ static create(options: QueryEngineOptions): Promise<QueryEngine>;
248
351
  constructor(options: QueryEngineOptions);
249
352
  /**
250
353
  * Execute a query request against the appropriate adapter.
@@ -300,28 +403,48 @@ declare function computeQueryHash(items: readonly unknown[]): string;
300
403
  declare function serializeQueryResult(result: QueryResult): string;
301
404
 
302
405
  /**
303
- * Explainability engine.
406
+ * Explainability & Causal Analysis engine.
304
407
  *
305
- * Generates structured, rule-driven ExplainChains for query results.
306
- * Every explanation is based on explicit rules, deterministic evidence,
307
- * and actual execution state. No speculative reasoning.
408
+ * Generates structured, technical ExplainBlocks and causal WhyBlocks.
409
+ * Every analysis is based on explicit rules, deterministic evidence,
410
+ * and actual execution state.
308
411
  */
309
412
 
413
+ declare function createExplainBlock(options: {
414
+ backend: string;
415
+ executionPlan: string[];
416
+ indexesUsed?: string[];
417
+ filtersApplied?: string[];
418
+ rowsRead: number;
419
+ scannedFiles: number;
420
+ freshness: QueryStoreStatus;
421
+ warnings?: string[];
422
+ }): ExplainBlock;
423
+ /**
424
+ * Why is this artifact valid or invalid?
425
+ */
310
426
  declare function explainIntegrity(artifact: ArtifactQueryItem, integrity: {
311
427
  ok: boolean;
312
428
  hashMatch: boolean;
313
429
  schemaValid: boolean;
314
430
  errors: readonly string[];
315
- }): ExplainChain;
316
- declare function explainTransition(transition: LineageTransition): ExplainChain;
317
- declare function explainOrphan(node: LineageNode, missingParentId: string): ExplainChain;
318
- declare function formatExplainBrief(chain: ExplainChain): string;
319
- declare function formatExplainFull(chain: ExplainChain): string;
431
+ }): WhyBlock;
432
+ /**
433
+ * Why is this transition valid or invalid?
434
+ */
435
+ declare function explainTransition(transition: LineageTransition): WhyBlock;
436
+ /**
437
+ * Why is this artifact an orphan?
438
+ */
439
+ declare function explainOrphan(node: LineageNode, missingParentId: string): WhyBlock;
440
+ declare function formatExplainBlock(block: ExplainBlock): string;
441
+ declare function formatWhyBlock(block: WhyBlock): string;
320
442
 
321
443
  declare class ArtifactQueryAdapter implements QueryAdapter {
322
444
  readonly domain: "artifacts";
323
445
  private readonly rootDir;
324
- constructor(rootDir: string);
446
+ private readonly backend;
447
+ constructor(rootDir: string, backend: QueryBackend);
325
448
  supportedOps(): readonly ["list", "inspect", "diff", "verify"];
326
449
  supportedFilters(): readonly ["schema", "version", "networkId", "mode", "from.address", "to.address", "amountSompi", "status", "contentHash", "createdAt"];
327
450
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -339,7 +462,8 @@ declare class ArtifactQueryAdapter implements QueryAdapter {
339
462
  declare class LineageQueryAdapter implements QueryAdapter {
340
463
  readonly domain: "lineage";
341
464
  private readonly rootDir;
342
- constructor(rootDir: string);
465
+ private readonly backend;
466
+ constructor(rootDir: string, backend: QueryBackend);
343
467
  supportedOps(): readonly ["chain", "transitions", "orphans"];
344
468
  supportedFilters(): readonly ["schema", "networkId", "mode", "rootArtifactId", "lineageId"];
345
469
  execute(request: QueryRequest): Promise<QueryResult<any>>;
@@ -357,7 +481,8 @@ declare class LineageQueryAdapter implements QueryAdapter {
357
481
  declare class ReplayQueryAdapter implements QueryAdapter {
358
482
  readonly domain: "replay";
359
483
  private readonly rootDir;
360
- constructor(rootDir: string);
484
+ private readonly backend;
485
+ constructor(rootDir: string, backend: QueryBackend);
361
486
  supportedOps(): readonly ["list", "summary", "divergences", "invariants"];
362
487
  supportedFilters(): readonly ["txId", "status", "networkId", "mode", "daaScore", "from.address", "to.address"];
363
488
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -365,18 +490,14 @@ declare class ReplayQueryAdapter implements QueryAdapter {
365
490
  private executeSummary;
366
491
  private executeDivergences;
367
492
  private executeInvariants;
368
- private loadAllReceipts;
369
- private loadAllTraces;
370
- private loadReceipt;
371
- private loadTrace;
372
- private loadJsonDir;
373
493
  private readJsonSafe;
374
494
  }
375
495
 
376
496
  declare class DagQueryAdapter implements QueryAdapter {
377
497
  readonly domain: "dag";
378
498
  private readonly rootDir;
379
- constructor(rootDir: string);
499
+ private readonly backend;
500
+ constructor(rootDir: string, backend: QueryBackend);
380
501
  supportedOps(): readonly ["conflicts", "displaced", "history", "sink-path", "anomalies"];
381
502
  supportedFilters(): readonly ["txId", "blockId", "daaScore"];
382
503
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -391,7 +512,8 @@ declare class DagQueryAdapter implements QueryAdapter {
391
512
  declare class EventsQueryAdapter implements QueryAdapter {
392
513
  readonly domain: "events";
393
514
  private readonly rootDir;
394
- constructor(rootDir: string);
515
+ private readonly backend;
516
+ constructor(rootDir: string, backend: QueryBackend);
395
517
  supportedOps(): readonly ["list", "summary"];
396
518
  supportedFilters(): readonly ["txId", "workflowId", "domain", "kind", "networkId", "artifactId"];
397
519
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -402,15 +524,14 @@ declare class EventsQueryAdapter implements QueryAdapter {
402
524
  declare class TxQueryAdapter implements QueryAdapter {
403
525
  readonly domain: "tx";
404
526
  private readonly rootDir;
405
- constructor(rootDir: string);
527
+ private readonly backend;
528
+ constructor(rootDir: string, backend: QueryBackend);
406
529
  supportedOps(): readonly ["aggregate"];
407
530
  supportedFilters(): readonly ["txId"];
408
531
  execute(request: QueryRequest): Promise<QueryResult>;
409
532
  private executeAggregate;
410
533
  private findArtifactsByTxId;
411
534
  private findEventsByTxId;
412
- private scanJsonFiles;
413
- private walkDir;
414
535
  }
415
536
 
416
- export { type ArtifactDiffEntry, type ArtifactDiffResult, type ArtifactInspectResult, type ArtifactOp, ArtifactQueryAdapter, type ArtifactQueryItem, type DagAnomaly, type DagConflict, type DagDisplacement, type DagOp, DagQueryAdapter, type DagSinkPath, type DagSinkPathNode, type DagTxHistory, type DivergenceKind, type EventsOp, EventsQueryAdapter, type ExplainChain, type FilterOp, type LineageChainResult, type LineageNode, type LineageOp, type LineageOrphan, LineageQueryAdapter, type LineageTransition, type QueryAdapter, type QueryAnnotations, type QueryDomain, QueryEngine, type QueryEngineOptions, type QueryFilter, type QueryRequest, type QueryResult, type QuerySort, type ReasoningStep, type ReplayDivergence, type ReplayInvariantsResult, type ReplayOp, ReplayQueryAdapter, type ReplaySummaryResult, type TxOp, TxQueryAdapter, computeQueryHash, createQueryRequest, evaluateFilter, evaluateFilters, explainIntegrity, explainOrphan, explainTransition, formatExplainBrief, formatExplainFull, resolveFieldPath, serializeQueryResult };
537
+ export { type ArtifactDiffEntry, type ArtifactDiffResult, type ArtifactInspectResult, type ArtifactOp, ArtifactQueryAdapter, type ArtifactQueryItem, type CausalStep, type DagAnomaly, type DagConflict, type DagDisplacement, type DagOp, DagQueryAdapter, type DagSinkPath, type DagSinkPathNode, type DagTxHistory, type DivergenceKind, type EventsOp, EventsQueryAdapter, type EvidenceRef, type ExplainBlock, type FilterOp, type LineageChainResult, type LineageNode, type LineageOp, type LineageOrphan, LineageQueryAdapter, type LineageTransition, type QueryAdapter, type QueryAnnotations, type QueryDomain, QueryEngine, type QueryEngineOptions, type QueryFilter, type QueryRequest, type QueryResult, type QuerySort, type QueryStoreStatus, type ReplayDivergence, type ReplayInvariantsResult, type ReplayOp, ReplayQueryAdapter, type ReplaySummaryResult, type TxOp, TxQueryAdapter, type WhyBlock, computeQueryHash, createExplainBlock, createQueryRequest, evaluateFilter, evaluateFilters, explainIntegrity, explainOrphan, explainTransition, formatExplainBlock, formatWhyBlock, resolveFieldPath, serializeQueryResult };