@elaraai/e3-types 1.0.107 → 1.0.109

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 (60) hide show
  1. package/dist/src/api.d.ts +382 -17
  2. package/dist/src/api.d.ts.map +1 -1
  3. package/dist/src/api.js +201 -17
  4. package/dist/src/api.js.map +1 -1
  5. package/dist/src/function-call.d.ts +91 -13
  6. package/dist/src/function-call.d.ts.map +1 -1
  7. package/dist/src/function-call.js +48 -4
  8. package/dist/src/function-call.js.map +1 -1
  9. package/dist/src/function-draft.d.ts +80 -0
  10. package/dist/src/function-draft.d.ts.map +1 -0
  11. package/dist/src/function-draft.js +71 -0
  12. package/dist/src/function-draft.js.map +1 -0
  13. package/dist/src/function-draft.spec.d.ts +6 -0
  14. package/dist/src/function-draft.spec.d.ts.map +1 -0
  15. package/dist/src/function-draft.spec.js +58 -0
  16. package/dist/src/function-draft.spec.js.map +1 -0
  17. package/dist/src/function.d.ts +45 -8
  18. package/dist/src/function.d.ts.map +1 -1
  19. package/dist/src/function.js +40 -7
  20. package/dist/src/function.js.map +1 -1
  21. package/dist/src/function.spec.d.ts +6 -0
  22. package/dist/src/function.spec.d.ts.map +1 -0
  23. package/dist/src/function.spec.js +61 -0
  24. package/dist/src/function.spec.js.map +1 -0
  25. package/dist/src/index.d.ts +6 -5
  26. package/dist/src/index.d.ts.map +1 -1
  27. package/dist/src/index.js +8 -6
  28. package/dist/src/index.js.map +1 -1
  29. package/dist/src/record.d.ts +81 -29
  30. package/dist/src/record.d.ts.map +1 -1
  31. package/dist/src/record.js +92 -22
  32. package/dist/src/record.js.map +1 -1
  33. package/dist/src/release.d.ts +1 -1
  34. package/dist/src/release.js +1 -1
  35. package/dist/src/structure.d.ts +18 -0
  36. package/dist/src/structure.d.ts.map +1 -1
  37. package/dist/src/structure.js +20 -0
  38. package/dist/src/structure.js.map +1 -1
  39. package/dist/src/structure.spec.js +8 -1
  40. package/dist/src/structure.spec.js.map +1 -1
  41. package/dist/src/task.d.ts +15 -2
  42. package/dist/src/task.d.ts.map +1 -1
  43. package/dist/src/task.js +14 -2
  44. package/dist/src/task.js.map +1 -1
  45. package/dist/src/task.spec.js +19 -2
  46. package/dist/src/task.spec.js.map +1 -1
  47. package/package.json +2 -2
  48. package/src/api.ts +215 -17
  49. package/src/function-call.ts +52 -4
  50. package/src/function-draft.spec.ts +69 -0
  51. package/src/function-draft.ts +96 -0
  52. package/src/function.spec.ts +66 -0
  53. package/src/function.ts +44 -7
  54. package/src/index.ts +35 -2
  55. package/src/record.ts +97 -24
  56. package/src/release.ts +1 -1
  57. package/src/structure.spec.ts +9 -1
  58. package/src/structure.ts +21 -0
  59. package/src/task.spec.ts +20 -2
  60. package/src/task.ts +14 -2
package/src/api.ts CHANGED
@@ -39,6 +39,7 @@ import { StructureType, TreePathType } from './structure.js';
39
39
  import { StopReasonType } from './execution.js';
40
40
  import { IntakeFileType } from './intake.js';
41
41
  import { RunnerType } from './runner.js';
42
+ import { FunctionInputType } from './function.js';
42
43
  import { TaskBodyType, TaskInputType, TaskOutputType, TaskPartitionType, TaskRoleType } from './task.js';
43
44
  import { DataflowForceType, RequeueReasonType, StageUnitType } from './dataflow.js';
44
45
 
@@ -1246,6 +1247,99 @@ export const DiagnosticType = StructType({
1246
1247
  column: OptionType(IntegerType),
1247
1248
  });
1248
1249
 
