Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
workflow-parser.ts20.2 KB · 651 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/**
 * Minimal GitHub-Actions-compatible workflow YAML parser.
 *
 * Block C1. Pure function — no DB, no file I/O, no external calls.
 * Input: YAML text. Output: normalised workflow object, or error.
 *
 * Supported subset:
 *   name: <scalar>
 *   on: <scalar> | [list] | { mapping }     (mapping is flattened to its top-level keys)
 *   jobs:
 *     <job-key>:
 *       runs-on: <scalar>                   (default "default")
 *       steps:
 *         - run: <scalar>                   (auto-name "Run command")
 *         - name: <scalar>
 *           run: <scalar>
 *
 * Quirks handled:
 *   - `#` comments (end-of-line and full-line)
 *   - Block literal strings (`|` and `>`) with indentation-stripped bodies
 *   - Inline flow arrays: [a, b, "c, still c"]
 *   - Inline flow mappings: { push: { branches: [main] } }
 *   - Single- and double-quoted scalars
 *   - Extra fields on jobs/steps (env, uses, with, matrix, …) are accepted and ignored
 *   - Job key order is preserved (Map-based accumulation)
 *   - Never throws — bad input returns { ok: false, error }
 */

export type WorkflowStep = {
  name: string;
  run: string;
};

export type WorkflowJob = {
  name: string;
  runsOn: string;
  steps: WorkflowStep[];
};

export type ParsedWorkflow = {
  name: string;
  on: string[];
  /**
   * Cron expressions captured from `on: { schedule: [{cron: "..."}, ...] }`.
   * Empty when the workflow has no schedule trigger (the common case).
   * Strings are passed through verbatim — validation happens later when
   * the scheduler tries to parse them via `src/lib/cron.ts`.
   */
  schedules?: string[];
  jobs: WorkflowJob[];
};

export type ParseResult =
  | { ok: true; workflow: ParsedWorkflow }
  | { ok: false; error: string };

// ---------------------------------------------------------------------------
// Tokeniser: split into logical lines, strip comments, record indent.
// ---------------------------------------------------------------------------

type Line = {
  indent: number;
  text: string; // comment-stripped, right-trimmed
  raw: string; // original (for block-literal body preservation)
  lineNo: number; // 1-based
};

function lex(source: string): Line[] {
  const out: Line[] = [];
  const rawLines = source.replace(/\r\n?/g, "\n").split("\n");
  for (let i = 0; i < rawLines.length; i++) {
    const raw = rawLines[i] ?? "";
    // Compute indent (expand tabs as 1 space — we disallow tabs for YAML indent anyway)
    let indent = 0;
    while (indent < raw.length && (raw[indent] === " " || raw[indent] === "\t")) {
      indent++;
    }
    const body = raw.slice(indent);
    // Skip pure blank / pure comment lines
    if (body.length === 0 || body.startsWith("#")) continue;
    // Strip trailing comment (respecting quotes)
    const stripped = stripTrailingComment(body).replace(/\s+$/, "");
    if (stripped.length === 0) continue;
    out.push({ indent, text: stripped, raw, lineNo: i + 1 });
  }
  return out;
}

function stripTrailingComment(s: string): string {
  let inSingle = false;
  let inDouble = false;
  for (let i = 0; i < s.length; i++) {
    const c = s[i];
    if (c === "\\" && inDouble) {
      i++;
      continue;
    }
    if (c === "'" && !inDouble) inSingle = !inSingle;
    else if (c === '"' && !inSingle) inDouble = !inDouble;
    else if (c === "#" && !inSingle && !inDouble) {
      // Must be preceded by whitespace (or start of line) to count as a comment
      if (i === 0 || /\s/.test(s[i - 1]!)) return s.slice(0, i);
    }
  }
  return s;
}

// ---------------------------------------------------------------------------
// Scalar + flow-value parsing.
// ---------------------------------------------------------------------------

