@genesislcap/ai-assistant 15.49.0 → 15.50.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 (52) hide show
  1. package/dist/custom-elements.json +100 -2
  2. package/dist/dts/genesis/config.d.ts +31 -0
  3. package/dist/dts/genesis/config.d.ts.map +1 -1
  4. package/dist/dts/genesis/consumes.d.ts.map +1 -1
  5. package/dist/dts/genesis/index.d.ts +2 -2
  6. package/dist/dts/genesis/index.d.ts.map +1 -1
  7. package/dist/dts/genesis/reference-check.d.ts +4 -3
  8. package/dist/dts/genesis/reference-check.d.ts.map +1 -1
  9. package/dist/dts/genesis/reference-existence.d.ts +3 -3
  10. package/dist/dts/genesis/reference-existence.d.ts.map +1 -1
  11. package/dist/dts/genesis/review-model.d.ts +59 -4
  12. package/dist/dts/genesis/review-model.d.ts.map +1 -1
  13. package/dist/dts/genesis/types.d.ts +43 -0
  14. package/dist/dts/genesis/types.d.ts.map +1 -1
  15. package/dist/dts/genesis/write-commit.d.ts +2 -2
  16. package/dist/dts/genesis/write-commit.d.ts.map +1 -1
  17. package/dist/dts/genesis/write-op.d.ts +34 -0
  18. package/dist/dts/genesis/write-op.d.ts.map +1 -0
  19. package/dist/dts/genesis/write-outcome.d.ts +2 -2
  20. package/dist/dts/genesis/write-outcome.d.ts.map +1 -1
  21. package/dist/dts/genesis/write-payload.d.ts +3 -3
  22. package/dist/dts/genesis/write-payload.d.ts.map +1 -1
  23. package/dist/dts/genesis/write-plan.d.ts +18 -7
  24. package/dist/dts/genesis/write-plan.d.ts.map +1 -1
  25. package/dist/dts/genesis/write-review/write-review.d.ts +6 -0
  26. package/dist/dts/genesis/write-review/write-review.d.ts.map +1 -1
  27. package/dist/dts/genesis/write-review/write-review.styles.d.ts.map +1 -1
  28. package/dist/dts/genesis/write-review/write-review.template.d.ts.map +1 -1
  29. package/dist/dts/genesis/write-tools.d.ts +2 -2
  30. package/dist/dts/genesis/write-tools.d.ts.map +1 -1
  31. package/dist/dts/genesis/write-turn.d.ts +5 -3
  32. package/dist/dts/genesis/write-turn.d.ts.map +1 -1
  33. package/dist/esm/genesis/config.js +204 -5
  34. package/dist/esm/genesis/consumes.js +5 -0
  35. package/dist/esm/genesis/index.js +1 -1
  36. package/dist/esm/genesis/review-model.js +62 -10
  37. package/dist/esm/genesis/write-op.js +18 -0
  38. package/dist/esm/genesis/write-outcome.js +13 -6
  39. package/dist/esm/genesis/write-payload.js +22 -10
  40. package/dist/esm/genesis/write-plan.js +121 -12
  41. package/dist/esm/genesis/write-review/write-review.js +20 -1
  42. package/dist/esm/genesis/write-review/write-review.styles.js +14 -0
  43. package/dist/esm/genesis/write-review/write-review.template.js +25 -7
  44. package/dist/esm/genesis/write-tools.js +304 -74
  45. package/dist/esm/genesis/write-turn.js +9 -5
  46. package/dist/genesis-consumes.cjs +6 -1
  47. package/dist/genesis-consumes.cjs.map +2 -2
  48. package/dist/genesis-consumes.mjs +6 -1
  49. package/dist/genesis-consumes.mjs.map +2 -2
  50. package/docs/genesis.md +171 -45
  51. package/package.json +17 -17
  52. package/scripts/smoke-genesis-consumes-node.mjs +14 -1
@@ -31,6 +31,33 @@ const defaultTitle = (name) => name
31
31
  * value long enough to push the change out of view wraps, where being on several lines is plain.
32
32
  */
33
33
  export const LONG_VALUE = 24;
34
+ /**
35
+ * The header of a row action's review. It names the handler and its table, and never the op: the
36
+ * write is the handler's, not a modify, and `row-action` is the bridge's own word.
37
+ *
38
+ * @internal
39
+ */
40
+ export const rowActionHeading = (rowAction, rows) => `Run ${rowAction.handler} on ${rows} ${rowAction.entity} row(s). Nothing is written until you ` +
41
+ 'choose rows and select Write.';
42
+ /**
43
+ * The lines under a row action's header: what its code wrote, that the row is shown as it is, and
44
+ * that the app may do more in response (contracts C-18.7.7 as C-18.R amends it).
45
+ *
46
+ * @remarks
47
+ * Dated to generation, because an exported app's code can change after it. The effects are what
48
+ * the code does, in its order, not a summary, and the last line is there because consolidators and
49
+ * the app's own logic can react to these writes, which no review here can see.
50
+ *
51
+ * @internal
52
+ */
53
+ export const rowActionLines = (rowAction) => [
54
+ "When this app was generated, the handler's code wrote: " +
55
+ `${rowAction.effects.map(({ op, table }) => `${op} ${table}`).join(', ')}.`,
56
+ "The row is shown as it is now; the handler's code decides what changes.",
57
+ 'Other parts of the app can update more records in response to this.',
58
+ ];
59
+ /** A run's inputs as its payload carries them. */
60
+ const inputsOf = (details, inputs) => Object.fromEntries(inputs.filter((field) => field in details).map((field) => [field, details[field]]));
34
61
  /** `A`, `A and B`, `A, B and C`. */
