@atolis-hq/wake 0.4.106 → 0.4.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/src/activities/pr/application.js +23 -9
  2. package/dist/src/bootstrap/composition-root.js +1 -1
  3. package/dist/src/bootstrap/version.js +1 -1
  4. package/dist/src/conversations/application/conversation-repository.d.ts +1 -1
  5. package/dist/src/conversations/application/conversation-repository.js +3 -1
  6. package/dist/src/execution/application/run-repository.js +27 -10
  7. package/dist/src/integrations/application/artifact-registration-reactor.js +7 -14
  8. package/dist/src/integrations/application/latest-unresolved-artifacts.d.ts +3 -0
  9. package/dist/src/integrations/application/latest-unresolved-artifacts.js +16 -0
  10. package/dist/src/integrations/github/application/comment-history-reader.d.ts +1 -1
  11. package/dist/src/integrations/github/application/comment-history-reader.js +56 -39
  12. package/dist/src/node_modules/@atolis-hq/eventing/dist/index.d.ts +1 -1
  13. package/dist/src/node_modules/@atolis-hq/eventing/dist/index.d.ts.map +1 -1
  14. package/dist/src/node_modules/@atolis-hq/eventing/dist/index.js +1 -1
  15. package/dist/src/node_modules/@atolis-hq/eventing/dist/projections/projection-processor.d.ts +14 -0
  16. package/dist/src/node_modules/@atolis-hq/eventing/dist/projections/projection-processor.d.ts.map +1 -1
  17. package/dist/src/node_modules/@atolis-hq/eventing/dist/projections/projection-processor.js +31 -1
  18. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/journal-view.d.ts +22 -0
  19. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/journal-view.d.ts.map +1 -0
  20. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/journal-view.js +60 -0
  21. package/dist/src/node_modules/@atolis-hq/eventing-filesystem/dist/file-event-journal.d.ts +2 -0
  22. package/dist/src/node_modules/@atolis-hq/eventing-filesystem/dist/file-event-journal.d.ts.map +1 -1
  23. package/dist/src/node_modules/@atolis-hq/eventing-filesystem/dist/file-event-journal.js +82 -15
  24. package/dist/src/orchestration/application/advance-workflow.js +7 -2
  25. package/dist/src/orchestration/application/deferred-signals.js +2 -6
  26. package/dist/src/orchestration/application/orchestration-repository.d.ts +14 -9
  27. package/dist/src/orchestration/application/orchestration-repository.js +139 -64
  28. package/dist/src/resources/application/resource-lookup.d.ts +2 -1
  29. package/dist/src/resources/application/resource-lookup.js +3 -11
  30. package/dist/src/resources/application/resource-repository.js +10 -10
  31. package/package.json +1 -1
  32. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/cached-journal-view.d.ts +0 -16
  33. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/cached-journal-view.d.ts.map +0 -1
  34. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/cached-journal-view.js +0 -79
