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
13 changes: 13 additions & 0 deletions .changeset/fix-event-accumulation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"perstack": patch
"@perstack/filesystem-storage": patch
---

fix(tui): fix event accumulation across multiple runs in start command

When using `perstack start` with interactive tools, events from all runs in a job
were not being loaded because `getAllEventContentsForJob` relied on `run-setting.json`
files which are not always present.

Added `getRunIdsByJobId` function that scans the runs directory directly, ensuring
all runs are discovered even when `run-setting.json` is missing.
7 changes: 4 additions & 3 deletions apps/perstack/src/lib/run-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { checkpointSchema } from "@perstack/core"
import {
getCheckpointPath,
getEventsByRun,
getRunIdsByJobId,
getAllJobs as runtimeGetAllJobs,
getAllRuns as runtimeGetAllRuns,
getCheckpointsByJobId as runtimeGetCheckpointsByJobId,
Expand Down Expand Up @@ -119,10 +120,10 @@ export function getEventContents(jobId: string, runId: string, maxStepNumber?: n
}

export function getAllEventContentsForJob(jobId: string, maxStepNumber?: number): RunEvent[] {
const runs = getRunsByJobId(jobId)
const runIds = getRunIdsByJobId(jobId)
const allEvents: RunEvent[] = []
for (const run of runs) {
const events = runtimeGetEventContents(jobId, run.runId, maxStepNumber)
for (const runId of runIds) {
const events = runtimeGetEventContents(jobId, runId, maxStepNumber)
allEvents.push(...events)
}
return allEvents.sort((a, b) => a.timestamp - b.timestamp)
Expand Down
11 changes: 11 additions & 0 deletions packages/storages/filesystem/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs"
import { mkdir, writeFile } from "node:fs/promises"
import path from "node:path"
import type { RunEvent } from "@perstack/core"
import { getJobDir } from "./job.js"
import { defaultGetRunDir as getRunDir } from "./run-setting.js"

export async function defaultStoreEvent(event: RunEvent): Promise<void> {
Expand Down Expand Up @@ -53,3 +54,13 @@ export function getEventContents(jobId: string, runId: string, maxStepNumber?: n
}
return events
}

export function getRunIdsByJobId(jobId: string): string[] {
const runsDir = path.resolve(getJobDir(jobId), "runs")
if (!existsSync(runsDir)) {
return []
}
return readdirSync(runsDir, { withFileTypes: true })
.filter((dir) => dir.isDirectory())
.map((dir) => dir.name)
}
2 changes: 1 addition & 1 deletion packages/storages/filesystem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export {
getCheckpointPath,
getCheckpointsByJobId,
} from "./checkpoint.js"
export { defaultStoreEvent, getEventContents, getEventsByRun } from "./event.js"
export { defaultStoreEvent, getEventContents, getEventsByRun, getRunIdsByJobId } from "./event.js"
export { FileSystemStorage, type FileSystemStorageConfig } from "./filesystem-storage.js"
export {
createInitialJob,
Expand Down