@otea/cli 0.0.0-stage → 0.1.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 +661 -0
- package/README.es.md +151 -0
- package/README.md +252 -2
- package/THIRD_PARTY_NOTICES.md +114 -0
- package/bin/otea.mjs +28 -0
- package/dist/app/agent-detail.js +26 -0
- package/dist/app/agent-draft.js +429 -0
- package/dist/app/agent-prompts.js +52 -0
- package/dist/app/agents.js +77 -0
- package/dist/app/asking.js +43 -0
- package/dist/app/board-archive.js +243 -0
- package/dist/app/board-migration.js +272 -0
- package/dist/app/boards.js +870 -0
- package/dist/app/branch-owner.js +48 -0
- package/dist/app/brief.js +53 -0
- package/dist/app/choices.js +61 -0
- package/dist/app/collections.js +101 -0
- package/dist/app/context.js +1 -0
- package/dist/app/crop.js +56 -0
- package/dist/app/devices.js +62 -0
- package/dist/app/draft-request.js +82 -0
- package/dist/app/events.js +12 -0
- package/dist/app/evidence.js +287 -0
- package/dist/app/gates.js +610 -0
- package/dist/app/guide.js +37 -0
- package/dist/app/home.js +145 -0
- package/dist/app/init-boards.js +36 -0
- package/dist/app/intake.js +25 -0
- package/dist/app/item-search.js +141 -0
- package/dist/app/items.js +318 -0
- package/dist/app/keep-waiting.js +44 -0
- package/dist/app/land-order.js +107 -0
- package/dist/app/language.js +52 -0
- package/dist/app/launcher.js +56 -0
- package/dist/app/lease-take.js +54 -0
- package/dist/app/leases.js +86 -0
- package/dist/app/machine.js +39 -0
- package/dist/app/merged.js +89 -0
- package/dist/app/migrate.js +171 -0
- package/dist/app/next.js +1637 -0
- package/dist/app/notes.js +319 -0
- package/dist/app/notify-targets.js +113 -0
- package/dist/app/opened.js +25 -0
- package/dist/app/owner-only.js +34 -0
- package/dist/app/performance.js +28 -0
- package/dist/app/phone.js +97 -0
- package/dist/app/presence.js +596 -0
- package/dist/app/preset-views.js +25 -0
- package/dist/app/presets.js +129 -0
- package/dist/app/priority.js +156 -0
- package/dist/app/profile.js +86 -0
- package/dist/app/project.js +247 -0
- package/dist/app/proofs.js +179 -0
- package/dist/app/questions.js +215 -0
- package/dist/app/reader-filter.js +58 -0
- package/dist/app/relations.js +164 -0
- package/dist/app/relay.js +98 -0
- package/dist/app/repos.js +176 -0
- package/dist/app/restart.js +92 -0
- package/dist/app/set-aside.js +64 -0
- package/dist/app/system-locale.js +6 -0
- package/dist/app/tailscale-cli.js +77 -0
- package/dist/app/tailscale-testing.js +41 -0
- package/dist/app/tailscale.js +177 -0
- package/dist/app/tips.js +30 -0
- package/dist/app/topics-everywhere.js +53 -0
- package/dist/app/topics.js +453 -0
- package/dist/app/undo.js +52 -0
- package/dist/app/validate.js +131 -0
- package/dist/app/watch.js +48 -0
- package/dist/cli/command.js +44 -0
- package/dist/cli/commands.js +3303 -0
- package/dist/cli/exit-codes.js +70 -0
- package/dist/cli/forge.js +176 -0
- package/dist/cli/format.js +340 -0
- package/dist/cli/gh.js +17 -0
- package/dist/cli/gitrepo.js +489 -0
- package/dist/cli/help.js +101 -0
- package/dist/cli/hooks.js +232 -0
- package/dist/cli/main.js +37 -0
- package/dist/cli/merged-outside.js +58 -0
- package/dist/cli/names.js +154 -0
- package/dist/cli/opener.js +21 -0
- package/dist/cli/owner.js +154 -0
- package/dist/cli/phone.js +221 -0
- package/dist/cli/ports.js +24 -0
- package/dist/cli/probe.js +18 -0
- package/dist/cli/run.js +246 -0
- package/dist/cli/serve-file.js +21 -0
- package/dist/cli/stats.js +55 -0
- package/dist/cli/version.js +3 -0
- package/dist/core/agent-guide.js +31 -0
- package/dist/core/agent-prompts.js +139 -0
- package/dist/core/authority.js +94 -0
- package/dist/core/board/analysis.js +86 -0
- package/dist/core/board/choices.js +100 -0
- package/dist/core/board/compile.js +362 -0
- package/dist/core/board/engine.js +523 -0
- package/dist/core/board/hints.js +46 -0
- package/dist/core/board/history.js +146 -0
- package/dist/core/board/ids.js +87 -0
- package/dist/core/board/joins.js +30 -0
- package/dist/core/board/mentions.js +68 -0
- package/dist/core/board/merged.js +18 -0
- package/dist/core/board/model.js +135 -0
- package/dist/core/board/next.js +1116 -0
- package/dist/core/board/notes.js +298 -0
- package/dist/core/board/performance.js +218 -0
- package/dist/core/board/plan.js +299 -0
- package/dist/core/board/problems.js +232 -0
- package/dist/core/board/proofs.js +84 -0
- package/dist/core/board/questions.js +114 -0
- package/dist/core/board/reading.js +220 -0
- package/dist/core/board/readings.js +534 -0
- package/dist/core/board/schema.js +335 -0
- package/dist/core/board/shape-words.js +179 -0
- package/dist/core/board/template-words.js +23 -0
- package/dist/core/board/templates-catalogue.js +965 -0
- package/dist/core/board/templates-coverage.js +261 -0
- package/dist/core/board/templates-flows.js +779 -0
- package/dist/core/board/templates.js +586 -0
- package/dist/core/brief.js +93 -0
- package/dist/core/diagram.js +80 -0
- package/dist/core/errors.js +12 -0
- package/dist/core/evidence.js +33 -0
- package/dist/core/fields.js +129 -0
- package/dist/core/hierarchy.js +30 -0
- package/dist/core/icons.js +181 -0
- package/dist/core/image-crop.js +166 -0
- package/dist/core/item.js +49 -0
- package/dist/core/lease.js +31 -0
- package/dist/core/modules.js +318 -0
- package/dist/core/notify/planner.js +117 -0
- package/dist/core/notify/signals.js +66 -0
- package/dist/core/notify/text.js +60 -0
- package/dist/core/path-key.js +22 -0
- package/dist/core/placement.js +161 -0
- package/dist/core/preset-labels.js +66 -0
- package/dist/core/preset-notes.js +109 -0
- package/dist/core/preset.js +541 -0
- package/dist/core/profile.js +55 -0
- package/dist/core/project-id.js +20 -0
- package/dist/core/refusal.js +1 -0
- package/dist/core/relations.js +208 -0
- package/dist/core/repo.js +54 -0
- package/dist/core/samples.js +161 -0
- package/dist/core/search-words.js +68 -0
- package/dist/core/test-report.js +120 -0
- package/dist/core/time.js +8 -0
- package/dist/core/validation.js +60 -0
- package/dist/core/views.js +171 -0
- package/dist/core/workflow.js +51 -0
- package/dist/mcp/commands.js +101 -0
- package/dist/mcp/server.js +239 -0
- package/dist/mcp/words.js +95 -0
- package/dist/server/boards.js +232 -0
- package/dist/server/changes.js +42 -0
- package/dist/server/diff-tip.js +83 -0
- package/dist/server/drift.js +87 -0
- package/dist/server/errors.js +87 -0
- package/dist/server/evidence.js +82 -0
- package/dist/server/hosts.js +6 -0
- package/dist/server/hub.js +98 -0
- package/dist/server/lan-access.js +62 -0
- package/dist/server/lan.js +164 -0
- package/dist/server/listen.js +70 -0
- package/dist/server/notifier.js +156 -0
- package/dist/server/notify-api.js +151 -0
- package/dist/server/pairing.js +124 -0
- package/dist/server/projects.js +191 -0
- package/dist/server/push.js +199 -0
- package/dist/server/server.js +960 -0
- package/dist/server/session.js +43 -0
- package/dist/server/stream.js +36 -0
- package/dist/server/test-push.js +38 -0
- package/dist/server/token.js +37 -0
- package/dist/server/transports.js +54 -0
- package/dist/server/ui.js +44 -0
- package/dist/server/writes.js +361 -0
- package/dist/server/wrong-address.js +36 -0
- package/dist/store/boards.js +111 -0
- package/dist/store/database.js +45 -0
- package/dist/store/machine.js +77 -0
- package/dist/store/migrations.js +493 -0
- package/dist/store/queries.js +585 -0
- package/dist/store/registry.js +72 -0
- package/package.json +73 -4
- package/presets/architecture/playbook.md +69 -0
- package/presets/architecture/preset.yaml +80 -0
- package/presets/architecture/samples.yaml +23 -0
- package/presets/bug-triage/playbook.md +129 -0
- package/presets/bug-triage/preset.yaml +127 -0
- package/presets/bug-triage/samples/wall-slide-after.png +0 -0
- package/presets/bug-triage/samples/wall-slide-before.png +0 -0
- package/presets/bug-triage/samples.yaml +72 -0
- package/presets/change/playbook.md +115 -0
- package/presets/change/preset.yaml +141 -0
- package/presets/change/samples.yaml +62 -0
- package/presets/review/playbook.md +51 -0
- package/presets/review/preset.yaml +62 -0
- package/presets/review/samples.yaml +27 -0
- package/presets/tasks/playbook.md +78 -0
- package/presets/tasks/preset.yaml +61 -0
- package/presets/tasks/samples.yaml +46 -0
- package/schemas/board.schema.json +764 -0
- package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
- package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
- package/ui/dist/assets/index-C3OzJope.js +103 -0
- package/ui/dist/assets/index-COdEjckQ.css +1 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
- package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
- package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
- package/ui/dist/assets/pdf-rELvHkft.js +56 -0
- package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
- package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
- package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
- package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
- package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
- package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
- package/ui/dist/icons/apple-touch-icon.png +0 -0
- package/ui/dist/icons/otea-192.png +0 -0
- package/ui/dist/icons/otea-512.png +0 -0
- package/ui/dist/icons/otea-badge.png +0 -0
- package/ui/dist/icons/otea-maskable-512.png +0 -0
- package/ui/dist/index.html +25 -0
- package/ui/dist/manifest.webmanifest +24 -0
- package/ui/dist/otea-idle.svg +5 -0
- package/ui/dist/otea-working.svg +5 -0
- package/ui/dist/otea-yours-working.svg +5 -0
- package/ui/dist/otea-yours.svg +5 -0
- package/ui/dist/sw.js +1 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { repoOfItem } from '../app/gates.js';
|
|
4
|
+
import { shellLine } from '../app/launcher.js';
|
|
5
|
+
import { checkoutsOf } from '../app/repos.js';
|
|
6
|
+
import { CommandFailure } from './command.js';
|
|
7
|
+
import { EXIT_CODES } from './exit-codes.js';
|
|
8
|
+
import { gitPath, repoRoot } from './gitrepo.js';
|
|
9
|
+
// The first lines of the pre-push hook Otea writes; a hook without them is someone else's.
|
|
10
|
+
const MARK = '# otea-hook: pre-push';
|
|
11
|
+
// The version of the hook Otea writes now, on the line after MARK. A hook with MARK and another
|
|
12
|
+
// version (or none: the ones before #495, which let a push through when Otea could not check it)
|
|
13
|
+
// is outdated: Otea says so, and rewrites it only on otea hook install --git. Bump it whenever the
|
|
14
|
+
// hook's lines change what a push does.
|
|
15
|
+
const VERSION = '# otea-hook-version: 2';
|
|
16
|
+
// How a hook runs this Otea, as `otea connect` does for the MCP server.
|
|
17
|
+
const oteaCommand = shellLine;
|
|
18
|
+
// Checks every branch the push writes to or deletes on the remote, not the local branch it sends
|
|
19
|
+
// from, which may be another; refuses when otea check refuses (gate_refused). It fails closed: when
|
|
20
|
+
// Otea fails or cannot run at all, the push stops too, saying so, with the person's way past it
|
|
21
|
+
// (Plan 7, item 4); only a gate nothing declares (invalid_gate: the project gates no push) lets it
|
|
22
|
+
// go. Tags and other refs are not branches and go unchecked. --commits names what the remote has
|
|
23
|
+
// and what it gets, so a push into a base branch a board protects is checked against the branches
|
|
24
|
+
// it brings in. Otea's language is unknown when it cannot run: the person's line is in both.
|
|
25
|
+
function gitCheckLines(otea) {
|
|
26
|
+
const failed = [
|
|
27
|
+
'Otea could not check this push of $branch (exit $code), so it does not go through: make sure otea runs here (otea check --gate push --branch $branch --repo), then push again.',
|
|
28
|
+
"For the person: to push it yourself this once, git push --no-verify, which skips Otea's check. An agent never uses --no-verify.",
|
|
29
|
+
'Para la persona: para subirlo tú esta vez, git push --no-verify, que se salta la revisión de Otea. Un agente nunca usa --no-verify.',
|
|
30
|
+
];
|
|
31
|
+
return [
|
|
32
|
+
'while read -r local_ref local_sha remote_ref remote_sha; do',
|
|
33
|
+
' case "$remote_ref" in',
|
|
34
|
+
' refs/heads/*)',
|
|
35
|
+
' branch="${remote_ref#refs/heads/}"',
|
|
36
|
+
` ${oteaCommand(otea)} check --gate push --branch "$branch" --repo --commits "$remote_sha..$local_sha" < /dev/null`,
|
|
37
|
+
' code=$?',
|
|
38
|
+
` if [ $code -eq ${EXIT_CODES.gate_refused} ]; then exit 1; fi`,
|
|
39
|
+
` if [ $code -ne 0 ] && [ $code -ne ${EXIT_CODES.invalid_gate} ]; then`,
|
|
40
|
+
...failed.map((line) => ` echo "${line}" >&2`),
|
|
41
|
+
' exit 1',
|
|
42
|
+
' fi',
|
|
43
|
+
' ;;',
|
|
44
|
+
' esac',
|
|
45
|
+
'done',
|
|
46
|
+
].join('\n');
|
|
47
|
+
}
|
|
48
|
+
function preHook(otea) {
|
|
49
|
+
const header = [
|
|
50
|
+
'#!/bin/sh',
|
|
51
|
+
MARK,
|
|
52
|
+
VERSION,
|
|
53
|
+
'# Written by otea hook install --git; otea hook remove --git removes it.',
|
|
54
|
+
];
|
|
55
|
+
return `${[...header, gitCheckLines(otea), 'exit 0'].join('\n')}\n`;
|
|
56
|
+
}
|
|
57
|
+
// The pre-push hook of the repo at cwd, wherever core.hooksPath puts it.
|
|
58
|
+
export function gitHookFile(cwd) {
|
|
59
|
+
return gitPath(cwd, 'hooks/pre-push');
|
|
60
|
+
}
|
|
61
|
+
export function installGitHook(cwd, otea) {
|
|
62
|
+
let path;
|
|
63
|
+
try {
|
|
64
|
+
path = gitHookFile(cwd);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
throw new CommandFailure('not_found', `${cwd} is not in a git repository: a pre-push hook goes in one (run otea hook install --git in the repo)`);
|
|
68
|
+
}
|
|
69
|
+
const content = preHook(otea);
|
|
70
|
+
const current = existsSync(path) ? readFileSync(path, 'utf8') : undefined;
|
|
71
|
+
if (current !== undefined && !current.includes(MARK)) {
|
|
72
|
+
throw new CommandFailure('already_exists', `${path} is another pre-push hook; Otea leaves it as it is. To gate pushes, add to it:\n\n${gitCheckLines(otea)}`);
|
|
73
|
+
}
|
|
74
|
+
const note = gitHookNote(path);
|
|
75
|
+
if (current === content)
|
|
76
|
+
return { path, action: 'unchanged', content, note };
|
|
77
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
78
|
+
writeFileSync(path, content);
|
|
79
|
+
chmodSync(path, 0o755);
|
|
80
|
+
return { path, action: current === undefined ? 'created' : 'updated', content, note };
|
|
81
|
+
}
|
|
82
|
+
// The hook names this machine's path to Otea: fine in .git/hooks, never committed, but a hooks
|
|
83
|
+
// folder that core.hooksPath puts in the work tree may be.
|
|
84
|
+
function gitHookNote(path) {
|
|
85
|
+
if (path.split(/[\\/]/).includes('.git')) {
|
|
86
|
+
return '.git/hooks is per machine and never committed: the hook names the path to Otea on this machine';
|
|
87
|
+
}
|
|
88
|
+
return 'core.hooksPath puts it outside .git: do not commit it, the hook names the path to Otea on this machine';
|
|
89
|
+
}
|
|
90
|
+
function gitHookKind(cwd) {
|
|
91
|
+
try {
|
|
92
|
+
const path = gitHookFile(cwd);
|
|
93
|
+
if (!existsSync(path))
|
|
94
|
+
return 'none';
|
|
95
|
+
const content = readFileSync(path, 'utf8');
|
|
96
|
+
if (content.includes(MARK))
|
|
97
|
+
return content.includes(VERSION) ? 'otea' : 'outdated';
|
|
98
|
+
return content.includes('check --gate push') ? 'otea' : 'foreign';
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return 'none';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Whether the repo at cwd has Otea's pre-push hook, which makes its gates hard; false outside a
|
|
105
|
+
// repo. An outdated one still refuses what the gates refuse.
|
|
106
|
+
export function hasOteaGitHook(cwd) {
|
|
107
|
+
const kind = gitHookKind(cwd);
|
|
108
|
+
return kind === 'otea' || kind === 'outdated';
|
|
109
|
+
}
|
|
110
|
+
// Whether the repo at cwd has a pre-push hook an older Otea wrote; false outside a repo.
|
|
111
|
+
export function hasOutdatedGitHook(cwd) {
|
|
112
|
+
return gitHookKind(cwd) === 'outdated';
|
|
113
|
+
}
|
|
114
|
+
// What Otea says of the repo at cwd when an older Otea wrote its pre-push hook, with the command
|
|
115
|
+
// that updates it; nothing otherwise.
|
|
116
|
+
export function outdatedGitHookLine(cwd) {
|
|
117
|
+
if (!hasOutdatedGitHook(cwd))
|
|
118
|
+
return undefined;
|
|
119
|
+
return `${gitHookFile(cwd)} is a pre-push hook an older Otea wrote, which lets a push through when Otea cannot check it: run otea hook install --git in ${repoRoot(cwd)} to update it`;
|
|
120
|
+
}
|
|
121
|
+
export function gitHookOf(project, item) {
|
|
122
|
+
const repo = repoOfItem(project, item);
|
|
123
|
+
if (typeof repo !== 'string' || repo.trim() === '')
|
|
124
|
+
return undefined;
|
|
125
|
+
const kinds = checkoutsOf(project, repo).map((path) => ({ path, kind: gitHookKind(path) }));
|
|
126
|
+
const of = (kind) => kinds.filter((c) => c.kind === kind).map(({ path }) => path);
|
|
127
|
+
const foreign = of('foreign');
|
|
128
|
+
const outdated = of('outdated');
|
|
129
|
+
return {
|
|
130
|
+
repo,
|
|
131
|
+
installed: of('otea'),
|
|
132
|
+
missing: of('none'),
|
|
133
|
+
...(foreign.length > 0 ? { foreign } : {}),
|
|
134
|
+
...(outdated.length > 0 ? { outdated } : {}),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export function removeGitHook(cwd) {
|
|
138
|
+
const path = gitHookFile(cwd);
|
|
139
|
+
if (!existsSync(path))
|
|
140
|
+
return { path, action: 'absent' };
|
|
141
|
+
const content = readFileSync(path, 'utf8');
|
|
142
|
+
if (!content.includes(MARK)) {
|
|
143
|
+
throw new CommandFailure('forbidden', `${path} is not Otea's pre-push hook; remove it yourself`);
|
|
144
|
+
}
|
|
145
|
+
rmSync(path);
|
|
146
|
+
return { path, action: 'removed', content };
|
|
147
|
+
}
|
|
148
|
+
// Otea no longer writes Claude Code hooks: correctness never depends on a harness. hook remove
|
|
149
|
+
// --claude still takes out what an older otea hook install --claude wrote (the PreToolUse gate
|
|
150
|
+
// that ran otea check --claude-hook, the Stop hook that ran otea topic session, and these allow
|
|
151
|
+
// rules), since the commands those hooks run are gone.
|
|
152
|
+
const CLAUDE_TOPIC_RULES = [
|
|
153
|
+
'Bash(otea topic add *)',
|
|
154
|
+
'Bash(otea topic answer *)',
|
|
155
|
+
'mcp__otea__topic_add',
|
|
156
|
+
'mcp__otea__topic_answer',
|
|
157
|
+
];
|
|
158
|
+
function isOurs(hook) {
|
|
159
|
+
const command = hook?.command;
|
|
160
|
+
return (typeof command === 'string' &&
|
|
161
|
+
(command.includes(' check --gate ') || command.includes(' topic session ')) &&
|
|
162
|
+
command.endsWith('--claude-hook'));
|
|
163
|
+
}
|
|
164
|
+
// Where an older otea hook install --claude wrote its hooks.
|
|
165
|
+
function claudeSettingsFile(cwd) {
|
|
166
|
+
return join(repoRoot(cwd), '.claude', 'settings.local.json');
|
|
167
|
+
}
|
|
168
|
+
function readSettings(path) {
|
|
169
|
+
if (!existsSync(path))
|
|
170
|
+
return {};
|
|
171
|
+
try {
|
|
172
|
+
const settings = JSON.parse(readFileSync(path, 'utf8'));
|
|
173
|
+
if (settings !== null && typeof settings === 'object' && !Array.isArray(settings)) {
|
|
174
|
+
return settings;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch { }
|
|
178
|
+
throw new CommandFailure('invalid_fields', `${path} is not a JSON object; fix it, then try again`);
|
|
179
|
+
}
|
|
180
|
+
// The settings with Otea's hooks (PreToolUse and Stop) taken out, and empty groups and keys with
|
|
181
|
+
// them.
|
|
182
|
+
function withoutOurs(settings) {
|
|
183
|
+
let hooks = { ...(settings.hooks ?? {}) };
|
|
184
|
+
for (const event of ['PreToolUse', 'Stop']) {
|
|
185
|
+
const groups = hooks[event];
|
|
186
|
+
if (!Array.isArray(groups))
|
|
187
|
+
continue;
|
|
188
|
+
const kept = groups.flatMap((group) => {
|
|
189
|
+
if (!Array.isArray(group?.hooks))
|
|
190
|
+
return [group];
|
|
191
|
+
const left = group.hooks.filter((hook) => !isOurs(hook));
|
|
192
|
+
if (left.length === group.hooks.length)
|
|
193
|
+
return [group];
|
|
194
|
+
return left.length > 0 ? [{ ...group, hooks: left }] : [];
|
|
195
|
+
});
|
|
196
|
+
const { [event]: _, ...others } = hooks;
|
|
197
|
+
hooks = kept.length > 0 ? { ...others, [event]: kept } : others;
|
|
198
|
+
}
|
|
199
|
+
const { hooks: __, permissions, ...rest } = settings;
|
|
200
|
+
const kept = withoutOurRules(permissions);
|
|
201
|
+
const others = kept === undefined ? rest : { ...rest, permissions: kept };
|
|
202
|
+
return Object.keys(hooks).length > 0 ? { ...others, hooks } : others;
|
|
203
|
+
}
|
|
204
|
+
// The permissions with Otea's allow rules taken out, and an empty allow list and permissions with
|
|
205
|
+
// them.
|
|
206
|
+
function withoutOurRules(permissions) {
|
|
207
|
+
if (permissions === undefined || !Array.isArray(permissions.allow))
|
|
208
|
+
return permissions;
|
|
209
|
+
const allow = permissions.allow.filter((rule) => !CLAUDE_TOPIC_RULES.includes(rule));
|
|
210
|
+
const { allow: _, ...others } = permissions;
|
|
211
|
+
const kept = allow.length > 0 ? { ...others, allow } : others;
|
|
212
|
+
return Object.keys(kept).length > 0 ? kept : undefined;
|
|
213
|
+
}
|
|
214
|
+
function writeSettings(path, settings) {
|
|
215
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
216
|
+
writeFileSync(path, `${JSON.stringify(settings, null, 2)}\n`);
|
|
217
|
+
}
|
|
218
|
+
export function removeClaudeHook(cwd) {
|
|
219
|
+
const path = claudeSettingsFile(cwd);
|
|
220
|
+
if (!existsSync(path))
|
|
221
|
+
return { path, action: 'absent' };
|
|
222
|
+
const before = readSettings(path);
|
|
223
|
+
const after = withoutOurs(before);
|
|
224
|
+
if (JSON.stringify(after) === JSON.stringify(before))
|
|
225
|
+
return { path, action: 'absent' };
|
|
226
|
+
// A file that held only Otea's hooks goes with them.
|
|
227
|
+
if (Object.keys(after).length === 0)
|
|
228
|
+
rmSync(path);
|
|
229
|
+
else
|
|
230
|
+
writeSettings(path, after);
|
|
231
|
+
return { path, action: 'removed' };
|
|
232
|
+
}
|
package/dist/cli/main.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline/promises';
|
|
2
|
+
import { fetchProbe } from '../app/phone.js';
|
|
3
|
+
import { systemTailscale } from '../app/tailscale-cli.js';
|
|
4
|
+
import { ghForge } from './forge.js';
|
|
5
|
+
import { systemOpener } from './opener.js';
|
|
6
|
+
import { run } from './run.js';
|
|
7
|
+
const interrupt = new AbortController();
|
|
8
|
+
process.once('SIGINT', () => interrupt.abort());
|
|
9
|
+
// Only a person at a terminal is asked anything; scripts pass every answer as an option.
|
|
10
|
+
async function ask(question) {
|
|
11
|
+
const prompt = createInterface({ input: process.stdin, output: process.stderr });
|
|
12
|
+
try {
|
|
13
|
+
return await prompt.question(question, { signal: interrupt.signal });
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
prompt.close();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function stdin() {
|
|
20
|
+
let text = '';
|
|
21
|
+
for await (const chunk of process.stdin)
|
|
22
|
+
text += chunk;
|
|
23
|
+
return text;
|
|
24
|
+
}
|
|
25
|
+
process.exitCode = await run(process.argv.slice(2), {
|
|
26
|
+
cwd: process.cwd(),
|
|
27
|
+
env: process.env,
|
|
28
|
+
stdout: process.stdout,
|
|
29
|
+
stderr: process.stderr,
|
|
30
|
+
signal: interrupt.signal,
|
|
31
|
+
openUrl: systemOpener(),
|
|
32
|
+
tailscale: systemTailscale(),
|
|
33
|
+
probe: fetchProbe,
|
|
34
|
+
gh: 'gh',
|
|
35
|
+
forge: ghForge('gh'),
|
|
36
|
+
...(process.stdin.isTTY ? { ask } : { stdin }),
|
|
37
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { mergedOf } from '../app/merged.js';
|
|
2
|
+
import { liveBoardOf } from '../app/next.js';
|
|
3
|
+
import { checkoutsOf } from '../app/repos.js';
|
|
4
|
+
import { isClosed, landStepOf, positionOf } from '../core/board/engine.js';
|
|
5
|
+
import { DONE, guardedBranch } from '../core/board/model.js';
|
|
6
|
+
import { mergedInto } from './gitrepo.js';
|
|
7
|
+
// A branch merged outside Otea (on the forge, or by hand) leaves its item waiting for a merge that
|
|
8
|
+
// already happened (P1-16): in the board's land step, or Done and not marked merged on a board
|
|
9
|
+
// without one. Says when git shows the item's branch in the base the board protects already, in
|
|
10
|
+
// the checkout here or the checkouts of the item's repo; undefined otherwise.
|
|
11
|
+
export function mergedOutside(project, cwd, item) {
|
|
12
|
+
const board = liveBoardOf(project, item.collection);
|
|
13
|
+
const part = board ? guardedBranch(board.def) : undefined;
|
|
14
|
+
const base = part?.guard.base;
|
|
15
|
+
if (!board || !part || !base)
|
|
16
|
+
return undefined;
|
|
17
|
+
const branch = String(item.fields[part.id] ?? '').trim();
|
|
18
|
+
if (branch === '' || branch === base)
|
|
19
|
+
return undefined;
|
|
20
|
+
const position = positionOf(item.state);
|
|
21
|
+
const land = landStepOf(board.def);
|
|
22
|
+
const landing = land !== undefined &&
|
|
23
|
+
!isClosed(position) &&
|
|
24
|
+
position.step === land.id &&
|
|
25
|
+
position.phase === 'doing';
|
|
26
|
+
const unmarked = land === undefined &&
|
|
27
|
+
isClosed(position) &&
|
|
28
|
+
position.closed === DONE &&
|
|
29
|
+
mergedOf(project, item.id) === undefined;
|
|
30
|
+
if (!landing && !unmarked)
|
|
31
|
+
return undefined;
|
|
32
|
+
const repoPart = part.guard.repo;
|
|
33
|
+
const repo = repoPart ? String(item.fields[repoPart] ?? '').trim() : '';
|
|
34
|
+
const checkouts = repo === '' ? [cwd] : [cwd, ...checkoutsOf(project, repo)];
|
|
35
|
+
const found = checkouts.map((at) => mergedInto(at, branch, base));
|
|
36
|
+
const where = found.find((one) => one?.merged) ?? found.find((one) => one !== undefined);
|
|
37
|
+
if (!where)
|
|
38
|
+
return undefined;
|
|
39
|
+
const local = where.merged ? {} : { local: { remote: where.remote, push: where.push } };
|
|
40
|
+
return landing && land ? { branch, base, land: land.name, ...local } : { branch, base, ...local };
|
|
41
|
+
}
|
|
42
|
+
// What the CLI says of it first, and what is left to do.
|
|
43
|
+
export function mergedOutsideLine(project, cwd, item) {
|
|
44
|
+
const merged = mergedOutside(project, cwd, item);
|
|
45
|
+
if (!merged)
|
|
46
|
+
return undefined;
|
|
47
|
+
if (merged.local) {
|
|
48
|
+
const push = `push it (git push ${merged.local.push} ${merged.base})`;
|
|
49
|
+
const said = `${item.id}: its branch ${merged.branch} is in your local ${merged.base}, not yet in ${merged.local.remote}:`;
|
|
50
|
+
return merged.land
|
|
51
|
+
? `${said} ${push}, then fill in what ${merged.land} asks and hand it off (otea handoff ${item.id}).`
|
|
52
|
+
: `${said} ${push}, then record it: otea merged ${item.id}.`;
|
|
53
|
+
}
|
|
54
|
+
const said = `${item.id}: its branch ${merged.branch} is in ${merged.base} already, merged outside Otea.`;
|
|
55
|
+
return merged.land
|
|
56
|
+
? `${said} Nothing is left to merge: fill in what ${merged.land} asks and hand it off (otea handoff ${item.id}).`
|
|
57
|
+
: `${said} Record it: otea merged ${item.id}.`;
|
|
58
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { liveBoardOf } from '../app/next.js';
|
|
2
|
+
import { isClosed, positionOf } from '../core/board/engine.js';
|
|
3
|
+
import { stepById } from '../core/board/model.js';
|
|
4
|
+
import { POINT_PROVED } from '../core/board/proofs.js';
|
|
5
|
+
import { langOf, stateName, valueName } from '../core/board/reading.js';
|
|
6
|
+
import { findItem } from '../store/queries.js';
|
|
7
|
+
// The board an item is on, looked up once per collection.
|
|
8
|
+
export function boardsOf(project) {
|
|
9
|
+
const known = new Map();
|
|
10
|
+
return (collection) => {
|
|
11
|
+
if (!known.has(collection))
|
|
12
|
+
known.set(collection, liveBoardOf(project, collection)?.def);
|
|
13
|
+
return known.get(collection);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
// The collection of the item an event is about, looked up once per item ('' when it is gone).
|
|
17
|
+
export function itemCollections(project) {
|
|
18
|
+
const known = new Map();
|
|
19
|
+
return (id) => {
|
|
20
|
+
let collection = known.get(id);
|
|
21
|
+
if (collection === undefined) {
|
|
22
|
+
collection = findItem(project.db, id)?.collection ?? '';
|
|
23
|
+
known.set(id, collection);
|
|
24
|
+
}
|
|
25
|
+
return collection;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const state = (def, id) => def === undefined ? String(id) : stateName(def, String(id));
|
|
29
|
+
// Whether a state's step is gone: deleted since, or its whole board.
|
|
30
|
+
function lost(def, id) {
|
|
31
|
+
if (def === undefined)
|
|
32
|
+
return true;
|
|
33
|
+
const position = positionOf(id);
|
|
34
|
+
return !isClosed(position) && stepById(def, position.step) === undefined;
|
|
35
|
+
}
|
|
36
|
+
// What an event written before step names were kept says of a step removed since.
|
|
37
|
+
const REMOVED_STEP = 'a removed step';
|
|
38
|
+
// A state an event is about: by the live board, or by the name the event kept once it is lost; an
|
|
39
|
+
// older event that kept none says the step is gone (its id stays in the --json data).
|
|
40
|
+
function stateThen(def, id, kept) {
|
|
41
|
+
if (!lost(def, String(id)))
|
|
42
|
+
return state(def, id);
|
|
43
|
+
if (typeof kept === 'string' && kept !== '')
|
|
44
|
+
return kept;
|
|
45
|
+
return def === undefined ? String(id) : REMOVED_STEP;
|
|
46
|
+
}
|
|
47
|
+
const partOf = (def, id) => def?.parts[String(id)];
|
|
48
|
+
const partName = (def, id) => partOf(def, id)?.name ?? String(id);
|
|
49
|
+
// The kinds whose stored value is a key: each comes back with its label (a level too, P2S-R3-6).
|
|
50
|
+
const OPTIONS = ['choice', 'multi', 'level'];
|
|
51
|
+
function fieldsData(def, fields) {
|
|
52
|
+
return Object.entries(fields).map(([id, value]) => {
|
|
53
|
+
const part = partOf(def, id);
|
|
54
|
+
const field = { id, name: part?.name ?? id, value };
|
|
55
|
+
if (part && OPTIONS.includes(part.kind) && value !== null && value !== undefined)
|
|
56
|
+
field.label = valueName(part, value, def ? langOf(def) : 'en');
|
|
57
|
+
return field;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
// An event as its reader says it, its ids replaced by names: what the text prints.
|
|
61
|
+
export function readableEvent(def, event) {
|
|
62
|
+
const { data } = event;
|
|
63
|
+
const named = (fields) => ({ ...event, data: { ...data, ...fields } });
|
|
64
|
+
if (event.type === 'created')
|
|
65
|
+
return named({ state: stateThen(def, data.state, data.stateName) });
|
|
66
|
+
if (event.type === 'transitioned')
|
|
67
|
+
return named({
|
|
68
|
+
from: stateThen(def, data.from, data.fromName),
|
|
69
|
+
to: stateThen(def, data.to, data.toName),
|
|
70
|
+
});
|
|
71
|
+
if (def === undefined)
|
|
72
|
+
return event;
|
|
73
|
+
if ((event.type === 'evidence_added' || event.type === 'evidence_detached') &&
|
|
74
|
+
typeof data.module === 'string')
|
|
75
|
+
return named({ module: partName(def, data.module) });
|
|
76
|
+
if (event.type === POINT_PROVED) {
|
|
77
|
+
const part = partOf(def, data.part);
|
|
78
|
+
return named({
|
|
79
|
+
part: partName(def, data.part),
|
|
80
|
+
point: part ? valueName(part, data.point, langOf(def)) : String(data.point),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (event.type === 'updated' && data.changes && typeof data.changes === 'object') {
|
|
84
|
+
const changes = Object.fromEntries(Object.entries(data.changes).map(([key, change]) => {
|
|
85
|
+
const part = partOf(def, key);
|
|
86
|
+
if (!part)
|
|
87
|
+
return [key, change];
|
|
88
|
+
const say = (value) => value === undefined || value === null ? value : valueName(part, value, langOf(def));
|
|
89
|
+
return [part.name, { from: say(change.from), to: say(change.to) }];
|
|
90
|
+
}));
|
|
91
|
+
return named({ changes });
|
|
92
|
+
}
|
|
93
|
+
return event;
|
|
94
|
+
}
|
|
95
|
+
// An event with its steps, its field and its evidence module named beside their ids.
|
|
96
|
+
export function eventData(def, event) {
|
|
97
|
+
const { data } = event;
|
|
98
|
+
const named = (fields) => ({ ...event, data: { ...data, ...fields } });
|
|
99
|
+
if (event.type === 'created')
|
|
100
|
+
return named({ stateName: stateThen(def, data.state, data.stateName) });
|
|
101
|
+
if (event.type === 'transitioned')
|
|
102
|
+
return named({
|
|
103
|
+
fromName: stateThen(def, data.from, data.fromName),
|
|
104
|
+
toName: stateThen(def, data.to, data.toName),
|
|
105
|
+
});
|
|
106
|
+
if (event.type === 'evidence_added' && typeof data.module === 'string')
|
|
107
|
+
return named({ moduleName: partName(def, data.module) });
|
|
108
|
+
if (event.type === 'updated' && data.changes && typeof data.changes === 'object') {
|
|
109
|
+
const changes = Object.fromEntries(Object.entries(data.changes).map(([id, change]) => [
|
|
110
|
+
id,
|
|
111
|
+
{ name: partName(def, id), ...change },
|
|
112
|
+
]));
|
|
113
|
+
return named({ changes });
|
|
114
|
+
}
|
|
115
|
+
return event;
|
|
116
|
+
}
|
|
117
|
+
// An item, a list row or the whole of show, with its names beside its ids.
|
|
118
|
+
export function itemData(project, item, boardOf = boardsOf(project)) {
|
|
119
|
+
const def = boardOf(item.collection);
|
|
120
|
+
const view = item;
|
|
121
|
+
const moves = (list) => list.map((move) => ({ ...move, toName: state(def, move.to) }));
|
|
122
|
+
// stateName right after state, where a reader looks for it; the version of its board it was
|
|
123
|
+
// written under as boardVersion (presets are what boards were called once).
|
|
124
|
+
const out = Object.fromEntries(Object.entries(item).flatMap(([key, value]) => {
|
|
125
|
+
if (key === 'state')
|
|
126
|
+
return [
|
|
127
|
+
[key, value],
|
|
128
|
+
['stateName', state(def, item.state)],
|
|
129
|
+
];
|
|
130
|
+
if (key === 'presetVersion')
|
|
131
|
+
return [['boardVersion', value]];
|
|
132
|
+
return [[key, value]];
|
|
133
|
+
}));
|
|
134
|
+
out.fields = fieldsData(def, item.fields);
|
|
135
|
+
if (view.transitions)
|
|
136
|
+
out.transitions = moves(view.transitions);
|
|
137
|
+
if (view.relayable)
|
|
138
|
+
out.relayable = moves(view.relayable);
|
|
139
|
+
if (view.missing)
|
|
140
|
+
out.missing = view.missing.map((id) => ({ id, name: partName(def, id) }));
|
|
141
|
+
if (view.evidence)
|
|
142
|
+
out.evidence = view.evidence.map((entry) => entry.module === null ? entry : { ...entry, moduleName: partName(def, entry.module) });
|
|
143
|
+
if (view.relations)
|
|
144
|
+
out.relations = view.relations.map((related) => {
|
|
145
|
+
// A list's relations are bare refs (no collection or state): nothing to name.
|
|
146
|
+
const { collection, state: id } = related;
|
|
147
|
+
if (typeof collection !== 'string' || typeof id !== 'string')
|
|
148
|
+
return related;
|
|
149
|
+
return { ...related, stateName: state(boardOf(collection), id) };
|
|
150
|
+
});
|
|
151
|
+
if (view.events)
|
|
152
|
+
out.events = view.events.map((event) => eventData(def, event));
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from 'node:child_process';
|
|
2
|
+
export function openCommand(platform, url) {
|
|
3
|
+
if (platform === 'win32')
|
|
4
|
+
return { command: 'rundll32', args: ['url.dll,FileProtocolHandler', url] };
|
|
5
|
+
if (platform === 'darwin')
|
|
6
|
+
return { command: 'open', args: [url] };
|
|
7
|
+
return { command: 'xdg-open', args: [url] };
|
|
8
|
+
}
|
|
9
|
+
export function systemOpener(platform = process.platform, spawn = nodeSpawn) {
|
|
10
|
+
return (url) => {
|
|
11
|
+
const { command, args } = openCommand(platform, url);
|
|
12
|
+
const child = spawn(command, args, { detached: true, stdio: 'ignore' });
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
child.once('error', (error) => reject(error));
|
|
15
|
+
child.once('spawn', () => {
|
|
16
|
+
child.unref();
|
|
17
|
+
resolve();
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { basename, dirname, join } from 'node:path';
|
|
5
|
+
import { ownerGone, processAlive } from '../app/presence.js';
|
|
6
|
+
// The programs that only run a command for someone else: a harness runs `otea` through them (its
|
|
7
|
+
// shell, npm's sh or cmd shim), and on Windows they outlive the harness that started them.
|
|
8
|
+
const SHELLS = new Set(['sh', 'bash', 'dash', 'zsh', 'ksh', 'fish', 'cmd', 'powershell', 'pwsh']);
|
|
9
|
+
const isShell = (name) => SHELLS.has(basename(name.replaceAll('\\', '/'))
|
|
10
|
+
.toLowerCase()
|
|
11
|
+
.replace(/\.exe$/, ''));
|
|
12
|
+
// The processes a command started by `parent` belongs to: the parent, and while it is a shell the
|
|
13
|
+
// one that started it, up to the first that is none (the harness). Claude Code on Windows runs a
|
|
14
|
+
// command through bash and npm's sh shim; ending its session leaves both running, so watching the
|
|
15
|
+
// parent alone would never see the session end. `complete`: each parent in `rows` is the real one,
|
|
16
|
+
// so a shell whose parent is not listed outlived it (a session that ended before the listing), and
|
|
17
|
+
// that parent joins the chain as gone; otherwise a missing parent may be a shell's ended fork.
|
|
18
|
+
export function ownerChain(rows, parent, complete = false) {
|
|
19
|
+
const byPid = new Map(rows.map((row) => [row.pid, row]));
|
|
20
|
+
const chain = [parent];
|
|
21
|
+
let row = byPid.get(parent);
|
|
22
|
+
while (row && isShell(row.name)) {
|
|
23
|
+
const next = byPid.get(row.parent);
|
|
24
|
+
if (!next && complete && row.parent > 0 && !chain.includes(row.parent))
|
|
25
|
+
chain.push(row.parent);
|
|
26
|
+
if (!next || chain.includes(next.pid))
|
|
27
|
+
break;
|
|
28
|
+
chain.push(next.pid);
|
|
29
|
+
row = next;
|
|
30
|
+
}
|
|
31
|
+
return chain;
|
|
32
|
+
}
|
|
33
|
+
// Lines of "pid parent name", as both listings below print them; on Windows a tab and the program's
|
|
34
|
+
// file follow, when Windows says it.
|
|
35
|
+
export function readProcessTable(text) {
|
|
36
|
+
return text.split(/\r?\n/).flatMap((line) => {
|
|
37
|
+
const match = /^\s*(\d+)\s+(\d+)\s+([^\t]+?)\s*(?:\t(.*?)\s*)?$/.exec(line);
|
|
38
|
+
if (!match)
|
|
39
|
+
return [];
|
|
40
|
+
const [, pid, parent, name, path] = match;
|
|
41
|
+
const row = { pid: Number(pid), parent: Number(parent), name: name };
|
|
42
|
+
return [path ? { ...row, path } : row];
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function readPosixTable(text) {
|
|
46
|
+
return text.split(/\r?\n/).flatMap((line) => {
|
|
47
|
+
const match = /^\s*(\d+)\s+(\d+)\s+\d+\s+(\d+)\s/.exec(line);
|
|
48
|
+
if (!match)
|
|
49
|
+
return [];
|
|
50
|
+
const [, pid, parent, winpid] = match;
|
|
51
|
+
return [{ pid: Number(pid), parent: Number(parent), winpid: Number(winpid) }];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// The Windows rows with each parent as MSYS knows it. Git Bash runs a command in a fork of itself
|
|
55
|
+
// that becomes the command and ends, so Windows names as parent a process long gone, while MSYS
|
|
56
|
+
// still links the command to the bash that forked it. A program that is no MSYS one (node) runs
|
|
57
|
+
// from MSYS with the shell left as its stub: Windows lists the stub as its parent, MSYS lists the
|
|
58
|
+
// program under the stub's id, so the stub's parent is the program's MSYS parent.
|
|
59
|
+
export function withPosixParents(rows, posix) {
|
|
60
|
+
const byPid = new Map(posix.map((row) => [row.pid, row]));
|
|
61
|
+
const listed = new Set(posix.map((row) => row.winpid));
|
|
62
|
+
const parents = new Map();
|
|
63
|
+
for (const row of posix) {
|
|
64
|
+
const parent = row.parent > 1 ? byPid.get(row.parent) : undefined;
|
|
65
|
+
if (parent)
|
|
66
|
+
parents.set(row.winpid, parent.winpid);
|
|
67
|
+
}
|
|
68
|
+
for (const row of rows) {
|
|
69
|
+
const parent = parents.get(row.pid);
|
|
70
|
+
if (parent !== undefined && !listed.has(row.parent) && !parents.has(row.parent)) {
|
|
71
|
+
parents.set(row.parent, parent);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return rows.map((row) => {
|
|
75
|
+
const parent = parents.get(row.pid);
|
|
76
|
+
return parent === undefined || parent === row.parent ? row : { ...row, parent };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const WINDOWS_LISTING = 'Get-CimInstance Win32_Process -Property ProcessId,ParentProcessId,Name,ExecutablePath | ForEach-Object { "$($_.ProcessId) $($_.ParentProcessId) $($_.Name)`t$($_.ExecutablePath)" }';
|
|
80
|
+
// The busy Windows runner answered WMI after 20 s; the owner check waits for it in the background.
|
|
81
|
+
const LISTING_TIMEOUT_MS = 60_000;
|
|
82
|
+
function run(command, args, signal) {
|
|
83
|
+
return new Promise((resolve) => {
|
|
84
|
+
execFile(command, args,
|
|
85
|
+
// From the temp folder: on Windows a process holds the folder it runs in, and the listing
|
|
86
|
+
// may outlive this command for a moment; it must never keep the project's folder busy.
|
|
87
|
+
{ cwd: tmpdir(), encoding: 'utf8', timeout: LISTING_TIMEOUT_MS, windowsHide: true, signal }, (error, stdout) => resolve(error ? undefined : stdout));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// The ps of the MSYS that runs `start` (Git Bash's usr/bin, next to its sh.exe), if it is one.
|
|
91
|
+
function posixLister(rows, start) {
|
|
92
|
+
const path = rows.find((row) => row.pid === start)?.path;
|
|
93
|
+
if (!path || !isShell(path))
|
|
94
|
+
return undefined;
|
|
95
|
+
const ps = join(dirname(path), 'ps.exe');
|
|
96
|
+
return existsSync(ps) ? ps : undefined;
|
|
97
|
+
}
|
|
98
|
+
// Every process on this computer with its parent, or undefined when the system does not say. On
|
|
99
|
+
// Windows, a command `start` runs through Git Bash gets its parents from MSYS (withPosixParents);
|
|
100
|
+
// without MSYS's table, a parent Windows names may be a shell's fork that ended.
|
|
101
|
+
export async function listProcesses(signal, start = process.ppid) {
|
|
102
|
+
if (process.platform !== 'win32') {
|
|
103
|
+
const out = await run('ps', ['-A', '-o', 'pid=,ppid=,comm='], signal);
|
|
104
|
+
return out === undefined ? undefined : { rows: readProcessTable(out), complete: true };
|
|
105
|
+
}
|
|
106
|
+
const out = await run('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', WINDOWS_LISTING], signal);
|
|
107
|
+
if (out === undefined)
|
|
108
|
+
return undefined;
|
|
109
|
+
const rows = readProcessTable(out);
|
|
110
|
+
const ps = posixLister(rows, start);
|
|
111
|
+
const posix = ps === undefined ? undefined : await run(ps, [], signal);
|
|
112
|
+
return posix === undefined
|
|
113
|
+
? { rows, complete: false }
|
|
114
|
+
: { rows: withPosixParents(rows, readPosixTable(posix)), complete: true };
|
|
115
|
+
}
|
|
116
|
+
// How often a long-running command checks that the processes that started it still run.
|
|
117
|
+
const OWNER_CHECK_MS = 5_000;
|
|
118
|
+
// Aborts `signal` once the session that started this process is gone (ownerGone), on `parent` (the
|
|
119
|
+
// command's interrupt): left running, a wait would keep its agent shown as listening until its
|
|
120
|
+
// timeout, and a server would keep serving no one. The shells above it are read on the first
|
|
121
|
+
// check, so a command that ends at once never lists the processes.
|
|
122
|
+
export function watchOwner(parent, { list = listProcesses, alive = processAlive, everyMs = OWNER_CHECK_MS, } = {}) {
|
|
123
|
+
const start = process.ppid;
|
|
124
|
+
const controller = new AbortController();
|
|
125
|
+
const listing = new AbortController();
|
|
126
|
+
let chain = [start];
|
|
127
|
+
let listed = false;
|
|
128
|
+
let orphaned = false;
|
|
129
|
+
const onAbort = () => controller.abort();
|
|
130
|
+
parent.addEventListener('abort', onAbort, { once: true });
|
|
131
|
+
const timer = setInterval(() => {
|
|
132
|
+
if (ownerGone(start, process.ppid, alive, chain)) {
|
|
133
|
+
orphaned = true;
|
|
134
|
+
controller.abort();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (listed)
|
|
138
|
+
return;
|
|
139
|
+
listed = true;
|
|
140
|
+
void list(listing.signal).then((table) => {
|
|
141
|
+
if (table)
|
|
142
|
+
chain = ownerChain(table.rows, start, table.complete);
|
|
143
|
+
});
|
|
144
|
+
}, everyMs);
|
|
145
|
+
timer.unref();
|
|
146
|
+
const stop = () => {
|
|
147
|
+
clearInterval(timer);
|
|
148
|
+
listing.abort();
|
|
149
|
+
parent.removeEventListener('abort', onAbort);
|
|
150
|
+
};
|
|
151
|
+
// Once stopped for any reason it lists nothing more, so a listing never keeps the process alive.
|
|
152
|
+
controller.signal.addEventListener('abort', stop, { once: true });
|
|
153
|
+
return { signal: controller.signal, orphaned: () => orphaned, stop, started: start };
|
|
154
|
+
}
|