@sanqianx/ai-coding-event-bridge-console 0.5.1 → 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.
Files changed (34) hide show
  1. package/package.json +2 -5
  2. package/src/agent-detect.js +1 -1
  3. package/src/bin.js +23 -4
  4. package/src/bridge/bin/commit-boundary.js +64 -0
  5. package/src/bridge/connectors/capture-guard.js +24 -0
  6. package/src/bridge/connectors/claude-code/hook-entry.js +45 -0
  7. package/src/bridge/connectors/claude-code/notify.js +57 -0
  8. package/src/bridge/connectors/codex/cursor-store.js +83 -0
  9. package/src/bridge/connectors/codex/hook-entry.js +215 -0
  10. package/src/bridge/connectors/codex/session-parser.js +100 -0
  11. package/src/bridge/connectors/opencode/hook-entry.js +55 -0
  12. package/src/bridge/core/bridge.js +205 -0
  13. package/src/bridge/core/commit-projection.js +288 -0
  14. package/src/bridge/core/compaction.js +23 -0
  15. package/src/bridge/core/consumer-registry.js +101 -0
  16. package/src/bridge/core/event-schema.js +127 -0
  17. package/src/bridge/core/fs-utils.js +81 -0
  18. package/src/bridge/core/journal-router.js +39 -0
  19. package/src/bridge/core/journal.js +507 -0
  20. package/src/bridge/core/lock.js +94 -0
  21. package/src/bridge/core/normalizer.js +114 -0
  22. package/src/bridge/core/paths.js +12 -0
  23. package/src/bridge/core/project-registry.js +176 -0
  24. package/src/bridge/core/repo-context.js +188 -0
  25. package/src/bridge/core/runtime-home.js +188 -0
  26. package/src/bridge/core/semver.js +23 -0
  27. package/src/bridge/core/turn-identity.js +127 -0
  28. package/src/bridge/index.js +86 -0
  29. package/src/bridge/installers/claude-code/installer.js +181 -0
  30. package/src/bridge/installers/codex/installer.js +114 -0
  31. package/src/bridge/installers/opencode/installer.js +96 -0
  32. package/src/bridge/installers/zcode/installer.js +188 -0
  33. package/src/bridge/query/conversation-query.js +194 -0
  34. package/src/server.js +2 -2
