@jagilber-org/index-server 1.19.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/CHANGELOG.md +1218 -0
- package/CODE_OF_CONDUCT.md +49 -0
- package/CONTRIBUTING.md +75 -0
- package/LICENSE +21 -0
- package/README.md +523 -0
- package/SECURITY.md +50 -0
- package/dist/config/configUtils.d.ts +11 -0
- package/dist/config/configUtils.js +87 -0
- package/dist/config/dashboardConfig.d.ts +45 -0
- package/dist/config/dashboardConfig.js +63 -0
- package/dist/config/defaultValues.d.ts +61 -0
- package/dist/config/defaultValues.js +70 -0
- package/dist/config/dirConstants.d.ts +17 -0
- package/dist/config/dirConstants.js +28 -0
- package/dist/config/featureConfig.d.ts +61 -0
- package/dist/config/featureConfig.js +121 -0
- package/dist/config/runtimeConfig.d.ts +145 -0
- package/dist/config/runtimeConfig.js +334 -0
- package/dist/config/serverConfig.d.ts +90 -0
- package/dist/config/serverConfig.js +164 -0
- package/dist/dashboard/analytics/AnalyticsEngine.d.ts +142 -0
- package/dist/dashboard/analytics/AnalyticsEngine.js +373 -0
- package/dist/dashboard/analytics/BusinessIntelligence.d.ts +187 -0
- package/dist/dashboard/analytics/BusinessIntelligence.js +594 -0
- package/dist/dashboard/client/admin.html +2150 -0
- package/dist/dashboard/client/chunks/mermaid-layout-elk.esm.min/chunk-SP2CHFBE.mjs +1 -0
- package/dist/dashboard/client/chunks/mermaid-layout-elk.esm.min/render-T6MDALS3.mjs +27 -0
- package/dist/dashboard/client/css/admin.css +1466 -0
- package/dist/dashboard/client/js/admin.boot.js +359 -0
- package/dist/dashboard/client/js/admin.config.js +196 -0
- package/dist/dashboard/client/js/admin.embeddings.js +425 -0
- package/dist/dashboard/client/js/admin.graph.js +583 -0
- package/dist/dashboard/client/js/admin.instances.js +120 -0
- package/dist/dashboard/client/js/admin.instructions.js +552 -0
- package/dist/dashboard/client/js/admin.logs.js +113 -0
- package/dist/dashboard/client/js/admin.maintenance.js +354 -0
- package/dist/dashboard/client/js/admin.messaging.js +635 -0
- package/dist/dashboard/client/js/admin.monitor.js +181 -0
- package/dist/dashboard/client/js/admin.overview.js +221 -0
- package/dist/dashboard/client/js/admin.performance.js +61 -0
- package/dist/dashboard/client/js/admin.sessions.js +293 -0
- package/dist/dashboard/client/js/admin.sqlite.js +366 -0
- package/dist/dashboard/client/js/admin.utils.js +49 -0
- package/dist/dashboard/client/js/chart.umd.js +14 -0
- package/dist/dashboard/client/js/elk.bundled.js +6696 -0
- package/dist/dashboard/client/js/marked.umd.js +74 -0
- package/dist/dashboard/client/js/mermaid.min.js +3022 -0
- package/dist/dashboard/client/mermaid-layout-elk.esm.min.mjs +1 -0
- package/dist/dashboard/export/DataExporter.d.ts +169 -0
- package/dist/dashboard/export/DataExporter.js +737 -0
- package/dist/dashboard/export/exporters/csvExporter.d.ts +11 -0
- package/dist/dashboard/export/exporters/csvExporter.js +46 -0
- package/dist/dashboard/export/exporters/exportTypes.d.ts +89 -0
- package/dist/dashboard/export/exporters/exportTypes.js +5 -0
- package/dist/dashboard/export/exporters/jsonExporter.d.ts +7 -0
- package/dist/dashboard/export/exporters/jsonExporter.js +22 -0
- package/dist/dashboard/export/exporters/xmlExporter.d.ts +17 -0
- package/dist/dashboard/export/exporters/xmlExporter.js +175 -0
- package/dist/dashboard/integration/APIIntegration.d.ts +41 -0
- package/dist/dashboard/integration/APIIntegration.js +95 -0
- package/dist/dashboard/security/SecurityMonitor.d.ts +167 -0
- package/dist/dashboard/security/SecurityMonitor.js +559 -0
- package/dist/dashboard/server/AdminPanel.d.ts +183 -0
- package/dist/dashboard/server/AdminPanel.js +792 -0
- package/dist/dashboard/server/AdminPanelConfig.d.ts +42 -0
- package/dist/dashboard/server/AdminPanelConfig.js +80 -0
- package/dist/dashboard/server/AdminPanelState.d.ts +47 -0
- package/dist/dashboard/server/AdminPanelState.js +214 -0
- package/dist/dashboard/server/ApiRoutes.d.ts +17 -0
- package/dist/dashboard/server/ApiRoutes.js +149 -0
- package/dist/dashboard/server/DashboardServer.d.ts +49 -0
- package/dist/dashboard/server/DashboardServer.js +159 -0
- package/dist/dashboard/server/FileMetricsStorage.d.ts +49 -0
- package/dist/dashboard/server/FileMetricsStorage.js +195 -0
- package/dist/dashboard/server/HttpTransport.d.ts +23 -0
- package/dist/dashboard/server/HttpTransport.js +116 -0
- package/dist/dashboard/server/InstanceManager.d.ts +53 -0
- package/dist/dashboard/server/InstanceManager.js +284 -0
- package/dist/dashboard/server/KnowledgeStore.d.ts +35 -0
- package/dist/dashboard/server/KnowledgeStore.js +105 -0
- package/dist/dashboard/server/LeaderElection.d.ts +81 -0
- package/dist/dashboard/server/LeaderElection.js +268 -0
- package/dist/dashboard/server/MetricsCollector.d.ts +200 -0
- package/dist/dashboard/server/MetricsCollector.js +803 -0
- package/dist/dashboard/server/SessionPersistenceManager.d.ts +88 -0
- package/dist/dashboard/server/SessionPersistenceManager.js +457 -0
- package/dist/dashboard/server/ThinClient.d.ts +64 -0
- package/dist/dashboard/server/ThinClient.js +237 -0
- package/dist/dashboard/server/WebSocketManager.d.ts +161 -0
- package/dist/dashboard/server/WebSocketManager.js +463 -0
- package/dist/dashboard/server/httpLifecycle.d.ts +17 -0
- package/dist/dashboard/server/httpLifecycle.js +35 -0
- package/dist/dashboard/server/legacyDashboardHtml.d.ts +9 -0
- package/dist/dashboard/server/legacyDashboardHtml.js +618 -0
- package/dist/dashboard/server/legacyDashboardStyles.d.ts +5 -0
- package/dist/dashboard/server/legacyDashboardStyles.js +490 -0
- package/dist/dashboard/server/metricsAggregation.d.ts +252 -0
- package/dist/dashboard/server/metricsAggregation.js +206 -0
- package/dist/dashboard/server/metricsSerializer.d.ts +25 -0
- package/dist/dashboard/server/metricsSerializer.js +195 -0
- package/dist/dashboard/server/routes/admin.routes.d.ts +16 -0
- package/dist/dashboard/server/routes/admin.routes.js +596 -0
- package/dist/dashboard/server/routes/alerts.routes.d.ts +7 -0
- package/dist/dashboard/server/routes/alerts.routes.js +93 -0
- package/dist/dashboard/server/routes/api.feedback.routes.d.ts +73 -0
- package/dist/dashboard/server/routes/api.feedback.routes.js +171 -0
- package/dist/dashboard/server/routes/api.instructions.routes.d.ts +101 -0
- package/dist/dashboard/server/routes/api.instructions.routes.js +213 -0
- package/dist/dashboard/server/routes/api.usage.routes.d.ts +57 -0
- package/dist/dashboard/server/routes/api.usage.routes.js +374 -0
- package/dist/dashboard/server/routes/embeddings.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/embeddings.routes.js +246 -0
- package/dist/dashboard/server/routes/graph.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/graph.routes.js +280 -0
- package/dist/dashboard/server/routes/index.d.ts +38 -0
- package/dist/dashboard/server/routes/index.js +194 -0
- package/dist/dashboard/server/routes/instances.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/instances.routes.js +35 -0
- package/dist/dashboard/server/routes/instructions.routes.d.ts +8 -0
- package/dist/dashboard/server/routes/instructions.routes.js +336 -0
- package/dist/dashboard/server/routes/knowledge.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/knowledge.routes.js +82 -0
- package/dist/dashboard/server/routes/logs.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/logs.routes.js +164 -0
- package/dist/dashboard/server/routes/messaging.routes.d.ts +16 -0
- package/dist/dashboard/server/routes/messaging.routes.js +293 -0
- package/dist/dashboard/server/routes/metrics.routes.d.ts +10 -0
- package/dist/dashboard/server/routes/metrics.routes.js +346 -0
- package/dist/dashboard/server/routes/scripts.routes.d.ts +9 -0
- package/dist/dashboard/server/routes/scripts.routes.js +84 -0
- package/dist/dashboard/server/routes/sqlite.routes.d.ts +9 -0
- package/dist/dashboard/server/routes/sqlite.routes.js +569 -0
- package/dist/dashboard/server/routes/status.routes.d.ts +7 -0
- package/dist/dashboard/server/routes/status.routes.js +183 -0
- package/dist/dashboard/server/routes/synthetic.routes.d.ts +7 -0
- package/dist/dashboard/server/routes/synthetic.routes.js +195 -0
- package/dist/dashboard/server/routes/tools.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/tools.routes.js +46 -0
- package/dist/dashboard/server/routes/usage.routes.d.ts +6 -0
- package/dist/dashboard/server/routes/usage.routes.js +25 -0
- package/dist/dashboard/server/wsInit.d.ts +16 -0
- package/dist/dashboard/server/wsInit.js +35 -0
- package/dist/externalClientLib.d.ts +1 -0
- package/dist/externalClientLib.js +2 -0
- package/dist/minimal/index.d.ts +1 -0
- package/dist/minimal/index.js +140 -0
- package/dist/models/SessionPersistence.d.ts +115 -0
- package/dist/models/SessionPersistence.js +66 -0
- package/dist/models/instruction.d.ts +45 -0
- package/dist/models/instruction.js +2 -0
- package/dist/perf/benchmark.d.ts +1 -0
- package/dist/perf/benchmark.js +50 -0
- package/dist/portableClientWrapper.d.ts +1 -0
- package/dist/portableClientWrapper.js +2 -0
- package/dist/schemas/index.d.ts +128 -0
- package/dist/schemas/index.js +371 -0
- package/dist/scripts/runPerformanceBaseline.d.ts +1 -0
- package/dist/scripts/runPerformanceBaseline.js +17 -0
- package/dist/server/handshakeManager.d.ts +25 -0
- package/dist/server/handshakeManager.js +472 -0
- package/dist/server/index-server.d.ts +56 -0
- package/dist/server/index-server.js +822 -0
- package/dist/server/registry.d.ts +44 -0
- package/dist/server/registry.js +236 -0
- package/dist/server/sdkServer.d.ts +8 -0
- package/dist/server/sdkServer.js +299 -0
- package/dist/server/shutdownGuard.d.ts +41 -0
- package/dist/server/shutdownGuard.js +52 -0
- package/dist/server/thin-client.d.ts +22 -0
- package/dist/server/thin-client.js +111 -0
- package/dist/server/transport.d.ts +41 -0
- package/dist/server/transport.js +312 -0
- package/dist/server/transportFactory.d.ts +21 -0
- package/dist/server/transportFactory.js +429 -0
- package/dist/services/atomicFs.d.ts +22 -0
- package/dist/services/atomicFs.js +103 -0
- package/dist/services/auditLog.d.ts +38 -0
- package/dist/services/auditLog.js +142 -0
- package/dist/services/autoBackup.d.ts +14 -0
- package/dist/services/autoBackup.js +171 -0
- package/dist/services/autoSplit.d.ts +32 -0
- package/dist/services/autoSplit.js +113 -0
- package/dist/services/backupZip.d.ts +25 -0
- package/dist/services/backupZip.js +110 -0
- package/dist/services/bootstrapGating.d.ts +123 -0
- package/dist/services/bootstrapGating.js +221 -0
- package/dist/services/canonical.d.ts +23 -0
- package/dist/services/canonical.js +65 -0
- package/dist/services/categoryRules.d.ts +7 -0
- package/dist/services/categoryRules.js +37 -0
- package/dist/services/classificationService.d.ts +42 -0
- package/dist/services/classificationService.js +168 -0
- package/dist/services/embeddingService.d.ts +62 -0
- package/dist/services/embeddingService.js +259 -0
- package/dist/services/errors.d.ts +22 -0
- package/dist/services/errors.js +31 -0
- package/dist/services/featureFlags.d.ts +25 -0
- package/dist/services/featureFlags.js +89 -0
- package/dist/services/features.d.ts +13 -0
- package/dist/services/features.js +35 -0
- package/dist/services/handlers/instructions.add.d.ts +1 -0
- package/dist/services/handlers/instructions.add.js +496 -0
- package/dist/services/handlers/instructions.groom.d.ts +1 -0
- package/dist/services/handlers/instructions.groom.js +523 -0
- package/dist/services/handlers/instructions.import.d.ts +1 -0
- package/dist/services/handlers/instructions.import.js +173 -0
- package/dist/services/handlers/instructions.patch.d.ts +1 -0
- package/dist/services/handlers/instructions.patch.js +167 -0
- package/dist/services/handlers/instructions.query.d.ts +163 -0
- package/dist/services/handlers/instructions.query.js +522 -0
- package/dist/services/handlers/instructions.reload.d.ts +1 -0
- package/dist/services/handlers/instructions.reload.js +13 -0
- package/dist/services/handlers/instructions.remove.d.ts +1 -0
- package/dist/services/handlers/instructions.remove.js +118 -0
- package/dist/services/handlers/instructions.shared.d.ts +31 -0
- package/dist/services/handlers/instructions.shared.js +124 -0
- package/dist/services/handlers.activation.d.ts +1 -0
- package/dist/services/handlers.activation.js +203 -0
- package/dist/services/handlers.bootstrap.d.ts +1 -0
- package/dist/services/handlers.bootstrap.js +38 -0
- package/dist/services/handlers.dashboardConfig.d.ts +34 -0
- package/dist/services/handlers.dashboardConfig.js +108 -0
- package/dist/services/handlers.diagnostics.d.ts +1 -0
- package/dist/services/handlers.diagnostics.js +64 -0
- package/dist/services/handlers.feedback.d.ts +15 -0
- package/dist/services/handlers.feedback.js +378 -0
- package/dist/services/handlers.gates.d.ts +1 -0
- package/dist/services/handlers.gates.js +46 -0
- package/dist/services/handlers.graph.d.ts +53 -0
- package/dist/services/handlers.graph.js +231 -0
- package/dist/services/handlers.help.d.ts +1 -0
- package/dist/services/handlers.help.js +119 -0
- package/dist/services/handlers.instructionSchema.d.ts +1 -0
- package/dist/services/handlers.instructionSchema.js +227 -0
- package/dist/services/handlers.instructions.d.ts +8 -0
- package/dist/services/handlers.instructions.js +14 -0
- package/dist/services/handlers.instructionsDiagnostics.d.ts +1 -0
- package/dist/services/handlers.instructionsDiagnostics.js +14 -0
- package/dist/services/handlers.integrity.d.ts +1 -0
- package/dist/services/handlers.integrity.js +35 -0
- package/dist/services/handlers.manifest.d.ts +1 -0
- package/dist/services/handlers.manifest.js +24 -0
- package/dist/services/handlers.messaging.d.ts +12 -0
- package/dist/services/handlers.messaging.js +203 -0
- package/dist/services/handlers.metrics.d.ts +1 -0
- package/dist/services/handlers.metrics.js +43 -0
- package/dist/services/handlers.promote.d.ts +1 -0
- package/dist/services/handlers.promote.js +306 -0
- package/dist/services/handlers.prompt.d.ts +1 -0
- package/dist/services/handlers.prompt.js +7 -0
- package/dist/services/handlers.search.d.ts +69 -0
- package/dist/services/handlers.search.js +645 -0
- package/dist/services/handlers.testPrimitive.d.ts +1 -0
- package/dist/services/handlers.testPrimitive.js +5 -0
- package/dist/services/handlers.trace.d.ts +1 -0
- package/dist/services/handlers.trace.js +31 -0
- package/dist/services/handlers.usage.d.ts +1 -0
- package/dist/services/handlers.usage.js +11 -0
- package/dist/services/hotScore.d.ts +137 -0
- package/dist/services/hotScore.js +244 -0
- package/dist/services/indexContext.d.ts +117 -0
- package/dist/services/indexContext.js +968 -0
- package/dist/services/indexLoader.d.ts +44 -0
- package/dist/services/indexLoader.js +921 -0
- package/dist/services/indexRepository.d.ts +32 -0
- package/dist/services/indexRepository.js +71 -0
- package/dist/services/indexingService.d.ts +1 -0
- package/dist/services/indexingService.js +2 -0
- package/dist/services/instructions.dispatcher.d.ts +1 -0
- package/dist/services/instructions.dispatcher.js +231 -0
- package/dist/services/logPrefix.d.ts +1 -0
- package/dist/services/logPrefix.js +30 -0
- package/dist/services/logger.d.ts +52 -0
- package/dist/services/logger.js +268 -0
- package/dist/services/manifestManager.d.ts +82 -0
- package/dist/services/manifestManager.js +200 -0
- package/dist/services/messaging/agentMailbox.d.ts +60 -0
- package/dist/services/messaging/agentMailbox.js +353 -0
- package/dist/services/messaging/messagingPersistence.d.ts +20 -0
- package/dist/services/messaging/messagingPersistence.js +111 -0
- package/dist/services/messaging/messagingTypes.d.ts +150 -0
- package/dist/services/messaging/messagingTypes.js +66 -0
- package/dist/services/ownershipService.d.ts +1 -0
- package/dist/services/ownershipService.js +38 -0
- package/dist/services/performanceBaseline.d.ts +19 -0
- package/dist/services/performanceBaseline.js +210 -0
- package/dist/services/preflight.d.ts +12 -0
- package/dist/services/preflight.js +79 -0
- package/dist/services/promptReviewService.d.ts +44 -0
- package/dist/services/promptReviewService.js +101 -0
- package/dist/services/responseEnvelope.d.ts +6 -0
- package/dist/services/responseEnvelope.js +25 -0
- package/dist/services/seedBootstrap.d.ts +34 -0
- package/dist/services/seedBootstrap.js +427 -0
- package/dist/services/storage/factory.d.ts +17 -0
- package/dist/services/storage/factory.js +35 -0
- package/dist/services/storage/hashUtils.d.ts +11 -0
- package/dist/services/storage/hashUtils.js +35 -0
- package/dist/services/storage/index.d.ts +12 -0
- package/dist/services/storage/index.js +18 -0
- package/dist/services/storage/jsonFileStore.d.ts +32 -0
- package/dist/services/storage/jsonFileStore.js +241 -0
- package/dist/services/storage/migrationEngine.d.ts +35 -0
- package/dist/services/storage/migrationEngine.js +93 -0
- package/dist/services/storage/sqliteMessageStore.d.ts +53 -0
- package/dist/services/storage/sqliteMessageStore.js +146 -0
- package/dist/services/storage/sqliteSchema.d.ts +12 -0
- package/dist/services/storage/sqliteSchema.js +122 -0
- package/dist/services/storage/sqliteStore.d.ts +41 -0
- package/dist/services/storage/sqliteStore.js +339 -0
- package/dist/services/storage/sqliteUsageStore.d.ts +35 -0
- package/dist/services/storage/sqliteUsageStore.js +94 -0
- package/dist/services/storage/types.d.ts +171 -0
- package/dist/services/storage/types.js +12 -0
- package/dist/services/toolHandlers.d.ts +23 -0
- package/dist/services/toolHandlers.js +50 -0
- package/dist/services/toolRegistry.d.ts +20 -0
- package/dist/services/toolRegistry.js +490 -0
- package/dist/services/toolRegistry.zod.d.ts +10 -0
- package/dist/services/toolRegistry.zod.js +323 -0
- package/dist/services/tracing.d.ts +26 -0
- package/dist/services/tracing.js +260 -0
- package/dist/services/usageBuckets.d.ts +161 -0
- package/dist/services/usageBuckets.js +364 -0
- package/dist/services/validationService.d.ts +38 -0
- package/dist/services/validationService.js +125 -0
- package/dist/utils/BufferRing.d.ts +203 -0
- package/dist/utils/BufferRing.js +551 -0
- package/dist/utils/BufferRingExamples.d.ts +55 -0
- package/dist/utils/BufferRingExamples.js +188 -0
- package/dist/utils/envUtils.d.ts +42 -0
- package/dist/utils/envUtils.js +80 -0
- package/dist/utils/memoryMonitor.d.ts +83 -0
- package/dist/utils/memoryMonitor.js +275 -0
- package/dist/versioning/schemaVersion.d.ts +6 -0
- package/dist/versioning/schemaVersion.js +93 -0
- package/package.json +134 -0
- package/schemas/README.md +13 -0
- package/schemas/feedback-entry.schema.json +27 -0
- package/schemas/graph-export-v2.schema.json +60 -0
- package/schemas/index-server.code-schema.json +38477 -0
- package/schemas/instruction.schema.json +262 -0
- package/schemas/json-schema/SessionPersistence-persisted-admin-session.schema.json +54 -0
- package/schemas/json-schema/SessionPersistence-persisted-session-history-entry.schema.json +51 -0
- package/schemas/json-schema/SessionPersistence-persisted-web-socket-connection.schema.json +54 -0
- package/schemas/json-schema/SessionPersistence-session-persistence-config.schema.json +110 -0
- package/schemas/json-schema/SessionPersistence-session-persistence-data.schema.json +229 -0
- package/schemas/json-schema/SessionPersistence-session-persistence-manifest.schema.json +109 -0
- package/schemas/json-schema/SessionPersistence-session-persistence-metadata.schema.json +55 -0
- package/schemas/json-schema/instruction-audience-scope.schema.json +14 -0
- package/schemas/json-schema/instruction-content-type.schema.json +17 -0
- package/schemas/json-schema/instruction-instruction-entry.schema.json +206 -0
- package/schemas/json-schema/instruction-requirement-level.schema.json +16 -0
- package/schemas/manifest.json +78 -0
- package/schemas/manifest.schema.json +33 -0
- package/schemas/usage-batch.schema.json +16 -0
- package/schemas/usage-buckets.schema.json +30 -0
- package/schemas/usage-event.schema.json +17 -0
- package/scripts/copy-dashboard-assets.mjs +170 -0
- package/scripts/setup-hooks.cjs +28 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Feature flag & metrics infrastructure (Phase 0)
|
|
3
|
+
// INDEX_SERVER_FEATURES=usage,window,hotness,drift,risk
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.hasFeature = hasFeature;
|
|
6
|
+
exports.incrementCounter = incrementCounter;
|
|
7
|
+
exports.getCounters = getCounters;
|
|
8
|
+
exports.featureStatus = featureStatus;
|
|
9
|
+
exports.enableFeature = enableFeature;
|
|
10
|
+
const runtimeConfig_1 = require("../config/runtimeConfig");
|
|
11
|
+
const BASE_FEATURES = Array.from((0, runtimeConfig_1.getRuntimeConfig)().featureFlags.indexFeatures);
|
|
12
|
+
const SET = new Set(BASE_FEATURES);
|
|
13
|
+
function hasFeature(name) { return SET.has(name); }
|
|
14
|
+
// Simple in-memory counters (could be exposed via metrics_snapshot already existing)
|
|
15
|
+
const counters = {};
|
|
16
|
+
function incrementCounter(name, by = 1) { counters[name] = (counters[name] || 0) + by; }
|
|
17
|
+
function getCounters() { return { ...counters }; }
|
|
18
|
+
// Record activation counts once at module load
|
|
19
|
+
for (const f of SET) {
|
|
20
|
+
incrementCounter(`featureActivated:${f}`);
|
|
21
|
+
}
|
|
22
|
+
function featureStatus() {
|
|
23
|
+
return {
|
|
24
|
+
features: Array.from(SET.values()).sort(),
|
|
25
|
+
counters: getCounters(),
|
|
26
|
+
env: BASE_FEATURES.length ? [...BASE_FEATURES] : [],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
// Test helper / dynamic enable (used in tests to activate features post-initial load)
|
|
30
|
+
function enableFeature(name) {
|
|
31
|
+
if (!SET.has(name)) {
|
|
32
|
+
SET.add(name);
|
|
33
|
+
incrementCounter(`featureActivated:${name}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
const registry_1 = require("../../server/registry");
|
|
10
|
+
const indexContext_1 = require("../indexContext");
|
|
11
|
+
const features_1 = require("../features");
|
|
12
|
+
const schemaVersion_1 = require("../../versioning/schemaVersion");
|
|
13
|
+
const classificationService_1 = require("../classificationService");
|
|
14
|
+
const ownershipService_1 = require("../ownershipService");
|
|
15
|
+
const atomicFs_1 = require("../atomicFs");
|
|
16
|
+
const auditLog_1 = require("../auditLog");
|
|
17
|
+
const toolRegistry_1 = require("../toolRegistry");
|
|
18
|
+
const runtimeConfig_1 = require("../../config/runtimeConfig");
|
|
19
|
+
const canonical_1 = require("../canonical");
|
|
20
|
+
const manifestManager_1 = require("../manifestManager");
|
|
21
|
+
const tracing_1 = require("../tracing");
|
|
22
|
+
const instructions_shared_1 = require("./instructions.shared");
|
|
23
|
+
(0, registry_1.registerHandler)('index_add', (0, instructions_shared_1.guard)('index_add', (p) => {
|
|
24
|
+
const e = p.entry;
|
|
25
|
+
const instructionsCfg = (0, runtimeConfig_1.getRuntimeConfig)().instructions;
|
|
26
|
+
const SEMVER_REGEX = /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:[-+].*)?$/;
|
|
27
|
+
const ADD_INPUT_SCHEMA = (0, toolRegistry_1.getToolRegistry)({ tier: 'admin' }).find(t => t.name === 'index_add')?.inputSchema;
|
|
28
|
+
const fail = (error, opts) => {
|
|
29
|
+
const id = opts?.id || (e && e.id) || 'unknown';
|
|
30
|
+
const rawBody = e && typeof e.body === 'string' ? e.body : (e && e.body ? String(e.body) : '');
|
|
31
|
+
const bodyPreview = rawBody.trim().slice(0, 200);
|
|
32
|
+
const reproEntry = e ? {
|
|
33
|
+
id,
|
|
34
|
+
title: e.title || id,
|
|
35
|
+
requirement: e.requirement,
|
|
36
|
+
priorityTier: e.priorityTier,
|
|
37
|
+
owner: e.owner,
|
|
38
|
+
bodyPreview
|
|
39
|
+
} : { id };
|
|
40
|
+
const base = {
|
|
41
|
+
id,
|
|
42
|
+
created: false,
|
|
43
|
+
overwritten: false,
|
|
44
|
+
skipped: false,
|
|
45
|
+
error,
|
|
46
|
+
hash: opts?.hash,
|
|
47
|
+
feedbackHint: 'Creation failed. If unexpected, call feedback_submit with reproEntry.',
|
|
48
|
+
reproEntry
|
|
49
|
+
};
|
|
50
|
+
if (/^missing (entry|id|required fields)/.test(error) || error === 'missing required fields') {
|
|
51
|
+
if (ADD_INPUT_SCHEMA) {
|
|
52
|
+
base.schemaRef = "meta_tools[name='index_add'].inputSchema";
|
|
53
|
+
base.inputSchema = ADD_INPUT_SCHEMA;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
base.schemaRef = 'meta_tools (lookup index_add)';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return base;
|
|
60
|
+
};
|
|
61
|
+
if (!e)
|
|
62
|
+
return fail('missing entry');
|
|
63
|
+
const lax = !!(p.lax || e?.lax);
|
|
64
|
+
if (lax) {
|
|
65
|
+
if (!e.id)
|
|
66
|
+
return fail('missing id');
|
|
67
|
+
const mutable = e;
|
|
68
|
+
if (!mutable.title)
|
|
69
|
+
mutable.title = mutable.id;
|
|
70
|
+
if (typeof mutable.priority !== 'number')
|
|
71
|
+
mutable.priority = 50;
|
|
72
|
+
if (!mutable.audience)
|
|
73
|
+
mutable.audience = 'all';
|
|
74
|
+
if (!mutable.requirement)
|
|
75
|
+
mutable.requirement = 'optional';
|
|
76
|
+
if (!Array.isArray(mutable.categories))
|
|
77
|
+
mutable.categories = [];
|
|
78
|
+
}
|
|
79
|
+
if (p.overwrite && (!e.body || !e.title)) {
|
|
80
|
+
try {
|
|
81
|
+
const dirCandidate = (0, indexContext_1.getInstructionsDir)();
|
|
82
|
+
const fileCandidate = path_1.default.join(dirCandidate, `${e.id}.json`);
|
|
83
|
+
if (fs_1.default.existsSync(fileCandidate)) {
|
|
84
|
+
try {
|
|
85
|
+
const raw = JSON.parse(fs_1.default.readFileSync(fileCandidate, 'utf8'));
|
|
86
|
+
if (raw) {
|
|
87
|
+
const mutableExisting = e;
|
|
88
|
+
if (!mutableExisting.body && typeof raw.body === 'string' && raw.body.trim()) {
|
|
89
|
+
mutableExisting.body = raw.body;
|
|
90
|
+
}
|
|
91
|
+
if (!mutableExisting.title && typeof raw.title === 'string' && raw.title.trim()) {
|
|
92
|
+
mutableExisting.title = raw.title;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch { /* ignore parse */ }
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch { /* ignore hydration errors */ }
|
|
100
|
+
}
|
|
101
|
+
if (!e.id || !e.title || !e.body)
|
|
102
|
+
return fail('missing required fields');
|
|
103
|
+
const dir = (0, indexContext_1.getInstructionsDir)();
|
|
104
|
+
if (!fs_1.default.existsSync(dir))
|
|
105
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
106
|
+
const file = path_1.default.join(dir, `${e.id}.json`);
|
|
107
|
+
const exists = fs_1.default.existsSync(file);
|
|
108
|
+
const existedBeforeOriginal = exists;
|
|
109
|
+
const overwrite = !!p.overwrite;
|
|
110
|
+
if (exists && !overwrite) {
|
|
111
|
+
let st0 = (0, indexContext_1.ensureLoaded)();
|
|
112
|
+
let visible = st0.byId.has(e.id);
|
|
113
|
+
let repaired = false;
|
|
114
|
+
if (!visible) {
|
|
115
|
+
try {
|
|
116
|
+
(0, indexContext_1.invalidate)();
|
|
117
|
+
st0 = (0, indexContext_1.ensureLoaded)();
|
|
118
|
+
visible = st0.byId.has(e.id);
|
|
119
|
+
if (visible)
|
|
120
|
+
repaired = true;
|
|
121
|
+
}
|
|
122
|
+
catch { /* ignore reload */ }
|
|
123
|
+
if (!visible) {
|
|
124
|
+
const filePath = file;
|
|
125
|
+
if (fs_1.default.existsSync(filePath)) {
|
|
126
|
+
try {
|
|
127
|
+
const rawTxt = fs_1.default.readFileSync(filePath, 'utf8');
|
|
128
|
+
if (rawTxt.trim()) {
|
|
129
|
+
const rawJson = JSON.parse(rawTxt);
|
|
130
|
+
const classifier = new classificationService_1.ClassificationService();
|
|
131
|
+
const issues = classifier.validate(rawJson);
|
|
132
|
+
if (!issues.length) {
|
|
133
|
+
const norm = classifier.normalize(rawJson);
|
|
134
|
+
st0.list.push(norm);
|
|
135
|
+
st0.byId.set(norm.id, norm);
|
|
136
|
+
visible = true;
|
|
137
|
+
repaired = true;
|
|
138
|
+
(0, features_1.incrementCounter)('instructions:addSkipLateMaterialize');
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
(0, features_1.incrementCounter)('instructions:addSkipLateMaterializeRejected');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
(0, features_1.incrementCounter)('instructions:addSkipLateMaterializeEmpty');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
(0, features_1.incrementCounter)('instructions:addSkipLateMaterializeParseError');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
(0, auditLog_1.logAudit)('add', e.id, { skipped: true, late_visible: visible, repaired });
|
|
155
|
+
if ((0, instructions_shared_1.traceVisibility)()) {
|
|
156
|
+
(0, tracing_1.emitTrace)('[trace:add:skip]', { id: e.id, visible, repaired });
|
|
157
|
+
}
|
|
158
|
+
if ((0, instructions_shared_1.traceVisibility)()) {
|
|
159
|
+
(0, instructions_shared_1.traceInstructionVisibility)(e.id, 'add-skip-pre-return', { visible, repaired });
|
|
160
|
+
if (!visible)
|
|
161
|
+
(0, instructions_shared_1.traceEnvSnapshot)('add-skip-anomalous', { repaired });
|
|
162
|
+
}
|
|
163
|
+
if (!visible) {
|
|
164
|
+
return { id: e.id, skipped: true, created: false, overwritten: false, hash: st0.hash, visibilityWarning: 'skipped_file_not_in_index' };
|
|
165
|
+
}
|
|
166
|
+
return { id: e.id, skipped: true, created: false, overwritten: false, hash: st0.hash, repaired: repaired ? true : undefined };
|
|
167
|
+
}
|
|
168
|
+
const now = new Date().toISOString();
|
|
169
|
+
const rawBody = typeof e.body === 'string' ? e.body : String(e.body || '');
|
|
170
|
+
const bodyTrimmed = rawBody.trim();
|
|
171
|
+
const { bodyMaxLength } = (0, runtimeConfig_1.getRuntimeConfig)().index;
|
|
172
|
+
if (bodyTrimmed.length > bodyMaxLength) {
|
|
173
|
+
return {
|
|
174
|
+
...fail('body_too_large', { id: e.id }),
|
|
175
|
+
bodyLength: bodyTrimmed.length,
|
|
176
|
+
maxLength: bodyMaxLength,
|
|
177
|
+
guidance: `Body exceeds the ${bodyMaxLength}-character limit (${bodyTrimmed.length} chars). Please split into multiple cross-linked instructions, refine/compress content, or categorize sections as separate entries. Use categories and cross-references (e.g., "See also: <sibling-id>") to maintain discoverability.`
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
let categories = Array.from(new Set((Array.isArray(e.categories) ? e.categories : []).filter((c) => typeof c === 'string' && c.trim().length > 0).map(c => c.toLowerCase()))).sort();
|
|
181
|
+
if (!categories.length) {
|
|
182
|
+
const allowAuto = lax || !instructionsCfg.requireCategory;
|
|
183
|
+
if (allowAuto) {
|
|
184
|
+
categories = ['uncategorized'];
|
|
185
|
+
if ((0, instructions_shared_1.traceVisibility)())
|
|
186
|
+
(0, tracing_1.emitTrace)('[trace:add:auto-category]', { id: e.id });
|
|
187
|
+
(0, features_1.incrementCounter)('instructions:autoCategory');
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
return fail('category_required', { id: e.id });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const suppliedPrimary = e.primaryCategory;
|
|
194
|
+
const primaryCategory = (suppliedPrimary && categories.includes(suppliedPrimary.toLowerCase())) ? suppliedPrimary.toLowerCase() : categories[0];
|
|
195
|
+
const sourceHash = instructionsCfg.canonicalDisable
|
|
196
|
+
? crypto_1.default.createHash('sha256').update(bodyTrimmed, 'utf8').digest('hex')
|
|
197
|
+
: (0, canonical_1.hashBody)(rawBody);
|
|
198
|
+
if (e.priorityTier === 'P1' && (!categories.length || !e.owner))
|
|
199
|
+
return fail('P1 requires category & owner', { id: e.id });
|
|
200
|
+
if ((e.requirement === 'mandatory' || e.requirement === 'critical') && !e.owner)
|
|
201
|
+
return fail('mandatory/critical require owner', { id: e.id });
|
|
202
|
+
const classifier = new classificationService_1.ClassificationService();
|
|
203
|
+
let base;
|
|
204
|
+
if (exists) {
|
|
205
|
+
try {
|
|
206
|
+
const existing = JSON.parse(fs_1.default.readFileSync(file, 'utf8'));
|
|
207
|
+
base = { ...existing };
|
|
208
|
+
const prevBody = existing.body;
|
|
209
|
+
const prevVersion = existing.version || '1.0.0';
|
|
210
|
+
if (e.title)
|
|
211
|
+
base.title = e.title;
|
|
212
|
+
if (e.body)
|
|
213
|
+
base.body = bodyTrimmed;
|
|
214
|
+
if (e.rationale !== undefined)
|
|
215
|
+
base.rationale = e.rationale;
|
|
216
|
+
if (typeof e.priority === 'number')
|
|
217
|
+
base.priority = e.priority;
|
|
218
|
+
if (e.audience)
|
|
219
|
+
base.audience = e.audience;
|
|
220
|
+
if (e.requirement)
|
|
221
|
+
base.requirement = e.requirement;
|
|
222
|
+
if (categories.length) {
|
|
223
|
+
base.categories = categories;
|
|
224
|
+
base.primaryCategory = primaryCategory;
|
|
225
|
+
}
|
|
226
|
+
base.updatedAt = now;
|
|
227
|
+
if (e.version !== undefined)
|
|
228
|
+
base.version = e.version;
|
|
229
|
+
if (e.changeLog !== undefined)
|
|
230
|
+
base.changeLog = e.changeLog;
|
|
231
|
+
const semverRegex = SEMVER_REGEX;
|
|
232
|
+
const parse = (v) => { const m = semverRegex.exec(v); if (!m)
|
|
233
|
+
return null; return { major: +m[1], minor: +m[2], patch: +m[3] }; };
|
|
234
|
+
const gt = (a, b) => { const pa = parse(a), pb = parse(b); if (!pa || !pb)
|
|
235
|
+
return false; if (pa.major !== pb.major)
|
|
236
|
+
return pa.major > pb.major; if (pa.minor !== pb.minor)
|
|
237
|
+
return pa.minor > pb.minor; return pa.patch > pb.patch; };
|
|
238
|
+
const bodyChanged = e.body ? (bodyTrimmed !== prevBody) : false;
|
|
239
|
+
const titleChanged = e.title !== undefined && e.title !== existing.title;
|
|
240
|
+
const eRec = e;
|
|
241
|
+
const ownerChanged = eRec.owner !== undefined && eRec.owner !== existing.owner;
|
|
242
|
+
const semanticSummaryChanged = eRec.semanticSummary !== undefined && eRec.semanticSummary !== existing.semanticSummary;
|
|
243
|
+
const classificationChanged = eRec.classification !== undefined && eRec.classification !== existing.classification;
|
|
244
|
+
const versionChanged = e.version !== undefined && e.version !== existing.version;
|
|
245
|
+
const categoriesChanged = categories.length > 0 && JSON.stringify(categories.sort()) !== JSON.stringify((existing.categories || []).sort());
|
|
246
|
+
const governanceMetaChanged = titleChanged || ownerChanged || semanticSummaryChanged || classificationChanged || versionChanged || categoriesChanged;
|
|
247
|
+
if (overwrite && !bodyChanged && !governanceMetaChanged) {
|
|
248
|
+
const stNoop = (0, indexContext_1.ensureLoaded)();
|
|
249
|
+
const respNoop = { id: e.id, created: false, overwritten: false, skipped: true, hash: stNoop.hash, verified: true };
|
|
250
|
+
if (instructionsCfg.strictCreate)
|
|
251
|
+
respNoop.strictVerified = true;
|
|
252
|
+
(0, auditLog_1.logAudit)('add', e.id, { created: false, overwritten: false, skipped: true, verified: true, noop: true });
|
|
253
|
+
if ((0, instructions_shared_1.traceVisibility)())
|
|
254
|
+
(0, tracing_1.emitTrace)('[trace:add:noop-overwrite]', { id: e.id, hash: stNoop.hash, reason: 'no body/governance delta' });
|
|
255
|
+
return respNoop;
|
|
256
|
+
}
|
|
257
|
+
let incomingVersion = e.version;
|
|
258
|
+
if (incomingVersion && !semverRegex.test(incomingVersion))
|
|
259
|
+
return fail('invalid_semver', { id: e.id });
|
|
260
|
+
if (bodyChanged) {
|
|
261
|
+
if (incomingVersion) {
|
|
262
|
+
if (!gt(incomingVersion, prevVersion))
|
|
263
|
+
return fail('version_not_bumped', { id: e.id });
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
const pv = parse(prevVersion) || { major: 1, minor: 0, patch: 0 };
|
|
267
|
+
const autoVersion = `${pv.major}.${pv.minor}.${pv.patch + 1}`;
|
|
268
|
+
base.version = autoVersion;
|
|
269
|
+
incomingVersion = autoVersion;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else if (incomingVersion) {
|
|
273
|
+
if (!gt(incomingVersion, prevVersion))
|
|
274
|
+
return fail('version_not_bumped', { id: e.id });
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
base.version = prevVersion;
|
|
278
|
+
incomingVersion = prevVersion;
|
|
279
|
+
}
|
|
280
|
+
if (!Array.isArray(base.changeLog) || !base.changeLog.length) {
|
|
281
|
+
base.changeLog = [{ version: prevVersion, changedAt: existing.createdAt || now, summary: 'initial import' }];
|
|
282
|
+
}
|
|
283
|
+
const finalVersion = base.version || incomingVersion || prevVersion;
|
|
284
|
+
const last = base.changeLog[base.changeLog.length - 1];
|
|
285
|
+
if (last.version !== finalVersion) {
|
|
286
|
+
const summary = bodyChanged ? (e.version ? 'body update' : 'auto bump (body change)') : 'metadata update';
|
|
287
|
+
base.changeLog.push({ version: finalVersion, changedAt: now, summary });
|
|
288
|
+
}
|
|
289
|
+
const repairChangeLog = (cl) => {
|
|
290
|
+
const out = [];
|
|
291
|
+
if (Array.isArray(cl)) {
|
|
292
|
+
for (const entry of cl) {
|
|
293
|
+
if (!entry || typeof entry !== 'object')
|
|
294
|
+
continue;
|
|
295
|
+
const { version: v, changedAt: ca, summary: sum } = entry;
|
|
296
|
+
if (typeof v === 'string' && v.trim() && typeof sum === 'string' && sum.trim()) {
|
|
297
|
+
const caIso = typeof ca === 'string' && /T/.test(ca) ? ca : now;
|
|
298
|
+
out.push({ version: v.trim(), changedAt: caIso, summary: sum.trim() });
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (!out.length) {
|
|
303
|
+
out.push({ version: prevVersion, changedAt: existing.createdAt || now, summary: 'initial import (repaired)' });
|
|
304
|
+
}
|
|
305
|
+
const lastVer = out[out.length - 1].version;
|
|
306
|
+
if (lastVer !== finalVersion) {
|
|
307
|
+
out.push({ version: finalVersion, changedAt: now, summary: bodyChanged ? 'body update (repaired)' : 'metadata update (repaired)' });
|
|
308
|
+
}
|
|
309
|
+
return out;
|
|
310
|
+
};
|
|
311
|
+
base.changeLog = repairChangeLog(base.changeLog);
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
base = { id: e.id, title: e.title, body: bodyTrimmed, rationale: e.rationale, priority: e.priority, audience: e.audience, requirement: e.requirement, categories, primaryCategory, sourceHash, schemaVersion: schemaVersion_1.SCHEMA_VERSION, deprecatedBy: e.deprecatedBy, createdAt: now, updatedAt: now, riskScore: e.riskScore, createdByAgent: instructionsCfg.agentId, sourceWorkspace: instructionsCfg.workspaceId };
|
|
315
|
+
base.version = '1.0.0';
|
|
316
|
+
base.changeLog = [{ version: '1.0.0', changedAt: now, summary: 'initial import' }];
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
base = { id: e.id, title: e.title, body: bodyTrimmed, rationale: e.rationale, priority: e.priority, audience: e.audience, requirement: e.requirement, categories, primaryCategory, sourceHash, schemaVersion: schemaVersion_1.SCHEMA_VERSION, deprecatedBy: e.deprecatedBy, createdAt: now, updatedAt: now, riskScore: e.riskScore, createdByAgent: instructionsCfg.agentId, sourceWorkspace: instructionsCfg.workspaceId };
|
|
321
|
+
if (e.version !== undefined) {
|
|
322
|
+
if (!SEMVER_REGEX.test(e.version))
|
|
323
|
+
return fail('invalid_semver', { id: e.id });
|
|
324
|
+
base.version = e.version;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
base.version = '1.0.0';
|
|
328
|
+
}
|
|
329
|
+
if (!Array.isArray(base.changeLog) || !base.changeLog.length) {
|
|
330
|
+
base.changeLog = [{ version: base.version, changedAt: now, summary: 'initial import' }];
|
|
331
|
+
}
|
|
332
|
+
if (Array.isArray(base.changeLog)) {
|
|
333
|
+
const repaired = [];
|
|
334
|
+
for (const entry of base.changeLog) {
|
|
335
|
+
if (!entry || typeof entry !== 'object')
|
|
336
|
+
continue;
|
|
337
|
+
const { version: v, changedAt: ca, summary: sum } = entry;
|
|
338
|
+
if (typeof v === 'string' && v.trim() && typeof sum === 'string' && sum.trim()) {
|
|
339
|
+
const caIso = typeof ca === 'string' && /T/.test(ca) ? ca : now;
|
|
340
|
+
repaired.push({ version: v.trim(), changedAt: caIso, summary: sum.trim() });
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (!repaired.length) {
|
|
344
|
+
repaired.push({ version: base.version, changedAt: now, summary: 'initial import (repaired)' });
|
|
345
|
+
}
|
|
346
|
+
if (repaired[repaired.length - 1].version !== base.version) {
|
|
347
|
+
repaired.push({ version: base.version, changedAt: now, summary: 'initial import (normalized)' });
|
|
348
|
+
}
|
|
349
|
+
base.changeLog = repaired;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const govKeys = ['version', 'owner', 'status', 'priorityTier', 'classification', 'lastReviewedAt', 'nextReviewDue', 'semanticSummary', 'contentType'];
|
|
353
|
+
for (const k of govKeys) {
|
|
354
|
+
const v = e[k];
|
|
355
|
+
if (v !== undefined) {
|
|
356
|
+
base[k] = v;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (!exists || base.body === bodyTrimmed) {
|
|
360
|
+
base.sourceHash = sourceHash;
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
base.sourceHash = instructionsCfg.canonicalDisable
|
|
364
|
+
? crypto_1.default.createHash('sha256').update(base.body, 'utf8').digest('hex')
|
|
365
|
+
: (0, canonical_1.hashBody)(base.body);
|
|
366
|
+
}
|
|
367
|
+
const record = classifier.normalize(base);
|
|
368
|
+
if (record.owner === 'unowned') {
|
|
369
|
+
const auto = (0, ownershipService_1.resolveOwner)(record.id);
|
|
370
|
+
if (auto) {
|
|
371
|
+
record.owner = auto;
|
|
372
|
+
record.updatedAt = new Date().toISOString();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
(0, atomicFs_1.atomicWriteJson)(file, record);
|
|
377
|
+
}
|
|
378
|
+
catch (err) {
|
|
379
|
+
return fail(err.message || 'write-failed', { id: e.id });
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
(0, indexContext_1.touchIndexVersion)();
|
|
383
|
+
}
|
|
384
|
+
catch { /* ignore */ }
|
|
385
|
+
let stReloaded;
|
|
386
|
+
const strictMode = instructionsCfg.strictVisibility;
|
|
387
|
+
if (strictMode) {
|
|
388
|
+
try {
|
|
389
|
+
const current = (0, indexContext_1.ensureLoaded)();
|
|
390
|
+
stReloaded = current;
|
|
391
|
+
if (!current.byId.has(record.id)) {
|
|
392
|
+
current.byId.set(record.id, record);
|
|
393
|
+
current.list.push(record);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
catch { /* fallback to reload path below if anything fails */ }
|
|
397
|
+
}
|
|
398
|
+
if (!stReloaded) {
|
|
399
|
+
try {
|
|
400
|
+
(0, indexContext_1.invalidate)();
|
|
401
|
+
}
|
|
402
|
+
catch { /* ignore */ }
|
|
403
|
+
stReloaded = (0, indexContext_1.ensureLoaded)();
|
|
404
|
+
}
|
|
405
|
+
const createdNow = !existedBeforeOriginal;
|
|
406
|
+
const overwrittenNow = overwrite && existedBeforeOriginal;
|
|
407
|
+
let strictVerified = false;
|
|
408
|
+
const verifyIssues = [];
|
|
409
|
+
try {
|
|
410
|
+
let diskRaw;
|
|
411
|
+
let parsed;
|
|
412
|
+
try {
|
|
413
|
+
diskRaw = fs_1.default.readFileSync(file, 'utf8');
|
|
414
|
+
}
|
|
415
|
+
catch (ex) {
|
|
416
|
+
verifyIssues.push('read-failed:' + ex.message);
|
|
417
|
+
}
|
|
418
|
+
if (diskRaw) {
|
|
419
|
+
try {
|
|
420
|
+
parsed = JSON.parse(diskRaw);
|
|
421
|
+
}
|
|
422
|
+
catch (ex) {
|
|
423
|
+
verifyIssues.push('parse-failed:' + ex.message);
|
|
424
|
+
}
|
|
425
|
+
if (parsed) {
|
|
426
|
+
if (parsed.id !== e.id)
|
|
427
|
+
verifyIssues.push('id-mismatch');
|
|
428
|
+
if (!parsed.title)
|
|
429
|
+
verifyIssues.push('missing-title');
|
|
430
|
+
if (!parsed.body)
|
|
431
|
+
verifyIssues.push('missing-body');
|
|
432
|
+
const wantCats = Array.isArray(e.categories) ? e.categories.filter((c) => typeof c === 'string').map(c => c.toLowerCase()) : [];
|
|
433
|
+
if (wantCats.length) {
|
|
434
|
+
for (const c of wantCats) {
|
|
435
|
+
if (!parsed.categories?.includes(c)) {
|
|
436
|
+
verifyIssues.push('missing-category:' + c);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const mem = stReloaded.byId.get(e.id);
|
|
443
|
+
if (!mem) {
|
|
444
|
+
verifyIssues.push('not-in-index');
|
|
445
|
+
}
|
|
446
|
+
const wantCats2 = Array.isArray(e.categories) ? e.categories.filter((c) => typeof c === 'string').map(c => c.toLowerCase()) : [];
|
|
447
|
+
if (wantCats2.length) {
|
|
448
|
+
const catHit = stReloaded.list.some(rec => rec.id === e.id && wantCats2.every(c => rec.categories.includes(c)));
|
|
449
|
+
if (!catHit)
|
|
450
|
+
verifyIssues.push('category-query-miss');
|
|
451
|
+
}
|
|
452
|
+
try {
|
|
453
|
+
if (parsed) {
|
|
454
|
+
const classifier2 = new classificationService_1.ClassificationService();
|
|
455
|
+
const issues = classifier2.validate(parsed);
|
|
456
|
+
if (issues.length) {
|
|
457
|
+
verifyIssues.push('classification-issues:' + issues.join(','));
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
catch (err) {
|
|
462
|
+
verifyIssues.push('classification-exception:' + err.message);
|
|
463
|
+
}
|
|
464
|
+
if (verifyIssues.includes('not-in-index')) {
|
|
465
|
+
try {
|
|
466
|
+
(0, indexContext_1.invalidate)();
|
|
467
|
+
const st2 = (0, indexContext_1.ensureLoaded)();
|
|
468
|
+
if (st2.byId.has(e.id)) {
|
|
469
|
+
const idx = verifyIssues.indexOf('not-in-index');
|
|
470
|
+
if (idx >= 0)
|
|
471
|
+
verifyIssues.splice(idx, 1);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
catch { /* ignore */ }
|
|
475
|
+
}
|
|
476
|
+
if (!verifyIssues.length)
|
|
477
|
+
strictVerified = true;
|
|
478
|
+
}
|
|
479
|
+
catch (err) {
|
|
480
|
+
verifyIssues.push('verify-exception:' + err.message);
|
|
481
|
+
}
|
|
482
|
+
try {
|
|
483
|
+
if (instructionsCfg.manifest.writeEnabled)
|
|
484
|
+
(0, manifestManager_1.writeManifestFromIndex)();
|
|
485
|
+
else
|
|
486
|
+
setImmediate(() => { try {
|
|
487
|
+
(0, manifestManager_1.attemptManifestUpdate)();
|
|
488
|
+
}
|
|
489
|
+
catch { /* ignore */ } });
|
|
490
|
+
}
|
|
491
|
+
catch { /* ignore manifest */ }
|
|
492
|
+
(0, auditLog_1.logAudit)('add', e.id, { created: createdNow, overwritten: overwrittenNow, verified: true, forcedReload: true });
|
|
493
|
+
if ((0, instructions_shared_1.traceVisibility)())
|
|
494
|
+
(0, tracing_1.emitTrace)('[trace:add:forced-reload]', { id: e.id, created: createdNow, overwritten: overwrittenNow, hash: stReloaded.hash, strictVerified, issues: verifyIssues.slice(0, 5), strictMode });
|
|
495
|
+
return { id: e.id, created: createdNow, overwritten: overwrittenNow, skipped: false, hash: stReloaded.hash, verified: true, strictVerified, verifyIssues: verifyIssues.length ? verifyIssues : undefined, strictMode, bodyLength: bodyTrimmed.length };
|
|
496
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|