Source cache¶
cache ¶
Persist AutoCorrect2 source fingerprints and extracted hotstrings.
The cache is an optimization only. SHA-256 of the exact source bytes is the authoritative content identity; filesystem size and modification time are stored as useful metadata but are never trusted as proof that a same-sized source is unchanged.
Cached hotstrings persist only source-derived state: canonical AHK trigger spelling and canonical option text. Semantic trigger text and the case-insensitive semantic comparison key are reconstructed by the domain model on every load so they always reflect the current trigger-normalization implementation.
LOGGER
module-attribute
¶
Module logger used for cache diagnostics.
CACHE_SCHEMA_VERSION
module-attribute
¶
Persistent-cache compatibility version, including parser semantics.
DEFAULT_SOURCE_CACHE_PATH
module-attribute
¶
DEFAULT_SOURCE_CACHE_PATH: Final[Path] = (
PROJECT_ROOT
/ ".cache"
/ "autocorrect2-source-cache.json"
)
Deterministic project-local path used for the persistent source cache.
CachedHotstring
dataclass
¶
Represent the minimal persisted form of one extracted hotstring.
Attributes:
| Name | Type | Description |
|---|---|---|
trigger |
str
|
Canonical AHK source-form trigger text. |
options |
str
|
Canonical hotstring option declaration. |
Source code in hotstring\autocorrect2\source_loading\cache.py
41 42 43 44 45 46 47 48 49 50 51 52 53 | |
SourceCacheEntry
dataclass
¶
Represent cached state for one AutoCorrect2 source file.
Attributes:
| Name | Type | Description |
|---|---|---|
modification_time_ns |
int
|
Source modification time recorded when the entry was refreshed. |
size |
int
|
Source size recorded when the entry was refreshed. |
sha256 |
str
|
SHA-256 digest of the exact source bytes. |
hotstrings |
tuple[CachedHotstring, ...]
|
Extracted hotstrings stored in declaration order. |
Source code in hotstring\autocorrect2\source_loading\cache.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
SourceCache
dataclass
¶
Represent the complete persistent AutoCorrect2 source cache.
Attributes:
| Name | Type | Description |
|---|---|---|
project_dir |
str
|
Canonical AutoCorrect2 project directory associated with the cache. This prevents accidental reuse for a different checkout at another path; machine identity is deliberately not part of cache validity. |
files |
dict[str, SourceCacheEntry]
|
Cache entries keyed by source path relative to the AutoCorrect2 project root. |
Source code in hotstring\autocorrect2\source_loading\cache.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
canonical_project_dir ¶
Return the canonical project-directory value stored in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_dir
|
Path
|
AutoCorrect2 project directory. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Absolute normalized project-directory string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the supplied project directory is not a path. |
OSError
|
If path resolution fails. |
Source code in hotstring\autocorrect2\source_loading\cache.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
source_cache_key ¶
Return the stable JSON key for a configured source path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Source path relative to the AutoCorrect2 project root. |
required |
Returns:
| Type | Description |
|---|---|
str
|
POSIX-style relative path suitable for use as a JSON object key. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the supplied source is not a path. |
Source code in hotstring\autocorrect2\source_loading\cache.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
compute_content_hash ¶
Compute the SHA-256 digest used as authoritative content identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
bytes
|
Exact source-file bytes. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Lowercase hexadecimal SHA-256 digest. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the supplied content is not bytes. |
Source code in hotstring\autocorrect2\source_loading\cache.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
create_source_cache_entry ¶
create_source_cache_entry(
hotstrings: Sequence[ExistingHotstring],
*,
modification_time_ns: int,
size: int,
sha256: str,
) -> SourceCacheEntry
Create a cache entry from parsed hotstrings and source state.
Cached triggers use each model's canonical ahk_trigger field, not its
semantic trigger. Restoring the entry therefore feeds exactly the input
representation expected by ExistingHotstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hotstrings
|
Sequence[ExistingHotstring]
|
Parsed hotstrings in declaration order. |
required |
modification_time_ns
|
int
|
Current source modification time. |
required |
size
|
int
|
Current source size. |
required |
sha256
|
str
|
SHA-256 digest of the exact source bytes. |
required |
Returns:
| Type | Description |
|---|---|
SourceCacheEntry
|
Cache entry ready for persistence. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If source metadata or a hotstring has an invalid type. |
ValueError
|
If source metadata or the digest is invalid. |
Source code in hotstring\autocorrect2\source_loading\cache.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
restore_hotstrings ¶
restore_hotstrings(
entry: SourceCacheEntry, *, source: Path
) -> list[ExistingHotstring]
Reconstruct domain hotstrings from one persisted cache entry.
The cached trigger is intentionally fed back to ExistingHotstring as
AHK source-form constructor input. Its semantic trigger, canonical source
representation, parsed options, and case-insensitive semantic key are therefore
rebuilt using the current code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
SourceCacheEntry
|
Cached source entry to reconstruct. |
required |
source
|
Path
|
Source identifier assigned to each reconstructed hotstring. |
required |
Returns:
| Type | Description |
|---|---|
list[ExistingHotstring]
|
Existing hotstrings in their cached declaration order. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the cache entry or source identifier has an invalid type. |
ValueError
|
If cached hotstring data is invalid under the current model. |
Source code in hotstring\autocorrect2\source_loading\cache.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
load_source_cache ¶
load_source_cache(
cache_path: Path, *, project_dir: Path
) -> SourceCache
Load the persistent source cache or return an empty compatible cache.
Missing, unreadable, malformed, incompatible, or project-mismatched cache files are treated as disposable optimization state. In those cases an empty cache associated with the requested AutoCorrect2 project is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path
|
JSON cache file to load. |
required |
project_dir
|
Path
|
AutoCorrect2 project directory expected by the caller. |
required |
Returns:
| Type | Description |
|---|---|
SourceCache
|
Loaded compatible cache, or a new empty cache when persisted state |
SourceCache
|
cannot be reused. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a path argument has an invalid type. |
OSError
|
If canonicalizing the project directory fails. |
Source code in hotstring\autocorrect2\source_loading\cache.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
save_source_cache ¶
save_source_cache(
cache: SourceCache, cache_path: Path
) -> None
Persist the source cache atomically as UTF-8 JSON.
Each cached hotstring contains exactly trigger and options. The trigger
value is the canonical AHK source representation. Source paths are stored
once as keys in the surrounding files object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
SourceCache
|
Source cache to persist. |
required |
cache_path
|
Path
|
Destination JSON file. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an argument has an invalid type. |
OSError
|
If the cache directory or cache file cannot be written or replaced. |
Source code in hotstring\autocorrect2\source_loading\cache.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
_parse_source_cache_entry ¶
_parse_source_cache_entry(
raw_entry: object,
) -> SourceCacheEntry | None
Parse one untrusted JSON cache entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_entry
|
object
|
Decoded JSON value representing a source entry. |
required |
Returns:
| Type | Description |
|---|---|
SourceCacheEntry | None
|
Validated cache entry, or |
Source code in hotstring\autocorrect2\source_loading\cache.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
_parse_cached_hotstring ¶
_parse_cached_hotstring(
raw_hotstring: object,
) -> CachedHotstring | None
Parse one minimal hotstring record from decoded JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_hotstring
|
object
|
Decoded JSON value representing one cached hotstring. |
required |
Returns:
| Type | Description |
|---|---|
CachedHotstring | None
|
Parsed cached hotstring, or |
Source code in hotstring\autocorrect2\source_loading\cache.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
_serialize_source_cache ¶
_serialize_source_cache(
cache: SourceCache,
) -> dict[str, object]
Convert the in-memory source cache to JSON-compatible values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
SourceCache
|
Source cache to serialize. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
JSON-compatible cache document. |
Source code in hotstring\autocorrect2\source_loading\cache.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
_validate_metadata ¶
Validate metadata used to construct a source-cache entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modification_time_ns
|
int
|
Source modification time. |
required |
size
|
int
|
Source size. |
required |
sha256
|
str
|
Source SHA-256 digest. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a metadata value has an invalid type. |
ValueError
|
If a metadata value is outside its valid domain. |
Source code in hotstring\autocorrect2\source_loading\cache.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
_metadata_is_valid ¶
Return whether decoded source metadata has the expected shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modification_time_ns
|
object
|
Decoded modification-time value. |
required |
size
|
object
|
Decoded file-size value. |
required |
sha256
|
object
|
Decoded digest value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether all metadata values are valid. |
Source code in hotstring\autocorrect2\source_loading\cache.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
_sha256_is_valid ¶
Return whether a string is a lowercase-or-uppercase SHA-256 hex digest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Candidate digest string. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the value contains exactly 64 hexadecimal characters. |
Source code in hotstring\autocorrect2\source_loading\cache.py
577 578 579 580 581 582 583 584 585 586 587 | |