@apifuse/provider-sdk 2.1.0-beta.20 → 2.1.0-beta.21

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # @apifuse/provider-sdk Changelog
2
2
 
3
+ ## 2.1.0-beta.21
4
+
5
+ - Release candidate for main commit 8124b3eb150cc7a73a86a95266b3747763a494ae.
6
+
3
7
  ## 2.1.0-beta.20
4
8
 
5
9
  - Release candidate for main commit 2a07cc5aef0d517c3b01d20445105f1669446bd3.
@@ -7,6 +7,7 @@ import { createServer } from "node:net";
7
7
  import { basename, dirname, join, relative, resolve } from "node:path";
8
8
  import { pathToFileURL } from "node:url";
9
9
 
10
+ import * as acorn from "acorn";
10
11
  import { z } from "zod";
11
12
 
12
13
  import packageJson from "../package.json";
@@ -1544,6 +1545,9 @@ function scoreRepositoryDx(providerRoot: string): SubmitCheck {
1544
1545
  if (!existsSync(resolve(providerRoot, ".gitignore"))) {
1545
1546
  missing.push(".gitignore");
1546
1547
  }
1548
+ if (!existsSync(resolve(providerRoot, "AGENTS.md"))) {
1549
+ missing.push("AGENTS.md");
1550
+ }
1547
1551
 
1548
1552
  const packageJsonPath = resolve(providerRoot, "package.json");
1549
1553
  const packageScripts = readPackageScripts(packageJsonPath);
@@ -1572,7 +1576,7 @@ function scoreRepositoryDx(providerRoot: string): SubmitCheck {
1572
1576
  maxPoints: 0,
1573
1577
  message: `Generated repository DX guardrails are missing: ${missing.join(", ")}.`,
1574
1578
  remediation:
1575
- "Regenerate with the current `apifuse create` template or add .gitignore plus `type-check: tsc --noEmit` and include it from `check`.",
1579
+ "Regenerate with the current `apifuse create` template or restore the missing files: .gitignore, AGENTS.md (agent contribution guide), plus `type-check: tsc --noEmit` included from `check`.",
1576
1580
  evidence: missing,
1577
1581
  };
1578
1582
  }
