@niwa-labs/pstack 0.0.0-stage → 0.0.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.
- package/LICENSE +21 -0
- package/README.md +260 -2
- package/package.json +28 -4
- package/scripts/upstream-map.ts +90 -0
- package/src/author.ts +57 -0
- package/src/binding.ts +32 -0
- package/src/catalog.ts +86 -0
- package/src/composition.ts +66 -0
- package/src/index.ts +50 -0
- package/src/mapping/analyze.ts +223 -0
- package/src/mapping/assemble.ts +96 -0
- package/src/mapping/diff.ts +139 -0
- package/src/mapping/format.ts +67 -0
- package/src/mapping/git.ts +69 -0
- package/src/mapping/index.ts +18 -0
- package/src/mapping/maintain.ts +36 -0
- package/src/mapping/schema.ts +50 -0
- package/src/mapping/sources.ts +108 -0
- package/src/mapping/tool.ts +77 -0
- package/src/mapping/verify.ts +130 -0
- package/src/plugin.ts +136 -0
- package/src/policy.ts +115 -0
- package/src/ports.ts +184 -0
- package/src/pr-binding.ts +88 -0
- package/src/records.ts +61 -0
- package/src/roles.ts +49 -0
- package/src/trunk.ts +59 -0
- package/src/upstream-actions.ts +135 -0
- package/src/upstream-records.ts +71 -0
- package/src/upstream.ts +151 -0
- package/src/workflows/adapted.md +780 -0
- package/src/workflows/authoring-a-skill.ts +52 -0
- package/src/workflows/autonomous-run.ts +56 -0
- package/src/workflows/autopilot-full.ts +90 -0
- package/src/workflows/autopilot-stack.ts +125 -0
- package/src/workflows/babysit.ts +124 -0
- package/src/workflows/benny-reproduce.ts +122 -0
- package/src/workflows/benny-triage.ts +89 -0
- package/src/workflows/bug-fix.ts +54 -0
- package/src/workflows/entry.ts +130 -0
- package/src/workflows/eval.ts +63 -0
- package/src/workflows/feature.ts +54 -0
- package/src/workflows/hillclimb.ts +78 -0
- package/src/workflows/investigation.ts +41 -0
- package/src/workflows/lib/catalog.ts +45 -0
- package/src/workflows/lib/change.ts +70 -0
- package/src/workflows/lib/forensics.ts +37 -0
- package/src/workflows/lib/git.ts +30 -0
- package/src/workflows/lib/go.ts +58 -0
- package/src/workflows/lib/inputs.ts +34 -0
- package/src/workflows/lib/lane.ts +170 -0
- package/src/workflows/lib/measure.ts +34 -0
- package/src/workflows/lib/plan.ts +24 -0
- package/src/workflows/lib/script.ts +79 -0
- package/src/workflows/lib/ship.ts +110 -0
- package/src/workflows/lib/texts.ts +18 -0
- package/src/workflows/lib/trunk.ts +91 -0
- package/src/workflows/multi-phase-plan.ts +83 -0
- package/src/workflows/opening-a-pr.ts +62 -0
- package/src/workflows/pause-safely.ts +50 -0
- package/src/workflows/perf-issue.ts +87 -0
- package/src/workflows/prototype.ts +76 -0
- package/src/workflows/refactoring.ts +54 -0
- package/src/workflows/runtime-forensics.ts +26 -0
- package/src/workflows/scenarios.ts +380 -0
- package/src/workflows/session-pickup.ts +57 -0
- package/src/workflows/shipping.ts +63 -0
- package/src/workflows/suite.ts +129 -0
- package/src/workflows/texts/authoring-a-skill.json +5 -0
- package/src/workflows/texts/autonomous-run.json +5 -0
- package/src/workflows/texts/autopilot-full.json +5 -0
- package/src/workflows/texts/autopilot-stack.json +5 -0
- package/src/workflows/texts/babysit.json +5 -0
- package/src/workflows/texts/benny-reproduce.json +5 -0
- package/src/workflows/texts/benny-triage.json +5 -0
- package/src/workflows/texts/bug-fix.json +5 -0
- package/src/workflows/texts/bugbot-triage.json +5 -0
- package/src/workflows/texts/eval.json +5 -0
- package/src/workflows/texts/feature.json +5 -0
- package/src/workflows/texts/hillclimb.json +5 -0
- package/src/workflows/texts/investigation.json +5 -0
- package/src/workflows/texts/multi-phase-plan.json +5 -0
- package/src/workflows/texts/opening-a-pr.json +5 -0
- package/src/workflows/texts/pause-safely.json +5 -0
- package/src/workflows/texts/perf-issue.json +5 -0
- package/src/workflows/texts/prototype.json +5 -0
- package/src/workflows/texts/refactoring.json +5 -0
- package/src/workflows/texts/runtime-forensics.json +5 -0
- package/src/workflows/texts/session-pickup.json +5 -0
- package/src/workflows/texts/trace-forensics.json +5 -0
- package/src/workflows/texts/visual-parity.json +5 -0
- package/src/workflows/texts/worker.json +5 -0
- package/src/workflows/trace-forensics.ts +27 -0
- package/src/workflows/upstream-sync.ts +182 -0
- package/src/workflows/visual-parity.ts +52 -0
- package/src/workflows/worktree-cleanup.ts +57 -0
- package/test/author.test.ts +61 -0
- package/test/benny.test.ts +138 -0
- package/test/binding.test.ts +54 -0
- package/test/catalog-helpers.test.ts +49 -0
- package/test/closure.test.ts +45 -0
- package/test/composition.test.ts +23 -0
- package/test/eval.test.ts +23 -0
- package/test/lanes.test.ts +334 -0
- package/test/mapping-diff.test.ts +64 -0
- package/test/mapping-support.ts +110 -0
- package/test/mapping-texts.test.ts +106 -0
- package/test/mapping.test.ts +276 -0
- package/test/package-layout.ts +68 -0
- package/test/policy.test.ts +98 -0
- package/test/ports.test.ts +45 -0
- package/test/ports.ts +173 -0
- package/test/roles.test.ts +21 -0
- package/test/suite.test.ts +167 -0
- package/test/support.ts +541 -0
- package/test/texts.test.ts +174 -0
- package/test/tool.test.ts +48 -0
- package/test/trunk-helpers.test.ts +149 -0
- package/test/trunk.test.ts +24 -0
- package/test/upstream-sync.test.ts +246 -0
- package/test/upstream.test.ts +106 -0
- package/test/worktree-cleanup.test.ts +107 -0
- package/tsconfig.json +7 -0
- package/upstream/LICENSE +21 -0
- package/upstream/SOURCE.json +5 -0
- package/upstream/benny/FOR_AGENTS.md +89 -0
- package/upstream/benny/README.md +23 -0
- package/upstream/benny/skills/reproduce-and-fix-issues/SKILL.md +310 -0
- package/upstream/benny/skills/reproduce-and-fix-issues/references/control-adapter.md +169 -0
- package/upstream/benny/skills/reproduce-and-fix-issues/references/feature-map.example.md +205 -0
- package/upstream/benny/skills/reproduce-and-fix-issues/references/verify-existing-fix.md +93 -0
- package/upstream/benny/skills/setup-benny/SKILL.md +265 -0
- package/upstream/benny/skills/triage-issue-reports/SKILL.md +240 -0
- package/upstream/benny/skills/triage-issue-reports/references/routing.example.md +61 -0
- package/upstream/benny/templates/configuration.example.yaml +84 -0
- package/upstream/benny/templates/reproduce-automation-prompt.md +33 -0
- package/upstream/benny/templates/triage-automation-prompt.md +39 -0
- package/upstream/mapping.json +699 -0
- package/upstream/poteto-mode/SKILL.md +110 -0
- package/upstream/poteto-mode/playbooks/authoring-a-skill.md +12 -0
- package/upstream/poteto-mode/playbooks/autonomous-run.md +13 -0
- package/upstream/poteto-mode/playbooks/autopilot-full.md +25 -0
- package/upstream/poteto-mode/playbooks/autopilot-stack.md +8 -0
- package/upstream/poteto-mode/playbooks/babysit.md +10 -0
- package/upstream/poteto-mode/playbooks/bug-fix.md +15 -0
- package/upstream/poteto-mode/playbooks/eval.md +25 -0
- package/upstream/poteto-mode/playbooks/feature.md +21 -0
- package/upstream/poteto-mode/playbooks/hillclimb.md +21 -0
- package/upstream/poteto-mode/playbooks/investigation.md +14 -0
- package/upstream/poteto-mode/playbooks/multi-phase-plan.md +11 -0
- package/upstream/poteto-mode/playbooks/opening-a-pr.md +9 -0
- package/upstream/poteto-mode/playbooks/pause-safely.md +10 -0
- package/upstream/poteto-mode/playbooks/perf-issue.md +25 -0
- package/upstream/poteto-mode/playbooks/prototype.md +14 -0
- package/upstream/poteto-mode/playbooks/refactoring.md +16 -0
- package/upstream/poteto-mode/playbooks/runtime-forensics.md +11 -0
- package/upstream/poteto-mode/playbooks/session-pickup.md +11 -0
- package/upstream/poteto-mode/playbooks/shipping.md +12 -0
- package/upstream/poteto-mode/playbooks/trace-forensics.md +14 -0
- package/upstream/poteto-mode/playbooks/visual-parity.md +11 -0
- package/upstream/poteto-mode/playbooks/worktree-cleanup.md +12 -0
- package/upstream/poteto-mode/references/bugbot-triage.md +142 -0
- package/upstream/poteto-mode/worker.md +11 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// The map of the upstream snapshot as the package carries it: it is sound, it covers the snapshot, and the texts the
|
|
2
|
+
// scripts import are assembled from it, whole and with nothing a session cannot reach.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
import { readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import { OursTexts, UpstreamTexts, analyze, assemble, jsonOf, parseMapping, verifyTextsIn } from "../src/mapping/index.ts";
|
|
10
|
+
import { Git } from "../src/mapping/git.ts";
|
|
11
|
+
import { MAPPING_FILE, PACKAGE_DIR, TEXTS_DIR, UPSTREAM_DIR } from "../src/upstream.ts";
|
|
12
|
+
|
|
13
|
+
const WORKFLOWS = join(PACKAGE_DIR, "src", "workflows");
|
|
14
|
+
const mapping = parseMapping(readFileSync(MAPPING_FILE, "utf8"));
|
|
15
|
+
const root = new Git(PACKAGE_DIR).out(["rev-parse", "--show-toplevel"]).trim();
|
|
16
|
+
const upstream = new UpstreamTexts(UPSTREAM_DIR);
|
|
17
|
+
const ours = new OursTexts(root);
|
|
18
|
+
const texts = readdirSync(TEXTS_DIR)
|
|
19
|
+
.filter((file) => file.endsWith(".json"))
|
|
20
|
+
.map((file) => ({ file, raw: readFileSync(join(TEXTS_DIR, file), "utf8"), value: JSON.parse(readFileSync(join(TEXTS_DIR, file), "utf8")) as { name: string; text: string } }));
|
|
21
|
+
|
|
22
|
+
/** The text of every script of the package, by file name. */
|
|
23
|
+
function scripts(): Array<{ file: string; source: string }> {
|
|
24
|
+
const read = (dir: string) => readdirSync(dir).filter((file) => file.endsWith(".ts")).map((file) => ({ file, source: readFileSync(join(dir, file), "utf8") }));
|
|
25
|
+
return [...read(WORKFLOWS), ...read(join(WORKFLOWS, "lib"))];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
test("the map is sound and covers the snapshot, with every range recorded at the snapshot's commit", () => {
|
|
29
|
+
const report = analyze(mapping, upstream, ours);
|
|
30
|
+
expect(report.problems).toEqual([]);
|
|
31
|
+
expect(report.coverage.percent).toBeGreaterThanOrEqual(95);
|
|
32
|
+
expect(report.coverage.files.length).toBeGreaterThan(30);
|
|
33
|
+
expect(mapping.entries.every((one) => one.upstream.commit === upstream.current)).toBe(true);
|
|
34
|
+
// Later changes to code the map links to are for the next sync to measure; unlinked code needs no entry.
|
|
35
|
+
expect(report.entries.filter((one) => one.status === "gone")).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("the texts the scripts import are the ones the map assembles, and no other", () => {
|
|
39
|
+
const made = assemble(mapping, upstream, ours);
|
|
40
|
+
expect(texts.map((one) => one.file).sort()).toEqual(made.map((one) => `${one.name}.json`).sort());
|
|
41
|
+
for (const one of made) expect(texts.find((found) => found.file === `${one.name}.json`)?.raw).toBe(jsonOf(one));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("every text equals upstream except exactly where an adapted or a dropped entry says so", () => {
|
|
45
|
+
expect(verifyTextsIn(mapping, upstream, ours, TEXTS_DIR)).toEqual([]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("no script edits a text it imports: it is given to a session as the map assembled it", () => {
|
|
49
|
+
const names = texts.map((one) => one.file.replace(/\.json$/, ""));
|
|
50
|
+
for (const { file, source } of scripts()) {
|
|
51
|
+
const bound = [...source.matchAll(/import (\w+) from "(?:\.\.?\/)+(?:workflows\/)?texts\/([\w-]+)\.json"/g)].filter((found) => names.includes(found[2] as string)).map((found) => found[1] as string);
|
|
52
|
+
for (const local of bound) {
|
|
53
|
+
const edited = new RegExp(`\\b${local}\\.text\\.(?!trim\\(\\))\\w+`).exec(source)?.[0];
|
|
54
|
+
expect({ file, local, edited }).toEqual({ file, local, edited: undefined });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("every text is imported by a script", () => {
|
|
60
|
+
const all = scripts();
|
|
61
|
+
for (const { file } of texts) {
|
|
62
|
+
const used = all.some((one) => one.source.includes(`texts/${file}"`));
|
|
63
|
+
expect({ file, used }).toEqual({ file, used: true });
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("no text names a file of upstream's that its session is not given", () => {
|
|
68
|
+
const dead = /(?:\.\.\/)?(?:references|playbooks|scripts)\/[\w./<>-]+|`[\w./-]+\.(?:md|sh)`|\]\([^)]*\.md\)/;
|
|
69
|
+
for (const { file, value } of texts) {
|
|
70
|
+
const found = dead.exec(value.text)?.[0];
|
|
71
|
+
expect({ file, found: found?.startsWith("/tmp") ? undefined : found }).toEqual({ file, found: undefined });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("babysit's sessions are given the Bugbot reference its playbook sends them to", () => {
|
|
76
|
+
const babysit = scripts().find((one) => one.file === "babysit.ts")?.source ?? "";
|
|
77
|
+
expect(babysit).toContain('texts/bugbot-triage.json"');
|
|
78
|
+
expect(babysit.match(/\.\.\.BUGBOT_REFERENCE/g)).toHaveLength(2);
|
|
79
|
+
const text = texts.find((one) => one.file === "babysit.json")?.value.text ?? "";
|
|
80
|
+
expect(text).toContain("the Bugbot triage reference given below");
|
|
81
|
+
const reference = texts.find((one) => one.file === "bugbot-triage.json")?.value.text ?? "";
|
|
82
|
+
expect(reference).toContain("## Decision rubric");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("Benny's reproduction is given the references its skill names, after the skill", () => {
|
|
86
|
+
const text = texts.find((one) => one.file === "benny-reproduce.json")?.value.text ?? "";
|
|
87
|
+
const skill = text.indexOf("# Reproduce and fix issues");
|
|
88
|
+
const verify = text.indexOf("# Verify an existing fix");
|
|
89
|
+
const adapter = text.indexOf("# Control-adapter contract");
|
|
90
|
+
expect(skill).toBeGreaterThanOrEqual(0);
|
|
91
|
+
expect(verify).toBeGreaterThan(skill);
|
|
92
|
+
expect(adapter).toBeGreaterThan(verify);
|
|
93
|
+
expect(text).toContain("the verify-existing-fix reference given after this text");
|
|
94
|
+
expect(text).toContain("the control-adapter reference given after this text");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("the playbooks that no session is given as text are code of ours, or dropped", () => {
|
|
98
|
+
for (const name of ["shipping", "worktree-cleanup"]) {
|
|
99
|
+
const entries = mapping.entries.filter((one) => one.upstream.file === `poteto-mode/playbooks/${name}.md`);
|
|
100
|
+
expect(entries.length).toBeGreaterThan(0);
|
|
101
|
+
expect(entries.every((one) => one.kind === "code" || one.kind === "dropped")).toBe(true);
|
|
102
|
+
expect(texts.some((one) => one.file === `${name}.json`)).toBe(false);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("each block of SKILL.md is dropped with its reason, or adapted into the worker rules", () => {
|
|
107
|
+
const blocks = mapping.entries.filter((one) => one.upstream.file === "poteto-mode/SKILL.md");
|
|
108
|
+
expect(blocks.length).toBeGreaterThan(20);
|
|
109
|
+
for (const one of blocks) {
|
|
110
|
+
expect(one.kind === "dropped" || one.kind === "adapted").toBe(true);
|
|
111
|
+
if (one.kind === "dropped") expect(one.note?.length).toBeGreaterThan(10);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("every session is given the three rules of the operator's skill that no other text carries", () => {
|
|
116
|
+
const worker = texts.find((one) => one.file === "worker.json")?.value.text ?? "";
|
|
117
|
+
expect(worker).toContain("restating the goal and the smallest change that meets it");
|
|
118
|
+
expect(worker).toContain("**No is an acceptable answer.**");
|
|
119
|
+
expect(worker).toContain("Write the reply in short declarative sentences");
|
|
120
|
+
const delivered = mapping.entries.filter((one) => one.upstream.file === "poteto-mode/SKILL.md" && one.kind === "adapted").map((one) => one.id);
|
|
121
|
+
expect(delivered).toEqual(["skill:05", "skill:20", "skill:24"]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("Benny's draft pull request is an explicit, recorded exception to the worker rule that every pull request is ready", () => {
|
|
125
|
+
const worker = texts.find((one) => one.file === "worker.json")?.value.text ?? "";
|
|
126
|
+
expect(worker).toContain("Open every PR ready for review, never as a draft.");
|
|
127
|
+
const reproduce = texts.find((one) => one.file === "benny-reproduce.json")?.value.text ?? "";
|
|
128
|
+
expect(reproduce).toContain('This is the one exception to the worker rule "Open every PR ready for review, never as a draft"');
|
|
129
|
+
expect(mapping.entries.some((one) => one.kind === "adapted" && one.id.startsWith("benny-reproduce") && /exception to the worker rule/.test(one.note ?? ""))).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("no playbook text sends a session to a Cursor skill, a model slug, /loop, gh, origin or agent-transcripts", () => {
|
|
133
|
+
const cursor = /\*\*[A-Za-z-]+\*\* (?:principle )?skill|principle-[a-z]|Subagents section|claude-[a-z]+-\d|\/loop|\bgh (?:api|pr)\b|`gh`|origin pr|agent-transcripts|cursor-team-kit|control-ui|control-cli|`(?:architect|unslop|tdd|why|interrogate|deslop)`|\*\*(?:architect|unslop|tdd|why|swarm|arena|interrogate|ketchup)\*\*/;
|
|
134
|
+
for (const { file, value } of texts) {
|
|
135
|
+
const found = cursor.exec(value.text)?.[0];
|
|
136
|
+
// babysit and autopilot-full name gh and origin to forbid them.
|
|
137
|
+
if (found === "`gh`" && (file === "babysit.json" || file === "autopilot-full.json")) continue;
|
|
138
|
+
expect({ file, found }).toEqual({ file, found: undefined });
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("the map tool prints the numbers of the snapshot it is run on", () => {
|
|
143
|
+
const ran = Bun.spawnSync(["bun", join(PACKAGE_DIR, "scripts", "upstream-map.ts"), "check", "--summary"], { stdout: "pipe", stderr: "pipe" });
|
|
144
|
+
expect(ran.exitCode).toBe(0);
|
|
145
|
+
const summary = JSON.parse(ran.stdout.toString().trim().split("\n").at(-1) as string) as { problems: number; coverage: number; entries: number };
|
|
146
|
+
expect(summary.problems).toBe(0);
|
|
147
|
+
expect(summary.coverage).toBeGreaterThanOrEqual(95);
|
|
148
|
+
expect(summary.entries).toBe(mapping.entries.length);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("the map tool says a map it cannot read is a problem, in the form it says the others, and does not crash", () => {
|
|
152
|
+
const file = join(tmpdir(), `broken-map-${String(process.pid)}.json`);
|
|
153
|
+
writeFileSync(file, '{"entries": [{"id": "x"}');
|
|
154
|
+
const ran = Bun.spawnSync(["bun", join(PACKAGE_DIR, "scripts", "upstream-map.ts"), "check", "--summary", "--map", file], { stdout: "pipe", stderr: "pipe" });
|
|
155
|
+
rmSync(file);
|
|
156
|
+
expect(ran.exitCode).toBe(1);
|
|
157
|
+
const summary = JSON.parse(ran.stdout.toString().trim().split("\n").at(-1) as string) as { problems: number; error: string };
|
|
158
|
+
expect(summary.problems).toBe(1);
|
|
159
|
+
expect(summary.error.length).toBeGreaterThan(0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("the summary the sync reads is one short line of numbers", () => {
|
|
163
|
+
const ran = Bun.spawnSync(["bun", join(PACKAGE_DIR, "scripts", "upstream-map.ts"), "check", "--summary"], { stdout: "pipe", stderr: "pipe" });
|
|
164
|
+
const line = ran.stdout.toString().trim();
|
|
165
|
+
expect(line.split("\n")).toHaveLength(1);
|
|
166
|
+
expect(line.length).toBeLessThan(300);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("every commit of ours the map records is in the history of HEAD", () => {
|
|
170
|
+
const git = new Git(root);
|
|
171
|
+
for (const commit of new Set(mapping.entries.flatMap((one) => one.ours.map((link) => link.commit)))) {
|
|
172
|
+
expect({ commit, ancestor: git.run(["merge-base", "--is-ancestor", commit, "HEAD"]).exit }).toEqual({ commit, ancestor: 0 });
|
|
173
|
+
}
|
|
174
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// The map tool on a package folder: the same code measures the folder of this package and the copy of it in a
|
|
2
|
+
// worktree, and every path it reads or writes comes from the folder it is given.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { differingTexts, openMap, recordMap, summaryOf, writeAssembled } from "../src/mapping/index.ts";
|
|
9
|
+
import { pathsIn, refreshUpstream } from "../src/upstream.ts";
|
|
10
|
+
import { commitFiles, git } from "./mapping-support.ts";
|
|
11
|
+
import { UPSTREAM_FILES_V1, packageRepo, upstreamCheckout } from "./package-layout.ts";
|
|
12
|
+
|
|
13
|
+
test("a package folder is measured, its texts assembled, and a changed upstream carried, with nothing read from this package", async () => {
|
|
14
|
+
const up = await upstreamCheckout();
|
|
15
|
+
const { root, packageDir } = await packageRepo(up.dir, up.commit);
|
|
16
|
+
const ctx = { paths: pathsIn(packageDir), root, checkout: up.dir };
|
|
17
|
+
|
|
18
|
+
// The map is sound; the two texts it puts into prompts are not written yet, and that is counted apart.
|
|
19
|
+
const first = summaryOf(openMap(ctx).report);
|
|
20
|
+
expect(first).toMatchObject({ problems: 0, textProblems: 2, coverage: 100, touched: 0, gone: 0 });
|
|
21
|
+
expect(differingTexts(ctx).sort()).toEqual(["bug-fix", "worker"]);
|
|
22
|
+
expect(writeAssembled(ctx)).toBe(2);
|
|
23
|
+
expect(JSON.parse(readFileSync(join(packageDir, "src", "workflows", "texts", "worker.json"), "utf8"))).toMatchObject({ name: "worker", text: "Work for the operator.\n" });
|
|
24
|
+
expect(summaryOf(openMap(ctx).report).textProblems).toBe(0);
|
|
25
|
+
expect(differingTexts(ctx)).toEqual([]);
|
|
26
|
+
|
|
27
|
+
// Upstream moves on: the snapshot is taken again, one block is touched, the text of it lags until it is assembled.
|
|
28
|
+
commitFiles(up.dir, { "skills/poteto-mode/worker.md": "Work for the operator, always.\n" }, "reword");
|
|
29
|
+
refreshUpstream(up.dir, { upstream: pathsIn(packageDir).upstream });
|
|
30
|
+
git(root, "add", "-A");
|
|
31
|
+
git(root, "commit", "-q", "-m", "take the snapshot");
|
|
32
|
+
const touched = summaryOf(openMap(ctx).report);
|
|
33
|
+
expect(touched).toMatchObject({ problems: 0, touched: 1, coverage: 100 });
|
|
34
|
+
expect(recordMap(ctx, "unchanged")).toBe(2);
|
|
35
|
+
expect(recordMap(ctx, ["worker:01"])).toBe(1);
|
|
36
|
+
expect(summaryOf(openMap(ctx).report)).toMatchObject({ touched: 0, textProblems: 1 });
|
|
37
|
+
expect(writeAssembled(ctx)).toBe(2);
|
|
38
|
+
expect(existsSync(join(packageDir, "src", "workflows", "texts", "bug-fix.json"))).toBe(true);
|
|
39
|
+
expect(UPSTREAM_FILES_V1["skills/poteto-mode/worker.md"]).toBe("Work for the operator.\n");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("recording refuses a map that has problems", async () => {
|
|
43
|
+
const up = await upstreamCheckout();
|
|
44
|
+
const { root, packageDir } = await packageRepo(up.dir, up.commit);
|
|
45
|
+
const paths = pathsIn(packageDir);
|
|
46
|
+
commitFiles(root, { "packages/pstack/upstream/mapping.json": '{"entries":[{"id":"x","upstream":{"commit":"' + up.commit + '","file":"poteto-mode/none.md","range":[0,5]},"ours":[],"kind":"dropped","note":"n"}]}' }, "break the map");
|
|
47
|
+
expect(() => recordMap({ paths, root, checkout: up.dir }, "unchanged")).toThrow("problems");
|
|
48
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// The script helpers that read trunk and carry a stack, on a real repository: git runs where `proc.run` is asked to
|
|
2
|
+
// run it, and the forge answers the pull requests a test names.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
import { mkdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { tempDir } from "@niwa-labs/testing";
|
|
9
|
+
|
|
10
|
+
import { restack, trunkAffects } from "../src/workflows/lib/trunk.ts";
|
|
11
|
+
import { commitFile, gitIn } from "./support.ts";
|
|
12
|
+
|
|
13
|
+
const globals = globalThis as Record<string, unknown>;
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
for (const name of ["call", "ask", "session"]) delete globals[name];
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const repo = { host: "gitlab.example.com", path: "acme/billing" };
|
|
19
|
+
|
|
20
|
+
/** Script globals over a forge that answers these pull requests, and a `proc.run` that runs for real. */
|
|
21
|
+
function scriptWith(prs: Record<number, { state?: string; branch: string; base?: string; head: string }>): void {
|
|
22
|
+
globals.ask = async () => ({});
|
|
23
|
+
globals.session = async () => ({});
|
|
24
|
+
globals.call = async (name: string, input: any) => {
|
|
25
|
+
if (name === "proc.run") {
|
|
26
|
+
const ran = Bun.spawnSync(input.cmd, { cwd: input.cwd, stdout: "pipe", stderr: "pipe" });
|
|
27
|
+
return { exit: ran.exitCode, tail: ran.stdout.toString() + ran.stderr.toString(), truncated: false };
|
|
28
|
+
}
|
|
29
|
+
if (name === "forge.pr.read") {
|
|
30
|
+
const pr = prs[input.number as number];
|
|
31
|
+
if (pr === undefined) throw new Error(`no pull request ${String(input.number)}`);
|
|
32
|
+
return { state: pr.state ?? "open", branch: pr.branch, base: pr.base ?? "main", head: pr.head };
|
|
33
|
+
}
|
|
34
|
+
throw new Error(`no answer for ${name}`);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function repository(): Promise<string> {
|
|
39
|
+
const dir = realpathSync(await tempDir("trunk"));
|
|
40
|
+
gitIn(dir, "init", "-q", "-b", "main");
|
|
41
|
+
mkdirSync(join(dir, "src"));
|
|
42
|
+
writeFileSync(join(dir, "src", "invoice.ts"), "export const invoice = 1;\n");
|
|
43
|
+
writeFileSync(join(dir, "src", "report.ts"), "export const report = 1;\n");
|
|
44
|
+
writeFileSync(join(dir, "package.json"), "{}\n");
|
|
45
|
+
writeFileSync(join(dir, "bun.lock"), "lock 1\n");
|
|
46
|
+
gitIn(dir, "add", ".");
|
|
47
|
+
gitIn(dir, "commit", "-q", "-m", "start");
|
|
48
|
+
return dir;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A branch off main with a commit of its own: its head. */
|
|
52
|
+
function branch(dir: string, name: string, file: string, text: string): string {
|
|
53
|
+
gitIn(dir, "checkout", "-q", "-b", name, "main");
|
|
54
|
+
const head = commitFile(dir, file, text);
|
|
55
|
+
gitIn(dir, "checkout", "-q", "main");
|
|
56
|
+
return head;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
test("what trunk did to a pull request is read from the two sides of its fork", async () => {
|
|
60
|
+
const dir = await repository();
|
|
61
|
+
const patch = branch(dir, "topic", "src/invoice.ts", "export const invoice = 2;\n");
|
|
62
|
+
const pr = { repo, number: 7 };
|
|
63
|
+
scriptWith({ 7: { branch: "topic", head: patch } });
|
|
64
|
+
expect(await trunkAffects("t1", { pr, base: "main", cwd: dir })).toEqual({ kind: "none", files: [] });
|
|
65
|
+
|
|
66
|
+
// Trunk names the patched file in a file of its own: a dependent.
|
|
67
|
+
commitFile(dir, "src/report.ts", 'import { invoice } from "./invoice";\n');
|
|
68
|
+
expect(await trunkAffects("t2", { pr, base: "main", cwd: dir })).toEqual({ kind: "dependency", files: ["src/report.ts"] });
|
|
69
|
+
|
|
70
|
+
// Trunk moved a manifest.
|
|
71
|
+
const manifest = branch(dir, "topic-2", "src/invoice.ts", "export const invoice = 3;\n");
|
|
72
|
+
gitIn(dir, "reset", "-q", "--hard", "HEAD~1");
|
|
73
|
+
commitFile(dir, "package.json", '{ "name": "billing" }\n');
|
|
74
|
+
scriptWith({ 7: { branch: "topic-2", head: manifest } });
|
|
75
|
+
expect(await trunkAffects("t3", { pr, base: "main", cwd: dir })).toEqual({ kind: "dependency", files: ["package.json"] });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a conflict, a lockfile both sides changed, and a file both sides changed are told apart", async () => {
|
|
79
|
+
const dir = await repository();
|
|
80
|
+
const pr = { repo, number: 7 };
|
|
81
|
+
const conflicting = branch(dir, "conflict", "src/invoice.ts", "export const invoice = 2;\n");
|
|
82
|
+
const locked = branch(dir, "locked", "bun.lock", "lock 2\n");
|
|
83
|
+
gitIn(dir, "checkout", "-q", "-b", "both", "main");
|
|
84
|
+
writeFileSync(join(dir, "src", "invoice.ts"), "// billing\nexport const invoice = 1;\n");
|
|
85
|
+
const both = commitFile(dir, "src/invoice.ts", "// billing\nexport const invoice = 1;\n");
|
|
86
|
+
gitIn(dir, "checkout", "-q", "main");
|
|
87
|
+
writeFileSync(join(dir, "src", "invoice.ts"), "export const invoice = 1;\nexport const tax = 1;\n");
|
|
88
|
+
writeFileSync(join(dir, "bun.lock"), "lock 1\nlock 3\n");
|
|
89
|
+
gitIn(dir, "commit", "-q", "-am", "trunk moves");
|
|
90
|
+
|
|
91
|
+
scriptWith({ 1: { branch: "conflict", head: conflicting }, 2: { branch: "locked", head: locked }, 3: { branch: "both", head: both } });
|
|
92
|
+
expect(await trunkAffects("c", { pr: { ...pr, number: 1 }, base: "main", cwd: dir })).toEqual({ kind: "conflict", files: ["src/invoice.ts"] });
|
|
93
|
+
expect((await trunkAffects("l", { pr: { ...pr, number: 2 }, base: "main", cwd: dir })).kind).toBe("conflict");
|
|
94
|
+
expect(await trunkAffects("b", { pr: { ...pr, number: 3 }, base: "main", cwd: dir })).toEqual({ kind: "both-touched", files: ["src/invoice.ts"] });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("a lockfile both sides changed without a conflict is a lockfile case", async () => {
|
|
98
|
+
const dir = await repository();
|
|
99
|
+
writeFileSync(join(dir, "bun.lock"), "a\nb\nc\nd\ne\nf\n");
|
|
100
|
+
gitIn(dir, "commit", "-q", "-am", "a longer lock");
|
|
101
|
+
const locked = branch(dir, "locked", "bun.lock", "a\nB\nc\nd\ne\nf\n");
|
|
102
|
+
writeFileSync(join(dir, "bun.lock"), "a\nb\nc\nd\ne\nF\n");
|
|
103
|
+
gitIn(dir, "commit", "-q", "-am", "trunk locks");
|
|
104
|
+
scriptWith({ 2: { branch: "locked", head: locked } });
|
|
105
|
+
expect(await trunkAffects("l", { pr: { repo, number: 2 }, base: "main", cwd: dir })).toEqual({ kind: "lockfile", files: ["bun.lock"] });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("a stack carried onto a new lower head keeps the patch of the one above, and says when it does not", async () => {
|
|
109
|
+
const dir = await repository();
|
|
110
|
+
// lower: one commit off main; upper: one commit on lower.
|
|
111
|
+
const lowerOld = branch(dir, "lower", "src/invoice.ts", "export const invoice = 2;\n");
|
|
112
|
+
gitIn(dir, "checkout", "-q", "-b", "upper", "lower");
|
|
113
|
+
const upperHead = commitFile(dir, "src/report.ts", "export const report = 2;\n");
|
|
114
|
+
// The lower pull request gets a fix round: a new head.
|
|
115
|
+
gitIn(dir, "checkout", "-q", "lower");
|
|
116
|
+
const lowerNew = commitFile(dir, "notes.md", "a fix round\n");
|
|
117
|
+
gitIn(dir, "checkout", "-q", "--detach", "main");
|
|
118
|
+
|
|
119
|
+
scriptWith({ 1: { branch: "lower", head: lowerNew }, 2: { branch: "upper", base: "lower", head: upperHead } });
|
|
120
|
+
const [moved] = await restack("r", { lower: { repo, number: 1 }, from: lowerOld, upper: [{ repo, number: 2 }], cwd: dir });
|
|
121
|
+
expect(moved?.patchKept).toBe(true);
|
|
122
|
+
expect(gitIn(dir, "rev-parse", `${moved?.head ?? ""}~1`)).toBe(lowerNew);
|
|
123
|
+
|
|
124
|
+
// The fix round changes a line the upper patch shows as context: it rebases cleanly, but it is not the patch it was.
|
|
125
|
+
const lines = (changed: Record<number, string>) => `${Array.from({ length: 10 }, (_, at) => changed[at + 1] ?? `line ${String(at + 1)}`).join("\n")}\n`;
|
|
126
|
+
gitIn(dir, "checkout", "-q", "-b", "lower-2", "main");
|
|
127
|
+
const lower2Old = commitFile(dir, "numbers.txt", lines({}));
|
|
128
|
+
gitIn(dir, "checkout", "-q", "-b", "upper-2", "lower-2");
|
|
129
|
+
const upper2 = commitFile(dir, "numbers.txt", lines({ 6: "six" }));
|
|
130
|
+
gitIn(dir, "checkout", "-q", "lower-2");
|
|
131
|
+
const lower2 = commitFile(dir, "numbers.txt", lines({ 3: "three" }));
|
|
132
|
+
gitIn(dir, "checkout", "-q", "--detach", "main");
|
|
133
|
+
scriptWith({ 1: { branch: "lower-2", head: lower2 }, 2: { branch: "upper-2", base: "lower-2", head: upper2 } });
|
|
134
|
+
const [changed] = await restack("s", { lower: { repo, number: 1 }, from: lower2Old, upper: [{ repo, number: 2 }], cwd: dir });
|
|
135
|
+
expect(changed?.patchKept).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a stack whose upper patch conflicts with the new lower head is not carried", async () => {
|
|
139
|
+
const dir = await repository();
|
|
140
|
+
const lowerOld = branch(dir, "lower", "src/invoice.ts", "export const invoice = 2;\n");
|
|
141
|
+
gitIn(dir, "checkout", "-q", "-b", "upper", "lower");
|
|
142
|
+
const upperHead = commitFile(dir, "src/invoice.ts", "export const invoice = 3;\n");
|
|
143
|
+
gitIn(dir, "checkout", "-q", "-b", "lower-fixed", lowerOld);
|
|
144
|
+
const lowerNew = commitFile(dir, "src/invoice.ts", "export const invoice = 4;\n");
|
|
145
|
+
gitIn(dir, "checkout", "-q", "--detach", "main");
|
|
146
|
+
scriptWith({ 1: { branch: "lower-fixed", head: lowerNew }, 2: { branch: "upper", head: upperHead } });
|
|
147
|
+
await expect(restack("r", { lower: { repo, number: 1 }, from: lowerOld, upper: [{ repo, number: 2 }], cwd: dir })).rejects.toThrow("does not rebase onto");
|
|
148
|
+
expect(gitIn(dir, "status", "--porcelain")).toBe("");
|
|
149
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// What trunk's changes mean for a patch: the first kind that applies, in the order of what being wrong costs.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { stemsOf, trunkKind } from "../src/trunk.ts";
|
|
6
|
+
|
|
7
|
+
const facts = (over: Partial<Parameters<typeof trunkKind>[0]>) => ({ patch: [], trunk: [], conflicts: [], dependents: [], ...over });
|
|
8
|
+
|
|
9
|
+
test("each kind is found, the costliest first", () => {
|
|
10
|
+
expect(trunkKind(facts({ patch: ["a.ts"], trunk: ["a.ts"], conflicts: ["a.ts"] }))).toEqual({ kind: "conflict", files: ["a.ts"] });
|
|
11
|
+
expect(trunkKind(facts({ patch: ["bun.lock", "a.ts"], trunk: ["bun.lock", "a.ts"] }))).toEqual({ kind: "lockfile", files: ["bun.lock"] });
|
|
12
|
+
expect(trunkKind(facts({ patch: ["a.ts"], trunk: ["a.ts", "b.ts"] }))).toEqual({ kind: "both-touched", files: ["a.ts"] });
|
|
13
|
+
expect(trunkKind(facts({ patch: ["a.ts"], trunk: ["pkg/package.json"] }))).toEqual({ kind: "dependency", files: ["pkg/package.json"] });
|
|
14
|
+
expect(trunkKind(facts({ patch: ["a.ts"], trunk: ["b.ts"], dependents: ["b.ts"] }))).toEqual({ kind: "dependency", files: ["b.ts"] });
|
|
15
|
+
expect(trunkKind(facts({ patch: ["a.ts"], trunk: ["b.ts"] }))).toEqual({ kind: "none", files: [] });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("a lockfile only trunk changed is no lockfile case", () => {
|
|
19
|
+
expect(trunkKind(facts({ patch: ["a.ts"], trunk: ["bun.lock"] })).kind).toBe("none");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("the stems of a patch's files leave out names every module has", () => {
|
|
23
|
+
expect(stemsOf(["src/billing/invoice.ts", "src/index.ts", "lib/io.ts", "src/invoice.test.ts"])).toEqual(["invoice", "invoice.test"]);
|
|
24
|
+
});
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// The upstream sync: it takes the snapshot in a worktree of its own, then measures the map and has an author work on
|
|
2
|
+
// what the report shows, round after round, until the map is settled or the rounds are spent. What would change a
|
|
3
|
+
// workflow goes to the owner. The tool and git run on the host, through the plugin's actions, on a tiny package folder
|
|
4
|
+
// and a tiny upstream checkout; the author is a fake that edits files as a boxed author can.
|
|
5
|
+
|
|
6
|
+
import { afterEach, expect, test } from "bun:test";
|
|
7
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
|
|
10
|
+
import { eventually } from "@niwa-labs/testing";
|
|
11
|
+
|
|
12
|
+
import { GATED, SYNC, UNGATED, gatedOf, scriptOf } from "../src/catalog.ts";
|
|
13
|
+
import { builtins, commitFile, gitIn, pstackHost, registeredAll } from "./support.ts";
|
|
14
|
+
import type { HarnessCall, PstackHost } from "./support.ts";
|
|
15
|
+
import { commitFiles } from "./mapping-support.ts";
|
|
16
|
+
import { UPSTREAM_FILES_V1, seedPackage, upstreamCheckout } from "./package-layout.ts";
|
|
17
|
+
|
|
18
|
+
const hosts: PstackHost[] = [];
|
|
19
|
+
afterEach(async () => {
|
|
20
|
+
for (const one of hosts.splice(0)) await one.close();
|
|
21
|
+
}, 30_000);
|
|
22
|
+
|
|
23
|
+
const WORKER = "skills/poteto-mode/worker.md";
|
|
24
|
+
const BUG_FIX = "skills/poteto-mode/playbooks/bug-fix.md";
|
|
25
|
+
const MAPPING = "packages/pstack/upstream/mapping.json";
|
|
26
|
+
|
|
27
|
+
/** A host whose repository holds a package folder that maps an upstream checkout, and the checkout. */
|
|
28
|
+
async function synced(kinds: Parameters<typeof seedPackage>[3] = {}): Promise<{ made: PstackHost; up: { dir: string; commit: string } }> {
|
|
29
|
+
const made = await pstackHost({ benny: false });
|
|
30
|
+
hosts.push(made);
|
|
31
|
+
const up = await upstreamCheckout();
|
|
32
|
+
seedPackage(made.repo, up.dir, up.commit, kinds);
|
|
33
|
+
await registeredAll(made.host, gatedOf(false).length + UNGATED.length);
|
|
34
|
+
return { made, up };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Start the sync and wait for its end. */
|
|
38
|
+
async function sync(made: PstackHost, up: { dir: string }, args: Record<string, unknown> = {}): Promise<any> {
|
|
39
|
+
// The sync is gated: until the suite promoted it, a built-in's candidate is started only by a run of pstack's own, so the
|
|
40
|
+
// script is run on its file, as its own rights, here. The suite's scenario starts it as a candidate.
|
|
41
|
+
const ended = (await made.host.call("workflow.start" as never, {
|
|
42
|
+
file: scriptOf(SYNC),
|
|
43
|
+
args: { repo: { local: made.repo }, worktrees: made.worktrees, base: "main", upstream: { checkout: up.dir }, ...args },
|
|
44
|
+
wait: true,
|
|
45
|
+
timeoutMs: 120_000,
|
|
46
|
+
} as never)) as { outcome?: { status: string; result?: unknown }; reason?: string };
|
|
47
|
+
if (ended.outcome?.status !== "done") throw new Error(`the sync did not finish: ${JSON.stringify(ended).slice(0, 2000)}`);
|
|
48
|
+
return ended.outcome.result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What the measurement left in the worktree for the author. */
|
|
52
|
+
const reportOf = (call: HarnessCall): any => JSON.parse(readFileSync(join(call.cwd as string, ".upstream-map-report.json"), "utf8"));
|
|
53
|
+
|
|
54
|
+
/** What a boxed author does: it edits files and answers; it cannot commit. The ids of the touched entries are settled as carried. */
|
|
55
|
+
const accepts = (call: HarnessCall) => ({ summary: "settled", decisions: [], accept: reportOf(call).entries.filter((one: { status: string }) => one.status === "touched").map((one: { id: string }) => one.id) });
|
|
56
|
+
|
|
57
|
+
/** Upstream rewords the worker rules: the entry that holds them is touched. */
|
|
58
|
+
const reword = (up: { dir: string }): string => commitFiles(up.dir, { [WORKER]: "Work for the operator, always.\n" }, "reword");
|
|
59
|
+
|
|
60
|
+
test("the author settles what the report shows, and the texts are assembled from the map once it is settled", async () => {
|
|
61
|
+
const { made, up } = await synced();
|
|
62
|
+
const next = reword(up);
|
|
63
|
+
made.harnesses.answers["upstream-sync:round"] = accepts;
|
|
64
|
+
const result = await sync(made, up);
|
|
65
|
+
|
|
66
|
+
expect(result.status).toBe("synced");
|
|
67
|
+
expect(result.upstream).toBe(next);
|
|
68
|
+
expect(result.summary).toMatchObject({ problems: 0, textProblems: 0, touched: 0, coverage: 100 });
|
|
69
|
+
expect(result.rounds).toHaveLength(1);
|
|
70
|
+
expect(result.changed).toContain("packages/pstack/src/workflows/texts/worker.json");
|
|
71
|
+
const subjects = gitIn(result.worktree, "log", "--format=%s", "main..HEAD").split("\n");
|
|
72
|
+
expect(subjects).toEqual(["Assemble the playbook texts from the map", "Record the upstream ranges that did not change", `Take upstream pstack at ${next.slice(0, 9)}`]);
|
|
73
|
+
expect(JSON.parse(readFileSync(join(result.worktree, "packages/pstack/src/workflows/texts/worker.json"), "utf8")).text).toBe("Work for the operator, always.\n");
|
|
74
|
+
expect(gitIn(result.worktree, "ls-files", ".upstream-map-report.json")).toBe("");
|
|
75
|
+
expect(gitIn(result.worktree, "rev-parse", "HEAD")).toBe(result.head);
|
|
76
|
+
|
|
77
|
+
const asked = made.harnesses.of("upstream-sync:round");
|
|
78
|
+
expect(asked).toHaveLength(1);
|
|
79
|
+
expect(asked[0]?.harness).toBe("author");
|
|
80
|
+
expect(asked[0]?.cwd).toBe(result.worktree);
|
|
81
|
+
expect(asked[0]?.prompt).toContain(".upstream-map-report.json");
|
|
82
|
+
expect(asked[0]?.prompt).toContain('"touched":1');
|
|
83
|
+
expect(asked[0]?.prompt).toContain("`accept`");
|
|
84
|
+
// The upstream checkout was only read.
|
|
85
|
+
expect(gitIn(up.dir, "status", "--porcelain")).toBe("");
|
|
86
|
+
}, 120_000);
|
|
87
|
+
|
|
88
|
+
test("a map that is settled at the start needs no round, and its texts, which lag it, are assembled", async () => {
|
|
89
|
+
const { made, up } = await synced();
|
|
90
|
+
const result = await sync(made, up);
|
|
91
|
+
expect(result.status).toBe("synced");
|
|
92
|
+
expect(result.rounds).toEqual([]);
|
|
93
|
+
expect(result.summary).toMatchObject({ touched: 0, textProblems: 0 });
|
|
94
|
+
expect(made.harnesses.of("upstream-sync:round")).toEqual([]);
|
|
95
|
+
expect(existsSync(join(result.worktree, "packages/pstack/src/workflows/texts/bug-fix.json"))).toBe(true);
|
|
96
|
+
}, 120_000);
|
|
97
|
+
|
|
98
|
+
test("a round that settles nothing while the map is unsettled stops the run as stalled, and the texts are not assembled", async () => {
|
|
99
|
+
const { made, up } = await synced();
|
|
100
|
+
reword(up);
|
|
101
|
+
made.harnesses.answers["upstream-sync:round"] = () => ({ summary: "looked", decisions: [], accept: [] });
|
|
102
|
+
const result = await sync(made, up);
|
|
103
|
+
|
|
104
|
+
expect(result.status).toBe("stalled");
|
|
105
|
+
expect(result.summary.touched).toBe(1);
|
|
106
|
+
expect(result.rounds).toHaveLength(1);
|
|
107
|
+
expect(result.changed).not.toContain("packages/pstack/src/workflows/texts/worker.json");
|
|
108
|
+
}, 120_000);
|
|
109
|
+
|
|
110
|
+
test("the rounds are bounded: what is left after them is reported, not worked on further", async () => {
|
|
111
|
+
const { made, up } = await synced();
|
|
112
|
+
reword(up);
|
|
113
|
+
let tries = 0;
|
|
114
|
+
made.harnesses.answers["upstream-sync:round"] = (call) => {
|
|
115
|
+
tries += 1;
|
|
116
|
+
writeFileSync(join(call.cwd as string, "packages/pstack", `notes-${String(tries)}.md`), "tried\n");
|
|
117
|
+
return { summary: "tried", decisions: [], accept: [] };
|
|
118
|
+
};
|
|
119
|
+
const result = await sync(made, up, { rounds: 2 });
|
|
120
|
+
|
|
121
|
+
expect(result.status).toBe("unfinished");
|
|
122
|
+
expect(result.rounds).toHaveLength(2);
|
|
123
|
+
expect(result.summary.touched).toBe(1);
|
|
124
|
+
expect(made.harnesses.of("upstream-sync:round")).toHaveLength(2);
|
|
125
|
+
// What the author edited is committed for it, as it cannot commit.
|
|
126
|
+
expect(gitIn(result.worktree, "ls-files", "packages/pstack/notes-1.md")).toBe("packages/pstack/notes-1.md");
|
|
127
|
+
}, 120_000);
|
|
128
|
+
|
|
129
|
+
test("a coverage below the minimum is worked on like a touched entry", async () => {
|
|
130
|
+
const { made, up } = await synced();
|
|
131
|
+
commitFiles(up.dir, { [BUG_FIX]: `${UPSTREAM_FILES_V1[BUG_FIX]}\nA new paragraph that nothing maps yet.\n` }, "add a paragraph");
|
|
132
|
+
made.harnesses.answers["upstream-sync:round"] = (call) => {
|
|
133
|
+
const report = reportOf(call);
|
|
134
|
+
const bug = report.coverage.files.find((one: { file: string }) => one.file === "poteto-mode/playbooks/bug-fix.md");
|
|
135
|
+
const gap = bug.uncovered.at(-1) as { start: number; end: number };
|
|
136
|
+
const path = join(call.cwd as string, MAPPING);
|
|
137
|
+
const map = JSON.parse(readFileSync(path, "utf8")) as { entries: Array<Record<string, any>> };
|
|
138
|
+
const own = map.entries.find((one) => one["id"] === "bug-fix:01") as Record<string, any>;
|
|
139
|
+
map.entries.push({ id: "bug-fix:02", upstream: { ...own["upstream"], commit: report.upstream.commit, range: [gap.start, gap.end] }, ours: own["ours"], kind: "prompt" });
|
|
140
|
+
writeFileSync(path, JSON.stringify(map));
|
|
141
|
+
return { summary: "mapped the paragraph", decisions: [], accept: [] };
|
|
142
|
+
};
|
|
143
|
+
const result = await sync(made, up, { min: 100 });
|
|
144
|
+
expect(result.status).toBe("synced");
|
|
145
|
+
expect(result.summary.coverage).toBe(100);
|
|
146
|
+
expect(made.harnesses.of("upstream-sync:round")[0]?.prompt).toContain("must reach 100%");
|
|
147
|
+
expect(JSON.parse(readFileSync(join(result.worktree, "packages/pstack/src/workflows/texts/bug-fix.json"), "utf8")).text).toContain("A new paragraph");
|
|
148
|
+
}, 120_000);
|
|
149
|
+
|
|
150
|
+
test("what would change a workflow is asked of the owner once, and its answer is carried out in a last round", async () => {
|
|
151
|
+
const { made, up } = await synced();
|
|
152
|
+
reword(up);
|
|
153
|
+
made.harnesses.answers["upstream-sync:round"] = (call) =>
|
|
154
|
+
call.prompt.includes("The owner answered")
|
|
155
|
+
? (writeFileSync(join(call.cwd as string, "packages/pstack/owner.md"), "done\n"), { summary: "carried out", decisions: [], accept: [] })
|
|
156
|
+
: { ...accepts(call), decisions: [{ question: "Deliver the autonomy rules to every session?", why: "It would change every prompt." }] };
|
|
157
|
+
const running = sync(made, up);
|
|
158
|
+
let id: string | undefined;
|
|
159
|
+
await eventually(
|
|
160
|
+
async () => {
|
|
161
|
+
const open = (await made.asOwner("request.open", {})) as { items?: Array<{ id: string }>; requests?: Array<{ id: string }> };
|
|
162
|
+
id = (open.items ?? open.requests ?? [])[0]?.id;
|
|
163
|
+
return id !== undefined;
|
|
164
|
+
},
|
|
165
|
+
30_000,
|
|
166
|
+
"the owner was not asked",
|
|
167
|
+
);
|
|
168
|
+
await made.asOwner("request.answer", { id, answer: { text: "Yes, deliver them." } });
|
|
169
|
+
const result = await running;
|
|
170
|
+
|
|
171
|
+
expect(result.status).toBe("synced");
|
|
172
|
+
expect(result.decisions).toEqual([{ question: "Deliver the autonomy rules to every session?", why: "It would change every prompt.", answer: "Yes, deliver them." }]);
|
|
173
|
+
const rounds = made.harnesses.of("upstream-sync:round");
|
|
174
|
+
expect(rounds).toHaveLength(2);
|
|
175
|
+
expect(rounds[1]?.prompt).toContain("- Deliver the autonomy rules to every session?: Yes, deliver them.");
|
|
176
|
+
expect(gitIn(result.worktree, "ls-files", "packages/pstack/owner.md")).toBe("packages/pstack/owner.md");
|
|
177
|
+
}, 120_000);
|
|
178
|
+
|
|
179
|
+
test("a map the author left broken is not a failed run: it is measured as a problem, and the next round mends it", async () => {
|
|
180
|
+
const { made, up } = await synced();
|
|
181
|
+
const good = readFileSync(join(made.repo, MAPPING), "utf8");
|
|
182
|
+
commitFile(made.repo, MAPPING, '{"entries": [{"id": "x"');
|
|
183
|
+
made.harnesses.answers["upstream-sync:round"] = (call) => {
|
|
184
|
+
writeFileSync(join(call.cwd as string, MAPPING), good);
|
|
185
|
+
return { summary: "mended the map", decisions: [], accept: [] };
|
|
186
|
+
};
|
|
187
|
+
const result = await sync(made, up);
|
|
188
|
+
|
|
189
|
+
expect(result.status).toBe("synced");
|
|
190
|
+
expect(result.rounds).toHaveLength(1);
|
|
191
|
+
expect(made.harnesses.of("upstream-sync:round")[0]?.prompt).toContain('"problems":1');
|
|
192
|
+
}, 120_000);
|
|
193
|
+
|
|
194
|
+
test("texts the assembled map still cannot make equal to it are not a finished sync, though the map is settled", async () => {
|
|
195
|
+
// A block of a playbook that goes into a text and is left out as code is a text problem that assembling does not mend.
|
|
196
|
+
const { made, up } = await synced();
|
|
197
|
+
const map = JSON.parse(readFileSync(join(made.repo, MAPPING), "utf8")) as { entries: Array<Record<string, any>> };
|
|
198
|
+
const whole = map.entries.find((one) => one["id"] === "bug-fix:01") as Record<string, any>;
|
|
199
|
+
const [start, end] = whole["upstream"].range as [number, number];
|
|
200
|
+
map.entries.splice(map.entries.indexOf(whole), 1, { ...whole, upstream: { ...whole["upstream"], range: [start, 10] } }, { ...whole, id: "bug-fix:02", kind: "code", upstream: { ...whole["upstream"], range: [10, end] } });
|
|
201
|
+
commitFile(made.repo, MAPPING, JSON.stringify(map));
|
|
202
|
+
const result = await sync(made, up);
|
|
203
|
+
expect(result.status).toBe("unfinished");
|
|
204
|
+
expect(result.rounds).toEqual([]);
|
|
205
|
+
expect(result.summary).toMatchObject({ problems: 0, touched: 0 });
|
|
206
|
+
expect(result.summary.textProblems).toBeGreaterThan(0);
|
|
207
|
+
}, 120_000);
|
|
208
|
+
|
|
209
|
+
test("the sync is gated like the playbooks: a new version of it is a candidate that starts on nothing", async () => {
|
|
210
|
+
expect(GATED.map((one) => one.name)).toContain(SYNC);
|
|
211
|
+
expect(UNGATED.map((one) => one.name)).not.toContain(SYNC);
|
|
212
|
+
const { made } = await synced();
|
|
213
|
+
const id = (await builtins(made.host))[SYNC]?.id;
|
|
214
|
+
await expect(made.host.call("workflow.start" as never, { id, args: {} } as never)).rejects.toThrow("candidate");
|
|
215
|
+
}, 60_000);
|
|
216
|
+
|
|
217
|
+
test("the sync runs its tool and git on the host: no step of it is a command in a box", async () => {
|
|
218
|
+
const source = readFileSync(scriptOf(SYNC), "utf8");
|
|
219
|
+
const uses = /uses: \[([^\]]*)\]/.exec(source)?.[1] ?? "";
|
|
220
|
+
expect(uses).toContain("pstack.upstream.measure");
|
|
221
|
+
expect(uses).not.toContain("proc.run");
|
|
222
|
+
const code = source.split("\n").filter((line) => !line.trim().startsWith("//")).join("\n");
|
|
223
|
+
expect(code).not.toMatch(/proc\.run|gitOut|lib\/git/);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("the upstream actions refuse a place that is not one of niwa's worktrees, and write the checkout never", async () => {
|
|
227
|
+
const { made, up } = await synced();
|
|
228
|
+
const inputs = { checkout: up.dir };
|
|
229
|
+
for (const place of [made.repo, made.worktrees, "/nowhere", ""]) {
|
|
230
|
+
await expect(made.asOwner("pstack.upstream.refresh", { place, ...inputs })).rejects.toThrow();
|
|
231
|
+
await expect(made.asOwner("pstack.upstream.measure", { place, ...inputs })).rejects.toThrow();
|
|
232
|
+
await expect(made.asOwner("pstack.upstream.commit", { place, message: "m", paths: ["packages"] })).rejects.toThrow();
|
|
233
|
+
await expect(made.asOwner("pstack.upstream.state", { place })).rejects.toThrow();
|
|
234
|
+
}
|
|
235
|
+
const wt = (await made.asOwner("git.worktree.create", { repo: made.repo, base: "main", description: "a worktree" })) as { path: string };
|
|
236
|
+
// A package folder or a path that leads out of the worktree is refused too.
|
|
237
|
+
await expect(made.asOwner("pstack.upstream.measure", { place: wt.path, package: "../repo/packages/pstack", ...inputs })).rejects.toThrow();
|
|
238
|
+
await expect(made.asOwner("pstack.upstream.commit", { place: wt.path, message: "m", paths: ["/etc/hosts"] })).rejects.toThrow();
|
|
239
|
+
// On a worktree they work, and the checkout stays as it was.
|
|
240
|
+
const before = gitIn(up.dir, "status", "--porcelain=v2", "--branch");
|
|
241
|
+
const measured = (await made.asOwner("pstack.upstream.measure", { place: wt.path, ...inputs })) as { problems: number; coverage: number };
|
|
242
|
+
expect(measured).toMatchObject({ problems: 0, coverage: 100 });
|
|
243
|
+
expect(await made.asOwner("pstack.upstream.refresh", { place: wt.path, ...inputs })).toEqual({ commit: up.commit });
|
|
244
|
+
expect(gitIn(up.dir, "status", "--porcelain=v2", "--branch")).toBe(before);
|
|
245
|
+
expect(existsSync(join(up.dir, ".upstream-map-report.json"))).toBe(false);
|
|
246
|
+
}, 120_000);
|