astrocode-workflow 0.3.1 → 0.3.3

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 (136) hide show
  1. package/dist/index.js +6 -0
  2. package/dist/shared/metrics.d.ts +66 -0
  3. package/dist/shared/metrics.js +112 -0
  4. package/dist/src/agents/commands.d.ts +9 -0
  5. package/dist/src/agents/commands.js +121 -0
  6. package/dist/src/agents/prompts.d.ts +3 -0
  7. package/dist/src/agents/prompts.js +232 -0
  8. package/dist/src/agents/registry.d.ts +6 -0
  9. package/dist/src/agents/registry.js +242 -0
  10. package/dist/src/agents/types.d.ts +14 -0
  11. package/dist/src/agents/types.js +8 -0
  12. package/dist/src/config/config-handler.d.ts +4 -0
  13. package/dist/src/config/config-handler.js +46 -0
  14. package/dist/src/config/defaults.d.ts +3 -0
  15. package/dist/src/config/defaults.js +3 -0
  16. package/dist/src/config/loader.d.ts +11 -0
  17. package/dist/src/config/loader.js +82 -0
  18. package/dist/src/config/schema.d.ts +194 -0
  19. package/dist/src/config/schema.js +223 -0
  20. package/dist/src/hooks/continuation-enforcer.d.ts +34 -0
  21. package/dist/src/hooks/continuation-enforcer.js +190 -0
  22. package/dist/src/hooks/inject-provider.d.ts +22 -0
  23. package/dist/src/hooks/inject-provider.js +120 -0
  24. package/dist/src/hooks/tool-output-truncator.d.ts +25 -0
  25. package/dist/src/hooks/tool-output-truncator.js +57 -0
  26. package/dist/src/index.d.ts +3 -0
  27. package/dist/src/index.js +308 -0
  28. package/dist/src/shared/deep-merge.d.ts +8 -0
  29. package/dist/src/shared/deep-merge.js +25 -0
  30. package/dist/src/shared/hash.d.ts +1 -0
  31. package/dist/src/shared/hash.js +4 -0
  32. package/dist/src/shared/log.d.ts +7 -0
  33. package/dist/src/shared/log.js +24 -0
  34. package/dist/src/shared/metrics.d.ts +66 -0
  35. package/dist/src/shared/metrics.js +112 -0
  36. package/dist/src/shared/model-tuning.d.ts +9 -0
  37. package/dist/src/shared/model-tuning.js +28 -0
  38. package/dist/src/shared/paths.d.ts +19 -0
  39. package/dist/src/shared/paths.js +64 -0
  40. package/dist/src/shared/text.d.ts +4 -0
  41. package/dist/src/shared/text.js +19 -0
  42. package/dist/src/shared/time.d.ts +1 -0
  43. package/dist/src/shared/time.js +3 -0
  44. package/dist/src/state/adapters/index.d.ts +41 -0
  45. package/dist/src/state/adapters/index.js +115 -0
  46. package/dist/src/state/db.d.ts +16 -0
  47. package/dist/src/state/db.js +225 -0
  48. package/dist/src/state/ids.d.ts +8 -0
  49. package/dist/src/state/ids.js +25 -0
  50. package/dist/src/state/repo-lock.d.ts +3 -0
  51. package/dist/src/state/repo-lock.js +29 -0
  52. package/dist/src/state/schema.d.ts +2 -0
  53. package/dist/src/state/schema.js +251 -0
  54. package/dist/src/state/types.d.ts +71 -0
  55. package/dist/src/state/types.js +1 -0
  56. package/dist/src/tools/artifacts.d.ts +18 -0
  57. package/dist/src/tools/artifacts.js +71 -0
  58. package/dist/src/tools/health.d.ts +8 -0
  59. package/dist/src/tools/health.js +119 -0
  60. package/dist/src/tools/index.d.ts +20 -0
  61. package/dist/src/tools/index.js +94 -0
  62. package/dist/src/tools/init.d.ts +17 -0
  63. package/dist/src/tools/init.js +96 -0
  64. package/dist/src/tools/injects.d.ts +53 -0
  65. package/dist/src/tools/injects.js +325 -0
  66. package/dist/src/tools/metrics.d.ts +7 -0
  67. package/dist/src/tools/metrics.js +61 -0
  68. package/dist/src/tools/repair.d.ts +8 -0
  69. package/dist/src/tools/repair.js +25 -0
  70. package/dist/src/tools/reset.d.ts +8 -0
  71. package/dist/src/tools/reset.js +92 -0
  72. package/dist/src/tools/run.d.ts +13 -0
  73. package/dist/src/tools/run.js +54 -0
  74. package/dist/src/tools/spec.d.ts +12 -0
  75. package/dist/src/tools/spec.js +44 -0
  76. package/dist/src/tools/stage.d.ts +23 -0
  77. package/dist/src/tools/stage.js +371 -0
  78. package/dist/src/tools/status.d.ts +8 -0
  79. package/dist/src/tools/status.js +125 -0
  80. package/dist/src/tools/story.d.ts +23 -0
  81. package/dist/src/tools/story.js +85 -0
  82. package/dist/src/tools/workflow.d.ts +13 -0
  83. package/dist/src/tools/workflow.js +355 -0
  84. package/dist/src/ui/inject.d.ts +12 -0
  85. package/dist/src/ui/inject.js +107 -0
  86. package/dist/src/ui/toasts.d.ts +13 -0
  87. package/dist/src/ui/toasts.js +39 -0
  88. package/dist/src/workflow/artifacts.d.ts +24 -0
  89. package/dist/src/workflow/artifacts.js +45 -0
  90. package/dist/src/workflow/baton.d.ts +72 -0
  91. package/dist/src/workflow/baton.js +166 -0
  92. package/dist/src/workflow/context.d.ts +20 -0
  93. package/dist/src/workflow/context.js +113 -0
  94. package/dist/src/workflow/directives.d.ts +39 -0
  95. package/dist/src/workflow/directives.js +137 -0
  96. package/dist/src/workflow/repair.d.ts +8 -0
  97. package/dist/src/workflow/repair.js +99 -0
  98. package/dist/src/workflow/state-machine.d.ts +86 -0
  99. package/dist/src/workflow/state-machine.js +216 -0
  100. package/dist/src/workflow/story-helpers.d.ts +9 -0
  101. package/dist/src/workflow/story-helpers.js +13 -0
  102. package/dist/state/db.d.ts +1 -0
  103. package/dist/state/db.js +9 -0
  104. package/dist/state/repo-lock.d.ts +3 -0
  105. package/dist/state/repo-lock.js +29 -0
  106. package/dist/test/integration/db-transactions.test.d.ts +1 -0
  107. package/dist/test/integration/db-transactions.test.js +126 -0
  108. package/dist/test/integration/injection-metrics.test.d.ts +1 -0
  109. package/dist/test/integration/injection-metrics.test.js +129 -0
  110. package/dist/tools/health.d.ts +8 -0
  111. package/dist/tools/health.js +119 -0
  112. package/dist/tools/index.js +9 -0
  113. package/dist/tools/metrics.d.ts +7 -0
  114. package/dist/tools/metrics.js +61 -0
  115. package/dist/tools/reset.d.ts +8 -0
  116. package/dist/tools/reset.js +92 -0
  117. package/dist/tools/workflow.js +178 -168
  118. package/dist/ui/inject.js +21 -9
  119. package/package.json +6 -3
  120. package/src/astro/workflow-runner.ts +36 -0
  121. package/src/index.ts +8 -0
  122. package/src/shared/metrics.ts +148 -0
  123. package/src/state/db.ts +10 -1
  124. package/src/state/repo-lock.ts +574 -0
  125. package/src/state/workflow-repo-lock.ts +74 -0
  126. package/src/tools/health.ts +128 -0
  127. package/src/tools/index.ts +12 -3
  128. package/src/tools/init.ts +7 -6
  129. package/src/tools/metrics.ts +71 -0
  130. package/src/tools/repair.ts +1 -0
  131. package/src/tools/reset.ts +100 -0
  132. package/src/tools/stage.ts +1 -0
  133. package/src/tools/status.ts +2 -1
  134. package/src/tools/story.ts +1 -0
  135. package/src/tools/workflow.ts +18 -1
  136. package/src/ui/inject.ts +21 -9
