Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeOS from "node:os";
3
4import { TRIAGE_PROMPT_FILE_NAME } from "@t3tools/contracts";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6import * as DateTime from "effect/DateTime";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Option from "effect/Option";
10import * as Path from "effect/Path";
11
12import * as BootService from "../cloud/bootService.ts";
13import type * as ServerConfig from "../config.ts";
14import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
15import {
16 buildTriageContext,
17 buildTriageSeedPrompt,
18 type TriageEntry,
19 type TriageSourceClone,
20} from "./triagePrompt.ts";
21
22export const describeServerProcess = Effect.fn("triage.describeServerProcess")(function* (
23 serverRuntimeStatePath: string,
24) {
25 const state = yield* readPersistedServerRuntimeState(serverRuntimeStatePath);
26 if (Option.isNone(state)) {
27 return "not running (no server-runtime.json; the server may never have started here)";
28 }
29 const version =
30 state.value.serverVersion === undefined
31 ? "version not recorded"
32 : `version ${state.value.serverVersion}`;
33 if (!isProcessAlive(state.value.pid)) {
34 return `not running (state file is stale: pid ${String(state.value.pid)} is dead; last origin ${state.value.origin}, ${version})`;
35 }
36 return `running (pid ${String(state.value.pid)}, ${state.value.origin}, ${version})`;
37});
38
39/** The source cache the playbook clones into, one subdirectory per commit. */
40export const triageSourceCacheDir = (baseDir: string, path: Path.Path) =>
41 path.join(baseDir, "source");
42
43/**
44 * Writes `context.md` and the playbook prompt into a fresh `<stateDir>/triage/<time>`
45 * folder. Both `t3 triage` and the in-app entry point start from these files.
46 */
47export const writeTriageSession = Effect.fn("triage.writeTriageSession")(function* (input: {
48 readonly baseDir: string;
49 readonly paths: ServerConfig.ServerDerivedPaths;
50 readonly entry: TriageEntry;
51 readonly server: string;
52 readonly sourceClone?: TriageSourceClone | undefined;
53}) {
54 const fs = yield* FileSystem.FileSystem;
55 const path = yield* Path.Path;
56 const { paths } = input;
57
58 const now = yield* DateTime.now;
59 const sessionDir = path.join(
60 paths.stateDir,
61 "triage",
62 DateTime.formatIso(now).replaceAll(":", "-").replace(".", "-"),
63 );
64 yield* fs.makeDirectory(sessionDir, { recursive: true });
65
66 const contextFilePath = path.join(sessionDir, "context.md");
67 yield* fs.writeFileString(
68 contextFilePath,
69 buildTriageContext({
70 generatedAt: DateTime.formatIso(now),
71 entry: input.entry,
72 os: `${yield* HostProcess.Platform} ${yield* HostProcess.Architecture} (${NodeOS.release()})`,
73 nodeVersion: process.version,
74 server: input.server,
75 sourceClone: input.sourceClone,
76 paths: {
77 stateDir: paths.stateDir,
78 dbPath: paths.dbPath,
79 settingsPath: paths.settingsPath,
80 logsDir: paths.logsDir,
81 serviceLogPath: path.join(paths.logsDir, BootService.BOOT_SERVICE_LOG_FILE),
82 desktopBackendLogGlob: path.join(paths.logsDir, "server-child*.log*"),
83 serverTracePath: paths.serverTracePath,
84 providerEventLogPath: paths.providerEventLogPath,
85 terminalLogsDir: paths.terminalLogsDir,
86 providerStatusCacheDir: paths.providerStatusCacheDir,
87 secretsDir: paths.secretsDir,
88 sourceCacheDir: triageSourceCacheDir(input.baseDir, path),
89 },
90 }),
91 );
92
93 const promptFilePath = path.join(sessionDir, TRIAGE_PROMPT_FILE_NAME);
94 yield* fs.writeFileString(
95 promptFilePath,
96 buildTriageSeedPrompt({ contextFilePath, entry: input.entry }),
97 );
98
99 return { sessionDir, contextFilePath, promptFilePath };
100});