function unquote(s: string): string {
  s = s.trim();
  if (s.length >= 2) {
    if (s.startsWith('"') && s.endsWith('"')) {
      return s
        .slice(1, -1)
        .replace(/\\n/g, "\n")
        .replace(/\\t/g, "\t")
        .replace(/\\"/g, '"')
        .replace(/\\\\/g, "\\");
    }
    if (s.startsWith("'") && s.endsWith("'")) {
      return s.slice(1, -1).replace(/''/g, "'");
    }
  }
  return s;
}

/**
 * Parse a flow-style value starting at `s[i]`. Returns the parsed JS value
 * and the index just after it. Supports nested [..] and {..}, quoted strings,
 * plain scalars, and comma separators.
 */
function parseFlow(s: string, i: number): { value: unknown; next: number } {
  i = skipWs(s, i);
  if (i >= s.length) return { value: "", next: i };
  const c = s[i];
  if (c === "[") return parseFlowSeq(s, i);
  if (c === "{") return parseFlowMap(s, i);
  if (c === '"' || c === "'") {
    const end = findQuoteEnd(s, i);
    return { value: unquote(s.slice(i, end + 1)), next: end + 1 };
  }
  // plain scalar — read until , ] } or end
  let j = i;
  while (j < s.length && s[j] !== "," && s[j] !== "]" && s[j] !== "}") j++;
  return { value: unquote(s.slice(i, j).trim()), next: j };
}

function parseFlowSeq(s: string, i: number): { value: unknown[]; next: number } {
  // assumes s[i] === '['
  const out: unknown[] = [];
  i++;
  i = skipWs(s, i);
  if (s[i] === "]") return { value: out, next: i + 1 };
  while (i < s.length) {
    const { value, next } = parseFlow(s, i);
    out.push(value);
    i = skipWs(s, next);
    if (s[i] === ",") {
      i++;
      i = skipWs(s, i);
      continue;
    }
    if (s[i] === "]") return { value: out, next: i + 1 };
    break; // malformed — bail out gracefully
  }
  return { value: out, next: i };
}

function parseFlowMap(
  s: string,
  i: number,
): { value: Record<string, unknown>; next: number } {
  // assumes s[i] === '{'
  const out: Record<string, unknown> = {};
  i++;
  i = skipWs(s, i);
  if (s[i] === "}") return { value: out, next: i + 1 };
  while (i < s.length) {
    // key
    let keyEnd = i;
    if (s[i] === '"' || s[i] === "'") keyEnd = findQuoteEnd(s, i) + 1;
    else {
      while (keyEnd < s.length && s[keyEnd] !== ":" && s[keyEnd] !== ",") keyEnd++;
    }
    const key = unquote(s.slice(i, keyEnd).trim());
    i = skipWs(s, keyEnd);
    if (s[i] === ":") {
      i++;
      i = skipWs(s, i);
      const { value, next } = parseFlow(s, i);
      out[key] = value;
      i = skipWs(s, next);
    } else {
      // bare key with no value — treat as true (rare in our subset)
      out[key] = true;
    }
    if (s[i] === ",") {
      i++;
      i = skipWs(s, i);
      continue;
    }
    if (s[i] === "}") return { value: out, next: i + 1 };
    break;
  }
  return { value: out, next: i };
}

function findQuoteEnd(s: string, i: number): number {
  const q = s[i];
  let j = i + 1;
  while (j < s.length) {
    if (q === '"' && s[j] === "\\") {
      j += 2;
      continue;
    }
    if (s[j] === q) {
      if (q === "'" && s[j + 1] === "'") {
        j += 2;
        continue;
      }
      return j;
    }
    j++;
  }
  return s.length - 1;
}

function skipWs(s: string, i: number): number {
  while (i < s.length && (s[i] === " " || s[i] === "\t")) i++;
  return i;
}

// ---------------------------------------------------------------------------
// Block-scalar (| and >) assembly: consumes continuation lines indented deeper
// than `parentIndent` and joins them per the YAML block-scalar rules.
// ---------------------------------------------------------------------------

function readBlockScalar(
  lines: Line[],
  idx: number,
  parentIndent: number,
  style: "literal" | "folded",
): { text: string; next: number } {
  const parts: string[] = [];
  let blockIndent = -1;
  let i = idx;
  while (i < lines.length) {
    const line = lines[i]!;
    if (line.indent <= parentIndent) break;
    if (blockIndent < 0) blockIndent = line.indent;
    // Use the raw line to preserve inner whitespace but strip the common indent.
    const raw = line.raw;
    const stripped = raw.slice(Math.min(blockIndent, raw.length));
    parts.push(stripped);
    i++;
  }
  const text =
    style === "literal"
      ? parts.join("\n")
      : parts
          .map((p) => p.trim())
          .filter((p) => p.length > 0)
          .join(" ");
  return { text, next: i };
}

// ---------------------------------------------------------------------------
// Block-style YAML parser. Recursive-descent over indented Line[] array.
// Returns a plain JS value (object / array / string).
// ---------------------------------------------------------------------------

type Cursor = { i: number };

function parseBlock(lines: Line[], cur: Cursor, indent: number): unknown {
  if (cur.i >= lines.length) return null;
  const first = lines[cur.i]!;
  if (first.indent < indent) return null;
  if (first.text.startsWith("- ") || first.text === "-") {
    return parseBlockSeq(lines, cur, first.indent);
  }
  return parseBlockMap(lines, cur, first.indent);
}

function parseBlockMap(
  lines: Line[],
  cur: Cursor,
  indent: number,
): Record<string, unknown> {
  const out: Record<string, unknown> = {};
  while (cur.i < lines.length) {
    const line = lines[cur.i]!;
    if (line.indent < indent) break;
    if (line.indent > indent) {
      // Shouldn't happen in well-formed input — skip defensively.
      cur.i++;
      continue;
    }
    const { text } = line;
    // Must be "key: …" at this indent.
    const colon = findMapColon(text);
    if (colon < 0) break;
    const key = unquote(text.slice(0, colon).trim());
    const rest = text.slice(colon + 1).trim();
    cur.i++;
    if (rest.length === 0) {
      // value is on following deeper-indented lines (or nothing -> null)
      const child = lines[cur.i];
      if (!child || child.indent <= indent) {
        out[key] = null;
      } else {
        out[key] = parseBlock(lines, cur, child.indent);
      }
    } else if (rest === "|" || rest === "|-" || rest === "|+") {
      const { text: bs, next } = readBlockScalar(lines, cur.i, indent, "literal");
      out[key] = rest === "|-" ? bs.replace(/\n+$/, "") : bs;
      cur.i = next;
    } else if (rest === ">" || rest === ">-" || rest === ">+") {
      const { text: bs, next } = readBlockScalar(lines, cur.i, indent, "folded");
      out[key] = bs;
      cur.i = next;
    } else if (rest.startsWith("[") || rest.startsWith("{")) {
      out[key] = parseFlow(rest, 0).value;
    } else {
      out[key] = unquote(rest);
    }
  }
  return out;
}

function parseBlockSeq(lines: Line[], cur: Cursor, indent: number): unknown[] {
  const out: unknown[] = [];
  while (cur.i < lines.length) {
    const line = lines[cur.i]!;
    if (line.indent < indent) break;
    if (line.indent > indent) {
      cur.i++;
      continue;
    }
    if (!(line.text.startsWith("- ") || line.text === "-")) break;
    const afterDash = line.text === "-" ? "" : line.text.slice(2);
    cur.i++;

    if (afterDash.length === 0) {
      // element is on following deeper-indented lines
      const child = lines[cur.i];
      if (!child || child.indent <= indent) {
        out.push(null);
      } else {
        out.push(parseBlock(lines, cur, child.indent));
      }
      continue;
    }

    // Could be "- key: value" (start of an inline map element) or "- scalar"
    const colon = findMapColon(afterDash);
    if (colon >= 0) {
      // Build a virtual map: first pair from `afterDash`, further pairs from
      // subsequent lines indented at (indent + 2 spaces past the dash).
      const key = unquote(afterDash.slice(0, colon).trim());
      const rest = afterDash.slice(colon + 1).trim();
      const elem: Record<string, unknown> = {};
      const childIndent = indent + 2;
      if (rest.length === 0) {
        const child = lines[cur.i];
        if (child && child.indent > childIndent) {
          elem[key] = parseBlock(lines, cur, child.indent);
        } else {
          elem[key] = null;
        }
      } else if (rest === "|" || rest === "|-" || rest === "|+") {
        const { text: bs, next } = readBlockScalar(
          lines,
          cur.i,
          childIndent - 1,
          "literal",
        );
        elem[key] = rest === "|-" ? bs.replace(/\n+$/, "") : bs;
        cur.i = next;
      } else if (rest === ">" || rest === ">-" || rest === ">+") {
        const { text: bs, next } = readBlockScalar(
          lines,
          cur.i,
          childIndent - 1,
          "folded",
        );
        elem[key] = bs;
        cur.i = next;
      } else if (rest.startsWith("[") || rest.startsWith("{")) {
        elem[key] = parseFlow(rest, 0).value;
      } else {
        elem[key] = unquote(rest);
      }
      // Sibling map keys for the same element: same indent as childIndent.
      while (cur.i < lines.length) {
        const sib = lines[cur.i]!;
        if (sib.indent < childIndent) break;
        if (sib.indent > childIndent) {
          cur.i++;
          continue;
        }
        if (sib.text.startsWith("- ") || sib.text === "-") break;
        const sc = findMapColon(sib.text);
        if (sc < 0) break;
        const sk = unquote(sib.text.slice(0, sc).trim());
        const sr = sib.text.slice(sc + 1).trim();
        cur.i++;
        if (sr.length === 0) {
          const child = lines[cur.i];
          if (child && child.indent > childIndent) {
            elem[sk] = parseBlock(lines, cur, child.indent);
          } else {
            elem[sk] = null;
          }
        } else if (sr === "|" || sr === "|-" || sr === "|+") {
          const { text: bs, next } = readBlockScalar(
            lines,
            cur.i,
            childIndent - 1,
            "literal",
          );
          elem[sk] = sr === "|-" ? bs.replace(/\n+$/, "") : bs;
          cur.i = next;
        } else if (sr === ">" || sr === ">-" || sr === ">+") {
          const { text: bs, next } = readBlockScalar(
            lines,
            cur.i,
            childIndent - 1,
            "folded",
          );
          elem[sk] = bs;
          cur.i = next;
        } else if (sr.startsWith("[") || sr.startsWith("{")) {
          elem[sk] = parseFlow(sr, 0).value;
        } else {
          elem[sk] = unquote(sr);
        }
      }
      out.push(elem);
    } else {
      // plain scalar element
      if (afterDash.startsWith("[") || afterDash.startsWith("{")) {
        out.push(parseFlow(afterDash, 0).value);
      } else {
        out.push(unquote(afterDash));
      }
    }
  }
  return out;
}

/** Locate the ':' that ends a mapping key, skipping quoted sections. */
function findMapColon(text: string): number {
  let inSingle = false;
  let inDouble = false;
  for (let i = 0; i < text.length; i++) {
    const c = text[i];
    if (c === "\\" && inDouble) {
      i++;
      continue;
    }
    if (c === "'" && !inDouble) inSingle = !inSingle;
    else if (c === '"' && !inSingle) inDouble = !inDouble;
    else if (c === ":" && !inSingle && !inDouble) {
      // Must be followed by space, EOL, or be at the very end.
      if (i + 1 >= text.length || text[i + 1] === " " || text[i + 1] === "\t") {
        return i;
      }
    }
  }
  return -1;
}

// ---------------------------------------------------------------------------
// Normalisation: raw YAML value → ParsedWorkflow
// ---------------------------------------------------------------------------

function asString(v: unknown): string | null {
  if (typeof v === "string") return v;
  if (typeof v === "number" || typeof v === "boolean") return String(v);
  return null;
}

function normaliseOn(v: unknown): string[] | null {
  if (v == null) return null;
  if (typeof v === "string") {
    const s = v.trim();
    return s.length ? [s] : null;
  }
  if (Array.isArray(v)) {
    const out: string[] = [];
    for (const item of v) {
      const s = asString(item);
      if (s && s.trim().length) out.push(s.trim());
    }
    return out.length ? out : null;
  }
  if (typeof v === "object") {
    const keys = Object.keys(v as Record<string, unknown>);
    return keys.length ? keys : null;
  }
  return null;
}

/**
 * Extract cron expressions from the raw `on:` value when it is a mapping
 * containing a `schedule:` key. Returns [] for any other shape so callers
 * can unconditionally read `parsed.schedules ?? []`. Tolerant of:
 *   - schedule: [{cron: "0 * * * *"}, ...]
 *   - schedule: {cron: "0 * * * *"}
 *   - schedule: "0 * * * *"  (legacy, not standard but seen in the wild)
 *
 * Pure helper — exported alongside the existing `__test` bundle.
 */
function extractSchedules(rawOn: unknown): string[] {
  if (!rawOn || typeof rawOn !== "object" || Array.isArray(rawOn)) return [];
  const m = rawOn as Record<string, unknown>;
  const node = m.schedule;
  if (node == null) return [];

  const out: string[] = [];
  const collect = (entry: unknown) => {
    if (typeof entry === "string") {
      const s = entry.trim();
      if (s) out.push(s);
      return;
    }
    if (entry && typeof entry === "object" && !Array.isArray(entry)) {
      const cron = (entry as Record<string, unknown>).cron;
      const s = typeof cron === "string" ? cron.trim() : "";
      if (s) out.push(s);
    }
  };

  if (Array.isArray(node)) {
    for (const e of node) collect(e);
  } else {
    collect(node);
  }
  return out;
}

function normaliseStep(
  raw: unknown,
  jobName: string,
): { ok: true; step: WorkflowStep } | { ok: false; error: string } {
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
    return { ok: false, error: `step in job '${jobName}' must be a mapping` };
  }
  const r = raw as Record<string, unknown>;
  const run = asString(r.run);
  if (!run || !run.trim().length) {
    return { ok: false, error: `step in job '${jobName}' missing 'run' command` };
  }
  const nameVal = asString(r.name);
  const name = nameVal && nameVal.trim().length ? nameVal.trim() : "Run command";
  return { ok: true, step: { name, run } };
}