@@ -1972,6 +1976,7 @@ function scoreFixtureCoverage(provider: ProviderDefinition): SubmitCheck {
1972
1976
  function scoreHealthCoverage(provider: ProviderDefinition): SubmitCheck {
1973
1977
  const operations = Object.entries(provider.operations);
1974
1978
  const missing: string[] = [];
1979
+ const vacuous: string[] = [];
1975
1980
  const placeholder: string[] = [];
1976
1981
  const unsupported: string[] = [];
1977
1982
  const generatedStarter: string[] = [];
@@ -1983,6 +1988,9 @@ function scoreHealthCoverage(provider: ProviderDefinition): SubmitCheck {
1983
1988
  missing.push(operationId);
1984
1989
  continue;
1985
1990
  }
1991
+ if (hasCheck && !hasUnsupported && hasOnlyVacuousHealthCases(operation.healthCheck)) {
1992
+ vacuous.push(operationId);
1993
+ }
1986
1994
  if (hasUnsupported) {
1987
1995
  const reason = operation.healthCheckUnsupported?.reason ?? "";
1988
1996
  unsupported.push(operationId);
@@ -2008,6 +2016,17 @@ function scoreHealthCoverage(provider: ProviderDefinition): SubmitCheck {
2008
2016
  );
2009
2017
  }
2010
2018
 
2019
+ if (vacuous.length > 0) {
2020
+ return blocker(
2021
+ "health-coverage",
2022
+ "health",
2023
+ "One or more operations have healthCheck cases with empty assertions.",
2024
+ `healthCheck.assertions for ${vacuous.join(", ")} is empty — assert on status and response shape (e.g. throw or return {status:'degraded'} when the upstream contract breaks), or declare healthCheckUnsupported with a specific reason if the operation genuinely cannot be probed.`,
2025
+ CATEGORY_MAX_POINTS.health,
2026
+ vacuous.map((operationId) => `${operationId}: empty healthCheck.assertions`),
2027
+ );
2028
+ }
2029
+
2011
2030
  if (placeholder.length > 0) {
2012
2031
  return {
2013
2032
  id: "health-coverage",
@@ -2059,6 +2078,381 @@ function scoreHealthCoverage(provider: ProviderDefinition): SubmitCheck {
2059
2078
  );
2060
2079
  }
2061
2080
 
2081
+ function hasOnlyVacuousHealthCases(
2082
+ healthCheck: ProviderDefinition["operations"][string]["healthCheck"],
2083
+ ): boolean {
2084
+ const cases = healthCheck?.cases;
2085
+ if (!Array.isArray(cases) || cases.length === 0) {
2086
+ return true;
2087
+ }
2088
+ return cases.every((healthCase) => isVacuousAssertionFunction(healthCase?.assertions));
2089
+ }
2090
+
2091
+ function isVacuousAssertionFunction(assertions: unknown): boolean {
2092
+ if (typeof assertions !== "function") {
2093
+ return true;
2094
+ }
2095
+
2096
+ let source: string;
2097
+ try {
2098
+ source = Function.prototype.toString.call(assertions);
2099
+ } catch {
2100
+ return false;
2101
+ }
2102
+
2103
+ // Native / bound functions stringify to `function () { [native code] }` with
2104
+ // no inspectable body or params. The underlying implementation may inspect
2105
+ // ctx, so fail open (do not flag) rather than mistake it for an empty body.
2106
+ if (/\[native code\]/.test(source)) {
2107
+ return false;
2108
+ }
2109
+
2110
+ const fn = parseAssertionFunction(source);
2111
+ if (!fn) {
2112
+ // Unparseable source → fail open (treat as a real assertion). A false
2113
+ // negative here only misses a no-op; a false positive would wrongly
2114
+ // reject a valid contributor.
2115
+ return false;
2116
+ }
2117
+
2118
+ // A real health assertion MUST either throw when the upstream contract
2119
+ // breaks, or inspect the probe response, which is delivered exclusively
2120
+ // through the assertion's own parameter(s). Working on the parsed AST (not
2121
+ // text) makes this precise at the syntactic layer: a `throw` only counts
2122
+ // when it is a real ThrowStatement in THIS function's body (not inside a
2123
+ // nested, uninvoked function), and a parameter reference is checked against
2124
+ // the actual bound names (destructuring binds the local alias, not the
2125
+ // property key). This closes the whole equivalent-no-op class — empty
2126
+ // bodies, `void 0`, `({})`, `Promise.resolve()`, `await Promise.resolve()`,
2127
+ // `.then()`, `new Promise(r => r())`, side-effect-only bodies, throws hidden
2128
+ // in uninvoked closures — without enumerating spellings.
2129
+ if (functionThrows(fn)) {
2130
+ return false;
2131
+ }
2132
+ const bound = new Set<string>();
2133
+ for (const param of fn.params) {
2134
+ collectBoundNames(param, bound);
2135
+ }
2136
+ if (bound.size === 0) {
2137
+ return true;
2138
+ }
2139
+ // A parameter reference anywhere in the (reachable) body is treated as
2140
+ // inspecting the response. This is deliberately syntactic, not a dataflow
2141
+ // analysis.
2142
+ //
2143
+ // KNOWN LIMITATION (accepted): a body that reads the parameter but never
2144
+ // turns that read into an outcome — no throw, no returned verdict — still
2145
+ // passes, e.g. `({ status }) => { console.info(status); }`. Precisely
2146
+ // rejecting it would require tracking whether the read flows to a throw
2147
+ // argument or return value through arbitrary local bindings and invoked
2148
+ // helpers (`const ok = ctx.output.ok; return ok ? ...` / a called helper that
2149
+ // throws). That is transitive use-def dataflow, and an imprecise version
2150
+ // FALSE-BLOCKS real assertions of exactly those shapes — verified
2151
+ // empirically. Under the fail-open contract (rejecting a real contributor is
2152
+ // strictly worse than missing a no-op) we accept the miss here. This gate
2153
+ // stops accidental/lazy no-ops (empty bodies, `void 0`, `Promise.resolve()`,
2154
+ // throws in uninvoked closures); a determined bypass via a decorative ctx
2155
+ // read is no easier than writing the real one-line `throw`, and the actual
2156
+ // defense against a runtime-empty assertion is the live `--smoke` probe.
2157
+ return !referencesBoundNames(fn, bound);
2158
+ }
2159
+
2160
+ type AssertionFunctionNode =
2161
+ | acorn.ArrowFunctionExpression
2162
+ | acorn.FunctionExpression
2163
+ | acorn.FunctionDeclaration;
2164
+
2165
+ function isFunctionNode(node: acorn.AnyNode): node is AssertionFunctionNode {
2166
+ return (
2167
+ node.type === "ArrowFunctionExpression" ||
2168
+ node.type === "FunctionExpression" ||
2169
+ node.type === "FunctionDeclaration"
2170
+ );
2171
+ }
2172
+
2173
+ /**
2174
+ * Parse the `Function.prototype.toString()` output of an assertion into its AST
2175
+ * function node. The stringified form can be an arrow (`(a) => {}`), a function
2176
+ * expression (`function (a) {}`), or a bare method (`foo() {}`), so try a few
2177
+ * wrappers until one parses. Returns undefined on any parse failure so callers
2178
+ * fail open.
2179
+ */
2180
+ function parseAssertionFunction(source: string): AssertionFunctionNode | undefined {
2181
+ const candidates = [source, `(${source})`, `({${source}})`];
2182
+ for (const candidate of candidates) {
2183
+ let program: acorn.Program;
2184
+ try {
2185
+ program = acorn.parse(candidate, { ecmaVersion: "latest" });
2186
+ } catch {
2187
+ continue;
2188
+ }
2189
+ const fn = findFirstFunction(program);
2190
+ if (fn) {
2191
+ return fn;
2192
+ }
2193
+ }
2194
+ return undefined;
2195
+ }
2196
+
2197
+ /** Depth-first search for the first function node in a parsed program. */
2198
+ function findFirstFunction(root: acorn.AnyNode): AssertionFunctionNode | undefined {
2199
+ let found: AssertionFunctionNode | undefined;
2200
+ walkAst(root, (node) => {
2201
+ if (found) {
2202
+ return false;
2203
+ }
2204
+ if (isFunctionNode(node)) {
2205
+ found = node;
2206
+ return false;
2207
+ }
2208
+ return true;
2209
+ });
2210
+ return found;
2211
+ }
2212
+
2213
+ /**
2214
+ * Collect the identifier names actually BOUND by a parameter pattern. For
2215
+ * destructuring, the binding is the local target (`value`), not the source
2216
+ * property key — so `({ status: ignored })` binds `ignored`, and a body that
2217
+ * merely mentions `status` is not referencing a parameter.
2218
+ */
2219
+ function collectBoundNames(pattern: acorn.Pattern | null, out: Set<string>): void {
2220
+ if (!pattern) {
2221
+ return;
2222
+ }
2223
+ switch (pattern.type) {
2224
+ case "Identifier":
2225
+ out.add(pattern.name);
2226
+ break;
2227
+ case "AssignmentPattern":
2228
+ collectBoundNames(pattern.left, out);
2229
+ break;
2230
+ case "RestElement":
2231
+ collectBoundNames(pattern.argument, out);
2232
+ break;
2233
+ case "ArrayPattern":
2234
+ for (const element of pattern.elements) {
2235
+ collectBoundNames(element, out);
2236
+ }
2237
+ break;
2238
+ case "ObjectPattern":
2239
+ for (const property of pattern.properties) {
2240
+ if (property.type === "RestElement") {
2241
+ collectBoundNames(property.argument, out);
2242
+ } else {
2243
+ // `.value` is the local binding target, `.key` is the source
2244
+ // property name — bind only the former.
2245
+ collectBoundNames(property.value, out);
2246
+ }
2247
+ }
2248
+ break;
2249
+ }
2250
+ }
2251
+
2252
+ /**
2253
+ * True if the function contains a real `throw` statement in ITS OWN body —
2254
+ * descending through control flow but NOT into nested functions, whose throws
2255
+ * do not execute unless that nested function is invoked.
2256
+ */
2257
+ function functionThrows(fn: AssertionFunctionNode): boolean {
2258
+ if (fn.body.type !== "BlockStatement") {
2259
+ // Concise arrow returning an expression cannot contain a throw statement.
2260
+ return false;
2261
+ }
2262
+ let throws = false;
2263
+ walkAst(fn.body, (node) => {
2264
+ if (throws) {
2265
+ return false;
2266
+ }
2267
+ if (node.type === "ThrowStatement") {
2268
+ throws = true;
2269
+ return false;
2270
+ }
2271
+ // Do not descend into nested function bodies.
2272
+ if (isFunctionNode(node)) {
2273
+ return false;
2274
+ }
2275
+ return true;
2276
+ });
2277
+ return throws;
2278
+ }
2279
+
2280
+ /**
2281
+ * True if the function's body references any of the given bound parameter names
2282
+ * as an actual value. Property KEYS (`{ status: ... }`, `obj.status`) are not
2283
+ * references; computed members (`obj[status]`) are. Nested functions that
2284
+ * re-bind the same name shadow it, so their bodies are searched with the
2285
+ * shadowed name removed from the target set.
2286
+ */
2287
+ function referencesBoundNames(fn: AssertionFunctionNode, bound: Set<string>): boolean {
2288
+ if (bound.size === 0) {
2289
+ return false;
2290
+ }
2291
+ let referenced = false;
2292
+ walkAstValues(fn.body, bound, fn.body, null, null, () => {
2293
+ referenced = true;
2294
+ });
2295
+ return referenced;
2296
+ }
2297
+
2298
+ /**
2299
+ * Walk `node`, invoking `onReference` when an Identifier in value position
2300
+ * matches a name in `names`. Skips property keys and non-computed member
2301
+ * properties. On entering a nested function, removes any parameter names it
2302
+ * rebinds (shadowing) from the active set for that subtree, and does NOT descend
2303
+ * into a PROVABLY-UNINVOKED helper — a function bound to a local name that is
2304
+ * never referenced again anywhere in the assertion body, so it cannot run when
2305
+ * the assertion runs (e.g. `(ctx) => { const later = () => ctx.status; }`). Its
2306
+ * parameter reads therefore must not count as inspecting the response, mirroring
2307
+ * how `functionThrows` ignores throws inside nested functions.
2308
+ *
2309
+ * Crucially, a helper that IS referenced again (a call site like `check()`) is
2310
+ * NOT skipped — its body is searched — so real assertions that factor the check
2311
+ * into a local helper still pass. Immediately-invoked callbacks (`.every(cb)`,
2312
+ * IIFEs, callees) are likewise searched. When in doubt we descend (fail open):
2313
+ * the only skip is a helper we can prove is never invoked.
2314
+ */
2315
+ function walkAstValues(
2316
+ node: acorn.AnyNode,
2317
+ names: Set<string>,
2318
+ outerBody: acorn.AnyNode,
2319
+ parent: acorn.AnyNode | null,
2320
+ parentKey: string | null,
2321
+ onReference: () => void,
2322
+ ): void {
2323
+ if (names.size === 0) {
2324
+ return;
2325
+ }
2326
+ if (node.type === "Identifier") {
2327
+ if (names.has(node.name)) {
2328
+ onReference();
2329
+ }
2330
+ return;
2331
+ }
2332
+ // Nested function: subtract its own parameter bindings (shadowing) before
2333
+ // descending into its body — and skip it only when it is a provably
2334
+ // uninvoked helper.
2335
+ if (isFunctionNode(node)) {
2336
+ const shadowed = new Set<string>();
2337
+ for (const param of node.params) {
2338
+ collectBoundNames(param, shadowed);
2339
+ }
2340
+ const visible = new Set<string>();
2341
+ for (const name of names) {
2342
+ if (!shadowed.has(name)) {
2343
+ visible.add(name);
2344
+ }
2345
+ }
2346
+ if (visible.size === 0 || isProvablyUninvokedHelper(node, parent, parentKey, outerBody)) {
2347
+ return;
2348
+ }
2349
+ for (const [key, child] of childEntries(node)) {
2350
+ if (key === "params") {
2351
+ continue;
2352
+ }
2353
+ walkAstValues(child, visible, outerBody, node, key, onReference);
2354
+ }
2355
+ return;
2356
+ }
2357
+ for (const [key, child] of childEntries(node)) {
2358
+ // Skip non-computed property keys (`{ status: x }`) and member
2359
+ // properties (`obj.status`) — these are names, not references.
2360
+ if (key === "key" && node.type === "Property" && !node.computed) {
2361
+ continue;
2362
+ }
2363
+ if (key === "property" && node.type === "MemberExpression" && !node.computed) {
2364
+ continue;
2365
+ }
2366
+ walkAstValues(child, names, outerBody, node, key, onReference);
2367
+ }
2368
+ }
2369
+
2370
+ /**
2371
+ * True if `fn` is a local helper bound to a name that is NEVER referenced again
2372
+ * anywhere in `outerBody` — meaning it is never invoked, so its body does not run
2373
+ * as part of evaluating the assertion. Only these provably-dead helpers are
2374
+ * skipped; a helper with any call site (its name appearing more than once, i.e.
2375
+ * beyond its own declaration) is treated as potentially executed and searched.
2376
+ * Anonymous functions in expression position (call args, callees, returns) are
2377
+ * never "uninvoked helpers" — they may run — so they are not skipped here.
2378
+ */
2379
+ function isProvablyUninvokedHelper(
2380
+ fn: AssertionFunctionNode,
2381
+ parent: acorn.AnyNode | null,
2382
+ parentKey: string | null,
2383
+ outerBody: acorn.AnyNode,
2384
+ ): boolean {
2385
+ let helperName: string | undefined;
2386
+ if (fn.type === "FunctionDeclaration" && fn.id) {
2387
+ helperName = fn.id.name;
2388
+ } else if (
2389
+ parent &&
2390
+ parent.type === "VariableDeclarator" &&
2391
+ parentKey === "init" &&
2392
+ parent.id.type === "Identifier"
2393
+ ) {
2394
+ helperName = parent.id.name;
2395
+ }
2396
+ if (helperName === undefined) {
2397
+ // Not a name-bound helper (anonymous callback / expression). It may run,
2398
+ // so do not skip it.
2399
+ return false;
2400
+ }
2401
+ // Count every occurrence of the helper name in the assertion body. Exactly
2402
+ // one occurrence is its own binding declaration; more than one means there is
2403
+ // at least one reference (call site), so the helper can run.
2404
+ let occurrences = 0;
2405
+ walkAst(outerBody, (node) => {
2406
+ if (node.type === "Identifier" && node.name === helperName) {
2407
+ occurrences += 1;
2408
+ }
2409
+ return true;
2410
+ });
2411
+ return occurrences <= 1;
2412
+ }
2413
+
2414
+ /** Generic pre-order AST walk; `visit` returns false to stop descending. */
2415
+ function walkAst(node: acorn.AnyNode, visit: (node: acorn.AnyNode) => boolean): void {
2416
+ if (!visit(node)) {
2417
+ return;
2418
+ }
2419
+ for (const child of childNodes(node)) {
2420
+ walkAst(child, visit);
2421
+ }
2422
+ }
2423
+
2424
+ function* childNodes(node: acorn.AnyNode): Generator<acorn.AnyNode> {
2425
+ for (const [, child] of childEntries(node)) {
2426
+ yield child;
2427
+ }
2428
+ }
2429
+
2430
+ function* childEntries(node: acorn.AnyNode): Generator<[string, acorn.AnyNode]> {
2431
+ for (const key of Object.keys(node)) {
2432
+ if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range") {
2433
+ continue;
2434
+ }
2435
+ const value = (node as unknown as Record<string, unknown>)[key];
2436
+ if (Array.isArray(value)) {
2437
+ for (const item of value) {
2438
+ if (isAstNode(item)) {
2439
+ yield [key, item];
2440
+ }
2441
+ }
2442
+ } else if (isAstNode(value)) {
2443
+ yield [key, value];
2444
+ }
2445
+ }
2446
+ }
2447
+
2448
+ function isAstNode(value: unknown): value is acorn.AnyNode {
2449
+ return (
2450
+ typeof value === "object" &&
2451
+ value !== null &&
2452
+ typeof (value as { type?: unknown }).type === "string"
2453
+ );
2454
+ }
2455
+
2062
2456
  function scoreSmoke(
2063
2457
  smokeResult: SmokeResult | undefined,
2064
2458
  smokeNote: string | undefined,
@@ -405,6 +405,38 @@ export async function buildProviderCreatePlan(options, cwd) {
405
405
  PROVIDER_ID: options.name,
406
406
  }),
407
407
  },
408
+ {
409
+ path: resolve(providerRoot, "AGENTS.md"),
410
+ content: await renderTemplate("AGENTS.md.tpl", {}),
411
+ },
412
+ {
413
+ path: resolve(providerRoot, "CLAUDE.md"),
414
+ content: await renderTemplate("CLAUDE.md.tpl", {}),
415
+ },
416
+ {
417
+ path: resolve(providerRoot, "skills", "normalization-standards", "SKILL.md"),
418
+ content: await renderTemplate("skills/normalization-standards/SKILL.md.tpl", {}),
419
+ },
420
+ {
421
+ path: resolve(providerRoot, "skills", "upstream-contract-verification", "SKILL.md"),
422
+ content: await renderTemplate("skills/upstream-contract-verification/SKILL.md.tpl", {}),
423
+ },
424
+ {
425
+ path: resolve(providerRoot, "skills", "fixtures-and-recording", "SKILL.md"),
426
+ content: await renderTemplate("skills/fixtures-and-recording/SKILL.md.tpl", {}),
427
+ },
428
+ {
429
+ path: resolve(providerRoot, "skills", "pagination-and-counts", "SKILL.md"),
430
+ content: await renderTemplate("skills/pagination-and-counts/SKILL.md.tpl", {}),
431
+ },
432
+ {
433
+ path: resolve(providerRoot, "skills", "health-checks-and-fail-closed", "SKILL.md"),
434
+ content: await renderTemplate("skills/health-checks-and-fail-closed/SKILL.md.tpl", {}),
435
+ },
436
+ {
437
+ path: resolve(providerRoot, "skills", "upstream-notes", "README.md"),
438
+ content: await renderTemplate("skills/upstream-notes/README.md.tpl", {}),
439
+ },
408
440
  ];
