# @goosefactory/learning

GooseFactory Learning & Feedback Processing System — transforms human feedback into AI learning through a sophisticated 6-stage pipeline.

## Quick Start

### Start the Service

```bash
# Foreground (see logs directly)
./start-service.sh

# Background
./start-service.sh --bg

# Check status
./start-service.sh --status

# View logs
./start-service.sh --logs

# Stop
./start-service.sh --stop
```

### Or Run Manually

```bash
npx tsx src/service.ts
```

The service will start on **http://localhost:4001** (configure via `LEARNING_PORT` env var).

## API Endpoints

### Health Check
```bash
curl http://localhost:4001/health
```

### Submit Feedback
```bash
curl -X POST http://localhost:4001/feedback \
  -H "Content-Type: application/json" \
  -d @test-feedback.json
```

### Run Analysis
```bash
curl -X POST http://localhost:4001/analyze
```

## Architecture

### 6-Stage Pipeline

1. **Validate** - Zod schema validation, dedup, range checks
2. **Enrich** - Time categorization, theme extraction, sentiment analysis
3. **Store** - Append to JSONL, daily partitions
4. **Analyze** - Pattern detection, calibration, trends
5. **Act** - Update memory files, adjust thresholds
6. **Learn** - Self-improvement loops, autonomy tiers

### 8 Feedback Types

- **Decision** - Approve/reject/needs-work with reasons
- **Dimension Scores** - 1-10 ratings across quality dimensions
- **Free Text** - Liked/disliked/general notes
- **Comparison** - A vs B preferences
- **Annotations** - Region-specific feedback (praise/criticism/questions)
- **Confidence** - Certainty levels and delegation decisions
- **Estimation** - Numeric estimates with context
- **Batch Decisions** - Bulk approve/reject flows

Plus: Temperature, Checklist, Ranking, Retrospective

### 5-Domain Pattern Analysis

1. **Approval Patterns** - By server type, pipeline stage
2. **Dimension Trends** - Quality metrics over time
3. **Theme Clustering** - NLP-based pattern extraction
4. **Calibration** - Confidence vs. actual accuracy
5. **Behavioral** - Time patterns, fatigue, anomalies

### Memory Files (4 Total)

- `feedback-patterns.md` - Recurring themes and anti-patterns
- `quality-standards.md` - Dimension thresholds and calibration
- `jake-preferences.md` - Personal taste model
- `improvement-log.md` - Changes to rules/thresholds/autonomy

## Data Storage

### Feedback Events
```
~/.config/goose/factory/feedback/raw/{YYYY-MM-DD}.jsonl
```
- One line per enriched feedback event
- Daily partitioned for easy archival
- Immutable append-only logs

### Memory Files
```
~/.config/goose/factory/memory/
```
- Markdown format for human readability
- Auto-updated on analysis runs
- Versioned with timestamps

## Configuration

Environment variables:
- `LEARNING_PORT` - Service port (default: 4001)
- `FEEDBACK_DATA_DIR` - Data directory (default: `~/.config/goose/factory`)

Pipeline config (in `service.ts`):
```typescript
{
  dataDir: DATA_DIR,
  memoryDir: `${DATA_DIR}/memory`,
  analysisThreshold: 10,      // Run analysis after N events
  analyzeOnEvery: false,      // Batch mode
  minDataPointsForRules: 5,   // Min data for rule generation
  calibrationEnabled: true,   // Enable confidence calibration
  analysisDays: 30,          // Days of data to analyze
}
```

## Testing

### Run Sample Test
```bash
# Submit test feedback with current timestamp
jq --arg ts "$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")" \
  '.timestamp = $ts' test-feedback.json | \
curl -X POST http://localhost:4001/feedback \
  -H "Content-Type: application/json" \
  -d @-
```

### Check Results
```bash
# View stored events
cat ~/.config/goose/factory/feedback/raw/*.jsonl | jq .

# View memory files
cat ~/.config/goose/factory/memory/*.md
```

## Integration

### From API
```typescript
import { processFeedback } from '@goosefactory/learning';

const result = await processFeedback(feedbackData, {
  dataDir: '~/.config/goose/factory',
  memoryDir: '~/.config/goose/factory/memory',
});
```

### HTTP Call
```bash
curl -X POST http://localhost:4001/feedback \
  -H "Content-Type: application/json" \
  -d '{
    "id": "feedback-123",
    "timestamp": "2026-02-07T12:00:00Z",
    "sessionId": "session-456",
    "modalType": "decision-modal",
    "workProduct": { ... },
    "feedback": { ... },
    "meta": { ... }
  }'
```

## Performance

- **Validation**: ~0.5ms per event
- **Enrichment**: ~0.3ms per event
- **Storage**: ~0.2ms per event
- **Analysis**: ~2ms for 30 days of data
- **Memory updates**: ~0.5ms per file

Total: **~1-2ms per feedback event**

## Self-Improvement Features

### Pre-Check (Coming Soon)
- Validate work products before generation
- Check against learned patterns
- Prevent predictable failures

### Prediction Tracking (Coming Soon)
- Store Buba's confidence predictions
- Compare against actual outcomes
- Continuous calibration

### Diminishing Review (Coming Soon)
- 5-tier autonomy system
- Auto-approve based on pattern confidence
- Regression detection and rollback

## Development

### Build
```bash
npm run build
```

### Type Check
```bash
npm run typecheck
```

### Watch Mode
```bash
npm run dev
```

## Files

- `src/index.ts` - Library exports
- `src/service.ts` - HTTP service
- `src/types.ts` - Zod schemas + TypeScript types
- `src/pipeline/` - 6-stage processing pipeline
- `src/storage/` - JSONL storage + queries
- `src/analysis/` - Pattern detection + calibration
- `src/memory/` - Memory file writers/readers
- `src/improvement/` - Self-improvement loops
- `src/metrics/` - KPIs and dashboard stats

## Status

✅ **All core features implemented and tested**

See [LEARNING_PIPELINE_REPORT.md](./LEARNING_PIPELINE_REPORT.md) for full test results.

## Next Steps

1. **Integration** - Connect to API and desktop app
2. **Monitoring** - Add metrics endpoint and alerts
3. **Enhancement** - Diminishing review, pre-check, prediction tracking
4. **UI** - Admin dashboard for memory file review

---

Part of the [GooseFactory](../../README.md) ecosystem.
