@atolis-hq/wake 0.2.34 → 0.2.36

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.
@@ -172,6 +172,9 @@ export function createClaudeRunner(options) {
172
172
  projection: input.projection,
173
173
  mode: isResume ? 'resume' : 'start',
174
174
  config: input.config,
175
+ ...(input.promptContextOverrides === undefined
176
+ ? {}
177
+ : { contextOverrides: input.promptContextOverrides }),
175
178
  ...(input.mergeConflictDetected === true ? { mergeConflictDetected: true } : {}),
176
179
  ...(input.upstreamChanges === undefined ? {} : { upstreamChanges: input.upstreamChanges }),
177
180
  });
@@ -215,7 +215,14 @@ export function createCodexRunner(options) {
215
215
  ...(input.workspaceMode === undefined ? {} : { workspaceMode: input.workspaceMode }),
216
216
  ...(input.mergeConflictDetected === true ? { mergeConflictDetected: true } : {}),
217
217
  ...(input.upstreamChanges === undefined ? {} : { upstreamChanges: input.upstreamChanges }),
218
- ...(toolCapabilityNote !== undefined ? { contextOverrides: { toolCapabilityNote } } : {}),
218
+ ...(toolCapabilityNote !== undefined || input.promptContextOverrides !== undefined
219
+ ? {
220
+ contextOverrides: {
221
+ ...input.promptContextOverrides,
222
+ ...(toolCapabilityNote === undefined ? {} : { toolCapabilityNote }),
223
+ },
224
+ }
225
+ : {}),
219
226
  });
