@terreno/ai 57.20.0 → 57.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/biome.jsonc +1 -0
  3. package/dist/harness/agentLoop.js +1 -0
  4. package/dist/observability/local/experimentRunner.d.ts +2 -5
  5. package/dist/types/harness.d.ts +5 -2
  6. package/e2e/smoke.ts +227 -0
  7. package/package.json +14 -12
  8. package/src/aiApp.test.ts +22 -17
  9. package/src/harness/agentLoop.test.ts +1 -1
  10. package/src/harness/agentLoop.ts +2 -1
  11. package/src/harness/agentRecovery.test.ts +31 -28
  12. package/src/harness/approvals.test.ts +11 -9
  13. package/src/harness/events.test.ts +10 -6
  14. package/src/harness/extensions.test.ts +7 -6
  15. package/src/harness/ownership.test.ts +20 -9
  16. package/src/harness/retries.test.ts +6 -3
  17. package/src/harness/routes/conversations.test.ts +2 -2
  18. package/src/harness/routes/tasks.test.ts +2 -2
  19. package/src/harness/runners/jobsRunner.test.ts +1 -1
  20. package/src/harness/subagent.test.ts +3 -2
  21. package/src/harness/waits.test.ts +6 -4
  22. package/src/index.test.ts +3 -3
  23. package/src/isolated/gptStream.isolated.ts +33 -28
  24. package/src/isolated/langfuseClient.isolated.ts +9 -3
  25. package/src/isolated/lazyProjectModel.isolated.ts +2 -2
  26. package/src/langfuseApp.test.ts +4 -20
  27. package/src/langfuseRoutes.test.ts +20 -15
  28. package/src/langfuseTracing.test.ts +6 -2
  29. package/src/langfuseVercelAi.test.ts +12 -9
  30. package/src/models/gptHistory.test.ts +10 -6
  31. package/src/observability/evaluate.test.ts +12 -10
  32. package/src/observability/local/experimentRunner.test.ts +9 -12
  33. package/src/observability/local/experimentRunner.ts +2 -2
  34. package/src/observability/local/promptStore.test.ts +4 -3
  35. package/src/observability/routes/datasets.test.ts +2 -2
  36. package/src/observability/routes/evaluators.test.ts +2 -2
  37. package/src/observability/routes/experiments.test.ts +2 -2
  38. package/src/observability/routes/prompts.test.ts +3 -3
  39. package/src/observability/routes/review.test.ts +2 -2
  40. package/src/observability/routes/status.test.ts +4 -4
  41. package/src/observability/routes/testMultiStage.test.ts +6 -6
  42. package/src/observability/routes/traces.test.ts +2 -2
  43. package/src/routes/files.test.ts +5 -3
  44. package/src/routes/gpt.test.ts +21 -14
  45. package/src/routes/gptActions.test.ts +7 -8
  46. package/src/routes/gptDatasets.test.ts +1 -1
  47. package/src/routes/gptFiles.test.ts +1 -1
  48. package/src/routes/gptHistories.test.ts +15 -9
  49. package/src/routes/mcp.test.ts +3 -3
  50. package/src/routes/projects.test.ts +5 -3
  51. package/src/routes/routes.test.ts +58 -42
  52. package/src/service/aiService.test.ts +1 -1
  53. package/src/service/asks.test.ts +14 -6
  54. package/src/service/fileStorage.test.ts +5 -2
  55. package/src/service/getMCPTools.test.ts +9 -5
  56. package/src/service/mcpService.test.ts +6 -3
  57. package/src/tests/bunSetup.ts +1 -1
  58. package/src/tests/chatHarness.ts +4 -4
  59. package/src/tests/fakeJudge.ts +8 -0
  60. package/src/tests/fakeLangfusePrompt.ts +10 -0
  61. package/src/tests/helpers.ts +26 -4
  62. package/src/types/harness.ts +6 -2
  63. package/tsconfig.typecheck.json +18 -0
