|
| 1 | +// Cross-target: muscle memory — runtime-observed output shapes. Most OpenAPI |
| 2 | +// operations declare no response schema, so `tools.describe.tool()` used to |
| 3 | +// render `data: unknown` forever and the model had to guess response shapes. |
| 4 | +// This journey proves the warm path end to end through public surfaces only: |
| 5 | +// a schemaless tool describes as `unknown`, one real invocation against a live |
| 6 | +// upstream teaches the shape, and the very next describe serves a real |
| 7 | +// TypeScript type marked as observed. |
| 8 | +import { randomBytes } from "node:crypto"; |
| 9 | +import { createServer } from "node:http"; |
| 10 | + |
| 11 | +import { expect } from "@effect/vitest"; |
| 12 | +import { Effect } from "effect"; |
| 13 | +import { composePluginApi } from "@executor-js/api/server"; |
| 14 | +import { openApiHttpPlugin } from "@executor-js/plugin-openapi/api"; |
| 15 | +import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
| 16 | + |
| 17 | +import { scenario } from "../src/scenario"; |
| 18 | +import { Api, Target } from "../src/services"; |
| 19 | + |
| 20 | +const api = composePluginApi([openApiHttpPlugin()] as const); |
| 21 | + |
| 22 | +/** One GET operation whose 200 declares no response schema — the shape the |
| 23 | + * model would otherwise have to guess. */ |
| 24 | +const issuesSpec = JSON.stringify({ |
| 25 | + openapi: "3.0.3", |
| 26 | + info: { title: "Issues API", version: "1.0.0" }, |
| 27 | + paths: { |
| 28 | + "/issues": { |
| 29 | + get: { |
| 30 | + operationId: "listIssues", |
| 31 | + summary: "List issues", |
| 32 | + responses: { "200": { description: "issues" } }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +/** A live upstream for the single invocation that teaches the shape. */ |
| 39 | +const serveIssuesFixture = Effect.acquireRelease( |
| 40 | + Effect.callback<{ readonly url: string; readonly close: () => void }>((resume) => { |
| 41 | + const server = createServer((_request, response) => { |
| 42 | + response.writeHead(200, { "content-type": "application/json" }); |
| 43 | + response.end( |
| 44 | + JSON.stringify({ |
| 45 | + issues: [ |
| 46 | + { id: 1, title: "first", open: true }, |
| 47 | + { id: 2, title: "second", open: false }, |
| 48 | + ], |
| 49 | + total: 2, |
| 50 | + }), |
| 51 | + ); |
| 52 | + }); |
| 53 | + server.listen(0, "127.0.0.1", () => { |
| 54 | + const addressInfo = server.address(); |
| 55 | + const port = typeof addressInfo === "object" && addressInfo !== null ? addressInfo.port : 0; |
| 56 | + resume( |
| 57 | + Effect.succeed({ |
| 58 | + url: `http://127.0.0.1:${port}`, |
| 59 | + close: () => { |
| 60 | + server.close(); |
| 61 | + server.closeAllConnections(); |
| 62 | + }, |
| 63 | + }), |
| 64 | + ); |
| 65 | + }); |
| 66 | + }), |
| 67 | + (fixture) => Effect.sync(fixture.close), |
| 68 | +); |
| 69 | + |
| 70 | +const describeCode = (slug: string) => ` |
| 71 | +const details = await tools.describe.tool({ path: "${slug}.org.main.issues.listIssues" }); |
| 72 | +return { |
| 73 | + outputTypeScript: details.outputTypeScript ?? null, |
| 74 | + note: details.outputTypeScriptNote ?? null, |
| 75 | + error: details.error ?? null, |
| 76 | +}; |
| 77 | +`; |
| 78 | + |
| 79 | +type DescribeOutcome = { |
| 80 | + readonly outputTypeScript: string | null; |
| 81 | + readonly note: string | null; |
| 82 | + readonly error: unknown; |
| 83 | +}; |
| 84 | + |
| 85 | +scenario( |
| 86 | + "Muscle memory · a schemaless tool's observed output shape reaches describe", |
| 87 | + {}, |
| 88 | + Effect.scoped( |
| 89 | + Effect.gen(function* () { |
| 90 | + const target = yield* Target; |
| 91 | + const { client: makeApiClient } = yield* Api; |
| 92 | + const identity = yield* target.newIdentity(); |
| 93 | + const client = yield* makeApiClient(api, identity); |
| 94 | + const slug = IntegrationSlug.make(`shape_memory_${randomBytes(4).toString("hex")}`); |
| 95 | + const upstream = yield* serveIssuesFixture; |
| 96 | + |
| 97 | + yield* Effect.ensuring( |
| 98 | + Effect.gen(function* () { |
| 99 | + yield* client.openapi.addSpec({ |
| 100 | + payload: { |
| 101 | + spec: { kind: "blob", value: issuesSpec }, |
| 102 | + slug, |
| 103 | + baseUrl: upstream.url, |
| 104 | + authenticationTemplate: [ |
| 105 | + { |
| 106 | + slug: "apiKey", |
| 107 | + type: "apiKey", |
| 108 | + headers: { authorization: ["Bearer ", { type: "variable", name: "token" }] }, |
| 109 | + }, |
| 110 | + ], |
| 111 | + }, |
| 112 | + }); |
| 113 | + yield* client.connections.create({ |
| 114 | + payload: { |
| 115 | + owner: "org", |
| 116 | + name: ConnectionName.make("main"), |
| 117 | + integration: slug, |
| 118 | + template: AuthTemplateSlug.make("apiKey"), |
| 119 | + value: `key_${randomBytes(8).toString("hex")}`, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + const describe = Effect.gen(function* () { |
| 124 | + const executed = yield* client.executions.execute({ |
| 125 | + payload: { code: describeCode(String(slug)), autoApprove: true }, |
| 126 | + }); |
| 127 | + expect(executed.status, executed.text).toBe("completed"); |
| 128 | + return JSON.parse(executed.text) as DescribeOutcome; |
| 129 | + }); |
| 130 | + |
| 131 | + // Cold: no declared response schema — the model sees unknown. |
| 132 | + const cold = yield* describe; |
| 133 | + expect(cold.error, "the tool resolves").toBeNull(); |
| 134 | + expect(cold.outputTypeScript, "cold describe has no shape").toContain("data: unknown;"); |
| 135 | + expect(cold.note, "cold describe carries no provenance note").toBeNull(); |
| 136 | + |
| 137 | + // One real call against the live upstream teaches the shape. |
| 138 | + const invoked = yield* client.executions.execute({ |
| 139 | + payload: { |
| 140 | + code: ` |
| 141 | +const result = await tools.${slug}.org.main.issues.listIssues({}); |
| 142 | +return { ok: result.ok }; |
| 143 | +`, |
| 144 | + autoApprove: true, |
| 145 | + }, |
| 146 | + }); |
| 147 | + expect(invoked.status, invoked.text).toBe("completed"); |
| 148 | + expect(JSON.parse(invoked.text), "the teaching call succeeded").toEqual({ ok: true }); |
| 149 | + |
| 150 | + // Warm: the observed shape is served, marked as observed. |
| 151 | + const warm = yield* describe; |
| 152 | + expect(warm.outputTypeScript, "warm describe serves the observed shape").toContain( |
| 153 | + "issues", |
| 154 | + ); |
| 155 | + expect(warm.outputTypeScript, "field types come from the live payload").toContain( |
| 156 | + "total", |
| 157 | + ); |
| 158 | + expect(warm.outputTypeScript, "the shape no longer collapses").not.toContain( |
| 159 | + "data: unknown;", |
| 160 | + ); |
| 161 | + expect(warm.note, "provenance is explicit").toContain("observed from 1 live response"); |
| 162 | + }), |
| 163 | + Effect.gen(function* () { |
| 164 | + yield* client.connections |
| 165 | + .remove({ |
| 166 | + params: { |
| 167 | + owner: "org", |
| 168 | + integration: slug, |
| 169 | + name: ConnectionName.make("main"), |
| 170 | + }, |
| 171 | + }) |
| 172 | + .pipe(Effect.ignore); |
| 173 | + yield* client.openapi.removeSpec({ params: { slug } }).pipe(Effect.ignore); |
| 174 | + }), |
| 175 | + ); |
| 176 | + }), |
| 177 | + ), |
| 178 | +); |
0 commit comments