1250
+ /**
1251
+ * A draft of a record that no longer fits it: the record changed under the
1252
+ * edit, so the mutation's write disagrees with the state it was laid over. It
1253
+ * is what a Save of the same edit ends `conflict` with
1254
+ * ({@link MutationResultType}), named for a function's call by the draft it
1255
+ * was.
1256
+ *
1257
+ * @property record - The record the draft was of
1258
+ * @property mutation - The draft's mutation
1259
+ * @property place - The draft's place among the call's, from 1: which of
1260
+ * several drafts of one mutation the record refused
1261
+ * @property of - How many drafts the call passed
1262
+ * @property message - The refusal's words, as a Save's conflict carries them
1263
+ * in its `detail`
1264
+ */
1265
+ export const FunctionDraftConflictType = StructType({
1266
+ record: StringType,
1267
+ mutation: StringType,
1268
+ place: IntegerType,
1269
+ of: IntegerType,
1270
+ message: StringType,
1271
+ });
1272
+
1273
+ /**
1274
+ * A record a call read with drafts laid over it, as the call's result names
1275
+ * it.
1276
+ *
1277
+ * @property record - The record's name
1278
+ * @property commit - The commit the record stood at as the call started, which
1279
+ * its drafts were laid over: a caller that holds another knows its edits
1280
+ * were laid over newer saves
1281
+ */
1282
+ export const FunctionDraftedType = StructType({
1283
+ record: StringType,
1284
+ commit: StringType,
1285
+ });
1286
+
1287
+ /**
1288
+ * The draft whose mutation ended a call `failed` or `timed_out`, before its
1289
+ * function ran. A call whose timeout ended between two drafts' runs names the
1290
+ * draft that did not start.
1291
+ *
1292
+ * @property record - The record the draft was of
1293
+ * @property mutation - The draft's mutation
1294
+ * @property place - The draft's place among the call's, from 1
1295
+ * @property of - How many drafts the call passed
1296
+ * @property how - How its mutation ended: as a Save of it would have
1297
+ */
1298
+ export const FunctionDraftFailureType = StructType({
1299
+ record: StringType,
1300
+ mutation: StringType,
1301
+ place: IntegerType,
1302
+ of: IntegerType,
1303
+ how: VariantType({ failed: NullType, timed_out: NullType }),
1304
+ });
1305
+
1306
+ /**
1307
+ * How a call's drafts ended it before its function ran, as its result names
1308
+ * it (#1619, #1680's review).
1309
+ *
1310
+ * - `draft`: a draft's mutation ended it ({@link FunctionDraftFailureType})
1311
+ * - `laid`: its timeout ended once its drafts were all laid, before its
1312
+ * function ran: `of` drafts, every one laid
1313
+ *
1314
+ * The first line of its stderr says the same, for people to read (`draft
1315
+ * jobs.patch (2 of 3) failed`, `drafts laid (3 of 3), and the call's timeout
1316
+ * ended before its function ran`). A caller learns what ended a call from this
1317
+ * alone, never from that line: a function's own stderr may begin the same way.
1318
+ */
1319
+ export const FunctionDraftEndType = VariantType({
1320
+ draft: FunctionDraftFailureType,
1321
+ laid: StructType({ of: IntegerType }),
1322
+ });
1323
+
1324
+ /**
1325
+ * A dataset a call read, and the hash it was pinned at: one of a function's
1326
+ * inputs, or a dataset argument.
1327
+ *
1328
+ * @property path - The dataset's path: for an index of a record, the record's
1329
+ * @property index - The index of the record at `path` that was read, by its
1330
+ * name on the record; `none` for the dataset itself, and for a dataset
1331
+ * argument
1332
+ * @property hash - The hash the dataset at `path` was pinned at. For a record,
1333
+ * the hash of its state, which names its rows and each of its indexes: so an
1334
+ * index's entry names the state its entries were read at, and the entries of
1335
+ * one record a call read name one hash
1336
+ */
1337
+ export const InputReadType = StructType({
1338
+ path: TreePathType,
1339
+ index: OptionType(StringType),
1340
+ hash: StringType,
1341
+ });
1342
+
1249
1343
  /**
1250
1344
  * The terminal result of a function/one-shot call.
1251
1345
  *
@@ -1254,12 +1348,39 @@ export const DiagnosticType = StructType({
1254
1348
  * - `invalid`: signature/IR error; nothing ran
1255
1349
  * - `too_large`: result over `maxResultBytes`; deploy a task and read it with `datasetGet`
1256
1350
  * - `timed_out`: exceeded `timeoutMs` / the server's sync deadline guard
1257
- *
1258
- * `inputs` names what the call read: each input the function declares, then
1259
- * each dataset argument, with the hash it was pinned at, in parameter order,
1260
- * so an answer can be reproduced and a stale one noticed. It is empty for a
1261
- * call of a function with no inputs and value arguments only, and for an
1262
- * `invalid` result.
1351
+ * - `conflict`: a draft the call passed no longer fits its record, and the
1352
+ * function's body did not run ({@link FunctionDraftConflictType})
1353
+ *
1354
+ * A call a draft's mutation ended `failed` or `timed_out`, before the
1355
+ * function's body ran, names the draft in `draftEnd`
1356
+ * ({@link FunctionDraftEndType}), and says so in the first line of its
1357
+ * `stderr`: `draft jobs.patch (2 of 3) failed`, or `… timed out`, the
1358
+ * mutation's own stderr following it. A call whose timeout ended once its
1359
+ * drafts were all laid, before its body ran, names that in `draftEnd` instead,
1360
+ * and says so in its first line (`drafts laid (3 of 3), and the call's timeout
1361
+ * ended before its function ran`). `functionDraftEnded` and
1362
+ * `functionDraftsOutlasted` read `draftEnd`, never the line: a function's own
1363
+ * stderr may begin the same way. It is `none` for every other call.
1364
+ *
1365
+ * `inputs` names what the call read ({@link InputReadType}): each input the
1366
+ * function declares, then each dataset argument, with the hash its dataset was
1367
+ * pinned at, in parameter order, so an answer can be reproduced and a stale
1368
+ * one noticed. An input that is an index of a record names the record, the
1369
+ * index and the hash the record was pinned at, which is the hash its other
1370
+ * inputs of that record name: they are read at one state. An input of a record
1371
+ * the call drafted, the record or an index of it, names the drafted state's
1372
+ * hash: a state no commit names, which the record itself never stands at. A
1373
+ * dataset argument naming that record names the state the drafts were laid
1374
+ * over, the one the drafted record's commit names, so a function handed one
1375
+ * reads the record before and after the edit. It is empty for a call of a
1376
+ * function with no inputs and value arguments only, and for an `invalid`
1377
+ * result.
1378
+ *
1379
+ * `drafts` names each record the call drafted, once, in the order its drafts
1380
+ * first name them, with the commit it stood at
1381
+ * ({@link FunctionDraftedType}). It is empty for a call that passed no draft,
1382
+ * for an `invalid` result, and for a one-shot or a split call, which take
1383
+ * none.
1263
1384
  */