@@ -0,0 +1,148 @@
1
+ // src/shared/metrics.ts
2
+
3
+ export interface TransactionMetrics {
4
+ startTime: number;
5
+ duration: number;
6
+ success: boolean;
7
+ nestedDepth: number;
8
+ operation?: string;
9
+ }
10
+
11
+ export interface InjectionMetrics {
12
+ sessionId: string;
13
+ attempts: number;
14
+ duration: number;
15
+ success: boolean;
16
+ agent?: string;
17
+ }
18
+
19
+ export interface SystemMetrics {
20
+ transactions: TransactionMetrics[];
21
+ injections: InjectionMetrics[];
22
+ errors: Array<{ type: string; message: string; timestamp: number }>;
23
+ }
24
+
25
+ class MetricsCollector {
26
+ private transactions: TransactionMetrics[] = [];
27
+ private injections: InjectionMetrics[] = [];
28
+ private errors: Array<{ type: string; message: string; timestamp: number }> = [];
29
+ private maxEntries = 1000; // Keep last 1000 entries per type
30
+
31
+ recordTransaction(metrics: TransactionMetrics) {
32
+ this.transactions.push(metrics);
33
+ if (this.transactions.length > this.maxEntries) {
34
+ this.transactions.shift();
35
+ }
36
+ }
37
+
38
+ recordInjection(metrics: InjectionMetrics) {
39
+ this.injections.push(metrics);
40
+ if (this.injections.length > this.maxEntries) {
41
+ this.injections.shift();
42
+ }
43
+ }
44
+
45
+ recordError(type: string, message: string) {
46
+ this.errors.push({ type, message, timestamp: Date.now() });
47
+ if (this.errors.length > this.maxEntries) {
48
+ this.errors.shift();
49
+ }
50
+ }
51
+
52
+ getMetrics(): SystemMetrics {
53
+ return {
54
+ transactions: [...this.transactions],
55
+ injections: [...this.injections],
56
+ errors: [...this.errors],
57
+ };
58
+ }
59
+
60
+ getTransactionStats() {
61
+ const txs = this.transactions;
62
+ if (txs.length === 0) return null;
63
+
64
+ const successful = txs.filter(t => t.success);
65
+ const failed = txs.filter(t => !t.success);
66
+
67
+ return {
68
+ total: txs.length,
69
+ successful: successful.length,
70
+ failed: failed.length,
71
+ successRate: successful.length / txs.length,
72
+ avgDuration: txs.reduce((sum, t) => sum + t.duration, 0) / txs.length,
73
+ avgNestedDepth: txs.reduce((sum, t) => sum + t.nestedDepth, 0) / txs.length,
74
+ minDuration: Math.min(...txs.map(t => t.duration)),
75
+ maxDuration: Math.max(...txs.map(t => t.duration)),
76
+ };
77
+ }
78
+
79
+ getInjectionStats() {
80
+ const injections = this.injections;
81
+ if (injections.length === 0) return null;
82
+
83
+ const successful = injections.filter(i => i.success);
84
+ const failed = injections.filter(i => !i.success);
85
+
86
+ return {
87
+ total: injections.length,
88
+ successful: successful.length,
89
+ failed: failed.length,
90
+ successRate: successful.length / injections.length,
91
+ avgAttempts: injections.reduce((sum, i) => sum + i.attempts, 0) / injections.length,
92
+ avgDuration: injections.reduce((sum, i) => sum + i.duration, 0) / injections.length,
93
+ totalRetries: injections.reduce((sum, i) => sum + Math.max(0, i.attempts - 1), 0),
94
+ };
95
+ }
96
+
97
+ clear() {
98
+ this.transactions = [];
99
+ this.injections = [];
100
+ this.errors = [];
101
+ }
102
+ }
103
+
104
+ // Global singleton
105
+ export const metrics = new MetricsCollector();
106
+
107
+ // Convenience functions
108
+ export function recordTransaction(metricsData: Omit<TransactionMetrics, 'startTime' | 'duration' | 'success'>) {
109
+ return {
110
+ start() {
111
+ return {
112
+ ...metricsData,
113
+ startTime: Date.now(),
114
+ };
115
+ },
116
+ end(startData: ReturnType<ReturnType<typeof recordTransaction>['start']>, success: boolean) {
117
+ const duration = Date.now() - startData.startTime;
118
+ metrics.recordTransaction({
119
+ ...startData,
120
+ duration,
121
+ success,
122
+ });
123
+ },
124
+ };
125
+ }
126
+
127
+ export function recordInjection(metricsData: Omit<InjectionMetrics, 'duration' | 'success'>) {
128
+ return {
129
+ start() {
130
+ return {
131
+ ...metricsData,
132
+ startTime: Date.now(),
133
+ };
134
+ },
135
+ end(startData: any, success: boolean) {
136
+ const duration = Date.now() - startData.startTime;
137
+ metrics.recordInjection({
138
+ ...startData,
139
+ duration,
140
+ success,
141
+ });
142
+ },
143
+ };
144
+ }
145
+
146
+ export function recordError(type: string, message: string) {
147
+ metrics.recordError(type, message);
148
+ }
package/src/state/db.ts CHANGED
@@ -5,6 +5,7 @@ import { SCHEMA_SQL, SCHEMA_VERSION } from "./schema";
5
5
  import { nowISO } from "../shared/time";
