Skip to content

Commit c86493e

Browse files
authored
fix: guard every migrated session accessor path (#117140)
1 parent 95e50d3 commit c86493e

1 file changed

Lines changed: 95 additions & 156 deletions

File tree

‎scripts/check-session-accessor-boundary.mjs‎

Lines changed: 95 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "node:path";
55
import ts from "typescript";
66
import {
77
collectFileViolations,
8+
getPropertyNameText,
89
resolveRepoRoot,
910
resolveSourceRoots,
1011
runAsScript,
@@ -315,13 +316,6 @@ function propertyAccessName(expression) {
315316
return null;
316317
}
317318

318-
function propertyNameText(name) {
319-
if (ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)) {
320-
return name.text;
321-
}
322-
return null;
323-
}
324-
325319
function bindingName(node) {
326320
if (node.propertyName && ts.isIdentifier(node.propertyName)) {
327321
return node.propertyName.text;
@@ -335,6 +329,12 @@ function bindingName(node) {
335329
function findNamedBoundaryViolations(content, fileName, legacyNames, subject) {
336330
const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true);
337331
const violations = [];
332+
const addViolation = (node, action, name) => {
333+
violations.push({
334+
line: toLine(sourceFile, node),
335+
reason: `${action} ${subject} "${name}"`,
336+
});
337+
};
338338

339339
const visit = (node) => {
340340
if (ts.isImportDeclaration(node)) {
@@ -343,10 +343,7 @@ function findNamedBoundaryViolations(content, fileName, legacyNames, subject) {
343343
for (const specifier of namedBindings.elements) {
344344
const importedName = specifier.propertyName?.text ?? specifier.name.text;
345345
if (legacyNames.has(importedName)) {
346-
violations.push({
347-
line: toLine(sourceFile, specifier),
348-
reason: `imports ${subject} "${importedName}"`,
349-
});
346+
addViolation(specifier, "imports", importedName);
350347
}
351348
}
352349
}
@@ -355,29 +352,20 @@ function findNamedBoundaryViolations(content, fileName, legacyNames, subject) {
355352
if (ts.isBindingElement(node)) {
356353
const name = bindingName(node);
357354
if (name && legacyNames.has(name)) {
358-
violations.push({
359-
line: toLine(sourceFile, node),
360-
reason: `aliases ${subject} "${name}"`,
361-
});
355+
addViolation(node, "aliases", name);
362356
}
363357
}
364358

365359
if (ts.isPropertyAccessExpression(node) && legacyNames.has(node.name.text)) {
366-
violations.push({
367-
line: toLine(sourceFile, node.name),
368-
reason: `references ${subject} "${node.name.text}"`,
369-
});
360+
addViolation(node.name, "references", node.name.text);
370361
}
371362

372363
if (
373364
ts.isElementAccessExpression(node) &&
374365
ts.isStringLiteral(node.argumentExpression) &&
375366
legacyNames.has(node.argumentExpression.text)
376367
) {
377-
violations.push({
378-
line: toLine(sourceFile, node.argumentExpression),
379-
reason: `references ${subject} "${node.argumentExpression.text}"`,
380-
});
368+
addViolation(node.argumentExpression, "references", node.argumentExpression.text);
381369
}
382370

383371
if (ts.isCallExpression(node)) {
@@ -387,10 +375,7 @@ function findNamedBoundaryViolations(content, fileName, legacyNames, subject) {
387375
legacyNames.has(calleeName) &&
388376
ts.isIdentifier(unwrapExpression(node.expression))
389377
) {
390-
violations.push({
391-
line: toLine(sourceFile, node.expression),
392-
reason: `calls ${subject} "${calleeName}"`,
393-
});
378+
addViolation(node.expression, "calls", calleeName);
394379
}
395380
}
396381

@@ -512,7 +497,10 @@ export function findEmbeddedAgentSessionTargetViolations(content, fileName = "so
512497

513498
const visitRunOptions = (options) => {
514499
for (const property of options.properties) {
515-
if (ts.isPropertyAssignment(property) && propertyNameText(property.name) === "sessionFile") {
500+
if (
501+
ts.isPropertyAssignment(property) &&
502+
getPropertyNameText(property.name) === "sessionFile"
503+
) {
516504
recordDeprecatedSessionFile(property.name);
517505
} else if (
518506
ts.isShorthandPropertyAssignment(property) &&
@@ -641,39 +629,27 @@ const transcriptWriterSourceRootPaths = [
641629
"src/sessions",
642630
];
643631

644-
function declarationName(node) {
645-
if (ts.isFunctionDeclaration(node) && node.name) {
646-
return node.name.text;
647-
}
648-
if (!ts.isVariableStatement(node)) {
649-
return null;
650-
}
651-
const declaration = node.declarationList.declarations[0];
652-
return declaration && ts.isIdentifier(declaration.name) ? declaration.name.text : null;
653-
}
654-
655-
function functionBodyForDeclaration(node) {
656-
if (ts.isFunctionDeclaration(node)) {
657-
return node.body ?? null;
658-
}
659-
if (!ts.isVariableStatement(node)) {
660-
return null;
661-
}
662-
const declaration = node.declarationList.declarations[0];
663-
const initializer = declaration?.initializer;
664-
if (initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))) {
665-
return initializer.body;
666-
}
667-
return null;
668-
}
669-
670632
function collectTopLevelFunctionBodies(sourceFile) {
671633
const bodies = new Map();
672634
for (const statement of sourceFile.statements) {
673-
const name = declarationName(statement);
674-
const body = functionBodyForDeclaration(statement);
675-
if (name && body) {
676-
bodies.set(name, body);
635+
if (ts.isFunctionDeclaration(statement)) {
636+
if (statement.name && statement.body) {
637+
bodies.set(statement.name.text, statement.body);
638+
}
639+
continue;
640+
}
641+
if (!ts.isVariableStatement(statement)) {
642+
continue;
643+
}
644+
const declaration = statement.declarationList.declarations[0];
645+
const initializer = declaration?.initializer;
646+
if (
647+
declaration &&
648+
ts.isIdentifier(declaration.name) &&
649+
initializer &&
650+
(ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))
651+
) {
652+
bodies.set(declaration.name.text, initializer.body);
677653
}
678654
}
679655
return bodies;
@@ -870,117 +846,80 @@ async function writeSessionAccessorDebtBaseline(repoRoot) {
870846
await fs.writeFile(resolveDebtBaselinePath(repoRoot), `${JSON.stringify(counts, null, 2)}\n`);
871847
}
872848

849+
function compareMigratedFilePaths(left, right, sourceRootPaths) {
850+
const rootIndex = (filePath) =>
851+
sourceRootPaths.findIndex((sourceRoot) => filePath.startsWith(`${sourceRoot}/`));
852+
const leftRoot = rootIndex(left);
853+
const rightRoot = rootIndex(right);
854+
const rootOrder =
855+
(leftRoot < 0 ? sourceRootPaths.length : leftRoot) -
856+
(rightRoot < 0 ? sourceRootPaths.length : rightRoot);
857+
if (rootOrder !== 0) {
858+
return rootOrder;
859+
}
860+
// Recursive directory walks visit nested files before same-prefix sibling files.
861+
const leftTraversalPath = left.replaceAll("/", "\0");
862+
const rightTraversalPath = right.replaceAll("/", "\0");
863+
return leftTraversalPath < rightTraversalPath
864+
? -1
865+
: leftTraversalPath > rightTraversalPath
866+
? 1
867+
: 0;
868+
}
869+
873870
export async function main() {
874871
const repoRoot = resolveRepoRoot(import.meta.url);
875872
if (process.argv.includes("--update-debt-baseline")) {
876873
await writeSessionAccessorDebtBaseline(repoRoot);
877874
console.log(`Wrote ${sessionAccessorDebtBaselineRelativePath}`);
878875
return;
879876
}
880-
const readSourceRoots = resolveSourceRoots(repoRoot, readSourceRootPaths);
881-
const writeSourceRoots = resolveSourceRoots(repoRoot, writeSourceRootPaths);
882-
const transcriptWriterSourceRoots = resolveSourceRoots(repoRoot, transcriptWriterSourceRootPaths);
883-
const readViolations = await collectFileViolations({
884-
repoRoot,
885-
sourceRoots: readSourceRoots,
886-
skipFile: (filePath) => {
887-
const relativePath = normalizeRelativePath(path.relative(repoRoot, filePath));
888-
return (
889-
!migratedSessionAccessorFiles.has(relativePath) &&
890-
!migratedBundledPluginSessionAccessorFiles.has(relativePath)
891-
);
877+
const debtConcerns = Object.fromEntries(
878+
sessionAccessorDebtConcerns.map((concern) => [concern.key, concern]),
879+
);
880+
const enforcementConcerns = [
881+
debtConcerns.sessionAccessorRead,
882+
debtConcerns.sessionAccessorWrite,
883+
debtConcerns.transcriptWriter,
884+
{
885+
sourceRootPaths: ["src/gateway/server-methods"],
886+
migratedFiles: new Set(["src/gateway/server-methods/sessions-create.ts"]),
887+
findViolations: findGatewaySessionCreateLifecycleViolations,
892888
},
893-
findViolations: findSessionAccessorBoundaryViolations,
894-
});
895-
const writeViolations = await collectFileViolations({
896-
repoRoot,
897-
sourceRoots: writeSourceRoots,
898-
skipFile: (filePath) =>
899-
!migratedSessionAccessorWriteFiles.has(
900-
normalizeRelativePath(path.relative(repoRoot, filePath)),
901-
),
902-
findViolations: findSessionAccessorWriteBoundaryViolations,
903-
});
904-
const transcriptWriterViolations = await collectFileViolations({
905-
repoRoot,
906-
sourceRoots: transcriptWriterSourceRoots,
907-
skipFile: (filePath) =>
908-
!migratedTranscriptWriterFiles.has(normalizeRelativePath(path.relative(repoRoot, filePath))),
909-
findViolations: findTranscriptWriterBoundaryViolations,
910-
});
911-
const sessionCreateLifecycleViolations = await collectFileViolations({
912-
repoRoot,
913-
sourceRoots: resolveSourceRoots(repoRoot, ["src/gateway/server-methods"]),
914-
skipFile: (filePath) =>
915-
normalizeRelativePath(path.relative(repoRoot, filePath)) !==
916-
"src/gateway/server-methods/sessions-create.ts",
917-
findViolations: findGatewaySessionCreateLifecycleViolations,
918-
});
919-
const manualCompactTrimViolations = await collectFileViolations({
920-
repoRoot,
921-
sourceRoots: resolveSourceRoots(repoRoot, ["src/gateway/server-methods"]),
922-
skipFile: (filePath) =>
923-
!migratedSessionCompactManualTrimFiles.has(
924-
normalizeRelativePath(path.relative(repoRoot, filePath)),
925-
),
926-
findViolations: findSessionCompactManualTrimBoundaryViolations,
927-
});
928-
const lifecycleCleanupViolations = await collectFileViolations({
929-
repoRoot,
930-
sourceRoots: readSourceRoots,
931-
skipFile: (filePath) =>
932-
!migratedSessionLifecycleCleanupFiles.has(
933-
normalizeRelativePath(path.relative(repoRoot, filePath)),
934-
),
935-
findViolations: findSessionLifecycleCleanupBoundaryViolations,
936-
});
937-
const memoryHostSessionCorpusViolations = await collectFileViolations({
938-
repoRoot,
939-
sourceRoots: resolveSourceRoots(repoRoot, ["packages/memory-host-sdk/src/host"]),
940-
skipFile: (filePath) =>
941-
!migratedMemoryHostSessionCorpusFiles.has(
942-
normalizeRelativePath(path.relative(repoRoot, filePath)),
943-
),
944-
findViolations: findMemoryHostSessionCorpusBoundaryViolations,
945-
});
946-
const embeddedAgentSessionTargetViolations = await collectFileViolations({
947-
repoRoot,
948-
sourceRoots: resolveSourceRoots(repoRoot, ["extensions/voice-call/src"]),
949-
skipFile: (filePath) =>
950-
!migratedEmbeddedAgentSessionTargetFiles.has(
951-
normalizeRelativePath(path.relative(repoRoot, filePath)),
952-
),
953-
findViolations: findEmbeddedAgentSessionTargetViolations,
954-
});
955-
const readOnlyGatewaySessionAccessorViolations = await collectFileViolations({
956-
repoRoot,
957-
sourceRoots: resolveSourceRoots(repoRoot, ["src/gateway"]),
958-
skipFile: (filePath) =>
959-
!readOnlyGatewaySessionAccessorFiles.has(
960-
normalizeRelativePath(path.relative(repoRoot, filePath)),
961-
),
962-
findViolations: findReadOnlySessionAccessorViolations,
963-
});
889+
debtConcerns.sessionCompactManualTrim,
890+
debtConcerns.sessionLifecycleCleanup,
891+
debtConcerns.memoryHostSessionCorpus,
892+
debtConcerns.embeddedAgentSessionTarget,
893+
{
894+
sourceRootPaths: ["src/gateway"],
895+
migratedFiles: readOnlyGatewaySessionAccessorFiles,
896+
findViolations: findReadOnlySessionAccessorViolations,
897+
},
898+
];
899+
const violations = [];
900+
for (const concern of enforcementConcerns) {
901+
violations.push(
902+
...(await collectFileViolations({
903+
repoRoot,
904+
sourceRoots: resolveSourceRoots(
905+
repoRoot,
906+
[...concern.migratedFiles].toSorted((left, right) =>
907+
compareMigratedFilePaths(left, right, concern.sourceRootPaths),
908+
),
909+
),
910+
findViolations: concern.findViolations,
911+
})),
912+
);
913+
}
964914
const sessionStoreRuntimePath = path.join(repoRoot, "src/plugin-sdk/session-store-runtime.ts");
965-
const sessionStoreRuntimeCompatViolations =
966-
findSessionStoreRuntimeFileBackedCompatExportViolations(
915+
violations.push(
916+
...findSessionStoreRuntimeFileBackedCompatExportViolations(
967917
await fs.readFile(sessionStoreRuntimePath, "utf8"),
968918
sessionStoreRuntimePath,
969919
).map((violation) =>
970920
Object.assign({ path: "src/plugin-sdk/session-store-runtime.ts" }, violation),
971-
);
972-
const violations = [
973-
...readViolations,
974-
...writeViolations,
975-
...transcriptWriterViolations,
976-
...sessionCreateLifecycleViolations,
977-
...manualCompactTrimViolations,
978-
...lifecycleCleanupViolations,
979-
...memoryHostSessionCorpusViolations,
980-
...embeddedAgentSessionTargetViolations,
981-
...readOnlyGatewaySessionAccessorViolations,
982-
...sessionStoreRuntimeCompatViolations,
983-
];
921+
),
922+
);
984923

985924
const baselineCounts = await readSessionAccessorDebtBaseline(repoRoot);
986925
if (!baselineCounts) {

0 commit comments

Comments
 (0)