fascicle 0.18.0 → 0.19.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 (51) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +27 -1
  3. package/dist/AGENTS.md +71 -94
  4. package/dist/a2a.d.ts +358 -0
  5. package/dist/a2a.js +2234 -0
  6. package/dist/a2a.js.map +1 -0
  7. package/dist/acp.d.ts +120 -9
  8. package/dist/acp.js +927 -154
  9. package/dist/acp.js.map +1 -1
  10. package/dist/adapters-DYFauXE1.js +1147 -0
  11. package/dist/adapters-DYFauXE1.js.map +1 -0
  12. package/dist/adapters.d.ts +124 -36
  13. package/dist/adapters.js +1 -1
  14. package/dist/agents.d.ts +2 -2
  15. package/dist/agents.js +1 -1
  16. package/dist/{core-BH7sWpEG.js → core-xTtRZqdb.js} +217 -99
  17. package/dist/core-xTtRZqdb.js.map +1 -0
  18. package/dist/diagram_bin.js +1 -1
  19. package/dist/durable-CgU0kO2H.d.ts +144 -0
  20. package/dist/{engine-fOjYHcv6.js → engine-B_Yl7acj.js} +128 -42
  21. package/dist/engine-B_Yl7acj.js.map +1 -0
  22. package/dist/index.d.ts +9 -136
  23. package/dist/index.js +6 -5
  24. package/dist/index.js.map +1 -1
  25. package/dist/{invoke-7iu8db6V.js → invoke-Bbk0RuVL.js} +3 -3
  26. package/dist/{invoke-7iu8db6V.js.map → invoke-Bbk0RuVL.js.map} +1 -1
  27. package/dist/{leading_system-BeQUSAim.js → leading_system-m-x7HzbQ.js} +16 -4
  28. package/dist/leading_system-m-x7HzbQ.js.map +1 -0
  29. package/dist/mcp-D1hN5ywq.js +662 -0
  30. package/dist/mcp-D1hN5ywq.js.map +1 -0
  31. package/dist/mcp.d.ts +115 -5
  32. package/dist/mcp.js +2 -2
  33. package/dist/otel.d.ts +1 -1
  34. package/dist/stdio.d.ts +1 -1
  35. package/dist/stdio.js +2 -2
  36. package/dist/testing.d.ts +7 -5
  37. package/dist/testing.js +618 -318
  38. package/dist/testing.js.map +1 -1
  39. package/dist/{types-BUXt_zgJ.d.ts → types-BtRpSctv.d.ts} +38 -5
  40. package/dist/{types-Dk9tLpUq.d.ts → types-CDTW7XWB.d.ts} +2 -2
  41. package/dist/ui.d.ts +2 -2
  42. package/dist/viewer.js +2 -2
  43. package/dist/web.d.ts +1 -1
  44. package/package.json +17 -5
  45. package/dist/adapters-CpKpDk7r.js +0 -461
  46. package/dist/adapters-CpKpDk7r.js.map +0 -1
  47. package/dist/core-BH7sWpEG.js.map +0 -1
  48. package/dist/engine-fOjYHcv6.js.map +0 -1
  49. package/dist/leading_system-BeQUSAim.js.map +0 -1
  50. package/dist/mcp-CHJ88HiG.js +0 -262
  51. package/dist/mcp-CHJ88HiG.js.map +0 -1