409
441
  return {
410
442
  displayName: options.displayName,
@@ -0,0 +1,87 @@
1
+ # APIFuse Provider Workspace — Agent Guide
2
+
3
+ You are building an APIFuse provider. APIFuse turns messy upstream APIs into
4
+ normalized, typed, evidence-backed public APIs. A provider that merely proxies
5
+ the upstream is a failed provider, even if every check passes.
6
+
7
+ This file is the core contract. Detailed procedures live in `skills/` — load
8
+ the matching skill BEFORE working on that area (index at the bottom).
9
+
10
+ ## Non-negotiable principles
11
+
12
+ ### 1. Normalize, don't proxy
13
+ Public output is an APIFuse contract, not the upstream's shape.
14
+ - Field names: `snake_case`, semantic, English. Never leak vendor keys
15
+ (`dutyTel1`, `hvec`, `MKioskTy7`) into public output.
16
+ - Timestamps: ISO 8601 in public output. Vendor formats (`20260707222855`)
17
+ are parsed inside mappers only. If a value cannot be parsed, omit/null it —
18
+ never pass the raw vendor string through.
19
+ - Enums: normalize vendor status text/codes (`Y` / `불가능` / `정보미제공`) into a
20
+ declared enum. Mixed raw-text passthrough is a contract failure.
21
+ - Units: every numeric field name states its unit (`distance_meters`), and the
22
+ mapper proves the conversion. Never relabel an upstream number without
23
+ verifying its unit against docs or live data.
24
+
25
+ ### 2. Fail closed, never fabricate
26
+ - Never invent output values to satisfy a schema. Missing upstream data → null
27
+ field or structured error, never a plausible dummy.
28
+ - Parse failures are errors, not defaults. Returning `0`, `[]`, or `null`
29
+ when the upstream shape changed hides breakage from every downstream gate.
30
+ - If a non-empty upstream collection normalizes to zero rows, throw
31
+ `UPSTREAM_SCHEMA_ERROR` — silent empty success is the worst failure mode.
32
+ - Model the upstream's real value domain. Check live data before adding
33
+ constraints like `nonnegative()` — some upstreams legitimately return
34
+ negative counts (e.g. overcapacity) and a wrong constraint silently
35
+ nulls real data.
36
+
37
+ ### 3. Preserve every input or fail loudly
38
+ - Every accepted input must be representable in the upstream request. If it
39
+ isn't, reject at the schema or throw — never silently drop it.
40
+ - Upstream parameter dependencies (param B ignored without param A) must be
41
+ enforced in YOUR schema. The upstream ignoring input silently is not an
42
+ excuse for your provider to do the same.
43
+
44
+ ### 4. Evidence over assumption
45
+ - Upstream parameter names and response fields must be verified against the
46
+ official spec AND at least one live call. Do not guess casing or
47
+ underscores; do not copy from a sibling API without re-verifying.
48
+ - No speculative field probing (`row.distance ?? row.dist ?? row.Distance`).
49
+ Map exactly the fields you have evidence for. One verified name per field.
50
+ - Fixtures are recorded live evidence (`bun run record`), never hand-written.
51
+ Placeholder-looking values (`02-1234-5678`, "테헤란로 123") mean the fixture
52
+ is fabricated and the submission is not reviewable.
53
+ - An empty result set from a dense query (0 hospitals within 5km of a city
54
+ center) is a request bug, not a valid fixture. Investigate before recording.
55
+
56
+ ### 5. Honest pagination and counts
57
+ - If you filter rows client-side, the upstream `totalCount` is no longer your
58
+ `total_count`. Either expose upstream semantics honestly (documented) or
59
+ don't expose a total at all. A count the caller cannot page against is a lie.
60
+
61
+ ### 6. Health checks must detect real regressions
62
+ - `Array.isArray(data.items)` alone can never fail. Every list operation's
63
+ health check must also flag the zero-rows case for a query that is known to
64
+ return data (dense-area query), so a broken upstream contract degrades
65
+ visibly instead of passing forever.
66
+
67
+ ## Verification loop (before every submit)
68
+
69
+ ```bash
70
+ bun run check # apifuse check + type-check
71
+ bun run test # your tests — cover mappers, error paths, edge rows
72
+ bun run submit-check # structural score; a high score does NOT prove quality
73
+ ```
74
+
75
+ `submit-check` is a structural gate. Every principle above can be violated
76
+ while scoring 95/100 — reviewers and CI audit for exactly these classes.
77
+
78
+ ## Skill index — load before working on:
79
+
80
+ | Area | Load |
81
+ | --- | --- |
82
+ | Output schemas, mappers, field naming, timestamps, enums | `skills/normalization-standards/SKILL.md` |
83
+ | Upstream request params, new endpoint wiring, field mapping | `skills/upstream-contract-verification/SKILL.md` |
84
+ | Recording fixtures, writing tests against fixtures | `skills/fixtures-and-recording/SKILL.md` |
85
+ | List operations, paging, totals, client-side filtering | `skills/pagination-and-counts/SKILL.md` |
86
+ | healthCheck blocks, error classification, fail-closed guards | `skills/health-checks-and-fail-closed/SKILL.md` |
87
+ | Upstream-specific known pitfalls for THIS bounty | `skills/upstream-notes/` (read every file) |
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,58 @@
1
+ ---
2
+ name: fixtures-and-recording
3
+ description: Recording live fixtures and writing trustworthy tests from them. Load before touching __fixtures__/ or writing operation tests.
4
+ ---
5
+
6
+ # Fixtures and recording
7
+
8
+ Fixtures are evidence, not examples. Reviewers treat fixtures as proof your
9
+ provider ran against the real upstream.
10
+
11
+ ## Recording
12
+ - Always record from the live upstream: `bun run record -- --operation <op>
13
+ --params '<json>'` with the real service key configured.
14
+ - Record queries that RETURN DATA. Choose dense/known-good inputs (major city
15
+ district, a real entity id from a prior list call).
16
+ - Re-record after any request-param or mapper change; stale fixtures make
17
+ every downstream test meaningless.
18
+
19
+ ## Forbidden fixture states
20
+ - **Hand-written fixtures.** Values like `02-1234-5678`, "테헤란로 123",
21
+ round-number coordinates, or sequential ids are fabrication tells. If it
22
+ wasn't returned by the upstream, it cannot be in `__fixtures__/`.
23
+ - **Empty-result fixtures for dense queries.** `items: [], total_count: 0`
24
+ for "hospitals within 5km of Gangnam" is not a fixture — it is an
25
+ unfixed request bug (wrong param name/format). Investigate first.
26
+ - **Fixtures that contradict each other.** If one operation's fixture proves
27
+ `total_count: 541` while returning 1 filtered row, your count semantics are
28
+ broken (see pagination skill), not your fixture.
29
+
30
+ ## Fixture shape
31
+ `apifuse record` (`bun run record`) writes the captured RAW UPSTREAM payload
32
+ to `__fixtures__/raw.json` (secrets sanitized). With `--append` it
33
+ accumulates an array of raw payloads. The recorder does NOT write your
34
+ normalized output — raw.json is upstream evidence only.
35
+
36
+ Derive normalized expectations in TESTS, not in the fixture file: load the
37
+ recorded raw payload, run your mapper over it, and assert the exact expected
38
+ normalized rows inline in the test. If you keep expected-output snapshots,
39
+ generate them from the mapper and review them row by row — never hand-author
40
+ values that the upstream did not return.
41
+
42
+ ## Tests to derive from fixtures
43
+ - Mapper: `map(recordedUpstreamRow)` equals the expected normalized row
44
+ (toEqual, not toMatchObject, for full rows — partial matching hides
45
+ dropped fields).
46
+ - Edge rows: single-item object vs array (`items.item` unwrapping), missing
47
+ optional fields, unpadded/numeric time values, vendor error headers.
48
+ - Error paths: upstream error `resultCode`, HTTP failure, missing secret,
49
+ NO_DATA — each asserts the structured `ProviderError` code.
50
+ - Handler-level: run the operation handler against a mock ctx that returns
51
+ the fixture upstream body; assert the full normalized envelope.
52
+
53
+ ## Checklist
54
+ - [ ] Every fixture recorded live; no placeholder-looking values
55
+ - [ ] No empty-result fixture for a query that must have data
56
+ - [ ] normalized expectations derived from mapper(recorded raw), not
57
+ hand-authored
58
+ - [ ] Error and edge-shape rows covered, not just the happy row
@@ -0,0 +1,65 @@
1
+ ---
2
+ name: health-checks-and-fail-closed
3
+ description: Writing health checks that can actually fail, and fail-closed guards at envelope and row level. Load before writing healthCheck blocks or error handling.
4
+ ---
5
+
6
+ # Health checks and fail-closed guards
7
+
8
+ ## Health checks that can actually fail
9
+ `Array.isArray(data.items)` alone can never fail. Every list operation's
10
+ health check must be able to detect the zero-rows regression.
11
+
12
+ Assertion contract (per SDK `HealthCheckCase`): THROW to fail the case
13
+ (recorded as `down`); return `{ status: "degraded", label }` to flag without
14
+ failing; return nothing for `ok`. There is no `"down"` return value.
15
+
16
+ ```ts
17
+ assertions: ({ status, data }) => {
18
+ if (status !== 200) {
19
+ throw new Error(`<op> request failed with status ${status}`);
20
+ }
21
+ if (!Array.isArray(data.items)) {
22
+ throw new Error("<op> missing items array");
23
+ }
24
+ // Dense query MUST return rows; zero rows = upstream contract drift
25
+ if (data.items.length === 0) {
26
+ return { status: "degraded", label: "<op> dense query returned 0 rows" };
27
+ }
28
+ }
29
+ ```
30
+
31
+ - Choose health-check inputs that are guaranteed-dense (major city district,
32
+ a stable well-known entity id). Verify the id still exists when picking it.
33
+ - Also assert one semantic field on the first row (e.g. `items[0].name` is a
34
+ non-empty string) so a mapper regression that empties fields degrades too.
35
+
36
+ ## Fail-closed: envelope level
37
+ - Upstream error headers/codes → structured `ProviderError` with a stable
38
+ `code` (`UPSTREAM_AUTH_ERROR`, `UPSTREAM_ERROR`, `NO_DATA`, ...).
39
+ - Non-JSON body, unexpected content type → `UPSTREAM_SCHEMA_ERROR`.
40
+ - HTTP non-2xx → classified error; never a fake empty success envelope.
41
+ Fixture-based tests cannot catch swallowed errors — write an explicit test:
42
+ mock a non-ok response and assert the handler REJECTS.
43
+
44
+ ## Fail-closed: row level
45
+ Envelope guards are not enough. The silent killer is: response is valid,
46
+ array is non-empty, but every row normalizes to nothing.
47
+ - If a non-empty upstream collection produces zero normalized rows, throw
48
+ `UPSTREAM_SCHEMA_ERROR` instead of returning `items: []`.
49
+ - Identity fields (id, name) missing on a row → throw, don't skip the row
50
+ silently.
51
+ - Regression-test both layers separately: a bad envelope AND a good envelope
52
+ with unmappable rows.
53
+
54
+ ## Error message hygiene
55
+ `ProviderError.message` reaches the tenant verbatim. Never interpolate
56
+ upstream free text that may contain personal data (names, phone numbers,
57
+ addresses); allowlist known code tokens and keep raw bodies in server-side
58
+ details/logs only.
59
+
60
+ ## Checklist
61
+ - [ ] Every list op health check flags 0 rows on a dense query
62
+ - [ ] One semantic field asserted on a real row
63
+ - [ ] Swallowed-error test exists (non-ok mock → handler rejects)
64
+ - [ ] Non-empty upstream → zero normalized rows throws
65
+ - [ ] No upstream free text in customer-facing error messages
@@ -0,0 +1,57 @@
1
+ ---
2
+ name: normalization-standards
3
+ description: Public output contract rules — field naming, timestamps, enums, units, nullability. Load before writing or editing any output schema or mapper.
4
+ ---
5
+
6
+ # Normalization standards
7
+
8
+ Public output is the product. Apply these to every schema + mapper pair.
9
+
10
+ ## Field naming
11
+ - `snake_case`, English, semantic. `emergency_phone`, not `dutyTel3`.
12
+ - Never expose vendor key vocabularies (`hv1`..`hv12`, `MKioskTy*`, `duty*`)
13
+ as public field names OR as dynamic record keys. A
14
+ `z.record(z.string(), ...)` keyed by vendor codes is still a vendor leak —
15
+ map codes to a stable public vocabulary (enum keys or an array of
16
+ `{ code, label, status }` objects with normalized status).
17
+
18
+ ## Timestamps and dates
19
+ - Public: ISO 8601 (`2026-07-07T22:28:55+09:00`, dates `2026-07-07`,
20
+ clock times `HH:MM`). Include the upstream's timezone offset; Korean public
21
+ APIs are KST (+09:00) — verify, then encode it.
22
+ - Vendor formats (`YYYYMMDDHHmmss`, `HHmm`, unpadded `900`) are parsed inside
23
+ the mapper. Unparseable → `null`, plus a test for that row shape.
24
+ - Never emit a raw vendor timestamp string in public output, including
25
+ fixtures.
26
+
27
+ ## Enums
28
+ - Vendor status values (codes, `Y`/`N`, Korean labels like `불가능`,
29
+ `정보미제공`) → declared `z.enum`. Unknown value → explicit `unknown` member
30
+ or fail closed; never pass raw text through.
31
+ - Map from the OFFICIAL code table, not from guessing what live samples mean.
32
+ Add a regression test per enum member.
33
+
34
+ ## Numbers and units
35
+ - Field name states the unit: `distance_meters`, `radius_meters`,
36
+ `price_krw`. Mapper proves the conversion (upstream km → `* 1000`).
37
+ - Verify the upstream unit from spec or live-data sanity check (a "distance"
38
+ of `1.2` from a nearby search is km, not meters). Sibling endpoints of the
39
+ same vendor may differ — verify each one.
40
+ - Value-domain constraints (`nonnegative`, `min`, `max`) must reflect the
41
+ upstream's REAL domain observed in live data, not what seems sensible.
42
+ A wrong `nonnegative()` turns real negative values into `null`/errors
43
+ silently.
44
+
45
+ ## Nullability
46
+ - `null` means "upstream did not provide it" — never "parsing failed" and
47
+ never a placeholder for invented data.
48
+ - Required-for-identity fields (ids, names) missing → throw
49
+ `UPSTREAM_SCHEMA_ERROR`; do not emit partial rows.
50
+
51
+ ## Checklist before submitting a schema/mapper change
52
+ - [ ] No vendor key visible in any public field name or record key
53
+ - [ ] All timestamps ISO 8601 with timezone; parsing tested for real vendor
54
+ shapes (padded/unpadded, string/number)
55
+ - [ ] All status-like strings are declared enums with official-table mapping
56
+ - [ ] Every numeric field's unit is in its name and conversion is tested
57
+ - [ ] Constraints checked against live data, not intuition
@@ -0,0 +1,52 @@
1
+ ---
2
+ name: pagination-and-counts
3
+ description: total_count semantics, client-side filtering, and paging honesty. Load before implementing any list/search operation.
4
+ ---
5
+
6
+ # Pagination and counts
7
+
8
+ A caller uses `total_count`, `page`, and `limit` to plan iteration. If those
9
+ numbers don't describe what the caller can actually page through, the
10
+ operation is lying.
11
+
12
+ ## The client-side filtering trap
13
+ If the upstream has no server-side filter for one of your inputs (e.g. no
14
+ radius param) and you filter rows after fetching:
15
+
16
+ - Upstream `totalCount` counts UNFILTERED rows. Returning it as your
17
+ `total_count` while returning filtered rows means: caller sees
18
+ `total_count: 541`, gets 1 row on page 1, and pages 2..28 return rows that
19
+ are outside the filter or empty. This is a contract failure, not a nuance.
20
+
21
+ Acceptable resolutions, in preference order:
22
+ 1. **Don't accept the input.** If the upstream can't filter by it and you
23
+ can't enumerate all pages, drop the input from the schema and document the
24
+ upstream's real semantics (e.g. "results are distance-sorted; no radius
25
+ cutoff").
26
+ 2. **Expose upstream semantics honestly.** Distance-sorted paging with a
27
+ documented "no radius filter" contract and no fake `radius` input.
28
+ 3. **Filter AND fix the metadata.** If you must filter client-side, do not
29
+ return the upstream total. Return only what you can prove (`returned_count`
30
+ plus a `has_more` you can actually compute) and document that totals are
31
+ unavailable.
32
+
33
+ Never combine: accepted filter input + client-side filter + upstream total.
34
+
35
+ ## Count integrity
36
+ - Parse failure of `totalCount` → `UPSTREAM_SCHEMA_ERROR`, not `0`.
37
+ A fail-open zero disguises upstream drift as an empty dataset.
38
+ - If `total_count > 0` but the page's row array normalizes to empty on
39
+ page 1, throw — that combination means broken extraction, not empty data.
40
+
41
+ ## Page/limit echo
42
+ - Echo the EFFECTIVE values: if you clamp `limit` to the upstream max, return
43
+ the clamped value, not the requested one.
44
+ - `page`/`limit` semantics must match the upstream's paging model
45
+ (1-indexed vs 0-indexed) — verify with two live pages, checking the
46
+ returned rows actually differ.
47
+
48
+ ## Checklist
49
+ - [ ] No input is filtered client-side while `total_count` comes from upstream
50
+ - [ ] totalCount parse failure fails closed
51
+ - [ ] Effective (clamped) limit echoed
52
+ - [ ] Two-page live check proves paging advances
@@ -0,0 +1,45 @@
1
+ ---
2
+ name: upstream-contract-verification
3
+ description: How to establish evidence for upstream request params and response fields before coding. Load before wiring any new endpoint or mapping new fields.
4
+ ---
5
+
6
+ # Upstream contract verification
7
+
8
+ Most provider P0s come from guessed upstream contracts. Every param name and
9
+ response field needs evidence BEFORE it ships.
10
+
11
+ ## Request parameters
12
+ 1. Start from the official spec document (data.go.kr 활용가이드, vendor API
13
+ docs). Copy exact names — casing and underscores matter
14
+ (`WGS84_LAT` ≠ `WGS84LAT`; the wrong one is often silently ignored).
15
+ 2. Confirm with ONE live call per endpoint. A param being ignored does not
16
+ produce an error — it produces plausible-looking wrong results, so compare:
17
+ - filtered vs unfiltered `totalCount` (identical → param ignored)
18
+ - a dense-area query returning 0 rows (→ param name/format wrong)
19
+ 3. Do NOT copy param names from a sibling endpoint or sibling API of the same
20
+ vendor without re-verifying. Same vendor ≠ same contract; endpoints drift.
21
+ 4. If a param only works together with another param (district requires
22
+ province), encode that dependency in the input schema with a clear error.
23
+ Test it: dependent-param-alone must be rejected, not silently national.
24
+
25
+ ## Response fields
26
+ - Map exactly the field names present in your recorded live fixtures.
27
+ - No speculative fallback chains (`row.distance ?? row.dist ?? row.Distance`).
28
+ If two shapes genuinely exist, you need a recorded fixture proving EACH
29
+ branch plus a row-level test per branch; otherwise map one name only.
30
+ - Field presence varies by endpoint within the same vendor. Detail endpoints
31
+ often return more/differently-named fields than list endpoints — record
32
+ fixtures per endpoint, not per vendor.
33
+
34
+ ## When results look wrong
35
+ - Same response body across different request payloads → the upstream is
36
+ ignoring your variation; stop tuning fields and re-check param names/auth.
37
+ - Empty result for a query that must have data (city-center radius search,
38
+ major-district listing) → treat as a request bug. Never record it as a
39
+ fixture and never ship it.
40
+
41
+ ## Deliverables per endpoint
42
+ - [ ] Spec reference (URL or doc name + section) noted in the PR/commit
43
+ - [ ] One recorded live fixture proving request params take effect
44
+ - [ ] Negative evidence checked: filtered count differs from unfiltered
45
+ - [ ] Param dependencies enforced in the input schema with tests
@@ -0,0 +1,13 @@
1
+ # Upstream notes
2
+
3
+ Per-vendor / per-API-family pitfalls proven by live evidence. These are the
4
+ highest-value files in this workspace: general principles are knowable, but
5
+ "this API silently ignores param X" is only discoverable by getting burned.
6
+
7
+ - Read EVERY file here before your first upstream call.
8
+ - When you discover a new upstream quirk (silently ignored param, unit
9
+ surprise, undocumented value domain, error-shape oddity), ADD it here in
10
+ the same format — evidence line included. Reviewers treat contributed
11
+ upstream notes as part of submission quality.
12
+
13
+ Format per entry: **Symptom → Cause → Rule → Evidence**.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.1.0-beta.20",
2
+ "version": "2.1.0-beta.21",
3
3
  "name": "@apifuse/provider-sdk",
4
4
  "private": false,
5
5
  "type": "module",
@@ -88,6 +88,7 @@
88
88
  "dependencies": {
89
89
  "@clack/prompts": "^1.5.1",
90
90
  "@types/ms": "^2.1.0",
91
+ "acorn": "^8.17.0",
91
92
  "ajv": "^8.17",
92
93
  "hono": "^4.12.25",
93
94
  "impit": "0.14.1",
package/src/cli/create.ts CHANGED
@@ -538,6 +538,38 @@ export async function buildProviderCreatePlan(
538
538
  PROVIDER_ID: options.name,
539
539
  }),
540
540
  },