35
62
  const joined = (items) => items.length > 1 ? `${items.slice(0, -1).join(', ')} and ${items[items.length - 1]}` : items[0];
36
63
  /**
@@ -172,11 +199,17 @@ export function displayValue(value, type) {
172
199
  export function buildReviewModel(options) {
173
200
  var _a;
174
201
  const { resource, op, fields = [], rows, customCode } = options;
202
+ const run = op === 'row-action' ? options.rowAction : undefined;
203
+ // A run without its facts would be drawn as a modify, its changes marked, when what changes is
204
+ // the handler's to decide. So every row of it is refused instead: nothing can be written.
205
+ const unexplained = op === 'row-action' && !run;
175
206
  const byName = new Map(fields.map((field) => [field.name, field]));
176
207
  // Declared order first, then anything a row carries that the metadata did not describe, in the
177
208
  // order it is first seen. One column list, so every row's cells line up under it.
178
209
  const columnNames = fields.map((field) => field.name);
179
- for (const row of rows) {
210
+ // A run's columns are its event's fields and no others: what is shown is what is checked before
211
+ // the write, and a column of the row it never sends would be shown and never checked.
212
+ for (const row of run ? [] : rows) {
180
213
  // What is RENDERED, which for a delete is the whole row rather than the key it sends. The
181
214
  // platform's own columns are left out: a modify's review hides them because they never
182
215
  // travel, and a delete showing RECORD_ID and TIMESTAMP would be the same table telling the
@@ -194,16 +227,24 @@ export function buildReviewModel(options) {
194
227
  });
195
228
  });
196
229
  const built = rows.map((row) => {
197
- var _a, _b, _c;
230
+ var _a, _b;
198
231
  // A REFUSED row will not be written, so it has no change to show — and a refused modify can come
199
232
  // with its pre-image and an EMPTY payload, which marked every non-empty field as changed and
200
233
  // rendered the row as a full wipe: each old value struck through over an empty new one. The
201
234
  // screen whose job is to show what will happen showed the most alarming outcome, for a row
202
235
  // that will not happen at all, often beside a refusal reading "this modify changes nothing".
203
236
  // So a refused row shows the row as it stands, and nothing on it is marked as changing.
204
- const refused = !!((_a = row.problems) === null || _a === void 0 ? void 0 : _a.length);
205
- const compared = refused ? undefined : row.preImage;
206
- const shown = (_b = row.display) !== null && _b !== void 0 ? _b : (refused && row.preImage ? row.preImage : row.details);
237
+ const problems = unexplained
238
+ ? [...((_a = row.problems) !== null && _a !== void 0 ? _a : []), 'this run cannot be shown without its handler and table']
239
+ : row.problems;
240
+ const refused = !!(problems === null || problems === void 0 ? void 0 : problems.length);
241
+ // A run is no change from the row, so nothing is compared: the row is shown as it is, with each
242
+ // input as the handler is given it. A refused run shows the row as read and no input, so a
243
+ // value under the input mark is always one the handler would be given. One refused for its
244
+ // inputs was never read, and shows only its problem.
245
+ const compared = refused || run ? undefined : row.preImage;
246
+ const shown = run
247
+ ? Object.assign(Object.assign({}, row.preImage), (refused ? {} : inputsOf(row.details, run.inputs))) : ((_b = row.display) !== null && _b !== void 0 ? _b : (refused && row.preImage ? row.preImage : row.details));
207
248
  // Only meaningful where the two differ; on a modify or an insert everything shown is sent.
208
249
  const marksIdentifying = !!row.display;
209
250
  const cells = columnNames.map((field) => {
@@ -212,13 +253,16 @@ export function buildReviewModel(options) {
212
253
  const previous = compared ? compared[field] : undefined;
213
254
  const display = displayValue(shown[field], declared === null || declared === void 0 ? void 0 : declared.type);
214
255
  const previousDisplay = compared ? displayValue(previous, declared === null || declared === void 0 ? void 0 : declared.type) : undefined;
215
- return Object.assign(Object.assign({ field, label: (_a = declared === null || declared === void 0 ? void 0 : declared.title) !== null && _a !== void 0 ? _a : defaultTitle(field), display }, (compared ? { previousDisplay } : {})), {
256
+ return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ field, label: (_a = declared === null || declared === void 0 ? void 0 : declared.title) !== null && _a !== void 0 ? _a : defaultTitle(field), display }, (compared ? { previousDisplay } : {})), {
216
257
  // Compared on the VALUES, with both sides normalised — never on the display text.
217
258
  // Display is deliberately lossy (a timestamp is rendered to the minute), so a change of
218
259
  // seconds would render identically on both sides and go unmarked, which is the review
219
260
  // failing at its one job. A raw comparison would be wrong the other way, marking every
220
261
  // row changed, so the normaliser is what makes both correct.
221
- changed: !!compared && !sameValue(previous, shown[field], declared === null || declared === void 0 ? void 0 : declared.type), identifying: marksIdentifying && field in row.details, long: display.length > LONG_VALUE || ((_b = previousDisplay === null || previousDisplay === void 0 ? void 0 : previousDisplay.length) !== null && _b !== void 0 ? _b : 0) > LONG_VALUE });
262
+ changed: !!compared && !sameValue(previous, shown[field], declared === null || declared === void 0 ? void 0 : declared.type),
263
+ // A run names its row by its key, explicitly: its details are the whole row, so the
264
+ // delete's rule would mark every cell.
265
+ identifying: run ? run.key.includes(field) : marksIdentifying && field in row.details }), (run && run.key.includes(field) ? { identifies: 'the row the handler runs on' } : {})), (run && !refused && run.inputs.includes(field) ? { input: true } : {})), { long: display.length > LONG_VALUE || ((_b = previousDisplay === null || previousDisplay === void 0 ? void 0 : previousDisplay.length) !== null && _b !== void 0 ? _b : 0) > LONG_VALUE });
222
266
  });
223
267
  // The identifying cells where the row has them — a delete marks its key, and a modify or an
224
268
  // insert has none, so those fall back to the first cell that carries a value.
@@ -229,11 +273,19 @@ export function buildReviewModel(options) {
229
273
  const spoken = (naming.length ? naming : cells.filter((cell) => cell.display).slice(0, 1))
230
274
  .map((cell) => `${cell.label} "${spokenValue(cell.display)}"`)
231
275
  .join(', ');
232
- return Object.assign({ index: row.index, details: row.details, cells, label: spoken || `row ${row.index + 1}` }, (((_c = row.problems) === null || _c === void 0 ? void 0 : _c.length) ? { problems: row.problems } : {}));
276
+ return Object.assign({ index: row.index, details: row.details, cells, label: spoken || `row ${row.index + 1}` }, ((problems === null || problems === void 0 ? void 0 : problems.length) ? { problems } : {}));
233
277
  });
234
- return Object.assign({ resource,
278
+ return Object.assign(Object.assign({ resource,
235
279
  op,
236
- columns, rows: built, selectable: built.filter((row) => { var _a; return !((_a = row.problems) === null || _a === void 0 ? void 0 : _a.length); }).map((row) => row.index) }, (customCode
280
+ columns, rows: built, selectable: built.filter((row) => { var _a; return !((_a = row.problems) === null || _a === void 0 ? void 0 : _a.length); }).map((row) => row.index) }, (run
281
+ ? {
282
+ rowAction: {
283
+ handler: run.handler,
284
+ entity: run.entity,
285
+ effects: run.effects.map(({ op: write, table }) => ({ op: write, table })),
286
+ },
287
+ }
288
+ : {})), (customCode
237
289
  ? {
238
290
  customCode: {
239
291
  alsoWrites: [...customCode.alsoWrites],
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Whether a write reads its row before the review, and again before it commits.
3
+ *
4
+ * @remarks
5
+ * Every place that asks this asks it here. A row action reads its row like a modify, and a site
6
+ * left asking "modify or delete" would treat one like an insert: no row read, no merge, no
7
+ * staleness check before the commit.
8
+ *
9
+ * @internal
10
+ */
11
+ export const readsRow = (op) => op === 'modify' || op === 'delete' || op === 'row-action';
12
+ /**
13
+ * How a model-facing sentence names one write: "a modify", "a delete", or, for a row action, "a run
14
+ * of this handler". The op's own name is never put in front of the model: `row-action` is ours.
15
+ *
16
+ * @internal
17
+ */
18
+ export const aWrite = (op) => op === 'row-action' ? 'a run of this handler' : `a ${op}`;
@@ -1,6 +1,7 @@
1
1
  import { logger } from '../utils/logger';
