Skip to content

Commit 899ffa2

Browse files
committed
fix(grok): discover workflow scripts through Effect FileSystem
Macroscope flagged readGrokWorkflowSlashCommands as blocking node:fs behind a plain function, so snapshot effects never declared the filesystem they used. Scan ~/.grok/workflows and project scripts with FileSystem and Path, and thread that requirement through the initial snapshot and health check.
1 parent 4f0eb5d commit 899ffa2

4 files changed

Lines changed: 132 additions & 121 deletions

File tree

‎apps/server/src/provider/Layers/GrokProvider.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { buildInitialGrokProviderSnapshot, checkGrokProviderStatus } from "./Gro
1010

1111
const decodeGrokSettings = Schema.decodeSync(GrokSettings);
1212

13-
describe("buildInitialGrokProviderSnapshot", () => {
13+
it.layer(NodeServices.layer)("buildInitialGrokProviderSnapshot", (it) => {
1414
it.effect("returns a disabled snapshot when settings.enabled is false", () =>
1515
Effect.gen(function* () {
1616
const snapshot = yield* buildInitialGrokProviderSnapshot(

‎apps/server/src/provider/Layers/GrokProvider.ts‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import * as Crypto from "effect/Crypto";
1010
import * as DateTime from "effect/DateTime";
1111
import * as Effect from "effect/Effect";
1212
import * as Exit from "effect/Exit";
13+
import * as FileSystem from "effect/FileSystem";
1314
import * as Option from "effect/Option";
15+
import * as Path from "effect/Path";
1416
import * as Result from "effect/Result";
1517
import { HttpClient } from "effect/unstable/http";
1618
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
@@ -46,23 +48,25 @@ const GROK_PRESENTATION = {
4648
} as const;
4749
const FALLBACK_CAPABILITIES: ModelCapabilities = fallbackGrokReasoningEffortCapabilities();
4850

49-
function buildGrokServerProvider(
51+
const buildGrokServerProvider = (
5052
input: Parameters<typeof buildServerProvider>[0],
5153
discovery: {
5254
readonly environment: NodeJS.ProcessEnv;
5355
readonly projectRoot?: string | undefined;
5456
},
55-
): ServerProviderDraft {
56-
return buildServerProvider({
57-
...input,
58-
slashCommands:
57+
) =>
58+
Effect.gen(function* () {
59+
const slashCommands =
5960
input.slashCommands ??
60-
readGrokWorkflowSlashCommands({
61+
(yield* readGrokWorkflowSlashCommands({
6162
homeDir: discovery.environment.HOME,
6263
projectRoot: discovery.projectRoot,
63-
}),
64+
}));
65+
return buildServerProvider({
66+
...input,
67+
slashCommands,
68+
});
6469
});
65-
}
6670

6771
const VERSION_PROBE_TIMEOUT_MS = 4_000;
6872
const GROK_ACP_MODEL_DISCOVERY_TIMEOUT_MS = 15_000;
@@ -83,7 +87,7 @@ export function buildInitialGrokProviderSnapshot(
8387
readonly environment?: NodeJS.ProcessEnv | undefined;
8488
readonly projectRoot?: string | undefined;
8589
},
86-
): Effect.Effect<ServerProviderDraft> {
90+
): Effect.Effect<ServerProviderDraft, never, FileSystem.FileSystem | Path.Path> {
8791
return Effect.gen(function* () {
8892
const checkedAt = yield* Effect.map(DateTime.now, DateTime.formatIso);
8993
const models = grokModelsFromSettings(grokSettings.customModels);
@@ -93,7 +97,7 @@ export function buildInitialGrokProviderSnapshot(
9397
projectRoot: discovery?.projectRoot,
9498
};
9599
if (!grokSettings.enabled) {
96-
return buildGrokServerProvider(
100+
return yield* buildGrokServerProvider(
97101
{
98102
presentation: GROK_PRESENTATION,
99103
enabled: false,
@@ -111,7 +115,7 @@ export function buildInitialGrokProviderSnapshot(
111115
);
112116
}
113117

114-
return buildGrokServerProvider(
118+
return yield* buildGrokServerProvider(
115119
{
116120
presentation: GROK_PRESENTATION,
117121
enabled: true,
@@ -210,7 +214,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
210214
): Effect.fn.Return<
211215
ServerProviderDraft,
212216
never,
213-
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto
217+
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | FileSystem.FileSystem | Path.Path
214218
> {
215219
const checkedAt = DateTime.formatIso(yield* DateTime.now);
216220
const fallbackModels = grokModelsFromSettings(grokSettings.customModels);
@@ -219,7 +223,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
219223
buildGrokServerProvider(input, discovery);
220224

221225
if (!grokSettings.enabled) {
222-
return providerDraft({
226+
return yield* providerDraft({
223227
presentation: GROK_PRESENTATION,
224228
enabled: false,
225229
checkedAt,
@@ -244,7 +248,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
244248
yield* Effect.logWarning("Grok CLI health check failed.", {
245249
errorTag: error._tag,
246250
});
247-
return providerDraft({
251+
return yield* providerDraft({
248252
presentation: GROK_PRESENTATION,
249253
enabled: grokSettings.enabled,
250254
checkedAt,
@@ -262,7 +266,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
262266
}
263267

264268
if (Option.isNone(versionResult.success)) {
265-
return providerDraft({
269+
return yield* providerDraft({
266270
presentation: GROK_PRESENTATION,
267271
enabled: grokSettings.enabled,
268272
checkedAt,
@@ -285,7 +289,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
285289
stdoutLength: versionOutput.stdout.length,
286290
stderrLength: versionOutput.stderr.length,
287291
});
288-
return providerDraft({
292+
return yield* providerDraft({
289293
presentation: GROK_PRESENTATION,
290294
enabled: grokSettings.enabled,
291295
checkedAt,
@@ -310,7 +314,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
310314
errorTag: causeErrorTag(discoveryExit.cause),
311315
authFailed,
312316
});
313-
return providerDraft({
317+
return yield* providerDraft({
314318
presentation: GROK_PRESENTATION,
315319
enabled: grokSettings.enabled,
316320
checkedAt,
@@ -330,7 +334,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
330334
yield* Effect.logWarning(
331335
`Grok ACP model discovery timed out after ${GROK_ACP_MODEL_DISCOVERY_TIMEOUT_MS}ms.`,
332336
);
333-
return providerDraft({
337+
return yield* providerDraft({
334338
presentation: GROK_PRESENTATION,
335339
enabled: grokSettings.enabled,
336340
checkedAt,
@@ -350,7 +354,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
350354
? grokModelsFromSettings(grokSettings.customModels, discoveredModels)
351355
: fallbackModels;
352356

353-
return providerDraft({
357+
return yield* providerDraft({
354358
presentation: GROK_PRESENTATION,
355359
enabled: grokSettings.enabled,
356360
checkedAt,
Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// @effect-diagnostics nodeBuiltinImport:off
2-
import * as NodeFS from "node:fs";
3-
import * as NodeOS from "node:os";
4-
import * as NodePath from "node:path";
5-
6-
import { describe, expect, it } from "vite-plus/test";
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { describe, expect, it } from "@effect/vitest";
3+
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
5+
import * as Path from "effect/Path";
76

87
import {
98
GROK_WORKFLOW_CONTROL_COMMANDS,
@@ -41,53 +40,64 @@ agent("review", "look at the diff")
4140
});
4241
});
4342

44-
describe("readGrokWorkflowSlashCommands", () => {
45-
it("includes pause/resume/stop and project scripts override user scripts", () => {
46-
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "grok-wf-"));
47-
const userDir = NodePath.join(root, "home", ".grok", "workflows");
48-
const projectDir = NodePath.join(root, "project", ".grok", "workflows");
49-
NodeFS.mkdirSync(userDir, { recursive: true });
50-
NodeFS.mkdirSync(projectDir, { recursive: true });
51-
NodeFS.writeFileSync(
52-
NodePath.join(userDir, "review-changes.rhai"),
53-
`let meta = #{ name: "review-changes", description: "user copy" };\n`,
54-
);
55-
NodeFS.writeFileSync(
56-
NodePath.join(projectDir, "review-changes.rhai"),
57-
`let meta = #{ name: "review-changes", description: "project copy" };\n`,
58-
);
59-
NodeFS.writeFileSync(
60-
NodePath.join(projectDir, "nowah-web-e2e.rhai"),
61-
`let meta = #{ name: "nowah-web-e2e", description: "Write locked Playwright specs" };\n`,
62-
);
43+
it.layer(NodeServices.layer)("readGrokWorkflowSlashCommands", (it) => {
44+
it.effect("includes pause/resume/stop and project scripts override user scripts", () =>
45+
Effect.scoped(
46+
Effect.gen(function* () {
47+
const fs = yield* FileSystem.FileSystem;
48+
const path = yield* Path.Path;
49+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "grok-wf-" });
50+
const home = path.join(root, "home");
51+
const project = path.join(root, "project");
52+
yield* fs.makeDirectory(path.join(home, ".grok", "workflows"), { recursive: true });
53+
yield* fs.makeDirectory(path.join(project, ".grok", "workflows"), { recursive: true });
54+
yield* fs.writeFileString(
55+
path.join(home, ".grok", "workflows", "review-changes.rhai"),
56+
`let meta = #{ name: "review-changes", description: "user copy" };\n`,
57+
);
58+
yield* fs.writeFileString(
59+
path.join(project, ".grok", "workflows", "review-changes.rhai"),
60+
`let meta = #{ name: "review-changes", description: "project copy" };\n`,
61+
);
62+
yield* fs.writeFileString(
63+
path.join(project, ".grok", "workflows", "nowah-web-e2e.rhai"),
64+
`let meta = #{ name: "nowah-web-e2e", description: "Write locked Playwright specs" };\n`,
65+
);
6366

64-
const commands = readGrokWorkflowSlashCommands({
65-
homeDir: NodePath.join(root, "home"),
66-
projectRoot: NodePath.join(root, "project"),
67-
});
68-
expect(commands.slice(0, 3)).toEqual([...GROK_WORKFLOW_CONTROL_COMMANDS]);
69-
expect(commands).toContainEqual({
70-
name: "workflow review-changes",
71-
description: "project copy",
72-
});
73-
expect(commands).toContainEqual({
74-
name: "workflow nowah-web-e2e",
75-
description: "Write locked Playwright specs",
76-
});
77-
});
67+
const commands = yield* readGrokWorkflowSlashCommands({
68+
homeDir: home,
69+
projectRoot: project,
70+
});
71+
expect(commands.slice(0, 3)).toEqual([...GROK_WORKFLOW_CONTROL_COMMANDS]);
72+
expect(commands).toContainEqual({
73+
name: "workflow review-changes",
74+
description: "project copy",
75+
});
76+
expect(commands).toContainEqual({
77+
name: "workflow nowah-web-e2e",
78+
description: "Write locked Playwright specs",
79+
});
80+
}),
81+
),
82+
);
7883

79-
it("reads only the capped prefix of an oversized workflow script", () => {
80-
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "grok-wf-cap-"));
81-
const userDir = NodePath.join(root, ".grok", "workflows");
82-
NodeFS.mkdirSync(userDir, { recursive: true });
83-
NodeFS.writeFileSync(
84-
NodePath.join(userDir, "huge.rhai"),
85-
`let meta = #{ name: "huge", description: "from prefix" };\n` + "x".repeat(80 * 1024),
86-
);
87-
const commands = readGrokWorkflowSlashCommands({ homeDir: root });
88-
expect(commands).toContainEqual({
89-
name: "workflow huge",
90-
description: "from prefix",
91-
});
92-
});
84+
it.effect("reads only the capped prefix of an oversized workflow script", () =>
85+
Effect.scoped(
86+
Effect.gen(function* () {
87+
const fs = yield* FileSystem.FileSystem;
88+
const path = yield* Path.Path;
89+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "grok-wf-cap-" });
90+
yield* fs.makeDirectory(path.join(root, ".grok", "workflows"), { recursive: true });
91+
yield* fs.writeFileString(
92+
path.join(root, ".grok", "workflows", "huge.rhai"),
93+
`let meta = #{ name: "huge", description: "from prefix" };\n` + "x".repeat(80 * 1024),
94+
);
95+
const commands = yield* readGrokWorkflowSlashCommands({ homeDir: root });
96+
expect(commands).toContainEqual({
97+
name: "workflow huge",
98+
description: "from prefix",
99+
});
100+
}),
101+
),
102+
);
93103
});

‎apps/server/src/provider/acp/GrokWorkflowCommands.ts‎

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// @effect-diagnostics nodeBuiltinImport:off
2-
import * as NodeFS from "node:fs";
3-
import * as NodeOS from "node:os";
4-
import * as NodePath from "node:path";
5-
61
import type { ServerProviderSlashCommand } from "@t3tools/contracts";
2+
import * as Effect from "effect/Effect";
3+
import * as FileSystem from "effect/FileSystem";
4+
import * as Path from "effect/Path";
75

86
const SCRIPT_BYTE_CAP = 64 * 1024;
97

@@ -65,65 +63,64 @@ export function grokWorkflowSlashCommandFromMeta(
6563
};
6664
}
6765

68-
function readWorkflowDir(dir: string, byName: Map<string, ServerProviderSlashCommand>): void {
69-
let entries: NodeFS.Dirent[];
70-
try {
71-
entries = NodeFS.readdirSync(dir, { withFileTypes: true });
72-
} catch {
73-
return;
74-
}
66+
const readWorkflowDir = Effect.fn("grok.readWorkflowDir")(function* (
67+
dir: string,
68+
byName: Map<string, ServerProviderSlashCommand>,
69+
) {
70+
const fileSystem = yield* FileSystem.FileSystem;
71+
const path = yield* Path.Path;
72+
const entries = yield* fileSystem
73+
.readDirectory(dir)
74+
.pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));
75+
7576
for (const entry of entries) {
76-
if (!entry.isFile() || !entry.name.endsWith(".rhai")) {
77+
if (!entry.endsWith(".rhai")) {
7778
continue;
7879
}
79-
const filePath = NodePath.join(dir, entry.name);
80-
let source: string;
81-
try {
82-
const stat = NodeFS.statSync(filePath);
83-
if (!stat.isFile() || stat.size <= 0) {
84-
continue;
85-
}
86-
const length = Math.min(stat.size, SCRIPT_BYTE_CAP);
87-
const handle = NodeFS.openSync(filePath, "r");
88-
try {
89-
const buffer = Buffer.alloc(length);
90-
const bytesRead = NodeFS.readSync(handle, buffer, 0, length, 0);
91-
source = buffer.subarray(0, bytesRead).toString("utf8");
92-
} finally {
93-
NodeFS.closeSync(handle);
94-
}
95-
} catch {
80+
const filePath = path.join(dir, entry);
81+
const info = yield* fileSystem.stat(filePath).pipe(Effect.orElseSucceed(() => undefined));
82+
if (!info || info.type !== "File" || info.size <= 0n) {
9683
continue;
9784
}
98-
const fallbackName = NodePath.basename(entry.name, ".rhai");
85+
const bytes = yield* fileSystem.readFile(filePath).pipe(Effect.orElseSucceed(() => undefined));
86+
if (!bytes) {
87+
continue;
88+
}
89+
const source = new TextDecoder("utf8").decode(bytes.subarray(0, SCRIPT_BYTE_CAP));
90+
const fallbackName = entry.endsWith(".rhai") ? entry.slice(0, -".rhai".length) : entry;
9991
const meta = parseGrokWorkflowScriptMeta(source, fallbackName);
10092
if (!meta) {
10193
continue;
10294
}
10395
const command = grokWorkflowSlashCommandFromMeta(meta);
10496
byName.set(command.name, command);
10597
}
106-
}
98+
});
10799

108100
/**
109101
* Built-in `/workflow pause|resume|stop` plus `~/.grok/workflows` and
110102
* `<project>/.grok/workflows` scripts. Project scripts override user scripts
111103
* of the same command name. T3 sends the slash text as a prompt — it does not
112104
* host Rhai.
113105
*/
114-
export function readGrokWorkflowSlashCommands(input: {
115-
readonly projectRoot?: string | undefined;
116-
readonly homeDir?: string | undefined;
117-
}): ReadonlyArray<ServerProviderSlashCommand> {
118-
const byName = new Map<string, ServerProviderSlashCommand>();
119-
for (const command of GROK_WORKFLOW_CONTROL_COMMANDS) {
120-
byName.set(command.name, command);
121-
}
122-
const homeDir = trimmed(input.homeDir) ?? NodeOS.homedir();
123-
readWorkflowDir(NodePath.join(homeDir, ".grok", "workflows"), byName);
124-
const projectRoot = trimmed(input.projectRoot);
125-
if (projectRoot && NodePath.isAbsolute(projectRoot)) {
126-
readWorkflowDir(NodePath.join(projectRoot, ".grok", "workflows"), byName);
127-
}
128-
return [...byName.values()];
129-
}
106+
export const readGrokWorkflowSlashCommands = Effect.fn("grok.readWorkflowSlashCommands")(
107+
function* (input: {
108+
readonly projectRoot?: string | undefined;
109+
readonly homeDir?: string | undefined;
110+
}) {
111+
const path = yield* Path.Path;
112+
const byName = new Map<string, ServerProviderSlashCommand>();
113+
for (const command of GROK_WORKFLOW_CONTROL_COMMANDS) {
114+
byName.set(command.name, command);
115+
}
116+
const homeDir = trimmed(input.homeDir);
117+
if (homeDir) {
118+
yield* readWorkflowDir(path.join(homeDir, ".grok", "workflows"), byName);
119+
}
120+
const projectRoot = trimmed(input.projectRoot);
121+
if (projectRoot && path.isAbsolute(projectRoot)) {
122+
yield* readWorkflowDir(path.join(projectRoot, ".grok", "workflows"), byName);
123+
}
124+
return [...byName.values()];
125+
},
126+
);

0 commit comments

Comments
 (0)