541
+ {
542
+ path: resolve(providerRoot, "AGENTS.md"),
543
+ content: await renderTemplate("AGENTS.md.tpl", {}),
544
+ },
545
+ {
546
+ path: resolve(providerRoot, "CLAUDE.md"),
547
+ content: await renderTemplate("CLAUDE.md.tpl", {}),
548
+ },
549
+ {
550
+ path: resolve(providerRoot, "skills", "normalization-standards", "SKILL.md"),
551
+ content: await renderTemplate("skills/normalization-standards/SKILL.md.tpl", {}),
552
+ },
553
+ {
554
+ path: resolve(providerRoot, "skills", "upstream-contract-verification", "SKILL.md"),
555
+ content: await renderTemplate("skills/upstream-contract-verification/SKILL.md.tpl", {}),
556
+ },
557
+ {
558
+ path: resolve(providerRoot, "skills", "fixtures-and-recording", "SKILL.md"),
559
+ content: await renderTemplate("skills/fixtures-and-recording/SKILL.md.tpl", {}),
560
+ },
561
+ {
562
+ path: resolve(providerRoot, "skills", "pagination-and-counts", "SKILL.md"),
563
+ content: await renderTemplate("skills/pagination-and-counts/SKILL.md.tpl", {}),
564
+ },
565
+ {
566
+ path: resolve(providerRoot, "skills", "health-checks-and-fail-closed", "SKILL.md"),
567
+ content: await renderTemplate("skills/health-checks-and-fail-closed/SKILL.md.tpl", {}),
568
+ },
569
+ {
570
+ path: resolve(providerRoot, "skills", "upstream-notes", "README.md"),
571
+ content: await renderTemplate("skills/upstream-notes/README.md.tpl", {}),
572
+ },
541
573
  ];
