@atolis-hq/wake 0.3.110 → 0.3.111

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.
@@ -108,4 +108,4 @@ export function resolveWakeVersion(options = {}) {
108
108
  return `g${headHash.slice(0, 7)}`;
109
109
  return '0.1.0-dev';
110
110
  }
111
- export const wakeVersion = "g8195817";
111
+ export const wakeVersion = "g6ea9313";
@@ -270,7 +270,7 @@ export class InboundTranslator {
270
270
  const intake = evaluateIntakeRules(this.intake, gitHubIntakeFacts(payload));
271
271
  if (intake.ignored)
272
272
  return;
273
- const identity = await this.resolveIdentity({ adapter: this.adapter, key: payload.externalKey }, intake.admitted);
273
+ const identity = await this.resolveIdentity(event.eventId, { adapter: this.adapter, key: payload.externalKey }, intake.admitted);
274
274
  if (identity === null)
275
275
  return;
276
276
  if (!identity.created) {
@@ -281,6 +281,7 @@ export class InboundTranslator {
281
281
  const workItemIdValue = identity.workItemId;
282
282
  if (workItemIdValue === null)
283
283
  throw new Error('Created identity is missing a WorkItem');
284
+ await this.recordAdmissionStarted(event, identity);
284
285
  const isPullRequest = payload.kind === 'pull-request';
285
286
  await admitObservedWork(this.admissionServices(), {
286
287
  adapter: this.adapter,
@@ -453,8 +454,13 @@ export class InboundTranslator {
453
454
  routing: this.routing,
454
455
  };
455
456
  }
456
- async resolveIdentity(externalKey, admitted) {
457
+ async resolveIdentity(sourceEventId, externalKey, admitted) {
457
458
  const key = `${externalKey.adapter}:${externalKey.key}`;
459
+ const recorded = await this.recordedAdmissionIdentity(sourceEventId);
460
+ if (recorded !== undefined) {
461
+ this.minted.set(key, recorded);
462
+ return this.resumeAdmissionIdentity(recorded);
463
+ }
458
464
  const inBatch = this.minted.get(key);
459
465
  if (inBatch !== undefined)
460
466
  return this.inBatchIdentity(inBatch);
@@ -468,6 +474,49 @@ export class InboundTranslator {
468
474
  return null;
469
475
  return { ...this.mintIdentity(externalKey), created: true, deleted: false };
470
476
  }
477
+ async recordedAdmissionIdentity(sourceEventId) {
478
+ const event = (await this.journal.readStream(integrationStream(this.adapter)))
479
+ .map(selectGitHubAdapterEvent)
480
+ .find((candidate) => candidate?.eventType === GitHubEventType.AdmissionStarted &&
481
+ candidate.payload.sourceEventId === sourceEventId);
482
+ if (event?.eventType !== GitHubEventType.AdmissionStarted)
483
+ return undefined;
484
+ return { resourceId: event.payload.resourceId, workItemId: event.payload.workItemId };
485
+ }
486
+ async resumeAdmissionIdentity(identity) {
487
+ const work = await this.work.get(identity.workItemId);
488
+ if (work?.deleted)
489
+ return { ...identity, created: false, deleted: true };
490
+ const correlation = (await this.resources.correlations(identity.resourceId)).find((candidate) => candidate.role === ResourceCorrelationRole.Primary &&
491
+ candidate.workItemId === identity.workItemId);
492
+ return { ...identity, created: correlation === undefined || work === null, deleted: false };
493
+ }
494
+ async recordAdmissionStarted(source, identity) {
495
+ if (identity.workItemId === null)
496
+ throw new Error('Created identity is missing a WorkItem');
497
+ const stream = integrationStream(this.adapter);
498
+ const eventId = `github:admission-started:${this.adapter}:${source.eventId}`;
499
+ const existing = await this.journal.readStream(stream);
500
+ if (existing.some((event) => event.eventId === eventId))
501
+ return;
502
+ await this.journal.append(stream, existing.length, [
503
+ createEventDraft({
504
+ eventId,
505
+ eventType: GitHubEventType.AdmissionStarted,
506
+ occurredAt: source.occurredAt,
507
+ correlationId: source.correlationId,
508
+ causationId: source.eventId,
509
+ actor: { kind: EventActorKind.Integration, id: this.adapter },
510
+ source: { kind: EventSourceKind.Internal, id: 'github-inbound-translator' },
511
+ stream,
512
+ payload: {
513
+ sourceEventId: source.eventId,
514
+ resourceId: identity.resourceId,
515
+ workItemId: identity.workItemId,
516
+ },
517
+ }),
518
+ ]);
519
+ }
471
520
  async inBatchIdentity(identity) {
472
521
  const existing = await this.work?.get(identity.workItemId);
473
522
  return { ...identity, created: false, deleted: existing?.deleted === true };
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { ProviderPermission, PullRequestCheckState, PullRequestState, ReviewActorKind, ReviewerAuthorizationSource, } from '../../../activities/index.js';
3
3
  import { brandedStringSchema, eventEnvelopeSchema, } from '../../../kernel/index.js';
4
+ import { resourceId } from '../../../resources/index.js';
4
5
  import { workItemId } from '../../../work/index.js';
5
6
  import { adapterId } from '../../contracts/identifiers.js';
6
7
  import { ExternalWorkOutcome } from '../../contracts/outcome-vocabulary.js';
@@ -10,6 +11,7 @@ export const GitHubEventType = {
10
11
  CommentObserved: 'integration.github.comment-observed',
11
12
  DeliveryObserved: 'integration.github.delivery-observed',
12
13
  DeletedWorkObservationSkipped: 'integration.github.deleted-work-observation-skipped',
14
+ AdmissionStarted: 'integration.github.admission-started',
13
15
  InboundTranslationRetried: 'integration.github.inbound-translation-retried',
14
16
  InboundTranslationRecovered: 'integration.github.inbound-translation-recovered',
15
17
  InboundTranslationFailed: 'integration.github.inbound-translation-failed',
@@ -77,6 +79,17 @@ const eventSchema = z.discriminatedUnion('eventType', [
77
79
  })
78
80
  .strict(),
79
81
  }),
82
+ eventEnvelopeSchema.extend({
83
+ eventType: z.literal(GitHubEventType.AdmissionStarted),
84
+ stream: streamSchema,
85
+ payload: z
86
+ .object({
87
+ sourceEventId: z.string(),
88
+ resourceId: brandedStringSchema(resourceId),
89
+ workItemId: brandedStringSchema(workItemId),
90
+ })
91
+ .strict(),
92
+ }),
80
93
  eventEnvelopeSchema.extend({
81
94
  eventType: z.literal(GitHubEventType.CommentObserved),
82
95
  stream: streamSchema,
@@ -76,7 +76,7 @@ export class FileEventJournal {
76
76
  this.changeSignalSource.notify();
77
77
  }
78
78
  return finalizedEnvelopes;
79
- });
79
+ }, { waitMs: 5_000, retryIntervalMs: 25 });
80
80
  }
81
81
  // Reads via the persisted per-segment manifest when the in-memory cache is
82
82
  // cold (fresh process, or an on-disk change this instance didn't make),
@@ -317,10 +317,11 @@ async function linuxProcessStartTicks(pid) {
317
317
  throw new Error('Invalid process start ticks');
318
318
  return startTicks;
319
319
  }
320
- export async function withFileLock(path, operation) {
321
- const lock = await acquireFileLock(path, {
320
+ export async function withFileLock(path, operation, options = {}) {
321
+ const lock = await acquireFileLockWithWait(path, {
322
322
  staleAfterMs: 60_000,
323
323
  staleRequiresDeadProcess: true,
324
+ ...options,
324
325
  });
325
326
  if (!lock.acquired)
326
327
  throw new Error(`File lock is already held: ${path}`);
@@ -331,3 +332,18 @@ export async function withFileLock(path, operation) {
331
332
  await lock.release();
332
333
  }
333
334
  }
335
+ async function acquireFileLockWithWait(path, options) {
336
+ const waitMs = options.waitMs ?? 0;
337
+ const retryIntervalMs = options.retryIntervalMs ?? 25;
338
+ const deadline = Date.now() + waitMs;
339
+ do {
340
+ const lock = await acquireFileLock(path, options);
341
+ if (lock.acquired || Date.now() >= deadline)
342
+ return lock;
343
+ await delay(Math.min(retryIntervalMs, Math.max(0, deadline - Date.now())));
344
+ } while (Date.now() < deadline);
345
+ return acquireFileLock(path, options);
346
+ }
347
+ function delay(milliseconds) {
348
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
349
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.3.110",
3
+ "version": "0.3.111",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {