Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/typescript/scripts/check-package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ const distFiles = new Set(
"scan-dashboard",
"scan-history-renderer",
"scan-logs",
"scan-sessions",
"targets",
"trusted-executable",
"version",
Expand Down
41 changes: 14 additions & 27 deletions sdk/typescript/src/cost.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { open, readdir } from "node:fs/promises";
import { isAbsolute, join, relative, sep } from "node:path";
import { join } from "node:path";
import {
estimateScanCost,
tokenUsage,
Expand All @@ -10,6 +10,11 @@ import {
scanActivityFromSessionEvent,
type ScanActivity,
} from "./scan-activity.js";
import {
isScanArtifactDirectory,
sessionParentThreadId,
sessionStartedAt,
} from "./scan-sessions.js";
import {
scanProgressUpdatesFromEvent,
type ScanProgress,
Expand Down Expand Up @@ -210,29 +215,19 @@ export class ScanCostTracker {
for (const session of this.#sessions.values()) {
if (
session.threadId === null ||
session.parentThreadId !== null ||
Comment thread
mldangelo-oai marked this conversation as resolved.
session.workingDirectory === null ||
scanStartedAt === null ||
session.startedAt === null ||
session.startedAt < scanStartedAt
) {
continue;
}
const artifactsDirectory = join(
this.#options.scanDirectory,
"artifacts",
);
const workers = join(artifactsDirectory, "deep_discovery", "workers");
const workerDirectory = relative(workers, session.workingDirectory);
const components = workerDirectory.split(sep);
if (
relative(artifactsDirectory, session.workingDirectory) === "" ||
(!isAbsolute(workerDirectory) &&
components.length === 2 &&
components[0] !== ".." &&
relative(
join(workers, components[0]!, "output"),
session.workingDirectory,
) === "")
isScanArtifactDirectory(
this.#options.scanDirectory,
session.workingDirectory,
)
) {
included.add(session.threadId);
}
Expand Down Expand Up @@ -470,16 +465,8 @@ function readSessionEvent(
if (typeof payload["cwd"] === "string") {
session.workingDirectory = payload["cwd"];
}
if (typeof payload["timestamp"] === "string") {
session.startedAt = Math.floor(Date.parse(payload["timestamp"]) / 1_000);
}
const source = payload["source"];
const subagent = isRecord(source) ? source["subagent"] : undefined;
const spawn = isRecord(subagent) ? subagent["thread_spawn"] : undefined;
const parent =
payload["parent_thread_id"] ??
(isRecord(spawn) ? spawn["parent_thread_id"] : undefined);
if (typeof parent === "string") session.parentThreadId = parent;
session.startedAt = sessionStartedAt(payload["timestamp"]);
session.parentThreadId = sessionParentThreadId(payload);
session.events?.push(event);
return;
}
Expand All @@ -493,7 +480,7 @@ function readSessionEvent(
payload["type"] === "task_started" &&
typeof payload["started_at"] === "number" &&
session.startedAt !== null &&
payload["started_at"] >= session.startedAt
payload["started_at"] >= Math.floor(session.startedAt / 1_000)
) {
session.replaying = false;
session.events?.push(event);
Expand Down
36 changes: 9 additions & 27 deletions sdk/typescript/src/scan-logs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { createReadStream } from "node:fs";
import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";
import { basename, dirname, join, relative } from "node:path";
import { createInterface } from "node:readline";
import { sessionFiles } from "./cost.js";
import { CodexSecurityError } from "./errors.js";
import {
isScanArtifactDirectory,
sessionParentThreadId,
sessionStartedAt,
} from "./scan-sessions.js";

interface ScanLogOptions {
scanId: string;
Expand Down Expand Up @@ -30,20 +35,10 @@ export async function readScanLogs(options: ScanLogOptions) {
const metadata = first["payload"];
const threadId = metadata["id"];
if (typeof threadId !== "string") break;
const source = metadata["source"];
const subagent = isRecord(source) ? source["subagent"] : undefined;
const spawn = isRecord(subagent) ? subagent["thread_spawn"] : undefined;
const parent =
metadata["parent_thread_id"] ??
(isRecord(spawn) ? spawn["parent_thread_id"] : undefined);
const startedAt =
typeof metadata["timestamp"] === "string"
? Date.parse(metadata["timestamp"])
: Number.NaN;
logs.set(threadId, {
threadId,
parentThreadId: typeof parent === "string" ? parent : null,
startedAt: Number.isFinite(startedAt) ? startedAt : null,
parentThreadId: sessionParentThreadId(metadata),
startedAt: sessionStartedAt(metadata["timestamp"]),
workingDirectory:
typeof metadata["cwd"] === "string" ? metadata["cwd"] : null,
path,
Expand Down Expand Up @@ -149,20 +144,7 @@ function belongsToScan(
}

for (const directoryRoot of roots) {
const artifacts = join(directoryRoot, "artifacts");
if (relative(artifacts, session.workingDirectory) === "") return true;
const workers = join(artifacts, "deep_discovery", "workers");
const directory = relative(workers, session.workingDirectory);
const components = directory.split(sep);
if (
!isAbsolute(directory) &&
components.length === 2 &&
components[0] !== ".." &&
relative(
join(workers, components[0]!, "output"),
session.workingDirectory,
) === ""
) {
if (isScanArtifactDirectory(directoryRoot, session.workingDirectory)) {
return true;
}
}
Expand Down
45 changes: 45 additions & 0 deletions sdk/typescript/src/scan-sessions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { isAbsolute, join, relative, sep } from "node:path";

export function sessionStartedAt(timestamp: unknown): number | null {
const startedAt =
typeof timestamp === "string" ? Date.parse(timestamp) : Number.NaN;
return Number.isFinite(startedAt) ? startedAt : null;
}

export function sessionParentThreadId(
metadata: Readonly<Record<string, unknown>>,
): string | null {
const source = metadata["source"];
const subagent = isRecord(source) ? source["subagent"] : undefined;
const spawn = isRecord(subagent) ? subagent["thread_spawn"] : undefined;
for (const parent of [
isRecord(spawn) ? spawn["parent_thread_id"] : undefined,
metadata["parent_thread_id"],
metadata["forked_from_id"],
]) {
if (typeof parent === "string" && parent !== "") return parent;
}
return null;
}

export function isScanArtifactDirectory(
scanDirectory: string,
workingDirectory: string,
): boolean {
const artifacts = join(scanDirectory, "artifacts");
if (relative(artifacts, workingDirectory) === "") return true;

const workers = join(artifacts, "deep_discovery", "workers");
const directory = relative(workers, workingDirectory);
const components = directory.split(sep);
return (
!isAbsolute(directory) &&
components.length === 2 &&
components[0] !== ".." &&
relative(join(workers, components[0]!, "output"), workingDirectory) === ""
);
}

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
Loading
Loading