@elaraai/e3-types 1.0.108 → 1.0.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/api.d.ts +382 -17
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/api.js +201 -17
- package/dist/src/api.js.map +1 -1
- package/dist/src/environment.d.ts +95 -8
- package/dist/src/environment.d.ts.map +1 -1
- package/dist/src/environment.js +102 -9
- package/dist/src/environment.js.map +1 -1
- package/dist/src/environment.spec.js +31 -19
- package/dist/src/environment.spec.js.map +1 -1
- package/dist/src/function-call.d.ts +91 -13
- package/dist/src/function-call.d.ts.map +1 -1
- package/dist/src/function-call.js +48 -4
- package/dist/src/function-call.js.map +1 -1
- package/dist/src/function-draft.d.ts +80 -0
- package/dist/src/function-draft.d.ts.map +1 -0
- package/dist/src/function-draft.js +71 -0
- package/dist/src/function-draft.js.map +1 -0
- package/dist/src/function-draft.spec.d.ts +6 -0
- package/dist/src/function-draft.spec.d.ts.map +1 -0
- package/dist/src/function-draft.spec.js +58 -0
- package/dist/src/function-draft.spec.js.map +1 -0
- package/dist/src/function.d.ts +45 -8
- package/dist/src/function.d.ts.map +1 -1
- package/dist/src/function.js +40 -7
- package/dist/src/function.js.map +1 -1
- package/dist/src/function.spec.d.ts +6 -0
- package/dist/src/function.spec.d.ts.map +1 -0
- package/dist/src/function.spec.js +61 -0
- package/dist/src/function.spec.js.map +1 -0
- package/dist/src/index.d.ts +7 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -7
- package/dist/src/index.js.map +1 -1
- package/dist/src/record.d.ts +81 -29
- package/dist/src/record.d.ts.map +1 -1
- package/dist/src/record.js +92 -22
- package/dist/src/record.js.map +1 -1
- package/dist/src/release.d.ts +1 -1
- package/dist/src/release.js +1 -1
- package/dist/src/structure.d.ts +18 -0
- package/dist/src/structure.d.ts.map +1 -1
- package/dist/src/structure.js +20 -0
- package/dist/src/structure.js.map +1 -1
- package/dist/src/structure.spec.js +8 -1
- package/dist/src/structure.spec.js.map +1 -1
- package/dist/src/task.d.ts +15 -2
- package/dist/src/task.d.ts.map +1 -1
- package/dist/src/task.js +14 -2
- package/dist/src/task.js.map +1 -1
- package/dist/src/task.spec.js +19 -2
- package/dist/src/task.spec.js.map +1 -1
- package/package.json +2 -2
- package/src/api.ts +215 -17
- package/src/environment.spec.ts +42 -21
- package/src/environment.ts +109 -9
- package/src/function-call.ts +52 -4
- package/src/function-draft.spec.ts +69 -0
- package/src/function-draft.ts +96 -0
- package/src/function.spec.ts +66 -0
- package/src/function.ts +44 -7
- package/src/index.ts +38 -2
- package/src/record.ts +97 -24
- package/src/release.ts +1 -1
- package/src/structure.spec.ts +9 -1
- package/src/structure.ts +21 -0
- package/src/task.spec.ts +20 -2
- package/src/task.ts +14 -2
package/src/environment.spec.ts
CHANGED
|
@@ -4,41 +4,48 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Encode/decode contract for {@link EnvironmentSpecType}
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* alphabetically-sorted case list, so the case order is load-bearing: a repo
|
|
11
|
-
* persists specs by that order, and a later code edit that inserts a case
|
|
12
|
-
* before an existing one silently re-tags every later case and mis-decodes
|
|
13
|
-
* already-stored specs. The frozen-order test pins the order so that mistake
|
|
14
|
-
* is a red test; the round-trip tests pin the new cases.
|
|
7
|
+
* Encode/decode contract for {@link EnvironmentSpecType}: every case round
|
|
8
|
+
* trips through {@link decodeEnvironmentSpec}, which reads a spec at this
|
|
9
|
+
* release's type alone.
|
|
15
10
|
*/
|
|
16
11
|
|
|
17
12
|
import { describe, it } from 'node:test';
|
|
18
13
|
import * as assert from 'node:assert';
|
|
19
|
-
import {
|
|
14
|
+
import { StringType, VariantType, encodeBeast2For, variant, some, none } from '@elaraai/east';
|
|
20
15
|
import {
|
|
21
16
|
EnvironmentSpecType,
|
|
17
|
+
ImageEnvironmentType,
|
|
18
|
+
NodeEnvironmentType,
|
|
19
|
+
PythonEnvironmentType,
|
|
20
|
+
ToolsEnvironmentType,
|
|
21
|
+
WorkspaceNodeEnvironmentType,
|
|
22
|
+
decodeEnvironmentSpec,
|
|
22
23
|
environmentSpecObjectHashes,
|
|
23
24
|
type EnvironmentSpec,
|
|
24
25
|
} from './environment.js';
|
|
25
26
|
|
|
26
27
|
const encodeCurrent = encodeBeast2For(EnvironmentSpecType);
|
|
27
|
-
const decodeCurrent =
|
|
28
|
+
const decodeCurrent = (bytes: Uint8Array): EnvironmentSpec => decodeEnvironmentSpec(bytes, H('9'));
|
|
28
29
|
|
|
29
30
|
const H = (c: string): string => c.repeat(64); // stand-in 64-hex object hash
|
|
30
31
|
|
|
31
|
-
describe('
|
|
32
|
-
it('
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
describe('decodeEnvironmentSpec', () => {
|
|
33
|
+
it('refuses a spec an earlier release exported, which decodes at this type, naming the re-export; a later release\'s, naming the upgrade; and what is no spec', () => {
|
|
34
|
+
// The type before its `workspace_python` case: its values decode at today's, and are refused by their type
|
|
35
|
+
const Earlier = VariantType({
|
|
36
|
+
python: PythonEnvironmentType, node: NodeEnvironmentType, image: ImageEnvironmentType, tools: ToolsEnvironmentType,
|
|
37
|
+
workspace_node: WorkspaceNodeEnvironmentType,
|
|
38
|
+
});
|
|
39
|
+
const tools = variant('tools', { files: [{ path: 'bin/my-runner', hash: H('1') }] });
|
|
40
|
+
assert.throws(() => decodeEnvironmentSpec(encodeBeast2For(Earlier)(tools), H('a')),
|
|
41
|
+
{ message: `the environment ${H('a')} was exported by an older e3 SDK — re-export its package with the current one` });
|
|
42
|
+
// A later release's, with a case this one does not know: re-exporting with this one cannot help
|
|
43
|
+
const Later = VariantType({ ...EnvironmentSpecType.cases, conda: StringType });
|
|
44
|
+
assert.throws(() => decodeEnvironmentSpec(encodeBeast2For(Later)(tools), H('a')),
|
|
45
|
+
{ message: `the environment ${H('a')} was exported by a newer e3 SDK than this one, as its conda says — upgrade e3 to the release that exported its package` });
|
|
46
|
+
for (const bytes of [encodeBeast2For(StringType)('not a spec'), new TextEncoder().encode('not beast2')]) {
|
|
47
|
+
assert.throws(() => decodeEnvironmentSpec(bytes, H('a')), { message: `the object ${H('a')} is not an environment's spec` });
|
|
48
|
+
}
|
|
42
49
|
});
|
|
43
50
|
});
|
|
44
51
|
|
|
@@ -52,6 +59,10 @@ describe('EnvironmentSpecType round-trips every case', () => {
|
|
|
52
59
|
packageJson: H('2'), lock: H('3'), config: none, subject: 'packages/pricing',
|
|
53
60
|
members: [{ path: 'packages/pricing', name: '@acme/pricing', tarball: H('4') }],
|
|
54
61
|
}),
|
|
62
|
+
variant('workspace_python', {
|
|
63
|
+
pyproject: H('5'), lock: H('6'), subjects: ['features'],
|
|
64
|
+
sdists: [{ filename: 'features-0.1.0.tar.gz', hash: H('7') }], requires: ['elaraai-east-py', 'elaraai-east-py-std'],
|
|
65
|
+
}),
|
|
55
66
|
];
|
|
56
67
|
|
|
57
68
|
it('encodes and decodes back to the same value', () => {
|
|
@@ -116,6 +127,16 @@ describe('environmentSpecObjectHashes', () => {
|
|
|
116
127
|
);
|
|
117
128
|
});
|
|
118
129
|
|
|
130
|
+
it('lists a workspace_python\'s root manifest, lock and every sdist', () => {
|
|
131
|
+
assert.deepStrictEqual(
|
|
132
|
+
environmentSpecObjectHashes(variant('workspace_python', {
|
|
133
|
+
pyproject: 'p', lock: 'l', subjects: ['features', 'trainer'],
|
|
134
|
+
sdists: [{ filename: 'features-0.1.0.tar.gz', hash: 's1' }, { filename: 'trainer-0.1.0.tar.gz', hash: 's2' }], requires: [],
|
|
135
|
+
})),
|
|
136
|
+
['p', 'l', 's1', 's2'],
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
119
140
|
it('omits the config hash for an npm workspace_node (none)', () => {
|
|
120
141
|
assert.deepStrictEqual(
|
|
121
142
|
environmentSpecObjectHashes(variant('workspace_node', {
|
package/src/environment.ts
CHANGED
|
@@ -19,7 +19,10 @@
|
|
|
19
19
|
* content-addressed object store and transfer machinery.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
ArrayType, OptionType, StringType, StructType, ValueTypeOf, VariantType, decodeBeast2For, isTypeValueEqual, readBeast2Type, toEastTypeValue,
|
|
24
|
+
type EastTypeValue,
|
|
25
|
+
} from '@elaraai/east';
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* A Python (uv) project environment.
|
|
@@ -157,6 +160,52 @@ export const WorkspaceNodeEnvironmentType = StructType({
|
|
|
157
160
|
export type WorkspaceNodeEnvironmentType = typeof WorkspaceNodeEnvironmentType;
|
|
158
161
|
export type WorkspaceNodeEnvironment = ValueTypeOf<typeof WorkspaceNodeEnvironmentType>;
|
|
159
162
|
|
|
163
|
+
/**
|
|
164
|
+
* A Python **workspace-member** environment (a uv workspace).
|
|
165
|
+
*
|
|
166
|
+
* Where {@link PythonEnvironmentType} captures one self-contained project,
|
|
167
|
+
* whose locked dependencies are all its own, this captures members of a
|
|
168
|
+
* workspace: the workspace root's manifest and lock, which pin everything,
|
|
169
|
+
* and the code of exactly the members the environment is of and the local
|
|
170
|
+
* packages they depend on. Materialized by installing, from the root lock,
|
|
171
|
+
* the third-party dependencies of those members alone — what `uv export
|
|
172
|
+
* --frozen --no-default-groups --package <member>` lists — then the
|
|
173
|
+
* distributions `requires` names, then the members' own sdists. Nothing else
|
|
174
|
+
* the workspace root or another member depends on is installed, and none of
|
|
175
|
+
* a member's development groups.
|
|
176
|
+
*
|
|
177
|
+
* @remarks
|
|
178
|
+
* - `pyproject`: object hash of the workspace **root** `pyproject.toml` blob.
|
|
179
|
+
* - `lock`: object hash of the **root** `uv.lock` blob.
|
|
180
|
+
* - `subjects`: the members the environment is of, by the names the lock
|
|
181
|
+
* gives them, sorted: a task's runner names one or more as its custom
|
|
182
|
+
* platform packages.
|
|
183
|
+
* - `sdists`: the source distributions of the subjects and of every local
|
|
184
|
+
* package they depend on, each with its original filename, sorted by it.
|
|
185
|
+
* - `requires`: the lock's registry distributions installed beside the
|
|
186
|
+
* subjects' own dependencies, sorted: the runtime's (`elaraai-east-py`,
|
|
187
|
+
* `elaraai-east-py-cli`) and those of the stock platform packages the
|
|
188
|
+
* task's runner names (`elaraai-east-py-std`), which a workspace commonly
|
|
189
|
+
* lists on its root alone. Each is named with the extras the workspace
|
|
190
|
+
* declares it with (`elaraai-east-py-datascience[lightning]`), and
|
|
191
|
+
* installed at the version the lock pins, with what it and those extras
|
|
192
|
+
* depend on.
|
|
193
|
+
*/
|
|
194
|
+
export const WorkspacePythonEnvironmentType = StructType({
|
|
195
|
+
/** Object hash of the workspace root pyproject.toml blob */
|
|
196
|
+
pyproject: StringType,
|
|
197
|
+
/** Object hash of the root uv.lock blob */
|
|
198
|
+
lock: StringType,
|
|
199
|
+
/** The members the environment is of, by the lock's names, sorted */
|
|
200
|
+
subjects: ArrayType(StringType),
|
|
201
|
+
/** The closure's sdist blobs (original filename + object hash), sorted by filename */
|
|
202
|
+
sdists: ArrayType(StructType({ filename: StringType, hash: StringType })),
|
|
203
|
+
/** The lock's registry distributions installed beside the subjects' own, sorted */
|
|
204
|
+
requires: ArrayType(StringType),
|
|
205
|
+
});
|
|
206
|
+
export type WorkspacePythonEnvironmentType = typeof WorkspacePythonEnvironmentType;
|
|
207
|
+
export type WorkspacePythonEnvironment = ValueTypeOf<typeof WorkspacePythonEnvironmentType>;
|
|
208
|
+
|
|
160
209
|
/**
|
|
161
210
|
* An execution environment specification.
|
|
162
211
|
*
|
|
@@ -165,14 +214,12 @@ export type WorkspaceNodeEnvironment = ValueTypeOf<typeof WorkspaceNodeEnvironme
|
|
|
165
214
|
* (`none`) runs on the stock runtime image as today.
|
|
166
215
|
*
|
|
167
216
|
* @remarks
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* original three). The frozen-order guard test in `environment.spec.ts`
|
|
175
|
-
* turns an accidental reorder into a red test.
|
|
217
|
+
* A spec is package-borne: an export writes it, at the type of the e3 that
|
|
218
|
+
* exported the package. It is read at this type alone
|
|
219
|
+
* ({@link decodeEnvironmentSpec}): a spec an earlier release wrote, of a type
|
|
220
|
+
* with other cases, is refused, and its package exported again
|
|
221
|
+
* (`docs/conventions/WIRE_MIGRATION.md`). So a case added here changes the
|
|
222
|
+
* bytes, and the hash, of every spec an export writes after it.
|
|
176
223
|
*/
|
|
177
224
|
export const EnvironmentSpecType = VariantType({
|
|
178
225
|
/** Python (uv) project environment */
|
|
@@ -185,10 +232,61 @@ export const EnvironmentSpecType = VariantType({
|
|
|
185
232
|
tools: ToolsEnvironmentType,
|
|
186
233
|
/** Node workspace-member environment (npm/pnpm workspaces) */
|
|
187
234
|
workspace_node: WorkspaceNodeEnvironmentType,
|
|
235
|
+
/** Python workspace-member environment (a uv workspace) */
|
|
236
|
+
workspace_python: WorkspacePythonEnvironmentType,
|
|
188
237
|
});
|
|
189
238
|
export type EnvironmentSpecType = typeof EnvironmentSpecType;
|
|
190
239
|
export type EnvironmentSpec = ValueTypeOf<typeof EnvironmentSpecType>;
|
|
191
240
|
|
|
241
|
+
const ENVIRONMENT_SPEC_TYPE = toEastTypeValue(EnvironmentSpecType);
|
|
242
|
+
const decodeSpec = decodeBeast2For(EnvironmentSpecType);
|
|
243
|
+
|
|
244
|
+
/** The names of this release's cases of a spec. */
|
|
245
|
+
const SPEC_CASES: ReadonlySet<string> = new Set(Object.keys(EnvironmentSpecType.cases));
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Decodes an environment's spec object, as this release writes it.
|
|
249
|
+
*
|
|
250
|
+
* @remarks
|
|
251
|
+
* Whatever reads a spec reads it here, so a spec of another type is refused in
|
|
252
|
+
* one set of words wherever it is read, by its type, before it is read. The
|
|
253
|
+
* spec's type grows by cases, so the refusal names the fix by its cases:
|
|
254
|
+
* - each a case this release names: an earlier SDK wrote it, of a type with
|
|
255
|
+
* fewer cases, which would decode at this one still; its package is
|
|
256
|
+
* exported again, as one whose file an older SDK stored raw is;
|
|
257
|
+
* - a case this release does not name: a later SDK wrote it, which this
|
|
258
|
+
* release cannot read; e3 is upgraded to the release that exported it;
|
|
259
|
+
* - no variant at all, or no beast2: it is no spec.
|
|
260
|
+
*
|
|
261
|
+
* @param data - The object's bytes
|
|
262
|
+
* @param hash - The object's hash, which a refusal names
|
|
263
|
+
* @returns The spec
|
|
264
|
+
* @throws {Error} When the object is not a spec of this release's type, saying
|
|
265
|
+
* which of the three it is
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const spec = decodeEnvironmentSpec(await storage.objects.read(repo, envHash), envHash);
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
export function decodeEnvironmentSpec(data: Uint8Array, hash: string): EnvironmentSpec {
|
|
272
|
+
let type: EastTypeValue | null = null;
|
|
273
|
+
try {
|
|
274
|
+
type = readBeast2Type(data);
|
|
275
|
+
} catch {
|
|
276
|
+
// Not beast2 at all
|
|
277
|
+
}
|
|
278
|
+
if (type !== null && isTypeValueEqual(type, ENVIRONMENT_SPEC_TYPE)) return decodeSpec(data);
|
|
279
|
+
if (type === null || type.type !== 'Variant' || type.value.length === 0) {
|
|
280
|
+
throw new Error(`the object ${hash} is not an environment's spec`);
|
|
281
|
+
}
|
|
282
|
+
if (type.value.every(({ name }) => SPEC_CASES.has(name))) {
|
|
283
|
+
throw new Error(`the environment ${hash} was exported by an older e3 SDK — re-export its package with the current one`);
|
|
284
|
+
}
|
|
285
|
+
const unknown = type.value.map(({ name }) => name).filter((name) => !SPEC_CASES.has(name));
|
|
286
|
+
throw new Error(`the environment ${hash} was exported by a newer e3 SDK than this one, as its ${unknown.join(', ')} says — ` +
|
|
287
|
+
'upgrade e3 to the release that exported its package');
|
|
288
|
+
}
|
|
289
|
+
|
|
192
290
|
/**
|
|
193
291
|
* Lists the object-store hashes an environment spec references.
|
|
194
292
|
*
|
|
@@ -215,5 +313,7 @@ export function environmentSpecObjectHashes(spec: EnvironmentSpec): string[] {
|
|
|
215
313
|
...(spec.value.config.type === 'some' ? [spec.value.config.value] : []),
|
|
216
314
|
...spec.value.members.map((m) => m.tarball),
|
|
217
315
|
];
|
|
316
|
+
case 'workspace_python':
|
|
317
|
+
return [spec.value.pyproject, spec.value.lock, ...spec.value.sdists.map((s) => s.hash)];
|
|
218
318
|
}
|
|
219
319
|
}
|
package/src/function-call.ts
CHANGED
|
@@ -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,
|
|
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,
|
|
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
|
|
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
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
* Function object wire: the function object's round trip, with an input that
|
|
8
|
+
* is an index of a record, and its refusal of the form before it.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it } from 'node:test';
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import { ArrayType, EastTypeType, IntegerType, OptionType, StringType, StructType, encodeBeast2For, equalFor, none, some, toEastTypeValue, variant } from '@elaraai/east';
|
|
14
|
+
import { FunctionObjectType, decodeFunctionObject, type FunctionObject } from './function.js';
|
|
15
|
+
import { RunnerType } from './runner.js';
|
|
16
|
+
import { TreePathType } from './structure.js';
|
|
17
|
+
|
|
18
|
+
describe('FunctionObjectType', () => {
|
|
19
|
+
const jobs = [variant('field', 'records'), variant('field', 'jobs')];
|
|
20
|
+
const presses = [variant('field', 'inputs'), variant('field', 'presses')];
|
|
21
|
+
const base: FunctionObject = {
|
|
22
|
+
bodyIr: 'a'.repeat(64),
|
|
23
|
+
inputs: [],
|
|
24
|
+
drafts: [],
|
|
25
|
+
argTypes: [toEastTypeValue(StringType)],
|
|
26
|
+
outputType: toEastTypeValue(IntegerType),
|
|
27
|
+
runner: variant('east_c', { platforms: [], decode: variant('lazy', null) }),
|
|
28
|
+
environment: none,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
it('round-trips a function that reads nothing, datasets, and an index of a record beside the record', () => {
|
|
32
|
+
const functions: FunctionObject[] = [
|
|
33
|
+
base,
|
|
34
|
+
{ ...base, inputs: [{ path: presses, index: none }], environment: some('b'.repeat(64)) },
|
|
35
|
+
{
|
|
36
|
+
...base,
|
|
37
|
+
inputs: [{ path: jobs, index: some('jobs_by_press') }, { path: jobs, index: none }],
|
|
38
|
+
drafts: ['jobs'],
|
|
39
|
+
},
|
|
40
|
+
];
|
|
41
|
+
const encode = encodeBeast2For(FunctionObjectType);
|
|
42
|
+
const same = equalFor(FunctionObjectType);
|
|
43
|
+
for (const original of functions) {
|
|
44
|
+
assert.ok(same(decodeFunctionObject(encode(original)), original), `a function with ${original.inputs.length} inputs decodes as it was encoded`);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('refuses a function object whose inputs predate the index they may name, saying to re-export the package', () => {
|
|
49
|
+
// A function object as a package exported before an input could be an
|
|
50
|
+
// index of a record: each input its dataset's path alone.
|
|
51
|
+
const older = encodeBeast2For(StructType({
|
|
52
|
+
bodyIr: StringType,
|
|
53
|
+
inputs: ArrayType(TreePathType),
|
|
54
|
+
drafts: ArrayType(StringType),
|
|
55
|
+
argTypes: ArrayType(EastTypeType),
|
|
56
|
+
outputType: EastTypeType,
|
|
57
|
+
runner: RunnerType,
|
|
58
|
+
environment: OptionType(StringType),
|
|
59
|
+
}))({ ...base, inputs: [presses] });
|
|
60
|
+
assert.throws(() => decodeFunctionObject(older), /exported by an older e3 SDK — re-export it with the current one/);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('throws for bytes of no known function shape', () => {
|
|
64
|
+
assert.throws(() => decodeFunctionObject(encodeBeast2For(IntegerType)(7n)), /re-export/);
|
|
65
|
+
});
|
|
66
|
+
});
|