package/CHANGELOG.md CHANGED
@@ -20,6 +20,56 @@ Upgrade notes for consumer action live in [`mcp-server/src/docs/upgrades/`](mcp-
20
20
 
21
21
  Unreleased changes live in [`changelog/unreleased/`](changelog/unreleased/). Add one Markdown file per feature (see that directory's README) instead of editing this section.
22
22
 
23
+ ## [57.21.0] - 2026-11-01
24
+
25
+ Upgrade note: [`mcp-server/src/docs/upgrades/57.21.0.md`](mcp-server/src/docs/upgrades/57.21.0.md).
26
+
27
+ ### Breaking
28
+
29
+ - The `terreno-scan` plugin is now `terreno-loop`, and its skills are renamed from `terreno-scan-*` to `terreno-loop-*`. The resident loop `terreno-scan-loop` is now `terreno-loop-run`: `/terreno-loop:run` in Claude Code, `/terreno-loop-run` in Cursor, `$terreno-loop-run` in Codex. Claude Code moves existing installs over through the marketplace `renames` entry. In Cursor and Codex, uninstall `terreno-scan` and install `terreno-loop`. Campaign state stays at `.terreno/scan/<goal>.json`, so campaigns already in progress resume.
30
+
31
+ ### Added
32
+
33
+ - `@terreno/admin-spa` serves an MCP OAuth consent screen at `/console/oauth/consent`, so a backend with no product frontend can set `mcpOAuth.consentUrl` to `${issuer}/console/oauth/consent`. Any signed-in user can approve or deny a request with their Better Auth session; signed-out users sign in and return through the login screen's new `returnTo` parameter, which accepts only in-app paths.
34
+ - Claude Code plugins now publish to npm with every release, as `@terreno/agent-plugin` and `@terreno/agent-plugin-loop`. A new `terreno-releases` marketplace installs them at the latest stable release. To pin one release, add `TerrenoLabs/terreno#<version>` instead. See [Install agent skills](docs/how-to/install-agent-skills.md#pick-a-channel).
35
+ - The generated Claude plugin manifest no longer pins `version`. Before this change, plugins installed from the `TerrenoLabs/terreno` marketplace stayed on the `2.12.0` copy, because their version never changed. They now update on every commit. Run `/plugin update terreno@terreno-plugins` once to leave `2.12.0`.
36
+ - The `terreno-planning` lifecycle plugin (2.13.0) plans e2e tests and manual regression cases in every IP:
37
+
38
+ - Grow requires `## E2E tests` and `## Regression cases` sections (either may be `None — <reason>`), read from the e2e guide at `docs/testing/e2e.md` or a `## Testing conventions` override in AGENTS.md or CLAUDE.md.
39
+ - Regression cases stay minimal: most features add none. A case is only for what a developer or QA must check by hand, such as push notifications, and Roast fails one that repeats an existing case.
40
+ - Pick writes each planned spec in the task that delivers it, and Roast runs it; a missing or failing row is `FAIL`.
41
+ - Brew gates on every planned spec and merges regression cases into `docs/regression/<area>.md` with the next free ID.
42
+ - Taste re-checks both sections after behaviour-changing fixes and renumbers regression ID collisions with `master`.
43
+ - `bun run check:lifecycle-skills` enforces these phrases in every stage.
44
+ - New `backfill-regression` skill proposes manual regression cases one area at a time and writes only the sets the human approves.
45
+ - A consumer opts out of manual regression suites with `Regression suites | none` in its `## Testing conventions` table.
46
+ - The `terreno-planning` lifecycle plugin (2.14.0) grows an end-user guide with every feature:
47
+
48
+ - Every IP has a `## User guide` section listing the guide pages it adds, updates, or retires, or `None — <reason>`. Pages live in `docs/user-guide/<area>/<feature>.md`, one feature per page, with `title`, `audience`, `platforms`, and `questions` frontmatter that chat agents use to answer "how do I…" questions.
49
+ - Pick writes each page in the task that ships the feature, Roast checks it against the shipped screens, Brew gates on it and the index, and Taste keeps it current after fixes.
50
+ - A repo opts out with `| User guide | none — <reason> |` in a `## User guide conventions` table in AGENTS.md or CLAUDE.md.
51
+ - New `backfill-user-guide` skill drafts pages for features that shipped without one, one area at a time, and writes only the pages the human approves.
52
+ - `bun run check:user-guide` validates the pages and index (CI `repo-policies`); `--uncovered <app dir>` lists screens no page names.
53
+ - Terreno's own guide covers the example app's main features.
54
+ - `@terreno/api`: opt-in `mcpRequestLog` on `TerrenoApp` records one metadata-only `McpRequestLog` row per MCP tool call (tool, user, credential, service token or OAuth client, duration, outcome, error class; never arguments, results, or error text), expiring after `retentionDays` (default 30).
55
+ - `@terreno/api`: opt-in `mcpToolSettings` on `TerrenoApp` registers every MCP tool in `McpToolSetting` at startup; tools an admin switches off are hidden from `tools/list` and rejected by `tools/call`.
56
+ - `@terreno/api`: deleting an `McpOAuthToken` row revokes its whole connection (token family) and keeps the rows. New `McpOAuthToken.revokeAllForUser` and `McpServiceToken.revokeAllForUser`.
57
+ - `@terreno/admin-backend`: `mcpAdminModels()` adds MCP admin screens (service tokens, OAuth clients, OAuth connections, request log, tool switches), `mcpAdminStatements` for RBAC, and `mcpAdminScripts()` adds `mcp-sign-out-everywhere`. See [Manage MCP from the admin panel](docs/how-to/manage-mcp-in-admin.md).
58
+ - Docs: [Expose an MCP server safely](docs/how-to/expose-mcp-server-safely.md) (OAuth, service tokens, Cloudflare Tunnel, Access) and the `bootstrap-mcp-server` skill for a single-binary MCP server with the admin console embedded.
59
+ - Every workspace package has a real-process smoke test and an e2e page:
60
+
61
+ - Each package's `e2e/smoke.ts` (or `smoke.spec.ts` for browser packages) runs its built output through its public entry: a server on a real port, the compiled binary, or a page in Chromium. `bun run --cwd <package> smoke` runs one; `bun run smoke` runs them all and prints each package's time. The CircleCI `smoke` job runs them on every package change.
62
+ - `@terreno/test` adds the smoke helpers `smokeMongoUri()`, `spawnUntilReady()`, `spawnExampleBackend()`, `runBin()`, and `freePort()`.
63
+ - The [E2E testing guide](docs/testing/e2e.md) explains the test tiers and links one page per package under `docs/testing/e2e/`.
64
+ - Manual regression cases live in `docs/regression/<area>.md`, one file per area, and `bun run check:regression-suites` validates them.
65
+ - example-frontend's smoke serves a web export that `bun run --cwd example-frontend export:smoke` builds once with a placeholder API URL; the smoke swaps in a `freePort()` API URL as it serves the bundle, so the 30 s smoke no longer rebuilds the export. `spawnUntilReady()` takes `stopGraceMs`, and `spawnExampleBackend()` stops example-backend after 0.5 s instead of 5 s.
66
+ - Every workspace package has a `typecheck` script that runs `tsc` over source and test files through a new `tsconfig.typecheck.json`. Root `bun run lint` ends with `bun run typecheck`, and CircleCI runs it in a new `typecheck` job. Test files that had type errors were fixed.
67
+
68
+ ### Changed
69
+
70
+ - `@terreno/api`: deleting an `McpOAuthToken` row with document or query `deleteOne` now revokes that row's whole token family (the connection) and keeps the rows instead of removing one pair. `deleteMany` is unchanged.
71
+ - `@terreno/ui`: new `theme.field` tokens (`background`, `backgroundDisabled`, `border`, `borderFocus`, `borderError`, `borderDisabled`) style form inputs. `TextField`, `SelectField`, `DateTimeField`, `HeightField`, and `OtpField` read them instead of `surface.base` and `border.dark`. Defaults equal the old colors, but overriding `surface` or `border` no longer recolors those fields; use `setTheme({field: ...})`.
72
+
23
73
  ## [57.20.0] - 2026-10-30
24
74
 
25
75
  Upgrade note: [`mcp-server/src/docs/upgrades/57.20.0.md`](mcp-server/src/docs/upgrades/57.20.0.md).
package/biome.jsonc CHANGED
@@ -7,6 +7,7 @@
7
7
  "package.json",
8
8
  "src/**/*.ts",
9
9
  "src/**/*.tsx",
10
+ "e2e/**/*.ts",
10
11
  "!!**/node_modules",
11
12
  "!!node_modules",
12
13
  "!!**/dist",
@@ -403,6 +403,7 @@ const createAgentTasks = (context) => {
403
403
  }
404
404
  let returned;
405
405
  try {
406
+ // `args.data` was parsed by this tool's own `parameters`, which match its `execute` args.
406
407
  returned = await tool.execute(args.data, {
407
408
  conversationId,
408
409
  env: rt.env,
@@ -2,7 +2,7 @@ import type { ExperimentAggregates, ExperimentEstimate, ExperimentVersionResult,
2
2
  import type { ModelPrice, ObservabilityAiServiceFactory, ObservabilityGenerateClient } from "../types";
3
3
  import type { LocalDatasetStore } from "./datasetStore";
4
4
  import type { LocalEvaluatorStore } from "./evaluatorStore";
5
- import type { LocalPromptStore } from "./promptStore";
5
+ import type { LocalPromptStore, MoveLabelResult } from "./promptStore";
6
6
  export interface ExperimentCreateInput {
7
7
  datasetId: string;
8
8
  evaluatorIds: string[];
@@ -87,10 +87,7 @@ export declare class LocalExperimentRunner {
87
87
  }): Promise<ExperimentListResult>;
88
88
  get(id: string): Promise<ExperimentView>;
89
89
  private toSummaryView;
90
- promote(id: string, version: number): Promise<{
91
- label: string;
92
- outgoingVersion?: number;
93
- }>;
90
+ promote(id: string, version: number): Promise<MoveLabelResult>;
94
91
  executeExperiment(id: string): Promise<void>;
95
92
  private computeGates;
96
93
  }
@@ -536,9 +536,12 @@ export interface HarnessToolDefinition<Args = unknown, Result = unknown> extends
536
536
  }
537
537
  /**
538
538
  * Any tool, whatever its argument and result types. `execute` arguments are contravariant, so
539
- * `never` accepts every tool in heterogeneous lists without an explicit `any`.
539
+ * `never` accepts every tool in heterogeneous lists without an explicit `any`. A Zod schema's
540
+ * output is covariant, so `parameters` widens to `unknown` instead.
540
541
  */
541
- export type AnyHarnessToolDefinition = HarnessToolDefinition<never, unknown>;
542
+ export interface AnyHarnessToolDefinition extends Omit<HarnessToolDefinition<never, unknown>, "parameters"> {
543
+ parameters: z.ZodType<unknown>;
544
+ }
542
545
  export interface HarnessAgentDefinitionInput {
543
546
  /** Tried in order once the primary model's retryable failures exhaust `modelRetry`. */
544
547
  fallbackModels?: HarnessModelRef[];
package/e2e/smoke.ts ADDED
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Real-process smoke for @terreno/ai. Run `bun e2e/smoke.ts` after `bun run compile`.
3
+ *
4
+ * The smoke re-runs this file with `serve` as a child process: a minimal TerrenoApp from the
5
+ * compiled `@terreno/api` with the compiled `AiApp` registered, on a free port, against
6
+ * `smokeMongoUri()`. The child's `AIService` wraps a deterministic fake provider
7
+ * (`MockLanguageModelV3` from `ai/test`) and runs with every provider key unset. The parent then
8
+ * checks over real HTTP that a signed-in user's chat request streams back the fake provider's
9
+ * text. Exits non-zero on the first failed check.
10
+ */
11
+ import {
12
+ freePort,
13
+ type SpawnedProcess,
14
+ smokeMongoUri,
15
+ spawnUntilReady,
16
+ stopSmokeMongo,
17
+ } from "@terreno/test";
18
+ import type express from "express";
19
+ import {DateTime} from "luxon";
20
+ import type {Schema} from "mongoose";
21
+
22
+ const SERVE_ARG = "serve";
23
+ const FAKE_REPLY = "Hello from the ai smoke's fake provider.";
24
+ const FAKE_TITLE = "Smoke chat";
25
+
26
+ /** Provider credentials the smoke unsets in the child, so no real provider can answer. */
27
+ const PROVIDER_ENV_KEYS = [
28
+ "ANTHROPIC_API_KEY",
29
+ "GEMINI_API_KEY",
30
+ "GOOGLE_APPLICATION_CREDENTIALS",
31
+ "GOOGLE_GENERATIVE_AI_API_KEY",
32
+ "GOOGLE_VERTEX_PROJECT",
33
+ "LANGFUSE_PUBLIC_KEY",
34
+ "LANGFUSE_SECRET_KEY",
35
+ "OPENAI_API_KEY",
36
+ ];
37
+
38
+ interface ChatEvent {
39
+ done?: boolean;
40
+ error?: string;
41
+ historyId?: string;
42
+ text?: string;
43
+ title?: string;
44
+ }
45
+
46
+ const ZERO_USAGE = {
47
+ inputTokens: {cacheRead: 0, cacheWrite: 0, noCache: 0, total: 0},
48
+ outputTokens: {reasoning: 0, text: 0, total: 0},
49
+ };
50
+
51
+ /**
52
+ * Child: the compiled API with auth, the compiled AiApp on a fake model, and a health plugin.
53
+ * The packages are imported here, not at the top, so the parent process never loads them.
54
+ */
55
+ const serve = async (): Promise<void> => {
56
+ const {createdUpdatedPlugin, logger, TerrenoApp} = await import("@terreno/api");
57
+ const {AIService, AiApp} = await import("@terreno/ai");
58
+ const {MockLanguageModelV3} = await import("ai/test");
59
+ const {default: mongoose} = await import("mongoose");
60
+ const {default: passportLocalMongoose} = await import("passport-local-mongoose");
61
+
62
+ const userSchema = new mongoose.Schema({
63
+ admin: {default: false, description: "Whether the user is an admin", type: Boolean},
64
+ email: {description: "Sign-in email", type: String},
65
+ name: {description: "Display name", type: String},
66
+ });
67
+ userSchema.plugin(passportLocalMongoose as unknown as (schema: Schema, options: object) => void, {
68
+ usernameCaseInsensitive: true,
69
+ usernameField: "email",
70
+ });
71
+ userSchema.plugin(createdUpdatedPlugin);
72
+ const User = mongoose.model("User", userSchema);
73
+
74
+ // Streams the chat reply and answers the title request, the two calls one chat turn makes.
75
+ const model = new MockLanguageModelV3({
76
+ doGenerate: async () => ({
77
+ content: [{text: FAKE_TITLE, type: "text"}],
78
+ finishReason: {raw: "stop", unified: "stop"},
79
+ usage: ZERO_USAGE,
80
+ warnings: [],
81
+ }),
82
+ doStream: async () => ({
83
+ stream: new ReadableStream({
84
+ start(controller): void {
85
+ controller.enqueue({type: "stream-start", warnings: []});
86
+ controller.enqueue({id: "text-1", type: "text-start"});
87
+ controller.enqueue({delta: FAKE_REPLY, id: "text-1", type: "text-delta"});
88
+ controller.enqueue({id: "text-1", type: "text-end"});
89
+ controller.enqueue({
90
+ finishReason: {raw: "stop", unified: "stop"},
91
+ type: "finish",
92
+ usage: ZERO_USAGE,
93
+ });
94
+ controller.close();
95
+ },
96
+ }),
97
+ }),
98
+ modelId: "ai-smoke-fake-model",
99
+ provider: "ai-smoke-fake-provider",
100
+ });
101
+
102
+ const healthPlugin = {
103
+ register: (app: express.Application): void => {
104
+ app.get("/health", (_req, res) => {
105
+ res.json({healthy: mongoose.connection.readyState === 1});
106
+ });
107
+ },
108
+ };
109
+
110
+ await mongoose.connect(process.env.MONGO_URI as string);
111
+ const app = new TerrenoApp({
112
+ loggingOptions: {logRequests: false},
113
+ userModel: User as unknown as ConstructorParameters<typeof TerrenoApp>[0]["userModel"],
114
+ })
115
+ .register(new AiApp({aiService: new AIService({model})}))
116
+ .register(healthPlugin);
117
+ app.start();
118
+ await app.whenReady();
119
+ logger.info(`[aiSmoke] serving on port ${process.env.PORT}`);
120
+ };
121
+
122
+ const expectStatus = async (res: Response, status: number, step: string): Promise<string> => {
123
+ const body = await res.text();
124
+ if (res.status !== status) {
125
+ throw new Error(`${step}: expected ${status}, got ${res.status}: ${body}`);
126
+ }
127
+ return body;
128
+ };
129
+
130
+ /** Splits a finished `text/event-stream` body into its JSON `data:` events. */
131
+ const parseEvents = (body: string): ChatEvent[] =>
132
+ body
133
+ .split("\n\n")
134
+ .map((chunk) => chunk.trim())
135
+ .filter((chunk) => chunk.startsWith("data: "))
136
+ .map((chunk) => JSON.parse(chunk.slice("data: ".length)) as ChatEvent);
137
+
138
+ /** Parent: boots the child and sends one chat request over HTTP. */
139
+ const run = async (): Promise<void> => {
140
+ const startedAt = DateTime.now();
141
+ const port = await freePort();
142
+ const baseUrl = `http://127.0.0.1:${port}`;
143
+ let server: SpawnedProcess | undefined;
144
+
145
+ try {
146
+ server = await spawnUntilReady({
147
+ cmd: [process.execPath, __filename, SERVE_ARG],
148
+ env: {
149
+ ...Object.fromEntries(PROVIDER_ENV_KEYS.map((key) => [key, undefined])),
150
+ MONGO_URI: await smokeMongoUri({databaseName: "aiSmoke"}),
151
+ PORT: String(port),
152
+ REFRESH_TOKEN_SECRET: "ai-smoke-refresh-secret",
153
+ SESSION_SECRET: "ai-smoke-session-secret",
154
+ TOKEN_ISSUER: "ai-smoke",
155
+ TOKEN_SECRET: "ai-smoke-token-secret",
156
+ },
157
+ timeoutMs: 30000,
158
+ url: `${baseUrl}/health`,
159
+ });
160
+
161
+ const signup = JSON.parse(
162
+ await expectStatus(
163
+ await fetch(`${baseUrl}/auth/signup`, {
164
+ body: JSON.stringify({
165
+ email: `ai-smoke-${startedAt.toMillis()}@example.com`,
166
+ password: "ai-smoke-password-1",
167
+ }),
168
+ headers: {"Content-Type": "application/json"},
169
+ method: "POST",
170
+ }),
171
+ 200,
172
+ "POST /auth/signup"
173
+ )
174
+ ) as {data: {token: string}};
175
+ console.info("✅ POST /auth/signup signs in a user");
176
+
177
+ const chat = await fetch(`${baseUrl}/gpt/prompt`, {
178
+ body: JSON.stringify({prompt: "Say hello."}),
179
+ headers: {
180
+ Authorization: `Bearer ${signup.data.token}`,
181
+ "Content-Type": "application/json",
182
+ },
183
+ method: "POST",
184
+ });
185
+ const events = parseEvents(await expectStatus(chat, 200, "POST /gpt/prompt"));
186
+ const failed = events.find((event) => event.error);
187
+ if (failed) {
188
+ throw new Error(`POST /gpt/prompt: the stream reported an error: ${failed.error}`);
189
+ }
190
+ const reply = events.map((event) => event.text ?? "").join("");
191
+ if (reply !== FAKE_REPLY) {
192
+ throw new Error(
193
+ `POST /gpt/prompt: expected the fake provider's text, got ${JSON.stringify(events)}`
194
+ );
195
+ }
196
+ const done = events.find((event) => event.done);
197
+ if (!done?.historyId || done.title !== FAKE_TITLE) {
198
+ throw new Error(
199
+ `POST /gpt/prompt: expected a done event with a history and the fake title, got ${JSON.stringify(done)}`
200
+ );
201
+ }
202
+ console.info("✅ POST /gpt/prompt streams the fake provider's reply");
203
+ } catch (error: unknown) {
204
+ if (server) {
205
+ console.error(`--- server output ---\n${server.output()}`);
206
+ }
207
+ throw error;
208
+ } finally {
209
+ await server?.stop();
210
+ await stopSmokeMongo();
211
+ }
212
+
213
+ console.info(`ai smoke passed in ${DateTime.now().diff(startedAt).toMillis()} ms`);
214
+ };
215
+
216
+ const main = process.argv[2] === SERVE_ARG ? serve : run;
217
+ main().then(
218
+ () => {
219
+ if (main === run) {
220
+ process.exit(0);
221
+ }
222
+ },
223
+ (error: unknown) => {
224
+ console.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
225
+ process.exit(1);
226
+ }
227
+ );
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "@langfuse/otel": "^5.0.1",
11
11
  "@opentelemetry/api": "^1.9.0",
12
12
  "@opentelemetry/sdk-node": "0.217.0",
13
- "@terreno/blocks": "57.20.0",
13
+ "@terreno/blocks": "57.21.0",
14
14
  "ai": "^6.0.97",
15
15
  "ajv": "8.20.0",
16
16
  "csv-parse": "^6.1.0",
@@ -22,10 +22,10 @@
22
22
  "description": "Provider-agnostic AI service with streaming chat, request logging, and admin tools — part of Terreno, the batteries-included TypeScript framework for universal apps.",
23
23
  "devDependencies": {
24
24
  "@biomejs/biome": "^2.3.6",
25
- "@terreno/admin-backend": "57.20.0",
26
- "@terreno/api": "57.20.0",
27
- "@terreno/jobs": "57.20.0",
28
- "@terreno/test": "57.20.0",
25
+ "@terreno/admin-backend": "57.21.0",
26
+ "@terreno/api": "57.21.0",
27
+ "@terreno/jobs": "57.21.0",
28
+ "@terreno/test": "57.21.0",
29
29
  "@types/bun": "^1.4.0",
30
30
  "@types/express": "^5.0.6",
31
31
  "@types/luxon": "^3.7.1",
@@ -136,8 +136,8 @@
136
136
  "name": "@terreno/ai",
137
137
  "peerDependencies": {
138
138
  "@ai-sdk/google-vertex": "^4.0.142",
139
- "@terreno/api": "57.20.0",
140
- "@terreno/jobs": "57.20.0",
139
+ "@terreno/api": "57.21.0",
140
+ "@terreno/jobs": "57.21.0",
141
141
  "mongoose": "^8.0.0 || ^9.0.0"
142
142
  },
143
143
  "peerDependenciesMeta": {
@@ -159,14 +159,16 @@
159
159
  "compile": "bun tsc",
160
160
  "compile:watch": "bun tsc -w",
161
161
  "dev": "bun tsc -w",
162
- "lint": "biome check ./src",
163
- "lint:fix": "biome check --write ./src",
164
- "lint:unsafefix": "biome check --fix --unsafe ./src",
162
+ "lint": "biome check ./src ./e2e",
163
+ "lint:fix": "biome check --write ./src ./e2e",
164
+ "lint:unsafefix": "biome check --fix --unsafe ./src ./e2e",
165
+ "smoke": "bun e2e/smoke.ts",
165
166
  "test": "bun test --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do bun test --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
166
167
  "test:agent": "AGENT=1 bun test --only-failures --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do AGENT=1 bun test --only-failures --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
167
168
  "test:ci": "bun test --preload ./src/tests/bunSetup.ts && for f in ./src/isolated/*.isolated.ts; do bun test --preload ./src/tests/bunSetup.ts \"$f\" || exit 1; done",
168
- "test:coverage": "bun run ../scripts/check-coverage.ts"
169
+ "test:coverage": "bun run ../scripts/check-coverage.ts",
170
+ "typecheck": "tsc -p tsconfig.typecheck.json"
169
171
  },
170
172
  "types": "dist/index.d.ts",
171
- "version": "57.20.0"
173
+ "version": "57.21.0"
172
174
  }
package/src/aiApp.test.ts CHANGED
@@ -10,7 +10,12 @@ import {GptHistory} from "./models/gptHistory";
10
10
  import type {FileStorageService} from "./service/fileStorage";
11
11
  import type {MCPService} from "./service/mcpService";
12
12
  import {createScriptedModel, modelCall, textStep, toolCallStep} from "./tests/chatHarness";
13
- import {authAsUserWithCredentials, ensureTestUsers, UserModel} from "./tests/helpers";
13
+ import {
14
+ authAsUserWithCredentials,
15
+ ensureTestUsers,
16
+ terrenoUserModel,
17
+ UserModel,
18
+ } from "./tests/helpers";
14
19
 
15
20
  const AI_APP_TEST_USER = {
16
21
  admin: false,
@@ -61,13 +66,13 @@ describe("AiApp", () => {
61
66
  };
62
67
 
63
68
  it("registers gpt and project routes by default", async () => {
64
- const {AIService} = await import("./service/aiService");
69
+ const {AIService} = await import("./service/aiService.js");
65
70
  const aiService = new AIService({model: createMockModel() as unknown as LanguageModel});
66
71
  const plugin = new AiApp({aiService});
67
72
  const app = new TerrenoApp({
68
73
  configureApp: (router) => plugin.register(router as unknown as express.Application),
69
74
  skipListen: true,
70
- userModel: UserModel,
75
+ userModel: terrenoUserModel,
71
76
  }).build();
72
77
 
73
78
  const agent = await authAsUser(app);
@@ -80,13 +85,13 @@ describe("AiApp", () => {
80
85
  });
81
86
 
82
87
  it("skips the project routes with projects: false", async () => {
83
- const {AIService} = await import("./service/aiService");
88
+ const {AIService} = await import("./service/aiService.js");
84
89
  const aiService = new AIService({model: createMockModel() as unknown as LanguageModel});
85
90
  const plugin = new AiApp({aiService, projects: false});
86
91
  const app = new TerrenoApp({
87
92
  configureApp: (router) => plugin.register(router as unknown as express.Application),
88
93
  skipListen: true,
89
- userModel: UserModel,
94
+ userModel: terrenoUserModel,
90
95
  }).build();
91
96
 
92
97
  const agent = await authAsUser(app);
@@ -95,14 +100,14 @@ describe("AiApp", () => {
95
100
  });
96
101
 
97
102
  it("passes asks to the gpt routes, so the model is offered the ask tools", async () => {
98
- const {AIService} = await import("./service/aiService");
103
+ const {AIService} = await import("./service/aiService.js");
99
104
  const model = createMockModel();
100
105
  const aiService = new AIService({model: model as unknown as LanguageModel});
101
106
  const plugin = new AiApp({aiService, asks: true});
102
107
  const app = new TerrenoApp({
103
108
  configureApp: (router) => plugin.register(router as unknown as express.Application),
104
109
  skipListen: true,
105
- userModel: UserModel,
110
+ userModel: terrenoUserModel,
106
111
  }).build();
107
112
  const agent = await authAsUser(app);
108
113
 
@@ -120,7 +125,7 @@ describe("AiApp", () => {
120
125
  });
121
126
 
122
127
  it("adds the documented headless turn actions to the history routes", async () => {
123
- const {AIService} = await import("./service/aiService");
128
+ const {AIService} = await import("./service/aiService.js");
124
129
  const model = createMockModel();
125
130
  const aiService = new AIService({model: model as unknown as LanguageModel});
126
131
  const app = new TerrenoApp({
@@ -129,7 +134,7 @@ describe("AiApp", () => {
129
134
  router as unknown as express.Application
130
135
  ),
131
136
  skipListen: true,
132
- userModel: UserModel,
137
+ userModel: terrenoUserModel,
133
138
  }).build();
134
139
  const agent = await authAsUser(app);
135
140
 
@@ -159,7 +164,7 @@ describe("AiApp", () => {
159
164
  });
160
165
 
161
166
  it("adds the headless actions only with asks on, so hosts without asks get no new endpoints", async () => {
162
- const {AIService} = await import("./service/aiService");
167
+ const {AIService} = await import("./service/aiService.js");
163
168
  const historyRoutes = async (asks?: boolean) => {
164
169
  const model = createMockModel();
165
170
  const aiService = new AIService({model: model as unknown as LanguageModel});
@@ -169,7 +174,7 @@ describe("AiApp", () => {
169
174
  router as unknown as express.Application
170
175
  ),
171
176
  skipListen: true,
172
- userModel: UserModel,
177
+ userModel: terrenoUserModel,
173
178
  }).build();
174
179
  const agent = await authAsUser(app);
175
180
  const openApi = await agent.get("/openapi.json");
@@ -226,7 +231,7 @@ describe("AiApp", () => {
226
231
  const app = new TerrenoApp({
227
232
  configureApp: (router) => plugin.register(router as unknown as express.Application),
228
233
  skipListen: true,
229
- userModel: UserModel,
234
+ userModel: terrenoUserModel,
230
235
  }).build();
231
236
  // Unauthenticated access to upload route: ensure it exists (returns 401, not 404).
232
237
  const upload = await supertest(app).post("/files/upload");
@@ -234,8 +239,8 @@ describe("AiApp", () => {
234
239
  });
235
240
 
236
241
  it("caps uploads at asks.maxFileSizeBytes and loads a files answer's uploads from storage", async () => {
237
- const {AIService} = await import("./service/aiService");
238
- const {FileAttachment} = await import("./models/fileAttachment");
242
+ const {AIService} = await import("./service/aiService.js");
243
+ const {FileAttachment} = await import("./models/fileAttachment.js");
239
244
  const text = Buffer.from("Coffee, 4.50\n");
240
245
  const upload = mock(async () => {
241
246
  const [user] = await UserModel.find({email: AI_APP_TEST_USER.email});
@@ -278,7 +283,7 @@ describe("AiApp", () => {
278
283
  const app = new TerrenoApp({
279
284
  configureApp: (router) => plugin.register(router as unknown as express.Application),
280
285
  skipListen: true,
281
- userModel: UserModel,
286
+ userModel: terrenoUserModel,
282
287
  }).build();
283
288
  const agent = await authAsUser(app);
284
289
 
@@ -329,7 +334,7 @@ describe("AiApp", () => {
329
334
  const app = new TerrenoApp({
330
335
  configureApp: (router) => plugin.register(router as unknown as express.Application),
331
336
  skipListen: true,
332
- userModel: UserModel,
337
+ userModel: terrenoUserModel,
333
338
  }).build();
334
339
  const upload = await supertest(app).post("/files/upload");
335
340
  expect(upload.status).toBe(404);
@@ -345,7 +350,7 @@ describe("AiApp", () => {
345
350
  const app = new TerrenoApp({
346
351
  configureApp: (router) => plugin.register(router as unknown as express.Application),
347
352
  skipListen: true,
348
- userModel: UserModel,
353
+ userModel: terrenoUserModel,
349
354
  }).build();
350
355
  const agent = await authAsUser(app);
351
356
  const servers = await agent.get("/mcp/servers");
@@ -180,7 +180,7 @@ describe("Agent turns", () => {
180
180
  beforeAll(async () => {
181
181
  // The shared preload's beforeEach imports this lazily under a 5 s hook timeout; a
182
182
  // cold first import can exceed it, so load it here first.
183
- await import("../langfuseClient");
183
+ await import("../langfuseClient.js");
184
184
  createLocalObservabilityPlugin();
185
185
  SpanModel = registerObsSpan();
186
186
  TraceModel = registerObsTrace();
@@ -653,7 +653,8 @@ export const createAgentTasks = (context: AgentLoopContext): AgentTasks => {
653
653
  }
654
654
  let returned: unknown;
655
655
  try {
656
- returned = await tool.execute(args.data, {
656
+ // `args.data` was parsed by this tool's own `parameters`, which match its `execute` args.
657
+ returned = await tool.execute(args.data as never, {
657
658
  conversationId,
658
659
  env: rt.env,
659
660
  output: (text: string) => {
@@ -44,38 +44,41 @@ type Reply = {
44
44
  };
45
45
 
46
46
  /** LanguageModelV2 mock answering each request with the next reply; `hang` blocks it first. */
47
- const scriptedModel = (replies: Reply[]) =>
48
- withGenerateStreaming({
49
- doGenerate: mock(async () => {
50
- const reply = replies.shift();
51
- if (!reply) {
52
- throw new Error("script exhausted");
53
- }
54
- reply.started?.release();
55
- if (reply.hang) {
56
- await reply.hang.promise;
57
- }
58
- return {
59
- content: reply.toolCall
60
- ? [
61
- {
62
- input: "{}",
63
- toolCallId: reply.toolCall.id,
64
- toolName: reply.toolCall.name,
65
- type: "tool-call" as const,
66
- },
67
- ]
68
- : [{text: reply.text ?? "", type: "text" as const}],
69
- finishReason: reply.toolCall ? ("tool-calls" as const) : ("stop" as const),
70
- usage: {inputTokens: 1, outputTokens: 1, totalTokens: 2},
71
- warnings: [],
72
- };
73
- }),
47
+ const scriptedModel = (replies: Reply[]) => {
48
+ // Declared before the call so `withGenerateStreaming` infers the mock's own type.
49
+ const doGenerate = mock(async () => {
50
+ const reply = replies.shift();
51
+ if (!reply) {
52
+ throw new Error("script exhausted");
53
+ }
54
+ reply.started?.release();
55
+ if (reply.hang) {
56
+ await reply.hang.promise;
57
+ }
58
+ return {
59
+ content: reply.toolCall
60
+ ? [
61
+ {
62
+ input: "{}",
63
+ toolCallId: reply.toolCall.id,
64
+ toolName: reply.toolCall.name,
65
+ type: "tool-call" as const,
66
+ },
67
+ ]
68
+ : [{text: reply.text ?? "", type: "text" as const}],
69
+ finishReason: reply.toolCall ? ("tool-calls" as const) : ("stop" as const),
70
+ usage: {inputTokens: 1, outputTokens: 1, totalTokens: 2},
71
+ warnings: [],
72
+ };
73
+ });
74
+ return withGenerateStreaming({
75
+ doGenerate,
74
76
  modelId: "mock-model",
75
77
  provider: "mock",
76
78
  specificationVersion: "v2" as const,
77
79
  supportedUrls: {},
78
80
  });
81
+ };
79
82
 
80
83
  interface ProcessHandle {
81
84
  die: () => void;
@@ -169,7 +172,7 @@ describe("Agent turns across a crash", () => {
169
172
  beforeAll(async () => {
170
173
  // The shared preload's beforeEach imports this lazily under a 5 s hook timeout; a
171
174
  // cold first import can exceed it, so load it here first.
172
- await import("../langfuseClient");
175
+ await import("../langfuseClient.js");
173
176
  createLocalObservabilityPlugin();
174
177
  SpanModel = registerObsSpan();
175
178
  TraceModel = registerObsTrace();