@@ -1534,7 +1534,9 @@ function link_external_abort(controller, external) {
1534
1534
  * Construct a run context and execute a flow to settlement.
1535
1535
  *
1536
1536
  * Shared engine behind `run` and `run.stream`; `high_water_mark` selects the
1537
- * mode (null means no streaming channel). Owns the full run lifecycle:
1537
+ * mode (null means no streaming channel). `ctx.streaming` is on when a
1538
+ * channel is, and when `record_chunks` asks for chunks without one. Owns the
1539
+ * full run lifecycle:
1538
1540
  * abort wiring, logger decoration, cleanup registration, signal handlers,
1539
1541
  * the `flow_structure` event that opens an observed run, and the
1540
1542
  * settle-time teardown ordering (cleanup handlers first, then unlinking and
@@ -1543,21 +1545,22 @@ function link_external_abort(controller, external) {
1543
1545
  function start_run$1(flow, input, options, high_water_mark) {
1544
1546
  const install_signal_handlers = options.install_signal_handlers !== false;
1545
1547
  const base_logger = options.trajectory ?? noop_logger;
1546
- const streaming = high_water_mark !== null;
1548
+ const has_channel = high_water_mark !== null;
1549
+ const streaming = has_channel || options.record_chunks === true;
1547
1550
  const controller = new AbortController();
1548
1551
  const unlink_abort = link_external_abort(controller, options.abort);
1549
1552
  const run_id = randomUUID();
1550
1553
  let stream_events = empty_async_iterable();
1551
1554
  let close_stream = null;
1552
1555
  let inner = base_logger;
1553
- if (streaming) {
1556
+ if (has_channel) {
1554
1557
  const channel = create_streaming_channel(base_logger, high_water_mark);
1555
1558
  inner = channel.logger;
1556
1559
  stream_events = channel.events;
1557
1560
  close_stream = channel.close;
1558
1561
  }
1559
1562
  const logger = decorate_logger(inner, run_id);
1560
- if (options.trajectory !== void 0 || streaming) logger.record({
1563
+ if (options.trajectory !== void 0 || has_channel) logger.record({
1561
1564
  kind: "flow_structure",
1562
1565
  structure: describe.json(flow)
1563
1566
  });
@@ -1806,22 +1809,42 @@ function format_issue(issue) {
1806
1809
  * usually get wrong, are built in. A lease (a claim on the run's scope that
1807
1810
  * the driver renews while it drives) keeps two invocations from driving one
1808
1811
  * run at once, so a CI webhook and a deadline timer that land together can't
1809
- * both run the same step, and the record's revision fences every write, so a
1810
- * drive that stalled past its lease can't write over the drive that took the
1811
- * run from it. An event that finds the lease taken waits in the run's inbox
1812
- * under its gate's id, and whoever holds the lease takes it up once the run
1813
- * reaches that gate, so the event is queued rather than dropped. And the run
1814
- * records a fingerprint of the flow's shape, so a deploy that changes the
1815
- * flow while a run waits fails loudly instead of replaying different steps
1816
- * against old checkpoints.
1812
+ * both run the same step. Every write to the run's record is conditional on
1813
+ * the version the writer read, and a drive writes only over the revision it
1814
+ * last saw, so a drive that stalled past its lease can't write over the drive
1815
+ * that took the run from it, wherever it stalled. An event that finds the
1816
+ * lease taken waits in the run's inbox under its gate's id, and whoever holds
1817
+ * the lease takes it up once the run reaches that gate, so the event is
1818
+ * queued rather than dropped. And the run records a fingerprint of the flow's
1819
+ * shape, so a deploy that changes the flow while a run waits fails loudly
1820
+ * instead of replaying different steps against old checkpoints.
1817
1821
  *
1818
1822
  * Everything the driver keeps (the input, resume data, gate payloads, and the
1819
1823
  * output) goes through the store, so it has to survive JSON, the same as any
1820
1824
  * checkpointed value.
1821
1825
  *
1822
- * A run's scope holds its record under `record`, the events waiting on gates
1823
- * in the `inbox` scope, the flow's checkpoints in the `flow` scope, and the
1824
- * lease as the claim on `lease`. Deleting a run clears the whole scope.
1826
+ * A run's scope holds its record under `record`, with the inbox inside it,
1827
+ * the flow's checkpoints in the `flow` scope, and the lease as the claim on
1828
+ * `lease`. Deleting a run clears the whole scope. Keeping the inbox in the
1829
+ * record puts every check a delivery makes, and its write, on one version of
1830
+ * one key, which is what lets the store's compare-and-set decide every race.
1831
+ *
1832
+ * A run id can be deleted and started again, so the record carries a `nonce`
1833
+ * that `start` draws once for each run it creates. A delivery writes only to
1834
+ * the run it first read, and since a store may derive a version from the
1835
+ * value's bytes, the nonce is also what keeps the record of a new run apart
1836
+ * from the identical-looking record of the run it replaced.
1837
+ *
1838
+ * A record written before the inbox moved into it is `format: 1`, with its
1839
+ * waiting events in an `inbox` scope of their own and no nonce. The first
1840
+ * drive to find one saves it as `format: 2`, draws its nonce, and marks it
1841
+ * `old_inbox`, and from then on a drive that stops at a gate looks in the
1842
+ * old scope for that gate's event before it looks in the record. That look
1843
+ * is one store read at every gate for as long as the run lives, because a
1844
+ * scope can't be listed and an event may wait there for a gate the run has
1845
+ * yet to reach. A driver from before this format reads a
1846
+ * `format: 2` record as no record at all, so a fleet runs one version of the
1847
+ * driver at a time.
1825
1848
  */
1826
1849
  const RECORD = "record";
1827
1850
  const LEASE = "lease";
@@ -1869,21 +1892,50 @@ function as_suspension(value) {
1869
1892
  };
1870
1893
  }
1871
1894
  /**
1895
+ * Read one inbox entry back, or `undefined` when the value isn't one.
1896
+ */
1897
+ function as_entry(value) {
1898
+ return is_record(value) && "data" in value ? { data: value["data"] } : void 0;
1899
+ }
1900
+ /**
1901
+ * Read a stored inbox back, keeping the entries that hold a datum. A record
1902
+ * with no inbox (every `format: 1` record) reads as an empty one.
1903
+ */
1904
+ function as_inbox(value) {
1905
+ if (!is_record(value)) return {};
1906
+ const entries = Object.entries(value).flatMap(([gate_id, stored]) => {
1907
+ const entry = as_entry(stored);
1908
+ return entry === void 0 ? [] : [[gate_id, entry]];
1909
+ });
1910
+ return Object.fromEntries(entries);
1911
+ }
1912
+ /**
1913
+ * The inbox without the entry for `gate_id`.
1914
+ */
1915
+ function without(inbox, gate_id) {
1916
+ return Object.fromEntries(Object.entries(inbox).filter(([id]) => id !== gate_id));
1917
+ }
1918
+ /**
1872
1919
  * Read a stored run record back, or `undefined` when the value isn't one this
1873
- * driver wrote. `start` treats an unreadable record as no record and begins
1874
- * the run again from its input, where its checkpoints still serve.
1920
+ * driver can read. `start` treats an unreadable record as no record and
1921
+ * begins the run again from its input, where its checkpoints still serve.
1875
1922
  */
1876
1923
  function as_record(value) {
1877
- if (!is_record(value) || value["format"] !== 1) return void 0;
1878
- const { revision, input, flow, resume_data, output, error, status } = value;
1924
+ if (!is_record(value)) return void 0;
1925
+ const { format, revision, input, flow, resume_data, output, error, status } = value;
1926
+ if (format !== 1 && format !== 2) return void 0;
1879
1927
  if (typeof revision !== "number" || typeof flow !== "string") return void 0;
1880
1928
  if (!is_record(resume_data) || !STATUSES.has(status)) return void 0;
1881
1929
  const base = {
1882
- format: 1,
1930
+ format,
1883
1931
  revision,
1932
+ nonce: typeof value["nonce"] === "string" ? value["nonce"] : "",
1933
+ writer: typeof value["writer"] === "string" ? value["writer"] : "",
1934
+ old_inbox: value["old_inbox"] === true,
1884
1935
  input,
1885
1936
  flow,
1886
1937
  resume_data,
1938
+ inbox: as_inbox(value["inbox"]),
1887
1939
  output,
1888
1940
  error: typeof error === "string" ? error : ""
1889
1941
  };
@@ -1974,6 +2026,7 @@ async function take_lease(scope, lease_ms) {
1974
2026
  }, lease_ms / 3);
1975
2027
  timer.unref();
1976
2028
  return {
2029
+ owner,
1977
2030
  signal: controller.signal,
1978
2031
  lost: () => lost,
1979
2032
  lose,
@@ -1991,20 +2044,39 @@ async function take_lease(scope, lease_ms) {
1991
2044
  * Write the run's record and resolve the record as written.
1992
2045
  *
1993
2046
  * The write goes through only while this drive holds the lease and the
1994
- * stored record is still the revision that `next` was built from. A drive
1995
- * that lost the lease, or that stalled while another drive moved the run on,
1996
- * finds the revision moved, loses the lease, and leaves the record alone.
1997
- */
1998
- async function save(scope, lease, next) {
2047
+ * stored record is still the revision that `next` was built from, and it
2048
+ * lands on the version it read that revision at. A drive that lost the
2049
+ * lease, or that stalled while another drive moved the run on, finds the
2050
+ * revision moved, loses the lease, and leaves the record alone, however late
2051
+ * it stalled.
2052
+ *
2053
+ * The inbox belongs to the deliveries, which write without the lease, so the
2054
+ * saved record takes the stored inbox and not the one `next` carries, after
2055
+ * `edit` has made the one change this drive means to make to it. A write
2056
+ * that conflicts while the revision still matches lost to a delivery (the
2057
+ * lease keeps other drives out), so it reads again and writes again with
2058
+ * that delivery kept.
2059
+ *
2060
+ * A store can land a write and still report a conflict, when the answer was
2061
+ * lost and a delivery changed the record before the store looked again. So a
2062
+ * record one revision on that names this drive as its writer is this save's
2063
+ * own write, and it resolves as written, with the delivery in its inbox.
2064
+ */
2065
+ async function save(scope, lease, next, edit = (inbox) => inbox) {
1999
2066
  await lease.confirm();
2000
- const stored = as_record(await scope.get(RECORD));
2001
- if (stored === void 0 || stored.revision !== next.revision) throw lease.lose();
2002
- const saved = {
2003
- ...next,
2004
- revision: next.revision + 1
2005
- };
2006
- await scope.set(RECORD, saved);
2007
- return saved;
2067
+ for (;;) {
2068
+ const read = await scope.get_versioned(RECORD);
2069
+ const stored = as_record(read?.value);
2070
+ if (stored?.revision === next.revision + 1 && stored.writer === lease.owner) return stored;
2071
+ if (read === void 0 || stored === void 0 || stored.revision !== next.revision) throw lease.lose();
2072
+ const saved = {
2073
+ ...next,
2074
+ revision: next.revision + 1,
2075
+ writer: lease.owner,
2076
+ inbox: edit(stored.inbox)
2077
+ };
2078
+ if (await scope.set_if(RECORD, saved, read.version)) return saved;
2079
+ }
2008
2080
  }
2009
2081
  /**
2010
2082
  * Hold a run to the flow shape it started on. A run on the same shape passes
@@ -2028,55 +2100,97 @@ async function check_shape(drive, lease, record) {
2028
2100
  });
2029
2101
  }
2030
2102
  /**
2031
- * The resume data waiting in the run's inbox for `gate_id`, if any arrived.
2103
+ * True when `record` is the run that a delivery first read with `nonce`.
2104
+ *
2105
+ * A `format: 1` record has no nonce and reads as '', and the drive that
2106
+ * migrates it draws one. A delivery that read the '' and then lost its write
2107
+ * to that migration would find another nonce and drop its datum, so it also
2108
+ * takes a record marked `old_inbox` for its run, which only a migrated run
2109
+ * is. A run this driver started is never marked, so the datum still can't
2110
+ * reach one. Two runs under one id that both began as `format: 1` are the
2111
+ * one pair a delivery that read '' can't tell apart.
2032
2112
  */
2033
- async function read_inbox(scope, gate_id) {
2034
- const entry = await scope.scope(INBOX).get(gate_id);
2035
- return is_record(entry) && "data" in entry ? { data: entry["data"] } : void 0;
2113
+ function is_same_run(record, nonce) {
2114
+ return record.nonce === nonce || nonce === "" && record.old_inbox;
2036
2115
  }
2037
2116
  /**
2038
- * Leave `data` in the run's inbox for `gate_id`, unless a datum already waits
2039
- * there, another resume is leaving one, the run has already passed the gate,
2117
+ * Leave `data` in the inbox of the run that carries `nonce` for `gate_id`,
2118
+ * unless a datum already waits there, the run has already passed the gate,
2040
2119
  * or the run is gone.
2041
2120
  *
2042
- * The reads and the write happen under a claim on the gate's inbox key, so of
2043
- * two resumes that race for one gate, only the first to claim it writes, and
2044
- * the other drops its datum as it would on finding one there. The inbox is
2045
- * read before the record because `take_up` saves a gate's datum into the
2046
- * record before it deletes the inbox entry, so a datum that's taken up between
2047
- * the two reads still shows in the record. The claim lasts `lease_ms` and
2048
- * comes off right after the write.
2049
- *
2050
- * Two windows stay open, because the store contract has no compare-and-set to
2051
- * close them with. A resume that stalls past its claim, after its reads and
2052
- * before its write, can still write once another resume has moved the run
2053
- * past the gate. And a `delete` that lands between the record read and the
2054
- * write leaves the datum in the cleared scope, where a new run under the same
2055
- * id takes it up.
2056
- */
2057
- async function deliver(drive, gate_id, data) {
2058
- const key = `${INBOX}/${gate_id}`;
2059
- const owner = randomUUID();
2060
- if (!await drive.scope.claim(key, owner, drive.lease_ms)) return;
2061
- try {
2062
- if (await read_inbox(drive.scope, gate_id) !== void 0) return;
2063
- const record = as_record(await drive.scope.get(RECORD));
2064
- if (record === void 0 || Object.hasOwn(record.resume_data, gate_id)) return;
2065
- await drive.scope.scope(INBOX).set(gate_id, { data });
2066
- } finally {
2067
- await drive.scope.release(key, owner);
2121
+ * All the checks read one version of the record, and the write lands only
2122
+ * on that version, so no claim guards it. A write that conflicts reads the
2123
+ * record again and decides again. So of two resumes that race for one gate,
2124
+ * the second finds the first one's datum and drops its own, a run that moved
2125
+ * past the gate in the meantime drops the datum, and a `delete` in the
2126
+ * meantime leaves no record to write to. A run started again under the same
2127
+ * id after that `delete` carries another nonce, so the datum never reaches
2128
+ * it, however late the delivery wakes up. `is_same_run` says how a delivery
2129
+ * that read a `format: 1` record finds that run again once it has a nonce.
2130
+ *
2131
+ * The record keeps the format it was read with. A `format: 1` record may
2132
+ * have events in its old inbox scope, which a drive reads only once it has
2133
+ * marked the record `old_inbox`, and a record that claimed to be `format: 2`
2134
+ * would skip that step.
2135
+ */
2136
+ async function deliver(scope, nonce, gate_id, data) {
2137
+ for (;;) {
2138
+ const read = await scope.get_versioned(RECORD);
2139
+ const record = as_record(read?.value);
2140
+ if (read === void 0 || record === void 0 || !is_same_run(record, nonce)) return;
2141
+ if (Object.hasOwn(record.resume_data, gate_id) || Object.hasOwn(record.inbox, gate_id)) return;
2142
+ const next = {
2143
+ ...record,
2144
+ inbox: {
2145
+ ...record.inbox,
2146
+ [gate_id]: { data }
2147
+ }
2148
+ };
2149
+ if (await scope.set_if(RECORD, next, read.version)) return;
2068
2150
  }
2069
2151
  }
2070
2152
  /**
2153
+ * Bring a `format: 1` record up to `format: 2`, marked as a run whose old
2154
+ * `inbox` scope may still hold events, and with a nonce when it has none.
2155
+ *
2156
+ * A scope can't be listed, so the events stay where they are, and `take_up`
2157
+ * reads the old scope one gate at a time, as the run reaches each gate. That
2158
+ * way an event for a gate the flow builds inside a step, or for a gate this
2159
+ * deploy renamed, is still there when a drive comes looking for it. It's
2160
+ * also why the mark stays for the life of the run: no read can show that
2161
+ * nothing waits for the gates still ahead.
2162
+ *
2163
+ * The nonce closes, for a run from before there was one, the window it
2164
+ * closes for every other run: a delivery that read this record can't land
2165
+ * in another run that began the same way under the same id.
2166
+ */
2167
+ function migrate(drive, lease, record) {
2168
+ const nonce = record.nonce === "" ? randomUUID() : record.nonce;
2169
+ return save(drive.scope, lease, {
2170
+ ...record,
2171
+ format: 2,
2172
+ old_inbox: true,
2173
+ nonce
2174
+ });
2175
+ }
2176
+ /**
2071
2177
  * Move the resume data waiting at the run's gate into the run, or resolve
2072
2178
  * `undefined` when none has arrived.
2073
2179
  *
2074
- * The record is saved before the inbox entry is deleted, so a crash between
2075
- * the two leaves the data in one place or the other and never in neither.
2180
+ * The datum leaves the inbox and joins the resume data in one write, so a
2181
+ * crash can't leave it in both places or in neither.
2182
+ *
2183
+ * A run that began as `format: 1` looks in its old inbox scope first. An
2184
+ * event that waited there got to the gate before anything in the record's
2185
+ * own inbox did, so it wins the gate. Its key is deleted only after the
2186
+ * record holds the datum, and after the flow's shape is checked, so neither
2187
+ * a crash nor a changed flow can lose it.
2076
2188
  */
2077
2189
  async function take_up(drive, lease, record) {
2078
2190
  const gate = record.suspended.id;
2079
- const entry = await read_inbox(drive.scope, gate);
2191
+ const old = drive.scope.scope(INBOX);
2192
+ const waited = record.old_inbox ? as_entry(await old.get(gate)) : void 0;
2193
+ const entry = waited ?? (Object.hasOwn(record.inbox, gate) ? record.inbox[gate] : void 0);
2080
2194
  if (entry === void 0) return void 0;
2081
2195
  const current = await check_shape(drive, lease, record);
2082
2196
  const saved = await save(drive.scope, lease, {
@@ -2086,8 +2200,8 @@ async function take_up(drive, lease, record) {
2086
2200
  ...current.resume_data,
2087
2201
  [gate]: entry.data
2088
2202
  }
2089
- });
2090
- await drive.scope.scope(INBOX).delete(gate);
2203
+ }, (inbox) => without(inbox, gate));
2204
+ if (waited !== void 0) await old.delete(gate);
2091
2205
  return saved;
2092
2206
  }
2093
2207
  /**
@@ -2167,7 +2281,7 @@ async function drive_once(drive, lease, stored) {
2167
2281
  * inbox for that gate.
2168
2282
  */
2169
2283
  async function settle$1(drive, lease, loaded) {
2170
- let record = loaded;
2284
+ let record = loaded.format === 1 ? await migrate(drive, lease, loaded) : loaded;
2171
2285
  for (;;) {
2172
2286
  if (record.status === "done") return done_view(drive, record);
2173
2287
  if (record.status === "suspended") {
@@ -2182,10 +2296,10 @@ async function settle$1(drive, lease, loaded) {
2182
2296
  * Take the lease, bring the run to rest, and let the lease go.
2183
2297
  *
2184
2298
  * An event that landed for the run's gate while this call held the lease
2185
- * found it taken and left its data in the inbox. So once the lease is gone,
2186
- * the call looks again and drives once more when something waits at the gate.
2187
- * Another call that took the lease in between gets it first, and this one
2188
- * reports the run busy.
2299
+ * found it taken and left its data in the record's inbox. So once the lease
2300
+ * is gone, the call reads the record again and drives once more when
2301
+ * something waits at the gate. Another call that took the lease in between
2302
+ * gets it first, and this one reports the run busy.
2189
2303
  */
2190
2304
  async function bring_to_rest(drive, load) {
2191
2305
  for (;;) {
@@ -2196,7 +2310,7 @@ async function bring_to_rest(drive, load) {
2196
2310
  };
2197
2311
  let outcome;
2198
2312
  try {
2199
- outcome = await settle$1(drive, lease, await load());
2313
+ outcome = await settle$1(drive, lease, await load(lease));
2200
2314
  } catch (err) {
2201
2315
  if (lease.lost()) return {
2202
2316
  kind: "busy",
@@ -2207,7 +2321,8 @@ async function bring_to_rest(drive, load) {
2207
2321
  await lease.end();
2208
2322
  }
2209
2323
  if (outcome.kind !== "suspended") return outcome;
2210
- if (await read_inbox(drive.scope, outcome.id) === void 0) return outcome;
2324
+ const record = as_record(await drive.scope.get(RECORD));
2325
+ if (record === void 0 || !Object.hasOwn(record.inbox, outcome.id)) return outcome;
2211
2326
  }
2212
2327
  }
2213
2328
  /**
@@ -2243,27 +2358,35 @@ function drive_for(driver, run_id, flow, options) {
2243
2358
  * Start a run, or pick it up where it is when it already exists. A repeated
2244
2359
  * start (a webhook delivered twice) neither restarts the run nor changes its
2245
2360
  * input, though it does drive a run that failed, or whose drive died, once
2246
- * more. The new record is read back before the first drive, so that drive
2247
- * sees the input the way every later one will, after a trip through the
2248
- * store.
2361
+ * more. The new record is created only while the key is absent, or on the
2362
+ * version of the unreadable record it replaces, so two starts can't both
2363
+ * create one, even outside the lease. A start that loses that write drives
2364
+ * the record that won. The record is read back before the first drive, so
2365
+ * that drive sees the input the way every later one will, after a trip
2366
+ * through the store.
2249
2367
  */
2250
2368
  async function start_run(driver, run_id, flow, input, options) {
2251
2369
  const drive = drive_for(driver, run_id, flow, options);
2252
- return bring_to_rest(drive, async () => {
2253
- const existing = as_record(await drive.scope.get(RECORD));
2370
+ return bring_to_rest(drive, async (lease) => {
2371
+ const read = await drive.scope.get_versioned(RECORD);
2372
+ const existing = as_record(read?.value);
2254
2373
  if (existing !== void 0) return existing;
2255
2374
  const record = {
2256
- format: 1,
2375
+ format: 2,
2257
2376
  revision: 0,
2377
+ nonce: randomUUID(),
2378
+ writer: lease.owner,
2379
+ old_inbox: false,
2258
2380
  input,
2259
2381
  flow: drive.shape,
2260
2382
  resume_data: {},
2383
+ inbox: {},
2261
2384
  output: null,
2262
2385
  error: "",
2263
2386
  status: "running",
2264
2387
  suspended: null
2265
2388
  };
2266
- await drive.scope.set(RECORD, record);
2389
+ await drive.scope.set_if(RECORD, record, read?.version ?? null);
2267
2390
  return existing_record(run_id, drive.scope);
2268
2391
  });
2269
2392
  }
@@ -2275,25 +2398,20 @@ async function start_run(driver, run_id, flow, input, options) {
2275
2398
  * it up. Data for a gate the run has already passed is dropped, because
2276
2399
  * changing it would change the replay, and so is data for a run that's gone,
2277
2400
  * and data for a gate whose inbox already holds some: the first datum to
2278
- * reach a gate is the one it gets. Each of those checks happens under a claim
2279
- * on the gate's inbox key, so it holds when two calls race for one gate, and
2280
- * when another call moves the run past the gate or deletes the run before
2281
- * this one claims. The claim lasts the driver's `lease_ms` and comes off
2282
- * right after the write, so a call that dies between the two holds that
2283
- * gate's data back until the claim lapses, and data sent for the gate in that
2284
- * window is dropped. Without a compare-and-set in the store contract, two
2285
- * windows stay open: a call that stalls past its claim can still write data
2286
- * for a gate the run has passed since, and a `delete` that lands between the
2287
- * call's record read and its write leaves the data for a new run under the
2288
- * same id. `undefined` resumes nothing. A call with no new data still drives
2289
- * a run that failed, or whose drive died, once more.
2401
+ * reach a gate is the one it gets. Each datum is checked against one version
2402
+ * of the run's record and written only on that version, so the rules hold
2403
+ * when two calls race for one gate, and when another call moves the run past
2404
+ * the gate or deletes the run at any point before the write, even when the
2405
+ * run is started again under the same id. The data go in
2406
+ * one after another, since they all write the same record. `undefined`
2407
+ * resumes nothing. A call with no new data still drives a run that failed,
2408
+ * or whose drive died, once more.
2290
2409
  */
2291
2410
  async function resume_run(driver, run_id, flow, resume_data, options) {
2292
2411
  const drive = drive_for(driver, run_id, flow, options);
2293
2412
  const record = await existing_record(run_id, drive.scope);
2294
2413
  if (record.status === "done") return done_view(drive, record);
2295
- const fresh = Object.entries(resume_data).filter(([, data]) => data !== void 0);
2296
- await Promise.all(fresh.map(([id, data]) => deliver(drive, id, data)));
2414
+ for (const [id, data] of Object.entries(resume_data)) if (data !== void 0) await deliver(drive.scope, record.nonce, id, data);
2297
2415
  return bring_to_rest(drive, () => existing_record(run_id, drive.scope));
2298
2416
  }
2299
2417
  /**
@@ -3915,4 +4033,4 @@ function chain(input_name) {
3915
4033
  register_traced_kind("chain");
3916
4034
  export { is_step as A, timeout_error as B, validate_schema as C, wait_with_abort as D, compute_backoff as E, flow_changed_error as F, is_step_kind as H, gate_result_error as I, resume_validation_error as L, describe_cycle_error as M, duplicate_suspend_id_error as N, same_input_as as O, error_path as P, run_not_found_error as R, format_schema_issues as S, failure_meta as T, resolve_display_name as U, STEP_KINDS as V, step as _, suspend as a, suggest_step_id as b, loop as c, retry as d, pipe as f, sequence as g, parallel as h, use as i, aborted_error as j, describe as k, timeout as l, branch as m, scope as n, checkpoint as o, map as p, stash as r, compose as s, chain as t, fallback as u, assert_valid_step_id as v, run as w, durable as x, is_valid_step_id as y, suspended_error as z };
3917
4035
 
3918
- //# sourceMappingURL=core-BH7sWpEG.js.map
4036
+ //# sourceMappingURL=core-xTtRZqdb.js.map