1264
1385
  export const ExecuteResultType = StructType({
1265
1386
  outcome: VariantType({
@@ -1268,12 +1389,15 @@ export const ExecuteResultType = StructType({
1268
1389
  invalid: StructType({ diagnostics: ArrayType(DiagnosticType) }),
1269
1390
  too_large: StructType({ bytes: IntegerType, limit: IntegerType }),
1270
1391
  timed_out: StructType({ ms: IntegerType }),
1392
+ conflict: FunctionDraftConflictType,
1271
1393
  }),
1272
1394
  stdout: StringType,
1273
1395
  stderr: StringType,
1274
1396
  stdoutTruncated: BooleanType,
1275
1397
  stderrTruncated: BooleanType,
1276
- inputs: ArrayType(StructType({ path: TreePathType, hash: StringType })),
1398
+ inputs: ArrayType(InputReadType),
1399
+ drafts: ArrayType(FunctionDraftedType),
1400
+ draftEnd: OptionType(FunctionDraftEndType),
1277
1401
  });
1278
1402
 
1279
1403
  /**
@@ -1286,6 +1410,27 @@ export const CallArgType = VariantType({
1286
1410
  dataset: TreePathType,
1287
1411
  });
1288
1412
 
1413
+ /**
1414
+ * A draft: a call of a record's mutation that is not committed. A function's
1415
+ * call passes a list of them, and reads the record with them laid over it, as
1416
+ * a Save of each would have left it; nothing is saved.
1417
+ *
1418
+ * It is not what the SDK exports as `MutationDraft`, which is a mutation's
1419
+ * declaration before `e3.record` builds it.
1420
+ *
1421
+ * @property record - The record's name, as the package declares it and as the
1422
+ * function lists it in its `drafts`
1423
+ * @property mutation - The mutation's name on the record
1424
+ * @property args - The mutation's arguments, as a mutation call takes them
1425
+ * ({@link MutationCallRequestType}): one beast2-encoded value per declared
1426
+ * parameter, after the implicit current state
1427
+ */
1428
+ export const FunctionDraftType = StructType({
1429
+ record: StringType,
1430
+ mutation: StringType,
1431
+ args: ArrayType(BlobType),
1432
+ });
1433
+
1289
1434
  /**
1290
1435
  * Named function call. Positional args, one per argument the function takes
1291
1436
  * (its `argTypes`, never its inputs, which the server reads): a value, or a
@@ -1293,11 +1438,18 @@ export const CallArgType = VariantType({
1293
1438
  * no workspace to read a dataset from, and answers a dataset argument — or a
1294
1439
  * function with inputs — `invalid`; the workspace-scoped route pins it, as a
1295
1440
  * one-shot's is.
1441
+ *
1442
+ * `drafts` are the unsaved edits the call lays over the records it reads, in
1443
+ * order ({@link FunctionDraftType}): each of a record the function lists in
1444
+ * its `drafts`, else the call is `invalid`. A record's drafts run in the order
1445
+ * given, each over the state the one before it left. The package-scoped route
1446
+ * reads no workspace, and answers a call that passes any `invalid`.
1296
1447
  */
1297
1448
  export const FunctionCallRequestType = StructType({
1298
1449
  args: ArrayType(CallArgType),
1299
1450
  runner: OptionType(RunnerType), // optional override; only the known runtimes
1300
1451
  limits: OptionType(ExecuteLimitsType),
1452
+ drafts: ArrayType(FunctionDraftType),
1301
1453
  });
1302
1454
 
1303
1455
  /**
@@ -1305,16 +1457,19 @@ export const FunctionCallRequestType = StructType({
1305
1457
  * args.
1306
1458
  *
1307
1459
  * @property name - The function's name
1308
- * @property inputs - The datasets a call reads from the workspace it runs in,
1309
- * which the caller does not pass; a function with any is called through a
1310
- * workspace
1460
+ * @property inputs - What a call reads from the workspace it runs in, which
1461
+ * the caller does not pass: each a dataset, or an index of a record
1462
+ * (`FunctionInputType`); a function with any is called through a workspace
1463
+ * @property drafts - The records a call may pass drafts of, by name: each a
1464
+ * record one of `inputs` reads; empty for a function that takes none
1311
1465
  * @property argTypes - The types of the arguments a caller passes
1312
1466
  * @property outputType - The result's type
1313
1467
  * @property runner - The runtime it runs on
1314
1468
  */
1315
1469
  export const FunctionSignatureType = StructType({
1316
1470
  name: StringType,
1317
- inputs: ArrayType(TreePathType),
1471
+ inputs: ArrayType(FunctionInputType),
1472
+ drafts: ArrayType(StringType),
1318
1473
  argTypes: ArrayType(EastTypeType),
1319
1474
  outputType: EastTypeType,
1320
1475
  runner: RunnerType,
@@ -1477,17 +1632,23 @@ export const SplitCallStatusType = VariantType({
1477
1632
  *
1478
1633
  * - `running`: it has not ended
1479
1634
  * - `succeeded`: it returned, and `value` is the object its value was stored as
1480
- * - `failed`: its runner exited non-zero (see its stderr)
1635
+ * - `failed`: its runner exited non-zero (see its stderr), or a draft's
1636
+ * mutation failed before its body ran, which its `draftEnd` names
1481
1637
  * - `invalid`: it was refused before it ran: a wrong arity, a runner the
1482
- * caller may not name, an input with no value
1638
+ * caller may not name, an input with no value, a draft the function does not
1639
+ * take or one its record's mutation does not
1483
1640
  * - `too_large`: its value was over its `maxResultBytes`, and was not kept
1484
- * - `timed_out`: it ran past its timeout, and was stopped
1641
+ * - `timed_out`: it ran past its timeout, and was stopped, its body or a
1642
+ * draft's mutation before it
1485
1643
  * - `cancelled`: it was asked to stop, by `by` (`none` without an identity),
1486
1644
  * and was stopped
1487
1645
  * - `lost`: e3 could not run it, or lost it: its host stopped before it ended
1646
+ * - `conflict`: a draft it passed no longer fits its record, and its
1647
+ * function's body did not run ({@link FunctionDraftConflictType})
1488
1648
  */
1489
1649
  export const FunctionCallStateType = VariantType({
1490
1650
  cancelled: StructType({ by: OptionType(StringType) }),
1651
+ conflict: FunctionDraftConflictType,
1491
1652
  failed: StructType({ exitCode: IntegerType }),
1492
1653
  invalid: StructType({ diagnostics: ArrayType(DiagnosticType) }),
1493
1654
  lost: StructType({ message: StringType }),
@@ -1507,6 +1668,24 @@ export const FunctionCallArgType = VariantType({
1507
1668
  value: StringType,
1508
1669
  });
1509
1670
 
1671
+ /**
1672
+ * A kept call's draft: the draft its caller passed ({@link FunctionDraftType}),
1673
+ * its arguments held by their object as the call's own are. The commit each
1674
+ * drafted record stood at is a record's, not a draft's: the call's result
1675
+ * names it once for each record ({@link FunctionDraftedType}).
1676
+ *
1677
+ * @property record - The record's name
1678
+ * @property mutation - The mutation's name on the record
1679
+ * @property args - The object the arguments' tuple was stored as, an Array of
1680
+ * Blobs, as a record's commit holds its mutation's; `none` for a mutation
1681
+ * that takes none
1682
+ */
1683
+ export const FunctionCallDraftType = StructType({
1684
+ record: StringType,
1685
+ mutation: StringType,
1686
+ args: OptionType(StringType),
1687
+ });
1688
+
1510
1689
  /**
1511
1690
  * A kept call, as a workspace's list of calls shows it.
1512
1691
  *
@@ -1536,8 +1715,18 @@ export const FunctionCallSummaryType = StructType({
1536
1715
  * `none` for a one-shot call, which runs no package's function
1537
1716
  * @property args - Its arguments, in order: values by their objects, datasets
1538
1717
  * by the hashes they were pinned at
1539
- * @property inputs - The function's own inputs, each by its path and the hash
1540
- * it was pinned at, in the order the function declares them
1718
+ * @property inputs - The function's own inputs ({@link InputReadType}), each
1719
+ * by its path — with the index's name, for an index of a record — and the
1720
+ * hash its dataset was pinned at, in the order the function declares them.
1721
+ * An input of a record the call drafts, the record or an index of it, names
1722
+ * the state the record held as the call started, and, once its drafts are
1723
+ * laid, the drafted state
1724
+ * @property drafts - The drafts it passed, in order; none for a call that
1725
+ * passed none, and for one refused as it started, which keeps no argument
1726
+ * either. Its result names the commit each drafted record stood at
1727
+ * @property draftEnd - How its drafts ended it before its function ran
1728
+ * ({@link FunctionDraftEndType}); `none` while it runs, and for a call they
1729
+ * did not end
1541
1730
  * @property limits - The limits it ran under
1542
1731
  * @property expiresAt - When it is forgotten: its lifetime's end, which its
1543
1732
  * end sets. A running call's is past its deadline by a grace and then a
@@ -1557,7 +1746,9 @@ export const FunctionCallType = StructType({
1557
1746
  state: FunctionCallStateType,
1558
1747
  package: OptionType(StructType({ name: StringType, version: StringType })),
1559
1748
  args: ArrayType(FunctionCallArgType),
1560
- inputs: ArrayType(StructType({ path: TreePathType, hash: StringType })),
1749
+ inputs: ArrayType(InputReadType),
1750
+ drafts: ArrayType(FunctionCallDraftType),
1751
+ draftEnd: OptionType(FunctionDraftEndType),
1561
1752
  limits: StructType({ timeoutMs: IntegerType, maxResultBytes: IntegerType, maxLogBytes: IntegerType }),
1562
1753
  expiresAt: DateTimeType,
1563
1754
  stdout: StringType,
@@ -1867,7 +2058,13 @@ export type DatasetStatusDetail = ValueTypeOf<typeof DatasetStatusDetailType>;
1867
2058
  export type ExecuteLimits = ValueTypeOf<typeof ExecuteLimitsType>;
1868
2059
  export type Diagnostic = ValueTypeOf<typeof DiagnosticType>;
1869
2060
  export type ExecuteResult = ValueTypeOf<typeof ExecuteResultType>;
2061
+ export type InputRead = ValueTypeOf<typeof InputReadType>;
1870
2062
  export type CallArg = ValueTypeOf<typeof CallArgType>;
2063
+ export type FunctionDraft = ValueTypeOf<typeof FunctionDraftType>;
2064
+ export type FunctionDraftConflict = ValueTypeOf<typeof FunctionDraftConflictType>;
2065
+ export type FunctionDrafted = ValueTypeOf<typeof FunctionDraftedType>;
2066
+ export type FunctionDraftFailure = ValueTypeOf<typeof FunctionDraftFailureType>;
2067
+ export type FunctionDraftEnd = ValueTypeOf<typeof FunctionDraftEndType>;
1871
2068
  export type FunctionCallRequest = ValueTypeOf<typeof FunctionCallRequestType>;
1872
2069
  export type FunctionSignature = ValueTypeOf<typeof FunctionSignatureType>;
1873
2070
  export type OneShotRequest = ValueTypeOf<typeof OneShotRequestType>;
@@ -1878,6 +2075,7 @@ export type SplitCallStatus = ValueTypeOf<typeof SplitCallStatusType>;
1878
2075
  export type SplitCallPlan = ValueTypeOf<typeof SplitCallPlanType>;
1879
2076
  export type FunctionCallState = ValueTypeOf<typeof FunctionCallStateType>;
1880
2077
  export type FunctionCallArg = ValueTypeOf<typeof FunctionCallArgType>;
2078
+ export type FunctionCallDraft = ValueTypeOf<typeof FunctionCallDraftType>;
1881
2079
  export type FunctionCallSummary = ValueTypeOf<typeof FunctionCallSummaryType>;
1882
2080
  export type FunctionCall = ValueTypeOf<typeof FunctionCallType>;
1883
2081
  export type FunctionCallStatus = ValueTypeOf<typeof FunctionCallStatusType>;
@@ -14,17 +14,19 @@
14
14
  * shown, the call as the API answers it ({@link FunctionCallType}). Beside it,
15
15
  * it keeps what the call's run needs and no reader is shown: the function
16
16
  * object it runs, the runner it runs on, whether it runs verbose, its
17
- * deadline, and a request to cancel it.
17
+ * deadline, a request to cancel it, and what its run lays its drafts from.
18
18
  *
19
19
  * It is stored state (`WIRE_MIGRATION.md`): a change to its type ships a
20
20
  * repository upgrade step.
21
21
  */
22
22
 
23
23
  import {
24
- BooleanType, DateTimeType, IntegerType, OptionType, StringType, StructType, decodeBeast2For, encodeBeast2For, type ValueTypeOf,
24
+ ArrayType, BooleanType, DateTimeType, DictType, IntegerType, OptionType, StringType, StructType, decodeBeast2For, encodeBeast2For,
25
+ type ValueTypeOf,
25
26
  } from '@elaraai/east';
26
27
  import { FunctionCallType } from './api.js';
27
28
  import { RunnerType } from './runner.js';
29
+ import { TreePathType } from './structure.js';
28
30
 
29
31
  /**
30
32
  * A request to cancel a running call: who asked, and when.
@@ -49,7 +51,7 @@ export type FunctionCallCancel = ValueTypeOf<typeof FunctionCallCancelType>;
49
51
  *
50
52
  * @property succeeded - A call that succeeded, or was cancelled
51
53
  * @property failed - A call that failed, was refused (`invalid`), returned too
52
- * large a value, timed out, or was lost
54
+ * large a value, timed out, met a `conflict` laying a draft, or was lost
53
55
  */
54
56
  export const FunctionCallKeepType = StructType({
55
57
  succeeded: IntegerType,
@@ -60,6 +62,37 @@ export type FunctionCallKeepType = typeof FunctionCallKeepType;
60
62
  /** How long a call is kept once it ends. */
61
63
  export type FunctionCallKeep = ValueTypeOf<typeof FunctionCallKeepType>;
62
64
 
65
+ /**
66
+ * A record a kept call drafts, as its run lays the drafts: everything the
67
+ * laying reads, named as the call started, so the run reads the workspace no
68
+ * more and lays the same state however often it is run; and the commit the
69
+ * record stood at, which the call's result names.
70
+ *
71
+ * @property record - The record's name, as the call's drafts name it
72
+ * @property path - Where the workspace holds the record: the call's inputs at
73
+ * this path are read at the drafted state, and a dataset argument naming it
74
+ * as saved, at `base`
75
+ * @property base - The state the record held as the call started, which the
76
+ * call's drafts of it are laid over, in order
77
+ * @property commit - The commit the record stood at as the call started,
78
+ * read with `base`: a caller that holds another knows its edits were laid
79
+ * over newer saves
80
+ * @property mutations - The object of each mutation the call's drafts of the
81
+ * record name, by the mutation's name: the one the deployed package named
82
+ * when the call started, whatever is deployed since
83
+ */
84
+ export const FunctionCallDraftedType = StructType({
85
+ record: StringType,
86
+ path: TreePathType,
87
+ base: StringType,
88
+ commit: StringType,
89
+ mutations: DictType(StringType, StringType),
90
+ });
91
+ export type FunctionCallDraftedType = typeof FunctionCallDraftedType;
92
+
93
+ /** A record a kept call drafts, as its run lays the drafts. */
94
+ export type FunctionCallDrafted = ValueTypeOf<typeof FunctionCallDraftedType>;
95
+
63
96
  /**
64
97
  * A kept call, as its store keeps it.
65
98
  *
@@ -76,6 +109,9 @@ export type FunctionCallKeep = ValueTypeOf<typeof FunctionCallKeepType>;
76
109
  * once its deadline and a grace have passed is lost
77
110
  * @property cancel - A request to cancel it, once one is made
78
111
  * @property keep - How long it is kept once it ends, by how it ended
112
+ * @property drafted - The records it drafts, each once, in the order its
113
+ * drafts first name them, with what its run lays them from and the commit
114
+ * each stood at; none for a call that passed no draft
79
115
  */
80
116
  export const FunctionCallJobType = StructType({
81
117
  call: FunctionCallType,
@@ -86,6 +122,7 @@ export const FunctionCallJobType = StructType({
86
122
  deadline: DateTimeType,
87
123
  cancel: OptionType(FunctionCallCancelType),
88
124
  keep: FunctionCallKeepType,
125
+ drafted: ArrayType(FunctionCallDraftedType),
89
126
  });
90
127
  export type FunctionCallJobType = typeof FunctionCallJobType;
91
128
 
@@ -124,14 +161,25 @@ export function decodeFunctionCallJob(bytes: Uint8Array): FunctionCallJob {
124
161
  * its own, which no package names, so the call alone keeps it, and its
125
162
  * program, for as long as it runs and is kept.
126
163
  *
164
+ * A call that drafts a record names more: each draft's arguments; the state
165
+ * each drafted record held as the call started and the mutations its drafts
166
+ * run, which its run lays the drafts from; and, among its inputs once the
167
+ * drafts are laid, the drafted state, which nothing else names. So the drafted
168
+ * state lives as long as the call is kept, and is collected after it.
169
+ *
127
170
  * @param job - The call
128
171
  * @returns The hashes it names: its function object's, then its arguments' in
129
- * order, then its inputs', then its value's
172
+ * order, then its inputs', then its drafts' arguments', then each drafted
173
+ * record's state and mutations, then its value's
130
174
  */
131
175
  export function functionCallRoots(job: FunctionCallJob): string[] {
132
176
  const { call } = job;
133
177
  const roots = [job.functionObject, ...call.args.map((arg) => (arg.type === 'value' ? arg.value : arg.value.hash))];
134
178
  for (const input of call.inputs) roots.push(input.hash);
179
+ for (const draft of call.drafts) {
180
+ if (draft.args.type === 'some') roots.push(draft.args.value);
181
+ }
182
+ for (const record of job.drafted) roots.push(record.base, ...record.mutations.values());
135
183
  if (call.state.type === 'succeeded') roots.push(call.state.value.value);
136
184
  return roots;
137
185
  }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+
6
+ /**
7
+ * How a call says which of its drafts ended it (east-workspace#1619): the
8
+ * lines its stderr says it in, for people to read, and the end its result
9
+ * names, which a caller reads it from — never the line, which a function's own
10
+ * stderr may say too (#1680's review).
11
+ */
12
+
13
+ import { describe, it } from 'node:test';
14
+ import assert from 'node:assert/strict';
15
+ import { equalFor, none, some, variant } from '@elaraai/east';
16
+ import { FunctionDraftFailureType, type ExecuteResult, type FunctionDraftFailure } from './api.js';
17
+ import { functionDraftEndLine, functionDraftEnded, functionDraftLabel, functionDraftsLaidLine, functionDraftsOutlasted } from './function-draft.js';
18
+
19
+ const sameFailure = equalFor(FunctionDraftFailureType);
20
+
21
+ /** A draft that ended a call, as its result names it. */
22
+ function draft(record: string, mutation: string, place: bigint, of: bigint, how: 'failed' | 'timed_out'): FunctionDraftFailure {
23
+ return { record, mutation, place, of, how: how === 'failed' ? variant('failed', null) : variant('timed_out', null) };
24
+ }
25
+
26
+ /** A call's result, as far as the readers read it: what ended it, its outcome and its stderr. */
27
+ type Ended = Pick<ExecuteResult, 'outcome' | 'stderr' | 'draftEnd'>;
28
+
29
+ const failed = variant('failed', { exitCode: 3n });
30
+ const timedOut = variant('timed_out', { ms: 600_000n });
31
+
32
+ describe('the lines a call says what of its drafts ended it in', () => {
33
+ it("names a draft by its record, its mutation and its place among the call's, and how many were laid", () => {
34
+ assert.equal(functionDraftLabel({ record: 'jobs', mutation: 'patch' }, 2, 3), 'draft jobs.patch (2 of 3)');
35
+ assert.equal(functionDraftEndLine(draft('jobs', 'patch', 2n, 3n, 'failed')), 'draft jobs.patch (2 of 3) failed');
36
+ assert.equal(functionDraftEndLine(draft('jobs', 'patch', 1n, 1n, 'timed_out')), 'draft jobs.patch (1 of 1) timed out');
37
+ assert.equal(functionDraftsLaidLine(3), 'drafts laid (3 of 3), and the call\'s timeout ended before its function ran');
38
+ });
39
+ });
40
+
41
+ describe('what of its drafts ended a call, as its result names it', () => {
42
+ it('reads back the draft a call names as its end, whatever its record\'s name holds', () => {
43
+ const ends = [draft('jobs', 'patch', 2n, 3n, 'failed'), draft('jobs', 'move', 12n, 12n, 'timed_out'), draft('plant.jobs', 'patch', 1n, 1n, 'failed')];
44
+ for (const end of ends) {
45
+ const result: Ended = { outcome: end.how.type === 'failed' ? failed : timedOut, stderr: `${functionDraftEndLine(end)}\n`, draftEnd: some(variant('draft', end)) };
46
+ const read = functionDraftEnded(result);
47
+ assert.ok(read !== null && sameFailure(read, end), functionDraftEndLine(end));
48
+ assert.equal(functionDraftsOutlasted(result), false, 'a draft that ended it is no timeout once every draft was laid');
49
+ }
50
+ });
51
+
52
+ it('reads that a call\'s timeout ended once its drafts were all laid, naming no draft', () => {
53
+ const result: Ended = { outcome: timedOut, stderr: `${functionDraftsLaidLine(3)}\n`, draftEnd: some(variant('laid', { of: 3n })) };
54
+ assert.equal(functionDraftsOutlasted(result), true);
55
+ assert.equal(functionDraftEnded(result), null);
56
+ });
57
+
58
+ it('names no end for a call its drafts did not end, whatever its stderr says: a function that ran and wrote the lines itself (#1680\'s review)', () => {
59
+ const line = functionDraftEndLine(draft('jobs', 'patch', 1n, 1n, 'failed'));
60
+ const mimic: Ended = { outcome: failed, stderr: `${line}\nError: refused\n`, draftEnd: none };
61
+ assert.equal(functionDraftEnded(mimic), null, 'a function whose own stderr begins with a draft\'s end');
62
+ const outlasting: Ended = { outcome: timedOut, stderr: `${functionDraftsLaidLine(2)}\n`, draftEnd: none };
63
+ assert.equal(functionDraftsOutlasted(outlasting), false, 'a function whose own stderr says its drafts were laid');
64
+ const success: Ended = { outcome: variant('success', { value: new Uint8Array() }), stderr: `${line}\n`, draftEnd: none };
65
+ assert.deepEqual([functionDraftEnded(success), functionDraftsOutlasted(success)], [null, false], 'a call whose function ran to its end');
66
+ const conflict: Ended = { outcome: variant('conflict', { record: 'jobs', mutation: 'patch', place: 1n, of: 1n, message: 'gone' }), stderr: '', draftEnd: none };
67
+ assert.equal(functionDraftEnded(conflict), null, 'a conflict, whose outcome names its draft');
68
+ });
69
+ });
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+
6
+ /**
7
+ * How a call says which of its drafts ended it (east-workspace#1619).
8
+ *
9
+ * A draft is an unsaved edit a call lays over a record its function reads. A
10
+ * call a draft's mutation ended `failed` or `timed_out`, before its function
11
+ * ran, names the draft in its result's `draftEnd` (`FunctionDraftEndType`),
12
+ * and says which in the first line of its stderr, for people to read: `draft
13
+ * jobs.patch (2 of 3) failed`, or `draft jobs.patch (2 of 3) timed out`. The
14
+ * mutation's own stderr follows it, as a Save returns it. A call whose timeout
15
+ * ended once its drafts were all laid, before its function ran, names that in
16
+ * its `draftEnd`, and says so in its first line instead: `drafts laid (3 of 3),
17
+ * and the call's timeout ended before its function ran`.
18
+ *
19
+ * A caller reads what ended a call from its `draftEnd` alone, never from the
20
+ * line (#1680's review): a function that ran may write the same line itself.
21
+ * The lines are written here, so every job that ends a call says them alike.
22
+ */
23
+
24
+ import type { ExecuteResult, FunctionDraftFailure } from './api.js';
25
+
26
+ /** How each end reads in the line. */
27
+ const HOW = { failed: 'failed', timed_out: 'timed out' } as const;
28
+
29
+ /**
30
+ * How a call names one of its drafts: `draft jobs.patch (2 of 3)`. A refusal
31
+ * of the draft begins with it, and so does the line a call it ended says so in
32
+ * ({@link functionDraftEndLine}).
33
+ *
34
+ * @param draft - The draft's record and mutation
35
+ * @param place - Its place among the call's drafts, from 1
36
+ * @param of - How many drafts the call passed
37
+ * @returns The draft's name
38
+ */
39
+ export function functionDraftLabel(draft: { readonly record: string; readonly mutation: string }, place: number, of: number): string {
40
+ return `draft ${draft.record}.${draft.mutation} (${place} of ${of})`;
41
+ }
42
+
43
+ /**
44
+ * The first line of the stderr of a call a draft ended: `draft jobs.patch (2
45
+ * of 3) failed`, or `… timed out`.
46
+ *
47
+ * @param end - The draft, and how its mutation ended, as the call's `draftEnd`
48
+ * names it
49
+ * @returns The line, with no line end
50
+ */
51
+ export function functionDraftEndLine(end: FunctionDraftFailure): string {
52
+ return `${functionDraftLabel(end, Number(end.place), Number(end.of))} ${HOW[end.how.type]}`;
53
+ }
54
+
55
+ /**
56
+ * The draft that ended a call before its function ran, as its result's
57
+ * `draftEnd` names it.
58
+ *
59
+ * @remarks
60
+ * A call whose timeout ended between two drafts' runs names the draft that
61
+ * did not start. One whose timeout ended once its drafts were all laid names
62
+ * none ({@link functionDraftsOutlasted} says so of it), and neither does a
63
+ * `conflict`, whose outcome names its draft itself. Neither does a call whose
64
+ * function ran, whatever the first line of its stderr says.
65
+ *
66
+ * @param result - A call's result: what ended it
67
+ * @returns The draft, or `null` when no draft's mutation ended the call
68
+ */
69
+ export function functionDraftEnded(result: Pick<ExecuteResult, 'draftEnd'>): FunctionDraftFailure | null {
70
+ return result.draftEnd.type === 'some' && result.draftEnd.value.type === 'draft' ? result.draftEnd.value.value : null;
71
+ }
72
+
73
+ /**
74
+ * The first line of the stderr of a call whose timeout ended once its drafts
75
+ * were all laid, before its function ran: `drafts laid (3 of 3), and the
76
+ * call's timeout ended before its function ran`.
77
+ *
78
+ * @param of - How many drafts the call passed, every one of them laid
79
+ * @returns The line, with no line end
80
+ */
81
+ export function functionDraftsLaidLine(of: number): string {
82
+ return `drafts laid (${of} of ${of}), and the call's timeout ended before its function ran`;
83
+ }
84
+
85
+ /**
86
+ * Whether a call's timeout ended once its drafts were all laid, before its
87
+ * function ran, as its result's `draftEnd` says: the time went on its drafts,
88
+ * and its function did not run.
89
+ *
90
+ * @param result - A call's result: what ended it
91
+ * @returns Whether it ended so; `false` for a call that passed no draft, one
92
+ * that did not time out, and one whose function ran
93
+ */
94
+ export function functionDraftsOutlasted(result: Pick<ExecuteResult, 'draftEnd'>): boolean {
95
+ return result.draftEnd.type === 'some' && result.draftEnd.value.type === 'laid';
96
+ }