@haaaiawd/second-nature 0.1.51 → 0.2.0

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.
Files changed (71) hide show
  1. package/openclaw.plugin.json +29 -29
  2. package/package.json +55 -55
  3. package/runtime/cli/commands/index.js +326 -325
  4. package/runtime/cli/ops/heartbeat-surface.d.ts +84 -84
  5. package/runtime/cli/ops/heartbeat-surface.js +100 -100
  6. package/runtime/cli/ops/ops-router.js +1555 -1482
  7. package/runtime/cli/ops/workspace-heartbeat-runner.d.ts +85 -85
  8. package/runtime/cli/ops/workspace-heartbeat-runner.js +242 -242
  9. package/runtime/connectors/base/contract.d.ts +111 -111
  10. package/runtime/connectors/base/failure-taxonomy.d.ts +13 -13
  11. package/runtime/connectors/base/failure-taxonomy.js +186 -186
  12. package/runtime/connectors/base/map-life-evidence.js +137 -137
  13. package/runtime/connectors/base/policy-layer.js +202 -202
  14. package/runtime/connectors/evidence-normalizer.d.ts +45 -0
  15. package/runtime/connectors/evidence-normalizer.js +115 -0
  16. package/runtime/connectors/manifest/manifest-schema.d.ts +152 -152
  17. package/runtime/connectors/manifest/manifest-schema.js +54 -54
  18. package/runtime/connectors/services/connector-executor-adapter.d.ts +20 -20
  19. package/runtime/connectors/services/connector-executor-adapter.js +645 -645
  20. package/runtime/core/second-nature/action/action-closure-recorder.d.ts +70 -0
  21. package/runtime/core/second-nature/action/action-closure-recorder.js +184 -0
  22. package/runtime/core/second-nature/action/action-proposal-builder.d.ts +70 -0
  23. package/runtime/core/second-nature/action/action-proposal-builder.js +217 -0
  24. package/runtime/core/second-nature/action/autonomy-policy-evaluator.d.ts +43 -0
  25. package/runtime/core/second-nature/action/autonomy-policy-evaluator.js +213 -0
  26. package/runtime/core/second-nature/action/policy-bound-dispatch.d.ts +69 -0
  27. package/runtime/core/second-nature/action/policy-bound-dispatch.js +112 -0
  28. package/runtime/core/second-nature/body/tool-affordance/affordance-side-effect.d.ts +49 -0
  29. package/runtime/core/second-nature/body/tool-affordance/affordance-side-effect.js +100 -0
  30. package/runtime/core/second-nature/control-plane/accepted-projection-loader.d.ts +45 -0
  31. package/runtime/core/second-nature/control-plane/accepted-projection-loader.js +85 -0
  32. package/runtime/core/second-nature/control-plane/heartbeat-orchestrator.d.ts +38 -0
  33. package/runtime/core/second-nature/control-plane/heartbeat-orchestrator.js +165 -0
  34. package/runtime/core/second-nature/guidance/guidance-proposal-consumer.d.ts +51 -0
  35. package/runtime/core/second-nature/guidance/guidance-proposal-consumer.js +113 -0
  36. package/runtime/core/second-nature/heartbeat/goal-lifecycle-policy.d.ts +24 -24
  37. package/runtime/core/second-nature/heartbeat/goal-lifecycle-policy.js +61 -61
  38. package/runtime/core/second-nature/heartbeat/heartbeat-loop.d.ts +97 -97
  39. package/runtime/core/second-nature/heartbeat/heartbeat-loop.js +397 -397
  40. package/runtime/core/second-nature/orchestrator/platform-capability-router.js +149 -149
  41. package/runtime/core/second-nature/perception/judgment-engine.d.ts +53 -0
  42. package/runtime/core/second-nature/perception/judgment-engine.js +239 -0
  43. package/runtime/core/second-nature/perception/perception-builder.d.ts +62 -0
  44. package/runtime/core/second-nature/perception/perception-builder.js +208 -0
  45. package/runtime/core/second-nature/perception/sensitivity-classifier.d.ts +37 -0
  46. package/runtime/core/second-nature/perception/sensitivity-classifier.js +87 -0
  47. package/runtime/core/second-nature/quiet-dream/dream-consolidation-runner.d.ts +44 -0
  48. package/runtime/core/second-nature/quiet-dream/dream-consolidation-runner.js +180 -0
  49. package/runtime/core/second-nature/quiet-dream/dream-scheduler.d.ts +36 -0
  50. package/runtime/core/second-nature/quiet-dream/dream-scheduler.js +105 -0
  51. package/runtime/core/second-nature/quiet-dream/memory-projection-lifecycle.d.ts +36 -0
  52. package/runtime/core/second-nature/quiet-dream/memory-projection-lifecycle.js +151 -0
  53. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.d.ts +46 -0
  54. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.js +123 -0
  55. package/runtime/observability/causal-loop-health.d.ts +44 -0
  56. package/runtime/observability/causal-loop-health.js +118 -0
  57. package/runtime/observability/diagnostic-redaction.d.ts +43 -0
  58. package/runtime/observability/diagnostic-redaction.js +114 -0
  59. package/runtime/observability/loop-stage-event-sink.d.ts +43 -0
  60. package/runtime/observability/loop-stage-event-sink.js +148 -0
  61. package/runtime/observability/loop-status.d.ts +46 -0
  62. package/runtime/observability/loop-status.js +85 -0
  63. package/runtime/shared/types/index.js +3 -0
  64. package/runtime/shared/types/v8-contracts.d.ts +86 -0
  65. package/runtime/shared/types/v8-contracts.js +84 -0
  66. package/runtime/storage/db/schema/index.d.ts +1 -0
  67. package/runtime/storage/db/schema/index.js +1 -0
  68. package/runtime/storage/db/schema/v8-entities.d.ts +1973 -0
  69. package/runtime/storage/db/schema/v8-entities.js +160 -0
  70. package/runtime/storage/v8-state-stores.d.ts +147 -0
  71. package/runtime/storage/v8-state-stores.js +491 -0
