@veda-hq/niyam 0.6.16 → 0.6.18
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/README.md +261 -48
- package/dist/agent.js +4 -0
- package/dist/as-agent.js +26 -18
- package/dist/board.js +3 -3
- package/dist/brief.js +370 -0
- package/dist/cloud.js +28 -11
- package/dist/cubic-dismiss.js +284 -0
- package/dist/doctor.js +3 -6
- package/dist/handoff.js +527 -0
- package/dist/issue-types.js +2 -2
- package/dist/keep-going.js +384 -0
- package/dist/onboard.js +45 -16
- package/dist/pr-autocreate.js +984 -0
- package/dist/release-tag.js +40 -0
- package/dist/run.js +55 -5
- package/dist/session-start.js +2 -2
- package/dist/sys.js +44 -1
- package/dist/upgrade.js +7 -0
- package/dist/work.js +4 -0
- package/package.json +1 -1
package/dist/brief.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { API } from './agent.js';
|
|
2
|
+
import { restList } from './pages.js';
|
|
3
|
+
/**
|
|
4
|
+
* A hand-off brief: what a session that tripped over a problem it did not cause writes for the session that will
|
|
5
|
+
* fix it. Its fields ride in a fenced `yaml brief` block at the top of a Brief issue, and its five sections follow.
|
|
6
|
+
* The structure is the point: a handoff in this shape (I-PASS) cut medical errors by a quarter without being longer.
|
|
7
|
+
*/
|
|
8
|
+
/** The issue type a brief is opened as. */
|
|
9
|
+
export const BRIEF_TYPE = 'Brief';
|
|
10
|
+
/** The info string of the fields block, so it is never mistaken for evidence. */
|
|
11
|
+
export const FIELDS_FENCE = '```yaml brief';
|
|
12
|
+
/** Fields the issue's own state replaces: open, closed as completed (resolved), or closed as not planned (rejected). */
|
|
13
|
+
export const STATE_KEYS = ['status', 'resolved-by', 'resolution-note'];
|
|
14
|
+
/** The sections every brief answers, in I-PASS order. */
|
|
15
|
+
export const SECTIONS = [
|
|
16
|
+
'## What happened',
|
|
17
|
+
'## Why this is not caused by the originating branch',
|
|
18
|
+
'## Hypothesis (UNVERIFIED)',
|
|
19
|
+
'## What the receiving session must verify first',
|
|
20
|
+
'## Sequencing',
|
|
21
|
+
];
|
|
22
|
+
/** What a session that carries the fix itself owes on top. */
|
|
23
|
+
export const CARRYING_SECTIONS = ['## What I verified', '## Why I did not hand off'];
|
|
24
|
+
/** The section that must hold pasted evidence, not a description of it. */
|
|
25
|
+
export const EVIDENCE_SECTION = '## What happened';
|
|
26
|
+
export const STATUSES = ['open', 'resolved', 'rejected'];
|
|
27
|
+
/** The YAML reader, loaded only when a brief is read: the shared checks run the CLI from its source with no packages installed. */
|
|
28
|
+
export async function loadYaml() {
|
|
29
|
+
const { parse } = await import('yaml');
|
|
30
|
+
return (text) => parse(text, { schema: 'core' });
|
|
31
|
+
}
|
|
32
|
+
/** A path as `git diff --name-only` spells it, or null for one outside the repository. */
|
|
33
|
+
export function repoPath(path) {
|
|
34
|
+
const normalised = path.trim().replace(/\\/g, '/');
|
|
35
|
+
if (normalised.startsWith('/'))
|
|
36
|
+
return null;
|
|
37
|
+
const out = [];
|
|
38
|
+
for (const segment of normalised.split('/')) {
|
|
39
|
+
if (segment === '' || segment === '.')
|
|
40
|
+
continue;
|
|
41
|
+
if (segment === '..') {
|
|
42
|
+
if (out.length === 0)
|
|
43
|
+
return null;
|
|
44
|
+
out.pop();
|
|
45
|
+
}
|
|
46
|
+
else
|
|
47
|
+
out.push(segment);
|
|
48
|
+
}
|
|
49
|
+
return out.length === 0 ? null : out.join('/');
|
|
50
|
+
}
|
|
51
|
+
/** A commit id, not a ref: a ref is an ancestor of itself, so it would release a blocker with no fix. */
|
|
52
|
+
export function isCommitSha(value) {
|
|
53
|
+
return /^[0-9a-f]{7,40}$/i.test(value.trim());
|
|
54
|
+
}
|
|
55
|
+
/** The frontmatter between the first two `---` lines, and the body after; null without one. */
|
|
56
|
+
export function splitFrontmatter(markdown) {
|
|
57
|
+
const lines = markdown.split(/\r?\n/);
|
|
58
|
+
if (lines[0] !== '---')
|
|
59
|
+
return null;
|
|
60
|
+
const end = lines.indexOf('---', 1);
|
|
61
|
+
if (end === -1)
|
|
62
|
+
return null;
|
|
63
|
+
return { frontmatter: lines.slice(1, end).join('\n'), body: lines.slice(end + 1).join('\n') };
|
|
64
|
+
}
|
|
65
|
+
/** The body's `## ` sections by heading; a heading inside a fence is captured output, not a section. */
|
|
66
|
+
export function splitSections(markdown) {
|
|
67
|
+
const sections = new Map();
|
|
68
|
+
let heading = null;
|
|
69
|
+
let buffer = [];
|
|
70
|
+
let fence = null;
|
|
71
|
+
const flush = () => {
|
|
72
|
+
if (heading !== null)
|
|
73
|
+
sections.set(heading, buffer.join('\n').trim());
|
|
74
|
+
};
|
|
75
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
76
|
+
const marker = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
77
|
+
if (marker) {
|
|
78
|
+
const [, run = '', rest = ''] = marker;
|
|
79
|
+
// A backtick fence's info string holds no backtick: ```x``` is an inline span, not a fence.
|
|
80
|
+
if (fence === null) {
|
|
81
|
+
if (run[0] === '~' || !rest.includes('`'))
|
|
82
|
+
fence = run;
|
|
83
|
+
}
|
|
84
|
+
else if (run[0] === fence[0] && run.length >= fence.length && /^\s*$/.test(rest))
|
|
85
|
+
fence = null;
|
|
86
|
+
}
|
|
87
|
+
if (fence === null && /^##\s+\S/.test(line)) {
|
|
88
|
+
flush();
|
|
89
|
+
heading = line.trim().replace(/\s+/g, ' ');
|
|
90
|
+
buffer = [];
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (heading !== null)
|
|
94
|
+
buffer.push(line);
|
|
95
|
+
}
|
|
96
|
+
flush();
|
|
97
|
+
return sections;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The frontmatter's values as text, read as YAML 1.2 core, so `blocking: no` stays "no" and a date stays as
|
|
101
|
+
* written; the keys whose value is a mapping, which is no answer; null for text that is not a YAML mapping.
|
|
102
|
+
*/
|
|
103
|
+
export function readFrontmatter(frontmatter, yaml) {
|
|
104
|
+
let parsed;
|
|
105
|
+
try {
|
|
106
|
+
parsed = yaml(frontmatter);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
if (parsed === null || parsed === undefined)
|
|
112
|
+
parsed = {};
|
|
113
|
+
if (typeof parsed !== 'object' || Array.isArray(parsed))
|
|
114
|
+
return null;
|
|
115
|
+
const flat = (value) => (value === null || value === undefined ? '' : typeof value === 'object' ? null : String(value).trim());
|
|
116
|
+
const fields = {};
|
|
117
|
+
const nonScalar = [];
|
|
118
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
119
|
+
if (Array.isArray(value)) {
|
|
120
|
+
const items = value.map(flat);
|
|
121
|
+
if (items.includes(null))
|
|
122
|
+
nonScalar.push(key);
|
|
123
|
+
else
|
|
124
|
+
fields[key] = items.filter(Boolean);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const one = flat(value);
|
|
128
|
+
if (one === null)
|
|
129
|
+
nonScalar.push(key);
|
|
130
|
+
else
|
|
131
|
+
fields[key] = one;
|
|
132
|
+
}
|
|
133
|
+
return { fields, nonScalar };
|
|
134
|
+
}
|
|
135
|
+
const scalar = (value) => (typeof value === 'string' ? value : '');
|
|
136
|
+
const list = (value) => (Array.isArray(value) ? value : value ? [value] : []);
|
|
137
|
+
/**
|
|
138
|
+
* Whether `sha` or a commit after it removed a path: it was there at `sha` or its parent, and `sha` is in this
|
|
139
|
+
* history. A path that was never there, or went on another line of history, is refused.
|
|
140
|
+
*/
|
|
141
|
+
export function deletedSince(probe, sha, path) {
|
|
142
|
+
return (probe.existsAt(sha, path) || probe.existsAt(`${sha}^`, path)) && probe.isAncestor(sha);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Checks a brief's shape, and the one check that can be held against the repository: a brief names the files it
|
|
146
|
+
* blames, and none may be in the branch's own diff, unless the session carries the fix itself. It cannot judge
|
|
147
|
+
* whether a brief is good; it refuses an empty one.
|
|
148
|
+
*/
|
|
149
|
+
export function lintBrief(markdown, context) {
|
|
150
|
+
const split = splitFrontmatter(markdown);
|
|
151
|
+
if (split === null)
|
|
152
|
+
return { ok: false, problems: [`no YAML frontmatter${context.template ? `; start the file from ${context.template}` : ''}`], fields: null };
|
|
153
|
+
const read = readFrontmatter(split.frontmatter, context.yaml);
|
|
154
|
+
if (read === null)
|
|
155
|
+
return { ok: false, problems: ['frontmatter is not valid YAML'], fields: null };
|
|
156
|
+
if (read.nonScalar.length) {
|
|
157
|
+
return { ok: false, problems: read.nonScalar.map((key) => `frontmatter \`${key}\` is a mapping, not a value; write it as text (or, for blamed-paths, a list of text)`), fields: null };
|
|
158
|
+
}
|
|
159
|
+
const raw = read.fields;
|
|
160
|
+
const fields = {
|
|
161
|
+
slug: scalar(raw.slug),
|
|
162
|
+
title: scalar(raw.title),
|
|
163
|
+
discoveredOn: scalar(raw['discovered-on']),
|
|
164
|
+
blocking: /^(yes|true)$/i.test(scalar(raw.blocking)),
|
|
165
|
+
status: scalar(raw.status),
|
|
166
|
+
resolvedBy: scalar(raw['resolved-by']),
|
|
167
|
+
resolutionNote: scalar(raw['resolution-note']),
|
|
168
|
+
blamedPaths: list(raw['blamed-paths']),
|
|
169
|
+
};
|
|
170
|
+
const problems = [];
|
|
171
|
+
for (const [key, value] of [['slug', fields.slug], ['title', fields.title], ['discovered-on', fields.discoveredOn]]) {
|
|
172
|
+
if (!value)
|
|
173
|
+
problems.push(`frontmatter \`${key}\` is empty`);
|
|
174
|
+
}
|
|
175
|
+
const blocking = scalar(raw.blocking);
|
|
176
|
+
if (!/^(yes|no|true|false)$/i.test(blocking))
|
|
177
|
+
problems.push(`frontmatter \`blocking\` must be yes or no (got "${blocking}"): an unreadable value would quietly downgrade a blocker`);
|
|
178
|
+
if (!STATUSES.includes(fields.status))
|
|
179
|
+
problems.push(`frontmatter \`status\` must be one of ${STATUSES.join(', ')} (got "${fields.status}")`);
|
|
180
|
+
if (fields.status === 'resolved' && !fields.resolvedBy)
|
|
181
|
+
problems.push('`status: resolved` needs a `resolved-by:` commit id');
|
|
182
|
+
else if (fields.status === 'resolved' && !isCommitSha(fields.resolvedBy)) {
|
|
183
|
+
problems.push(`\`resolved-by: ${fields.resolvedBy}\` is a ref, not a commit id: a ref is an ancestor of itself, so it would release this handoff without a fix`);
|
|
184
|
+
}
|
|
185
|
+
if (fields.status === 'rejected' && !fields.resolutionNote)
|
|
186
|
+
problems.push('`status: rejected` needs a `resolution-note:` saying why');
|
|
187
|
+
if (fields.blamedPaths.length === 0)
|
|
188
|
+
problems.push('`blamed-paths` is empty: a handoff names the files it holds responsible');
|
|
189
|
+
const changed = new Set(context.changedFiles.map(repoPath).filter((file) => file !== null));
|
|
190
|
+
for (const path of fields.blamedPaths) {
|
|
191
|
+
const canonical = repoPath(path);
|
|
192
|
+
if (canonical === null) {
|
|
193
|
+
problems.push(`blamed path is not a path inside this repository: ${path}; name the file as git does`);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// An open brief blames a path that is there; a resolved one outlives files its fix may have deleted.
|
|
197
|
+
if (!context.pathExists(canonical) && fields.status !== 'rejected') {
|
|
198
|
+
if (fields.status !== 'resolved' || !isCommitSha(fields.resolvedBy))
|
|
199
|
+
problems.push(`blamed path does not exist in this tree: ${canonical}`);
|
|
200
|
+
else if (!context.pathDeletedSince(fields.resolvedBy, canonical)) {
|
|
201
|
+
problems.push(`blamed path does not exist in this tree: ${canonical}, and ${fields.resolvedBy.slice(0, 7)} or a later commit in this history did not remove it`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (changed.has(canonical) && context.disposition !== 'carrying') {
|
|
205
|
+
problems.push(`blamed path is in this branch's own diff: ${canonical}; a file you changed cannot be handed off: revert the edit, or record --disposition carrying`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const sections = splitSections(split.body);
|
|
209
|
+
for (const heading of [...SECTIONS, ...(context.disposition === 'carrying' ? CARRYING_SECTIONS : [])]) {
|
|
210
|
+
const body = sections.get(heading);
|
|
211
|
+
if (body === undefined)
|
|
212
|
+
problems.push(`missing section: ${heading}`);
|
|
213
|
+
else if (!body)
|
|
214
|
+
problems.push(`section is empty: ${heading}`);
|
|
215
|
+
}
|
|
216
|
+
const evidence = sections.get(EVIDENCE_SECTION);
|
|
217
|
+
// A fence of either marker, in a blockquote too, is evidence; an inline ```x``` span is not.
|
|
218
|
+
const fenced = evidence?.split('\n').some((line) => {
|
|
219
|
+
const open = /^(?: {0,3}> ?)* {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
220
|
+
return open !== null && !(open[1]?.[0] === '`' && open[2]?.includes('`'));
|
|
221
|
+
});
|
|
222
|
+
if (evidence && !fenced)
|
|
223
|
+
problems.push(`${EVIDENCE_SECTION} holds no fenced evidence: paste the command and its output, not a description of it`);
|
|
224
|
+
return { ok: problems.length === 0, problems, fields };
|
|
225
|
+
}
|
|
226
|
+
/** A brief draft as an issue's title and body: the fields in the fenced block, without the state keys or comments. */
|
|
227
|
+
export function briefToIssue(markdown, title) {
|
|
228
|
+
const split = splitFrontmatter(markdown) ?? { frontmatter: '', body: markdown };
|
|
229
|
+
const state = new RegExp(`^(?:${STATE_KEYS.join('|')}):`);
|
|
230
|
+
const fields = split.frontmatter
|
|
231
|
+
.split('\n')
|
|
232
|
+
.filter((line) => !line.trimStart().startsWith('#') && !state.test(line))
|
|
233
|
+
.join('\n');
|
|
234
|
+
return { title, body: `${FIELDS_FENCE}\n${fields}\n\`\`\`\n${split.body}` };
|
|
235
|
+
}
|
|
236
|
+
/** An issue body back into a brief, `status: open`, so one lint judges both; null for a body not opened as one. */
|
|
237
|
+
export function issueToBrief(body) {
|
|
238
|
+
const lines = body.replace(/\r\n/g, '\n').split('\n');
|
|
239
|
+
if (lines[0] !== FIELDS_FENCE)
|
|
240
|
+
return null;
|
|
241
|
+
const end = lines.indexOf('```', 1);
|
|
242
|
+
if (end === -1)
|
|
243
|
+
return null;
|
|
244
|
+
return ['---', ...lines.slice(1, end), 'status: open', '---', ...lines.slice(end + 1)].join('\n');
|
|
245
|
+
}
|
|
246
|
+
/** Whether pull request text closes issue `number` by GitHub's keywords; only the bare `#N`, as `owner/repo#N` may be elsewhere. */
|
|
247
|
+
export function saysFixes(text, number) {
|
|
248
|
+
return new RegExp(`\\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?):?\\s+#${number}\\b`, 'i').test(text);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* The gate's judgement of one declared Brief issue: it is there, it is a brief, this branch found it, it lints,
|
|
252
|
+
* and a blocking one handed off has landed: closed as not planned, or by a merged pull request saying "Fixes #N"
|
|
253
|
+
* whose merge commit is in this history. Until then the line stays stopped.
|
|
254
|
+
*/
|
|
255
|
+
export function judgeBrief(ref, issue, context) {
|
|
256
|
+
if (issue === null)
|
|
257
|
+
return { ok: false, reason: 'brief-unreadable', detail: `declared brief ${ref} could not be read from GitHub; check the number, and that this session works as the agent` };
|
|
258
|
+
if (issue.type !== BRIEF_TYPE)
|
|
259
|
+
return { ok: false, reason: 'brief-not-brief', detail: `${ref} is not a Brief issue (type: ${issue.type ?? 'none'}); open one with niyam brief <draft.md>` };
|
|
260
|
+
const markdown = issueToBrief(issue.body);
|
|
261
|
+
if (markdown === null)
|
|
262
|
+
return { ok: false, reason: 'brief-not-brief', detail: `${ref} has no fields block at the top of its body; open briefs with niyam brief <draft.md>, never by hand` };
|
|
263
|
+
// A closed brief's blamed paths may be gone; its fields must still read, or a hand close could release `blocking: maybe`.
|
|
264
|
+
const lint = lintBrief(markdown, { ...context.lint, ...(issue.state === 'OPEN' ? {} : { pathExists: () => true }), disposition: context.disposition });
|
|
265
|
+
if (!lint.ok || lint.fields === null)
|
|
266
|
+
return { ok: false, reason: 'brief-invalid', detail: `${ref}: ${lint.problems.join('; ')}` };
|
|
267
|
+
if (lint.fields.discoveredOn !== context.branch) {
|
|
268
|
+
return { ok: false, reason: 'brief-other-branch', detail: `${ref} was discovered on "${lint.fields.discoveredOn}", not this branch "${context.branch}"; an older brief cannot answer for this push, so open one here` };
|
|
269
|
+
}
|
|
270
|
+
if (context.disposition !== 'handed-off' || !lint.fields.blocking)
|
|
271
|
+
return { ok: true };
|
|
272
|
+
if (issue.state === 'OPEN') {
|
|
273
|
+
return { ok: false, reason: 'handoff-not-landed', detail: `${ref} is blocking and still open: the push waits until a pull request saying "Fixes ${ref}" merges and you merge it in, or the issue is closed as not planned` };
|
|
274
|
+
}
|
|
275
|
+
if (issue.stateReason === 'not_planned')
|
|
276
|
+
return { ok: true };
|
|
277
|
+
const sha = issue.closingSha;
|
|
278
|
+
if (sha === null || !isCommitSha(sha)) {
|
|
279
|
+
return { ok: false, reason: 'handoff-not-landed', detail: `${ref} was closed (${issue.stateReason ?? 'no reason'}) by no merged pull request saying "Fixes ${ref}"; reopen it and let the fixing pull request close it, or close it as not planned` };
|
|
280
|
+
}
|
|
281
|
+
if (!context.isAncestorOfHead(sha))
|
|
282
|
+
return { ok: false, reason: 'handoff-not-landed', detail: `${ref} was closed by ${sha.slice(0, 7)}, which is not in this branch's history; merge it in before pushing` };
|
|
283
|
+
return { ok: true };
|
|
284
|
+
}
|
|
285
|
+
/** A draft's lint, and what the gate asks of the issue later: it opens open, on this branch. */
|
|
286
|
+
export function lintDraft(markdown, branch, context) {
|
|
287
|
+
const lint = lintBrief(markdown, context);
|
|
288
|
+
if (lint.fields === null)
|
|
289
|
+
return { ok: false, problems: lint.problems };
|
|
290
|
+
const problems = [...lint.problems];
|
|
291
|
+
if (lint.fields.status !== 'open')
|
|
292
|
+
problems.push('a new brief is `status: open`');
|
|
293
|
+
if (lint.fields.discoveredOn !== branch)
|
|
294
|
+
problems.push(`\`discovered-on: ${lint.fields.discoveredOn}\` must be this branch, ${branch}`);
|
|
295
|
+
return problems.length ? { ok: false, problems } : { ok: true, draft: briefToIssue(markdown, lint.fields.title) };
|
|
296
|
+
}
|
|
297
|
+
async function rest(sys, token, url, what, options = {}) {
|
|
298
|
+
const { status, json } = await sys.http(url, { token, ...options });
|
|
299
|
+
if (status < 200 || status >= 300) {
|
|
300
|
+
const message = json?.message;
|
|
301
|
+
throw new Error(`GitHub refused to ${what} (HTTP ${status}${typeof message === 'string' ? `: ${message}` : ''})`);
|
|
302
|
+
}
|
|
303
|
+
return json;
|
|
304
|
+
}
|
|
305
|
+
/** Opens the draft as a Brief issue, over REST, which takes the type by name. */
|
|
306
|
+
export async function openBrief(sys, token, slug, draft) {
|
|
307
|
+
const opened = (await rest(sys, token, `${API}/repos/${slug}/issues`, 'open the Brief issue', { method: 'POST', body: { ...draft, type: BRIEF_TYPE } }));
|
|
308
|
+
if (typeof opened?.number !== 'number' || typeof opened.id !== 'number' || typeof opened.html_url !== 'string')
|
|
309
|
+
throw new Error('GitHub opened the Brief issue but did not say which');
|
|
310
|
+
return { number: opened.number, id: opened.id, url: opened.html_url };
|
|
311
|
+
}
|
|
312
|
+
/** Adds the issue to the owner's open Project titled `title`, over REST, which a cloud session's proxy answers. */
|
|
313
|
+
export async function addToProject(sys, token, slug, issueId, title) {
|
|
314
|
+
const repo = (await rest(sys, token, `${API}/repos/${slug}`, `read ${slug}`));
|
|
315
|
+
const owner = repo?.owner?.login ?? slug.split('/')[0];
|
|
316
|
+
const base = `${API}/${repo?.owner?.type === 'User' ? 'users' : 'orgs'}/${owner}/projectsV2`;
|
|
317
|
+
for (let page = 1;; page++) {
|
|
318
|
+
const projects = (await rest(sys, token, `${base}?per_page=100&page=${page}`, `list ${owner}'s Projects`));
|
|
319
|
+
if (!Array.isArray(projects))
|
|
320
|
+
throw new Error(`GitHub did not list ${owner}'s Projects`);
|
|
321
|
+
const project = projects.find((one) => one.title === title && one.state !== 'closed' && !one.closed_at);
|
|
322
|
+
if (project) {
|
|
323
|
+
await rest(sys, token, `${base}/${project.number}/items`, `add the issue to "${title}"`, { method: 'POST', body: { type: 'Issue', id: issueId } });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (projects.length < 100)
|
|
327
|
+
throw new Error(`${owner} has no open Project titled "${title}"`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Reads issue `number` as a brief, over REST: its state, type and body, and the merge commit of the newest pull
|
|
332
|
+
* request that closed it saying "Fixes #N". Null when GitHub shows no such issue.
|
|
333
|
+
*/
|
|
334
|
+
export async function readBrief(sys, token, slug, number) {
|
|
335
|
+
const got = await sys.http(`${API}/repos/${slug}/issues/${number}`, { token });
|
|
336
|
+
if (got.status === 404 || got.status === 410)
|
|
337
|
+
return null;
|
|
338
|
+
const issue = got.json;
|
|
339
|
+
if (got.status !== 200 || issue?.number !== number || issue.pull_request)
|
|
340
|
+
return null;
|
|
341
|
+
let closingSha = null;
|
|
342
|
+
if (issue.state === 'closed') {
|
|
343
|
+
// REST names no closer (a pull request's close leaves the event's commit_id null), so the fix is the newest
|
|
344
|
+
// merged pull request of this repository, into its default branch, that the issue's timeline cross-references
|
|
345
|
+
// and whose text says "Fixes #N": the one GitHub closes the issue by. A brief fixed, reopened and closed by
|
|
346
|
+
// hand still has its fix.
|
|
347
|
+
const timeline = (await restList(sys, token, `repos/${slug}/issues/${number}/timeline`));
|
|
348
|
+
const here = `${API}/repos/${slug}`.toLowerCase();
|
|
349
|
+
const merged = timeline.flatMap(({ event, source }) => {
|
|
350
|
+
const pull = source?.issue;
|
|
351
|
+
return event === 'cross-referenced' && typeof pull?.number === 'number' && pull.pull_request?.merged_at && String(pull.repository_url).toLowerCase() === here ? [pull.number] : [];
|
|
352
|
+
});
|
|
353
|
+
const repo = merged.length ? (await rest(sys, token, `${API}/repos/${slug}`, `read ${slug}`)) : null;
|
|
354
|
+
for (const pr of [...new Set(merged)].reverse()) {
|
|
355
|
+
const pull = (await rest(sys, token, `${API}/repos/${slug}/pulls/${pr}`, `read pull request #${pr}`));
|
|
356
|
+
if (pull?.merged_at && pull.merge_commit_sha && pull.base?.ref === repo?.default_branch && saysFixes(`${pull.title ?? ''}\n${pull.body ?? ''}`, number)) {
|
|
357
|
+
closingSha = pull.merge_commit_sha;
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
number,
|
|
364
|
+
state: issue.state === 'open' ? 'OPEN' : 'CLOSED',
|
|
365
|
+
stateReason: typeof issue.state_reason === 'string' ? issue.state_reason : null,
|
|
366
|
+
type: typeof issue.type?.name === 'string' ? issue.type.name : null,
|
|
367
|
+
body: typeof issue.body === 'string' ? issue.body : '',
|
|
368
|
+
closingSha,
|
|
369
|
+
};
|
|
370
|
+
}
|
package/dist/cloud.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { API, isNoToken } from './agent.js';
|
|
2
|
-
import { SETUP_FIRST, workflowPins } from './onboard.js';
|
|
2
|
+
import { CLAUDE_CODE_WEB, SETUP_FIRST, workflowPins } from './onboard.js';
|
|
3
3
|
import { PRODUCT, tarballName } from './names.js';
|
|
4
4
|
import { CLOUD_SETUP_PATH, WORKFLOW_PATH } from './onboard-pull.js';
|
|
5
5
|
import { GITHUB_WEB } from './host.js';
|
|
@@ -120,17 +120,18 @@ export async function releaseSha256(sys, version, token) {
|
|
|
120
120
|
* release of the CLI, unpacked under CLOUD_DIR as root, and a `niyam` on
|
|
121
121
|
* PATH that runs one with the other, leaving the image's own Node alone. Each download is checked against the sha256 pinned here
|
|
122
122
|
* before it is unpacked; a Node already unpacked at this version is kept,
|
|
123
|
-
* so a session that runs it again fetches the CLI alone. The
|
|
124
|
-
* runs it
|
|
125
|
-
* environment's snapshot holds another release
|
|
123
|
+
* so a session that runs it again fetches the CLI alone. The SessionStart
|
|
124
|
+
* hook runs it when a session has no CLI (HOOK_COMMAND), session-start when
|
|
125
|
+
* the environment's snapshot holds another release, and the Setup snippet,
|
|
126
|
+
* where it is pasted, before a session starts.
|
|
126
127
|
*/
|
|
127
128
|
export function cloudRepoScript(version, sha256) {
|
|
128
129
|
return [
|
|
129
130
|
'#!/bin/bash',
|
|
130
131
|
`# niyam ${version} for Claude cloud sessions: Node ${CLOUD_NODE} and the CLI, as niyam on PATH.`,
|
|
131
132
|
`# niyam onboard writes this file at the version ${WORKFLOW_PATH} pins, and writes it again on every run.`,
|
|
132
|
-
"# The
|
|
133
|
-
|
|
133
|
+
"# The SessionStart hook runs it when a session has no niyam, session-start when it has another release,",
|
|
134
|
+
"# and the Setup script when it holds what niyam cloud-setup prints. Change it by running onboard, not by hand.",
|
|
134
135
|
'set -euo pipefail',
|
|
135
136
|
`dir=${CLOUD_DIR}`,
|
|
136
137
|
`node=${CLOUD_NODE}`,
|
|
@@ -163,8 +164,10 @@ export function cloudRepoScript(version, sha256) {
|
|
|
163
164
|
].join('\n');
|
|
164
165
|
}
|
|
165
166
|
/**
|
|
166
|
-
* The Setup snippet for a Claude cloud environment,
|
|
167
|
-
*
|
|
167
|
+
* The Setup snippet for a Claude cloud environment, optional since P-60,
|
|
168
|
+
* when the SessionStart hook installs niyam itself: Claude caches what a
|
|
169
|
+
* Setup script installs, so sessions start faster. It names no version,
|
|
170
|
+
* so a new release needs no new paste. It runs the clone's
|
|
168
171
|
* CLOUD_SETUP_PATH, which onboard keeps at the workflow's pin, setting the
|
|
169
172
|
* mark's variable. The Setup
|
|
170
173
|
* script runs as root after the clone, from the folder that holds it (the
|
|
@@ -188,9 +191,23 @@ export function cloudSetupSnippet() {
|
|
|
188
191
|
'',
|
|
189
192
|
].join('\n');
|
|
190
193
|
}
|
|
191
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* What `cloud-setup` says above the snippet, as comment lines of the script
|
|
196
|
+
* itself: `niyam cloud-setup | pbcopy` copies them with it, and the Setup
|
|
197
|
+
* field keeps them, so nothing is said on standard error behind a pipe.
|
|
198
|
+
*/
|
|
199
|
+
export function cloudSetupHelp() {
|
|
200
|
+
return [
|
|
201
|
+
"# Optional: cloud sessions install niyam themselves when they have none. This makes them start faster, since Claude caches what a Setup script installs.",
|
|
202
|
+
`# Paste all of this at the top of the Claude cloud environment's Setup script, once (${CLAUDE_CODE_WEB} → the environment's settings).`,
|
|
203
|
+
`# It runs the clone's ${CLOUD_SETUP_PATH}, which niyam onboard keeps at the version the repo pins, so a new release needs no new paste.`,
|
|
204
|
+
`# ${SETUP_FIRST}`,
|
|
205
|
+
`# Cloud sessions also need claude.ai's GitHub connected as the agent the repo's settings name: ${CLAUDE_CODE_WEB}.`,
|
|
206
|
+
];
|
|
207
|
+
}
|
|
208
|
+
/** Prints the Setup snippet for a Claude cloud environment, its instructions in comment lines at the top. */
|
|
192
209
|
export function cloudSetup(io) {
|
|
193
|
-
|
|
194
|
-
io.out(
|
|
210
|
+
const [shebang, ...rest] = cloudSetupSnippet().trimEnd().split('\n');
|
|
211
|
+
io.out([shebang, ...cloudSetupHelp(), ...rest].join('\n'));
|
|
195
212
|
return 0;
|
|
196
213
|
}
|