@vercel/board 0.45.1 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{access-3udXjJAg.mjs → access-Crbvd2Dm.mjs} +2 -2
- package/dist/access.d.mts +2 -2
- package/dist/access.mjs +5 -5
- package/dist/{actors-DasGZnfb.mjs → actors-BBphaJ_G.mjs} +3 -3
- package/dist/{auth-aiOVE82-.mjs → auth-C-pFnJBA.mjs} +2 -2
- package/dist/{auth-BG_whr9_.d.mts → auth-CkNXnQno.d.mts} +1 -1
- package/dist/{backend-D6yTo8Un.d.mts → backend-D3p8gjba.d.mts} +126 -4
- package/dist/backend-local.d.mts +1 -1
- package/dist/backend-local.mjs +1 -1
- package/dist/{chat-provider-BvNqOQFL.mjs → chat-provider-1zpY6Qo_.mjs} +2 -2
- package/dist/{chat-provider-V2jnjVLq.d.mts → chat-provider-Bn33acHd.d.mts} +2 -2
- package/dist/cli-commands.d.mts +1 -1
- package/dist/cli-commands.mjs +1 -1
- package/dist/cli-embedded.d.mts +2 -2
- package/dist/cli-embedded.mjs +3 -2
- package/dist/cli.mjs +6 -6
- package/dist/{commands-DewS14_P.d.mts → commands-CSZPQ38I.d.mts} +54 -0
- package/dist/{commands-CqdnjUkm.mjs → commands-D0lyo8O8.mjs} +319 -0
- package/dist/{conformance-BInS4_6b.mjs → conformance-Blzd4CXG.mjs} +972 -6
- package/dist/conformance.d.mts +6 -5
- package/dist/conformance.mjs +1 -1
- package/dist/{engine-XVBt02Bl.mjs → engine-C_SlBihU.mjs} +15 -7
- package/dist/{errors-ZazTZh-s.mjs → errors-DZ1tbwSM.mjs} +151 -3
- package/dist/{events-D17RndlV.mjs → events-mGuhF4i5.mjs} +2 -2
- package/dist/{exports-DCxecX15.mjs → exports-DQauuHrc.mjs} +1226 -42
- package/dist/exports-DUX_Yzo7.d.mts +44 -0
- package/dist/{external-DfhN7lKK.mjs → external-BVr1Sq0y.mjs} +1 -1
- package/dist/{index-Djmr4GzQ.d.mts → index-BHXg4Xdo.d.mts} +2 -2
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +5 -5
- package/dist/index.node.d.mts +7 -6
- package/dist/index.node.mjs +5 -4
- package/dist/inspect/app.js +215 -115
- package/dist/inspect/engine.js +5 -5
- package/dist/{inspect-CHsgnf_5.mjs → inspect-2OIjv2lW.mjs} +242 -20
- package/dist/inspect-F-b4GPXX.mjs +2 -0
- package/dist/{local-DE9vmanM.mjs → local-11FMUJrU.mjs} +1593 -60
- package/dist/{main-CF1Me3jg.mjs → main-BYmbTDgo.mjs} +1167 -24
- package/dist/{memory-DyEL0fvN.mjs → memory-CZBdVNn8.mjs} +2 -2
- package/dist/{middleware-BHHKHA9q.mjs → middleware-BKsTcMAv.mjs} +2 -2
- package/dist/{provider-BC9mPYVh.d.mts → provider-D3bfHRHA.d.mts} +119 -3
- package/dist/{routes-CkUetTO2.mjs → routes-B2HHHD_C.mjs} +246 -6
- package/dist/{routes-zm4TXKCC.d.mts → routes-NEc6w3QC.d.mts} +540 -6
- package/dist/{schemas-BhZaystC.mjs → schemas-7VFdomFB.mjs} +34 -1
- package/dist/{serve-CEMvl0EH.mjs → serve-DS2T2iWI.mjs} +4 -4
- package/dist/{server-Bmz0zqYM.mjs → server-Ch3tuK2k.mjs} +10 -10
- package/dist/{serving-BLVRrJ7P.mjs → serving-9riPNrHk.mjs} +1 -1
- package/dist/{sql-CyVcjw8x.mjs → sql-Bml29DpM.mjs} +174 -9
- package/dist/{sqlite-Con-8duk.mjs → sqlite-4C5u7-ag.mjs} +2 -2
- package/dist/{sqlite-db-BdzxtUOV.mjs → sqlite-db-eVb9v3zc.mjs} +1 -1
- package/dist/sync-chat-sdk.d.mts +2 -2
- package/dist/sync-chat-sdk.mjs +2 -2
- package/dist/sync-github.d.mts +2 -2
- package/dist/sync-github.mjs +2 -2
- package/dist/sync-linear.d.mts +3 -3
- package/dist/sync-linear.mjs +2 -2
- package/dist/sync-slack.d.mts +3 -3
- package/dist/sync-slack.mjs +4 -4
- package/dist/sync.d.mts +2 -2
- package/dist/sync.mjs +2 -2
- package/dist/{origin-BbwhnKrg.mjs → task-batch-BJcLiYe1.mjs} +83 -8
- package/dist/{tokens-BErYXuHZ.mjs → tokens-DPKF_cb5.mjs} +2 -2
- package/docs/01-board/01-mounting.md +1 -1
- package/docs/01-board/08-middleware.md +7 -0
- package/docs/01-board/11-tasks.md +348 -0
- package/docs/03-guides/02-inspector.md +66 -9
- package/docs/03-guides/06-sandboxed-agents.md +5 -3
- package/docs/03-guides/07-build-a-ui.md +210 -0
- package/docs/04-reference/01-cli.md +216 -8
- package/docs/04-reference/02-board.md +277 -2
- package/docs/04-reference/03-http.md +184 -10
- package/docs/04-reference/05-conformance.md +10 -7
- package/package.json +1 -1
- package/dist/exports-Gspe1eEq.d.mts +0 -4
- package/dist/inspect-C8U3A4jQ.mjs +0 -2
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { S as
|
|
2
|
-
import {
|
|
3
|
-
import { a as Link, i as ExternalTarget, n as ExternalEvent, o as Origin } from "./external-
|
|
4
|
-
import "./routes-
|
|
5
|
-
import { a as waitUntil, c as platformProfile, d as userIdOf, h as isKeyIdOf, l as shownName, m as derivedId, n as MIDDLEWARE_ACTOR, o as actorParts, s as importedHandle, t as DroppedError } from "./middleware-
|
|
1
|
+
import { S as fileName, a as duplicateId, b as DIRECT_UPLOAD_BYTES_MAX, c as notFound, f as BoardError, i as batchTooLarge, l as textTooLong, m as duration, o as forbidden$1, r as badRequest, t as HttpError, u as toErrorResponse, w as mediaType, x as IMPORT_BYTES_MAX, y as ATTACHMENT_BYTES_MAX } from "./errors-DZ1tbwSM.mjs";
|
|
2
|
+
import { A as authorView, D as TaskNote, E as TaskHoldKind, M as storable, T as TASK_NOTE_MAX, _ as OPEN_TASK_STATES, a as CAN_MAX, b as SeenActorKind, c as EDITED_HOLD, d as LabelKey, g as MessageText, h as MessageId, i as Author, j as platformAvatar, k as actorKind, l as Emoji, n as Attachment, o as CHANNEL_PATTERN_MAX, p as Labels, s as ChannelPath, t as ActorId, u as Handle, w as TASK_LABEL, y as RemoteWatcherId } from "./schemas-7VFdomFB.mjs";
|
|
3
|
+
import { a as Link, i as ExternalTarget, n as ExternalEvent, o as Origin } from "./external-BVr1Sq0y.mjs";
|
|
4
|
+
import "./routes-B2HHHD_C.mjs";
|
|
5
|
+
import { a as waitUntil, c as platformProfile, d as userIdOf, h as isKeyIdOf, l as shownName, m as derivedId, n as MIDDLEWARE_ACTOR, o as actorParts, s as importedHandle, t as DroppedError } from "./middleware-BKsTcMAv.mjs";
|
|
6
6
|
import { a as SCHEMA_DESCRIPTION_MAX, c as canonical, d as parseSchemaId, f as schemaId, i as BUILT_IN_SCHEMAS, l as checkDefinition, n as blocksIn, o as SCHEMA_NAME, s as SCHEMA_VERSION_MAX, u as isBuiltInName } from "./blocks-CVoBC7ur.mjs";
|
|
7
|
-
import { a as
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { a as replaceMentions, i as mentionTerms, n as isEmail, r as mentionTerm, t as extractMentions } from "./mentions-DfZGp3B-.mjs";
|
|
8
|
+
import { a as runMatch, g as VERSION, h as SYNC, i as canonicalWatchers, l as METHODS, n as SYNC_ACTOR, o as compileWatcher, r as syncTarget, s as hasCriteria, t as batchPlan } from "./task-batch-BJcLiYe1.mjs";
|
|
9
|
+
import { c as compileAccess, i as authorize, o as checkGrantChange, t as FULL_ACCESS, u as parseCan } from "./access-Crbvd2Dm.mjs";
|
|
10
|
+
import { S as wroteAs, _ as removes, a as authenticates, c as checkPrincipal, d as grantIssuers, m as joined, n as GITHUB_ISSUER, o as checkCan, p as issuerName, s as checkGrant, x as workflowPath } from "./auth-C-pFnJBA.mjs";
|
|
10
11
|
import { n as setOwn, t as own } from "./own-OwgPWdmh.mjs";
|
|
11
|
-
import { i as ownedActor, n as actorSeen, o as viaOf } from "./actors-
|
|
12
|
+
import { i as ownedActor, n as actorSeen, o as viaOf } from "./actors-BBphaJ_G.mjs";
|
|
12
13
|
import { C as sessionClearedError, a as SOCKET_TIMINGS, c as drainSettledHooks, d as nullProtoRecord, f as runtimeConsumerFactory, g as A2Error, h as stateTailReaders, i as RESERVED_PARTICIPANT_IDS, l as failedPreviousAttempt, m as serverInternals, n as POLL_TIMINGS, o as STATE_CACHE, p as runtimeConsumers, r as PRESENCE_TIMINGS, s as STREAM_TIMINGS, t as DRAIN_TIMINGS, u as markSchedulerSendFailure, v as NonRetriableError, x as isSessionCleared, y as asStoreUnavailable } from "./internal-CvsrprnU-CEPOUpAv.mjs";
|
|
13
14
|
import { i as streamActivities, n as defaultSleep, t as createStreamActivity } from "./store-polling-BZt2Rf3D-oh3aU_An.mjs";
|
|
14
|
-
import { a as foldActors, i as concernedMessageId, o as foldMessages, s as iso, t as TABLES_CONSUMER } from "./sql-
|
|
15
|
-
import { a as replaceMentions, i as mentionTerms, n as isEmail, r as mentionTerm, t as extractMentions } from "./mentions-DfZGp3B-.mjs";
|
|
15
|
+
import { a as foldActors, i as concernedMessageId, o as foldMessages, s as iso, t as TABLES_CONSUMER } from "./sql-Bml29DpM.mjs";
|
|
16
16
|
import { z } from "zod";
|
|
17
|
-
import { createHash, randomBytes } from "node:crypto";
|
|
17
|
+
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
18
18
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
19
19
|
import { dirname, join, resolve } from "node:path";
|
|
20
20
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
@@ -3832,6 +3832,15 @@ const stateCacheKey = (sessionId, reducerName) => `${sessionId}\u0000${reducerNa
|
|
|
3832
3832
|
const From = z.string().nullable();
|
|
3833
3833
|
/** The thread key: the event ID of the thread's root. Lanes and sync state are keyed by it. */
|
|
3834
3834
|
const ThreadKey = z.string().min(1).max(200).refine(storable, "thread key has a NUL character or a lone surrogate");
|
|
3835
|
+
/** The task an event changes: its root's ID, and the root's thread key, which lanes go by. */
|
|
3836
|
+
const Task = {
|
|
3837
|
+
id: MessageId,
|
|
3838
|
+
thread: ThreadKey
|
|
3839
|
+
};
|
|
3840
|
+
/** Which claim of a task this is: 1 for its first, one more for each after. A write that names an older one changes nothing. */
|
|
3841
|
+
const Epoch = z.number().int().positive();
|
|
3842
|
+
/** When a claim's lease runs out, as an ISO time. */
|
|
3843
|
+
const Until = z.string().datetime();
|
|
3835
3844
|
/** The board's one log. Every board fact is an event in session `board`. */
|
|
3836
3845
|
const boardEvents = {
|
|
3837
3846
|
"message.posted": z.object({
|
|
@@ -3864,6 +3873,12 @@ const boardEvents = {
|
|
|
3864
3873
|
*/
|
|
3865
3874
|
blind: z.literal(true).optional(),
|
|
3866
3875
|
/**
|
|
3876
|
+
* (server) How many events right after it, in its own append, are about it: what made a new
|
|
3877
|
+
* thread a task. A writer that can't read its channel gets those back with its post on a
|
|
3878
|
+
* retry, and nothing written since. A post that came alone has none.
|
|
3879
|
+
*/
|
|
3880
|
+
along: z.number().int().positive().max(3).optional(),
|
|
3881
|
+
/**
|
|
3867
3882
|
* (server) The principal of the sync elsewhere that imported it, whose copy `origin` names:
|
|
3868
3883
|
* that sync's to act on. None for the board's own sync, or a write on the board.
|
|
3869
3884
|
*/
|
|
@@ -3962,6 +3977,92 @@ const boardEvents = {
|
|
|
3962
3977
|
target: ExternalTarget,
|
|
3963
3978
|
error: z.string().min(1).max(500)
|
|
3964
3979
|
}),
|
|
3980
|
+
/**
|
|
3981
|
+
* The thread rooted at `id` is a task, and `by.principal` owns it. It waits while it has `needs`,
|
|
3982
|
+
* until `task.ready` says they've all completed; with none, it's ready.
|
|
3983
|
+
*/
|
|
3984
|
+
"task.created": z.object({
|
|
3985
|
+
...Task,
|
|
3986
|
+
needs: z.array(MessageId).max(20),
|
|
3987
|
+
for: ActorId.nullable(),
|
|
3988
|
+
by: Author
|
|
3989
|
+
}),
|
|
3990
|
+
/** Only actor `for` claims it now, or with null, anyone who may. */
|
|
3991
|
+
"task.assigned": z.object({
|
|
3992
|
+
...Task,
|
|
3993
|
+
for: ActorId.nullable(),
|
|
3994
|
+
by: Author
|
|
3995
|
+
}),
|
|
3996
|
+
/**
|
|
3997
|
+
* It waits for `needs` now, in place of what it waited for. As from `task.created`, it waits
|
|
3998
|
+
* while it has any, until `task.ready` says they've all completed, and with none it's ready. One
|
|
3999
|
+
* that's held stays held.
|
|
4000
|
+
*/
|
|
4001
|
+
"task.needs_changed": z.object({
|
|
4002
|
+
...Task,
|
|
4003
|
+
needs: z.array(MessageId).max(20),
|
|
4004
|
+
by: Author
|
|
4005
|
+
}),
|
|
4006
|
+
/** (server) Every task it needs has completed. */
|
|
4007
|
+
"task.ready": z.object(Task),
|
|
4008
|
+
/** (server) `by` holds it until `until`. `token` is the SHA-256 of the claim's secret, hex: no view shows it. */
|
|
4009
|
+
"task.claimed": z.object({
|
|
4010
|
+
...Task,
|
|
4011
|
+
epoch: Epoch,
|
|
4012
|
+
until: Until,
|
|
4013
|
+
token: z.string().regex(/^[0-9a-f]{64}$/),
|
|
4014
|
+
by: Author
|
|
4015
|
+
}),
|
|
4016
|
+
"task.renewed": z.object({
|
|
4017
|
+
...Task,
|
|
4018
|
+
epoch: Epoch,
|
|
4019
|
+
until: Until,
|
|
4020
|
+
by: Author
|
|
4021
|
+
}),
|
|
4022
|
+
"task.released": z.object({
|
|
4023
|
+
...Task,
|
|
4024
|
+
epoch: Epoch,
|
|
4025
|
+
by: Author
|
|
4026
|
+
}),
|
|
4027
|
+
/** (server) Its lease ran out. */
|
|
4028
|
+
"task.expired": z.object({
|
|
4029
|
+
...Task,
|
|
4030
|
+
epoch: Epoch
|
|
4031
|
+
}),
|
|
4032
|
+
"task.completed": z.object({
|
|
4033
|
+
...Task,
|
|
4034
|
+
epoch: Epoch,
|
|
4035
|
+
result: TaskNote.nullable(),
|
|
4036
|
+
by: Author
|
|
4037
|
+
}),
|
|
4038
|
+
"task.failed": z.object({
|
|
4039
|
+
...Task,
|
|
4040
|
+
epoch: Epoch,
|
|
4041
|
+
reason: TaskNote,
|
|
4042
|
+
by: Author
|
|
4043
|
+
}),
|
|
4044
|
+
/** Held for a person, which ends any claim. Kind `edited` is the board's: `by` edited its root, and isn't its owner. */
|
|
4045
|
+
"task.blocked": z.object({
|
|
4046
|
+
...Task,
|
|
4047
|
+
kind: TaskHoldKind.nullable(),
|
|
4048
|
+
reason: TaskNote,
|
|
4049
|
+
by: Author
|
|
4050
|
+
}),
|
|
4051
|
+
"task.unblocked": z.object({
|
|
4052
|
+
...Task,
|
|
4053
|
+
by: Author
|
|
4054
|
+
}),
|
|
4055
|
+
/** Reason `deleted` when its root was. */
|
|
4056
|
+
"task.canceled": z.object({
|
|
4057
|
+
...Task,
|
|
4058
|
+
reason: TaskNote,
|
|
4059
|
+
by: Author
|
|
4060
|
+
}),
|
|
4061
|
+
/** A failed task is ready again. */
|
|
4062
|
+
"task.reopened": z.object({
|
|
4063
|
+
...Task,
|
|
4064
|
+
by: Author
|
|
4065
|
+
}),
|
|
3965
4066
|
"actor.seen": z.object({
|
|
3966
4067
|
actor: ActorId,
|
|
3967
4068
|
kind: SeenActorKind,
|
|
@@ -4243,6 +4344,22 @@ const schedulesContract = contract({
|
|
|
4243
4344
|
}
|
|
4244
4345
|
});
|
|
4245
4346
|
const SCHEDULES_SESSION = "schedules";
|
|
4347
|
+
/**
|
|
4348
|
+
* A claim's lease timers, outside the board log: one session, `leases`. Each claim and renewal arms
|
|
4349
|
+
* one for when its lease runs out, carrying `lease.due`. Timers can't be revoked, so one for a claim
|
|
4350
|
+
* that's over, or renewed since, fires into a no-op: the board log holds only a real `task.expired`.
|
|
4351
|
+
*/
|
|
4352
|
+
const leasesContract = contract({
|
|
4353
|
+
name: "board-leases",
|
|
4354
|
+
events: {
|
|
4355
|
+
/** (server) A timer: task `id`'s claim at `epoch` was to run out at `at`. */
|
|
4356
|
+
"lease.due": z.object({
|
|
4357
|
+
id: MessageId,
|
|
4358
|
+
epoch: z.number().int().positive(),
|
|
4359
|
+
at: Millis
|
|
4360
|
+
}) }
|
|
4361
|
+
});
|
|
4362
|
+
const LEASES_SESSION = "leases";
|
|
4246
4363
|
const Grant = z.union([z.literal("all"), z.array(z.string().min(1).max(16)).max(8)]);
|
|
4247
4364
|
/** A `can`, as config writes it: board() checks it before it's kept. */
|
|
4248
4365
|
const Can = z.union([Grant, z.record(z.string().min(1).max(CHANNEL_PATTERN_MAX), Grant)]);
|
|
@@ -4400,11 +4517,12 @@ const actorsReducer = reducer({
|
|
|
4400
4517
|
}).fold(foldActors);
|
|
4401
4518
|
//#endregion
|
|
4402
4519
|
//#region src/server/feed.ts
|
|
4403
|
-
/** Bookkeeping events, shown only with `all`: who's who, and
|
|
4520
|
+
/** Bookkeeping events, shown only with `all`: who's who, delivery receipts, and a claim's renewals. */
|
|
4404
4521
|
const HIDDEN = /* @__PURE__ */ new Set([
|
|
4405
4522
|
"actor.seen",
|
|
4406
4523
|
"message.synced",
|
|
4407
|
-
"message.files_synced"
|
|
4524
|
+
"message.files_synced",
|
|
4525
|
+
"task.renewed"
|
|
4408
4526
|
]);
|
|
4409
4527
|
/** Whether the board checked who an actor is, as people see them, at their last write (`actor.seen`'s `verified`). */
|
|
4410
4528
|
const verifiedRecord = (actor) => actor.verified === true;
|
|
@@ -4498,11 +4616,25 @@ const ref = (index) => String(index);
|
|
|
4498
4616
|
const scheduleRef = (index) => `s${index}`;
|
|
4499
4617
|
/** The log index a ref names on this board, or null for a string that names nothing here. */
|
|
4500
4618
|
const indexOf$1 = (value) => /^(0|[1-9]\d{0,15})$/.test(value) ? Number(value) : null;
|
|
4619
|
+
/** A task as every reader sees it: the tasks it waits for as refs, and its claim without the token, hashed or not. */
|
|
4620
|
+
function taskView(task) {
|
|
4621
|
+
const { needs, claim, ...shown } = task;
|
|
4622
|
+
return {
|
|
4623
|
+
...shown,
|
|
4624
|
+
needs: needs.map(ref),
|
|
4625
|
+
claim: claim && {
|
|
4626
|
+
actor: claim.actor,
|
|
4627
|
+
until: claim.until
|
|
4628
|
+
},
|
|
4629
|
+
blocked: task.blocked && { ...task.blocked }
|
|
4630
|
+
};
|
|
4631
|
+
}
|
|
4501
4632
|
/** A message as `reader` sees it. Every view names its reader, so none shows a link's snapshot by default. */
|
|
4502
4633
|
function view$1(message, actors, reader) {
|
|
4503
|
-
const { blind, version, revision, schedule, platformText, mentions: _mentions, ...shown } = message;
|
|
4634
|
+
const { blind, version, revision, schedule, task, platformText, mentions: _mentions, ...shown } = message;
|
|
4504
4635
|
return {
|
|
4505
4636
|
...shown,
|
|
4637
|
+
...task ? { task: taskView(task) } : {},
|
|
4506
4638
|
by: authorView(message.by),
|
|
4507
4639
|
...blind ? { blind } : {},
|
|
4508
4640
|
id: ref(message.id),
|
|
@@ -4718,14 +4850,15 @@ function eventView(event, after, actors, reader) {
|
|
|
4718
4850
|
} } : {}
|
|
4719
4851
|
};
|
|
4720
4852
|
}
|
|
4721
|
-
/** An event's payload as clients see it: the message IDs in it are refs. */
|
|
4853
|
+
/** An event's payload as clients see it: the message IDs in it are refs, and a claim's token is left out. */
|
|
4722
4854
|
function payloadView(payload, links) {
|
|
4723
|
-
const { mentions: _mentions, ...shown } = payload;
|
|
4855
|
+
const { token: _token, mentions: _mentions, along: _along, ...shown } = payload;
|
|
4724
4856
|
for (const key of [
|
|
4725
4857
|
"id",
|
|
4726
4858
|
"re",
|
|
4727
4859
|
"replyTo"
|
|
4728
4860
|
]) if (typeof shown[key] === "number") shown[key] = ref(shown[key]);
|
|
4861
|
+
if (Array.isArray(shown.needs)) shown.needs = shown.needs.map(ref);
|
|
4729
4862
|
if (typeof shown.by === "object" && shown.by !== null) shown.by = authorView(shown.by);
|
|
4730
4863
|
const schedule = shown.schedule;
|
|
4731
4864
|
if (schedule) shown.schedule = {
|
|
@@ -4844,7 +4977,7 @@ async function start(options) {
|
|
|
4844
4977
|
const { index: head } = await options.session.state(channelsReducer);
|
|
4845
4978
|
return options.after === void 0 || options.after >= head ? head : options.after;
|
|
4846
4979
|
}
|
|
4847
|
-
const PAGE = 200;
|
|
4980
|
+
const PAGE$1 = 200;
|
|
4848
4981
|
/**
|
|
4849
4982
|
* One long-poll: matching events already in the log return at once; otherwise
|
|
4850
4983
|
* it waits up to `waitMs` for the first new match. The cursor is the last
|
|
@@ -4857,7 +4990,7 @@ async function pollFeed(options, waitMs, signal) {
|
|
|
4857
4990
|
let cursor = index;
|
|
4858
4991
|
const backlog = await options.session.history({
|
|
4859
4992
|
gte: index + 1,
|
|
4860
|
-
lte: index + PAGE
|
|
4993
|
+
lte: index + PAGE$1
|
|
4861
4994
|
});
|
|
4862
4995
|
await feed.preload(backlog, index);
|
|
4863
4996
|
for (const event of backlog) {
|
|
@@ -4865,7 +4998,7 @@ async function pollFeed(options, waitMs, signal) {
|
|
|
4865
4998
|
cursor = item.index;
|
|
4866
4999
|
if (item.view) events.push(item.view);
|
|
4867
5000
|
}
|
|
4868
|
-
if (events.length > 0 || backlog.length === PAGE || waitMs <= 0) return {
|
|
5001
|
+
if (events.length > 0 || backlog.length === PAGE$1 || waitMs <= 0) return {
|
|
4869
5002
|
events,
|
|
4870
5003
|
cursor
|
|
4871
5004
|
};
|
|
@@ -4949,6 +5082,15 @@ function verifiedAs({ principal, author, actor }) {
|
|
|
4949
5082
|
* any of its agents, and a platform user a sync speaks for only as that same user.
|
|
4950
5083
|
*/
|
|
4951
5084
|
const authored = (message, identity) => message.by.principal === identity.principal.id && (platformProfile(identity.author.profile) === null || message.by.actor === identity.author.actor);
|
|
5085
|
+
/**
|
|
5086
|
+
* Whether `identity` is a task's owner, to change it or what it says: the principal that made it a
|
|
5087
|
+
* task, as itself, or as the profile it made it as. Another of its profiles isn't: they share a
|
|
5088
|
+
* credential, and nothing else keeps one's work from another.
|
|
5089
|
+
*/
|
|
5090
|
+
const ownedBy = (task, identity) => wroteAs(identity, {
|
|
5091
|
+
principal: task.owner,
|
|
5092
|
+
actor: task.by
|
|
5093
|
+
});
|
|
4952
5094
|
const messageNotFound = (id) => () => notFound(`message not found: #${id}`, "message_not_found");
|
|
4953
5095
|
/**
|
|
4954
5096
|
* A message the board looked up and the caller may act on with `permission`,
|
|
@@ -5042,6 +5184,7 @@ async function writePost(ctx, input) {
|
|
|
5042
5184
|
if (input.after !== void 0) throw badRequest("`after` is for replies", "invalid_after");
|
|
5043
5185
|
thread = id;
|
|
5044
5186
|
}
|
|
5187
|
+
if (input.with && re !== null) throw new Error("only a new thread is posted with more: a reply has no task to be");
|
|
5045
5188
|
spend(input.proofs.post, "post", { channel });
|
|
5046
5189
|
if (hasLabels(input.labels)) spend(input.proofs.label, "label", { channel });
|
|
5047
5190
|
if (input.sync?.length) spend(input.proofs.sync, "sync", { channel });
|
|
@@ -5084,14 +5227,17 @@ async function landedFor(ctx, identity, landed) {
|
|
|
5084
5227
|
const { state: actors } = await ctx.session.state(actorsReducer);
|
|
5085
5228
|
if (access.readable(landed.channel)) return view$1(landed, actors, await readerFor(ctx, access, landed));
|
|
5086
5229
|
if (!landed.blind) return null;
|
|
5087
|
-
const
|
|
5230
|
+
const events = await ctx.session.history({
|
|
5088
5231
|
gte: landed.id,
|
|
5089
|
-
lte: landed.id
|
|
5232
|
+
lte: landed.id + 3
|
|
5090
5233
|
});
|
|
5091
|
-
const
|
|
5234
|
+
const along = events[0].payload.along ?? 0;
|
|
5235
|
+
const folded = {
|
|
5092
5236
|
byId: {},
|
|
5093
5237
|
replies: {}
|
|
5094
|
-
}
|
|
5238
|
+
};
|
|
5239
|
+
for (const event of events.slice(0, 1 + along)) foldMessages(folded, event);
|
|
5240
|
+
const posted = folded.byId[landed.id];
|
|
5095
5241
|
return {
|
|
5096
5242
|
...view$1(posted, actors, await readerFor(ctx, access, posted)),
|
|
5097
5243
|
text: blindText(posted.text, identity.actor.id)
|
|
@@ -5156,9 +5302,29 @@ async function commitPost(ctx, input, placed) {
|
|
|
5156
5302
|
];
|
|
5157
5303
|
let rows;
|
|
5158
5304
|
try {
|
|
5159
|
-
|
|
5305
|
+
if (input.with) {
|
|
5306
|
+
const along = input.with;
|
|
5307
|
+
const before = events.findIndex((event) => event.type === "message.posted");
|
|
5308
|
+
rows = await appendFrom(ctx, async () => {
|
|
5309
|
+
const { position, events: more } = await along();
|
|
5310
|
+
const came = more({
|
|
5311
|
+
id: position + 1 + before,
|
|
5312
|
+
thread
|
|
5313
|
+
});
|
|
5314
|
+
return {
|
|
5315
|
+
position,
|
|
5316
|
+
events: [...events.map((event) => event.type === "message.posted" ? {
|
|
5317
|
+
...event,
|
|
5318
|
+
payload: {
|
|
5319
|
+
...event.payload,
|
|
5320
|
+
along: came.length
|
|
5321
|
+
}
|
|
5322
|
+
} : event), ...came]
|
|
5323
|
+
};
|
|
5324
|
+
});
|
|
5325
|
+
} else rows = input.after === void 0 || re === null ? await ctx.session.append(...events) : await appendWhile(ctx, nothingAfter(ctx, re, input.after), events);
|
|
5160
5326
|
} catch (error) {
|
|
5161
|
-
const replayed = error instanceof HttpError && error.code === "newer_message" ? await replayOf(ctx, input, {
|
|
5327
|
+
const replayed = error instanceof HttpError && error.code === "newer_message" || input.with !== void 0 && duplicateEvent(error) ? await replayOf(ctx, input, {
|
|
5162
5328
|
channel,
|
|
5163
5329
|
re,
|
|
5164
5330
|
replyTo
|
|
@@ -5168,22 +5334,130 @@ async function commitPost(ctx, input, placed) {
|
|
|
5168
5334
|
}
|
|
5169
5335
|
const posted = rows.find((row) => row.type === "message.posted");
|
|
5170
5336
|
await untilApplied(ctx, rows);
|
|
5171
|
-
const
|
|
5337
|
+
const folded = {
|
|
5172
5338
|
byId: {},
|
|
5173
5339
|
replies: {}
|
|
5174
|
-
}
|
|
5175
|
-
const
|
|
5176
|
-
|
|
5177
|
-
|
|
5178
|
-
|
|
5179
|
-
|
|
5180
|
-
|
|
5181
|
-
|
|
5182
|
-
|
|
5183
|
-
|
|
5340
|
+
};
|
|
5341
|
+
for (const row of [...rows].sort((a, b) => a.index - b.index)) foldMessages(folded, row);
|
|
5342
|
+
const message = folded.byId[posted.index];
|
|
5343
|
+
return shownTo(view$1(message, knownWith(actors, identity, seen), await readerFor(ctx, identity.principal.access, message)), identity.principal.access, sent);
|
|
5344
|
+
}
|
|
5345
|
+
/** The actors with a write's author as the board now records them: a host's principal stays the host's. */
|
|
5346
|
+
const knownWith = (actors, identity, seen) => seen ? {
|
|
5347
|
+
...actors,
|
|
5348
|
+
byId: {
|
|
5349
|
+
...actors.byId,
|
|
5350
|
+
[identity.actor.id]: {
|
|
5351
|
+
...identity.actor,
|
|
5352
|
+
...seen.payload.host ? { host: true } : {},
|
|
5353
|
+
...seen.payload.verified ? { verified: true } : {}
|
|
5184
5354
|
}
|
|
5185
|
-
}
|
|
5186
|
-
|
|
5355
|
+
}
|
|
5356
|
+
} : actors;
|
|
5357
|
+
/**
|
|
5358
|
+
* Several new threads in `channel`, in one append: each root with what `with` makes of it right
|
|
5359
|
+
* after it, in the order given, all of them or none. Answers the roots as the append left them.
|
|
5360
|
+
* With `again`, the roots are a caller's own to send again: `again.landed` finds the ones the same
|
|
5361
|
+
* request landed already, which are answered as they are with nothing written, and `again.asked`
|
|
5362
|
+
* is the ID the event right after the first root carries, which says what was asked for.
|
|
5363
|
+
*/
|
|
5364
|
+
async function writeRoots(ctx, input) {
|
|
5365
|
+
const { identity, channel, roots } = input;
|
|
5366
|
+
const { access } = identity.principal;
|
|
5367
|
+
spend(input.proofs.post, "post", { channel });
|
|
5368
|
+
if (roots.some((root) => hasLabels(root.labels))) spend(input.proofs.label, "label", { channel });
|
|
5369
|
+
const { state: actors } = await ctx.session.state(actorsReducer);
|
|
5370
|
+
const seen = seenFor(identity, actors);
|
|
5371
|
+
const blind = !access.readable(channel);
|
|
5372
|
+
const read = { landed: null };
|
|
5373
|
+
const rows = await appendFrom(ctx, async () => {
|
|
5374
|
+
const found = input.again ? await input.again.landed() : null;
|
|
5375
|
+
if (found && "not" in found && found.not !== "made") throw duplicateId();
|
|
5376
|
+
read.landed = found && "tasks" in found ? found.tasks : null;
|
|
5377
|
+
if (read.landed) return {
|
|
5378
|
+
position: 0,
|
|
5379
|
+
events: []
|
|
5380
|
+
};
|
|
5381
|
+
const { position, counts, events: more } = await input.with();
|
|
5382
|
+
let next = position + 1 + (seen ? 1 : 0);
|
|
5383
|
+
const came = more(roots.map((root, at) => {
|
|
5384
|
+
const here = {
|
|
5385
|
+
id: next,
|
|
5386
|
+
thread: root.id
|
|
5387
|
+
};
|
|
5388
|
+
next += 1 + counts[at];
|
|
5389
|
+
return here;
|
|
5390
|
+
}));
|
|
5391
|
+
if (input.again && came[0]?.[0]) came[0][0] = {
|
|
5392
|
+
...came[0][0],
|
|
5393
|
+
id: input.again.asked
|
|
5394
|
+
};
|
|
5395
|
+
const events = seen ? [seen] : [];
|
|
5396
|
+
for (const [at, root] of roots.entries()) {
|
|
5397
|
+
if (came[at].length !== counts[at]) throw new Error(`a root's append carries the ${counts[at]} events it said, not ${came[at].length}`);
|
|
5398
|
+
events.push({
|
|
5399
|
+
type: "message.posted",
|
|
5400
|
+
id: root.id,
|
|
5401
|
+
payload: {
|
|
5402
|
+
channel,
|
|
5403
|
+
text: root.text,
|
|
5404
|
+
re: null,
|
|
5405
|
+
replyTo: null,
|
|
5406
|
+
thread: root.id,
|
|
5407
|
+
labels: root.labels ?? {},
|
|
5408
|
+
noSync: root.noSync ?? false,
|
|
5409
|
+
by: identity.author,
|
|
5410
|
+
origin: null,
|
|
5411
|
+
from: null,
|
|
5412
|
+
...root.attachments?.length ? { attachments: root.attachments } : {},
|
|
5413
|
+
...blind ? { blind: true } : {},
|
|
5414
|
+
...came[at].length > 0 ? { along: came[at].length } : {}
|
|
5415
|
+
}
|
|
5416
|
+
}, ...came[at]);
|
|
5417
|
+
}
|
|
5418
|
+
return {
|
|
5419
|
+
position,
|
|
5420
|
+
events
|
|
5421
|
+
};
|
|
5422
|
+
});
|
|
5423
|
+
if (read.landed) return read.landed;
|
|
5424
|
+
await untilApplied(ctx, rows);
|
|
5425
|
+
const folded = {
|
|
5426
|
+
byId: {},
|
|
5427
|
+
replies: {}
|
|
5428
|
+
};
|
|
5429
|
+
const ordered = [...rows].sort((a, b) => a.index - b.index);
|
|
5430
|
+
for (const row of ordered) foldMessages(folded, row);
|
|
5431
|
+
const known = knownWith(actors, identity, seen);
|
|
5432
|
+
const posted = ordered.filter((row) => row.type === "message.posted");
|
|
5433
|
+
return Promise.all(posted.map(async (row, at) => {
|
|
5434
|
+
const message = folded.byId[row.index];
|
|
5435
|
+
return shownTo(view$1(message, known, await readerFor(ctx, access, message)), access, roots[at].text);
|
|
5436
|
+
}));
|
|
5437
|
+
}
|
|
5438
|
+
/**
|
|
5439
|
+
* The batch a caller made under an `id`, found by the IDs its events carry, as a post is by its
|
|
5440
|
+
* own: `rootId` names each root's post in turn, and `asked`, when the caller says what it asks
|
|
5441
|
+
* for, the event right after the first one. What's happened to its tasks since makes it no other
|
|
5442
|
+
* batch, and neither does what the middleware made of it: `asked` is of the request as given.
|
|
5443
|
+
*/
|
|
5444
|
+
async function landedBatch(ctx, identity, rootId, asked) {
|
|
5445
|
+
const first = await ctx.reads.byEventId(await rootId(0));
|
|
5446
|
+
if (!first) return { not: "made" };
|
|
5447
|
+
const tasks = [];
|
|
5448
|
+
for (let at = 0, landed = first; landed; landed = await ctx.reads.byEventId(await rootId(++at))) {
|
|
5449
|
+
const mine = await landedFor(ctx, identity, landed);
|
|
5450
|
+
if (!mine) return { not: "yours" };
|
|
5451
|
+
tasks.push(mine);
|
|
5452
|
+
}
|
|
5453
|
+
if (asked !== void 0) {
|
|
5454
|
+
const [, made] = await ctx.session.history({
|
|
5455
|
+
gte: first.id,
|
|
5456
|
+
lte: first.id + 1
|
|
5457
|
+
});
|
|
5458
|
+
if (made?.id !== asked) return { not: "asked" };
|
|
5459
|
+
}
|
|
5460
|
+
return { tasks };
|
|
5187
5461
|
}
|
|
5188
5462
|
/**
|
|
5189
5463
|
* A post's view for its writer. One that can't read the channel gets back the
|
|
@@ -5218,11 +5492,17 @@ async function unclaimed(ctx, target, thread, access) {
|
|
|
5218
5492
|
* slow or stuck, it still succeeds.
|
|
5219
5493
|
*/
|
|
5220
5494
|
async function untilApplied(ctx, rows) {
|
|
5495
|
+
if (rows.length === 0) return;
|
|
5221
5496
|
const events = [...rows].sort((a, b) => a.index - b.index);
|
|
5222
5497
|
const last = events.at(-1).index;
|
|
5223
5498
|
try {
|
|
5224
5499
|
let caughtUp = true;
|
|
5225
|
-
for (const event of events)
|
|
5500
|
+
for (const event of events) {
|
|
5501
|
+
if (await ctx.reads.tables.applyNext(event)) continue;
|
|
5502
|
+
if (await ctx.reads.tables.position() >= event.index) continue;
|
|
5503
|
+
caughtUp = false;
|
|
5504
|
+
break;
|
|
5505
|
+
}
|
|
5226
5506
|
if (caughtUp) return;
|
|
5227
5507
|
} catch {}
|
|
5228
5508
|
await ctx.reads.ensure(last, AbortSignal.timeout(LANDED_WAIT_MS)).catch((error) => {
|
|
@@ -5238,12 +5518,32 @@ const LANDED_WAIT_MS = 1e4;
|
|
|
5238
5518
|
* anywhere on the board, moves it, and it looks again from there.
|
|
5239
5519
|
*/
|
|
5240
5520
|
async function appendWhile(ctx, holds, events) {
|
|
5521
|
+
return appendFrom(ctx, async () => ({
|
|
5522
|
+
position: await holds(),
|
|
5523
|
+
events
|
|
5524
|
+
}));
|
|
5525
|
+
}
|
|
5526
|
+
/**
|
|
5527
|
+
* As `appendWhile`, for a write whose events depend on what it finds: `look` reads the tables,
|
|
5528
|
+
* throws what refuses the write, and returns what to append with the index its reads reflect. It
|
|
5529
|
+
* looks again, and decides again, each time the board moves on first. Nothing to append appends
|
|
5530
|
+
* nothing.
|
|
5531
|
+
*/
|
|
5532
|
+
async function appendFrom(ctx, look) {
|
|
5241
5533
|
return whileHeadMoves(async () => {
|
|
5242
5534
|
await ctx.reads.ensure();
|
|
5243
|
-
|
|
5535
|
+
const { position, events } = await look();
|
|
5536
|
+
return events.length === 0 ? [] : appendIfLast(ctx.session, position, events);
|
|
5244
5537
|
});
|
|
5245
5538
|
}
|
|
5246
5539
|
/**
|
|
5540
|
+
* What a look throws when its reads don't all reflect one index: the board moved on under it, so
|
|
5541
|
+
* `whileHeadMoves` has it look again.
|
|
5542
|
+
*/
|
|
5543
|
+
const movedOn = () => Object.assign(/* @__PURE__ */ new Error("the board moved on while a write read it"), { code: "LAST_INDEX_MISMATCH" });
|
|
5544
|
+
/** An append that holds an event ID already in the log. */
|
|
5545
|
+
const duplicateEvent = (error) => ["PARTIAL_DUPLICATE_BATCH", "DUPLICATE_EVENT_ID"].includes(error?.code ?? "");
|
|
5546
|
+
/**
|
|
5247
5547
|
* Runs `attempt`, a look and a conditional append, again each time the board moves on in
|
|
5248
5548
|
* between, up to `CONDITIONAL_TRIES` times: how every conditional write here looks again.
|
|
5249
5549
|
*/
|
|
@@ -5294,7 +5594,15 @@ async function commitEdit(ctx, input, message) {
|
|
|
5294
5594
|
from: input.from ?? null
|
|
5295
5595
|
}
|
|
5296
5596
|
}];
|
|
5297
|
-
|
|
5597
|
+
const { revision } = input;
|
|
5598
|
+
await untilApplied(ctx, message.re === null ? await appendFrom(ctx, async () => {
|
|
5599
|
+
const { position, found } = await asOfNow(ctx, message.id);
|
|
5600
|
+
if (revision !== void 0) atRevision(found, message.id, revision);
|
|
5601
|
+
return {
|
|
5602
|
+
position,
|
|
5603
|
+
events: [...events, ...heldByEdit(found, input.identity)]
|
|
5604
|
+
};
|
|
5605
|
+
}) : revision === void 0 ? await ctx.session.append(...events) : await appendWhile(ctx, revisionIs(ctx, message.id, revision), events));
|
|
5298
5606
|
}
|
|
5299
5607
|
/** A message's links, replaced: a platform edit or unfurl changed what it links to. */
|
|
5300
5608
|
async function writeLinks(ctx, input) {
|
|
@@ -5327,16 +5635,72 @@ async function writeDelete(ctx, input) {
|
|
|
5327
5635
|
from: input.from ?? null
|
|
5328
5636
|
}
|
|
5329
5637
|
}];
|
|
5330
|
-
|
|
5638
|
+
const { revision } = input;
|
|
5639
|
+
await untilApplied(ctx, message.re === null ? await appendFrom(ctx, async () => {
|
|
5640
|
+
const { position, found } = await asOfNow(ctx, message.id);
|
|
5641
|
+
if (revision !== void 0) atRevision(found, message.id, revision);
|
|
5642
|
+
return {
|
|
5643
|
+
position,
|
|
5644
|
+
events: found?.deleted ? [] : [...canceledByDelete(found, input.identity), ...deleted]
|
|
5645
|
+
};
|
|
5646
|
+
}) : revision === void 0 ? await ctx.session.append(...deleted) : await appendWhile(ctx, revisionIs(ctx, message.id, revision), deleted));
|
|
5331
5647
|
}
|
|
5332
|
-
/**
|
|
5333
|
-
|
|
5648
|
+
/** A message as the tables have it now, and the index that read reflects. */
|
|
5649
|
+
async function asOfNow(ctx, id) {
|
|
5334
5650
|
const position = await ctx.reads.tables.position();
|
|
5335
|
-
|
|
5651
|
+
return {
|
|
5652
|
+
position,
|
|
5653
|
+
found: (await ctx.reads.tables.messagesAt([id], position)).get(id)
|
|
5654
|
+
};
|
|
5655
|
+
}
|
|
5656
|
+
/** Refuses an edit or a delete at `revision` when the message has been edited or deleted since. */
|
|
5657
|
+
function atRevision(message, id, revision) {
|
|
5336
5658
|
const now = message ? message.revision ?? message.id : null;
|
|
5337
5659
|
if (now !== revision) throw new HttpError(409, "revision_conflict", message?.deleted ? `#${id} was deleted since revision ${revision}` : `#${id} was edited since revision ${revision}: it's at ${now}`);
|
|
5660
|
+
}
|
|
5661
|
+
/** An edit's or a delete's `revision`: no edit or delete since, as of the index the read reflects. */
|
|
5662
|
+
const revisionIs = (ctx, id, revision) => async () => {
|
|
5663
|
+
const { position, found } = await asOfNow(ctx, id);
|
|
5664
|
+
atRevision(found, id, revision);
|
|
5338
5665
|
return position;
|
|
5339
5666
|
};
|
|
5667
|
+
/** Why a task is held once its root was edited by someone who doesn't own it. */
|
|
5668
|
+
const EDITED = "its text was edited by someone who does not own the task";
|
|
5669
|
+
/**
|
|
5670
|
+
* The hold an edit of a task's root brings, by anyone but the task's owner: the thread's author when
|
|
5671
|
+
* someone else made it a task, or a platform's edit, whichever principal's sync imported it. The task says what its owner read, so it waits
|
|
5672
|
+
* for them to read it again, and whoever held it has lost it. One that's held or finished stays as it is.
|
|
5673
|
+
*/
|
|
5674
|
+
function heldByEdit(root, editor) {
|
|
5675
|
+
const task = root?.task;
|
|
5676
|
+
const owner = task !== void 0 && ownedBy(task, editor) && editor.principal.kind !== "external";
|
|
5677
|
+
if (!root || root.deleted || !task || owner) return [];
|
|
5678
|
+
if (task.state !== "waiting" && task.state !== "ready" && task.state !== "claimed") return [];
|
|
5679
|
+
return [{
|
|
5680
|
+
type: "task.blocked",
|
|
5681
|
+
payload: {
|
|
5682
|
+
id: root.id,
|
|
5683
|
+
thread: root.thread,
|
|
5684
|
+
kind: EDITED_HOLD,
|
|
5685
|
+
reason: EDITED,
|
|
5686
|
+
by: editor.author
|
|
5687
|
+
}
|
|
5688
|
+
}];
|
|
5689
|
+
}
|
|
5690
|
+
/** The cancel a delete of a task's root brings, unless the task has completed or been canceled: before the delete, while the root still wears its labels. */
|
|
5691
|
+
function canceledByDelete(root, deleter) {
|
|
5692
|
+
const task = root?.task;
|
|
5693
|
+
if (!root || !task || task.state === "completed" || task.state === "canceled") return [];
|
|
5694
|
+
return [{
|
|
5695
|
+
type: "task.canceled",
|
|
5696
|
+
payload: {
|
|
5697
|
+
id: root.id,
|
|
5698
|
+
thread: root.thread,
|
|
5699
|
+
reason: "deleted",
|
|
5700
|
+
by: deleter.author
|
|
5701
|
+
}
|
|
5702
|
+
}];
|
|
5703
|
+
}
|
|
5340
5704
|
async function writeReaction(ctx, input) {
|
|
5341
5705
|
const message = spent(input.authorized, "react");
|
|
5342
5706
|
if (message.deleted) throw badRequest("message was deleted", "message_deleted");
|
|
@@ -6311,9 +6675,18 @@ function createReads(tables, session, projection) {
|
|
|
6311
6675
|
catchUp();
|
|
6312
6676
|
if (filter.channels?.length === 0) return {
|
|
6313
6677
|
threads: [],
|
|
6314
|
-
position: await tables.position()
|
|
6678
|
+
position: await tables.position(),
|
|
6679
|
+
...page.after === void 0 ? { older: null } : {}
|
|
6315
6680
|
};
|
|
6316
6681
|
return tables.threads(filter, page);
|
|
6682
|
+
},
|
|
6683
|
+
async roots(filter, page) {
|
|
6684
|
+
catchUp();
|
|
6685
|
+
if (filter.channels?.length === 0) return {
|
|
6686
|
+
threads: [],
|
|
6687
|
+
position: await tables.position()
|
|
6688
|
+
};
|
|
6689
|
+
return tables.roots(filter, page);
|
|
6317
6690
|
}
|
|
6318
6691
|
};
|
|
6319
6692
|
}
|
|
@@ -6406,7 +6779,7 @@ function createRegistry(options) {
|
|
|
6406
6779
|
/** Vercel Queues delays at most 6 days: a send further out than this goes in hops. */
|
|
6407
6780
|
const MAX_HOP_MS = 432e6;
|
|
6408
6781
|
/** A timer that fires this early just waits again; any closer counts as on time. */
|
|
6409
|
-
const EARLY_MS = 1e3;
|
|
6782
|
+
const EARLY_MS$1 = 1e3;
|
|
6410
6783
|
/**
|
|
6411
6784
|
* Who a schedule sends as: its creator, shown as its principal names it. One made before handles were
|
|
6412
6785
|
* checked keeps the actor its creator's client named; it sends as what verified that creator instead.
|
|
@@ -6600,7 +6973,7 @@ function createSchedules(options) {
|
|
|
6600
6973
|
if (deployment !== null && state.takeover !== null && state.takeover.deployment !== deployment) return;
|
|
6601
6974
|
if (schedule && ctx.attempt > 1 && schedule.sent === n + 1 && schedule.next !== null) return arm("next", id, n + 1, schedule.next);
|
|
6602
6975
|
if (!schedule || schedule.next !== at || schedule.sent !== n) return;
|
|
6603
|
-
if (Date.now() < at - EARLY_MS) return arm("hop", id, n, at);
|
|
6976
|
+
if (Date.now() < at - EARLY_MS$1) return arm("hop", id, n, at);
|
|
6604
6977
|
if (schedule.re !== null) {
|
|
6605
6978
|
const root = await writes.reads.message(schedule.re, ctx.signal);
|
|
6606
6979
|
if (!root || root.deleted) {
|
|
@@ -6858,10 +7231,971 @@ function createSchedules(options) {
|
|
|
6858
7231
|
}
|
|
6859
7232
|
};
|
|
6860
7233
|
}
|
|
7234
|
+
const LEASE_CAP_S = 3600;
|
|
7235
|
+
/** How many owners one claim takes tasks from on this board, beside its caller. */
|
|
7236
|
+
const FROM_MAX = 32;
|
|
7237
|
+
/**
|
|
7238
|
+
* This board's own limits on a task. board() checks a request's shape and leaves how much a
|
|
7239
|
+
* backend holds to the backend, so these are checked here: what's over one is refused as a
|
|
7240
|
+
* misshapen request is, 400 `invalid_request`, before anything is read or written.
|
|
7241
|
+
*/
|
|
7242
|
+
const taskLimits = {
|
|
7243
|
+
lease(seconds) {
|
|
7244
|
+
if (seconds !== void 0 && !(seconds >= 30 && seconds <= 3600)) throw badRequest(`lease: a lease is 30 to ${LEASE_CAP_S} seconds`);
|
|
7245
|
+
},
|
|
7246
|
+
needs(refs) {
|
|
7247
|
+
if (new Set(refs).size > 20) throw badRequest(`needs: a task waits for at most 20 tasks`);
|
|
7248
|
+
},
|
|
7249
|
+
note(what, note) {
|
|
7250
|
+
if (typeof note === "string" && note.length > 1e3) throw badRequest(`${what}: a task's ${what} is at most ${TASK_NOTE_MAX.toLocaleString("en-US")} characters`);
|
|
7251
|
+
},
|
|
7252
|
+
kind(kind) {
|
|
7253
|
+
if (kind !== void 0 && !TaskHoldKind.safeParse(kind).success) throw badRequest("kind: a kind is up to 64 lowercase letters, numbers, and _.-");
|
|
7254
|
+
},
|
|
7255
|
+
from(owners) {
|
|
7256
|
+
if ((owners?.length ?? 0) > FROM_MAX) throw badRequest(`from: a claim takes tasks from at most ${FROM_MAX} owners`);
|
|
7257
|
+
},
|
|
7258
|
+
/** A batch: how many tasks this board makes in one append, which its sender splits on, and of each what one task holds. */
|
|
7259
|
+
batch(tasks) {
|
|
7260
|
+
if (tasks.length > 100) throw batchTooLarge(tasks.length, 100);
|
|
7261
|
+
for (const [at, task] of tasks.entries()) {
|
|
7262
|
+
if (task.key !== void 0 && task.key.length > 64) throw badRequest(`tasks.${at}.key: a task's key is at most 64 characters`);
|
|
7263
|
+
if (new Set(task.needs?.map((need) => typeof need === "string" ? `id ${need}` : `key ${need.key}`)).size > 20) throw badRequest(`tasks.${at}.needs: a task waits for at most 20 tasks`);
|
|
7264
|
+
}
|
|
7265
|
+
}
|
|
7266
|
+
};
|
|
7267
|
+
/** A timer that fires this early waits again; any closer counts as on time. */
|
|
7268
|
+
const EARLY_MS = 1e3;
|
|
7269
|
+
/** Roots a scan of tasks reads at a time. */
|
|
7270
|
+
const PAGE = 500;
|
|
7271
|
+
const conflict = (code, message) => new HttpError(409, code, message);
|
|
7272
|
+
const notATask = (id) => notFound(`#${id} isn't a task`, "not_a_task");
|
|
7273
|
+
/** 128 random bits, base64url: a claim's secret, which the board answers once and keeps only the hash of. */
|
|
7274
|
+
const newToken = () => randomBytes(16).toString("base64url");
|
|
7275
|
+
const hashOf = (token) => createHash("sha256").update(token).digest("hex");
|
|
7276
|
+
/** Whether `token` is the secret `hash` was made from, compared without saying how much of it matched. */
|
|
7277
|
+
const hashes = (token, hash) => timingSafeEqual(Buffer.from(hashOf(token), "hex"), Buffer.from(hash, "hex"));
|
|
7278
|
+
/** A lease asked for in seconds, as this board gives it, in milliseconds. */
|
|
7279
|
+
const leaseOf = (seconds) => Math.min(seconds ?? 900, LEASE_CAP_S) * 1e3;
|
|
7280
|
+
/** Whether a task's claim has run out its lease: it's claimable at once, whatever its label still says. */
|
|
7281
|
+
const lapsed = (task, at) => task.state === "claimed" && Date.parse(task.claim.until) <= at;
|
|
7282
|
+
/** A task's state as claims and lists take it: a claim whose lease has run out is ready. */
|
|
7283
|
+
const stateNow = (task, at) => lapsed(task, at) ? "ready" : task.state;
|
|
7284
|
+
const finished = (task) => task.state === "completed" || task.state === "failed" || task.state === "canceled";
|
|
7285
|
+
/** Whether `value` is shaped like an ID, `<where it's from>:<who>`, as every principal's and actor's is. What it names is a credential's to say, never a client's. */
|
|
7286
|
+
const isId = (value) => /^[^\s:]+:\S+$/.test(value);
|
|
7287
|
+
/** Waits `ms`, or until `signal` aborts, whichever is first. */
|
|
7288
|
+
const paused = (ms, signal) => new Promise((resolve) => {
|
|
7289
|
+
const done = () => {
|
|
7290
|
+
clearTimeout(timer);
|
|
7291
|
+
signal.removeEventListener("abort", done);
|
|
7292
|
+
resolve();
|
|
7293
|
+
};
|
|
7294
|
+
const timer = setTimeout(done, ms);
|
|
7295
|
+
signal.addEventListener("abort", done, { once: true });
|
|
7296
|
+
});
|
|
7297
|
+
/** The profile part of an actor ID, as the board writes one: lowercase. */
|
|
7298
|
+
const PROFILE = /^[a-z][a-z0-9-]{0,31}$/;
|
|
7299
|
+
/**
|
|
7300
|
+
* The actor a task is assigned to: one of its owner's (`<owner>+<profile>`), or null for anyone who
|
|
7301
|
+
* may claim it. Another principal's actor is refused: a profile is the owner's own word for which of
|
|
7302
|
+
* its agents does what.
|
|
7303
|
+
*/
|
|
7304
|
+
function ownActor(owner, actor) {
|
|
7305
|
+
if (actor === void 0 || actor === null) return null;
|
|
7306
|
+
if (!actor.startsWith(`${owner}+`) || !PROFILE.test(actor.slice(owner.length + 1))) throw badRequest(`for: a task is assigned to one of its owner's profiles, ${owner}+<profile>, got ${actor}`);
|
|
7307
|
+
return actor;
|
|
7308
|
+
}
|
|
7309
|
+
/**
|
|
7310
|
+
* Tasks on a local board: a thread with a state, kept on its root. Every write here is one
|
|
7311
|
+
* conditional append: it reads the task as the tables have it, decides, and lands only while that
|
|
7312
|
+
* read is still the log's last word, so of writes that race, one lands and the rest look again.
|
|
7313
|
+
* A claim's lease is timed outside the board log, which holds only what came of it.
|
|
7314
|
+
*/
|
|
7315
|
+
function createTasks(options) {
|
|
7316
|
+
let bound = null;
|
|
7317
|
+
const writes = () => {
|
|
7318
|
+
if (!bound) throw new Error("tasks used before the board bound them");
|
|
7319
|
+
return bound;
|
|
7320
|
+
};
|
|
7321
|
+
/**
|
|
7322
|
+
* A claim's lease, timed: when it's due, the task's claim expires if it's still that claim and
|
|
7323
|
+
* its lease wasn't renewed. A renewal arms its own timer, so one for an earlier end fires into
|
|
7324
|
+
* nothing.
|
|
7325
|
+
*/
|
|
7326
|
+
async function due(context) {
|
|
7327
|
+
const ctx = writes();
|
|
7328
|
+
const { id, epoch, at } = context.event.payload;
|
|
7329
|
+
const read = { early: null };
|
|
7330
|
+
await untilApplied(ctx, await appendFrom(ctx, async () => {
|
|
7331
|
+
read.early = null;
|
|
7332
|
+
const position = await ctx.reads.tables.position();
|
|
7333
|
+
const root = (await ctx.reads.tables.messagesAt([id], position)).get(id);
|
|
7334
|
+
const task = root?.task;
|
|
7335
|
+
if (!root || !task || task.state !== "claimed" || task.epoch !== epoch) return {
|
|
7336
|
+
position,
|
|
7337
|
+
events: []
|
|
7338
|
+
};
|
|
7339
|
+
const until = Date.parse(task.claim.until);
|
|
7340
|
+
if (until - Date.now() > EARLY_MS) {
|
|
7341
|
+
if (until === at) read.early = task.claim.until;
|
|
7342
|
+
return {
|
|
7343
|
+
position,
|
|
7344
|
+
events: []
|
|
7345
|
+
};
|
|
7346
|
+
}
|
|
7347
|
+
return {
|
|
7348
|
+
position,
|
|
7349
|
+
events: [{
|
|
7350
|
+
type: "task.expired",
|
|
7351
|
+
payload: {
|
|
7352
|
+
id,
|
|
7353
|
+
thread: root.thread,
|
|
7354
|
+
epoch
|
|
7355
|
+
}
|
|
7356
|
+
}]
|
|
7357
|
+
};
|
|
7358
|
+
}));
|
|
7359
|
+
if (read.early !== null) await arm(id, epoch, read.early, `again-${context.event.index}`);
|
|
7360
|
+
}
|
|
7361
|
+
const expiring = consumer({
|
|
7362
|
+
contract: leasesContract,
|
|
7363
|
+
name: "board-leases",
|
|
7364
|
+
types: ["lease.due"],
|
|
7365
|
+
lane: ({ event }) => `task:${event.payload.id}`
|
|
7366
|
+
}).run(due);
|
|
7367
|
+
const server = createServer({
|
|
7368
|
+
contract: leasesContract,
|
|
7369
|
+
...options.store ? { store: options.store } : {},
|
|
7370
|
+
...options.scheduler ? { scheduler: options.scheduler } : {},
|
|
7371
|
+
consumers: [expiring]
|
|
7372
|
+
});
|
|
7373
|
+
const leases = server.session(LEASES_SESSION);
|
|
7374
|
+
/**
|
|
7375
|
+
* Arms the timer for a claim's lease. Without a scheduler there's none: a claim still checks the
|
|
7376
|
+
* clock, so a lease that ran out is claimable, and its label says so once someone claims it. One
|
|
7377
|
+
* that can't be armed is the same, so the claim that landed still answers.
|
|
7378
|
+
*/
|
|
7379
|
+
async function arm(id, epoch, until, again) {
|
|
7380
|
+
if (!options.scheduler) return;
|
|
7381
|
+
const at = Date.parse(until);
|
|
7382
|
+
const name = `lease-${id}-${epoch}-${at}${again ? `-${again}` : ""}`;
|
|
7383
|
+
await leases.schedule(name, { at: new Date(at) }, {
|
|
7384
|
+
type: "lease.due",
|
|
7385
|
+
payload: {
|
|
7386
|
+
id,
|
|
7387
|
+
epoch,
|
|
7388
|
+
at
|
|
7389
|
+
}
|
|
7390
|
+
}).catch((error) => {
|
|
7391
|
+
console.warn(`[board] no timer for #${id}'s lease, which a claim still ends:`, error.message);
|
|
7392
|
+
});
|
|
7393
|
+
}
|
|
7394
|
+
/** A message the caller may read, as the tables have it: a 404 otherwise, as if it weren't there. */
|
|
7395
|
+
async function readable(access, id) {
|
|
7396
|
+
const message = await writes().reads.message(id);
|
|
7397
|
+
if (!message || !access.readable(message.channel)) throw notFound(`message not found: #${id}`, "message_not_found");
|
|
7398
|
+
return message;
|
|
7399
|
+
}
|
|
7400
|
+
/** A task the caller may read, by its root's ID. Who it is and where it is never change, so what a caller may do to it is checked on this read. */
|
|
7401
|
+
async function taskOf(access, id) {
|
|
7402
|
+
const message = await readable(access, id);
|
|
7403
|
+
if (!message.task) throw notATask(id);
|
|
7404
|
+
return message;
|
|
7405
|
+
}
|
|
7406
|
+
/** `message` as `identity` sees it. */
|
|
7407
|
+
async function shown(identity, message) {
|
|
7408
|
+
const ctx = writes();
|
|
7409
|
+
const { state: actors } = await ctx.session.state(actorsReducer);
|
|
7410
|
+
return view$1(message, actors, await ctx.reads.reader(identity.principal.access, [message]));
|
|
7411
|
+
}
|
|
7412
|
+
/**
|
|
7413
|
+
* One write to the message `id`: `decide` gets it as it is at the index the write will land after,
|
|
7414
|
+
* throws what refuses the write, and returns what to append. Answers the message as the write left
|
|
7415
|
+
* it: as it was, when there was nothing to append.
|
|
7416
|
+
*/
|
|
7417
|
+
async function change(identity, id, decide) {
|
|
7418
|
+
const ctx = writes();
|
|
7419
|
+
const seen = seenFor(identity, (await ctx.session.state(actorsReducer)).state);
|
|
7420
|
+
const read = {};
|
|
7421
|
+
const rows = await appendFrom(ctx, async () => {
|
|
7422
|
+
const position = await ctx.reads.tables.position();
|
|
7423
|
+
const found = (await ctx.reads.tables.messagesAt([id], position)).get(id);
|
|
7424
|
+
if (!found) throw notFound(`message not found: #${id}`, "message_not_found");
|
|
7425
|
+
read.before = found;
|
|
7426
|
+
const events = await decide(found, position);
|
|
7427
|
+
return {
|
|
7428
|
+
position,
|
|
7429
|
+
events: events.length > 0 && seen ? [seen, ...events] : events
|
|
7430
|
+
};
|
|
7431
|
+
});
|
|
7432
|
+
await untilApplied(ctx, rows);
|
|
7433
|
+
return after(read.before, rows);
|
|
7434
|
+
}
|
|
7435
|
+
/** `message` once `rows`, a write's own events, are applied to it: what the write answers, whatever lands next. */
|
|
7436
|
+
function after(message, rows) {
|
|
7437
|
+
const state = {
|
|
7438
|
+
byId: { [message.id]: structuredClone(message) },
|
|
7439
|
+
replies: {}
|
|
7440
|
+
};
|
|
7441
|
+
for (const row of [...rows].sort((a, b) => a.index - b.index)) foldMessages(state, row);
|
|
7442
|
+
return state.byId[message.id];
|
|
7443
|
+
}
|
|
7444
|
+
/** `change`, for a write to a task: `decide` gets its root. */
|
|
7445
|
+
const changeTask = (identity, id, decide) => change(identity, id, (found, position) => {
|
|
7446
|
+
if (!found.task) throw notATask(id);
|
|
7447
|
+
return decide(found, position);
|
|
7448
|
+
});
|
|
7449
|
+
/**
|
|
7450
|
+
* Whether the caller changes a task as its owner: `manage` in its channel, or the owner while it
|
|
7451
|
+
* may still write there. A principal ID is coarser than access (a proxy narrows one token with
|
|
7452
|
+
* `x-board-can`), so one narrowed to reading doesn't cancel its principal's tasks. And a
|
|
7453
|
+
* principal's profiles share its credential (a sandbox's agent writes as one), so, as with a
|
|
7454
|
+
* message, a profile changes only the tasks it made, and the principal itself any of them:
|
|
7455
|
+
* otherwise one profile would assign itself what was another's.
|
|
7456
|
+
*/
|
|
7457
|
+
function owns(identity, root) {
|
|
7458
|
+
const { access } = identity.principal;
|
|
7459
|
+
if (access.can("manage", root.channel)) return;
|
|
7460
|
+
if (ownedBy(root.task, identity) && (access.can("post", root.channel) || access.can("label", root.channel))) return;
|
|
7461
|
+
throw new HttpError(403, "task_not_owned", `#${root.id} is someone else's task: its owner as itself, the profile that made it, or manage in ${root.channel}, changes it`);
|
|
7462
|
+
}
|
|
7463
|
+
/** Refuses a write whose claim is over: released, settled, held, taken over, or past its lease. It never says who holds the task now. */
|
|
7464
|
+
function fenced(root, proof) {
|
|
7465
|
+
const { task } = root;
|
|
7466
|
+
if (!(task.state === "claimed" && task.epoch === proof.epoch && Date.parse(task.claim.until) > Date.now() && hashes(proof.token, task.claim.token))) throw conflict("claim_lost", `this claim on #${root.id} is over: claim it again`);
|
|
7467
|
+
}
|
|
7468
|
+
/**
|
|
7469
|
+
* Whether `identity` is who a task is assigned to: its owner's principal, writing as that actor.
|
|
7470
|
+
* The actor ID alone doesn't say whose it is. An ID is a principal's and then `+<profile>`, and
|
|
7471
|
+
* another principal's own ID can end that way, so the two can be one string.
|
|
7472
|
+
*/
|
|
7473
|
+
const assignedTo = (task, identity) => task.for === identity.actor.id && task.owner === identity.principal.id;
|
|
7474
|
+
/** Refuses a claim of a task that can't be claimed now, by `identity`. */
|
|
7475
|
+
function claimable(root, identity) {
|
|
7476
|
+
const { task } = root;
|
|
7477
|
+
if (task.state === "waiting") throw conflict("task_waiting", `#${root.id} waits for ${task.needs.map((need) => `#${need}`).join(", ")}`);
|
|
7478
|
+
if (task.state === "blocked") throw conflict("task_blocked", `#${root.id} is held for a person: ${task.blocked.reason}`);
|
|
7479
|
+
if (finished(task)) throw conflict("task_finished", `#${root.id} is ${task.state}`);
|
|
7480
|
+
if (task.for !== null && !assignedTo(task, identity)) throw conflict("task_assigned", `#${root.id} is assigned to ${task.for}`);
|
|
7481
|
+
if (task.state === "claimed" && !lapsed(task, Date.now())) throw conflict("task_claimed", `#${root.id} is claimed until ${task.claim.until}`);
|
|
7482
|
+
}
|
|
7483
|
+
/**
|
|
7484
|
+
* A claim of a task as it stands, by `identity`: the events, and what the claim answers. A claim
|
|
7485
|
+
* whose lease ran out expires in the same append, so the log says what happened to it.
|
|
7486
|
+
*/
|
|
7487
|
+
function claiming(root, task, identity, lease, token) {
|
|
7488
|
+
const about = {
|
|
7489
|
+
id: root.id,
|
|
7490
|
+
thread: root.thread
|
|
7491
|
+
};
|
|
7492
|
+
const epoch = (task?.epoch ?? 0) + 1;
|
|
7493
|
+
const until = new Date(Date.now() + leaseOf(lease)).toISOString();
|
|
7494
|
+
return {
|
|
7495
|
+
events: [...task?.state === "claimed" ? [{
|
|
7496
|
+
type: "task.expired",
|
|
7497
|
+
payload: {
|
|
7498
|
+
...about,
|
|
7499
|
+
epoch: task.epoch
|
|
7500
|
+
}
|
|
7501
|
+
}] : [], {
|
|
7502
|
+
type: "task.claimed",
|
|
7503
|
+
payload: {
|
|
7504
|
+
...about,
|
|
7505
|
+
epoch,
|
|
7506
|
+
until,
|
|
7507
|
+
token: hashOf(token),
|
|
7508
|
+
by: identity.author
|
|
7509
|
+
}
|
|
7510
|
+
}],
|
|
7511
|
+
claim: {
|
|
7512
|
+
epoch,
|
|
7513
|
+
token,
|
|
7514
|
+
until
|
|
7515
|
+
}
|
|
7516
|
+
};
|
|
7517
|
+
}
|
|
7518
|
+
/**
|
|
7519
|
+
* The tasks `refs` name, as of `position`, and whether they've all completed. One that isn't a
|
|
7520
|
+
* task, or that the caller can't read, is refused alike, so `needs` says nothing of what's out of
|
|
7521
|
+
* sight. Needs never change, and nothing can need a thread before it's a task, so the only cycle
|
|
7522
|
+
* there could be is a task that waits for itself: `self` isn't a task yet, so it's refused as any
|
|
7523
|
+
* thread that isn't one, and named here only so the refusal says why. One that was canceled is
|
|
7524
|
+
* refused too: it can never complete, so a task that waited for it would wait for good, and its
|
|
7525
|
+
* maker hears now. A failed one may yet be reopened, so it's still something to wait for.
|
|
7526
|
+
*/
|
|
7527
|
+
async function needsAt(access, refs, position, self) {
|
|
7528
|
+
const named = namedNeeds(refs, self);
|
|
7529
|
+
return needsAmong(access, named, named.size === 0 ? /* @__PURE__ */ new Map() : await writes().reads.tables.messagesAt([...named.keys()], position));
|
|
7530
|
+
}
|
|
7531
|
+
const invalidNeed = (value, why) => badRequest(`needs: #${value} ${why}`, "invalid_needs");
|
|
7532
|
+
/** The messages `refs` name, each by the ref that named it: one that names none, or `self`, is refused. */
|
|
7533
|
+
function namedNeeds(refs, self) {
|
|
7534
|
+
const named = /* @__PURE__ */ new Map();
|
|
7535
|
+
for (const value of refs ?? []) {
|
|
7536
|
+
const id = indexOf$1(value);
|
|
7537
|
+
if (id === null || id === 0) throw invalidNeed(value, "isn't a task");
|
|
7538
|
+
if (id === self) throw invalidNeed(value, "is the task itself: it can't wait for itself");
|
|
7539
|
+
named.set(id, value);
|
|
7540
|
+
}
|
|
7541
|
+
return named;
|
|
7542
|
+
}
|
|
7543
|
+
/** The tasks `named` names among `found`, the messages read for them, and whether they've all completed. */
|
|
7544
|
+
function needsAmong(access, named, found) {
|
|
7545
|
+
for (const [id, value] of named) {
|
|
7546
|
+
const need = found.get(id);
|
|
7547
|
+
if (!need?.task || !access.readable(need.channel)) throw invalidNeed(value, "isn't a task");
|
|
7548
|
+
if (need.task.state === "canceled") throw invalidNeed(value, "was canceled: it can never complete");
|
|
7549
|
+
}
|
|
7550
|
+
const ids = [...named.keys()];
|
|
7551
|
+
return {
|
|
7552
|
+
ids,
|
|
7553
|
+
completed: ids.every((id) => found.get(id).task.state === "completed")
|
|
7554
|
+
};
|
|
7555
|
+
}
|
|
7556
|
+
/**
|
|
7557
|
+
* Refuses prerequisites that would make task `id` wait for itself: one that waits for it already,
|
|
7558
|
+
* through however many tasks. A new task can't be waited for, so only a change of what a task
|
|
7559
|
+
* needs can close a cycle, and only with a prerequisite it adds: one the task keeps was looked
|
|
7560
|
+
* behind when it was added, and whatever came to wait behind it since was checked as it did. So
|
|
7561
|
+
* it walks what the `added` ones need, and what those need, a level a read, and gives up past
|
|
7562
|
+
* `NEEDS_WALK_MAX` tasks: a change it can't check is refused too. One that only drops some walks
|
|
7563
|
+
* nothing.
|
|
7564
|
+
*
|
|
7565
|
+
* It follows no completed task's needs that `access` reads. A task completes by a claim, which
|
|
7566
|
+
* it takes only once all it needs has completed, and neither it nor they change after: completed
|
|
7567
|
+
* is for good, and a finished task's needs can't be changed. So everything behind a completed
|
|
7568
|
+
* task has completed, and task `id`, which hasn't, isn't there. Finished work behind a
|
|
7569
|
+
* prerequisite, however much, is never too much to check. One the caller can't read is followed
|
|
7570
|
+
* all the same, completed or not: the walk reads tasks out of the caller's sight, as it must to
|
|
7571
|
+
* find a cycle, so where it stops, and whether it gets past `NEEDS_WALK_MAX`, must depend on
|
|
7572
|
+
* nothing the caller can't see. A canceled task is followed: it may have waited for anything.
|
|
7573
|
+
*/
|
|
7574
|
+
async function acyclic(access, id, added, position) {
|
|
7575
|
+
const invalid = (need, why) => badRequest(`needs: #${need} ${why}`, "invalid_needs");
|
|
7576
|
+
/** Each task the walk reached, by the added prerequisite it reached it from. */
|
|
7577
|
+
const from = new Map(added.map((need) => [need, need]));
|
|
7578
|
+
for (let level = [...added]; level.length > 0;) {
|
|
7579
|
+
const found = await writes().reads.tables.messagesAt(level, position);
|
|
7580
|
+
const next = [];
|
|
7581
|
+
for (const at of level) {
|
|
7582
|
+
const message = found.get(at);
|
|
7583
|
+
const task = message?.task;
|
|
7584
|
+
if (!task || task.state === "completed" && access.readable(message.channel)) continue;
|
|
7585
|
+
for (const need of task.needs) {
|
|
7586
|
+
if (need === id) throw invalid(from.get(at), `waits for #${id} already: they would wait for each other`);
|
|
7587
|
+
if (from.has(need)) continue;
|
|
7588
|
+
from.set(need, from.get(at));
|
|
7589
|
+
next.push(need);
|
|
7590
|
+
}
|
|
7591
|
+
}
|
|
7592
|
+
if (from.size > 500) throw invalid(added[0], `has more than 500 tasks behind it and the others: too many to check that none waits for #${id}`);
|
|
7593
|
+
level = next;
|
|
7594
|
+
}
|
|
7595
|
+
}
|
|
7596
|
+
/** Whether every task `task` waits for has completed, as of `position`, leaving out `but`, which is completing now. */
|
|
7597
|
+
async function satisfied(task, position, but) {
|
|
7598
|
+
const ids = task.needs.filter((id) => id !== but);
|
|
7599
|
+
if (ids.length === 0) return true;
|
|
7600
|
+
const found = await writes().reads.tables.messagesAt(ids, position);
|
|
7601
|
+
return ids.every((id) => found.get(id)?.task?.state === "completed");
|
|
7602
|
+
}
|
|
7603
|
+
/** Every root that wears `board.task` as one of `states` and matches `filter`, oldest first, all as of `position`. */
|
|
7604
|
+
async function* rootsAt(filter, states, position) {
|
|
7605
|
+
const labels = [...filter.labels ?? [], {
|
|
7606
|
+
key: TASK_LABEL,
|
|
7607
|
+
values: states
|
|
7608
|
+
}];
|
|
7609
|
+
for (let after = 0;;) {
|
|
7610
|
+
const page = await writes().reads.tables.roots({
|
|
7611
|
+
...filter,
|
|
7612
|
+
labels
|
|
7613
|
+
}, {
|
|
7614
|
+
after,
|
|
7615
|
+
limit: PAGE
|
|
7616
|
+
});
|
|
7617
|
+
if (page.position !== position) throw movedOn();
|
|
7618
|
+
for (const { root } of page.threads) if (root.task) yield root;
|
|
7619
|
+
if (page.threads.length < PAGE) return;
|
|
7620
|
+
after = page.threads.at(-1).root.id;
|
|
7621
|
+
}
|
|
7622
|
+
}
|
|
7623
|
+
/** `task.ready` for each task that waited only for `id`, which is completing: its dependents, in the same append. */
|
|
7624
|
+
async function readied(id, position) {
|
|
7625
|
+
const events = [];
|
|
7626
|
+
for await (const root of rootsAt({}, ["waiting"], position)) if (root.task.needs.includes(id) && await satisfied(root.task, position, id)) events.push({
|
|
7627
|
+
type: "task.ready",
|
|
7628
|
+
payload: {
|
|
7629
|
+
id: root.id,
|
|
7630
|
+
thread: root.thread
|
|
7631
|
+
}
|
|
7632
|
+
});
|
|
7633
|
+
return events;
|
|
7634
|
+
}
|
|
7635
|
+
/**
|
|
7636
|
+
* `filter`, narrowed to the channels where `access` gives `permission` among those the log has,
|
|
7637
|
+
* and the index that list reflects. One that gives it everywhere needs nothing more.
|
|
7638
|
+
*/
|
|
7639
|
+
async function within(filter, access, permission) {
|
|
7640
|
+
if (access.everywhere(permission)) return {
|
|
7641
|
+
filter,
|
|
7642
|
+
through: Number.POSITIVE_INFINITY
|
|
7643
|
+
};
|
|
7644
|
+
const { state, index } = await writes().session.state(channelsReducer);
|
|
7645
|
+
const { channel, ...rest } = filter;
|
|
7646
|
+
const inside = (path) => !channel || path === channel.path || channel.subtree && path.startsWith(`${channel.path}/`);
|
|
7647
|
+
return {
|
|
7648
|
+
filter: {
|
|
7649
|
+
...rest,
|
|
7650
|
+
channels: Object.keys(state).filter((path) => inside(path) && access.can(permission, path))
|
|
7651
|
+
},
|
|
7652
|
+
through: index
|
|
7653
|
+
};
|
|
7654
|
+
}
|
|
7655
|
+
/** The IDs a handle or an ID names, itself included: an owner or an assignee the board has yet to see write is still found by its ID. */
|
|
7656
|
+
async function named(value) {
|
|
7657
|
+
const who = whoIs(value, (await writes().session.state(actorsReducer)).state);
|
|
7658
|
+
return /* @__PURE__ */ new Set([
|
|
7659
|
+
value,
|
|
7660
|
+
...who.actors,
|
|
7661
|
+
...who.principals
|
|
7662
|
+
]);
|
|
7663
|
+
}
|
|
7664
|
+
/**
|
|
7665
|
+
* The owner a claim's `from` names: the principal with that ID. A task's text is instructions for
|
|
7666
|
+
* whoever claims it, so whose it takes is never a handle's to say: a handle is how someone shows,
|
|
7667
|
+
* and several can show the same, one a client picked and one a credential gave alike (two projects
|
|
7668
|
+
* on different teams share a name). An ID is one principal's alone, whether or not the board has
|
|
7669
|
+
* seen it write as itself: one that only ever wrote through a profile owns its tasks all the same.
|
|
7670
|
+
*/
|
|
7671
|
+
function ownerFrom(value) {
|
|
7672
|
+
if (isId(value)) return value;
|
|
7673
|
+
throw badRequest(`from: ${value} is a handle, which several can share: name the owner by ID`, "invalid_from");
|
|
7674
|
+
}
|
|
7675
|
+
/** A claim of the oldest ready task there that the caller takes, or null when there's none now. */
|
|
7676
|
+
async function claimNext(identity, input, signal) {
|
|
7677
|
+
const ctx = writes();
|
|
7678
|
+
const { principal, actor } = identity;
|
|
7679
|
+
const subtree = input.channel.endsWith("/");
|
|
7680
|
+
const channel = {
|
|
7681
|
+
path: subtree ? input.channel.slice(0, -1) : input.channel,
|
|
7682
|
+
subtree
|
|
7683
|
+
};
|
|
7684
|
+
if (!subtree) authorize(principal.access, "label", channel.path);
|
|
7685
|
+
else if (!principal.access.anywhere("label")) throw forbidden$1("this token can't label anywhere, so it can't claim a task");
|
|
7686
|
+
const { filter } = await within({
|
|
7687
|
+
channel,
|
|
7688
|
+
labels: Object.entries(input.label ?? {}).map(([key, value]) => ({
|
|
7689
|
+
key,
|
|
7690
|
+
values: [value]
|
|
7691
|
+
}))
|
|
7692
|
+
}, principal.access, "label");
|
|
7693
|
+
if (filter.channels?.length === 0) return null;
|
|
7694
|
+
const owners = /* @__PURE__ */ new Set([principal.id]);
|
|
7695
|
+
for (const value of input.from ?? []) owners.add(ownerFrom(value));
|
|
7696
|
+
const seen = seenFor(identity, (await ctx.session.state(actorsReducer)).state);
|
|
7697
|
+
const token = newToken();
|
|
7698
|
+
const read = { taken: null };
|
|
7699
|
+
const rows = await appendFrom(ctx, async () => {
|
|
7700
|
+
read.taken = null;
|
|
7701
|
+
const position = await ctx.reads.tables.position();
|
|
7702
|
+
const now = Date.now();
|
|
7703
|
+
let pool = null;
|
|
7704
|
+
let mine = null;
|
|
7705
|
+
for await (const root of rootsAt(filter, ["ready", "claimed"], position)) {
|
|
7706
|
+
const { task } = root;
|
|
7707
|
+
if (stateNow(task, now) !== "ready") continue;
|
|
7708
|
+
if (assignedTo(task, identity)) {
|
|
7709
|
+
mine = root;
|
|
7710
|
+
break;
|
|
7711
|
+
}
|
|
7712
|
+
if (task.for === null && pool === null && owners.has(task.owner)) pool = root;
|
|
7713
|
+
}
|
|
7714
|
+
const root = mine ?? pool;
|
|
7715
|
+
if (!root || signal.aborted) return {
|
|
7716
|
+
position,
|
|
7717
|
+
events: []
|
|
7718
|
+
};
|
|
7719
|
+
const { events, claim } = claiming(root, root.task, identity, input.lease, token);
|
|
7720
|
+
read.taken = {
|
|
7721
|
+
root,
|
|
7722
|
+
claim
|
|
7723
|
+
};
|
|
7724
|
+
return {
|
|
7725
|
+
position,
|
|
7726
|
+
events: seen ? [seen, ...events] : events
|
|
7727
|
+
};
|
|
7728
|
+
});
|
|
7729
|
+
if (read.taken === null) return null;
|
|
7730
|
+
const { root, claim } = read.taken;
|
|
7731
|
+
await untilApplied(ctx, rows);
|
|
7732
|
+
await arm(root.id, claim.epoch, claim.until);
|
|
7733
|
+
return {
|
|
7734
|
+
root: after(root, rows),
|
|
7735
|
+
claim
|
|
7736
|
+
};
|
|
7737
|
+
}
|
|
7738
|
+
return {
|
|
7739
|
+
server,
|
|
7740
|
+
bind: (binding) => {
|
|
7741
|
+
bound = binding.writes;
|
|
7742
|
+
},
|
|
7743
|
+
/**
|
|
7744
|
+
* What makes a new thread a task, to append with its root: `task.created`, `task.ready` when
|
|
7745
|
+
* the tasks it needs have all completed, and its claim when it asks for one. `claimed(id)` is
|
|
7746
|
+
* what that claim answers once root `id` landed: null for a task made without one, or one this
|
|
7747
|
+
* request didn't make (a retry of a post that landed gets the post back, and no claim).
|
|
7748
|
+
*/
|
|
7749
|
+
starting(identity, input) {
|
|
7750
|
+
const { principal, actor, author } = identity;
|
|
7751
|
+
const assigned = ownActor(principal.id, input.for);
|
|
7752
|
+
if (input.claim) authorize(principal.access, "label", input.channel);
|
|
7753
|
+
const token = newToken();
|
|
7754
|
+
const along = async () => {
|
|
7755
|
+
const position = await writes().reads.tables.position();
|
|
7756
|
+
const needs = await needsAt(principal.access, input.needs, position, null);
|
|
7757
|
+
const waits = needs.ids.length > 0 && !needs.completed;
|
|
7758
|
+
if (input.claim && waits) throw conflict("task_waiting", `the task would wait for ${needs.ids.map((id) => `#${id}`).join(", ")}: nothing was made`);
|
|
7759
|
+
if (input.claim && assigned !== null && assigned !== actor.id) throw conflict("task_assigned", `the task would be assigned to ${assigned}: nothing was made`);
|
|
7760
|
+
return {
|
|
7761
|
+
position,
|
|
7762
|
+
events: (root) => {
|
|
7763
|
+
const about = {
|
|
7764
|
+
id: root.id,
|
|
7765
|
+
thread: root.thread
|
|
7766
|
+
};
|
|
7767
|
+
const events = [{
|
|
7768
|
+
type: "task.created",
|
|
7769
|
+
payload: {
|
|
7770
|
+
...about,
|
|
7771
|
+
needs: needs.ids,
|
|
7772
|
+
for: assigned,
|
|
7773
|
+
by: author
|
|
7774
|
+
}
|
|
7775
|
+
}];
|
|
7776
|
+
if (needs.ids.length > 0 && !waits) events.push({
|
|
7777
|
+
type: "task.ready",
|
|
7778
|
+
payload: about
|
|
7779
|
+
});
|
|
7780
|
+
if (input.claim) events.push(...claiming(root, null, identity, input.lease, token).events);
|
|
7781
|
+
return events;
|
|
7782
|
+
}
|
|
7783
|
+
};
|
|
7784
|
+
};
|
|
7785
|
+
const claimed = async (id) => {
|
|
7786
|
+
if (!input.claim) return null;
|
|
7787
|
+
const held = (await writes().reads.message(id))?.task;
|
|
7788
|
+
if (!held?.claim || !hashes(token, held.claim.token)) return null;
|
|
7789
|
+
await arm(id, held.epoch, held.claim.until);
|
|
7790
|
+
return {
|
|
7791
|
+
epoch: held.epoch,
|
|
7792
|
+
token,
|
|
7793
|
+
until: held.claim.until
|
|
7794
|
+
};
|
|
7795
|
+
};
|
|
7796
|
+
return {
|
|
7797
|
+
along,
|
|
7798
|
+
claimed
|
|
7799
|
+
};
|
|
7800
|
+
},
|
|
7801
|
+
/**
|
|
7802
|
+
* What makes several new threads tasks, to append with their roots, all in one write: for each,
|
|
7803
|
+
* `task.created`, and `task.ready` when it waits only for tasks that were there and have all
|
|
7804
|
+
* completed. A task waits for the batch's others by their `key`, wherever they come in it, and
|
|
7805
|
+
* those are new, so it waits. Refused before anything is read: an assignee that isn't its
|
|
7806
|
+
* owner's, a `key` two tasks have or none has, and tasks that wait for each other.
|
|
7807
|
+
*/
|
|
7808
|
+
startingMany(identity, tasks) {
|
|
7809
|
+
const { principal, author } = identity;
|
|
7810
|
+
const assigned = tasks.map((task) => ownActor(principal.id, task.for));
|
|
7811
|
+
const plan = batchPlan(tasks);
|
|
7812
|
+
if ("problem" in plan) throw badRequest(`needs: ${plan.problem}`, "invalid_needs");
|
|
7813
|
+
const { inside } = plan;
|
|
7814
|
+
const named = tasks.map((task) => namedNeeds((task.needs ?? []).filter((need) => typeof need === "string"), null));
|
|
7815
|
+
const outside = [...new Set(named.flatMap((needs) => [...needs.keys()]))];
|
|
7816
|
+
return async () => {
|
|
7817
|
+
const position = await writes().reads.tables.position();
|
|
7818
|
+
const found = outside.length === 0 ? /* @__PURE__ */ new Map() : await writes().reads.tables.messagesAt(outside, position);
|
|
7819
|
+
const needs = named.map((refs) => needsAmong(principal.access, refs, found));
|
|
7820
|
+
const readied = needs.map(({ ids, completed }, at) => ids.length > 0 && completed && inside[at].length === 0);
|
|
7821
|
+
return {
|
|
7822
|
+
position,
|
|
7823
|
+
counts: readied.map((ready) => ready ? 2 : 1),
|
|
7824
|
+
events: (roots) => roots.map((root, at) => {
|
|
7825
|
+
const about = {
|
|
7826
|
+
id: root.id,
|
|
7827
|
+
thread: root.thread
|
|
7828
|
+
};
|
|
7829
|
+
const waitsFor = [...needs[at].ids, ...inside[at].map((other) => roots[other].id)];
|
|
7830
|
+
return [{
|
|
7831
|
+
type: "task.created",
|
|
7832
|
+
payload: {
|
|
7833
|
+
...about,
|
|
7834
|
+
needs: waitsFor,
|
|
7835
|
+
for: assigned[at],
|
|
7836
|
+
by: author
|
|
7837
|
+
}
|
|
7838
|
+
}, ...readied[at] ? [{
|
|
7839
|
+
type: "task.ready",
|
|
7840
|
+
payload: about
|
|
7841
|
+
}] : []];
|
|
7842
|
+
})
|
|
7843
|
+
};
|
|
7844
|
+
};
|
|
7845
|
+
},
|
|
7846
|
+
/**
|
|
7847
|
+
* Makes the thread rooted at `id` a task, as it is: `label` in its channel, and the caller's
|
|
7848
|
+
* principal owns it. A reply names no task, and neither does a root that's gone.
|
|
7849
|
+
*/
|
|
7850
|
+
async adopt(identity, id, input) {
|
|
7851
|
+
const { principal, actor, author } = identity;
|
|
7852
|
+
await authorizeMessage(writes(), principal.access, "label", id);
|
|
7853
|
+
const assigned = ownActor(principal.id, input.for);
|
|
7854
|
+
const token = newToken();
|
|
7855
|
+
const made = { claim: null };
|
|
7856
|
+
const root = await change(identity, id, async (found, position) => {
|
|
7857
|
+
made.claim = null;
|
|
7858
|
+
if (found.re !== null) throw badRequest(`#${id} is a reply: a task is a thread, so name its root, #${found.re}`);
|
|
7859
|
+
if (found.deleted) throw badRequest("message was deleted", "message_deleted");
|
|
7860
|
+
if (found.task) throw conflict("already_a_task", `#${id} is a task already`);
|
|
7861
|
+
const needs = await needsAt(principal.access, input.needs, position, id);
|
|
7862
|
+
const waits = needs.ids.length > 0 && !needs.completed;
|
|
7863
|
+
if (input.claim && waits) throw conflict("task_waiting", `#${id} would wait for ${needs.ids.map((need) => `#${need}`).join(", ")}: it wasn't made a task`);
|
|
7864
|
+
if (input.claim && assigned !== null && assigned !== actor.id) throw conflict("task_assigned", `#${id} would be assigned to ${assigned}: it wasn't made a task`);
|
|
7865
|
+
const about = {
|
|
7866
|
+
id,
|
|
7867
|
+
thread: found.thread
|
|
7868
|
+
};
|
|
7869
|
+
const events = [{
|
|
7870
|
+
type: "task.created",
|
|
7871
|
+
payload: {
|
|
7872
|
+
...about,
|
|
7873
|
+
needs: needs.ids,
|
|
7874
|
+
for: assigned,
|
|
7875
|
+
by: author
|
|
7876
|
+
}
|
|
7877
|
+
}];
|
|
7878
|
+
if (needs.ids.length > 0 && !waits) events.push({
|
|
7879
|
+
type: "task.ready",
|
|
7880
|
+
payload: about
|
|
7881
|
+
});
|
|
7882
|
+
if (input.claim) {
|
|
7883
|
+
const claimed = claiming(found, null, identity, input.lease, token);
|
|
7884
|
+
events.push(...claimed.events);
|
|
7885
|
+
made.claim = claimed.claim;
|
|
7886
|
+
}
|
|
7887
|
+
return events;
|
|
7888
|
+
});
|
|
7889
|
+
const { claim } = made;
|
|
7890
|
+
if (claim) await arm(id, claim.epoch, claim.until);
|
|
7891
|
+
return {
|
|
7892
|
+
task: await shown(identity, root),
|
|
7893
|
+
...claim ? { claim } : {}
|
|
7894
|
+
};
|
|
7895
|
+
},
|
|
7896
|
+
/** Tasks the caller reads, oldest first, a page at a time. */
|
|
7897
|
+
async list(identity, query) {
|
|
7898
|
+
const ctx = writes();
|
|
7899
|
+
const { principal, actor } = identity;
|
|
7900
|
+
const { state: actors } = await ctx.session.state(actorsReducer);
|
|
7901
|
+
const wanted = query.state ?? "open";
|
|
7902
|
+
const states = wanted === "all" ? null : wanted === "open" ? OPEN_TASK_STATES : wanted === "ready" ? ["ready", "claimed"] : [wanted];
|
|
7903
|
+
const base = toMessageFilter({
|
|
7904
|
+
...query.channel !== void 0 ? { channel: query.channel } : {},
|
|
7905
|
+
...query.by !== void 0 ? { by: query.by } : {},
|
|
7906
|
+
...query.label ? { label: query.label } : {}
|
|
7907
|
+
}, {
|
|
7908
|
+
me: {
|
|
7909
|
+
id: actor.id,
|
|
7910
|
+
handle: actor.handle
|
|
7911
|
+
},
|
|
7912
|
+
actors,
|
|
7913
|
+
access: principal.access
|
|
7914
|
+
});
|
|
7915
|
+
const { filter, through } = await within({
|
|
7916
|
+
...base,
|
|
7917
|
+
labels: [...base.labels ?? [], {
|
|
7918
|
+
key: TASK_LABEL,
|
|
7919
|
+
values: states
|
|
7920
|
+
}]
|
|
7921
|
+
}, principal.access, "read");
|
|
7922
|
+
const owners = query.owner === void 0 ? null : await named(query.owner);
|
|
7923
|
+
const assignees = query.for === void 0 ? null : await named(query.for);
|
|
7924
|
+
const needed = query.needs === void 0 ? null : indexOf$1(query.needs) ?? -1;
|
|
7925
|
+
const now = Date.now();
|
|
7926
|
+
const matches = ({ task }) => (wanted === "all" || wanted === "open" || stateNow(task, now) === wanted) && (owners === null || owners.has(task.owner)) && (assignees === null || task.for !== null && assignees.has(task.for)) && (needed === null || task.needs.includes(needed));
|
|
7927
|
+
const limit = query.limit ?? 50;
|
|
7928
|
+
const found = [];
|
|
7929
|
+
let last = 0;
|
|
7930
|
+
if (query.after !== void 0) {
|
|
7931
|
+
const at = indexOf$1(query.after);
|
|
7932
|
+
if (at === null) throw badRequest(`${query.after} isn't a cursor or a message ID on this board`, "invalid_cursor");
|
|
7933
|
+
last = at;
|
|
7934
|
+
}
|
|
7935
|
+
let position = await ctx.reads.position();
|
|
7936
|
+
scan: for (let more = filter.channels?.length !== 0; more;) {
|
|
7937
|
+
const page = await ctx.reads.roots(filter, {
|
|
7938
|
+
after: last,
|
|
7939
|
+
limit: PAGE
|
|
7940
|
+
});
|
|
7941
|
+
position = page.position;
|
|
7942
|
+
more = page.threads.length === PAGE;
|
|
7943
|
+
for (const thread of page.threads) {
|
|
7944
|
+
last = thread.root.id;
|
|
7945
|
+
if (thread.root.task && matches(thread.root)) found.push(thread);
|
|
7946
|
+
if (found.length === limit) break scan;
|
|
7947
|
+
}
|
|
7948
|
+
}
|
|
7949
|
+
const cursor = found.length === limit ? last : Math.max(last, Math.min(position, through));
|
|
7950
|
+
const reader = await ctx.reads.reader(principal.access, found.flatMap((t) => t.last ? [t.root, t.last] : [t.root]));
|
|
7951
|
+
return {
|
|
7952
|
+
tasks: found.map((t) => ({
|
|
7953
|
+
message: view$1(t.root, actors, reader),
|
|
7954
|
+
replies: t.replies,
|
|
7955
|
+
last: t.last && view$1(t.last, actors, reader)
|
|
7956
|
+
})),
|
|
7957
|
+
cursor: ref(cursor)
|
|
7958
|
+
};
|
|
7959
|
+
},
|
|
7960
|
+
/** Claims a task: `label` in its channel, whoever owns it. Of claims that race, one lands. */
|
|
7961
|
+
async claim(identity, id, lease) {
|
|
7962
|
+
const { principal, actor } = identity;
|
|
7963
|
+
const { channel } = await taskOf(principal.access, id);
|
|
7964
|
+
authorize(principal.access, "label", channel);
|
|
7965
|
+
const token = newToken();
|
|
7966
|
+
const made = {};
|
|
7967
|
+
const root = await changeTask(identity, id, (found) => {
|
|
7968
|
+
claimable(found, identity);
|
|
7969
|
+
const claimed = claiming(found, found.task, identity, lease, token);
|
|
7970
|
+
made.claim = claimed.claim;
|
|
7971
|
+
return claimed.events;
|
|
7972
|
+
});
|
|
7973
|
+
const claim = made.claim;
|
|
7974
|
+
await arm(id, claim.epoch, claim.until);
|
|
7975
|
+
return {
|
|
7976
|
+
task: await shown(identity, root),
|
|
7977
|
+
claim
|
|
7978
|
+
};
|
|
7979
|
+
},
|
|
7980
|
+
/** Claims the oldest ready task there, waiting up to `wait` seconds for one. */
|
|
7981
|
+
async claimReady(identity, input, signal) {
|
|
7982
|
+
const deadline = Date.now() + (input.wait ?? 0) * 1e3;
|
|
7983
|
+
for (let pause = 100;; pause = Math.min(pause * 2, 1e3)) {
|
|
7984
|
+
if (signal.aborted) return { task: null };
|
|
7985
|
+
const next = await claimNext(identity, input, signal);
|
|
7986
|
+
if (next) return {
|
|
7987
|
+
task: await shown(identity, next.root),
|
|
7988
|
+
claim: next.claim
|
|
7989
|
+
};
|
|
7990
|
+
if (Date.now() >= deadline) return { task: null };
|
|
7991
|
+
await paused(Math.min(pause, Math.max(0, deadline - Date.now())), signal);
|
|
7992
|
+
}
|
|
7993
|
+
},
|
|
7994
|
+
/** Extends a claim's lease from now. */
|
|
7995
|
+
async renew(identity, id, proof, lease) {
|
|
7996
|
+
await taskOf(identity.principal.access, id);
|
|
7997
|
+
const made = { until: "" };
|
|
7998
|
+
const root = await changeTask(identity, id, (found) => {
|
|
7999
|
+
fenced(found, proof);
|
|
8000
|
+
made.until = new Date(Date.now() + leaseOf(lease)).toISOString();
|
|
8001
|
+
return [{
|
|
8002
|
+
type: "task.renewed",
|
|
8003
|
+
payload: {
|
|
8004
|
+
id,
|
|
8005
|
+
thread: found.thread,
|
|
8006
|
+
epoch: proof.epoch,
|
|
8007
|
+
until: made.until,
|
|
8008
|
+
by: identity.author
|
|
8009
|
+
}
|
|
8010
|
+
}];
|
|
8011
|
+
});
|
|
8012
|
+
await arm(id, proof.epoch, made.until);
|
|
8013
|
+
return {
|
|
8014
|
+
task: await shown(identity, root),
|
|
8015
|
+
until: made.until
|
|
8016
|
+
};
|
|
8017
|
+
},
|
|
8018
|
+
/** What ends a claim, by the claim: released, completed, or failed. A completion readies the tasks that waited only for it. */
|
|
8019
|
+
async settle(identity, id, proof, how) {
|
|
8020
|
+
await taskOf(identity.principal.access, id);
|
|
8021
|
+
return { task: await shown(identity, await changeTask(identity, id, async (found, position) => {
|
|
8022
|
+
fenced(found, proof);
|
|
8023
|
+
const about = {
|
|
8024
|
+
id,
|
|
8025
|
+
thread: found.thread,
|
|
8026
|
+
epoch: proof.epoch,
|
|
8027
|
+
by: identity.author
|
|
8028
|
+
};
|
|
8029
|
+
if (how.type === "task.released") return [{
|
|
8030
|
+
type: "task.released",
|
|
8031
|
+
payload: about
|
|
8032
|
+
}];
|
|
8033
|
+
if (how.type === "task.failed") return [{
|
|
8034
|
+
type: "task.failed",
|
|
8035
|
+
payload: {
|
|
8036
|
+
...about,
|
|
8037
|
+
reason: how.reason
|
|
8038
|
+
}
|
|
8039
|
+
}];
|
|
8040
|
+
return [{
|
|
8041
|
+
type: "task.completed",
|
|
8042
|
+
payload: {
|
|
8043
|
+
...about,
|
|
8044
|
+
result: how.result
|
|
8045
|
+
}
|
|
8046
|
+
}, ...await readied(id, position)];
|
|
8047
|
+
})) };
|
|
8048
|
+
},
|
|
8049
|
+
/**
|
|
8050
|
+
* Holds a task for a person. A claimed task's hold is its claim's, which it ends; otherwise its
|
|
8051
|
+
* owner's, or `manage`'s. One whose lease ran out is no one's, so its owner holds it.
|
|
8052
|
+
*/
|
|
8053
|
+
async block(identity, id, input) {
|
|
8054
|
+
const task = await taskOf(identity.principal.access, id);
|
|
8055
|
+
if (!input.proof) owns(identity, task);
|
|
8056
|
+
const { proof } = input;
|
|
8057
|
+
return { task: await shown(identity, await changeTask(identity, id, (found) => {
|
|
8058
|
+
if (proof) fenced(found, proof);
|
|
8059
|
+
else {
|
|
8060
|
+
const state = stateNow(found.task, Date.now());
|
|
8061
|
+
if (state === "claimed") throw conflict("task_claimed", `#${id} is claimed until ${found.task.claim.until}: only its claim holds it`);
|
|
8062
|
+
if (state === "blocked") throw conflict("task_blocked", `#${id} is held already: ${found.task.blocked.reason}`);
|
|
8063
|
+
if (finished(found.task)) throw conflict("task_finished", `#${id} is ${found.task.state}`);
|
|
8064
|
+
}
|
|
8065
|
+
return [{
|
|
8066
|
+
type: "task.blocked",
|
|
8067
|
+
payload: {
|
|
8068
|
+
id,
|
|
8069
|
+
thread: found.thread,
|
|
8070
|
+
kind: input.kind ?? null,
|
|
8071
|
+
reason: input.reason,
|
|
8072
|
+
by: identity.author
|
|
8073
|
+
}
|
|
8074
|
+
}];
|
|
8075
|
+
})) };
|
|
8076
|
+
},
|
|
8077
|
+
/** Lifts a hold: back to ready, or to waiting while a task it needs hasn't completed. One that isn't held is left as it is. */
|
|
8078
|
+
async unblock(identity, id) {
|
|
8079
|
+
owns(identity, await taskOf(identity.principal.access, id));
|
|
8080
|
+
return { task: await shown(identity, await changeTask(identity, id, async (found, position) => {
|
|
8081
|
+
if (finished(found.task)) throw conflict("task_finished", `#${id} is ${found.task.state}`);
|
|
8082
|
+
if (found.task.state !== "blocked") return [];
|
|
8083
|
+
const about = {
|
|
8084
|
+
id,
|
|
8085
|
+
thread: found.thread
|
|
8086
|
+
};
|
|
8087
|
+
const ready = found.task.needs.length > 0 && await satisfied(found.task, position);
|
|
8088
|
+
return [{
|
|
8089
|
+
type: "task.unblocked",
|
|
8090
|
+
payload: {
|
|
8091
|
+
...about,
|
|
8092
|
+
by: identity.author
|
|
8093
|
+
}
|
|
8094
|
+
}, ...ready ? [{
|
|
8095
|
+
type: "task.ready",
|
|
8096
|
+
payload: about
|
|
8097
|
+
}] : []];
|
|
8098
|
+
})) };
|
|
8099
|
+
},
|
|
8100
|
+
/** Makes a failed task ready again, in place, so what waits for it still does. One that's open is left as it is. */
|
|
8101
|
+
async reopen(identity, id) {
|
|
8102
|
+
owns(identity, await taskOf(identity.principal.access, id));
|
|
8103
|
+
return { task: await shown(identity, await changeTask(identity, id, (found) => {
|
|
8104
|
+
if (found.task.state === "completed" || found.task.state === "canceled") throw conflict("task_finished", `#${id} is ${found.task.state}`);
|
|
8105
|
+
return found.task.state === "failed" ? [{
|
|
8106
|
+
type: "task.reopened",
|
|
8107
|
+
payload: {
|
|
8108
|
+
id,
|
|
8109
|
+
thread: found.thread,
|
|
8110
|
+
by: identity.author
|
|
8111
|
+
}
|
|
8112
|
+
}] : [];
|
|
8113
|
+
})) };
|
|
8114
|
+
},
|
|
8115
|
+
/** Assigns a task to one of its owner's actors, or with null to anyone who may claim it. A claim it has stays. */
|
|
8116
|
+
async assign(identity, id, actor) {
|
|
8117
|
+
const task = await taskOf(identity.principal.access, id);
|
|
8118
|
+
owns(identity, task);
|
|
8119
|
+
const assigned = ownActor(task.task.owner, actor);
|
|
8120
|
+
return { task: await shown(identity, await changeTask(identity, id, (found) => {
|
|
8121
|
+
if (finished(found.task)) throw conflict("task_finished", `#${id} is ${found.task.state}`);
|
|
8122
|
+
return found.task.for === assigned ? [] : [{
|
|
8123
|
+
type: "task.assigned",
|
|
8124
|
+
payload: {
|
|
8125
|
+
id,
|
|
8126
|
+
thread: found.thread,
|
|
8127
|
+
for: assigned,
|
|
8128
|
+
by: identity.author
|
|
8129
|
+
}
|
|
8130
|
+
}];
|
|
8131
|
+
})) };
|
|
8132
|
+
},
|
|
8133
|
+
/**
|
|
8134
|
+
* Replaces what a task waits for, for its owner or `manage`, while no one holds it and it isn't
|
|
8135
|
+
* finished. They're checked as a new task's are, and must leave it waiting for no task that
|
|
8136
|
+
* waits for it. It waits for them from this write, and `task.ready` follows in it when they've
|
|
8137
|
+
* all completed; with none it's ready. A held task stays held, and the same set changes nothing.
|
|
8138
|
+
*/
|
|
8139
|
+
async needs(identity, id, refs) {
|
|
8140
|
+
const { principal } = identity;
|
|
8141
|
+
owns(identity, await taskOf(principal.access, id));
|
|
8142
|
+
return { task: await shown(identity, await changeTask(identity, id, async (found, position) => {
|
|
8143
|
+
const { task } = found;
|
|
8144
|
+
if (finished(task)) throw conflict("task_finished", `#${id} is ${task.state}`);
|
|
8145
|
+
const over = lapsed(task, Date.now());
|
|
8146
|
+
if (task.state === "claimed" && !over) throw conflict("task_claimed", `#${id} is claimed until ${task.claim.until}: what it needs changes once no one holds it`);
|
|
8147
|
+
const needs = await needsAt(principal.access, refs, position, id);
|
|
8148
|
+
if (needs.ids.length === task.needs.length && needs.ids.every((need) => task.needs.includes(need))) return [];
|
|
8149
|
+
await acyclic(principal.access, id, needs.ids.filter((need) => !task.needs.includes(need)), position);
|
|
8150
|
+
const about = {
|
|
8151
|
+
id,
|
|
8152
|
+
thread: found.thread
|
|
8153
|
+
};
|
|
8154
|
+
return [
|
|
8155
|
+
...over ? [{
|
|
8156
|
+
type: "task.expired",
|
|
8157
|
+
payload: {
|
|
8158
|
+
...about,
|
|
8159
|
+
epoch: task.epoch
|
|
8160
|
+
}
|
|
8161
|
+
}] : [],
|
|
8162
|
+
{
|
|
8163
|
+
type: "task.needs_changed",
|
|
8164
|
+
payload: {
|
|
8165
|
+
...about,
|
|
8166
|
+
needs: needs.ids,
|
|
8167
|
+
by: identity.author
|
|
8168
|
+
}
|
|
8169
|
+
},
|
|
8170
|
+
...needs.ids.length > 0 && needs.completed && task.state !== "blocked" ? [{
|
|
8171
|
+
type: "task.ready",
|
|
8172
|
+
payload: about
|
|
8173
|
+
}] : []
|
|
8174
|
+
];
|
|
8175
|
+
})) };
|
|
8176
|
+
},
|
|
8177
|
+
/** Cancels a task that hasn't completed, ending any claim. What waits for it keeps waiting. */
|
|
8178
|
+
async cancel(identity, id, reason) {
|
|
8179
|
+
owns(identity, await taskOf(identity.principal.access, id));
|
|
8180
|
+
return { task: await shown(identity, await changeTask(identity, id, (found) => {
|
|
8181
|
+
if (found.task.state === "completed" || found.task.state === "canceled") throw conflict("task_finished", `#${id} is ${found.task.state}`);
|
|
8182
|
+
return [{
|
|
8183
|
+
type: "task.canceled",
|
|
8184
|
+
payload: {
|
|
8185
|
+
id,
|
|
8186
|
+
thread: found.thread,
|
|
8187
|
+
reason,
|
|
8188
|
+
by: identity.author
|
|
8189
|
+
}
|
|
8190
|
+
}];
|
|
8191
|
+
})) };
|
|
8192
|
+
}
|
|
8193
|
+
};
|
|
8194
|
+
}
|
|
6861
8195
|
//#endregion
|
|
6862
8196
|
//#region src/server/watch.ts
|
|
6863
8197
|
const ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
6864
|
-
/** The events a tail shows, receipts included for `--all`: each concerns a message, and carries its thread for the lane. */
|
|
8198
|
+
/** The events a tail shows, receipts and a claim's renewals included for `--all`: each concerns a message, and carries its thread for the lane. */
|
|
6865
8199
|
const WATCHED = [
|
|
6866
8200
|
"message.posted",
|
|
6867
8201
|
"message.edited",
|
|
@@ -6872,10 +8206,28 @@ const WATCHED = [
|
|
|
6872
8206
|
"message.sync_failed",
|
|
6873
8207
|
"message.synced",
|
|
6874
8208
|
"message.files_synced",
|
|
6875
|
-
"thread.synced"
|
|
8209
|
+
"thread.synced",
|
|
8210
|
+
"task.created",
|
|
8211
|
+
"task.assigned",
|
|
8212
|
+
"task.needs_changed",
|
|
8213
|
+
"task.ready",
|
|
8214
|
+
"task.claimed",
|
|
8215
|
+
"task.renewed",
|
|
8216
|
+
"task.released",
|
|
8217
|
+
"task.expired",
|
|
8218
|
+
"task.completed",
|
|
8219
|
+
"task.failed",
|
|
8220
|
+
"task.blocked",
|
|
8221
|
+
"task.unblocked",
|
|
8222
|
+
"task.canceled",
|
|
8223
|
+
"task.reopened"
|
|
6876
8224
|
];
|
|
6877
|
-
/** Receipts: only a `--all` tail shows them. */
|
|
6878
|
-
const RECEIPTS = /* @__PURE__ */ new Set([
|
|
8225
|
+
/** Receipts and a claim's renewals: only a `--all` tail shows them. */
|
|
8226
|
+
const RECEIPTS = /* @__PURE__ */ new Set([
|
|
8227
|
+
"message.synced",
|
|
8228
|
+
"message.files_synced",
|
|
8229
|
+
"task.renewed"
|
|
8230
|
+
]);
|
|
6879
8231
|
/**
|
|
6880
8232
|
* What a registrant's sync gets, in every channel where it may sync: what it delivers to platforms,
|
|
6881
8233
|
* and a thread's new targets. A tail never shows `thread.synced`, so no watcher gets it.
|
|
@@ -7342,6 +8694,16 @@ function cursorAt(value) {
|
|
|
7342
8694
|
if (index === null) throw badRequest(`${value} isn't a cursor or a message ID on this board`, "invalid_cursor");
|
|
7343
8695
|
return index;
|
|
7344
8696
|
}
|
|
8697
|
+
/** A `/threads` page's `older` on this board: where its oldest thread stands, its latest message's index then its root's. */
|
|
8698
|
+
const olderRef = (position) => `${position.at}.${position.id}`;
|
|
8699
|
+
function olderAt(value) {
|
|
8700
|
+
const match = /^(\d{1,15})\.(\d{1,15})$/.exec(value);
|
|
8701
|
+
if (!match) throw badRequest(`${value} isn't a page of threads on this board`, "invalid_cursor");
|
|
8702
|
+
return {
|
|
8703
|
+
at: Number(match[1]),
|
|
8704
|
+
id: Number(match[2])
|
|
8705
|
+
};
|
|
8706
|
+
}
|
|
7345
8707
|
/** The message a ref in a request names, as its log index, or a 404 for one that names nothing here. */
|
|
7346
8708
|
function messageAt(value) {
|
|
7347
8709
|
const index = indexOf$1(value);
|
|
@@ -7518,6 +8880,10 @@ function build(options, binding) {
|
|
|
7518
8880
|
store: log,
|
|
7519
8881
|
scheduler: options.scheduler
|
|
7520
8882
|
});
|
|
8883
|
+
const tasks = createTasks({
|
|
8884
|
+
store: log,
|
|
8885
|
+
scheduler: options.scheduler
|
|
8886
|
+
});
|
|
7521
8887
|
const projection = tablesConsumer(store.tables);
|
|
7522
8888
|
/** Sync's delivery in progress here: its receipts are its consumer's appends, fenced by its attempt. */
|
|
7523
8889
|
const delivering = new AsyncLocalStorage();
|
|
@@ -7598,6 +8964,7 @@ function build(options, binding) {
|
|
|
7598
8964
|
accessNow: binding.accessNow,
|
|
7599
8965
|
send: async (identity, origin, input) => sendAs(identity, origin, input)
|
|
7600
8966
|
});
|
|
8967
|
+
tasks.bind({ writes });
|
|
7601
8968
|
watchers.bind({ reads });
|
|
7602
8969
|
/**
|
|
7603
8970
|
* Whether `principal` may have a file: one it uploaded, or one a message it
|
|
@@ -7643,6 +9010,12 @@ function build(options, binding) {
|
|
|
7643
9010
|
before = messages[0].id;
|
|
7644
9011
|
}
|
|
7645
9012
|
}
|
|
9013
|
+
/**
|
|
9014
|
+
* The IDs the events of a batch made under `id` carry, as a post's carries its own: each root's post in turn, and the
|
|
9015
|
+
* event after the first root, which says what the batch was asked for. A batch is found again by them alone.
|
|
9016
|
+
*/
|
|
9017
|
+
const batchRoot = (id, at) => derivedId("board-batch", id, String(at));
|
|
9018
|
+
const batchAsked = (id, request) => derivedId("board-batch-request", id, request);
|
|
7646
9019
|
/** Files a write carries, as the board stored them: each one the caller may have, or 404 as if it didn't exist. */
|
|
7647
9020
|
const attachmentsFor = (principal, ids) => Promise.all([...new Set(ids ?? [])].map(async (id) => {
|
|
7648
9021
|
const { attachment, ...uploader } = await readUpload(blob, id);
|
|
@@ -7673,6 +9046,13 @@ function build(options, binding) {
|
|
|
7673
9046
|
throw error;
|
|
7674
9047
|
}
|
|
7675
9048
|
}
|
|
9049
|
+
/** Refuses a request's post an ID that's someone else's to post with: the board's own, another client's key, or a sync's import. */
|
|
9050
|
+
function ownId(caller, principal, id) {
|
|
9051
|
+
if (!fromRequest(caller) || id === void 0) return;
|
|
9052
|
+
if (DERIVED_ID.test(id)) throw badRequest("id: UUIDv8s are the board's own, for the writes it makes itself");
|
|
9053
|
+
if (id.startsWith("key:") && !isKeyIdOf(id, principal.id)) throw badRequest(`id: key:… IDs derive from a client's key, and this token's are key:${principal.id}:…`);
|
|
9054
|
+
if (id.startsWith("sync:")) throw badRequest("id: sync:… IDs are what sync imports, under the principal that imports them");
|
|
9055
|
+
}
|
|
7676
9056
|
/** A post as it lands: what the caller may post, with the files it may have. */
|
|
7677
9057
|
async function postNow(caller, identity, parsed) {
|
|
7678
9058
|
const { principal } = identity;
|
|
@@ -7682,9 +9062,7 @@ function build(options, binding) {
|
|
|
7682
9062
|
re: parsed.re === void 0 ? void 0 : messageAt(parsed.re)
|
|
7683
9063
|
};
|
|
7684
9064
|
const proofs = await authorizePost(writes, principal.access, body);
|
|
7685
|
-
|
|
7686
|
-
if (fromRequest(caller) && body.id?.startsWith("key:") && !isKeyIdOf(body.id, principal.id)) throw badRequest(`id: key:… IDs derive from a client's key, and this token's are key:${principal.id}:…`);
|
|
7687
|
-
if (fromRequest(caller) && body.id?.startsWith("sync:")) throw badRequest("id: sync:… IDs are what sync imports, under the principal that imports them");
|
|
9065
|
+
ownId(caller, principal, body.id);
|
|
7688
9066
|
const attachments = await attachmentsFor(principal, body.attachments);
|
|
7689
9067
|
const { schedule } = caller.origin;
|
|
7690
9068
|
return { message: await writePost(writes, {
|
|
@@ -7806,8 +9184,9 @@ function build(options, binding) {
|
|
|
7806
9184
|
actors,
|
|
7807
9185
|
access: principal.access
|
|
7808
9186
|
}), principal.access);
|
|
7809
|
-
const { threads, position } = await reads.threads(filter, {
|
|
9187
|
+
const { threads, position, older } = await reads.threads(filter, {
|
|
7810
9188
|
...query.after !== void 0 ? { after: cursorAt(query.after) } : {},
|
|
9189
|
+
...query.before !== void 0 ? { before: olderAt(query.before) } : {},
|
|
7811
9190
|
limit: query.limit ?? 50
|
|
7812
9191
|
});
|
|
7813
9192
|
const reader = await reads.reader(principal.access, threads.flatMap((t) => t.last ? [t.root, t.last] : [t.root]));
|
|
@@ -7817,7 +9196,8 @@ function build(options, binding) {
|
|
|
7817
9196
|
replies: t.replies,
|
|
7818
9197
|
last: t.last && view$1(t.last, actors, reader)
|
|
7819
9198
|
})),
|
|
7820
|
-
cursor: ref(Math.min(position, through))
|
|
9199
|
+
cursor: ref(Math.min(position, through)),
|
|
9200
|
+
...older !== void 0 ? { older: older && olderRef(older) } : {}
|
|
7821
9201
|
};
|
|
7822
9202
|
},
|
|
7823
9203
|
async message(caller, { id }, signal) {
|
|
@@ -7927,6 +9307,158 @@ function build(options, binding) {
|
|
|
7927
9307
|
schedule: blindSchedule(answer.schedule, identity.principal.access, identity.actor.id)
|
|
7928
9308
|
};
|
|
7929
9309
|
},
|
|
9310
|
+
tasks: async (caller, query) => tasks.list(await identityOf(caller), query),
|
|
9311
|
+
async createTask(caller, body) {
|
|
9312
|
+
if (body.re === void 0) withinLimits({
|
|
9313
|
+
text: body.text ?? "",
|
|
9314
|
+
attachments: body.attachments
|
|
9315
|
+
});
|
|
9316
|
+
taskLimits.needs(body.needs);
|
|
9317
|
+
taskLimits.lease(body.lease);
|
|
9318
|
+
const identity = await identityOf(caller);
|
|
9319
|
+
const { principal } = identity;
|
|
9320
|
+
const about = {
|
|
9321
|
+
needs: body.needs,
|
|
9322
|
+
for: body.for,
|
|
9323
|
+
claim: body.claim,
|
|
9324
|
+
lease: body.lease
|
|
9325
|
+
};
|
|
9326
|
+
if (body.re !== void 0) return tasks.adopt(identity, messageAt(body.re), about);
|
|
9327
|
+
const proofs = await authorizePost(writes, principal.access, {
|
|
9328
|
+
channel: body.channel,
|
|
9329
|
+
labels: body.labels
|
|
9330
|
+
});
|
|
9331
|
+
ownId(caller, principal, body.id);
|
|
9332
|
+
const attachments = await attachmentsFor(principal, body.attachments);
|
|
9333
|
+
const made = tasks.starting(identity, {
|
|
9334
|
+
channel: body.channel,
|
|
9335
|
+
...about
|
|
9336
|
+
});
|
|
9337
|
+
const task = await writePost(writes, {
|
|
9338
|
+
attachments,
|
|
9339
|
+
identity,
|
|
9340
|
+
proofs,
|
|
9341
|
+
channel: body.channel,
|
|
9342
|
+
text: body.text ?? "",
|
|
9343
|
+
labels: body.labels,
|
|
9344
|
+
noSync: body.noSync,
|
|
9345
|
+
id: body.id,
|
|
9346
|
+
with: made.along
|
|
9347
|
+
});
|
|
9348
|
+
if (!task.task) throw duplicateId();
|
|
9349
|
+
const claim = await made.claimed(messageAt(task.id));
|
|
9350
|
+
return {
|
|
9351
|
+
task,
|
|
9352
|
+
...claim ? { claim } : {}
|
|
9353
|
+
};
|
|
9354
|
+
},
|
|
9355
|
+
async createTasks(caller, body) {
|
|
9356
|
+
taskLimits.batch(body.tasks);
|
|
9357
|
+
for (const [at, task] of body.tasks.entries()) {
|
|
9358
|
+
if (task.text.length > 1e4) throw textTooLong();
|
|
9359
|
+
if ((task.attachments?.length ?? 0) > 10) throw badRequest(`tasks.${at}.attachments: a message carries at most 10`);
|
|
9360
|
+
}
|
|
9361
|
+
const identity = await identityOf(caller);
|
|
9362
|
+
const { principal } = identity;
|
|
9363
|
+
const proofs = await authorizePost(writes, principal.access, {
|
|
9364
|
+
channel: body.channel,
|
|
9365
|
+
labels: body.tasks.find((task) => Object.keys(task.labels ?? {}).length > 0)?.labels
|
|
9366
|
+
});
|
|
9367
|
+
ownId(caller, principal, body.id);
|
|
9368
|
+
const made = tasks.startingMany(identity, body.tasks);
|
|
9369
|
+
const rootId = (at) => body.id !== void 0 ? batchRoot(body.id, at) : Promise.resolve(crypto.randomUUID());
|
|
9370
|
+
const roots = await Promise.all(body.tasks.map(async (task, at) => ({
|
|
9371
|
+
id: await rootId(at),
|
|
9372
|
+
text: task.text,
|
|
9373
|
+
labels: task.labels,
|
|
9374
|
+
noSync: task.noSync,
|
|
9375
|
+
attachments: await attachmentsFor(principal, task.attachments)
|
|
9376
|
+
})));
|
|
9377
|
+
const { id } = body;
|
|
9378
|
+
const asked = id !== void 0 ? await batchAsked(id, body.request ?? "") : void 0;
|
|
9379
|
+
const again = id !== void 0 && asked !== void 0 ? {
|
|
9380
|
+
asked,
|
|
9381
|
+
landed: () => landedBatch(writes, identity, (at) => batchRoot(id, at), asked)
|
|
9382
|
+
} : void 0;
|
|
9383
|
+
return { tasks: await writeRoots(writes, {
|
|
9384
|
+
identity,
|
|
9385
|
+
proofs,
|
|
9386
|
+
channel: body.channel,
|
|
9387
|
+
roots,
|
|
9388
|
+
with: made,
|
|
9389
|
+
again
|
|
9390
|
+
}) };
|
|
9391
|
+
},
|
|
9392
|
+
async createdTasks(caller, { id, request }) {
|
|
9393
|
+
const identity = await identityOf(caller);
|
|
9394
|
+
const found = await landedBatch(writes, identity, (at) => batchRoot(id, at), request !== void 0 ? await batchAsked(id, request) : void 0);
|
|
9395
|
+
if ("tasks" in found) return found;
|
|
9396
|
+
if (found.not === "asked") throw duplicateId();
|
|
9397
|
+
throw notFound(`no batch of yours has id ${id}`, "batch_not_found");
|
|
9398
|
+
},
|
|
9399
|
+
async claimReadyTask(caller, body, signal) {
|
|
9400
|
+
taskLimits.lease(body.lease);
|
|
9401
|
+
taskLimits.from(body.from);
|
|
9402
|
+
return tasks.claimReady(await identityOf(caller), body, signal);
|
|
9403
|
+
},
|
|
9404
|
+
async claimTask(caller, { id, lease }) {
|
|
9405
|
+
taskLimits.lease(lease);
|
|
9406
|
+
return tasks.claim(await identityOf(caller), messageAt(id), lease);
|
|
9407
|
+
},
|
|
9408
|
+
async renewTask(caller, { id, epoch, token, lease }) {
|
|
9409
|
+
taskLimits.lease(lease);
|
|
9410
|
+
return tasks.renew(await identityOf(caller), messageAt(id), {
|
|
9411
|
+
epoch,
|
|
9412
|
+
token
|
|
9413
|
+
}, lease);
|
|
9414
|
+
},
|
|
9415
|
+
releaseTask: async (caller, { id, epoch, token }) => tasks.settle(await identityOf(caller), messageAt(id), {
|
|
9416
|
+
epoch,
|
|
9417
|
+
token
|
|
9418
|
+
}, { type: "task.released" }),
|
|
9419
|
+
async completeTask(caller, { id, epoch, token, result }) {
|
|
9420
|
+
taskLimits.note("result", result);
|
|
9421
|
+
return tasks.settle(await identityOf(caller), messageAt(id), {
|
|
9422
|
+
epoch,
|
|
9423
|
+
token
|
|
9424
|
+
}, {
|
|
9425
|
+
type: "task.completed",
|
|
9426
|
+
result: result ?? null
|
|
9427
|
+
});
|
|
9428
|
+
},
|
|
9429
|
+
async failTask(caller, { id, epoch, token, reason }) {
|
|
9430
|
+
taskLimits.note("reason", reason);
|
|
9431
|
+
return tasks.settle(await identityOf(caller), messageAt(id), {
|
|
9432
|
+
epoch,
|
|
9433
|
+
token
|
|
9434
|
+
}, {
|
|
9435
|
+
type: "task.failed",
|
|
9436
|
+
reason
|
|
9437
|
+
});
|
|
9438
|
+
},
|
|
9439
|
+
async blockTask(caller, { id, reason, kind, epoch, token }) {
|
|
9440
|
+
taskLimits.note("reason", reason);
|
|
9441
|
+
taskLimits.kind(kind);
|
|
9442
|
+
return tasks.block(await identityOf(caller), messageAt(id), {
|
|
9443
|
+
reason,
|
|
9444
|
+
kind,
|
|
9445
|
+
...epoch !== void 0 && token !== void 0 ? { proof: {
|
|
9446
|
+
epoch,
|
|
9447
|
+
token
|
|
9448
|
+
} } : {}
|
|
9449
|
+
});
|
|
9450
|
+
},
|
|
9451
|
+
unblockTask: async (caller, { id }) => tasks.unblock(await identityOf(caller), messageAt(id)),
|
|
9452
|
+
reopenTask: async (caller, { id }) => tasks.reopen(await identityOf(caller), messageAt(id)),
|
|
9453
|
+
assignTask: async (caller, { id, for: actor }) => tasks.assign(await identityOf(caller), messageAt(id), actor),
|
|
9454
|
+
async setTaskNeeds(caller, { id, needs }) {
|
|
9455
|
+
taskLimits.needs(needs);
|
|
9456
|
+
return tasks.needs(await identityOf(caller), messageAt(id), needs);
|
|
9457
|
+
},
|
|
9458
|
+
async cancelTask(caller, { id, reason }) {
|
|
9459
|
+
taskLimits.note("reason", reason);
|
|
9460
|
+
return tasks.cancel(await identityOf(caller), messageAt(id), reason);
|
|
9461
|
+
},
|
|
7930
9462
|
grants: async (caller) => access.grants(await identityOf(caller)),
|
|
7931
9463
|
createGrant: async (caller, body) => access.createGrant(await identityOf(caller), body),
|
|
7932
9464
|
updateGrant: async (caller, input) => access.updateGrant(await identityOf(caller), input),
|
|
@@ -8657,6 +10189,7 @@ function build(options, binding) {
|
|
|
8657
10189
|
inboxServer,
|
|
8658
10190
|
schedules.server,
|
|
8659
10191
|
registry.server,
|
|
10192
|
+
tasks.server,
|
|
8660
10193
|
watchers.server
|
|
8661
10194
|
];
|
|
8662
10195
|
return {
|
|
@@ -8813,10 +10346,10 @@ function local(options = {}) {
|
|
|
8813
10346
|
opening ??= (async () => {
|
|
8814
10347
|
const file = localFile(bound.name);
|
|
8815
10348
|
let store;
|
|
8816
|
-
if (options.memory) store = (await import("./memory-
|
|
10349
|
+
if (options.memory) store = (await import("./memory-CZBdVNn8.mjs")).memory();
|
|
8817
10350
|
else {
|
|
8818
10351
|
const { mkdir } = await import("node:fs/promises");
|
|
8819
|
-
const { sqlite } = await import("./sqlite-
|
|
10352
|
+
const { sqlite } = await import("./sqlite-4C5u7-ag.mjs");
|
|
8820
10353
|
const path = join(dir, `${file}.db`);
|
|
8821
10354
|
try {
|
|
8822
10355
|
await mkdir(dir, {
|