Skip to content
47 changes: 43 additions & 4 deletions packages/core/src/ripgrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ const decodeField = (value: unknown): { text: string; raw?: Buffer } | undefined
* than silently switching these records to a different unit: without it, a line beginning with one
* bad byte reports `needle` at [3,9) of a string where [3,9) reads "edle t".
*/
const normalizeMatch = (json: object): unknown => {
const normalizeMatch = (json: object, onSourceLine?: (text: string) => void): unknown => {
const data = readProp(json, "data")
if (!data || typeof data !== "object") return json
const lines = decodeField(readProp(data, "lines"))
if (!lines) return json
onSourceLine?.(lines.text)
const raw = lines.raw
// Submatch `start`/`end` are BYTE offsets into the RAW line, so a lossy decode invalidates them:
// every undecodable byte widens to a 3-byte U+FFFD. They are rebased onto the decoded text's own
Expand Down Expand Up @@ -256,8 +257,32 @@ export interface GrepInput {
readonly filterPath?: (relativePath: string) => boolean
readonly scanLimit?: number
// altimate_change end
// altimate_change start — supply source context separately from the capped public match
readonly sourceContext?: {
readonly lookaround: number
readonly onMatch: (line: GrepSourceLine) => void
}
}
// altimate_change end

// altimate_change start — retain only enough source to render the visible window and adjacent lines
export interface GrepSourceLine {
readonly head: string
readonly tail?: string
readonly visibleEnd: number
}

function retainSourceLine(text: string, lookaround: number): GrepSourceLine {
const visibleEnd = Math.min(text.length, LINE_TEXT_CAP)
if (text.length <= LINE_TEXT_CAP + 2 * lookaround) return { head: text, visibleEnd }
return {
head: text.slice(0, LINE_TEXT_CAP + lookaround),
tail: text.slice(-lookaround),
visibleEnd,
}
}
// altimate_change end

export interface Interface {
readonly find: (input: FindInput) => Effect.Effect<readonly Entry[], Error>
readonly glob: (input: GlobInput) => Effect.Effect<readonly Entry[], Error>
Expand Down Expand Up @@ -432,7 +457,7 @@ export const layer = Layer.effect(
grep: (input) =>
Effect.suspend(() => {
const skipped: { count: number; samples: string[] } = { count: 0, samples: [] }
return run<RawMatchData>({
return run<RawMatchData & { sourceLine?: GrepSourceLine }>({
...input,
filter: input.filterPath ? (match) => input.filterPath!(match.path.text) : undefined,
args: [
Expand Down Expand Up @@ -485,15 +510,28 @@ export const layer = Layer.effect(
// `normalizeMatch` is plain synchronous code. A throw there would be a DEFECT,
// which `Effect.catch` deliberately does not catch — so it would abort the
// stream rather than skip one record, defeating the point of this change.
const context = input.sourceContext
let sourceLine: GrepSourceLine | undefined
const normalized = yield* Effect.try({
try: () => normalizeMatch(json),
try: () =>
normalizeMatch(
json,
context &&
((text) => {
sourceLine = retainSourceLine(text, context.lookaround)
}),
),
catch: (cause) => failure("record could not be normalized", cause),
})
const match = yield* Schema.decodeUnknownEffect(RawMatch)(normalized).pipe(
Effect.mapError((cause) => failure("unexpected match shape", cause)),
)
// `normalizeMatch` already caps submatches and line text, so nothing is re-trimmed.
return { ...match.data, path: { text: match.data.path.text.replace(/^\.[\\/]/, "") } }
return {
...match.data,
path: { text: match.data.path.text.replace(/^\.[\\/]/, "") },
sourceLine,
}
}).pipe(
Effect.catch((cause) =>
Effect.sync(() => {
Expand Down Expand Up @@ -524,6 +562,7 @@ export const layer = Layer.effect(
// altimate_change end
Effect.map((result) =>
result.items.map((match) => {
if (match.sourceLine) input.sourceContext?.onMatch(match.sourceLine)
const relative = match.path.text
.replace(/^(?:\.[\\/])+/u, "")
.replace(/^[\\/]+/u, "")
Expand Down
48 changes: 45 additions & 3 deletions packages/core/test/ripgrep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ describe("Ripgrep", () => {
)
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "c-plain.txt"), "needle here\n"))

const matches = yield* (yield* Ripgrep.Service).grep({ cwd: tmp.path, pattern: "needle", limit: 10 })
const source: Ripgrep.GrepSourceLine[] = []
const matches = yield* (yield* Ripgrep.Service).grep({
cwd: tmp.path,
pattern: "needle",
limit: 10,
sourceContext: { lookaround: 64, onMatch: (line) => source.push(line) },
})
expect(source).toEqual(matches.map((match) => ({ head: match.text, visibleEnd: match.text.length })))

// The non-UTF8 file is reported like any other rather than dropped or fatal.
expect(matches.map((item) => item.entry.path).sort()).toEqual([
Expand Down Expand Up @@ -196,7 +203,7 @@ describe("Ripgrep", () => {
// risk is a Windows-only spawn/quoting regression going unnoticed rather than a parsing one.
const stubTest = bunTest.skipIf(process.platform === "win32")

const grepWithStubbedRecords = (records: string[]) =>
const grepWithStubbedRecords = (records: string[], options: Partial<Ripgrep.GrepInput> = {}) =>
Effect.acquireUseRelease(
Effect.promise(() => tmpdir()),
(tmp) =>
Expand All @@ -209,7 +216,7 @@ describe("Ripgrep", () => {

return yield* Effect.gen(function* () {
const rg = yield* Ripgrep.Service
return yield* rg.grep({ cwd: tmp.path, pattern: "needle", limit: 100 })
return yield* rg.grep({ cwd: tmp.path, pattern: "needle", limit: 100, ...options })
}).pipe(
Effect.provide(
Ripgrep.layer.pipe(
Expand All @@ -232,6 +239,41 @@ describe("Ripgrep", () => {
/** The single aggregate warning for the last search, or undefined when nothing was skipped. */
const lastSkip = () => skipWarnings.at(-1)

stubTest("keeps source context aligned with retained matches after rejected and filtered records", async () => {
const source: Ripgrep.GrepSourceLine[] = []
const longLine = "needle".padEnd(1_996, "x") + "fixture-overlapping-credential\n"
const decodedLine = "needle \ufffd tail\n"
const matches = await Effect.runPromise(
grepWithStubbedRecords(
[
matchRecord("a.txt", { lines: { text: longLine } }),
// No source callback is possible, but these cannot pass RawMatch either.
matchRecord("bad-lines.txt", { lines: { text: 42 } }),
matchRecord("bad-bytes.txt", { lines: { bytes: "!!!" } }),
// Normalization captures source, but schema rejection must not publish it.
matchRecord("bad-line-number.txt", { line_number: 0 }),
matchRecord("filtered.txt"),
matchRecord("b.txt", { lines: { bytes: Buffer.from("needle \xff tail\n", "binary").toString("base64") } }),
matchRecord("past-limit.txt"),
],
{
limit: 2,
filterPath: (relative) => !relative.endsWith("filtered.txt"),
sourceContext: { lookaround: 64, onMatch: (line) => source.push(line) },
},
),
)

expect(matches.map((item) => item.entry.path)).toEqual([RelativePath.make("a.txt"), RelativePath.make("b.txt")])
expect(source).toEqual([
{ head: longLine, visibleEnd: 2_000 },
{ head: decodedLine, visibleEnd: decodedLine.length },
])
expect(matches[0].text).toBe(longLine.slice(0, 2_000) + "...")
expect(matches[1].text).toBe(decodedLine)
expect(lastSkip()?.skipped).toBe(3)
})

stubTest("skips an unparseable record without failing the search", async () => {
const matches = await Effect.runPromise(
grepWithStubbedRecords([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
import { Log } from "@/altimate/util/log"
import type { ConnectionConfig } from "@altimateai/drivers"

// altimate_change start — credentials this process has already read or written
const knownCredentials = new Map<string, { field: string; value: string }>()

export function knownStoredCredentials(): Iterable<{ field: string; value: string }> {
return knownCredentials.values()
}
// altimate_change end

const SERVICE_NAME = "altimate-code"

const SENSITIVE_FIELDS = new Set([
Expand Down Expand Up @@ -104,6 +112,7 @@ async function getBackend(): Promise<SecretBackend | null> {
/** Swap the credential backend in tests. `undefined` re-probes on next use. */
export function setSecretBackendForTests(next: SecretBackend | null | undefined): void {
backend = next
knownCredentials.clear() // altimate_change — reset observations when replacing the backend
}

/** Store a single credential in the OS credential store (or return false if unavailable). */
Expand All @@ -116,6 +125,8 @@ export async function storeCredential(
if (!store) return false
try {
await store.set(`${connectionName}/${field}`, value)
// altimate_change — retain the current credential for exact tool-result redaction
knownCredentials.set(`${connectionName}/${field}`, { field, value })
return true
} catch (e) {
// e.g. no libsecret on Linux, or a locked keychain: report "not stored" so
Expand All @@ -135,7 +146,13 @@ export async function getCredential(
const store = await getBackend()
if (!store) return null
try {
return await store.get(`${connectionName}/${field}`)
// altimate_change start — remember only the latest value for this account
const account = `${connectionName}/${field}`
const value = await store.get(account)
if (value === null) knownCredentials.delete(account)
else knownCredentials.set(account, { field, value })
return value
// altimate_change end
} catch (e) {
// Treated as absent so a locked store cannot stop every connect, but logged, so a connection that then fails
// for want of its password shows why.
Expand All @@ -152,7 +169,12 @@ export async function deleteCredential(
const store = await getBackend()
if (!store) return false
try {
return await store.delete(`${connectionName}/${field}`)
// altimate_change start — forget removed credentials
const account = `${connectionName}/${field}`
const deleted = await store.delete(account)
if (deleted) knownCredentials.delete(account)
return deleted
// altimate_change end
} catch {
return false
}
Expand Down Expand Up @@ -187,7 +209,12 @@ export async function forgetCredentials(name: string, keep?: ConnectionConfig):
} catch {
present = "unknown"
}
if (present === false) continue
// altimate_change start — discard stale observed values
if (present === false) {
knownCredentials.delete(account)
continue
}
// altimate_change end
let deleted: boolean
try {
deleted = await store.delete(account)
Expand All @@ -196,6 +223,7 @@ export async function forgetCredentials(name: string, keep?: ConnectionConfig):
}
// A delete that reports "nothing to delete" after an unreadable entry cannot be told from a refusal.
if (!deleted) notRemoved.push(field)
else knownCredentials.delete(account) // altimate_change — remove the observation too
}
return notRemoved
}
Expand Down
42 changes: 40 additions & 2 deletions packages/opencode/src/altimate/native/connections/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,17 @@ function resolveStorePaths(
// Loading
// ---------------------------------------------------------------------------

function loadFromFile(filePath: string): Record<string, ConnectionConfig> {
// altimate_change — observational reads leave registry diagnostics to warehouse loading
function loadFromFile(filePath: string, quiet = false): Record<string, ConnectionConfig> {
try {
if (!fs.existsSync(filePath)) return {}
const raw = fs.readFileSync(filePath, "utf-8")
const parsed = JSON.parse(raw)
if (typeof parsed !== "object" || parsed === null) return {}
return parsed as Record<string, ConnectionConfig>
} catch (e) {
Log.Default.warn(`Failed to load connections from ${filePath}: ${e}`)
// altimate_change — leave diagnostics to an explicit warehouse load
if (!quiet) Log.Default.warn(`Failed to load connections from ${filePath}: ${e}`)
return {}
}
}
Expand Down Expand Up @@ -1068,6 +1070,41 @@ export async function reload(): Promise<void> {
// altimate_change end
}

// altimate_change start — observe current sources without initializing or updating the registry
type FileSource = "global" | "local"
const observedFiles = new Map<
FileSource,
{ file: string; version: string; configs: Record<string, ConnectionConfig> }
>()

function observeFile(file: string, source: FileSource): Record<string, ConnectionConfig> {
try {
const stat = fs.statSync(file, { bigint: true, throwIfNoEntry: false })
if (stat?.isFile()) {
const version = [stat.dev, stat.ino, stat.size, stat.mtimeNs, stat.ctimeNs].join(":")
const previous = observedFiles.get(source)
if (previous?.file === file && previous.version === version) return previous.configs
const configs = loadFromFile(file, true)
observedFiles.set(source, { file, version, configs })
return configs
}
} catch {
// Connections files are optional, and a concurrent edit can remove one.
}
observedFiles.delete(source)
return {}
}

export function* knownCredentialConfigs(projectDir = projectRoot()): Iterable<ConnectionConfig> {
const dir = path.resolve(projectDir)
const active = injectionActive ? injectedState : states.get(dir)
if (active?.loaded) yield* active.configs.values()
// A file can be read by tools even when another source shadows its connection name.
yield* Object.values(observeFile(globalConfigPath(), "global"))
yield* Object.values(observeFile(localConfigPathFor(dir), "local"))
}
// altimate_change end

/** Get the raw config for a connection (for testing). */
export function getConfig(name: string): ConnectionConfig | undefined {
// altimate_change start — the current project's registry
Expand All @@ -1084,6 +1121,7 @@ export function reset(): void {
states.clear()
injectedState = undefined
injectionActive = false
observedFiles.clear()
// altimate_change end
censusSent = false
}
Expand Down
Loading
Loading