opencode-model-router 2.7.0 → 2.9.0

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 (111) hide show
  1. package/CHANGELOG.md +583 -0
  2. package/README.md +20 -0
  3. package/package.json +5 -1
  4. package/src/commands/approvals.ts +354 -0
  5. package/src/commands/output.ts +34 -1
  6. package/src/compat/background.ts +2439 -0
  7. package/src/compat/child-session.ts +6 -0
  8. package/src/compat/turn-state.ts +215 -0
  9. package/src/compat/v2-client.ts +136 -17
  10. package/src/compat/v2-hooks.ts +2853 -109
  11. package/src/compat/v2-observations.ts +328 -0
  12. package/src/executor/capacity.ts +362 -0
  13. package/src/executor/child-guards.ts +40 -0
  14. package/src/executor/command.ts +265 -0
  15. package/src/executor/compile/edges.ts +809 -0
  16. package/src/executor/compile/estimate.ts +158 -0
  17. package/src/executor/compile/footprint.ts +829 -0
  18. package/src/executor/compile/index.ts +1080 -0
  19. package/src/executor/compile/parse.ts +1492 -0
  20. package/src/executor/compile/residue.ts +706 -0
  21. package/src/executor/compile/validate.ts +280 -0
  22. package/src/executor/create.ts +672 -0
  23. package/src/executor/expand.ts +551 -0
  24. package/src/executor/git.ts +1214 -0
  25. package/src/executor/host-preamble.ts +17 -0
  26. package/src/executor/instructions.ts +35 -0
  27. package/src/executor/integrate/bisect.ts +73 -0
  28. package/src/executor/integrate/checks.ts +1086 -0
  29. package/src/executor/integrate/land.ts +177 -0
  30. package/src/executor/integrate/phase-suite.ts +407 -0
  31. package/src/executor/integrate/queue.ts +3083 -0
  32. package/src/executor/integrate/run-resolver.ts +19 -0
  33. package/src/executor/launch.ts +652 -0
  34. package/src/executor/ledger.ts +2208 -0
  35. package/src/executor/observations.ts +212 -0
  36. package/src/executor/paths.ts +159 -0
  37. package/src/executor/platform.ts +39 -0
  38. package/src/executor/process-identity.ts +19 -0
  39. package/src/executor/qa-loop.ts +470 -0
  40. package/src/executor/recovery.ts +403 -0
  41. package/src/executor/runtime.ts +6233 -0
  42. package/src/executor/schedule.ts +246 -0
  43. package/src/executor/slots/env.ts +145 -0
  44. package/src/executor/slots/pool.ts +1564 -0
  45. package/src/executor/slots/provision.ts +853 -0
  46. package/src/executor/tool.ts +534 -0
  47. package/src/executor/types.ts +2064 -0
  48. package/src/executor/unattended.ts +488 -0
  49. package/src/guard/store.ts +3 -1
  50. package/src/index.ts +1789 -288
  51. package/src/router/config.ts +823 -26
  52. package/src/router/dispatch-header.ts +95 -2
  53. package/src/router/git-tools.ts +266 -19
  54. package/src/router/leases.ts +1469 -0
  55. package/src/router/logger.ts +44 -32
  56. package/src/router/protocol.ts +44 -10
  57. package/src/router/role-agents.ts +334 -20
  58. package/src/router/roles-config.ts +93 -1
  59. package/src/router/roles.ts +1 -1
  60. package/src/router/run-tools.ts +56 -4
  61. package/src/router/sessions.ts +11 -0
  62. package/src/router/tool-paths.ts +308 -0
  63. package/src/router/work-roots.ts +86 -0
  64. package/src/routing/advisor/findings.ts +25 -5
  65. package/src/routing/classify/index.ts +7 -1
  66. package/src/routing/classify/route-line.ts +96 -5
  67. package/src/routing/classify/rules.ts +11 -0
  68. package/src/routing/classify/types.ts +26 -0
  69. package/src/routing/commands/stats.ts +2 -2
  70. package/src/routing/outcomes/durations.ts +225 -0
  71. package/src/routing/outcomes/index.ts +59 -2
  72. package/src/routing/outcomes/persist.ts +409 -0
  73. package/src/routing/outcomes/signals.ts +17 -0
  74. package/src/routing/outcomes/stats.ts +170 -11
  75. package/src/routing/outcomes/timings.ts +836 -0
  76. package/src/routing/outcomes/types.ts +98 -0
  77. package/src/routing/roles/admission.ts +1028 -0
  78. package/src/routing/roles/approvals.ts +957 -0
  79. package/src/routing/roles/binding.ts +116 -13
  80. package/src/routing/roles/check-placement.ts +112 -0
  81. package/src/routing/roles/git-root.ts +500 -0
  82. package/src/routing/roles/orphans.ts +68 -0
  83. package/src/routing/roles/policy.ts +30 -3
  84. package/src/routing/roles/read-set.ts +325 -0
  85. package/src/routing/roles/work-root.ts +12 -2
  86. package/src/routing/wire/dispatch.ts +697 -30
  87. package/src/routing/wire/hint.ts +56 -8
  88. package/src/routing/wire/working-set.ts +58 -0
  89. package/src/util/linear-trim.ts +32 -0
  90. package/src/v2.ts +94 -49
  91. package/src/verify/batch.ts +7 -2
  92. package/src/verify/branch.ts +141 -0
  93. package/src/verify/canonical-path.ts +35 -0
  94. package/src/verify/deterministic.ts +268 -17
  95. package/src/verify/dispatch.ts +348 -37
  96. package/src/verify/dod.ts +46 -0
  97. package/src/verify/edges.ts +817 -0
  98. package/src/verify/exec.ts +52 -9
  99. package/src/verify/gate.ts +349 -16
  100. package/src/verify/git-top.ts +43 -0
  101. package/src/verify/head.ts +107 -0
  102. package/src/verify/pending.ts +1846 -18
  103. package/src/verify/perimeter.ts +1186 -0
  104. package/src/verify/pin.ts +109 -0
  105. package/src/verify/plan-task.ts +729 -0
  106. package/src/verify/reference.ts +297 -8
  107. package/src/verify/risk.ts +16 -3
  108. package/src/verify/runner.ts +706 -32
  109. package/src/verify/tree.ts +372 -16
  110. package/src/verify/types.ts +66 -2
  111. package/src/verify/wiring.ts +1594 -46
