@keemakr/operator-sdk 0.9.0 → 0.10.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keemakr/operator-sdk",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "The keemakr connector contract: defineProvider + ConnectionProvider/ConnectionOperation types + OperationContext (governed fetch) + the connector manifest schema, shared by keemakr-core's built-in adapters and external connector repos. One shape, two consumers.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -110,11 +110,14 @@ export async function runConformance(
110
110
  // 9/10 — per-op access + idempotency match the reviewed declarations.
111
111
  const accessMismatch: string[] = [];
112
112
  const idemMismatch: string[] = [];
113
+ const decisionMismatch: string[] = [];
113
114
  for (const decl of manifest.operations) {
114
115
  const op = provider.operations?.[decl.name];
115
116
  if (!op) continue;
116
117
  if ((op.access ?? 'write') !== decl.readWrite) accessMismatch.push(decl.name);
117
118
  if ((op.idempotent ?? false) !== decl.idempotent) idemMismatch.push(decl.name);
119
+ if ((op.decisionOnly ?? false) !== (decl.decision_only ?? false))
120
+ decisionMismatch.push(decl.name);
118
121
  }
119
122
  add(
120
123
  'ops-access',
@@ -128,6 +131,14 @@ export async function runConformance(
128
131
  idemMismatch.length === 0,
129
132
  idemMismatch.length ? idemMismatch.join(', ') : undefined,
130
133
  );
134
+ // Core reads the flag from the manifest for a remote or MCP connector and from the code for a
135
+ // vendored one, so a connector whose two disagree is a person's decision on one lane only.
136
+ add(
137
+ 'ops-decision-only',
138
+ 'op decision-only flags match the manifest',
139
+ decisionMismatch.length === 0,
140
+ decisionMismatch.length ? decisionMismatch.join(', ') : undefined,
141
+ );
131
142
 
132
143
  // 11/12 — schemas parse safely; call signatures accept (cred, args).
133
144
  const badSchema: string[] = [];
package/src/index.ts CHANGED
@@ -134,6 +134,14 @@ export interface OperationContext {
134
134
  // needs attribution must treat an absent actor the same as an unattended one and refuse, rather
135
135
  // than falling back to an args value. Frozen: read it, never write it.
136
136
  actor?: Readonly<OperationActor>;
137
+ // A key for this one write, derived by the platform from the caller's Idempotency-Key, when the
138
+ // caller sent one. The same retried call carries the same key; another call never does. Pass it
139
+ // to a provider that accepts its own idempotency key (Stripe's `Idempotency-Key`, for example)
140
+ // so a retry the platform cannot see, such as your own retry after a timeout, is still applied
141
+ // once. It is not the caller's raw key: a value derived per tenant and install, safe to send to
142
+ // the provider. Absent on reads, on calls made without a key, and outside an in-process
143
+ // connector's ctx.
144
+ idempotencyKey?: string;
137
145
  }
138
146
 
139
147
  // Slim context for the lifecycle hooks (buildAuthUrl/exchangeCode/refresh, and
@@ -325,6 +333,19 @@ export interface ConnectionOperation {
325
333
  // Safe-to-retry marker. Omitted ⇒ false. The platform NEVER auto-retries a
326
334
  // non-idempotent operation; callers (agents) read this to know what is safe.
327
335
  idempotent?: boolean;
336
+ // How long this one operation may run, in milliseconds, end to end. Omitted ⇒ the platform's
337
+ // default (30 s). The platform caps it at 120 s: work that needs longer must not hold a request
338
+ // open. When the caller's own deadline is nearer, the platform uses that instead, so a call
339
+ // never outlives the request that asked for it. A write that runs out of time answers
340
+ // `retry: 'unknown'`: it may have reached the provider. Read for in-process operations (built-in
341
+ // and local-runtime connectors); a remote or MCP runtime keeps its own clock for now.
342
+ timeoutMs?: number;
343
+ // A decision a person makes (approve a task, start a paid render). The platform runs it only
344
+ // from a person's approval of these exact arguments (an Approvals card), and names that approver
345
+ // to the connector as `ctx.actor` with `via: 'approval'`; a direct call from an agent, a routine,
346
+ // chat or the MCP server is refused once the company's gate enforces. Declare it on a write.
347
+ // Omitted ⇒ false. Mirror it in the manifest as `decision_only: true`.
348
+ decisionOnly?: boolean;
328
349
  // The fields of the RESULT that are files a person should see: generated
329
350
  // images, synthesized audio. `path` names them with dots and `[]` for every
330
351
  // item of a list, e.g. `images[].url` or `audio.url`. When the Operator calls
package/src/manifest.ts CHANGED
@@ -124,6 +124,10 @@ export const connectorOperationDeclaration = z.object({
124
124
  readWrite: z.enum(['read', 'write']),
125
125
  idempotent: z.boolean(),
126
126
  description: z.string().max(500).optional(),
127
+ // A decision a person makes (approve a task, start a paid render): core runs it only from a
128
+ // person's approval of the exact call and refuses every other caller once the company's gate
129
+ // enforces. Optional for the same reason as input_schema below.
130
+ decision_only: z.boolean().optional(),
127
131
  // OPTIONAL, and it must stay optional: stored manifests of already-published versions are
128
132
  // re-parsed with the current schema at every publish, and they do not carry it.
129
133
  input_schema: operationInputJsonSchema.optional(),
@@ -168,6 +172,16 @@ const pushAccountOp = z.object({
168
172
  account_ref_arg: z.string().min(1).max(100),
169
173
  });
170
174
 
175
+ /**
176
+ * The subscribe op of a scheme signed per account (`hmac_sha256`) may also name the argument core
177
+ * hands the account's signing secret in. Core mints that secret for the workspace's account and
178
+ * fills the argument itself (a value the caller sends there is dropped), so the connector can give
179
+ * it to its provider; the door then verifies that account's deliveries with it.
180
+ */
181
+ const pushRegisterOp = pushAccountOp.extend({
182
+ secret_arg: z.string().min(1).max(100).optional(),
183
+ });
184
+
171
185
  /**
172
186
  * PUSHED UPDATES: the provider calls core when something happens, instead of core asking. Core
173
187
  * owns the door (`/api/webhooks/connections/<provider>`): it checks the provider's signature with
@@ -176,7 +190,13 @@ const pushAccountOp = z.object({
176
190
  *
177
191
  * `scheme` is the provider's protocol, implemented once in core. `websub_graph` is the WebSub
178
192
  * verification handshake (`hub.challenge`) plus an `X-Hub-Signature-256` HMAC of the raw body and
179
- * the `{ object, entry: [{ id, changes: [{ field, value }] }] }` envelope.
193
+ * the `{ object, entry: [{ id, changes: [{ field, value }] }] }` envelope. `hmac_sha256` is a
194
+ * generic signed callback with no handshake: an `X-Kee-Signature: t=<unix seconds>,v1=<hex>`
195
+ * header, the HMAC-SHA256 of `<t>.<raw body>`, refused outside a five-minute window, and the
196
+ * `{ account, events: [{ id, object, field, value }] }` envelope, one account per delivery. An
197
+ * event's `id` is the provider's own: a resend of the same id for the same account is one event.
198
+ * Its secret is the connector's config secret (`secret_key`) or, once core signs per account, the
199
+ * one core minted for that account and handed to `register.secret_arg`.
180
200
  *
181
201
  * `fields` is the REVIEWED allowlist of what may come through: an update whose (object, field) is
182
202
  * not listed is dropped at the door. What a listed field carries reaches a handler under the
@@ -194,9 +214,13 @@ const pushAccountOp = z.object({
194
214
  */
195
215
  export const connectorPushDeclaration = z
196
216
  .object({
197
- scheme: z.enum(['websub_graph']),
217
+ scheme: z.enum(['websub_graph', 'hmac_sha256']),
198
218
  secret_key: z.string().regex(/^[A-Z][A-Z0-9_]*$/, 'config keys are SCREAMING_SNAKE'),
199
- verify_token_key: z.string().regex(/^[A-Z][A-Z0-9_]*$/, 'config keys are SCREAMING_SNAKE'),
219
+ // `websub_graph` only: the token of the provider's handshake. `hmac_sha256` has no handshake.
220
+ verify_token_key: z
221
+ .string()
222
+ .regex(/^[A-Z][A-Z0-9_]*$/, 'config keys are SCREAMING_SNAKE')
223
+ .optional(),
200
224
  fields: z
201
225
  .array(
202
226
  z.object({
@@ -206,13 +230,35 @@ export const connectorPushDeclaration = z
206
230
  )
207
231
  .min(1, 'declare at least one (object, field) the provider may push')
208
232
  .max(50),
209
- register: pushAccountOp,
233
+ register: pushRegisterOp,
210
234
  unregister: pushAccountOp.optional(),
211
235
  })
212
236
  .superRefine((p, ctx) => {
237
+ if (p.scheme === 'websub_graph' && !p.verify_token_key) {
238
+ ctx.addIssue({
239
+ code: 'custom',
240
+ path: ['verify_token_key'],
241
+ message: 'websub_graph needs verify_token_key: the provider verifies the address with it',
242
+ });
243
+ }
244
+ if (p.register.secret_arg !== undefined) {
245
+ if (p.scheme !== 'hmac_sha256') {
246
+ ctx.addIssue({
247
+ code: 'custom',
248
+ path: ['register', 'secret_arg'],
249
+ message: 'register.secret_arg is only for hmac_sha256: its provider signs per account',
250
+ });
251
+ } else if (p.register.secret_arg === p.register.account_ref_arg) {
252
+ ctx.addIssue({
253
+ code: 'custom',
254
+ path: ['register', 'secret_arg'],
255
+ message: 'register.secret_arg must be a different argument from account_ref_arg',
256
+ });
257
+ }
258
+ }
213
259
  // The verify token is sent in the clear (a URL query) during the provider's handshake; the secret
214
260
  // signs every delivery for every customer. One key for both would put the secret in access logs.
215
- if (p.secret_key === p.verify_token_key) {
261
+ if (p.verify_token_key !== undefined && p.secret_key === p.verify_token_key) {
216
262
  ctx.addIssue({
217
263
  code: 'custom',
218
264
  path: ['verify_token_key'],
@@ -242,17 +288,25 @@ export const connectorPushDeclaration = z
242
288
 
243
289
  /**
244
290
  * What a push declaration must agree on with the rest of its manifest: the secret and the verify
245
- * token are declared config keys (staff can only set a declared key), and the register and
246
- * unregister operations are declared operations. Paths are relative to `push`. Pure. Core runs it
247
- * again when it reads a pinned manifest, so a block that no longer agrees opens no door.
291
+ * token are declared config keys (staff can only set a declared key), the register and unregister
292
+ * operations are declared operations, and `register.secret_arg` is an argument the register
293
+ * operation declares but does not require (a caller never has it to send: core fills it). Paths are
294
+ * relative to `push`. Pure. Core runs it again when it reads a pinned manifest, so a block that no
295
+ * longer agrees opens no door.
248
296
  */
249
297
  export function pushCrossCheckIssues(
250
298
  push: ConnectorPushDeclaration,
251
- manifest: { env: readonly string[]; operations: ReadonlyArray<{ name: string }> },
299
+ manifest: {
300
+ env: readonly string[];
301
+ operations: ReadonlyArray<{
302
+ name: string;
303
+ input_schema?: { properties?: Record<string, unknown>; required?: readonly string[] };
304
+ }>;
305
+ },
252
306
  ): Array<{ path: (string | number)[]; message: string }> {
253
307
  const out: Array<{ path: (string | number)[]; message: string }> = [];
254
308
  for (const key of ['secret_key', 'verify_token_key'] as const) {
255
- if (!manifest.env.includes(push[key])) {
309
+ if (push[key] !== undefined && !manifest.env.includes(push[key])) {
256
310
  out.push({
257
311
  path: [key],
258
312
  message: `push.${key} '${push[key]}' must be one of the config keys declared in env`,
@@ -269,6 +323,33 @@ export function pushCrossCheckIssues(
269
323
  });
270
324
  }
271
325
  }
326
+ const secretArg = push.register.secret_arg;
327
+ if (secretArg !== undefined) {
328
+ const schema = manifest.operations.find((o) => o.name === push.register.op)?.input_schema;
329
+ // Read defensively: core runs this on a stored manifest it reads back as plain JSON.
330
+ const properties = schema?.properties;
331
+ if (!properties || typeof properties !== 'object' || !Object.hasOwn(properties, secretArg)) {
332
+ out.push({
333
+ path: ['register', 'secret_arg'],
334
+ message: `push.register.secret_arg '${secretArg}' must be one of the register operation's input_schema properties`,
335
+ });
336
+ } else if (Array.isArray(schema?.required) && schema.required.includes(secretArg)) {
337
+ out.push({
338
+ path: ['register', 'secret_arg'],
339
+ message: `push.register.secret_arg '${secretArg}' must not be required: core fills it, a caller never sends it`,
340
+ });
341
+ } else {
342
+ // Core sends the secret as a string: a property of another type fails every subscribe.
343
+ const declared = (properties as Record<string, { type?: unknown } | undefined>)[secretArg]
344
+ ?.type;
345
+ if (declared !== 'string' && !(Array.isArray(declared) && declared.includes('string'))) {
346
+ out.push({
347
+ path: ['register', 'secret_arg'],
348
+ message: `push.register.secret_arg '${secretArg}' must be declared type 'string': core sends the secret as one`,
349
+ });
350
+ }
351
+ }
352
+ }
272
353
  return out;
273
354
  }
274
355
 
@@ -384,6 +465,16 @@ export const connectorManifestSchema = z
384
465
  });
385
466
  }
386
467
  m.operations.forEach((op, i) => {
468
+ // A person's decision runs only from an Approvals card, and only a write is held for one: a
469
+ // decision-only read would be refused on every lane once a company enforces (WS8-S18).
470
+ if (op.decision_only === true && op.readWrite !== 'write') {
471
+ ctx.addIssue({
472
+ code: 'custom',
473
+ path: ['operations', i, 'decision_only'],
474
+ message:
475
+ 'decision_only is only for an operation with readWrite: "write" — a person approves a write, and a read is never held for approval',
476
+ });
477
+ }
387
478
  if (!op.text_chunks) return;
388
479
  const path = ['operations', i, 'text_chunks'];
389
480
  // Core sends a file's parts under one approval and a retry resends every part from 0, which