harden struct
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Index;
|
||||
@@ -8,14 +7,14 @@ namespace App\Index;
|
||||
final class IndexMetaManager
|
||||
{
|
||||
private string $metaPath;
|
||||
private IndexConfiguration $config;
|
||||
|
||||
public function __construct(
|
||||
string $projectDir,
|
||||
private readonly IndexConfiguration $config,
|
||||
string $relativeMetaPath = '/var/knowledge/index_meta.json'
|
||||
)
|
||||
{
|
||||
$this->metaPath = rtrim($projectDir, '/') . $relativeMetaPath;
|
||||
string $metaPath,
|
||||
IndexConfiguration $config
|
||||
) {
|
||||
$this->metaPath = $metaPath;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function getMetaPath(): string
|
||||
@@ -24,8 +23,6 @@ final class IndexMetaManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt null zurück, wenn noch kein Meta existiert (frisches System).
|
||||
*
|
||||
* @return array<string,mixed>|null
|
||||
*/
|
||||
public function readMeta(): ?array
|
||||
@@ -48,43 +45,19 @@ final class IndexMetaManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt Meta, falls nicht vorhanden (z. B. nach erstem Global Reindex).
|
||||
* Überschreibt NICHT automatisch, wenn vorhanden.
|
||||
*
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function createInitialMetaIfMissing(): array
|
||||
{
|
||||
$existing = $this->readMeta();
|
||||
if ($existing !== null) {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$meta = $this->buildMetaPayload(indexVersion: 1);
|
||||
$this->atomicWriteJson($meta);
|
||||
|
||||
return $meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Guardrail: Prüft, ob die aktuelle Config kompatibel zur gespeicherten Meta ist.
|
||||
* Wenn nicht: IndexStructureChangedException -> Global Reindex erzwingen.
|
||||
* Guardrail:
|
||||
* - Wenn Meta fehlt → initialisieren
|
||||
* - Wenn Struktur driftet → Exception
|
||||
*/
|
||||
public function validateAgainstCurrent(): void
|
||||
{
|
||||
$meta = $this->readMeta();
|
||||
|
||||
// Wenn noch kein Meta existiert, lassen wir lokale Ingests NICHT einfach laufen.
|
||||
// Governance: Erst Global Reindex erzeugt Meta sauber.
|
||||
if ($meta === null) {
|
||||
throw new IndexStructureChangedException(
|
||||
'index_meta.json missing. Please run a Global Reindex to initialize index structure metadata.',
|
||||
['reason' => 'missing_meta']
|
||||
);
|
||||
$meta = $this->createInitialMeta();
|
||||
}
|
||||
|
||||
$expected = $this->config->toStructureArray();
|
||||
|
||||
$diff = $this->diffStructure($meta, $expected);
|
||||
|
||||
if ($diff !== []) {
|
||||
@@ -96,11 +69,7 @@ final class IndexMetaManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Wird beim Global Reindex verwendet:
|
||||
* - index_version++ (oder initialisieren)
|
||||
* - Meta atomar schreiben
|
||||
*
|
||||
* @return array<string,mixed> new meta
|
||||
* Wird beim Global Reindex aufgerufen
|
||||
*/
|
||||
public function writeMetaForGlobalReindex(): array
|
||||
{
|
||||
@@ -122,35 +91,34 @@ final class IndexMetaManager
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
// -------------------------
|
||||
// ---------------------------------------------------------
|
||||
// Internals
|
||||
// -------------------------
|
||||
// ---------------------------------------------------------
|
||||
|
||||
private function createInitialMeta(): array
|
||||
{
|
||||
$meta = $this->buildMetaPayload(1);
|
||||
$this->atomicWriteJson($meta);
|
||||
return $meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private function buildMetaPayload(int $indexVersion): array
|
||||
{
|
||||
$structure = $this->config->toStructureArray();
|
||||
|
||||
return [
|
||||
'index_version' => $indexVersion,
|
||||
'created_at' => (new \DateTimeImmutable())->format(DATE_ATOM),
|
||||
'embedding_model' => $structure['embedding_model'],
|
||||
'index_version' => $indexVersion,
|
||||
'created_at' => (new \DateTimeImmutable())->format(DATE_ATOM),
|
||||
'embedding_model' => $structure['embedding_model'],
|
||||
'embedding_dimension' => $structure['embedding_dimension'],
|
||||
'chunk_size' => $structure['chunk_size'],
|
||||
'chunk_overlap' => $structure['chunk_overlap'],
|
||||
'scoring_version' => $structure['scoring_version'],
|
||||
'index_format' => $structure['index_format'],
|
||||
'vector_backend' => $structure['vector_backend'],
|
||||
'chunk_size' => $structure['chunk_size'],
|
||||
'chunk_overlap' => $structure['chunk_overlap'],
|
||||
'scoring_version' => $structure['scoring_version'],
|
||||
'index_format' => $structure['index_format'],
|
||||
'vector_backend' => $structure['vector_backend'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $meta
|
||||
* @param array<string,mixed> $expected
|
||||
* @return array<string,mixed> diff
|
||||
*/
|
||||
private function diffStructure(array $meta, array $expected): array
|
||||
{
|
||||
$diff = [];
|
||||
@@ -160,28 +128,18 @@ final class IndexMetaManager
|
||||
if ($actual !== $value) {
|
||||
$diff[$key] = [
|
||||
'expected' => $value,
|
||||
'actual' => $actual,
|
||||
'actual' => $actual,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// index_format ist zwingend
|
||||
if (($meta['index_format'] ?? null) !== 'ndjson') {
|
||||
$diff['index_format'] = [
|
||||
'expected' => 'ndjson',
|
||||
'actual' => $meta['index_format'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
return $diff;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $payload
|
||||
*/
|
||||
private function atomicWriteJson(array $payload): void
|
||||
{
|
||||
$dir = \dirname($this->metaPath);
|
||||
$dir = dirname($this->metaPath);
|
||||
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
throw new \RuntimeException('Unable to create directory: ' . $dir);
|
||||
}
|
||||
@@ -197,7 +155,6 @@ final class IndexMetaManager
|
||||
throw new \RuntimeException('Unable to write temp meta file');
|
||||
}
|
||||
|
||||
// atomarer Switch
|
||||
if (!rename($tmp, $this->metaPath)) {
|
||||
@unlink($tmp);
|
||||
throw new \RuntimeException('Unable to switch meta file atomically');
|
||||
|
||||
Reference in New Issue
Block a user