542
574
 
543
575
  return {
@@ -0,0 +1,87 @@
1
+ # APIFuse Provider Workspace — Agent Guide
2
+
3
+ You are building an APIFuse provider. APIFuse turns messy upstream APIs into
4
+ normalized, typed, evidence-backed public APIs. A provider that merely proxies
5
+ the upstream is a failed provider, even if every check passes.
6
+
7
+ This file is the core contract. Detailed procedures live in `skills/` — load
8
+ the matching skill BEFORE working on that area (index at the bottom).
9
+
10
+ ## Non-negotiable principles
11
+
12
+ ### 1. Normalize, don't proxy
13
+ Public output is an APIFuse contract, not the upstream's shape.
14
+ - Field names: `snake_case`, semantic, English. Never leak vendor keys
15
+ (`dutyTel1`, `hvec`, `MKioskTy7`) into public output.
16
+ - Timestamps: ISO 8601 in public output. Vendor formats (`20260707222855`)
17
+ are parsed inside mappers only. If a value cannot be parsed, omit/null it —
18
+ never pass the raw vendor string through.
19
+ - Enums: normalize vendor status text/codes (`Y` / `불가능` / `정보미제공`) into a
20
+ declared enum. Mixed raw-text passthrough is a contract failure.
21
+ - Units: every numeric field name states its unit (`distance_meters`), and the
22
+ mapper proves the conversion. Never relabel an upstream number without
23
+ verifying its unit against docs or live data.
24
+
25
+ ### 2. Fail closed, never fabricate
26
+ - Never invent output values to satisfy a schema. Missing upstream data → null
27
+ field or structured error, never a plausible dummy.
28
+ - Parse failures are errors, not defaults. Returning `0`, `[]`, or `null`
29
+ when the upstream shape changed hides breakage from every downstream gate.
30
+ - If a non-empty upstream collection normalizes to zero rows, throw
31
+ `UPSTREAM_SCHEMA_ERROR` — silent empty success is the worst failure mode.
32
+ - Model the upstream's real value domain. Check live data before adding
33
+ constraints like `nonnegative()` — some upstreams legitimately return
34
+ negative counts (e.g. overcapacity) and a wrong constraint silently
35
+ nulls real data.
36
+
37
+ ### 3. Preserve every input or fail loudly
38
+ - Every accepted input must be representable in the upstream request. If it
39
+ isn't, reject at the schema or throw — never silently drop it.
40
+ - Upstream parameter dependencies (param B ignored without param A) must be
41
+ enforced in YOUR schema. The upstream ignoring input silently is not an
42
+ excuse for your provider to do the same.
43
+
44
+ ### 4. Evidence over assumption
45
+ - Upstream parameter names and response fields must be verified against the
46
+ official spec AND at least one live call. Do not guess casing or
47
+ underscores; do not copy from a sibling API without re-verifying.
48
+ - No speculative field probing (`row.distance ?? row.dist ?? row.Distance`).
49
+ Map exactly the fields you have evidence for. One verified name per field.
50
+ - Fixtures are recorded live evidence (`bun run record`), never hand-written.
51
+ Placeholder-looking values (`02-1234-5678`, "테헤란로 123") mean the fixture
52
+ is fabricated and the submission is not reviewable.
53
+ - An empty result set from a dense query (0 hospitals within 5km of a city
54
+ center) is a request bug, not a valid fixture. Investigate before recording.
55
+
56
+ ### 5. Honest pagination and counts
57
+ - If you filter rows client-side, the upstream `totalCount` is no longer your
58
+ `total_count`. Either expose upstream semantics honestly (documented) or
59
+ don't expose a total at all. A count the caller cannot page against is a lie.
60
+
61
+ ### 6. Health checks must detect real regressions
62
+ - `Array.isArray(data.items)` alone can never fail. Every list operation's
63
+ health check must also flag the zero-rows case for a query that is known to
64
+ return data (dense-area query), so a broken upstream contract degrades
65
+ visibly instead of passing forever.
66
+
67
+ ## Verification loop (before every submit)
68
+
69
+ ```bash
70
+ bun run check # apifuse check + type-check
71
+ bun run test # your tests — cover mappers, error paths, edge rows
72
+ bun run submit-check # structural score; a high score does NOT prove quality
73
+ ```
74
+
75
+ `submit-check` is a structural gate. Every principle above can be violated
76
+ while scoring 95/100 — reviewers and CI audit for exactly these classes.
77
+
78
+ ## Skill index — load before working on:
79
+
80
+ | Area | Load |
81
+ | --- | --- |
82
+ | Output schemas, mappers, field naming, timestamps, enums | `skills/normalization-standards/SKILL.md` |
83
+ | Upstream request params, new endpoint wiring, field mapping | `skills/upstream-contract-verification/SKILL.md` |
84
+ | Recording fixtures, writing tests against fixtures | `skills/fixtures-and-recording/SKILL.md` |
85
+ | List operations, paging, totals, client-side filtering | `skills/pagination-and-counts/SKILL.md` |
86
+ | healthCheck blocks, error classification, fail-closed guards | `skills/health-checks-and-fail-closed/SKILL.md` |
87
+ | Upstream-specific known pitfalls for THIS bounty | `skills/upstream-notes/` (read every file) |
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,58 @@
1
+ ---
2
+ name: fixtures-and-recording
3
+ description: Recording live fixtures and writing trustworthy tests from them. Load before touching __fixtures__/ or writing operation tests.
4
+ ---
5
+
6
+ # Fixtures and recording
7
+
8
+ Fixtures are evidence, not examples. Reviewers treat fixtures as proof your
9
+ provider ran against the real upstream.
10
+
11
+ ## Recording
12
+ - Always record from the live upstream: `bun run record -- --operation <op>
13
+ --params '<json>'` with the real service key configured.
14
+ - Record queries that RETURN DATA. Choose dense/known-good inputs (major city
15
+ district, a real entity id from a prior list call).
16
+ - Re-record after any request-param or mapper change; stale fixtures make
17
+ every downstream test meaningless.
18
+
19
+ ## Forbidden fixture states
20
+ - **Hand-written fixtures.** Values like `02-1234-5678`, "테헤란로 123",
21
+ round-number coordinates, or sequential ids are fabrication tells. If it
22
+ wasn't returned by the upstream, it cannot be in `__fixtures__/`.
23
+ - **Empty-result fixtures for dense queries.** `items: [], total_count: 0`
24
+ for "hospitals within 5km of Gangnam" is not a fixture — it is an
25
+ unfixed request bug (wrong param name/format). Investigate first.
26
+ - **Fixtures that contradict each other.** If one operation's fixture proves
27
+ `total_count: 541` while returning 1 filtered row, your count semantics are
28
+ broken (see pagination skill), not your fixture.
29
+
30
+ ## Fixture shape
31
+ `apifuse record` (`bun run record`) writes the captured RAW UPSTREAM payload
32
+ to `__fixtures__/raw.json` (secrets sanitized). With `--append` it
33
+ accumulates an array of raw payloads. The recorder does NOT write your
34
+ normalized output — raw.json is upstream evidence only.
35
+
36
+ Derive normalized expectations in TESTS, not in the fixture file: load the
37
+ recorded raw payload, run your mapper over it, and assert the exact expected
38
+ normalized rows inline in the test. If you keep expected-output snapshots,
39
+ generate them from the mapper and review them row by row — never hand-author
40
+ values that the upstream did not return.
41
+
42
+ ## Tests to derive from fixtures
43
+ - Mapper: `map(recordedUpstreamRow)` equals the expected normalized row
44
+ (toEqual, not toMatchObject, for full rows — partial matching hides
45
+ dropped fields).
46
+ - Edge rows: single-item object vs array (`items.item` unwrapping), missing
47
+ optional fields, unpadded/numeric time values, vendor error headers.
48
+ - Error paths: upstream error `resultCode`, HTTP failure, missing secret,
49
+ NO_DATA — each asserts the structured `ProviderError` code.
50
+ - Handler-level: run the operation handler against a mock ctx that returns
51
+ the fixture upstream body; assert the full normalized envelope.
52
+
53
+ ## Checklist
54
+ - [ ] Every fixture recorded live; no placeholder-looking values
55
+ - [ ] No empty-result fixture for a query that must have data
56
+ - [ ] normalized expectations derived from mapper(recorded raw), not
57
+ hand-authored
58
+ - [ ] Error and edge-shape rows covered, not just the happy row
@@ -0,0 +1,65 @@
1
+ ---
2
+ name: health-checks-and-fail-closed
3
+ description: Writing health checks that can actually fail, and fail-closed guards at envelope and row level. Load before writing healthCheck blocks or error handling.
4
+ ---
5
+
6
+ # Health checks and fail-closed guards
7
+
8
+ ## Health checks that can actually fail
9
+ `Array.isArray(data.items)` alone can never fail. Every list operation's
10
+ health check must be able to detect the zero-rows regression.
11
+
12
+ Assertion contract (per SDK `HealthCheckCase`): THROW to fail the case
13
+ (recorded as `down`); return `{ status: "degraded", label }` to flag without
14
+ failing; return nothing for `ok`. There is no `"down"` return value.
15
+
16
+ ```ts
17
+ assertions: ({ status, data }) => {
18
+ if (status !== 200) {
19
+ throw new Error(`<op> request failed with status ${status}`);
20
+ }
21
+ if (!Array.isArray(data.items)) {
22
+ throw new Error("<op> missing items array");
23
+ }
24
+ // Dense query MUST return rows; zero rows = upstream contract drift
25
+ if (data.items.length === 0) {
26
+ return { status: "degraded", label: "<op> dense query returned 0 rows" };
27
+ }
28
+ }
29
+ ```
30
+
31
+ - Choose health-check inputs that are guaranteed-dense (major city district,
32
+ a stable well-known entity id). Verify the id still exists when picking it.
33
+ - Also assert one semantic field on the first row (e.g. `items[0].name` is a
34
+ non-empty string) so a mapper regression that empties fields degrades too.
35
+
36
+ ## Fail-closed: envelope level
37
+ - Upstream error headers/codes → structured `ProviderError` with a stable
38
+ `code` (`UPSTREAM_AUTH_ERROR`, `UPSTREAM_ERROR`, `NO_DATA`, ...).
39
+ - Non-JSON body, unexpected content type → `UPSTREAM_SCHEMA_ERROR`.
40
+ - HTTP non-2xx → classified error; never a fake empty success envelope.
41
+ Fixture-based tests cannot catch swallowed errors — write an explicit test:
42
+ mock a non-ok response and assert the handler REJECTS.
43
+
44
+ ## Fail-closed: row level
45
+ Envelope guards are not enough. The silent killer is: response is valid,
46
+ array is non-empty, but every row normalizes to nothing.
47
+ - If a non-empty upstream collection produces zero normalized rows, throw
48
+ `UPSTREAM_SCHEMA_ERROR` instead of returning `items: []`.
49
+ - Identity fields (id, name) missing on a row → throw, don't skip the row
50
+ silently.
51
+ - Regression-test both layers separately: a bad envelope AND a good envelope
52
+ with unmappable rows.
53
+
54
+ ## Error message hygiene
55
+ `ProviderError.message` reaches the tenant verbatim. Never interpolate
56
+ upstream free text that may contain personal data (names, phone numbers,
57
+ addresses); allowlist known code tokens and keep raw bodies in server-side
58
+ details/logs only.
59
+
60
+ ## Checklist
61
+ - [ ] Every list op health check flags 0 rows on a dense query
62
+ - [ ] One semantic field asserted on a real row
63
+ - [ ] Swallowed-error test exists (non-ok mock → handler rejects)
64
+ - [ ] Non-empty upstream → zero normalized rows throws
65
+ - [ ] No upstream free text in customer-facing error messages
@@ -0,0 +1,57 @@
1
+ ---
2
+ name: normalization-standards
3
+ description: Public output contract rules — field naming, timestamps, enums, units, nullability. Load before writing or editing any output schema or mapper.
4
+ ---
5
+
6
+ # Normalization standards
7
+
8
+ Public output is the product. Apply these to every schema + mapper pair.
9
+
10
+ ## Field naming
11
+ - `snake_case`, English, semantic. `emergency_phone`, not `dutyTel3`.
12
+ - Never expose vendor key vocabularies (`hv1`..`hv12`, `MKioskTy*`, `duty*`)
13
+ as public field names OR as dynamic record keys. A
14
+ `z.record(z.string(), ...)` keyed by vendor codes is still a vendor leak —
15
+ map codes to a stable public vocabulary (enum keys or an array of
16
+ `{ code, label, status }` objects with normalized status).
17
+
18
+ ## Timestamps and dates
19
+ - Public: ISO 8601 (`2026-07-07T22:28:55+09:00`, dates `2026-07-07`,
20
+ clock times `HH:MM`). Include the upstream's timezone offset; Korean public
21
+ APIs are KST (+09:00) — verify, then encode it.
22
+ - Vendor formats (`YYYYMMDDHHmmss`, `HHmm`, unpadded `900`) are parsed inside
23
+ the mapper. Unparseable → `null`, plus a test for that row shape.
24
+ - Never emit a raw vendor timestamp string in public output, including
25
+ fixtures.
26
+
27
+ ## Enums
28
+ - Vendor status values (codes, `Y`/`N`, Korean labels like `불가능`,
29
+ `정보미제공`) → declared `z.enum`. Unknown value → explicit `unknown` member
30
+ or fail closed; never pass raw text through.
31
+ - Map from the OFFICIAL code table, not from guessing what live samples mean.
32
+ Add a regression test per enum member.
33
+
34
+ ## Numbers and units
35
+ - Field name states the unit: `distance_meters`, `radius_meters`,
36
+ `price_krw`. Mapper proves the conversion (upstream km → `* 1000`).
37
+ - Verify the upstream unit from spec or live-data sanity check (a "distance"
38
+ of `1.2` from a nearby search is km, not meters). Sibling endpoints of the
39
+ same vendor may differ — verify each one.
40
+ - Value-domain constraints (`nonnegative`, `min`, `max`) must reflect the
41
+ upstream's REAL domain observed in live data, not what seems sensible.
42
+ A wrong `nonnegative()` turns real negative values into `null`/errors
43
+ silently.
44
+
45
+ ## Nullability
46
+ - `null` means "upstream did not provide it" — never "parsing failed" and
47
+ never a placeholder for invented data.
48
+ - Required-for-identity fields (ids, names) missing → throw
49
+ `UPSTREAM_SCHEMA_ERROR`; do not emit partial rows.
50
+
51
+ ## Checklist before submitting a schema/mapper change
52
+ - [ ] No vendor key visible in any public field name or record key
53
+ - [ ] All timestamps ISO 8601 with timezone; parsing tested for real vendor
54
+ shapes (padded/unpadded, string/number)
55
+ - [ ] All status-like strings are declared enums with official-table mapping
56
+ - [ ] Every numeric field's unit is in its name and conversion is tested
57
+ - [ ] Constraints checked against live data, not intuition
@@ -0,0 +1,52 @@
1
+ ---
2
+ name: pagination-and-counts
3
+ description: total_count semantics, client-side filtering, and paging honesty. Load before implementing any list/search operation.
4
+ ---
5
+
6
+ # Pagination and counts
7
+
8
+ A caller uses `total_count`, `page`, and `limit` to plan iteration. If those
9
+ numbers don't describe what the caller can actually page through, the
10
+ operation is lying.
11
+
12
+ ## The client-side filtering trap
13
+ If the upstream has no server-side filter for one of your inputs (e.g. no
14
+ radius param) and you filter rows after fetching:
15
+
16
+ - Upstream `totalCount` counts UNFILTERED rows. Returning it as your
17
+ `total_count` while returning filtered rows means: caller sees
18
+ `total_count: 541`, gets 1 row on page 1, and pages 2..28 return rows that
19
+ are outside the filter or empty. This is a contract failure, not a nuance.
20
+
21
+ Acceptable resolutions, in preference order:
22
+ 1. **Don't accept the input.** If the upstream can't filter by it and you
23
+ can't enumerate all pages, drop the input from the schema and document the
24
+ upstream's real semantics (e.g. "results are distance-sorted; no radius
25
+ cutoff").
26
+ 2. **Expose upstream semantics honestly.** Distance-sorted paging with a
27
+ documented "no radius filter" contract and no fake `radius` input.
28
+ 3. **Filter AND fix the metadata.** If you must filter client-side, do not
29
+ return the upstream total. Return only what you can prove (`returned_count`
30
+ plus a `has_more` you can actually compute) and document that totals are
31
+ unavailable.
32
+
33
+ Never combine: accepted filter input + client-side filter + upstream total.
34
+
35
+ ## Count integrity
36
+ - Parse failure of `totalCount` → `UPSTREAM_SCHEMA_ERROR`, not `0`.
37
+ A fail-open zero disguises upstream drift as an empty dataset.
38
+ - If `total_count > 0` but the page's row array normalizes to empty on
39
+ page 1, throw — that combination means broken extraction, not empty data.
40
+
41
+ ## Page/limit echo
42
+ - Echo the EFFECTIVE values: if you clamp `limit` to the upstream max, return
43
+ the clamped value, not the requested one.
44
+ - `page`/`limit` semantics must match the upstream's paging model
45
+ (1-indexed vs 0-indexed) — verify with two live pages, checking the
46
+ returned rows actually differ.
47
+
48
+ ## Checklist
49
+ - [ ] No input is filtered client-side while `total_count` comes from upstream
50
+ - [ ] totalCount parse failure fails closed
51
+ - [ ] Effective (clamped) limit echoed
52
+ - [ ] Two-page live check proves paging advances
@@ -0,0 +1,45 @@
1
+ ---
2
+ name: upstream-contract-verification
3
+ description: How to establish evidence for upstream request params and response fields before coding. Load before wiring any new endpoint or mapping new fields.
4
+ ---
5
+
6
+ # Upstream contract verification
7
+
8
+ Most provider P0s come from guessed upstream contracts. Every param name and
9
+ response field needs evidence BEFORE it ships.
10
+
11
+ ## Request parameters
12
+ 1. Start from the official spec document (data.go.kr 활용가이드, vendor API
13
+ docs). Copy exact names — casing and underscores matter
14
+ (`WGS84_LAT` ≠ `WGS84LAT`; the wrong one is often silently ignored).
15
+ 2. Confirm with ONE live call per endpoint. A param being ignored does not
16
+ produce an error — it produces plausible-looking wrong results, so compare:
17
+ - filtered vs unfiltered `totalCount` (identical → param ignored)
18
+ - a dense-area query returning 0 rows (→ param name/format wrong)
19
+ 3. Do NOT copy param names from a sibling endpoint or sibling API of the same
20
+ vendor without re-verifying. Same vendor ≠ same contract; endpoints drift.
21
+ 4. If a param only works together with another param (district requires
22
+ province), encode that dependency in the input schema with a clear error.
23
+ Test it: dependent-param-alone must be rejected, not silently national.
24
+
25
+ ## Response fields
26
+ - Map exactly the field names present in your recorded live fixtures.
27
+ - No speculative fallback chains (`row.distance ?? row.dist ?? row.Distance`).
28
+ If two shapes genuinely exist, you need a recorded fixture proving EACH
29
+ branch plus a row-level test per branch; otherwise map one name only.
30
+ - Field presence varies by endpoint within the same vendor. Detail endpoints
31
+ often return more/differently-named fields than list endpoints — record
32
+ fixtures per endpoint, not per vendor.
33
+
34
+ ## When results look wrong
35
+ - Same response body across different request payloads → the upstream is
36
+ ignoring your variation; stop tuning fields and re-check param names/auth.
37
+ - Empty result for a query that must have data (city-center radius search,
38
+ major-district listing) → treat as a request bug. Never record it as a
39
+ fixture and never ship it.
40
+
41
+ ## Deliverables per endpoint
42
+ - [ ] Spec reference (URL or doc name + section) noted in the PR/commit
43
+ - [ ] One recorded live fixture proving request params take effect
44
+ - [ ] Negative evidence checked: filtered count differs from unfiltered
45
+ - [ ] Param dependencies enforced in the input schema with tests
@@ -0,0 +1,13 @@
1
+ # Upstream notes
2
+
3
+ Per-vendor / per-API-family pitfalls proven by live evidence. These are the
4
+ highest-value files in this workspace: general principles are knowable, but
5
+ "this API silently ignores param X" is only discoverable by getting burned.
6
+
7
+ - Read EVERY file here before your first upstream call.
8
+ - When you discover a new upstream quirk (silently ignored param, unit
9
+ surprise, undocumented value domain, error-shape oddity), ADD it here in
10
+ the same format — evidence line included. Reviewers treat contributed
11
+ upstream notes as part of submission quality.
12
+
13
+ Format per entry: **Symptom → Cause → Rule → Evidence**.