@@ -12,6 +12,11 @@ const createFileSystemWatcher = async (directory, notify) => watch(directory, {
12
12
  // tick would leave it identical. Younger stamps are re-listed, never trusted.
13
13
  const unchangedStampMinAgeMs = 2_500;
14
14
  const statBatchSize = 256;
15
+ // Reads may skip the directory listing while a probe for the next segment finds
16
+ // nothing, but never for longer than this: a full listing still bounds how long
17
+ // an unexpected change (a removed segment, an odd clock) can go unnoticed.
18
+ const maxProbeOnlyMs = 5_000;
19
+ const maxProbedDays = 8;
15
20
  export class FileEventJournal {
16
21
  root;
17
22
  clock;
@@ -53,6 +58,7 @@ export class FileEventJournal {
53
58
  runningListing;
54
59
  queuedListing;
55
60
  temporariesReclaimed = false;
61
+ lastFullListingMs = 0;
56
62
  async appendToStream(stream, expectedSequence, drafts) {
57
63
  if (drafts.length === 0)
58
64
  throw new Error('appendToStream requires at least one event');
@@ -61,7 +67,8 @@ export class FileEventJournal {
61
67
  await reclaimStaleSegmentTemps(join(this.root, 'events'));
62
68
  this.temporariesReclaimed = true;
63
69
  }
64
- const snapshot = await this.scan();
70
+ // Positions are assigned from this snapshot, so it must come from a full listing.
71
+ const snapshot = await this.scan(await this.currentEntries({ exhaustive: true }));
65
72
  const current = snapshot.events;
66
73
  const streamEvents = snapshot.eventsByStream.get(key(stream)) ?? [];
67
74
  if (streamEvents.length !== expectedSequence)
@@ -357,31 +364,73 @@ export class FileEventJournal {
357
364
  // call. A listing already running may have read the directory before the
358
365
  // call, so it cannot answer it; every caller arriving while one runs shares a
359
366
  // single follow-up listing. Any number of concurrent reads cost at most two.
360
- currentEntries() {
367
+ currentEntries(options = {}) {
368
+ if (options.exhaustive === true) {
369
+ const running = this.runningListing;
370
+ return (running === undefined ? Promise.resolve() : running.then(noop, noop)).then(() => this.startListing(true));
371
+ }
361
372
  if (this.queuedListing !== undefined)
362
373
  return this.queuedListing;
363
374
  const running = this.runningListing;
364
375
  if (running === undefined)
365
- return this.startListing();
376
+ return this.startListing(false);
366
377
  const queued = running
367
378
  .catch(() => undefined)
368
379
  .then(() => {
369
380
  this.queuedListing = undefined;
370
- return this.startListing();
381
+ return this.startListing(false);
371
382
  });
372
383
  this.queuedListing = queued;
373
384
  return queued;
374
385
  }
375
- startListing() {
376
- const run = this.listSegments().finally(() => {
386
+ startListing(exhaustive) {
387
+ const run = this.listSegments(exhaustive).finally(() => {
377
388
  if (this.runningListing === run)
378
389
  this.runningListing = undefined;
379
390
  });
380
391
  this.runningListing = run;
381
392
  return run;
382
393
  }
383
- async listSegments() {
394
+ // Segments are immutable, contiguous, and named by the global position of
395
+ // their first event (the first segment of a day is `<day>.jsonl`). When the
396
+ // cache holds L events and is the newest listing, nothing newer exists exactly
397
+ // when no segment for position L+1 exists, which costs a few stats instead of
398
+ // a directory listing proportional to the whole history.
399
+ async nothingNewerThanCache() {
400
+ const cached = this.cached;
401
+ const last = cached?.entries.at(-1);
402
+ if (cached === undefined || last === undefined)
403
+ return false;
404
+ if (Date.now() - this.lastFullListingMs > maxProbeOnlyMs)
405
+ return false;
406
+ if (!sameEntries(cached.entries, this.latestEntries))
407
+ return false;
408
+ const next = String(cached.events.length + 1).padStart(20, '0');
409
+ // A writer names a segment from its own clock, so cover every day between the
410
+ // newest cached segment (or yesterday, for a clock running behind) and
411
+ // tomorrow. A wider gap cannot be probed cheaply, so list the directory.
412
+ const dayMs = 24 * 60 * 60 * 1000;
413
+ const lastDay = Date.parse(`${last.file.slice(0, 10)}T00:00:00Z`);
414
+ const firstDay = Math.min(lastDay, Date.UTC(...utcDayParts(Date.now() - dayMs)));
415
+ const lastProbedDay = Date.UTC(...utcDayParts(Date.now() + dayMs));
416
+ if (!Number.isFinite(lastDay) || (lastProbedDay - firstDay) / dayMs > maxProbedDays)
417
+ return false;
418
+ const days = [];
419
+ for (let at = firstDay; at <= lastProbedDay; at += dayMs)
420
+ days.push(new Date(at).toISOString().slice(0, 10));
421
+ const candidates = days.flatMap((d) => [`${d}~${next}.jsonl`, `${d}.jsonl`]);
422
+ const unknown = candidates.filter((name) => !this.knownSegments.has(name));
423
+ const found = await Promise.all(unknown.map((name) => stat(join(this.root, 'events', name)).then(() => true, (error) => {
424
+ if (error.code === 'ENOENT')
425
+ return false;
426
+ throw error;
427
+ })));
428
+ return !found.some(Boolean);
429
+ }
430
+ async listSegments(exhaustive) {
384
431
  const directory = join(this.root, 'events');
432
+ if (!exhaustive && (await this.nothingNewerThanCache()))
433
+ return this.latestEntries;
385
434
  const startedAtMs = Date.now();
386
435
  let stamp;
387
436
  try {
@@ -403,15 +452,27 @@ export class FileEventJournal {
403
452
  this.latestEntries.length === this.knownSegments.size)
404
453
  return this.latestEntries;
405
454
  // The stamp is taken before the listing so a change during it cannot be hidden.
406
- const names = (await readdir(directory)).filter((name) => this.knownSegments.has(name) || segmentName.test(name));
407
- const present = new Set(names);
455
+ // One pass over the directory: names already known are only counted, and
456
+ // the set of present names is built only if a known segment has gone missing.
457
+ const listed = await readdir(directory);
458
+ const unseen = [];
459
+ let knownPresent = 0;
460
+ for (const name of listed) {
461
+ if (this.knownSegments.has(name))
462
+ knownPresent += 1;
463
+ else if (segmentName.test(name))
464
+ unseen.push(name);
465
+ }
408
466
  let removed = false;
409
- for (const name of this.knownSegments.keys())
410
- if (!present.has(name)) {
411
- this.knownSegments.delete(name);
412
- removed = true;
413
- }
414
- const unseen = names.filter((name) => !this.knownSegments.has(name)).sort();
467
+ if (knownPresent !== this.knownSegments.size) {
468
+ const present = new Set(listed);
469
+ for (const name of this.knownSegments.keys())
470
+ if (!present.has(name)) {
471
+ this.knownSegments.delete(name);
472
+ removed = true;
473
+ }
474
+ }
475
+ unseen.sort();
415
476
  for (let index = 0; index < unseen.length; index += statBatchSize)
416
477
  await Promise.all(unseen.slice(index, index + statBatchSize).map(async (file) => {
417
478
  try {
@@ -424,6 +485,7 @@ export class FileEventJournal {
424
485
  }
425
486
  }));
426
487
  this.listing = { stamp, startedAtMs };
488
+ this.lastFullListingMs = startedAtMs;
427
489
  const added = unseen.flatMap((file) => this.knownSegments.get(file) ?? []);
428
490
  const last = this.latestEntries.at(-1);
429
491
  const consistent = this.latestEntries.length + added.length === this.knownSegments.size;
@@ -593,6 +655,11 @@ function sameEntries(a, b) {
593
655
  return (entry.file === other.file && entry.size === other.size && entry.mtimeMs === other.mtimeMs);
594
656
  })));
595
657
  }
658
+ function utcDayParts(ms) {
659
+ const date = new Date(ms);
660
+ return [date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()];
661
+ }
662
+ function noop() { }
596
663
  const key = (stream) => `${stream.kind}:${stream.id}`;
597
664
  function indexEventsByStream(events) {
598
665
  const indexed = new Map();
@@ -1,7 +1,7 @@
1
1
  import { EventSourceKind } from '@atolis-hq/eventing';
2
2
  import { OrchestrationEventType } from '../contracts/events.js';
3
3
  import { commandName } from '../contracts/identifiers.js';
4
- import { WorkflowStatus } from '../contracts/vocabulary.js';
4
+ import { ActivityActivationStatus, WorkflowStatus } from '../contracts/vocabulary.js';
5
5
  import { workflowEventData } from '../domain/decision-events.js';
6
6
  import { requestChangesResume as decideChangesResume, requestFreshOperatorRetry as decideFreshOperatorRetry, requestOperatorRetry as decideOperatorRetry, requestSupplementalActivity as decideSupplementalActivity, isNeedsClarificationResumeEligible, requestRunnerQuotaRetry, } from '../domain/interpreter.js';
7
7
  import { isAuthorisedActor } from '../domain/supplemental-policy.js';
@@ -50,7 +50,12 @@ export class AdvanceWorkflow {
50
50
  }
51
51
  async markActivationStarted(id, activationId, context) {
52
52
  const loaded = await this.repository.load(id);
53
- if (loaded.view?.pendingActivation?.activationId !== activationId)
53
+ const pending = loaded.view?.pendingActivation;
54
+ if (pending?.activationId !== activationId)
55
+ return loaded.view;
56
+ // Dispatch marks before every attempt; a retried tick must not append a new
57
+ // fact for an activation whose recorded state already says it is running.
58
+ if (pending.status === ActivityActivationStatus.Running)
54
59
  return loaded.view;
55
60
  const event = workflowEventData({
56
61
  eventId: `${context.commandId}:${OrchestrationEventType.ActivityStarted}`,
@@ -64,15 +64,11 @@ export class DeferredSignals {
64
64
  return view;
65
65
  }
66
66
  async hasDeferred() {
67
- return (await this.repository.events()).some((entry) => entry.event.eventType === OrchestrationEventType.SignalDeferred);
67
+ return (await this.repository.instancesWithDeferredSignals()).size > 0;
68
68
  }
69
69
  async reconcile(context) {
70
70
  // Only instances holding a deferred signal have anything to replay.
71
- const deferred = new Set();
72
- for (const entry of await this.repository.events())
73
- if (entry.event.eventType === OrchestrationEventType.SignalDeferred)
74
- deferred.add(entry.stream.id);
75
- const ids = [...deferred];
71
+ const ids = [...(await this.repository.instancesWithDeferredSignals())];
76
72
  if (ids.length === 0)
77
73
  return;
78
74
  const fenced = await this.fencedAmong(ids);
@@ -2,29 +2,35 @@ import { type EventJournal } from '@atolis-hq/eventing';
2
2
  import type { DeliveryCompletionBlock } from '../contracts/delivery.js';
3
3
  import type { WorkflowOrchestrationEvent, WorkflowOrchestrationEventData } from '../contracts/events.js';
4
4
  import type { WorkflowInstanceView } from '../contracts/views.js';
5
- type ListedInstance = {
5
+ export type ListedInstance = {
6
6
  readonly sequence: number;
7
7
  readonly view: WorkflowInstanceView | null;
8
8
  };
9
+ export interface OrchestrationSnapshot {
10
+ readonly instances: readonly ListedInstance[];
11
+ readonly events: readonly WorkflowOrchestrationEvent[];
12
+ readonly inherited: ReadonlyMap<string, readonly WorkflowOrchestrationEvent[]>;
13
+ readonly deferredInstanceIds: ReadonlySet<string>;
14
+ }
9
15
  export declare class OrchestrationRepository {
10
16
  private readonly journal;
11
17
  private readonly completionBlock?;
12
- private readonly listed;
18
+ private readonly state;
13
19
  constructor(journal: EventJournal, completionBlock?: ((id: string) => Promise<DeliveryCompletionBlock | undefined>) | undefined);
14
20
  load(id: string): Promise<{
15
21
  sequence: number;
16
22
  view: WorkflowInstanceView | null;
17
23
  }>;
18
- snapshot(): Promise<{
19
- instances: readonly ListedInstance[];
20
- events: readonly WorkflowOrchestrationEvent[];
21
- inherited: ReadonlyMap<string, readonly WorkflowOrchestrationEvent[]>;
22
- }>;
24
+ snapshot(): Promise<OrchestrationSnapshot>;
23
25
  events(): Promise<readonly WorkflowOrchestrationEvent[]>;
26
+ /** Workflow instances that hold at least one deferred signal. */
27
+ instancesWithDeferredSignals(): Promise<ReadonlySet<string>>;
24
28
  history(id: string): Promise<{
25
29
  sequence: number;
26
- events: WorkflowOrchestrationEvent[];
30
+ events: readonly WorkflowOrchestrationEvent[];
27
31
  }>;
32
+ /** The state for reads that span every instance, which fail while any instance is malformed. */
33
+ private healthyState;
28
34
  loadRequired(id: string): Promise<{
29
35
  sequence: number;
30
36
  view: WorkflowInstanceView;
@@ -32,4 +38,3 @@ export declare class OrchestrationRepository {
32
38
  append(id: string, sequence: number, drafts: readonly WorkflowOrchestrationEventData[]): Promise<readonly WorkflowOrchestrationEvent[]>;
33
39
  list(): Promise<readonly ListedInstance[]>;
34
40
  }
35
- export {};
@@ -1,31 +1,32 @@
1
- import { cachedJournalView } from '@atolis-hq/eventing';
1
+ import { foldedJournalView } from '@atolis-hq/eventing';
2
2
  import { decodeOrchestrationEvent, selectWorkflowOrchestrationEvent, } from '../contracts/event-decoder.js';
3
3
  import { OrchestrationEventType } from '../contracts/events.js';
4
4
  import { workflowInstanceId } from '../contracts/identifiers.js';
5
5
  import { isWorkflowInstanceStream, workflowInstanceStream } from '../contracts/streams.js';
6
6
  import { foldWorkflowInstance } from '../domain/workflow-instance.js';
7
+ const emptyState = {
8
+ failures: new Map(),
9
+ instances: [],
10
+ events: [],
11
+ inherited: new Map(),
12
+ deferredInstanceIds: new Set(),
13
+ sequences: new Map(),
14
+ own: new Map(),
15
+ grouped: new Map(),
16
+ listedById: new Map(),
17
+ };
7
18
  export class OrchestrationRepository {
8
19
  journal;
9
20
  completionBlock;
10
- listed;
21
+ state;
11
22
  constructor(journal, completionBlock) {
12
23
  this.journal = journal;
13
24
  this.completionBlock = completionBlock;
14
- this.listed = cachedJournalView(journal, (events) => {
15
- const owned = events.flatMap((entry) => {
16
- const decoded = selectWorkflowOrchestrationEvent(entry);
17
- return decoded === null ? [] : [decoded];
18
- });
19
- return {
20
- instances: deriveList(events, owned),
21
- events: owned,
22
- inherited: inheritedReservations(owned),
23
- };
24
- });
25
+ this.state = foldedJournalView(journal, { initial: emptyState, extend: extendState });
25
26
  }
26
27
  async load(id) {
27
28
  const { events: own, sequence } = await this.history(id);
28
- const inherited = ((await this.listed.get()).inherited.get(id) ?? []).filter((entry) => {
29
+ const inherited = ((await this.state.get()).inherited.get(id) ?? []).filter((entry) => {
29
30
  const fact = entry.event;
30
31
  if (entry.stream.id === id ||
31
32
  fact.eventType !== OrchestrationEventType.DelegationReserved ||
@@ -39,18 +40,27 @@ export class OrchestrationRepository {
39
40
  return { sequence, view: foldWorkflowInstance([...own, ...inherited]) };
40
41
  }
41
42
  snapshot() {
42
- return this.listed.get();
43
+ return this.healthyState();
44
+ }
45
+ async events() {
46
+ return (await this.healthyState()).events;
43
47
  }
44
- events() {
45
- return this.listed.get().then((snapshot) => snapshot.events);
48
+ /** Workflow instances that hold at least one deferred signal. */
49
+ async instancesWithDeferredSignals() {
50
+ return (await this.healthyState()).deferredInstanceIds;
46
51
  }
47
52
  async history(id) {
48
- const raw = await this.journal.readStream(workflowInstanceStream(workflowInstanceId(id)));
49
- const events = raw.flatMap((entry) => {
50
- const owned = selectWorkflowOrchestrationEvent(entry);
51
- return owned === null ? [] : [owned];
52
- });
53
- return { sequence: raw.length, events };
53
+ const state = await this.state.get();
54
+ if (state.failures.has(id))
55
+ throw state.failures.get(id);
56
+ return { sequence: state.sequences.get(id) ?? 0, events: state.own.get(id) ?? [] };
57
+ }
58
+ /** The state for reads that span every instance, which fail while any instance is malformed. */
59
+ async healthyState() {
60
+ const state = await this.state.get();
61
+ for (const failure of state.failures.values())
62
+ throw failure;
63
+ return state;
54
64
  }
55
65
  async loadRequired(id) {
56
66
  const loaded = await this.load(id);
@@ -80,57 +90,122 @@ export class OrchestrationRepository {
80
90
  return events.map(decodeOrchestrationEvent).filter(isWorkflowEvent);
81
91
  }
82
92
  async list() {
83
- return (await this.listed.get()).instances;
93
+ return (await this.healthyState()).instances;
84
94
  }
85
95
  }
86
96
  function isWorkflowEvent(event) {
87
97
  return isWorkflowInstanceStream(event.stream);
88
98
  }
89
- // `sequence` counts every event on the stream, matching readStream, while the
90
- // fold sees only owned orchestration events — exactly what load() computes.
91
- function deriveList(events, owned) {
92
- const streams = new Map();
93
- for (const event of events) {
94
- if (!isWorkflowInstanceStream(event.stream))
99
+ /** Refolds only the touched instances; a fold error fails that instance alone. */
100
+ function refoldTouched(touched, previous, sequences, grouped, failures) {
101
+ const listedById = new Map(previous);
102
+ for (const id of touched) {
103
+ // A parent named by a reservation may not have a stream of its own yet.
104
+ if (!sequences.has(id))
95
105
  continue;
96
- const existing = streams.get(event.stream.id);
97
- if (existing === undefined)
98
- streams.set(event.stream.id, [event]);
99
- else
100
- existing.push(event);
106
+ try {
107
+ listedById.set(id, {
108
+ sequence: sequences.get(id),
109
+ view: foldWorkflowInstance(grouped.get(id) ?? []),
110
+ });
111
+ }
112
+ catch (error) {
113
+ listedById.delete(id);
114
+ if (!failures.has(id))
115
+ failures.set(id, error);
116
+ }
101
117
  }
102
- const grouped = new Map();
103
- const add = (id, event) => {
104
- const group = grouped.get(id);
105
- if (group === undefined)
106
- grouped.set(id, [event]);
107
- else
108
- group.push(event);
109
- };
110
- for (const event of owned) {
111
- add(event.stream.id, event);
112
- if (event.event.eventType === OrchestrationEventType.DelegationReserved &&
113
- event.event.payload.parentWorkflowInstanceId !== event.stream.id)
114
- add(event.event.payload.parentWorkflowInstanceId, event);
118
+ return listedById;
119
+ }
120
+ /** A malformed event poisons only the instance whose stream holds it. */
121
+ function decodeIsolated(raw, instanceId, failures) {
122
+ try {
123
+ return selectWorkflowOrchestrationEvent(raw);
124
+ }
125
+ catch (error) {
126
+ if (!failures.has(instanceId))
127
+ failures.set(instanceId, error);
128
+ // A reservation recorded on a child's stream is part of its parent's view, so
129
+ // a malformed one must not leave the parent quietly missing it.
130
+ const parent = rawReservationParent(raw);
131
+ if (parent !== undefined && !failures.has(parent))
132
+ failures.set(parent, error);
133
+ return null;
115
134
  }
116
- return [...streams.values()].map((streamEvents) => ({
117
- sequence: streamEvents.length,
118
- view: foldWorkflowInstance(grouped.get(streamEvents[0].stream.id) ?? []),
119
- }));
120
135
  }
121
- function inheritedReservations(events) {
122
- const inherited = new Map();
123
- for (const entry of events) {
124
- if (entry.event.eventType !== OrchestrationEventType.DelegationReserved)
136
+ function rawReservationParent(raw) {
137
+ if (raw.event.eventType !== OrchestrationEventType.DelegationReserved)
138
+ return undefined;
139
+ const payload = raw.event.payload;
140
+ if (typeof payload !== 'object' || payload === null)
141
+ return undefined;
142
+ const parent = payload.parentWorkflowInstanceId;
143
+ return typeof parent === 'string' ? parent : undefined;
144
+ }
145
+ /** The parent instance a child-stream DelegationReserved fact is recorded for. */
146
+ function reservingParent(event) {
147
+ return event.event.eventType === OrchestrationEventType.DelegationReserved &&
148
+ event.event.payload.parentWorkflowInstanceId !== event.stream.id
149
+ ? event.event.payload.parentWorkflowInstanceId
150
+ : undefined;
151
+ }
152
+ // `sequence` counts every event on the stream, matching readStream, while the
153
+ // fold sees only owned orchestration events. A DelegationReserved fact recorded
154
+ // on a child's stream also belongs to the parent's group, in journal order.
155
+ function extendState(state, events) {
156
+ const sequences = new Map(state.sequences);
157
+ const own = new Map(state.own);
158
+ const grouped = new Map(state.grouped);
159
+ const inherited = new Map(state.inherited);
160
+ const deferred = new Set(state.deferredInstanceIds);
161
+ const failures = new Map(state.failures);
162
+ const added = [];
163
+ const touched = new Set();
164
+ // Arrays carried over from earlier states are copied on first touch, then
165
+ // extended in place for the rest of this fold.
166
+ const fresh = new Set();
167
+ const push = (map, key, event) => {
168
+ let list = map.get(key);
169
+ if (list === undefined || !fresh.has(list)) {
170
+ list = [...(list ?? [])];
171
+ fresh.add(list);
172
+ map.set(key, list);
173
+ }
174
+ list.push(event);
175
+ };
176
+ for (const raw of events) {
177
+ if (!isWorkflowInstanceStream(raw.stream))
125
178
  continue;
126
- const id = entry.event.payload.parentWorkflowInstanceId;
127
- if (entry.stream.id === id)
179
+ const id = raw.stream.id;
180
+ sequences.set(id, (sequences.get(id) ?? 0) + 1);
181
+ touched.add(id);
182
+ const event = decodeIsolated(raw, id, failures);
183
+ if (event === null)
128
184
  continue;
129
- const prior = inherited.get(id);
130
- if (prior === undefined)
131
- inherited.set(id, [entry]);
132
- else
133
- prior.push(entry);
185
+ added.push(event);
186
+ push(own, id, event);
187
+ push(grouped, id, event);
188
+ if (event.event.eventType === OrchestrationEventType.SignalDeferred)
189
+ deferred.add(id);
190
+ const parent = reservingParent(event);
191
+ if (parent !== undefined) {
192
+ push(grouped, parent, event);
193
+ push(inherited, parent, event);
194
+ touched.add(parent);
195
+ }
134
196
  }
135
- return inherited;
197
+ if (touched.size === 0)
198
+ return state;
199
+ const listedById = refoldTouched(touched, state.listedById, sequences, grouped, failures);
200
+ return {
201
+ failures,
202
+ instances: [...listedById.values()],
203
+ events: added.length === 0 ? state.events : [...state.events, ...added],
204
+ inherited,
205
+ deferredInstanceIds: deferred,
206
+ sequences,
207
+ own,
208
+ grouped,
209
+ listedById,
210
+ };
136
211
  }
@@ -1,4 +1,4 @@
1
- import type { EventJournal, ProjectionStore } from '@atolis-hq/eventing';
1
+ import { type CheckpointStore, type EventJournal, type ProjectionStore } from '@atolis-hq/eventing';
2
2
  import type { WorkItemId } from '../../work/index.js';
3
3
  import type { ResourceId } from '../contracts/identifiers.js';
4
4
  import type { ExternalResourceKey, ResourceCorrelationView } from '../contracts/views.js';
@@ -14,4 +14,5 @@ export interface ResourceLookup {
14
14
  export declare function createResourceLookup(dependencies: {
15
15
  readonly journal: EventJournal;
16
16
  readonly projections: ProjectionStore;
17
+ readonly checkpoints: Pick<CheckpointStore, 'load'>;
17
18
  }): ResourceLookup;
@@ -1,20 +1,12 @@
1
+ import { readCurrentProjection, } from '@atolis-hq/eventing';
1
2
  import { ResourceEventType, selectResourceEvent } from '../contracts/events.js';
2
3
  import { resourceStream } from '../contracts/streams.js';
3
4
  import { ResourceCorrelationRole } from '../contracts/vocabulary.js';
4
5
  import { externalKeyProjectionKey, resourcesByExternalKeyProjection, workCorrelationsProjection, } from './lookup-projections.js';
5
6
  export function createResourceLookup(dependencies) {
6
- const seeded = async (definition, key) => {
7
- const stored = await dependencies.projections.read(definition.name, key);
8
- let value = stored?.value ?? definition.initial(key);
9
- for (const event of await dependencies.journal.readAll(stored?.lastGlobalPosition ?? 0)) {
10
- if (definition.select(event)?.key === key)
11
- value = definition.project(value, event);
12
- }
13
- return value;
14
- };
15
7
  return {
16
- resourceIdForExternalKey: (externalKey) => seeded(resourcesByExternalKeyProjection, externalKeyProjectionKey(externalKey)),
17
- correlationsForWork: (workItemId) => seeded(workCorrelationsProjection, workItemId),
8
+ resourceIdForExternalKey: (externalKey) => readCurrentProjection(dependencies, resourcesByExternalKeyProjection, externalKeyProjectionKey(externalKey)),
9
+ correlationsForWork: (workItemId) => readCurrentProjection(dependencies, workCorrelationsProjection, workItemId),
18
10
  async primaryCorrelation(resourceId) {
19
11
  const active = new Map();
20
12
  const retracted = [];
@@ -1,17 +1,18 @@
1
- import { cachedJournalView } from '@atolis-hq/eventing';
1
+ import { decodeOnce, foldedJournalView, } from '@atolis-hq/eventing';
2
2
  import { ResourceEventType, decodeResourceEvent, selectResourceEvent, } from '../contracts/events.js';
3
3
  import { resourceStream } from '../contracts/streams.js';
4
4
  import { foldResource } from '../domain/resource.js';
5
+ const selectResource = decodeOnce(selectResourceEvent);
5
6
  export class ResourceRepository {
6
7
  journal;
7
8
  resourceIds;
8
9
  constructor(journal) {
9
10
  this.journal = journal;
10
- this.resourceIds = cachedJournalView(journal, deriveResourceIds);
11
+ this.resourceIds = foldedJournalView(journal, { initial: [], extend: extendResourceIds });
11
12
  }
12
13
  async load(resourceId) {
13
14
  const events = await this.journal.readStream(resourceStream(resourceId));
14
- const owned = events.map(selectResourceEvent).filter((event) => event !== null);
15
+ const owned = events.map(selectResource).filter((event) => event !== null);
15
16
  return { sequence: events.length, resource: foldResource(owned) };
16
17
  }
17
18
  async append(resourceId, expectedSequence, drafts) {
@@ -22,11 +23,10 @@ export class ResourceRepository {
22
23
  return this.resourceIds.get();
23
24
  }
24
25
  }
25
- function deriveResourceIds(events) {
26
- return [
27
- ...new Set(events
28
- .map(selectResourceEvent)
29
- .filter((event) => event?.event.eventType === ResourceEventType.ResourceDiscovered)
30
- .map((event) => event.stream.id)),
31
- ];
26
+ function extendResourceIds(known, events) {
27
+ const discovered = events
28
+ .map(selectResourceEvent)
29
+ .filter((event) => event?.event.eventType === ResourceEventType.ResourceDiscovered)
30
+ .map((event) => event.stream.id);
31
+ return [...new Set([...known, ...discovered])];
32
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.4.106",
3
+ "version": "0.4.107",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -1,16 +0,0 @@
1
- import type { EventEnvelope } from '../contracts/events.js';
2
- import type { EventJournal } from '../store/event-journal.js';
3
- export declare const JOURNAL_CHANGE_FALLBACK_MS = 30000;
4
- export interface CachedJournalView<Value> {
5
- get(): Promise<Value>;
6
- }
7
- type ViewJournal = Pick<EventJournal, 'readAll' | 'latestGlobalPosition'>;
8
- export declare function cachedJournalView<Value>(journal: ViewJournal, derive: (events: readonly EventEnvelope[]) => Value | Promise<Value>, _now?: () => number): CachedJournalView<Value>;
9
- export interface JournalFold<Value> {
10
- readonly initial: Value;
11
- /** Returns the value after `events`, which follow everything already folded. Must not mutate `value`. */
12
- extend(value: Value, events: readonly EventEnvelope[]): Value;
13
- }
14
- export declare function foldedJournalView<Value>(journal: ViewJournal, fold: JournalFold<Value>): CachedJournalView<Value>;
15
- export {};
16
- //# sourceMappingURL=cached-journal-view.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cached-journal-view.d.ts","sourceRoot":"","sources":["../../src/runtime/cached-journal-view.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAE9D,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAEjD,MAAM,WAAW,iBAAiB,CAAC,KAAK;IACtC,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;CACvB;AAED,KAAK,WAAW,GAAG,IAAI,CAAC,YAAY,EAAE,SAAS,GAAG,sBAAsB,CAAC,CAAC;AAe1E,wBAAgB,iBAAiB,CAAC,KAAK,EACrC,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,CAAC,MAAM,EAAE,SAAS,aAAa,EAAE,KAAK,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,EACpE,IAAI,GAAE,MAAM,MAAiB,GAC5B,iBAAiB,CAAC,KAAK,CAAC,CAsB1B;AAED,MAAM,WAAW,WAAW,CAAC,KAAK;IAChC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IACxB,yGAAyG;IACzG,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,aAAa,EAAE,GAAG,KAAK,CAAC;CAC/D;AAMD,wBAAgB,iBAAiB,CAAC,KAAK,EACrC,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,GACvB,iBAAiB,CAAC,KAAK,CAAC,CA+B1B"}