@@ -0,0 +1,127 @@
1
+ 'use strict';
2
+
3
+ const { isValidRepoIdentityV1 } = require('./repo-context');
4
+
5
+ const SOURCES = ['claude-code', 'codex', 'opencode'];
6
+ const IDENTITY_CONFIDENCE_LEVELS = ['exact', 'partial', 'unavailable'];
7
+ const CAPTURE_STATUSES = ['complete', 'partial', 'gap'];
8
+ const ROLES = ['user', 'assistant'];
9
+
10
+ // AUD-ID-001: a shared fake session id aggregates unrelated events.
11
+ const FORBIDDEN_SESSION_IDS = new Set(['', 'unknown-session', 'unknown', 'null', 'none']);
12
+
13
+ class EventSchemaError extends Error {
14
+ constructor(message, field) {
15
+ super(message);
16
+ this.name = 'EventSchemaError';
17
+ this.field = field;
18
+ }
19
+ }
20
+
21
+ function evidenceLevelFor({ sessionId, turnId }) {
22
+ if (sessionId && turnId) return 'exact';
23
+ if (sessionId || turnId) return 'partial';
24
+ return 'unavailable';
25
+ }
26
+
27
+ const CONFIDENCE_RANK = { unavailable: 0, partial: 1, exact: 2 };
28
+
29
+ function clampConfidence(requested, evidence) {
30
+ return CONFIDENCE_RANK[requested] <= CONFIDENCE_RANK[evidence] ? requested : evidence;
31
+ }
32
+
33
+ function raiseConfidenceToEvidence(current, evidence) {
34
+ return CONFIDENCE_RANK[evidence] > CONFIDENCE_RANK[current] ? evidence : current;
35
+ }
36
+
37
+ function validateAndNormalizeEvent(input) {
38
+ if (!input || typeof input !== 'object') {
39
+ throw new EventSchemaError('event must be an object', 'event');
40
+ }
41
+ if (!SOURCES.includes(input.source)) {
42
+ throw new EventSchemaError(`source must be one of ${SOURCES.join(', ')}`, 'source');
43
+ }
44
+ if (typeof input.eventType !== 'string' || !input.eventType) {
45
+ throw new EventSchemaError('eventType is required', 'eventType');
46
+ }
47
+ if (input.role !== undefined && input.role !== null && !ROLES.includes(input.role)) {
48
+ throw new EventSchemaError('role must be user, assistant or null', 'role');
49
+ }
50
+ if (input.content !== undefined && input.content !== null && typeof input.content !== 'string') {
51
+ throw new EventSchemaError('content must be a string or null', 'content');
52
+ }
53
+
54
+ let sessionId = input.sessionId === undefined ? null : input.sessionId;
55
+ if (sessionId !== null) {
56
+ if (typeof sessionId !== 'string') {
57
+ throw new EventSchemaError('sessionId must be a string or null', 'sessionId');
58
+ }
59
+ if (FORBIDDEN_SESSION_IDS.has(sessionId.toLowerCase())) {
60
+ throw new EventSchemaError('fake session ids such as unknown-session are forbidden', 'sessionId');
61
+ }
62
+ }
63
+ let turnId = input.turnId === undefined ? null : input.turnId;
64
+ if (turnId !== null && typeof turnId !== 'string') {
65
+ throw new EventSchemaError('turnId must be a string or null', 'turnId');
66
+ }
67
+
68
+ if (
69
+ input.identityConfidence !== undefined &&
70
+ !IDENTITY_CONFIDENCE_LEVELS.includes(input.identityConfidence)
71
+ ) {
72
+ throw new EventSchemaError('identityConfidence must be exact, partial or unavailable', 'identityConfidence');
73
+ }
74
+ if (input.repoIdentity !== undefined && input.repoIdentity !== null) {
75
+ // New automatic data must use repo-identity/v1 objects from
76
+ // resolveRepoContext. Plain strings are legacy records only; malformed
77
+ // objects are rejected so a guessed identity can never enter the journal.
78
+ if (typeof input.repoIdentity !== 'string' && !isValidRepoIdentityV1(input.repoIdentity)) {
79
+ throw new EventSchemaError('repoIdentity must be a valid repo-identity/v1 object, a legacy string, or null', 'repoIdentity');
80
+ }
81
+ }
82
+ if (input.eventKey !== undefined && input.eventKey !== null) {
83
+ if (typeof input.eventKey !== 'string' || !input.eventKey) {
84
+ throw new EventSchemaError('eventKey must be a non-empty string when present', 'eventKey');
85
+ }
86
+ }
87
+ if (input.captureStatus !== undefined && !CAPTURE_STATUSES.includes(input.captureStatus)) {
88
+ throw new EventSchemaError('captureStatus must be complete, partial or gap', 'captureStatus');
89
+ }
90
+
91
+ // Confidence may never overstate the captured evidence.
92
+ const evidenceLevel = evidenceLevelFor({ sessionId, turnId });
93
+ const requested = input.identityConfidence || evidenceLevel;
94
+ const identityConfidence = clampConfidence(requested, evidenceLevel);
95
+
96
+ const normalized = {
97
+ source: input.source,
98
+ eventType: input.eventType,
99
+ role: input.role === undefined ? null : input.role,
100
+ sessionId,
101
+ turnId,
102
+ identityConfidence,
103
+ captureStatus: input.captureStatus || 'complete'
104
+ };
105
+ if (input.content !== undefined) normalized.content = input.content;
106
+ if (input.eventKey !== undefined) normalized.eventKey = input.eventKey;
107
+ if (input.repoIdentity !== undefined) normalized.repoIdentity = input.repoIdentity;
108
+ if (input.projectPath !== undefined) normalized.projectPath = input.projectPath;
109
+ if (input.branch !== undefined) normalized.branch = input.branch;
110
+ if (input.headAtCapture !== undefined) normalized.headAtCapture = input.headAtCapture;
111
+ if (input.capturedAt !== undefined) normalized.capturedAt = input.capturedAt;
112
+ if (input.rawEventType !== undefined) normalized.rawEventType = input.rawEventType;
113
+ if (input.meta !== undefined) normalized.meta = input.meta;
114
+ return normalized;
115
+ }
116
+
117
+ module.exports = {
118
+ validateAndNormalizeEvent,
119
+ EventSchemaError,
120
+ SOURCES,
121
+ IDENTITY_CONFIDENCE_LEVELS,
122
+ CAPTURE_STATUSES,
123
+ FORBIDDEN_SESSION_IDS,
124
+ evidenceLevelFor,
125
+ clampConfidence,
126
+ raiseConfidenceToEvidence
127
+ };
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ function fsyncDir(dir) {
7
+ try {
8
+ const fd = fs.openSync(dir, 'r');
9
+ try {
10
+ fs.fsyncSync(fd);
11
+ } finally {
12
+ fs.closeSync(fd);
13
+ }
14
+ } catch (_) {
15
+ // Directory fsync is best effort (not supported for directories on Windows).
16
+ }
17
+ }
18
+
19
+ function appendLineSync(file, line) {
20
+ const fd = fs.openSync(file, 'a');
21
+ try {
22
+ fs.writeSync(fd, line);
23
+ fs.fsyncSync(fd);
24
+ } finally {
25
+ fs.closeSync(fd);
26
+ }
27
+ }
28
+
29
+ function writeJsonAtomicSync(file, value) {
30
+ const tmp = `${file}.tmp`;
31
+ const fd = fs.openSync(tmp, 'w');
32
+ try {
33
+ fs.writeSync(fd, `${JSON.stringify(value, null, 2)}\n`);
34
+ fs.fsyncSync(fd);
35
+ } finally {
36
+ fs.closeSync(fd);
37
+ }
38
+ fs.renameSync(tmp, file);
39
+ fsyncDir(path.dirname(file));
40
+ }
41
+
42
+ function writeTextAtomicSync(file, text) {
43
+ const tmp = `${file}.tmp`;
44
+ const fd = fs.openSync(tmp, 'w');
45
+ try {
46
+ fs.writeSync(fd, text);
47
+ fs.fsyncSync(fd);
48
+ } finally {
49
+ fs.closeSync(fd);
50
+ }
51
+ fs.renameSync(tmp, file);
52
+ fsyncDir(path.dirname(file));
53
+ }
54
+
55
+ function readJsonIfExists(file) {
56
+ try {
57
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
58
+ } catch (err) {
59
+ if (err && (err.code === 'ENOENT' || err instanceof SyntaxError)) return null;
60
+ throw err;
61
+ }
62
+ }
63
+
64
+ function ensureDirSync(dir) {
65
+ fs.mkdirSync(dir, { recursive: true });
66
+ }
67
+
68
+ function copyDirSync(src, dest) {
69
+ ensureDirSync(dest);
70
+ for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
71
+ const from = path.join(src, entry.name);
72
+ const to = path.join(dest, entry.name);
73
+ if (entry.isDirectory()) {
74
+ copyDirSync(from, to);
75
+ } else if (entry.isFile()) {
76
+ fs.copyFileSync(from, to);
77
+ }
78
+ }
79
+ }
80
+
81
+ module.exports = { fsyncDir, appendLineSync, writeJsonAtomicSync, writeTextAtomicSync, readJsonIfExists, ensureDirSync, copyDirSync };
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const { Journal } = require('./journal');
5
+ const { findProject } = require('./project-registry');
6
+
7
+ /**
8
+ * Capture-side journal routing. Events for a registered project (matched by
9
+ * repoIdentity.workspaceId) are appended to that project's journal at
10
+ * `<store>/journal`; everything else — unknown repos and events with no repo
11
+ * identity — goes to the global journal at `<home>/journal`, so capture never
12
+ * depends on the registry being readable. Journal instances are cached per
13
+ * directory for the life of the process (the Codex connector appends many
14
+ * records per wake-up).
15
+ */
16
+
17
+ const journalCache = new Map();
18
+
19
+ function globalJournalDir(home) {
20
+ return path.join(home, 'journal');
21
+ }
22
+
23
+ function projectJournalDir(project) {
24
+ return path.join(project.store, 'journal');
25
+ }
26
+
27
+ function journalFor(home, repoIdentity) {
28
+ let dir = globalJournalDir(home);
29
+ const project = findProject(home, repoIdentity);
30
+ if (project) dir = projectJournalDir(project);
31
+ let journal = journalCache.get(dir);
32
+ if (!journal) {
33
+ journal = new Journal(dir);
34
+ journalCache.set(dir, journal);
35
+ }
36
+ return journal;
37
+ }
38
+
39
+ module.exports = { journalFor, globalJournalDir, projectJournalDir };