@skillre/dsh-plugin-work-dashboard 0.0.0-stage → 0.6.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.en.md +253 -0
- package/README.md +347 -2
- package/UNINSTALL.md +7 -0
- package/compatibility.json +155 -0
- package/cordis.patch.yml +24 -0
- package/docs/DESIGN-0.3.md +122 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +254 -0
- package/docs/preview.zh.svg +254 -0
- package/icon.svg +17 -0
- package/lib/client/api.d.ts +35 -0
- package/lib/client/api.js +44 -0
- package/lib/client/contracts.d.ts +94 -0
- package/lib/client/contracts.js +21 -0
- package/lib/client/css/base.d.ts +31 -0
- package/lib/client/css/base.js +422 -0
- package/lib/client/css/charts.d.ts +14 -0
- package/lib/client/css/charts.js +191 -0
- package/lib/client/css/detail.d.ts +21 -0
- package/lib/client/css/detail.js +399 -0
- package/lib/client/css/insights.d.ts +27 -0
- package/lib/client/css/insights.js +660 -0
- package/lib/client/css/overview.d.ts +22 -0
- package/lib/client/css/overview.js +338 -0
- package/lib/client/preferences.d.ts +56 -0
- package/lib/client/preferences.js +118 -0
- package/lib/client/styles.d.ts +17 -0
- package/lib/client/styles.js +22 -0
- package/lib/client/text.d.ts +32 -0
- package/lib/client/text.js +934 -0
- package/lib/client/view-contract.d.ts +110 -0
- package/lib/client/view-contract.js +28 -0
- package/lib/client.js +5389 -0
- package/lib/config.d.ts +61 -0
- package/lib/config.js +67 -0
- package/lib/gap.d.ts +60 -0
- package/lib/gap.js +227 -0
- package/lib/index.d.ts +40 -0
- package/lib/index.js +130 -0
- package/lib/repo.d.ts +124 -0
- package/lib/repo.js +407 -0
- package/lib/risks.d.ts +24 -0
- package/lib/risks.js +85 -0
- package/lib/skill.d.ts +94 -0
- package/lib/skill.js +220 -0
- package/lib/store.d.ts +63 -0
- package/lib/store.js +494 -0
- package/lib/timeline.d.ts +23 -0
- package/lib/timeline.js +93 -0
- package/lib/transport.d.ts +61 -0
- package/lib/transport.js +121 -0
- package/lib/types.d.ts +612 -0
- package/lib/types.js +62 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +117 -4
- package/skills/poc-export/SKILL.md +81 -0
- package/skills/poc-export/scripts/export-form.mjs +654 -0
- package/skills/poc-ledger/SKILL.md +171 -0
- package/skills/poc-submit/SKILL.md +186 -0
package/lib/repo.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local git cache of the POC ledger repository.
|
|
3
|
+
*
|
|
4
|
+
* Three invariants drive this module:
|
|
5
|
+
*
|
|
6
|
+
* 1. **The cache is disposable.** It is wiped at mount and rebuilt on demand,
|
|
7
|
+
* so the plugin can never corrupt the real data source: the repository on
|
|
8
|
+
* GitHub stays the only truth.
|
|
9
|
+
* 2. **The cache is only ever a fast-forward copy.** A refresh is
|
|
10
|
+
* `clone` (first time) or `fetch --depth 1` + `reset --hard origin/<branch>`.
|
|
11
|
+
* Never `pull`: a merge could block on a conflict in a directory nobody is
|
|
12
|
+
* meant to edit.
|
|
13
|
+
* 3. **A refresh is exclusive.** One lock file keeps two console instances in
|
|
14
|
+
* the same DSH home from cloning into the same directory at once.
|
|
15
|
+
*
|
|
16
|
+
* Authentication is deliberately never stored: the token is read from the
|
|
17
|
+
* already-authenticated `gh` CLI at call time and passed to git through an
|
|
18
|
+
* environment-scoped `http.extraheader`, so it never lands in `.git/config`,
|
|
19
|
+
* never appears in a process argument list, and never reaches the Client.
|
|
20
|
+
*/
|
|
21
|
+
/** Failure carrying a stable code the transport maps onto the RPC envelope. */
|
|
22
|
+
export declare class RepoError extends Error {
|
|
23
|
+
readonly code: string;
|
|
24
|
+
readonly details: Record<string, unknown>;
|
|
25
|
+
constructor(code: string, message: string, details?: Record<string, unknown>);
|
|
26
|
+
}
|
|
27
|
+
/** Largest single YAML file the store will read, in bytes. */
|
|
28
|
+
export declare const MAX_YAML_BYTES: number;
|
|
29
|
+
/** Resolved cache settings this module needs. */
|
|
30
|
+
export interface RepoOptions {
|
|
31
|
+
/** Absolute cache root; wiped at mount. */
|
|
32
|
+
cacheRoot: string;
|
|
33
|
+
/** `owner/name` on GitHub. */
|
|
34
|
+
repository: string;
|
|
35
|
+
/** Branch tracked by the clone. */
|
|
36
|
+
branch: string;
|
|
37
|
+
/** Clone URL; the canonical GitHub URL unless overridden. */
|
|
38
|
+
remoteUrl: string;
|
|
39
|
+
/** Ceiling for one clone, in milliseconds. */
|
|
40
|
+
cloneTimeoutMs: number;
|
|
41
|
+
/** Ceiling for one fetch + reset, in milliseconds. */
|
|
42
|
+
fetchTimeoutMs: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Reject a cache root that is obviously not a disposable directory.
|
|
46
|
+
*
|
|
47
|
+
* The mount wipes this path, so a mistaken value (home, `/`, a parent of the
|
|
48
|
+
* DSH home) must fail at mount rather than delete real user data.
|
|
49
|
+
*/
|
|
50
|
+
export declare function assertSafeCacheRoot(cacheRoot: string, home: string): string;
|
|
51
|
+
/** Result of one successful refresh. */
|
|
52
|
+
export interface RefreshResult {
|
|
53
|
+
mode: 'clone' | 'fetch';
|
|
54
|
+
durationMs: number;
|
|
55
|
+
finishedAt: string;
|
|
56
|
+
/** Short HEAD sha the cache now points at. */
|
|
57
|
+
head: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The disposable git cache.
|
|
61
|
+
*
|
|
62
|
+
* Lifecycle: `prepare()` at mount wipes and recreates the cache root; the first
|
|
63
|
+
* `refresh()` clones, later ones fast-forward. Every method is safe to call
|
|
64
|
+
* concurrently except that refreshes serialize on a lock.
|
|
65
|
+
*/
|
|
66
|
+
export declare class RepoCache {
|
|
67
|
+
private readonly options;
|
|
68
|
+
/** Directory the ledger is read from once a clone exists. */
|
|
69
|
+
readonly repoPath: string;
|
|
70
|
+
private fetchedAt;
|
|
71
|
+
private head;
|
|
72
|
+
constructor(options: RepoOptions);
|
|
73
|
+
/**
|
|
74
|
+
* Wipe the cache and recreate it, so every DSH start begins from "no data".
|
|
75
|
+
*
|
|
76
|
+
* Only a directory this plugin created is ever removed: a pre-existing
|
|
77
|
+
* directory without the ownership marker fails loudly instead of being
|
|
78
|
+
* deleted, because a mistaken `cacheRoot` must not destroy user files.
|
|
79
|
+
*/
|
|
80
|
+
prepare(): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Map a filesystem failure onto an authored, user-actionable code.
|
|
83
|
+
*
|
|
84
|
+
* The cache lives under `$DSH_HOME/cache`, which the Host process normally
|
|
85
|
+
* owns; a sandboxed or read-only deployment can deny it. Without this mapping
|
|
86
|
+
* the first request would surface as an opaque "internal error".
|
|
87
|
+
*/
|
|
88
|
+
private fsFailure;
|
|
89
|
+
private hasMarker;
|
|
90
|
+
/**
|
|
91
|
+
* An empty directory is safe to adopt: nothing can be lost by clearing it.
|
|
92
|
+
*
|
|
93
|
+
* This matters on the very first mount, when `mkdir -p` may have been run
|
|
94
|
+
* outside the plugin (a user pre-creating `~/.dsh/cache/work-dashboard`), and on
|
|
95
|
+
* a re-mount after a wipe that left the root empty.
|
|
96
|
+
*/
|
|
97
|
+
private isEmptyDirectory;
|
|
98
|
+
/** Whether a clone exists and its working tree is readable. */
|
|
99
|
+
isReady(): Promise<boolean>;
|
|
100
|
+
/** ISO timestamp of the last successful refresh in this process. */
|
|
101
|
+
get lastFetchedAt(): string | null;
|
|
102
|
+
/** The configured `owner/name` data source. */
|
|
103
|
+
get repository(): string;
|
|
104
|
+
/** The configured branch the cache tracks. */
|
|
105
|
+
get branch(): string;
|
|
106
|
+
/** Short HEAD sha after the last successful refresh, if known. */
|
|
107
|
+
get lastHead(): string | null;
|
|
108
|
+
/** Read the `gh` token at call time; absent when `gh` is not authenticated. */
|
|
109
|
+
private ghToken;
|
|
110
|
+
/**
|
|
111
|
+
* Clone or fast-forward the cache.
|
|
112
|
+
*
|
|
113
|
+
* Serialized by a lock file in the cache root: two browser tabs pressing
|
|
114
|
+
* refresh, or two DSH instances sharing a home, must not clone into the same
|
|
115
|
+
* directory concurrently.
|
|
116
|
+
*/
|
|
117
|
+
refresh(): Promise<RefreshResult>;
|
|
118
|
+
/** Take the cache lock, waiting briefly and reclaiming a stale one. */
|
|
119
|
+
private acquireLock;
|
|
120
|
+
/** Every `projects/*.yaml` file path in the cache, sorted by file name. */
|
|
121
|
+
projectFiles(): Promise<string[]>;
|
|
122
|
+
/** Read one project file, bounded so one huge file cannot stall the console. */
|
|
123
|
+
readProject(path: string): Promise<string>;
|
|
124
|
+
}
|
package/lib/repo.js
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local git cache of the POC ledger repository.
|
|
3
|
+
*
|
|
4
|
+
* Three invariants drive this module:
|
|
5
|
+
*
|
|
6
|
+
* 1. **The cache is disposable.** It is wiped at mount and rebuilt on demand,
|
|
7
|
+
* so the plugin can never corrupt the real data source: the repository on
|
|
8
|
+
* GitHub stays the only truth.
|
|
9
|
+
* 2. **The cache is only ever a fast-forward copy.** A refresh is
|
|
10
|
+
* `clone` (first time) or `fetch --depth 1` + `reset --hard origin/<branch>`.
|
|
11
|
+
* Never `pull`: a merge could block on a conflict in a directory nobody is
|
|
12
|
+
* meant to edit.
|
|
13
|
+
* 3. **A refresh is exclusive.** One lock file keeps two console instances in
|
|
14
|
+
* the same DSH home from cloning into the same directory at once.
|
|
15
|
+
*
|
|
16
|
+
* Authentication is deliberately never stored: the token is read from the
|
|
17
|
+
* already-authenticated `gh` CLI at call time and passed to git through an
|
|
18
|
+
* environment-scoped `http.extraheader`, so it never lands in `.git/config`,
|
|
19
|
+
* never appears in a process argument list, and never reaches the Client.
|
|
20
|
+
*/
|
|
21
|
+
import { spawn } from 'node:child_process';
|
|
22
|
+
import { access, mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
23
|
+
import { join } from 'node:path';
|
|
24
|
+
/** Failure carrying a stable code the transport maps onto the RPC envelope. */
|
|
25
|
+
export class RepoError extends Error {
|
|
26
|
+
code;
|
|
27
|
+
details;
|
|
28
|
+
constructor(code, message, details = {}) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.details = details;
|
|
32
|
+
this.name = 'RepoError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Ownership marker: the plugin only wipes a cache directory it created. */
|
|
36
|
+
const MARKER = '.work-dashboard-cache';
|
|
37
|
+
/** Lock file name inside the cache root. */
|
|
38
|
+
const LOCK = '.lock';
|
|
39
|
+
/** A lock older than this is treated as a crashed refresh and reclaimed. */
|
|
40
|
+
const LOCK_STALE_MS = 120_000;
|
|
41
|
+
/** Longest a lock wait will block before reporting a concurrent refresh. */
|
|
42
|
+
const LOCK_WAIT_MS = 15_000;
|
|
43
|
+
/** Largest single YAML file the store will read, in bytes. */
|
|
44
|
+
export const MAX_YAML_BYTES = 512 * 1024;
|
|
45
|
+
/**
|
|
46
|
+
* Reject a cache root that is obviously not a disposable directory.
|
|
47
|
+
*
|
|
48
|
+
* The mount wipes this path, so a mistaken value (home, `/`, a parent of the
|
|
49
|
+
* DSH home) must fail at mount rather than delete real user data.
|
|
50
|
+
*/
|
|
51
|
+
export function assertSafeCacheRoot(cacheRoot, home) {
|
|
52
|
+
const path = cacheRoot.trim();
|
|
53
|
+
if (path === '')
|
|
54
|
+
throw new RepoError('bad-cache-root', 'The POC console cache root is empty.');
|
|
55
|
+
if (path === home)
|
|
56
|
+
throw new RepoError('bad-cache-root', 'The POC console cache root must not be the home directory.');
|
|
57
|
+
if (path.split('/').filter((part) => part !== '').length < 3) {
|
|
58
|
+
throw new RepoError('bad-cache-root', `The POC console cache root is too shallow to be a cache: ${path}`);
|
|
59
|
+
}
|
|
60
|
+
return path;
|
|
61
|
+
}
|
|
62
|
+
/** Environment for one git invocation: no prompts, and optional auth header. */
|
|
63
|
+
function gitEnv(token) {
|
|
64
|
+
const env = {
|
|
65
|
+
...process.env,
|
|
66
|
+
// A missing credential must fail, never wait on a terminal prompt.
|
|
67
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
68
|
+
GIT_ASKPASS: 'true',
|
|
69
|
+
// No interactive pager and no locale-dependent output parsing.
|
|
70
|
+
GIT_PAGER: 'cat',
|
|
71
|
+
LC_ALL: 'C',
|
|
72
|
+
};
|
|
73
|
+
if (token !== null) {
|
|
74
|
+
// Passed through the environment so the token is not visible in `ps`.
|
|
75
|
+
// `x-access-token` is the GitHub App-compatible basic-auth user for a PAT.
|
|
76
|
+
const basic = Buffer.from(`x-access-token:${token}`, 'utf8').toString('base64');
|
|
77
|
+
env.GIT_CONFIG_COUNT = '1';
|
|
78
|
+
env.GIT_CONFIG_KEY_0 = 'http.https://github.com/.extraheader';
|
|
79
|
+
env.GIT_CONFIG_VALUE_0 = `Authorization: Basic ${basic}`;
|
|
80
|
+
}
|
|
81
|
+
return env;
|
|
82
|
+
}
|
|
83
|
+
/** Run one command with a hard timeout, killing its process group on expiry. */
|
|
84
|
+
function run(command, args, options) {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
const child = spawn(command, args, {
|
|
87
|
+
...(options.cwd === undefined ? {} : { cwd: options.cwd }),
|
|
88
|
+
env: options.env,
|
|
89
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
90
|
+
// Own process group so a timeout can kill git and any transport child.
|
|
91
|
+
detached: process.platform !== 'win32',
|
|
92
|
+
});
|
|
93
|
+
let stdout = '';
|
|
94
|
+
let stderr = '';
|
|
95
|
+
let timedOut = false;
|
|
96
|
+
const append = (current, chunk) => (current + chunk.toString('utf8')).slice(-8_000);
|
|
97
|
+
child.stdout.on('data', (chunk) => { stdout = append(stdout, chunk); });
|
|
98
|
+
child.stderr.on('data', (chunk) => { stderr = append(stderr, chunk); });
|
|
99
|
+
const timer = setTimeout(() => {
|
|
100
|
+
timedOut = true;
|
|
101
|
+
killTree(child.pid);
|
|
102
|
+
}, options.timeoutMs);
|
|
103
|
+
child.once('error', (error) => {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
reject(new RepoError('git-missing', `Cannot run ${command}: ${error.message}`, { command }));
|
|
106
|
+
});
|
|
107
|
+
child.once('close', (code, signal) => {
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
resolve({ code, signal, stdout, stderr, timedOut });
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** Kill a detached child's whole process group, falling back to the child. */
|
|
114
|
+
function killTree(pid) {
|
|
115
|
+
if (pid === undefined)
|
|
116
|
+
return;
|
|
117
|
+
try {
|
|
118
|
+
if (process.platform === 'win32')
|
|
119
|
+
process.kill(pid, 'SIGKILL');
|
|
120
|
+
else
|
|
121
|
+
process.kill(-pid, 'SIGKILL');
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// The group may already be gone; a later SIGTERM would be a no-op anyway.
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Map a failed git invocation onto a user-actionable message. */
|
|
128
|
+
function classify(result, action, repository) {
|
|
129
|
+
const text = `${result.stderr}\n${result.stdout}`.toLowerCase();
|
|
130
|
+
if (result.timedOut) {
|
|
131
|
+
return new RepoError('timeout', `git ${action} for ${repository} did not finish within the configured timeout. On a slow network, raise cloneTimeoutMs / fetchTimeoutMs in the plugin configuration.`, { action, repository });
|
|
132
|
+
}
|
|
133
|
+
if (text.includes('authentication failed') ||
|
|
134
|
+
text.includes('could not read username') ||
|
|
135
|
+
text.includes('permission denied') ||
|
|
136
|
+
text.includes('repository not found') ||
|
|
137
|
+
text.includes('invalid username or password') ||
|
|
138
|
+
text.includes('terminal prompts disabled')) {
|
|
139
|
+
return new RepoError('auth', `Cannot read ${repository}: the GitHub credential was rejected. Run "gh auth login" for the account that can read this private repository.`, { action, repository });
|
|
140
|
+
}
|
|
141
|
+
if (text.includes('could not resolve host') ||
|
|
142
|
+
text.includes('connection timed out') ||
|
|
143
|
+
text.includes('failed to connect') ||
|
|
144
|
+
text.includes('network is unreachable') ||
|
|
145
|
+
text.includes('connection reset')) {
|
|
146
|
+
return new RepoError('network', `Cannot reach github.com while running git ${action}.`, { action, repository });
|
|
147
|
+
}
|
|
148
|
+
if (text.includes('not found') || text.includes('does not exist')) {
|
|
149
|
+
return new RepoError('not-found', `The repository ${repository} does not exist, or this account cannot see it.`, { action, repository });
|
|
150
|
+
}
|
|
151
|
+
const detail = `${result.stderr}`.trim().split('\n').slice(-3).join(' ').slice(0, 500);
|
|
152
|
+
return new RepoError('git-failed', `git ${action} failed${detail === '' ? '' : `: ${detail}`}`, { action, repository, exitCode: result.code });
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The disposable git cache.
|
|
156
|
+
*
|
|
157
|
+
* Lifecycle: `prepare()` at mount wipes and recreates the cache root; the first
|
|
158
|
+
* `refresh()` clones, later ones fast-forward. Every method is safe to call
|
|
159
|
+
* concurrently except that refreshes serialize on a lock.
|
|
160
|
+
*/
|
|
161
|
+
export class RepoCache {
|
|
162
|
+
options;
|
|
163
|
+
/** Directory the ledger is read from once a clone exists. */
|
|
164
|
+
repoPath;
|
|
165
|
+
fetchedAt = null;
|
|
166
|
+
head = null;
|
|
167
|
+
constructor(options) {
|
|
168
|
+
this.options = options;
|
|
169
|
+
this.repoPath = join(options.cacheRoot, 'repo');
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Wipe the cache and recreate it, so every DSH start begins from "no data".
|
|
173
|
+
*
|
|
174
|
+
* Only a directory this plugin created is ever removed: a pre-existing
|
|
175
|
+
* directory without the ownership marker fails loudly instead of being
|
|
176
|
+
* deleted, because a mistaken `cacheRoot` must not destroy user files.
|
|
177
|
+
*/
|
|
178
|
+
async prepare() {
|
|
179
|
+
const root = this.options.cacheRoot;
|
|
180
|
+
let exists = false;
|
|
181
|
+
try {
|
|
182
|
+
exists = (await stat(root)).isDirectory();
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
// A permission denial is not "absent": report it instead of pretending
|
|
186
|
+
// the cache is cold, then failing later with an opaque mkdir error.
|
|
187
|
+
if (error.code !== 'ENOENT')
|
|
188
|
+
throw this.fsFailure(error, root);
|
|
189
|
+
exists = false;
|
|
190
|
+
}
|
|
191
|
+
if (exists) {
|
|
192
|
+
const owned = (await this.hasMarker(root)) || (await this.isEmptyDirectory(root));
|
|
193
|
+
if (!owned) {
|
|
194
|
+
throw new RepoError('cache-not-owned', `The cache root ${root} already exists but was not created by the POC console; refusing to clear it. Remove it manually or point cacheRoot at a fresh directory.`, { cacheRoot: root });
|
|
195
|
+
}
|
|
196
|
+
await rm(root, { recursive: true, force: true });
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await mkdir(root, { recursive: true });
|
|
200
|
+
await writeFile(join(root, MARKER), 'This directory is a disposable POC console cache.\n', 'utf8');
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
throw this.fsFailure(error, root);
|
|
204
|
+
}
|
|
205
|
+
this.fetchedAt = null;
|
|
206
|
+
this.head = null;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Map a filesystem failure onto an authored, user-actionable code.
|
|
210
|
+
*
|
|
211
|
+
* The cache lives under `$DSH_HOME/cache`, which the Host process normally
|
|
212
|
+
* owns; a sandboxed or read-only deployment can deny it. Without this mapping
|
|
213
|
+
* the first request would surface as an opaque "internal error".
|
|
214
|
+
*/
|
|
215
|
+
fsFailure(error, path) {
|
|
216
|
+
const code = error.code ?? '';
|
|
217
|
+
if (code === 'EPERM' || code === 'EACCES' || code === 'EROFS') {
|
|
218
|
+
return new RepoError('cache-unwritable', `Cannot write the POC console cache at ${path}: the Host process has no permission. Point cacheRoot at a writable directory in the plugin configuration.`, { path, filesystemCode: code });
|
|
219
|
+
}
|
|
220
|
+
if (code === 'ENOSPC') {
|
|
221
|
+
return new RepoError('cache-full', `Cannot write the POC console cache at ${path}: the disk is full.`, { path });
|
|
222
|
+
}
|
|
223
|
+
return new RepoError('cache-failed', `Cannot prepare the POC console cache at ${path}: ${code || String(error)}.`, { path, filesystemCode: code });
|
|
224
|
+
}
|
|
225
|
+
async hasMarker(root) {
|
|
226
|
+
try {
|
|
227
|
+
const marker = await stat(join(root, MARKER));
|
|
228
|
+
return marker.isFile();
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* An empty directory is safe to adopt: nothing can be lost by clearing it.
|
|
236
|
+
*
|
|
237
|
+
* This matters on the very first mount, when `mkdir -p` may have been run
|
|
238
|
+
* outside the plugin (a user pre-creating `~/.dsh/cache/work-dashboard`), and on
|
|
239
|
+
* a re-mount after a wipe that left the root empty.
|
|
240
|
+
*/
|
|
241
|
+
async isEmptyDirectory(root) {
|
|
242
|
+
try {
|
|
243
|
+
return (await readdir(root)).length === 0;
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/** Whether a clone exists and its working tree is readable. */
|
|
250
|
+
async isReady() {
|
|
251
|
+
try {
|
|
252
|
+
await access(join(this.repoPath, '.git'));
|
|
253
|
+
await access(join(this.repoPath, 'projects'));
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** ISO timestamp of the last successful refresh in this process. */
|
|
261
|
+
get lastFetchedAt() {
|
|
262
|
+
return this.fetchedAt;
|
|
263
|
+
}
|
|
264
|
+
/** The configured `owner/name` data source. */
|
|
265
|
+
get repository() {
|
|
266
|
+
return this.options.repository;
|
|
267
|
+
}
|
|
268
|
+
/** The configured branch the cache tracks. */
|
|
269
|
+
get branch() {
|
|
270
|
+
return this.options.branch;
|
|
271
|
+
}
|
|
272
|
+
/** Short HEAD sha after the last successful refresh, if known. */
|
|
273
|
+
get lastHead() {
|
|
274
|
+
return this.head;
|
|
275
|
+
}
|
|
276
|
+
/** Read the `gh` token at call time; absent when `gh` is not authenticated. */
|
|
277
|
+
async ghToken() {
|
|
278
|
+
const result = await run('gh', ['auth', 'token'], {
|
|
279
|
+
env: gitEnv(null),
|
|
280
|
+
timeoutMs: 10_000,
|
|
281
|
+
}).catch(() => null);
|
|
282
|
+
if (result === null || result.code !== 0)
|
|
283
|
+
return null;
|
|
284
|
+
const token = result.stdout.trim();
|
|
285
|
+
return token === '' ? null : token;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Clone or fast-forward the cache.
|
|
289
|
+
*
|
|
290
|
+
* Serialized by a lock file in the cache root: two browser tabs pressing
|
|
291
|
+
* refresh, or two DSH instances sharing a home, must not clone into the same
|
|
292
|
+
* directory concurrently.
|
|
293
|
+
*/
|
|
294
|
+
async refresh() {
|
|
295
|
+
const startedAt = Date.now();
|
|
296
|
+
const release = await this.acquireLock();
|
|
297
|
+
try {
|
|
298
|
+
const token = await this.ghToken();
|
|
299
|
+
if (token === null) {
|
|
300
|
+
throw new RepoError('gh-unauthenticated', 'The gh CLI is not authenticated. Run "gh auth login" for an account that can read the POC ledger repository.', { repository: this.options.repository });
|
|
301
|
+
}
|
|
302
|
+
const remote = this.options.remoteUrl;
|
|
303
|
+
const env = gitEnv(token);
|
|
304
|
+
const ready = await this.isReady();
|
|
305
|
+
const mode = ready ? 'fetch' : 'clone';
|
|
306
|
+
if (!ready) {
|
|
307
|
+
try {
|
|
308
|
+
await rm(this.repoPath, { recursive: true, force: true });
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
throw this.fsFailure(error, this.repoPath);
|
|
312
|
+
}
|
|
313
|
+
const result = await run('git', ['clone', '--depth', '1', '--single-branch', '--branch', this.options.branch, remote, this.repoPath], { env, timeoutMs: this.options.cloneTimeoutMs });
|
|
314
|
+
if (result.code !== 0)
|
|
315
|
+
throw classify(result, 'clone', this.options.repository);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
const fetch = await run('git', ['fetch', '--depth', '1', 'origin', this.options.branch], {
|
|
319
|
+
cwd: this.repoPath,
|
|
320
|
+
env,
|
|
321
|
+
timeoutMs: this.options.fetchTimeoutMs,
|
|
322
|
+
});
|
|
323
|
+
if (fetch.code !== 0)
|
|
324
|
+
throw classify(fetch, 'fetch', this.options.repository);
|
|
325
|
+
// The cache is a read-only copy, so align it forcibly instead of merging.
|
|
326
|
+
const reset = await run('git', ['reset', '--hard', `origin/${this.options.branch}`], {
|
|
327
|
+
cwd: this.repoPath,
|
|
328
|
+
env,
|
|
329
|
+
timeoutMs: this.options.fetchTimeoutMs,
|
|
330
|
+
});
|
|
331
|
+
if (reset.code !== 0)
|
|
332
|
+
throw classify(reset, 'fetch', this.options.repository);
|
|
333
|
+
}
|
|
334
|
+
const head = await run('git', ['rev-parse', '--short', 'HEAD'], { cwd: this.repoPath, env, timeoutMs: 10_000 });
|
|
335
|
+
this.head = head.code === 0 ? head.stdout.trim() : null;
|
|
336
|
+
this.fetchedAt = new Date().toISOString();
|
|
337
|
+
return {
|
|
338
|
+
mode,
|
|
339
|
+
durationMs: Date.now() - startedAt,
|
|
340
|
+
finishedAt: this.fetchedAt,
|
|
341
|
+
head: this.head ?? '',
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
await release();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/** Take the cache lock, waiting briefly and reclaiming a stale one. */
|
|
349
|
+
async acquireLock() {
|
|
350
|
+
const path = join(this.options.cacheRoot, LOCK);
|
|
351
|
+
const deadline = Date.now() + LOCK_WAIT_MS;
|
|
352
|
+
for (;;) {
|
|
353
|
+
try {
|
|
354
|
+
await writeFile(path, `${process.pid} ${Date.now()}\n`, { encoding: 'utf8', flag: 'wx' });
|
|
355
|
+
return async () => {
|
|
356
|
+
await rm(path, { force: true });
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
catch (error) {
|
|
360
|
+
const code = error.code;
|
|
361
|
+
if (code !== 'EEXIST')
|
|
362
|
+
throw this.fsFailure(error, path);
|
|
363
|
+
// Reclaim a lock left behind by a crashed process.
|
|
364
|
+
try {
|
|
365
|
+
const info = await stat(path);
|
|
366
|
+
if (Date.now() - info.mtimeMs > LOCK_STALE_MS) {
|
|
367
|
+
await rm(path, { force: true });
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (Date.now() > deadline) {
|
|
375
|
+
throw new RepoError('busy', 'Another POC console refresh is already running; try again in a moment.');
|
|
376
|
+
}
|
|
377
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/** Every `projects/*.yaml` file path in the cache, sorted by file name. */
|
|
382
|
+
async projectFiles() {
|
|
383
|
+
const dir = join(this.repoPath, 'projects');
|
|
384
|
+
let names;
|
|
385
|
+
try {
|
|
386
|
+
names = await readdir(dir);
|
|
387
|
+
}
|
|
388
|
+
catch (error) {
|
|
389
|
+
if (error.code === 'ENOENT') {
|
|
390
|
+
throw new RepoError('no-projects', `The repository has no projects/ directory at ${dir}.`, { path: dir });
|
|
391
|
+
}
|
|
392
|
+
throw error;
|
|
393
|
+
}
|
|
394
|
+
return names
|
|
395
|
+
.filter((name) => name.endsWith('.yaml') || name.endsWith('.yml'))
|
|
396
|
+
.sort()
|
|
397
|
+
.map((name) => join(dir, name));
|
|
398
|
+
}
|
|
399
|
+
/** Read one project file, bounded so one huge file cannot stall the console. */
|
|
400
|
+
async readProject(path) {
|
|
401
|
+
const info = await stat(path);
|
|
402
|
+
if (info.size > MAX_YAML_BYTES) {
|
|
403
|
+
throw new RepoError('too-large', `${path} is larger than ${MAX_YAML_BYTES} bytes and was skipped.`, { path, size: info.size });
|
|
404
|
+
}
|
|
405
|
+
return readFile(path, 'utf8');
|
|
406
|
+
}
|
|
407
|
+
}
|
package/lib/risks.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The risk board: every project's risks, sorted by severity.
|
|
3
|
+
*
|
|
4
|
+
* The board answers a different question from the project table. The table says
|
|
5
|
+
* "which project needs attention"; the board says "what could go wrong, and
|
|
6
|
+
* where". It is built from the same records, so the two can never disagree.
|
|
7
|
+
*
|
|
8
|
+
* Severity ordering is deliberate: a `high` risk sorts above every `medium`,
|
|
9
|
+
* and a blank level sorts LAST rather than first. An unset level is not a
|
|
10
|
+
* statement that the risk is mild — it is a missing judgement, and putting it
|
|
11
|
+
* at the top would bury the risks someone actually assessed as serious.
|
|
12
|
+
*/
|
|
13
|
+
import type { ScanOptions } from './gap.js';
|
|
14
|
+
import type { PocRecord, RiskBoard } from './types.js';
|
|
15
|
+
/** Severity rank: higher sorts first. A blank level ranks below `low`. */
|
|
16
|
+
export declare function riskRank(level: string): number;
|
|
17
|
+
/**
|
|
18
|
+
* Build the board from the loaded records.
|
|
19
|
+
*
|
|
20
|
+
* Rows are ordered by severity, then by project id, then by the risk's own
|
|
21
|
+
* order inside the record — a stable order that does not reshuffle between
|
|
22
|
+
* refreshes.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildRiskBoard(records: readonly PocRecord[], options: ScanOptions): RiskBoard;
|
package/lib/risks.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The risk board: every project's risks, sorted by severity.
|
|
3
|
+
*
|
|
4
|
+
* The board answers a different question from the project table. The table says
|
|
5
|
+
* "which project needs attention"; the board says "what could go wrong, and
|
|
6
|
+
* where". It is built from the same records, so the two can never disagree.
|
|
7
|
+
*
|
|
8
|
+
* Severity ordering is deliberate: a `high` risk sorts above every `medium`,
|
|
9
|
+
* and a blank level sorts LAST rather than first. An unset level is not a
|
|
10
|
+
* statement that the risk is mild — it is a missing judgement, and putting it
|
|
11
|
+
* at the top would bury the risks someone actually assessed as serious.
|
|
12
|
+
*/
|
|
13
|
+
import { normalizeStage, scanGaps, text } from './gap.js';
|
|
14
|
+
/** Severity rank: higher sorts first. A blank level ranks below `low`. */
|
|
15
|
+
export function riskRank(level) {
|
|
16
|
+
switch (level.toLowerCase()) {
|
|
17
|
+
case 'high':
|
|
18
|
+
return 3;
|
|
19
|
+
case 'medium':
|
|
20
|
+
return 2;
|
|
21
|
+
case 'low':
|
|
22
|
+
return 1;
|
|
23
|
+
default:
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Count the gaps a project carries, for board context.
|
|
29
|
+
*
|
|
30
|
+
* Delegates to the shared scanner so the board and the pending list can never
|
|
31
|
+
* disagree about a project's gaps.
|
|
32
|
+
*/
|
|
33
|
+
function gapCounts(record, options) {
|
|
34
|
+
const gaps = scanGaps(record, options);
|
|
35
|
+
return { total: gaps.length, high: gaps.filter((item) => item.level === 'high').length };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Build the board from the loaded records.
|
|
39
|
+
*
|
|
40
|
+
* Rows are ordered by severity, then by project id, then by the risk's own
|
|
41
|
+
* order inside the record — a stable order that does not reshuffle between
|
|
42
|
+
* refreshes.
|
|
43
|
+
*/
|
|
44
|
+
export function buildRiskBoard(records, options) {
|
|
45
|
+
const rows = [];
|
|
46
|
+
let projectsWithRisks = 0;
|
|
47
|
+
for (const record of records) {
|
|
48
|
+
const pocId = text(record.poc_id);
|
|
49
|
+
const pocName = text(record.name) === '' ? pocId : text(record.name);
|
|
50
|
+
const { stage } = normalizeStage(record.stage);
|
|
51
|
+
const risks = Array.isArray(record.plan?.risks) ? record.plan.risks : [];
|
|
52
|
+
if (risks.length === 0)
|
|
53
|
+
continue;
|
|
54
|
+
projectsWithRisks += 1;
|
|
55
|
+
const counts = gapCounts(record, options);
|
|
56
|
+
for (const raw of risks) {
|
|
57
|
+
const value = (raw ?? {});
|
|
58
|
+
const level = text(value.level).toLowerCase();
|
|
59
|
+
rows.push({
|
|
60
|
+
pocId,
|
|
61
|
+
pocName,
|
|
62
|
+
stage,
|
|
63
|
+
level,
|
|
64
|
+
rank: riskRank(level),
|
|
65
|
+
name: text(value.name),
|
|
66
|
+
mitigation: text(value.mitigation),
|
|
67
|
+
gapCount: counts.total,
|
|
68
|
+
highGapCount: counts.high,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
rows.sort((left, right) => right.rank - left.rank || left.pocId.localeCompare(right.pocId) || left.name.localeCompare(right.name));
|
|
73
|
+
const counts = { high: 0, medium: 0, low: 0, unset: 0, total: rows.length };
|
|
74
|
+
for (const row of rows) {
|
|
75
|
+
if (row.level === 'high')
|
|
76
|
+
counts.high += 1;
|
|
77
|
+
else if (row.level === 'medium')
|
|
78
|
+
counts.medium += 1;
|
|
79
|
+
else if (row.level === 'low')
|
|
80
|
+
counts.low += 1;
|
|
81
|
+
else
|
|
82
|
+
counts.unset += 1;
|
|
83
|
+
}
|
|
84
|
+
return { rows, counts, projectsWithRisks, projectsTotal: records.length };
|
|
85
|
+
}
|