2
2
  import { serialisable } from './serialisable';
3
3
  import { plainFieldName, plainServerText, readsLikeFieldName, readsLikeKeyValue, } from './server-text';
4
+ import { readsRow } from './write-op';
4
5
  /**
5
6
  * Whether a MSG_NACK error says the event is not mapped to a service — the one MSG_NACK that
6
7
  * means nothing ran. Live 8.15: `{"CODE": 404, "TEXT": "Resource EVENT_X is not mapped to a
@@ -238,7 +239,9 @@ export function mapWriteReply(reply, { mode, op }) {
238
239
  }
239
240
  // Everything from here is an EVENT_NACK: the HANDLER's own answer. It is read as "not written"
240
241
  // whatever STATUS_CODE its errors carry, 5xx included — do not "fix" that to `unknown` by
241
- // analogy with the router's MSG_NACK above, but know exactly what it rests on.
242
+ // analogy with the router's MSG_NACK above, but know exactly what it rests on. The code is not
243
+ // a signal here: a handler's own nack() arrives as INTERNAL_ERROR 500, a throw as
244
+ // REQUEST_FAILED 400 (contracts C-18.L, LC-8 and LC-9).
242
245
  //
243
246
  // genesis-server v8.15.14, TransactionalEventHandlerContainer.transactionalEventFlow
244
247
  // (genesis-parent/genesis-pal-eventhandler/.../eventhandler/pal/parse/
@@ -250,17 +253,21 @@ export function mapWriteReply(reply, { mode, op }) {
250
253
  // - onCommit writes rows and then RETURNS nack(): the block completes normally, nothing rolls
251
254
  // back, and those writes COMMIT, under a NACK.
252
255
  // So "an EVENT_NACK means not written" holds for the handlers this path accepts: Create-
253
- // generated CRUD handlers, which write and ack(), or throw and roll back. It does not hold in
254
- // general — a hand-written handler that writes and then returns nack() commits its writes —
255
- // and that is one more reason `custom` events get no write tool.
256
+ // generated CRUD handlers, which write and ack(), or throw and roll back; and a ROW ACTION's
257
+ // handler, which the app generator accepts only when its code never nacks after a write and
258
+ // never writes while validating (contracts C-18.4 G12 and G7), so it too nacks before writing
259
+ // or throws and rolls back (contracts C-18.L, LC-9: a throw after a modify and an insert
260
+ // rolled both back). It does not hold in general — a hand-written handler that writes and
261
+ // then returns nack() commits its writes — and that is why any other custom event gets no
262
+ // write tool.
256
263
  //
257
- // Only a modify or a delete has an existing row to lose. On an insert RECORD_NOT_FOUND is a
264
+ // Only an op that read its row has an existing row to lose. On an insert RECORD_NOT_FOUND is a
258
265
  // reference the handler looked up ("Counterparty 'CP999' does not exist"), and "that row changed
259
266
  // or was removed" was said about a row it was creating, with the app's reason dropped. An insert
260
267
  // falls through to the ordinary classification: the live shape, a TableStandardError, is a
261
268
  // database_error; anything else, the app's refusal. Both are not written, and both carry the
262
269
  // server's own text.
263
- if ((op === 'modify' || op === 'delete') && codes.has('RECORD_NOT_FOUND')) {
270
+ if (readsRow(op) && codes.has('RECORD_NOT_FOUND')) {
264
271
  return Object.assign(Object.assign({ status: 'gone' }, withFields), { message: `That row changed or was removed, so nothing was written: ${joined(errors)}` });
265
272
  }
266
273
  if (codes.has('DUPLICATE_KEY')) {
@@ -2,6 +2,7 @@ import { utcMillisOf } from './calendar';
2
2
  import { cannotBeChanged, notAcceptedHere } from './reference-check';
3
3
  import { genesisTypeName } from './type-name';
4
4
  import { sameValue } from './value-compare';
5
+ import { aWrite, readsRow } from './write-op';
5
6
  /**
6
7
  * Epoch millis, the floor below which a "date" is not one.
7
8
  *
@@ -85,7 +86,7 @@ const timeOfDayOnADay = (field, value) => DAY_TYPES.has(genesisTypeName(field.ty
85
86
  * row's value is the row's too. `fromRow` is false for an echo, so the value is compared with the
86
87
  * row's.
87
88
  */
