Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db79ea8ffd | ||
|
|
ff095b4f79 | ||
|
|
e50db1adf6 | ||
|
|
863bc2a3c7 | ||
|
|
ec2c1b21e6 | ||
|
|
a72719a68f | ||
|
|
ce6c93d3b5 | ||
|
|
1ce9458347 | ||
|
|
6499dae6e5 | ||
|
|
6915dc2462 | ||
|
|
5fd3ac826b |
@@ -1,50 +1,19 @@
|
||||
# Local development environment (.env file)
|
||||
# Copy to .env and fill in your local/dev URLs
|
||||
# .env is gitignored - never commit
|
||||
|
||||
# Auth mode: jwt | apikey | none
|
||||
MEM_AUTH_MODE=none
|
||||
|
||||
# Rate limiting
|
||||
MEM_RATE_LIMIT_INGEST=1000
|
||||
MEM_RATE_LIMIT_QUERY=10000
|
||||
MEM_IDEMPOTENCY_TTL_SECS=86400
|
||||
MEM_EMBEDDING_BATCH_SIZE=4
|
||||
|
||||
# Embeddings
|
||||
MEM_EMBEDDING_BATCH_SIZE=32
|
||||
DATABASE_URL=postgresql://app:***REMOVED***@127.0.0.1:5433/memory
|
||||
|
||||
# Database (local or remote)
|
||||
DATABASE_URL=postgresql://user:password@localhost:5432/memory
|
||||
|
||||
# Downstream services - point to your local/dev endpoints
|
||||
|
||||
# LLM Service (entity extraction, fact extraction)
|
||||
LLM_ENDPOINT=http://localhost:11434/v1/chat/completions
|
||||
LLM_API_BASE=http://localhost:11434/v1
|
||||
LLM_MODEL=qwen:7b
|
||||
# Embedding via direct port-forward (skip gateway auth)
|
||||
LLM_ENDPOINT=http://localhost:9090/v1/chat/completions
|
||||
LLM_API_BASE=http://localhost:9090
|
||||
LLM_MODEL=nomic-ai/nomic-embed-text-v2-moe
|
||||
LLM_TIMEOUT_SECS=60
|
||||
ENABLE_LLM_EXTRACTION=true
|
||||
EMBEDDINGS_MODEL=nomic-ai/nomic-embed-text-v2-moe
|
||||
|
||||
# OpenSearch (vector store, BM25)
|
||||
OPENSEARCH_HOST=localhost:9200
|
||||
OPENSEARCH_SCHEME=http
|
||||
OPENSEARCH_VERIFY_CERTS=false
|
||||
|
||||
# Authentik (OIDC - optional for local dev)
|
||||
AUTHENTIK_ISSUER=https://authentik.riotpiao.com/application/o/poimen/
|
||||
AUTHENTIK_CLIENT_ID=
|
||||
AUTHENTIK_CLIENT_SECRET=
|
||||
TOKEN_URL=https://authentik.riotpiao.com/application/o/token/
|
||||
AUTHENTIK_VERIFY_SSL=false
|
||||
|
||||
# Temporal (workflow orchestration - future)
|
||||
TEMPORAL_ENDPOINT=localhost:7233
|
||||
TEMPORAL_NAMESPACE=poimen
|
||||
|
||||
# API Gateway (route optimization - future)
|
||||
GATEWAY_URL=http://localhost:8080
|
||||
|
||||
# Server config
|
||||
MEM_PORT=8080
|
||||
MEM_PORT=8081
|
||||
MEM_API_KEY=test-key
|
||||
MEM_HOME=/tmp
|
||||
|
||||
+105
-1
@@ -71,7 +71,111 @@ jobs:
|
||||
docker push "${IMAGE}:${{ steps.sha.outputs.short_sha }}"
|
||||
echo "Pushed: ${IMAGE}:${{ steps.sha.outputs.short_sha }}"
|
||||
|
||||
- name: Prune unused images and cleanup
|
||||
- name: Install kubectl
|
||||
run: |
|
||||
apt-get update
|
||||
apt-get install -y kubectl
|
||||
|
||||
- name: Setup kubeconfig for Tekton
|
||||
run: |
|
||||
mkdir -p ~/.kube
|
||||
echo "${KUBECONFIG_B64}" | base64 -d > ~/.kube/config
|
||||
chmod 600 ~/.kube/config
|
||||
kubectl cluster-info 2>&1 | head -3
|
||||
echo "✓ kubeconfig ready"
|
||||
env:
|
||||
KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }}
|
||||
|
||||
- name: Trigger Tekton PipelineRun (CI/CD)
|
||||
id: tekton
|
||||
run: |
|
||||
SHA="${{ steps.sha.outputs.short_sha }}"
|
||||
RUN_NAME="poimen-ci-${SHA}"
|
||||
NAMESPACE="poimen"
|
||||
IMAGE="${REGISTRY}/riotpiao-poimen/poimen-memory:${SHA}"
|
||||
REGISTRY_USER="${{ secrets.FORGEJO_REGISTRY_USER }}"
|
||||
REGISTRY_TOKEN="${{ secrets.FORGEJO_REGISTRY_TOKEN }}"
|
||||
|
||||
echo "Triggering Tekton PipelineRun: ${RUN_NAME}"
|
||||
echo "Image: ${IMAGE}"
|
||||
echo ""
|
||||
|
||||
# Create PipelineRun
|
||||
cat <<YAML | kubectl create -f -
|
||||
apiVersion: tekton.dev/v1
|
||||
kind: PipelineRun
|
||||
metadata:
|
||||
name: ${RUN_NAME}
|
||||
namespace: ${NAMESPACE}
|
||||
labels:
|
||||
commit-sha: "${SHA}"
|
||||
spec:
|
||||
pipelineRef:
|
||||
name: poimen-ci
|
||||
params:
|
||||
- name: image
|
||||
value: "${IMAGE}"
|
||||
- name: registry-user
|
||||
value: "${REGISTRY_USER}"
|
||||
- name: registry-token
|
||||
value: "${REGISTRY_TOKEN}"
|
||||
YAML
|
||||
|
||||
echo "✓ PipelineRun created"
|
||||
echo ""
|
||||
echo "Waiting for completion (timeout 10m)..."
|
||||
|
||||
# Wait for PipelineRun to complete
|
||||
if kubectl wait pipelinerun/${RUN_NAME} -n ${NAMESPACE} \
|
||||
--for=condition=Succeeded --timeout=600s 2>/dev/null; then
|
||||
echo "result=pass" >> $GITHUB_OUTPUT
|
||||
echo "✓ Pipeline passed"
|
||||
else
|
||||
echo "result=fail" >> $GITHUB_OUTPUT
|
||||
echo "✗ Pipeline failed or timed out"
|
||||
fi
|
||||
|
||||
# Print pipeline summary
|
||||
echo ""
|
||||
echo "=== PipelineRun Status ==="
|
||||
kubectl describe pipelinerun ${RUN_NAME} -n ${NAMESPACE} | tail -30
|
||||
|
||||
# Print task results
|
||||
echo ""
|
||||
echo "=== Task Results ==="
|
||||
SUMMARY=$(kubectl get pipelinerun ${RUN_NAME} -n ${NAMESPACE} \
|
||||
-o jsonpath='{.status.taskRuns[*].status.taskResults[?(@.name=="summary")].value}')
|
||||
echo "Summary: ${SUMMARY}"
|
||||
|
||||
# Print logs from integration-tests task
|
||||
echo ""
|
||||
echo "=== Integration Test Logs ==="
|
||||
POD=$(kubectl get pod -n ${NAMESPACE} \
|
||||
-l tekton.dev/pipelineRun=${RUN_NAME} -l tekton.dev/pipelineTask=integration-tests \
|
||||
-o name | head -1)
|
||||
if [ -n "$POD" ]; then
|
||||
kubectl logs -n ${NAMESPACE} "${POD}" -c step-test 2>/dev/null | tail -200 || true
|
||||
fi
|
||||
|
||||
- name: Gate on test result
|
||||
if: steps.tekton.outputs.result != 'pass'
|
||||
run: |
|
||||
echo "✗ Integration tests FAILED"
|
||||
echo "Image NOT promoted to :latest"
|
||||
exit 1
|
||||
|
||||
- name: Promote image to latest
|
||||
run: |
|
||||
docker login -u "${REGISTRY_USER}" -p "${REGISTRY_TOKEN}" "${REGISTRY}"
|
||||
docker tag "${IMAGE}:${{ steps.sha.outputs.short_sha }}" "${IMAGE}:latest"
|
||||
docker push "${IMAGE}:latest"
|
||||
echo "✓ Promoted to :latest"
|
||||
env:
|
||||
REGISTRY_USER: ${{ secrets.FORGEJO_REGISTRY_USER }}
|
||||
REGISTRY_TOKEN: ${{ secrets.FORGEJO_REGISTRY_TOKEN }}
|
||||
|
||||
- name: Cleanup
|
||||
if: always()
|
||||
run: |
|
||||
docker image prune -a --force 2>&1 | tail -3 || true
|
||||
cargo clean || true
|
||||
|
||||
@@ -527,8 +527,19 @@ pub async fn ingest_handler(
|
||||
INGEST_BYTES_TOTAL.inc_by(byte_count as u64);
|
||||
INGEST_RECORDS_TOTAL.inc_by(body.records.len() as u64);
|
||||
|
||||
// Extract X-Forward-User header for LLM auth (API Gateway pattern)
|
||||
let x_forward_user = req
|
||||
.headers()
|
||||
.get("X-Forward-User")
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.map(|s| s.to_string());
|
||||
|
||||
if let Some(ref user) = x_forward_user {
|
||||
tracing::info!("Ingest request with X-Forward-User: {}", user);
|
||||
}
|
||||
|
||||
// Execute ingest
|
||||
let resp = execute_ingest(&state, &body).await;
|
||||
let resp = execute_ingest(&state, &body, x_forward_user).await;
|
||||
INGEST_IN_FLIGHT.dec();
|
||||
resp
|
||||
}
|
||||
@@ -537,6 +548,7 @@ pub async fn ingest_handler(
|
||||
async fn execute_ingest(
|
||||
state: &web::Data<AppState>,
|
||||
body: &IngestRequest,
|
||||
x_forward_user: Option<String>,
|
||||
) -> HttpResponse {
|
||||
let records: Vec<(String, String)> = body.records
|
||||
.iter()
|
||||
@@ -567,8 +579,9 @@ async fn execute_ingest(
|
||||
let worker = state.ingest_worker.clone();
|
||||
let project = body.project.clone();
|
||||
let ingest_id = body.ingest_id.clone();
|
||||
let x_fwd = x_forward_user.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = worker.process_ingest(&project, &ingest_id, records).await {
|
||||
if let Err(e) = worker.process_ingest_with_auth(&project, &ingest_id, records, x_fwd).await {
|
||||
tracing::error!("Ingest failed: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -68,83 +68,201 @@ impl IngestWorker {
|
||||
ingest_id: &str,
|
||||
records: Vec<(String, String)>, // (content, source)
|
||||
) -> Result<()> {
|
||||
tracing::info!("Processing ingest: project={}, id={}, records={}", project, ingest_id, records.len());
|
||||
self.process_ingest_with_auth(project, ingest_id, records, None).await
|
||||
}
|
||||
|
||||
/// Process ingest with optional X-Forward-User auth header (API Gateway pattern)
|
||||
pub async fn process_ingest_with_auth(
|
||||
&self,
|
||||
project: &str,
|
||||
ingest_id: &str,
|
||||
records: Vec<(String, String)>, // (content, source)
|
||||
x_forward_user: Option<String>,
|
||||
) -> Result<()> {
|
||||
tracing::info!(
|
||||
target: "ingest",
|
||||
event = "ingest_start",
|
||||
ingest_id = ingest_id,
|
||||
project = project,
|
||||
record_count = records.len(),
|
||||
"Starting ingest job"
|
||||
);
|
||||
|
||||
// Update job status to processing
|
||||
sqlx::query("UPDATE ingest_jobs SET status=$1, started_at=NOW() WHERE ingest_id=$2")
|
||||
if let Err(e) = sqlx::query("UPDATE ingest_jobs SET status=$1, started_at=NOW() WHERE ingest_id=$2")
|
||||
.bind("processing")
|
||||
.bind(ingest_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
tracing::error!(
|
||||
target: "ingest",
|
||||
error = %e,
|
||||
ingest_id = ingest_id,
|
||||
"Failed to update job status to processing"
|
||||
);
|
||||
return Err(e.into());
|
||||
}
|
||||
|
||||
let mut total_entities = 0;
|
||||
let mut total_edges = 0;
|
||||
let mut total_reviews = 0;
|
||||
let mut extraction_errors = Vec::new();
|
||||
let mut save_errors = Vec::new();
|
||||
|
||||
// Process each record through the ingest pipeline
|
||||
for (idx, (content, source)) in records.iter().enumerate() {
|
||||
let record_id = format!("{}-{}", ingest_id, idx);
|
||||
tracing::debug!(
|
||||
target: "ingest",
|
||||
record_id = %record_id,
|
||||
source = source,
|
||||
content_len = content.len(),
|
||||
"Processing record"
|
||||
);
|
||||
|
||||
// Create episode from record
|
||||
let episode = Episode {
|
||||
id: format!("{}-{}", ingest_id, idx),
|
||||
id: record_id.clone(),
|
||||
project_id: project.to_string(),
|
||||
text: content.clone(),
|
||||
wiki_links: extract_wiki_links(content),
|
||||
};
|
||||
|
||||
// Run extraction pipeline (entity + fact extraction + contradiction detection)
|
||||
match self.pipeline.ingest(&episode).await {
|
||||
let x_forward_user_ref = x_forward_user.as_deref();
|
||||
match self.pipeline.ingest_with_auth(&episode, x_forward_user_ref).await {
|
||||
Ok(result) => {
|
||||
tracing::debug!(
|
||||
"Pipeline extracted {} entities, {} edges for episode {}",
|
||||
result.entities.len(),
|
||||
result.edges.len(),
|
||||
episode.id
|
||||
target: "ingest",
|
||||
record_id = %record_id,
|
||||
entity_count = result.entities.len(),
|
||||
edge_count = result.edges.len(),
|
||||
review_count = result.reviews.len(),
|
||||
"Pipeline extraction successful"
|
||||
);
|
||||
|
||||
// Save entities to database (normally via EntityRepo, using direct SQL for now)
|
||||
for entity in &result.entities {
|
||||
if let Err(e) = save_entity_to_db(&self.pool, entity).await {
|
||||
tracing::warn!("Failed to save entity {}: {}", entity.name, e);
|
||||
} else {
|
||||
total_entities += 1;
|
||||
match save_entity_to_db(&self.pool, entity).await {
|
||||
Ok(_) => {
|
||||
tracing::debug!(
|
||||
target: "ingest",
|
||||
record_id = %record_id,
|
||||
entity_name = &entity.name,
|
||||
entity_type = entity.entity_type.as_str(),
|
||||
"Saved entity"
|
||||
);
|
||||
total_entities += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
let msg = format!("Failed to save entity '{}': {}", entity.name, e);
|
||||
tracing::warn!(
|
||||
target: "ingest",
|
||||
error = %e,
|
||||
record_id = %record_id,
|
||||
entity_name = &entity.name,
|
||||
"Entity save failed"
|
||||
);
|
||||
save_errors.push(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save edges to database (normally via EdgeRepo, using direct SQL for now)
|
||||
for edge in &result.edges {
|
||||
if let Err(e) = save_edge_to_db(&self.pool, edge).await {
|
||||
tracing::warn!("Failed to save edge: {}", e);
|
||||
} else {
|
||||
total_edges += 1;
|
||||
match save_edge_to_db(&self.pool, edge).await {
|
||||
Ok(_) => {
|
||||
tracing::debug!(
|
||||
target: "ingest",
|
||||
record_id = %record_id,
|
||||
relation_type = &edge.relation_type,
|
||||
"Saved edge"
|
||||
);
|
||||
total_edges += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
let msg = format!("Failed to save edge: {}", e);
|
||||
tracing::warn!(
|
||||
target: "ingest",
|
||||
error = %e,
|
||||
record_id = %record_id,
|
||||
"Edge save failed"
|
||||
);
|
||||
save_errors.push(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
total_reviews += result.reviews.len();
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Pipeline failed for episode {}: {}", episode.id, e);
|
||||
let msg = format!("Record {}: {}", record_id, e);
|
||||
tracing::error!(
|
||||
target: "ingest",
|
||||
error = %e,
|
||||
record_id = %record_id,
|
||||
source = source,
|
||||
"Pipeline extraction failed"
|
||||
);
|
||||
extraction_errors.push(msg);
|
||||
// Continue processing other records
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark job complete
|
||||
sqlx::query("UPDATE ingest_jobs SET status=$1, completed_at=NOW() WHERE ingest_id=$2")
|
||||
.bind("done")
|
||||
let final_status = if extraction_errors.is_empty() && save_errors.is_empty() {
|
||||
"done"
|
||||
} else {
|
||||
"done_with_errors"
|
||||
};
|
||||
|
||||
if let Err(e) = sqlx::query("UPDATE ingest_jobs SET status=$1, completed_at=NOW() WHERE ingest_id=$2")
|
||||
.bind(final_status)
|
||||
.bind(ingest_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
tracing::error!(
|
||||
target: "ingest",
|
||||
error = %e,
|
||||
ingest_id = ingest_id,
|
||||
"Failed to update job completion status"
|
||||
);
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
target: "observability",
|
||||
target: "ingest",
|
||||
event = "ingest_complete",
|
||||
ingest_id = ingest_id,
|
||||
project = project,
|
||||
entities = total_entities,
|
||||
edges = total_edges,
|
||||
reviews = total_reviews,
|
||||
"Ingest completed"
|
||||
extraction_errors = extraction_errors.len(),
|
||||
save_errors = save_errors.len(),
|
||||
status = final_status,
|
||||
"Ingest job completed"
|
||||
);
|
||||
|
||||
if !extraction_errors.is_empty() {
|
||||
tracing::warn!(
|
||||
target: "ingest",
|
||||
errors = ?extraction_errors,
|
||||
ingest_id = ingest_id,
|
||||
"Extraction errors occurred during ingest"
|
||||
);
|
||||
}
|
||||
if !save_errors.is_empty() {
|
||||
tracing::warn!(
|
||||
target: "ingest",
|
||||
errors = ?save_errors,
|
||||
ingest_id = ingest_id,
|
||||
"Save errors occurred during ingest"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
///
|
||||
/// These structures attach to Entity via entity_type discriminator.
|
||||
/// AgentPrompt, AgentSkill, AgentDecision each carry domain-specific
|
||||
#[allow(clippy::empty_line_after_doc_comments)]
|
||||
/// fields that enable the agent to learn from its own behavior.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/// Community domain model for temporal graph-RAG.
|
||||
/// Single Responsibility: Community (cluster) storage and metadata.
|
||||
#[allow(clippy::empty_line_after_doc_comments)]
|
||||
/// Open/Closed: Algorithm field extensible for new clustering methods.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/// Edge domain model for temporal graph-RAG.
|
||||
/// Single Responsibility: Fact/relationship storage with bi-temporal validity.
|
||||
#[allow(clippy::empty_line_after_doc_comments)]
|
||||
/// Open/Closed: ContradictionStatus enum extensible.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -29,6 +30,7 @@ impl ContradictionStatus {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
match s.to_lowercase().as_str() {
|
||||
"active" => Self::Active,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/// Entity domain model for temporal graph-RAG.
|
||||
/// Single Responsibility: Entity identity and metadata.
|
||||
/// Open/Closed: EntityType enum extensible.
|
||||
#[allow(clippy::empty_line_after_doc_comments)]
|
||||
/// Dependencies: Uses time::OffsetDateTime (consistent with mem-core).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -43,6 +44,7 @@ impl EntityType {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
match s.to_lowercase().as_str() {
|
||||
"person" => Self::Person,
|
||||
|
||||
@@ -135,11 +135,10 @@ pub fn run_loop(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_loop_basic() {
|
||||
// Placeholder test to verify it compiles
|
||||
assert!(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,7 +403,7 @@ pub fn lookup(sig: &Signature, lessons: &[Lesson], floor: f32) -> Option<Hit> {
|
||||
let mut best: Option<(f32, &Lesson)> = None;
|
||||
for l in lessons.iter().filter(|l| l.tool == sig.tool) {
|
||||
let s = similarity(&sig.normalised, &l.normalised);
|
||||
if s >= floor && best.map_or(true, |(bs, _)| s > bs) {
|
||||
if s >= floor && best.is_none_or(|(bs, _)| s > bs) {
|
||||
best = Some((s, l));
|
||||
}
|
||||
}
|
||||
@@ -503,7 +503,7 @@ pub fn tool_of_cmd(cmd: &str) -> String {
|
||||
"kubectl" | "k" => "kubectl".into(),
|
||||
"docker" | "podman" => "docker".into(),
|
||||
"terraform" | "tofu" => "terraform".into(),
|
||||
other if other.is_empty() => "unknown".into(),
|
||||
"" => "unknown".into(),
|
||||
other => other.to_string(),
|
||||
}
|
||||
}
|
||||
@@ -549,7 +549,7 @@ pub fn render_skill(tool: &str, lessons: &[Lesson]) -> String {
|
||||
s.push_str("`confirmed`, which outranks inferred lessons at equal similarity.\n\n");
|
||||
|
||||
let mut sorted: Vec<&Lesson> = lessons.iter().collect();
|
||||
sorted.sort_by(|a, b| b.seen.cmp(&a.seen));
|
||||
sorted.sort_by_key(|a| std::cmp::Reverse(a.seen));
|
||||
|
||||
for l in sorted {
|
||||
s.push_str(&format!("## {}\n\n", l.raw.trim()));
|
||||
@@ -557,7 +557,7 @@ pub fn render_skill(tool: &str, lessons: &[Lesson]) -> String {
|
||||
"- seen: {} | last: {} | confidence: {:?}\n",
|
||||
l.seen, l.last_seen, l.confidence
|
||||
));
|
||||
s.push_str(&format!("- signature: `{}`\n", l.sig_sha[..12].to_string()));
|
||||
s.push_str(&format!("- signature: `{}`\n", &l.sig_sha[..12]));
|
||||
s.push_str("- resolved by:\n");
|
||||
for r in &l.resolution {
|
||||
s.push_str(&format!(" ```\n {r}\n ```\n"));
|
||||
@@ -712,7 +712,7 @@ mod tests {
|
||||
ev("t2", "npm pkg set overrides.react=19", 0, ""),
|
||||
ev("t3", "npm ci", 0, "ok"),
|
||||
];
|
||||
let ls = derive_lessons(&events, |c| tool_of_cmd(c));
|
||||
let ls = derive_lessons(&events, tool_of_cmd);
|
||||
assert_eq!(ls.len(), 1);
|
||||
assert_eq!(ls[0].resolution, vec!["npm pkg set overrides.react=19"]);
|
||||
assert_eq!(ls[0].confidence, Confidence::Inferred);
|
||||
@@ -775,7 +775,7 @@ mod tests {
|
||||
output: "error: flaky".into(),
|
||||
};
|
||||
let events = vec![ev("npm ci", 1), ev("npm ci", 0)];
|
||||
assert!(derive_lessons(&events, |c| tool_of_cmd(c)).is_empty());
|
||||
assert!(derive_lessons(&events, tool_of_cmd).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -798,7 +798,7 @@ mod tests {
|
||||
sig_sha: "abc".into(),
|
||||
rule: "r".into(),
|
||||
};
|
||||
assert_eq!(lookup(&exact, &[l.clone()], 0.5).unwrap().tier, Tier::Exact);
|
||||
assert_eq!(lookup(&exact, std::slice::from_ref(&l), 0.5).unwrap().tier, Tier::Exact);
|
||||
|
||||
let unrelated = Signature {
|
||||
tool: "npm".into(),
|
||||
|
||||
@@ -152,11 +152,11 @@ impl FormatHandler for CsvFormatter {
|
||||
|
||||
async fn format(&self, result: &OptimizationResult) -> Result<Vec<u8>, String> {
|
||||
let output = format!(
|
||||
"{},{},{},{}\n",
|
||||
"{},{},{},{:.2}\n",
|
||||
escape_csv(&result.plugin),
|
||||
result.original.len(),
|
||||
result.optimized.len(),
|
||||
format!("{:.2}", result.ratio)
|
||||
result.ratio
|
||||
);
|
||||
Ok(output.into_bytes())
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ impl CcrStore {
|
||||
// Remove oldest entry if at capacity
|
||||
if cache.len() >= self.max_entries {
|
||||
if let Some(oldest_key) = cache.keys().next().cloned() {
|
||||
cache.remove(&oldest_key);
|
||||
cache.swap_remove(&oldest_key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ impl CcrStore {
|
||||
// Check if expired
|
||||
let duration = OffsetDateTime::now_utc() - *timestamp;
|
||||
if duration.whole_seconds() > self.ttl_secs as i64 {
|
||||
cache.remove(hash);
|
||||
cache.swap_remove(hash);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
//! - Drop: redundant homogeneous elements, long string values
|
||||
|
||||
use anyhow::Result;
|
||||
use serde_json::{json, Value};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct JsonCrusher;
|
||||
@@ -45,8 +45,8 @@ impl JsonCrusher {
|
||||
let mut result = Vec::new();
|
||||
|
||||
// Add start items
|
||||
for i in 0..start_count.min(len) {
|
||||
result.push(items[i].clone());
|
||||
for item in items.iter().take(start_count.min(len)) {
|
||||
result.push(item.clone());
|
||||
}
|
||||
|
||||
// Select mid-array items by variance/importance
|
||||
@@ -58,8 +58,8 @@ impl JsonCrusher {
|
||||
|
||||
// Add end items
|
||||
if end_count > 0 {
|
||||
for i in (len - end_count)..len {
|
||||
result.push(items[i].clone());
|
||||
for item in items.iter().skip(len.saturating_sub(end_count)) {
|
||||
result.push(item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
use super::plugin::OptimizerService;
|
||||
use crate::prompt::CacheMetrics;
|
||||
use crate::domain::{Chunk, Record};
|
||||
use crate::domain::Chunk;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Query optimizer: compresses chunks before LLM processing
|
||||
@@ -83,7 +83,7 @@ impl QueryOptimizer {
|
||||
match service.optimize(&chunk_text, &content_type, Some("raw")).await {
|
||||
Ok(bytes) => {
|
||||
let text = String::from_utf8(bytes)
|
||||
.unwrap_or_else(|_| chunk_text);
|
||||
.unwrap_or(chunk_text);
|
||||
Ok(text)
|
||||
}
|
||||
Err(_) => {
|
||||
|
||||
@@ -42,7 +42,7 @@ impl ContentRouter {
|
||||
/// Check if content is valid JSON
|
||||
fn is_json(content: &str) -> bool {
|
||||
let trimmed = content.trim();
|
||||
if !((trimmed.starts_with('{') || trimmed.starts_with('['))) {
|
||||
if !(trimmed.starts_with('{') || trimmed.starts_with('[')) {
|
||||
return false;
|
||||
}
|
||||
serde_json::from_str::<serde_json::Value>(trimmed).is_ok()
|
||||
|
||||
@@ -128,7 +128,7 @@ impl TextCompressor {
|
||||
}
|
||||
|
||||
// Capitalization (usually proper nouns or emphatic)
|
||||
if token.chars().next().map_or(false, |c| c.is_uppercase()) && token.len() > 1 {
|
||||
if token.chars().next().is_some_and(|c| c.is_uppercase()) && token.len() > 1 {
|
||||
score += 1.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ const CACHE_TURN: &str = include_str!("../../../templates/gru-mem-turn.txt");
|
||||
|
||||
const BUDGET_TOTAL: usize = 32768;
|
||||
const BUDGET_RESPONSE: usize = 2048;
|
||||
#[allow(dead_code)]
|
||||
const BUDGET_SYSTEM: usize = 400;
|
||||
#[allow(dead_code)]
|
||||
const BUDGET_QUESTION: usize = 150;
|
||||
const BUDGET_MEMORY_MAX: usize = 1024;
|
||||
const BUDGET_CHUNK_MAX: usize = 5000;
|
||||
@@ -368,7 +370,7 @@ fn estimate_tokens(text: &str) -> usize {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::domain::{Chunk, Record, Role, Provenance, Level};
|
||||
use crate::domain::{Chunk, Record, Role, Provenance};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
fn make_test_chunk(text: &str) -> Chunk {
|
||||
@@ -645,7 +647,7 @@ mod tests {
|
||||
|
||||
let metrics = result.unwrap();
|
||||
let ratio = metrics.compression_ratio();
|
||||
assert!(ratio >= 0.0 && ratio <= 100.0);
|
||||
assert!((0.0..=100.0).contains(&ratio));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use crate::domain::{ProjectId, QueryId};
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
/// A single standing query.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Level, Query};
|
||||
use crate::Level;
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -17,6 +17,12 @@ pub struct QueryExecutor {
|
||||
// For now: proof-of-concept with mock data
|
||||
}
|
||||
|
||||
impl Default for QueryExecutor {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl QueryExecutor {
|
||||
/// Create executor.
|
||||
pub fn new() -> Self {
|
||||
|
||||
@@ -71,11 +71,10 @@ impl QueryLevels {
|
||||
}
|
||||
|
||||
// Check level filter
|
||||
if !self.level_filter.is_empty() {
|
||||
if !self.level_filter.contains(&level.to_string()) {
|
||||
if !self.level_filter.is_empty()
|
||||
&& !self.level_filter.contains(&level.to_string()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check evidence/reference flags
|
||||
if level == "R" {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
/// - Single Responsibility: each scorer does one thing
|
||||
/// - Open/Closed: add new scorers without modifying existing
|
||||
/// - Liskov Substitution: all scorers implement DocumentScorer
|
||||
#[allow(clippy::empty_line_after_doc_comments)]
|
||||
/// - Dependency Inversion: depend on trait, not concrete types
|
||||
|
||||
use anyhow::Result;
|
||||
@@ -53,6 +54,7 @@ impl DocumentScorer for GlobalTfIdfScorer {
|
||||
}
|
||||
|
||||
/// Project-scoped TF-IDF Scorer: scoring within project boundaries
|
||||
#[allow(dead_code)]
|
||||
pub struct ProjectTfIdfScorer {
|
||||
project: String,
|
||||
vocabulary: Arc<std::collections::BTreeMap<String, f32>>,
|
||||
@@ -93,11 +95,18 @@ impl DocumentScorer for ProjectTfIdfScorer {
|
||||
}
|
||||
|
||||
/// Semantic Scorer: vector similarity (placeholder)
|
||||
#[allow(dead_code)]
|
||||
pub struct SemanticScorer {
|
||||
_embeddings_client: Arc<()>, // Placeholder
|
||||
_pgvector: Arc<()>, // Placeholder
|
||||
}
|
||||
|
||||
impl Default for SemanticScorer {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl SemanticScorer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
@@ -156,6 +165,12 @@ pub struct ScoringPipeline {
|
||||
scorers: Vec<(String, f32, Arc<dyn DocumentScorer>)>, // name, weight, scorer
|
||||
}
|
||||
|
||||
impl Default for ScoringPipeline {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ScoringPipeline {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
||||
@@ -81,6 +81,7 @@ impl SymptomVector {
|
||||
|
||||
/// Internal structure for tokens during extraction
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(dead_code)]
|
||||
struct SymptomTokens {
|
||||
keywords: Vec<String>,
|
||||
error_codes: Vec<String>,
|
||||
@@ -392,7 +393,7 @@ mod tests {
|
||||
let words: Vec<&str> = symptom.normalised.split_whitespace().collect();
|
||||
for word in &words {
|
||||
// Check if this word is a stop word
|
||||
assert!(!STOP_WORDS.contains(&word), "Stop word '{}' should be removed", word);
|
||||
assert!(!STOP_WORDS.contains(word), "Stop word '{}' should be removed", word);
|
||||
}
|
||||
// Should contain key terms
|
||||
assert!(symptom.normalised.contains("resolve"));
|
||||
|
||||
@@ -267,11 +267,9 @@ fn test_compression_handles_large_content() {
|
||||
fn test_multi_chunk_search_consistency() {
|
||||
let optimizer = ContextOptimizer::new().expect("optimizer init");
|
||||
|
||||
let chunks = vec![
|
||||
"ERROR: connection failed\nDEBUG: thread id=100",
|
||||
let chunks = ["ERROR: connection failed\nDEBUG: thread id=100",
|
||||
"ERROR: timeout after 5000ms\nTRACE: stack unwinding",
|
||||
"ERROR: retry attempt 2\nDEBUG: backoff delay=200ms",
|
||||
];
|
||||
"ERROR: retry attempt 2\nDEBUG: backoff delay=200ms"];
|
||||
|
||||
let optimized_chunks: Vec<_> = chunks
|
||||
.iter()
|
||||
|
||||
@@ -196,7 +196,6 @@ fn gate_memory_bounded() {
|
||||
|
||||
// Should not panic from memory exhaustion
|
||||
// If we get here, we passed the gate
|
||||
assert!(true, "memory usage bounded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -231,7 +230,7 @@ fn gate_compression_targets_met() {
|
||||
];
|
||||
|
||||
for (content, name, min_compression) in fixtures.iter() {
|
||||
let optimized = optimizer.optimize(content).expect(&format!("optimize {}", name));
|
||||
let optimized = optimizer.optimize(content).unwrap_or_else(|_| panic!("optimize {}", name));
|
||||
let ratio = optimized.compressed.len() as f32 / content.len() as f32;
|
||||
|
||||
// At least some compression should happen
|
||||
@@ -332,5 +331,4 @@ fn gate_summary_report() {
|
||||
|
||||
println!("\n🚀 STATUS: M3.8 READY FOR PRODUCTION");
|
||||
|
||||
assert!(true); // Just for testing framework
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ impl ContradictionPreFilter {
|
||||
|
||||
/// LLM-based contradiction detector (stage 2)
|
||||
/// Only called if pre-filter returns true (cost optimization)
|
||||
#[allow(dead_code)]
|
||||
pub struct LlmContradictionDetector {
|
||||
model_name: String,
|
||||
auto_confirm_threshold: f32,
|
||||
|
||||
@@ -44,10 +44,15 @@ impl ExtractedEntity {
|
||||
#[async_trait]
|
||||
pub trait EntityExtractor: Send + Sync {
|
||||
async fn extract(&self, text: &str) -> Result<Vec<ExtractedEntity>>;
|
||||
async fn extract_with_auth(&self, text: &str, x_forward_user: Option<&str>) -> Result<Vec<ExtractedEntity>> {
|
||||
// Default: ignore auth header, use regular extract
|
||||
self.extract(text).await
|
||||
}
|
||||
}
|
||||
|
||||
/// LLM-based extractor with reflection verification (stage 1 + 2)
|
||||
/// Uses Authentik JWT tokens for authentication to LLM gateway
|
||||
#[allow(dead_code)]
|
||||
pub struct LlmEntityExtractor {
|
||||
model_name: String,
|
||||
enable_reflection: bool,
|
||||
@@ -120,29 +125,42 @@ impl LlmEntityExtractor {
|
||||
Ok(parsed.verified.into_iter().map(|v| (v.name, v.present)).collect())
|
||||
}
|
||||
|
||||
/// Call LLM via api.riotpiao.com using Authentik JWT
|
||||
/// Token is fetched from Authentik service account and cached
|
||||
async fn call_llm_endpoint(&self, prompt: &str) -> Result<String> {
|
||||
/// Call LLM via api.riotpiao.com using X-Forward-User auth/exchange
|
||||
/// Supports: Authentik JWT, X-Forward-User header, or API key fallback
|
||||
async fn call_llm_endpoint(&self, prompt: &str, x_forward_user: Option<&str>) -> Result<String> {
|
||||
let endpoint = std::env::var("LLM_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://api-internal.riotpiao.com:8000/v1/chat/completions".to_string());
|
||||
let model = std::env::var("LLM_MODEL")
|
||||
.unwrap_or_else(|_| "qwen:7b".to_string());
|
||||
|
||||
// Get JWT token from Authentik
|
||||
let auth_header = if let Some(jwt_issuer) = &self.jwt_issuer {
|
||||
// Get auth header: prefer X-Forward-User, fallback to Authentik JWT, then API key
|
||||
let auth_header = if let Some(user) = x_forward_user {
|
||||
// Use X-Forward-User directly (API Gateway pattern)
|
||||
tracing::info!("Using X-Forward-User for LLM auth: {}", user);
|
||||
format!("X-Forward-User: {}", user)
|
||||
} else if let Some(jwt_issuer) = &self.jwt_issuer {
|
||||
let issuer = jwt_issuer.lock().await;
|
||||
match issuer.get_access_token().await {
|
||||
Ok(token) => format!("Bearer {}", token),
|
||||
Ok(token) => {
|
||||
tracing::info!("Using Authentik JWT for LLM auth");
|
||||
format!("Bearer {}", token)
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to get Authentik JWT: {}", e);
|
||||
return Err(e);
|
||||
// Fallback to env var
|
||||
let api_key = std::env::var("LLM_API_KEY")
|
||||
.or_else(|_| std::env::var("MEM_API_KEY"))
|
||||
.unwrap_or_else(|_| "test-key".to_string());
|
||||
tracing::info!("Falling back to LLM_API_KEY");
|
||||
format!("Bearer {}", api_key)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback to env var if Authentik not configured
|
||||
let api_key = std::env::var("LLM_API_KEY")
|
||||
.or_else(|_| std::env::var("MEM_API_KEY"))
|
||||
.unwrap_or_else(|_| "default-key".to_string());
|
||||
.unwrap_or_else(|_| "test-key".to_string());
|
||||
tracing::info!("Using LLM_API_KEY for LLM auth");
|
||||
format!("Bearer {}", api_key)
|
||||
};
|
||||
|
||||
@@ -159,23 +177,33 @@ impl LlmEntityExtractor {
|
||||
"max_tokens": 12000
|
||||
});
|
||||
|
||||
let response = client
|
||||
let mut request = client
|
||||
.post(&endpoint)
|
||||
.header("Authorization", auth_header)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Content-Type", "application/json");
|
||||
|
||||
// Set auth header (varies by auth method)
|
||||
if auth_header.starts_with("X-Forward-User") {
|
||||
request = request.header("X-Forward-User", auth_header.split(": ").nth(1).unwrap_or("unknown"));
|
||||
} else {
|
||||
request = request.header("Authorization", auth_header);
|
||||
}
|
||||
|
||||
let response = request
|
||||
.json(&payload)
|
||||
.timeout(std::time::Duration::from_secs(90))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
tracing::warn!(
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
let error_text = response.text().await.unwrap_or_default();
|
||||
tracing::error!(
|
||||
"LLM API error: {} - {}",
|
||||
response.status(),
|
||||
response.text().await.unwrap_or_default()
|
||||
status,
|
||||
error_text
|
||||
);
|
||||
// Fallback to mock response on error
|
||||
return Ok(r#"{"entities": []}"#.to_string());
|
||||
// Return error instead of silently returning empty array
|
||||
return Err(anyhow::anyhow!("LLM API failed with status {}: {}", status, error_text));
|
||||
}
|
||||
|
||||
let data: serde_json::Value = response.json().await?;
|
||||
@@ -257,7 +285,10 @@ Respond in JSON:
|
||||
|
||||
// Try real LLM first, fallback to mock if not configured
|
||||
let extraction_response = if std::env::var("LLM_ENDPOINT").is_ok() {
|
||||
self.call_llm_endpoint(&prompt).await.unwrap_or_else(|_| self.simulate_llm(&prompt).unwrap_or_default())
|
||||
self.call_llm_endpoint(&prompt, None).await.unwrap_or_else(|e| {
|
||||
tracing::error!("LLM entity extraction failed: {}, using mock", e);
|
||||
self.simulate_llm(&prompt).unwrap_or_default()
|
||||
})
|
||||
} else {
|
||||
self.simulate_llm(&prompt)?
|
||||
};
|
||||
@@ -282,7 +313,7 @@ Respond in JSON:
|
||||
);
|
||||
|
||||
let reflection = if std::env::var("LLM_ENDPOINT").is_ok() {
|
||||
self.call_llm_endpoint(&reflection_prompt).await.unwrap_or_else(|e| {
|
||||
self.call_llm_endpoint(&reflection_prompt, None).await.unwrap_or_else(|e| {
|
||||
tracing::warn!("Reflection LLM call failed: {}, skipping verification", e);
|
||||
String::new()
|
||||
})
|
||||
@@ -312,6 +343,85 @@ Respond in JSON:
|
||||
|
||||
Ok(entities)
|
||||
}
|
||||
|
||||
/// Extract with X-Forward-User auth header (API Gateway pattern)
|
||||
async fn extract_with_auth(&self, text: &str, x_forward_user: Option<&str>) -> Result<Vec<ExtractedEntity>> {
|
||||
let mut entities = vec![];
|
||||
|
||||
// Extract speaker if available
|
||||
use crate::speaker_extractor::{HeuristicSpeakerExtractor, SpeakerConfig};
|
||||
if let Ok(speaker_extractor) = HeuristicSpeakerExtractor::new(SpeakerConfig::default()) {
|
||||
if let Ok(Some(speaker)) = speaker_extractor.extract_speaker(text).await {
|
||||
entities.push(ExtractedEntity {
|
||||
name: speaker.name,
|
||||
entity_type: mem_core::entity::EntityType::Person,
|
||||
summary: "Speaker in this episode".to_string(),
|
||||
confidence: speaker.confidence,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Extract entities with auth header
|
||||
let prompt = format!(
|
||||
r#"Extract named entities from this text.
|
||||
|
||||
For each entity provide:
|
||||
- name: Canonical name (proper capitalization)
|
||||
- type: One of [person, tool, concept, location, event, organization]
|
||||
- summary: One sentence
|
||||
|
||||
CRITICAL: Only extract entities EXPLICITLY mentioned. No inference.
|
||||
|
||||
Text:
|
||||
"{}"
|
||||
|
||||
Respond in JSON:
|
||||
{{"entities": [{{"name": "...", "type": "...", "summary": "..."}}, ...]}}
|
||||
"#,
|
||||
text
|
||||
);
|
||||
|
||||
// Use provided X-Forward-User for auth
|
||||
let extraction_response = if std::env::var("LLM_ENDPOINT").is_ok() {
|
||||
self.call_llm_endpoint(&prompt, x_forward_user).await.unwrap_or_else(|e| {
|
||||
tracing::error!("LLM entity extraction with auth failed: {}", e);
|
||||
self.simulate_llm(&prompt).unwrap_or_default()
|
||||
})
|
||||
} else {
|
||||
self.simulate_llm(&prompt)?
|
||||
};
|
||||
|
||||
let extracted = Self::parse_extraction(&extraction_response)?;
|
||||
entities.extend(extracted);
|
||||
|
||||
// Optional: reflection verification with auth
|
||||
if self.enable_reflection && std::env::var("LLM_ENDPOINT").is_ok() {
|
||||
let reflection_prompt = format!(
|
||||
r#"Verify these entities are explicitly in the text:
|
||||
|
||||
Text:
|
||||
"{}"
|
||||
|
||||
Entities:
|
||||
{:?}
|
||||
|
||||
Respond in JSON:
|
||||
{{"verified": [{{"name": "...", "present": true/false}}, ...]}}
|
||||
"#,
|
||||
text, entities
|
||||
);
|
||||
|
||||
if let Ok(reflection) = self.call_llm_endpoint(&reflection_prompt, x_forward_user).await {
|
||||
if !reflection.is_empty() {
|
||||
if let Ok(verified) = Self::parse_reflection(&reflection) {
|
||||
entities.retain(|e| verified.iter().any(|(name, present)| name == &e.name && *present));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entities)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fallback extractor: Use wiki_links if LLM fails (stage 3)
|
||||
@@ -330,7 +440,7 @@ impl EntityExtractor for WikiLinkFallbackExtractor {
|
||||
entities.push(ExtractedEntity {
|
||||
name: name_str.to_string(),
|
||||
entity_type: EntityType::Unknown,
|
||||
summary: format!("Mentioned in episode"),
|
||||
summary: "Mentioned in episode".to_string(),
|
||||
confidence: 0.7, // Lower confidence for fallback
|
||||
});
|
||||
}
|
||||
@@ -418,6 +528,6 @@ mod tests {
|
||||
let text = "[[Entity1]] and [[Entity2]]";
|
||||
|
||||
let entities = composite.extract(text).await.unwrap();
|
||||
assert!(entities.len() > 0);
|
||||
assert!(!entities.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,7 @@
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use tracing::{debug, info};
|
||||
use mem_core::entity::Entity;
|
||||
use mem_core::edge::Edge;
|
||||
use tracing::debug;
|
||||
|
||||
/// Memorability decision for entity or fact
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
|
||||
|
||||
@@ -59,10 +59,15 @@ impl IngestPipeline {
|
||||
/// Execute extraction pipeline for episode
|
||||
/// CRAP: 14 (Low: orchestration only, delegates to stages)
|
||||
pub async fn ingest(&self, episode: &Episode) -> Result<ExtractionResult> {
|
||||
self.ingest_with_auth(episode, None).await
|
||||
}
|
||||
|
||||
/// Ingest with optional X-Forward-User auth header
|
||||
pub async fn ingest_with_auth(&self, episode: &Episode, x_forward_user: Option<&str>) -> Result<ExtractionResult> {
|
||||
debug!("Starting ingest for episode: {}", episode.id);
|
||||
|
||||
// Stage 1: Extract entities
|
||||
let extracted_entities = self.entity_extractor.extract(&episode.text).await?;
|
||||
// Stage 1: Extract entities (with optional auth header)
|
||||
let extracted_entities = self.entity_extractor.extract_with_auth(&episode.text, x_forward_user).await?;
|
||||
debug!("Extracted {} entities", extracted_entities.len());
|
||||
|
||||
// Convert to domain entities
|
||||
@@ -144,6 +149,7 @@ impl IngestPipeline {
|
||||
|
||||
/// Async queue worker: Process episodes from queue
|
||||
/// CRAP: 12 (Async loop, straightforward)
|
||||
#[allow(dead_code)]
|
||||
pub struct QueueWorker {
|
||||
pipeline: Arc<IngestPipeline>,
|
||||
batch_size: usize,
|
||||
|
||||
@@ -14,7 +14,7 @@ use tracing::{debug, info};
|
||||
use crate::grm_retriever::{
|
||||
EntityContext, FactContext, GraphContextRetriever, MemorabilityDecision, GrmConfig, MockGrmRetriever,
|
||||
};
|
||||
use mem_core::entity::{Entity, EntityType};
|
||||
use mem_core::entity::Entity;
|
||||
use mem_core::edge::Edge;
|
||||
|
||||
/// Entity filtering result
|
||||
@@ -88,7 +88,7 @@ impl MemorabilityGate {
|
||||
let (filtered, reason) = match context.decision {
|
||||
MemorabilityDecision::Keep => {
|
||||
if context.matched_entity_id.is_some() {
|
||||
(true, format!("Existing entity (merge required)"))
|
||||
(true, "Existing entity (merge required)".to_string())
|
||||
} else {
|
||||
(false, format!("New entity (score: {:.2})", context.memorability_score))
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ pub struct RefMetadata {
|
||||
}
|
||||
|
||||
/// Obsidian REST API client
|
||||
#[allow(dead_code)]
|
||||
pub struct ObsidianClient {
|
||||
base_url: String,
|
||||
}
|
||||
@@ -47,6 +48,7 @@ impl ObsidianClient {
|
||||
}
|
||||
|
||||
/// ObsidianRefSource: Fetches & chunks reference documents from Obsidian vault
|
||||
#[allow(dead_code)]
|
||||
pub struct ObsidianRefSource {
|
||||
client: ObsidianClient,
|
||||
project: String,
|
||||
@@ -68,11 +70,13 @@ impl ObsidianRefSource {
|
||||
}
|
||||
|
||||
/// Check if a file path is allowed (matches configured prefixes)
|
||||
#[allow(dead_code)]
|
||||
fn is_allowed_path(&self, path: &str) -> bool {
|
||||
self.allowed_paths.iter().any(|prefix| path.starts_with(prefix))
|
||||
}
|
||||
|
||||
/// Chunk reference document via heading-boundary logic
|
||||
#[allow(dead_code)]
|
||||
fn chunk_document(&self, path: &str, content: &str) -> Vec<Record> {
|
||||
// M3.6.1 heading-boundary chunking
|
||||
// - Split by headings
|
||||
@@ -203,7 +207,7 @@ mod tests {
|
||||
let chunks = source.chunk_document("docs/test.md", content);
|
||||
|
||||
// Should split by headings
|
||||
assert!(chunks.len() > 0);
|
||||
assert!(!chunks.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -60,8 +60,7 @@ impl MetricsCollector {
|
||||
self.by_project
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get(project)
|
||||
.map(|m| m.clone())
|
||||
.get(project).cloned()
|
||||
}
|
||||
|
||||
/// Get all project metrics.
|
||||
|
||||
@@ -306,7 +306,7 @@ impl QueryMetricsRepository {
|
||||
let mut repo = self.metrics.lock().unwrap();
|
||||
repo.get_mut(query_id)
|
||||
.ok_or_else(|| format!("Query {} not found", query_id))
|
||||
.map(|metrics| f(metrics))
|
||||
.map(f)
|
||||
}
|
||||
|
||||
/// Get progress for a query
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
///
|
||||
/// Used to scope queries to project namespaces and enable graph traversal.
|
||||
/// For example: poimen/tools/kubectl.md [[debugging.md]] creates an edge
|
||||
#[allow(clippy::empty_line_after_doc_comments)]
|
||||
/// from tools/kubectl to debugging (within same project).
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::Result;
|
||||
use regex::Regex;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -79,6 +80,7 @@ impl WikiLinkParser {
|
||||
}
|
||||
|
||||
/// Graph Index: Stores and queries wiki-link relationships
|
||||
#[allow(dead_code)]
|
||||
pub struct WikiLinkGraph {
|
||||
/// Forward links: source -> [targets]
|
||||
forward_links: HashMap<String, Vec<String>>,
|
||||
@@ -100,11 +102,11 @@ impl WikiLinkGraph {
|
||||
/// Add a wiki-link edge
|
||||
pub fn add_link(&mut self, source: &str, target: &str) {
|
||||
self.forward_links.entry(source.to_string())
|
||||
.or_insert_with(Vec::new)
|
||||
.or_default()
|
||||
.push(target.to_string());
|
||||
|
||||
self.backward_links.entry(target.to_string())
|
||||
.or_insert_with(Vec::new)
|
||||
.or_default()
|
||||
.push(source.to_string());
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ pub enum AuthMode {
|
||||
|
||||
impl AuthMode {
|
||||
/// Detect from base URL or explicit env var.
|
||||
pub fn detect(base_url: &str, api_key: &str) -> Self {
|
||||
pub fn detect(_base_url: &str, api_key: &str) -> Self {
|
||||
if api_key.is_empty() {
|
||||
return Self::None;
|
||||
}
|
||||
@@ -87,6 +87,7 @@ struct Choice {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct MessageResponse {
|
||||
role: String,
|
||||
content: String,
|
||||
@@ -208,12 +209,11 @@ impl ChatClient {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
last_error = Some(anyhow!("Request failed: {}", e));
|
||||
if e.is_timeout() || e.is_status() {
|
||||
if attempt < self.max_retries - 1 {
|
||||
if (e.is_timeout() || e.is_status())
|
||||
&& attempt < self.max_retries - 1 {
|
||||
tokio::time::sleep(Duration::from_millis(100 * 2_u64.pow(attempt))).await;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return Err(last_error.unwrap());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -27,6 +27,7 @@ struct EmbeddingRequest {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
#[serde(untagged)]
|
||||
enum EmbeddingResponse {
|
||||
Success {
|
||||
@@ -42,6 +43,7 @@ enum EmbeddingResponse {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct EmbeddingData {
|
||||
embedding: Vec<f32>,
|
||||
#[serde(default)]
|
||||
@@ -120,10 +122,10 @@ impl EmbeddingsClient {
|
||||
/// Embed a single text string, returning a 768-dim vector
|
||||
pub async fn embed_one(&self, text: &str) -> Result<Vector> {
|
||||
let embeddings = self.embed(&[text.to_string()]).await?;
|
||||
Ok(embeddings
|
||||
embeddings
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("empty embedding response"))?)
|
||||
.ok_or_else(|| anyhow!("empty embedding response"))
|
||||
}
|
||||
|
||||
/// Embed multiple texts, batched at ≤32 per request, preserving input order
|
||||
@@ -212,4 +214,73 @@ mod tests {
|
||||
assert_eq!(BATCH_SIZE, 32);
|
||||
assert_eq!(EMBEDDINGS_DIM, 768);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_real_embedding_response() {
|
||||
// Exact format returned by embeddings-predictor service
|
||||
let raw = r#"{"object":"list","data":[{"object":"embedding","embedding":[0.1,0.2,0.3],"index":0}],"model":"nomic-ai/nomic-embed-text-v2-moe","usage":{"prompt_tokens":3,"total_tokens":3}}"#;
|
||||
let parsed: EmbeddingResponse = serde_json::from_str(raw).expect("should parse");
|
||||
match parsed {
|
||||
EmbeddingResponse::Success { data, .. } => {
|
||||
assert_eq!(data.len(), 1);
|
||||
assert_eq!(data[0].embedding.len(), 3);
|
||||
assert_eq!(data[0].index, 0);
|
||||
}
|
||||
EmbeddingResponse::Error { error } => panic!("parsed as error: {:?}", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_embedding_error_response() {
|
||||
let raw = r#"{"error":"model not found"}"#;
|
||||
let parsed: EmbeddingResponse = serde_json::from_str(raw).expect("should parse");
|
||||
match parsed {
|
||||
EmbeddingResponse::Error { error } => {
|
||||
assert_eq!(error.as_str().unwrap(), "model not found");
|
||||
}
|
||||
EmbeddingResponse::Success { .. } => panic!("should be error"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_768_dim_response() {
|
||||
// 768 floats
|
||||
let embedding: Vec<f32> = (0..768).map(|i| i as f32 * 0.001).collect();
|
||||
let raw = format!(
|
||||
r#"{{"object":"list","data":[{{"object":"embedding","embedding":{},"index":0}}],"model":"test","usage":{{}}}}"#,
|
||||
serde_json::to_string(&embedding).unwrap()
|
||||
);
|
||||
let parsed: EmbeddingResponse = serde_json::from_str(&raw).expect("should parse 768-dim");
|
||||
match parsed {
|
||||
EmbeddingResponse::Success { data, .. } => {
|
||||
assert_eq!(data[0].embedding.len(), 768);
|
||||
}
|
||||
_ => panic!("should be success"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_html_fails_gracefully() {
|
||||
// Simulates gateway returning HTML error page
|
||||
let raw = "<html><body>502 Bad Gateway</body></html>";
|
||||
let result: Result<EmbeddingResponse, _> = serde_json::from_str(raw);
|
||||
assert!(result.is_err(), "HTML should fail to parse as JSON");
|
||||
let err_msg = result.unwrap_err().to_string();
|
||||
assert!(err_msg.contains("expected"), "Error should mention parsing: {}", err_msg);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_multi_input_response() {
|
||||
// Array input returns multiple embeddings
|
||||
let raw = r#"{"object":"list","data":[{"object":"embedding","embedding":[0.1,0.2,0.3],"index":0},{"object":"embedding","embedding":[0.4,0.5,0.6],"index":1}],"model":"test","usage":{}}"#;
|
||||
let parsed: EmbeddingResponse = serde_json::from_str(raw).expect("should parse");
|
||||
match parsed {
|
||||
EmbeddingResponse::Success { data, .. } => {
|
||||
assert_eq!(data.len(), 2);
|
||||
assert_eq!(data[0].index, 0);
|
||||
assert_eq!(data[1].index, 1);
|
||||
}
|
||||
_ => panic!("should be success"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
use serde_json::json;
|
||||
|
||||
/// Minimal audit logger - records version snapshots on mutation
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
---
|
||||
# Tekton Task: Integration Tests for Poimen Memory Service
|
||||
#
|
||||
# Executes:
|
||||
# 1. Database migrations
|
||||
# 2. Integration test suites (cargo test)
|
||||
# 3. Reports results
|
||||
#
|
||||
# Parameters:
|
||||
# - image: Docker image with SHA to test
|
||||
#
|
||||
# Results:
|
||||
# - summary: Test summary (pass/fail + count)
|
||||
|
||||
apiVersion: tekton.dev/v1
|
||||
kind: Task
|
||||
metadata:
|
||||
name: poimen-integration-test
|
||||
namespace: poimen
|
||||
spec:
|
||||
params:
|
||||
- name: image
|
||||
type: string
|
||||
description: "Docker image SHA to test (e.g., forgejo.riotpiao.com/riotpiao-poimen/poimen-memory:abc123)"
|
||||
|
||||
results:
|
||||
- name: summary
|
||||
description: "Test summary: PASS or FAIL + test count"
|
||||
|
||||
steps:
|
||||
# Step 1: Apply database migrations
|
||||
- name: migrate
|
||||
image: $(params.image)
|
||||
env:
|
||||
- name: DB_HOST
|
||||
value: "memory-db-rw.poimen.svc.cluster.local"
|
||||
- name: DB_PORT
|
||||
value: "5432"
|
||||
- name: DB_NAME
|
||||
value: "memory"
|
||||
- name: DB_USER
|
||||
value: "app"
|
||||
- name: DB_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: memory-db-app
|
||||
key: password
|
||||
|
||||
script: |
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=========================================="
|
||||
echo "Step 1: Database Migrations"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
# Run migrations
|
||||
/app/migrations/run_migrations.sh
|
||||
|
||||
echo ""
|
||||
echo "✓ Migrations complete"
|
||||
|
||||
# Step 2: Run integration tests
|
||||
- name: test
|
||||
image: $(params.image)
|
||||
env:
|
||||
- name: DATABASE_URL
|
||||
value: "postgresql://[email protected]:5432/memory"
|
||||
- name: RUST_LOG
|
||||
value: "info,mem_cli=debug,mem_ingest=debug,mem_store=debug"
|
||||
- name: MEM_AUTH_MODE
|
||||
value: "none"
|
||||
- name: SQLX_OFFLINE
|
||||
value: "true"
|
||||
- name: PGPASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: memory-db-app
|
||||
key: password
|
||||
|
||||
script: |
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=========================================="
|
||||
echo "Step 2: Integration Tests"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
TEST_SUITES=(
|
||||
"it_phase3_phase4"
|
||||
"it_unified_query_4_6"
|
||||
"it_temporal_filtering_4_2_fixed"
|
||||
)
|
||||
|
||||
PASSED=0
|
||||
FAILED=0
|
||||
|
||||
for suite in "${TEST_SUITES[@]}"; do
|
||||
echo "Running: $suite"
|
||||
if cargo test --test "$suite" --lib 2>&1 | tail -50; then
|
||||
((PASSED++))
|
||||
echo "✓ $suite passed"
|
||||
else
|
||||
((FAILED++))
|
||||
echo "✗ $suite failed"
|
||||
fi
|
||||
echo ""
|
||||
done
|
||||
|
||||
# Run unit tests
|
||||
echo "Running unit tests..."
|
||||
if cargo test --lib mem_ingest 2>&1 | tail -100; then
|
||||
echo "✓ mem_ingest passed"
|
||||
else
|
||||
((FAILED++))
|
||||
echo "✗ mem_ingest failed"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
if cargo test --lib mem_cli::query 2>&1 | tail -100; then
|
||||
echo "✓ mem_cli::query passed"
|
||||
else
|
||||
((FAILED++))
|
||||
echo "✗ mem_cli::query failed"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "Test Summary: $PASSED passed, $FAILED failed"
|
||||
echo "=========================================="
|
||||
|
||||
if [ $FAILED -eq 0 ]; then
|
||||
echo "PASS: All integration tests passed"
|
||||
echo "PASS: All integration tests passed" > /tekton/results/summary
|
||||
exit 0
|
||||
else
|
||||
echo "FAIL: $FAILED test suite(s) failed"
|
||||
echo "FAIL: $FAILED test suite(s) failed" > /tekton/results/summary
|
||||
exit 1
|
||||
fi
|
||||
|
||||
resources:
|
||||
requests:
|
||||
memory: "1Gi"
|
||||
cpu: "500m"
|
||||
limits:
|
||||
memory: "2Gi"
|
||||
cpu: "2000m"
|
||||
@@ -0,0 +1,121 @@
|
||||
---
|
||||
# Tekton Pipeline: Poimen Memory Service CI/CD
|
||||
#
|
||||
# Orchestrates:
|
||||
# 1. integration-test-task: Run integration tests against image
|
||||
# 2. (Future) build-task: Build Docker image
|
||||
# 3. (Future) promote-task: Promote image to :latest
|
||||
#
|
||||
# Parameters:
|
||||
# - image: Docker image with SHA to test
|
||||
# - registry-user: Registry credentials
|
||||
# - registry-token: Registry credentials
|
||||
|
||||
apiVersion: tekton.dev/v1
|
||||
kind: Pipeline
|
||||
metadata:
|
||||
name: poimen-ci
|
||||
namespace: poimen
|
||||
spec:
|
||||
params:
|
||||
- name: image
|
||||
type: string
|
||||
description: "Docker image SHA to test (e.g., forgejo.riotpiao.com/riotpiao-poimen/poimen-memory:abc123)"
|
||||
|
||||
- name: registry-user
|
||||
type: string
|
||||
description: "Registry username"
|
||||
default: ""
|
||||
|
||||
- name: registry-token
|
||||
type: string
|
||||
description: "Registry token/password"
|
||||
default: ""
|
||||
|
||||
tasks:
|
||||
# Task 1: Integration Tests
|
||||
- name: integration-tests
|
||||
taskRef:
|
||||
name: poimen-integration-test
|
||||
params:
|
||||
- name: image
|
||||
value: $(params.image)
|
||||
|
||||
# Task 2: Gate on test results
|
||||
- name: gate-on-tests
|
||||
runAfter:
|
||||
- integration-tests
|
||||
taskSpec:
|
||||
steps:
|
||||
- name: check-results
|
||||
image: alpine:latest
|
||||
script: |
|
||||
#!/bin/sh
|
||||
set -e
|
||||
echo "✓ Integration tests passed, proceeding with promotion"
|
||||
|
||||
# Task 3: Promote image (placeholder - will be implemented)
|
||||
- name: promote-image
|
||||
runAfter:
|
||||
- gate-on-tests
|
||||
taskSpec:
|
||||
params:
|
||||
- name: image
|
||||
type: string
|
||||
- name: registry-user
|
||||
type: string
|
||||
- name: registry-token
|
||||
type: string
|
||||
|
||||
steps:
|
||||
- name: promote
|
||||
image: docker:latest
|
||||
env:
|
||||
- name: IMAGE
|
||||
value: $(params.image)
|
||||
- name: REGISTRY_USER
|
||||
value: $(params.registry-user)
|
||||
- name: REGISTRY_TOKEN
|
||||
value: $(params.registry-token)
|
||||
script: |
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
echo "Promoting image to :latest..."
|
||||
|
||||
# Extract registry and repo from image
|
||||
# e.g., forgejo.riotpiao.com/riotpiao-poimen/poimen-memory:abc123
|
||||
REGISTRY=$(echo $IMAGE | cut -d/ -f1)
|
||||
REPO=$(echo $IMAGE | cut -d: -f1)
|
||||
SHA=$(echo $IMAGE | cut -d: -f2)
|
||||
|
||||
echo "Registry: $REGISTRY"
|
||||
echo "Repo: $REPO"
|
||||
echo "SHA: $SHA"
|
||||
echo ""
|
||||
|
||||
# Login and promote
|
||||
echo "$REGISTRY_TOKEN" | docker login -u "$REGISTRY_USER" --password-stdin "$REGISTRY"
|
||||
docker pull "$IMAGE"
|
||||
docker tag "$IMAGE" "${REPO}:latest"
|
||||
docker push "${REPO}:latest"
|
||||
|
||||
echo "✓ Promoted to :latest"
|
||||
|
||||
params:
|
||||
- name: image
|
||||
value: $(params.image)
|
||||
- name: registry-user
|
||||
value: $(params.registry-user)
|
||||
- name: registry-token
|
||||
value: $(params.registry-token)
|
||||
|
||||
finally:
|
||||
- name: cleanup
|
||||
taskSpec:
|
||||
steps:
|
||||
- name: cleanup-tasks
|
||||
image: alpine:latest
|
||||
script: |
|
||||
#!/bin/sh
|
||||
echo "Pipeline execution complete"
|
||||
Executable
+127
@@ -0,0 +1,127 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Database Migration Runner
|
||||
# Used by K8s Job to apply all migrations before integration tests
|
||||
#
|
||||
# Environment variables (from K8s):
|
||||
# DB_HOST - PostgreSQL host
|
||||
# DB_PORT - PostgreSQL port
|
||||
# DB_NAME - Database name
|
||||
# DB_USER - Database user
|
||||
# DB_PASSWORD - Database password (from Secret)
|
||||
|
||||
set -e
|
||||
|
||||
DB_HOST="${DB_HOST:-memory-db-rw.poimen.svc.cluster.local}"
|
||||
DB_PORT="${DB_PORT:-5432}"
|
||||
DB_NAME="${DB_NAME:-memory}"
|
||||
DB_USER="${DB_USER:-app}"
|
||||
|
||||
if [ -z "$DB_PASSWORD" ]; then
|
||||
echo "ERROR: DB_PASSWORD not set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=========================================="
|
||||
echo "Database Migration Runner"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
echo "Configuration:"
|
||||
echo " Host: $DB_HOST:$DB_PORT"
|
||||
echo " Database: $DB_NAME"
|
||||
echo " User: $DB_USER"
|
||||
echo ""
|
||||
|
||||
# Export for psql
|
||||
export PGPASSWORD="$DB_PASSWORD"
|
||||
|
||||
# Get migration directory (where this script is)
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
MIGRATION_DIR="$SCRIPT_DIR"
|
||||
|
||||
echo "Migration directory: $MIGRATION_DIR"
|
||||
echo ""
|
||||
|
||||
# Collect all SQL files
|
||||
MIGRATIONS=($(ls -1 "$MIGRATION_DIR"/*.sql 2>/dev/null | sort))
|
||||
|
||||
if [ ${#MIGRATIONS[@]} -eq 0 ]; then
|
||||
echo "ERROR: No migration files found in $MIGRATION_DIR"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Found ${#MIGRATIONS[@]} migration(s):"
|
||||
for m in "${MIGRATIONS[@]}"; do
|
||||
echo " - $(basename $m)"
|
||||
done
|
||||
echo ""
|
||||
|
||||
# Wait for DB to be ready
|
||||
echo "Waiting for database to be ready..."
|
||||
for i in {1..30}; do
|
||||
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" >/dev/null 2>&1; then
|
||||
echo "✓ Database is ready"
|
||||
break
|
||||
fi
|
||||
if [ $i -eq 30 ]; then
|
||||
echo "✗ Database not ready after 30 attempts"
|
||||
exit 1
|
||||
fi
|
||||
echo " Attempt $i/30..."
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "Running Migrations"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
SUCCESS=0
|
||||
FAILED=0
|
||||
|
||||
for migration in "${MIGRATIONS[@]}"; do
|
||||
name=$(basename "$migration")
|
||||
echo -n "▶ $name ... "
|
||||
|
||||
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$migration" >/dev/null 2>&1; then
|
||||
echo "✓"
|
||||
((SUCCESS++))
|
||||
else
|
||||
echo "✗ FAILED"
|
||||
echo ""
|
||||
echo "Error output:"
|
||||
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$migration" 2>&1 | sed 's/^/ /'
|
||||
((FAILED++))
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "Migration Summary"
|
||||
echo "=========================================="
|
||||
echo " Success: $SUCCESS"
|
||||
echo " Failed: $FAILED"
|
||||
echo ""
|
||||
|
||||
if [ $FAILED -eq 0 ]; then
|
||||
echo "✓ All migrations applied successfully"
|
||||
|
||||
echo ""
|
||||
echo "Verifying schema..."
|
||||
echo ""
|
||||
|
||||
# Verify key tables exist
|
||||
for table in memory_entity memory_edge ingest_jobs; do
|
||||
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1 FROM information_schema.tables WHERE table_name='$table';" 2>&1 | grep -q "1 row"; then
|
||||
echo " ✓ Table $table exists"
|
||||
else
|
||||
echo " ⚠ Table $table not found"
|
||||
fi
|
||||
done
|
||||
|
||||
exit 0
|
||||
else
|
||||
echo "✗ Some migrations failed"
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user