Files
kefu/deploy/model-usage-20260917/patch_meter_write_budget.py
T
2026-09-21 10:34:06 +08:00

42 lines
2.0 KiB
Python

from pathlib import Path
backend = Path(r'C:\kefu\wechat_rpa\admin_backend.py')
source = backend.read_text(encoding='utf-8')
anchor = ''' with self.connect() as db:
db.execute("BEGIN")
before = db.total_changes
'''
replacement = ''' with self.connect() as db:
# Metering must not hold up an otherwise completed model reply.
# Keep the normal 10s busy timeout for all other application writes.
db.execute("PRAGMA busy_timeout = 250")
db.execute("BEGIN")
before = db.total_changes
'''
assert source.count(anchor) == 1
backend.write_text(source.replace(anchor, replacement, 1), encoding='utf-8', newline='')
tests = Path(r'C:\kefu\wechat_rpa\test_model_usage_ledger.py')
source = tests.read_text(encoding='utf-8').replace('import tempfile\n', 'import tempfile\nimport time\n', 1)
anchor = ' def test_invalid_metadata_and_noninteger_tokens_are_rejected(self):'
addition = ''' def test_usage_write_lock_has_short_budget_without_changing_other_connections(self):
record = event()
with self.db.connect() as locker:
self.assertEqual(locker.execute('PRAGMA busy_timeout').fetchone()[0], 10000)
locker.execute('BEGIN IMMEDIATE')
started = time.monotonic()
with self.assertRaises(sqlite3.OperationalError) as raised:
self.db.record_model_usage_batch([record])
elapsed = time.monotonic() - started
self.assertIn('locked', str(raised.exception))
self.assertGreaterEqual(elapsed, .15)
self.assertLess(elapsed, 1.5, 'metering must not inherit the normal 10s lock wait')
self.assertTrue(self.db.record_model_usage(record))
with self.db.connect() as other:
self.assertEqual(other.execute('PRAGMA busy_timeout').fetchone()[0], 10000)
'''
assert source.count(anchor) == 1
tests.write_text(source.replace(anchor, addition + anchor, 1), encoding='utf-8', newline='')
print('usage transaction busy timeout limited to 250ms; real-lock regression added')