in codex-cli/src/utils/singlepass/context_limit.ts [67:105]
export function buildChildrenMap(
root: string,
totalSizeMap: Record<string, number>,
): Record<string, Array<string>> {
const rootAbs = path.resolve(root);
const childrenMap: Record<string, Array<string>> = {};
// Initialize all potential keys so that each path has an entry.
for (const p of Object.keys(totalSizeMap)) {
if (!childrenMap[p]) {
childrenMap[p] = [];
}
}
for (const p of Object.keys(totalSizeMap)) {
if (p === rootAbs) {
continue;
}
const parent = path.dirname(p);
// If the parent is also tracked in totalSizeMap, we record p as a child.
if (totalSizeMap[parent] !== undefined && parent !== p) {
if (!childrenMap[parent]) {
childrenMap[parent] = [];
}
childrenMap[parent].push(p);
}
}
// Sort the children.
for (const val of Object.values(childrenMap)) {
val.sort((a, b) => {
return a.localeCompare(b);
});
}
return childrenMap;
}