@@ -0,0 +1,107 @@
1
+ // Impure, cheap HEAD reader: where a working tree is checked out, and how many HEAD reflog entries it has.
2
+ // Kept apart from tree.ts on purpose: several tests mock that module with `snapshotTree` alone.
3
+ import { realpath } from "node:fs/promises";
4
+ import { execGit, gitExitCode } from "./tree";
5
+
6
+ /** What the branch-switch rule reads of a working tree (see branch.ts `treeMove`). */
7
+ export interface HeadState {
8
+ /** realpath of the toplevel, as snapshotTree reports it. */
9
+ readonly root: string;
10
+ readonly head: string;
11
+ /** The branch name without `refs/heads/`; null on a detached HEAD; any other ref kept whole. */
12
+ readonly branch: string | null;
13
+ /** Number of HEAD reflog entries (per worktree); absent when it could not be counted. */
14
+ readonly reflog?: number;
15
+ }
16
+
17
+ export interface HeadReadOptions {
18
+ readonly signal?: AbortSignal;
19
+ /** Each git process. Default 5000. */
20
+ readonly timeoutMs?: number;
21
+ readonly lowPriority?: boolean;
22
+ }
23
+
24
+ const DEFAULT_TIMEOUT_MS = 5000;
25
+ const MAX_BUFFER = 1024 * 1024;
26
+ const MAX_WINDOW = 1000;
27
+
28
+ function runner(cwd: string, opts: HeadReadOptions | undefined): (args: readonly string[]) => Promise<string> {
29
+ const signal = opts?.signal ?? new AbortController().signal;
30
+ return (args) => execGit(["--no-pager", "--no-optional-locks", ...args], {
31
+ cwd, signal, timeoutMs: opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS, maxBuffer: MAX_BUFFER, lowPriority: opts?.lowPriority === true,
32
+ });
33
+ }
34
+
35
+ /**
36
+ * What a head read found: the state, `"none"` when git RAN and exited non-zero (not a repository, an unborn branch: there is
37
+ * no HEAD to compare), or undefined when the answer is unknown (timeout, abort, spawn failure, unparsable output).
38
+ */
39
+ export type HeadRead = HeadState | "none" | undefined;
40
+
41
+ /**
42
+ * The toplevel, HEAD and branch of the checkout at `cwd` (one git process), plus its HEAD reflog count (a second one).
43
+ * `"none"` only for a git exit code (never decided from git's localised message); every other failure is undefined. A failing
44
+ * count, or a count of 0 (reflogs disabled: `core.logAllRefUpdates=false`), only leaves `reflog` absent.
45
+ */
46
+ export async function readHead(cwd: string, opts?: HeadReadOptions): Promise<HeadRead> {
47
+ const git = runner(cwd, opts);
48
+ let root: string;
49
+ let head: string;
50
+ let branch: string | null;
51
+ try {
52
+ const lines = (await git(["rev-parse", "--show-toplevel", "HEAD", "--symbolic-full-name", "HEAD"])).split(/\r?\n/);
53
+ const top = (lines[0] ?? "").trim();
54
+ head = (lines[1] ?? "").trim();
55
+ const ref = (lines[2] ?? "").trim();
56
+ if (top === "" || !/^[0-9a-f]{40,64}$/i.test(head) || ref === "") return undefined;
57
+ branch = ref === "HEAD" ? null : ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
58
+ root = await realpath(top);
59
+ } catch (err) {
60
+ return gitExitCode(err) !== undefined ? "none" : undefined;
61
+ }
62
+ try {
63
+ const text = (await git(["rev-list", "--walk-reflogs", "--count", "HEAD"])).trim();
64
+ // HEAD exists, so a reflog of 0 entries means there is none (reflogs off), not an empty one.
65
+ if (/^\d+$/.test(text) && Number(text) > 0) return { root, head, branch, reflog: Number(text) };
66
+ } catch {
67
+ // The count is optional.
68
+ }
69
+ return { root, head, branch };
70
+ }
71
+
72
+ /** `readHead` for callers that treat "no repository / unborn" and "unknown" alike: undefined for both. */
73
+ export async function readHeadState(cwd: string, opts?: HeadReadOptions): Promise<HeadState | undefined> {
74
+ const read = await readHead(cwd, opts);
75
+ return read === "none" ? undefined : read;
76
+ }
77
+
78
+ /** One HEAD reflog entry: the commit HEAD pointed at after it, and its subject (`%gs`). */
79
+ export interface ReflogEntry {
80
+ readonly commit: string;
81
+ readonly subject: string;
82
+ }
83
+
84
+ /**
85
+ * The HEAD reflog window since a recorded count, read in ONE git process so it is anchored: `count` new entries plus the one
86
+ * that was newest at the earlier read, newest first. [] for count <= 0 without running git; undefined on failure, on a count
87
+ * above 1000, or when the output cannot be parsed.
88
+ */
89
+ export async function readReflogWindow(cwd: string, count: number, opts?: HeadReadOptions): Promise<ReflogEntry[] | undefined> {
90
+ if (!(count > 0)) return [];
91
+ const n = Math.floor(count);
92
+ if (n > MAX_WINDOW) return undefined;
93
+ try {
94
+ const lines = (await runner(cwd, opts)(["log", "-g", "-n", String(n + 1), "--format=%H%x09%gs", "HEAD"])).split(/\r?\n/);
95
+ if (lines[lines.length - 1] === "") lines.pop();
96
+ const entries: ReflogEntry[] = [];
97
+ for (const line of lines) {
98
+ const tab = line.indexOf("\t");
99
+ const commit = tab < 0 ? line : line.slice(0, tab);
100
+ if (!/^[0-9a-f]{40,64}$/i.test(commit)) return undefined;
101
+ entries.push({ commit, subject: tab < 0 ? "" : line.slice(tab + 1) });
102
+ }
103
+ return entries;
104
+ } catch {
105
+ return undefined;
106
+ }
107
+ }