@atolis-hq/wake 0.4.83 → 0.4.84

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.
@@ -740,6 +740,7 @@ async function checkProviders(root, failures, notices) {
740
740
  failures.push('configured provider has no adapter identity');
741
741
  continue;
742
742
  }
743
+ notices.push(...((await provider.webhook?.diagnostics?.()) ?? []));
743
744
  if (provider.checkConnectivity === undefined)
744
745
  continue;
745
746
  try {
@@ -124,6 +124,6 @@ export function resolveWakeVersion(options = {}) {
124
124
  return `g${headHash.slice(0, 7)}`;
125
125
  return '0.1.0-dev';
126
126
  }
127
- export const wakeVersion = "v0.4.83";
127
+ export const wakeVersion = "v0.4.84";
128
128
  /** The registry-installable version used by packaged sandbox images. */
129
129
  export const wakePackageVersion = packageVersion(defaultRepoRoot) ?? wakeVersion;
@@ -83,6 +83,8 @@ export interface ProviderInstance {
83
83
  }
84
84
  export interface ProviderWebhook {
85
85
  provision(): Promise<void>;
86
+ /** Safe provisioning notices, read without contacting the external provider. */
87
+ diagnostics?(): Promise<readonly string[]>;
86
88
  setupInstructions(): Promise<readonly ProviderWebhookSetup[]>;
87
89
  receive(body: Buffer, headers: Readonly<Record<string, string | string[] | undefined>>, trigger: () => void): Promise<404 | 400 | 401 | 202>;
88
90
  }
@@ -26,6 +26,9 @@ function logGitHubRequestFailure(message) {
26
26
  const text = redactGitHubRequestMessage(message);
27
27
  if (requestLogStatus(text) === 304)
28
28
  return;
29
+ // Provisioning reports a repository-specific, secret-free diagnosis instead.
30
+ if (/^(?:GET|POST|PATCH) \/repos\/[^/\s]+\/[^/\s]+\/hooks(?:\/\d+)? - /.test(text))
31
+ return;
29
32
  process.stderr.write(`GitHub request failed: ${text}\n`);
30
33
  }
31
34
  export function createGitHubClient(token) {
@@ -0,0 +1,25 @@
1
+ import { z } from 'zod';
2
+ export declare const WebhookOperation: {
3
+ readonly Configure: "configure webhook";
4
+ readonly Read: "read managed webhook";
5
+ readonly Create: "create webhook";
6
+ readonly Update: "update managed webhook";
7
+ readonly State: "persist webhook state";
8
+ };
9
+ export declare const webhookDiagnosisSchema: z.ZodObject<{
10
+ operation: z.ZodEnum<{
11
+ readonly Configure: "configure webhook";
12
+ readonly Read: "read managed webhook";
13
+ readonly Create: "create webhook";
14
+ readonly Update: "update managed webhook";
15
+ readonly State: "persist webhook state";
16
+ }>;
17
+ status: z.ZodOptional<z.ZodNumber>;
18
+ }, z.core.$strip>;
19
+ export type WebhookDiagnosis = z.infer<typeof webhookDiagnosisSchema>;
20
+ export declare class WebhookProvisioningFailure extends Error {
21
+ readonly diagnosis: WebhookDiagnosis;
22
+ constructor(diagnosis: WebhookDiagnosis);
23
+ }
24
+ export declare function renderWebhookDiagnosis(scope: string, diagnosis: WebhookDiagnosis): string;
25
+ export declare function webhookOperation<T>(operation: WebhookDiagnosis['operation'], run: () => Promise<T>): Promise<T>;
@@ -0,0 +1,51 @@
1
+ import { z } from 'zod';
2
+ import { defineClosedVocabulary } from '../../../kernel/index.js';
3
+ export const WebhookOperation = defineClosedVocabulary({
4
+ Configure: 'configure webhook',
5
+ Read: 'read managed webhook',
6
+ Create: 'create webhook',
7
+ Update: 'update managed webhook',
8
+ State: 'persist webhook state',
9
+ });
10
+ export const webhookDiagnosisSchema = z.object({
11
+ operation: z.enum(WebhookOperation),
12
+ status: z.number().int().min(100).max(599).optional(),
13
+ });
14
+ export class WebhookProvisioningFailure extends Error {
15
+ diagnosis;
16
+ constructor(diagnosis) {
17
+ super('Webhook provisioning failed');
18
+ this.diagnosis = diagnosis;
19
+ }
20
+ }
21
+ export function renderWebhookDiagnosis(scope, diagnosis) {
22
+ const { operation, status } = diagnosis;
23
+ const reason = operation === WebhookOperation.Configure
24
+ ? 'surfaces.web.publicUrl is not configured'
25
+ : status === 403 || status === 404
26
+ ? `HTTP ${status}; the token may lack repository Webhooks read/write permission; verify repository access and token permissions`
27
+ : status === undefined
28
+ ? 'request or local webhook state operation failed; check connectivity and provider state access'
29
+ : `HTTP ${status}; check GitHub availability and webhook configuration`;
30
+ return `GitHub ${scope}: ${operation} failed: ${reason}. Intake continues by polling.`;
31
+ }
32
+ export async function webhookOperation(operation, run) {
33
+ try {
34
+ return await run();
35
+ }
36
+ catch (error) {
37
+ const status = typeof error === 'object' &&
38
+ error !== null &&
39
+ 'status' in error &&
40
+ typeof error.status === 'number' &&
41
+ Number.isInteger(error.status) &&
42
+ error.status >= 100 &&
43
+ error.status <= 599
44
+ ? error.status
45
+ : undefined;
46
+ throw new WebhookProvisioningFailure({
47
+ operation,
48
+ ...(status === undefined ? {} : { status }),
49
+ });
50
+ }
51
+ }
@@ -1,7 +1,9 @@
1
1
  import type { AdapterId } from '../../contracts/identifiers.js';
2
+ import { type WebhookDiagnosis } from './webhook-diagnosis.js';
2
3
  export interface GitHubWebhookState {
3
4
  readonly secret: string;
4
5
  readonly hookId?: number;
6
+ readonly diagnosis?: WebhookDiagnosis;
5
7
  }
6
8
  /** Provider operational state: intentionally outside configuration and projections. */
7
9
  export declare class GitHubWebhookStateStore {
@@ -9,6 +11,7 @@ export declare class GitHubWebhookStateStore {
9
11
  private readonly adapter;
10
12
  constructor(root: string, adapter: AdapterId);
11
13
  load(owner: string, repo: string): Promise<GitHubWebhookState>;
14
+ diagnosis(owner: string, repo: string): Promise<WebhookDiagnosis | undefined>;
12
15
  save(owner: string, repo: string, state: GitHubWebhookState): Promise<void>;
13
16
  serialise<Value>(owner: string, repo: string, operation: () => Promise<Value>): Promise<Value>;
14
17
  private path;
@@ -1,6 +1,7 @@
1
1
  import { randomBytes } from 'node:crypto';
2
2
  import { mkdir, open, readFile, rename, unlink, writeFile } from 'node:fs/promises';
3
3
  import { join } from 'node:path';
4
+ import { WebhookOperation, webhookDiagnosisSchema, } from './webhook-diagnosis.js';
4
5
  /** Provider operational state: intentionally outside configuration and projections. */
5
6
  export class GitHubWebhookStateStore {
6
7
  root;
@@ -28,6 +29,21 @@ export class GitHubWebhookStateStore {
28
29
  await this.save(owner, repo, state);
29
30
  return state;
30
31
  }
32
+ async diagnosis(owner, repo) {
33
+ try {
34
+ const parsed = JSON.parse(await readFile(this.path(owner, repo), 'utf8'));
35
+ if (typeof parsed === 'object' && parsed !== null && 'diagnosis' in parsed) {
36
+ const result = webhookDiagnosisSchema.safeParse(parsed.diagnosis);
37
+ return result.success ? result.data : undefined;
38
+ }
39
+ return undefined;
40
+ }
41
+ catch (error) {
42
+ if (error.code === 'ENOENT')
43
+ return undefined;
44
+ return { operation: WebhookOperation.State };
45
+ }
46
+ }
31
47
  async save(owner, repo, state) {
32
48
  const path = this.path(owner, repo);
33
49
  await mkdir(join(this.root, 'github-webhooks', this.adapter), { recursive: true });
@@ -1,4 +1,5 @@
1
1
  import { createHmac, timingSafeEqual } from 'node:crypto';
2
+ import { WebhookOperation, WebhookProvisioningFailure, renderWebhookDiagnosis, webhookOperation, } from './webhook-diagnosis.js';
2
3
  import { GitHubWebhookStateStore } from './webhook-state.js';
3
4
  const events = new Set([
4
5
  'issues',
@@ -17,18 +18,22 @@ export function createGitHubWebhook(adapter, config, publicUrl, root, client, he
17
18
  for (const repository of config.repositories) {
18
19
  if (!config.webhooks.enabled)
19
20
  continue;
20
- if (endpoint === undefined) {
21
- health.recordFailure(`${repository.owner}/${repository.repo}`, 'webhook', new Error('surfaces.web.publicUrl is not configured'));
22
- continue;
23
- }
24
- try {
25
- await state.serialise(repository.owner, repository.repo, () => provisionRepository(repository, endpoint, state, client, health));
26
- }
27
- catch (error) {
28
- health.recordFailure(`${repository.owner}/${repository.repo}`, 'webhook', error);
29
- }
21
+ await provisionWithDiagnosis(repository, endpoint, state, client, health);
30
22
  }
31
23
  },
24
+ async diagnostics() {
25
+ if (!config.webhooks.enabled)
26
+ return [];
27
+ const messages = await Promise.all(config.repositories.map(async ({ owner, repo }) => {
28
+ const diagnosis = endpoint === undefined
29
+ ? { operation: WebhookOperation.Configure }
30
+ : await state.diagnosis(owner, repo);
31
+ return diagnosis === undefined
32
+ ? undefined
33
+ : renderWebhookDiagnosis(owner + '/' + repo, diagnosis);
34
+ }));
35
+ return messages.filter((message) => message !== undefined);
36
+ },
32
37
  async receive(body, headers, trigger) {
33
38
  if (!config.webhooks.enabled)
34
39
  return 404;
@@ -55,7 +60,8 @@ export function createGitHubWebhook(adapter, config, publicUrl, root, client, he
55
60
  return 401;
56
61
  if (!events.has(event))
57
62
  return 404;
58
- health.recordSuccess(`${owner}/${repo}`, 'webhook');
63
+ if (saved.diagnosis === undefined)
64
+ health.recordSuccess(`${owner}/${repo}`, 'webhook');
59
65
  trigger();
60
66
  return 202;
61
67
  });
@@ -72,42 +78,70 @@ export function createGitHubWebhook(adapter, config, publicUrl, root, client, he
72
78
  },
73
79
  };
74
80
  }
75
- async function provisionRepository(repository, endpoint, state, client, health) {
76
- const scope = `${repository.owner}/${repository.repo}`;
81
+ async function provisionWithDiagnosis(repository, endpoint, state, client, health) {
82
+ const scope = repository.owner + '/' + repository.repo;
77
83
  try {
78
- const saved = await state.load(repository.owner, repository.repo);
79
- if (saved.hookId !== undefined &&
80
- (await updateManagedHook(repository, endpoint, saved, client))) {
81
- return;
82
- }
83
- const hookId = await client.createHook({
84
- ...repository,
85
- url: endpoint,
86
- secret: saved.secret,
87
- events: [...events],
84
+ await state.serialise(repository.owner, repository.repo, async () => {
85
+ try {
86
+ if (endpoint === undefined)
87
+ throw new WebhookProvisioningFailure({ operation: WebhookOperation.Configure });
88
+ await provisionRepository(repository, endpoint, state, client);
89
+ const { diagnosis: _diagnosis, ...saved } = await state.load(repository.owner, repository.repo);
90
+ await state.save(repository.owner, repository.repo, saved);
91
+ health.recordSuccess(scope, 'webhook');
92
+ }
93
+ catch (error) {
94
+ const diagnosis = error instanceof WebhookProvisioningFailure
95
+ ? error.diagnosis
96
+ : { operation: WebhookOperation.State };
97
+ const message = renderWebhookDiagnosis(scope, diagnosis);
98
+ const saved = await state.load(repository.owner, repository.repo);
99
+ await state.save(repository.owner, repository.repo, { ...saved, diagnosis });
100
+ health.recordFailure(scope, 'webhook', new Error(message));
101
+ process.stderr.write(message + '\n');
102
+ }
88
103
  });
89
- await state.save(repository.owner, repository.repo, { ...saved, hookId });
90
104
  }
91
- catch (error) {
92
- health.recordFailure(scope, 'webhook', error);
105
+ catch {
106
+ // Unavailable provider state must not prevent polling from starting.
107
+ const message = renderWebhookDiagnosis(scope, { operation: WebhookOperation.State });
108
+ health.recordFailure(scope, 'webhook', new Error(message));
109
+ process.stderr.write(message + '\n');
93
110
  }
94
111
  }
112
+ async function provisionRepository(repository, endpoint, state, client) {
113
+ const saved = await state.load(repository.owner, repository.repo);
114
+ if (saved.hookId !== undefined &&
115
+ (await updateManagedHook(repository, endpoint, saved, client))) {
116
+ return;
117
+ }
118
+ const hookId = await webhookOperation(WebhookOperation.Create, () => client.createHook({
119
+ ...repository,
120
+ url: endpoint,
121
+ secret: saved.secret,
122
+ events: [...events],
123
+ }));
124
+ await state.save(repository.owner, repository.repo, { ...saved, hookId });
125
+ }
95
126
  async function updateManagedHook(repository, endpoint, saved, client) {
96
- if (saved.hookId === undefined)
127
+ const hookId = saved.hookId;
128
+ if (hookId === undefined)
97
129
  return false;
98
130
  try {
99
- await client.getHook(repository.owner, repository.repo, saved.hookId);
100
- await client.updateHook({
131
+ await webhookOperation(WebhookOperation.Read, () => client.getHook(repository.owner, repository.repo, hookId));
132
+ await webhookOperation(WebhookOperation.Update, () => client.updateHook({
101
133
  ...repository,
102
- hookId: saved.hookId,
134
+ hookId,
103
135
  url: endpoint,
104
136
  secret: saved.secret,
105
137
  events: [...events],
106
- });
138
+ }));
107
139
  return true;
108
140
  }
109
141
  catch (error) {
110
- if (statusOf(error) === 404)
142
+ if (error instanceof WebhookProvisioningFailure &&
143
+ error.diagnosis.operation === WebhookOperation.Read &&
144
+ error.diagnosis.status === 404)
111
145
  return false;
112
146
  throw error;
113
147
  }
@@ -140,14 +174,6 @@ function safeEqual(left, right) {
140
174
  const b = Buffer.from(right);
141
175
  return a.length === b.length && timingSafeEqual(a, b);
142
176
  }
143
- function statusOf(error) {
144
- return typeof error === 'object' &&
145
- error !== null &&
146
- 'status' in error &&
147
- typeof error.status === 'number'
148
- ? error.status
149
- : undefined;
150
- }
151
177
  function webhookEndpoint(publicUrl) {
152
178
  const base = new URL(publicUrl);
153
179
  base.pathname = `${base.pathname.replace(/\/$/, '')}/webhooks/github`;
@@ -67,7 +67,9 @@ export const gitHubProviderDefinition = {
67
67
  if (!config.webhooks.enabled)
68
68
  return [];
69
69
  return [
70
- check.successCount === 0 ? { ...check, status: AdapterHealthStatus.Unknown } : check,
70
+ check.successCount === 0 && check.failureCount === 0
71
+ ? { ...check, status: AdapterHealthStatus.Unknown }
72
+ : check,
71
73
  ];
72
74
  }),
73
75
  webhook: createGitHubWebhook(adapter, config, services.publicUiUrl, services.providerStateRoot, client, health),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.4.83",
3
+ "version": "0.4.84",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {