@venizia/vulcan 0.4.9-4 → 0.4.9-5
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/base/ci-global.json +13 -0
- package/base/claude/events.json +60 -0
- package/base/hooks/event.ts +37 -0
- package/base/rules.md +1 -1
- package/base/templates/ci/github-actions.yml +75 -0
- package/base/templates/ci/gitlab-ci.yml +52 -0
- package/package.json +3 -3
- package/src/ci/affected.ts +176 -0
- package/src/ci/command.ts +166 -0
- package/src/ci/common/constants.ts +76 -0
- package/src/ci/common/index.ts +2 -0
- package/src/ci/common/types.ts +37 -0
- package/src/ci/git.ts +72 -0
- package/src/ci/global.ts +48 -0
- package/src/ci/graph.ts +43 -0
- package/src/ci/index.ts +7 -0
- package/src/ci/workspace.ts +119 -0
- package/src/commands/ci.command.ts +4 -0
- package/src/commands/clean.command.ts +3 -5
- package/src/commands/event.command.ts +5 -0
- package/src/commands/help.command.ts +10 -2
- package/src/commands/index.ts +3 -0
- package/src/common/constants.ts +10 -0
- package/src/config/common/constants.ts +1 -0
- package/src/config/schema.ts +49 -0
- package/src/doctor/checks.ts +81 -7
- package/src/doctor/common/constants.ts +1 -0
- package/src/doctor/runner.ts +2 -0
- package/src/events/actor.ts +116 -0
- package/src/events/command.ts +191 -0
- package/src/events/common/constants.ts +118 -0
- package/src/events/common/index.ts +2 -0
- package/src/events/common/types.ts +39 -0
- package/src/events/flow.ts +92 -0
- package/src/events/hook.ts +163 -0
- package/src/events/index.ts +8 -0
- package/src/events/log.ts +162 -0
- package/src/events/settings.ts +80 -0
- package/src/events/text.ts +49 -0
- package/src/local/constants.ts +36 -1
- package/src/local/scan.ts +256 -27
- package/src/manifest/render.ts +18 -0
- package/src/router.ts +24 -0
- package/src/sync/common/constants.ts +2 -0
- package/src/sync/local-choice.ts +3 -3
- package/src/sync/planner.ts +10 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { RecordValue } from '../common/record.ts';
|
|
5
|
+
import { EventConstants } from './common/index.ts';
|
|
6
|
+
import { EventText } from './text.ts';
|
|
7
|
+
|
|
8
|
+
const SESSION_FILE = /^[\w.-]+\.json$/;
|
|
9
|
+
const SESSION_ID_SHORT = 8;
|
|
10
|
+
/** A person has a handful of sessions; a folder with more is not the registry this expects. */
|
|
11
|
+
const MAX_SESSION_FILES = 500;
|
|
12
|
+
|
|
13
|
+
/** Who is writing: the session's name, the way other sessions address it. */
|
|
14
|
+
export class EventActor {
|
|
15
|
+
/**
|
|
16
|
+
* In order: `VULCAN_ACTOR` from the environment; the name Claude Code gave the session in its own
|
|
17
|
+
* session registry (`<config dir>/sessions/<pid>.json`, matched by session id); `claude:` and the
|
|
18
|
+
* start of the session id; `unknown`. The registry is not a documented interface, so a read that fails
|
|
19
|
+
* or finds no match falls through to the next answer.
|
|
20
|
+
*/
|
|
21
|
+
static resolve(opts: { sessionId?: string; env: Record<string, string | undefined> }): string {
|
|
22
|
+
const fromEnv = EventActor.clean({ text: opts.env[EventConstants.ACTOR_ENV] ?? '' });
|
|
23
|
+
|
|
24
|
+
if (fromEnv !== '') {
|
|
25
|
+
return fromEnv;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { sessionId } = opts;
|
|
29
|
+
|
|
30
|
+
if (sessionId === undefined || sessionId === '') {
|
|
31
|
+
return EventConstants.UNKNOWN_ACTOR;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const named = EventActor.registryName({ sessionId, env: opts.env });
|
|
35
|
+
|
|
36
|
+
return named ?? `claude:${sessionId.slice(0, SESSION_ID_SHORT)}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static clean(opts: { text: string }): string {
|
|
40
|
+
return EventText.oneLine({ text: opts.text, max: EventConstants.ACTOR_MAX });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private static registryName(opts: {
|
|
44
|
+
sessionId: string;
|
|
45
|
+
env: Record<string, string | undefined>;
|
|
46
|
+
}): string | undefined {
|
|
47
|
+
const config = opts.env[EventConstants.CONFIG_DIR_ENV] ?? join(homedir(), '.claude');
|
|
48
|
+
const directory = join(config, 'sessions');
|
|
49
|
+
|
|
50
|
+
let files: string[];
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
files = readdirSync(directory)
|
|
54
|
+
.filter((name) => SESSION_FILE.test(name))
|
|
55
|
+
.slice(0, MAX_SESSION_FILES);
|
|
56
|
+
} catch {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const matches = files.flatMap(
|
|
61
|
+
(file) =>
|
|
62
|
+
EventActor.candidateIn({ file: join(directory, file), sessionId: opts.sessionId }) ?? [],
|
|
63
|
+
);
|
|
64
|
+
// Several files can hold one session id (a resumed session leaves its old file): a live process wins, then the newest.
|
|
65
|
+
const live = matches.filter((match) => match.alive);
|
|
66
|
+
const best = [...(live.length > 0 ? live : matches)].sort(
|
|
67
|
+
(left, right) => right.updatedAt - left.updatedAt,
|
|
68
|
+
)[0];
|
|
69
|
+
|
|
70
|
+
return best?.name;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The name, the process and the time of one registry file when it is the session's; a file that cannot be read is no match. */
|
|
74
|
+
private static candidateIn(opts: {
|
|
75
|
+
file: string;
|
|
76
|
+
sessionId: string;
|
|
77
|
+
}): { name: string; alive: boolean; updatedAt: number }[] | undefined {
|
|
78
|
+
try {
|
|
79
|
+
const entry: unknown = JSON.parse(readFileSync(opts.file, 'utf8'));
|
|
80
|
+
|
|
81
|
+
if (!RecordValue.isRecord(entry) || entry.sessionId !== opts.sessionId) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const name = typeof entry.name === 'string' ? EventActor.clean({ text: entry.name }) : '';
|
|
86
|
+
|
|
87
|
+
if (name === '') {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return [
|
|
92
|
+
{
|
|
93
|
+
name,
|
|
94
|
+
alive: typeof entry.pid === 'number' && EventActor.isAlive({ pid: entry.pid }),
|
|
95
|
+
updatedAt:
|
|
96
|
+
typeof entry.updatedAt === 'number'
|
|
97
|
+
? entry.updatedAt
|
|
98
|
+
: Number(statSync(opts.file).mtimeMs),
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
} catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Whether a process with this id exists (signal 0 sends nothing); a process of another user exists too. */
|
|
107
|
+
private static isAlive(opts: { pid: number }): boolean {
|
|
108
|
+
try {
|
|
109
|
+
process.kill(opts.pid, 0);
|
|
110
|
+
|
|
111
|
+
return true;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
return error instanceof Error && 'code' in error && error.code === 'EPERM';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { existsSync, statSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { ExitCodes } from '../common/index.ts';
|
|
4
|
+
import type { ICommandContext } from '../common/index.ts';
|
|
5
|
+
import {
|
|
6
|
+
EventConstants,
|
|
7
|
+
EventFlags,
|
|
8
|
+
EventKinds,
|
|
9
|
+
EventSources,
|
|
10
|
+
EventSubcommands,
|
|
11
|
+
} from './common/index.ts';
|
|
12
|
+
import type { IEvent, TEventKind } from './common/index.ts';
|
|
13
|
+
import { EventActor } from './actor.ts';
|
|
14
|
+
import { EventFlow } from './flow.ts';
|
|
15
|
+
import { EventLog } from './log.ts';
|
|
16
|
+
import { EventSettings } from './settings.ts';
|
|
17
|
+
|
|
18
|
+
const USAGE = `usage: vulcan event add ${EventFlags.KIND} <kind> ${EventFlags.SUMMARY} <text> [${EventFlags.FLOW} F-n] [${EventFlags.REF} <ref>] [${EventFlags.ACTOR} <name>] [${EventFlags.DIR} <folder>]
|
|
19
|
+
vulcan event tail [${EventFlags.SINCE} <time>] [${EventFlags.JSON}] [${EventFlags.DIR} <folder>]
|
|
20
|
+
kinds: ${[...EventKinds.SCHEME_SET].join(', ')}`;
|
|
21
|
+
|
|
22
|
+
type TFlagValues = Record<string, string>;
|
|
23
|
+
|
|
24
|
+
/** `vulcan event`: write a line to the agent event log, or read the log. */
|
|
25
|
+
export class EventCommand {
|
|
26
|
+
static async run(opts: ICommandContext): Promise<number> {
|
|
27
|
+
const [subcommand, ...rest] = opts.args;
|
|
28
|
+
const parsed = EventCommand.parse({ args: rest });
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
parsed === undefined ||
|
|
32
|
+
(subcommand !== EventSubcommands.ADD && subcommand !== EventSubcommands.TAIL)
|
|
33
|
+
) {
|
|
34
|
+
process.stderr.write(`event: ${USAGE}\n`);
|
|
35
|
+
|
|
36
|
+
return ExitCodes.USAGE;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return subcommand === EventSubcommands.ADD
|
|
40
|
+
? EventCommand.add({ cwd: opts.cwd, values: parsed.values })
|
|
41
|
+
: EventCommand.tail({ cwd: opts.cwd, values: parsed.values, json: parsed.json });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Flags with a value, and `--json`; `undefined` for anything else (the router has already refused unknown flags). */
|
|
45
|
+
private static parse(opts: {
|
|
46
|
+
args: string[];
|
|
47
|
+
}): { values: TFlagValues; json: boolean } | undefined {
|
|
48
|
+
const values: TFlagValues = {};
|
|
49
|
+
let json = false;
|
|
50
|
+
|
|
51
|
+
for (let index = 0; index < opts.args.length; index++) {
|
|
52
|
+
const arg = opts.args[index];
|
|
53
|
+
|
|
54
|
+
if (arg === EventFlags.JSON) {
|
|
55
|
+
json = true;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const value = opts.args[index + 1];
|
|
60
|
+
|
|
61
|
+
if (!arg.startsWith('--') || value === undefined || Object.hasOwn(values, arg)) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
values[arg] = value;
|
|
66
|
+
index++;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return { values, json };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The folder to use: `--dir`, else the manifest's `events.dir`, else the project's own. */
|
|
73
|
+
private static dirOf(opts: { cwd: string; values: TFlagValues }): string {
|
|
74
|
+
return EventSettings.dirOf({
|
|
75
|
+
root: EventSettings.rootOf({ cwd: opts.cwd }),
|
|
76
|
+
...(opts.values[EventFlags.DIR] === undefined
|
|
77
|
+
? {}
|
|
78
|
+
: { dir: resolve(opts.cwd, opts.values[EventFlags.DIR]) }),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private static add(opts: { cwd: string; values: TFlagValues }): number {
|
|
83
|
+
const { values } = opts;
|
|
84
|
+
const kind = values[EventFlags.KIND];
|
|
85
|
+
const summary = values[EventFlags.SUMMARY];
|
|
86
|
+
const flow = values[EventFlags.FLOW];
|
|
87
|
+
|
|
88
|
+
if (kind === undefined || !EventKinds.isValid(kind)) {
|
|
89
|
+
process.stderr.write(
|
|
90
|
+
`event: ${EventFlags.KIND} must be one of ${[...EventKinds.SCHEME_SET].join(', ')}\n`,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
return ExitCodes.USAGE;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (summary === undefined || summary.trim() === '') {
|
|
97
|
+
process.stderr.write(`event: ${EventFlags.SUMMARY} needs a short text\n`);
|
|
98
|
+
|
|
99
|
+
return ExitCodes.USAGE;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (flow !== undefined && !EventFlow.isValid({ flow })) {
|
|
103
|
+
process.stderr.write(`event: ${EventFlags.FLOW} must look like F-12, got '${flow}'\n`);
|
|
104
|
+
|
|
105
|
+
return ExitCodes.USAGE;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const flowId = flow ?? EventFlow.fromBranch({ cwd: opts.cwd });
|
|
109
|
+
const actor =
|
|
110
|
+
values[EventFlags.ACTOR] === undefined
|
|
111
|
+
? EventActor.resolve({
|
|
112
|
+
sessionId: process.env[EventConstants.SESSION_ENV],
|
|
113
|
+
env: process.env,
|
|
114
|
+
})
|
|
115
|
+
: EventActor.clean({ text: values[EventFlags.ACTOR] });
|
|
116
|
+
const event: IEvent = EventLog.build({
|
|
117
|
+
kind: kind as TEventKind,
|
|
118
|
+
summary,
|
|
119
|
+
actor: actor === '' ? EventConstants.UNKNOWN_ACTOR : actor,
|
|
120
|
+
source: EventSources.CLI,
|
|
121
|
+
...(flowId === undefined ? {} : { flowId }),
|
|
122
|
+
...(values[EventFlags.REF] === undefined ? {} : { ref: values[EventFlags.REF] }),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
EventLog.append({ dir: EventCommand.dirOf(opts), event });
|
|
127
|
+
} catch (error) {
|
|
128
|
+
process.stderr.write(
|
|
129
|
+
`event: cannot write the log in ${EventCommand.dirOf(opts)}: ${error instanceof Error ? error.message : String(error)}\n`,
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
return ExitCodes.FAILURE;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
process.stdout.write(`${event.id}\n`);
|
|
136
|
+
|
|
137
|
+
return ExitCodes.OK;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private static tail(opts: { cwd: string; values: TFlagValues; json: boolean }): number {
|
|
141
|
+
const { values } = opts;
|
|
142
|
+
const since = EventCommand.sinceOf({ value: values[EventFlags.SINCE] });
|
|
143
|
+
|
|
144
|
+
if (since === undefined) {
|
|
145
|
+
process.stderr.write(
|
|
146
|
+
`event: ${EventFlags.SINCE} needs a time such as 2026-11-03 or 2026-11-03T09:00:00Z\n`,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
return ExitCodes.USAGE;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const dir = EventCommand.dirOf(opts);
|
|
153
|
+
|
|
154
|
+
// A folder named on purpose that is not there is a typo; the default folder does not exist until the first write.
|
|
155
|
+
if (
|
|
156
|
+
values[EventFlags.DIR] !== undefined &&
|
|
157
|
+
(!existsSync(dir) || !statSync(dir).isDirectory())
|
|
158
|
+
) {
|
|
159
|
+
process.stderr.write(`event: ${dir} is not a folder\n`);
|
|
160
|
+
|
|
161
|
+
return ExitCodes.USAGE;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const { events, skipped } = EventLog.read({ dir, since });
|
|
165
|
+
|
|
166
|
+
for (const event of events) {
|
|
167
|
+
process.stdout.write(
|
|
168
|
+
opts.json
|
|
169
|
+
? `${JSON.stringify(event)}\n`
|
|
170
|
+
: `${event.ts} ${event.kind.padEnd(13)} ${event.actor} ${event.flowId ?? '-'} ${event.summary}\n`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (skipped > 0) {
|
|
175
|
+
process.stderr.write(`event: skipped ${skipped} line(s) that are not events\n`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return ExitCodes.OK;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The time to read from: the value, or 24 hours ago; `undefined` when the value is not a time. */
|
|
182
|
+
private static sinceOf(opts: { value: string | undefined }): number | undefined {
|
|
183
|
+
if (opts.value === undefined) {
|
|
184
|
+
return Date.now() - EventConstants.DAY_MS;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const time = Date.parse(opts.value);
|
|
188
|
+
|
|
189
|
+
return Number.isFinite(time) ? time : undefined;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/** What happened. A board reads these; add one only when a reader needs to tell it apart. */
|
|
2
|
+
export class EventKinds {
|
|
3
|
+
static readonly SESSION_START = 'session-start';
|
|
4
|
+
static readonly SESSION_STOP = 'session-stop';
|
|
5
|
+
static readonly MSG_SENT = 'msg-sent';
|
|
6
|
+
static readonly MSG_RECEIVED = 'msg-received';
|
|
7
|
+
static readonly NEEDS_OWNER = 'needs-owner';
|
|
8
|
+
static readonly SUBAGENT_STOP = 'subagent-stop';
|
|
9
|
+
static readonly COMMIT = 'commit';
|
|
10
|
+
static readonly PR = 'pr';
|
|
11
|
+
|
|
12
|
+
static readonly SCHEME_SET = new Set<string>([
|
|
13
|
+
this.SESSION_START,
|
|
14
|
+
this.SESSION_STOP,
|
|
15
|
+
this.MSG_SENT,
|
|
16
|
+
this.MSG_RECEIVED,
|
|
17
|
+
this.NEEDS_OWNER,
|
|
18
|
+
this.SUBAGENT_STOP,
|
|
19
|
+
this.COMMIT,
|
|
20
|
+
this.PR,
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
static isValid(value: string): boolean {
|
|
24
|
+
return this.SCHEME_SET.has(value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Who wrote the line: the hook installed for Claude Code, or `vulcan event add`. */
|
|
29
|
+
export class EventSources {
|
|
30
|
+
static readonly CLAUDE_HOOK = 'claude-hook';
|
|
31
|
+
static readonly CLI = 'cli';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class EventSubcommands {
|
|
35
|
+
static readonly ADD = 'add';
|
|
36
|
+
static readonly TAIL = 'tail';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class EventFlags {
|
|
40
|
+
static readonly KIND = '--kind';
|
|
41
|
+
static readonly SUMMARY = '--summary';
|
|
42
|
+
static readonly FLOW = '--flow';
|
|
43
|
+
static readonly REF = '--ref';
|
|
44
|
+
static readonly ACTOR = '--actor';
|
|
45
|
+
static readonly DIR = '--dir';
|
|
46
|
+
static readonly SINCE = '--since';
|
|
47
|
+
static readonly JSON = '--json';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The names a Claude Code hook payload carries, as Claude Code 2.1.317 sends them. */
|
|
51
|
+
export class ClaudeHookEvents {
|
|
52
|
+
static readonly SESSION_START = 'SessionStart';
|
|
53
|
+
static readonly STOP = 'Stop';
|
|
54
|
+
static readonly SUBAGENT_STOP = 'SubagentStop';
|
|
55
|
+
static readonly NOTIFICATION = 'Notification';
|
|
56
|
+
static readonly POST_TOOL_USE = 'PostToolUse';
|
|
57
|
+
/** The cross-session tool whose calls are logged as `msg-sent`. */
|
|
58
|
+
static readonly SEND_MESSAGE_TOOL = 'SendMessage';
|
|
59
|
+
/** The notification types that mean an agent waits for a person; `auth_success` does not. */
|
|
60
|
+
static readonly OWNER_NOTIFICATIONS = new Set<string>([
|
|
61
|
+
'permission_prompt',
|
|
62
|
+
'idle_prompt',
|
|
63
|
+
'elicitation_dialog',
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class EventConstants {
|
|
68
|
+
/** Relative to the project root; the default place of the log. */
|
|
69
|
+
static readonly DEFAULT_DIRECTORY = '.agents/local/events';
|
|
70
|
+
static readonly MANIFEST_FILE = '.agents/vulcan.manifest.json';
|
|
71
|
+
static readonly FILE_SUFFIX = '.jsonl';
|
|
72
|
+
static readonly SUMMARY_MAX = 200;
|
|
73
|
+
/** What a sent message is called when its sender gave no label: the text of a message is never used. */
|
|
74
|
+
static readonly MESSAGE_SENT_LABEL = 'message sent';
|
|
75
|
+
static readonly REF_MAX = 120;
|
|
76
|
+
static readonly ACTOR_MAX = 80;
|
|
77
|
+
static readonly REDACTED = '[redacted]';
|
|
78
|
+
static readonly FLOW_PATTERN = /\bF-\d+\b/;
|
|
79
|
+
static readonly FLOW_EXACT = /^F-\d+$/;
|
|
80
|
+
/** `Flow: F-n` in a message's first line. */
|
|
81
|
+
static readonly FLOW_LINE = /\bFlow:\s*(F-\d+)\b/;
|
|
82
|
+
/** Where the log goes for a hook that has no better answer: the name of an actor with no session name. */
|
|
83
|
+
static readonly UNKNOWN_ACTOR = 'unknown';
|
|
84
|
+
static readonly ACTOR_ENV = 'VULCAN_ACTOR';
|
|
85
|
+
static readonly SESSION_ENV = 'CLAUDE_CODE_SESSION_ID';
|
|
86
|
+
static readonly CONFIG_DIR_ENV = 'CLAUDE_CONFIG_DIR';
|
|
87
|
+
static readonly DAY_MS = 24 * 60 * 60 * 1000;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Shapes of a secret that must never reach a log line. Best effort: a summary is a label, not a document. */
|
|
91
|
+
export class EventSecrets {
|
|
92
|
+
static readonly PATTERNS: RegExp[] = [
|
|
93
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----.*/g,
|
|
94
|
+
/\b(?:ghp|gho|ghs|ghu|ghr)_[A-Za-z0-9]{16,}/g,
|
|
95
|
+
/\bgithub_pat_[A-Za-z0-9_]{16,}/g,
|
|
96
|
+
/\bnpm_[A-Za-z0-9]{16,}/g,
|
|
97
|
+
/\bsk-[A-Za-z0-9_-]{16,}/g,
|
|
98
|
+
/\bAKIA[0-9A-Z]{16}\b/g,
|
|
99
|
+
/\bAIza[0-9A-Za-z_-]{35}/g,
|
|
100
|
+
/\bglpat-[A-Za-z0-9_-]{16,}/g,
|
|
101
|
+
/\b[sr]k_(?:live|test)_[A-Za-z0-9]{8,}/g,
|
|
102
|
+
/\beyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]*/g,
|
|
103
|
+
// A Basic credential is base64 of user:password: it has a digit, a + or a /, a capital inside it, or ends in =; a plain word after "Basic" has none of these.
|
|
104
|
+
/\bBasic\s+(?:(?=[A-Za-z0-9+/]*(?:[0-9+/]|[a-z][A-Z]))[A-Za-z0-9+/]{8,}={0,2}|[A-Za-z0-9+/]{8,}={1,2})/g,
|
|
105
|
+
/\bxox[abprs]-[A-Za-z0-9-]{10,}/g,
|
|
106
|
+
/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/gi,
|
|
107
|
+
/\b[0-9a-fA-F]{64,}\b/g,
|
|
108
|
+
/\b(?=[A-Za-z0-9+]*\d)(?=[A-Za-z0-9+]*[a-z])(?=[A-Za-z0-9+]*[A-Z])[A-Za-z0-9+]{40,}={0,2}/g,
|
|
109
|
+
];
|
|
110
|
+
/** `user:password@host` inside a URL. */
|
|
111
|
+
static readonly URL_USERINFO = /(\/\/)[^\s/@:]+:[^\s/@]+@/g;
|
|
112
|
+
/** `name=value`, `name: value` and `"name": "value"` where the name says it is a secret. */
|
|
113
|
+
static readonly ASSIGNMENT =
|
|
114
|
+
/["']?\b([A-Za-z0-9_.-]*(?:key|token|secret|password|passwd|pwd|credential)[A-Za-z0-9_.-]*)["']?\s*[:=]\s*("[^"]*"|'[^']*'|\S+)/gi;
|
|
115
|
+
/** "the password is hunter2". */
|
|
116
|
+
static readonly SPOKEN =
|
|
117
|
+
/\b((?:password|passwd|passphrase|secret|token|api[ _-]?key)s?)\s+(?:is|are|was|were)\s+("[^"]*"|'[^']*'|\S+)/gi;
|
|
118
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { TConstValue } from '../../common/index.ts';
|
|
2
|
+
import type { EventKinds } from './constants.ts';
|
|
3
|
+
|
|
4
|
+
export type TEventKind = TConstValue<typeof EventKinds>;
|
|
5
|
+
|
|
6
|
+
/** One line of the log. Field order is the order on disk. */
|
|
7
|
+
export interface IEvent {
|
|
8
|
+
id: string;
|
|
9
|
+
/** ISO 8601, UTC. */
|
|
10
|
+
ts: string;
|
|
11
|
+
flowId?: string;
|
|
12
|
+
actor: string;
|
|
13
|
+
kind: TEventKind;
|
|
14
|
+
summary: string;
|
|
15
|
+
ref?: string;
|
|
16
|
+
source: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The manifest's `events` block, as `vulcan sync` writes it. */
|
|
20
|
+
export interface IEventSettings {
|
|
21
|
+
log: boolean;
|
|
22
|
+
/** An absolute folder; the log goes here instead of `.agents/local/events` of the project. */
|
|
23
|
+
dir?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** What a hook payload says happened, before the actor, the flow and the id are added. */
|
|
27
|
+
export interface IHookEvent {
|
|
28
|
+
kind: TEventKind;
|
|
29
|
+
summary: string;
|
|
30
|
+
ref?: string;
|
|
31
|
+
/** Text a flow id may be read from: the first line of a message. */
|
|
32
|
+
flowText?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface IEventRead {
|
|
36
|
+
events: IEvent[];
|
|
37
|
+
/** Lines that were not an event; a reader reports the count and goes on. */
|
|
38
|
+
skipped: number;
|
|
39
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
3
|
+
import { EventConstants } from './common/index.ts';
|
|
4
|
+
|
|
5
|
+
const GITDIR_LINE = /^gitdir:\s*(.+)$/m;
|
|
6
|
+
const HEAD_REF = /^ref:\s*refs\/heads\/(.+?)\s*$/m;
|
|
7
|
+
const BRANCH_FLOW = /(?:^|\/)(F-\d+)(?=-|\/|$)/;
|
|
8
|
+
/** Enough parents for a deep folder inside a project; the walk stops at the filesystem root anyway. */
|
|
9
|
+
const MAX_PARENTS = 40;
|
|
10
|
+
|
|
11
|
+
/** The flow a line of the log belongs to: `F-n`, from the writer, the first line of a message or the branch. */
|
|
12
|
+
export class EventFlow {
|
|
13
|
+
static isValid(opts: { flow: string }): boolean {
|
|
14
|
+
return EventConstants.FLOW_EXACT.test(opts.flow);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `Flow: F-n` in the first line of `text`. */
|
|
18
|
+
static fromText(opts: { text: string }): string | undefined {
|
|
19
|
+
const first = opts.text.split(/\r\n|\r|\n/).find((line) => line.trim() !== '') ?? '';
|
|
20
|
+
|
|
21
|
+
return EventConstants.FLOW_LINE.exec(first)?.[1];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `F-n` as a whole segment of a branch name: `feature/F-12-events-log`. */
|
|
25
|
+
static fromBranchName(opts: { branch: string }): string | undefined {
|
|
26
|
+
return BRANCH_FLOW.exec(opts.branch)?.[1];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The flow in the branch checked out at `cwd`. It reads `HEAD` itself, in the folder or in the folder a
|
|
31
|
+
* linked worktree points to, because a hook cannot afford to start git. `undefined` when it cannot tell.
|
|
32
|
+
*/
|
|
33
|
+
static fromBranch(opts: { cwd: string }): string | undefined {
|
|
34
|
+
const branch = EventFlow.branchOf({ cwd: opts.cwd });
|
|
35
|
+
|
|
36
|
+
return branch === undefined ? undefined : EventFlow.fromBranchName({ branch });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private static branchOf(opts: { cwd: string }): string | undefined {
|
|
40
|
+
let directory = resolve(opts.cwd);
|
|
41
|
+
|
|
42
|
+
for (let step = 0; step < MAX_PARENTS; step++) {
|
|
43
|
+
const gitdir = EventFlow.gitDirOf({ directory });
|
|
44
|
+
|
|
45
|
+
if (gitdir !== undefined) {
|
|
46
|
+
return EventFlow.headOf({ gitdir });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const parent = dirname(directory);
|
|
50
|
+
|
|
51
|
+
if (parent === directory) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
directory = parent;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private static gitDirOf(opts: { directory: string }): string | undefined {
|
|
62
|
+
const path = join(opts.directory, '.git');
|
|
63
|
+
|
|
64
|
+
if (!existsSync(path)) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
if (statSync(path).isDirectory()) {
|
|
70
|
+
return path;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const target = GITDIR_LINE.exec(readFileSync(path, 'utf8'))?.[1]?.trim();
|
|
74
|
+
|
|
75
|
+
return target === undefined
|
|
76
|
+
? undefined
|
|
77
|
+
: isAbsolute(target)
|
|
78
|
+
? target
|
|
79
|
+
: resolve(opts.directory, target);
|
|
80
|
+
} catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private static headOf(opts: { gitdir: string }): string | undefined {
|
|
86
|
+
try {
|
|
87
|
+
return HEAD_REF.exec(readFileSync(join(opts.gitdir, 'HEAD'), 'utf8'))?.[1];
|
|
88
|
+
} catch {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|