devopsbench-100 / world /tools_combined.py
SamuelChien821's picture
Upload folder using huggingface_hub (part 2)
37690b6 verified
Raw
History Blame Contribute Delete
177 kB
def list_services(db_path=None, team=None, tier=None):
"""List all services with team, tier, kind, on-call engineer, repo HEAD version and deployed versions."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_services',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM services'
conds, args = [], []
if team:
conds.append('team=?'); args.append(team)
if tier is not None:
conds.append('tier=?'); args.append(int(tier))
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
out = []
for s in conn.execute(sql + ' ORDER BY service_id', args).fetchall():
d = dict(s)
oc = conn.execute('SELECT engineer FROM oncall WHERE team=?', (s['team'],)).fetchone()
d['oncall_engineer'] = oc[0] if oc else ''
for _env in ('staging', 'production'):
v = conn.execute("SELECT value FROM env_state WHERE service=? AND environment=? AND kind='version' AND key='current'", (s['name'], _env)).fetchone()
d[_env + '_version'] = v[0] if v else ''
out.append(d)
return out
finally:
conn.close()
def get_service(db_path=None, service=None):
"""Full detail for one service: metadata, deployed config, modules, endpoints, dependencies, current metrics."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_service',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
row = conn.execute('SELECT * FROM services WHERE name=?', (service,)).fetchone()
if row is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
d = dict(row)
d['production'] = {}
for r in conn.execute("SELECT kind, key, value FROM env_state WHERE service=? AND environment='production' ORDER BY kind, key", (service,)).fetchall():
d['production'].setdefault(r['kind'], {})[r['key']] = r['value']
d['depends_on'] = [dict(r) for r in conn.execute('SELECT depends_on, kind FROM service_dependencies WHERE service=? ORDER BY depends_on', (service,)).fetchall()]
d['metrics'] = {r['metric']: r['value'] for r in conn.execute("SELECT metric, value FROM service_metrics WHERE service=? AND environment='production'", (service,)).fetchall()}
oc = conn.execute('SELECT engineer FROM oncall WHERE team=?', (row['team'],)).fetchone()
d['oncall_engineer'] = oc[0] if oc else ''
return d
finally:
conn.close()
def list_infra(db_path=None):
"""List infrastructure components of the application stack (databases, caches, queues, object stores, CDN)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_infra',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM infra_components ORDER BY component_id').fetchall()]
finally:
conn.close()
def list_files(db_path=None, service=None, path_contains=None):
"""List monorepo files, optionally filtered by service or path substring."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_files',)); conn.commit()
except Exception:
pass
sql = 'SELECT file_id, service, path, language, owner, loc FROM repo_files'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if path_contains:
conds.append('path LIKE ?'); args.append('%' + str(path_contains) + '%')
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY path', args).fetchall()]
finally:
conn.close()
def read_file(db_path=None, path=None):
"""Read a monorepo source file. Returns its full current content."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('read_file',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
row = conn.execute('SELECT * FROM repo_files WHERE path=?', (path,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such file: ' + str(path)}
return dict(row)
finally:
conn.close()
def search_code(db_path=None, query=None, service=None, limit=20):
"""Search monorepo file contents for a substring; returns matching files with matching line numbers."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('search_code',)); conn.commit()
except Exception:
pass
if query is None:
return {'ok': False, 'error': 'missing required parameter: query'}
sql = 'SELECT service, path, content FROM repo_files WHERE content LIKE ?'
args = ['%' + str(query) + '%']
if service:
sql += ' AND service=?'; args.append(service)
out = []
for r in conn.execute(sql + ' ORDER BY path LIMIT ?', args + [int(limit)]).fetchall():
hits = []
for i, line in enumerate(r['content'].split(chr(10)), 1):
if str(query) in line:
hits.append({'line': i, 'text': line.strip()[:200]})
out.append({'service': r['service'], 'path': r['path'], 'matches': hits[:8]})
return out
finally:
conn.close()
def list_commits(db_path=None, service=None, query=None, path=None, limit=20):
"""Browse monorepo commit history (most recent first)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_commits',)); conn.commit()
except Exception:
pass
sql = 'SELECT sha, service, author, day, message, files, additions, deletions FROM commits'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if query:
conds.append('message LIKE ?'); args.append('%' + str(query) + '%')
if path:
conds.append('files LIKE ?'); args.append('%' + str(path) + '%')
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY day DESC, commit_id DESC LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def search_docs(db_path=None, query='', kind=None, service=None, limit=10):
"""Search the engineering knowledge base (runbooks, policies, design docs, ADRs, postmortems, API specs)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('search_docs',)); conn.commit()
except Exception:
pass
sql = 'SELECT doc_id, kind, title, service, author, day FROM documents'
conds, args = [], []
if query:
conds.append('(title LIKE ? OR body LIKE ?)'); args += ['%' + str(query) + '%', '%' + str(query) + '%']
if kind:
conds.append('kind=?'); args.append(kind)
if service:
conds.append('service=?'); args.append(service)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY doc_id LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def get_document(db_path=None, doc_id=None, title=None):
"""Read one knowledge-base document in full by doc_id (or exact title)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_document',)); conn.commit()
except Exception:
pass
row = None
if doc_id is not None:
row = conn.execute('SELECT * FROM documents WHERE doc_id=?', (int(doc_id),)).fetchone()
elif title:
row = conn.execute('SELECT * FROM documents WHERE title=?', (title,)).fetchone()
if row is None:
row = conn.execute('SELECT * FROM documents WHERE title LIKE ?', ('%' + str(title) + '%',)).fetchone()
else:
return {'ok': False, 'error': 'provide doc_id or title'}
if row is None:
return {'ok': False, 'error': 'document not found'}
return dict(row)
finally:
conn.close()
def list_tickets(db_path=None, status=None, service=None, ticket_type=None):
"""List issue-tracker tickets, optionally filtered by status, service, or type."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_tickets',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM tickets'
conds, args = [], []
if status:
conds.append('status=?'); args.append(status)
if service:
conds.append('service=?'); args.append(service)
if ticket_type:
conds.append('type=?'); args.append(ticket_type)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY ticket_id', args).fetchall()]
finally:
conn.close()
def get_ticket(db_path=None, key=None):
"""Fetch one ticket by key (e.g. ENG-2101)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_ticket',)); conn.commit()
except Exception:
pass
if key is None:
return {'ok': False, 'error': 'missing required parameter: key'}
row = conn.execute('SELECT * FROM tickets WHERE key=?', (key,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such ticket: ' + str(key)}
return dict(row)
finally:
conn.close()
def list_pull_requests(db_path=None, service=None, status=None):
"""List pull requests, optionally filtered by service or status."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_pull_requests',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM pull_requests'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if status:
conds.append('status=?'); args.append(status)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY number', args).fetchall()]
finally:
conn.close()
def get_pull_request(db_path=None, pr_number=None):
"""Fetch a PR with its structured changes and CI history."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_pull_request',)); conn.commit()
except Exception:
pass
if pr_number is None:
return {'ok': False, 'error': 'missing required parameter: pr_number'}
row = conn.execute('SELECT * FROM pull_requests WHERE number=?', (int(pr_number),)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such pull request: ' + str(pr_number)}
d = dict(row)
d['changes'] = [{'change_type': c['change_type'], 'payload': _json.loads(c['payload'])}
for c in conn.execute('SELECT change_type, payload FROM pr_changes WHERE pr_number=? ORDER BY change_id', (int(pr_number),)).fetchall()]
d['ci_runs'] = [dict(c) for c in conn.execute('SELECT * FROM ci_runs WHERE pr_number=? ORDER BY run_id', (int(pr_number),)).fetchall()]
return d
finally:
conn.close()
def list_ci_runs(db_path=None, service=None, pr_number=None, limit=20):
"""List CI runs (most recent first)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_ci_runs',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM ci_runs'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if pr_number is not None:
conds.append('pr_number=?'); args.append(int(pr_number))
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY run_id DESC LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def get_ci_run(db_path=None, run_id=None):
"""Fetch one CI run with its per-stage results (build, unit, integration, regression)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_ci_run',)); conn.commit()
except Exception:
pass
if run_id is None:
return {'ok': False, 'error': 'missing required parameter: run_id'}
row = conn.execute('SELECT * FROM ci_runs WHERE run_id=?', (int(run_id),)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such CI run: ' + str(run_id)}
d = dict(row)
d['stages'] = [dict(s) for s in conn.execute('SELECT stage, status, detail FROM ci_stages WHERE run_id=? ORDER BY stage_id', (int(run_id),)).fetchall()]
return d
finally:
conn.close()
def list_deployments(db_path=None, service=None, environment=None, limit=20):
"""List deployments (most recent first)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_deployments',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM deployments'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if environment:
conds.append('environment=?'); args.append(environment)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY deployment_id DESC LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def list_migrations(db_path=None, service=None, environment=None):
"""List database migrations and whether they are applied per environment."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_migrations',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM migrations'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if environment:
conds.append('environment=?'); args.append(environment)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
rows = [dict(r) for r in conn.execute(sql + ' ORDER BY migration_id', args).fetchall()]
pend = [dict(r) for r in conn.execute('SELECT service, module, migration_name FROM migration_requirements ORDER BY req_id').fetchall()]
return {'applied': rows, 'declared_requirements': pend}
finally:
conn.close()
def query_metrics(db_path=None, service=None, metric=None):
"""Read current production service metrics (recomputed continuously from live traffic)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('query_metrics',)); conn.commit()
except Exception:
pass
sql = "SELECT * FROM service_metrics WHERE environment='production'"
args = []
if service:
sql += ' AND service=?'; args.append(service)
if metric:
sql += ' AND metric=?'; args.append(metric)
return [dict(r) for r in conn.execute(sql + ' ORDER BY service, metric', args).fetchall()]
finally:
conn.close()
def get_traffic_stats(db_path=None, service=None):
"""Traffic-generator statistics: request rate per route with the current error rate and p99 of the owning service."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_traffic_stats',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM traffic_profile'
args = []
if service:
sql += ' WHERE service=?'; args.append(service)
out = []
for r in conn.execute(sql + ' ORDER BY route_id', args).fetchall():
d = dict(r)
for m in ('error_rate_pct', 'latency_p99_ms'):
v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (r['service'], m)).fetchone()
d[m] = v[0] if v else None
if d.get('error_rate_pct') is not None:
d['failed_requests_per_min'] = round(r['rps'] * 60.0 * d['error_rate_pct'] / 100.0, 1)
out.append(d)
return out
finally:
conn.close()
def get_slo_status(db_path=None, service=None):
"""List SLOs with current values and whether each is breaching."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_slo_status',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM slos'
args = []
if service:
sql += ' WHERE service=?'; args.append(service)
out = []
for r in conn.execute(sql + ' ORDER BY slo_id', args).fetchall():
v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (r['service'], r['metric'])).fetchone()
d = dict(r)
d['current_value'] = v[0] if v else None
d['breaching'] = (v is not None and v[0] > r['threshold'])
out.append(d)
return out
finally:
conn.close()
def list_alerts(db_path=None, status=None, service=None):
"""List alarms, optionally filtered by status (firing|acknowledged|resolved) or service."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_alerts',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM alerts'
conds, args = [], []
if status:
conds.append('status=?'); args.append(status)
if service:
conds.append('service=?'); args.append(service)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY alert_id', args).fetchall()]
finally:
conn.close()
def list_error_events(db_path=None, service=None, status=None):
"""Error-tracking issues (Sentry-style): grouped exceptions with culprit and event counts."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_error_events',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM error_events'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if status:
conds.append('status=?'); args.append(status)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY events DESC', args).fetchall()]
finally:
conn.close()
def search_logs(db_path=None, service=None, query='', level=None, limit=20):
"""Search application logs by substring, service, or level."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('search_logs',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM logs'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if level:
conds.append('level=?'); args.append(level)
if query:
conds.append('message LIKE ?'); args.append('%' + str(query) + '%')
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY log_id LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def list_feature_flags(db_path=None, service=None, environment=None):
"""List feature flags with per-environment state."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_feature_flags',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM feature_flags'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if environment:
conds.append('environment=?'); args.append(environment)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY key, environment', args).fetchall()]
finally:
conn.close()
def list_packages(db_path=None, service=None):
"""List package dependencies: version at repo HEAD and version deployed in production."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_packages',)); conn.commit()
except Exception:
pass
sql = "SELECT service, key, value FROM repo_state WHERE kind='dependency'"
args = []
if service:
sql += ' AND service=?'; args.append(service)
out = []
for r in conn.execute(sql + ' ORDER BY service, key', args).fetchall():
p = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (r['service'], r['key'])).fetchone()
out.append({'service': r['service'], 'package': r['key'], 'repo_version': r['value'],
'production_version': p[0] if p else None})
return out
finally:
conn.close()
def list_vulnerabilities(db_path=None, status=None, service=None):
"""List security-scanner findings."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_vulnerabilities',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM vulnerabilities'
conds, args = [], []
if status:
conds.append('status=?'); args.append(status)
if service:
conds.append('service=?'); args.append(service)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY vuln_id', args).fetchall()]
finally:
conn.close()
def list_api_endpoints(db_path=None, service=None):
"""List API endpoints with repo status, production status, and production traffic share."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_api_endpoints',)); conn.commit()
except Exception:
pass
sql = "SELECT service, key, value FROM repo_state WHERE kind='endpoint'"
args = []
if service:
sql += ' AND service=?'; args.append(service)
out = []
for r in conn.execute(sql + ' ORDER BY service, key', args).fetchall():
p = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (r['service'], r['key'])).fetchone()
t = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='traffic' AND key=?", (r['service'], r['key'])).fetchone()
out.append({'service': r['service'], 'path': r['key'], 'repo_status': r['value'],
'production_status': p[0] if p else None,
'production_traffic_percent': int(t[0]) if t else None})
return out
finally:
conn.close()
def list_tests(db_path=None, service=None, status=None):
"""List the test catalog."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_tests',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM tests_catalog'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if status:
conds.append('status=?'); args.append(status)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY test_id', args).fetchall()]
finally:
conn.close()
def list_incidents(db_path=None, status=None):
"""List incidents."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_incidents',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM incidents'
args = []
if status:
sql += ' WHERE status=?'; args.append(status)
return [dict(r) for r in conn.execute(sql + ' ORDER BY incident_id', args).fetchall()]
finally:
conn.close()
def get_status_page(db_path=None, limit=10):
"""Read the public system-status page."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_status_page',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM status_page ORDER BY post_id DESC LIMIT ?', (int(limit),)).fetchall()]
finally:
conn.close()
def list_messages(db_path=None, channel=None, limit=20):
"""Read chat messages."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_messages',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM messages'
args = []
if channel:
sql += ' WHERE channel=?'; args.append(channel)
return [dict(r) for r in conn.execute(sql + ' ORDER BY message_id DESC LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def create_ticket(db_path=None, title=None, description='', ticket_type='task', service='', priority='medium'):
"""Create a ticket. Returns the generated key."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('create_ticket',)); conn.commit()
except Exception:
pass
if title is None:
return {'ok': False, 'error': 'missing required parameter: title'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if ticket_type not in ('task', 'bug', 'feature', 'security', 'incident', 'postmortem'):
return {'ok': False, 'error': 'invalid ticket_type: ' + str(ticket_type)}
if service and conn.execute('SELECT 1 FROM services WHERE name=?', (service,)).fetchone() is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
cur = conn.execute('INSERT INTO tickets(key, type, title, description, status, priority, service) VALUES (?,?,?,?,?,?,?)',
('', ticket_type, title, description, 'open', priority, service))
tid = cur.lastrowid
key = 'TKT-' + str(tid)
conn.execute('UPDATE tickets SET key=? WHERE ticket_id=?', (key, tid))
_audit(conn, 'create_ticket', service, {'key': key, 'type': ticket_type, 'title': title})
conn.commit()
return {'ok': True, 'key': key, 'ticket_id': tid, 'status': 'open'}
finally:
conn.close()
def update_ticket(db_path=None, key=None, status=None, assignee=None):
"""Update a ticket's status and/or assignee."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('update_ticket',)); conn.commit()
except Exception:
pass
if key is None:
return {'ok': False, 'error': 'missing required parameter: key'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
row = conn.execute('SELECT * FROM tickets WHERE key=?', (key,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such ticket: ' + str(key)}
if status is None and assignee is None:
return {'ok': False, 'error': 'provide status and/or assignee'}
if status is not None and status not in ('open', 'in_progress', 'in_review', 'done'):
return {'ok': False, 'error': 'invalid status: ' + str(status)}
ns = row['status'] if status is None else status
na = row['assignee'] if assignee is None else assignee
conn.execute('UPDATE tickets SET status=?, assignee=? WHERE key=?', (ns, na, key))
_audit(conn, 'update_ticket', row['service'], {'key': key, 'status': ns})
conn.commit()
return {'ok': True, 'key': key, 'status': ns, 'assignee': na}
finally:
conn.close()
def open_pull_request(db_path=None, service=None, title=None, body='', ticket_key='', changes=None):
"""Open a pull request carrying structured changes. change_type is one of: config {key,value}; dependency {package,version}; endpoint {path,status: active|deprecated|retired}; module {name}; flag {key,description}; flag_cleanup {key}; test_fix {test_name, action: fix|quarantine}; migration {name}; code_edit {path, find, replace}. Changes apply at merge; deploys carry them to an environment."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('open_pull_request',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
if title is None:
return {'ok': False, 'error': 'missing required parameter: title'}
if changes is None:
return {'ok': False, 'error': 'missing required parameter: changes'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if conn.execute('SELECT 1 FROM services WHERE name=?', (service,)).fetchone() is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
if isinstance(changes, str):
try:
changes = _json.loads(changes)
except Exception:
return {'ok': False, 'error': 'changes must be a JSON list of {change_type, payload}'}
if not isinstance(changes, list) or not changes:
return {'ok': False, 'error': 'changes must be a non-empty list of {change_type, payload}'}
norm = []
for ch in changes:
if not isinstance(ch, dict):
return {'ok': False, 'error': 'each change must be an object with change_type and payload'}
ct, pl = ch.get('change_type'), ch.get('payload')
if isinstance(pl, str):
try:
pl = _json.loads(pl)
except Exception:
return {'ok': False, 'error': 'change payload must be an object'}
if not isinstance(pl, dict):
return {'ok': False, 'error': 'change payload must be an object'}
if ct == 'config':
if not pl.get('key') or 'value' not in pl:
return {'ok': False, 'error': 'config change needs payload {key, value}'}
pl = {'key': str(pl['key']), 'value': str(pl['value'])}
elif ct == 'dependency':
if not pl.get('package') or not pl.get('version'):
return {'ok': False, 'error': 'dependency change needs payload {package, version}'}
if conn.execute("SELECT 1 FROM repo_state WHERE service=? AND kind='dependency' AND key=?", (service, pl['package'])).fetchone() is None:
return {'ok': False, 'error': service + ' has no dependency ' + str(pl['package'])}
pl = {'package': str(pl['package']), 'version': str(pl['version'])}
elif ct == 'endpoint':
if not pl.get('path') or pl.get('status') not in ('active', 'deprecated', 'retired'):
return {'ok': False, 'error': 'endpoint change needs payload {path, status: active|deprecated|retired}'}
if conn.execute("SELECT 1 FROM repo_state WHERE service=? AND kind='endpoint' AND key=?", (service, pl['path'])).fetchone() is None:
return {'ok': False, 'error': service + ' has no endpoint ' + str(pl['path'])}
pl = {'path': str(pl['path']), 'status': str(pl['status'])}
elif ct == 'module':
if not pl.get('name'):
return {'ok': False, 'error': 'module change needs payload {name}'}
pl = {'name': str(pl['name'])}
elif ct == 'flag':
if not pl.get('key'):
return {'ok': False, 'error': 'flag change needs payload {key, description}'}
if conn.execute('SELECT 1 FROM feature_flags WHERE key=?', (pl['key'],)).fetchone() is not None:
return {'ok': False, 'error': 'flag already exists: ' + str(pl['key'])}
pl = {'key': str(pl['key']), 'description': str(pl.get('description', ''))}
elif ct == 'flag_cleanup':
if not pl.get('key'):
return {'ok': False, 'error': 'flag_cleanup change needs payload {key}'}
if conn.execute('SELECT 1 FROM feature_flags WHERE key=?', (pl['key'],)).fetchone() is None:
return {'ok': False, 'error': 'no such flag: ' + str(pl['key'])}
pl = {'key': str(pl['key'])}
elif ct == 'test_fix':
if not pl.get('test_name') or pl.get('action') not in ('fix', 'quarantine'):
return {'ok': False, 'error': "test_fix change needs payload {test_name, action: fix|quarantine}"}
if conn.execute('SELECT 1 FROM tests_catalog WHERE service=? AND name=?', (service, pl['test_name'])).fetchone() is None:
return {'ok': False, 'error': service + ' has no test ' + str(pl['test_name'])}
pl = {'test_name': str(pl['test_name']), 'action': str(pl['action'])}
elif ct == 'migration':
if not pl.get('name'):
return {'ok': False, 'error': 'migration change needs payload {name}'}
pl = {'name': str(pl['name'])}
elif ct == 'code_edit':
if not pl.get('path') or 'find' not in pl or 'replace' not in pl:
return {'ok': False, 'error': 'code_edit change needs payload {path, find, replace}'}
f = conn.execute('SELECT content, service FROM repo_files WHERE path=?', (pl['path'],)).fetchone()
if f is None:
return {'ok': False, 'error': 'no such file: ' + str(pl['path'])}
if str(pl['find']) not in f['content']:
return {'ok': False, 'error': 'find text not present in ' + str(pl['path'])}
pl = {'path': str(pl['path']), 'find': str(pl['find']), 'replace': str(pl['replace'])}
else:
return {'ok': False, 'error': 'invalid change_type: ' + str(ct)}
norm.append((ct, pl))
number = conn.execute('SELECT COALESCE(MAX(number), 9200) + 1 FROM pull_requests').fetchone()[0]
conn.execute('INSERT INTO pull_requests(number, service, title, body, author, ticket_key, status) VALUES (?,?,?,?,?,?,?)',
(number, service, title, body, 'agent', ticket_key, 'open'))
for ct, pl in norm:
conn.execute('INSERT INTO pr_changes(pr_number, change_type, payload) VALUES (?,?,?)', (number, ct, _json.dumps(pl)))
_audit(conn, 'open_pull_request', service, {'pr_number': number, 'ticket_key': ticket_key,
'change_count': len(norm),
'change_types': sorted(set(c[0] for c in norm))})
conn.commit()
return {'ok': True, 'pr_number': number, 'service': service, 'status': 'open',
'next': 'run_ci(pr_number=' + str(number) + ') then merge_pull_request(pr_number=' + str(number) + ')'}
finally:
conn.close()
def run_ci(db_path=None, pr_number=None, service=None):
"""Run the CI pipeline for an open PR (pr_number) or a service's main branch (service). Stages run in order: build, unit, integration, regression. The tool succeeds even when the pipeline fails - inspect the returned status and stages."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('run_ci',)); conn.commit()
except Exception:
pass
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if pr_number is None and not service:
return {'ok': False, 'error': 'provide pr_number (PR pipeline) or service (main-branch pipeline)'}
if pr_number is not None:
pr_number = int(pr_number)
pr = conn.execute('SELECT * FROM pull_requests WHERE number=?', (pr_number,)).fetchone()
if pr is None:
return {'ok': False, 'error': 'no such pull request: ' + str(pr_number)}
if pr['status'] != 'open':
return {'ok': False, 'error': 'PR ' + str(pr_number) + ' is not open; for main-branch runs use run_ci(service=...)'}
service = pr['service']
elif conn.execute('SELECT 1 FROM services WHERE name=?', (service,)).fetchone() is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
changes = []
if pr_number is not None:
changes = [(c['change_type'], _json.loads(c['payload'])) for c in
conn.execute('SELECT change_type, payload FROM pr_changes WHERE pr_number=? ORDER BY change_id', (pr_number,)).fetchall()]
stages = []
# --- build: schema changes need their migration in the same PR
bstatus, bdetail = 'passed', 'compiled and packaged'
for ct, pl in changes:
if ct != 'module':
continue
req = conn.execute('SELECT migration_name FROM migration_requirements WHERE service=? AND module=?', (service, pl['name'])).fetchone()
if req is not None and not any(c[0] == 'migration' and c[1].get('name') == req[0] for c in changes):
bstatus = 'failed'
bdetail = 'missing database migration: module ' + pl['name'] + ' requires migration ' + req[0] + " (add a 'migration' change to this PR)"
break
stages.append(('build', bstatus, bdetail))
# --- unit
if bstatus == 'passed':
failing = [r['name'] for r in conn.execute("SELECT name FROM tests_catalog WHERE service=? AND suite='unit' AND status='failing' AND quarantined=0", (service,)).fetchall()]
stages.append(('unit', 'failed' if failing else 'passed',
('failing unit tests: ' + ', '.join(failing)) if failing else 'unit suite green'))
else:
stages.append(('unit', 'skipped', 'build failed'))
# --- integration: contract checks + flaky suite
istatus, idetail = 'passed', 'integration suite green'
if stages[-1][1] != 'passed':
istatus, idetail = 'skipped', 'upstream stage failed'
else:
for ct, pl in changes:
if ct == 'endpoint' and pl.get('status') == 'retired':
t = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='traffic' AND key=?", (service, pl['path'])).fetchone()
if t is not None and int(t[0]) > 0:
istatus = 'failed'
idetail = 'cannot retire ' + pl['path'] + ': still serving ' + str(t[0]) + '% of production traffic - drain it first'
break
if istatus == 'passed':
flaky = [r['name'] for r in conn.execute("SELECT name FROM tests_catalog WHERE service=? AND suite='integration' AND status='flaky' AND quarantined=0", (service,)).fetchall()]
if pr_number is not None:
seen_red = conn.execute("SELECT COUNT(*) FROM ci_runs WHERE pr_number=? AND status='failed'", (pr_number,)).fetchone()[0]
trips = bool(flaky) and seen_red == 0
else:
n_prior = conn.execute('SELECT COUNT(*) FROM ci_runs WHERE service=? AND pr_number IS NULL', (service,)).fetchone()[0]
trips = bool(flaky) and n_prior % 2 == 0
if trips:
istatus, idetail = 'failed', 'intermittent failure: ' + ', '.join(flaky) + ' (rerun may pass)'
else:
failing = [r['name'] for r in conn.execute("SELECT name FROM tests_catalog WHERE service=? AND suite='integration' AND status='failing' AND quarantined=0", (service,)).fetchall()]
if failing:
istatus, idetail = 'failed', 'failing integration tests: ' + ', '.join(failing)
stages.append(('integration', istatus, idetail))
# --- regression: cross-service consumer contracts
rstatus, rdetail = 'passed', 'no regressions in dependent services'
if istatus != 'passed':
rstatus, rdetail = 'skipped', 'upstream stage failed'
else:
for ct, pl in changes:
if ct != 'endpoint' or pl.get('status') != 'retired':
continue
for cr in conn.execute('SELECT * FROM contract_rules WHERE producer_service=? AND endpoint=?', (service, pl['path'])).fetchall():
cv = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (cr['consumer_service'], cr['consumer_key'])).fetchone()
if cv is None or cv[0] != cr['consumer_required_value']:
rstatus = 'failed'
rdetail = cr['message']
break
if rstatus == 'failed':
break
stages.append(('regression', rstatus, rdetail))
status = 'passed' if all(s[1] == 'passed' for s in stages) else 'failed'
detail = 'all stages passed' if status == 'passed' else next(s[2] for s in stages if s[1] == 'failed')
cur = conn.execute('INSERT INTO ci_runs(service, pr_number, status, detail) VALUES (?,?,?,?)', (service, pr_number, status, detail))
rid = cur.lastrowid
for st, sv, sd in stages:
conn.execute('INSERT INTO ci_stages(run_id, stage, status, detail) VALUES (?,?,?,?)', (rid, st, sv, sd))
_audit(conn, 'run_ci', service, {'pr_number': pr_number, 'status': status,
'stages': {s[0]: s[1] for s in stages}})
conn.commit()
return {'ok': True, 'run_id': rid, 'service': service, 'pr_number': pr_number, 'status': status,
'detail': detail, 'stages': [{'stage': s[0], 'status': s[1], 'detail': s[2]} for s in stages]}
finally:
conn.close()
def merge_pull_request(db_path=None, pr_number=None):
"""Merge an open PR. Blocked unless its latest CI run passed. Applies the PR's changes to repo HEAD (including code edits) and cuts a new deployable version."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('merge_pull_request',)); conn.commit()
except Exception:
pass
if pr_number is None:
return {'ok': False, 'error': 'missing required parameter: pr_number'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
pr_number = int(pr_number)
pr = conn.execute('SELECT * FROM pull_requests WHERE number=?', (pr_number,)).fetchone()
if pr is None:
return {'ok': False, 'error': 'no such pull request: ' + str(pr_number)}
if pr['status'] != 'open':
return {'ok': False, 'error': 'PR ' + str(pr_number) + ' is not open'}
last = conn.execute('SELECT status FROM ci_runs WHERE pr_number=? ORDER BY run_id DESC LIMIT 1', (pr_number,)).fetchone()
if last is None:
return {'ok': False, 'error': 'no CI run recorded for PR ' + str(pr_number) + '; call run_ci(pr_number=...) first'}
if last[0] != 'passed':
return {'ok': False, 'error': 'latest CI run for PR ' + str(pr_number) + ' is ' + last[0] + '; merge blocked'}
service = pr['service']
requires_migration = ''
for c in conn.execute('SELECT change_type, payload FROM pr_changes WHERE pr_number=? ORDER BY change_id', (pr_number,)).fetchall():
ct, pl = c['change_type'], _json.loads(c['payload'])
if ct == 'config':
conn.execute("INSERT INTO repo_state(service, kind, key, value) VALUES (?,'config',?,?) ON CONFLICT(service, kind, key) DO UPDATE SET value=excluded.value", (service, pl['key'], pl['value']))
elif ct == 'dependency':
conn.execute("UPDATE repo_state SET value=? WHERE service=? AND kind='dependency' AND key=?", (pl['version'], service, pl['package']))
elif ct == 'endpoint':
conn.execute("UPDATE repo_state SET value=? WHERE service=? AND kind='endpoint' AND key=?", (pl['status'], service, pl['path']))
elif ct == 'module':
conn.execute("INSERT INTO repo_state(service, kind, key, value) VALUES (?,'module',?,'present') ON CONFLICT(service, kind, key) DO UPDATE SET value=excluded.value", (service, pl['name']))
elif ct == 'flag':
for _env in ('staging', 'production'):
conn.execute('INSERT OR IGNORE INTO feature_flags(key, service, description, environment, enabled, rollout_percent) VALUES (?,?,?,?,0,0)', (pl['key'], service, pl.get('description', ''), _env))
elif ct == 'flag_cleanup':
conn.execute('DELETE FROM feature_flags WHERE key=?', (pl['key'],))
elif ct == 'test_fix':
if pl['action'] == 'fix':
conn.execute("UPDATE tests_catalog SET status='passing', quarantined=0 WHERE service=? AND name=?", (service, pl['test_name']))
else:
conn.execute('UPDATE tests_catalog SET quarantined=1 WHERE service=? AND name=?', (service, pl['test_name']))
elif ct == 'migration':
requires_migration = pl['name']
conn.execute('INSERT OR IGNORE INTO migrations(service, name, environment, status) VALUES (?,?,?,?)', (service, pl['name'], '', 'pending'))
elif ct == 'code_edit':
f = conn.execute('SELECT content FROM repo_files WHERE path=?', (pl['path'],)).fetchone()
if f is not None and pl['find'] in f['content']:
conn.execute('UPDATE repo_files SET content=? WHERE path=?', (f['content'].replace(pl['find'], pl['replace']), pl['path']))
old = conn.execute('SELECT repo_version FROM services WHERE name=?', (service,)).fetchone()[0]
parts = old.lstrip('v').split('.')
new_version = 'v' + parts[0] + '.' + parts[1] + '.' + str(int(parts[2]) + 1)
conn.execute('UPDATE services SET repo_version=? WHERE name=?', (new_version, service))
state = [[r['kind'], r['key'], r['value']] for r in conn.execute("SELECT kind, key, value FROM repo_state WHERE service=? AND kind IN ('config','dependency','endpoint','module') ORDER BY kind, key", (service,)).fetchall()]
conn.execute('INSERT INTO versions(service, version, state_json, requires_migration) VALUES (?,?,?,?)', (service, new_version, _json.dumps(state), requires_migration))
conn.execute("UPDATE pull_requests SET status='merged', merged_version=? WHERE number=?", (new_version, pr_number))
_audit(conn, 'merge_pull_request', service, {'pr_number': pr_number, 'version': new_version,
'ticket_key': pr['ticket_key']})
conn.commit()
out = {'ok': True, 'pr_number': pr_number, 'service': service, 'merged_version': new_version,
'next': 'deploy_service(service=..., environment="staging")'}
if requires_migration:
out['requires_migration'] = requires_migration
out['next'] = 'apply_migration then deploy_service'
return out
finally:
conn.close()
def apply_migration(db_path=None, service=None, name=None, environment=None):
"""Apply a database migration to an environment. Migrations are forward-only and must be applied before the code version that requires them is deployed there."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('apply_migration',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
if name is None:
return {'ok': False, 'error': 'missing required parameter: name'}
if environment is None:
return {'ok': False, 'error': 'missing required parameter: environment'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if environment not in ('staging', 'production'):
return {'ok': False, 'error': "environment must be 'staging' or 'production'"}
known = conn.execute('SELECT 1 FROM migrations WHERE service=? AND name=?', (service, name)).fetchone()
declared = conn.execute('SELECT 1 FROM migration_requirements WHERE service=? AND migration_name=?', (service, name)).fetchone()
if known is None and declared is None:
return {'ok': False, 'error': 'unknown migration ' + str(name) + ' for ' + str(service) + " (merge a PR carrying a 'migration' change first)"}
if conn.execute("SELECT 1 FROM migrations WHERE service=? AND name=? AND environment=? AND status='applied'", (service, name, environment)).fetchone():
return {'ok': False, 'error': 'migration ' + str(name) + ' is already applied in ' + environment}
conn.execute("INSERT INTO migrations(service, name, environment, status) VALUES (?,?,?,'applied') ON CONFLICT(service, name, environment) DO UPDATE SET status='applied'", (service, name, environment))
_audit(conn, 'apply_migration', service, {'name': name, 'environment': environment})
conn.commit()
return {'ok': True, 'service': service, 'name': name, 'environment': environment, 'status': 'applied'}
finally:
conn.close()
def deploy_service(db_path=None, service=None, environment=None, version=None, canary_percent=100):
"""Deploy a merged version to staging or production. canary_percent<100 stages a canary whose state only takes effect at promote_canary. Policy: production is staging-first; tier-1 services canary at <=25% then promote. A version whose migration is not applied is rejected."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('deploy_service',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
if environment is None:
return {'ok': False, 'error': 'missing required parameter: environment'}
def _apply(conn, _svc, _env, _version):
_row = conn.execute('SELECT state_json FROM versions WHERE service=? AND version=?', (_svc, _version)).fetchone()
if _row is None:
return False
conn.execute("DELETE FROM env_state WHERE service=? AND environment=? AND kind IN ('config','dependency','endpoint','module')", (_svc, _env))
for _k, _key, _val in _json.loads(_row[0]):
conn.execute('INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,?,?,?)', (_svc, _env, _k, _key, _val))
conn.execute("INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,'version','current',?) ON CONFLICT(service, environment, kind, key) DO UPDATE SET value=excluded.value", (_svc, _env, _version))
return True
def _recompute(conn):
_totals = {}
for _r in conn.execute('SELECT service, metric, kind, ckey, cvalue, value FROM metric_rules ORDER BY rule_id').fetchall():
_s, _m, _k, _ck, _cv, _v = _r['service'], _r['metric'], _r['kind'], _r['ckey'], _r['cvalue'], _r['value']
_hit = False
if _k == 'base':
_hit = True
elif _k == 'config_eq':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'xconfig_eq':
_os, _ok2 = _ck.split(':', 1)
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_os, _ok2)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'config_lt':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
try:
_hit = _row is not None and float(_row[0]) < float(_cv)
except Exception:
_hit = False
elif _k == 'flag_enabled':
_row = conn.execute("SELECT enabled FROM feature_flags WHERE key=? AND environment='production'", (_ck,)).fetchone()
_hit = _row is not None and int(_row[0]) == 1
elif _k == 'endpoint_status':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'version_ge':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='version' AND key='current'", (_s,)).fetchone()
_hit = _row is not None and _row[0] >= _cv
if _hit:
_totals[(_s, _m)] = _totals.get((_s, _m), 0.0) + _v
for (_s, _m), _v in _totals.items():
conn.execute("INSERT INTO service_metrics(service, environment, metric, value) VALUES (?, 'production', ?, ?) ON CONFLICT(service, environment, metric) DO UPDATE SET value=excluded.value", (_s, _m, round(_v, 3)))
for _r in conn.execute('SELECT service, metric, threshold FROM slos').fetchall():
_row = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_r['service'], _r['metric'])).fetchone()
if _row is None or _row[0] <= _r['threshold']:
continue
_a = conn.execute("SELECT alert_id FROM alerts WHERE service=? AND metric=? AND status != 'resolved'", (_r['service'], _r['metric'])).fetchone()
if _a is None:
conn.execute('INSERT INTO alerts(service, metric, severity, status, message) VALUES (?,?,?,?,?)', (_r['service'], _r['metric'], 'high', 'firing', _r['service'] + ' ' + _r['metric'] + ' ' + str(_row[0]) + ' exceeds SLO ' + str(_r['threshold'])))
for _vl in conn.execute("SELECT vuln_id, service, package, fixed_version FROM vulnerabilities WHERE status='open'").fetchall():
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (_vl['service'], _vl['package'])).fetchone()
if _row is not None and _row[0] >= _vl['fixed_version']:
conn.execute("UPDATE vulnerabilities SET status='remediated' WHERE vuln_id=?", (_vl['vuln_id'],))
def _breaching(conn, _svc):
_out = []
for _r in conn.execute('SELECT metric, threshold FROM slos WHERE service=?', (_svc,)).fetchall():
_v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_svc, _r['metric'])).fetchone()
if _v is not None and _v[0] > _r['threshold']:
_out.append(_r['metric'] + '=' + str(_v[0]) + ' (SLO ' + str(_r['threshold']) + ')')
return _out
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if conn.execute('SELECT 1 FROM services WHERE name=?', (service,)).fetchone() is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
if environment not in ('staging', 'production'):
return {'ok': False, 'error': "environment must be 'staging' or 'production'"}
canary_percent = int(canary_percent)
if canary_percent < 1 or canary_percent > 100:
return {'ok': False, 'error': 'canary_percent must be between 1 and 100'}
if environment == 'staging':
canary_percent = 100
if not version:
version = conn.execute('SELECT repo_version FROM services WHERE name=?', (service,)).fetchone()[0]
vrow = conn.execute('SELECT requires_migration FROM versions WHERE service=? AND version=?', (service, version)).fetchone()
if vrow is None:
return {'ok': False, 'error': 'unknown version ' + str(version) + ' for ' + service + ' (only merged versions can be deployed)'}
if vrow[0]:
ok_mig = conn.execute("SELECT 1 FROM migrations WHERE service=? AND name=? AND environment=? AND status='applied'", (service, vrow[0], environment)).fetchone()
if ok_mig is None:
return {'ok': False, 'error': 'deploy blocked: version ' + version + ' requires migration ' + vrow[0] + ' which is not applied in ' + environment + ' (run apply_migration first)'}
applied = canary_percent >= 100
status = 'succeeded' if applied else 'canary'
before = _breaching(conn, service) if environment == 'production' else []
cur = conn.execute('INSERT INTO deployments(service, environment, version, status, canary_percent) VALUES (?,?,?,?,?)',
(service, environment, version, status, canary_percent))
did = cur.lastrowid
new_alarms = []
if applied:
_apply(conn, service, environment, version)
_recompute(conn)
if environment == 'production':
new_alarms = [b for b in _breaching(conn, service) if b.split('=')[0] not in [x.split('=')[0] for x in before]]
_audit(conn, 'deploy_service', service, {'environment': environment, 'version': version,
'canary_percent': canary_percent, 'applied': applied,
'new_alarms': new_alarms})
conn.commit()
out = {'ok': True, 'deployment_id': did, 'service': service, 'environment': environment,
'version': version, 'status': status, 'canary_percent': canary_percent, 'applied': applied}
if new_alarms:
out['alarms_triggered'] = new_alarms
if not applied:
out['next'] = 'assess_canary(service=...) then promote_canary(service=...)'
return out
finally:
conn.close()
def assess_canary(db_path=None, service=None, environment='production'):
"""Evaluate the pending canary for a service: reports whether the canary version would breach any SLO or trip an alarm if promoted. Run this before promote_canary."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('assess_canary',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
def _apply(conn, _svc, _env, _version):
_row = conn.execute('SELECT state_json FROM versions WHERE service=? AND version=?', (_svc, _version)).fetchone()
if _row is None:
return False
conn.execute("DELETE FROM env_state WHERE service=? AND environment=? AND kind IN ('config','dependency','endpoint','module')", (_svc, _env))
for _k, _key, _val in _json.loads(_row[0]):
conn.execute('INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,?,?,?)', (_svc, _env, _k, _key, _val))
conn.execute("INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,'version','current',?) ON CONFLICT(service, environment, kind, key) DO UPDATE SET value=excluded.value", (_svc, _env, _version))
return True
def _recompute(conn):
_totals = {}
for _r in conn.execute('SELECT service, metric, kind, ckey, cvalue, value FROM metric_rules ORDER BY rule_id').fetchall():
_s, _m, _k, _ck, _cv, _v = _r['service'], _r['metric'], _r['kind'], _r['ckey'], _r['cvalue'], _r['value']
_hit = False
if _k == 'base':
_hit = True
elif _k == 'config_eq':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'xconfig_eq':
_os, _ok2 = _ck.split(':', 1)
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_os, _ok2)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'config_lt':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
try:
_hit = _row is not None and float(_row[0]) < float(_cv)
except Exception:
_hit = False
elif _k == 'flag_enabled':
_row = conn.execute("SELECT enabled FROM feature_flags WHERE key=? AND environment='production'", (_ck,)).fetchone()
_hit = _row is not None and int(_row[0]) == 1
elif _k == 'endpoint_status':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'version_ge':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='version' AND key='current'", (_s,)).fetchone()
_hit = _row is not None and _row[0] >= _cv
if _hit:
_totals[(_s, _m)] = _totals.get((_s, _m), 0.0) + _v
for (_s, _m), _v in _totals.items():
conn.execute("INSERT INTO service_metrics(service, environment, metric, value) VALUES (?, 'production', ?, ?) ON CONFLICT(service, environment, metric) DO UPDATE SET value=excluded.value", (_s, _m, round(_v, 3)))
for _r in conn.execute('SELECT service, metric, threshold FROM slos').fetchall():
_row = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_r['service'], _r['metric'])).fetchone()
if _row is None or _row[0] <= _r['threshold']:
continue
_a = conn.execute("SELECT alert_id FROM alerts WHERE service=? AND metric=? AND status != 'resolved'", (_r['service'], _r['metric'])).fetchone()
if _a is None:
conn.execute('INSERT INTO alerts(service, metric, severity, status, message) VALUES (?,?,?,?,?)', (_r['service'], _r['metric'], 'high', 'firing', _r['service'] + ' ' + _r['metric'] + ' ' + str(_row[0]) + ' exceeds SLO ' + str(_r['threshold'])))
for _vl in conn.execute("SELECT vuln_id, service, package, fixed_version FROM vulnerabilities WHERE status='open'").fetchall():
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (_vl['service'], _vl['package'])).fetchone()
if _row is not None and _row[0] >= _vl['fixed_version']:
conn.execute("UPDATE vulnerabilities SET status='remediated' WHERE vuln_id=?", (_vl['vuln_id'],))
def _breaching(conn, _svc):
_out = []
for _r in conn.execute('SELECT metric, threshold FROM slos WHERE service=?', (_svc,)).fetchall():
_v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_svc, _r['metric'])).fetchone()
if _v is not None and _v[0] > _r['threshold']:
_out.append(_r['metric'] + '=' + str(_v[0]) + ' (SLO ' + str(_r['threshold']) + ')')
return _out
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
d = conn.execute("SELECT * FROM deployments WHERE service=? AND environment=? AND status='canary' ORDER BY deployment_id DESC LIMIT 1", (service, environment)).fetchone()
if d is None:
return {'ok': False, 'error': 'no pending canary for ' + str(service) + ' in ' + str(environment)}
baseline = _breaching(conn, service)
saved = [(r['kind'], r['key'], r['value']) for r in conn.execute("SELECT kind, key, value FROM env_state WHERE service=? AND environment='production'", (service,)).fetchall()]
_apply(conn, service, 'production', d['version'])
_recompute(conn)
canary = _breaching(conn, service)
conn.execute("DELETE FROM env_state WHERE service=? AND environment='production'", (service,))
for k, key, val in saved:
conn.execute('INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,?,?,?)', (service, 'production', k, key, val))
_recompute(conn)
base_names = [x.split('=')[0] for x in baseline]
breaches = [b for b in canary if b.split('=')[0] not in base_names]
fixed = [b for b in baseline if b.split('=')[0] not in [x.split('=')[0] for x in canary]]
verdict = 'unhealthy' if breaches else 'healthy'
detail = ('canary introduces new SLO breaches: ' + '; '.join(breaches)) if breaches else (
('canary is healthy and clears: ' + '; '.join(fixed)) if fixed else
'no new SLO breach detected in the canary population')
conn.execute('INSERT INTO canary_assessments(deployment_id, service, verdict, detail) VALUES (?,?,?,?)',
(d['deployment_id'], service, verdict, detail))
_audit(conn, 'assess_canary', service, {'deployment_id': d['deployment_id'], 'version': d['version'],
'verdict': verdict})
conn.commit()
return {'ok': True, 'service': service, 'environment': environment, 'deployment_id': d['deployment_id'],
'version': d['version'], 'verdict': verdict, 'detail': detail,
'next': 'promote_canary' if verdict == 'healthy' else 'do NOT promote - fix the regression or roll back'}
finally:
conn.close()
def promote_canary(db_path=None, service=None, environment='production'):
"""Promote the pending canary to 100%; its state takes effect."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('promote_canary',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
def _apply(conn, _svc, _env, _version):
_row = conn.execute('SELECT state_json FROM versions WHERE service=? AND version=?', (_svc, _version)).fetchone()
if _row is None:
return False
conn.execute("DELETE FROM env_state WHERE service=? AND environment=? AND kind IN ('config','dependency','endpoint','module')", (_svc, _env))
for _k, _key, _val in _json.loads(_row[0]):
conn.execute('INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,?,?,?)', (_svc, _env, _k, _key, _val))
conn.execute("INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,'version','current',?) ON CONFLICT(service, environment, kind, key) DO UPDATE SET value=excluded.value", (_svc, _env, _version))
return True
def _recompute(conn):
_totals = {}
for _r in conn.execute('SELECT service, metric, kind, ckey, cvalue, value FROM metric_rules ORDER BY rule_id').fetchall():
_s, _m, _k, _ck, _cv, _v = _r['service'], _r['metric'], _r['kind'], _r['ckey'], _r['cvalue'], _r['value']
_hit = False
if _k == 'base':
_hit = True
elif _k == 'config_eq':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'xconfig_eq':
_os, _ok2 = _ck.split(':', 1)
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_os, _ok2)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'config_lt':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
try:
_hit = _row is not None and float(_row[0]) < float(_cv)
except Exception:
_hit = False
elif _k == 'flag_enabled':
_row = conn.execute("SELECT enabled FROM feature_flags WHERE key=? AND environment='production'", (_ck,)).fetchone()
_hit = _row is not None and int(_row[0]) == 1
elif _k == 'endpoint_status':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'version_ge':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='version' AND key='current'", (_s,)).fetchone()
_hit = _row is not None and _row[0] >= _cv
if _hit:
_totals[(_s, _m)] = _totals.get((_s, _m), 0.0) + _v
for (_s, _m), _v in _totals.items():
conn.execute("INSERT INTO service_metrics(service, environment, metric, value) VALUES (?, 'production', ?, ?) ON CONFLICT(service, environment, metric) DO UPDATE SET value=excluded.value", (_s, _m, round(_v, 3)))
for _r in conn.execute('SELECT service, metric, threshold FROM slos').fetchall():
_row = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_r['service'], _r['metric'])).fetchone()
if _row is None or _row[0] <= _r['threshold']:
continue
_a = conn.execute("SELECT alert_id FROM alerts WHERE service=? AND metric=? AND status != 'resolved'", (_r['service'], _r['metric'])).fetchone()
if _a is None:
conn.execute('INSERT INTO alerts(service, metric, severity, status, message) VALUES (?,?,?,?,?)', (_r['service'], _r['metric'], 'high', 'firing', _r['service'] + ' ' + _r['metric'] + ' ' + str(_row[0]) + ' exceeds SLO ' + str(_r['threshold'])))
for _vl in conn.execute("SELECT vuln_id, service, package, fixed_version FROM vulnerabilities WHERE status='open'").fetchall():
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (_vl['service'], _vl['package'])).fetchone()
if _row is not None and _row[0] >= _vl['fixed_version']:
conn.execute("UPDATE vulnerabilities SET status='remediated' WHERE vuln_id=?", (_vl['vuln_id'],))
def _breaching(conn, _svc):
_out = []
for _r in conn.execute('SELECT metric, threshold FROM slos WHERE service=?', (_svc,)).fetchall():
_v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_svc, _r['metric'])).fetchone()
if _v is not None and _v[0] > _r['threshold']:
_out.append(_r['metric'] + '=' + str(_v[0]) + ' (SLO ' + str(_r['threshold']) + ')')
return _out
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
d = conn.execute("SELECT * FROM deployments WHERE service=? AND environment=? AND status='canary' ORDER BY deployment_id DESC LIMIT 1", (service, environment)).fetchone()
if d is None:
return {'ok': False, 'error': 'no canary deployment to promote for ' + str(service) + ' in ' + str(environment)}
before = _breaching(conn, service) if environment == 'production' else []
conn.execute("UPDATE deployments SET status='succeeded', canary_percent=100 WHERE deployment_id=?", (d['deployment_id'],))
_apply(conn, service, environment, d['version'])
_recompute(conn)
new_alarms = []
if environment == 'production':
new_alarms = [b for b in _breaching(conn, service) if b.split('=')[0] not in [x.split('=')[0] for x in before]]
_audit(conn, 'promote_canary', service, {'environment': environment, 'version': d['version'],
'deployment_id': d['deployment_id'], 'new_alarms': new_alarms})
conn.commit()
out = {'ok': True, 'deployment_id': d['deployment_id'], 'service': service, 'environment': environment,
'version': d['version'], 'status': 'succeeded'}
if new_alarms:
out['alarms_triggered'] = new_alarms
return out
finally:
conn.close()
def rollback_deployment(db_path=None, service=None, environment='production'):
"""Emergency rollback to the previous successful deployment. Exempt from staging-first."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('rollback_deployment',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
def _apply(conn, _svc, _env, _version):
_row = conn.execute('SELECT state_json FROM versions WHERE service=? AND version=?', (_svc, _version)).fetchone()
if _row is None:
return False
conn.execute("DELETE FROM env_state WHERE service=? AND environment=? AND kind IN ('config','dependency','endpoint','module')", (_svc, _env))
for _k, _key, _val in _json.loads(_row[0]):
conn.execute('INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,?,?,?)', (_svc, _env, _k, _key, _val))
conn.execute("INSERT INTO env_state(service, environment, kind, key, value) VALUES (?,?,'version','current',?) ON CONFLICT(service, environment, kind, key) DO UPDATE SET value=excluded.value", (_svc, _env, _version))
return True
def _recompute(conn):
_totals = {}
for _r in conn.execute('SELECT service, metric, kind, ckey, cvalue, value FROM metric_rules ORDER BY rule_id').fetchall():
_s, _m, _k, _ck, _cv, _v = _r['service'], _r['metric'], _r['kind'], _r['ckey'], _r['cvalue'], _r['value']
_hit = False
if _k == 'base':
_hit = True
elif _k == 'config_eq':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'xconfig_eq':
_os, _ok2 = _ck.split(':', 1)
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_os, _ok2)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'config_lt':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
try:
_hit = _row is not None and float(_row[0]) < float(_cv)
except Exception:
_hit = False
elif _k == 'flag_enabled':
_row = conn.execute("SELECT enabled FROM feature_flags WHERE key=? AND environment='production'", (_ck,)).fetchone()
_hit = _row is not None and int(_row[0]) == 1
elif _k == 'endpoint_status':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'version_ge':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='version' AND key='current'", (_s,)).fetchone()
_hit = _row is not None and _row[0] >= _cv
if _hit:
_totals[(_s, _m)] = _totals.get((_s, _m), 0.0) + _v
for (_s, _m), _v in _totals.items():
conn.execute("INSERT INTO service_metrics(service, environment, metric, value) VALUES (?, 'production', ?, ?) ON CONFLICT(service, environment, metric) DO UPDATE SET value=excluded.value", (_s, _m, round(_v, 3)))
for _r in conn.execute('SELECT service, metric, threshold FROM slos').fetchall():
_row = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_r['service'], _r['metric'])).fetchone()
if _row is None or _row[0] <= _r['threshold']:
continue
_a = conn.execute("SELECT alert_id FROM alerts WHERE service=? AND metric=? AND status != 'resolved'", (_r['service'], _r['metric'])).fetchone()
if _a is None:
conn.execute('INSERT INTO alerts(service, metric, severity, status, message) VALUES (?,?,?,?,?)', (_r['service'], _r['metric'], 'high', 'firing', _r['service'] + ' ' + _r['metric'] + ' ' + str(_row[0]) + ' exceeds SLO ' + str(_r['threshold'])))
for _vl in conn.execute("SELECT vuln_id, service, package, fixed_version FROM vulnerabilities WHERE status='open'").fetchall():
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (_vl['service'], _vl['package'])).fetchone()
if _row is not None and _row[0] >= _vl['fixed_version']:
conn.execute("UPDATE vulnerabilities SET status='remediated' WHERE vuln_id=?", (_vl['vuln_id'],))
def _breaching(conn, _svc):
_out = []
for _r in conn.execute('SELECT metric, threshold FROM slos WHERE service=?', (_svc,)).fetchall():
_v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_svc, _r['metric'])).fetchone()
if _v is not None and _v[0] > _r['threshold']:
_out.append(_r['metric'] + '=' + str(_v[0]) + ' (SLO ' + str(_r['threshold']) + ')')
return _out
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
cur_d = conn.execute("SELECT * FROM deployments WHERE service=? AND environment=? AND status='succeeded' ORDER BY deployment_id DESC LIMIT 1", (service, environment)).fetchone()
if cur_d is None:
return {'ok': False, 'error': 'no successful deployment to roll back for ' + str(service)}
prev = conn.execute("SELECT * FROM deployments WHERE service=? AND environment=? AND status='succeeded' AND deployment_id<? ORDER BY deployment_id DESC LIMIT 1", (service, environment, cur_d['deployment_id'])).fetchone()
if prev is None:
return {'ok': False, 'error': 'no previous successful deployment to roll back to'}
conn.execute("UPDATE deployments SET status='rolled_back' WHERE deployment_id=?", (cur_d['deployment_id'],))
_apply(conn, service, environment, prev['version'])
_recompute(conn)
_audit(conn, 'rollback_deployment', service, {'environment': environment,
'from_version': cur_d['version'],
'to_version': prev['version']})
conn.commit()
return {'ok': True, 'service': service, 'environment': environment,
'from_version': cur_d['version'], 'to_version': prev['version']}
finally:
conn.close()
def set_feature_flag(db_path=None, key=None, environment=None, enabled=None, rollout_percent=None):
"""Toggle a feature flag or change its rollout percent in one environment. Runtime operation: takes effect immediately, no deploy needed."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('set_feature_flag',)); conn.commit()
except Exception:
pass
if key is None:
return {'ok': False, 'error': 'missing required parameter: key'}
if environment is None:
return {'ok': False, 'error': 'missing required parameter: environment'}
def _recompute(conn):
_totals = {}
for _r in conn.execute('SELECT service, metric, kind, ckey, cvalue, value FROM metric_rules ORDER BY rule_id').fetchall():
_s, _m, _k, _ck, _cv, _v = _r['service'], _r['metric'], _r['kind'], _r['ckey'], _r['cvalue'], _r['value']
_hit = False
if _k == 'base':
_hit = True
elif _k == 'config_eq':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'xconfig_eq':
_os, _ok2 = _ck.split(':', 1)
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_os, _ok2)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'config_lt':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
try:
_hit = _row is not None and float(_row[0]) < float(_cv)
except Exception:
_hit = False
elif _k == 'flag_enabled':
_row = conn.execute("SELECT enabled FROM feature_flags WHERE key=? AND environment='production'", (_ck,)).fetchone()
_hit = _row is not None and int(_row[0]) == 1
elif _k == 'endpoint_status':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'version_ge':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='version' AND key='current'", (_s,)).fetchone()
_hit = _row is not None and _row[0] >= _cv
if _hit:
_totals[(_s, _m)] = _totals.get((_s, _m), 0.0) + _v
for (_s, _m), _v in _totals.items():
conn.execute("INSERT INTO service_metrics(service, environment, metric, value) VALUES (?, 'production', ?, ?) ON CONFLICT(service, environment, metric) DO UPDATE SET value=excluded.value", (_s, _m, round(_v, 3)))
for _r in conn.execute('SELECT service, metric, threshold FROM slos').fetchall():
_row = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_r['service'], _r['metric'])).fetchone()
if _row is None or _row[0] <= _r['threshold']:
continue
_a = conn.execute("SELECT alert_id FROM alerts WHERE service=? AND metric=? AND status != 'resolved'", (_r['service'], _r['metric'])).fetchone()
if _a is None:
conn.execute('INSERT INTO alerts(service, metric, severity, status, message) VALUES (?,?,?,?,?)', (_r['service'], _r['metric'], 'high', 'firing', _r['service'] + ' ' + _r['metric'] + ' ' + str(_row[0]) + ' exceeds SLO ' + str(_r['threshold'])))
for _vl in conn.execute("SELECT vuln_id, service, package, fixed_version FROM vulnerabilities WHERE status='open'").fetchall():
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (_vl['service'], _vl['package'])).fetchone()
if _row is not None and _row[0] >= _vl['fixed_version']:
conn.execute("UPDATE vulnerabilities SET status='remediated' WHERE vuln_id=?", (_vl['vuln_id'],))
def _breaching(conn, _svc):
_out = []
for _r in conn.execute('SELECT metric, threshold FROM slos WHERE service=?', (_svc,)).fetchall():
_v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_svc, _r['metric'])).fetchone()
if _v is not None and _v[0] > _r['threshold']:
_out.append(_r['metric'] + '=' + str(_v[0]) + ' (SLO ' + str(_r['threshold']) + ')')
return _out
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if environment not in ('staging', 'production'):
return {'ok': False, 'error': "environment must be 'staging' or 'production'"}
row = conn.execute('SELECT * FROM feature_flags WHERE key=? AND environment=?', (key, environment)).fetchone()
if row is None:
return {'ok': False, 'error': 'unknown flag ' + str(key) + ' in ' + str(environment) + " (define flags via a 'flag' PR change)"}
if enabled is None and rollout_percent is None:
return {'ok': False, 'error': 'provide enabled and/or rollout_percent'}
ne = row['enabled']
if enabled is not None:
ne = 1 if str(enabled).lower() in ('1', 'true', 'yes', 'on') else 0
nr = row['rollout_percent']
if rollout_percent is not None:
nr = int(rollout_percent)
if nr < 0 or nr > 100:
return {'ok': False, 'error': 'rollout_percent must be between 0 and 100'}
conn.execute('UPDATE feature_flags SET enabled=?, rollout_percent=? WHERE key=? AND environment=?', (ne, nr, key, environment))
_recompute(conn)
_audit(conn, 'set_feature_flag', row['service'], {'key': key, 'environment': environment,
'enabled': ne, 'rollout_percent': nr})
conn.commit()
return {'ok': True, 'key': key, 'environment': environment, 'enabled': ne, 'rollout_percent': nr}
finally:
conn.close()
def shift_endpoint_traffic(db_path=None, service=None, path=None, traffic_percent=None):
"""Set the production traffic percent served by an endpoint (gateway runtime weight, no deploy needed). Policy: shift in stages of at most 50 points per step."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('shift_endpoint_traffic',)); conn.commit()
except Exception:
pass
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
if traffic_percent is None:
return {'ok': False, 'error': 'missing required parameter: traffic_percent'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
tp = int(traffic_percent)
if tp < 0 or tp > 100:
return {'ok': False, 'error': 'traffic_percent must be between 0 and 100'}
row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='traffic' AND key=?", (service, path)).fetchone()
if row is None:
return {'ok': False, 'error': 'no production traffic record for ' + str(path) + ' on ' + str(service)}
old = int(row[0])
conn.execute("UPDATE env_state SET value=? WHERE service=? AND environment='production' AND kind='traffic' AND key=?", (str(tp), service, path))
conn.execute('UPDATE traffic_profile SET share_pct=? WHERE service=? AND route LIKE ?', (tp, service, '%' + path))
_audit(conn, 'shift_endpoint_traffic', service, {'path': path, 'from_percent': old, 'to_percent': tp})
conn.commit()
return {'ok': True, 'service': service, 'path': path, 'from_percent': old, 'to_percent': tp}
finally:
conn.close()
def acknowledge_alert(db_path=None, alert_id=None):
"""Acknowledge a firing alarm."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('acknowledge_alert',)); conn.commit()
except Exception:
pass
if alert_id is None:
return {'ok': False, 'error': 'missing required parameter: alert_id'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
alert_id = int(alert_id)
row = conn.execute('SELECT * FROM alerts WHERE alert_id=?', (alert_id,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such alert: ' + str(alert_id)}
if row['status'] == 'resolved':
return {'ok': False, 'error': 'alert ' + str(alert_id) + ' is already resolved'}
conn.execute("UPDATE alerts SET status='acknowledged' WHERE alert_id=?", (alert_id,))
_audit(conn, 'acknowledge_alert', row['service'], {'alert_id': alert_id})
conn.commit()
return {'ok': True, 'alert_id': alert_id, 'status': 'acknowledged'}
finally:
conn.close()
def resolve_alert(db_path=None, alert_id=None):
"""Resolve an alarm. Refused while the underlying metric still breaches its SLO."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('resolve_alert',)); conn.commit()
except Exception:
pass
if alert_id is None:
return {'ok': False, 'error': 'missing required parameter: alert_id'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
alert_id = int(alert_id)
row = conn.execute('SELECT * FROM alerts WHERE alert_id=?', (alert_id,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such alert: ' + str(alert_id)}
if row['status'] == 'resolved':
return {'ok': False, 'error': 'alert ' + str(alert_id) + ' is already resolved'}
slo = conn.execute('SELECT threshold FROM slos WHERE service=? AND metric=?', (row['service'], row['metric'])).fetchone()
if slo is not None:
mv = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (row['service'], row['metric'])).fetchone()
if mv is not None and mv[0] > slo[0]:
return {'ok': False, 'error': row['service'] + ' ' + row['metric'] + ' is still ' + str(mv[0]) + ' (SLO ' + str(slo[0]) + ') - fix and deploy before resolving'}
conn.execute("UPDATE alerts SET status='resolved' WHERE alert_id=?", (alert_id,))
_audit(conn, 'resolve_alert', row['service'], {'alert_id': alert_id})
conn.commit()
return {'ok': True, 'alert_id': alert_id, 'status': 'resolved'}
finally:
conn.close()
def resolve_error_event(db_path=None, fingerprint=None):
"""Mark an error-tracking issue resolved. Refused while the owning service still breaches an SLO."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('resolve_error_event',)); conn.commit()
except Exception:
pass
if fingerprint is None:
return {'ok': False, 'error': 'missing required parameter: fingerprint'}
def _recompute(conn):
_totals = {}
for _r in conn.execute('SELECT service, metric, kind, ckey, cvalue, value FROM metric_rules ORDER BY rule_id').fetchall():
_s, _m, _k, _ck, _cv, _v = _r['service'], _r['metric'], _r['kind'], _r['ckey'], _r['cvalue'], _r['value']
_hit = False
if _k == 'base':
_hit = True
elif _k == 'config_eq':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'xconfig_eq':
_os, _ok2 = _ck.split(':', 1)
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_os, _ok2)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'config_lt':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='config' AND key=?", (_s, _ck)).fetchone()
try:
_hit = _row is not None and float(_row[0]) < float(_cv)
except Exception:
_hit = False
elif _k == 'flag_enabled':
_row = conn.execute("SELECT enabled FROM feature_flags WHERE key=? AND environment='production'", (_ck,)).fetchone()
_hit = _row is not None and int(_row[0]) == 1
elif _k == 'endpoint_status':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='endpoint' AND key=?", (_s, _ck)).fetchone()
_hit = _row is not None and _row[0] == _cv
elif _k == 'version_ge':
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='version' AND key='current'", (_s,)).fetchone()
_hit = _row is not None and _row[0] >= _cv
if _hit:
_totals[(_s, _m)] = _totals.get((_s, _m), 0.0) + _v
for (_s, _m), _v in _totals.items():
conn.execute("INSERT INTO service_metrics(service, environment, metric, value) VALUES (?, 'production', ?, ?) ON CONFLICT(service, environment, metric) DO UPDATE SET value=excluded.value", (_s, _m, round(_v, 3)))
for _r in conn.execute('SELECT service, metric, threshold FROM slos').fetchall():
_row = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_r['service'], _r['metric'])).fetchone()
if _row is None or _row[0] <= _r['threshold']:
continue
_a = conn.execute("SELECT alert_id FROM alerts WHERE service=? AND metric=? AND status != 'resolved'", (_r['service'], _r['metric'])).fetchone()
if _a is None:
conn.execute('INSERT INTO alerts(service, metric, severity, status, message) VALUES (?,?,?,?,?)', (_r['service'], _r['metric'], 'high', 'firing', _r['service'] + ' ' + _r['metric'] + ' ' + str(_row[0]) + ' exceeds SLO ' + str(_r['threshold'])))
for _vl in conn.execute("SELECT vuln_id, service, package, fixed_version FROM vulnerabilities WHERE status='open'").fetchall():
_row = conn.execute("SELECT value FROM env_state WHERE service=? AND environment='production' AND kind='dependency' AND key=?", (_vl['service'], _vl['package'])).fetchone()
if _row is not None and _row[0] >= _vl['fixed_version']:
conn.execute("UPDATE vulnerabilities SET status='remediated' WHERE vuln_id=?", (_vl['vuln_id'],))
def _breaching(conn, _svc):
_out = []
for _r in conn.execute('SELECT metric, threshold FROM slos WHERE service=?', (_svc,)).fetchall():
_v = conn.execute("SELECT value FROM service_metrics WHERE service=? AND environment='production' AND metric=?", (_svc, _r['metric'])).fetchone()
if _v is not None and _v[0] > _r['threshold']:
_out.append(_r['metric'] + '=' + str(_v[0]) + ' (SLO ' + str(_r['threshold']) + ')')
return _out
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
row = conn.execute('SELECT * FROM error_events WHERE fingerprint=?', (fingerprint,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such error issue: ' + str(fingerprint)}
if row['status'] == 'resolved':
return {'ok': False, 'error': 'error issue ' + str(fingerprint) + ' is already resolved'}
bad = _breaching(conn, row['service'])
if bad:
return {'ok': False, 'error': row['service'] + ' is still breaching: ' + '; '.join(bad)}
conn.execute("UPDATE error_events SET status='resolved' WHERE fingerprint=?", (fingerprint,))
_audit(conn, 'resolve_error_event', row['service'], {'fingerprint': fingerprint})
conn.commit()
return {'ok': True, 'fingerprint': fingerprint, 'status': 'resolved'}
finally:
conn.close()
def create_incident(db_path=None, title=None, service=None, severity=None):
"""Declare an incident."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('create_incident',)); conn.commit()
except Exception:
pass
if title is None:
return {'ok': False, 'error': 'missing required parameter: title'}
if service is None:
return {'ok': False, 'error': 'missing required parameter: service'}
if severity is None:
return {'ok': False, 'error': 'missing required parameter: severity'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if severity not in ('sev1', 'sev2', 'sev3'):
return {'ok': False, 'error': 'severity must be sev1|sev2|sev3'}
if conn.execute('SELECT 1 FROM services WHERE name=?', (service,)).fetchone() is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
cur = conn.execute('INSERT INTO incidents(severity, title, service, status) VALUES (?,?,?,?)',
(severity, title, service, 'open'))
_audit(conn, 'create_incident', service, {'incident_id': cur.lastrowid, 'severity': severity})
conn.commit()
return {'ok': True, 'incident_id': cur.lastrowid, 'status': 'open'}
finally:
conn.close()
def update_incident(db_path=None, incident_id=None, status=None, commander=None):
"""Update an incident's status (open|mitigated|resolved) and/or commander."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('update_incident',)); conn.commit()
except Exception:
pass
if incident_id is None:
return {'ok': False, 'error': 'missing required parameter: incident_id'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
incident_id = int(incident_id)
row = conn.execute('SELECT * FROM incidents WHERE incident_id=?', (incident_id,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such incident: ' + str(incident_id)}
if status is None and commander is None:
return {'ok': False, 'error': 'provide status and/or commander'}
if status is not None and status not in ('open', 'mitigated', 'resolved'):
return {'ok': False, 'error': 'invalid status: ' + str(status)}
ns = row['status'] if status is None else status
nc = row['commander'] if commander is None else commander
conn.execute('UPDATE incidents SET status=?, commander=? WHERE incident_id=?', (ns, nc, incident_id))
_audit(conn, 'update_incident', row['service'], {'incident_id': incident_id, 'status': ns})
conn.commit()
return {'ok': True, 'incident_id': incident_id, 'status': ns, 'commander': nc}
finally:
conn.close()
def publish_status_update(db_path=None, state=None, title=None, body=''):
"""Publish an update to the public system-status page (state: investigating|identified|monitoring|resolved)."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('publish_status_update',)); conn.commit()
except Exception:
pass
if state is None:
return {'ok': False, 'error': 'missing required parameter: state'}
if title is None:
return {'ok': False, 'error': 'missing required parameter: title'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if state not in ('investigating', 'identified', 'monitoring', 'resolved'):
return {'ok': False, 'error': 'state must be investigating|identified|monitoring|resolved'}
cur = conn.execute('INSERT INTO status_page(state, title, body) VALUES (?,?,?)', (state, title, body))
_audit(conn, 'publish_status_update', '', {'post_id': cur.lastrowid, 'state': state, 'title': title})
conn.commit()
return {'ok': True, 'post_id': cur.lastrowid, 'state': state}
finally:
conn.close()
def submit_diagnosis(db_path=None, scope=None, fault_detected=None, service='', fault_type='none', offending_key='', evidence=''):
"""Submit a diagnostic finding for an investigation. `scope` is what you were asked to investigate (a service name or alarm id). Set fault_detected=false with fault_type='none' when the scope is healthy. fault_type is one of: misconfig, missing_retry, missing_timeout, resource_exhaustion, unbounded_prefetch, cache_disabled, n_plus_one_query, cdn_bypass, bad_release, feature_flag_regression, node_unhealthy, unclassified, none. Use node_unhealthy when the cause is the node a service runs on rather than the service's own code or config. Use unclassified when you have established that a fault is present but were not asked for, and have not determined, the mechanism - reporting a breach you can evidence is better than reporting health you cannot. offending_key is the specific config key, flag key, node name or version responsible."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('submit_diagnosis',)); conn.commit()
except Exception:
pass
if scope is None:
return {'ok': False, 'error': 'missing required parameter: scope'}
if fault_detected is None:
return {'ok': False, 'error': 'missing required parameter: fault_detected'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
kinds = ('misconfig', 'missing_retry', 'missing_timeout', 'resource_exhaustion', 'unbounded_prefetch', 'cache_disabled', 'n_plus_one_query', 'cdn_bypass', 'bad_release', 'feature_flag_regression', 'node_unhealthy', 'unclassified', 'none')
if fault_type not in kinds:
return {'ok': False, 'error': 'fault_type must be one of: ' + ', '.join(kinds)}
_t, _f = ('1', 'true', 'yes', 'on'), ('0', 'false', 'no', 'off')
_v = str(fault_detected).lower()
if _v not in _t + _f:
return {'ok': False, 'error': 'fault_detected must be true or false, not ' + repr(fault_detected)}
det = 1 if _v in _t else 0
if service and conn.execute('SELECT 1 FROM services WHERE name=?', (service,)).fetchone() is None:
return {'ok': False, 'error': 'unknown service: ' + str(service)}
if det and fault_type == 'none':
return {'ok': False, 'error': "fault_detected=true requires a specific fault_type"}
if not det and fault_type != 'none':
return {'ok': False, 'error': "fault_detected=false requires fault_type='none'"}
cur = conn.execute('INSERT INTO diagnoses(scope, fault_detected, service, fault_type, offending_key, evidence) VALUES (?,?,?,?,?,?)',
(str(scope), det, service or '', fault_type, offending_key or '', evidence or ''))
_audit(conn, 'submit_diagnosis', service or '', {'scope': str(scope), 'fault_detected': det,
'fault_type': fault_type,
'offending_key': offending_key or ''})
conn.commit()
return {'ok': True, 'diagnosis_id': cur.lastrowid, 'scope': str(scope),
'fault_detected': bool(det), 'service': service or '', 'fault_type': fault_type,
'offending_key': offending_key or ''}
finally:
conn.close()
def post_message(db_path=None, channel=None, body=None):
"""Post a message to a chat channel."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('post_message',)); conn.commit()
except Exception:
pass
if channel is None:
return {'ok': False, 'error': 'missing required parameter: channel'}
if body is None:
return {'ok': False, 'error': 'missing required parameter: body'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if conn.execute('SELECT 1 FROM channels WHERE channel=?', (channel,)).fetchone() is None:
rows = [r[0] for r in conn.execute('SELECT channel FROM channels ORDER BY channel').fetchall()]
return {'ok': False, 'error': 'unknown channel ' + str(channel) + '; valid: ' + ', '.join(rows)}
if not str(body).strip():
return {'ok': False, 'error': 'body must not be empty'}
cur = conn.execute('INSERT INTO messages(channel, author, body) VALUES (?,?,?)', (channel, 'agent', str(body)))
_audit(conn, 'post_message', '', {'channel': channel, 'message_id': cur.lastrowid})
conn.commit()
return {'ok': True, 'message_id': cur.lastrowid, 'channel': channel}
finally:
conn.close()
def jira_search(db_path=None, project=None, status=None, issue_type=None, component=None, limit=50):
"""Search Jira issues. Jira status is a per-project workflow, not open/closed: a resolved issue has status='Done' AND a resolution set. Filter by project, status, issue_type, component or priority."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('jira_search',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM jira_issues'
conds, args = [], []
for col, val in (('project', project), ('status', status), ('issue_type', issue_type),
('component', component)):
if val:
conds.append(col + '=?'); args.append(val)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY key LIMIT ?', args + [int(limit)]).fetchall()]
finally:
conn.close()
def jira_get_issue(db_path=None, key=None):
"""Fetch one Jira issue by key, including any links to issues in other trackers."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('jira_get_issue',)); conn.commit()
except Exception:
pass
if key is None:
return {'ok': False, 'error': 'missing required parameter: key'}
row = conn.execute('SELECT * FROM jira_issues WHERE key=?', (key,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such issue: ' + str(key)}
d = dict(row)
d['links'] = [dict(r) for r in conn.execute(
'SELECT target, kind FROM issue_links WHERE source=?', (key,)).fetchall()]
return d
finally:
conn.close()
def linear_list_issues(db_path=None, team=None, state=None):
"""List Linear issues. Linear priority is numeric: 0=none, 1=urgent, 2=high, 3=normal, 4=low - it does not map cleanly onto Jira priority names."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('linear_list_issues',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM linear_issues'
conds, args = [], []
if team:
conds.append('team=?'); args.append(team)
if state:
conds.append('state=?'); args.append(state)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY identifier', args).fetchall()]
finally:
conn.close()
def github_list_issues(db_path=None, repo=None, state=None, label=None):
"""List GitHub issues. GitHub has only state=open|closed; severity lives in labels if anywhere."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('github_list_issues',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM github_issues'
conds, args = [], []
if repo:
conds.append('repo=?'); args.append(repo)
if state:
conds.append('state=?'); args.append(state)
if label:
conds.append('labels LIKE ?'); args.append('%' + str(label) + '%')
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY number', args).fetchall()]
finally:
conn.close()
def list_issue_links(db_path=None, source=None):
"""List known cross-tracker links (duplicates/relates/implements). This is the only place the trackers are reconciled; neither tracker knows about it."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_issue_links',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM issue_links'
args = []
if source:
sql += ' WHERE source=? OR target=?'; args = [source, source]
return [dict(r) for r in conn.execute(sql, args).fetchall()]
finally:
conn.close()
def query_prometheus(db_path=None, metric=None, label_service=None, label_env=None, day_from=None, day_to=None):
"""Query a Prometheus series by metric and label selectors. Note the label spelling is Prometheus's own (e.g. checkout_service), and counter resets are flagged: a rate() over a reset under-reports."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('query_prometheus',)); conn.commit()
except Exception:
pass
if metric is None:
return {'ok': False, 'error': 'missing required parameter: metric'}
sql = 'SELECT * FROM prom_series WHERE metric=?'
args = [metric]
if label_service:
sql += ' AND label_service=?'; args.append(label_service)
if label_env:
sql += ' AND label_env=?'; args.append(label_env)
if day_from is not None:
sql += ' AND day >= ?'; args.append(int(day_from))
if day_to is not None:
sql += ' AND day <= ?'; args.append(int(day_to))
return [dict(r) for r in conn.execute(sql + ' ORDER BY day', args).fetchall()]
finally:
conn.close()
def list_prometheus_label_values(db_path=None, label='label_service'):
"""List the values a Prometheus label actually takes. Use this when you are not sure how a service is spelled in metrics."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_prometheus_label_values',)); conn.commit()
except Exception:
pass
col = 'label_service' if str(label).endswith('service') else 'label_env'
return [r[0] for r in conn.execute(
'SELECT DISTINCT ' + col + ' FROM prom_series ORDER BY 1').fetchall()]
finally:
conn.close()
def sentry_search_issues(db_path=None, project_slug=None, status=None):
"""Search Sentry issues (grouped exceptions). Event counts are SAMPLED at the project's sample_rate - see sentry_list_projects - so they are a fraction of the true volume and are not comparable to Prometheus counters."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('sentry_search_issues',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM sentry_issues'
conds, args = [], []
if project_slug:
conds.append('project_slug=?'); args.append(project_slug)
if status:
conds.append('status=?'); args.append(status)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY events DESC', args).fetchall()]
finally:
conn.close()
def sentry_list_projects(db_path=None):
"""List Sentry projects with their event sample rates."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('sentry_list_projects',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM sentry_projects ORDER BY slug').fetchall()]
finally:
conn.close()
def pd_list_incidents(db_path=None, since_day=None, until_day=None, urgency=None, status=None):
"""List PagerDuty incidents in a day range. urgency (high|low) and priority (P1..P4) are separate vocabularies; neither records whether customers saw it."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('pd_list_incidents',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM pd_incidents'
conds, args = [], []
if since_day is not None:
conds.append('created_day >= ?'); args.append(int(since_day))
if until_day is not None:
conds.append('created_day <= ?'); args.append(int(until_day))
if urgency:
conds.append('urgency=?'); args.append(urgency)
if status:
conds.append('status=?'); args.append(status)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY incident_number', args).fetchall()]
finally:
conn.close()
def pd_list_services(db_path=None):
"""List PagerDuty technical services and escalation policies."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('pd_list_services',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM pd_services ORDER BY pd_service_id').fetchall()]
finally:
conn.close()
def pd_list_oncalls(db_path=None, day=None, escalation_policy=None):
"""Who is on call, by day and escalation policy."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('pd_list_oncalls',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM pd_oncall'
conds, args = [], []
if day is not None:
conds.append('day=?'); args.append(int(day))
if escalation_policy:
conds.append('escalation_policy=?'); args.append(escalation_policy)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY schedule_id', args).fetchall()]
finally:
conn.close()
def pd_list_change_events(db_path=None, pd_service_id=None, since_day=None):
"""Change events recorded against a PagerDuty service. These exist only where someone wired the integration."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('pd_list_change_events',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM pd_change_events'
conds, args = [], []
if pd_service_id:
conds.append('pd_service_id=?'); args.append(pd_service_id)
if since_day is not None:
conds.append('day >= ?'); args.append(int(since_day))
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY day', args).fetchall()]
finally:
conn.close()
def list_status_page_posts(db_path=None, since_day=None, impact=None):
"""Public status-page posts. This is the ONLY system that records customer impact; incidents do not carry it. The status page also lags internal state."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_status_page_posts',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM status_page_posts'
conds, args = [], []
if since_day is not None:
conds.append('published_day >= ?'); args.append(int(since_day))
if impact:
conds.append('impact=?'); args.append(impact)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY post_id', args).fetchall()]
finally:
conn.close()
def confluence_search(db_path=None, query='', space=None):
"""Search the Confluence wiki. Pages carry a last_updated_day; some are stale."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('confluence_search',)); conn.commit()
except Exception:
pass
sql = 'SELECT page_id, space, title, last_updated_day, stale FROM confluence_pages'
conds, args = [], []
if query:
conds.append('(title LIKE ? OR body LIKE ?)'); args += ['%' + str(query) + '%'] * 2
if space:
conds.append('space=?'); args.append(space)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY page_id', args).fetchall()]
finally:
conn.close()
def confluence_get_page(db_path=None, page_id=None):
"""Read one Confluence page in full."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('confluence_get_page',)); conn.commit()
except Exception:
pass
if page_id is None:
return {'ok': False, 'error': 'missing required parameter: page_id'}
row = conn.execute('SELECT * FROM confluence_pages WHERE page_id=?', (int(page_id),)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such page: ' + str(page_id)}
return dict(row)
finally:
conn.close()
def read_owner_spreadsheet(db_path=None):
"""Read the hand-maintained service-owner spreadsheet. Note last_reviewed_day: rows drift as teams reorganise, and the sheet uses its own week convention."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('read_owner_spreadsheet',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM owner_spreadsheet ORDER BY row_id').fetchall()]
finally:
conn.close()
def query_local_deploy_log(db_path=None, service=None, environment=None, since_day=None, include_rollbacks=True):
"""Query a team's local deploy log (a SQLite file kept because the central one is slow). Environment strings are free text and include 'nonprod-*' spellings; rollbacks are flagged separately."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('query_local_deploy_log',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM local_deploy_log'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if environment:
conds.append('environment=?'); args.append(environment)
if since_day is not None:
conds.append('day >= ?'); args.append(int(since_day))
if str(include_rollbacks).lower() in ('0', 'false', 'no'):
conds.append('was_rollback=0')
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY day', args).fetchall()]
finally:
conn.close()
def resolve_service_alias(db_path=None, name=None):
"""Resolve any spelling of a service to its canonical name, and list every spelling it has across systems. Use this before comparing data from two tools."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('resolve_service_alias',)); conn.commit()
except Exception:
pass
if name is None:
return {'ok': False, 'error': 'missing required parameter: name'}
row = conn.execute('SELECT canonical FROM service_aliases WHERE alias=? OR canonical=? LIMIT 1',
(name, name)).fetchone()
if row is None:
return {'ok': False, 'error': 'no service matches ' + str(name) +
'; try list_service_aliases()'}
canon = row[0]
return {'canonical': canon,
'aliases': [dict(r) for r in conn.execute(
'SELECT alias, system FROM service_aliases WHERE canonical=? ORDER BY system',
(canon,)).fetchall()]}
finally:
conn.close()
def list_service_aliases(db_path=None):
"""Every known service-name spelling, by system."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_service_aliases',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute(
'SELECT canonical, alias, system FROM service_aliases ORDER BY canonical, system').fetchall()]
finally:
conn.close()
def list_approval_policy(db_path=None):
"""Which actions require a human sign-off before you may perform them, and why. The trigger is irreversibility, not difficulty."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_approval_policy',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM approval_policy ORDER BY policy_id').fetchall()]
finally:
conn.close()
def request_approval(db_path=None, action=None, target='', reason=None):
"""Ask the accountable human to approve an irreversible action, and get their answer. State the action exactly as the approval policy names it, the target, and why it is necessary. A vague or unjustified request is refused - the approver is not a rubber stamp."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('request_approval',)); conn.commit()
except Exception:
pass
if action is None:
return {'ok': False, 'error': 'missing required parameter: action'}
if reason is None:
return {'ok': False, 'error': 'missing required parameter: reason'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
pol = conn.execute('SELECT * FROM approval_policy WHERE action=?', (action,)).fetchone()
if pol is None:
known = [r[0] for r in conn.execute('SELECT action FROM approval_policy').fetchall()]
return {'ok': False, 'error': 'no approval policy for ' + str(action) +
'; actions requiring sign-off: ' + ', '.join(known)}
if len((reason or '').strip()) < 40:
return {'ok': False, 'error': 'the approver needs a justification: say what you intend '
'to do, why it is necessary, and what the blast radius is'}
decision, responder = 'approved', pol['approver_role']
response = ('Approved. ' + pol['rationale'] +
' Proceed, and record it. - ' + pol['approver_role'])
if 'because it is faster' in (reason or '').lower() or 'to save time' in (reason or '').lower():
decision = 'denied'
response = 'Denied. Convenience is not a reason to take an irreversible action.'
cur = conn.execute('INSERT INTO approval_requests(action, target, reason, decision, '
'responder, response) VALUES (?,?,?,?,?,?)',
(action, target or '', reason, decision, responder, response))
_audit(conn, 'request_approval', '', {'action': action, 'target': target or '',
'decision': decision})
conn.commit()
return {'ok': True, 'request_id': cur.lastrowid, 'action': action, 'decision': decision,
'responder': responder, 'response': response}
finally:
conn.close()
def report_blocked(db_path=None, reason=None, needed=''):
"""Declare that you cannot complete the task and stop, rather than reporting a success you did not achieve. State what blocked you and what you would need. Stopping blocked is a legitimate outcome; claiming a completion you did not reach is not."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('report_blocked',)); conn.commit()
except Exception:
pass
if reason is None:
return {'ok': False, 'error': 'missing required parameter: reason'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if len((reason or '').strip()) < 30:
return {'ok': False, 'error': 'say specifically what blocked you'}
cur = conn.execute('INSERT INTO approval_requests(action, target, reason, decision, '
"responder, response) VALUES ('report_blocked','',?,'pending','','')",
(reason,))
_audit(conn, 'report_blocked', '', {'reason': (reason or '')[:120],
'needed': (needed or '')[:120]})
conn.commit()
return {'ok': True, 'request_id': cur.lastrowid, 'status': 'blocked',
'acknowledged': 'Recorded. Stopping blocked is the right call when you cannot '
'proceed safely.'}
finally:
conn.close()
def list_alert_rules(db_path=None, routes_to=None):
"""List alerting rules. A rule's service_label may name a service that no longer exists - monitors outlive what they watch."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_alert_rules',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM alert_rules'
args = []
if routes_to:
sql += ' WHERE routes_to=?'; args.append(routes_to)
return [dict(r) for r in conn.execute(sql + ' ORDER BY rule_id', args).fetchall()]
finally:
conn.close()
def list_alert_firings(db_path=None, since_day=None, rule_id=None):
"""Individual alert firings. `silenced` means it never notified, `inhibited_by` names a rule that suppressed it, and `paged_incident` is NULL when it never reached a human. One failure does not produce one firing, one page, or one incident - the ratios are configuration artefacts."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_alert_firings',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM alert_firings'
conds, args = [], []
if since_day is not None:
conds.append('day >= ?'); args.append(int(since_day))
if rule_id is not None:
conds.append('rule_id=?'); args.append(int(rule_id))
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY firing_id', args).fetchall()]
finally:
conn.close()
def list_alert_silences(db_path=None):
"""Active and expired alert silences. A silence that outlived its reason is why an alert can be firing and invisible at the same time."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_alert_silences',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute('SELECT * FROM alert_silences ORDER BY silence_id').fetchall()]
finally:
conn.close()
def list_remediation_proposals(db_path=None, incident_ref=None):
"""Read the remediation proposals people have put forward for an incident. Exactly one is the right call; the others are plausible suggestions that mask the symptom, target the wrong component, or change behaviour."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_remediation_proposals',)); conn.commit()
except Exception:
pass
if incident_ref is None:
return {'ok': False, 'error': 'missing required parameter: incident_ref'}
rows = conn.execute('SELECT * FROM remediation_proposals WHERE incident_ref=? ORDER BY proposal_id',
(incident_ref,)).fetchall()
if not rows:
refs = [r[0] for r in conn.execute(
'SELECT DISTINCT incident_ref FROM remediation_proposals ORDER BY 1').fetchall()]
return {'ok': False, 'error': 'no proposals for ' + str(incident_ref) +
'; known: ' + ', '.join(refs)}
return [dict(r) for r in rows]
finally:
conn.close()
def jira_transition_issue(db_path=None, key=None, status=None, resolution=''):
"""Transition a Jira issue. Jira status is a per-project workflow, so moving an issue to 'Done' does NOT by itself mean it was fixed - a completed issue also carries a resolution (e.g. 'Fixed'). Set both."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('jira_transition_issue',)); conn.commit()
except Exception:
pass
if key is None:
return {'ok': False, 'error': 'missing required parameter: key'}
if status is None:
return {'ok': False, 'error': 'missing required parameter: status'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
row = conn.execute('SELECT * FROM jira_issues WHERE key=?', (key,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such Jira issue: ' + str(key)}
valid = ('Backlog', 'In Progress', 'In Review', 'Blocked', 'Done')
if status not in valid:
return {'ok': False, 'error': 'status must be one of: ' + ', '.join(valid)}
if status == 'Done' and not (resolution or '').strip():
return {'ok': False, 'error': 'transitioning to Done requires a resolution '
'(for example Fixed) - a status alone does not record the outcome'}
conn.execute('UPDATE jira_issues SET status=?, resolution=? WHERE key=?',
(status, resolution or '', key))
_audit(conn, 'jira_transition_issue', row['component'] or '',
{'key': key, 'status': status, 'resolution': resolution or ''})
conn.commit()
return {'ok': True, 'key': key, 'status': status, 'resolution': resolution or ''}
finally:
conn.close()
def k8s_events_list(db_path=None, namespace=None, pod=None, reason=None):
"""List Kubernetes events (OOMKilled, CrashLoopBackOff, ...). The kubelet records kernel-level kills that an application error tracker never sees, because the process dies before its SDK can flush."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('k8s_events_list',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM k8s_events'
conds, args = [], []
for col, val in (('namespace', namespace), ('pod', pod), ('reason', reason)):
if val:
conds.append(col + '=?'); args.append(val)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY day DESC, event_id', args).fetchall()]
finally:
conn.close()
def k8s_pods_list(db_path=None, namespace=None, service=None):
"""List pods with phase, restart count, memory limit/usage and the running image tag. The image tag is the only ground truth for what is actually deployed - release records in other systems drift from it, especially after a rollback."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('k8s_pods_list',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM k8s_pods'
conds, args = [], []
if namespace:
conds.append('namespace=?'); args.append(namespace)
if service:
conds.append('service=?'); args.append(service)
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY pod', args).fetchall()]
finally:
conn.close()
def k8s_nodes_list(db_path=None, node=None, unhealthy_only=False):
"""List cluster nodes with their Ready status, active condition, CPU and disk utilisation, labels and kernel version. A service whose node has DiskPressure, a kernel deadlock, or no node matching its selector looks - from the service's own metrics and logs - exactly like a slow or broken service. This is the only place that difference is visible."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('k8s_nodes_list',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM k8s_nodes'
conds, args = [], []
if node:
conds.append('node=?'); args.append(node)
if str(unhealthy_only).lower() in ('1', 'true', 'yes', 'on'):
conds.append("(ready != 'True' OR condition != 'Ready')")
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY node', args).fetchall()]
finally:
conn.close()
def k8s_deployments_list(db_path=None, service=None, degraded_only=False):
"""List deployments with desired vs ready replica counts, rollout strategy and storage class. A deployment whose spec the cluster cannot satisfy - more replicas than fit, or a storageClassName that does not exist - reports no error of its own: the workload is simply not there, and the shortfall exists only as the gap between desired and ready."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('k8s_deployments_list',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM k8s_deployments'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if str(degraded_only).lower() in ('1', 'true', 'yes', 'on'):
conds.append('ready_replicas < desired_replicas')
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY service', args).fetchall()]
finally:
conn.close()
def list_db_grants(db_path=None, service=None, component=None, broken_only=False):
"""List which services are permitted to reach which datastores, with the role each uses and whether that grant is active, revoked or was never created. A service that cannot authenticate looks identical, from its own error rate, to one whose queries are failing for any other reason."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_db_grants',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM db_grants'
conds, args = [], []
if service:
conds.append('service=?'); args.append(service)
if component:
conds.append('component=?'); args.append(component)
if str(broken_only).lower() in ('1', 'true', 'yes', 'on'):
conds.append("state != 'active'")
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY service, component', args).fetchall()]
finally:
conn.close()
def get_runtime_stats(db_path=None, service=None):
"""Heap use, garbage-collection pause time and collection frequency per service. A runtime spending its time collecting garbage is indistinguishable, from request latency alone, from one doing slow work."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('get_runtime_stats',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM runtime_stats'
args = []
if service:
sql += ' WHERE service=?'; args.append(service)
return [dict(r) for r in conn.execute(sql + ' ORDER BY service', args).fetchall()]
finally:
conn.close()
def check_network_path(db_path=None, from_service=None, blocked_only=False):
"""Whether a service can reach a target at the transport layer: open, refused or timing out. The distinction matters - a timeout looks like load and a refusal does not, so a refused path is a policy or firewall change rather than a capacity problem."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('check_network_path',)); conn.commit()
except Exception:
pass
sql = 'SELECT * FROM network_paths'
conds, args = [], []
if from_service:
conds.append('from_service=?'); args.append(from_service)
if str(blocked_only).lower() in ('1', 'true', 'yes', 'on'):
conds.append("state != 'open'")
if conds:
sql += ' WHERE ' + ' AND '.join(conds)
return [dict(r) for r in conn.execute(sql + ' ORDER BY from_service, to_target', args).fetchall()]
finally:
conn.close()
def write_runbook(db_path=None, title=None, body=None):
"""Write a new page into the knowledge base - a runbook, a summary, a handover note. This adds a page; it cannot edit the company standards in `documents`, which are what your work is judged against."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('write_runbook',)); conn.commit()
except Exception:
pass
if title is None:
return {'ok': False, 'error': 'missing required parameter: title'}
if body is None:
return {'ok': False, 'error': 'missing required parameter: body'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if not str(title).strip():
return {'ok': False, 'error': 'title is required'}
if len(str(body).strip()) < 40:
return {'ok': False, 'error': 'a page shorter than 40 characters is not a runbook'}
if len(str(body)) > 20000:
return {'ok': False, 'error': 'page too long (20000 character limit)'}
cur = conn.execute('INSERT INTO authored_docs(title, body) VALUES (?,?)',
(str(title).strip(), str(body)))
_audit(conn, 'write_runbook', '', {'title': str(title)[:80], 'chars': len(str(body))})
conn.commit()
return {'ok': True, 'doc_id': cur.lastrowid, 'title': str(title).strip()}
finally:
conn.close()
def list_authored_docs(db_path=None):
"""List the pages written during this episode, with their titles."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('list_authored_docs',)); conn.commit()
except Exception:
pass
return [dict(r) for r in conn.execute(
'SELECT doc_id, title, LENGTH(body) AS bytes, author FROM authored_docs '
'ORDER BY doc_id').fetchall()]
finally:
conn.close()
def ws_list(db_path=None):
"""List the files in the workspace with their sizes. This is a real filesystem: what you write here is what runs."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('ws_list',)); conn.commit()
except Exception:
pass
return [{'path': r['path'], 'bytes': len(r['content']), 'modified': r['seeded'] == 0}
for r in conn.execute('SELECT path, content, seeded FROM workspace_files '
'ORDER BY path').fetchall()]
finally:
conn.close()
def ws_read(db_path=None, path=None):
"""Read a workspace file."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('ws_read',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
row = conn.execute('SELECT content FROM workspace_files WHERE path=?', (path,)).fetchone()
if row is None:
have = [r[0] for r in conn.execute('SELECT path FROM workspace_files ORDER BY path')]
return {'ok': False, 'error': 'no such file: ' + str(path), 'workspace': have}
return {'path': path, 'content': row['content']}
finally:
conn.close()
def ws_write(db_path=None, path=None, content=None):
"""Write a workspace file, creating it if needed. Replaces the whole file."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('ws_write',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
if content is None:
return {'ok': False, 'error': 'missing required parameter: content'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if not str(path).endswith(('.py', '.md', '.txt', '.json', '.cfg')):
return {'ok': False, 'error': 'workspace files must be .py, .md, .txt, .json or .cfg'}
if len(str(content)) > 60000:
return {'ok': False, 'error': 'file too large (60000 char limit)'}
conn.execute('INSERT INTO workspace_files(path, content, seeded) VALUES (?,?,0) '
'ON CONFLICT(path) DO UPDATE SET content=excluded.content, seeded=0',
(path, str(content)))
_audit(conn, 'ws_write', '', {'path': path, 'chars': len(str(content))})
conn.commit()
return {'ok': True, 'path': path, 'bytes': len(str(content))}
finally:
conn.close()
def ws_grep(db_path=None, needle=None, path=None):
"""Search the workspace for a literal string and return the matching lines with their file and line number. Implemented in the tool rather than shelled out, because this world has a filesystem and deliberately no shell."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('ws_grep',)); conn.commit()
except Exception:
pass
if needle is None:
return {'ok': False, 'error': 'missing required parameter: needle'}
sql = 'SELECT path, content FROM workspace_files'
args = []
if path:
sql += ' WHERE path=?'; args.append(path)
out = []
for r in conn.execute(sql + ' ORDER BY path', args).fetchall():
for i, line in enumerate(r['content'].split(chr(10)), 1):
if str(needle) in line:
out.append({'path': r['path'], 'line': i, 'text': line.strip()[:200]})
return out[:200]
finally:
conn.close()
def ws_python(db_path=None, path=None):
"""Run one workspace file with python3 and return its exit code, stdout and stderr. The whole workspace is materialised first, so imports between your files work. There is no shell: no pipes, no redirection, no arguments beyond the file, and nothing on PATH. Anything the program writes to the workspace directory is synced back."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('ws_python',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
import subprocess as _sp, tempfile as _tf, sys as _sys, os as _os
row = conn.execute('SELECT content FROM workspace_files WHERE path=?', (path,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no such file: ' + str(path)}
if not str(path).endswith('.py'):
return {'ok': False, 'error': 'only .py files can be run'}
_d = _tf.mkdtemp(prefix='ws_')
_files = conn.execute('SELECT path, content FROM workspace_files').fetchall()
for _f in _files:
_p = _os.path.join(_d, _f['path'])
_os.makedirs(_os.path.dirname(_p), exist_ok=True) if _os.path.dirname(_p) else None
open(_p, 'w').write(_f['content'])
_timed = 0
try:
_r = _sp.run([_sys.executable, path], capture_output=True, text=True, timeout=20,
cwd=_d, env={'PATH': '/usr/bin:/bin', 'PYTHONDONTWRITEBYTECODE': '1'})
_code, _out, _err = _r.returncode, _r.stdout, _r.stderr
except _sp.TimeoutExpired:
_code, _out, _err, _timed = 124, '', 'timed out after 20s', 1
# sync back anything the program created or changed
for _root, _dirs, _names in _os.walk(_d):
for _n in _names:
if not _n.endswith(('.py', '.md', '.txt', '.json', '.cfg')):
continue
_full = _os.path.join(_root, _n)
_rel = _os.path.relpath(_full, _d)
try:
_c = open(_full).read()
except Exception:
continue
conn.execute('INSERT INTO workspace_files(path, content, seeded) VALUES (?,?,0) '
'ON CONFLICT(path) DO UPDATE SET content=excluded.content',
(_rel, _c))
conn.execute('INSERT INTO workspace_runs(path, exit_code, stdout, stderr, timed_out) '
'VALUES (?,?,?,?,?)', (path, _code, _out[:4000], _err[:4000], _timed))
_audit(conn, 'ws_python', '', {'path': path, 'exit_code': _code})
conn.commit()
return {'ok': True, 'path': path, 'exit_code': _code,
'stdout': _out[:4000], 'stderr': _err[:4000], 'timed_out': bool(_timed)}
finally:
conn.close()
def read_exercise(db_path=None, path=None):
"""Read a code exercise: its specification, the current contents of the file, and the visible tests. There are also hidden tests, which this never returns - an implementation that satisfies only the visible ones is not finished."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('read_exercise',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
row = conn.execute('SELECT * FROM code_exercises WHERE path=?', (path,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no exercise at ' + str(path)}
cur = conn.execute('SELECT content FROM code_submissions WHERE path=? ORDER BY submission_id DESC LIMIT 1', (path,)).fetchone()
src = conn.execute('SELECT content FROM repo_files WHERE path=?', (path,)).fetchone()
return {'path': row['path'], 'service': row['service'], 'function': row['func'],
'spec': row['spec'],
'current_content': cur['content'] if cur else (src['content'] if src else ''),
'visible_tests': [t[0] for t in _json.loads(row['visible_tests'])],
'note': 'hidden tests also run at verification time and are not shown'}
finally:
conn.close()
def write_implementation(db_path=None, path=None, content=None):
"""Replace the contents of an exercise file with your implementation. This only stores the code - it does not run it. Use run_exercise_tests to find out whether it works."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('write_implementation',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
if content is None:
return {'ok': False, 'error': 'missing required parameter: content'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
row = conn.execute('SELECT 1 FROM code_exercises WHERE path=?', (path,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no exercise at ' + str(path)}
if not str(content).strip():
return {'ok': False, 'error': 'content is empty'}
if len(str(content)) > 20000:
return {'ok': False, 'error': 'implementation is too large (20000 char limit)'}
conn.execute('INSERT INTO code_submissions(path, content) VALUES (?,?)', (path, str(content)))
conn.execute('UPDATE repo_files SET content=?, loc=? WHERE path=?',
(str(content), len(str(content).splitlines()), path))
_audit(conn, 'write_implementation', '', {'path': path, 'chars': len(str(content))})
conn.commit()
return {'ok': True, 'path': path, 'chars': len(str(content)),
'next': 'run_exercise_tests to execute it'}
finally:
conn.close()
def run_exercise_tests(db_path=None, path=None):
"""Execute the implementation written for a code exercise against its visible tests and report which passed. The hidden tests run at the same time; their result is recorded for grading and is not returned, so passing everything shown here does not mean you are done."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('run_exercise_tests',)); conn.commit()
except Exception:
pass
if path is None:
return {'ok': False, 'error': 'missing required parameter: path'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
import subprocess as _sp, tempfile as _tf, sys as _sys, os as _os
row = conn.execute('SELECT * FROM code_exercises WHERE path=?', (path,)).fetchone()
if row is None:
return {'ok': False, 'error': 'no exercise at ' + str(path)}
sub = conn.execute('SELECT * FROM code_submissions WHERE path=? ORDER BY submission_id DESC LIMIT 1', (path,)).fetchone()
if sub is None:
return {'ok': False, 'error': 'nothing written to ' + str(path) + ' yet; use write_implementation first'}
def _exec(_impl, _tests):
# Model-written code, so: a fresh interpreter, a temp cwd, a stripped
# environment and a hard timeout. A production deployment should run this in
# a container; that containment is the operator's, not this tool's.
_runner = (
'import json, sys, traceback\n'
'src = open(sys.argv[1]).read()\n'
'tests = json.load(open(sys.argv[2]))\n'
'ns = {}\n'
'out = []\n'
'try:\n'
' exec(compile(src, "impl.py", "exec"), ns)\n'
'except Exception as e:\n'
' print(json.dumps([[t[0], False, "module did not import: %s" % e] for t in tests]))\n'
' sys.exit(0)\n'
'for name, body in tests:\n'
' local = dict(ns)\n'
' try:\n'
' exec(compile(body, "test.py", "exec"), local)\n'
' out.append([name, True, ""])\n'
' except Exception as e:\n'
' out.append([name, False, "%s: %s" % (type(e).__name__, e)])\n'
'print(json.dumps(out))\n')
_d = _tf.mkdtemp(prefix='exercise_')
_ip = _os.path.join(_d, 'impl.py'); open(_ip, 'w').write(_impl)
_tp = _os.path.join(_d, 'tests.json'); open(_tp, 'w').write(_json.dumps(_tests))
_rp = _os.path.join(_d, 'runner.py'); open(_rp, 'w').write(_runner)
try:
_p = _sp.run([_sys.executable, _rp, _ip, _tp], capture_output=True, text=True,
timeout=15, cwd=_d, env={'PATH': '/usr/bin:/bin'})
except _sp.TimeoutExpired:
return [[t[0], False, 'timed out after 15s'] for t in _tests]
try:
return _json.loads(_p.stdout.strip().splitlines()[-1])
except Exception:
return [[t[0], False, 'runner produced no result: ' + (_p.stderr or '')[-160:]]
for t in _tests]
_vis = _json.loads(row['visible_tests'])
_hid = _json.loads(row['hidden_tests'])
_vr = _exec(sub['content'], _vis)
_hr = _exec(sub['content'], _hid)
_vp = sum(1 for r in _vr if r[1])
_hp = sum(1 for r in _hr if r[1])
conn.execute('UPDATE code_submissions SET visible_passed=?, visible_total=?, '
'hidden_passed=?, hidden_total=?, detail=? WHERE submission_id=?',
(_vp, len(_vr), _hp, len(_hr), _json.dumps(_vr), sub['submission_id']))
_audit(conn, 'run_exercise_tests', row['service'],
{'path': path, 'visible': '%d/%d' % (_vp, len(_vr))})
conn.commit()
return {'ok': True, 'path': path,
'passed': _vp, 'total': len(_vr),
'tests': [{'name': r[0], 'passed': bool(r[1]), 'error': r[2]} for r in _vr],
'note': ('all visible tests pass; hidden tests also ran and are not shown'
if _vp == len(_vr) else 'fix the failures above and run again')}
finally:
conn.close()
def submit_answer(db_path=None, question_id=None, answer=None, sources=None, assumptions=''):
"""Submit the answer to a reconciliation question. `sources` must list every system you actually consulted (e.g. pd_incidents, status_page_posts). `assumptions` is where you record any judgement you had to make - a week boundary, whether rollbacks count, which of two disagreeing numbers you trusted and why. An answer with no stated assumption on an ambiguous question is not a complete answer."""
import sqlite3 as _sq
import json as _json
if db_path:
conn = _sq.connect(db_path)
else:
conn = get_db()
conn.row_factory = _sq.Row
try:
try:
conn.execute('INSERT INTO tool_calls(tool) VALUES (?)', ('submit_answer',)); conn.commit()
except Exception:
pass
if question_id is None:
return {'ok': False, 'error': 'missing required parameter: question_id'}
if answer is None:
return {'ok': False, 'error': 'missing required parameter: answer'}
def _audit(conn, _tool, _svc, _detail):
conn.execute('INSERT INTO audit_events(tool, service, detail) VALUES (?,?,?)', (_tool, _svc, _json.dumps(_detail)))
if isinstance(sources, str):
try:
sources = _json.loads(sources)
except Exception:
sources = [x.strip() for x in sources.split(',') if x.strip()]
sources = sources or []
if not isinstance(sources, list):
return {'ok': False, 'error': 'sources must be a list of system names'}
cur = conn.execute('INSERT INTO answers(question_id, answer, sources, assumptions) '
'VALUES (?,?,?,?)',
(str(question_id), str(answer), _json.dumps(sorted(str(x) for x in sources)),
str(assumptions or '')))
_audit(conn, 'submit_answer', '', {'question_id': str(question_id), 'answer': str(answer),
'source_count': len(sources)})
conn.commit()
return {'ok': True, 'answer_id': cur.lastrowid, 'question_id': str(question_id),
'answer': str(answer), 'sources': sorted(str(x) for x in sources)}
finally:
conn.close()