88
- function timeOfDayRefusal(field, { raw, value, fromRow, rowValue, }) {
89
+ function timeOfDayRefusal(field, { raw, value, fromRow, rowValue, op, }) {
89
90
  if (fromRow || !timeOfDayOnADay(field, value))
90
91
  return undefined;
91
92
  const rowMillis = rowValue === undefined ? undefined : toEpochMillis(rowValue);
@@ -94,7 +95,10 @@ function timeOfDayRefusal(field, { raw, value, fromRow, rowValue, }) {
94
95
  // The row itself holds a time here, and an echo of it that is off by a second, or spelt without
95
96
  // its seconds, is not that value. Sent as the bare day it would overwrite the stored time, so
96
97
  // the model is told it can leave the field out instead.
97
- const keep = timeOfDayOnADay(field, rowMillis) ? ", or leave it out to keep the row's value" : '';
98
+ // A run's input cannot be left out: the handler needs it.
99
+ const keep = op !== 'row-action' && timeOfDayOnADay(field, rowMillis)
100
+ ? ", or leave it out to keep the row's value"
101
+ : '';
98
102
  return `${field.name} is a date; send it without a time of day${keep} (got '${quoted(raw)}')`;
99
103
  }
100
104
  /** Genesis types that hold a whole number, whatever JSON type their metadata declares. */
@@ -325,10 +329,11 @@ export const PLATFORM_MAINTAINED = new Set(['RECORD_ID', 'TIMESTAMP']);
325
329
  * field is carried from the row (see `sourceOf`), and a model value that differs is refused by
326
330
  * name: keyed on another field, `changes: { TRADE_ID: 'SOMEONE_ELSE' }` used to go on the wire.
327
331
  *
328
- * A `custom` event stays strict: its semantics are the app's, so there is nothing to reason
329
- * from, and its NACK will say what it wanted.
332
+ * A row action is loosened as a modify is: its handler loads the row by the key it is sent, so
333
+ * the row's own key has to travel. A plain `custom` event stays strict: its semantics are the
334
+ * app's, so there is nothing to reason from, and its NACK will say what it wanted.
330
335
  */
331
- const readOnlyAllowedFor = (op) => op === 'modify' || op === 'delete';
336
+ const readOnlyAllowedFor = (op) => readsRow(op);
332
337
  /**
333
338
  * The fields this op may put on the wire: what the event declares, minus the platform's own
334
339
  * columns, minus anything the op may not send.
@@ -490,6 +495,12 @@ function sourceOf(field, given, changes, rowValue, merge, op) {
490
495
  // "change" whose two sides render identically, or a `' '` that in effect cleared the column
491
496
  // this tool says it cannot clear; on an insert, spaces written into a column the review showed
492
497
  // as empty. With no row value to keep, a blank is simply not set.
498
+ // A run's input is never the row's value: the handler is given what the model sent or nothing,
499
+ // and a blank one is refused. The planner refuses it first; this is the second guard, so the row's
500
+ // current price can never go in as the "new" one (contracts C-18.7.6).
501
+ if (modelSent && isBlankEntry(raw) && op === 'row-action') {
502
+ return { raw, fromRow: false, problem: `input ${field.name} is required` };
503
+ }
493
504
  if (modelSent && isBlankEntry(raw)) {
494
505
  return rowHas ? { raw: rowValue, fromRow: true } : { raw: '', fromRow: false };
495
506
  }
@@ -547,13 +558,14 @@ export function buildEventPayload(fields, row, { op, preImage, unchecked }) {
547
558
  // semantics the unnamed fields would be written as null. Saying so beats the required-field
548
559
  // loop's "SYMBOL needs a value", which blames the model for a value it cannot supply — the
549
560
  // row was never read.
550
- if ((op === 'modify' || op === 'delete') && !preImage) {
561
+ if (readsRow(op) && !preImage) {
551
562
  return {
552
563
  status: 'rejected',
553
- problems: [`a ${op} must be based on a row that was read first`],
564
+ problems: [`${aWrite(op)} must be based on a row that was read first`],
554
565
  };
555
566
  }
556
- // Merged for a MODIFY only.
567
+ // Merged for a MODIFY and a row action only. A row action's handler loads the row and saves it
568
+ // back, so it is sent whole: the row as read, with the inputs applied (contracts C-18.7.6).
557
569
  //
558
570
  // The merge exists because a generated modify replaces the record, so a payload carrying only
559
571
  // the named fields writes null over the rest. A delete has no such semantics: the showcase's
@@ -564,7 +576,7 @@ export function buildEventPayload(fields, row, { op, preImage, unchecked }) {
564
576
  //
565
577
  // An insert never inherits either, even if a caller hands it a pre-image: a new record must
566
578
  // contain what the user saw proposed and nothing else.
567
- const merge = !!preImage && op === 'modify';
579
+ const merge = !!preImage && (op === 'modify' || op === 'row-action');
568
580
  // A delete is its key: `changes` is meaningless on one, and the rest of the record is not
569
581
  // something it sends.
570
582
  // A DELETE is built from the ROW'S OWN key, never from the key the model searched with.
@@ -644,7 +656,7 @@ export function buildEventPayload(fields, row, { op, preImage, unchecked }) {
644
656
  }
645
657
  const converted = valueFor(field, raw);
646
658
  const { value } = converted;
647
- const problem = (_a = converted.problem) !== null && _a !== void 0 ? _a : timeOfDayRefusal(field, { raw, value, fromRow, rowValue });
659
+ const problem = (_a = converted.problem) !== null && _a !== void 0 ? _a : timeOfDayRefusal(field, { raw, value, fromRow, rowValue, op });
648
660
  if (problem) {
649
661
  // Attribute it correctly. A value carried from the pre-image is the SERVER's own — most
650
662
  // often a LONG beyond 2^53 that the read stamped as text and a JSON integer cannot carry
@@ -1,6 +1,31 @@
1
1
  import { __awaiter } from "tslib";
2
+ import { fieldKindOf } from './criteria';
2
3
  import { sameValue } from './value-compare';
4
+ import { aWrite, readsRow } from './write-op';
3
5
  import { buildEventPayload, isBlankEntry, PLATFORM_MAINTAINED, sendableFields, } from './write-payload';
6
+ /**
7
+ * The key a modify or a delete names its row by: the model's own, or, with a declared key (C-19),
8
+ * cut to those fields. Every declared field is asked for before anything is read, and anything
9
+ * else the model put in its key is let go, so it can neither steer the read nor read as a change
10
+ * to the row's own key.
11
+ */
12
+ function namedKey(op, named, keyFields) {
13
+ if ((op !== 'modify' && op !== 'delete') || !(keyFields === null || keyFields === void 0 ? void 0 : keyFields.length))
14
+ return { key: named };
15
+ const missing = keyFields.filter((field) => named[field] === undefined || isBlankEntry(named[field]));
16
+ if (missing.length) {
17
+ return {
18
+ problem: `${aWrite(op)} names its row by ${keyFields.join(', ')}; send ${missing.join(', ')} in key`,
19
+ };
20
+ }
21
+ return { key: Object.fromEntries(keyFields.map((field) => [field, named[field]])) };
22
+ }
23
+ /**
24
+ * A key's values as the row holds them, never as the model wrote them: a read can match a value
25
+ * the payload would send differently (an INT '010' finds row 8 and would write row 10), and the row
26
+ * the user approved is the row the read found.
27
+ */
28
+ const asTheRowHolds = (key, row) => Object.fromEntries(Object.keys(key).map((name) => [name, name in row ? row[name] : key[name]]));
4
29
  /**
5
30
  * A batch a person can actually read before approving it.
6
31
  *
@@ -16,7 +41,33 @@ const asRecord = (value) => value && typeof value === 'object' && !Array.isArray
16
41
  /** The key a modify or delete entry names its row by; `changes` carries what it wants set. */
17
42
  const keyOf = (entry) => asRecord(entry.key);
18
43
  /**
19
- * Builds what the review will show, reading the current row for every modify and delete.
44
+ * Why a run's inputs cannot be sent, before anything is read: every declared one present and not
45
+ * blank, and nothing else.
46
+ *
47
+ * @remarks
48
+ * Blank is the payload builder's own test, so a whitespace-only or zero-width value is refused here
49
+ * rather than reaching the builder, where a modify would have kept the row's value in its place.
50
+ * For a row action that is the hazard itself: a handler told to respond with the row's current
51
+ * price, as though the model had sent it (contracts C-18.7.6).
52
+ */
53
+ function inputProblems(given, declared) {
54
+ var _a;
55
+ if (given !== undefined && !asRecord(given)) {
56
+ return ['inputs must be an object of field names to values'];
57
+ }
58
+ const sent = (_a = asRecord(given)) !== null && _a !== void 0 ? _a : {};
59
+ const problems = declared
60
+ .filter((field) => sent[field] === undefined || isBlankEntry(sent[field]))
61
+ .map((field) => `input ${field} is required`);
62
+ const stray = Object.keys(sent).filter((name) => !declared.includes(name));
63
+ if (stray.length) {
64
+ problems.push(`${stray.join(', ')} ${stray.length > 1 ? 'are not inputs' : 'is not an input'} of this ` +
65
+ `handler. ${declared.length ? `Its inputs are: ${declared.join(', ')}` : 'It takes none'}`);
66
+ }
67
+ return problems;
68
+ }
69
+ /**
70
+ * Builds what the review will show, reading the current row for every modify, delete and row action.
20
71
  *
21
72
  * @remarks
22
73
  * Reads run together — they are reads, and a batch of 25 sequential round trips is a visibly
@@ -25,7 +76,17 @@ const keyOf = (entry) => asRecord(entry.key);
25
76
  * @internal
26
77
  */
27
78
  export function planWrite(rows_1, _a) {
28
- return __awaiter(this, arguments, void 0, function* (rows, { op, fields, source, unchecked }) {
79
+ return __awaiter(this, arguments, void 0, function* (rows, { op, fields, source, unchecked, keyFields, inputs }) {
80
+ // Without its key fields there is nothing to check the row's key against: refused, rather than
81
+ // run with the check skipped.
82
+ if (op === 'row-action' && !(keyFields === null || keyFields === void 0 ? void 0 : keyFields.length)) {
83
+ return { status: 'rejected', reason: 'This handler has no key to look its row up by.' };
84
+ }
85
+ // Nor without its inputs. Read as none, every input would be refused as "not an input", or,
86
+ // past that check, filled from the row as it stands. An empty list is a handler that takes none.
87
+ if (op === 'row-action' && !inputs) {
88
+ return { status: 'rejected', reason: 'This handler has no list of the inputs it takes.' };
89
+ }
29
90
  if (!Array.isArray(rows) || !rows.length) {
30
91
  return { status: 'rejected', reason: 'rows must be a non-empty list of rows to write.' };
31
92
  }
@@ -36,7 +97,7 @@ export function planWrite(rows_1, _a) {
36
97
  `${MAX_WRITE_ROWS} at a time.`,
37
98
  };
38
99
  }
39
- const needsRow = op === 'modify' || op === 'delete';
100
+ const needsRow = readsRow(op);
40
101
  // Shape first, so a bad entry costs no round trip and the reads that follow all have a key.
41
102
  const prepared = rows.map((entry, index) => {
42
103
  const given = asRecord(entry);
@@ -45,13 +106,17 @@ export function planWrite(rows_1, _a) {
45
106
  }
46
107
  if (!needsRow)
47
108
  return { index, given };
48
- const key = keyOf(given);
49
- if (!key) {
109
+ const named = keyOf(given);
110
+ if (!named) {
50
111
  return {
51
112
  index,
52
- problems: [`a ${op} names its row with key, e.g. {"key": {"TRADE_ID": "..."}}`],
113
+ problems: [`${aWrite(op)} names its row with key, e.g. {"key": {"TRADE_ID": "..."}}`],
53
114
  };
54
115
  }
116
+ const keyed = namedKey(op, named, keyFields);
117
+ if ('problem' in keyed)
118
+ return { index, key: named, problems: [keyed.problem] };
119
+ const { key } = keyed;
55
120
  // A modify has to SAY what changes, and is refused by name when it does not — the same way a
56
121
  // missing key is, one line up. `changes` used to fall back to `{}`, so a modify with none
57
122
  // (absent, not an object, or empty) merged nothing over the row, wrote it back UNCHANGED, and
@@ -68,8 +133,19 @@ export function planWrite(rows_1, _a) {
68
133
  ],
69
134
  };
70
135
  }
136
+ // A run sends its inputs, every one, and nothing else: refused before any read, so a row
137
+ // refused here has no pre-image, and its review shows only the problem (contracts C-18.R2).
138
+ if (op === 'row-action') {
139
+ const wrong = inputProblems(given.inputs, inputs !== null && inputs !== void 0 ? inputs : []);
140
+ if (wrong.length)
141
+ return { index, key, problems: wrong };
142
+ }
71
143
  if (!source) {
72
- return { index, key, problems: [`a ${op} needs a row to read, and none is configured`] };
144
+ return {
145
+ index,
146
+ key,
147
+ problems: [`${aWrite(op)} needs a row to read, and none is configured`],
148
+ };
73
149
  }
74
150
  return { index, given, key };
75
151
  });
@@ -82,7 +158,7 @@ export function planWrite(rows_1, _a) {
82
158
  return {
83
159
  status: 'ok',
84
160
  rows: prepared.map((entry, at) => {
85
- var _a;
161
+ var _a, _b;
86
162
  if (entry.problems)
87
163
  return { index: entry.index, key: entry.key, problems: entry.problems };
88
164
  if (!needsRow) {
@@ -143,6 +219,22 @@ export function planWrite(rows_1, _a) {
143
219
  };
144
220
  }
145
221
  seen.set(identity, entry.index);
222
+ // A run is built from the row as read with the inputs applied, and from nothing else. The
223
+ // search key is never merged in: it found the row, and the row's own key is what the handler
224
+ // looks it up by, so a search key naming an input cannot override the input (C-18.7.6).
225
+ if (op === 'row-action') {
226
+ const empty = (keyFields !== null && keyFields !== void 0 ? keyFields : []).filter((field) => read.row[field] == null);
227
+ const built = buildEventPayload(fields, (_a = asRecord(entry.given.inputs)) !== null && _a !== void 0 ? _a : {}, {
228
+ op,
229
+ preImage: read.row,
230
+ unchecked,
231
+ });
232
+ const problems = [
233
+ ...empty.map((field) => `the handler looks this row up by ${field}, which is empty`),
234
+ ...(built.status === 'rejected' ? built.problems : []),
235
+ ];
236
+ return Object.assign(Object.assign({ index: entry.index, key: entry.key, preImage: read.row }, (built.status === 'ok' ? { details: built.details } : {})), (problems.length ? { problems } : {}));
237
+ }
146
238
  // `changes` on a delete is meaningless: the row is the row. Ignored rather than refused,
147
239
  // so a model that sends one does not lose the whole batch over it. On a modify it has
148
240
  // already been checked above to be a non-empty object, so no default is needed here —
@@ -153,7 +245,7 @@ export function planWrite(rows_1, _a) {
153
245
  // generated handler it either creates a second record or fails, neither of which is what
154
246
  // a review of "change TR1's quantity" asked for. Said out loud rather than done quietly.
155
247
  const overridden = Object.keys(changes).filter((name) => name in entry.key && !sameValue(changes[name], entry.key[name]));
156
- const built = buildEventPayload(fields, Object.assign(Object.assign({}, changes), entry.key), {
248
+ const built = buildEventPayload(fields, Object.assign(Object.assign({}, changes), asTheRowHolds(entry.key, read.row)), {
157
249
  op,
158
250
  preImage: read.row,
159
251
  unchecked,
@@ -178,7 +270,7 @@ export function planWrite(rows_1, _a) {
178
270
  // "the row already has that value" is not the reason, and a model told so may report to
179
271
  // the user that it cleared the column. Said as it is instead.
180
272
  const blanked = op === 'modify'
181
- ? Object.entries((_a = asRecord(entry.given.changes)) !== null && _a !== void 0 ? _a : {})
273
+ ? Object.entries((_b = asRecord(entry.given.changes)) !== null && _b !== void 0 ? _b : {})
182
274
  .filter(([name, value]) => isBlankEntry(value) && read.row[name] != null)
183
275
  .map(([name]) => name)
184
276
  : [];
@@ -252,11 +344,28 @@ function rowIdentity(row) {
252
344
  * @internal
253
345
  */
254
346
  export function stillCurrent(row_1, _a) {
255
- return __awaiter(this, arguments, void 0, function* (row, { source, fields }) {
347
+ return __awaiter(this, arguments, void 0, function* (row, { source, fields, keyFields, }) {
256
348
  if (!row.key || !row.preImage) {
257
349
  return { status: 'unreadable', reason: 'it was never read in the first place' };
258
350
  }
259
- const read = yield source.read(row.key);
351
+ // The model's search key can name a field somebody changed while the review was open, and a row
352
+ // looked for by its old value reads as gone when it has only changed. A run is looked for by its
353
+ // own key, as the row held it.
354
+ // A DATE key field is the one exception: the row holds it as epoch millis, which a read does not
355
+ // match the way it matched the day the model sent, so that value is the one asked for again.
356
+ const preImage = row.preImage;
357
+ const asked = row.key;
358
+ const isDate = (name) => {
359
+ const field = fields.find((one) => one.name === name);
360
+ return !!field && fieldKindOf(field.type) === 'date';
361
+ };
362
+ const key = keyFields
363
+ ? Object.fromEntries(keyFields.map((name) => [
364
+ name,
365
+ isDate(name) && name in asked ? asked[name] : preImage[name],
366
+ ]))
367
+ : row.key;
368
+ const read = yield source.read(key);
260
369
  if (read.status === 'missing')
261
370
  return { status: 'gone' };
262
371
  if (read.status === 'ambiguous') {
@@ -1,6 +1,6 @@
1
1
  import { __decorate } from "tslib";
2
2
  import { customElement, GenesisElement, observable } from '@genesislcap/web-core';
3
- import { canWrite, customCodeLine, initialSelection, restoredSelection, reviewDecisionOf, reviewOutcomeFrom, stoppedSummary, } from '../review-model';
3
+ import { canWrite, customCodeLine, initialSelection, restoredSelection, reviewDecisionOf, rowActionHeading, rowActionLines, reviewOutcomeFrom, stoppedSummary, } from '../review-model';
4
4
  import { MAX_FEEDBACK_CHARS, } from '../review-payload';
5
5
  import { isBlankText } from '../write-payload';
6
6
  import { WriteReviewStyles as styles } from './write-review.styles';
@@ -135,6 +135,25 @@ let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
135
135
  const model = this.model;
136
136
  return (model === null || model === void 0 ? void 0 : model.customCode) ? customCodeLine(model.op, model.customCode) : '';
137
137
  }
138
+ /** A row action's header, or `''` for any other write: see `rowActionHeading`. */
139
+ get rowActionHeading() {
140
+ const model = this.model;
141
+ return (model === null || model === void 0 ? void 0 : model.rowAction) ? rowActionHeading(model.rowAction, model.rows.length) : '';
142
+ }
143
+ /** The table's accessible name. A run's names its handler and table, as its header does. */
144
+ get tableLabel() {
145
+ var _a;
146
+ const model = this.model;
147
+ return (model === null || model === void 0 ? void 0 : model.rowAction)
148
+ ? `${model.rowAction.entity} rows to run ${model.rowAction.handler} on`
149
+ : `Rows to be written to ${(_a = model === null || model === void 0 ? void 0 : model.resource) !== null && _a !== void 0 ? _a : ''}`;
150
+ }
151
+ /** The lines under a row action's header, from the model, so a re-mount says them too. */
152
+ get rowActionLines() {
153
+ var _a;
154
+ const rowAction = (_a = this.model) === null || _a === void 0 ? void 0 : _a.rowAction;
155
+ return rowAction ? rowActionLines(rowAction) : [];
156
+ }
138
157
  /**
139
158
  * Whether the review is past taking input: it has resolved, or it has sent its answer. Every
140
159
  * action checks this rather than `resolved`, which is still false in between.
@@ -165,6 +165,20 @@ export const WriteReviewStyles = css `
165
165
  color: var(--neutral-foreground-hint);
166
166
  }
167
167
 
168
+ /*
169
+ * A row action's input is what its handler is given, not a change to the row, so it is labelled
170
+ * rather than marked as a diff. The label is text, so it reaches a screen reader as well.
171
+ */
172
+ .input-mark {
173
+ font-style: italic;
174
+ color: var(--neutral-foreground-hint);
175
+ padding-inline-end: calc(var(--design-unit, 4) * 1px);
176
+ }
177
+
178
+ .row-action p {
179
+ margin-block: calc(var(--design-unit, 4) * 1px);
180
+ }
181
+
168
182
  /* The empty placeholder: a style no row text can take, so a literal "(blank)" cannot pass for it. */
169
183
  .blank {
170
184
  font-style: italic;
@@ -4,6 +4,7 @@ import { MAX_FEEDBACK_CHARS } from '../review-payload';
4
4
  import { isBlankText } from '../write-payload';
5
5
  /** The custom-code line's id: the table is described by it, when there is one. */
6
6
  const CUSTOM_CODE_LINE_ID = 'custom-code';
7
+ const ROW_ACTION_LINES_ID = 'row-action-lines';
7
8
  /**
8
9
  * What a checkbox says NOW, read from the event rather than inferred from the state.
9
10
  *
@@ -84,7 +85,7 @@ const cell = html `
84
85
  class="${(x) => [x.changed ? 'changed' : '', x.identifying ? 'identifying' : '', x.long ? 'long' : '']
85
86
  .join(' ')
86
87
  .trim()}"
87
- title="${(x) => (x.identifying ? 'This field identifies the row being removed' : '')}"
88
+ title="${(x) => { var _a; return x.identifying ? `This field identifies ${(_a = x.identifies) !== null && _a !== void 0 ? _a : 'the row being removed'}` : ''; }}"
88
89
  >
89
90
  ${when((x) => x.identifying, html `
90
91
  <span class="sr-only">identifies this row:</span>
@@ -95,6 +96,9 @@ const cell = html `
95
96
  <span class="sr-only">now</span>
96
97
  <span class="now">${mark('+')}${valueOrBlank((x) => x.display)}</span>
97
98
  `)}
99
+ ${when((x) => !!x.input, html `
100
+ <span class="input-mark">input:</span>
101
+ `)}
98
102
  ${when((x) => !(x.changed && x.previousDisplay !== undefined), html `
99
103
  ${(x) => x.display}
100
104
  `)}
@@ -136,17 +140,31 @@ const row = html `
136
140
  `;
137
141
  export const WriteReviewTemplate = html `
138
142
  ${when((x) => !!x.model, html `
139
- <p>
140
- ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.op; }} ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.rows.length; }} row(s) in ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.resource; }}.
141
- Nothing is written until you choose rows and select Write.
142
- </p>
143
+ ${when((x) => !x.rowActionHeading, html `
144
+ <p>
145
+ ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.op; }} ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.rows.length; }} row(s) in
146
+ ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.resource; }}. Nothing is written until you choose rows and select Write.
147
+ </p>
148
+ `)}
149
+ ${when((x) => !!x.rowActionHeading, html `
150
+ <p class="row-action-heading">${(x) => x.rowActionHeading}</p>
151
+ <div class="row-action" id="${ROW_ACTION_LINES_ID}">
152
+ ${repeat((x) => x.rowActionLines, html `
153
+ <p>${(x) => x}</p>
154
+ `)}
155
+ </div>
156
+ `)}
143
157
  ${when((x) => !!x.customCodeLine, html `
144
158
  <p class="custom-code" id="${CUSTOM_CODE_LINE_ID}">${(x) => x.customCodeLine}</p>
145
159
  `)}
146
160
  <div class="scroller">
147
161
  <table
148
- aria-label="Rows to be written to ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.resource; }}"
149
- aria-describedby="${(x) => (x.customCodeLine ? CUSTOM_CODE_LINE_ID : undefined)}"
162
+ aria-label="${(x) => x.tableLabel}"
163
+ aria-describedby="${(x) => x.customCodeLine
164
+ ? CUSTOM_CODE_LINE_ID
165
+ : x.rowActionHeading
166
+ ? ROW_ACTION_LINES_ID
167
+ : undefined}"
150
168
  >
151
169
  <thead>
152
170
  <tr>