@toon-protocol/rig 3.3.1 → 3.5.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/README.md +31 -0
- package/dist/index.d.ts +472 -3
- package/dist/index.js +384 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -585,6 +585,37 @@ it — that stays behind the `Publisher` seam:
|
|
|
585
585
|
`executePush` (crash-resume safe via content-addressed skip).
|
|
586
586
|
- `routes.ts` — the JSON wire shapes of the daemon's `/git/*` control routes.
|
|
587
587
|
- `cli/` — the `rig` bin.
|
|
588
|
+
- `factory-job-{events,plan,delivery,execute}.ts` — the factory job adapter
|
|
589
|
+
(job → sandcastle milestones → paid increments,
|
|
590
|
+
[toon-meta#262](https://github.com/toon-protocol/toon-meta/issues/262) "agents
|
|
591
|
+
earning"): `planFactoryJob`/`executeFactoryJob` mirror `planPush`/`executePush`'s
|
|
592
|
+
split, and `JobDeliveryPort` is the injected seam for the per-increment
|
|
593
|
+
encrypt/pay leg. `factory-job-delivery-client.ts` (`ClientJobDeliveryPort`) is
|
|
594
|
+
the concrete `JobDeliveryPort`, backed by `@toon-protocol/client`'s real
|
|
595
|
+
hashlock helpers; `factory-job-pay.ts` is the buyer-side counterpart
|
|
596
|
+
(`payIncrementOffer`/`decryptIncrementArtifact`). See "Factory job proof"
|
|
597
|
+
below.
|
|
598
|
+
|
|
599
|
+
### Factory job proof — one paid increment end to end (#56)
|
|
600
|
+
|
|
601
|
+
`scripts/factory-job-proof.ts` is the runnable proof that a provider can
|
|
602
|
+
deliver one factory-job increment to a buyer and get paid for it, entirely
|
|
603
|
+
off-chain, using the real `@toon-protocol/client` (no fakes): encrypt → upload
|
|
604
|
+
→ offer → pay → fulfil → decrypt, then assert the provider's `getClaimState()`
|
|
605
|
+
spendable balance rose with the on-chain deposit unchanged (no settlement).
|
|
606
|
+
|
|
607
|
+
```sh
|
|
608
|
+
cd packages/rig
|
|
609
|
+
pnpm factory-job-proof
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
By default this generates and faucet-funds two fresh devnet identities (a
|
|
613
|
+
provider and a buyer) against the public devnet (see "Devnet reference"
|
|
614
|
+
below) and tears them down at the end — safe to re-run any number of times.
|
|
615
|
+
Set `FACTORY_JOB_PROOF_PROVIDER_MNEMONIC` / `FACTORY_JOB_PROOF_BUYER_MNEMONIC`
|
|
616
|
+
to reuse already-funded identities instead, or `FACTORY_JOB_PROOF_SKIP_FUND=1`
|
|
617
|
+
to skip the faucet drip. See the script's own header comment for the full env
|
|
618
|
+
var list.
|
|
588
619
|
|
|
589
620
|
Part of [epic #222](https://github.com/toon-protocol/toon-client/issues/222) and
|
|
590
621
|
[epic #246](https://github.com/toon-protocol/toon-client/issues/246).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { c as GitObjectType, P as Publisher, F as FeeRates, b as PublishReceipt } 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,
|
|
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';
|
|
3
4
|
import '@toon-protocol/core/nip34';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -1045,4 +1046,472 @@ declare function serializeEventReceipt(kind: number, receipt: PublishReceipt): G
|
|
|
1045
1046
|
/** Serialize a PushResult onto the wire (bigints → strings, Maps → records). */
|
|
1046
1047
|
declare function serializePushResult(plan: PushPlan, result: PushResult): GitPushResponse;
|
|
1047
1048
|
|
|
1048
|
-
|
|
1049
|
+
/**
|
|
1050
|
+
* Factory job protocol — increment schedule planner (#52).
|
|
1051
|
+
*
|
|
1052
|
+
* `planFactoryJob` is the network-free half of the adapter (mirrors
|
|
1053
|
+
* `planPush`'s split from `executePush` in push.ts): given a job's `bid` and
|
|
1054
|
+
* the ticket list the factory's plan phase produced, it prices the
|
|
1055
|
+
* milestone boundaries decision 6 of toon-meta#262 fixed as increments —
|
|
1056
|
+
* plan → implement ×N tickets → review — and returns the schedule that
|
|
1057
|
+
* becomes the `kind:7000 status:"quote"` content (§3.2 of
|
|
1058
|
+
* `docs/factory-job-protocol.md`).
|
|
1059
|
+
*
|
|
1060
|
+
* Per the issue gotcha, `implement` must be many increments, not one: this
|
|
1061
|
+
* always emits one increment per ticket rather than a single lump-sum
|
|
1062
|
+
* implement increment, using the factory's existing parallel per-issue
|
|
1063
|
+
* fan-out (`.sandcastle/main.ts`'s Plan phase output — `{issues: [{id,
|
|
1064
|
+
* title, branch}]}`) as the boundary.
|
|
1065
|
+
*/
|
|
1066
|
+
/** One ticket the factory's plan phase fanned the implement milestone into. */
|
|
1067
|
+
interface FactoryTicket {
|
|
1068
|
+
id: string;
|
|
1069
|
+
title: string;
|
|
1070
|
+
branch: string;
|
|
1071
|
+
}
|
|
1072
|
+
type FactoryMilestone = 'plan' | 'implement' | 'review';
|
|
1073
|
+
/** One priced increment in the schedule (§3.2's `increments[]` shape). */
|
|
1074
|
+
interface IncrementSpec {
|
|
1075
|
+
n: number;
|
|
1076
|
+
of: number;
|
|
1077
|
+
milestone: FactoryMilestone;
|
|
1078
|
+
/** Set only for `milestone: "implement"` increments. */
|
|
1079
|
+
ticket?: FactoryTicket;
|
|
1080
|
+
priceUsdc: string;
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Build the increment schedule for a job, given the tickets its plan phase
|
|
1084
|
+
* produced. Plan and review each get a fixed 10% of the bid; the remaining
|
|
1085
|
+
* 80% ("implement") splits evenly across tickets, with the integer-division
|
|
1086
|
+
* remainder distributed one micro-USDC at a time to the earliest tickets —
|
|
1087
|
+
* so `sum(priceUsdc) === bid` exactly (§3.2 only requires `<=`, but an exact
|
|
1088
|
+
* match avoids "where did the leftover micro-USDC go" questions).
|
|
1089
|
+
*
|
|
1090
|
+
* Per decision 7, this schedule is agreed before any paid work starts and is
|
|
1091
|
+
* never repriced — a job that runs larger than `tickets` implied is a
|
|
1092
|
+
* lesson, not a renegotiation.
|
|
1093
|
+
*
|
|
1094
|
+
* Throws if `tickets` is empty (a job with no implementation work has no
|
|
1095
|
+
* increments to schedule) or `bidMicroUsdc` is not a positive integer.
|
|
1096
|
+
*/
|
|
1097
|
+
declare function planFactoryJob(bidMicroUsdc: string, tickets: FactoryTicket[]): IncrementSpec[];
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Factory job protocol — reproducible gate result per increment (#53).
|
|
1101
|
+
*
|
|
1102
|
+
* Decision 5 of toon-meta#262 splits trust into three layers: delivery (the
|
|
1103
|
+
* hashlock), the objective floor (this — "did this increment pass the
|
|
1104
|
+
* repo's own gate?"), and quality (reputation, out of scope here). Per
|
|
1105
|
+
* `FACTORY.md`, the pinned pipeline's gate is lint/typecheck/test/build run
|
|
1106
|
+
* before a PR is opened; this module is the pure, network-free shape for
|
|
1107
|
+
* recording exactly what ran, not a summary verdict.
|
|
1108
|
+
*
|
|
1109
|
+
* This proves conformance, not fitness for purpose — a gate-passing
|
|
1110
|
+
* increment can still completely miss the brief. Reputation is what
|
|
1111
|
+
* distinguishes those, never this.
|
|
1112
|
+
*/
|
|
1113
|
+
/** One command the gate ran, and whether it passed. */
|
|
1114
|
+
interface GateCheck {
|
|
1115
|
+
/** e.g. "lint", "typecheck", "test", "build" — whatever actually ran,
|
|
1116
|
+
* never a canonical list (gates differ per repo and drift over time). */
|
|
1117
|
+
name: string;
|
|
1118
|
+
/** The literal command, e.g. "eslint .", so a buyer can reproduce it verbatim. */
|
|
1119
|
+
command: string;
|
|
1120
|
+
pass: boolean;
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* A reproducible gate result: enough for a buyer to re-run the gate
|
|
1124
|
+
* themselves and get the same answer. Omitting the commit or the command
|
|
1125
|
+
* set turns this from a reproducible result into a provider's unverifiable
|
|
1126
|
+
* claim of having done well — both are required, not advisory.
|
|
1127
|
+
*/
|
|
1128
|
+
interface GateResult {
|
|
1129
|
+
/** The commit this gate ran against. */
|
|
1130
|
+
commit: string;
|
|
1131
|
+
/** Toolchain versions relevant to reproducing the result, e.g. `{ node: "20.11.0", pnpm: "9.1.0" }`. */
|
|
1132
|
+
toolchain: Record<string, string>;
|
|
1133
|
+
checks: GateCheck[];
|
|
1134
|
+
}
|
|
1135
|
+
/** 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. */
|
|
1136
|
+
declare function gatePassed(gate: GateResult): boolean;
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* Factory job protocol — pure NIP-90 event builders/parser (#52).
|
|
1140
|
+
*
|
|
1141
|
+
* Wire format per toon-meta's `docs/factory-job-protocol.md`
|
|
1142
|
+
* (toon-meta#263), fixed 2026-08-03: kind:5097 job request, kind:6097 job
|
|
1143
|
+
* result, kind:7000 job feedback (quote / increment offer / narration,
|
|
1144
|
+
* disambiguated by the `status` tag). Mirrors nip34-events.ts: every builder
|
|
1145
|
+
* returns an UnsignedEvent — the caller signs and publishes (e.g. through a
|
|
1146
|
+
* Publisher, publisher.ts) elsewhere. No crypto, no network, no payment.
|
|
1147
|
+
*/
|
|
1148
|
+
|
|
1149
|
+
/** Factory job request (the brief). */
|
|
1150
|
+
declare const FACTORY_JOB_REQUEST_KIND = 5097;
|
|
1151
|
+
/** Factory job result — terminal state (completed/abandoned-*). */
|
|
1152
|
+
declare const FACTORY_JOB_RESULT_KIND = 6097;
|
|
1153
|
+
/** Factory job feedback — quote, increment offer, or free narration. */
|
|
1154
|
+
declare const FACTORY_JOB_FEEDBACK_KIND = 7000;
|
|
1155
|
+
/** The three §5.2 terminal outcomes — decision 8 has no fourth. */
|
|
1156
|
+
type FactoryJobOutcome = 'completed' | 'abandoned-provider' | 'abandoned-buyer';
|
|
1157
|
+
/** A parsed kind:5097 job request (§2). */
|
|
1158
|
+
interface FactoryJobRequest {
|
|
1159
|
+
/** The requesting kind:5097 event's id. */
|
|
1160
|
+
requestEventId: string;
|
|
1161
|
+
/** The requesting kind:5097 event's pubkey (who to reply/bill). */
|
|
1162
|
+
buyerPubkey: string;
|
|
1163
|
+
/** The brief — free text or a chained milestone reference (§2.3). */
|
|
1164
|
+
brief: string;
|
|
1165
|
+
/** Maximum the buyer will pay across the whole job (micro-USDC, §2.1). */
|
|
1166
|
+
bidMicroUsdc: string;
|
|
1167
|
+
/** `param repo` — target repo, e.g. `toon-protocol/buzz`. */
|
|
1168
|
+
repo?: string;
|
|
1169
|
+
/** `param target` — the ticket/issue this job resolves. */
|
|
1170
|
+
target?: string;
|
|
1171
|
+
/** `param constraints` — gate requirements / scope limits. */
|
|
1172
|
+
constraints?: string;
|
|
1173
|
+
/** `output` — expected deliverable MIME type. */
|
|
1174
|
+
outputMime?: string;
|
|
1175
|
+
}
|
|
1176
|
+
/** Minimal shape of a relay event this module reads/builds — no signing. */
|
|
1177
|
+
interface RelayEvent {
|
|
1178
|
+
id: string;
|
|
1179
|
+
pubkey: string;
|
|
1180
|
+
tags: string[][];
|
|
1181
|
+
content: string;
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* Parse a kind:5097 job request event's tags into a {@link FactoryJobRequest}.
|
|
1185
|
+
* Throws if the required `i` (brief) or `bid` tag is missing — both are
|
|
1186
|
+
* REQUIRED per §2.1.
|
|
1187
|
+
*/
|
|
1188
|
+
declare function parseFactoryJobRequest(event: RelayEvent): FactoryJobRequest;
|
|
1189
|
+
/**
|
|
1190
|
+
* Build the `kind:7000 status:"quote"` reply: the proposed increment
|
|
1191
|
+
* schedule, committing nothing (§3.3 — there is no accept message; payment
|
|
1192
|
+
* of increment 1 is acceptance).
|
|
1193
|
+
*/
|
|
1194
|
+
declare function buildQuoteEvent(job: FactoryJobRequest, schedule: IncrementSpec[]): UnsignedEvent;
|
|
1195
|
+
/** Where the encrypted increment artifact lives, and its hashlock condition. */
|
|
1196
|
+
interface EncryptedArtifactRef {
|
|
1197
|
+
/** Arweave tx id the ciphertext was uploaded to. */
|
|
1198
|
+
arweaveTxId: string;
|
|
1199
|
+
/** sha256 of the ciphertext, hex — lets the buyer detect a bad fetch. */
|
|
1200
|
+
ciphertextSha256: string;
|
|
1201
|
+
/** `sha256(key)` hex, where `key` decrypts the artifact (§4.2 — the join). */
|
|
1202
|
+
conditionHex: string;
|
|
1203
|
+
}
|
|
1204
|
+
interface BuildIncrementOfferOptions {
|
|
1205
|
+
job: FactoryJobRequest;
|
|
1206
|
+
/** The quote event's id for increment 1, else the previous offer's id. */
|
|
1207
|
+
parentEventId: string;
|
|
1208
|
+
increment: IncrementSpec;
|
|
1209
|
+
artifact: EncryptedArtifactRef;
|
|
1210
|
+
/**
|
|
1211
|
+
* The reproducible gate result for this increment's work (#53 — the
|
|
1212
|
+
* objective floor), when one ran. Omitted for milestones with nothing to
|
|
1213
|
+
* gate (e.g. `plan` — there is no code to lint against a brief).
|
|
1214
|
+
*/
|
|
1215
|
+
gate?: GateResult;
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Build the `kind:7000 status:"partial"` increment offer — published once
|
|
1219
|
+
* the provider has done that increment's work and uploaded the encrypted
|
|
1220
|
+
* artifact. This is the join between the relay plane and the connector
|
|
1221
|
+
* plane (§4.2): `condition` here MUST equal the paying PREPARE's
|
|
1222
|
+
* `executionCondition`, byte for byte.
|
|
1223
|
+
*
|
|
1224
|
+
* When `gate` is supplied, the offer also carries a `["gate", "pass"|"fail"]`
|
|
1225
|
+
* tag (cheap to scan for a gate-pass rate, decision 8) and the full
|
|
1226
|
+
* reproducible result in `content` — visible to the buyer before they pay,
|
|
1227
|
+
* per the issue: a gate-failing increment is still offered, never hidden.
|
|
1228
|
+
*/
|
|
1229
|
+
declare function buildIncrementOfferEvent(options: BuildIncrementOfferOptions): UnsignedEvent;
|
|
1230
|
+
interface BuildNarrationOptions {
|
|
1231
|
+
job: FactoryJobRequest;
|
|
1232
|
+
/** The prior event in the thread (quote, offer, or narration). */
|
|
1233
|
+
parentEventId: string;
|
|
1234
|
+
message: string;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Build a free narration event — no artifact, no `amount`/`condition`, MUST
|
|
1238
|
+
* NOT be paid against (§6). This is what makes a provider look alive between
|
|
1239
|
+
* increments.
|
|
1240
|
+
*/
|
|
1241
|
+
declare function buildNarrationEvent(options: BuildNarrationOptions): UnsignedEvent;
|
|
1242
|
+
interface BuildResultEventOptions {
|
|
1243
|
+
job: FactoryJobRequest;
|
|
1244
|
+
/** The original kind:5097 request event, verbatim (the `request` tag). */
|
|
1245
|
+
requestEvent: RelayEvent;
|
|
1246
|
+
/** The last kind:7000 event in the thread (offer or narration). */
|
|
1247
|
+
lastEventId: string;
|
|
1248
|
+
outcome: FactoryJobOutcome;
|
|
1249
|
+
/** How far the job got — `n-reached == of` only when outcome is completed. */
|
|
1250
|
+
reachedIncrement: number;
|
|
1251
|
+
totalIncrements: number;
|
|
1252
|
+
/** Required (and only meaningful) when outcome is `"completed"` (§5.2). */
|
|
1253
|
+
finalArtifact?: {
|
|
1254
|
+
arweaveTxId: string;
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Build the terminal `kind:6097` job result. Reputation (decision 8) is
|
|
1259
|
+
* computed from exactly this event and nothing else.
|
|
1260
|
+
*/
|
|
1261
|
+
declare function buildResultEvent(options: BuildResultEventOptions): UnsignedEvent;
|
|
1262
|
+
|
|
1263
|
+
/**
|
|
1264
|
+
* JobDeliveryPort — the paid-increment seam for `executeFactoryJob` (#52).
|
|
1265
|
+
* The upload and event-publish legs reuse the EXISTING `Publisher`
|
|
1266
|
+
* (publisher.ts — `uploadBlob` for the ciphertext, `publishEvent` for the
|
|
1267
|
+
* quote/offer/result events); this port covers only what `Publisher` can't:
|
|
1268
|
+
* the job-specific crypto (encrypt-under-a-fresh-key → condition) and
|
|
1269
|
+
* waiting for the increment's payment to settle.
|
|
1270
|
+
*
|
|
1271
|
+
* Per decision 5 of toon-meta#262 and §4.2 of `docs/factory-job-protocol.md`,
|
|
1272
|
+
* delivering an increment is: encrypt the artifact with a fresh key, upload
|
|
1273
|
+
* the ciphertext, publish `condition = sha256(key)`, then wait for an ILP
|
|
1274
|
+
* PREPARE whose `executionCondition` matches — releasing `key` as the
|
|
1275
|
+
* FULFILL preimage is simultaneously payment settling and key handoff.
|
|
1276
|
+
* `@toon-protocol/client` 0.25.1 (the latest published version as of this
|
|
1277
|
+
* ticket) does not yet export the earning-API/hashlock helpers merged to
|
|
1278
|
+
* its `main` branch today (toon-client#496, toon-client#498) — this
|
|
1279
|
+
* interface is the injection point a follow-up ticket wires a real
|
|
1280
|
+
* implementation into once a release carries them. Do not re-derive the
|
|
1281
|
+
* encrypt/condition logic locally in an implementation of this port — use
|
|
1282
|
+
* `@toon-protocol/client`'s `encryptArtifact`/`fulfillIncrement` once
|
|
1283
|
+
* available, per the issue's explicit instruction not to hand-roll it.
|
|
1284
|
+
*/
|
|
1285
|
+
/** A freshly-encrypted increment artifact, ready to upload. */
|
|
1286
|
+
interface EncryptedArtifact {
|
|
1287
|
+
/** The encrypted bytes — upload this, never the plaintext. */
|
|
1288
|
+
ciphertext: Uint8Array;
|
|
1289
|
+
/** sha256 of `ciphertext`, hex — lets the buyer detect a bad fetch. */
|
|
1290
|
+
ciphertextSha256: string;
|
|
1291
|
+
/** `sha256(key)` hex, where `key` decrypts the artifact (§4.2 — the join). */
|
|
1292
|
+
conditionHex: string;
|
|
1293
|
+
}
|
|
1294
|
+
/** One increment offered for payment — what {@link JobDeliveryPort.waitForPayment} waits on. */
|
|
1295
|
+
interface OfferedIncrement {
|
|
1296
|
+
/** The kind:7000 status:"partial" event id this offer was published as. */
|
|
1297
|
+
offerEventId: string;
|
|
1298
|
+
/** Must equal the offer's `condition` tag, byte for byte (§4.2). */
|
|
1299
|
+
conditionHex: string;
|
|
1300
|
+
priceUsdc: string;
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Injected transport for `executeFactoryJob` — implemented against
|
|
1304
|
+
* `@toon-protocol/client`'s earning API in a follow-up ticket. Must be safe
|
|
1305
|
+
* to call sequentially (increments are delivered and paid for one at a
|
|
1306
|
+
* time, never in parallel) and should throw on a provider-side failure —
|
|
1307
|
+
* `executeFactoryJob` does not retry an increment.
|
|
1308
|
+
*/
|
|
1309
|
+
interface JobDeliveryPort {
|
|
1310
|
+
/**
|
|
1311
|
+
* Encrypt `bytes` under a fresh, single-use key and return the
|
|
1312
|
+
* ciphertext plus `condition = sha256(key)`. Per the issue's gotchas:
|
|
1313
|
+
* NEVER reuse a key across increments — paying for the plan must not
|
|
1314
|
+
* unlock the implementation.
|
|
1315
|
+
*/
|
|
1316
|
+
encryptArtifact(bytes: Uint8Array): Promise<EncryptedArtifact>;
|
|
1317
|
+
/**
|
|
1318
|
+
* Wait for the buyer's ILP PREPARE against `offer.conditionHex` to
|
|
1319
|
+
* settle. Resolves `true` once the FULFILL is released (the increment is
|
|
1320
|
+
* paid); resolves `false` if the buyer never pays (implementation-defined
|
|
1321
|
+
* timeout/backoff) — `executeFactoryJob` treats `false` as the signal to
|
|
1322
|
+
* halt (decision 6: one unpaid increment and work stops).
|
|
1323
|
+
*/
|
|
1324
|
+
waitForPayment(offer: OfferedIncrement): Promise<boolean>;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
/**
|
|
1328
|
+
* executeFactoryJob — the adapter's imperative shell (#52): runs the
|
|
1329
|
+
* factory's own milestones (`.sandcastle/main.ts`'s plan → per-ticket
|
|
1330
|
+
* implement+review → merge phases) through `hooks`, and turns each
|
|
1331
|
+
* milestone boundary into a paid increment via `publisher` (existing,
|
|
1332
|
+
* publisher.ts — upload + event publish) and `delivery` (new,
|
|
1333
|
+
* factory-job-delivery.ts — encrypt + payment wait).
|
|
1334
|
+
*
|
|
1335
|
+
* Mirrors `executePush`'s split from `planPush`: `planFactoryJob` is the
|
|
1336
|
+
* network-free planning half; this is the half that spends money.
|
|
1337
|
+
*
|
|
1338
|
+
* Decision 6 (toon-meta#262): work happens BEFORE the packet, so every
|
|
1339
|
+
* increment's artifact is produced and uploaded before that increment is
|
|
1340
|
+
* offered for payment. Decision 7: the schedule is fixed once the quote is
|
|
1341
|
+
* published — this never reprices mid-job. Per the issue: "Stop on
|
|
1342
|
+
* non-payment. One unpaid increment and work halts" — the symmetric
|
|
1343
|
+
* protection that neither side is ever exposed for more than one increment.
|
|
1344
|
+
*/
|
|
1345
|
+
|
|
1346
|
+
/** One milestone's deliverable, plus the gate result that ran against it, when one did. */
|
|
1347
|
+
interface FactoryJobWork {
|
|
1348
|
+
artifact: Uint8Array;
|
|
1349
|
+
/**
|
|
1350
|
+
* The reproducible gate result (#53) for this milestone's work, per
|
|
1351
|
+
* `FACTORY.md`'s pinned pipeline (lint/typecheck/test/build). Omitted for
|
|
1352
|
+
* `plan` — there is no code to gate against a brief.
|
|
1353
|
+
*/
|
|
1354
|
+
gate?: GateResult;
|
|
1355
|
+
}
|
|
1356
|
+
/** Runs one factory milestone and returns its deliverable, plus that milestone's gate result. */
|
|
1357
|
+
interface FactoryJobHooks {
|
|
1358
|
+
/**
|
|
1359
|
+
* Runs the factory's plan phase for `job` (mirrors `.sandcastle/main.ts`'s
|
|
1360
|
+
* Phase 1): returns the per-ticket fan-out the implement milestone splits
|
|
1361
|
+
* into, plus the plan document itself as increment 1's artifact. No gate
|
|
1362
|
+
* result — planning produces no code to lint/typecheck/test/build.
|
|
1363
|
+
*/
|
|
1364
|
+
plan(job: FactoryJobRequest): Promise<{
|
|
1365
|
+
tickets: FactoryTicket[];
|
|
1366
|
+
artifact: Uint8Array;
|
|
1367
|
+
}>;
|
|
1368
|
+
/**
|
|
1369
|
+
* Runs one ticket's implement sandbox pipeline (mirrors Phase 2's
|
|
1370
|
+
* per-issue `sandbox.run`, which ends in the gate per `FACTORY.md`) and
|
|
1371
|
+
* returns its deliverable (e.g. the ticket's diff) plus that run's gate
|
|
1372
|
+
* result.
|
|
1373
|
+
*/
|
|
1374
|
+
implement(ticket: FactoryTicket): Promise<FactoryJobWork>;
|
|
1375
|
+
/**
|
|
1376
|
+
* Runs the factory's merge/review phase over every completed ticket
|
|
1377
|
+
* (mirrors Phase 3, which re-runs the gate per `merge-prompt.md`) and
|
|
1378
|
+
* returns the final deliverable plus that run's gate result.
|
|
1379
|
+
*/
|
|
1380
|
+
review(tickets: FactoryTicket[]): Promise<FactoryJobWork>;
|
|
1381
|
+
}
|
|
1382
|
+
interface ExecuteFactoryJobOptions {
|
|
1383
|
+
job: FactoryJobRequest;
|
|
1384
|
+
/** The original kind:5097 request event, verbatim — carried in the result's `request` tag. */
|
|
1385
|
+
requestEvent: RelayEvent;
|
|
1386
|
+
hooks: FactoryJobHooks;
|
|
1387
|
+
publisher: Publisher;
|
|
1388
|
+
delivery: JobDeliveryPort;
|
|
1389
|
+
relayUrls: string[];
|
|
1390
|
+
}
|
|
1391
|
+
interface FactoryJobExecution {
|
|
1392
|
+
outcome: FactoryJobOutcome;
|
|
1393
|
+
/** The last increment successfully paid for (0 if the buyer never paid increment 1). */
|
|
1394
|
+
reachedIncrement: number;
|
|
1395
|
+
totalIncrements: number;
|
|
1396
|
+
/** The published kind:6097 result event's id. */
|
|
1397
|
+
resultEventId: string;
|
|
1398
|
+
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Run a job end to end: plan → quote → one increment per milestone boundary
|
|
1401
|
+
* (encrypt, upload, offer, wait for payment) → terminal kind:6097 result.
|
|
1402
|
+
* Halts at the first unpaid increment (`abandoned-buyer`) or the first
|
|
1403
|
+
* hook failure once work is underway (`abandoned-provider`) — see §5.2 of
|
|
1404
|
+
* `docs/factory-job-protocol.md` for the three terminal outcomes.
|
|
1405
|
+
*
|
|
1406
|
+
* Throws if `hooks.plan` itself fails — with no quote ever published, there
|
|
1407
|
+
* is no prior kind:7000 event for a result's `reply` e-tag to reference, so
|
|
1408
|
+
* no protocol event can be honestly emitted for that failure.
|
|
1409
|
+
*/
|
|
1410
|
+
declare function executeFactoryJob(options: ExecuteFactoryJobOptions): Promise<FactoryJobExecution>;
|
|
1411
|
+
|
|
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` — this is the only path that
|
|
1451
|
+
* releases a key, and it releases exactly the one the buyer paid for.
|
|
1452
|
+
*/
|
|
1453
|
+
readonly handleJob: JobHandler;
|
|
1454
|
+
encryptArtifact(bytes: Uint8Array): Promise<EncryptedArtifact>;
|
|
1455
|
+
waitForPayment(offer: OfferedIncrement): Promise<boolean>;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
* Buyer-side payment for one factory-job increment offer (#56) — the other
|
|
1460
|
+
* half of the hashlock join `ClientJobDeliveryPort` (the provider side)
|
|
1461
|
+
* answers. Built against `@toon-protocol/client`'s real `buildIncrementPrepare`
|
|
1462
|
+
* / `decryptArtifact` (toon-client#495): this module does not re-derive the
|
|
1463
|
+
* PREPARE fields or the AEAD decrypt itself.
|
|
1464
|
+
*
|
|
1465
|
+
* Per docs/factory-job-protocol.md §4.2 (toon-meta), paying an offer is:
|
|
1466
|
+
* `executionCondition` equal to the offer's `condition` tag byte for byte,
|
|
1467
|
+
* and `data` carrying a reference back to the offer's own kind:7000 event id
|
|
1468
|
+
* — "the payment names the job." The provider returns the decryption key as
|
|
1469
|
+
* the ILP fulfillment; revealing it is the same act as accepting payment.
|
|
1470
|
+
*/
|
|
1471
|
+
|
|
1472
|
+
/** What a buyer reads off an advertised kind:7000 `status:"partial"` increment offer. */
|
|
1473
|
+
interface PayableIncrementOffer extends IncrementOfferTags {
|
|
1474
|
+
/** The offer event's own id — carried in the PREPARE's `data` field (§4.2). */
|
|
1475
|
+
offerEventId: string;
|
|
1476
|
+
}
|
|
1477
|
+
/** The outcome of paying one increment offer. */
|
|
1478
|
+
type PaidIncrementOffer = {
|
|
1479
|
+
paid: true;
|
|
1480
|
+
key: Uint8Array;
|
|
1481
|
+
} | {
|
|
1482
|
+
paid: false;
|
|
1483
|
+
};
|
|
1484
|
+
/** The narrow slice of `ToonClient` a buyer needs to pay an increment offer. */
|
|
1485
|
+
interface SwapPacketSender {
|
|
1486
|
+
sendSwapPacket(params: {
|
|
1487
|
+
destination: string;
|
|
1488
|
+
amount: bigint;
|
|
1489
|
+
toonData: Uint8Array;
|
|
1490
|
+
executionCondition?: Uint8Array;
|
|
1491
|
+
timeout?: number;
|
|
1492
|
+
}): Promise<{
|
|
1493
|
+
accepted: boolean;
|
|
1494
|
+
fulfillment?: string;
|
|
1495
|
+
code?: string;
|
|
1496
|
+
message?: string;
|
|
1497
|
+
}>;
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* Buyer: pay one advertised increment offer. Sends an ILP PREPARE addressed
|
|
1501
|
+
* to `providerDestination` with `executionCondition` equal to the offer's
|
|
1502
|
+
* `condition` tag and `data` referencing the offer's event id, per §4.2.
|
|
1503
|
+
* Resolves `{ paid: false }` on a REJECT (expired/no such increment/wrong
|
|
1504
|
+
* fulfillment) rather than throwing — a buyer declining or failing to pay is
|
|
1505
|
+
* a protocol outcome (`abandoned-buyer`), not an error.
|
|
1506
|
+
*/
|
|
1507
|
+
declare function payIncrementOffer(client: SwapPacketSender, providerDestination: string, offer: PayableIncrementOffer): Promise<PaidIncrementOffer>;
|
|
1508
|
+
/**
|
|
1509
|
+
* Buyer: decrypt an increment's artifact using the key revealed as the ILP
|
|
1510
|
+
* fulfillment. `offerConditionHex` MUST be the offer's own `condition` tag
|
|
1511
|
+
* as read from the relay before payment — never re-derived from `key` —
|
|
1512
|
+
* so a provider who reveals a different key than it advertised is caught
|
|
1513
|
+
* here rather than trusted.
|
|
1514
|
+
*/
|
|
1515
|
+
declare function decryptIncrementArtifact(ciphertext: Uint8Array, key: Uint8Array, offerConditionHex: string): Uint8Array;
|
|
1516
|
+
|
|
1517
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -51,9 +51,383 @@ import {
|
|
|
51
51
|
parseMaintainers,
|
|
52
52
|
queryRelay
|
|
53
53
|
} from "./chunk-2KJJ7LSO.js";
|
|
54
|
+
|
|
55
|
+
// src/factory-job-gate.ts
|
|
56
|
+
function gatePassed(gate) {
|
|
57
|
+
return gate.checks.every((check) => check.pass);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/factory-job-events.ts
|
|
61
|
+
var FACTORY_JOB_REQUEST_KIND = 5097;
|
|
62
|
+
var FACTORY_JOB_RESULT_KIND = 6097;
|
|
63
|
+
var FACTORY_JOB_FEEDBACK_KIND = 7e3;
|
|
64
|
+
function parseFactoryJobRequest(event) {
|
|
65
|
+
const briefTag = event.tags.find((t) => t[0] === "i" && t[2] === "text");
|
|
66
|
+
if (!briefTag?.[1]) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`kind:${FACTORY_JOB_REQUEST_KIND} event ${event.id} is missing its required brief ("i", "<brief>", "text") tag`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
const bidTag = event.tags.find((t) => t[0] === "bid");
|
|
72
|
+
if (!bidTag?.[1]) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`kind:${FACTORY_JOB_REQUEST_KIND} event ${event.id} is missing its required "bid" tag`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const params = event.tags.filter((t) => t[0] === "param");
|
|
78
|
+
const paramValue = (name) => params.find((t) => t[1] === name)?.[2];
|
|
79
|
+
const repo = paramValue("repo");
|
|
80
|
+
const target = paramValue("target");
|
|
81
|
+
const constraints = paramValue("constraints");
|
|
82
|
+
const outputMime = event.tags.find((t) => t[0] === "output")?.[1];
|
|
83
|
+
return {
|
|
84
|
+
requestEventId: event.id,
|
|
85
|
+
buyerPubkey: event.pubkey,
|
|
86
|
+
brief: briefTag[1],
|
|
87
|
+
bidMicroUsdc: bidTag[1],
|
|
88
|
+
...repo !== void 0 ? { repo } : {},
|
|
89
|
+
...target !== void 0 ? { target } : {},
|
|
90
|
+
...constraints !== void 0 ? { constraints } : {},
|
|
91
|
+
...outputMime !== void 0 ? { outputMime } : {}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function buildQuoteEvent(job, schedule) {
|
|
95
|
+
return {
|
|
96
|
+
kind: FACTORY_JOB_FEEDBACK_KIND,
|
|
97
|
+
content: JSON.stringify({
|
|
98
|
+
increments: schedule.map((s) => ({
|
|
99
|
+
n: s.n,
|
|
100
|
+
of: s.of,
|
|
101
|
+
milestone: s.milestone,
|
|
102
|
+
priceUsdc: s.priceUsdc
|
|
103
|
+
}))
|
|
104
|
+
}),
|
|
105
|
+
tags: [
|
|
106
|
+
["e", job.requestEventId, "", "root"],
|
|
107
|
+
["p", job.buyerPubkey],
|
|
108
|
+
["status", "quote"]
|
|
109
|
+
],
|
|
110
|
+
created_at: Math.floor(Date.now() / 1e3)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function assertReproducible(gate) {
|
|
114
|
+
if (gate.checks.length === 0) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
"gate result must record at least one check for the offer to be reproducible"
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (!gate.commit) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
"gate result must record the commit it ran against for the offer to be reproducible"
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function buildIncrementOfferEvent(options) {
|
|
126
|
+
const { job, parentEventId, increment, artifact, gate } = options;
|
|
127
|
+
if (gate) assertReproducible(gate);
|
|
128
|
+
const tags = [
|
|
129
|
+
["e", job.requestEventId, "", "root"],
|
|
130
|
+
["e", parentEventId, "", "reply"],
|
|
131
|
+
["p", job.buyerPubkey],
|
|
132
|
+
["status", "partial"],
|
|
133
|
+
["increment", String(increment.n), String(increment.of)],
|
|
134
|
+
["i", artifact.arweaveTxId, "url"],
|
|
135
|
+
["i", artifact.ciphertextSha256, "text", "", "hash"],
|
|
136
|
+
["amount", increment.priceUsdc, "usdc"],
|
|
137
|
+
["condition", artifact.conditionHex]
|
|
138
|
+
];
|
|
139
|
+
if (gate) tags.push(["gate", gatePassed(gate) ? "pass" : "fail"]);
|
|
140
|
+
return {
|
|
141
|
+
kind: FACTORY_JOB_FEEDBACK_KIND,
|
|
142
|
+
content: gate ? JSON.stringify({ gate }) : "",
|
|
143
|
+
tags,
|
|
144
|
+
created_at: Math.floor(Date.now() / 1e3)
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function buildNarrationEvent(options) {
|
|
148
|
+
const { job, parentEventId, message } = options;
|
|
149
|
+
return {
|
|
150
|
+
kind: FACTORY_JOB_FEEDBACK_KIND,
|
|
151
|
+
content: message,
|
|
152
|
+
tags: [
|
|
153
|
+
["e", job.requestEventId, "", "root"],
|
|
154
|
+
["e", parentEventId, "", "reply"],
|
|
155
|
+
["p", job.buyerPubkey],
|
|
156
|
+
["status", "processing"]
|
|
157
|
+
],
|
|
158
|
+
created_at: Math.floor(Date.now() / 1e3)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function buildResultEvent(options) {
|
|
162
|
+
const {
|
|
163
|
+
job,
|
|
164
|
+
requestEvent,
|
|
165
|
+
lastEventId,
|
|
166
|
+
outcome,
|
|
167
|
+
reachedIncrement,
|
|
168
|
+
totalIncrements,
|
|
169
|
+
finalArtifact
|
|
170
|
+
} = options;
|
|
171
|
+
const tags = [
|
|
172
|
+
["e", job.requestEventId, "", "root"],
|
|
173
|
+
["e", lastEventId, "", "reply"],
|
|
174
|
+
["p", job.buyerPubkey],
|
|
175
|
+
["request", JSON.stringify(requestEvent)],
|
|
176
|
+
["outcome", outcome],
|
|
177
|
+
["increment", String(reachedIncrement), String(totalIncrements)]
|
|
178
|
+
];
|
|
179
|
+
if (outcome === "completed" && finalArtifact) {
|
|
180
|
+
tags.push(["i", finalArtifact.arweaveTxId, "url"]);
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
kind: FACTORY_JOB_RESULT_KIND,
|
|
184
|
+
content: "",
|
|
185
|
+
tags,
|
|
186
|
+
created_at: Math.floor(Date.now() / 1e3)
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/factory-job-plan.ts
|
|
191
|
+
var PLAN_SHARE_BPS = 1000n;
|
|
192
|
+
var REVIEW_SHARE_BPS = 1000n;
|
|
193
|
+
var BPS_DENOM = 10000n;
|
|
194
|
+
function planFactoryJob(bidMicroUsdc, tickets) {
|
|
195
|
+
if (tickets.length === 0) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
"planFactoryJob requires at least one implementation ticket from the plan phase"
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
let bid;
|
|
201
|
+
try {
|
|
202
|
+
bid = BigInt(bidMicroUsdc);
|
|
203
|
+
} catch {
|
|
204
|
+
throw new Error(`bidMicroUsdc must be an integer string, got ${bidMicroUsdc}`);
|
|
205
|
+
}
|
|
206
|
+
if (bid <= 0n) {
|
|
207
|
+
throw new Error(`bidMicroUsdc must be positive, got ${bidMicroUsdc}`);
|
|
208
|
+
}
|
|
209
|
+
const planPrice = bid * PLAN_SHARE_BPS / BPS_DENOM;
|
|
210
|
+
const reviewPrice = bid * REVIEW_SHARE_BPS / BPS_DENOM;
|
|
211
|
+
const implementTotal = bid - planPrice - reviewPrice;
|
|
212
|
+
const ticketCount = BigInt(tickets.length);
|
|
213
|
+
const base = implementTotal / ticketCount;
|
|
214
|
+
const remainder = implementTotal % ticketCount;
|
|
215
|
+
const total = tickets.length + 2;
|
|
216
|
+
const schedule = [
|
|
217
|
+
{ n: 1, of: total, milestone: "plan", priceUsdc: planPrice.toString() }
|
|
218
|
+
];
|
|
219
|
+
tickets.forEach((ticket, i) => {
|
|
220
|
+
const price = base + (BigInt(i) < remainder ? 1n : 0n);
|
|
221
|
+
schedule.push({
|
|
222
|
+
n: i + 2,
|
|
223
|
+
of: total,
|
|
224
|
+
milestone: "implement",
|
|
225
|
+
ticket,
|
|
226
|
+
priceUsdc: price.toString()
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
schedule.push({
|
|
230
|
+
n: total,
|
|
231
|
+
of: total,
|
|
232
|
+
milestone: "review",
|
|
233
|
+
priceUsdc: reviewPrice.toString()
|
|
234
|
+
});
|
|
235
|
+
return schedule;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/factory-job-execute.ts
|
|
239
|
+
async function workFor(spec, tickets, planArtifact, hooks) {
|
|
240
|
+
if (spec.milestone === "plan") return { artifact: planArtifact };
|
|
241
|
+
if (spec.milestone === "implement") {
|
|
242
|
+
if (!spec.ticket) {
|
|
243
|
+
throw new Error(
|
|
244
|
+
`internal: implement increment ${spec.n} is missing its ticket`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
return hooks.implement(spec.ticket);
|
|
248
|
+
}
|
|
249
|
+
return hooks.review(tickets);
|
|
250
|
+
}
|
|
251
|
+
async function executeFactoryJob(options) {
|
|
252
|
+
const { job, requestEvent, hooks, publisher, delivery, relayUrls } = options;
|
|
253
|
+
if (!publisher.uploadBlob) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
"the active publisher cannot upload an encrypted increment artifact (uploadBlob unavailable) \u2014 executeFactoryJob requires the standalone publisher"
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const uploadBlob = publisher.uploadBlob.bind(publisher);
|
|
259
|
+
const { tickets, artifact: planArtifact } = await hooks.plan(job);
|
|
260
|
+
const schedule = planFactoryJob(job.bidMicroUsdc, tickets);
|
|
261
|
+
const totalIncrements = schedule.length;
|
|
262
|
+
const quoteEvent = buildQuoteEvent(job, schedule);
|
|
263
|
+
const quoteReceipt = await publisher.publishEvent(quoteEvent, relayUrls);
|
|
264
|
+
let parentEventId = quoteReceipt.eventId;
|
|
265
|
+
let reachedIncrement = 0;
|
|
266
|
+
let finalArtifactTxId;
|
|
267
|
+
const haltWith = async (outcome) => {
|
|
268
|
+
const resultEvent = buildResultEvent({
|
|
269
|
+
job,
|
|
270
|
+
requestEvent,
|
|
271
|
+
lastEventId: parentEventId,
|
|
272
|
+
outcome,
|
|
273
|
+
reachedIncrement,
|
|
274
|
+
totalIncrements,
|
|
275
|
+
...outcome === "completed" && finalArtifactTxId ? { finalArtifact: { arweaveTxId: finalArtifactTxId } } : {}
|
|
276
|
+
});
|
|
277
|
+
const resultReceipt = await publisher.publishEvent(
|
|
278
|
+
resultEvent,
|
|
279
|
+
relayUrls
|
|
280
|
+
);
|
|
281
|
+
return {
|
|
282
|
+
outcome,
|
|
283
|
+
reachedIncrement,
|
|
284
|
+
totalIncrements,
|
|
285
|
+
resultEventId: resultReceipt.eventId
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
for (const spec of schedule) {
|
|
289
|
+
let work;
|
|
290
|
+
try {
|
|
291
|
+
work = await workFor(spec, tickets, planArtifact, hooks);
|
|
292
|
+
} catch {
|
|
293
|
+
return haltWith("abandoned-provider");
|
|
294
|
+
}
|
|
295
|
+
const encrypted = await delivery.encryptArtifact(work.artifact);
|
|
296
|
+
const uploadReceipt = await uploadBlob({
|
|
297
|
+
body: Buffer.from(encrypted.ciphertext),
|
|
298
|
+
contentType: "application/octet-stream",
|
|
299
|
+
...job.repo ? { repoId: job.repo } : {}
|
|
300
|
+
});
|
|
301
|
+
const offerEvent = buildIncrementOfferEvent({
|
|
302
|
+
job,
|
|
303
|
+
parentEventId,
|
|
304
|
+
increment: spec,
|
|
305
|
+
artifact: {
|
|
306
|
+
arweaveTxId: uploadReceipt.txId,
|
|
307
|
+
ciphertextSha256: encrypted.ciphertextSha256,
|
|
308
|
+
conditionHex: encrypted.conditionHex
|
|
309
|
+
},
|
|
310
|
+
...work.gate ? { gate: work.gate } : {}
|
|
311
|
+
});
|
|
312
|
+
const offerReceipt = await publisher.publishEvent(offerEvent, relayUrls);
|
|
313
|
+
const paid = await delivery.waitForPayment({
|
|
314
|
+
offerEventId: offerReceipt.eventId,
|
|
315
|
+
conditionHex: encrypted.conditionHex,
|
|
316
|
+
priceUsdc: spec.priceUsdc
|
|
317
|
+
});
|
|
318
|
+
if (!paid) {
|
|
319
|
+
parentEventId = offerReceipt.eventId;
|
|
320
|
+
return haltWith("abandoned-buyer");
|
|
321
|
+
}
|
|
322
|
+
reachedIncrement = spec.n;
|
|
323
|
+
parentEventId = offerReceipt.eventId;
|
|
324
|
+
if (spec.milestone === "review") finalArtifactTxId = uploadReceipt.txId;
|
|
325
|
+
}
|
|
326
|
+
return haltWith("completed");
|
|
327
|
+
}
|
|
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` — this is the only path that
|
|
350
|
+
* releases a key, and it releases exactly the one the buyer paid for.
|
|
351
|
+
*/
|
|
352
|
+
handleJob = (job) => {
|
|
353
|
+
const conditionHex = toHex(job.executionCondition);
|
|
354
|
+
if (!this.armed || conditionHex !== this.armed.conditionHex) {
|
|
355
|
+
throw new Error(
|
|
356
|
+
`no factory-job increment is awaiting payment for condition ${conditionHex}`
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
const { key, resolve } = this.armed;
|
|
360
|
+
this.armed = void 0;
|
|
361
|
+
resolve(true);
|
|
362
|
+
return { fulfillment: fulfillIncrement(key) };
|
|
363
|
+
};
|
|
364
|
+
async encryptArtifact(bytes) {
|
|
365
|
+
const encrypted = clientEncryptArtifact(bytes);
|
|
366
|
+
const conditionHex = toHex(encrypted.condition);
|
|
367
|
+
this.staged = { key: encrypted.key, conditionHex };
|
|
368
|
+
return {
|
|
369
|
+
ciphertext: encrypted.ciphertext,
|
|
370
|
+
ciphertextSha256: createHash("sha256").update(encrypted.ciphertext).digest("hex"),
|
|
371
|
+
conditionHex
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
async waitForPayment(offer) {
|
|
375
|
+
if (!this.staged || offer.conditionHex !== this.staged.conditionHex) {
|
|
376
|
+
throw new Error(
|
|
377
|
+
"waitForPayment was called for a condition that does not match the most recent encryptArtifact() call"
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
const { key, conditionHex } = this.staged;
|
|
381
|
+
this.staged = void 0;
|
|
382
|
+
return new Promise((resolve) => {
|
|
383
|
+
const timer = setTimeout(() => {
|
|
384
|
+
this.armed = void 0;
|
|
385
|
+
resolve(false);
|
|
386
|
+
}, this.paymentTimeoutMs);
|
|
387
|
+
this.armed = {
|
|
388
|
+
key,
|
|
389
|
+
conditionHex,
|
|
390
|
+
resolve: (paid) => {
|
|
391
|
+
clearTimeout(timer);
|
|
392
|
+
resolve(paid);
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// src/factory-job-pay.ts
|
|
400
|
+
import {
|
|
401
|
+
buildIncrementPrepare,
|
|
402
|
+
decryptArtifact
|
|
403
|
+
} from "@toon-protocol/client";
|
|
404
|
+
async function payIncrementOffer(client, providerDestination, offer) {
|
|
405
|
+
const { executionCondition, amount } = buildIncrementPrepare(offer);
|
|
406
|
+
const result = await client.sendSwapPacket({
|
|
407
|
+
destination: providerDestination,
|
|
408
|
+
amount,
|
|
409
|
+
toonData: new TextEncoder().encode(offer.offerEventId),
|
|
410
|
+
executionCondition
|
|
411
|
+
});
|
|
412
|
+
if (!result.accepted || !result.fulfillment) {
|
|
413
|
+
return { paid: false };
|
|
414
|
+
}
|
|
415
|
+
return { paid: true, key: Buffer.from(result.fulfillment, "base64") };
|
|
416
|
+
}
|
|
417
|
+
function decryptIncrementArtifact(ciphertext, key, offerConditionHex) {
|
|
418
|
+
return decryptArtifact(
|
|
419
|
+
ciphertext,
|
|
420
|
+
key,
|
|
421
|
+
Buffer.from(offerConditionHex, "hex")
|
|
422
|
+
);
|
|
423
|
+
}
|
|
54
424
|
export {
|
|
55
425
|
COMMENT_KIND,
|
|
426
|
+
ClientJobDeliveryPort,
|
|
56
427
|
DEFAULT_CONCURRENCY,
|
|
428
|
+
FACTORY_JOB_FEEDBACK_KIND,
|
|
429
|
+
FACTORY_JOB_REQUEST_KIND,
|
|
430
|
+
FACTORY_JOB_RESULT_KIND,
|
|
57
431
|
GitError,
|
|
58
432
|
GitRepoReader,
|
|
59
433
|
MAINTAINERS_TAG,
|
|
@@ -65,27 +439,37 @@ export {
|
|
|
65
439
|
REPOSITORY_STATE_KIND,
|
|
66
440
|
authorizedStatusAuthors,
|
|
67
441
|
buildComment,
|
|
442
|
+
buildIncrementOfferEvent,
|
|
68
443
|
buildIssue,
|
|
444
|
+
buildNarrationEvent,
|
|
69
445
|
buildPatch,
|
|
446
|
+
buildQuoteEvent,
|
|
70
447
|
buildRepoAnnouncement,
|
|
71
448
|
buildRepoRefs,
|
|
449
|
+
buildResultEvent,
|
|
72
450
|
buildStatus,
|
|
73
451
|
collectRepoObjects,
|
|
74
452
|
createGitBlob,
|
|
75
453
|
createGitCommit,
|
|
76
454
|
createGitTag,
|
|
77
455
|
createGitTree,
|
|
456
|
+
decryptIncrementArtifact,
|
|
78
457
|
downloadGitObjects,
|
|
458
|
+
executeFactoryJob,
|
|
79
459
|
executePush,
|
|
80
460
|
fetchRemoteState,
|
|
81
461
|
fetchTxBytes,
|
|
462
|
+
gatePassed,
|
|
82
463
|
hashGitObject,
|
|
83
464
|
hexToNpub,
|
|
84
465
|
isSafeRefname,
|
|
85
466
|
missingObjectsMessage,
|
|
86
467
|
npubToHex,
|
|
87
468
|
ownerToHex,
|
|
469
|
+
parseFactoryJobRequest,
|
|
88
470
|
parseMaintainers,
|
|
471
|
+
payIncrementOffer,
|
|
472
|
+
planFactoryJob,
|
|
89
473
|
planPush,
|
|
90
474
|
queryRelay,
|
|
91
475
|
referencedShas,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","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","../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 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` — this is the only path that\n * releases a key, and it 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 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 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 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;;;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,MAAM,MAAM;AACvE,MAAI,CAAC,WAAW,CAAC,GAAG;AAClB,UAAM,IAAI;AAAA,MACR,QAAQ,wBAAwB,UAAU,MAAM,EAAE;AAAA,IACpD;AAAA,EACF;AACA,QAAM,SAAS,MAAM,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,KAAK;AACpD,MAAI,CAAC,SAAS,CAAC,GAAG;AAChB,UAAM,IAAI;AAAA,MACR,QAAQ,wBAAwB,UAAU,MAAM,EAAE;AAAA,IACpD;AAAA,EACF;AAEA,QAAM,SAAS,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,MAAM,OAAO;AACxD,QAAM,a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"3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "Git-to-TOON write path core — build git objects and NIP-34 events for the Rig control plane",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Jonathan Green",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@toon-format/toon": "^1.0.0",
|
|
46
|
-
"@toon-protocol/client": "^0.
|
|
46
|
+
"@toon-protocol/client": "^0.26.0",
|
|
47
47
|
"@toon-protocol/core": "^3.1.2",
|
|
48
48
|
"nostr-tools": "^2.20.0"
|
|
49
49
|
},
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
"@types/node": "^20.0.0",
|
|
58
58
|
"@types/ws": "^8.18.1",
|
|
59
59
|
"tsup": "^8.0.0",
|
|
60
|
+
"tsx": "^4.19.2",
|
|
60
61
|
"typescript": "^5.3.0",
|
|
61
62
|
"vitest": "^1.0.0",
|
|
62
63
|
"ws": "^8.18.0"
|
|
@@ -65,6 +66,7 @@
|
|
|
65
66
|
"build": "tsup",
|
|
66
67
|
"test": "vitest run",
|
|
67
68
|
"test:coverage": "vitest run --coverage",
|
|
68
|
-
"typecheck": "tsc --noEmit -p tsconfig.typecheck.json"
|
|
69
|
+
"typecheck": "tsc --noEmit -p tsconfig.typecheck.json",
|
|
70
|
+
"factory-job-proof": "tsx scripts/factory-job-proof.ts"
|
|
69
71
|
}
|
|
70
72
|
}
|