neurastream 0.2.86 → 0.3.2

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.
@@ -0,0 +1,127 @@
1
+ // Share decision for team-queue P1. queue/lib cannot import mr-sweep (mr-sweep already
2
+ // imports it), and the forge reconciler should not own the Queue header, so the decision
3
+ // lives here. planShare is the gate later sync must call before any send: null means the
4
+ // plan stays on this machine.
5
+
6
+ import type { Config } from "./config.ts";
7
+ import { defaultRunner, remotePath, type CommandRunner } from "./mr-sweep.ts";
8
+ import { headerField, type Plan } from "./queue/lib.ts";
9
+ import { globMatch } from "./trace-actions.ts";
10
+
11
+ function isObj(v: unknown): v is Record<string, unknown> {
12
+ return typeof v === "object" && v !== null && !Array.isArray(v);
13
+ }
14
+
15
+ /** Host lowercased, no trailing slash, so `GitLab.com/acme/*` and `gitlab.com/acme/*` are one pattern. */
16
+ function normPattern(p: string): string {
17
+ const t = p.trim().replace(/\/+$/, "");
18
+ const i = t.indexOf("/");
19
+ return i < 0 ? t.toLowerCase() : t.slice(0, i).toLowerCase() + t.slice(i);
20
+ }
21
+
22
+ function fill(p: string): string {
23
+ return p.replaceAll("**/", "a/").replaceAll("**", "a").replaceAll("*", "a").replaceAll("?", "a");
24
+ }
25
+
26
+ function segMatch(pattern: string, text: string): boolean {
27
+ let re = "";
28
+ for (const c of pattern) {
29
+ if (c === "*") re += ".*";
30
+ else if (c === "?") re += ".";
31
+ else re += c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
32
+ }
33
+ return new RegExp(`^${re}$`).test(text);
34
+ }
35
+
36
+ /** One path segment. `*` is anything in the segment, matching globMatch (a star does not cross `/`). */
37
+ function segOverlap(a: string, b: string): boolean {
38
+ if (a === "*" || b === "*") return true;
39
+ if (a === b) return true;
40
+ if (!(a.includes("*") || a.includes("?"))) return segMatch(b, a);
41
+ if (!(b.includes("*") || b.includes("?"))) return segMatch(a, b);
42
+ return segMatch(a, fill(b)) || segMatch(b, fill(a));
43
+ }
44
+
45
+ /** True when the two origin globs can name the same `host/path`. */
46
+ function globsOverlap(a: string, b: string): boolean {
47
+ const left = normPattern(a);
48
+ const right = normPattern(b);
49
+ if (left === right) return true;
50
+ if (left.includes("**") || right.includes("**")) return globMatch(left, fill(right), "") || globMatch(right, fill(left), "");
51
+ const as = left.split("/");
52
+ const bs = right.split("/");
53
+ if (as.length !== bs.length) return false;
54
+ return as.every((seg, i) => segOverlap(seg, bs[i]!));
55
+ }
56
+
57
+ /** Team names become unit names, file names and URL-safe ids, so one shape everywhere (CLI, config, unit files). */
58
+ export const TEAM_NAME_RE = /^[a-z0-9][a-z0-9-]{0,31}$/;
59
+ export const TEAM_NAME_HINT = `team name must match ${TEAM_NAME_RE.source}`;
60
+
61
+ /** Refusal text when a team's repo list (config `repos`, or `--repo` on the CLI) is not a non-empty list of non-empty globs. */
62
+ export function repoListError(repos: unknown): string | null {
63
+ if (Array.isArray(repos) && repos.length > 0 && repos.every(r => typeof r === "string" && r.trim() !== "" && !/^\*+$/.test(r.trim()))) return null;
64
+ return "repos must be a list of non-empty origin globs (host/path, * does not cross /); a bare * or ** is refused";
65
+ }
66
+
67
+ /**
68
+ * Why `teams` cannot load, or null. Two teams that can name the same remote are an error:
69
+ * a repo must belong to one team. Shape is checked here because an array in the template
70
+ * is replaced whole, so item types never pass through merge.
71
+ */
72
+ export function teamConfigError(teams: unknown): string | null {
73
+ if (!Array.isArray(teams)) return "teams must be a list of {name, server, repos}";
74
+ const names = new Set<string>();
75
+ const parsed: { name: string; repos: string[] }[] = [];
76
+ for (const [i, raw] of teams.entries()) {
77
+ if (!isObj(raw)) return `teams[${i}] must be {name, server, repos}`;
78
+ const { name, server, repos, max_share } = raw;
79
+ if (typeof name !== "string" || !TEAM_NAME_RE.test(name)) return `teams[${i}].name must match ${TEAM_NAME_RE.source}`;
80
+ if (names.has(name)) return `teams: two entries named "${name}"`;
81
+ names.add(name);
82
+ if (typeof server !== "string" || server.trim() === "") return `teams[${i}].server must be a non-empty string`;
83
+ if (max_share !== undefined && (typeof max_share !== "number" || !(max_share > 0 && max_share <= 1))) return `teams[${i}].max_share must be a number above 0 and at most 1`;
84
+ const repoErr = repoListError(repos);
85
+ if (repoErr) return `teams[${i}].${repoErr}`;
86
+ parsed.push({ name, repos: repos as string[] });
87
+ }
88
+ for (let i = 0; i < parsed.length; i++) {
89
+ for (let j = i + 1; j < parsed.length; j++) {
90
+ const a = parsed[i]!;
91
+ const b = parsed[j]!;
92
+ for (const pa of a.repos) {
93
+ for (const pb of b.repos) {
94
+ if (globsOverlap(pa, pb)) return `teams: "${a.name}" and "${b.name}" both match the same remote (${pa} and ${pb})`;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ return null;
100
+ }
101
+
102
+ /** True when a git origin URL matches one of these repo globs. Unparseable is not listed. The team server calls this with the remote it was given, so it does not shell out. */
103
+ export function remoteListed(remote: string, repos: string[]): boolean {
104
+ const path = remotePath(remote);
105
+ if (!path) return false;
106
+ return repos.some(p => globMatch(normPattern(p), path, ""));
107
+ }
108
+
109
+ /** The team whose `repos` glob matches this repo's origin, or null (no origin, unparseable, or no match). */
110
+ export function repoTeam(repoDir: string, cfg: Config, run: CommandRunner = defaultRunner): string | null {
111
+ const remote = run(["git", "-C", repoDir, "remote", "get-url", "origin"], {});
112
+ const url = remote.code === 0 ? remote.stdout.trim() : "";
113
+ if (!url) return null;
114
+ for (const team of cfg.teams) {
115
+ if (remoteListed(url, team.repos)) return team.name;
116
+ }
117
+ return null;
118
+ }
119
+
120
+ /**
121
+ * The team this plan shares with, or null. A missing or non-`team` Queue header returns
122
+ * before the origin is read, so a personal plan is never looked up to send.
123
+ */
124
+ export function planShare(plan: Plan, cfg: Config, run: CommandRunner = defaultRunner): string | null {
125
+ if (headerField(plan.text, "Queue") !== "team") return null;
126
+ return repoTeam(plan.repo, cfg, run);
127
+ }