function normaliseJob(
  name: string,
  raw: unknown,
): { ok: true; job: WorkflowJob } | { ok: false; error: string } {
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
    return { ok: false, error: `job '${name}' is not a mapping` };
  }
  const r = raw as Record<string, unknown>;
  const runsOnRaw = asString(r["runs-on"]);
  const runsOn = runsOnRaw && runsOnRaw.trim().length ? runsOnRaw.trim() : "default";
  const stepsRaw = r.steps;
  if (!Array.isArray(stepsRaw) || stepsRaw.length === 0) {
    return { ok: false, error: `job '${name}' has no steps` };
  }
  const steps: WorkflowStep[] = [];
  for (const s of stepsRaw) {
    const res = normaliseStep(s, name);
    if (!res.ok) return res;
    steps.push(res.step);
  }
  return { ok: true, job: { name, runsOn, steps } };
}

// ---------------------------------------------------------------------------
// Public entry point.
// ---------------------------------------------------------------------------

export function parseWorkflow(yaml: string): ParseResult {
  if (typeof yaml !== "string") {
    return { ok: false, error: "workflow input must be a string" };
  }
  let root: unknown;
  try {
    const lines = lex(yaml);
    if (lines.length === 0) {
      return { ok: false, error: "workflow is empty" };
    }
    const cur: Cursor = { i: 0 };
    root = parseBlock(lines, cur, lines[0]!.indent);
  } catch (err) {
    return {
      ok: false,
      error: `failed to parse YAML: ${err instanceof Error ? err.message : String(err)}`,
    };
  }

  if (!root || typeof root !== "object" || Array.isArray(root)) {
    return { ok: false, error: "workflow root must be a mapping" };
  }
  const doc = root as Record<string, unknown>;

  const nameRaw = asString(doc.name);
  const name = nameRaw && nameRaw.trim().length ? nameRaw.trim() : "(unnamed)";

  if (!("on" in doc) || doc.on == null) {
    return { ok: false, error: "workflow missing 'on' trigger" };
  }
  const on = normaliseOn(doc.on);
  if (!on || on.length === 0) {
    return { ok: false, error: "workflow missing 'on' trigger" };
  }
  const schedules = extractSchedules(doc.on);

  const jobsRaw = doc.jobs;
  if (
    !jobsRaw ||
    typeof jobsRaw !== "object" ||
    Array.isArray(jobsRaw) ||
    Object.keys(jobsRaw as Record<string, unknown>).length === 0
  ) {
    return { ok: false, error: "workflow has no jobs" };
  }

  const jobs: WorkflowJob[] = [];
  // Object.keys preserves insertion order for string keys in modern engines.
  for (const key of Object.keys(jobsRaw as Record<string, unknown>)) {
    const res = normaliseJob(key, (jobsRaw as Record<string, unknown>)[key]);
    if (!res.ok) return res;
    jobs.push(res.job);
  }

  const workflow: ParsedWorkflow = { name, on, jobs };
  if (schedules.length > 0) workflow.schedules = schedules;
  return { ok: true, workflow };
}

/**
 * Test-only export of the schedule extractor. Pure helper, no DB.
 */
export const __test = { extractSchedules };