lake-gate 0.0.0-stage → 0.2.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 +152 -2
- package/action.yml +21 -0
- package/bin/lake-gate.js +3 -0
- package/package.json +48 -4
- package/src/action.js +75 -0
- package/src/cli.js +124 -0
- package/src/config-core.js +38 -0
- package/src/config.js +10 -0
- package/src/estimate.js +70 -0
- package/src/github.js +170 -0
- package/src/hook.js +73 -0
- package/src/index.js +10 -0
- package/src/init.js +62 -0
- package/src/mcp.js +312 -0
- package/src/meter-core.js +535 -0
- package/src/models.js +193 -0
- package/src/plan.js +90 -0
- package/src/policy.js +57 -0
- package/src/review.js +319 -0
- package/src/sha256.js +37 -0
- package/src/store.js +90 -0
- package/src/sync.js +50 -0
- package/src/ui-server.js +81 -0
- package/src/workspace.js +104 -0
- package/templates/lake-gate.yml +28 -0
- package/templates/plans-README.md +10 -0
- package/ui/app.css +536 -0
- package/ui/app.js +836 -0
- package/ui/btn/approve.svg +5 -0
- package/ui/btn/changes.svg +5 -0
- package/ui/btn/open.svg +5 -0
- package/ui/data.js +95 -0
- package/ui/demo.js +262 -0
- package/ui/index.html +55 -0
- package/ui/inter.woff2 +0 -0
- package/ui/logo-mark.svg +1 -0
- package/ui/md.js +82 -0
package/src/workspace.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Reads review state for plans from GitHub PRs and writes review actions back as PR comments.
|
|
2
|
+
// Browser-safe: used by the Lake review UI (directly or through `lake-gate ui`'s local proxy) and by the MCP server.
|
|
3
|
+
import { parseConfig } from './config-core.js';
|
|
4
|
+
import { resolveBudgetApprovers } from './github.js';
|
|
5
|
+
import { applyPatch } from './models.js';
|
|
6
|
+
import { splitFrontmatter, renderPlanFile, readPlanText } from './plan.js';
|
|
7
|
+
import { estimatePlan, renderEstimate } from './estimate.js';
|
|
8
|
+
import { buildPlanReview, versionsFromSnapshots, normalizeIssueComment, normalizeReview, normalizeLineComment,
|
|
9
|
+
commentBody, replyBody, resolveBody, verdictBody, suggestionBody, acceptBody } from './review.js';
|
|
10
|
+
|
|
11
|
+
export const isPlanFile = (f) => /^\.lake\/plans\/[^/]+\.md$/.test(f) && !f.endsWith('/README.md');
|
|
12
|
+
const MAX_COMMITS = 60;
|
|
13
|
+
|
|
14
|
+
export async function loadPr(gh, number, { pr: prIn } = {}) {
|
|
15
|
+
const pr = prIn && prIn.base ? prIn : await gh.req('GET', `/pulls/${number}`);
|
|
16
|
+
const [cfgText, files, commitsAll, comments, reviews, lines] = await Promise.all([
|
|
17
|
+
gh.file('.lake/config.json', pr.base.sha), gh.all(`/pulls/${number}/files`), gh.all(`/pulls/${number}/commits`),
|
|
18
|
+
gh.all(`/issues/${number}/comments`), gh.all(`/pulls/${number}/reviews`), gh.all(`/pulls/${number}/comments`)]);
|
|
19
|
+
const config = parseConfig(cfgText);
|
|
20
|
+
const commits = commitsAll.slice(-MAX_COMMITS);
|
|
21
|
+
const events = [...comments.map(normalizeIssueComment), ...reviews.filter((r) => r.state !== 'DISMISSED' && r.state !== 'PENDING').map(normalizeReview), ...lines.map(normalizeLineComment)];
|
|
22
|
+
const requestedReviewers = (pr.requested_reviewers || []).map((u) => u.login);
|
|
23
|
+
const budgetApprovers = await resolveBudgetApprovers(gh, config);
|
|
24
|
+
const plans = [];
|
|
25
|
+
for (const f of files.filter((x) => isPlanFile(x.filename) && x.status !== 'removed')) {
|
|
26
|
+
const path = f.filename; const fallbackId = path.split('/').pop().slice(0, -3);
|
|
27
|
+
// Which PR commits touched this file? One call, then one content read per touching commit.
|
|
28
|
+
const touching = new Set(((await gh.req('GET', `/commits?sha=${encodeURIComponent(pr.head.sha)}&path=${encodeURIComponent(path)}&per_page=100`)) || []).map((c) => c.sha));
|
|
29
|
+
const snaps = []; const digestAt = {}; let lastText = null;
|
|
30
|
+
for (const c of commits) {
|
|
31
|
+
if (touching.has(c.sha) || lastText === null) {
|
|
32
|
+
const text = await gh.file(path, c.sha);
|
|
33
|
+
if (text != null) { lastText = text; snaps.push({ text, sha: c.sha, at: c.commit?.committer?.date || c.commit?.author?.date || null, author: c.author?.login || c.commit?.author?.name || null }); }
|
|
34
|
+
}
|
|
35
|
+
if (lastText != null) digestAt[c.sha] = versionsFromSnapshots([{ text: lastText }], fallbackId)[0].digest;
|
|
36
|
+
}
|
|
37
|
+
const headText = await gh.file(path, pr.head.sha);
|
|
38
|
+
if (headText == null) continue;
|
|
39
|
+
snaps.push({ text: headText, sha: pr.head.sha, at: commits.at(-1)?.commit?.committer?.date || pr.updated_at, author: pr.user.login });
|
|
40
|
+
const versions = versionsFromSnapshots(snaps, fallbackId);
|
|
41
|
+
const plan = versions.find((v) => v.digest === versionsFromSnapshots([{ text: headText }], fallbackId)[0].digest);
|
|
42
|
+
digestAt[pr.head.sha] = plan.digest;
|
|
43
|
+
plans.push(buildPlanReview({ plan, versions, events, prAuthor: pr.user.login, config, digestAt, pr, requestedReviewers, budgetApprovers }));
|
|
44
|
+
}
|
|
45
|
+
return { pr, config, plans, events };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// All plans in recent PRs of a repo (open first). One page of PRs keeps API use predictable.
|
|
49
|
+
export async function loadWorkspace(gh, { state = 'all', limit = 20 } = {}) {
|
|
50
|
+
const prs = ((await gh.req('GET', `/pulls?state=${state}&sort=updated&direction=desc&per_page=${limit}`)) || []);
|
|
51
|
+
const out = [];
|
|
52
|
+
for (const pr of prs) {
|
|
53
|
+
const files = await gh.all(`/pulls/${pr.number}/files`);
|
|
54
|
+
if (!files.some((f) => isPlanFile(f.filename) && f.status !== 'removed')) continue;
|
|
55
|
+
const r = await loadPr(gh, pr.number);
|
|
56
|
+
out.push(...r.plans);
|
|
57
|
+
}
|
|
58
|
+
return { repo: gh.repo, plans: out };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---- Writes: everything is a PR comment (or a reply on a native line comment) ----
|
|
62
|
+
async function postIssueComment(gh, number, body) {
|
|
63
|
+
const c = await gh.req('POST', `/issues/${number}/comments`, { body });
|
|
64
|
+
return normalizeIssueComment(c);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const actions = {
|
|
68
|
+
comment: (gh, number, o) => postIssueComment(gh, number, commentBody(o)),
|
|
69
|
+
reply: async (gh, number, o) => {
|
|
70
|
+
if (String(o.thread).startsWith('l')) { // native GitHub line comment thread: reply natively
|
|
71
|
+
const c = await gh.req('POST', `/pulls/${number}/comments/${String(o.thread).slice(1)}/replies`, { body: o.text });
|
|
72
|
+
return normalizeLineComment({ ...c, in_reply_to_id: c.in_reply_to_id || Number(String(o.thread).slice(1)) });
|
|
73
|
+
}
|
|
74
|
+
return postIssueComment(gh, number, replyBody(o));
|
|
75
|
+
},
|
|
76
|
+
resolve: (gh, number, o) => postIssueComment(gh, number, resolveBody(o)),
|
|
77
|
+
verdict: (gh, number, o) => postIssueComment(gh, number, verdictBody(o)),
|
|
78
|
+
suggest: (gh, number, o) => postIssueComment(gh, number, suggestionBody(o)),
|
|
79
|
+
// Author accepts a suggestion: commit the patched plan file to the PR branch, then mark the thread accepted.
|
|
80
|
+
accept: async (gh, number, o) => {
|
|
81
|
+
const pr = await gh.req('GET', `/pulls/${number}`);
|
|
82
|
+
const path = `.lake/plans/${o.planId}.md`;
|
|
83
|
+
const cur = await gh.req('GET', `/contents/${path.split('/').map(encodeURIComponent).join('/')}?ref=${encodeURIComponent(pr.head.ref)}`);
|
|
84
|
+
if (!cur) throw new Error(`${path} not found on ${pr.head.ref}`);
|
|
85
|
+
const config = parseConfig(await gh.file('.lake/config.json', pr.base.sha));
|
|
86
|
+
const text = patchPlanFile(fromB64(cur.content), o.patch, config);
|
|
87
|
+
await gh.req('PUT', `/contents/${path.split('/').map(encodeURIComponent).join('/')}`, { message: `Plan ${o.planId}: ${o.title || 'apply suggestion'}`, content: toB64(text), sha: cur.sha, branch: pr.head.ref });
|
|
88
|
+
const nv = readPlanText(text).digest;
|
|
89
|
+
return postIssueComment(gh, number, acceptBody({ ...o, newVersion: nv }));
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// The whole plan file with a suggestion applied and its estimate block regenerated.
|
|
94
|
+
export function patchPlanFile(fileText, patch, config) {
|
|
95
|
+
const { meta, body } = splitFrontmatter(fileText);
|
|
96
|
+
const next = applyPatch(body.replace(/<!-- lake:estimate:start[\s\S]*?<!-- lake:estimate:end -->\n?/, '').replace(/\s+$/, '') + '\n', patch);
|
|
97
|
+
return renderPlanFile({ id: meta.id, title: meta.title || readPlanText(fileText).title, author: meta.author, source: meta.source, created: meta.created, body: next, estimateMd: renderEstimate(estimatePlan(next, config), config) });
|
|
98
|
+
}
|
|
99
|
+
const fromB64 = (b) => new TextDecoder().decode(Uint8Array.from(atob(String(b).replace(/\s/g, '')), (c) => c.charCodeAt(0)));
|
|
100
|
+
const toB64 = (t) => { const u = new TextEncoder().encode(t); let s = ''; for (const x of u) s += String.fromCharCode(x); return btoa(s); };
|
|
101
|
+
|
|
102
|
+
export async function viewer(gh) {
|
|
103
|
+
try { return await gh.req('GET', '~/user'); } catch { return null; }
|
|
104
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Lake plan gate: checks that every plan in .lake/plans/ touched by a PR is approved,
|
|
2
|
+
# for its current version, by someone other than the PR author.
|
|
3
|
+
# Make "lake/plan-approval" a required status check in branch protection to enforce it.
|
|
4
|
+
name: Lake plan gate
|
|
5
|
+
on:
|
|
6
|
+
pull_request:
|
|
7
|
+
types: [opened, synchronize, reopened, ready_for_review]
|
|
8
|
+
pull_request_review:
|
|
9
|
+
types: [submitted, edited, dismissed]
|
|
10
|
+
issue_comment:
|
|
11
|
+
types: [created, edited, deleted]
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
pull-requests: read
|
|
15
|
+
issues: write
|
|
16
|
+
statuses: write
|
|
17
|
+
concurrency:
|
|
18
|
+
group: lake-gate-${{ github.event.pull_request.number || github.event.issue.number }}
|
|
19
|
+
cancel-in-progress: false
|
|
20
|
+
jobs:
|
|
21
|
+
plan-gate:
|
|
22
|
+
if: github.event_name != 'issue_comment' || (github.event.issue.pull_request && contains(github.event.comment.body, '/lake'))
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
timeout-minutes: 5
|
|
25
|
+
steps:
|
|
26
|
+
- uses: __ACTION_REF__
|
|
27
|
+
with:
|
|
28
|
+
token: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Plans
|
|
2
|
+
|
|
3
|
+
Each file here is an implementation plan an AI coding agent will follow, with a generated cost estimate.
|
|
4
|
+
|
|
5
|
+
- Approve a plan on its PR with a comment: `/lake approve <plan-id>@<version>` (the PR bot comment shows the exact line).
|
|
6
|
+
- Request changes: `/lake reject <plan-id>@<version>`. Withdraw: `/lake revoke <plan-id>`.
|
|
7
|
+
- The PR author cannot approve their own plan. Bots cannot approve.
|
|
8
|
+
- Editing a plan changes its version and needs a fresh approval. The generated "Lake estimate" block is not part of the version.
|
|
9
|
+
|
|
10
|
+
Made with lake-gate. No telemetry: nothing leaves your repo except what GitHub already runs.
|