This commit is contained in:
Oleg Maslov
2026-09-02 10:14:22 +02:00
parent 0c3e2ead3b
commit b20b138fe4
771 changed files with 161561 additions and 9027 deletions

View File

@@ -65,6 +65,10 @@ export interface McpToolRetrievalSettings {
const DEFAULT_MODEL = 'claude-sonnet-4-6';
function getNonBlankEnv(name: string): string | undefined {
return process.env[name]?.trim() || undefined;
}
function getDefaultConfigDir(): string {
const home = process.env.HOME || process.env.USERPROFILE || os.homedir();
return path.join(home, '.waggle');
@@ -247,15 +251,15 @@ export class WaggleConfig {
getEmbeddingConfig(): EmbeddingProviderConfig {
const emb = this.data.embedding;
const config: EmbeddingProviderConfig = {
provider: (process.env.EMBEDDING_PROVIDER as EmbeddingProviderType | 'auto' | undefined) ?? emb?.provider ?? 'auto',
provider: (getNonBlankEnv('EMBEDDING_PROVIDER') as EmbeddingProviderType | 'auto' | undefined) ?? emb?.provider ?? 'auto',
targetDimensions: 1024,
inprocess: {
model: process.env.EMBEDDING_MODEL ?? emb?.inprocessModel,
model: getNonBlankEnv('EMBEDDING_MODEL') ?? emb?.inprocessModel,
cacheDir: path.join(this.configDir, 'models'),
},
ollama: {
baseUrl: process.env.OLLAMA_HOST ?? emb?.ollamaUrl,
model: process.env.OLLAMA_EMBED_MODEL ?? emb?.ollamaModel,
baseUrl: getNonBlankEnv('OLLAMA_HOST') ?? emb?.ollamaUrl,
model: getNonBlankEnv('OLLAMA_EMBED_MODEL') ?? emb?.ollamaModel,
},
// API keys injected separately from Vault — not stored in config.json
};

View File

@@ -20,7 +20,7 @@
import { createHash } from 'node:crypto';
import path from 'node:path';
import type { MindDB } from '@waggle/hive-mind-core';
import { FrameStore } from '@waggle/hive-mind-core';
import { evaluateExternalMemoryIngress, FrameStore } from '@waggle/hive-mind-core';
import { SessionStore } from '@waggle/hive-mind-core';
const FILE_INDEX_TABLE_SQL = `
@@ -57,7 +57,7 @@ export interface FileIndexRow {
}
export type FileIndexResult =
| { skipped: true; reason: 'unsupported_format' | 'unchanged' | 'empty' }
| { skipped: true; reason: 'unsupported_format' | 'unchanged' | 'empty' | 'unsafe_content' }
| { skipped: false; frameId: number; truncated: boolean };
export class FileIndexer {
@@ -121,13 +121,21 @@ export class FileIndexer {
| Record<string, unknown>
| undefined;
if (existingRow && existingRow.content_hash === hash) {
return { skipped: true, reason: 'unchanged' };
}
const normalized = this.normalizeText(content);
const truncated = content.length > MAX_CONTENT_BYTES;
const frameBody = this.buildFrameContent(filePath, normalized, truncated, mime);
if (evaluateExternalMemoryIngress({ content: frameBody }).action === 'block') {
// An unchanged row may pre-date this guard. Remove that already-indexed
// unsafe projection atomically; on a new unsafe overwrite, retain the
// prior benign index while the file write itself remains successful.
if (existingRow && existingRow.content_hash === hash) {
raw.transaction(() => this.removeFile(filePath))();
}
return { skipped: true, reason: 'unsafe_content' };
}
if (existingRow && existingRow.content_hash === hash) {
return { skipped: true, reason: 'unchanged' };
}
const gopId = this.ensureSession();
// Atomicity contract (L-20 BLOCKER-1 fix): createIFrame → (conditional
// old-frame delete) → UPDATE/INSERT on file_index must commit as one unit.

View File

@@ -2,7 +2,9 @@
//
// As of CC Sesija B 2026-04-30 (PM Q3 Plan A ratification), the substrate
// (mind/ + harvest/ + logger + injection-scanner) lives in @waggle/hive-mind-core
// and is distributed as Apache 2.0 OSS via subtree-split. This barrel re-exports
// and is distributed as Apache 2.0 OSS through a reviewed, maintainer-curated
// forward-port. Raw subtree-split output is never a publication source. This
// barrel re-exports
// substrate symbols for backward compatibility — existing consumers (packages/agent,
// apps/web, packages/server, etc.) keep their `import { ... } from '@waggle/core'`
// imports unchanged.
@@ -17,6 +19,9 @@ export {
// Logger + injection scanner
createCoreLogger, type CoreLogger,
scanForInjection, type ScanResult,
evaluateExternalMemoryIngress, projectExternalMemoryContent,
type ExternalMemoryIngressDecision, type ExternalMemoryIngressInput,
type ExternalMemoryProjectionInput,
// mind/ — memory substrate
MindDB, EmbeddingDimMismatchError,
type EmbeddingFingerprint, type FingerprintCheck,
@@ -144,7 +149,7 @@ export {
type OptimizationLogEntry, type CreateOptimizationLogInput,
} from './optimization-log.js';
// ── Compliance (AI Act) — stays in @waggle/core (NOT extracted per .github/sync.md) ──
// ── Compliance (AI Act) — stays in @waggle/core; excluded by curated export ──
export { InteractionStore } from './compliance/interaction-store.js';
export { ComplianceStatusChecker } from './compliance/status-checker.js';
export { ReportGenerator, type ReportGeneratorDeps } from './compliance/report-generator.js';

View File

@@ -63,9 +63,10 @@ export interface RecordAuditInput {
// P7/D15 A3: the CHECK lists are generated from the canonical @waggle/shared
// arrays via sqlInList, so the SQLite constraint and the TS union can no longer
// drift (divergence #14 — the old comment admitted "drift silently crashes
// record()"). The mirror DDL in hive-mind-core/src/mind/schema.ts stays a
// standalone literal (it's the OSS substrate, §7.5) but is locked to these same
// canonical lists by the parity test in install-audit-check-parity.test.ts.
// record()"). The matching DDL in hive-mind-core/src/mind/schema.ts stays a
// standalone private-monorepo literal and is locked to these same canonical
// lists by install-audit-check-parity.test.ts. It is stripped from the curated
// OSS export.
// P7/D15 #15: trust_source now also carries a CHECK (was unconstrained at the DB
// while the TS type claimed a closed set). Every historical value came from the
// typed AuditTrustSource (the pre-security-gate 6-set ⊂ the current 7-set), so

View File

@@ -25,6 +25,8 @@ export interface TeamSyncConfig {
displayName: string;
}
export type TeamSyncFetch = (url: string, init?: RequestInit) => Promise<Response>;
export interface SyncedFrame {
/** Server-side entity ID (UUID). */
remoteId: string;
@@ -88,10 +90,12 @@ export function entityToSyncedFrame(entity: {
*/
export class TeamSync {
private config: TeamSyncConfig;
private fetchTeamServer: TeamSyncFetch;
private lastSyncTimestamp: string | null = null;
constructor(config: TeamSyncConfig) {
constructor(config: TeamSyncConfig, fetchTeamServer: TeamSyncFetch) {
this.config = config;
this.fetchTeamServer = fetchTeamServer;
}
/**
@@ -102,7 +106,7 @@ export class TeamSync {
const entity = frameToEntity(frame, this.config.userId, this.config.displayName);
try {
const response = await fetch(
const response = await this.fetchTeamServer(
`${this.config.teamServerUrl}/api/teams/${this.config.teamSlug}/entities`,
{
method: 'POST',
@@ -137,7 +141,7 @@ export class TeamSync {
const url = `${this.config.teamServerUrl}/api/teams/${this.config.teamSlug}/entities?type=memory_frame`;
// Note: `since` filtering would require server-side support. For now, pull all and filter client-side.
const response = await fetch(url, {
const response = await this.fetchTeamServer(url, {
headers: {
'Authorization': `Bearer ${this.config.authToken}`,
},

View File

@@ -31,6 +31,14 @@ interface VaultRecord {
updatedAt: string;
}
interface ConnectorCredentialInput {
type: 'api_key' | 'oauth2' | 'bearer' | 'basic';
value: string;
refreshToken?: string;
expiresAt?: string;
scopes?: string[];
}
export class VaultStore {
private dataDir: string;
private vaultPath: string;
@@ -52,41 +60,118 @@ export class VaultStore {
/** Ensure the encryption key exists. Generate if missing. */
private ensureKey(): Buffer {
let key: Buffer;
let created = false;
if (fs.existsSync(this.keyPath)) {
const key = Buffer.from(fs.readFileSync(this.keyPath, 'utf-8').trim(), 'hex');
const keyStat = fs.lstatSync(this.keyPath);
if (!keyStat.isFile() || keyStat.isSymbolicLink()) {
throw new Error(`Vault key path is not a regular file: ${this.keyPath}`);
}
key = Buffer.from(fs.readFileSync(this.keyPath, 'utf-8').trim(), 'hex');
if (key.length !== KEY_LENGTH) {
throw new Error(
`Vault key file is corrupted — expected ${KEY_LENGTH} bytes, got ${key.length}. Delete ${this.keyPath} to regenerate.`
);
}
return key;
} else {
key = crypto.randomBytes(KEY_LENGTH);
if (process.platform !== 'win32') {
fs.writeFileSync(this.keyPath, key.toString('hex'), { mode: 0o600, flag: 'wx' });
}
created = true;
}
const key = crypto.randomBytes(KEY_LENGTH);
fs.writeFileSync(this.keyPath, key.toString('hex'), { mode: 0o600 });
// Review Critical #1: On Windows, restrict key file access to current user only.
// Previously used `require('node:child_process')` inline which fails silently under
// ESM (`type: module` in the sidecar) — the try/catch swallowed the ReferenceError
// and every Windows install left the vault key with no ACL restriction.
// Now imported statically at the top of the file; the try/catch only covers actual
// icacls failures (e.g. icacls.exe not on PATH in a minimal Windows image).
if (process.platform === 'win32') {
try {
// Resolve the current user via whoami (safer than process.env.USERNAME
// which can be absent or spoofed in containerized/scripted setups)
const currentUser = execFileSync('whoami', { encoding: 'utf-8' }).trim();
execFileSync('icacls', [
this.keyPath,
'/inheritance:r',
'/grant:r',
`${currentUser}:F`,
], { stdio: 'ignore' });
this.restrictWindowsKeyPermissions(created ? key : undefined);
} catch (err) {
log.warn('Could not restrict key file permissions via icacls — vault key may be readable by other users', err);
const reason = err instanceof Error ? err.message : String(err);
throw new Error(`Could not restrict vault key permissions on Windows: ${reason}.`);
}
}
return key;
}
private restrictWindowsKeyPermissions(newKey?: Buffer): void {
const systemRoot = process.env.SystemRoot?.trim();
const normalizedRoot = systemRoot ? path.win32.normalize(systemRoot) : '';
if (!/^[A-Za-z]:\\/.test(normalizedRoot)) {
throw new Error('SystemRoot is missing or is not a drive-rooted Windows path');
}
const system32 = path.win32.join(normalizedRoot, 'System32');
const powershell = path.win32.join(
system32,
'WindowsPowerShell',
'v1.0',
'powershell.exe',
);
const aclScript = [
"$ErrorActionPreference = 'Stop'",
'$keyPath = $env:WAGGLE_VAULT_KEY_PATH',
'$createKey = $env:WAGGLE_VAULT_CREATE_KEY -eq "1"',
'$createdByThisInvocation = $false',
'try {',
' $sid = [Security.Principal.WindowsIdentity]::GetCurrent().User',
' $full = [Security.AccessControl.FileSystemRights]::FullControl',
' $allow = [Security.AccessControl.AccessControlType]::Allow',
' $targetAcl = New-Object Security.AccessControl.FileSecurity',
' $targetAcl.SetOwner($sid)',
' $targetAcl.SetAccessRuleProtection($true, $false)',
' $targetAcl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule -ArgumentList @($sid, $full, $allow)))',
' if ($createKey) {',
' $keyHex = [Console]::In.ReadToEnd().Trim()',
' if ($keyHex -notmatch "^[0-9a-f]{64}$") { throw "Invalid generated vault key" }',
' $keyBytes = [Text.Encoding]::ASCII.GetBytes($keyHex)',
' $stream = [IO.FileStream]::new($keyPath, [IO.FileMode]::CreateNew, $full, [IO.FileShare]::None, 4096, [IO.FileOptions]::WriteThrough, $targetAcl)',
' $createdByThisInvocation = $true',
' try { $stream.Write($keyBytes, 0, $keyBytes.Length); $stream.Flush($true) } finally { $stream.Dispose() }',
' } else {',
' $existingAttributes = [IO.File]::GetAttributes($keyPath)',
' if (($existingAttributes -band [IO.FileAttributes]::Directory) -or ($existingAttributes -band [IO.FileAttributes]::ReparsePoint)) { throw "Vault key is not a regular file" }',
' [IO.File]::SetAccessControl($keyPath, $targetAcl)',
' }',
' $attributes = [IO.File]::GetAttributes($keyPath)',
' $length = [IO.FileInfo]::new($keyPath).Length',
' if (($attributes -band [IO.FileAttributes]::Directory) -or ($attributes -band [IO.FileAttributes]::ReparsePoint) -or $length -ne 64) { throw "Vault key payload is not a regular 64-byte file" }',
' $acl = [IO.File]::GetAccessControl($keyPath)',
' $ownerSid = $acl.GetOwner([Security.Principal.SecurityIdentifier])',
' $rules = @($acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier]))',
' $current = @($rules | Where-Object { -not $_.IsInherited -and $_.IdentityReference.Value -eq $sid.Value -and $_.AccessControlType -eq [Security.AccessControl.AccessControlType]::Allow })',
' $hasFull = @($current | Where-Object { ($_.FileSystemRights -band $full) -eq $full }).Count -eq 1',
' if (-not $acl.AreAccessRulesProtected -or $ownerSid.Value -ne $sid.Value -or $rules.Count -ne 1 -or -not $hasFull) { throw "Vault ACL is not current-user-only" }',
'} catch {',
' if ($createdByThisInvocation) { try { [IO.File]::Delete($keyPath) } catch {} }',
' throw',
'}',
].join('\n');
const childEnv: NodeJS.ProcessEnv = {
SystemRoot: normalizedRoot,
WINDIR: normalizedRoot,
WAGGLE_VAULT_KEY_PATH: this.keyPath,
WAGGLE_VAULT_CREATE_KEY: newKey ? '1' : '0',
};
for (const name of ['TEMP', 'TMP', 'ComSpec', 'SystemDrive', 'PROCESSOR_ARCHITECTURE']) {
const value = process.env[name];
if (value) childEnv[name] = value;
}
execFileSync(powershell, [
'-NoLogo',
'-NoProfile',
'-NonInteractive',
'-EncodedCommand',
Buffer.from(aclScript, 'utf16le').toString('base64'),
], {
encoding: 'utf-8',
env: childEnv,
input: newKey?.toString('hex') ?? '',
stdio: ['pipe', 'ignore', 'pipe'],
windowsHide: true,
});
}
/** Encrypt a plaintext string. Returns iv:authTag:ciphertext (all hex). */
private encrypt(plaintext: string): string {
const iv = crypto.randomBytes(IV_LENGTH);
@@ -233,26 +318,55 @@ export class VaultStore {
return name in vault;
}
/** Set a connector credential with typed metadata */
setConnectorCredential(connectorId: string, credential: {
type: 'api_key' | 'oauth2' | 'bearer' | 'basic';
value: string;
refreshToken?: string;
expiresAt?: string;
scopes?: string[];
}): void {
this.set(`connector:${connectorId}`, credential.value, {
credentialType: credential.type,
expiresAt: credential.expiresAt,
scopes: credential.scopes,
});
// Store refresh token as a separate encrypted entry (never in plaintext metadata)
if (credential.refreshToken) {
this.set(`connector:${connectorId}:refresh`, credential.refreshToken);
} else {
// Clear any previously stored refresh token if not provided
this.delete(`connector:${connectorId}:refresh`);
/** Set a connector credential with typed metadata. */
setConnectorCredential(connectorId: string, credential: ConnectorCredentialInput): void {
this.setConnectorCredentialBundle(connectorId, credential);
}
/**
* Persist a connector credential and its encrypted companion values with one
* vault-file replacement. This prevents a multi-field credential (for
* example Jira token + email + site origin) from being partially updated.
*/
setConnectorCredentialBundle(
connectorId: string,
credential: ConnectorCredentialInput,
relatedSecrets: Readonly<Record<string, string>> = {},
): void {
const secretEntries = Object.entries(relatedSecrets);
if (secretEntries.some(([suffix]) => !/^[a-z][a-z0-9_]*$/.test(suffix))) {
throw new TypeError('Invalid connector credential suffix');
}
const vault = this.readVault();
const updatedAt = new Date().toISOString();
vault[`connector:${connectorId}`] = {
encrypted: this.encrypt(credential.value),
metadata: {
credentialType: credential.type,
expiresAt: credential.expiresAt,
scopes: credential.scopes,
},
updatedAt,
};
const refreshKey = `connector:${connectorId}:refresh`;
if (credential.refreshToken) {
vault[refreshKey] = {
encrypted: this.encrypt(credential.refreshToken),
updatedAt,
};
} else {
delete vault[refreshKey];
}
for (const [suffix, value] of secretEntries) {
vault[`connector:${connectorId}:${suffix}`] = {
encrypted: this.encrypt(value),
updatedAt,
};
}
this.writeVault(vault);
}
/** Get a connector credential with typed metadata */