6
6
  import { info, warn } from "../shared/log";
7
7
  import { createDatabaseAdapter, DatabaseConnection } from "./adapters";
8
+ import { recordTransaction } from "../shared/metrics";
8
9
 
9
10
  export type SqliteDb = DatabaseConnection;
10
11
 
@@ -74,7 +75,7 @@ function savepointName(depth: number): string {
74
75
  }
75
76
 
76
77
  /** BEGIN IMMEDIATE transaction helper (re-entrant). */
77
- export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean }): T {
78
+ export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean; operation?: string }): T {
78
79
  const adapter = createDatabaseAdapter();
79
80
  const available = adapter.isAvailable();
80
81
 
@@ -84,13 +85,17 @@ export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean
84
85
  }
85
86
 
86
87
  const depth = getDepth(db);
88
+ const isNested = depth > 0;
89
+ const txRecorder = recordTransaction({ nestedDepth: depth, operation: opts?.operation });
87
90
 
88
91
  if (depth === 0) {
92
+ const txStart = txRecorder.start();
89
93
  db.exec("BEGIN IMMEDIATE");
90
94
  setDepth(db, 1);
91
95
  try {
92
96
  const out = fn();
93
97
  db.exec("COMMIT");
98
+ txRecorder.end(txStart, true);
94
99
  return out;
95
100
  } catch (e) {
96
101
  try {
@@ -98,6 +103,7 @@ export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean
98
103
  } catch {
99
104
  // ignore
100
105
  }
106
+ txRecorder.end(txStart, false);
101
107
  throw e;
102
108
  } finally {
103
109
  setDepth(db, 0);
@@ -107,6 +113,7 @@ export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean
107
113
  // Nested: use SAVEPOINT
108
114
  const nextDepth = depth + 1;
109
115
  const sp = savepointName(nextDepth);
116
+ const txStart = txRecorder.start();
110
117
 
111
118
  db.exec(`SAVEPOINT ${sp}`);
112
119
  setDepth(db, nextDepth);
@@ -114,6 +121,7 @@ export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean
114
121
  try {
115
122
  const out = fn();
116
123
  db.exec(`RELEASE SAVEPOINT ${sp}`);
124
+ txRecorder.end(txStart, true);
117
125
  return out;
118
126
  } catch (e) {
119
127
  try {
@@ -126,6 +134,7 @@ export function withTx<T>(db: SqliteDb, fn: () => T, opts?: { require?: boolean
126
134
  } catch {
127
135
  // ignore
128
136
  }
137
+ txRecorder.end(txStart, false);
129
138
  throw e;
130
139
  } finally {
131
140
  setDepth(db, depth);