All files / kernel-ui/src/services db-parser.ts

96.73% Statements 89/92
80.64% Branches 75/93
100% Functions 8/8
96.7% Lines 88/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                                  2x 2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x     2x 83x 2x 2x   81x 2x 2x   79x 2x 2x   77x 5x 5x 5x       5x     72x 4x 4x   68x 4x 4x   64x 1x 1x   63x 7x 7x   56x 7x 7x   49x 7x           7x   42x 9x           9x         2x 2x 5x     5x                   2x 6x 1x       2x 7x 7x     7x         7x 4x           3x       2x 5x 4x     28x       3x         2x 9x 9x     9x 9x     9x 9x 9x 2x               9x           9x 5x     4x   17x       4x       2x 5x 5x     21x       2x       2x                
import type {
  ObjectRegistry,
  VatSnapshot,
  SlotInfo,
  ExportedOcapURL,
} from '../types.ts';
 
/**
 * Parse a flat kernel DB dump into per-vat grouped info
 *
 * @param entries - The flat kernel DB dump.
 * @returns A record of vat names to their KernelGroupedVat info.
 */
export function parseObjectRegistry(
  entries: { key: string; value: string }[],
): ObjectRegistry {
  // Raw metadata
  const koOwner: Record<string, string> = {};
  const koRefCount: Record<string, string> = {};
  const koRevoked: Record<string, string> = {};
  const kpState: Record<string, string> = {};
  const kpValueRaw: Record<string, { body: string; slots: string[] }> = {};
  const vatConfigs: Record<string, { name: string; bundleSpec: string }> = {};
  // C-lists
  const objCList: { vat: string; kref: string; eref: string }[] = [];
  const prmCList: { vat: string; kref: string; eref: string }[] = [];
 
  let gcActions = '';
  let reapQueue = '';
  let terminatedVats = '';
  const ocapUrls: ExportedOcapURL[] = [];
 
  // 1) Collect
  for (const { key, value } of entries) {
    if (key === 'gcActions') {
      gcActions = value;
      continue;
    }
    if (key === 'reapQueue') {
      reapQueue = value;
      continue;
    }
    if (key === 'vats.terminated') {
      terminatedVats = value;
      continue;
    }
    if (key.startsWith('vatConfig.')) {
      const vat = key.split('.')[1] as string;
      const config = JSON.parse(value);
      vatConfigs[vat] = {
        name: config.parameters?.name ?? vat,
        bundleSpec: config.bundleSpec,
      };
      continue;
    }
    let matches;
    if ((matches = key.match(/^(ko\d+)\.owner$/u))) {
      matches[1] && (koOwner[matches[1]] = value);
      continue;
    }
    if ((matches = key.match(/^(ko\d+)\.refCount$/u))) {
      matches[1] && (koRefCount[matches[1]] = value);
      continue;
    }
    if ((matches = key.match(/^(ko\d+)\.revoked$/u))) {
      matches[1] && (koRevoked[matches[1]] = value);
      continue;
    }
    if ((matches = key.match(/^(kp\d+)\.state$/u))) {
      matches[1] && (kpState[matches[1]] = value);
      continue;
    }
    if ((matches = key.match(/^(kp\d+)\.value$/u))) {
      matches[1] && (kpValueRaw[matches[1]] = JSON.parse(value));
      continue;
    }
    if ((matches = key.match(/^(v\d+)\.c\.(ko\d+)$/u))) {
      matches[1] &&
        objCList.push({
          vat: matches[1] ?? '',
          kref: matches[2] ?? '',
          eref: value.replace(/^R\s*/u, ''),
        });
      continue;
    }
    if ((matches = key.match(/^(v\d+)\.c\.(kp\d+)$/u))) {
      matches[1] &&
        prmCList.push({
          vat: matches[1] ?? '',
          kref: matches[2] ?? '',
          eref: value.replace(/^R\s*/u, ''),
        });
      continue;
    }
  }
 
  // 2) Init vats
  const vats: Record<string, VatSnapshot> = {};
  for (const vat of Object.keys(vatConfigs)) {
    Iif (!vatConfigs[vat]) {
      continue;
    }
    vats[vat] = {
      overview: vatConfigs[vat],
      ownedObjects: [],
      importedObjects: [],
      importedPromises: [],
      exportedPromises: [],
    };
  }
 
  // Helper to resolve slots
  const resolveSlot = (kref: string): SlotInfo => {
    const entry = objCList.find((item) => item.kref === kref);
    return { kref, eref: entry?.eref ?? null, vat: entry?.vat ?? null };
  };
 
  // 3) Populate objects
  for (const { vat, kref, eref } of objCList) {
    const bucket = vats[vat];
    Iif (!bucket) {
      continue;
    }
    const rec = {
      kref,
      eref,
      refCount: koRefCount[kref] ?? '0',
    };
    if (eref.startsWith('o+')) {
      bucket.ownedObjects.push({
        ...rec,
        toVats: [],
        revoked: koRevoked[kref] ?? 'false',
      });
    } else {
      bucket.importedObjects.push({ ...rec, fromVat: koOwner[kref] ?? null });
    }
  }
  // Cross-link objects
  for (const vat of Object.keys(vats)) {
    for (const obj of vats[vat]?.ownedObjects ?? []) {
      obj.toVats = objCList
        .filter(
          (entry) =>
            entry.kref === obj.kref &&
            entry.vat !== vat &&
            entry.eref.startsWith('o-'),
        )
        .map((entry) => entry.vat);
    }
  }
 
  // 4) Populate promises
  for (const { vat, kref, eref } of prmCList) {
    const bucket = vats[vat];
    Iif (!bucket) {
      continue;
    }
    const raw = kpValueRaw[kref] ?? { body: '', slots: [] };
    const slots = raw.slots.map(resolveSlot);
 
    // Extract ocap URL if present in the promise body
    Eif (raw.body && typeof raw.body === 'string') {
      const ocapMatch = raw.body.match(/^#"(ocap:[^"]+)"/u);
      if (ocapMatch) {
        ocapUrls.push({
          vatId: vat,
          promiseId: kref,
          ocapUrl: ocapMatch[1] ?? '',
        });
      }
    }
 
    const base = {
      kref,
      eref,
      state: kpState[kref] ?? 'unresolved',
      value: { body: raw.body, slots },
    };
    if (eref.startsWith('p+')) {
      bucket.exportedPromises.push({ ...base, toVats: [] });
    } else {
      const origin =
        prmCList.find(
          (entry) =>
            entry.kref === kref &&
            entry.vat !== vat &&
            entry.eref.startsWith('p+'),
        )?.vat ?? null;
      bucket.importedPromises.push({ ...base, fromVat: origin });
    }
  }
  // Cross-link promises
  for (const vat of Object.keys(vats)) {
    for (const prm of vats[vat]?.exportedPromises ?? []) {
      prm.toVats = prmCList
        .filter(
          (entry) =>
            entry.kref === prm.kref &&
            entry.vat !== vat &&
            entry.eref.startsWith('p-'),
        )
        .map((entry) => entry.vat);
    }
  }
 
  return {
    gcActions,
    reapQueue,
    terminatedVats,
    vats,
    ocapUrls,
  };
}