@toon-protocol/rig 3.6.0 → 4.1.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 (37) hide show
  1. package/README.md +112 -153
  2. package/dist/{chunk-HJDPM37T.js → chunk-7VCVR4GK.js} +12 -8
  3. package/dist/{chunk-HJDPM37T.js.map → chunk-7VCVR4GK.js.map} +1 -1
  4. package/dist/chunk-CDSFVZDV.js +290 -0
  5. package/dist/chunk-CDSFVZDV.js.map +1 -0
  6. package/dist/{chunk-B5ISARMU.js → chunk-GVXBUL3C.js} +15 -2
  7. package/dist/chunk-GVXBUL3C.js.map +1 -0
  8. package/dist/chunk-LTFMPOHU.js +194 -0
  9. package/dist/chunk-LTFMPOHU.js.map +1 -0
  10. package/dist/{chunk-2KJJ7LSO.js → chunk-NCEMQUPA.js} +40 -2
  11. package/dist/chunk-NCEMQUPA.js.map +1 -0
  12. package/dist/{chunk-LCYCKWAT.js → chunk-YZRR2UO6.js} +86 -5
  13. package/dist/chunk-YZRR2UO6.js.map +1 -0
  14. package/dist/cli/rig.js +631 -568
  15. package/dist/cli/rig.js.map +1 -1
  16. package/dist/index.d.ts +10 -115
  17. package/dist/index.js +16 -122
  18. package/dist/index.js.map +1 -1
  19. package/dist/{publisher-BFcnZeJI.d.ts → publisher-DMJ06cuL.d.ts} +98 -2
  20. package/dist/standalone/index.d.ts +241 -438
  21. package/dist/standalone/index.js +19 -11
  22. package/dist/standalone-mode-KTYRTUS6.js +390 -0
  23. package/dist/standalone-mode-KTYRTUS6.js.map +1 -0
  24. package/package.json +7 -5
  25. package/dist/chunk-2KJJ7LSO.js.map +0 -1
  26. package/dist/chunk-B5ISARMU.js.map +0 -1
  27. package/dist/chunk-IZ6KLYQ6.js +0 -724
  28. package/dist/chunk-IZ6KLYQ6.js.map +0 -1
  29. package/dist/chunk-LCYCKWAT.js.map +0 -1
  30. package/dist/chunk-PWNZWQOH.js +0 -404
  31. package/dist/chunk-PWNZWQOH.js.map +0 -1
  32. package/dist/chunk-UZZ2RN7A.js +0 -469
  33. package/dist/chunk-UZZ2RN7A.js.map +0 -1
  34. package/dist/network-bootstrap-27FYE3N7.js +0 -40
  35. package/dist/network-bootstrap-27FYE3N7.js.map +0 -1
  36. package/dist/standalone-mode-5MNNJC2D.js +0 -928
  37. package/dist/standalone-mode-5MNNJC2D.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { c as GitObjectType, P as Publisher, F as FeeRates, b as PublishReceipt, U as UnsignedEvent } from './publisher-BFcnZeJI.js';
2
- export { C as COMMENT_KIND, d as GitObject, G as GitObjectUpload, M as MAINTAINERS_TAG, e as MAX_OBJECT_SIZE, R as REPOSITORY_STATE_KIND, S as StatusKind, a as UploadReceipt, f as authorizedStatusAuthors, g as buildComment, h as buildIssue, i as buildPatch, j as buildRepoAnnouncement, k as buildRepoRefs, l as buildStatus, m as createGitBlob, n as createGitCommit, o as createGitTag, p as createGitTree, q as hashGitObject, r as parseMaintainers } from './publisher-BFcnZeJI.js';
3
- import { JobHandler, IncrementOfferTags } from '@toon-protocol/client';
1
+ import { d as GitObjectType, e as PayoutPointer, P as Publisher, F as FeeRates, c as PublishReceipt, b as UnsignedEvent } from './publisher-DMJ06cuL.js';
2
+ export { C as COMMENT_KIND, f as GitObject, G as GitObjectUpload, M as MAINTAINERS_TAG, g as MAX_OBJECT_SIZE, h as PAYOUT_TAG, i as PayoutChain, R as REPOSITORY_STATE_KIND, j as StatusKind, U as UploadReceipt, k as authorizedStatusAuthors, l as buildComment, m as buildIssue, n as buildPatch, o as buildRepoAnnouncement, p as buildRepoRefs, q as buildStatus, r as createGitBlob, s as createGitCommit, t as createGitTag, u as createGitTree, v as hashGitObject, w as isValidEvmPayoutAddress, x as parseMaintainers, y as parsePayout } from './publisher-DMJ06cuL.js';
4
3
  import '@toon-protocol/core/nip34';
5
4
 
6
5
  /**
@@ -211,6 +210,7 @@ declare class GitRepoReader {
211
210
  * double-JSON-encoded string (devnet relay quirk: a JSON string containing
212
211
  * the event JSON), or as a TOON-encoded string. All three are tolerated.
213
212
  */
213
+
214
214
  /** A signed Nostr event as seen on a relay (read side). */
