@hardkas/query 0.2.2-alpha → 0.3.0-alpha

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 +172 -35
  2. package/dist/index.js +576 -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,112 @@ 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(options?: {
327
+ strict?: boolean;
328
+ }): Promise<any>;
329
+ /** Incremental index update. */
330
+ sync(options?: {
331
+ strict?: boolean;
332
+ }): Promise<any>;
333
+ /** Apply pending schema migrations. */
334
+ migrate(): Promise<{
335
+ applied: number;
336
+ }>;
337
+ /** Execute raw SQL (if supported). */
338
+ executeRawSql(sql: string): Promise<any[]>;
339
+ /** Find all transaction receipts (for replay analysis). */
340
+ findReceipts(filters?: {
341
+ status?: string;
342
+ networkId?: string;
343
+ }): Promise<ArtifactDocument[]>;
344
+ /** Find all transaction traces. */
345
+ findTraces(filters?: {
346
+ txId?: string;
347
+ }): Promise<ArtifactDocument[]>;
348
+ }
349
+
242
350
  interface QueryEngineOptions {
243
351
  /** Root directory for artifact/lineage scanning (typically .hardkas/ or project root). */
244
352
  readonly artifactDir: string;
353
+ /** Primary data backend. If not provided, defaults to auto-discovery (SQLite > Filesystem). */
354
+ readonly backend?: QueryBackend;
355
+ /** Automatically synchronize the store before queries. Requires 'query-store' lock. */
356
+ readonly autoSync?: boolean;
357
+ /** Whether to wait for the lock if held. */
358
+ readonly waitLock?: boolean;
245
359
  }
