@sanqianx/project-knowledge 4.6.1
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/.agents/plugins/marketplace.json +20 -0
- package/.claude-plugin/marketplace.json +17 -0
- package/CHANGELOG.md +780 -0
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +266 -0
- package/_site/README.md +30 -0
- package/_site/_test/ai-profile-resolver-test.js +137 -0
- package/_site/_test/ai-profile-test.js +75 -0
- package/_site/_test/ai-vendor-presets-test.js +91 -0
- package/_site/_test/automation-queue-test.js +14 -0
- package/_site/_test/automation-ui-test.js +40 -0
- package/_site/_test/background-task-registry-test.js +43 -0
- package/_site/_test/baseline-schema-test.js +91 -0
- package/_site/_test/bin-cli-test.js +218 -0
- package/_site/_test/bridge-adapter-test.js +95 -0
- package/_site/_test/bridge-consumer-service-test.js +310 -0
- package/_site/_test/cdp-browser-failure-test.js +100 -0
- package/_site/_test/chat-claudecodeui-match-test.js +92 -0
- package/_site/_test/claude-executable-discovery-test.js +90 -0
- package/_site/_test/claude-session-lifecycle-sweep-test.js +252 -0
- package/_site/_test/claude-workbench-test.js +170 -0
- package/_site/_test/client-capture-restart-e2e-test.js +189 -0
- package/_site/_test/codex-conversation-projection-test.js +59 -0
- package/_site/_test/codex-notify-fanout-test.js +45 -0
- package/_site/_test/commit-boundary-freeze-test.js +147 -0
- package/_site/_test/commit-conversation-binding-test.js +99 -0
- package/_site/_test/commit-evidence-test.js +110 -0
- package/_site/_test/context-pack-test.js +198 -0
- package/_site/_test/conversation-api-test.js +119 -0
- package/_site/_test/conversation-store-test.js +136 -0
- package/_site/_test/cross-repo-isolation-e2e-test.js +187 -0
- package/_site/_test/data-dir-migration-test.js +77 -0
- package/_site/_test/desktop-browser-compat-test.js +28 -0
- package/_site/_test/desktop-hook-runtime-regression-test.js +204 -0
- package/_site/_test/embedding-config-test.js +32 -0
- package/_site/_test/explicit-commit-processor-test.js +37 -0
- package/_site/_test/fixtures/fake-browser-exit.js +10 -0
- package/_site/_test/fixtures/fake-browser-hang.js +10 -0
- package/_site/_test/fixtures/make-git-repos.js +89 -0
- package/_site/_test/folder-picker-output-test.js +19 -0
- package/_site/_test/full-integration-e2e-test.js +165 -0
- package/_site/_test/git-validation-test.js +81 -0
- package/_site/_test/hook-runtime-endpoint-test.js +88 -0
- package/_site/_test/hook-status-repair-api-test.js +152 -0
- package/_site/_test/hook-trigger-test.js +85 -0
- package/_site/_test/hook-worktree-test.js +42 -0
- package/_site/_test/import-preflight-api-test.js +233 -0
- package/_site/_test/import-ui-flow-test.js +171 -0
- package/_site/_test/index-builder-test.js +41 -0
- package/_site/_test/index-writer-concurrency-test.js +135 -0
- package/_site/_test/integration-adapters-test.js +243 -0
- package/_site/_test/integration-capture-setup-test.js +108 -0
- package/_site/_test/integration-surface-coverage-test.js +165 -0
- package/_site/_test/kb-framework-test.js +57 -0
- package/_site/_test/kbpath-follow-test.js +63 -0
- package/_site/_test/knowledge-db-test.js +144 -0
- package/_site/_test/knowledge-language-control-test.js +170 -0
- package/_site/_test/knowledge-maintenance-test.js +40 -0
- package/_site/_test/knowledge-migration-test.js +75 -0
- package/_site/_test/knowledge-promotion-recovery-test.js +260 -0
- package/_site/_test/knowledge-query-test.js +54 -0
- package/_site/_test/knowledge-retrieval-service-test.js +77 -0
- package/_site/_test/knowledge-scopes-test.js +37 -0
- package/_site/_test/knowledge-storage-location-test.js +28 -0
- package/_site/_test/knowledge-storage-startup-test.js +52 -0
- package/_site/_test/knowledge-store-logs-supervision-test.js +89 -0
- package/_site/_test/legacy-claude-session-alias-test.js +106 -0
- package/_site/_test/legacy-forward-compat-test.js +257 -0
- package/_site/_test/legacy-project-upgrade-e2e-test.js +363 -0
- package/_site/_test/legacy-routes-removed-test.js +47 -0
- package/_site/_test/legacy-workbench-exclusion-test.js +92 -0
- package/_site/_test/log-redaction-test.js +58 -0
- package/_site/_test/logging-api-test.js +89 -0
- package/_site/_test/logging-sse-no-gap-test.js +88 -0
- package/_site/_test/logging-ui-test.js +104 -0
- package/_site/_test/markdown-delta-overlay-test.js +85 -0
- package/_site/_test/markdown-maintenance-api-test.js +75 -0
- package/_site/_test/markdown-maintenance-test.js +11 -0
- package/_site/_test/mcp-server-test.js +149 -0
- package/_site/_test/model-context-windows-test.js +89 -0
- package/_site/_test/module-bridge-eventbridge-test.js +193 -0
- package/_site/_test/non-release-ci-test.js +34 -0
- package/_site/_test/offline-boundary-isolation-test.js +32 -0
- package/_site/_test/orphaned-claim-recovery-test.js +80 -0
- package/_site/_test/p0-data-migration-characterization-test.js +37 -0
- package/_site/_test/p0-e2e-gate-test.js +347 -0
- package/_site/_test/package-boundary-test.js +42 -0
- package/_site/_test/package-startup-test.js +56 -0
- package/_site/_test/packaged-ui-smoke-test.js +79 -0
- package/_site/_test/path-consistency-test.js +151 -0
- package/_site/_test/pending-sweep-test.js +9 -0
- package/_site/_test/post-commit-automation-test.js +87 -0
- package/_site/_test/pr-consumer-contract-test.js +55 -0
- package/_site/_test/project-control-panel-task14-test.js +63 -0
- package/_site/_test/project-delete-recovery-test.js +64 -0
- package/_site/_test/project-goal-editor-test.js +145 -0
- package/_site/_test/project-layout-v2-migration-test.js +127 -0
- package/_site/_test/project-lifecycle-transaction-test.js +106 -0
- package/_site/_test/project-remove-running-guard-test.js +79 -0
- package/_site/_test/project-store-test.js +90 -0
- package/_site/_test/prompt-settings-test.js +115 -0
- package/_site/_test/protected-architecture-gate-test.js +123 -0
- package/_site/_test/refactor-characterization-test.js +36 -0
- package/_site/_test/release-version-sync-test.js +98 -0
- package/_site/_test/requirement-binding-test.js +113 -0
- package/_site/_test/requirement-recorder-test.js +173 -0
- package/_site/_test/run-all-tests.js +156 -0
- package/_site/_test/runtime-endpoint-test.js +64 -0
- package/_site/_test/scanner-test.js +83 -0
- package/_site/_test/server-runtime-migration-safety-test.js +27 -0
- package/_site/_test/server-security-test.js +96 -0
- package/_site/_test/sessions-stream-test.js +92 -0
- package/_site/_test/shared-contracts-test.js +58 -0
- package/_site/_test/simple-import-test.js +97 -0
- package/_site/_test/startup-analysis-disabled-test.js +16 -0
- package/_site/_test/storage-foundation-test.js +78 -0
- package/_site/_test/structured-logger-test.js +101 -0
- package/_site/_test/task15-20-ui-flow-test.js +148 -0
- package/_site/_test/team-knowledge-store-sparse-checkout-test.js +196 -0
- package/_site/_test/team-knowledge-store-test.js +131 -0
- package/_site/_test/tracking-start-test.js +143 -0
- package/_site/_test/ui-i18n-toggle-test.js +114 -0
- package/_site/_test/ui-smoke-test.js +73 -0
- package/_site/_test/v4122-upgrade-data-contract-test.js +56 -0
- package/_site/_test/workbench-permission-test.js +118 -0
- package/_site/_test/workspace-ui-contract-test.js +43 -0
- package/_site/lib/ai-profile-resolver.js +78 -0
- package/_site/lib/ai-vendor-presets.js +83 -0
- package/_site/lib/ai-workspace.js +104 -0
- package/_site/lib/atomic-file.js +204 -0
- package/_site/lib/automation-config.js +140 -0
- package/_site/lib/bridge-adapter.js +256 -0
- package/_site/lib/bridge-consumer-service.js +402 -0
- package/_site/lib/claude-cli-runner.js +1599 -0
- package/_site/lib/commit-conversation-binder.js +246 -0
- package/_site/lib/commit-processing-ledger.js +27 -0
- package/_site/lib/commit-prompt.js +202 -0
- package/_site/lib/commit-reconciler.js +444 -0
- package/_site/lib/context-pack-builder.js +299 -0
- package/_site/lib/contracts.js +279 -0
- package/_site/lib/conversation-exclusions.js +204 -0
- package/_site/lib/conversation-query-service.js +161 -0
- package/_site/lib/conversation-store.js +385 -0
- package/_site/lib/data-dir.js +81 -0
- package/_site/lib/data-state-classifier.js +40 -0
- package/_site/lib/embedding-service.js +85 -0
- package/_site/lib/evidence-bundle.js +228 -0
- package/_site/lib/folder-picker-output.js +24 -0
- package/_site/lib/git-runner.js +62 -0
- package/_site/lib/github-team-store.js +1175 -0
- package/_site/lib/hook-manager.js +202 -0
- package/_site/lib/index-builder.js +157 -0
- package/_site/lib/index-service.js +173 -0
- package/_site/lib/integration-installer.js +907 -0
- package/_site/lib/kb-framework.js +191 -0
- package/_site/lib/kb-validator.js +124 -0
- package/_site/lib/knowledge-db.js +363 -0
- package/_site/lib/knowledge-promotion.js +435 -0
- package/_site/lib/knowledge-retrieval-service.js +401 -0
- package/_site/lib/knowledge-schema.js +121 -0
- package/_site/lib/knowledge-tool-runtime.js +359 -0
- package/_site/lib/legacy-data-manifest.js +26 -0
- package/_site/lib/llm-client.js +173 -0
- package/_site/lib/markdown-knowledge-indexer.js +230 -0
- package/_site/lib/migration-service.js +442 -0
- package/_site/lib/model-context-windows.js +82 -0
- package/_site/lib/module-bridge.js +496 -0
- package/_site/lib/post-commit-automation.js +141 -0
- package/_site/lib/project-lifecycle-service.js +511 -0
- package/_site/lib/project-registry-store.js +129 -0
- package/_site/lib/project-store.js +262 -0
- package/_site/lib/prompt-registry.js +148 -0
- package/_site/lib/requirement-adapters.js +10 -0
- package/_site/lib/requirement-binder.js +112 -0
- package/_site/lib/requirement-recorder.js +278 -0
- package/_site/lib/runtime-endpoint.js +160 -0
- package/_site/lib/scanner.js +298 -0
- package/_site/lib/server-app.js +1520 -0
- package/_site/lib/settings-store.js +113 -0
- package/_site/lib/storage-layout.js +162 -0
- package/_site/lib/structured-logger.js +574 -0
- package/_site/scripts/codex-notify-fanout.cjs +51 -0
- package/_site/scripts/folder-picker.ps1 +156 -0
- package/_site/scripts/hook-trigger.js +156 -0
- package/_site/scripts/sync-release-version.js +158 -0
- package/_site/server.js +12 -0
- package/_site/start.bat +26 -0
- package/_site/stop.bat +11 -0
- package/bin/project-knowledge-integrations.js +241 -0
- package/bin/project-knowledge-kb.js +65 -0
- package/bin/project-knowledge-mcp.js +195 -0
- package/bin/project-knowledge.js +439 -0
- package/claude-prompts.json +21 -0
- package/docs/README.zh-CN.md +175 -0
- package/docs/assets/architecture.svg +182 -0
- package/docs/assets/dashboard.png +0 -0
- package/docs/assets/draft-review.png +0 -0
- package/docs/assets/logo.svg +50 -0
- package/docs/assets/runs-drafts.png +0 -0
- package/docs/assets/settings.png +0 -0
- package/docs/project-registry-schema.md +33 -0
- package/docs/team-knowledge-mode-a-plan.md +73 -0
- package/docs/testing-strategy.md +33 -0
- package/package.json +100 -0
- package/plugins/project-knowledge/.claude-plugin/plugin.json +19 -0
- package/plugins/project-knowledge/.codex-plugin/plugin.json +38 -0
- package/plugins/project-knowledge/.mcp.json +14 -0
- package/plugins/project-knowledge/opencode/project-knowledge.md +3 -0
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -0
- package/plugins/project-knowledge/skills/project-knowledge/agents/openai.yaml +11 -0
- package/templates/change.md +30 -0
- package/templates/project-readme.md +52 -0
- package/ui/app.css +58 -0
- package/ui/app.js +607 -0
- package/ui/favicon.svg +38 -0
- package/ui/i18n.js +146 -0
- package/ui/index.html +162 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { validateProjectId } = require('./contracts');
|
|
4
|
+
const AtomicFile = require('./atomic-file');
|
|
5
|
+
|
|
6
|
+
const EXCLUSION_SCHEMA = 'conversation-exclusions/v1';
|
|
7
|
+
const EMBEDDED_ASSISTANT_PREFIX = 'embedded-assistant-';
|
|
8
|
+
const EXPLICIT_PREFIX = 'explicit-';
|
|
9
|
+
const CODEX_CONTEXT_TAGS = new Set([
|
|
10
|
+
'recommended_plugins',
|
|
11
|
+
'environment_context',
|
|
12
|
+
'in-app-browser-context',
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* T17: legacy embedded Workbench capture exclusion.
|
|
17
|
+
*
|
|
18
|
+
* Old builds wrote internal Workbench conversations into Development
|
|
19
|
+
* Conversation as `explicit-<requirementId>` + `embedded-assistant-<requirementId>`
|
|
20
|
+
* pairs (rawEventType embedded-claude-result). Those events are excluded from
|
|
21
|
+
* Development Conversation presentation and from NEW commit binding. Nothing
|
|
22
|
+
* is deleted: the JSONL lines stay, immutable snapshots stay untouched, and a
|
|
23
|
+
* manifest is persisted for auditability. A genuine explicit MCP requirement
|
|
24
|
+
* (no embedded-assistant pair) is never excluded.
|
|
25
|
+
*/
|
|
26
|
+
function isEmbeddedAssistantEvent(event) {
|
|
27
|
+
if (!event) return false;
|
|
28
|
+
return String(event.eventId || '').startsWith(EMBEDDED_ASSISTANT_PREFIX)
|
|
29
|
+
|| event.rawEventType === 'embedded-claude-result';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Codex rollout files contain generated context records with role=user. They
|
|
34
|
+
* are transport/control data, not user-authored prompts. Match only complete,
|
|
35
|
+
* known envelopes so ordinary prompts containing similar text survive.
|
|
36
|
+
*/
|
|
37
|
+
function isCodexControlUserEvent(event) {
|
|
38
|
+
if (!event || event.source !== 'codex' || event.role !== 'user' || event.rawEventType !== 'response_item') return false;
|
|
39
|
+
let remaining = String(event.content || '').trim();
|
|
40
|
+
if (/^<turn_aborted>\s*[\s\S]*?\s*<\/turn_aborted>$/.test(remaining)) return true;
|
|
41
|
+
|
|
42
|
+
let removed = false;
|
|
43
|
+
const envelope = /^<([A-Za-z0-9_-]+)(?:\s[^>]*)?>[\s\S]*?<\/\1>\s*/;
|
|
44
|
+
while (remaining) {
|
|
45
|
+
const match = remaining.match(envelope);
|
|
46
|
+
if (!match || !CODEX_CONTEXT_TAGS.has(match[1])) break;
|
|
47
|
+
remaining = remaining.slice(match[0].length).trimStart();
|
|
48
|
+
removed = true;
|
|
49
|
+
}
|
|
50
|
+
return removed && remaining.trim() === '';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isCodexAbortEvent(event) {
|
|
54
|
+
return isCodexControlUserEvent(event)
|
|
55
|
+
&& /^<turn_aborted>/.test(String(event.content || '').trim());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function computeConversationExclusions(projectId, events) {
|
|
59
|
+
validateProjectId(projectId);
|
|
60
|
+
const paired = new Set();
|
|
61
|
+
for (const event of events || []) {
|
|
62
|
+
const eventId = String((event && event.eventId) || '');
|
|
63
|
+
if (eventId.startsWith(EMBEDDED_ASSISTANT_PREFIX)) {
|
|
64
|
+
paired.add(eventId.slice(EMBEDDED_ASSISTANT_PREFIX.length));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const excludedEventIds = [];
|
|
68
|
+
for (const event of events || []) {
|
|
69
|
+
if (!event) continue;
|
|
70
|
+
const eventId = String(event.eventId || '');
|
|
71
|
+
if (isEmbeddedAssistantEvent(event)) {
|
|
72
|
+
excludedEventIds.push(eventId);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (isCodexControlUserEvent(event)) {
|
|
76
|
+
excludedEventIds.push(eventId);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (eventId.startsWith(EXPLICIT_PREFIX) && paired.has(eventId.slice(EXPLICIT_PREFIX.length))) {
|
|
80
|
+
excludedEventIds.push(eventId);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
schema: EXCLUSION_SCHEMA,
|
|
85
|
+
projectId,
|
|
86
|
+
excludedEventIds: [...new Set(excludedEventIds)].sort(),
|
|
87
|
+
generatedAt: new Date().toISOString(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function eventOrder(left, right) {
|
|
92
|
+
if (Number.isInteger(left.sequence) && Number.isInteger(right.sequence)) return left.sequence - right.sequence;
|
|
93
|
+
if (Number.isInteger(left.sequence)) return -1;
|
|
94
|
+
if (Number.isInteger(right.sequence)) return 1;
|
|
95
|
+
return String(left.capturedAt || '').localeCompare(String(right.capturedAt || ''))
|
|
96
|
+
|| String(left.eventId || '').localeCompare(String(right.eventId || ''));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Bridge 0.1.1 did not read Codex's nested turn id, so historical assistant
|
|
101
|
+
* response_item records have turnId=null while each user record received a
|
|
102
|
+
* generated id. Repair that projection without rewriting the append-only
|
|
103
|
+
* JSONL: orphan assistants attach to the latest real user in the same Codex
|
|
104
|
+
* session. A later real prompt or turn_aborted record closes that projected
|
|
105
|
+
* turn, which also lets commit binding ignore stale Bridge open-turn facts.
|
|
106
|
+
*/
|
|
107
|
+
function projectCodexTurns(events, excludedEventIds) {
|
|
108
|
+
const excluded = new Set(excludedEventIds || []);
|
|
109
|
+
const activeBySession = new Map();
|
|
110
|
+
const closedAtByTurn = new Map();
|
|
111
|
+
const projected = [];
|
|
112
|
+
const ordered = [...(events || [])].sort(eventOrder);
|
|
113
|
+
const sessionKey = event => event && event.source === 'codex' && event.sessionId
|
|
114
|
+
? `codex\n${event.sessionId}`
|
|
115
|
+
: null;
|
|
116
|
+
|
|
117
|
+
for (const original of ordered) {
|
|
118
|
+
const eventId = String((original && original.eventId) || '');
|
|
119
|
+
const key = sessionKey(original || {});
|
|
120
|
+
if (excluded.has(eventId)) {
|
|
121
|
+
if (key && isCodexAbortEvent(original)) {
|
|
122
|
+
const active = activeBySession.get(key);
|
|
123
|
+
if (active && Number.isInteger(original.sequence)) closedAtByTurn.set(active, original.sequence);
|
|
124
|
+
activeBySession.delete(key);
|
|
125
|
+
}
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let event = original;
|
|
130
|
+
if (key && original && original.source === 'codex' && original.role === 'user' && original.turnId) {
|
|
131
|
+
const previous = activeBySession.get(key);
|
|
132
|
+
if (previous && previous !== original.turnId && Number.isInteger(original.sequence)) {
|
|
133
|
+
closedAtByTurn.set(previous, original.sequence);
|
|
134
|
+
}
|
|
135
|
+
activeBySession.set(key, original.turnId);
|
|
136
|
+
} else if (key && original && original.source === 'codex' && original.role === 'assistant' && !original.turnId) {
|
|
137
|
+
const active = activeBySession.get(key);
|
|
138
|
+
if (active) event = { ...original, turnId: active };
|
|
139
|
+
}
|
|
140
|
+
projected.push(event);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return projected.map(event => {
|
|
144
|
+
const closedAt = event && event.turnId ? closedAtByTurn.get(event.turnId) : null;
|
|
145
|
+
return Number.isInteger(closedAt)
|
|
146
|
+
? { ...event, developmentTurnClosedAtSequence: closedAt }
|
|
147
|
+
: event;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function manifestPath(layout, projectId) {
|
|
152
|
+
return path.join(layout.getProjectMetadataDir(projectId), 'conversation-exclusions.json');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function readManifest(layout, projectId) {
|
|
156
|
+
try {
|
|
157
|
+
return AtomicFile.readJsonStrict(manifestPath(layout, projectId), {
|
|
158
|
+
category: 'conversation-exclusions',
|
|
159
|
+
validate: value => value,
|
|
160
|
+
});
|
|
161
|
+
} catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Single source of truth for Development Conversation reads: applies durable
|
|
168
|
+
* exclusions and the non-destructive Codex turn compatibility projection for
|
|
169
|
+
* both Conversation Explorer and new CommitConversationBinder runs. The
|
|
170
|
+
* manifest is refreshed idempotently (best effort) so operators can audit
|
|
171
|
+
* what is excluded; read failures never break the read itself.
|
|
172
|
+
*/
|
|
173
|
+
function readDevelopmentEvents(conversationStore, projectId) {
|
|
174
|
+
const events = conversationStore.readEvents(projectId);
|
|
175
|
+
const manifest = computeConversationExclusions(projectId, events);
|
|
176
|
+
if (manifest.excludedEventIds.length) {
|
|
177
|
+
try {
|
|
178
|
+
const layout = conversationStore.layout;
|
|
179
|
+
const dir = layout.getProjectMetadataDir(projectId);
|
|
180
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
181
|
+
const previous = readManifest(layout, projectId);
|
|
182
|
+
const unchanged = previous
|
|
183
|
+
&& Array.isArray(previous.excludedEventIds)
|
|
184
|
+
&& previous.excludedEventIds.length === manifest.excludedEventIds.length
|
|
185
|
+
&& previous.excludedEventIds.every((id, index) => id === manifest.excludedEventIds[index]);
|
|
186
|
+
if (!unchanged) {
|
|
187
|
+
(conversationStore.atomic || AtomicFile).writeJsonAtomic(manifestPath(layout, projectId), manifest);
|
|
188
|
+
}
|
|
189
|
+
} catch {
|
|
190
|
+
// Manifest persistence is best effort; exclusion itself is computed
|
|
191
|
+
// deterministically from the durable events on every read.
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return projectCodexTurns(events, manifest.excludedEventIds);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = {
|
|
198
|
+
EXCLUSION_SCHEMA,
|
|
199
|
+
isEmbeddedAssistantEvent,
|
|
200
|
+
isCodexControlUserEvent,
|
|
201
|
+
computeConversationExclusions,
|
|
202
|
+
projectCodexTurns,
|
|
203
|
+
readDevelopmentEvents,
|
|
204
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const { DomainError, validateProjectId } = require('./contracts');
|
|
3
|
+
const { ConversationStore } = require('./conversation-store');
|
|
4
|
+
const { readDevelopmentEvents } = require('./conversation-exclusions');
|
|
5
|
+
|
|
6
|
+
function localDate(iso) {
|
|
7
|
+
const date = new Date(iso);
|
|
8
|
+
if (Number.isNaN(date.getTime())) return '';
|
|
9
|
+
const pad = value => String(value).padStart(2, '0');
|
|
10
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function validDate(value) {
|
|
14
|
+
const date = String(value || '');
|
|
15
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || Number.isNaN(Date.parse(`${date}T00:00:00`))) {
|
|
16
|
+
throw new DomainError('INVALID_ARGUMENT', 'date must be YYYY-MM-DD.');
|
|
17
|
+
}
|
|
18
|
+
return date;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function fingerprint(projectId, date) {
|
|
22
|
+
return crypto.createHash('sha256').update(`${projectId}\n${date}`, 'utf8').digest('hex');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function encodeCursor(projectId, date, beforeKey) {
|
|
26
|
+
return Buffer.from(JSON.stringify({ v: 1, fingerprint: fingerprint(projectId, date), beforeKey }), 'utf8').toString('base64url');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function decodeCursor(value, projectId, date) {
|
|
30
|
+
if (!value) return null;
|
|
31
|
+
try {
|
|
32
|
+
const cursor = JSON.parse(Buffer.from(String(value), 'base64url').toString('utf8'));
|
|
33
|
+
if (!cursor || cursor.v !== 1 || cursor.fingerprint !== fingerprint(projectId, date) || typeof cursor.beforeKey !== 'string') throw new Error('mismatch');
|
|
34
|
+
return cursor;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new DomainError('INVALID_ARGUMENT', 'Conversation cursor is invalid for this project/date query.', { status: 409, cause: error });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class ConversationQueryService {
|
|
41
|
+
constructor(options = {}) {
|
|
42
|
+
this.registryStore = options.registryStore;
|
|
43
|
+
this.projectStore = options.projectStore;
|
|
44
|
+
this.conversationStore = options.conversationStore || new ConversationStore(options);
|
|
45
|
+
this.logger = options.logger || null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async log(level, event, message, context) {
|
|
49
|
+
try { if (this.logger && typeof this.logger[level] === 'function') await this.logger[level](event, message, context); } catch {
|
|
50
|
+
// Read-only conversation queries do not depend on observer availability.
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
listProjects() {
|
|
55
|
+
return this.registryStore.listIds().map(projectId => {
|
|
56
|
+
const config = this.projectStore.readConfig(projectId);
|
|
57
|
+
const state = this.projectStore.readState(projectId);
|
|
58
|
+
return {
|
|
59
|
+
projectId,
|
|
60
|
+
displayName: config.displayName,
|
|
61
|
+
enabled: config.enabled !== false,
|
|
62
|
+
captureStatus: state.conversation && state.conversation.captureStatus || 'unavailable',
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
annotations(projectId, events = []) {
|
|
68
|
+
const byTurn = new Map();
|
|
69
|
+
const closedAtByTurn = new Map(events
|
|
70
|
+
.filter(event => event.turnId && Number.isInteger(event.developmentTurnClosedAtSequence))
|
|
71
|
+
.map(event => [event.turnId, event.developmentTurnClosedAtSequence]));
|
|
72
|
+
for (const snapshot of this.conversationStore.listSnapshots(projectId)) {
|
|
73
|
+
for (const turn of snapshot.turns) {
|
|
74
|
+
const closedAt = closedAtByTurn.get(turn.turnId);
|
|
75
|
+
if (turn.bindingKind === 'shared-spanning'
|
|
76
|
+
&& Number.isInteger(snapshot.boundaryStartCursor)
|
|
77
|
+
&& Number.isInteger(closedAt)
|
|
78
|
+
&& closedAt <= snapshot.boundaryStartCursor) continue;
|
|
79
|
+
const current = byTurn.get(turn.turnId) || [];
|
|
80
|
+
current.push({
|
|
81
|
+
commitSha: snapshot.commitSha,
|
|
82
|
+
shortSha: snapshot.commitSha.slice(0, 7),
|
|
83
|
+
status: turn.bindingKind === 'shared-spanning' ? 'associated' : 'committed',
|
|
84
|
+
bindingKind: turn.bindingKind,
|
|
85
|
+
snapshotHash: snapshot.snapshotHash,
|
|
86
|
+
});
|
|
87
|
+
byTurn.set(turn.turnId, current);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const values of byTurn.values()) values.sort((left, right) => left.commitSha.localeCompare(right.commitSha));
|
|
91
|
+
return byTurn;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async turns(input = {}) {
|
|
95
|
+
const projectId = validateProjectId(input.projectId);
|
|
96
|
+
if (!this.registryStore.readDisplaySnapshot(projectId)) throw new DomainError('PROJECT_NOT_FOUND', 'Project was not found.', { status: 404 });
|
|
97
|
+
const date = validDate(input.date);
|
|
98
|
+
const limit = Math.max(1, Math.min(Number(input.limit || 50), 200));
|
|
99
|
+
const cursor = decodeCursor(input.cursor, projectId, date);
|
|
100
|
+
const groups = new Map();
|
|
101
|
+
const events = readDevelopmentEvents(this.conversationStore, projectId);
|
|
102
|
+
for (const event of events) {
|
|
103
|
+
const turnId = event.turnId || `event:${event.eventId}`;
|
|
104
|
+
let group = groups.get(turnId);
|
|
105
|
+
if (!group) {
|
|
106
|
+
group = { turnId, source: event.source, sessionId: event.sessionId, events: [] };
|
|
107
|
+
groups.set(turnId, group);
|
|
108
|
+
}
|
|
109
|
+
group.events.push(event);
|
|
110
|
+
}
|
|
111
|
+
const annotations = this.annotations(projectId, events);
|
|
112
|
+
let projected = [...groups.values()].map(group => {
|
|
113
|
+
group.events.sort((left, right) => String(left.capturedAt).localeCompare(String(right.capturedAt)) || left.eventId.localeCompare(right.eventId));
|
|
114
|
+
const first = group.events[0];
|
|
115
|
+
const last = group.events.at(-1);
|
|
116
|
+
const userEvents = group.events.filter(event => event.role === 'user');
|
|
117
|
+
const assistantEvents = group.events.filter(event => event.role === 'assistant');
|
|
118
|
+
const eventDate = localDate((userEvents[0] || first).capturedAt);
|
|
119
|
+
const turnAnnotations = annotations.get(group.turnId) || [];
|
|
120
|
+
const status = turnAnnotations.some(annotation => annotation.status === 'committed')
|
|
121
|
+
? 'committed'
|
|
122
|
+
: turnAnnotations.length ? 'associated' : 'uncommitted';
|
|
123
|
+
return {
|
|
124
|
+
turnId: group.turnId,
|
|
125
|
+
source: group.source,
|
|
126
|
+
sessionId: group.sessionId,
|
|
127
|
+
date: eventDate,
|
|
128
|
+
startedAt: first.capturedAt,
|
|
129
|
+
updatedAt: last.capturedAt,
|
|
130
|
+
userPrompt: userEvents.map(event => event.content).join('\n\n'),
|
|
131
|
+
assistantReply: assistantEvents.map(event => event.content).join('\n\n'),
|
|
132
|
+
userEventIds: userEvents.map(event => event.eventId),
|
|
133
|
+
assistantEventIds: assistantEvents.map(event => event.eventId),
|
|
134
|
+
annotation: { status, commits: turnAnnotations },
|
|
135
|
+
renderAs: 'plain-text',
|
|
136
|
+
key: `${first.capturedAt}|${group.turnId}`,
|
|
137
|
+
};
|
|
138
|
+
}).filter(turn => turn.userEventIds.length > 0 && turn.date === date)
|
|
139
|
+
.sort((left, right) => right.key.localeCompare(left.key));
|
|
140
|
+
if (cursor) projected = projected.filter(turn => turn.key < cursor.beforeKey);
|
|
141
|
+
const page = projected.slice(0, limit);
|
|
142
|
+
const hasMore = projected.length > page.length;
|
|
143
|
+
const result = {
|
|
144
|
+
ok: true,
|
|
145
|
+
projectId,
|
|
146
|
+
date,
|
|
147
|
+
turns: page.map(({ key, ...turn }) => turn),
|
|
148
|
+
nextCursor: hasMore ? encodeCursor(projectId, date, page.at(-1).key) : null,
|
|
149
|
+
hasMore,
|
|
150
|
+
limit,
|
|
151
|
+
};
|
|
152
|
+
await this.log('debug', 'conversation.turns_queried', 'Development conversation turns were queried.', {
|
|
153
|
+
projectId,
|
|
154
|
+
component: 'conversation-query',
|
|
155
|
+
context: { date, resultCount: result.turns.length, hasMore },
|
|
156
|
+
});
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
module.exports = { ConversationQueryService, localDate, validDate, encodeCursor, decodeCursor };
|