220
227
  const model = resolveModel({
221
228
  action: input.action,
@@ -173,7 +173,14 @@ export function createCursorRunner(options) {
173
173
  ...(input.workspaceMode === undefined ? {} : { workspaceMode: input.workspaceMode }),
174
174
  ...(input.mergeConflictDetected === true ? { mergeConflictDetected: true } : {}),
175
175
  ...(input.upstreamChanges === undefined ? {} : { upstreamChanges: input.upstreamChanges }),
176
- ...(toolCapabilityNote !== undefined ? { contextOverrides: { toolCapabilityNote } } : {}),
176
+ ...(toolCapabilityNote !== undefined || input.promptContextOverrides !== undefined
177
+ ? {
178
+ contextOverrides: {
179
+ ...input.promptContextOverrides,
180
+ ...(toolCapabilityNote === undefined ? {} : { toolCapabilityNote }),
181
+ },
182
+ }
183
+ : {}),
177
184
  });
178
185
  const model = resolveModel({ action: input.action, settings: options.settings });
179
186
  const cwd = input.workspacePath ?? options.cwd;
@@ -1,4 +1,5 @@
1
1
  import { defaultAgentIdentity } from '../../domain/schema.js';
2
+ import { alwaysManualIgnoredLabels } from '../../core/policy-engine.js';
2
3
  import { chooseAction, workflowForProjection } from '../../domain/workflows.js';
3
4
  import { branchNameForIssue } from '../git/git-workspace-manager.js';
4
5
  import { loadPromptTemplate, renderPromptTemplate } from './prompt-templates.js';
@@ -165,6 +166,17 @@ export async function buildStagePrompt(input) {
165
166
  isStart: mode === 'start',
166
167
  isResume: mode === 'resume',
167
168
  };
169
+ if (input.action === 'triage-assign' && input.config !== undefined) {
170
+ const ignoredLabels = [
171
+ ...new Set([
172
+ ...alwaysManualIgnoredLabels,
173
+ ...input.config.sources.github.policy.ignoredLabels,
174
+ ]),
175
+ ];
176
+ context.triageIgnoredLabels = ignoredLabels;
177
+ context.triageIgnoredLabelsJson = JSON.stringify(ignoredLabels);
178
+ context.triageReposJson = JSON.stringify(input.config.sources.github.repos);
179
+ }
168
180
  if (resolvedWorkspaceMode === 'branch') {
169
181
  context.branch = branchNameForIssue(input.projection.issue.number);
170
182
  }
@@ -1,6 +1,7 @@
1
1
  import { awaitingApprovalRunnerSentinel, failedRunnerSentinel } from '../domain/stages.js';
2
2
  import { resolveCustomCommand } from '../domain/custom-commands.js';
3
3
  import { builtInDefaultWorkflowDefinition, chooseAction as chooseWorkflowAction, isKnownWorkflowStage, selectWorkflowForEvent, workflowForProjection, } from '../domain/workflows.js';
4
+ export const alwaysManualIgnoredLabels = ['security', 'wake:manual', 'wake:always-manual'];
4
5
  function isAwaitingApproval(issue) {
5
6
  const context = issue.context;
6
7
  return context.lastRunSentinel === awaitingApprovalRunnerSentinel;
@@ -20,6 +21,8 @@ function belowFailureRetryLimit(issue, config) {
20
21
  // quoted reply containing /approved does not approve the gate.
21
22
  const approvedCommandPattern = /^\/approved\b/i;
22
23
  const changesCommandPattern = /^\/changes\b/i;
24
+ const prReviewApprovalMarker = '<!-- wake:pr-review-approved -->';
25
+ const prReviewChangesMarker = '<!-- wake:pr-review-changes-requested -->';
23
26
  // The action Wake runs when a correlated PR gets new reviewer feedback while
24
27
  // the work item is awaiting approval. Not configurable per workflow: it's a
25
28
  // lateral response to a PR surface, not a workflow stage.
@@ -36,7 +39,8 @@ function labelsAndAssigneesQualify(input) {
36
39
  if (input.requiredLabels.some((label) => !labels.has(label))) {
37
40
  return false;
38
41
  }
39
- if (input.ignoredLabels.some((label) => labels.has(label))) {
42
+ const ignoredLabels = new Set([...input.ignoredLabels, ...alwaysManualIgnoredLabels]);
43
+ if ([...ignoredLabels].some((label) => labels.has(label))) {
40
44
  return false;
41
45
  }
42
46
  if (input.requiredAssignees.length > 0 &&
@@ -61,12 +65,25 @@ function latestUnhandledHumanComment(issue) {
61
65
  }
62
66
  return latestHumanComment;
63
67
  }
68
+ function latestUnhandledComment(issue) {
69
+ const context = issue.context;
70
+ const handledCommentId = typeof context.lastHandledCommentId === 'string' ? context.lastHandledCommentId : undefined;
71
+ const lastBotIndex = issue.comments.reduce((acc, c, i) => (c.isBotAuthored ? i : acc), -1);
72
+ const latest = issue.comments.slice(lastBotIndex).at(-1);
73
+ if (latest === undefined || latest.id === handledCommentId) {
74
+ return undefined;
75
+ }
76
+ return latest;
77
+ }
64
78
  export function createPolicyEngine() {
65
79
  return {
66
80
  isEligible(issue, config) {
67
81
  if (issue.issue.state !== 'open') {
68
82
  return false;
69
83
  }
84
+ if (issue.issue.labels.some((label) => alwaysManualIgnoredLabels.includes(label))) {
85
+ return false;
86
+ }
70
87
  const context = issue.context;
71
88
  if (config.workflowSelectors.length > 0) {
72
89
  return typeof context.workflow === 'string';
@@ -154,10 +171,16 @@ export function createPolicyEngine() {
154
171
  // No new human comment since the last handled one; stay idle instead of
155
172
  // falling through to the LLM while awaiting explicit approval feedback.
156
173
  const latestHumanComment = latestUnhandledHumanComment(issue);
157
- if (latestHumanComment === undefined) {
174
+ const latestComment = latestUnhandledComment(issue);
175
+ if (pendingAction === undefined) {
158
176
  return null;
159
177
  }
160
- if (pendingAction === undefined) {
178
+ if (latestComment?.isBotAuthored === true &&
179
+ latestComment.resourceUri !== undefined &&
180
+ latestComment.body.includes(prReviewApprovalMarker)) {
181
+ return { approved: true, pendingAction };
182
+ }
183
+ if (latestHumanComment === undefined) {
161
184
  return null;
162
185
  }
163
186
  const approved = matchesCommand(latestHumanComment.body, approvedCommandPattern);
@@ -183,6 +206,12 @@ export function createPolicyEngine() {
183
206
  return null;
184
207
  }
185
208
  const latestHumanComment = latestUnhandledHumanComment(issue);
209
+ const latestComment = latestUnhandledComment(issue);
210
+ if (latestComment?.isBotAuthored === true &&
211
+ latestComment.resourceUri !== undefined &&
212
+ latestComment.body.includes(prReviewChangesMarker)) {
213
+ return reviewFeedbackAction;
214
+ }
186
215
  // resourceUri is set only on comments folded from a correlated PR/review
187
216
  // surface (schema.ts's commentSnapshotSchema: "absent = the originating
188
217
  // issue thread"). A comment on that surface is itself the deliberate
@@ -240,6 +269,10 @@ export function createPolicyEngine() {
240
269
  return false;
241
270
  }
242
271
  const kind = resourceUri.split(':')[1];
272
+ if (kind === 'schedule') {
273
+ const workflow = unresolved.payload.workflow;
274
+ return typeof workflow === 'string' && config.workflows[workflow] !== undefined;
275
+ }
243
276
  if (config.workflowSelectors.length > 0) {
244
277
  return selectWorkflowForEvent(unresolved, config) !== null;
245
278
  }
@@ -192,6 +192,16 @@ async function applyEvent(current, event, ctx, config) {
192
192
  }
193
193
  if (event.sourceEventType === 'wake.run.completed') {
194
194
  const payload = event.payload;
195
+ if (payload.watcherRun === true) {
196
+ return parseIssueStateRecord({
197
+ ...current,
198
+ wake: {
199
+ ...current.wake,
200
+ syncedAt: event.ingestedAt,
201
+ recentEventIds: [...current.wake.recentEventIds, event.eventId].slice(-10),
202
+ },
203
+ });
204
+ }
195
205
  // Clear the session when the stage moves forward (new action needed) or the
196
206
  // run failed outright. Keep it for BLOCKED so the same action can resume
197
207
  // the in-progress session after a human replies.
@@ -0,0 +1,199 @@
1
+ import { createUnkeyedEventEnvelope } from '../lib/event-log.js';
2
+ import { buildResourceUri } from '../domain/resource-uri.js';
3
+ import { readJsonFile, writeJsonFile } from '../lib/json-file.js';
4
+ import { createWakePaths } from '../lib/paths.js';
5
+ import { isMissingPathError } from '../lib/state-health.js';
6
+ const syntheticRepo = 'wake/internal';
7
+ const syntheticIssueNumber = 1;
8
+ function parseCronField(raw, min, max) {
9
+ const allowed = new Set();
10
+ for (const part of raw.split(',')) {
11
+ const trimmed = part.trim();
12
+ if (trimmed.length === 0) {
13
+ throw new Error(`invalid cron field "${raw}"`);
14
+ }
15
+ const stepMatch = /^(.*)\/(\d+)$/.exec(trimmed);
16
+ const base = stepMatch?.[1] ?? trimmed;
17
+ const step = stepMatch === null ? 1 : Number(stepMatch[2]);
18
+ if (!Number.isInteger(step) || step <= 0) {
19
+ throw new Error(`invalid cron step "${trimmed}"`);
20
+ }
21
+ const range = base === '*'
22
+ ? [min, max]
23
+ : (() => {
24
+ const match = /^(\d+)(?:-(\d+))?$/.exec(base);
25
+ if (match === null) {
26
+ throw new Error(`invalid cron segment "${trimmed}"`);
27
+ }
28
+ const start = Number(match[1]);
29
+ const end = match[2] === undefined ? start : Number(match[2]);
30
+ if (start < min || end > max || start > end) {
31
+ throw new Error(`cron segment "${trimmed}" is outside ${min}-${max}`);
32
+ }
33
+ return [start, end];
34
+ })();
35
+ for (let value = range[0]; value <= range[1]; value += step) {
36
+ allowed.add(value);
37
+ }
38
+ }
39
+ return { matches: (value) => allowed.has(value) };
40
+ }
41
+ function parseCron(cron) {
42
+ const fields = cron.trim().split(/\s+/);
43
+ if (fields.length !== 5) {
44
+ throw new Error(`cron schedule must have five fields: ${cron}`);
45
+ }
46
+ return [
47
+ parseCronField(fields[0], 0, 59),
48
+ parseCronField(fields[1], 0, 23),
49
+ parseCronField(fields[2], 1, 31),
50
+ parseCronField(fields[3], 1, 12),
51
+ parseCronField(fields[4], 0, 6),
52
+ ];
53
+ }
54
+ function matchesCron(date, cron) {
55
+ return (cron[0].matches(date.getUTCMinutes()) &&
56
+ cron[1].matches(date.getUTCHours()) &&
57
+ cron[2].matches(date.getUTCDate()) &&
58
+ cron[3].matches(date.getUTCMonth() + 1) &&
59
+ cron[4].matches(date.getUTCDay()));
60
+ }
61
+ function floorToMinute(date) {
62
+ const copy = new Date(date);
63
+ copy.setUTCSeconds(0, 0);
64
+ return copy;
65
+ }
66
+ export function previousMatchingSlot(input) {
67
+ const parsed = parseCron(input.cron);
68
+ const slot = floorToMinute(input.now);
69
+ const afterMs = input.after === undefined ? undefined : Date.parse(input.after);
70
+ const lookbackMinutes = 366 * 24 * 60;
71
+ for (let checked = 0; checked < lookbackMinutes; checked += 1) {
72
+ const slotIso = slot.toISOString();
73
+ if (afterMs !== undefined && slot.getTime() <= afterMs) {
74
+ return null;
75
+ }
76
+ if (matchesCron(slot, parsed)) {
77
+ return slotIso;
78
+ }
79
+ slot.setUTCMinutes(slot.getUTCMinutes() - 1);
80
+ }
81
+ return null;
82
+ }
83
+ function triggerStateFile(wakeRoot, workflow) {
84
+ return `${createWakePaths(wakeRoot).dataRoot}/triggers/${workflow}.json`;
85
+ }
86
+ async function readTriggerState(wakeRoot, workflow) {
87
+ try {
88
+ const raw = await readJsonFile(triggerStateFile(wakeRoot, workflow));
89
+ if (raw === null || typeof raw !== 'object') {
90
+ return null;
91
+ }
92
+ const record = raw;
93
+ return record.schemaVersion === 1 &&
94
+ record.workflow === workflow &&
95
+ typeof record.lastFiredSlot === 'string' &&
96
+ typeof record.updatedAt === 'string'
97
+ ? {
98
+ schemaVersion: 1,
99
+ workflow,
100
+ lastFiredSlot: record.lastFiredSlot,
101
+ updatedAt: record.updatedAt,
102
+ }
103
+ : null;
104
+ }
105
+ catch (error) {
106
+ if (isMissingPathError(error)) {
107
+ return null;
108
+ }
109
+ throw error;
110
+ }
111
+ }
112
+ function scheduledEventId(workflow, slot) {
113
+ return `scheduled-workflow-${workflow}-${slot.replace(/[^a-z0-9]+/gi, '-')}`;
114
+ }
115
+ function scheduledResourceUri(workflow, slot) {
116
+ return buildResourceUri('wake', 'schedule', `${workflow}@${slot}`);
117
+ }
118
+ function syntheticTicket(input) {
119
+ const eventId = scheduledEventId(input.workflow, input.slot);
120
+ const resourceUri = scheduledResourceUri(input.workflow, input.slot);
121
+ const url = `https://wake.local/schedules/${encodeURIComponent(input.workflow)}/${encodeURIComponent(input.slot)}`;
122
+ return createUnkeyedEventEnvelope({
123
+ eventId,
124
+ streamScope: 'global-intake',
125
+ direction: 'inbound',
126
+ sourceSystem: 'wake',
127
+ sourceEventType: 'ticket.upsert',
128
+ sourceRefs: {
129
+ repo: syntheticRepo,
130
+ issueNumber: syntheticIssueNumber,
131
+ sourceUrl: url,
132
+ resourceUri,
133
+ },
134
+ occurredAt: input.slot,
135
+ ingestedAt: input.now,
136
+ trigger: 'immediate',
137
+ payload: {
138
+ ticket: {
139
+ repo: syntheticRepo,
140
+ number: syntheticIssueNumber,
141
+ title: `Scheduled workflow: ${input.workflow}`,
142
+ body: `Wake fired scheduled workflow "${input.workflow}" for cron slot ${input.slot}.`,
143
+ labels: ['wake:scheduled-workflow', `wake:workflow.${input.workflow}`],
144
+ assignees: [],
145
+ isPullRequest: false,
146
+ state: 'open',
147
+ url,
148
+ createdAt: input.slot,
149
+ updatedAt: input.slot,
150
+ },
151
+ workflow: input.workflow,
152
+ trigger: {
153
+ kind: 'schedule',
154
+ slot: input.slot,
155
+ idempotencyKey: eventId,
156
+ },
157
+ providerEventType: 'wake.schedule.fired',
158
+ },
159
+ derivedHints: {
160
+ workflow: input.workflow,
161
+ },
162
+ });
163
+ }
164
+ export function createScheduledWorkflowSource(deps) {
165
+ return {
166
+ async pollEvents() {
167
+ const events = [];
168
+ const now = deps.now();
169
+ const nowIso = now.toISOString();
170
+ for (const [workflow, definition] of Object.entries(deps.config.workflows)) {
171
+ const cron = definition.trigger?.schedule?.cron;
172
+ if (cron === undefined) {
173
+ continue;
174
+ }
175
+ const state = await readTriggerState(deps.config.paths.wakeRoot, workflow);
176
+ const slot = previousMatchingSlot({
177
+ cron,
178
+ now,
179
+ ...(state?.lastFiredSlot === undefined ? {} : { after: state.lastFiredSlot }),
180
+ });
181
+ if (slot === null) {
182
+ continue;
183
+ }
184
+ const eventId = scheduledEventId(workflow, slot);
185
+ if ((await deps.stateStore.readEventEnvelope(eventId)) !== null) {
186
+ await writeJsonFile(triggerStateFile(deps.config.paths.wakeRoot, workflow), {
187
+ schemaVersion: 1,
188
+ workflow,
189
+ lastFiredSlot: slot,
190
+ updatedAt: nowIso,
191
+ });
192
+ continue;
193
+ }
194
+ events.push(syntheticTicket({ workflow, slot, now: nowIso }));
195
+ }
196
+ return events;
197
+ },
198
+ };
199
+ }
@@ -1,22 +1,27 @@
1
+ import { join } from 'node:path';
1
2
  import { createLifecycleService } from './lifecycle-service.js';
2
3
  import { createPolicyEngine } from './policy-engine.js';
3
4
  import { createProjectionUpdater } from './projection-updater.js';
4
5
  import { acquireFileLock } from '../lib/lock.js';
5
- import { CORRELATION_REGISTERED_EVENT, parseRunnerArtifacts, parseRunnerResult, } from '../domain/schema.js';
6
+ import { CORRELATION_REGISTERED_EVENT, CORRELATION_PRIMARY_CONFLICT_EVENT, parseRunnerArtifacts, parseRunnerResult, } from '../domain/schema.js';
6
7
  import { maxConfiguredRunnerTimeoutMs, resolveRunnerRouting } from '../domain/runner-routing.js';
7
8
  import { awaitingApprovalRunnerSentinel, stageLabelForStage } from '../domain/stages.js';
8
- import { chooseAction as chooseWorkflowAction, isKnownWorkflowStage, workflowChangedBlockReason, workflowForProjection, workflowLabelForWorkflowName, workflowNameForProjection, } from '../domain/workflows.js';
9
+ import { chooseAction as chooseWorkflowAction, entryStage as workflowEntryStage, isKnownWorkflowStage, workflowChangedBlockReason, workflowForProjection, workflowLabelForWorkflowName, workflowNameForProjection, } from '../domain/workflows.js';
9
10
  import { createEventEnvelope } from '../lib/event-log.js';
10
11
  import { branchNameForIssue } from '../domain/branch-naming.js';
11
12
  import { customCommandWorkspace, isCustomCommandAction } from '../domain/custom-commands.js';
12
13
  import { resolveQuotaPauseUntil } from './quota-backoff.js';
13
14
  import { createLabelsEvent, createPublishIntentEvent } from './event-builders.js';
15
+ import { previousMatchingSlot } from './scheduled-workflow-source.js';
14
16
  import { createOutbox } from './outbox.js';
15
17
  import { createEventResolver } from './event-resolver.js';
16
18
  import { createStaleRunReconciler } from './stale-run-reconciler.js';
17
19
  import { createWorkspaceCleanup } from './workspace-cleanup.js';
18
20
  import { createRunLease, isRunLeaseExpired, renewRunLease, runLeaseRenewalIntervalMs, } from './run-lease.js';
19
21
  import { currentProcessIdentity, processIdentityMatches } from '../lib/process-identity.js';
22
+ import { readJsonFile, writeJsonFile } from '../lib/json-file.js';
23
+ import { isMissingPathError } from '../lib/state-health.js';
24
+ const prReviewApprovalMarker = '<!-- wake:pr-review-approved -->';
20
25
  function latestHumanCommentId(candidate) {
21
26
  const human = candidate.comments.filter((c) => !c.isBotAuthored);
22
27
  return human.at(-1)?.id;
@@ -317,6 +322,201 @@ export function createTickRunner(deps) {
317
322
  await lock.release();
318
323
  }
319
324
  }
325
+ function watcherStatus(projection) {
326
+ const sentinel = projection.context.lastRunSentinel;
327
+ if (sentinel === 'AWAITING_APPROVAL')
328
+ return 'awaiting-approval';
329
+ if (sentinel === 'BLOCKED')
330
+ return 'blocked';
331
+ if (sentinel === 'FAILED')
332
+ return 'failed';
333
+ if (sentinel === 'DONE')
334
+ return 'done';
335
+ return 'pending';
336
+ }
337
+ function watcherKey(input) {
338
+ return [
339
+ input.workItemKey,
340
+ input.parentWorkflowName,
341
+ input.parentStage,
342
+ String(input.watcherIndex),
343
+ ]
344
+ .join('__')
345
+ .replace(/[^A-Za-z0-9._-]/g, '_');
346
+ }
347
+ function watcherStateFile(key) {
348
+ return join(deps.stateStore.paths.dataRoot, 'watchers', `${key}.json`);
349
+ }
350
+ async function readWatcherState(key) {
351
+ try {
352
+ const raw = await readJsonFile(watcherStateFile(key));
353
+ if (raw === null || typeof raw !== 'object')
354
+ return null;
355
+ const record = raw;
356
+ return record.schemaVersion === 1 &&
357
+ record.key === key &&
358
+ typeof record.updatedAt === 'string'
359
+ ? {
360
+ schemaVersion: 1,
361
+ key,
362
+ ...(typeof record.lastDispatchedEventId === 'string'
363
+ ? { lastDispatchedEventId: record.lastDispatchedEventId }
364
+ : {}),
365
+ ...(typeof record.lastDispatchedSlot === 'string'
366
+ ? { lastDispatchedSlot: record.lastDispatchedSlot }
367
+ : {}),
368
+ updatedAt: record.updatedAt,
369
+ }
370
+ : null;
371
+ }
372
+ catch (error) {
373
+ if (isMissingPathError(error))
374
+ return null;
375
+ throw error;
376
+ }
377
+ }
378
+ async function writeWatcherState(key, patch) {
379
+ const current = await readWatcherState(key);
380
+ await writeJsonFile(watcherStateFile(key), {
381
+ schemaVersion: 1,
382
+ key,
383
+ ...(current?.lastDispatchedEventId === undefined
384
+ ? {}
385
+ : { lastDispatchedEventId: current.lastDispatchedEventId }),
386
+ ...(current?.lastDispatchedSlot === undefined
387
+ ? {}
388
+ : { lastDispatchedSlot: current.lastDispatchedSlot }),
389
+ ...patch,
390
+ updatedAt: deps.clock.now().toISOString(),
391
+ });
392
+ }
393
+ async function nextWatcherDispatch(projections, now) {
394
+ for (const projection of projections) {
395
+ const parentWorkflow = workflowForProjection(projection, deps.config);
396
+ if (parentWorkflow === null)
397
+ continue;
398
+ const parentWorkflowName = workflowNameForProjection(projection, deps.config);
399
+ const stage = parentWorkflow.stages[projection.wake.stage];
400
+ if (stage === undefined)
401
+ continue;
402
+ for (const [watcherIndex, watcher] of (stage.watch ?? []).entries()) {
403
+ if (!watcher.while.status.includes(watcherStatus(projection)))
404
+ continue;
405
+ const key = watcherKey({
406
+ workItemKey: projection.workItemKey,
407
+ parentWorkflowName,
408
+ parentStage: projection.wake.stage,
409
+ watcherIndex,
410
+ });
411
+ const state = await readWatcherState(key);
412
+ if (watcher.on !== undefined) {
413
+ const events = await deps.stateStore.listRecentEventEnvelopes({
414
+ workItemKey: projection.workItemKey,
415
+ direction: 'internal',
416
+ limit: 200,
417
+ });
418
+ const matchingEvents = events
419
+ .filter((event) => watcher.on.event.includes(event.sourceEventType))
420
+ .sort((left, right) => left.ingestedAt.localeCompare(right.ingestedAt));
421
+ const cursorIndex = state?.lastDispatchedEventId === undefined
422
+ ? -1
423
+ : matchingEvents.findIndex((event) => event.eventId === state.lastDispatchedEventId);
424
+ const newest = matchingEvents.slice(cursorIndex + 1).at(-1);
425
+ if (newest !== undefined) {
426
+ return {
427
+ projection,
428
+ parentWorkflowName,
429
+ parentStage: projection.wake.stage,
430
+ watcherIndex,
431
+ targetWorkflowName: watcher.workflow,
432
+ trigger: { kind: 'event', eventId: newest.eventId },
433
+ };
434
+ }
435
+ }
436
+ if (watcher.schedule !== undefined) {
437
+ const slot = previousMatchingSlot({
438
+ cron: watcher.schedule.cron,
439
+ now,
440
+ ...(state?.lastDispatchedSlot === undefined ? {} : { after: state.lastDispatchedSlot }),
441
+ });
442
+ if (slot !== null) {
443
+ return {
444
+ projection,
445
+ parentWorkflowName,
446
+ parentStage: projection.wake.stage,
447
+ watcherIndex,
448
+ targetWorkflowName: watcher.workflow,
449
+ trigger: { kind: 'schedule', slot },
450
+ };
451
+ }
452
+ }
453
+ }
454
+ }
455
+ return null;
456
+ }
457
+ async function resolvePrReviewTarget(input) {
458
+ if (deps.artifactVerifier === undefined) {
459
+ return null;
460
+ }
461
+ const { artifacts } = parseRunnerArtifacts(input.runnerResult.result);
462
+ for (const artifact of artifacts) {
463
+ const verified = await deps.artifactVerifier.verify(artifact, {
464
+ branch: branchNameForIssue(input.projection.issue.number),
465
+ repo: input.projection.issue.repo,
466
+ });
467
+ if (verified === null) {
468
+ continue;
469
+ }
470
+ const incumbent = await deps.resourceIndex.resolve(verified.resourceUri);
471
+ if (incumbent !== undefined && incumbent !== input.projection.workItemKey) {
472
+ const conflict = createEventEnvelope({
473
+ eventId: `${input.runId}-pr-review-primary-conflict`,
474
+ workItemKey: input.projection.workItemKey,
475
+ streamScope: 'work-item',
476
+ direction: 'internal',
477
+ sourceSystem: 'wake',
478
+ sourceEventType: CORRELATION_PRIMARY_CONFLICT_EVENT,
479
+ sourceRefs: { runId: input.runId },
480
+ occurredAt: input.occurredAt,
481
+ ingestedAt: input.occurredAt,
482
+ trigger: 'context-only',
483
+ payload: {
484
+ resourceUri: verified.resourceUri,
485
+ incumbentWorkItemKey: incumbent,
486
+ },
487
+ });
488
+ const appended = await deps.stateStore.appendEventEnvelope(conflict);
489
+ await projectionUpdater.rebuildFromEvents([appended]);
490
+ return null;
491
+ }
492
+ if (incumbent === undefined) {
493
+ const event = createEventEnvelope({
494
+ eventId: `${input.runId}-pr-review-artifact-${verified.resourceUri.replace(/[^a-z0-9]+/gi, '-')}`,
495
+ workItemKey: input.projection.workItemKey,
496
+ streamScope: 'work-item',
497
+ direction: 'internal',
498
+ sourceSystem: 'wake',
499
+ sourceEventType: CORRELATION_REGISTERED_EVENT,
500
+ sourceRefs: { runId: input.runId },
501
+ occurredAt: input.occurredAt,
502
+ ingestedAt: input.occurredAt,
503
+ trigger: 'context-only',
504
+ payload: {
505
+ resourceUri: verified.resourceUri,
506
+ role: 'implementation',
507
+ relation: 'primary',
508
+ provenance: 'agent-reported',
509
+ registeredBy: input.runId,
510
+ idempotencyKey: `${input.runId}:pr-review-artifact-registration:${verified.resourceUri}`,
511
+ },
512
+ });
513
+ const appended = await deps.stateStore.appendEventEnvelope(event);
514
+ await projectionUpdater.rebuildFromEvents([appended]);
515
+ }
516
+ return verified.resourceUri;
517
+ }
518
+ return null;
519
+ }
320
520
  async function runRunnerTick() {
321
521
  const lock = await acquireFileLock(deps.stateStore.paths.runnerLockFile);
322
522
  if (!lock.acquired) {
@@ -335,7 +535,12 @@ export function createTickRunner(deps) {
335
535
  if (await parkConfigDriftedProjections(projections)) {
336
536
  return { status: 'processed' };
337
537
  }
338
- let candidate = projections.find((issue) => policy.resolveNextEligibleAction(issue, deps.config) !== null);
538
+ const watcherDispatch = await nextWatcherDispatch(projections, tickStartedAt);
539
+ let candidate = watcherDispatch?.projection;
540
+ let watcherStateKeyForRun;
541
+ let watcherTriggerForRun;
542
+ const watcherRun = watcherDispatch !== null;
543
+ candidate ??= projections.find((issue) => policy.resolveNextEligibleAction(issue, deps.config) !== null);
339
544
  if (candidate === undefined) {
340
545
  return { status: 'idle' };
341
546
  }
@@ -357,19 +562,41 @@ export function createTickRunner(deps) {
357
562
  candidate = (await deps.stateStore.readIssueState(candidate.workItemKey)) ?? candidate;
358
563
  }
359
564
  }
360
- if (policy.resolveNextEligibleAction(candidate, deps.config) === null) {
565
+ if (!watcherRun && policy.resolveNextEligibleAction(candidate, deps.config) === null) {
361
566
  return { status: 'idle' };
362
567
  }
363
568
  const workflow = workflowForProjection(candidate, deps.config);
364
569
  if (workflow === null) {
365
570
  return { status: 'idle' };
366
571
  }
367
- const workflowName = workflowNameForProjection(candidate, deps.config);
572
+ let workflowName = workflowNameForProjection(candidate, deps.config);
368
573
  let action;
369
574
  let command;
370
575
  let claimedStage = candidate.wake.stage;
371
576
  let workspaceMode = 'none';
372
- if (isAwaitingApproval(candidate)) {
577
+ if (watcherDispatch !== null) {
578
+ const targetWorkflow = deps.config.workflows[watcherDispatch.targetWorkflowName];
579
+ if (targetWorkflow === undefined) {
580
+ return { status: 'idle' };
581
+ }
582
+ const entryStageName = workflowEntryStage(targetWorkflow);
583
+ const entryStage = targetWorkflow.stages[entryStageName];
584
+ if (entryStage === undefined) {
585
+ return { status: 'idle' };
586
+ }
587
+ action = entryStage.action ?? entryStageName;
588
+ claimedStage = entryStageName;
589
+ workspaceMode = entryStage.workspace;
590
+ workflowName = watcherDispatch.targetWorkflowName;
591
+ watcherStateKeyForRun = watcherKey({
592
+ workItemKey: watcherDispatch.projection.workItemKey,
593
+ parentWorkflowName: watcherDispatch.parentWorkflowName,
594
+ parentStage: watcherDispatch.parentStage,
595
+ watcherIndex: watcherDispatch.watcherIndex,
596
+ });
597
+ watcherTriggerForRun = watcherDispatch.trigger;
598
+ }
599
+ else if (isAwaitingApproval(candidate)) {
373
600
  const customCommandRequest = policy.resolveCustomCommandRequest(candidate, deps.config);
374
601
  if (customCommandRequest !== null) {
375
602
  action = customCommandRequest.action;
@@ -508,6 +735,13 @@ export function createTickRunner(deps) {
508
735
  workerProcessStartedAt: workerIdentity.processStartedAt,
509
736
  metadata: {
510
737
  sourceRevision,
738
+ ...(watcherRun
739
+ ? {
740
+ watcher: true,
741
+ watcherWorkflow: workflowName,
742
+ watcherTrigger: watcherTriggerForRun,
743
+ }
744
+ : {}),
511
745
  },
512
746
  };
513
747
  await deps.stateStore.writeRunRecord(runningRecord);
@@ -536,13 +770,19 @@ export function createTickRunner(deps) {
536
770
  payload: {
537
771
  action,
538
772
  priorStage: candidate.wake.stage,
539
- claimedStage,
773
+ ...(watcherRun ? {} : { claimedStage }),
540
774
  sourceRevision,
775
+ ...(watcherRun ? { watcherRun: true, watcherTrigger: watcherTriggerForRun } : {}),
541
776
  },
542
777
  });
543
778
  await deps.stateStore.appendEventEnvelope(claimedEvent);
544
779
  await transitionRunLifecycle('CLAIMED');
545
780
  await projectionUpdater.rebuildFromEvents([claimedEvent]);
781
+ if (watcherStateKeyForRun !== undefined && watcherTriggerForRun !== undefined) {
782
+ await writeWatcherState(watcherStateKeyForRun, watcherTriggerForRun.kind === 'event'
783
+ ? { lastDispatchedEventId: watcherTriggerForRun.eventId }
784
+ : { lastDispatchedSlot: watcherTriggerForRun.slot });
785
+ }
546
786
  await deliverOutboundEvent(createLabelsEvent({
547
787
  projection: candidate,
548
788
  runId,
@@ -594,14 +834,32 @@ export function createTickRunner(deps) {
594
834
  const recentEvents = await deps.stateStore.listEventEnvelopesForWorkItem(candidate.workItemKey, 6);
595
835
  await transitionRunLifecycle('RUNNING');
596
836
  startLeaseRenewal();
837
+ const runnerProjection = watcherRun
838
+ ? {
839
+ ...candidate,
840
+ wake: {
841
+ ...candidate.wake,
842
+ sessionId: undefined,
843
+ sessionCli: undefined,
844
+ },
845
+ }
846
+ : candidate;
597
847
  const runnerResult = await deps.runner.run({
598
848
  action,
599
- projection: candidate,
849
+ projection: runnerProjection,
600
850
  recentEvents,
601
851
  config: deps.config,
602
852
  runId,
603
853
  routing,
604
854
  workspaceMode,
855
+ ...(action === 'triage-assign'
856
+ ? {
857
+ promptContextOverrides: {
858
+ triageCapacityAvailable: true,
859
+ triageWipCapDescription: 'Wake has no active running work item at triage start; do not assign more than one issue.',
860
+ },
861
+ }
862
+ : {}),
605
863
  ...(workspacePath === undefined ? {} : { workspacePath }),
606
864
  ...(mergeConflictDetected ? { mergeConflictDetected: true } : {}),
607
865
  ...(upstreamChanges === undefined ? {} : { upstreamChanges }),
@@ -631,7 +889,16 @@ export function createTickRunner(deps) {
631
889
  ? null
632
890
  : lifecycle.nextStageFromSentinel(claimedStage, sentinel, workflow);
633
891
  const finishedAt = deps.clock.now().toISOString();
634
- if (workspaceMode === 'branch') {
892
+ let prReviewTargetResourceUri = null;
893
+ if (watcherRun && action === 'pr-review') {
894
+ prReviewTargetResourceUri = await resolvePrReviewTarget({
895
+ projection: candidate,
896
+ runId,
897
+ runnerResult,
898
+ occurredAt: finishedAt,
899
+ });
900
+ }
901
+ else if (workspaceMode === 'branch') {
635
902
  await registerReportedArtifacts({
636
903
  projection: candidate,
637
904
  runId,
@@ -756,21 +1023,24 @@ export function createTickRunner(deps) {
756
1023
  envelope: parsedRunnerResult.envelope,
757
1024
  executionOutcome,
758
1025
  ...(workflowOutcome !== undefined ? { workflowOutcome } : {}),
1026
+ ...(watcherRun ? { watcherRun: true, watcherTrigger: watcherTriggerForRun } : {}),
759
1027
  },
760
1028
  });
761
1029
  await deps.stateStore.appendEventEnvelope(runCompletedEvent);
762
1030
  await projectionUpdater.rebuildFromEvents([runCompletedEvent]);
763
- await deliverOutboundEvent(createLabelsEvent({
764
- projection: candidate,
765
- runId,
766
- statusLabel: statusLabelForOutcome({
767
- sentinel,
768
- stage: nextStage ?? claimedStage,
769
- }),
770
- stageLabel: stageLabelForStage(nextStage ?? claimedStage),
771
- workflowLabel: workflowLabelForWorkflowName(workflowName),
772
- occurredAt: finishedAt,
773
- }));
1031
+ if (!watcherRun) {
1032
+ await deliverOutboundEvent(createLabelsEvent({
1033
+ projection: candidate,
1034
+ runId,
1035
+ statusLabel: statusLabelForOutcome({
1036
+ sentinel,
1037
+ stage: nextStage ?? claimedStage,
1038
+ }),
1039
+ stageLabel: stageLabelForStage(nextStage ?? claimedStage),
1040
+ workflowLabel: workflowLabelForWorkflowName(workflowName),
1041
+ occurredAt: finishedAt,
1042
+ }));
1043
+ }
774
1044
  const publishIntent = createPublishIntentEvent({
775
1045
  projection: candidate,
776
1046
  runId,
@@ -785,7 +1055,32 @@ export function createTickRunner(deps) {
785
1055
  ? { previousFailureClass: candidate.context.lastFailureClass }
786
1056
  : {}),
787
1057
  });
788
- if (shouldPublishRunResult({
1058
+ if (watcherRun && action === 'pr-review') {
1059
+ if (prReviewTargetResourceUri !== null &&
1060
+ (sentinel === 'DONE' || sentinel === 'FAILED')) {
1061
+ await deliverOutboundEvent({
1062
+ ...publishIntent,
1063
+ sourceRefs: {
1064
+ ...publishIntent.sourceRefs,
1065
+ resourceUri: prReviewTargetResourceUri,
1066
+ },
1067
+ payload: {
1068
+ ...publishIntent.payload,
1069
+ kind: sentinel === 'DONE' ? 'approval-request' : 'status-update',
1070
+ body: sentinel === 'DONE'
1071
+ ? `${parsedRunnerResult.body}\n\n${prReviewApprovalMarker}`
1072
+ : `${parsedRunnerResult.body}\n\n<!-- wake:pr-review-changes-requested -->`,
1073
+ idempotencyKey: `${runId}:pr-review-verdict-comment`,
1074
+ },
1075
+ });
1076
+ }
1077
+ else {
1078
+ await suppressOutboundEvent(publishIntent, {
1079
+ suppressedPublishReason: 'pr-review-no-actionable-verdict',
1080
+ });
1081
+ }
1082
+ }
1083
+ else if (shouldPublishRunResult({
789
1084
  failureClass: runnerResult.failureClass,
790
1085
  previousFailureClass: candidate.context.lastFailureClass,
791
1086
  })) {
@@ -371,15 +371,36 @@ const runnerHealthEntrySchema = z.object({
371
371
  lastFailureAt: isoTimestampSchema.optional(),
372
372
  });
373
373
  const workflowWorkspaceSchema = z.enum(['none', 'read-only', 'branch']);
374
+ const workflowTriggerScheduleSchema = z.object({
375
+ cron: z.string().min(1),
376
+ });
374
377
  const workflowStageSchema = stageRouteSchema.extend({
375
378
  workspace: workflowWorkspaceSchema,
376
379
  onDone: identifierSchema,
380
+ watch: z
381
+ .array(z.object({
382
+ while: z.object({
383
+ status: z.array(z.string().min(1)).min(1),
384
+ }),
385
+ on: z
386
+ .object({
387
+ event: z.array(z.string().min(1)).min(1),
388
+ })
389
+ .optional(),
390
+ schedule: workflowTriggerScheduleSchema.optional(),
391
+ workflow: identifierSchema,
392
+ }))
393
+ .optional(),
394
+ });
395
+ const workflowTriggerSchema = z.object({
396
+ schedule: workflowTriggerScheduleSchema.optional(),
377
397
  });
378
398
  const customCommandSchema = stageRouteSchema.extend({
379
399
  workspace: workflowWorkspaceSchema.default('read-only'),
380
400
  });
381
401
  const workflowDefinitionSchema = z.object({
382
402
  entryStage: identifierSchema.optional(),
403
+ trigger: workflowTriggerSchema.optional(),
383
404
  stages: z.record(identifierSchema, workflowStageSchema),
384
405
  });
385
406
  const workflowSelectorMatchSchema = z
@@ -605,6 +626,21 @@ const wakeConfigBaseSchema = z.object({
605
626
  },
606
627
  },
607
628
  },
629
+ triage: {
630
+ trigger: {
631
+ schedule: {
632
+ cron: '*/10 * * * *',
633
+ },
634
+ },
635
+ stages: {
636
+ assign: {
637
+ action: 'triage-assign',
638
+ workspace: 'none',
639
+ tier: 'light',
640
+ onDone: 'done',
641
+ },
642
+ },
643
+ },
608
644
  }),
609
645
  workflowSelectors: z.array(workflowSelectorSchema).default([]),
610
646
  commands: z.record(identifierSchema, customCommandSchema).default({
@@ -809,6 +845,22 @@ export const wakeConfigSchema = wakeConfigBaseSchema.superRefine((config, ctx) =
809
845
  message: `Workflow stage "${stageName}" omits action but no prompts/${stageName}.md template exists.`,
810
846
  });
811
847
  }
848
+ for (const [index, watcher] of (stage.watch ?? []).entries()) {
849
+ if (watcher.on === undefined && watcher.schedule === undefined) {
850
+ ctx.addIssue({
851
+ code: z.ZodIssueCode.custom,
852
+ path: ['workflows', workflowName, 'stages', stageName, 'watch', index],
853
+ message: 'Watcher must declare at least one of on or schedule.',
854
+ });
855
+ }
856
+ if (config.workflows[watcher.workflow] === undefined) {
857
+ ctx.addIssue({
858
+ code: z.ZodIssueCode.custom,
859
+ path: ['workflows', workflowName, 'stages', stageName, 'watch', index, 'workflow'],
860
+ message: `Watcher targets unknown workflow "${watcher.workflow}".`,
861
+ });
862
+ }
863
+ }
812
864
  }
813
865
  if (actualEntryStage !== undefined &&
814
866
  stageSet.has(actualEntryStage) &&
@@ -102,6 +102,10 @@ function selectorMatches(selector, input) {
102
102
  return true;
103
103
  }
104
104
  export function selectWorkflowForEvent(event, config) {
105
+ const hintedWorkflow = event.payload.workflow;
106
+ if (typeof hintedWorkflow === 'string' && config.workflows[hintedWorkflow] !== undefined) {
107
+ return hintedWorkflow;
108
+ }
105
109
  const input = workflowSelectorInputFromEvent(event);
106
110
  const selector = config.workflowSelectors.find((candidate) => selectorMatches(candidate, input));
107
111
  return selector?.workflow ?? null;
package/dist/src/main.js CHANGED
@@ -31,6 +31,7 @@ import { loadWakeConfig } from './config/load-config.js';
31
31
  import { createActiveRunRecovery } from './core/active-run-recovery.js';
32
32
  import { createControlPlane } from './core/control-plane.js';
33
33
  import { createOutboundSinkRouter, createWorkSourceFanIn } from './core/sink-router.js';
34
+ import { createScheduledWorkflowSource } from './core/scheduled-workflow-source.js';
34
35
  import { createTickRunner } from './core/tick-runner.js';
35
36
  import { systemClock } from './lib/clock.js';
36
37
  import { createDetachedProcessLogSink } from './lib/detached-process-logging.js';
@@ -521,6 +522,14 @@ export async function buildRuntime(args) {
521
522
  })
522
523
  : null;
523
524
  const workSource = createWorkSourceFanIn([
525
+ {
526
+ source: 'wake-schedule',
527
+ pollEvents: createScheduledWorkflowSource({
528
+ config,
529
+ stateStore,
530
+ now: () => systemClock.now(),
531
+ }).pollEvents,
532
+ },
524
533
  {
525
534
  source: sourceName,
526
535
  pollEvents: ticketingSystem.pollEvents,
@@ -124,4 +124,4 @@ export function resolveWakeVersion(options = {}) {
124
124
  }
125
125
  return '0.1.0-dev';
126
126
  }
127
- export const wakeVersion = "gacd501e";
127
+ export const wakeVersion = "g45bda3a";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.2.34",
3
+ "version": "0.2.36",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -0,0 +1,29 @@
1
+ ---
2
+ permissionMode: default
3
+ allowedTools: Bash(gh pr view *), Bash(gh pr diff *), Bash(gh pr checks *), Bash(gh run view *), Bash(gh api repos/*/pulls/*), Bash(gh api repos/*/commits/*), Bash(git status), Bash(git log *), Bash(git diff *), Read, Glob, Grep
4
+ maxTurns: 8
5
+ skipApproval: true
6
+ ---
7
+ You are Wake, in the PR-REVIEW workflow for work item {{workItemKey}}.
8
+
9
+ Objective:
10
+ - Identify the pull request for this work item using read-only GitHub commands.
11
+ - Review the PR's diff, tests/checks, and surrounding code for correctness.
12
+ - Report the PR you examined using a `wake-artifacts` block.
13
+ - End with the Wake result envelope.
14
+
15
+ Verdict mapping:
16
+ - Use `DONE` only when you are confident the PR is safe to merge.
17
+ - Use `FAILED` when the PR needs changes; explain the required changes clearly.
18
+ - Use `BLOCKED` when you cannot determine a safe verdict.
19
+
20
+ Safety rules:
21
+ - Do not merge, approve via GitHub review, enable auto-merge, edit labels, push commits, or perform any administrative GitHub mutation.
22
+ - Do not use any `gh` subcommand other than the read-only commands allowed for this prompt.
23
+ - If you cannot find exactly one plausible PR for this work item, report `BLOCKED`.
24
+ - If the PR you reviewed is not reported in the `wake-artifacts` block, Wake will ignore the verdict.
25
+
26
+ Artifact reporting:
27
+ ```wake-artifacts
28
+ { "artifacts": [{ "kind": "pr", "url": "<the PR URL>" }] }
29
+ ```
@@ -0,0 +1,33 @@
1
+ ---
2
+ stage: assign
3
+ permissionMode: acceptEdits
4
+ allowedTools: Bash(gh *), Bash(jq *), Read
5
+ extraArgs:
6
+ maxTurns: 30
7
+ skipApproval: true
8
+ ---
9
+
10
+ You are Wake, in the TRIAGE ASSIGN stage for {{workItemKey}}.
11
+
12
+ This stage may inspect the broad GitHub backlog and assign at most one issue to Wake.
13
+ {{toolCapabilityNote}}
14
+
15
+ Wake-side guardrails for this run:
16
+
17
+ - Capacity available: {{triageCapacityAvailable}}
18
+ - Do not assign more than one issue.
19
+ - Do not inspect or assign issues carrying any of these always-manual labels:
20
+ {{triageIgnoredLabelsJson}}
21
+ - Configured repositories:
22
+ {{triageReposJson}}
23
+
24
+ Use `gh issue list` and `gh issue view` only against the configured repositories.
25
+ Filter out every always-manual label in the GitHub query before viewing candidate
26
+ details. If no suitable issue remains, report DONE without assigning anything.
27
+
28
+ When you choose a candidate, assign it to the authenticated Wake GitHub user with
29
+ `gh issue edit <number> --repo <owner/repo> --add-assignee @me`.
30
+
31
+ Wake will provide the schedule trigger item below in a delimited untrusted data
32
+ block. It is an audit record, not the backlog to triage.
33
+