layout.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Native-kernel buffer layout — TS mirror of codegraph-kernel/src/buffers.rs.
  3. *
  4. * The kernel returns five Buffers per file: meta, nodes, edges, refs, arena.
  5. * Rows are fixed-width little-endian; strings are (offset, len) pairs into
  6. * the UTF-8 arena; `offset === NONE` means "field absent".
  7. *
  8. * THIS FILE AND buffers.rs MUST MATCH BYTE FOR BYTE. Any layout change bumps
  9. * KERNEL_ABI_VERSION on both sides — the loader refuses a version it doesn't
  10. * know and the extraction path falls back to wasm.
  11. *
  12. * NodeKind / EdgeKind / provenance / visibility cross the boundary as indexes
  13. * into NODE_KINDS / EDGE_KINDS (src/types.ts) and the small tables below, so
  14. * those array orders are part of the contract (append, never reorder). The
  15. * loader additionally verifies the kernel's own kind tables against
  16. * NODE_KINDS/EDGE_KINDS at load time, so a stale .node degrades to the wasm
  17. * path instead of mis-decoding.
  18. */
  19. export const KERNEL_ABI_VERSION = 1;
  20. /** Sentinel for "absent" in u32 slots and string-ref offsets. */
  21. export const NONE = 0xffffffff;
  22. export const META_SIZE = 36;
  23. export const NODE_ROW_SIZE = 96;
  24. export const EDGE_ROW_SIZE = 44;
  25. export const REF_ROW_SIZE = 40;
  26. /** meta byte offsets */
  27. export const META = {
  28. version: 0, // u8
  29. nodeCount: 4, // u32
  30. edgeCount: 8, // u32
  31. refCount: 12, // u32
  32. arenaLen: 16, // u32
  33. errorsOff: 20, // u32 (NONE = no errors)
  34. errorsLen: 24, // u32
  35. durationMs: 28, // f64 (kernel-side wall; introspection only)
  36. } as const;
  37. /** node row byte offsets */
  38. export const NODE = {
  39. kind: 0, // u8 — NODE_KINDS index
  40. visibility: 1, // u8 — VISIBILITIES index (0 = absent)
  41. flags: 2, // u16 — (present, value) bit pairs, see FLAG
  42. startLine: 4, // u32
  43. endLine: 8, // u32
  44. startColumn: 12, // u32
  45. endColumn: 16, // u32
  46. name: 20, // str
  47. qualifiedName: 28, // str
  48. id: 36, // str — kernel-computed node id
  49. docstring: 44, // str
  50. signature: 52, // str
  51. decorators: 60, // str — NUL-joined list
  52. typeParameters: 68, // str — NUL-joined list
  53. returnType: 76, // str
  54. extraJson: 84, // str — JSON of any extra Node props (escape hatch)
  55. metrics: 92, // u32 — reserved (Arc 3.2 per-node code metrics)
  56. } as const;
  57. /** edge row byte offsets */
  58. export const EDGE = {
  59. sourceIdx: 0, // u32 (NONE → sourceIdStr)
  60. targetIdx: 4, // u32 (NONE → targetIdStr)
  61. kind: 8, // u8 — EDGE_KINDS index
  62. provenance: 9, // u8 — PROVENANCES index (0 = absent)
  63. line: 12, // u32 (NONE = absent)
  64. column: 16, // u32 (NONE = absent)
  65. metadataJson: 20, // str
  66. sourceIdStr: 28, // str
  67. targetIdStr: 36, // str
  68. } as const;
  69. /** ref row byte offsets */
  70. export const REF = {
  71. fromIdx: 0, // u32 (NONE → fromIdStr)
  72. kind: 4, // u8 — EDGE_KINDS index, or FUNCTION_REF_CODE
  73. line: 8, // u32
  74. column: 12, // u32
  75. referenceName: 16, // str
  76. candidates: 24, // str — NUL-joined list
  77. fromIdStr: 32, // str
  78. } as const;
  79. /** ReferenceKind wire code for the internal-only `function_ref` (#756). */
  80. export const FUNCTION_REF_CODE = 200;
  81. /** Node bool-flag bit pairs: bit(2n) = present, bit(2n+1) = value. */
  82. export const FLAG = {
  83. isExported: 0,
  84. isAsync: 1,
  85. isStatic: 2,
  86. isAbstract: 3,
  87. } as const;
  88. /** visibility byte values (0 = absent). */
  89. export const VISIBILITIES = [undefined, 'public', 'private', 'protected', 'internal'] as const;
  90. /** provenance byte values (0 = absent). */
  91. export const PROVENANCES = [undefined, 'tree-sitter', 'scip', 'heuristic'] as const;