246
360
  declare class QueryEngine {
247
361
  private readonly adapters;
362
+ readonly backend: QueryBackend;
363
+ /**
364
+ * Primary entry point for creating a QueryEngine with auto-discovery.
365
+ */
366
+ static create(options: QueryEngineOptions): Promise<QueryEngine>;
248
367
  constructor(options: QueryEngineOptions);
249
368
  /**
250
369
  * Execute a query request against the appropriate adapter.
@@ -300,28 +419,48 @@ declare function computeQueryHash(items: readonly unknown[]): string;
300
419
  declare function serializeQueryResult(result: QueryResult): string;
301
420
 
302
421
  /**
303
- * Explainability engine.
422
+ * Explainability & Causal Analysis engine.
304
423
  *
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.
424
+ * Generates structured, technical ExplainBlocks and causal WhyBlocks.
425
+ * Every analysis is based on explicit rules, deterministic evidence,
426
+ * and actual execution state.
308
427
  */
309
428
 
429
+ declare function createExplainBlock(options: {
430
+ backend: string;
431
+ executionPlan: string[];
432
+ indexesUsed?: string[];
433
+ filtersApplied?: string[];
434
+ rowsRead: number;
435
+ scannedFiles: number;
436
+ freshness: QueryStoreStatus;
437
+ warnings?: string[];
438
+ }): ExplainBlock;
439
+ /**
440
+ * Why is this artifact valid or invalid?
441
+ */
310
442
  declare function explainIntegrity(artifact: ArtifactQueryItem, integrity: {
311
443
  ok: boolean;
312
444
  hashMatch: boolean;
313
445
  schemaValid: boolean;
314
446
  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;
447
+ }): WhyBlock;
448
+ /**
449
+ * Why is this transition valid or invalid?
450
+ */
451
+ declare function explainTransition(transition: LineageTransition): WhyBlock;
452
+ /**
453
+ * Why is this artifact an orphan?
454
+ */
455
+ declare function explainOrphan(node: LineageNode, missingParentId: string): WhyBlock;
456
+ declare function formatExplainBlock(block: ExplainBlock): string;
457
+ declare function formatWhyBlock(block: WhyBlock): string;
320
458
 
321
459
  declare class ArtifactQueryAdapter implements QueryAdapter {
322
460
  readonly domain: "artifacts";
323
461
  private readonly rootDir;
324
- constructor(rootDir: string);
462
+ private readonly backend;
463
+ constructor(rootDir: string, backend: QueryBackend);
325
464
  supportedOps(): readonly ["list", "inspect", "diff", "verify"];
326
465
  supportedFilters(): readonly ["schema", "version", "networkId", "mode", "from.address", "to.address", "amountSompi", "status", "contentHash", "createdAt"];
327
466
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -339,7 +478,8 @@ declare class ArtifactQueryAdapter implements QueryAdapter {
339
478
  declare class LineageQueryAdapter implements QueryAdapter {
340
479
  readonly domain: "lineage";
341
480
  private readonly rootDir;
342
- constructor(rootDir: string);
481
+ private readonly backend;
482
+ constructor(rootDir: string, backend: QueryBackend);
343
483
  supportedOps(): readonly ["chain", "transitions", "orphans"];
344
484
  supportedFilters(): readonly ["schema", "networkId", "mode", "rootArtifactId", "lineageId"];
345
485
  execute(request: QueryRequest): Promise<QueryResult<any>>;
@@ -357,7 +497,8 @@ declare class LineageQueryAdapter implements QueryAdapter {
357
497
  declare class ReplayQueryAdapter implements QueryAdapter {
358
498
  readonly domain: "replay";
359
499
  private readonly rootDir;
360
- constructor(rootDir: string);
500
+ private readonly backend;
501
+ constructor(rootDir: string, backend: QueryBackend);
361
502
  supportedOps(): readonly ["list", "summary", "divergences", "invariants"];
362
503
  supportedFilters(): readonly ["txId", "status", "networkId", "mode", "daaScore", "from.address", "to.address"];
363
504
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -365,18 +506,14 @@ declare class ReplayQueryAdapter implements QueryAdapter {
365
506
  private executeSummary;
366
507
  private executeDivergences;
367
508
  private executeInvariants;
368
- private loadAllReceipts;
369
- private loadAllTraces;
370
- private loadReceipt;
371
- private loadTrace;
372
- private loadJsonDir;
373
509
  private readJsonSafe;
374
510
  }
375
511
 
376
512
  declare class DagQueryAdapter implements QueryAdapter {
377
513
  readonly domain: "dag";
378
514
  private readonly rootDir;
379
- constructor(rootDir: string);
515
+ private readonly backend;
516
+ constructor(rootDir: string, backend: QueryBackend);
380
517
  supportedOps(): readonly ["conflicts", "displaced", "history", "sink-path", "anomalies"];
381
518
  supportedFilters(): readonly ["txId", "blockId", "daaScore"];
382
519
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -391,7 +528,8 @@ declare class DagQueryAdapter implements QueryAdapter {
391
528
  declare class EventsQueryAdapter implements QueryAdapter {
392
529
  readonly domain: "events";
393
530
  private readonly rootDir;
394
- constructor(rootDir: string);
531
+ private readonly backend;
532
+ constructor(rootDir: string, backend: QueryBackend);
395
533
  supportedOps(): readonly ["list", "summary"];
396
534
  supportedFilters(): readonly ["txId", "workflowId", "domain", "kind", "networkId", "artifactId"];
397
535
  execute(request: QueryRequest): Promise<QueryResult>;
@@ -402,15 +540,14 @@ declare class EventsQueryAdapter implements QueryAdapter {
402
540
  declare class TxQueryAdapter implements QueryAdapter {
403
541
  readonly domain: "tx";
404
542
  private readonly rootDir;
405
- constructor(rootDir: string);
543
+ private readonly backend;
544
+ constructor(rootDir: string, backend: QueryBackend);
406
545
  supportedOps(): readonly ["aggregate"];
407
546
  supportedFilters(): readonly ["txId"];
408
547
  execute(request: QueryRequest): Promise<QueryResult>;
409
548
  private executeAggregate;
410
549
  private findArtifactsByTxId;
411
550
  private findEventsByTxId;
412
- private scanJsonFiles;
413
- private walkDir;
414
551
  }
415
552
 
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 };
553
+ 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 };