/** * @module vcs-diagnostic-helper * * Diagnostic helpers for logging and tracing b2tfs item/branch/index state * across spaces with custom searchable prefixes. */ import { joinPaths } from '@ibgib/helper-gib/dist/helpers/path-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs'; import { IbGibAddr } from '@ibgib/ts-gib/dist/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs'; import { getLatestAddrs } from '../../witness/space/space-helper.mjs'; import { B2tFSBranchIbGib_V1 } from './branch/branch-types.mjs'; import { B2tFSItemIbGib_V1, B2TFS_CHILD_ITEM_REL8N_NAME } from './item/item-types.mjs'; import { getIbGibFromSpace } from '../../common/other/ibgib-helper.mjs'; const logalot = GLOBAL_LOG_A_LOT; export const TRACE_VCS_PREFIX = '[TRACE_VCS]'; export function shortAddr(addr: IbGibAddr | undefined): string { if (!addr) { return '(none)'; } const caretIdx = addr.lastIndexOf('^'); if (caretIdx === -1) { return addr.slice(0, 40); } const ib = addr.slice(0, caretIdx); const gib = addr.slice(caretIdx + 1); const dotIdx = gib.indexOf('.'); const gibShort = dotIdx === -1 ? gib.slice(0, 8) : `${gib.slice(0, 8)}.${gib.slice(dotIdx + 1, dotIdx + 9)}`; return `${ib}^${gibShort}`; } export function shortAddrs(addrs: IbGibAddr[] | undefined): string { if (!addrs || addrs.length === 0) { return '[]'; } return `[\n ${addrs.map(shortAddr).join('\n ')}\n ]`; } export async function logBranchDiagnostic({ label, branchIbGib, space, }: { label: string; branchIbGib: B2tFSBranchIbGib_V1 | IbGib_V1 | undefined; space?: IbGibSpaceAny; }): Promise { const lc = `${TRACE_VCS_PREFIX}[logBranch][${label}]`; if (!branchIbGib) { console.log(`${lc} branchIbGib: (UNDEFINED)`); return; } const b = branchIbGib as B2tFSBranchIbGib_V1; const addr = getIbGibAddr({ ibGib: b }); const n = (b.data as any)?.n ?? '?'; const name = b.data?.name ?? '?'; const tjpAddr = b.rel8ns?.tjp?.at(0); const itemAddrs = b.rel8ns?.b2tfs_item ?? []; console.log(`${lc} addr : ${shortAddr(addr)} name : ${name} n : ${n} tjp : ${shortAddr(tjpAddr)} b2tfs_item: ${shortAddrs(itemAddrs)} (${itemAddrs.length} total, tip: ${shortAddr(itemAddrs.at(-1))})`); if (space && tjpAddr) { try { const res = await getLatestAddrs({ addrs: [tjpAddr], space }); const latest = (res?.data as any)?.latestAddrs?.[0]; console.log(` [space ${(space as any).ib ?? space.constructor.name}] latestAddr for branch TJP: ${shortAddr(latest)}`); } catch (e) { console.log(` [space] getLatestAddrs for branch TJP THREW: ${e}`); } } } export async function logTreeHierarchyDiagnostic({ label, rootItemAddr, space, metaspace, }: { label: string; rootItemAddr?: string; space: any; metaspace?: any; }): Promise { const lc = `${TRACE_VCS_PREFIX}[logTree][${label}]`; const spaceName = space?.data?.name || space?.data?.spaceSubPath || space?.ib || 'unknown'; console.log(`${lc} === Hierarchy for root: ${shortAddr(rootItemAddr)} in space: ${spaceName} ===`); if (!rootItemAddr) { console.log(`${lc} rootItemAddr is falsy`); return; } async function walk(addr: string, depth: number, currentPath: string): Promise { const indent = ' '.repeat(depth); const item = await getIbGibFromSpace({ addr, space, metaspace }); if (!item) { console.log(`${lc}${indent} ? MISSING item at ${shortAddr(addr)} (path: ${currentPath})`); return; } const name = item.data?.name || '(root)'; const fsType = item.data?.fsType || 'unknown'; const comments = item.rel8ns?.comment || []; const children = item.rel8ns?.[B2TFS_CHILD_ITEM_REL8N_NAME] || []; console.log(`${lc}${indent} ?? ${name} (${fsType}) addr: ${shortAddr(addr)} comments: ${comments.length} children: ${children.length}`); for (const cAddr of comments) { const comment = await getIbGibFromSpace({ addr: cAddr, space, metaspace }); console.log(`${lc}${indent} ?? comment: "${(comment?.data as any)?.text?.slice(0, 50)}"`); } for (const childAddr of children) { await walk(childAddr, depth + 1, currentPath ? joinPaths(currentPath, name) : name); } } await walk(rootItemAddr, 1, ''); }