Skip to content
Myr

history.ts

This file

Path

packages/project/src/code/history.ts

139 lines · 3.7 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge build/1003oscabrielbefore adoptione8561dc

line 1why, one step back
1/**

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

lines 2–6why, one step back
2 * Code history kept by immutable ids. Keys:
3 * `<repo>/<kind>/v<version>/<commit>/<scan>/<sha256(path)>`; `commit` is never4 * `main` or a tag (those resolve first), the path is hashed to keep a key5 * under the store's 1,024 bytes, and only a canonical repo is ever keyed.6 * Values hold ids and ranges, never a file's text or a landing.

Merge build/1003oscabrielbefore adoptione8561dc

lines 7–81why, one step back
7 */
8import { WhyRange } from "@repo/protocol/domain/code";9import { BlobSha, CommitSha } from "@repo/protocol/ids";10import type { RepoName, RepoPath } from "@repo/protocol/ids";11import { Effect, Option, Schema } from "effect";1213import { CodeStore } from "../code-store.ts";14import { CodeCache } from "./cache.ts";1516export const WHY_VERSION = 1;1718export const LOG_VERSION = 1;1920export const INCREMENT_REACH = 16;2122export const StoredWhy = Schema.Struct({23 blob: BlobSha,24 ranges: Schema.Array(WhyRange),25 shas: Schema.Array(CommitSha),26 stoppedAt: Schema.optionalKey(CommitSha),27});2829export type StoredWhy = typeof StoredWhy.Type;3031export const StoredPathLog = Schema.Struct({32 reachedRoot: Schema.Boolean,33 shas: Schema.Array(CommitSha),34});3536export type StoredPathLog = typeof StoredPathLog.Type;3738const sha256 = (text: string) =>39 Effect.map(40 Effect.promise(() =>41 crypto.subtle.digest("SHA-256", new TextEncoder().encode(text))42 ),43 (digest) =>44 [...new Uint8Array(digest)]45 .map((byte) => byte.toString(16).padStart(2, "0"))46 .join("")47 );4849export interface Kept<A> {50 readonly kind: string;51 readonly version: number;52 readonly schema: Schema.Codec<A, unknown>;53}5455export const whyKept: Kept<StoredWhy> = {56 kind: "why",57 schema: StoredWhy,58 version: WHY_VERSION,59};6061export const pathLogKept: Kept<StoredPathLog> = {62 kind: "path-log",63 schema: StoredPathLog,64 version: LOG_VERSION,65};6667export const keyOf = <A>(68 kept: Kept<A>,69 at: {70 readonly repo: RepoName;71 readonly commit: CommitSha;72 readonly scan: number;73 readonly path: RepoPath;74 }75) =>76 Effect.map(77 sha256(at.path),78 (hash) =>79 `${at.repo}/${kept.kind}/v${kept.version}/${at.commit}/${at.scan}/${hash}`80 );81

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 82why, one step back
82/** A miss is never cached, so a value another isolate keeps later is found. */

Merge build/1003oscabrielbefore adoptione8561dc

lines 83–118why, one step back
83export const recall = <A>(
84 kept: Kept<A>,85 key: string86): Effect.Effect<Option.Option<A>> =>87 Effect.gen(function* () {88 const cache = yield* CodeCache;89 const store = yield* CodeStore;9091 const decode = Schema.decodeUnknownOption(92 Schema.fromJsonString(kept.schema)93 );9495 return yield* cache.through(96 key,97 Effect.map(store.get(key), Option.flatMap(decode)),98 { heavy: true, keep: Option.isSome }99 );100 });101102export const remember = <A>(103 kept: Kept<A>,104 key: string,105 value: A106): Effect.Effect<void> =>107 Effect.gen(function* () {108 const cache = yield* CodeCache;109 const store = yield* CodeStore;110111 const encoded = yield* Schema.encodeEffect(112 Schema.fromJsonString(kept.schema)113 )(value).pipe(Effect.orDie);114115 yield* store.put(key, encoded);116 yield* cache.through(key, Effect.succeedSome(value), { heavy: true });117 });118

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 119–127why, one step back
119/** A fork's reads keep nothing: the store is keyed under a canonical repo alone. */
120export const keptOnlyIfCanonical = <A, E, R>(121 at: { readonly canonical: boolean },122 read: Effect.Effect<A, E, R>123): Effect.Effect<A, E, R> =>124 at.canonical125 ? read126 : Effect.provideService(read, CodeStore, CodeStore.defaultValue());127

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 128why, one step back
128/** A failure is never kept. */

Merge build/1003oscabrielbefore adoptione8561dc

lines 129–139why, one step back
129export const throughHistory = <A, E, R>(
130 kept: Kept<A>,131 key: string,132 compute: Effect.Effect<A, E, R>133): Effect.Effect<A, E, R> =>134 Effect.flatMap(recall(kept, key), (found) =>135 Option.match(found, {136 onNone: () => Effect.tap(compute, (value) => remember(kept, key, value)),137 onSome: Effect.succeed,138 })139 );