215
215
  interface NostrEvent {
216
216
  id: string;
@@ -293,6 +293,12 @@ interface RemoteState {
293
293
  * when unannounced or owner-only.
294
294
  */
295
295
  maintainers: string[];
296
+ /**
297
+ * Declared payout pointer (rig#92) from the announcement `payout` tag, or
298
+ * `null` when unannounced or no pointer is declared — no pointer means the
299
+ * serving node keeps 100% of repo-scoped write fees (toon-meta#391).
300
+ */
301
+ payout: PayoutPointer | null;
296
302
  /**
297
303
  * Resolve SHAs to Arweave txIds: served from the `arweave` tag map when
298
304
  * present, otherwise via the GraphQL Git-SHA resolver. SHAs that resolve
@@ -1409,115 +1415,4 @@ interface FactoryJobExecution {
1409
1415
  */
1410
1416
  declare function executeFactoryJob(options: ExecuteFactoryJobOptions): Promise<FactoryJobExecution>;
1411
1417
 
1412
- /**
1413
- * ClientJobDeliveryPort — the concrete `JobDeliveryPort` (#52, #56) backed
1414
- * by the real, published `@toon-protocol/client` (>=0.26.0) hashlock
1415
- * helpers (toon-client#495) and serve-side job handling (toon-client#494).
1416
- *
1417
- * Per the issue's explicit instruction, this never re-derives the
1418
- * encrypt/condition relationship locally: `encryptArtifact` is the only
1419
- * place a key or condition is minted, and `fulfillIncrement` is the only
1420
- * place a key becomes an ILP fulfillment.
1421
- *
1422
- * Wiring: pass `handleJob` as `ToonClientConfig.jobHandler` for the SAME
1423
- * identity that runs `executeFactoryJob` (job serving rides the provider's
1424
- * own BTP session, per that field's doc). The connector delivers the
1425
- * buyer's paying PREPARE as a server-originated BTP MESSAGE addressed to
1426
- * this client's own ILP address; `handleJob` reveals the key minted by the
1427
- * immediately-preceding `encryptArtifact` call as the fulfillment — which
1428
- * is simultaneously the payment settling and the decrypt-key handoff
1429
- * (docs/factory-job-protocol.md §4.2, toon-meta).
1430
- *
1431
- * One increment in flight at a time, matching `JobDeliveryPort`'s own
1432
- * sequential-use contract: `encryptArtifact` stages a key, and only
1433
- * `waitForPayment` for that SAME condition arms `handleJob` to spend it. A
1434
- * job PREPARE for any other condition — stray, replayed, or for an
1435
- * increment this port never staged — is refused rather than fulfilled.
1436
- */
1437
-
1438
- interface ClientJobDeliveryPortOptions {
1439
- /** How long to wait for the buyer's paying PREPARE before giving up on an increment. Default 120s. */
1440
- paymentTimeoutMs?: number;
1441
- }
1442
- declare class ClientJobDeliveryPort implements JobDeliveryPort {
1443
- private readonly paymentTimeoutMs;
1444
- private staged?;
1445
- private armed?;
1446
- constructor(options?: ClientJobDeliveryPortOptions);
1447
- /**
1448
- * The `JobHandler` to register as `ToonClientConfig.jobHandler`. Throws
1449
- * (answered F99 by `createJobMessageHandler`) when no increment is armed
1450
- * for the PREPARE's `executionCondition`, or when the PREPARE's `amount`
1451
- * is below the armed increment's advertised price (#87 — the condition is
1452
- * public, published on the kind:7000 offer, so anyone can send a PREPARE
1453
- * carrying it; only a PREPARE that actually pays the armed price may
1454
- * release the key). An underpaying PREPARE is rejected without consuming
1455
- * the arming, so a correctly-priced PREPARE can still land before the
1456
- * payment timeout. This is the only path that releases a key, and it
1457
- * releases exactly the one the buyer paid for.
1458
- */
1459
- readonly handleJob: JobHandler;
1460
- encryptArtifact(bytes: Uint8Array): Promise<EncryptedArtifact>;
1461
- waitForPayment(offer: OfferedIncrement): Promise<boolean>;
1462
- }
1463
-
1464
- /**
1465
- * Buyer-side payment for one factory-job increment offer (#56) — the other
1466
- * half of the hashlock join `ClientJobDeliveryPort` (the provider side)
1467
- * answers. Built against `@toon-protocol/client`'s real `buildIncrementPrepare`
1468
- * / `decryptArtifact` (toon-client#495): this module does not re-derive the
1469
- * PREPARE fields or the AEAD decrypt itself.
1470
- *
1471
- * Per docs/factory-job-protocol.md §4.2 (toon-meta), paying an offer is:
1472
- * `executionCondition` equal to the offer's `condition` tag byte for byte,
1473
- * and `data` carrying a reference back to the offer's own kind:7000 event id
1474
- * — "the payment names the job." The provider returns the decryption key as
1475
- * the ILP fulfillment; revealing it is the same act as accepting payment.
1476
- */
1477
-
1478
- /** What a buyer reads off an advertised kind:7000 `status:"partial"` increment offer. */
1479
- interface PayableIncrementOffer extends IncrementOfferTags {
1480
- /** The offer event's own id — carried in the PREPARE's `data` field (§4.2). */
1481
- offerEventId: string;
1482
- }
1483
- /** The outcome of paying one increment offer. */
1484
- type PaidIncrementOffer = {
1485
- paid: true;
1486
- key: Uint8Array;
1487
- } | {
1488
- paid: false;
1489
- };
1490
- /** The narrow slice of `ToonClient` a buyer needs to pay an increment offer. */
1491
- interface SwapPacketSender {
1492
- sendSwapPacket(params: {
1493
- destination: string;
1494
- amount: bigint;
1495
- toonData: Uint8Array;
1496
- executionCondition?: Uint8Array;
1497
- timeout?: number;
1498
- }): Promise<{
1499
- accepted: boolean;
1500
- fulfillment?: string;
1501
- code?: string;
1502
- message?: string;
1503
- }>;
1504
- }
1505
- /**
1506
- * Buyer: pay one advertised increment offer. Sends an ILP PREPARE addressed
1507
- * to `providerDestination` with `executionCondition` equal to the offer's
1508
- * `condition` tag and `data` referencing the offer's event id, per §4.2.
1509
- * Resolves `{ paid: false }` on a REJECT (expired/no such increment/wrong
1510
- * fulfillment) rather than throwing — a buyer declining or failing to pay is
1511
- * a protocol outcome (`abandoned-buyer`), not an error.
1512
- */
1513
- declare function payIncrementOffer(client: SwapPacketSender, providerDestination: string, offer: PayableIncrementOffer): Promise<PaidIncrementOffer>;
1514
- /**
1515
- * Buyer: decrypt an increment's artifact using the key revealed as the ILP
1516
- * fulfillment. `offerConditionHex` MUST be the offer's own `condition` tag
1517
- * as read from the relay before payment — never re-derived from `key` —
1518
- * so a provider who reveals a different key than it advertised is caught
1519
- * here rather than trusted.
1520
- */
1521
- declare function decryptIncrementArtifact(ciphertext: Uint8Array, key: Uint8Array, offerConditionHex: string): Uint8Array;
1522
-
1523
- export { type BuildIncrementOfferOptions, type BuildNarrationOptions, type BuildResultEventOptions, ClientJobDeliveryPort, type ClientJobDeliveryPortOptions, type ClosureResult, type CollectRepoObjectsOptions, type CollectRepoObjectsResult, DEFAULT_CONCURRENCY, type DownloadOptions, type DownloadResult, type EncryptedArtifact, type EncryptedArtifactRef, type ExecuteFactoryJobOptions, type ExecutePushOptions, FACTORY_JOB_FEEDBACK_KIND, FACTORY_JOB_REQUEST_KIND, FACTORY_JOB_RESULT_KIND, type FactoryJobExecution, type FactoryJobHooks, type FactoryJobOutcome, type FactoryJobRequest, type FactoryJobWork, type FactoryMilestone, type FactoryTicket, FeeRates, type FetchLike, type FetchRemoteStateOptions, type FetchedObject, type GateCheck, type GateResult, type GatewayFetchOptions, type GitCommentRequest, GitError, type GitErrorEnvelope, type GitEstimateRequest, type GitEstimateResponse, type GitEventResponse, type GitFeeEstimate, type GitIssueRequest, GitObjectType, type GitPatchRequest, type GitPlannedObject, type GitPublishReceipt, type GitPushRequest, type GitPushResponse, type GitRef, type GitRefUpdate, type GitRepoAddr, GitRepoReader, type GitStatusRequest, type GitStatusValue, type GitUploadStep, type IncrementSpec, type JobDeliveryPort, type MissingObject, NonFastForwardError, type NostrEvent, type NostrFilter, ObjectIntegrityError, type ObjectStat, type ObjectWithPath, ObjectWriteMismatchError, type OfferedIncrement, type OversizeObject, OversizeObjectsError, type PaidIncrementOffer, type PayableIncrementOffer, type PlanPushOptions, type PlannedObject, PublishReceipt, Publisher, type PushFeeEstimate, type PushPlan, type PushResult, type ReadGitObject, type ReadObjectsResult, type RefUpdate, type RefUpdateKind, type RejectedRefUpdate, type RelayEvent, type RemoteState, type RepoRefs, type StatObjectsResult, type SwapPacketSender, UnsignedEvent, type UploadStepResult, type WebSocketFactory, type WebSocketLike, buildIncrementOfferEvent, buildNarrationEvent, buildQuoteEvent, buildResultEvent, collectRepoObjects, decryptIncrementArtifact, downloadGitObjects, executeFactoryJob, executePush, fetchRemoteState, fetchTxBytes, gatePassed, hexToNpub, isSafeRefname, missingObjectsMessage, npubToHex, ownerToHex, parseFactoryJobRequest, payIncrementOffer, planFactoryJob, planPush, queryRelay, referencedShas, serializeEventReceipt, serializeFeeEstimate, serializePushPlan, serializePushResult, setHeadSymref, updateRef, verifyObjectBody, walkClosure, writeGitObject, writeGitObjects };
1418
+ export { type BuildIncrementOfferOptions, type BuildNarrationOptions, type BuildResultEventOptions, type ClosureResult, type CollectRepoObjectsOptions, type CollectRepoObjectsResult, DEFAULT_CONCURRENCY, type DownloadOptions, type DownloadResult, type EncryptedArtifact, type EncryptedArtifactRef, type ExecuteFactoryJobOptions, type ExecutePushOptions, FACTORY_JOB_FEEDBACK_KIND, FACTORY_JOB_REQUEST_KIND, FACTORY_JOB_RESULT_KIND, type FactoryJobExecution, type FactoryJobHooks, type FactoryJobOutcome, type FactoryJobRequest, type FactoryJobWork, type FactoryMilestone, type FactoryTicket, FeeRates, type FetchLike, type FetchRemoteStateOptions, type FetchedObject, type GateCheck, type GateResult, type GatewayFetchOptions, type GitCommentRequest, GitError, type GitErrorEnvelope, type GitEstimateRequest, type GitEstimateResponse, type GitEventResponse, type GitFeeEstimate, type GitIssueRequest, GitObjectType, type GitPatchRequest, type GitPlannedObject, type GitPublishReceipt, type GitPushRequest, type GitPushResponse, type GitRef, type GitRefUpdate, type GitRepoAddr, GitRepoReader, type GitStatusRequest, type GitStatusValue, type GitUploadStep, type IncrementSpec, type JobDeliveryPort, type MissingObject, NonFastForwardError, type NostrEvent, type NostrFilter, ObjectIntegrityError, type ObjectStat, type ObjectWithPath, ObjectWriteMismatchError, type OfferedIncrement, type OversizeObject, OversizeObjectsError, PayoutPointer, type PlanPushOptions, type PlannedObject, PublishReceipt, Publisher, type PushFeeEstimate, type PushPlan, type PushResult, type ReadGitObject, type ReadObjectsResult, type RefUpdate, type RefUpdateKind, type RejectedRefUpdate, type RelayEvent, type RemoteState, type RepoRefs, type StatObjectsResult, UnsignedEvent, type UploadStepResult, type WebSocketFactory, type WebSocketLike, buildIncrementOfferEvent, buildNarrationEvent, buildQuoteEvent, buildResultEvent, collectRepoObjects, downloadGitObjects, executeFactoryJob, executePush, fetchRemoteState, fetchTxBytes, gatePassed, hexToNpub, isSafeRefname, missingObjectsMessage, npubToHex, ownerToHex, parseFactoryJobRequest, planFactoryJob, planPush, queryRelay, referencedShas, serializeEventReceipt, serializeFeeEstimate, serializePushPlan, serializePushResult, setHeadSymref, updateRef, verifyObjectBody, walkClosure, writeGitObject, writeGitObjects };
package/dist/index.js CHANGED
@@ -27,18 +27,11 @@ import {
27
27
  walkClosure,
28
28
  writeGitObject,
29
29
  writeGitObjects
30
- } from "./chunk-HJDPM37T.js";
31
- import {
32
- MAX_OBJECT_SIZE,
33
- createGitBlob,
34
- createGitCommit,
35
- createGitTag,
36
- createGitTree,
37
- hashGitObject
38
- } from "./chunk-B5ISARMU.js";
30
+ } from "./chunk-7VCVR4GK.js";
39
31
  import {
40
32
  COMMENT_KIND,
41
33
  MAINTAINERS_TAG,
34
+ PAYOUT_TAG,
42
35
  REPOSITORY_STATE_KIND,
43
36
  authorizedStatusAuthors,
44
37
  buildComment,
@@ -48,9 +41,19 @@ import {
48
41
  buildRepoRefs,
49
42
  buildStatus,
50
43
  fetchRemoteState,
44
+ isValidEvmPayoutAddress,
51
45
  parseMaintainers,
46
+ parsePayout,
52
47
  queryRelay
53
- } from "./chunk-2KJJ7LSO.js";
48
+ } from "./chunk-NCEMQUPA.js";
49
+ import {
50
+ MAX_OBJECT_SIZE,
51
+ createGitBlob,
52
+ createGitCommit,
53
+ createGitTag,
54
+ createGitTree,
55
+ hashGitObject
56
+ } from "./chunk-GVXBUL3C.js";
54
57
 
55
58
  // src/factory-job-gate.ts
56
59
  function gatePassed(gate) {
@@ -325,118 +328,8 @@ async function executeFactoryJob(options) {
325
328
  }
326
329
  return haltWith("completed");
327
330
  }
328
-
329
- // src/factory-job-delivery-client.ts
330
- import { createHash } from "crypto";
331
- import {
332
- encryptArtifact as clientEncryptArtifact,
333
- fulfillIncrement
334
- } from "@toon-protocol/client";
335
- function toHex(bytes) {
336
- return Buffer.from(bytes).toString("hex");
337
- }
338
- var DEFAULT_PAYMENT_TIMEOUT_MS = 12e4;
339
- var ClientJobDeliveryPort = class {
340
- paymentTimeoutMs;
341
- staged;
342
- armed;
343
- constructor(options = {}) {
344
- this.paymentTimeoutMs = options.paymentTimeoutMs ?? DEFAULT_PAYMENT_TIMEOUT_MS;
345
- }
346
- /**
347
- * The `JobHandler` to register as `ToonClientConfig.jobHandler`. Throws
348
- * (answered F99 by `createJobMessageHandler`) when no increment is armed
349
- * for the PREPARE's `executionCondition`, or when the PREPARE's `amount`
350
- * is below the armed increment's advertised price (#87 — the condition is
351
- * public, published on the kind:7000 offer, so anyone can send a PREPARE
352
- * carrying it; only a PREPARE that actually pays the armed price may
353
- * release the key). An underpaying PREPARE is rejected without consuming
354
- * the arming, so a correctly-priced PREPARE can still land before the
355
- * payment timeout. This is the only path that releases a key, and it
356
- * releases exactly the one the buyer paid for.
357
- */
358
- handleJob = (job) => {
359
- const conditionHex = toHex(job.executionCondition);
360
- if (!this.armed || conditionHex !== this.armed.conditionHex) {
361
- throw new Error(
362
- `no factory-job increment is awaiting payment for condition ${conditionHex}`
363
- );
364
- }
365
- if (job.amount < BigInt(this.armed.priceUsdc)) {
366
- throw new Error(
367
- `PREPARE amount ${job.amount} is below the armed price ${this.armed.priceUsdc} for condition ${conditionHex}`
368
- );
369
- }
370
- const { key, resolve } = this.armed;
371
- this.armed = void 0;
372
- resolve(true);
373
- return { fulfillment: fulfillIncrement(key) };
374
- };
375
- async encryptArtifact(bytes) {
376
- const encrypted = clientEncryptArtifact(bytes);
377
- const conditionHex = toHex(encrypted.condition);
378
- this.staged = { key: encrypted.key, conditionHex };
379
- return {
380
- ciphertext: encrypted.ciphertext,
381
- ciphertextSha256: createHash("sha256").update(encrypted.ciphertext).digest("hex"),
382
- conditionHex
383
- };
384
- }
385
- async waitForPayment(offer) {
386
- if (!this.staged || offer.conditionHex !== this.staged.conditionHex) {
387
- throw new Error(
388
- "waitForPayment was called for a condition that does not match the most recent encryptArtifact() call"
389
- );
390
- }
391
- const { key, conditionHex } = this.staged;
392
- const { priceUsdc } = offer;
393
- this.staged = void 0;
394
- return new Promise((resolve) => {
395
- const timer = setTimeout(() => {
396
- this.armed = void 0;
397
- resolve(false);
398
- }, this.paymentTimeoutMs);
399
- this.armed = {
400
- key,
401
- conditionHex,
402
- priceUsdc,
403
- resolve: (paid) => {
404
- clearTimeout(timer);
405
- resolve(paid);
406
- }
407
- };
408
- });
409
- }
410
- };
411
-
412
- // src/factory-job-pay.ts
413
- import {
414
- buildIncrementPrepare,
415
- decryptArtifact
416
- } from "@toon-protocol/client";
417
- async function payIncrementOffer(client, providerDestination, offer) {
418
- const { executionCondition, amount } = buildIncrementPrepare(offer);
419
- const result = await client.sendSwapPacket({
420
- destination: providerDestination,
421
- amount,
422
- toonData: new TextEncoder().encode(offer.offerEventId),
423
- executionCondition
424
- });
425
- if (!result.accepted || !result.fulfillment) {
426
- return { paid: false };
427
- }
428
- return { paid: true, key: Buffer.from(result.fulfillment, "base64") };
429
- }
430
- function decryptIncrementArtifact(ciphertext, key, offerConditionHex) {
431
- return decryptArtifact(
432
- ciphertext,
433
- key,
434
- Buffer.from(offerConditionHex, "hex")
435
- );
436
- }
437
331
  export {
438
332
  COMMENT_KIND,
439
- ClientJobDeliveryPort,
440
333
  DEFAULT_CONCURRENCY,
441
334
  FACTORY_JOB_FEEDBACK_KIND,
442
335
  FACTORY_JOB_REQUEST_KIND,
@@ -449,6 +342,7 @@ export {
449
342
  ObjectIntegrityError,
450
343
  ObjectWriteMismatchError,
451
344
  OversizeObjectsError,
345
+ PAYOUT_TAG,
452
346
  REPOSITORY_STATE_KIND,
453
347
  authorizedStatusAuthors,
454
348
  buildComment,
@@ -466,7 +360,6 @@ export {
466
360
  createGitCommit,
467
361
  createGitTag,
468
362
  createGitTree,
469
- decryptIncrementArtifact,
470
363
  downloadGitObjects,
471
364
  executeFactoryJob,
472
365
  executePush,
@@ -476,12 +369,13 @@ export {
476
369
  hashGitObject,
477
370
  hexToNpub,
478
371
  isSafeRefname,
372
+ isValidEvmPayoutAddress,
479
373
  missingObjectsMessage,
480
374
  npubToHex,
481
375
  ownerToHex,
482
376
  parseFactoryJobRequest,
483
377
  parseMaintainers,
484
- payIncrementOffer,
378
+ parsePayout,
485
379
  planFactoryJob,
486
380
  planPush,
487
381
  queryRelay,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/factory-job-gate.ts","../src/factory-job-events.ts","../src/factory-job-plan.ts","../src/factory-job-execute.ts","../src/factory-job-delivery-client.ts","../src/factory-job-pay.ts"],"sourcesContent":["/**\n * Factory job protocol — reproducible gate result per increment (#53).\n *\n * Decision 5 of toon-meta#262 splits trust into three layers: delivery (the\n * hashlock), the objective floor (this — \"did this increment pass the\n * repo's own gate?\"), and quality (reputation, out of scope here). Per\n * `FACTORY.md`, the pinned pipeline's gate is lint/typecheck/test/build run\n * before a PR is opened; this module is the pure, network-free shape for\n * recording exactly what ran, not a summary verdict.\n *\n * This proves conformance, not fitness for purpose — a gate-passing\n * increment can still completely miss the brief. Reputation is what\n * distinguishes those, never this.\n */\n\n/** One command the gate ran, and whether it passed. */\nexport interface GateCheck {\n /** e.g. \"lint\", \"typecheck\", \"test\", \"build\" — whatever actually ran,\n * never a canonical list (gates differ per repo and drift over time). */\n name: string;\n /** The literal command, e.g. \"eslint .\", so a buyer can reproduce it verbatim. */\n command: string;\n pass: boolean;\n}\n\n/**\n * A reproducible gate result: enough for a buyer to re-run the gate\n * themselves and get the same answer. Omitting the commit or the command\n * set turns this from a reproducible result into a provider's unverifiable\n * claim of having done well — both are required, not advisory.\n */\nexport interface GateResult {\n /** The commit this gate ran against. */\n commit: string;\n /** Toolchain versions relevant to reproducing the result, e.g. `{ node: \"20.11.0\", pnpm: \"9.1.0\" }`. */\n toolchain: Record<string, string>;\n checks: GateCheck[];\n}\n\n/** Overall pass/fail — every recorded check passed. A failing gate is not a protocol violation (it is still published, just visibly failing) — this is only the aggregate the `gate` tag and reputation's gate-pass rate read. */\nexport function gatePassed(gate: GateResult): boolean {\n return gate.checks.every((check) => check.pass);\n}\n","/**\n * Factory job protocol — pure NIP-90 event builders/parser (#52).\n *\n * Wire format per toon-meta's `docs/factory-job-protocol.md`\n * (toon-meta#263), fixed 2026-08-03: kind:5097 job request, kind:6097 job\n * result, kind:7000 job feedback (quote / increment offer / narration,\n * disambiguated by the `status` tag). Mirrors nip34-events.ts: every builder\n * returns an UnsignedEvent — the caller signs and publishes (e.g. through a\n * Publisher, publisher.ts) elsewhere. No crypto, no network, no payment.\n */\n\nimport type { UnsignedEvent } from './nip34-events.js';\nimport type { IncrementSpec } from './factory-job-plan.js';\nimport { gatePassed, type GateResult } from './factory-job-gate.js';\n\n// ---------------------------------------------------------------------------\n// Kinds\n// ---------------------------------------------------------------------------\n\n/** Factory job request (the brief). */\nexport const FACTORY_JOB_REQUEST_KIND = 5097;\n/** Factory job result — terminal state (completed/abandoned-*). */\nexport const FACTORY_JOB_RESULT_KIND = 6097;\n/** Factory job feedback — quote, increment offer, or free narration. */\nexport const FACTORY_JOB_FEEDBACK_KIND = 7000;\n\n/** The three §5.2 terminal outcomes — decision 8 has no fourth. */\nexport type FactoryJobOutcome =\n | 'completed'\n | 'abandoned-provider'\n | 'abandoned-buyer';\n\n// ---------------------------------------------------------------------------\n// kind:5097 — job request\n// ---------------------------------------------------------------------------\n\n/** A parsed kind:5097 job request (§2). */\nexport interface FactoryJobRequest {\n /** The requesting kind:5097 event's id. */\n requestEventId: string;\n /** The requesting kind:5097 event's pubkey (who to reply/bill). */\n buyerPubkey: string;\n /** The brief — free text or a chained milestone reference (§2.3). */\n brief: string;\n /** Maximum the buyer will pay across the whole job (micro-USDC, §2.1). */\n bidMicroUsdc: string;\n /** `param repo` — target repo, e.g. `toon-protocol/buzz`. */\n repo?: string;\n /** `param target` — the ticket/issue this job resolves. */\n target?: string;\n /** `param constraints` — gate requirements / scope limits. */\n constraints?: string;\n /** `output` — expected deliverable MIME type. */\n outputMime?: string;\n}\n\n/** Minimal shape of a relay event this module reads/builds — no signing. */\nexport interface RelayEvent {\n id: string;\n pubkey: string;\n tags: string[][];\n content: string;\n}\n\n/**\n * Parse a kind:5097 job request event's tags into a {@link FactoryJobRequest}.\n * Throws if the required `i` (brief) or `bid` tag is missing — both are\n * REQUIRED per §2.1.\n */\nexport function parseFactoryJobRequest(event: RelayEvent): FactoryJobRequest {\n const briefTag = event.tags.find((t) => t[0] === 'i' && t[2] === 'text');\n if (!briefTag?.[1]) {\n throw new Error(\n `kind:${FACTORY_JOB_REQUEST_KIND} event ${event.id} is missing its required brief (\"i\", \"<brief>\", \"text\") tag`\n );\n }\n const bidTag = event.tags.find((t) => t[0] === 'bid');\n if (!bidTag?.[1]) {\n throw new Error(\n `kind:${FACTORY_JOB_REQUEST_KIND} event ${event.id} is missing its required \"bid\" tag`\n );\n }\n\n const params = event.tags.filter((t) => t[0] === 'param');\n const paramValue = (name: string): string | undefined =>\n params.find((t) => t[1] === name)?.[2];\n\n const repo = paramValue('repo');\n const target = paramValue('target');\n const constraints = paramValue('constraints');\n const outputMime = event.tags.find((t) => t[0] === 'output')?.[1];\n\n return {\n requestEventId: event.id,\n buyerPubkey: event.pubkey,\n brief: briefTag[1],\n bidMicroUsdc: bidTag[1],\n ...(repo !== undefined ? { repo } : {}),\n ...(target !== undefined ? { target } : {}),\n ...(constraints !== undefined ? { constraints } : {}),\n ...(outputMime !== undefined ? { outputMime } : {}),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:7000 status:\"quote\" — the RFQ reply (§3)\n// ---------------------------------------------------------------------------\n\n/**\n * Build the `kind:7000 status:\"quote\"` reply: the proposed increment\n * schedule, committing nothing (§3.3 — there is no accept message; payment\n * of increment 1 is acceptance).\n */\nexport function buildQuoteEvent(\n job: FactoryJobRequest,\n schedule: IncrementSpec[]\n): UnsignedEvent {\n return {\n kind: FACTORY_JOB_FEEDBACK_KIND,\n content: JSON.stringify({\n increments: schedule.map((s) => ({\n n: s.n,\n of: s.of,\n milestone: s.milestone,\n priceUsdc: s.priceUsdc,\n })),\n }),\n tags: [\n ['e', job.requestEventId, '', 'root'],\n ['p', job.buyerPubkey],\n ['status', 'quote'],\n ],\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:7000 status:\"partial\" — the increment offer (§4)\n// ---------------------------------------------------------------------------\n\n/** Where the encrypted increment artifact lives, and its hashlock condition. */\nexport interface EncryptedArtifactRef {\n /** Arweave tx id the ciphertext was uploaded to. */\n arweaveTxId: string;\n /** sha256 of the ciphertext, hex — lets the buyer detect a bad fetch. */\n ciphertextSha256: string;\n /** `sha256(key)` hex, where `key` decrypts the artifact (§4.2 — the join). */\n conditionHex: string;\n}\n\nexport interface BuildIncrementOfferOptions {\n job: FactoryJobRequest;\n /** The quote event's id for increment 1, else the previous offer's id. */\n parentEventId: string;\n increment: IncrementSpec;\n artifact: EncryptedArtifactRef;\n /**\n * The reproducible gate result for this increment's work (#53 — the\n * objective floor), when one ran. Omitted for milestones with nothing to\n * gate (e.g. `plan` — there is no code to lint against a brief).\n */\n gate?: GateResult;\n}\n\n/** A gate result missing its commit or check list cannot be reproduced by the buyer — reject it here rather than publish an unverifiable claim. */\nfunction assertReproducible(gate: GateResult): void {\n if (gate.checks.length === 0) {\n throw new Error(\n 'gate result must record at least one check for the offer to be reproducible'\n );\n }\n if (!gate.commit) {\n throw new Error(\n 'gate result must record the commit it ran against for the offer to be reproducible'\n );\n }\n}\n\n/**\n * Build the `kind:7000 status:\"partial\"` increment offer — published once\n * the provider has done that increment's work and uploaded the encrypted\n * artifact. This is the join between the relay plane and the connector\n * plane (§4.2): `condition` here MUST equal the paying PREPARE's\n * `executionCondition`, byte for byte.\n *\n * When `gate` is supplied, the offer also carries a `[\"gate\", \"pass\"|\"fail\"]`\n * tag (cheap to scan for a gate-pass rate, decision 8) and the full\n * reproducible result in `content` — visible to the buyer before they pay,\n * per the issue: a gate-failing increment is still offered, never hidden.\n */\nexport function buildIncrementOfferEvent(\n options: BuildIncrementOfferOptions\n): UnsignedEvent {\n const { job, parentEventId, increment, artifact, gate } = options;\n if (gate) assertReproducible(gate);\n\n const tags: string[][] = [\n ['e', job.requestEventId, '', 'root'],\n ['e', parentEventId, '', 'reply'],\n ['p', job.buyerPubkey],\n ['status', 'partial'],\n ['increment', String(increment.n), String(increment.of)],\n ['i', artifact.arweaveTxId, 'url'],\n ['i', artifact.ciphertextSha256, 'text', '', 'hash'],\n ['amount', increment.priceUsdc, 'usdc'],\n ['condition', artifact.conditionHex],\n ];\n if (gate) tags.push(['gate', gatePassed(gate) ? 'pass' : 'fail']);\n\n return {\n kind: FACTORY_JOB_FEEDBACK_KIND,\n content: gate ? JSON.stringify({ gate }) : '',\n tags,\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:7000 status:\"processing\" — free narration (§6)\n// ---------------------------------------------------------------------------\n\nexport interface BuildNarrationOptions {\n job: FactoryJobRequest;\n /** The prior event in the thread (quote, offer, or narration). */\n parentEventId: string;\n message: string;\n}\n\n/**\n * Build a free narration event — no artifact, no `amount`/`condition`, MUST\n * NOT be paid against (§6). This is what makes a provider look alive between\n * increments.\n */\nexport function buildNarrationEvent(\n options: BuildNarrationOptions\n): UnsignedEvent {\n const { job, parentEventId, message } = options;\n return {\n kind: FACTORY_JOB_FEEDBACK_KIND,\n content: message,\n tags: [\n ['e', job.requestEventId, '', 'root'],\n ['e', parentEventId, '', 'reply'],\n ['p', job.buyerPubkey],\n ['status', 'processing'],\n ],\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:6097 — job result (§5)\n// ---------------------------------------------------------------------------\n\nexport interface BuildResultEventOptions {\n job: FactoryJobRequest;\n /** The original kind:5097 request event, verbatim (the `request` tag). */\n requestEvent: RelayEvent;\n /** The last kind:7000 event in the thread (offer or narration). */\n lastEventId: string;\n outcome: FactoryJobOutcome;\n /** How far the job got — `n-reached == of` only when outcome is completed. */\n reachedIncrement: number;\n totalIncrements: number;\n /** Required (and only meaningful) when outcome is `\"completed\"` (§5.2). */\n finalArtifact?: { arweaveTxId: string };\n}\n\n/**\n * Build the terminal `kind:6097` job result. Reputation (decision 8) is\n * computed from exactly this event and nothing else.\n */\nexport function buildResultEvent(\n options: BuildResultEventOptions\n): UnsignedEvent {\n const {\n job,\n requestEvent,\n lastEventId,\n outcome,\n reachedIncrement,\n totalIncrements,\n finalArtifact,\n } = options;\n\n const tags: string[][] = [\n ['e', job.requestEventId, '', 'root'],\n ['e', lastEventId, '', 'reply'],\n ['p', job.buyerPubkey],\n ['request', JSON.stringify(requestEvent)],\n ['outcome', outcome],\n ['increment', String(reachedIncrement), String(totalIncrements)],\n ];\n if (outcome === 'completed' && finalArtifact) {\n tags.push(['i', finalArtifact.arweaveTxId, 'url']);\n }\n\n return {\n kind: FACTORY_JOB_RESULT_KIND,\n content: '',\n tags,\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n","/**\n * Factory job protocol — increment schedule planner (#52).\n *\n * `planFactoryJob` is the network-free half of the adapter (mirrors\n * `planPush`'s split from `executePush` in push.ts): given a job's `bid` and\n * the ticket list the factory's plan phase produced, it prices the\n * milestone boundaries decision 6 of toon-meta#262 fixed as increments —\n * plan → implement ×N tickets → review — and returns the schedule that\n * becomes the `kind:7000 status:\"quote\"` content (§3.2 of\n * `docs/factory-job-protocol.md`).\n *\n * Per the issue gotcha, `implement` must be many increments, not one: this\n * always emits one increment per ticket rather than a single lump-sum\n * implement increment, using the factory's existing parallel per-issue\n * fan-out (`.sandcastle/main.ts`'s Plan phase output — `{issues: [{id,\n * title, branch}]}`) as the boundary.\n */\n\n/** One ticket the factory's plan phase fanned the implement milestone into. */\nexport interface FactoryTicket {\n id: string;\n title: string;\n branch: string;\n}\n\nexport type FactoryMilestone = 'plan' | 'implement' | 'review';\n\n/** One priced increment in the schedule (§3.2's `increments[]` shape). */\nexport interface IncrementSpec {\n n: number;\n of: number;\n milestone: FactoryMilestone;\n /** Set only for `milestone: \"implement\"` increments. */\n ticket?: FactoryTicket;\n priceUsdc: string;\n}\n\n/** Plan milestone's share of the bid, in basis points. */\nconst PLAN_SHARE_BPS = 1000n; // 10%\n/** Review milestone's share of the bid, in basis points. */\nconst REVIEW_SHARE_BPS = 1000n; // 10%\nconst BPS_DENOM = 10000n;\n\n/**\n * Build the increment schedule for a job, given the tickets its plan phase\n * produced. Plan and review each get a fixed 10% of the bid; the remaining\n * 80% (\"implement\") splits evenly across tickets, with the integer-division\n * remainder distributed one micro-USDC at a time to the earliest tickets —\n * so `sum(priceUsdc) === bid` exactly (§3.2 only requires `<=`, but an exact\n * match avoids \"where did the leftover micro-USDC go\" questions).\n *\n * Per decision 7, this schedule is agreed before any paid work starts and is\n * never repriced — a job that runs larger than `tickets` implied is a\n * lesson, not a renegotiation.\n *\n * Throws if `tickets` is empty (a job with no implementation work has no\n * increments to schedule) or `bidMicroUsdc` is not a positive integer.\n */\nexport function planFactoryJob(\n bidMicroUsdc: string,\n tickets: FactoryTicket[]\n): IncrementSpec[] {\n if (tickets.length === 0) {\n throw new Error(\n 'planFactoryJob requires at least one implementation ticket from the plan phase'\n );\n }\n\n let bid: bigint;\n try {\n bid = BigInt(bidMicroUsdc);\n } catch {\n throw new Error(`bidMicroUsdc must be an integer string, got ${bidMicroUsdc}`);\n }\n if (bid <= 0n) {\n throw new Error(`bidMicroUsdc must be positive, got ${bidMicroUsdc}`);\n }\n\n const planPrice = (bid * PLAN_SHARE_BPS) / BPS_DENOM;\n const reviewPrice = (bid * REVIEW_SHARE_BPS) / BPS_DENOM;\n const implementTotal = bid - planPrice - reviewPrice;\n\n const ticketCount = BigInt(tickets.length);\n const base = implementTotal / ticketCount;\n const remainder = implementTotal % ticketCount;\n\n const total = tickets.length + 2;\n const schedule: IncrementSpec[] = [\n { n: 1, of: total, milestone: 'plan', priceUsdc: planPrice.toString() },\n ];\n\n tickets.forEach((ticket, i) => {\n const price = base + (BigInt(i) < remainder ? 1n : 0n);\n schedule.push({\n n: i + 2,\n of: total,\n milestone: 'implement',\n ticket,\n priceUsdc: price.toString(),\n });\n });\n\n schedule.push({\n n: total,\n of: total,\n milestone: 'review',\n priceUsdc: reviewPrice.toString(),\n });\n\n return schedule;\n}\n","/**\n * executeFactoryJob — the adapter's imperative shell (#52): runs the\n * factory's own milestones (`.sandcastle/main.ts`'s plan → per-ticket\n * implement+review → merge phases) through `hooks`, and turns each\n * milestone boundary into a paid increment via `publisher` (existing,\n * publisher.ts — upload + event publish) and `delivery` (new,\n * factory-job-delivery.ts — encrypt + payment wait).\n *\n * Mirrors `executePush`'s split from `planPush`: `planFactoryJob` is the\n * network-free planning half; this is the half that spends money.\n *\n * Decision 6 (toon-meta#262): work happens BEFORE the packet, so every\n * increment's artifact is produced and uploaded before that increment is\n * offered for payment. Decision 7: the schedule is fixed once the quote is\n * published — this never reprices mid-job. Per the issue: \"Stop on\n * non-payment. One unpaid increment and work halts\" — the symmetric\n * protection that neither side is ever exposed for more than one increment.\n */\n\nimport type { Publisher } from './publisher.js';\nimport type { JobDeliveryPort } from './factory-job-delivery.js';\nimport {\n buildIncrementOfferEvent,\n buildQuoteEvent,\n buildResultEvent,\n type FactoryJobOutcome,\n type FactoryJobRequest,\n type RelayEvent,\n} from './factory-job-events.js';\nimport type { GateResult } from './factory-job-gate.js';\nimport {\n planFactoryJob,\n type FactoryTicket,\n type IncrementSpec,\n} from './factory-job-plan.js';\n\n/** One milestone's deliverable, plus the gate result that ran against it, when one did. */\nexport interface FactoryJobWork {\n artifact: Uint8Array;\n /**\n * The reproducible gate result (#53) for this milestone's work, per\n * `FACTORY.md`'s pinned pipeline (lint/typecheck/test/build). Omitted for\n * `plan` — there is no code to gate against a brief.\n */\n gate?: GateResult;\n}\n\n/** Runs one factory milestone and returns its deliverable, plus that milestone's gate result. */\nexport interface FactoryJobHooks {\n /**\n * Runs the factory's plan phase for `job` (mirrors `.sandcastle/main.ts`'s\n * Phase 1): returns the per-ticket fan-out the implement milestone splits\n * into, plus the plan document itself as increment 1's artifact. No gate\n * result — planning produces no code to lint/typecheck/test/build.\n */\n plan(\n job: FactoryJobRequest\n ): Promise<{ tickets: FactoryTicket[]; artifact: Uint8Array }>;\n /**\n * Runs one ticket's implement sandbox pipeline (mirrors Phase 2's\n * per-issue `sandbox.run`, which ends in the gate per `FACTORY.md`) and\n * returns its deliverable (e.g. the ticket's diff) plus that run's gate\n * result.\n */\n implement(ticket: FactoryTicket): Promise<FactoryJobWork>;\n /**\n * Runs the factory's merge/review phase over every completed ticket\n * (mirrors Phase 3, which re-runs the gate per `merge-prompt.md`) and\n * returns the final deliverable plus that run's gate result.\n */\n review(tickets: FactoryTicket[]): Promise<FactoryJobWork>;\n}\n\nexport interface ExecuteFactoryJobOptions {\n job: FactoryJobRequest;\n /** The original kind:5097 request event, verbatim — carried in the result's `request` tag. */\n requestEvent: RelayEvent;\n hooks: FactoryJobHooks;\n publisher: Publisher;\n delivery: JobDeliveryPort;\n relayUrls: string[];\n}\n\nexport interface FactoryJobExecution {\n outcome: FactoryJobOutcome;\n /** The last increment successfully paid for (0 if the buyer never paid increment 1). */\n reachedIncrement: number;\n totalIncrements: number;\n /** The published kind:6097 result event's id. */\n resultEventId: string;\n}\n\nasync function workFor(\n spec: IncrementSpec,\n tickets: FactoryTicket[],\n planArtifact: Uint8Array,\n hooks: FactoryJobHooks\n): Promise<FactoryJobWork> {\n if (spec.milestone === 'plan') return { artifact: planArtifact };\n if (spec.milestone === 'implement') {\n if (!spec.ticket) {\n throw new Error(\n `internal: implement increment ${spec.n} is missing its ticket`\n );\n }\n return hooks.implement(spec.ticket);\n }\n return hooks.review(tickets);\n}\n\n/**\n * Run a job end to end: plan → quote → one increment per milestone boundary\n * (encrypt, upload, offer, wait for payment) → terminal kind:6097 result.\n * Halts at the first unpaid increment (`abandoned-buyer`) or the first\n * hook failure once work is underway (`abandoned-provider`) — see §5.2 of\n * `docs/factory-job-protocol.md` for the three terminal outcomes.\n *\n * Throws if `hooks.plan` itself fails — with no quote ever published, there\n * is no prior kind:7000 event for a result's `reply` e-tag to reference, so\n * no protocol event can be honestly emitted for that failure.\n */\nexport async function executeFactoryJob(\n options: ExecuteFactoryJobOptions\n): Promise<FactoryJobExecution> {\n const { job, requestEvent, hooks, publisher, delivery, relayUrls } =\n options;\n\n if (!publisher.uploadBlob) {\n throw new Error(\n 'the active publisher cannot upload an encrypted increment artifact ' +\n '(uploadBlob unavailable) — executeFactoryJob requires the standalone publisher'\n );\n }\n const uploadBlob = publisher.uploadBlob.bind(publisher);\n\n const { tickets, artifact: planArtifact } = await hooks.plan(job);\n const schedule = planFactoryJob(job.bidMicroUsdc, tickets);\n const totalIncrements = schedule.length;\n\n const quoteEvent = buildQuoteEvent(job, schedule);\n const quoteReceipt = await publisher.publishEvent(quoteEvent, relayUrls);\n\n let parentEventId = quoteReceipt.eventId;\n let reachedIncrement = 0;\n let finalArtifactTxId: string | undefined;\n\n const haltWith = async (\n outcome: FactoryJobOutcome\n ): Promise<FactoryJobExecution> => {\n const resultEvent = buildResultEvent({\n job,\n requestEvent,\n lastEventId: parentEventId,\n outcome,\n reachedIncrement,\n totalIncrements,\n ...(outcome === 'completed' && finalArtifactTxId\n ? { finalArtifact: { arweaveTxId: finalArtifactTxId } }\n : {}),\n });\n const resultReceipt = await publisher.publishEvent(\n resultEvent,\n relayUrls\n );\n return {\n outcome,\n reachedIncrement,\n totalIncrements,\n resultEventId: resultReceipt.eventId,\n };\n };\n\n for (const spec of schedule) {\n let work: FactoryJobWork;\n try {\n work = await workFor(spec, tickets, planArtifact, hooks);\n } catch {\n // Provider-side failure mid-job: stop where we are, no renegotiation.\n return haltWith('abandoned-provider');\n }\n\n const encrypted = await delivery.encryptArtifact(work.artifact);\n const uploadReceipt = await uploadBlob({\n body: Buffer.from(encrypted.ciphertext),\n contentType: 'application/octet-stream',\n ...(job.repo ? { repoId: job.repo } : {}),\n });\n\n const offerEvent = buildIncrementOfferEvent({\n job,\n parentEventId,\n increment: spec,\n artifact: {\n arweaveTxId: uploadReceipt.txId,\n ciphertextSha256: encrypted.ciphertextSha256,\n conditionHex: encrypted.conditionHex,\n },\n ...(work.gate ? { gate: work.gate } : {}),\n });\n const offerReceipt = await publisher.publishEvent(offerEvent, relayUrls);\n\n const paid = await delivery.waitForPayment({\n offerEventId: offerReceipt.eventId,\n conditionHex: encrypted.conditionHex,\n priceUsdc: spec.priceUsdc,\n });\n if (!paid) {\n parentEventId = offerReceipt.eventId;\n return haltWith('abandoned-buyer');\n }\n\n reachedIncrement = spec.n;\n parentEventId = offerReceipt.eventId;\n if (spec.milestone === 'review') finalArtifactTxId = uploadReceipt.txId;\n }\n\n return haltWith('completed');\n}\n","/**\n * ClientJobDeliveryPort — the concrete `JobDeliveryPort` (#52, #56) backed\n * by the real, published `@toon-protocol/client` (>=0.26.0) hashlock\n * helpers (toon-client#495) and serve-side job handling (toon-client#494).\n *\n * Per the issue's explicit instruction, this never re-derives the\n * encrypt/condition relationship locally: `encryptArtifact` is the only\n * place a key or condition is minted, and `fulfillIncrement` is the only\n * place a key becomes an ILP fulfillment.\n *\n * Wiring: pass `handleJob` as `ToonClientConfig.jobHandler` for the SAME\n * identity that runs `executeFactoryJob` (job serving rides the provider's\n * own BTP session, per that field's doc). The connector delivers the\n * buyer's paying PREPARE as a server-originated BTP MESSAGE addressed to\n * this client's own ILP address; `handleJob` reveals the key minted by the\n * immediately-preceding `encryptArtifact` call as the fulfillment — which\n * is simultaneously the payment settling and the decrypt-key handoff\n * (docs/factory-job-protocol.md §4.2, toon-meta).\n *\n * One increment in flight at a time, matching `JobDeliveryPort`'s own\n * sequential-use contract: `encryptArtifact` stages a key, and only\n * `waitForPayment` for that SAME condition arms `handleJob` to spend it. A\n * job PREPARE for any other condition — stray, replayed, or for an\n * increment this port never staged — is refused rather than fulfilled.\n */\n\nimport { createHash } from 'node:crypto';\nimport {\n encryptArtifact as clientEncryptArtifact,\n fulfillIncrement,\n type JobAnswer,\n type JobHandler,\n type JobRequest,\n} from '@toon-protocol/client';\nimport type {\n EncryptedArtifact,\n JobDeliveryPort,\n OfferedIncrement,\n} from './factory-job-delivery.js';\n\nfunction toHex(bytes: Uint8Array): string {\n return Buffer.from(bytes).toString('hex');\n}\n\ninterface ArmedIncrement {\n key: Uint8Array;\n conditionHex: string;\n /** Decimal micro-USDC, the same units as `JobRequest.amount` (#87). */\n priceUsdc: string;\n resolve: (paid: boolean) => void;\n}\n\nexport interface ClientJobDeliveryPortOptions {\n /** How long to wait for the buyer's paying PREPARE before giving up on an increment. Default 120s. */\n paymentTimeoutMs?: number;\n}\n\nconst DEFAULT_PAYMENT_TIMEOUT_MS = 120_000;\n\nexport class ClientJobDeliveryPort implements JobDeliveryPort {\n private readonly paymentTimeoutMs: number;\n private staged?: { key: Uint8Array; conditionHex: string };\n private armed?: ArmedIncrement;\n\n constructor(options: ClientJobDeliveryPortOptions = {}) {\n this.paymentTimeoutMs =\n options.paymentTimeoutMs ?? DEFAULT_PAYMENT_TIMEOUT_MS;\n }\n\n /**\n * The `JobHandler` to register as `ToonClientConfig.jobHandler`. Throws\n * (answered F99 by `createJobMessageHandler`) when no increment is armed\n * for the PREPARE's `executionCondition`, or when the PREPARE's `amount`\n * is below the armed increment's advertised price (#87 — the condition is\n * public, published on the kind:7000 offer, so anyone can send a PREPARE\n * carrying it; only a PREPARE that actually pays the armed price may\n * release the key). An underpaying PREPARE is rejected without consuming\n * the arming, so a correctly-priced PREPARE can still land before the\n * payment timeout. This is the only path that releases a key, and it\n * releases exactly the one the buyer paid for.\n */\n readonly handleJob: JobHandler = (job: JobRequest): JobAnswer => {\n const conditionHex = toHex(job.executionCondition);\n if (!this.armed || conditionHex !== this.armed.conditionHex) {\n throw new Error(\n `no factory-job increment is awaiting payment for condition ${conditionHex}`\n );\n }\n if (job.amount < BigInt(this.armed.priceUsdc)) {\n throw new Error(\n `PREPARE amount ${job.amount} is below the armed price ${this.armed.priceUsdc} for condition ${conditionHex}`\n );\n }\n const { key, resolve } = this.armed;\n this.armed = undefined;\n resolve(true);\n return { fulfillment: fulfillIncrement(key) };\n };\n\n async encryptArtifact(bytes: Uint8Array): Promise<EncryptedArtifact> {\n const encrypted = clientEncryptArtifact(bytes);\n const conditionHex = toHex(encrypted.condition);\n this.staged = { key: encrypted.key, conditionHex };\n return {\n ciphertext: encrypted.ciphertext,\n ciphertextSha256: createHash('sha256')\n .update(encrypted.ciphertext)\n .digest('hex'),\n conditionHex,\n };\n }\n\n async waitForPayment(offer: OfferedIncrement): Promise<boolean> {\n if (!this.staged || offer.conditionHex !== this.staged.conditionHex) {\n throw new Error(\n 'waitForPayment was called for a condition that does not match the most recent encryptArtifact() call'\n );\n }\n const { key, conditionHex } = this.staged;\n const { priceUsdc } = offer;\n this.staged = undefined;\n\n return new Promise<boolean>((resolve) => {\n const timer = setTimeout(() => {\n this.armed = undefined;\n resolve(false);\n }, this.paymentTimeoutMs);\n this.armed = {\n key,\n conditionHex,\n priceUsdc,\n resolve: (paid) => {\n clearTimeout(timer);\n resolve(paid);\n },\n };\n });\n }\n}\n","/**\n * Buyer-side payment for one factory-job increment offer (#56) — the other\n * half of the hashlock join `ClientJobDeliveryPort` (the provider side)\n * answers. Built against `@toon-protocol/client`'s real `buildIncrementPrepare`\n * / `decryptArtifact` (toon-client#495): this module does not re-derive the\n * PREPARE fields or the AEAD decrypt itself.\n *\n * Per docs/factory-job-protocol.md §4.2 (toon-meta), paying an offer is:\n * `executionCondition` equal to the offer's `condition` tag byte for byte,\n * and `data` carrying a reference back to the offer's own kind:7000 event id\n * — \"the payment names the job.\" The provider returns the decryption key as\n * the ILP fulfillment; revealing it is the same act as accepting payment.\n */\n\nimport {\n buildIncrementPrepare,\n decryptArtifact,\n type IncrementOfferTags,\n} from '@toon-protocol/client';\n\n/** What a buyer reads off an advertised kind:7000 `status:\"partial\"` increment offer. */\nexport interface PayableIncrementOffer extends IncrementOfferTags {\n /** The offer event's own id — carried in the PREPARE's `data` field (§4.2). */\n offerEventId: string;\n}\n\n/** The outcome of paying one increment offer. */\nexport type PaidIncrementOffer =\n | { paid: true; key: Uint8Array }\n | { paid: false };\n\n/** The narrow slice of `ToonClient` a buyer needs to pay an increment offer. */\nexport interface SwapPacketSender {\n sendSwapPacket(params: {\n destination: string;\n amount: bigint;\n toonData: Uint8Array;\n executionCondition?: Uint8Array;\n timeout?: number;\n }): Promise<{\n accepted: boolean;\n fulfillment?: string;\n code?: string;\n message?: string;\n }>;\n}\n\n/**\n * Buyer: pay one advertised increment offer. Sends an ILP PREPARE addressed\n * to `providerDestination` with `executionCondition` equal to the offer's\n * `condition` tag and `data` referencing the offer's event id, per §4.2.\n * Resolves `{ paid: false }` on a REJECT (expired/no such increment/wrong\n * fulfillment) rather than throwing — a buyer declining or failing to pay is\n * a protocol outcome (`abandoned-buyer`), not an error.\n */\nexport async function payIncrementOffer(\n client: SwapPacketSender,\n providerDestination: string,\n offer: PayableIncrementOffer\n): Promise<PaidIncrementOffer> {\n const { executionCondition, amount } = buildIncrementPrepare(offer);\n const result = await client.sendSwapPacket({\n destination: providerDestination,\n amount,\n toonData: new TextEncoder().encode(offer.offerEventId),\n executionCondition,\n });\n if (!result.accepted || !result.fulfillment) {\n return { paid: false };\n }\n return { paid: true, key: Buffer.from(result.fulfillment, 'base64') };\n}\n\n/**\n * Buyer: decrypt an increment's artifact using the key revealed as the ILP\n * fulfillment. `offerConditionHex` MUST be the offer's own `condition` tag\n * as read from the relay before payment — never re-derived from `key` —\n * so a provider who reveals a different key than it advertised is caught\n * here rather than trusted.\n */\nexport function decryptIncrementArtifact(\n ciphertext: Uint8Array,\n key: Uint8Array,\n offerConditionHex: string\n): Uint8Array {\n return decryptArtifact(\n ciphertext,\n key,\n Buffer.from(offerConditionHex, 'hex')\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCO,SAAS,WAAW,MAA2B;AACpD,SAAO,KAAK,OAAO,MAAM,CAAC,UAAU,MAAM,IAAI;AAChD;;;AC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A,QACX;AAAA,QACA;AAAA,QACA;AAAA,QACA,SAAS,CAAC,SAAS;AACjB,uBAAa,KAAK;AAClB,kBAAQ,IAAI;AAAA,QACd;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF;;;AC5HA;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AAqCP,eAAsB,kBACpB,QACA,qBACA,OAC6B;AAC7B,QAAM,EAAE,oBAAoB,OAAO,IAAI,sBAAsB,KAAK;AAClE,QAAM,SAAS,MAAM,OAAO,eAAe;AAAA,IACzC,aAAa;AAAA,IACb;AAAA,IACA,UAAU,IAAI,YAAY,EAAE,OAAO,MAAM,YAAY;AAAA,IACrD;AAAA,EACF,CAAC;AACD,MAAI,CAAC,OAAO,YAAY,CAAC,OAAO,aAAa;AAC3C,WAAO,EAAE,MAAM,MAAM;AAAA,EACvB;AACA,SAAO,EAAE,MAAM,MAAM,KAAK,OAAO,KAAK,OAAO,aAAa,QAAQ,EAAE;AACtE;AASO,SAAS,yBACd,YACA,KACA,mBACY;AACZ,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,OAAO,KAAK,mBAAmB,KAAK;AAAA,EACtC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/factory-job-gate.ts","../src/factory-job-events.ts","../src/factory-job-plan.ts","../src/factory-job-execute.ts"],"sourcesContent":["/**\n * Factory job protocol — reproducible gate result per increment (#53).\n *\n * Decision 5 of toon-meta#262 splits trust into three layers: delivery (the\n * hashlock), the objective floor (this — \"did this increment pass the\n * repo's own gate?\"), and quality (reputation, out of scope here). Per\n * `FACTORY.md`, the pinned pipeline's gate is lint/typecheck/test/build run\n * before a PR is opened; this module is the pure, network-free shape for\n * recording exactly what ran, not a summary verdict.\n *\n * This proves conformance, not fitness for purpose — a gate-passing\n * increment can still completely miss the brief. Reputation is what\n * distinguishes those, never this.\n */\n\n/** One command the gate ran, and whether it passed. */\nexport interface GateCheck {\n /** e.g. \"lint\", \"typecheck\", \"test\", \"build\" — whatever actually ran,\n * never a canonical list (gates differ per repo and drift over time). */\n name: string;\n /** The literal command, e.g. \"eslint .\", so a buyer can reproduce it verbatim. */\n command: string;\n pass: boolean;\n}\n\n/**\n * A reproducible gate result: enough for a buyer to re-run the gate\n * themselves and get the same answer. Omitting the commit or the command\n * set turns this from a reproducible result into a provider's unverifiable\n * claim of having done well — both are required, not advisory.\n */\nexport interface GateResult {\n /** The commit this gate ran against. */\n commit: string;\n /** Toolchain versions relevant to reproducing the result, e.g. `{ node: \"20.11.0\", pnpm: \"9.1.0\" }`. */\n toolchain: Record<string, string>;\n checks: GateCheck[];\n}\n\n/** Overall pass/fail — every recorded check passed. A failing gate is not a protocol violation (it is still published, just visibly failing) — this is only the aggregate the `gate` tag and reputation's gate-pass rate read. */\nexport function gatePassed(gate: GateResult): boolean {\n return gate.checks.every((check) => check.pass);\n}\n","/**\n * Factory job protocol — pure NIP-90 event builders/parser (#52).\n *\n * Wire format per toon-meta's `docs/factory-job-protocol.md`\n * (toon-meta#263), fixed 2026-08-03: kind:5097 job request, kind:6097 job\n * result, kind:7000 job feedback (quote / increment offer / narration,\n * disambiguated by the `status` tag). Mirrors nip34-events.ts: every builder\n * returns an UnsignedEvent — the caller signs and publishes (e.g. through a\n * Publisher, publisher.ts) elsewhere. No crypto, no network, no payment.\n */\n\nimport type { UnsignedEvent } from './nip34-events.js';\nimport type { IncrementSpec } from './factory-job-plan.js';\nimport { gatePassed, type GateResult } from './factory-job-gate.js';\n\n// ---------------------------------------------------------------------------\n// Kinds\n// ---------------------------------------------------------------------------\n\n/** Factory job request (the brief). */\nexport const FACTORY_JOB_REQUEST_KIND = 5097;\n/** Factory job result — terminal state (completed/abandoned-*). */\nexport const FACTORY_JOB_RESULT_KIND = 6097;\n/** Factory job feedback — quote, increment offer, or free narration. */\nexport const FACTORY_JOB_FEEDBACK_KIND = 7000;\n\n/** The three §5.2 terminal outcomes — decision 8 has no fourth. */\nexport type FactoryJobOutcome =\n | 'completed'\n | 'abandoned-provider'\n | 'abandoned-buyer';\n\n// ---------------------------------------------------------------------------\n// kind:5097 — job request\n// ---------------------------------------------------------------------------\n\n/** A parsed kind:5097 job request (§2). */\nexport interface FactoryJobRequest {\n /** The requesting kind:5097 event's id. */\n requestEventId: string;\n /** The requesting kind:5097 event's pubkey (who to reply/bill). */\n buyerPubkey: string;\n /** The brief — free text or a chained milestone reference (§2.3). */\n brief: string;\n /** Maximum the buyer will pay across the whole job (micro-USDC, §2.1). */\n bidMicroUsdc: string;\n /** `param repo` — target repo, e.g. `toon-protocol/buzz`. */\n repo?: string;\n /** `param target` — the ticket/issue this job resolves. */\n target?: string;\n /** `param constraints` — gate requirements / scope limits. */\n constraints?: string;\n /** `output` — expected deliverable MIME type. */\n outputMime?: string;\n}\n\n/** Minimal shape of a relay event this module reads/builds — no signing. */\nexport interface RelayEvent {\n id: string;\n pubkey: string;\n tags: string[][];\n content: string;\n}\n\n/**\n * Parse a kind:5097 job request event's tags into a {@link FactoryJobRequest}.\n * Throws if the required `i` (brief) or `bid` tag is missing — both are\n * REQUIRED per §2.1.\n */\nexport function parseFactoryJobRequest(event: RelayEvent): FactoryJobRequest {\n const briefTag = event.tags.find((t) => t[0] === 'i' && t[2] === 'text');\n if (!briefTag?.[1]) {\n throw new Error(\n `kind:${FACTORY_JOB_REQUEST_KIND} event ${event.id} is missing its required brief (\"i\", \"<brief>\", \"text\") tag`\n );\n }\n const bidTag = event.tags.find((t) => t[0] === 'bid');\n if (!bidTag?.[1]) {\n throw new Error(\n `kind:${FACTORY_JOB_REQUEST_KIND} event ${event.id} is missing its required \"bid\" tag`\n );\n }\n\n const params = event.tags.filter((t) => t[0] === 'param');\n const paramValue = (name: string): string | undefined =>\n params.find((t) => t[1] === name)?.[2];\n\n const repo = paramValue('repo');\n const target = paramValue('target');\n const constraints = paramValue('constraints');\n const outputMime = event.tags.find((t) => t[0] === 'output')?.[1];\n\n return {\n requestEventId: event.id,\n buyerPubkey: event.pubkey,\n brief: briefTag[1],\n bidMicroUsdc: bidTag[1],\n ...(repo !== undefined ? { repo } : {}),\n ...(target !== undefined ? { target } : {}),\n ...(constraints !== undefined ? { constraints } : {}),\n ...(outputMime !== undefined ? { outputMime } : {}),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:7000 status:\"quote\" — the RFQ reply (§3)\n// ---------------------------------------------------------------------------\n\n/**\n * Build the `kind:7000 status:\"quote\"` reply: the proposed increment\n * schedule, committing nothing (§3.3 — there is no accept message; payment\n * of increment 1 is acceptance).\n */\nexport function buildQuoteEvent(\n job: FactoryJobRequest,\n schedule: IncrementSpec[]\n): UnsignedEvent {\n return {\n kind: FACTORY_JOB_FEEDBACK_KIND,\n content: JSON.stringify({\n increments: schedule.map((s) => ({\n n: s.n,\n of: s.of,\n milestone: s.milestone,\n priceUsdc: s.priceUsdc,\n })),\n }),\n tags: [\n ['e', job.requestEventId, '', 'root'],\n ['p', job.buyerPubkey],\n ['status', 'quote'],\n ],\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:7000 status:\"partial\" — the increment offer (§4)\n// ---------------------------------------------------------------------------\n\n/** Where the encrypted increment artifact lives, and its hashlock condition. */\nexport interface EncryptedArtifactRef {\n /** Arweave tx id the ciphertext was uploaded to. */\n arweaveTxId: string;\n /** sha256 of the ciphertext, hex — lets the buyer detect a bad fetch. */\n ciphertextSha256: string;\n /** `sha256(key)` hex, where `key` decrypts the artifact (§4.2 — the join). */\n conditionHex: string;\n}\n\nexport interface BuildIncrementOfferOptions {\n job: FactoryJobRequest;\n /** The quote event's id for increment 1, else the previous offer's id. */\n parentEventId: string;\n increment: IncrementSpec;\n artifact: EncryptedArtifactRef;\n /**\n * The reproducible gate result for this increment's work (#53 — the\n * objective floor), when one ran. Omitted for milestones with nothing to\n * gate (e.g. `plan` — there is no code to lint against a brief).\n */\n gate?: GateResult;\n}\n\n/** A gate result missing its commit or check list cannot be reproduced by the buyer — reject it here rather than publish an unverifiable claim. */\nfunction assertReproducible(gate: GateResult): void {\n if (gate.checks.length === 0) {\n throw new Error(\n 'gate result must record at least one check for the offer to be reproducible'\n );\n }\n if (!gate.commit) {\n throw new Error(\n 'gate result must record the commit it ran against for the offer to be reproducible'\n );\n }\n}\n\n/**\n * Build the `kind:7000 status:\"partial\"` increment offer — published once\n * the provider has done that increment's work and uploaded the encrypted\n * artifact. This is the join between the relay plane and the connector\n * plane (§4.2): `condition` here MUST equal the paying PREPARE's\n * `executionCondition`, byte for byte.\n *\n * When `gate` is supplied, the offer also carries a `[\"gate\", \"pass\"|\"fail\"]`\n * tag (cheap to scan for a gate-pass rate, decision 8) and the full\n * reproducible result in `content` — visible to the buyer before they pay,\n * per the issue: a gate-failing increment is still offered, never hidden.\n */\nexport function buildIncrementOfferEvent(\n options: BuildIncrementOfferOptions\n): UnsignedEvent {\n const { job, parentEventId, increment, artifact, gate } = options;\n if (gate) assertReproducible(gate);\n\n const tags: string[][] = [\n ['e', job.requestEventId, '', 'root'],\n ['e', parentEventId, '', 'reply'],\n ['p', job.buyerPubkey],\n ['status', 'partial'],\n ['increment', String(increment.n), String(increment.of)],\n ['i', artifact.arweaveTxId, 'url'],\n ['i', artifact.ciphertextSha256, 'text', '', 'hash'],\n ['amount', increment.priceUsdc, 'usdc'],\n ['condition', artifact.conditionHex],\n ];\n if (gate) tags.push(['gate', gatePassed(gate) ? 'pass' : 'fail']);\n\n return {\n kind: FACTORY_JOB_FEEDBACK_KIND,\n content: gate ? JSON.stringify({ gate }) : '',\n tags,\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:7000 status:\"processing\" — free narration (§6)\n// ---------------------------------------------------------------------------\n\nexport interface BuildNarrationOptions {\n job: FactoryJobRequest;\n /** The prior event in the thread (quote, offer, or narration). */\n parentEventId: string;\n message: string;\n}\n\n/**\n * Build a free narration event — no artifact, no `amount`/`condition`, MUST\n * NOT be paid against (§6). This is what makes a provider look alive between\n * increments.\n */\nexport function buildNarrationEvent(\n options: BuildNarrationOptions\n): UnsignedEvent {\n const { job, parentEventId, message } = options;\n return {\n kind: FACTORY_JOB_FEEDBACK_KIND,\n content: message,\n tags: [\n ['e', job.requestEventId, '', 'root'],\n ['e', parentEventId, '', 'reply'],\n ['p', job.buyerPubkey],\n ['status', 'processing'],\n ],\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n\n// ---------------------------------------------------------------------------\n// kind:6097 — job result (§5)\n// ---------------------------------------------------------------------------\n\nexport interface BuildResultEventOptions {\n job: FactoryJobRequest;\n /** The original kind:5097 request event, verbatim (the `request` tag). */\n requestEvent: RelayEvent;\n /** The last kind:7000 event in the thread (offer or narration). */\n lastEventId: string;\n outcome: FactoryJobOutcome;\n /** How far the job got — `n-reached == of` only when outcome is completed. */\n reachedIncrement: number;\n totalIncrements: number;\n /** Required (and only meaningful) when outcome is `\"completed\"` (§5.2). */\n finalArtifact?: { arweaveTxId: string };\n}\n\n/**\n * Build the terminal `kind:6097` job result. Reputation (decision 8) is\n * computed from exactly this event and nothing else.\n */\nexport function buildResultEvent(\n options: BuildResultEventOptions\n): UnsignedEvent {\n const {\n job,\n requestEvent,\n lastEventId,\n outcome,\n reachedIncrement,\n totalIncrements,\n finalArtifact,\n } = options;\n\n const tags: string[][] = [\n ['e', job.requestEventId, '', 'root'],\n ['e', lastEventId, '', 'reply'],\n ['p', job.buyerPubkey],\n ['request', JSON.stringify(requestEvent)],\n ['outcome', outcome],\n ['increment', String(reachedIncrement), String(totalIncrements)],\n ];\n if (outcome === 'completed' && finalArtifact) {\n tags.push(['i', finalArtifact.arweaveTxId, 'url']);\n }\n\n return {\n kind: FACTORY_JOB_RESULT_KIND,\n content: '',\n tags,\n created_at: Math.floor(Date.now() / 1000),\n };\n}\n","/**\n * Factory job protocol — increment schedule planner (#52).\n *\n * `planFactoryJob` is the network-free half of the adapter (mirrors\n * `planPush`'s split from `executePush` in push.ts): given a job's `bid` and\n * the ticket list the factory's plan phase produced, it prices the\n * milestone boundaries decision 6 of toon-meta#262 fixed as increments —\n * plan → implement ×N tickets → review — and returns the schedule that\n * becomes the `kind:7000 status:\"quote\"` content (§3.2 of\n * `docs/factory-job-protocol.md`).\n *\n * Per the issue gotcha, `implement` must be many increments, not one: this\n * always emits one increment per ticket rather than a single lump-sum\n * implement increment, using the factory's existing parallel per-issue\n * fan-out (`.sandcastle/main.ts`'s Plan phase output — `{issues: [{id,\n * title, branch}]}`) as the boundary.\n */\n\n/** One ticket the factory's plan phase fanned the implement milestone into. */\nexport interface FactoryTicket {\n id: string;\n title: string;\n branch: string;\n}\n\nexport type FactoryMilestone = 'plan' | 'implement' | 'review';\n\n/** One priced increment in the schedule (§3.2's `increments[]` shape). */\nexport interface IncrementSpec {\n n: number;\n of: number;\n milestone: FactoryMilestone;\n /** Set only for `milestone: \"implement\"` increments. */\n ticket?: FactoryTicket;\n priceUsdc: string;\n}\n\n/** Plan milestone's share of the bid, in basis points. */\nconst PLAN_SHARE_BPS = 1000n; // 10%\n/** Review milestone's share of the bid, in basis points. */\nconst REVIEW_SHARE_BPS = 1000n; // 10%\nconst BPS_DENOM = 10000n;\n\n/**\n * Build the increment schedule for a job, given the tickets its plan phase\n * produced. Plan and review each get a fixed 10% of the bid; the remaining\n * 80% (\"implement\") splits evenly across tickets, with the integer-division\n * remainder distributed one micro-USDC at a time to the earliest tickets —\n * so `sum(priceUsdc) === bid` exactly (§3.2 only requires `<=`, but an exact\n * match avoids \"where did the leftover micro-USDC go\" questions).\n *\n * Per decision 7, this schedule is agreed before any paid work starts and is\n * never repriced — a job that runs larger than `tickets` implied is a\n * lesson, not a renegotiation.\n *\n * Throws if `tickets` is empty (a job with no implementation work has no\n * increments to schedule) or `bidMicroUsdc` is not a positive integer.\n */\nexport function planFactoryJob(\n bidMicroUsdc: string,\n tickets: FactoryTicket[]\n): IncrementSpec[] {\n if (tickets.length === 0) {\n throw new Error(\n 'planFactoryJob requires at least one implementation ticket from the plan phase'\n );\n }\n\n let bid: bigint;\n try {\n bid = BigInt(bidMicroUsdc);\n } catch {\n throw new Error(`bidMicroUsdc must be an integer string, got ${bidMicroUsdc}`);\n }\n if (bid <= 0n) {\n throw new Error(`bidMicroUsdc must be positive, got ${bidMicroUsdc}`);\n }\n\n const planPrice = (bid * PLAN_SHARE_BPS) / BPS_DENOM;\n const reviewPrice = (bid * REVIEW_SHARE_BPS) / BPS_DENOM;\n const implementTotal = bid - planPrice - reviewPrice;\n\n const ticketCount = BigInt(tickets.length);\n const base = implementTotal / ticketCount;\n const remainder = implementTotal % ticketCount;\n\n const total = tickets.length + 2;\n const schedule: IncrementSpec[] = [\n { n: 1, of: total, milestone: 'plan', priceUsdc: planPrice.toString() },\n ];\n\n tickets.forEach((ticket, i) => {\n const price = base + (BigInt(i) < remainder ? 1n : 0n);\n schedule.push({\n n: i + 2,\n of: total,\n milestone: 'implement',\n ticket,\n priceUsdc: price.toString(),\n });\n });\n\n schedule.push({\n n: total,\n of: total,\n milestone: 'review',\n priceUsdc: reviewPrice.toString(),\n });\n\n return schedule;\n}\n","/**\n * executeFactoryJob — the adapter's imperative shell (#52): runs the\n * factory's own milestones (`.sandcastle/main.ts`'s plan → per-ticket\n * implement+review → merge phases) through `hooks`, and turns each\n * milestone boundary into a paid increment via `publisher` (existing,\n * publisher.ts — upload + event publish) and `delivery` (new,\n * factory-job-delivery.ts — encrypt + payment wait).\n *\n * Mirrors `executePush`'s split from `planPush`: `planFactoryJob` is the\n * network-free planning half; this is the half that spends money.\n *\n * Decision 6 (toon-meta#262): work happens BEFORE the packet, so every\n * increment's artifact is produced and uploaded before that increment is\n * offered for payment. Decision 7: the schedule is fixed once the quote is\n * published — this never reprices mid-job. Per the issue: \"Stop on\n * non-payment. One unpaid increment and work halts\" — the symmetric\n * protection that neither side is ever exposed for more than one increment.\n */\n\nimport type { Publisher } from './publisher.js';\nimport type { JobDeliveryPort } from './factory-job-delivery.js';\nimport {\n buildIncrementOfferEvent,\n buildQuoteEvent,\n buildResultEvent,\n type FactoryJobOutcome,\n type FactoryJobRequest,\n type RelayEvent,\n} from './factory-job-events.js';\nimport type { GateResult } from './factory-job-gate.js';\nimport {\n planFactoryJob,\n type FactoryTicket,\n type IncrementSpec,\n} from './factory-job-plan.js';\n\n/** One milestone's deliverable, plus the gate result that ran against it, when one did. */\nexport interface FactoryJobWork {\n artifact: Uint8Array;\n /**\n * The reproducible gate result (#53) for this milestone's work, per\n * `FACTORY.md`'s pinned pipeline (lint/typecheck/test/build). Omitted for\n * `plan` — there is no code to gate against a brief.\n */\n gate?: GateResult;\n}\n\n/** Runs one factory milestone and returns its deliverable, plus that milestone's gate result. */\nexport interface FactoryJobHooks {\n /**\n * Runs the factory's plan phase for `job` (mirrors `.sandcastle/main.ts`'s\n * Phase 1): returns the per-ticket fan-out the implement milestone splits\n * into, plus the plan document itself as increment 1's artifact. No gate\n * result — planning produces no code to lint/typecheck/test/build.\n */\n plan(\n job: FactoryJobRequest\n ): Promise<{ tickets: FactoryTicket[]; artifact: Uint8Array }>;\n /**\n * Runs one ticket's implement sandbox pipeline (mirrors Phase 2's\n * per-issue `sandbox.run`, which ends in the gate per `FACTORY.md`) and\n * returns its deliverable (e.g. the ticket's diff) plus that run's gate\n * result.\n */\n implement(ticket: FactoryTicket): Promise<FactoryJobWork>;\n /**\n * Runs the factory's merge/review phase over every completed ticket\n * (mirrors Phase 3, which re-runs the gate per `merge-prompt.md`) and\n * returns the final deliverable plus that run's gate result.\n */\n review(tickets: FactoryTicket[]): Promise<FactoryJobWork>;\n}\n\nexport interface ExecuteFactoryJobOptions {\n job: FactoryJobRequest;\n /** The original kind:5097 request event, verbatim — carried in the result's `request` tag. */\n requestEvent: RelayEvent;\n hooks: FactoryJobHooks;\n publisher: Publisher;\n delivery: JobDeliveryPort;\n relayUrls: string[];\n}\n\nexport interface FactoryJobExecution {\n outcome: FactoryJobOutcome;\n /** The last increment successfully paid for (0 if the buyer never paid increment 1). */\n reachedIncrement: number;\n totalIncrements: number;\n /** The published kind:6097 result event's id. */\n resultEventId: string;\n}\n\nasync function workFor(\n spec: IncrementSpec,\n tickets: FactoryTicket[],\n planArtifact: Uint8Array,\n hooks: FactoryJobHooks\n): Promise<FactoryJobWork> {\n if (spec.milestone === 'plan') return { artifact: planArtifact };\n if (spec.milestone === 'implement') {\n if (!spec.ticket) {\n throw new Error(\n `internal: implement increment ${spec.n} is missing its ticket`\n );\n }\n return hooks.implement(spec.ticket);\n }\n return hooks.review(tickets);\n}\n\n/**\n * Run a job end to end: plan → quote → one increment per milestone boundary\n * (encrypt, upload, offer, wait for payment) → terminal kind:6097 result.\n * Halts at the first unpaid increment (`abandoned-buyer`) or the first\n * hook failure once work is underway (`abandoned-provider`) — see §5.2 of\n * `docs/factory-job-protocol.md` for the three terminal outcomes.\n *\n * Throws if `hooks.plan` itself fails — with no quote ever published, there\n * is no prior kind:7000 event for a result's `reply` e-tag to reference, so\n * no protocol event can be honestly emitted for that failure.\n */\nexport async function executeFactoryJob(\n options: ExecuteFactoryJobOptions\n): Promise<FactoryJobExecution> {\n const { job, requestEvent, hooks, publisher, delivery, relayUrls } =\n options;\n\n if (!publisher.uploadBlob) {\n throw new Error(\n 'the active publisher cannot upload an encrypted increment artifact ' +\n '(uploadBlob unavailable) — executeFactoryJob requires the standalone publisher'\n );\n }\n const uploadBlob = publisher.uploadBlob.bind(publisher);\n\n const { tickets, artifact: planArtifact } = await hooks.plan(job);\n const schedule = planFactoryJob(job.bidMicroUsdc, tickets);\n const totalIncrements = schedule.length;\n\n const quoteEvent = buildQuoteEvent(job, schedule);\n const quoteReceipt = await publisher.publishEvent(quoteEvent, relayUrls);\n\n let parentEventId = quoteReceipt.eventId;\n let reachedIncrement = 0;\n let finalArtifactTxId: string | undefined;\n\n const haltWith = async (\n outcome: FactoryJobOutcome\n ): Promise<FactoryJobExecution> => {\n const resultEvent = buildResultEvent({\n job,\n requestEvent,\n lastEventId: parentEventId,\n outcome,\n reachedIncrement,\n totalIncrements,\n ...(outcome === 'completed' && finalArtifactTxId\n ? { finalArtifact: { arweaveTxId: finalArtifactTxId } }\n : {}),\n });\n const resultReceipt = await publisher.publishEvent(\n resultEvent,\n relayUrls\n );\n return {\n outcome,\n reachedIncrement,\n totalIncrements,\n resultEventId: resultReceipt.eventId,\n };\n };\n\n for (const spec of schedule) {\n let work: FactoryJobWork;\n try {\n work = await workFor(spec, tickets, planArtifact, hooks);\n } catch {\n // Provider-side failure mid-job: stop where we are, no renegotiation.\n return haltWith('abandoned-provider');\n }\n\n const encrypted = await delivery.encryptArtifact(work.artifact);\n const uploadReceipt = await uploadBlob({\n body: Buffer.from(encrypted.ciphertext),\n contentType: 'application/octet-stream',\n ...(job.repo ? { repoId: job.repo } : {}),\n });\n\n const offerEvent = buildIncrementOfferEvent({\n job,\n parentEventId,\n increment: spec,\n artifact: {\n arweaveTxId: uploadReceipt.txId,\n ciphertextSha256: encrypted.ciphertextSha256,\n conditionHex: encrypted.conditionHex,\n },\n ...(work.gate ? { gate: work.gate } : {}),\n });\n const offerReceipt = await publisher.publishEvent(offerEvent, relayUrls);\n\n const paid = await delivery.waitForPayment({\n offerEventId: offerReceipt.eventId,\n conditionHex: encrypted.conditionHex,\n priceUsdc: spec.priceUsdc,\n });\n if (!paid) {\n parentEventId = offerReceipt.eventId;\n return haltWith('abandoned-buyer');\n }\n\n reachedIncrement = spec.n;\n parentEventId = offerReceipt.eventId;\n if (spec.milestone === 'review') finalArtifactTxId = uploadReceipt.txId;\n }\n\n return haltWith('completed');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCO,SAAS,WAAW,MAA2B;AACpD,SAAO,KAAK,OAAO,MAAM,CAAC,UAAU,MAAM,IAAI;AAChD;;;ACtBO,IAAM,2BAA2B;AAEjC,IAAM,0BAA0B;AAEhC,IAAM,4BAA4B;AA6ClC,SAAS,uBAAuB,OAAsC;AAC3E,QAAM,WAAW,MAAM,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,OAAO,EAAE,CAAC,MAA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