tracer-sh 0.1.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.
@@ -0,0 +1,196 @@
1
+ // src/providers/gcp/domain-knowledge.ts
2
+ var GCP_AUTH_STOP_RULE = `## Authentication Failure \u2014 STOP IMMEDIATELY
3
+ If any tool returns an authentication or permission error (e.g. "unauthenticated", "permission denied", "credentials", "401", "403", "UNAUTHENTICATED", "insufficient permissions"), **STOP ALL FURTHER TOOL CALLS** and report:
4
+ 1. The exact error message received.
5
+ 2. That authentication/credentials need to be fixed before proceeding.
6
+ Do NOT retry, do NOT try alternative tools \u2014 auth errors cannot be resolved by the sub-agent.`;
7
+ var GCP_PAGE_SIZE_RULE = `**NEVER request large result sets.** Always set pageSize \u2264 20. Always use specific filters (severity, resource type, service name, time range). If you get a size-limit error, halve your pageSize and add tighter filters before retrying.`;
8
+ var GCP_LOGGING_FILTER_SYNTAX = `## Cloud Logging Filter Syntax
9
+ - Comparison operators: \`=\`, \`!=\`, \`>\`, \`>=\`, \`<\`, \`<=\`, \`:\` (has/substring), \`=~\` (regex), \`!~\` (not regex)
10
+ - Boolean: \`AND\`, \`OR\`, \`NOT\` (AND is implicit between expressions)
11
+ - Severity levels: DEFAULT, DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY
12
+ - Severity comparison: \`severity >= ERROR\` \u2014 NOT quoted, NOT prefixed with anything
13
+ - Text search: \`textPayload:"error message"\` or \`jsonPayload.message:"error"\`
14
+ - Resource filter: \`resource.type="cloud_run_revision"\` \u2014 string values MUST be double-quoted
15
+ - Resource labels: \`resource.labels.service_name="my-service"\`
16
+ - Log name: \`logName="projects/PROJECT/logs/LOG_ID"\` or \`log_id("LOG_ID")\`
17
+ - Timestamp: \`timestamp >= "2024-01-15T00:00:00Z"\`
18
+ - NEVER use SQL syntax (WHERE, SELECT, FROM) \u2014 this is NOT SQL
19
+ - NEVER quote severity values: \`severity >= ERROR\` not \`severity >= "ERROR"\``;
20
+ var GCP_RESOURCE_TYPES = `## Common Resource Types & Labels
21
+ - \`cloud_run_revision\`: labels: service_name, revision_name, location, configuration_name
22
+ - \`cloud_run_job\`: labels: job_name, location
23
+ - \`cloud_function\`: labels: function_name, region
24
+ - \`gce_instance\`: labels: instance_id, zone
25
+ - \`k8s_container\`: labels: cluster_name, namespace_name, container_name, pod_name, location
26
+ - \`k8s_pod\`: labels: cluster_name, namespace_name, pod_name, location
27
+ - \`k8s_node\`: labels: cluster_name, node_name, location
28
+ - \`gae_app\`: labels: module_id, version_id, zone
29
+ - \`cloud_sql_database\`: labels: database_id, region
30
+ - \`global\`: for logs not tied to a specific resource`;
31
+ var GCP_METRIC_TYPES = `## Common Metric Types (for list_time_series)
32
+ - Cloud Run: \`run.googleapis.com/request_count\`, \`run.googleapis.com/request_latencies\`, \`run.googleapis.com/container/memory/utilizations\`, \`run.googleapis.com/container/cpu/utilizations\`, \`run.googleapis.com/container/instance_count\`
33
+ - Cloud Functions: \`cloudfunctions.googleapis.com/function/execution_count\`, \`cloudfunctions.googleapis.com/function/execution_times\`, \`cloudfunctions.googleapis.com/function/active_instances\`
34
+ - GCE: \`compute.googleapis.com/instance/cpu/utilization\`, \`compute.googleapis.com/instance/network/received_bytes_count\`
35
+ - GKE: \`kubernetes.io/container/cpu/core_usage_time\`, \`kubernetes.io/container/memory/used_bytes\`, \`kubernetes.io/container/restart_count\`
36
+ - Cloud SQL: \`cloudsql.googleapis.com/database/cpu/utilization\`, \`cloudsql.googleapis.com/database/memory/utilization\`
37
+ - Load Balancing: \`loadbalancing.googleapis.com/https/request_count\`, \`loadbalancing.googleapis.com/https/total_latencies\`
38
+ - Use \`list_metric_descriptors\` to discover available metrics when unsure.`;
39
+ var GCP_TOOL_PARAMS = `## Tool Parameter Reference
40
+
41
+ ### list_log_entries
42
+ \`\`\`json
43
+ { "projectName": "projects/PROJECT_ID", "filter": "resource.type=\\"cloud_run_revision\\" AND severity>=ERROR", "pageSize": 10, "orderBy": "timestamp desc" }
44
+ \`\`\`
45
+ GOTCHA: \`projectName\` must be \`"projects/PROJECT_ID"\` format, NOT just \`"PROJECT_ID"\`
46
+ GOTCHA: Always include \`orderBy: "timestamp desc"\` \u2014 default may return oldest entries first
47
+
48
+ ### list_time_series
49
+ \`\`\`json
50
+ { "name": "projects/PROJECT_ID", "filter": "metric.type=\\"run.googleapis.com/request_count\\" resource.type=\\"cloud_run_revision\\"", "interval": { "startTime": "RFC3339", "endTime": "RFC3339" }, "aggregation": { "alignmentPeriod": "60s", "perSeriesAligner": "ALIGN_RATE" } }
51
+ \`\`\`
52
+ GOTCHA: \`interval\` is REQUIRED \u2014 always compute startTime/endTime from the user's timeframe
53
+ GOTCHA: Metric filter uses SPACE-separated conditions (no explicit AND between metric.type and resource.type)
54
+
55
+ ### list_traces
56
+ \`\`\`json
57
+ { "projectId": "PROJECT_ID", "filter": "latency:>100ms", "startTime": "RFC3339", "endTime": "RFC3339", "pageSize": 10 }
58
+ \`\`\`
59
+
60
+ ### get_trace
61
+ \`\`\`json
62
+ { "projectId": "PROJECT_ID", "traceId": "TRACE_ID" }
63
+ \`\`\`
64
+
65
+ ### list_group_stats
66
+ \`\`\`json
67
+ { "projectId": "PROJECT_ID", "timeRange": { "startTime": "RFC3339", "endTime": "RFC3339" } }
68
+ \`\`\`
69
+
70
+ ### list_metric_descriptors
71
+ \`\`\`json
72
+ { "projectName": "projects/PROJECT_ID", "filter": "metric.type = starts_with(\\"run.googleapis.com\\")" }
73
+ \`\`\`
74
+ Use this FIRST when you don't know the exact metric type.`;
75
+ var GCP_ANTI_PATTERNS = `## Common Mistakes \u2014 AVOID THESE
76
+
77
+ ### Filter Syntax Errors
78
+ 1. Using bare \`PROJECT_ID\` where \`"projects/PROJECT_ID"\` is required (\`list_log_entries\`, \`list_time_series\`, \`list_metric_descriptors\`)
79
+ 2. Quoting severity: \`severity >= ERROR\` NOT \`severity >= "ERROR"\`
80
+ 3. Boolean operators MUST be uppercase: \`AND\`, \`OR\`, \`NOT\` \u2014 lowercase \`and\`/\`or\`/\`not\` silently fails or produces wrong results
81
+ 4. \`:\` (has/substring) vs \`=\` (exact match) confusion: \`textPayload:"error"\` matches substring, \`textPayload="error"\` requires exact string equality. Use \`:\` for searching within messages.
82
+ 5. Using SQL syntax in Cloud Logging filters \u2014 it is NOT SQL (no WHERE, SELECT, FROM)
83
+ 6. Missing \`interval\` for \`list_time_series\` \u2014 it is REQUIRED
84
+ 7. Using AND in metric filters \u2014 \`metric.type\` and \`resource.type\` are SPACE-separated, not AND-separated
85
+ 8. Forgetting \`orderBy: "timestamp desc"\` \u2014 default returns oldest entries first
86
+
87
+ ### Tool Misuse
88
+ 9. Using \`list_group_stats\` to search for a specific error \u2014 it returns GROUPED error patterns by stack trace similarity, NOT arbitrary error log searches. For specific error text, use \`list_log_entries\` with \`textPayload:"your error message"\`.
89
+ 10. Expecting full trace details from \`list_traces\` \u2014 it returns ROOT SPAN ONLY. You MUST call \`get_trace\` with the \`traceId\` to see the full span tree.
90
+ 11. Guessing metric types \u2014 use \`list_metric_descriptors\` first if unsure. Metric type strings must be exact.
91
+
92
+ ### Platform Gotchas
93
+ 12. Cloud Run STDOUT/STDERR loses severity \u2014 apps writing plain text to stdout produce \`textPayload\` with \`severity=DEFAULT\`. Only structured JSON output preserves severity levels.
94
+ 13. Trace filter syntax: no spaces allowed except between separate filter terms. Misspelled filter keys silently become label filters instead of erroring.
95
+ 14. \`source()\` at org/folder level does NOT include child project logs \u2014 query each project explicitly.
96
+ 15. Query performance: >200 time series buckets degrades response time. Use tighter filters or coarser \`alignmentPeriod\`.
97
+ 16. Empty results with no filter adjustments \u2014 try: broader time range, remove resource.type filter, or filter by just \`severity>=ERROR\``;
98
+ var GCP_QUERY_DEFAULTS = `## Query Defaults
99
+ - **pageSize: 10** (default) for \`list_log_entries\`. Maximum 20. Exceeding this WILL cause the server to reject the response.
100
+ - Always use a **specific filter** \u2014 never fetch all logs unfiltered. Filter by severity, resource type, service name, or time window.
101
+ - For metrics: fetch **1\u20133 metric types** per call, not broad dumps.`;
102
+ var GCP_FIELD_REFERENCE = `## GCP Field Reference & Payload Types
103
+
104
+ ### Log Entry Payload Inconsistencies
105
+ | Concept | textPayload | jsonPayload | protoPayload (audit) |
106
+ |---------|------------|-------------|---------------------|
107
+ | Message | the entire string | \`.message\`, \`.msg\`, \`.error\`, \`.text\` | \`.status.message\` |
108
+ | Severity | top-level \`severity\` | top-level \`severity\` | top-level \`severity\` |
109
+ | Error detail | search the text | \`.error\`, \`.exception\`, \`.stack_trace\` | \`.status.code\` + \`.status.message\` |
110
+ | HTTP info | not structured | \`.httpRequest.*\` | \`.requestMetadata.*\` |
111
+ | User/principal | not available | app-specific | \`.authenticationInfo.principalEmail\` |
112
+
113
+ Cloud Run STDOUT/STDERR \u2192 \`textPayload\` with **severity=DEFAULT** (original severity is LOST).
114
+ Cloud Run structured JSON \u2192 \`jsonPayload\` with proper severity IF the app writes JSON to stdout.
115
+
116
+ ### Cross-Signal Field Mapping
117
+ | Concept | Log Entry | Trace/Span | Metric | Error Reporting |
118
+ |---------|-----------|------------|--------|-----------------|
119
+ | Trace link | \`trace\` = \`"projects/P/traces/ID"\` | \`traceId\` (bare hex) | \u2014 | \u2014 |
120
+ | Span link | \`spanId\` | \`spanId\` | \u2014 | \u2014 |
121
+ | Service | \`resource.labels.service_name\` | span \`displayName\` or labels | \`resource.labels.service_name\` | \`serviceContext.service\` |
122
+ | Project | \`resource.labels.project_id\` | \`projectId\` parameter | \`name\` = \`projects/ID\` | \`projectId\` parameter |
123
+ | Timestamp | \`timestamp\` (RFC3339) | \`startTime\`/\`endTime\` (RFC3339) | \`interval.startTime\`/\`endTime\` | \`firstSeenTime\`/\`lastSeenTime\` |
124
+
125
+ ### CRITICAL: Trace ID Format Mismatch
126
+ - In logs: \`trace\` = \`"projects/my-project/traces/abc123def456"\` (full resource path)
127
+ - In traces: \`traceId\` = \`"abc123def456"\` (bare hex string)
128
+ - Logs \u2192 Traces: STRIP the prefix \u2014 extract hex ID after \`/traces/\`
129
+ - Traces \u2192 Logs: ADD the prefix \u2014 \`trace="projects/PROJECT/traces/TRACE_ID"\`
130
+
131
+ ### Error Reporting Structure
132
+ - \`list_group_stats\` returns: \`group.groupId\`, \`group.title\` (first line of stack trace), \`count\`, \`firstSeenTime\`, \`lastSeenTime\`, \`representative.message\`
133
+ - Error groups are clustered by STACK TRACE SIMILARITY \u2014 they are for recurring exceptions, NOT arbitrary error searches
134
+ - To search for a specific error message, use \`list_log_entries\` with \`severity>=ERROR\` and \`textPayload:"message"\` or \`jsonPayload.message:"message"\``;
135
+ var GCP_CROSS_SIGNAL = `## Cross-Signal Correlation
136
+ - **Logs \u2192 Traces**: Log entries have a \`trace\` field (format: \`"projects/P/traces/TRACE_ID"\`). Extract TRACE_ID and use \`get_trace\`.
137
+ - **Logs \u2192 Metrics**: \`resource.type\` + \`resource.labels\` in logs match metric filters \u2014 same service in logs = same filter in metrics.
138
+ - **Error Reporting \u2192 Logs**: Use the error message from \`list_group_stats\` to filter logs: \`textPayload:"<message>"\`.
139
+ - **Traces \u2192 Logs**: Filter logs by trace ID: \`trace="projects/PROJECT/traces/TRACE_ID"\``;
140
+ var GCP_INSIDE_OUT_DEBUGGING = `## Inside-Out Debugging
141
+
142
+ **With a specific identifier** (service name, error message, trace ID):
143
+ 1. Search directly \u2014 \`list_log_entries\` with a filter targeting that identifier. Do NOT start with broad overviews.
144
+ 2. Extract context \u2014 resource type, service name, trace ID, severity pattern, timestamps.
145
+ 3. Expand ONLY if needed \u2014 \`get_trace\` for the request chain, \`list_time_series\` for trends. But only if the log context doesn't already answer the question.
146
+ 4. If multiple trace IDs surface, investigate 1-2 representative samples. If they show the same pattern, stop \u2014 that IS the pattern.
147
+
148
+ **Without a specific identifier** (vague symptoms):
149
+ 1. Start broad \u2014 \`list_log_entries\` with \`severity>=ERROR\` and pageSize=5 to discover what's happening.
150
+ 2. Check Error Reporting \u2014 \`list_group_stats\` for grouped patterns.
151
+ 3. Once you have a specific identifier, switch to the "with identifier" flow above.
152
+
153
+ **Diagnostic safeguards:**
154
+ - **No data?** If your first 2 tool calls across different signals return empty, try: broader time range, remove resource.type filter, try just \`severity>=ERROR\`. If still nothing, report no data \u2014 stop investigating.
155
+ - **Suspiciously few results?** Check the filter syntax \u2014 remember: boolean operators must be uppercase (AND, OR, NOT), severity is unquoted, string values must be double-quoted.
156
+
157
+ NEVER start with \`list_metric_descriptors\` or \`list_group_stats\` when you have a specific error message or service name \u2014 go straight to logs.`;
158
+ var GCP_TOOL_REFERENCE = `## Tool Reference
159
+
160
+ **Logs:**
161
+ - \`list_log_entries\` \u2014 filter by severity, resource type, service name, timestamp
162
+ - Common filters: \`severity>=ERROR\`, \`resource.type="cloud_run_revision"\`, \`resource.labels.service_name="<name>"\`
163
+
164
+ **Metrics:**
165
+ - \`list_time_series\` \u2014 Cloud Monitoring metric types (e.g. \`run.googleapis.com/request_count\`, \`run.googleapis.com/container/memory/utilizations\`)
166
+ - \`list_metric_descriptors\` \u2014 discover available metric types
167
+ - \`list_alert_policies\` \u2014 active Cloud Monitoring alerting policies
168
+
169
+ **Traces:**
170
+ - \`list_traces\` \u2014 list recent traces, filter by latency or service
171
+ - \`get_trace\` \u2014 get a specific trace by ID
172
+
173
+ **Error Reporting:**
174
+ - \`list_group_stats\` \u2014 top error groups with occurrence counts and first/last seen`;
175
+ var GCP_DOMAIN_KNOWLEDGE = `${GCP_QUERY_DEFAULTS}
176
+
177
+ ${GCP_LOGGING_FILTER_SYNTAX}
178
+
179
+ ${GCP_RESOURCE_TYPES}
180
+
181
+ ${GCP_METRIC_TYPES}
182
+
183
+ ${GCP_FIELD_REFERENCE}
184
+
185
+ ${GCP_TOOL_PARAMS}
186
+
187
+ ${GCP_ANTI_PATTERNS}`;
188
+
189
+ export {
190
+ GCP_AUTH_STOP_RULE,
191
+ GCP_PAGE_SIZE_RULE,
192
+ GCP_CROSS_SIGNAL,
193
+ GCP_INSIDE_OUT_DEBUGGING,
194
+ GCP_TOOL_REFERENCE,
195
+ GCP_DOMAIN_KNOWLEDGE
196
+ };