@namzu/sdk 74.0.0 → 75.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +65 -0
- package/dist/bridge/acp/server.d.ts +2 -0
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +9 -1
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/config/runtime.d.ts +3 -3
- package/dist/connector/mcp/adapter.d.ts +1 -1
- package/dist/connector/mcp/adapter.d.ts.map +1 -1
- package/dist/connector/mcp/adapter.js +3 -2
- package/dist/connector/mcp/adapter.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +4 -25
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +32 -32
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/manager/session/turn-recorder.d.ts +1 -0
- package/dist/manager/session/turn-recorder.d.ts.map +1 -1
- package/dist/manager/session/turn-recorder.js +31 -7
- package/dist/manager/session/turn-recorder.js.map +1 -1
- package/dist/pricing/catalogue.generated.d.ts.map +1 -1
- package/dist/pricing/catalogue.generated.js +176 -21
- package/dist/pricing/catalogue.generated.js.map +1 -1
- package/dist/provider/errors.d.ts.map +1 -1
- package/dist/provider/errors.js +13 -18
- package/dist/provider/errors.js.map +1 -1
- package/dist/provider/stream-request-timeout.d.ts +9 -0
- package/dist/provider/stream-request-timeout.d.ts.map +1 -0
- package/dist/provider/stream-request-timeout.js +34 -0
- package/dist/provider/stream-request-timeout.js.map +1 -0
- package/dist/public-runtime.d.ts +2 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +2 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +1 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +1 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +3 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +15 -3
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/file-evidence-replay.d.ts +1 -0
- package/dist/runtime/query/file-evidence-replay.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-replay.js +64 -21
- package/dist/runtime/query/file-evidence-replay.js.map +1 -1
- package/dist/runtime/query/file-evidence-seed.d.ts +7 -0
- package/dist/runtime/query/file-evidence-seed.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-seed.js +12 -1
- package/dist/runtime/query/file-evidence-seed.js.map +1 -1
- package/dist/runtime/query/guard.d.ts.map +1 -1
- package/dist/runtime/query/guard.js +12 -2
- package/dist/runtime/query/guard.js.map +1 -1
- package/dist/runtime/query/index.d.ts +2 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +6 -2
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +3 -0
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +32 -11
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/advisory.js +2 -2
- package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +4 -3
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
- package/dist/runtime/query/prepare-turn.js +1 -1
- package/dist/runtime/query/prepare-turn.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +2 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +1 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/sandbox/file-walk-program.js +1 -1
- package/dist/sandbox/ignore-matcher.d.ts +2 -0
- package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
- package/dist/sandbox/ignore-matcher.js +8 -2
- package/dist/sandbox/ignore-matcher.js.map +1 -1
- package/dist/sandbox/ignore-source.d.ts.map +1 -1
- package/dist/sandbox/ignore-source.js +51 -7
- package/dist/sandbox/ignore-source.js.map +1 -1
- package/dist/sandbox/limit-error.d.ts +7 -0
- package/dist/sandbox/limit-error.d.ts.map +1 -0
- package/dist/sandbox/limit-error.js +13 -0
- package/dist/sandbox/limit-error.js.map +1 -0
- package/dist/sandbox/provider/local.d.ts +1 -0
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +95 -31
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/schedules/cron.js +1 -1
- package/dist/schedules/cron.js.map +1 -1
- package/dist/schedules/describe.js +1 -1
- package/dist/schedules/describe.js.map +1 -1
- package/dist/schedules/next-fire.d.ts +2 -2
- package/dist/schedules/next-fire.d.ts.map +1 -1
- package/dist/schedules/next-fire.js +7 -4
- package/dist/schedules/next-fire.js.map +1 -1
- package/dist/schedules/spec.d.ts.map +1 -1
- package/dist/schedules/spec.js +23 -7
- package/dist/schedules/spec.js.map +1 -1
- package/dist/schedules/types.d.ts +2 -0
- package/dist/schedules/types.d.ts.map +1 -1
- package/dist/store/budget/ledger.d.ts +15 -1
- package/dist/store/budget/ledger.d.ts.map +1 -1
- package/dist/store/budget/ledger.js +80 -1
- package/dist/store/budget/ledger.js.map +1 -1
- package/dist/store/evidence/source-text.d.ts +1 -1
- package/dist/store/session-index/sqlite.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.js +7 -3
- package/dist/store/session-index/sqlite.js.map +1 -1
- package/dist/store/session-log/core.d.ts.map +1 -1
- package/dist/store/session-log/core.js +88 -24
- package/dist/store/session-log/core.js.map +1 -1
- package/dist/store/session-log/disk.js +1 -1
- package/dist/store/session-log/disk.js.map +1 -1
- package/dist/tools/builtins/atomic-write-file.d.ts +2 -0
- package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
- package/dist/tools/builtins/atomic-write-file.js +37 -8
- package/dist/tools/builtins/atomic-write-file.js.map +1 -1
- package/dist/tools/builtins/code-analysis.d.ts.map +1 -1
- package/dist/tools/builtins/code-analysis.js +4 -1
- package/dist/tools/builtins/code-analysis.js.map +1 -1
- package/dist/tools/builtins/edit-apply.d.ts.map +1 -1
- package/dist/tools/builtins/edit-apply.js +24 -10
- package/dist/tools/builtins/edit-apply.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +31 -15
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/file-text.d.ts +3 -0
- package/dist/tools/builtins/file-text.d.ts.map +1 -0
- package/dist/tools/builtins/file-text.js +14 -0
- package/dist/tools/builtins/file-text.js.map +1 -0
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +7 -2
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +8 -5
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/ls.d.ts.map +1 -1
- package/dist/tools/builtins/ls.js +2 -1
- package/dist/tools/builtins/ls.js.map +1 -1
- package/dist/tools/builtins/notebook-edit.d.ts.map +1 -1
- package/dist/tools/builtins/notebook-edit.js +19 -10
- package/dist/tools/builtins/notebook-edit.js.map +1 -1
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +61 -29
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-regular-file.d.ts +5 -0
- package/dist/tools/builtins/read-regular-file.d.ts.map +1 -0
- package/dist/tools/builtins/read-regular-file.js +23 -0
- package/dist/tools/builtins/read-regular-file.js.map +1 -0
- package/dist/tools/builtins/search-support.d.ts +2 -0
- package/dist/tools/builtins/search-support.d.ts.map +1 -1
- package/dist/tools/builtins/search-support.js +14 -0
- package/dist/tools/builtins/search-support.js.map +1 -1
- package/dist/tools/builtins/stale-before-review.d.ts.map +1 -1
- package/dist/tools/builtins/stale-before-review.js +8 -5
- package/dist/tools/builtins/stale-before-review.js.map +1 -1
- package/dist/tools/builtins/write-file.d.ts.map +1 -1
- package/dist/tools/builtins/write-file.js +17 -13
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/defineTool.d.ts.map +1 -1
- package/dist/tools/defineTool.js +4 -3
- package/dist/tools/defineTool.js.map +1 -1
- package/dist/tools/paths.d.ts +9 -0
- package/dist/tools/paths.d.ts.map +1 -1
- package/dist/tools/paths.js +39 -9
- package/dist/tools/paths.js.map +1 -1
- package/dist/tools/posix-path.d.ts +2 -14
- package/dist/tools/posix-path.d.ts.map +1 -1
- package/dist/tools/posix-path.js +7 -0
- package/dist/tools/posix-path.js.map +1 -1
- package/dist/tools/tool-is-available.d.ts +4 -0
- package/dist/tools/tool-is-available.d.ts.map +1 -0
- package/dist/tools/tool-is-available.js +8 -0
- package/dist/tools/tool-is-available.js.map +1 -0
- package/dist/types/code-analysis/index.d.ts +1 -1
- package/dist/types/code-analysis/index.d.ts.map +1 -1
- package/dist/types/common/index.d.ts +6 -0
- package/dist/types/common/index.d.ts.map +1 -1
- package/dist/types/common/index.js +13 -0
- package/dist/types/common/index.js.map +1 -1
- package/dist/types/provider/errors.d.ts.map +1 -1
- package/dist/types/provider/errors.js +3 -5
- package/dist/types/provider/errors.js.map +1 -1
- package/dist/types/sandbox/index.d.ts +13 -2
- package/dist/types/sandbox/index.d.ts.map +1 -1
- package/dist/types/sandbox/index.js.map +1 -1
- package/dist/types/session/checkpoint.d.ts +4 -0
- package/dist/types/session/checkpoint.d.ts.map +1 -1
- package/dist/types/session/checkpoint.js +2 -0
- package/dist/types/session/checkpoint.js.map +1 -1
- package/dist/types/session/events.d.ts +1 -0
- package/dist/types/session/events.d.ts.map +1 -1
- package/dist/types/session/events.js.map +1 -1
- package/dist/types/session/records.d.ts +42 -6
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +1 -0
- package/dist/types/session/records.js.map +1 -1
- package/dist/types/tool/index.d.ts +2 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/utils/cost.d.ts +17 -0
- package/dist/utils/cost.d.ts.map +1 -1
- package/dist/utils/cost.js +78 -8
- package/dist/utils/cost.js.map +1 -1
- package/package.json +1 -1
- package/src/bridge/acp/server.ts +16 -7
- package/src/connector/mcp/adapter.ts +4 -3
- package/src/connector/mcp/client.ts +33 -32
- package/src/index.ts +2 -0
- package/src/manager/session/turn-recorder.ts +31 -7
- package/src/pricing/catalogue.generated.ts +176 -21
- package/src/pricing/rates.source.json +108 -33
- package/src/provider/errors.ts +12 -19
- package/src/provider/stream-request-timeout.ts +34 -0
- package/src/public-runtime.ts +2 -0
- package/src/public-tools.ts +1 -0
- package/src/runtime/query/executor.ts +29 -3
- package/src/runtime/query/file-evidence-replay.ts +73 -19
- package/src/runtime/query/file-evidence-seed.ts +16 -0
- package/src/runtime/query/guard.ts +13 -2
- package/src/runtime/query/index.ts +10 -1
- package/src/runtime/query/iteration/index.ts +32 -11
- package/src/runtime/query/iteration/phases/advisory.ts +2 -2
- package/src/runtime/query/path-escalation.ts +4 -3
- package/src/runtime/query/prepare-turn.ts +2 -1
- package/src/runtime/query/tooling.ts +3 -0
- package/src/sandbox/file-walk-program.ts +1 -1
- package/src/sandbox/ignore-matcher.ts +12 -3
- package/src/sandbox/ignore-source.ts +51 -7
- package/src/sandbox/limit-error.ts +10 -0
- package/src/sandbox/provider/local.ts +107 -48
- package/src/schedules/cron.ts +1 -1
- package/src/schedules/describe.ts +1 -1
- package/src/schedules/next-fire.ts +7 -4
- package/src/schedules/spec.ts +36 -7
- package/src/schedules/types.ts +2 -0
- package/src/store/budget/ledger.ts +93 -2
- package/src/store/session-index/sqlite.ts +8 -4
- package/src/store/session-log/core.ts +96 -25
- package/src/store/session-log/disk.ts +1 -1
- package/src/tools/builtins/atomic-write-file.ts +49 -7
- package/src/tools/builtins/code-analysis.ts +6 -1
- package/src/tools/builtins/edit-apply.ts +25 -10
- package/src/tools/builtins/edit.ts +31 -15
- package/src/tools/builtins/file-text.ts +14 -0
- package/src/tools/builtins/glob.ts +6 -1
- package/src/tools/builtins/grep.ts +7 -5
- package/src/tools/builtins/ls.ts +2 -1
- package/src/tools/builtins/notebook-edit.ts +18 -11
- package/src/tools/builtins/read-file.ts +71 -24
- package/src/tools/builtins/read-regular-file.ts +25 -0
- package/src/tools/builtins/search-support.ts +13 -0
- package/src/tools/builtins/stale-before-review.ts +8 -6
- package/src/tools/builtins/write-file.ts +18 -12
- package/src/tools/defineTool.ts +4 -3
- package/src/tools/paths.ts +49 -7
- package/src/tools/posix-path.ts +9 -0
- package/src/tools/tool-is-available.ts +11 -0
- package/src/types/code-analysis/index.ts +1 -1
- package/src/types/common/index.ts +24 -0
- package/src/types/provider/errors.ts +3 -5
- package/src/types/sandbox/index.ts +12 -1
- package/src/types/session/checkpoint.ts +2 -0
- package/src/types/session/events.ts +1 -0
- package/src/types/session/records.ts +1 -0
- package/src/types/tool/index.ts +2 -0
- package/src/utils/cost.ts +101 -8
|
@@ -440,8 +440,9 @@ export class SessionLogCore implements SessionLog {
|
|
|
440
440
|
/** Records accepted into the chain that the medium has not taken yet, oldest first. */
|
|
441
441
|
#pending: { readonly bytes: Buffer; readonly sync: boolean }[] = []
|
|
442
442
|
#pendingBytes = 0
|
|
443
|
-
/**
|
|
444
|
-
#
|
|
443
|
+
/** The verified chain on the medium while the in-memory chain is ahead. */
|
|
444
|
+
#durableChain: SessionLogChain | undefined
|
|
445
|
+
#unrecordedDrops = 0
|
|
445
446
|
#degradedCause: SessionLogDegradedCause | undefined
|
|
446
447
|
#droppedRecords = 0
|
|
447
448
|
#droppedNoticeSent = false
|
|
@@ -512,7 +513,7 @@ export class SessionLogCore implements SessionLog {
|
|
|
512
513
|
*/
|
|
513
514
|
async #catchUp(): Promise<void> {
|
|
514
515
|
// Records waiting for the disk are ahead of it; the lease keeps others out.
|
|
515
|
-
if (this.#
|
|
516
|
+
if (this.#durableChain !== undefined) return
|
|
516
517
|
const size = await this.#medium.size()
|
|
517
518
|
if (size === this.#synced + this.#torn && size >= this.#synced) return
|
|
518
519
|
if (size < this.#synced) this.#reset()
|
|
@@ -831,7 +832,11 @@ export class SessionLogCore implements SessionLog {
|
|
|
831
832
|
}
|
|
832
833
|
const bytes = Buffer.from(line, 'utf8')
|
|
833
834
|
const sync = this.#sync === 'all' || (this.#sync === 'boundaries' && BOUNDARY_TYPES.has(type))
|
|
834
|
-
await this.#persist(bytes, sync)
|
|
835
|
+
const recovered = await this.#persist(bytes, sync)
|
|
836
|
+
if (recovered !== undefined) {
|
|
837
|
+
this.#turns.apply(recovered.record)
|
|
838
|
+
return recovered
|
|
839
|
+
}
|
|
835
840
|
const entry = this.#chain.accept(bytes, this.#synced)
|
|
836
841
|
this.#turns.apply(entry.record)
|
|
837
842
|
this.#synced += bytes.byteLength
|
|
@@ -856,15 +861,19 @@ export class SessionLogCore implements SessionLog {
|
|
|
856
861
|
* buffer and goes out, in order, with the next record that can be written.
|
|
857
862
|
* Any other error is thrown as before. Caller holds the mutex.
|
|
858
863
|
*/
|
|
859
|
-
async #persist(bytes: Buffer, sync: boolean): Promise<
|
|
860
|
-
if (this.#
|
|
864
|
+
async #persist(bytes: Buffer, sync: boolean): Promise<SessionLogEntry | undefined> {
|
|
865
|
+
if (this.#durableChain === undefined) {
|
|
861
866
|
try {
|
|
862
867
|
await this.#medium.append(bytes, this.#synced, sync)
|
|
863
|
-
return
|
|
868
|
+
return undefined
|
|
864
869
|
} catch (error) {
|
|
865
870
|
const cause = sessionLogDegradedCause(error)
|
|
866
871
|
if (cause === undefined) throw error
|
|
867
|
-
this.#
|
|
872
|
+
this.#durableChain = new SessionLogChain({
|
|
873
|
+
head: this.#chain.head,
|
|
874
|
+
sessionId: this.sessionId,
|
|
875
|
+
gen: this.#chain.gen,
|
|
876
|
+
})
|
|
868
877
|
this.#degradedCause = cause
|
|
869
878
|
this.#bufferRecord(bytes, sync)
|
|
870
879
|
this.#notify({
|
|
@@ -873,12 +882,12 @@ export class SessionLogCore implements SessionLog {
|
|
|
873
882
|
bufferedRecords: this.#pending.length,
|
|
874
883
|
bufferedBytes: this.#pendingBytes,
|
|
875
884
|
})
|
|
876
|
-
return
|
|
885
|
+
return undefined
|
|
877
886
|
}
|
|
878
887
|
}
|
|
879
888
|
const queued = this.#bufferRecord(bytes, sync)
|
|
880
889
|
try {
|
|
881
|
-
await this.#flushPending()
|
|
890
|
+
return await this.#flushPending()
|
|
882
891
|
} catch (error) {
|
|
883
892
|
// Only the volume refusing writes keeps the session going; anything else
|
|
884
893
|
// fails this record, which must then not stay queued.
|
|
@@ -890,6 +899,7 @@ export class SessionLogCore implements SessionLog {
|
|
|
890
899
|
}
|
|
891
900
|
throw error
|
|
892
901
|
}
|
|
902
|
+
return undefined
|
|
893
903
|
}
|
|
894
904
|
}
|
|
895
905
|
|
|
@@ -899,11 +909,14 @@ export class SessionLogCore implements SessionLog {
|
|
|
899
909
|
this.#pendingBytes += bytes.byteLength
|
|
900
910
|
let dropped = false
|
|
901
911
|
// The newest record always stays, whatever the bound.
|
|
902
|
-
|
|
903
|
-
|
|
912
|
+
// The first record is required even if the volume refused the session start.
|
|
913
|
+
const keepStart = this.#durableChain?.head === null ? 1 : 0
|
|
914
|
+
while (this.#pendingBytes > this.#degradedBufferBytes && this.#pending.length > keepStart + 1) {
|
|
915
|
+
const [oldest] = this.#pending.splice(keepStart, 1)
|
|
904
916
|
if (oldest === undefined) break
|
|
905
917
|
this.#pendingBytes -= oldest.bytes.byteLength
|
|
906
918
|
this.#droppedRecords += 1
|
|
919
|
+
this.#unrecordedDrops += 1
|
|
907
920
|
dropped = true
|
|
908
921
|
}
|
|
909
922
|
if (dropped && !this.#droppedNoticeSent && this.#degradedCause !== undefined) {
|
|
@@ -919,20 +932,56 @@ export class SessionLogCore implements SessionLog {
|
|
|
919
932
|
return queued
|
|
920
933
|
}
|
|
921
934
|
|
|
922
|
-
/** Write the buffer in order
|
|
923
|
-
async #flushPending(): Promise<
|
|
924
|
-
|
|
935
|
+
/** Write the buffer in order, leaving a valid prefix even if a later write fails. */
|
|
936
|
+
async #flushPending(): Promise<SessionLogEntry | undefined> {
|
|
937
|
+
const chain = this.#durableChain
|
|
938
|
+
if (chain === undefined) return
|
|
925
939
|
const size = await this.#medium.size()
|
|
926
|
-
if (size >
|
|
927
|
-
|
|
940
|
+
if (size > chain.end) await this.#medium.truncate(chain.end, size)
|
|
941
|
+
let last: SessionLogEntry | undefined
|
|
942
|
+
for (;;) {
|
|
928
943
|
const next = this.#pending[0]
|
|
929
944
|
if (next === undefined) break
|
|
930
|
-
|
|
931
|
-
|
|
945
|
+
// The pending bytes were already validated when the record was accepted.
|
|
946
|
+
const record = JSON.parse(next.bytes.toString('utf8')) as SessionRecord
|
|
947
|
+
if (this.#unrecordedDrops > 0 && chain.head !== null) {
|
|
948
|
+
const gap = {
|
|
949
|
+
v: SESSION_RECORD_SCHEMA_VERSION,
|
|
950
|
+
id: generateRecordId(),
|
|
951
|
+
sessionId: this.sessionId,
|
|
952
|
+
ts: record.ts,
|
|
953
|
+
gen: record.gen,
|
|
954
|
+
type: 'log_gap',
|
|
955
|
+
ignorable: true,
|
|
956
|
+
seq: chain.head.seq + 1,
|
|
957
|
+
prev: chain.head,
|
|
958
|
+
droppedRecords: this.#unrecordedDrops,
|
|
959
|
+
firstDroppedSeq: record.seq - this.#unrecordedDrops,
|
|
960
|
+
lastDroppedSeq: record.seq - 1,
|
|
961
|
+
}
|
|
962
|
+
const bytes = Buffer.from(`${JSON.stringify(gap)}\n`, 'utf8')
|
|
963
|
+
await this.#appendPendingLine(chain, bytes, this.#sync !== 'none')
|
|
964
|
+
this.#unrecordedDrops = 0
|
|
965
|
+
}
|
|
966
|
+
const head = chain.head
|
|
967
|
+
let bytes = next.bytes
|
|
968
|
+
if (record.seq !== (head?.seq ?? 0) + 1 || record.prev?.sha256 !== head?.sha256) {
|
|
969
|
+
// Skip links into the provisional chain are no longer valid.
|
|
970
|
+
const { prevText: _prevText, ...payload } = record
|
|
971
|
+
bytes = Buffer.from(
|
|
972
|
+
formatSessionLogLine({ ...payload, seq: (head?.seq ?? 0) + 1, prev: head }),
|
|
973
|
+
'utf8',
|
|
974
|
+
)
|
|
975
|
+
}
|
|
976
|
+
last = await this.#appendPendingLine(chain, bytes, next.sync)
|
|
932
977
|
this.#pendingBytes -= next.bytes.byteLength
|
|
933
|
-
this.#pending.shift()
|
|
934
978
|
this.#flushedWhileDegraded += 1
|
|
979
|
+
// Drop each durable record immediately so a retry starts after it.
|
|
980
|
+
this.#pending.shift()
|
|
935
981
|
}
|
|
982
|
+
this.#chain = chain
|
|
983
|
+
this.#synced = chain.end
|
|
984
|
+
this.#durableChain = undefined
|
|
936
985
|
const flushed = this.#flushedWhileDegraded
|
|
937
986
|
this.#flushedWhileDegraded = 0
|
|
938
987
|
const dropped = this.#droppedRecords
|
|
@@ -941,6 +990,19 @@ export class SessionLogCore implements SessionLog {
|
|
|
941
990
|
this.#droppedNoticeSent = false
|
|
942
991
|
this.#pendingBytes = 0
|
|
943
992
|
this.#notify({ state: 'recovered', flushedRecords: flushed, droppedRecords: dropped })
|
|
993
|
+
return last
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
async #appendPendingLine(
|
|
997
|
+
chain: SessionLogChain,
|
|
998
|
+
bytes: Buffer,
|
|
999
|
+
sync: boolean,
|
|
1000
|
+
): Promise<SessionLogEntry> {
|
|
1001
|
+
const offset = chain.end
|
|
1002
|
+
await this.#medium.append(bytes, offset, sync)
|
|
1003
|
+
const entry = chain.accept(bytes, offset)
|
|
1004
|
+
if (this.#ledgerKey !== undefined) verifiedPrefixOf(this.#ledgerKey)?.append(entry)
|
|
1005
|
+
return entry
|
|
944
1006
|
}
|
|
945
1007
|
|
|
946
1008
|
/**
|
|
@@ -1037,10 +1099,10 @@ export class SessionLogCore implements SessionLog {
|
|
|
1037
1099
|
}
|
|
1038
1100
|
const content = candidate.content as Message & { toolCallId?: unknown }
|
|
1039
1101
|
const key =
|
|
1040
|
-
|
|
1041
|
-
?
|
|
1042
|
-
:
|
|
1043
|
-
?
|
|
1102
|
+
type === 'message_replaced' || type === 'message_edited'
|
|
1103
|
+
? `record:${String(candidate.id)}`
|
|
1104
|
+
: content.role === 'tool' && typeof content.toolCallId === 'string'
|
|
1105
|
+
? content.toolCallId
|
|
1044
1106
|
: `message:${String(candidate.messageId)}`
|
|
1045
1107
|
const ref = await this.#spillOrLose(key, 'message', JSON.stringify(content))
|
|
1046
1108
|
const text =
|
|
@@ -1272,7 +1334,16 @@ export async function* walkSessionLog(
|
|
|
1272
1334
|
}
|
|
1273
1335
|
|
|
1274
1336
|
const splitter = new LineSplitter(start)
|
|
1275
|
-
|
|
1337
|
+
// A resumed walk starts past its verified cursor.
|
|
1338
|
+
const cursor = chain.head
|
|
1339
|
+
const anchor = options.expectHead
|
|
1340
|
+
let anchorSeen =
|
|
1341
|
+
cursor !== null &&
|
|
1342
|
+
anchor !== undefined &&
|
|
1343
|
+
cursor.seq === anchor.seq &&
|
|
1344
|
+
cursor.offset === anchor.offset &&
|
|
1345
|
+
cursor.length === anchor.length &&
|
|
1346
|
+
cursor.sha256 === anchor.sha256
|
|
1276
1347
|
let stopped = false
|
|
1277
1348
|
outer: for await (const chunk of medium.stream(start)) {
|
|
1278
1349
|
let lines: Generator<{ line: Uint8Array; offset: number }>
|
|
@@ -120,7 +120,7 @@ export class DiskLogMedium implements LogMedium {
|
|
|
120
120
|
|
|
121
121
|
async append(bytes: Uint8Array, expectedOffset: number, sync: boolean): Promise<void> {
|
|
122
122
|
await mkdir(dirname(this.file), { recursive: true, mode: 0o700 })
|
|
123
|
-
const handle = await open(this.file, 'a', 0o600)
|
|
123
|
+
const handle = await open(this.file, 'a+', 0o600)
|
|
124
124
|
try {
|
|
125
125
|
const before = (await handle.stat()).size
|
|
126
126
|
if (before !== expectedOffset) {
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto'
|
|
2
|
-
import { lstatSync, realpathSync } from 'node:fs'
|
|
3
|
-
import {
|
|
2
|
+
import { constants, lstatSync, realpathSync } from 'node:fs'
|
|
3
|
+
import {
|
|
4
|
+
type FileHandle,
|
|
5
|
+
access,
|
|
6
|
+
mkdir,
|
|
7
|
+
open,
|
|
8
|
+
realpath,
|
|
9
|
+
rename,
|
|
10
|
+
stat,
|
|
11
|
+
unlink,
|
|
12
|
+
} from 'node:fs/promises'
|
|
4
13
|
import nodePath, { basename, dirname, join, sep } from 'node:path'
|
|
5
14
|
|
|
6
15
|
/** A write refused because its path runs through a symbolic link or out of its root. */
|
|
@@ -71,6 +80,25 @@ function assertNoLinkBelow(anchor: string, file: string): void {
|
|
|
71
80
|
}
|
|
72
81
|
}
|
|
73
82
|
|
|
83
|
+
async function writableMode(filePath: string): Promise<number | undefined> {
|
|
84
|
+
const existing = await stat(filePath).catch((error: NodeJS.ErrnoException) => {
|
|
85
|
+
if (error.code !== 'ENOENT') throw error
|
|
86
|
+
return undefined
|
|
87
|
+
})
|
|
88
|
+
if (existing === undefined) return undefined
|
|
89
|
+
|
|
90
|
+
const mode = existing.mode & 0o7777
|
|
91
|
+
// A privileged process can pass W_OK even when every write bit is clear.
|
|
92
|
+
if ((mode & 0o222) === 0) {
|
|
93
|
+
throw Object.assign(new Error(`EACCES: permission denied, file is read-only: ${filePath}`), {
|
|
94
|
+
code: 'EACCES',
|
|
95
|
+
path: filePath,
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
await access(filePath, constants.W_OK)
|
|
99
|
+
return mode
|
|
100
|
+
}
|
|
101
|
+
|
|
74
102
|
/**
|
|
75
103
|
* Commit a complete file body without exposing a truncated destination.
|
|
76
104
|
*
|
|
@@ -92,12 +120,20 @@ export async function atomicWriteFile(
|
|
|
92
120
|
if (anchor !== undefined) assertNoLinkBelow(anchor, requestedPath)
|
|
93
121
|
const filePath =
|
|
94
122
|
anchor === undefined ? await realpath(requestedPath).catch(() => requestedPath) : requestedPath
|
|
123
|
+
return atomicWriteResolvedFile(filePath, content, hooks)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Commit at a path already resolved by the caller's containment policy. */
|
|
127
|
+
export async function atomicWriteResolvedFile(
|
|
128
|
+
filePath: string,
|
|
129
|
+
content: string | Buffer,
|
|
130
|
+
hooks: AtomicWriteOptions = {},
|
|
131
|
+
): Promise<void> {
|
|
95
132
|
const directory = dirname(filePath)
|
|
133
|
+
const existingMode = await writableMode(filePath)
|
|
96
134
|
await mkdir(directory, { recursive: true })
|
|
97
135
|
|
|
98
|
-
const
|
|
99
|
-
.then((value) => value.mode)
|
|
100
|
-
.catch(() => undefined)
|
|
136
|
+
const preservedMode = hooks.mode === undefined ? existingMode : undefined
|
|
101
137
|
const tempPath = join(
|
|
102
138
|
directory,
|
|
103
139
|
`.${basename(filePath)}.namzu-${process.pid}-${randomUUID()}.tmp`,
|
|
@@ -106,13 +142,19 @@ export async function atomicWriteFile(
|
|
|
106
142
|
let committed = false
|
|
107
143
|
|
|
108
144
|
try {
|
|
109
|
-
handle = await open(tempPath, 'wx', hooks.mode ??
|
|
145
|
+
handle = await open(tempPath, 'wx', preservedMode === undefined ? (hooks.mode ?? 0o666) : 0o600)
|
|
110
146
|
await handle.writeFile(content, 'utf-8')
|
|
147
|
+
// Creation applies the umask. Set the retained bits after writing, which
|
|
148
|
+
// can clear special mode bits on some filesystems.
|
|
149
|
+
if (preservedMode !== undefined) await handle.chmod(preservedMode)
|
|
111
150
|
await handle.sync()
|
|
112
151
|
await handle.close()
|
|
113
152
|
handle = undefined
|
|
114
153
|
await hooks.beforeCommit?.()
|
|
115
|
-
if (
|
|
154
|
+
if (hooks.refuseLinksBelow !== undefined) assertNoLinkBelow(hooks.refuseLinksBelow, filePath)
|
|
155
|
+
if ((await writableMode(filePath)) !== existingMode) {
|
|
156
|
+
throw new Error(`File permissions changed before the write could commit: ${filePath}`)
|
|
157
|
+
}
|
|
116
158
|
await rename(tempPath, filePath)
|
|
117
159
|
committed = true
|
|
118
160
|
await syncDirectory(directory)
|
|
@@ -86,7 +86,12 @@ const findLiteralOp = z.object({
|
|
|
86
86
|
.string()
|
|
87
87
|
.optional()
|
|
88
88
|
.describe('File or directory to search; defaults to the working directory.'),
|
|
89
|
-
case_sensitive: z
|
|
89
|
+
case_sensitive: z
|
|
90
|
+
.boolean()
|
|
91
|
+
.optional()
|
|
92
|
+
.describe(
|
|
93
|
+
'Default true. False matches using Unicode simple case folding without locale tailoring.',
|
|
94
|
+
),
|
|
90
95
|
...paging,
|
|
91
96
|
})
|
|
92
97
|
|
|
@@ -363,25 +363,29 @@ function nearLine(content: string, needle: string): number | undefined {
|
|
|
363
363
|
return undefined
|
|
364
364
|
}
|
|
365
365
|
|
|
366
|
+
function insertionLineEnding(content: string): '\r\n' | '\n' {
|
|
367
|
+
return content.includes('\r\n') ? '\r\n' : '\n'
|
|
368
|
+
}
|
|
369
|
+
|
|
366
370
|
function applyLineInsert(
|
|
367
371
|
content: string,
|
|
368
372
|
input: Extract<NormalizedEditInput, { operation: 'insert' }>,
|
|
369
373
|
): { success: true; content: string; replacements: number } {
|
|
374
|
+
const ending = insertionLineEnding(content)
|
|
370
375
|
const hasTrailingNewline = content.endsWith('\n')
|
|
371
|
-
const lines = content.split(
|
|
376
|
+
const lines = content === '' ? [] : content.split(/\r?\n/)
|
|
372
377
|
if (hasTrailingNewline) lines.pop()
|
|
373
378
|
|
|
374
379
|
const line =
|
|
375
380
|
input.insertLine === 'end'
|
|
376
381
|
? lines.length
|
|
377
382
|
: Math.min(Math.max(input.insertLine, 0), lines.length)
|
|
378
|
-
const inserted = input.newString.
|
|
379
|
-
|
|
380
|
-
: input.newString.split('\n')
|
|
383
|
+
const inserted = input.newString.split(/\r?\n/)
|
|
384
|
+
if (input.newString.endsWith('\n')) inserted.pop()
|
|
381
385
|
lines.splice(line, 0, ...inserted)
|
|
382
386
|
return {
|
|
383
387
|
success: true,
|
|
384
|
-
content: `${lines.join(
|
|
388
|
+
content: `${lines.join(ending)}${hasTrailingNewline ? ending : ''}`,
|
|
385
389
|
replacements: 1,
|
|
386
390
|
}
|
|
387
391
|
}
|
|
@@ -568,12 +572,15 @@ export function replayOperationsWithin(
|
|
|
568
572
|
*/
|
|
569
573
|
function predictOne(content: string, operation: NormalizedEditInput, allowance: number): number {
|
|
570
574
|
if (operation.operation === 'insert') {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
//
|
|
574
|
-
//
|
|
575
|
+
const ending = insertionLineEnding(content)
|
|
576
|
+
const { newString } = operation
|
|
577
|
+
// Existing and inserted breaks are normalized before joining. A nonempty
|
|
578
|
+
// file contributes one join boundary, while the insert's final break is removed.
|
|
575
579
|
return (
|
|
576
|
-
content
|
|
580
|
+
normalizedLineEndingLength(content, ending.length) +
|
|
581
|
+
normalizedLineEndingLength(newString, ending.length) +
|
|
582
|
+
(content === '' ? 0 : ending.length) -
|
|
583
|
+
(newString.endsWith('\n') ? ending.length : 0)
|
|
577
584
|
)
|
|
578
585
|
}
|
|
579
586
|
const { oldString, newString, replace_all } = normalizeLineEndings(content, operation)
|
|
@@ -591,6 +598,14 @@ function predictOne(content: string, operation: NormalizedEditInput, allowance:
|
|
|
591
598
|
return Math.max(content.length + countOccurrences(content, oldString, cap) * growth, 0)
|
|
592
599
|
}
|
|
593
600
|
|
|
601
|
+
function normalizedLineEndingLength(text: string, endingLength: number): number {
|
|
602
|
+
let length = text.length
|
|
603
|
+
for (let index = text.indexOf('\n'); index !== -1; index = text.indexOf('\n', index + 1)) {
|
|
604
|
+
length += endingLength - (text[index - 1] === '\r' ? 2 : 1)
|
|
605
|
+
}
|
|
606
|
+
return length
|
|
607
|
+
}
|
|
608
|
+
|
|
594
609
|
/** Non-overlapping matches, up to `cap`, counted the way `split` counts them. */
|
|
595
610
|
function countOccurrences(content: string, needle: string, cap: number): number {
|
|
596
611
|
// `split('')` yields one part per character; scanning for it would never
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
|
|
3
1
|
import { z } from 'zod'
|
|
4
2
|
import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
5
3
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
6
4
|
import { defineTool } from '../defineTool.js'
|
|
7
|
-
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
5
|
+
import { resolveAllowedFile, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
6
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
8
7
|
import { atomicWriteFile } from './atomic-write-file.js'
|
|
9
8
|
import { clip, describeChange } from './classifier-input.js'
|
|
10
9
|
import {
|
|
@@ -21,9 +20,11 @@ import {
|
|
|
21
20
|
normalizeEditInput,
|
|
22
21
|
} from './edit-apply.js'
|
|
23
22
|
import { withFileMutationLock } from './file-mutation-lock.js'
|
|
23
|
+
import { decodeFileText, unreadableFileText } from './file-text.js'
|
|
24
24
|
import { jsonStringEscapes } from './json-string-hint.js'
|
|
25
25
|
import { lineDiff } from './line-diff.js'
|
|
26
26
|
import { notebookRefusal } from './notebook-guard.js'
|
|
27
|
+
import { readRegularFileBytes } from './read-regular-file.js'
|
|
27
28
|
import { staleBeforeReview } from './stale-before-review.js'
|
|
28
29
|
|
|
29
30
|
type RawEditInput = {
|
|
@@ -550,12 +551,17 @@ async function refuseEditBeforeReview(
|
|
|
550
551
|
// unread-file refusals are given when the call runs there, so a guest that is
|
|
551
552
|
// not admitted yet, or that holds a file the host has never seen, is not
|
|
552
553
|
// judged from a read made before anyone approved the call.
|
|
553
|
-
if (context.sandbox) return staleBeforeReview(context, parsed.data.path, 'edit')
|
|
554
554
|
let key: string
|
|
555
555
|
let content: string
|
|
556
556
|
try {
|
|
557
|
-
|
|
558
|
-
|
|
557
|
+
const allowed = await resolveAllowedFile(context, parsed.data.path)
|
|
558
|
+
if (context.sandbox && allowed === undefined)
|
|
559
|
+
return staleBeforeReview(context, parsed.data.path, 'edit')
|
|
560
|
+
key = allowed ?? (await resolveWithinAnyReal(toolRoots(context), parsed.data.path))
|
|
561
|
+
const bytes = await readRegularFileBytes(key, 'Edit')
|
|
562
|
+
const decoded = decodeFileText(bytes)
|
|
563
|
+
if (decoded === undefined) return { error: unreadableFileText(bytes.length) }
|
|
564
|
+
content = decoded
|
|
559
565
|
} catch {
|
|
560
566
|
return undefined
|
|
561
567
|
}
|
|
@@ -703,25 +709,31 @@ export const EditTool = defineTool({
|
|
|
703
709
|
}
|
|
704
710
|
}
|
|
705
711
|
|
|
712
|
+
const allowed = await resolveAllowedFile(context, parsed.data.path)
|
|
713
|
+
const sandbox = allowed === undefined ? context.sandbox : undefined
|
|
706
714
|
// Host-side containment, on the host branch only. The sandbox has its
|
|
707
715
|
// own root and its own resolver; canonicalizing a sandbox-relative
|
|
708
716
|
// path against the HOST filesystem asks a question about the wrong
|
|
709
717
|
// machine, and answers it with whatever happens to exist there.
|
|
710
|
-
const filePath =
|
|
711
|
-
|
|
712
|
-
: await resolveWithinAnyReal(toolRoots(context), parsed.data.path)
|
|
718
|
+
const filePath =
|
|
719
|
+
allowed ??
|
|
720
|
+
(sandbox ? undefined : await resolveWithinAnyReal(toolRoots(context), parsed.data.path))
|
|
713
721
|
// Read-modify-write is not atomic on its own: two edits to the same
|
|
714
722
|
// path interleave their reads, and the second write lands on content
|
|
715
723
|
// the first had already replaced — so one edit vanishes and the loser
|
|
716
724
|
// reports "old_string not found", blaming the model for a race. The
|
|
717
725
|
// key spans both branches because sandbox and local are distinct
|
|
718
726
|
// files even when the path string matches.
|
|
719
|
-
const lockKey =
|
|
727
|
+
const lockKey = sandbox ? `sandbox:${parsed.data.path}` : `local:${filePath as string}`
|
|
720
728
|
|
|
721
729
|
return withFileMutationLock(lockKey, async () => {
|
|
722
|
-
if (
|
|
723
|
-
|
|
724
|
-
const
|
|
730
|
+
if (sandbox) {
|
|
731
|
+
refuseSandboxHomePath(parsed.data.path)
|
|
732
|
+
const buffer = await sandbox.readFile(parsed.data.path)
|
|
733
|
+
const content = decodeFileText(buffer)
|
|
734
|
+
if (content === undefined) {
|
|
735
|
+
return { success: false as const, output: '', error: unreadableFileText(buffer.length) }
|
|
736
|
+
}
|
|
725
737
|
const admitted = admitAgainstRead(context, parsed.data.path, content, normalized.operations)
|
|
726
738
|
if (!admitted.success) return admitted
|
|
727
739
|
const result = admitted.result
|
|
@@ -729,7 +741,7 @@ export const EditTool = defineTool({
|
|
|
729
741
|
const notebook = notebookRefusal(parsed.data.path, result.content, content)
|
|
730
742
|
if (notebook) return { success: false as const, output: '', error: notebook }
|
|
731
743
|
|
|
732
|
-
await
|
|
744
|
+
await sandbox.writeFile(parsed.data.path, result.content)
|
|
733
745
|
recordEditedContent(context, parsed.data.path, result.content)
|
|
734
746
|
const changed = editFacts(parsed.data.path, result.replacements, content, result.content)
|
|
735
747
|
return {
|
|
@@ -745,7 +757,11 @@ export const EditTool = defineTool({
|
|
|
745
757
|
}
|
|
746
758
|
|
|
747
759
|
const hostPath = filePath as string
|
|
748
|
-
const
|
|
760
|
+
const buffer = await readRegularFileBytes(hostPath, 'Edit')
|
|
761
|
+
const content = decodeFileText(buffer)
|
|
762
|
+
if (content === undefined) {
|
|
763
|
+
return { success: false as const, output: '', error: unreadableFileText(buffer.length) }
|
|
764
|
+
}
|
|
749
765
|
|
|
750
766
|
const admitted = admitAgainstRead(context, hostPath, content, normalized.operations)
|
|
751
767
|
if (!admitted.success) return admitted
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { humanBytes } from './read-render.js'
|
|
2
|
+
|
|
3
|
+
// Keep a leading BOM in the body so read-modify-write tools can round-trip it.
|
|
4
|
+
export function decodeFileText(bytes: Buffer): string | undefined {
|
|
5
|
+
try {
|
|
6
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes)
|
|
7
|
+
} catch {
|
|
8
|
+
return undefined
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function unreadableFileText(bytes: number): string {
|
|
13
|
+
return `This file is not readable as text (${humanBytes(bytes)} of binary or non-UTF-8 data). Inspect it with a shell command such as file or xxd, or use a tool made for its format.`
|
|
14
|
+
}
|
|
@@ -5,7 +5,7 @@ import { walkFilesLocally } from '../../sandbox/file-walk.js'
|
|
|
5
5
|
import type { SandboxWalkFilesOptions, SandboxWalkSummary } from '../../types/sandbox/index.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
7
|
import { resolveWithin, resolveWithinAny, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
8
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
8
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
9
9
|
import {
|
|
10
10
|
SEARCH_BUDGET_MS,
|
|
11
11
|
SEARCH_TOOL_TIMEOUT_MS,
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
ignoredHint,
|
|
14
14
|
isEntryLimit,
|
|
15
15
|
isReadDenied,
|
|
16
|
+
missingSearchRoot,
|
|
16
17
|
openSearchBudget,
|
|
17
18
|
plural,
|
|
18
19
|
quoteControlPath,
|
|
@@ -106,10 +107,14 @@ export const GlobTool = defineTool({
|
|
|
106
107
|
async execute(input, context) {
|
|
107
108
|
context.abortSignal?.throwIfAborted()
|
|
108
109
|
const sandbox = context.sandbox
|
|
110
|
+
if (sandbox) refuseSandboxHomePath(input.path)
|
|
109
111
|
const lexicalRoot = sandbox
|
|
110
112
|
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
111
113
|
: resolveWithinAny(toolRoots(context), input.path)
|
|
112
114
|
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
115
|
+
if (!sandbox && (await missingSearchRoot(root))) {
|
|
116
|
+
return { success: false, output: '', error: `search path does not exist: ${lexicalRoot}` }
|
|
117
|
+
}
|
|
113
118
|
const pattern = relativePattern(input.pattern, lexicalRoot, sandbox !== undefined)
|
|
114
119
|
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
115
120
|
if (sandbox && !walkSandbox) {
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
resolveWithinReal,
|
|
15
15
|
toolRoots,
|
|
16
16
|
} from '../paths.js'
|
|
17
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
17
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
18
18
|
import {
|
|
19
19
|
CAN_REFUSE_LINKS,
|
|
20
20
|
REFUSE_LINK_FLAG,
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
ignoredHint,
|
|
31
31
|
isEntryLimit,
|
|
32
32
|
isReadDenied,
|
|
33
|
+
missingSearchRoot,
|
|
33
34
|
openSearchBudget,
|
|
34
35
|
plural,
|
|
35
36
|
readAhead,
|
|
@@ -388,10 +389,14 @@ export const GrepTool = defineTool({
|
|
|
388
389
|
}
|
|
389
390
|
|
|
390
391
|
const sandbox = context.sandbox
|
|
392
|
+
if (sandbox) refuseSandboxHomePath(input.path)
|
|
391
393
|
const lexicalRoot = sandbox
|
|
392
394
|
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
393
395
|
: resolveWithinAny(toolRoots(context), input.path)
|
|
394
396
|
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
397
|
+
if (!sandbox && (await missingSearchRoot(root))) {
|
|
398
|
+
return { success: false, output: '', error: `search path does not exist: ${lexicalRoot}` }
|
|
399
|
+
}
|
|
395
400
|
const filePattern = relativeInclude(input.include ?? '**/*', lexicalRoot, sandbox !== undefined)
|
|
396
401
|
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
397
402
|
if (sandbox && !walkSandbox) {
|
|
@@ -830,10 +835,7 @@ function multilineHits(content: string, regex: RegExp): [number, number][] {
|
|
|
830
835
|
regex.lastIndex = 0
|
|
831
836
|
for (let m = regex.exec(content); m !== null; m = regex.exec(content)) {
|
|
832
837
|
const text = m[0]
|
|
833
|
-
if (text.length === 0)
|
|
834
|
-
regex.lastIndex++
|
|
835
|
-
continue
|
|
836
|
-
}
|
|
838
|
+
if (text.length === 0) regex.lastIndex++
|
|
837
839
|
// A match that ends on a newline covers the lines before it, not the next one.
|
|
838
840
|
const end = m.index + text.length - (text.endsWith('\n') ? 1 : 0)
|
|
839
841
|
hits.push([lineOf(m.index), lineOf(Math.max(m.index, end))])
|
package/src/tools/builtins/ls.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { Sandbox } from '../../types/sandbox/index.js'
|
|
|
5
5
|
import type { ToolResult } from '../../types/tool/index.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
7
|
import { resolveWithinAny, toolRoots } from '../paths.js'
|
|
8
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
8
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
9
9
|
|
|
10
10
|
const inputSchema = z.object({
|
|
11
11
|
path: z.string().default('.').describe('Directory path to list. Defaults to working directory.'),
|
|
@@ -134,6 +134,7 @@ async function listInSandbox(
|
|
|
134
134
|
input: { path: string; all: boolean; recursive: boolean; max_depth: number },
|
|
135
135
|
sandbox: Sandbox,
|
|
136
136
|
): Promise<ToolResult> {
|
|
137
|
+
refuseSandboxHomePath(input.path)
|
|
137
138
|
const root = resolveWithinPosix(sandbox.rootDir, input.path)
|
|
138
139
|
const entries = await sandbox.listFiles(root)
|
|
139
140
|
|