@@ -0,0 +1,160 @@
1
+ /**
2
+ * v8 Living Perception Loop entity schemas.
3
+ *
4
+ * Core logic: Persist EvidenceItem, PerceptionCard, JudgmentVerdict,
5
+ * ActionClosureRecord, QuietDailyReview, DreamConsolidationRun,
6
+ * LongTermMemoryProjection, HeartbeatCycleTrace, and LoopStageEvent.
7
+ *
8
+ * Design authority:
9
+ * - `.anws/v8/04_SYSTEM_DESIGN/state-memory-system.md`
10
+ * - `.anws/v8/04_SYSTEM_DESIGN/shared-v8-contracts.md`
11
+ *
12
+ * Dependencies: drizzle-orm/sqlite-core
13
+ * Boundary: Schema definitions only; no runtime logic.
14
+ */
15
+ import { sqliteTable, text, integer, real } from "drizzle-orm/sqlite-core";
16
+ // ───────────────────────────────────────────────────────────────
17
+ // 1. EvidenceItem
18
+ // ───────────────────────────────────────────────────────────────
19
+ export const evidenceItem = sqliteTable("evidence_item", {
20
+ id: text("id").primaryKey(),
21
+ createdAt: text("created_at").notNull(),
22
+ platformId: text("platform_id").notNull(),
23
+ contentHash: text("content_hash").notNull(),
24
+ observedAt: text("observed_at").notNull(),
25
+ sensitivityHint: text("sensitivity_hint"),
26
+ sourceRefsJson: text("source_refs_json").notNull(),
27
+ redactionClass: text("redaction_class").notNull().default("none"),
28
+ payloadJson: text("payload_json"),
29
+ lifecycleStatus: text("lifecycle_status").notNull().default("pending"),
30
+ });
31
+ // ───────────────────────────────────────────────────────────────
32
+ // 2. PerceptionCard
33
+ // ───────────────────────────────────────────────────────────────
34
+ export const perceptionCard = sqliteTable("perception_card", {
35
+ id: text("id").primaryKey(),
36
+ createdAt: text("created_at").notNull(),
37
+ cycleId: text("cycle_id").notNull(),
38
+ topic: text("topic"),
39
+ entitiesJson: text("entities_json"),
40
+ novelty: text("novelty"),
41
+ relevance: real("relevance"),
42
+ summary: text("summary"),
43
+ riskFlagsJson: text("risk_flags_json"),
44
+ confidence: real("confidence"),
45
+ reviewPriority: text("review_priority"),
46
+ sourceRefsJson: text("source_refs_json").notNull(),
47
+ redactionClass: text("redaction_class").notNull().default("none"),
48
+ payloadJson: text("payload_json"),
49
+ lifecycleStatus: text("lifecycle_status").notNull().default("pending"),
50
+ });
51
+ // ───────────────────────────────────────────────────────────────
52
+ // 3. JudgmentVerdict
53
+ // ───────────────────────────────────────────────────────────────
54
+ export const judgmentVerdict = sqliteTable("judgment_verdict", {
55
+ id: text("id").primaryKey(),
56
+ createdAt: text("created_at").notNull(),
57
+ cycleId: text("cycle_id").notNull(),
58
+ perceptionCardId: text("perception_card_id").notNull(),
59
+ actionKind: text("action_kind").notNull(),
60
+ confidence: real("confidence"),
61
+ reason: text("reason"),
62
+ riskPosture: text("risk_posture"),
63
+ sourceRefsJson: text("source_refs_json").notNull(),
64
+ redactionClass: text("redaction_class").notNull().default("none"),
65
+ payloadJson: text("payload_json"),
66
+ lifecycleStatus: text("lifecycle_status").notNull().default("pending"),
67
+ });
68
+ // ───────────────────────────────────────────────────────────────
69
+ // 4. ActionClosureRecord
70
+ // ───────────────────────────────────────────────────────────────
71
+ export const actionClosureRecord = sqliteTable("action_closure_record", {
72
+ id: text("id").primaryKey(),
73
+ createdAt: text("created_at").notNull(),
74
+ cycleId: text("cycle_id").notNull(),
75
+ proposalId: text("proposal_id"),
76
+ decisionId: text("decision_id"),
77
+ status: text("status").notNull(),
78
+ reason: text("reason"),
79
+ nextState: text("next_state"),
80
+ sourceRefsJson: text("source_refs_json").notNull(),
81
+ redactionClass: text("redaction_class").notNull().default("none"),
82
+ payloadJson: text("payload_json"),
83
+ lifecycleStatus: text("lifecycle_status").notNull().default("closed"),
84
+ });
85
+ // ───────────────────────────────────────────────────────────────
86
+ // 5. QuietDailyReview
87
+ // ───────────────────────────────────────────────────────────────
88
+ export const quietDailyReview = sqliteTable("quiet_daily_review", {
89
+ id: text("id").primaryKey(),
90
+ createdAt: text("created_at").notNull(),
91
+ day: text("day").notNull(),
92
+ closureCount: integer("closure_count").notNull().default(0),
93
+ memoryCandidateCount: integer("memory_candidate_count").notNull().default(0),
94
+ sourceRefsJson: text("source_refs_json").notNull(),
95
+ redactionClass: text("redaction_class").notNull().default("none"),
96
+ payloadJson: text("payload_json"),
97
+ lifecycleStatus: text("lifecycle_status").notNull().default("pending"),
98
+ });
99
+ // ───────────────────────────────────────────────────────────────
100
+ // 6. DreamConsolidationRun
101
+ // ───────────────────────────────────────────────────────────────
102
+ export const dreamConsolidationRun = sqliteTable("dream_consolidation_run", {
103
+ id: text("id").primaryKey(),
104
+ createdAt: text("created_at").notNull(),
105
+ quietReviewId: text("quiet_review_id").notNull(),
106
+ status: text("status").notNull(),
107
+ reason: text("reason"),
108
+ sourceRefsJson: text("source_refs_json").notNull(),
109
+ redactionClass: text("redaction_class").notNull().default("none"),
110
+ payloadJson: text("payload_json"),
111
+ lifecycleStatus: text("lifecycle_status").notNull().default("pending"),
112
+ });
113
+ // ───────────────────────────────────────────────────────────────
114
+ // 7. LongTermMemoryProjection
115
+ // ───────────────────────────────────────────────────────────────
116
+ export const longTermMemoryProjection = sqliteTable("long_term_memory_projection", {
117
+ id: text("id").primaryKey(),
118
+ createdAt: text("created_at").notNull(),
119
+ candidateId: text("candidate_id").notNull(),
120
+ topicKey: text("topic_key").notNull(),
121
+ status: text("status").notNull().default("candidate"),
122
+ sourceRefsJson: text("source_refs_json").notNull(),
123
+ redactionClass: text("redaction_class").notNull().default("none"),
124
+ payloadJson: text("payload_json"),
125
+ lifecycleStatus: text("lifecycle_status").notNull().default("candidate"),
126
+ });
127
+ // ───────────────────────────────────────────────────────────────
128
+ // 8. HeartbeatCycleTrace
129
+ // ───────────────────────────────────────────────────────────────
130
+ export const heartbeatCycleTrace = sqliteTable("heartbeat_cycle_trace", {
131
+ id: text("id").primaryKey(),
132
+ cycleSequence: integer("cycle_sequence").notNull(),
133
+ heartbeatStartedAt: text("heartbeat_started_at").notNull(),
134
+ heartbeatCompletedAt: text("heartbeat_completed_at"),
135
+ inputCount: integer("input_count").notNull().default(0),
136
+ outputCount: integer("output_count").notNull().default(0),
137
+ expectedDownstreamByCycle: integer("expected_downstream_by_cycle"),
138
+ status: text("status").notNull(),
139
+ sourceRefsJson: text("source_refs_json"),
140
+ redactionClass: text("redaction_class").notNull().default("none"),
141
+ payloadJson: text("payload_json"),
142
+ lifecycleStatus: text("lifecycle_status").notNull().default("started"),
143
+ });
144
+ // ───────────────────────────────────────────────────────────────
145
+ // 9. LoopStageEvent
146
+ // ───────────────────────────────────────────────────────────────
147
+ export const loopStageEvent = sqliteTable("loop_stage_event", {
148
+ id: text("id").primaryKey(),
149
+ cycleId: text("cycle_id").notNull(),
150
+ cycleSequence: integer("cycle_sequence").notNull(),
151
+ stage: text("stage").notNull(),
152
+ status: text("status").notNull(),
153
+ reason: text("reason"),
154
+ sourceRefsJson: text("source_refs_json").notNull(),
155
+ redactionClass: text("redaction_class").notNull().default("none"),
156
+ occurredAt: text("occurred_at").notNull(),
157
+ expectedDownstreamByCycle: integer("expected_downstream_by_cycle"),
158
+ payloadJson: text("payload_json"),
159
+ lifecycleStatus: text("lifecycle_status").notNull().default("started"),
160
+ });
@@ -0,0 +1,147 @@
1
+ /**
2
+ * v8 State Stores — Bounded write/read ports for Living Perception Loop entities.
3
+ *
4
+ * Core logic: Persist and retrieve EvidenceItem, PerceptionCard, JudgmentVerdict,
5
+ * ActionClosureRecord, QuietDailyReview, DreamConsolidationRun,
6
+ * LongTermMemoryProjection, HeartbeatCycleTrace, and LoopStageEvent.
7
+ *
8
+ * Design authority:
9
+ * - `.anws/v8/04_SYSTEM_DESIGN/state-memory-system.md`
10
+ * - `.anws/v8/04_SYSTEM_DESIGN/shared-v8-contracts.md`
11
+ *
12
+ * Dependencies:
13
+ * - drizzle-orm (SQLite)
14
+ * - `src/storage/db/schema/v8-entities.js`
15
+ * - `src/shared/types/v8-contracts.js` (SourceRef, DegradedOperationResult)
16
+ *
17
+ * Boundary:
18
+ * - Write validation: rejects missing source refs, checks redaction class.
19
+ * - Read models: bounded by family + status filters; no cross-family joins.
20
+ * - Degraded state: returns DegradedOperationResult on DB failure, never throws.
21
+ *
22
+ * Test coverage: tests/unit/storage/v8-state-stores.test.ts
23
+ */
24
+ import type { StateDatabase } from "./db/index.js";
25
+ import { type EvidenceItemRecord, type NewEvidenceItemRecord, type PerceptionCardRecord, type NewPerceptionCardRecord, type JudgmentVerdictRecord, type NewJudgmentVerdictRecord, type ActionClosureRecordSelect, type ActionClosureRecordInsert, type QuietDailyReviewRecord, type NewQuietDailyReviewRecord, type DreamConsolidationRunRecord, type NewDreamConsolidationRunRecord, type LongTermMemoryProjectionRecord, type NewLongTermMemoryProjectionRecord, type HeartbeatCycleTraceRecord, type NewHeartbeatCycleTraceRecord, type LoopStageEventRecord, type NewLoopStageEventRecord } from "./db/schema/v8-entities.js";
26
+ import type { SourceRef, DegradedOperationResult } from "../shared/types/v8-contracts.js";
27
+ export interface WriteValidationError {
28
+ ok: false;
29
+ degraded: DegradedOperationResult;
30
+ }
31
+ export interface WriteValidationOk<T> {
32
+ ok: true;
33
+ record: T;
34
+ }
35
+ export type WriteValidationResult<T> = WriteValidationOk<T> | WriteValidationError;
36
+ export declare function writeEvidenceItem(db: StateDatabase, row: Omit<NewEvidenceItemRecord, "sourceRefsJson"> & {
37
+ sourceRefs: SourceRef[];
38
+ }): Promise<{
39
+ id: string;
40
+ } | DegradedOperationResult>;
41
+ export declare function readEvidenceItemsByStatus(db: StateDatabase, lifecycleStatus: EvidenceItemRecord["lifecycleStatus"]): Promise<{
42
+ rows: EvidenceItemRecord[];
43
+ degraded?: DegradedOperationResult;
44
+ }>;
45
+ export declare function writePerceptionCard(db: StateDatabase, row: Omit<NewPerceptionCardRecord, "sourceRefsJson"> & {
46
+ sourceRefs: SourceRef[];
47
+ }): Promise<{
48
+ id: string;
49
+ } | DegradedOperationResult>;
50
+ export declare function readPerceptionCardsByCycle(db: StateDatabase, cycleId: string): Promise<{
51
+ rows: PerceptionCardRecord[];
52
+ degraded?: DegradedOperationResult;
53
+ }>;
54
+ export declare function readPerceptionCardById(db: StateDatabase, id: string): Promise<{
55
+ row?: PerceptionCardRecord;
56
+ degraded?: DegradedOperationResult;
57
+ }>;
58
+ export declare function writeJudgmentVerdict(db: StateDatabase, row: Omit<NewJudgmentVerdictRecord, "sourceRefsJson"> & {
59
+ sourceRefs: SourceRef[];
60
+ }): Promise<{
61
+ id: string;
62
+ } | DegradedOperationResult>;
63
+ export declare function readJudgmentVerdictsByCycle(db: StateDatabase, cycleId: string): Promise<{
64
+ rows: JudgmentVerdictRecord[];
65
+ degraded?: DegradedOperationResult;
66
+ }>;
67
+ export declare function readJudgmentVerdictById(db: StateDatabase, id: string): Promise<{
68
+ row?: JudgmentVerdictRecord;
69
+ degraded?: DegradedOperationResult;
70
+ }>;
71
+ export declare function writeActionClosureRecord(db: StateDatabase, row: Omit<ActionClosureRecordInsert, "sourceRefsJson"> & {
72
+ sourceRefs: SourceRef[];
73
+ }): Promise<{
74
+ id: string;
75
+ } | DegradedOperationResult>;
76
+ export declare function readActionClosuresByCycle(db: StateDatabase, cycleId: string): Promise<{
77
+ rows: ActionClosureRecordSelect[];
78
+ degraded?: DegradedOperationResult;
79
+ }>;
80
+ export declare function readActionClosuresByDay(db: StateDatabase, day: string): Promise<{
81
+ rows: ActionClosureRecordSelect[];
82
+ degraded?: DegradedOperationResult;
83
+ }>;
84
+ export declare function writeQuietDailyReview(db: StateDatabase, row: Omit<NewQuietDailyReviewRecord, "sourceRefsJson"> & {
85
+ sourceRefs: SourceRef[];
86
+ }): Promise<{
87
+ id: string;
88
+ } | DegradedOperationResult>;
89
+ export declare function readQuietDailyReviewById(db: StateDatabase, id: string): Promise<{
90
+ row?: QuietDailyReviewRecord;
91
+ degraded?: DegradedOperationResult;
92
+ }>;
93
+ export declare function readQuietDailyReviewsByDay(db: StateDatabase, day: string): Promise<{
94
+ rows: QuietDailyReviewRecord[];
95
+ degraded?: DegradedOperationResult;
96
+ }>;
97
+ export declare function writeDreamConsolidationRun(db: StateDatabase, row: Omit<NewDreamConsolidationRunRecord, "sourceRefsJson"> & {
98
+ sourceRefs: SourceRef[];
99
+ }): Promise<{
100
+ id: string;
101
+ } | DegradedOperationResult>;
102
+ export declare function readDreamConsolidationRunById(db: StateDatabase, id: string): Promise<{
103
+ row?: DreamConsolidationRunRecord;
104
+ degraded?: DegradedOperationResult;
105
+ }>;
106
+ export declare function readDreamConsolidationRunsByQuietId(db: StateDatabase, quietReviewId: string): Promise<{
107
+ rows: DreamConsolidationRunRecord[];
108
+ degraded?: DegradedOperationResult;
109
+ }>;
110
+ export declare function writeLongTermMemoryProjection(db: StateDatabase, row: Omit<NewLongTermMemoryProjectionRecord, "sourceRefsJson"> & {
111
+ sourceRefs: SourceRef[];
112
+ }): Promise<{
113
+ id: string;
114
+ } | DegradedOperationResult>;
115
+ export declare function readMemoryProjectionsByStatus(db: StateDatabase, status: LongTermMemoryProjectionRecord["status"]): Promise<{
116
+ rows: LongTermMemoryProjectionRecord[];
117
+ degraded?: DegradedOperationResult;
118
+ }>;
119
+ export declare function readMemoryProjectionsByTopic(db: StateDatabase, topicKey: string): Promise<{
120
+ rows: LongTermMemoryProjectionRecord[];
121
+ degraded?: DegradedOperationResult;
122
+ }>;
123
+ export declare function writeHeartbeatCycleTrace(db: StateDatabase, row: Omit<NewHeartbeatCycleTraceRecord, "sourceRefsJson"> & {
124
+ sourceRefs?: SourceRef[];
125
+ }): Promise<{
126
+ id: string;
127
+ } | DegradedOperationResult>;
128
+ export declare function readHeartbeatCycleTraces(db: StateDatabase, limit?: number): Promise<{
129
+ rows: HeartbeatCycleTraceRecord[];
130
+ degraded?: DegradedOperationResult;
131
+ }>;
132
+ export declare function writeLoopStageEvent(db: StateDatabase, row: Omit<NewLoopStageEventRecord, "sourceRefsJson"> & {
133
+ sourceRefs: SourceRef[];
134
+ }): Promise<{
135
+ id: string;
136
+ } | DegradedOperationResult>;
137
+ export declare function readLoopStageEventsByCycle(db: StateDatabase, cycleId: string): Promise<{
138
+ rows: LoopStageEventRecord[];
139
+ degraded?: DegradedOperationResult;
140
+ }>;
141
+ export declare function readLoopStageEventsByStage(db: StateDatabase, stage: LoopStageEventRecord["stage"], limit?: number): Promise<{
142
+ rows: LoopStageEventRecord[];
143
+ degraded?: DegradedOperationResult;
144
+ }>;
145
+ export declare function extractSourceRefs(row: {
146
+ sourceRefsJson: string | null;
147
+ }): SourceRef[];